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 WTF_ATTRIBUTE_PRINTF(1, 0) 79 static void vprintf_stderr_common(const char* format, va_list args) 80 { 81 #if USE(CF) && !OS(WIN) 82 if (strstr(format, "%@")) { 83 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8); 84 85 #if COMPILER(CLANG) 86 #pragma clang diagnostic push 87 #pragma clang diagnostic ignored "-Wformat-nonliteral" 88 #endif 89 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args); 90 #if COMPILER(CLANG) 91 #pragma clang diagnostic pop 92 #endif 93 CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8); 94 char* buffer = (char*)malloc(length + 1); 95 96 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8); 97 98 #if USE(APPLE_SYSTEM_LOG) 99 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer); 100 #endif 101 fputs(buffer, stderr); 102 103 free(buffer); 104 CFRelease(str); 105 CFRelease(cfFormat); 106 return; 107 } 108 109 #if USE(APPLE_SYSTEM_LOG) 110 va_list copyOfArgs; 111 va_copy(copyOfArgs, args); 112 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs); 113 va_end(copyOfArgs); 114 #endif 115 116 // Fall through to write to stderr in the same manner as other platforms. 117 118 #elif OS(ANDROID) 119 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args); 120 #elif HAVE(ISDEBUGGERPRESENT) 121 if (IsDebuggerPresent()) { 122 size_t size = 1024; 123 124 do { 125 char* buffer = (char*)malloc(size); 126 127 if (buffer == NULL) 128 break; 129 130 if (_vsnprintf(buffer, size, format, args) != -1) { 131 OutputDebugStringA(buffer); 132 free(buffer); 133 break; 134 } 135 136 free(buffer); 137 size *= 2; 138 } while (size > 1024); 139 } 140 #endif 141 vfprintf(stderr, format, args); 142 } 143 144 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0)) 145 #pragma GCC diagnostic push 146 #pragma GCC diagnostic ignored "-Wformat-nonliteral" 147 #endif 148 149 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args) 150 { 151 size_t prefixLength = strlen(prefix); 152 size_t formatLength = strlen(format); 153 OwnPtr<char[]> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]); 154 memcpy(formatWithPrefix.get(), prefix, prefixLength); 155 memcpy(formatWithPrefix.get() + prefixLength, format, formatLength); 156 formatWithPrefix[prefixLength + formatLength] = 0; 157 158 vprintf_stderr_common(formatWithPrefix.get(), args); 159 } 160 161 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args) 162 { 163 size_t formatLength = strlen(format); 164 if (formatLength && format[formatLength - 1] == '\n') { 165 vprintf_stderr_common(format, args); 166 return; 167 } 168 169 OwnPtr<char[]> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]); 170 memcpy(formatWithNewline.get(), format, formatLength); 171 formatWithNewline[formatLength] = '\n'; 172 formatWithNewline[formatLength + 1] = 0; 173 174 vprintf_stderr_common(formatWithNewline.get(), args); 175 } 176 177 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0)) 178 #pragma GCC diagnostic pop 179 #endif 180 181 WTF_ATTRIBUTE_PRINTF(1, 2) 182 static void printf_stderr_common(const char* format, ...) 183 { 184 va_list args; 185 va_start(args, format); 186 vprintf_stderr_common(format, args); 187 va_end(args); 188 } 189 190 static void printCallSite(const char* file, int line, const char* function) 191 { 192 #if OS(WIN) && defined(_DEBUG) 193 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function); 194 #else 195 // By using this format, which matches the format used by MSVC for compiler errors, developers 196 // using Visual Studio can double-click the file/line number in the Output Window to have the 197 // editor navigate to that line of code. It seems fine for other developers, too. 198 printf_stderr_common("%s(%d) : %s\n", file, line, function); 199 #endif 200 } 201 202 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion) 203 { 204 if (assertion) 205 printf_stderr_common("ASSERTION FAILED: %s\n", assertion); 206 else 207 printf_stderr_common("SHOULD NEVER BE REACHED\n"); 208 printCallSite(file, line, function); 209 } 210 211 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) 212 { 213 va_list args; 214 va_start(args, format); 215 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args); 216 va_end(args); 217 printf_stderr_common("\n%s\n", assertion); 218 printCallSite(file, line, function); 219 } 220 221 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion) 222 { 223 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion); 224 printCallSite(file, line, function); 225 } 226 227 void WTFGetBacktrace(void** stack, int* size) 228 { 229 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__)) 230 *size = backtrace(stack, *size); 231 #elif OS(WIN) 232 // The CaptureStackBackTrace function is available in XP, but it is not defined 233 // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function 234 // through GetProcAddress. 235 typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD); 236 HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll"); 237 if (!kernel32) { 238 *size = 0; 239 return; 240 } 241 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>( 242 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace")); 243 if (captureStackBackTraceFunc) 244 *size = captureStackBackTraceFunc(0, *size, stack, 0); 245 else 246 *size = 0; 247 #else 248 *size = 0; 249 #endif 250 } 251 252 void WTFReportBacktrace(int framesToShow) 253 { 254 static const int framesToSkip = 2; 255 // Use alloca to allocate on the stack since this function is used in OOM situations. 256 void** samples = static_cast<void**>(alloca((framesToShow + framesToSkip) * sizeof(void *))); 257 int frames = framesToShow + framesToSkip; 258 259 WTFGetBacktrace(samples, &frames); 260 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip); 261 } 262 263 FrameToNameScope::FrameToNameScope(void* addr) 264 : m_name(0) 265 , m_cxaDemangled(0) 266 { 267 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__)) 268 Dl_info info; 269 if (!dladdr(addr, &info) || !info.dli_sname) 270 return; 271 const char* mangledName = info.dli_sname; 272 if ((m_cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0))) 273 m_name = m_cxaDemangled; 274 else 275 m_name = mangledName; 276 #else 277 (void)addr; 278 #endif 279 } 280 281 FrameToNameScope::~FrameToNameScope() 282 { 283 free(m_cxaDemangled); 284 } 285 286 void WTFPrintBacktrace(void** stack, int size) 287 { 288 for (int i = 0; i < size; ++i) { 289 FrameToNameScope frameToName(stack[i]); 290 const int frameNumber = i + 1; 291 if (frameToName.nullableName()) 292 printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], frameToName.nullableName()); 293 else 294 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]); 295 } 296 } 297 298 static WTFCrashHookFunction globalHook = 0; 299 300 void WTFSetCrashHook(WTFCrashHookFunction function) 301 { 302 globalHook = function; 303 } 304 305 void WTFInvokeCrashHook() 306 { 307 if (globalHook) 308 globalHook(); 309 } 310 311 #if HAVE(SIGNAL_H) 312 static NO_RETURN void dumpBacktraceSignalHandler(int sig) 313 { 314 WTFReportBacktrace(); 315 exit(128 + sig); 316 } 317 318 static void installSignalHandlersForFatalErrors(void (*handler)(int)) 319 { 320 signal(SIGILL, handler); // 4: illegal instruction (not reset when caught). 321 signal(SIGTRAP, handler); // 5: trace trap (not reset when caught). 322 signal(SIGFPE, handler); // 8: floating point exception. 323 signal(SIGBUS, handler); // 10: bus error. 324 signal(SIGSEGV, handler); // 11: segmentation violation. 325 signal(SIGSYS, handler); // 12: bad argument to system call. 326 signal(SIGPIPE, handler); // 13: write on a pipe with no reader. 327 signal(SIGXCPU, handler); // 24: exceeded CPU time limit. 328 signal(SIGXFSZ, handler); // 25: exceeded file size limit. 329 } 330 331 static void resetSignalHandlersForFatalErrors() 332 { 333 installSignalHandlersForFatalErrors(SIG_DFL); 334 } 335 #endif 336 337 void WTFInstallReportBacktraceOnCrashHook() 338 { 339 #if HAVE(SIGNAL_H) 340 // Needed otherwise we are going to dump the stack trace twice 341 // in case we hit an assertion. 342 WTFSetCrashHook(&resetSignalHandlersForFatalErrors); 343 installSignalHandlersForFatalErrors(&dumpBacktraceSignalHandler); 344 #endif 345 } 346 347 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) 348 { 349 va_list args; 350 va_start(args, format); 351 vprintf_stderr_with_prefix("FATAL ERROR: ", format, args); 352 va_end(args); 353 printf_stderr_common("\n"); 354 printCallSite(file, line, function); 355 } 356 357 void WTFReportError(const char* file, int line, const char* function, const char* format, ...) 358 { 359 va_list args; 360 va_start(args, format); 361 vprintf_stderr_with_prefix("ERROR: ", format, args); 362 va_end(args); 363 printf_stderr_common("\n"); 364 printCallSite(file, line, function); 365 } 366 367 void WTFLog(WTFLogChannel* channel, const char* format, ...) 368 { 369 if (channel->state != WTFLogChannelOn) 370 return; 371 372 va_list args; 373 va_start(args, format); 374 vprintf_stderr_with_trailing_newline(format, args); 375 va_end(args); 376 } 377 378 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...) 379 { 380 if (channel->state != WTFLogChannelOn) 381 return; 382 383 va_list args; 384 va_start(args, format); 385 vprintf_stderr_with_trailing_newline(format, args); 386 va_end(args); 387 388 printCallSite(file, line, function); 389 } 390 391 void WTFLogAlways(const char* format, ...) 392 { 393 va_list args; 394 va_start(args, format); 395 vprintf_stderr_with_trailing_newline(format, args); 396 va_end(args); 397 } 398 399