Merge "Fix build break by removing TIZEN_RECORDING_SURFACE_SET" into tizen_2.1
[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_MAX_LENGTH 1024
66 #endif
67
68 #if OS(ANDROID)
69 #include "android/log.h"
70 #endif
71
72 #if PLATFORM(BLACKBERRY)
73 #include <BlackBerryPlatformLog.h>
74 #endif
75
76 extern "C" {
77
78 WTF_ATTRIBUTE_PRINTF(1, 0)
79 static void vprintf_stderr_common(const char* format, va_list args)
80 {
81 #if PLATFORM(MAC)
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         int length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
94         char* buffer = (char*)malloc(length + 1);
95
96         CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
97
98 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
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 PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
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 PLATFORM(BLACKBERRY)
119     BlackBerry::Platform::logStreamV(format, args);
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 #if OS(WINCE)
134                 // WinCE only supports wide chars
135                 wchar_t* wideBuffer = (wchar_t*)malloc(size * sizeof(wchar_t));
136                 if (wideBuffer == NULL)
137                     break;
138                 for (unsigned int i = 0; i < size; ++i) {
139                     if (!(wideBuffer[i] = buffer[i]))
140                         break;
141                 }
142                 OutputDebugStringW(wideBuffer);
143                 free(wideBuffer);
144 #else
145                 OutputDebugStringA(buffer);
146 #endif
147                 free(buffer);
148                 break;
149             }
150
151             free(buffer);
152             size *= 2;
153         } while (size > 1024);
154     }
155 #endif
156 #if !PLATFORM(BLACKBERRY)
157     vfprintf(stderr, format, args);
158 #endif
159 }
160
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"
164 #endif
165
166 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
167 {
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;
174
175     vprintf_stderr_common(formatWithPrefix.get(), args);
176 }
177
178 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
179 {
180     size_t formatLength = strlen(format);
181     if (formatLength && format[formatLength - 1] == '\n') {
182         vprintf_stderr_common(format, args);
183         return;
184     }
185
186     OwnArrayPtr<char> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
187     memcpy(formatWithNewline.get(), format, formatLength);
188     formatWithNewline[formatLength] = '\n';
189     formatWithNewline[formatLength + 1] = 0;
190
191     vprintf_stderr_common(formatWithNewline.get(), args);
192 }
193
194 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
195 #pragma GCC diagnostic pop
196 #endif
197
198 WTF_ATTRIBUTE_PRINTF(1, 2)
199 static void printf_stderr_common(const char* format, ...)
200 {
201     va_list args;
202     va_start(args, format);
203     vprintf_stderr_common(format, args);
204     va_end(args);
205 }
206
207 static void printCallSite(const char* file, int line, const char* function)
208 {
209 #if OS(WINDOWS) && !OS(WINCE) && defined(_DEBUG)
210     _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
211 #else
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);
216 #endif
217 }
218
219 #if PLATFORM(BLACKBERRY)
220 struct WTFLogLocker {
221     WTFLogLocker(BlackBerry::Platform::MessageLogLevel logLevel)
222     {
223         BlackBerry::Platform::lockLogging(logLevel);
224     }
225
226     ~WTFLogLocker()
227     {
228         BlackBerry::Platform::unlockLogging();
229     }
230 };
231 #endif
232
233 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
234 {
235 #if PLATFORM(BLACKBERRY)
236     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
237 #endif
238
239     if (assertion)
240         printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
241     else
242         printf_stderr_common("SHOULD NEVER BE REACHED\n");
243     printCallSite(file, line, function);
244 }
245
246 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
247 {
248 #if PLATFORM(BLACKBERRY)
249     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
250 #endif
251
252     va_list args;
253     va_start(args, format);
254     vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
255     va_end(args);
256     printf_stderr_common("\n%s\n", assertion);
257     printCallSite(file, line, function);
258 }
259
260 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
261 {
262 #if PLATFORM(BLACKBERRY)
263     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
264 #endif
265
266     printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
267     printCallSite(file, line, function);
268 }
269
270 void WTFGetBacktrace(void** stack, int* size)
271 {
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");
280     if (!kernel32) {
281         *size = 0;
282         return;
283     }
284     RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
285         ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
286     if (captureStackBackTraceFunc)
287         *size = captureStackBackTraceFunc(0, *size, stack, 0);
288     else
289         *size = 0;
290 #else
291     *size = 0;
292 #endif
293 }
294
295 void WTFReportBacktrace()
296 {
297     static const int framesToShow = 31;
298     static const int framesToSkip = 2;
299     void* samples[framesToShow + framesToSkip];
300     int frames = framesToShow + framesToSkip;
301
302     WTFGetBacktrace(samples, &frames);
303     WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
304 }
305
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
310 #    endif
311 #  elif !OS(ANDROID)
312 #    define WTF_USE_DLADDR 1
313 #  endif
314 #endif
315
316 void WTFPrintBacktrace(void** stack, int size)
317 {
318 #if USE(BACKTRACE_SYMBOLS)
319     char** symbols = backtrace_symbols(stack, size);
320     if (!symbols)
321         return;
322 #endif
323
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];
329 #elif USE(DLADDR)
330         Dl_info info;
331         if (dladdr(stack[i], &info) && info.dli_sname)
332             mangledName = info.dli_sname;
333         if (mangledName)
334             cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
335 #endif
336         const int frameNumber = i + 1;
337         if (mangledName || cxaDemangled)
338             printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
339         else
340             printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
341         free(cxaDemangled);
342     }
343
344 #if USE(BACKTRACE_SYMBOLS)
345     free(symbols);
346 #endif
347 }
348
349 #undef WTF_USE_BACKTRACE_SYMBOLS
350 #undef WTF_USE_DLADDR
351
352 static WTFCrashHookFunction globalHook = 0;
353
354 void WTFSetCrashHook(WTFCrashHookFunction function)
355 {
356     globalHook = function;
357 }
358
359 void WTFInvokeCrashHook()
360 {
361     if (globalHook)
362         globalHook();
363 }
364
365 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
366 {
367 #if PLATFORM(BLACKBERRY)
368     WTFLogLocker locker(BlackBerry::Platform::LogLevelCritical);
369 #endif
370
371     va_list args;
372     va_start(args, format);
373     vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
374     va_end(args);
375     printf_stderr_common("\n");
376     printCallSite(file, line, function);
377 }
378
379 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
380 {
381 #if PLATFORM(BLACKBERRY)
382     WTFLogLocker locker(BlackBerry::Platform::LogLevelWarn);
383 #endif
384
385     va_list args;
386     va_start(args, format);
387     vprintf_stderr_with_prefix("ERROR: ", format, args);
388     va_end(args);
389     printf_stderr_common("\n");
390     printCallSite(file, line, function);
391 }
392
393 void WTFLog(WTFLogChannel* channel, const char* format, ...)
394 {
395     if (channel->state != WTFLogChannelOn)
396         return;
397
398 #if PLATFORM(BLACKBERRY)
399     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
400 #endif
401
402     va_list args;
403     va_start(args, format);
404     vprintf_stderr_with_trailing_newline(format, args);
405     va_end(args);
406 }
407
408 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
409 {
410     if (channel->state != WTFLogChannelOn)
411         return;
412
413 #if PLATFORM(BLACKBERRY)
414     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
415 #endif
416
417     va_list args;
418     va_start(args, format);
419 #if ENABLE(TIZEN_DLOG_SUPPORT)
420     static char logMessage[DLOG_MAX_LENGTH];
421
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);
428     else
429         LOG(LOG_INFO, DLOG_WEBKIT_TAG, "%s", logMessage); // LOG macro from dlog
430
431     va_end(args);
432 #else
433     vprintf_stderr_with_trailing_newline(format, args);
434     va_end(args);
435
436     printCallSite(file, line, function);
437 #endif
438 }
439
440 void WTFLogAlways(const char* format, ...)
441 {
442 #if PLATFORM(BLACKBERRY)
443     WTFLogLocker locker(BlackBerry::Platform::LogLevelInfo);
444 #endif
445
446     va_list args;
447     va_start(args, format);
448     vprintf_stderr_with_trailing_newline(format, args);
449     va_end(args);
450 }
451
452 #if ENABLE(TIZEN_DLOG_SUPPORT)
453 void TizenLogError(int line, const char* function, const char* format, ...)
454 {
455     va_list args;
456     va_start(args, format);
457
458     static char logMessage[DLOG_MAX_LENGTH];
459
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
463
464     va_end(args);
465 }
466
467 void TizenLogInfo(int line, const char* function, const char* format, ...)
468 {
469     va_list args;
470     va_start(args, format);
471
472     static char logMessage[DLOG_MAX_LENGTH];
473
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
477
478     va_end(args);
479 }
480 #endif
481 } // extern "C"