1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 * Copyright (C) 2017 Yusuke Suzuki <[email protected]>
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 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 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#include "config.h"
28#include <wtf/StackTrace.h>
29
30#include <wtf/Assertions.h>
31#include <wtf/PrintStream.h>
32
33#if HAVE(BACKTRACE_SYMBOLS) || HAVE(BACKTRACE)
34#include <execinfo.h>
35#endif
36
37#if HAVE(DLADDR)
38#include <cxxabi.h>
39#include <dlfcn.h>
40#endif
41
42#if OS(WINDOWS)
43#include <windows.h>
44#include <wtf/win/DbgHelperWin.h>
45#endif
46
47void WTFGetBacktrace(void** stack, int* size)
48{
49#if HAVE(BACKTRACE)
50 *size = backtrace(stack, *size);
51#elif OS(WINDOWS)
52 *size = RtlCaptureStackBackTrace(0, *size, stack, 0);
53#else
54 UNUSED_PARAM(stack);
55 *size = 0;
56#endif
57}
58
59namespace WTF {
60
61ALWAYS_INLINE size_t StackTrace::instanceSize(int capacity)
62{
63 ASSERT(capacity >= 1);
64 return sizeof(StackTrace) + (capacity - 1) * sizeof(void*);
65}
66
67std::unique_ptr<StackTrace> StackTrace::captureStackTrace(int maxFrames, int framesToSkip)
68{
69 maxFrames = std::max(1, maxFrames);
70 size_t sizeToAllocate = instanceSize(maxFrames);
71 std::unique_ptr<StackTrace> trace(new (NotNull, fastMalloc(sizeToAllocate)) StackTrace());
72
73 // Skip 2 additional frames i.e. StackTrace::captureStackTrace and WTFGetBacktrace.
74 framesToSkip += 2;
75 int numberOfFrames = maxFrames + framesToSkip;
76
77 WTFGetBacktrace(&trace->m_skippedFrame0, &numberOfFrames);
78 if (numberOfFrames) {
79 RELEASE_ASSERT(numberOfFrames >= framesToSkip);
80 trace->m_size = numberOfFrames - framesToSkip;
81 } else
82 trace->m_size = 0;
83
84 trace->m_capacity = maxFrames;
85
86 return trace;
87}
88
89auto StackTrace::demangle(void* pc) -> Optional<DemangleEntry>
90{
91#if HAVE(DLADDR)
92 const char* mangledName = nullptr;
93 const char* cxaDemangled = nullptr;
94 Dl_info info;
95 if (dladdr(pc, &info) && info.dli_sname)
96 mangledName = info.dli_sname;
97 if (mangledName) {
98 int status = 0;
99 cxaDemangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
100 UNUSED_PARAM(status);
101 }
102 if (mangledName || cxaDemangled)
103 return DemangleEntry { mangledName, cxaDemangled };
104#else
105 UNUSED_PARAM(pc);
106#endif
107 return WTF::nullopt;
108}
109
110void StackTrace::dump(PrintStream& out, const char* indentString) const
111{
112 const auto* stack = this->stack();
113#if HAVE(BACKTRACE_SYMBOLS)
114 char** symbols = backtrace_symbols(stack, m_size);
115 if (!symbols)
116 return;
117#elif OS(WINDOWS)
118 HANDLE hProc = GetCurrentProcess();
119 uint8_t symbolData[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)] = { 0 };
120 auto symbolInfo = reinterpret_cast<SYMBOL_INFO*>(symbolData);
121
122 symbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
123 symbolInfo->MaxNameLen = MAX_SYM_NAME;
124#endif
125
126 if (!indentString)
127 indentString = "";
128 for (int i = 0; i < m_size; ++i) {
129 const char* mangledName = nullptr;
130 const char* cxaDemangled = nullptr;
131#if HAVE(BACKTRACE_SYMBOLS)
132 mangledName = symbols[i];
133#elif HAVE(DLADDR)
134 auto demangled = demangle(stack[i]);
135 if (demangled) {
136 mangledName = demangled->mangledName();
137 cxaDemangled = demangled->demangledName();
138 }
139#elif OS(WINDOWS)
140 if (DbgHelper::SymFromAddress(hProc, reinterpret_cast<DWORD64>(stack[i]), 0, symbolInfo))
141 mangledName = symbolInfo->Name;
142#endif
143 const int frameNumber = i + 1;
144 if (mangledName || cxaDemangled)
145 out.printf("%s%-3d %p %s\n", indentString, frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
146 else
147 out.printf("%s%-3d %p\n", indentString, frameNumber, stack[i]);
148 }
149
150#if HAVE(BACKTRACE_SYMBOLS)
151 free(symbols);
152#endif
153}
154
155} // namespace WTF
156