~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WTF/wtf/Assertions.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
 
3
 * Copyright (C) 2007-2009 Torch Mobile, Inc.
 
4
 * Copyright (C) 2011 University of Szeged. All rights reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions
 
8
 * are met:
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
16
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
18
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
19
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
22
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
23
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
25
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
// The vprintf_stderr_common function triggers this error in the Mac build.
 
29
// Feel free to remove this pragma if this file builds on Mac.
 
30
// According to http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
 
31
// we need to place this directive before any data or functions are defined.
 
32
#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
 
33
 
 
34
#include "config.h"
 
35
#include "Assertions.h"
 
36
 
 
37
#include "Compiler.h"
 
38
#include "OwnArrayPtr.h"
 
39
 
 
40
#include <stdio.h>
 
41
#include <stdarg.h>
 
42
#include <string.h>
 
43
 
 
44
#if HAVE(SIGNAL_H)
 
45
#include <signal.h>
 
46
#endif
 
47
 
 
48
#if PLATFORM(MAC)
 
49
#include <CoreFoundation/CFString.h>
 
50
#include <asl.h>
 
51
#endif
 
52
 
 
53
#if COMPILER(MSVC) && !OS(WINCE)
 
54
#include <crtdbg.h>
 
55
#endif
 
56
 
 
57
#if OS(WINDOWS)
 
58
#include <windows.h>
 
59
#endif
 
60
 
 
61
#if (OS(DARWIN) || (OS(LINUX) && !defined(__UCLIBC__))) && !OS(ANDROID)
 
62
#include <cxxabi.h>
 
63
#include <dlfcn.h>
 
64
#include <execinfo.h>
 
65
#endif
 
66
 
 
67
#if OS(ANDROID)
 
68
#include "android/log.h"
 
69
#endif
 
70
 
 
71
#if PLATFORM(BLACKBERRY)
 
72
#include <BlackBerryPlatformLog.h>
 
73
#endif
 
74
 
 
75
extern "C" {
 
76
 
 
77
WTF_ATTRIBUTE_PRINTF(1, 0)
 
78
static void vprintf_stderr_common(const char* format, va_list args)
 
79
{
 
80
#if PLATFORM(MAC)
 
81
    if (strstr(format, "%@")) {
 
82
        CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
 
83
 
 
84
#if COMPILER(CLANG)
 
85
#pragma clang diagnostic push
 
86
#pragma clang diagnostic ignored "-Wformat-nonliteral"
 
87
#endif
 
88
        CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
 
89
#if COMPILER(CLANG)
 
90
#pragma clang diagnostic pop
 
91
#endif
 
92
        int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
 
93
        char* buffer = (char*)malloc(length + 1);
 
94
 
 
95
        CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
 
96
 
 
97
#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
 
98
        asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
 
99
#endif
 
100
        fputs(buffer, stderr);
 
101
 
 
102
        free(buffer);
 
103
        CFRelease(str);
 
104
        CFRelease(cfFormat);
 
105
        return;
 
106
    }
 
107
 
 
108
#if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
 
109
    va_list copyOfArgs;
 
110
    va_copy(copyOfArgs, args);
 
111
    asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
 
112
    va_end(copyOfArgs);
 
113
#endif
 
114
 
 
115
    // Fall through to write to stderr in the same manner as other platforms.
 
116
 
 
117
#elif PLATFORM(BLACKBERRY)
 
118
    BBLOGV(BlackBerry::Platform::LogLevelCritical, format, args);
 
119
#elif OS(ANDROID)
 
120
    __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
 
121
#elif HAVE(ISDEBUGGERPRESENT)
 
122
    if (IsDebuggerPresent()) {
 
123
        size_t size = 1024;
 
124
 
 
125
        do {
 
126
            char* buffer = (char*)malloc(size);
 
127
 
 
128
            if (buffer == NULL)
 
129
                break;
 
130
 
 
131
            if (_vsnprintf(buffer, size, format, args) != -1) {
 
132
#if OS(WINCE)
 
133
                // WinCE only supports wide chars
 
134
                wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t));
 
135
                if (wideBuffer == NULL)
 
136
                    break;
 
137
                for (unsigned int i = 0; i < size; ++i) {
 
138
                    if (!(wideBuffer[i] = buffer[i]))
 
139
                        break;
 
140
                }
 
141
                OutputDebugStringW(wideBuffer);
 
142
                free(wideBuffer);
 
143
#else
 
144
                OutputDebugStringA(buffer);
 
145
#endif
 
146
                free(buffer);
 
147
                break;
 
148
            }
 
149
 
 
150
            free(buffer);
 
151
            size *= 2;
 
152
        } while (size > 1024);
 
153
    }
 
154
#endif
 
155
#if !PLATFORM(BLACKBERRY)
 
156
    vfprintf(stderr, format, args);
 
157
#endif
 
158
}
 
159
 
 
160
#if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
 
161
#pragma GCC diagnostic push
 
162
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
163
#endif
 
164
 
 
165
static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
 
166
{
 
167
    size_t prefixLength = strlen(prefix);
 
168
    size_t formatLength = strlen(format);
 
169
    OwnArrayPtr<char> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]);
 
170
    memcpy(formatWithPrefix.get(), prefix, prefixLength);
 
171
    memcpy(formatWithPrefix.get() + prefixLength, format, formatLength);
 
172
    formatWithPrefix[prefixLength + formatLength] = 0;
 
173
 
 
174
    vprintf_stderr_common(formatWithPrefix.get(), args);
 
175
}
 
176
 
 
177
static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
 
178
{
 
179
    size_t formatLength = strlen(format);
 
180
    if (formatLength && format[formatLength - 1] == '\n') {
 
181
        vprintf_stderr_common(format, args);
 
182
        return;
 
183
    }
 
184
 
 
185
    OwnArrayPtr<char> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
 
186
    memcpy(formatWithNewline.get(), format, formatLength);
 
187
    formatWithNewline[formatLength] = '\n';
 
188
    formatWithNewline[formatLength + 1] = 0;
 
189
 
 
190
    vprintf_stderr_common(formatWithNewline.get(), args);
 
191
}
 
192
 
 
193
#if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
 
194
#pragma GCC diagnostic pop
 
195
#endif
 
196
 
 
197
WTF_ATTRIBUTE_PRINTF(1, 2)
 
198
static void printf_stderr_common(const char* format, ...)
 
199
{
 
200
    va_list args;
 
201
    va_start(args, format);
 
202
    vprintf_stderr_common(format, args);
 
203
    va_end(args);
 
204
}
 
205
 
 
206
static void printCallSite(const char* file, int line, const char* function)
 
207
{
 
208
#if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
 
209
    _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
 
210
#else
 
211
    // By using this format, which matches the format used by MSVC for compiler errors, developers
 
212
    // using Visual Studio can double-click the file/line number in the Output Window to have the
 
213
    // editor navigate to that line of code. It seems fine for other developers, too.
 
214
    printf_stderr_common("%s(%d) : %s\n", file, line, function);
 
215
#endif
 
216
}
 
217
 
 
218
void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
 
219
{
 
220
    if (assertion)
 
221
        printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
 
222
    else
 
223
        printf_stderr_common("SHOULD NEVER BE REACHED\n");
 
224
    printCallSite(file, line, function);
 
225
}
 
226
 
 
227
void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
 
228
{
 
229
    va_list args;
 
230
    va_start(args, format);
 
231
    vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
 
232
    va_end(args);
 
233
    printf_stderr_common("\n%s\n", assertion);
 
234
    printCallSite(file, line, function);
 
235
}
 
236
 
 
237
void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
 
238
{
 
239
    printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
 
240
    printCallSite(file, line, function);
 
241
}
 
242
 
 
243
void WTFGetBacktrace(void** stack, int* size)
 
244
{
 
245
#if (OS(DARWIN) || (OS(LINUX) && !defined(__UCLIBC__))) && !OS(ANDROID)
 
246
    *size = backtrace(stack, *size);
 
247
#elif OS(WINDOWS) && !OS(WINCE)
 
248
    // The CaptureStackBackTrace function is available in XP, but it is not defined
 
249
    // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
 
250
    // through GetProcAddress.
 
251
    typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
 
252
    HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
 
253
    if (!kernel32) {
 
254
        *size = 0;
 
255
        return;
 
256
    }
 
257
    RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
 
258
        ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
 
259
    if (captureStackBackTraceFunc)
 
260
        *size = captureStackBackTraceFunc(0, *size, stack, 0);
 
261
    else
 
262
        *size = 0;
 
263
#else
 
264
    *size = 0;
 
265
#endif
 
266
}
 
267
 
 
268
void WTFReportBacktrace()
 
269
{
 
270
    static const int framesToShow = 31;
 
271
    static const int framesToSkip = 2;
 
272
    void* samples[framesToShow + framesToSkip];
 
273
    int frames = framesToShow + framesToSkip;
 
274
 
 
275
    WTFGetBacktrace(samples, &frames);
 
276
    WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
 
277
}
 
278
 
 
279
#if OS(DARWIN) || OS(LINUX)
 
280
#  if PLATFORM(QT) || PLATFORM(GTK)
 
281
#    if defined(__GLIBC__) && !defined(__UCLIBC__)
 
282
#      define WTF_USE_BACKTRACE_SYMBOLS 1
 
283
#    endif
 
284
#  elif !OS(ANDROID)
 
285
#    define WTF_USE_DLADDR 1
 
286
#  endif
 
287
#endif
 
288
 
 
289
void WTFPrintBacktrace(void** stack, int size)
 
290
{
 
291
#if USE(BACKTRACE_SYMBOLS)
 
292
    char** symbols = backtrace_symbols(stack, size);
 
293
    if (!symbols)
 
294
        return;
 
295
#endif
 
296
 
 
297
    for (int i = 0; i < size; ++i) {
 
298
        const char* mangledName = 0;
 
299
        char* cxaDemangled = 0;
 
300
#if USE(BACKTRACE_SYMBOLS)
 
301
        mangledName = symbols[i];
 
302
#elif USE(DLADDR)
 
303
        Dl_info info;
 
304
        if (dladdr(stack[i], &info) && info.dli_sname)
 
305
            mangledName = info.dli_sname;
 
306
        if (mangledName)
 
307
            cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
 
308
#endif
 
309
        const int frameNumber = i + 1;
 
310
        if (mangledName || cxaDemangled)
 
311
            printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
 
312
        else
 
313
            printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
 
314
        free(cxaDemangled);
 
315
    }
 
316
 
 
317
#if USE(BACKTRACE_SYMBOLS)
 
318
    free(symbols);
 
319
#endif
 
320
}
 
321
 
 
322
#undef WTF_USE_BACKTRACE_SYMBOLS
 
323
#undef WTF_USE_DLADDR
 
324
 
 
325
static WTFCrashHookFunction globalHook = 0;
 
326
 
 
327
void WTFSetCrashHook(WTFCrashHookFunction function)
 
328
{
 
329
    globalHook = function;
 
330
}
 
331
 
 
332
void WTFInvokeCrashHook()
 
333
{
 
334
    if (globalHook)
 
335
        globalHook();
 
336
}
 
337
 
 
338
#if HAVE(SIGNAL_H)
 
339
static NO_RETURN void dumpBacktraceSignalHandler(int sig)
 
340
{
 
341
    WTFReportBacktrace();
 
342
    exit(128 + sig);
 
343
}
 
344
 
 
345
static void installSignalHandlersForFatalErrors(void (*handler)(int))
 
346
{
 
347
    signal(SIGILL, handler); //    4: illegal instruction (not reset when caught).
 
348
    signal(SIGTRAP, handler); //   5: trace trap (not reset when caught).
 
349
    signal(SIGFPE, handler); //    8: floating point exception.
 
350
    signal(SIGBUS, handler); //   10: bus error.
 
351
    signal(SIGSEGV, handler); //  11: segmentation violation.
 
352
    signal(SIGSYS, handler); //   12: bad argument to system call.
 
353
    signal(SIGPIPE, handler); //  13: write on a pipe with no reader.
 
354
    signal(SIGXCPU, handler); //  24: exceeded CPU time limit.
 
355
    signal(SIGXFSZ, handler); //  25: exceeded file size limit.
 
356
}
 
357
 
 
358
static void resetSignalHandlersForFatalErrors()
 
359
{
 
360
    installSignalHandlersForFatalErrors(SIG_DFL);
 
361
}
 
362
#endif
 
363
 
 
364
void WTFInstallReportBacktraceOnCrashHook()
 
365
{
 
366
#if HAVE(SIGNAL_H)
 
367
    // Needed otherwise we are going to dump the stack trace twice
 
368
    // in case we hit an assertion.
 
369
    WTFSetCrashHook(&resetSignalHandlersForFatalErrors);
 
370
    installSignalHandlersForFatalErrors(&dumpBacktraceSignalHandler);
 
371
#endif
 
372
}
 
373
 
 
374
void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
 
375
{
 
376
    va_list args;
 
377
    va_start(args, format);
 
378
    vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
 
379
    va_end(args);
 
380
    printf_stderr_common("\n");
 
381
    printCallSite(file, line, function);
 
382
}
 
383
 
 
384
void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
 
385
{
 
386
    va_list args;
 
387
    va_start(args, format);
 
388
    vprintf_stderr_with_prefix("ERROR: ", format, args);
 
389
    va_end(args);
 
390
    printf_stderr_common("\n");
 
391
    printCallSite(file, line, function);
 
392
}
 
393
 
 
394
void WTFLog(WTFLogChannel* channel, const char* format, ...)
 
395
{
 
396
    if (channel->state != WTFLogChannelOn)
 
397
        return;
 
398
 
 
399
    va_list args;
 
400
    va_start(args, format);
 
401
    vprintf_stderr_with_trailing_newline(format, args);
 
402
    va_end(args);
 
403
}
 
404
 
 
405
void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
 
406
{
 
407
    if (channel->state != WTFLogChannelOn)
 
408
        return;
 
409
 
 
410
    va_list args;
 
411
    va_start(args, format);
 
412
    vprintf_stderr_with_trailing_newline(format, args);
 
413
    va_end(args);
 
414
 
 
415
    printCallSite(file, line, function);
 
416
}
 
417
 
 
418
void WTFLogAlways(const char* format, ...)
 
419
{
 
420
    va_list args;
 
421
    va_start(args, format);
 
422
    vprintf_stderr_with_trailing_newline(format, args);
 
423
    va_end(args);
 
424
}
 
425
 
 
426
} // extern "C"