2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2009 Torch Mobile, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 // The vprintf_stderr_common function triggers this error in the Mac build.
28 // Feel free to remove this pragma if this file builds on Mac.
29 // According to http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
30 // we need to place this directive before any data or functions are defined.
31 #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
34 #include "Assertions.h"
36 #include "OwnArrayPtr.h"
43 #include <CoreFoundation/CFString.h>
47 #if COMPILER(MSVC) && !OS(WINCE)
55 #if (OS(DARWIN) || OS(LINUX)) && !OS(ANDROID)
61 #if ENABLE(TIZEN_DLOG_SUPPORT)
62 #undef LOG // Disable internal WebKit log to use LOG in dlog.h
63 #include <dlog/dlog.h>
64 #define DLOG_WEBKIT_TAG "WEBKIT"
65 #define DLOG_MAX_LENGTH 1024
69 #include "android/log.h"
72 #if PLATFORM(BLACKBERRY)
73 #include <BlackBerryPlatformLog.h>
78 WTF_ATTRIBUTE_PRINTF(1, 0)
79 static void vprintf_stderr_common(const char* format, va_list args)
82 if (strstr(format, "%@")) {
83 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
86 #pragma clang diagnostic push
87 #pragma clang diagnostic ignored "-Wformat-nonliteral"
89 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
91 #pragma clang diagnostic pop
93 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
94 char* buffer = (char*)malloc(length + 1);
96 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
98 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
99 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
101 fputs(buffer, stderr);
109 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
111 va_copy(copyOfArgs, args);
112 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
116 // Fall through to write to stderr in the same manner as other platforms.
118 #elif PLATFORM(BLACKBERRY)
119 BlackBerry::Platform::logStreamV(format, args);
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) {
134 // WinCE only supports wide chars
135 wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t));
136 if (wideBuffer == NULL)
138 for (unsigned int i = 0; i < size; ++i) {
139 if (!(wideBuffer[i] = buffer[i]))
142 OutputDebugStringW(wideBuffer);
145 OutputDebugStringA(buffer);
153 } while (size > 1024);
156 #if !PLATFORM(BLACKBERRY)
157 vfprintf(stderr, format, args);
161 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
162 #pragma GCC diagnostic push
163 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
166 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
168 size_t prefixLength = strlen(prefix);
169 size_t formatLength = strlen(format);
170 OwnArrayPtr<char> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]);
171 memcpy(formatWithPrefix.get(), prefix, prefixLength);
172 memcpy(formatWithPrefix.get() + prefixLength, format, formatLength);
173 formatWithPrefix[prefixLength + formatLength] = 0;
175 vprintf_stderr_common(formatWithPrefix.get(), args);
178 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
180 size_t formatLength = strlen(format);
181 if (formatLength && format[formatLength - 1] == '\n') {
182 vprintf_stderr_common(format, args);
186 OwnArrayPtr<char> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
187 memcpy(formatWithNewline.get(), format, formatLength);
188 formatWithNewline[formatLength] = '\n';
189 formatWithNewline[formatLength + 1] = 0;
191 vprintf_stderr_common(formatWithNewline.get(), args);
194 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
195 #pragma GCC diagnostic pop
198 WTF_ATTRIBUTE_PRINTF(1, 2)
199 static void printf_stderr_common(const char* format, ...)
202 va_start(args, format);
203 vprintf_stderr_common(format, args);
207 static void printCallSite(const char* file, int line, const char* function)
209 #if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
210 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
212 // By using this format, which matches the format used by MSVC for compiler errors, developers
213 // using Visual Studio can double-click the file/line number in the Output Window to have the
214 // editor navigate to that line of code. It seems fine for other developers, too.
215 printf_stderr_common("%s(%d) : %s\n", file, line, function);
219 #if PLATFORM(BLACKBERRY)
220 struct WTFLogLocker {
221 WTFLogLocker(BlackBerry::Platform::MessageLogLevel logLevel)
223 BlackBerry::Platform::lockLogging(logLevel);
228 BlackBerry::Platform::unlockLogging();
233 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
235 #if PLATFORM(BLACKBERRY)
236 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
240 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
242 printf_stderr_common("SHOULD NEVER BE REACHED\n");
243 printCallSite(file, line, function);
246 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
248 #if PLATFORM(BLACKBERRY)
249 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
253 va_start(args, format);
254 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
256 printf_stderr_common("\n%s\n", assertion);
257 printCallSite(file, line, function);
260 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
262 #if PLATFORM(BLACKBERRY)
263 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
266 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
267 printCallSite(file, line, function);
270 void WTFGetBacktrace(void** stack, int* size)
272 #if (OS(DARWIN) || OS(LINUX)) && !OS(ANDROID)
273 *size = backtrace(stack, *size);
274 #elif OS(WINDOWS) && !OS(WINCE)
275 // The CaptureStackBackTrace function is available in XP, but it is not defined
276 // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
277 // through GetProcAddress.
278 typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
279 HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
284 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
285 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
286 if (captureStackBackTraceFunc)
287 *size = captureStackBackTraceFunc(0, *size, stack, 0);
295 void WTFReportBacktrace()
297 static const int framesToShow = 31;
298 static const int framesToSkip = 2;
299 void* samples[framesToShow + framesToSkip];
300 int frames = framesToShow + framesToSkip;
302 WTFGetBacktrace(samples, &frames);
303 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
306 #if OS(DARWIN) || OS(LINUX)
307 # if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
308 # if defined(__GLIBC__) && !defined(__UCLIBC__)
309 # define WTF_USE_BACKTRACE_SYMBOLS 1
312 # define WTF_USE_DLADDR 1
316 void WTFPrintBacktrace(void** stack, int size)
318 #if USE(BACKTRACE_SYMBOLS)
319 char** symbols = backtrace_symbols(stack, size);
324 for (int i = 0; i < size; ++i) {
325 const char* mangledName = 0;
326 char* cxaDemangled = 0;
327 #if USE(BACKTRACE_SYMBOLS)
328 mangledName = symbols[i];
331 if (dladdr(stack[i], &info) && info.dli_sname)
332 mangledName = info.dli_sname;
334 cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
336 const int frameNumber = i + 1;
337 if (mangledName || cxaDemangled)
338 printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
340 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
344 #if USE(BACKTRACE_SYMBOLS)
349 #undef WTF_USE_BACKTRACE_SYMBOLS
350 #undef WTF_USE_DLADDR
352 static WTFCrashHookFunction globalHook = 0;
354 void WTFSetCrashHook(WTFCrashHookFunction function)
356 globalHook = function;
359 void WTFInvokeCrashHook()
365 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
367 #if PLATFORM(BLACKBERRY)
368 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
372 va_start(args, format);
373 vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
375 printf_stderr_common("\n");
376 printCallSite(file, line, function);
379 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
381 #if PLATFORM(BLACKBERRY)
382 WTFLogLocker locker(BlackBerry::Platform::LogLevelWarn);
386 va_start(args, format);
387 vprintf_stderr_with_prefix("ERROR: ", format, args);
389 printf_stderr_common("\n");
390 printCallSite(file, line, function);
393 void WTFLog(WTFLogChannel* channel, const char* format, ...)
395 if (channel->state != WTFLogChannelOn)
398 #if PLATFORM(BLACKBERRY)
399 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
403 va_start(args, format);
404 vprintf_stderr_with_trailing_newline(format, args);
408 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
410 if (channel->state != WTFLogChannelOn)
413 #if PLATFORM(BLACKBERRY)
414 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
418 va_start(args, format);
419 #if ENABLE(TIZEN_DLOG_SUPPORT)
420 static char logMessage[DLOG_MAX_LENGTH];
422 int prefixLength = snprintf(logMessage, DLOG_MAX_LENGTH, "%s(%d) > ", function, line);
423 vsnprintf(logMessage + prefixLength, DLOG_MAX_LENGTH - prefixLength, format, args);
424 if (channel->mask == 0x00040000)
425 ALOG(LOG_INFO, DLOG_WEBKIT_TAG, "%s", logMessage);
426 else if (channel->mask == 0x00080000)
427 ALOG(LOG_ERROR, DLOG_WEBKIT_TAG, "%s", logMessage);
429 LOG(LOG_INFO, DLOG_WEBKIT_TAG, "%s", logMessage); // LOG macro from dlog
433 vprintf_stderr_with_trailing_newline(format, args);
436 printCallSite(file, line, function);
440 void WTFLogAlways(const char* format, ...)
442 #if PLATFORM(BLACKBERRY)
443 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
447 va_start(args, format);
448 vprintf_stderr_with_trailing_newline(format, args);
452 #if ENABLE(TIZEN_DLOG_SUPPORT)
453 void TizenLogError(int line, const char* function, const char* format, ...)
456 va_start(args, format);
458 static char logMessage[DLOG_MAX_LENGTH];
460 int prefixLength = snprintf(logMessage, DLOG_MAX_LENGTH, "%s(%d) > ERROR: ", function, line);
461 vsnprintf(logMessage + prefixLength, DLOG_MAX_LENGTH - prefixLength, format, args);
462 LOG(LOG_ERROR, DLOG_WEBKIT_TAG, "%s", logMessage); // LOG macro from dlog
467 void TizenLogInfo(int line, const char* function, const char* format, ...)
470 va_start(args, format);
472 static char logMessage[DLOG_MAX_LENGTH];
474 int prefixLength = snprintf(logMessage, DLOG_MAX_LENGTH, "%s(%d) > ", function, line);
475 vsnprintf(logMessage + prefixLength, DLOG_MAX_LENGTH - prefixLength, format, args);
476 LOG(LOG_INFO, DLOG_WEBKIT_TAG, "%s", logMessage); // LOG macro from dlog