4f824297d5bd56cf6ea02680a982fdf6b4f9759b
[framework/web/webkit-efl.git] / Source / WTF / wtf / Assertions.cpp
1 /*
2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007-2009 Torch Mobile, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
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"
32
33 #include "config.h"
34 #include "Assertions.h"
35
36 #include "OwnArrayPtr.h"
37
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41
42 #if PLATFORM(MAC)
43 #include <CoreFoundation/CFString.h>
44 #include <asl.h>
45 #endif
46
47 #if COMPILER(MSVC) && !OS(WINCE)
48 #include <crtdbg.h>
49 #endif
50
51 #if OS(WINDOWS)
52 #include <windows.h>
53 #endif
54
55 #if (OS(DARWIN) || OS(LINUX)) && !OS(ANDROID)
56 #include <cxxabi.h>
57 #include <dlfcn.h>
58 #include <execinfo.h>
59 #endif
60
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
67 #endif
68
69 #if OS(ANDROID)
70 #include "android/log.h"
71 #endif
72
73 #if PLATFORM(BLACKBERRY)
74 #include <BlackBerryPlatformLog.h>
75 #endif
76
77 extern "C" {
78
79 WTF_ATTRIBUTE_PRINTF(1, 0)
80 static void vprintf_stderr_common(const char* format, va_list args)
81 {
82 #if PLATFORM(MAC)
83     if (strstr(format, "%@")) {
84         CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
85
86 #if COMPILER(CLANG)
87 #pragma clang diagnostic push
88 #pragma clang diagnostic ignored "-Wformat-nonliteral"
89 #endif
90         CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
91 #if COMPILER(CLANG)
92 #pragma clang diagnostic pop
93 #endif
94         int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
95         char* buffer = (char*)malloc(length + 1);
96
97         CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
98
99 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
100         asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
101 #endif
102         fputs(buffer, stderr);
103
104         free(buffer);
105         CFRelease(str);
106         CFRelease(cfFormat);
107         return;
108     }
109
110 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
111     va_list copyOfArgs;
112     va_copy(copyOfArgs, args);
113     asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
114     va_end(copyOfArgs);
115 #endif
116
117     // Fall through to write to stderr in the same manner as other platforms.
118
119 #elif PLATFORM(BLACKBERRY)
120     BlackBerry::Platform::logStreamV(format, args);
121 #elif OS(ANDROID)
122     __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
123 #elif HAVE(ISDEBUGGERPRESENT)
124     if (IsDebuggerPresent()) {
125         size_t size = 1024;
126
127         do {
128             char* buffer = (char*)malloc(size);
129
130             if (buffer == NULL)
131                 break;
132
133             if (_vsnprintf(buffer, size, format, args) != -1) {
134 #if OS(WINCE)
135                 // WinCE only supports wide chars
136                 wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t));
137                 if (wideBuffer == NULL)
138                     break;
139                 for (unsigned int i = 0; i < size; ++i) {
140                     if (!(wideBuffer[i] = buffer[i]))
141                         break;
142                 }
143                 OutputDebugStringW(wideBuffer);
144                 free(wideBuffer);
145 #else
146                 OutputDebugStringA(buffer);
147 #endif
148                 free(buffer);
149                 break;
150             }
151
152             free(buffer);
153             size *= 2;
154         } while (size > 1024);
155     }
156 #endif
157 #if !PLATFORM(BLACKBERRY)
158     vfprintf(stderr, format, args);
159 #endif
160 }
161
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"
165 #endif
166
167 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
168 {
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;
175
176     vprintf_stderr_common(formatWithPrefix.get(), args);
177 }
178
179 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
180 {
181     size_t formatLength = strlen(format);
182     if (formatLength && format[formatLength - 1] == '\n') {
183         vprintf_stderr_common(format, args);
184         return;
185     }
186
187     OwnArrayPtr<char> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
188     memcpy(formatWithNewline.get(), format, formatLength);
189     formatWithNewline[formatLength] = '\n';
190     formatWithNewline[formatLength + 1] = 0;
191
192     vprintf_stderr_common(formatWithNewline.get(), args);
193 }
194
195 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
196 #pragma GCC diagnostic pop
197 #endif
198
199 WTF_ATTRIBUTE_PRINTF(1, 2)
200 static void printf_stderr_common(const char* format, ...)
201 {
202     va_list args;
203     va_start(args, format);
204     vprintf_stderr_common(format, args);
205     va_end(args);
206 }
207
208 static void printCallSite(const char* file, int line, const char* function)
209 {
210 #if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
211     _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
212 #else
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);
217 #endif
218 }
219
220 #if PLATFORM(BLACKBERRY)
221 struct WTFLogLocker {
222     WTFLogLocker(BlackBerry::Platform::MessageLogLevel logLevel)
223     {
224         BlackBerry::Platform::lockLogging(logLevel);
225     }
226
227     ~WTFLogLocker()
228     {
229         BlackBerry::Platform::unlockLogging();
230     }
231 };
232 #endif
233
234 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
235 {
236 #if PLATFORM(BLACKBERRY)
237     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
238 #endif
239
240     if (assertion)
241         printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
242     else
243         printf_stderr_common("SHOULD NEVER BE REACHED\n");
244     printCallSite(file, line, function);
245 }
246
247 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
248 {
249 #if PLATFORM(BLACKBERRY)
250     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
251 #endif
252
253     va_list args;
254     va_start(args, format);
255     vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
256     va_end(args);
257     printf_stderr_common("\n%s\n", assertion);
258     printCallSite(file, line, function);
259 }
260
261 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
262 {
263 #if PLATFORM(BLACKBERRY)
264     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
265 #endif
266
267     printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
268     printCallSite(file, line, function);
269 }
270
271 void WTFGetBacktrace(void** stack, int* size)
272 {
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");
281     if (!kernel32) {
282         *size = 0;
283         return;
284     }
285     RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
286         ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
287     if (captureStackBackTraceFunc)
288         *size = captureStackBackTraceFunc(0, *size, stack, 0);
289     else
290         *size = 0;
291 #else
292     *size = 0;
293 #endif
294 }
295
296 void WTFReportBacktrace()
297 {
298     static const int framesToShow = 31;
299     static const int framesToSkip = 2;
300     void* samples[framesToShow + framesToSkip];
301     int frames = framesToShow + framesToSkip;
302
303     WTFGetBacktrace(samples, &frames);
304     WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
305 }
306
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
311 #    endif
312 #  elif !OS(ANDROID)
313 #    define WTF_USE_DLADDR 1
314 #  endif
315 #endif
316
317 void WTFPrintBacktrace(void** stack, int size)
318 {
319 #if USE(BACKTRACE_SYMBOLS)
320     char** symbols = backtrace_symbols(stack, size);
321     if (!symbols)
322         return;
323 #endif
324
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];
330 #elif USE(DLADDR)
331         Dl_info info;
332         if (dladdr(stack[i], &info) && info.dli_sname)
333             mangledName = info.dli_sname;
334         if (mangledName)
335             cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
336 #endif
337         const int frameNumber = i + 1;
338         if (mangledName || cxaDemangled)
339             printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
340         else
341             printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
342         free(cxaDemangled);
343     }
344
345 #if USE(BACKTRACE_SYMBOLS)
346     free(symbols);
347 #endif
348 }
349
350 #undef WTF_USE_BACKTRACE_SYMBOLS
351 #undef WTF_USE_DLADDR
352
353 static WTFCrashHookFunction globalHook = 0;
354
355 void WTFSetCrashHook(WTFCrashHookFunction function)
356 {
357     globalHook = function;
358 }
359
360 void WTFInvokeCrashHook()
361 {
362     if (globalHook)
363         globalHook();
364 }
365
366 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
367 {
368 #if PLATFORM(BLACKBERRY)
369     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
370 #endif
371
372     va_list args;
373     va_start(args, format);
374     vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
375     va_end(args);
376     printf_stderr_common("\n");
377     printCallSite(file, line, function);
378 }
379
380 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
381 {
382 #if PLATFORM(BLACKBERRY)
383     WTFLogLocker locker(BlackBerry::Platform::LogLevelWarn);
384 #endif
385
386     va_list args;
387     va_start(args, format);
388     vprintf_stderr_with_prefix("ERROR: ", format, args);
389     va_end(args);
390     printf_stderr_common("\n");
391     printCallSite(file, line, function);
392 }
393
394 void WTFLog(WTFLogChannel* channel, const char* format, ...)
395 {
396     if (channel->state != WTFLogChannelOn)
397         return;
398
399 #if PLATFORM(BLACKBERRY)
400     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
401 #endif
402
403     va_list args;
404     va_start(args, format);
405     vprintf_stderr_with_trailing_newline(format, args);
406     va_end(args);
407 }
408
409 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
410 {
411     if (channel->state != WTFLogChannelOn)
412         return;
413
414 #if PLATFORM(BLACKBERRY)
415     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
416 #endif
417
418     va_list args;
419     va_start(args, format);
420 #if ENABLE(TIZEN_DLOG_SUPPORT)
421     char logFormat[DLOG_MAX_LENGTH] = { 0 };
422
423     snprintf(logFormat, DLOG_MAX_LENGTH, "%s(%d) > %s", function, line, format);
424     LOG_VA(LOG_INFO, DLOG_WEBKIT_TAG, logFormat, args);
425
426     va_end(args);
427 #else
428     vprintf_stderr_with_trailing_newline(format, args);
429     va_end(args);
430
431     printCallSite(file, line, function);
432 #endif
433 }
434
435 void WTFLogAlways(const char* format, ...)
436 {
437 #if PLATFORM(BLACKBERRY)
438     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
439 #endif
440
441     va_list args;
442     va_start(args, format);
443     vprintf_stderr_with_trailing_newline(format, args);
444     va_end(args);
445 }
446
447 #if ENABLE(TIZEN_DLOG_SUPPORT)
448 void TizenLog(TizenDlogPriority priority, bool secureLog, int line, const char* function, const char* format, ...)
449 {
450 #ifndef _SECURE_LOG
451     // Do not print logs if _SECURE_LOG is not defined by dlog package and secureLog is true.
452     if (secureLog)
453         return;
454 #endif
455
456     va_list args;
457     va_start(args, format);
458
459     char logFormat[DLOG_MAX_LENGTH] = { 0 };
460
461     const char* secureLogTag = "";
462     if (secureLog)
463         secureLogTag = "[SECURE_LOG] ";
464
465     switch (priority) {
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);
469         break;
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);
473         break;
474     default:
475         ASSERT_NOT_REACHED();
476     }
477
478     va_end(args);
479 }
480
481 #if ENABLE(TIZEN_DISPLAY_MESSAGE_TO_CONSOLE) // Request for WAC SDK
482 void TizenConsoleMessage(TizenDlogPriority priority, const char* format, ...)
483 {
484     va_list args;
485     va_start(args, format);
486
487     switch (priority) {
488     case TIZEN_DLOG_PRIORITY_INFO:
489         ALOG_VA(LOG_INFO, DLOG_CONSOLE_MESSAGE_TAG, format, args);
490         break;
491     case TIZEN_DLOG_PRIORITY_ERROR:
492         ALOG_VA(LOG_ERROR, DLOG_CONSOLE_MESSAGE_TAG, format, args);
493         break;
494     case TIZEN_DLOG_PRIORITY_WARN:
495         ALOG_VA(LOG_WARN, DLOG_CONSOLE_MESSAGE_TAG, format, args);
496         break;
497     case TIZEN_DLOG_PRIORITY_DEBUG:
498         ALOG_VA(LOG_DEBUG, DLOG_CONSOLE_MESSAGE_TAG, format, args);
499         break;
500     default:
501         ASSERT_NOT_REACHED();
502     }
503
504     va_end(args);
505 }
506 #endif
507 #endif
508 } // extern "C"