cc5e4deb07afbf3e13ad1d6a29e8a6153ec5697e
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / Assertions.cpp
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 USE(CF)
47 #include <AvailabilityMacros.h>
48 #include <CoreFoundation/CFString.h>
49 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
50 #define WTF_USE_APPLE_SYSTEM_LOG 1
51 #include <asl.h>
52 #endif
53 #endif // USE(CF)
54
55 #if COMPILER(MSVC)
56 #include <crtdbg.h>
57 #endif
58
59 #if OS(WIN)
60 #include <windows.h>
61 #define HAVE_ISDEBUGGERPRESENT 1
62 #endif
63
64 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
65 #include <cxxabi.h>
66 #include <dlfcn.h>
67 #include <execinfo.h>
68 #endif
69
70 #if OS(ANDROID)
71 #include <android/log.h>
72 #endif
73
74 WTF_ATTRIBUTE_PRINTF(1, 0)
75 static void vprintf_stderr_common(const char* format, va_list args)
76 {
77 #if USE(CF) && !OS(WIN)
78     if (strstr(format, "%@")) {
79         CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
80
81 #if COMPILER(CLANG)
82 #pragma clang diagnostic push
83 #pragma clang diagnostic ignored "-Wformat-nonliteral"
84 #endif
85         CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
86 #if COMPILER(CLANG)
87 #pragma clang diagnostic pop
88 #endif
89         CFIndex length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
90         char* buffer = (char*)malloc(length + 1);
91
92         CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
93
94 #if USE(APPLE_SYSTEM_LOG)
95         asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
96 #endif
97         fputs(buffer, stderr);
98
99         free(buffer);
100         CFRelease(str);
101         CFRelease(cfFormat);
102         return;
103     }
104
105 #if USE(APPLE_SYSTEM_LOG)
106     va_list copyOfArgs;
107     va_copy(copyOfArgs, args);
108     asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
109     va_end(copyOfArgs);
110 #endif
111
112     // Fall through to write to stderr in the same manner as other platforms.
113
114 #elif OS(ANDROID)
115     __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
116 #elif HAVE(ISDEBUGGERPRESENT)
117     if (IsDebuggerPresent()) {
118         size_t size = 1024;
119
120         do {
121             char* buffer = (char*)malloc(size);
122
123             if (buffer == NULL)
124                 break;
125
126             if (_vsnprintf(buffer, size, format, args) != -1) {
127                 OutputDebugStringA(buffer);
128                 free(buffer);
129                 break;
130             }
131
132             free(buffer);
133             size *= 2;
134         } while (size > 1024);
135     }
136 #endif
137     vfprintf(stderr, format, args);
138 }
139
140 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
141 #pragma GCC diagnostic push
142 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
143 #endif
144
145 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
146 {
147     size_t prefixLength = strlen(prefix);
148     size_t formatLength = strlen(format);
149     OwnPtr<char[]> formatWithPrefix = adoptArrayPtr(new char[prefixLength + formatLength + 1]);
150     memcpy(formatWithPrefix.get(), prefix, prefixLength);
151     memcpy(formatWithPrefix.get() + prefixLength, format, formatLength);
152     formatWithPrefix[prefixLength + formatLength] = 0;
153
154     vprintf_stderr_common(formatWithPrefix.get(), args);
155 }
156
157 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
158 {
159     size_t formatLength = strlen(format);
160     if (formatLength && format[formatLength - 1] == '\n') {
161         vprintf_stderr_common(format, args);
162         return;
163     }
164
165     OwnPtr<char[]> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
166     memcpy(formatWithNewline.get(), format, formatLength);
167     formatWithNewline[formatLength] = '\n';
168     formatWithNewline[formatLength + 1] = 0;
169
170     vprintf_stderr_common(formatWithNewline.get(), args);
171 }
172
173 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
174 #pragma GCC diagnostic pop
175 #endif
176
177 WTF_ATTRIBUTE_PRINTF(1, 2)
178 static void printf_stderr_common(const char* format, ...)
179 {
180     va_list args;
181     va_start(args, format);
182     vprintf_stderr_common(format, args);
183     va_end(args);
184 }
185
186 static void printCallSite(const char* file, int line, const char* function)
187 {
188 #if OS(WIN) && defined(_DEBUG)
189     _CrtDbgReport(_CRT_WARN, file, line, NULL, "%s\n", function);
190 #else
191     // By using this format, which matches the format used by MSVC for compiler errors, developers
192     // using Visual Studio can double-click the file/line number in the Output Window to have the
193     // editor navigate to that line of code. It seems fine for other developers, too.
194     printf_stderr_common("%s(%d) : %s\n", file, line, function);
195 #endif
196 }
197
198 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
199 {
200     if (assertion)
201         printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
202     else
203         printf_stderr_common("SHOULD NEVER BE REACHED\n");
204     printCallSite(file, line, function);
205 }
206
207 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
208 {
209     va_list args;
210     va_start(args, format);
211     vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, args);
212     va_end(args);
213     printf_stderr_common("\n%s\n", assertion);
214     printCallSite(file, line, function);
215 }
216
217 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
218 {
219     printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
220     printCallSite(file, line, function);
221 }
222
223 void WTFGetBacktrace(void** stack, int* size)
224 {
225 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
226     *size = backtrace(stack, *size);
227 #elif OS(WIN)
228     // The CaptureStackBackTrace function is available in XP, but it is not defined
229     // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
230     // through GetProcAddress.
231     typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
232     HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
233     if (!kernel32) {
234         *size = 0;
235         return;
236     }
237     RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
238         ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
239     if (captureStackBackTraceFunc)
240         *size = captureStackBackTraceFunc(0, *size, stack, 0);
241     else
242         *size = 0;
243 #else
244     *size = 0;
245 #endif
246 }
247
248 void WTFReportBacktrace(int framesToShow)
249 {
250     static const int framesToSkip = 2;
251     // Use alloca to allocate on the stack since this function is used in OOM situations.
252     void** samples = static_cast<void**>(alloca((framesToShow + framesToSkip) * sizeof(void *)));
253     int frames = framesToShow + framesToSkip;
254
255     WTFGetBacktrace(samples, &frames);
256     WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
257 }
258
259 FrameToNameScope::FrameToNameScope(void* addr)
260     : m_name(0)
261     , m_cxaDemangled(0)
262 {
263 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
264     Dl_info info;
265     if (!dladdr(addr, &info) || !info.dli_sname)
266         return;
267     const char* mangledName = info.dli_sname;
268     if ((m_cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0)))
269         m_name = m_cxaDemangled;
270     else
271         m_name = mangledName;
272 #else
273     (void)addr;
274 #endif
275 }
276
277 FrameToNameScope::~FrameToNameScope()
278 {
279     free(m_cxaDemangled);
280 }
281
282 void WTFPrintBacktrace(void** stack, int size)
283 {
284     for (int i = 0; i < size; ++i) {
285         FrameToNameScope frameToName(stack[i]);
286         const int frameNumber = i + 1;
287         if (frameToName.nullableName())
288             printf_stderr_common("%-3d %p %s\n", frameNumber, stack[i], frameToName.nullableName());
289         else
290             printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
291     }
292 }
293
294 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
295 {
296     va_list args;
297     va_start(args, format);
298     vprintf_stderr_with_prefix("FATAL ERROR: ", format, args);
299     va_end(args);
300     printf_stderr_common("\n");
301     printCallSite(file, line, function);
302 }
303
304 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
305 {
306     va_list args;
307     va_start(args, format);
308     vprintf_stderr_with_prefix("ERROR: ", format, args);
309     va_end(args);
310     printf_stderr_common("\n");
311     printCallSite(file, line, function);
312 }
313
314 void WTFLog(WTFLogChannel* channel, const char* format, ...)
315 {
316     if (channel->state != WTFLogChannelOn)
317         return;
318
319     va_list args;
320     va_start(args, format);
321     vprintf_stderr_with_trailing_newline(format, args);
322     va_end(args);
323 }
324
325 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
326 {
327     if (channel->state != WTFLogChannelOn)
328         return;
329
330     va_list args;
331     va_start(args, format);
332     vprintf_stderr_with_trailing_newline(format, args);
333     va_end(args);
334
335     printCallSite(file, line, function);
336 }
337
338 void WTFLogAlways(const char* format, ...)
339 {
340     va_list args;
341     va_start(args, format);
342     vprintf_stderr_with_trailing_newline(format, args);
343     va_end(args);
344 }
345