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 "OwnPtr.h"
39 #include "PassOwnPtr.h"
40
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #if HAVE(SIGNAL_H)
47 #include <signal.h>
48 #endif
49
50 #if USE(CF)
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
55 #include <asl.h>
56 #endif
57 #endif // USE(CF)
58
59 #if COMPILER(MSVC)
60 #include <crtdbg.h>
61 #endif
62
63 #if OS(WIN)
64 #include <windows.h>
65 #define HAVE_ISDEBUGGERPRESENT 1
66 #endif
67
68 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
69 #include <cxxabi.h>
70 #include <dlfcn.h>
71 #include <execinfo.h>
72 #endif
73
74 #if OS(ANDROID)
75 #include <android/log.h>
76 #endif
77
78 extern "C" {
79
80 WTF_ATTRIBUTE_PRINTF(1, 0)
vprintf_stderr_common(const char * format,va_list args)81 static void vprintf_stderr_common(const char* format, va_list args)
82 {
83 #if USE(CF) && !OS(WIN)
84 if (strstr(format, "%@")) {
85 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
86
87 #if COMPILER(CLANG)
88 #pragma clang diagnostic push
89 #pragma clang diagnostic ignored "-Wformat-nonliteral"
90 #endif
91 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
92 #if COMPILER(CLANG)
93 #pragma clang diagnostic pop
94 #endif
95 CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
96 char* buffer = (char*)malloc(length + 1);
97
98 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
99
100 #if USE(APPLE_SYSTEM_LOG)
101 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
102 #endif
103 fputs(buffer, stderr);
104
105 free(buffer);
106 CFRelease(str);
107 CFRelease(cfFormat);
108 return;
109 }
110
111 #if USE(APPLE_SYSTEM_LOG)
112 va_list copyOfArgs;
113 va_copy(copyOfArgs, args);
114 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
115 va_end(copyOfArgs);
116 #endif
117
118 // Fall through to write to stderr in the same manner as other platforms.
119
120 #elif OS(ANDROID)
121 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
122 #elif HAVE(ISDEBUGGERPRESENT)
123 if (IsDebuggerPresent()) {
124 size_t size = 1024;
125
126 do {
127 char* buffer = (char*)malloc(size);
128
129 if (buffer == NULL)
130 break;
131
132 if (_vsnprintf(buffer, size, format, args) != -1) {
133 OutputDebugStringA(buffer);
134 free(buffer);
135 break;
136 }
137
138 free(buffer);
139 size *= 2;
140 } while (size > 1024);
141 }
142 #endif
143 vfprintf(stderr, format, args);
144 }
145
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"
149 #endif
150
vprintf_stderr_with_prefix(const char * prefix,const char * format,va_list args)151 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
152 {
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;
159
160 vprintf_stderr_common(formatWithPrefix.get(), args);
161 }
162
vprintf_stderr_with_trailing_newline(const char * format,va_list args)163 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
164 {
165 size_t formatLength = strlen(format);
166 if (formatLength && format[formatLength - 1] == '\n') {
167 vprintf_stderr_common(format, args);
168 return;
169 }
170
171 OwnPtr<char[]> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
172 memcpy(formatWithNewline.get(), format, formatLength);
173 formatWithNewline[formatLength] = '\n';
174 formatWithNewline[formatLength + 1] = 0;
175
176 vprintf_stderr_common(formatWithNewline.get(), args);
177 }
178
179 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
180 #pragma GCC diagnostic pop
181 #endif
182
183 WTF_ATTRIBUTE_PRINTF(1, 2)
printf_stderr_common(const char * format,...)184 static void printf_stderr_common(const char* format, ...)
185 {
186 va_list args;
187 va_start(args, format);
188 vprintf_stderr_common(format, args);
189 va_end(args);
190 }
191
printCallSite(const char * file,int line,const char * function)192 static void printCallSite(const char* file, int line, const char* function)
193 {
194 #if OS(WIN) && defined(_DEBUG)
195 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
196 #else
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);
201 #endif
202 }
203
WTFReportAssertionFailure(const char * file,int line,const char * function,const char * assertion)204 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
205 {
206 if (assertion)
207 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
208 else
209 printf_stderr_common("SHOULD NEVER BE REACHED\n");
210 printCallSite(file, line, function);
211 }
212
WTFReportAssertionFailureWithMessage(const char * file,int line,const char * function,const char * assertion,const char * format,...)213 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
214 {
215 va_list args;
216 va_start(args, format);
217 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
218 va_end(args);
219 printf_stderr_common("\n%s\n", assertion);
220 printCallSite(file, line, function);
221 }
222
WTFReportArgumentAssertionFailure(const char * file,int line,const char * function,const char * argName,const char * assertion)223 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
224 {
225 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
226 printCallSite(file, line, function);
227 }
228
WTFGetBacktrace(void ** stack,int * size)229 void WTFGetBacktrace(void** stack, int* size)
230 {
231 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
232 *size = backtrace(stack, *size);
233 #elif OS(WIN)
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");
239 if (!kernel32) {
240 *size = 0;
241 return;
242 }
243 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
244 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
245 if (captureStackBackTraceFunc)
246 *size = captureStackBackTraceFunc(0, *size, stack, 0);
247 else
248 *size = 0;
249 #else
250 *size = 0;
251 #endif
252 }
253
WTFReportBacktrace(int framesToShow)254 void WTFReportBacktrace(int framesToShow)
255 {
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;
260
261 WTFGetBacktrace(samples, &frames);
262 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
263 }
264
WTFPrintBacktrace(void ** stack,int size)265 void WTFPrintBacktrace(void** stack, int size)
266 {
267 for (int i = 0; i < size; ++i) {
268 const char* mangledName = 0;
269 char* cxaDemangled = 0;
270 #if OS(MACOSX) || OS(LINUX)
271 Dl_info info;
272 if (dladdr(stack[i], &info) && info.dli_sname)
273 mangledName = info.dli_sname;
274 if (mangledName)
275 cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
276 #endif
277 const int frameNumber = i + 1;
278 if (mangledName || cxaDemangled)
279 printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
280 else
281 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
282 free(cxaDemangled);
283 }
284 }
285
286 static WTFCrashHookFunction globalHook = 0;
287
WTFSetCrashHook(WTFCrashHookFunction function)288 void WTFSetCrashHook(WTFCrashHookFunction function)
289 {
290 globalHook = function;
291 }
292
WTFInvokeCrashHook()293 void WTFInvokeCrashHook()
294 {
295 if (globalHook)
296 globalHook();
297 }
298
299 #if HAVE(SIGNAL_H)
dumpBacktraceSignalHandler(int sig)300 static NO_RETURN void dumpBacktraceSignalHandler(int sig)
301 {
302 WTFReportBacktrace();
303 exit(128 + sig);
304 }
305
installSignalHandlersForFatalErrors(void (* handler)(int))306 static void installSignalHandlersForFatalErrors(void (*handler)(int))
307 {
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.
317 }
318
resetSignalHandlersForFatalErrors()319 static void resetSignalHandlersForFatalErrors()
320 {
321 installSignalHandlersForFatalErrors(SIG_DFL);
322 }
323 #endif
324
WTFInstallReportBacktraceOnCrashHook()325 void WTFInstallReportBacktraceOnCrashHook()
326 {
327 #if HAVE(SIGNAL_H)
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);
332 #endif
333 }
334
WTFReportFatalError(const char * file,int line,const char * function,const char * format,...)335 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
336 {
337 va_list args;
338 va_start(args, format);
339 vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
340 va_end(args);
341 printf_stderr_common("\n");
342 printCallSite(file, line, function);
343 }
344
WTFReportError(const char * file,int line,const char * function,const char * format,...)345 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
346 {
347 va_list args;
348 va_start(args, format);
349 vprintf_stderr_with_prefix("ERROR: ", format, args);
350 va_end(args);
351 printf_stderr_common("\n");
352 printCallSite(file, line, function);
353 }
354
WTFLog(WTFLogChannel * channel,const char * format,...)355 void WTFLog(WTFLogChannel* channel, const char* format, ...)
356 {
357 if (channel->state != WTFLogChannelOn)
358 return;
359
360 va_list args;
361 va_start(args, format);
362 vprintf_stderr_with_trailing_newline(format, args);
363 va_end(args);
364 }
365
WTFLogVerbose(const char * file,int line,const char * function,WTFLogChannel * channel,const char * format,...)366 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
367 {
368 if (channel->state != WTFLogChannelOn)
369 return;
370
371 va_list args;
372 va_start(args, format);
373 vprintf_stderr_with_trailing_newline(format, args);
374 va_end(args);
375
376 printCallSite(file, line, function);
377 }
378
WTFLogAlways(const char * format,...)379 void WTFLogAlways(const char* format, ...)
380 {
381 va_list args;
382 va_start(args, format);
383 vprintf_stderr_with_trailing_newline(format, args);
384 va_end(args);
385 }
386
387 } // extern "C"
388