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_CONSOLE_MESSAGE_TAG "ConsoleMessage"
66 #define DLOG_MAX_LENGTH 1024
70 #include "android/log.h"
73 #if PLATFORM(BLACKBERRY)
74 #include <BlackBerryPlatformLog.h>
79 WTF_ATTRIBUTE_PRINTF(1, 0)
80 static void vprintf_stderr_common(const char* format, va_list args)
83 if (strstr(format, "%@")) {
84 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
87 #pragma clang diagnostic push
88 #pragma clang diagnostic ignored "-Wformat-nonliteral"
90 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
92 #pragma clang diagnostic pop
94 int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
95 char* buffer = (char*)malloc(length + 1);
97 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
99 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
100 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
102 fputs(buffer, stderr);
110 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
112 va_copy(copyOfArgs, args);
113 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
117 // Fall through to write to stderr in the same manner as other platforms.
119 #elif PLATFORM(BLACKBERRY)
120 BlackBerry::Platform::logStreamV(format, args);
122 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
123 #elif HAVE(ISDEBUGGERPRESENT)
124 if (IsDebuggerPresent()) {
128 char* buffer = (char*)malloc(size);
133 if (_vsnprintf(buffer, size, format, args) != -1) {
135 // WinCE only supports wide chars
136 wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t));
137 if (wideBuffer == NULL)
139 for (unsigned int i = 0; i < size; ++i) {
140 if (!(wideBuffer[i] = buffer[i]))
143 OutputDebugStringW(wideBuffer);
146 OutputDebugStringA(buffer);
154 } while (size > 1024);
157 #if !PLATFORM(BLACKBERRY)
158 vfprintf(stderr, format, args);
162 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
163 #pragma GCC diagnostic push
164 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
167 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
169 size_t prefixLength = strlen(prefix);
170 size_t formatLength = strlen(format);
171 OwnArrayPtr<char> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]);
172 memcpy(formatWithPrefix.get(), prefix, prefixLength);
173 memcpy(formatWithPrefix.get() + prefixLength, format, formatLength);
174 formatWithPrefix[prefixLength + formatLength] = 0;
176 vprintf_stderr_common(formatWithPrefix.get(), args);
179 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
181 size_t formatLength = strlen(format);
182 if (formatLength && format[formatLength - 1] == '\n') {
183 vprintf_stderr_common(format, args);
187 OwnArrayPtr<char> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
188 memcpy(formatWithNewline.get(), format, formatLength);
189 formatWithNewline[formatLength] = '\n';
190 formatWithNewline[formatLength + 1] = 0;
192 vprintf_stderr_common(formatWithNewline.get(), args);
195 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
196 #pragma GCC diagnostic pop
199 WTF_ATTRIBUTE_PRINTF(1, 2)
200 static void printf_stderr_common(const char* format, ...)
203 va_start(args, format);
204 vprintf_stderr_common(format, args);
208 static void printCallSite(const char* file, int line, const char* function)
210 #if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
211 _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
213 // By using this format, which matches the format used by MSVC for compiler errors, developers
214 // using Visual Studio can double-click the file/line number in the Output Window to have the
215 // editor navigate to that line of code. It seems fine for other developers, too.
216 printf_stderr_common("%s(%d) : %s\n", file, line, function);
220 #if PLATFORM(BLACKBERRY)
221 struct WTFLogLocker {
222 WTFLogLocker(BlackBerry::Platform::MessageLogLevel logLevel)
224 BlackBerry::Platform::lockLogging(logLevel);
229 BlackBerry::Platform::unlockLogging();
234 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
236 #if PLATFORM(BLACKBERRY)
237 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
241 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
243 printf_stderr_common("SHOULD NEVER BE REACHED\n");
244 printCallSite(file, line, function);
247 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
249 #if PLATFORM(BLACKBERRY)
250 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
254 va_start(args, format);
255 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
257 printf_stderr_common("\n%s\n", assertion);
258 printCallSite(file, line, function);
261 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
263 #if PLATFORM(BLACKBERRY)
264 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
267 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
268 printCallSite(file, line, function);
271 void WTFGetBacktrace(void** stack, int* size)
273 #if (OS(DARWIN) || OS(LINUX)) && !OS(ANDROID)
274 *size = backtrace(stack, *size);
275 #elif OS(WINDOWS) && !OS(WINCE)
276 // The CaptureStackBackTrace function is available in XP, but it is not defined
277 // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
278 // through GetProcAddress.
279 typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
280 HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
285 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
286 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
287 if (captureStackBackTraceFunc)
288 *size = captureStackBackTraceFunc(0, *size, stack, 0);
296 void WTFReportBacktrace()
298 static const int framesToShow = 31;
299 static const int framesToSkip = 2;
300 void* samples[framesToShow + framesToSkip];
301 int frames = framesToShow + framesToSkip;
303 WTFGetBacktrace(samples, &frames);
304 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
307 #if OS(DARWIN) || OS(LINUX)
308 # if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
309 # if defined(__GLIBC__) && !defined(__UCLIBC__)
310 # define WTF_USE_BACKTRACE_SYMBOLS 1
313 # define WTF_USE_DLADDR 1
317 void WTFPrintBacktrace(void** stack, int size)
319 #if USE(BACKTRACE_SYMBOLS)
320 char** symbols = backtrace_symbols(stack, size);
325 for (int i = 0; i < size; ++i) {
326 const char* mangledName = 0;
327 char* cxaDemangled = 0;
328 #if USE(BACKTRACE_SYMBOLS)
329 mangledName = symbols[i];
332 if (dladdr(stack[i], &info) && info.dli_sname)
333 mangledName = info.dli_sname;
335 cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
337 const int frameNumber = i + 1;
338 if (mangledName || cxaDemangled)
339 printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
341 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
345 #if USE(BACKTRACE_SYMBOLS)
350 #undef WTF_USE_BACKTRACE_SYMBOLS
351 #undef WTF_USE_DLADDR
353 static WTFCrashHookFunction globalHook = 0;
355 void WTFSetCrashHook(WTFCrashHookFunction function)
357 globalHook = function;
360 void WTFInvokeCrashHook()
366 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
368 #if PLATFORM(BLACKBERRY)
369 WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
373 va_start(args, format);
374 vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
376 printf_stderr_common("\n");
377 printCallSite(file, line, function);
380 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
382 #if PLATFORM(BLACKBERRY)
383 WTFLogLocker locker(BlackBerry::Platform::LogLevelWarn);
387 va_start(args, format);
388 vprintf_stderr_with_prefix("ERROR: ", format, args);
390 printf_stderr_common("\n");
391 printCallSite(file, line, function);
394 void WTFLog(WTFLogChannel* channel, const char* format, ...)
396 if (channel->state != WTFLogChannelOn)
399 #if PLATFORM(BLACKBERRY)
400 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
404 va_start(args, format);
405 vprintf_stderr_with_trailing_newline(format, args);
409 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
411 if (channel->state != WTFLogChannelOn)
414 #if PLATFORM(BLACKBERRY)
415 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
419 va_start(args, format);
420 #if ENABLE(TIZEN_DLOG_SUPPORT)
421 char logFormat[DLOG_MAX_LENGTH] = { 0 };
423 snprintf(logFormat, DLOG_MAX_LENGTH, "%s(%d) > %s", function, line, format);
424 LOG_VA(LOG_INFO, DLOG_WEBKIT_TAG, logFormat, args);
428 vprintf_stderr_with_trailing_newline(format, args);
431 printCallSite(file, line, function);
435 void WTFLogAlways(const char* format, ...)
437 #if PLATFORM(BLACKBERRY)
438 WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
442 va_start(args, format);
443 vprintf_stderr_with_trailing_newline(format, args);
447 #if ENABLE(TIZEN_DLOG_SUPPORT)
448 void TizenLog(TizenDlogPriority priority, bool secureLog, int line, const char* function, const char* format, ...)
451 // Do not print logs if _SECURE_LOG is not defined by dlog package and secureLog is true.
457 va_start(args, format);
459 char logFormat[DLOG_MAX_LENGTH] = { 0 };
461 const char* secureLogTag = "";
463 secureLogTag = "[SECURE_LOG] ";
466 case TIZEN_DLOG_PRIORITY_INFO:
467 snprintf(logFormat, DLOG_MAX_LENGTH, "%s(%d) > %s%s", function, line, secureLogTag, format);
468 LOG_VA(LOG_DEBUG, DLOG_WEBKIT_TAG, logFormat, args);
470 case TIZEN_DLOG_PRIORITY_ERROR:
471 snprintf(logFormat, DLOG_MAX_LENGTH, "%s(%d) > %sERROR: %s", function, line, secureLogTag, format);
472 LOG_VA(LOG_ERROR, DLOG_WEBKIT_TAG, logFormat, args);
475 ASSERT_NOT_REACHED();
481 #if ENABLE(TIZEN_DISPLAY_MESSAGE_TO_CONSOLE) // Request for WAC SDK
482 void TizenConsoleMessage(TizenDlogPriority priority, const char* format, ...)
485 va_start(args, format);
488 case TIZEN_DLOG_PRIORITY_INFO:
489 ALOG_VA(LOG_INFO, DLOG_CONSOLE_MESSAGE_TAG, format, args);
491 case TIZEN_DLOG_PRIORITY_ERROR:
492 ALOG_VA(LOG_ERROR, DLOG_CONSOLE_MESSAGE_TAG, format, args);
494 case TIZEN_DLOG_PRIORITY_WARN:
495 ALOG_VA(LOG_WARN, DLOG_CONSOLE_MESSAGE_TAG, format, args);
497 case TIZEN_DLOG_PRIORITY_DEBUG:
498 ALOG_VA(LOG_DEBUG, DLOG_CONSOLE_MESSAGE_TAG, format, args);
501 ASSERT_NOT_REACHED();