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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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"
35 #include "Assertions.h"
39 #include "PassOwnPtr.h"
51 #include <AvailabilityMacros.h>
52 #include <CoreFoundation/CFString.h>
53 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
54 #define WTF_USE_APPLE_SYSTEM_LOG 1
65 #define HAVE_ISDEBUGGERPRESENT 1
68 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
75 #include <android/log.h>
80 WTF_ATTRIBUTE_PRINTF(1, 0)
81 static void vprintf_stderr_common(const char* format, va_list args)
83 #if USE(CF) && !OS(WIN)
84 if (strstr(format, "%@")) {
85 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
88 #pragma clang diagnostic push
89 #pragma clang diagnostic ignored "-Wformat-nonliteral"
91 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
93 #pragma clang diagnostic pop
95 CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
96 char* buffer = (char*)malloc(length + 1);
98 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
100 #if USE(APPLE_SYSTEM_LOG)
101 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
103 fputs(buffer, stderr);
111 #if USE(APPLE_SYSTEM_LOG)
113 va_copy(copyOfArgs, args);
114 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
118 // Fall through to write to stderr in the same manner as other platforms.
121 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
122 #elif HAVE(ISDEBUGGERPRESENT)
123 if (IsDebuggerPresent()) {
127 char* buffer = (char*)malloc(size);
132 if (_vsnprintf(buffer, size, format, args) != -1) {
133 OutputDebugStringA(buffer);
140 } while (size > 1024);
143 vfprintf(stderr, format, args);
146 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
147 #pragma GCC diagnostic push
148 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
151 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
153 size_t prefixLength = strlen(prefix);
154 size_t formatLength = strlen(format);
155 OwnPtr<char[]> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]);
156 memcpy(formatWithPrefix.get(), prefix, prefixLength);
157 memcpy(formatWithPrefix.get() + prefixLength, format, formatLength);
158 formatWithPrefix[prefixLength + formatLength] = 0;
160 vprintf_stderr_common(formatWithPrefix.get(), args);
163 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
165 size_t formatLength = strlen(format);
166 if (formatLength && format[formatLength - 1] == '\n') {
167 vprintf_stderr_common(format, args);
171 OwnPtr<char[]> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
172 memcpy(formatWithNewline.get(), format, formatLength);
173 formatWithNewline[formatLength] = '\n';
174 formatWithNewline[formatLength + 1] = 0;
176 vprintf_stderr_common(formatWithNewline.get(), args);
179 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
180 #pragma GCC diagnostic pop
183 WTF_ATTRIBUTE_PRINTF(1, 2)
184 static void printf_stderr_common(const char* format, ...)
187 va_start(args, format);
188 vprintf_stderr_common(format, args);
192 static void printCallSite(const char* file, int line, const char* function)
194 #if OS(WIN) && defined(_DEBUG)
195 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
197 // By using this format, which matches the format used by MSVC for compiler errors, developers
198 // using Visual Studio can double-click the file/line number in the Output Window to have the
199 // editor navigate to that line of code. It seems fine for other developers, too.
200 printf_stderr_common("%s(%d) : %s\n", file, line, function);
204 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
207 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
209 printf_stderr_common("SHOULD NEVER BE REACHED\n");
210 printCallSite(file, line, function);
213 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
216 va_start(args, format);
217 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
219 printf_stderr_common("\n%s\n", assertion);
220 printCallSite(file, line, function);
223 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
225 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
226 printCallSite(file, line, function);
229 void WTFGetBacktrace(void** stack, int* size)
231 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
232 *size = backtrace(stack, *size);
234 // The CaptureStackBackTrace function is available in XP, but it is not defined
235 // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
236 // through GetProcAddress.
237 typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
238 HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
243 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
244 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
245 if (captureStackBackTraceFunc)
246 *size = captureStackBackTraceFunc(0, *size, stack, 0);
254 void WTFReportBacktrace(int framesToShow)
256 static const int framesToSkip = 2;
257 // Use alloca to allocate on the stack since this function is used in OOM situations.
258 void** samples = static_cast<void**>(alloca((framesToShow + framesToSkip) * sizeof(void *)));
259 int frames = framesToShow + framesToSkip;
261 WTFGetBacktrace(samples, &frames);
262 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
265 void WTFPrintBacktrace(void** stack, int size)
267 for (int i = 0; i < size; ++i) {
268 const char* mangledName = 0;
269 char* cxaDemangled = 0;
270 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
272 if (dladdr(stack[i], &info) && info.dli_sname)
273 mangledName = info.dli_sname;
275 cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
277 const int frameNumber = i + 1;
278 if (mangledName || cxaDemangled)
279 printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
281 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
286 static WTFCrashHookFunction globalHook = 0;
288 void WTFSetCrashHook(WTFCrashHookFunction function)
290 globalHook = function;
293 void WTFInvokeCrashHook()
300 static NO_RETURN void dumpBacktraceSignalHandler(int sig)
302 WTFReportBacktrace();
306 static void installSignalHandlersForFatalErrors(void (*handler)(int))
308 signal(SIGILL, handler); // 4: illegal instruction (not reset when caught).
309 signal(SIGTRAP, handler); // 5: trace trap (not reset when caught).
310 signal(SIGFPE, handler); // 8: floating point exception.
311 signal(SIGBUS, handler); // 10: bus error.
312 signal(SIGSEGV, handler); // 11: segmentation violation.
313 signal(SIGSYS, handler); // 12: bad argument to system call.
314 signal(SIGPIPE, handler); // 13: write on a pipe with no reader.
315 signal(SIGXCPU, handler); // 24: exceeded CPU time limit.
316 signal(SIGXFSZ, handler); // 25: exceeded file size limit.
319 static void resetSignalHandlersForFatalErrors()
321 installSignalHandlersForFatalErrors(SIG_DFL);
325 void WTFInstallReportBacktraceOnCrashHook()
328 // Needed otherwise we are going to dump the stack trace twice
329 // in case we hit an assertion.
330 WTFSetCrashHook(&resetSignalHandlersForFatalErrors);
331 installSignalHandlersForFatalErrors(&dumpBacktraceSignalHandler);
335 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
338 va_start(args, format);
339 vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
341 printf_stderr_common("\n");
342 printCallSite(file, line, function);
345 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
348 va_start(args, format);
349 vprintf_stderr_with_prefix("ERROR: ", format, args);
351 printf_stderr_common("\n");
352 printCallSite(file, line, function);
355 void WTFLog(WTFLogChannel* channel, const char* format, ...)
357 if (channel->state != WTFLogChannelOn)
361 va_start(args, format);
362 vprintf_stderr_with_trailing_newline(format, args);
366 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
368 if (channel->state != WTFLogChannelOn)
372 va_start(args, format);
373 vprintf_stderr_with_trailing_newline(format, args);
376 printCallSite(file, line, function);
379 void WTFLogAlways(const char* format, ...)
382 va_start(args, format);
383 vprintf_stderr_with_trailing_newline(format, args);