Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / Assertions.cpp
blobcc5e4deb07afbf3e13ad1d6a29e8a6153ec5697e
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.
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.
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.
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"
34 #include "config.h"
35 #include "Assertions.h"
37 #include "Compiler.h"
38 #include "OwnPtr.h"
39 #include "PassOwnPtr.h"
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
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)
55 #if COMPILER(MSVC)
56 #include <crtdbg.h>
57 #endif
59 #if OS(WIN)
60 #include <windows.h>
61 #define HAVE_ISDEBUGGERPRESENT 1
62 #endif
64 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
65 #include <cxxabi.h>
66 #include <dlfcn.h>
67 #include <execinfo.h>
68 #endif
70 #if OS(ANDROID)
71 #include <android/log.h>
72 #endif
74 WTF_ATTRIBUTE_PRINTF(1, 0)
75 static void vprintf_stderr_common(const char* format, va_list args)
77 #if USE(CF) && !OS(WIN)
78 if (strstr(format, "%@")) {
79 CFStringRef cfFormat = CFStringCreateWithCString(NULL, format, kCFStringEncodingUTF8);
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);
92 CFStringGetCString(str, buffer, length, kCFStringEncodingUTF8);
94 #if USE(APPLE_SYSTEM_LOG)
95 asl_log(0, 0, ASL_LEVEL_NOTICE, "%s", buffer);
96 #endif
97 fputs(buffer, stderr);
99 free(buffer);
100 CFRelease(str);
101 CFRelease(cfFormat);
102 return;
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
112 // Fall through to write to stderr in the same manner as other platforms.
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;
120 do {
121 char* buffer = (char*)malloc(size);
123 if (buffer == NULL)
124 break;
126 if (_vsnprintf(buffer, size, format, args) != -1) {
127 OutputDebugStringA(buffer);
128 free(buffer);
129 break;
132 free(buffer);
133 size *= 2;
134 } while (size > 1024);
136 #endif
137 vfprintf(stderr, format, args);
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
145 static void vprintf_stderr_with_prefix(const char* prefix, const char* format, va_list args)
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;
154 vprintf_stderr_common(formatWithPrefix.get(), args);
157 static void vprintf_stderr_with_trailing_newline(const char* format, va_list args)
159 size_t formatLength = strlen(format);
160 if (formatLength && format[formatLength - 1] == '\n') {
161 vprintf_stderr_common(format, args);
162 return;
165 OwnPtr<char[]> formatWithNewline = adoptArrayPtr(new char[formatLength + 2]);
166 memcpy(formatWithNewline.get(), format, formatLength);
167 formatWithNewline[formatLength] = '\n';
168 formatWithNewline[formatLength + 1] = 0;
170 vprintf_stderr_common(formatWithNewline.get(), args);
173 #if COMPILER(CLANG) || (COMPILER(GCC) && GCC_VERSION_AT_LEAST(4, 6, 0))
174 #pragma GCC diagnostic pop
175 #endif
177 WTF_ATTRIBUTE_PRINTF(1, 2)
178 static void printf_stderr_common(const char* format, ...)
180 va_list args;
181 va_start(args, format);
182 vprintf_stderr_common(format, args);
183 va_end(args);
186 static void printCallSite(const char* file, int line, const char* function)
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
198 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
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);
207 void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...)
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);
217 void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion)
219 printf_stderr_common("ARGUMENT BAD: %s, %s\n", argName, assertion);
220 printCallSite(file, line, function);
223 void WTFGetBacktrace(void** stack, int* size)
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;
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
248 void WTFReportBacktrace(int framesToShow)
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;
255 WTFGetBacktrace(samples, &frames);
256 WTFPrintBacktrace(samples + framesToSkip, frames - framesToSkip);
259 FrameToNameScope::FrameToNameScope(void* addr)
260 : m_name(0)
261 , m_cxaDemangled(0)
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
277 FrameToNameScope::~FrameToNameScope()
279 free(m_cxaDemangled);
282 void WTFPrintBacktrace(void** stack, int size)
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]);
294 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
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);
304 void WTFReportError(const char* file, int line, const char* function, const char* format, ...)
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);
314 void WTFLog(WTFLogChannel* channel, const char* format, ...)
316 if (channel->state != WTFLogChannelOn)
317 return;
319 va_list args;
320 va_start(args, format);
321 vprintf_stderr_with_trailing_newline(format, args);
322 va_end(args);
325 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
327 if (channel->state != WTFLogChannelOn)
328 return;
330 va_list args;
331 va_start(args, format);
332 vprintf_stderr_with_trailing_newline(format, args);
333 va_end(args);
335 printCallSite(file, line, function);
338 void WTFLogAlways(const char* format, ...)
340 va_list args;
341 va_start(args, format);
342 vprintf_stderr_with_trailing_newline(format, args);
343 va_end(args);