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
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"
35 #include "Assertions.h"
39 #include "PassOwnPtr.h"
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
61 #define HAVE_ISDEBUGGERPRESENT 1
64 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
71 #include <android/log.h>
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);
82 #pragma clang diagnostic push
83 #pragma clang diagnostic ignored "-Wformat-nonliteral"
85 CFStringRef str = CFStringCreateWithFormatAndArguments(NULL, NULL, cfFormat, args);
87 #pragma clang diagnostic pop
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);
97 fputs(buffer, stderr);
105 #if USE(APPLE_SYSTEM_LOG)
107 va_copy(copyOfArgs, args);
108 asl_vlog(0, 0, ASL_LEVEL_NOTICE, format, copyOfArgs);
112 // Fall through to write to stderr in the same manner as other platforms.
115 __android_log_vprint(ANDROID_LOG_WARN, "WebKit", format, args);
116 #elif HAVE(ISDEBUGGERPRESENT)
117 if (IsDebuggerPresent()) {
121 char* buffer = (char*)malloc(size);
126 if (_vsnprintf(buffer, size, format, args) != -1) {
127 OutputDebugStringA(buffer);
134 } while (size > 1024);
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"
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);
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
177 WTF_ATTRIBUTE_PRINTF(1, 2)
178 static void printf_stderr_common(const char* format, ...)
181 va_start(args, format);
182 vprintf_stderr_common(format, 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);
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);
198 void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion)
201 printf_stderr_common("ASSERTION FAILED: %s\n", assertion);
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, ...)
210 va_start(args, format);
211 vprintf_stderr_with_prefix("ASSERTION FAILED: ", format, 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);
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");
237 RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
238 ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
239 if (captureStackBackTraceFunc)
240 *size = captureStackBackTraceFunc(0, *size, stack, 0);
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)
263 #if OS(MACOSX) || (OS(LINUX) && !defined(__UCLIBC__))
265 if (!dladdr(addr, &info) || !info.dli_sname)
267 const char* mangledName = info.dli_sname;
268 if ((m_cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0)))
269 m_name = m_cxaDemangled;
271 m_name = mangledName;
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());
290 printf_stderr_common("%-3d %p\n", frameNumber, stack[i]);
294 void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...)
297 va_start(args, format);
298 vprintf_stderr_with_prefix("FATAL ERROR: ", format, 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, ...)
307 va_start(args, format);
308 vprintf_stderr_with_prefix("ERROR: ", format, args);
310 printf_stderr_common("\n");
311 printCallSite(file, line, function);
314 void WTFLog(WTFLogChannel* channel, const char* format, ...)
316 if (channel->state != WTFLogChannelOn)
320 va_start(args, format);
321 vprintf_stderr_with_trailing_newline(format, args);
325 void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel* channel, const char* format, ...)
327 if (channel->state != WTFLogChannelOn)
331 va_start(args, format);
332 vprintf_stderr_with_trailing_newline(format, args);
335 printCallSite(file, line, function);
338 void WTFLogAlways(const char* format, ...)
341 va_start(args, format);
342 vprintf_stderr_with_trailing_newline(format, args);