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 | #pragma once |
28 | |
29 | #include <wtf/Optional.h> |
30 | #include <wtf/SystemFree.h> |
31 | |
32 | namespace WTF { |
33 | |
34 | class PrintStream; |
35 | |
36 | class StackTrace { |
37 | WTF_MAKE_FAST_ALLOCATED; |
38 | public: |
39 | WTF_EXPORT_PRIVATE static std::unique_ptr<StackTrace> captureStackTrace(int maxFrames, int framesToSkip = 0); |
40 | |
41 | // Borrowed stack trace. |
42 | StackTrace(void** stack, int size) |
43 | : m_size(size) |
44 | , m_capacity(0) |
45 | , m_borrowedStack(stack) |
46 | { } |
47 | |
48 | int size() const { return m_size; } |
49 | void* const * stack() const |
50 | { |
51 | if (!m_capacity) |
52 | return m_borrowedStack; |
53 | return m_stack; |
54 | } |
55 | |
56 | class DemangleEntry { |
57 | WTF_MAKE_FAST_ALLOCATED; |
58 | public: |
59 | friend class StackTrace; |
60 | const char* mangledName() const { return m_mangledName; } |
61 | const char* demangledName() const { return m_demangledName.get(); } |
62 | |
63 | private: |
64 | DemangleEntry(const char* mangledName, const char* demangledName) |
65 | : m_mangledName(mangledName) |
66 | , m_demangledName(demangledName) |
67 | { } |
68 | |
69 | const char* m_mangledName { nullptr }; |
70 | std::unique_ptr<const char[], SystemFree<const char[]>> m_demangledName; |
71 | }; |
72 | |
73 | WTF_EXPORT_PRIVATE static Optional<DemangleEntry> demangle(void*); |
74 | |
75 | WTF_EXPORT_PRIVATE void dump(PrintStream&, const char* indentString = nullptr) const; |
76 | |
77 | private: |
78 | inline static size_t instanceSize(int capacity); |
79 | |
80 | StackTrace() |
81 | : m_size(0) |
82 | { } |
83 | |
84 | // We structure the top fields this way because the underlying stack capture |
85 | // facility will capture from the top of the stack, and we'll need to skip the |
86 | // top 2 frame which is of no interest. Setting up the fields layout this way |
87 | // allows us to capture the stack in place and minimize space wastage. |
88 | union { |
89 | struct { |
90 | int m_size; |
91 | int m_capacity; |
92 | }; |
93 | struct { |
94 | void* m_skippedFrame0; |
95 | void* m_skippedFrame1; |
96 | }; |
97 | }; |
98 | union { |
99 | void** m_borrowedStack; |
100 | void* m_stack[1]; |
101 | }; |
102 | }; |
103 | |
104 | } // namespace WTF |
105 | |
106 | using WTF::StackTrace; |
107 | |