| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com> |
| 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 | public: |
| 58 | friend class StackTrace; |
| 59 | const char* mangledName() const { return m_mangledName; } |
| 60 | const char* demangledName() const { return m_demangledName.get(); } |
| 61 | |
| 62 | private: |
| 63 | DemangleEntry(const char* mangledName, const char* demangledName) |
| 64 | : m_mangledName(mangledName) |
| 65 | , m_demangledName(demangledName) |
| 66 | { } |
| 67 | |
| 68 | const char* m_mangledName { nullptr }; |
| 69 | std::unique_ptr<const char[], SystemFree<const char[]>> m_demangledName; |
| 70 | }; |
| 71 | |
| 72 | WTF_EXPORT_PRIVATE static Optional<DemangleEntry> demangle(void*); |
| 73 | |
| 74 | WTF_EXPORT_PRIVATE void dump(PrintStream&, const char* indentString = nullptr) const; |
| 75 | |
| 76 | private: |
| 77 | inline static size_t instanceSize(int capacity); |
| 78 | |
| 79 | StackTrace() |
| 80 | : m_size(0) |
| 81 | { } |
| 82 | |
| 83 | // We structure the top fields this way because the underlying stack capture |
| 84 | // facility will capture from the top of the stack, and we'll need to skip the |
| 85 | // top 2 frame which is of no interest. Setting up the fields layout this way |
| 86 | // allows us to capture the stack in place and minimize space wastage. |
| 87 | union { |
| 88 | struct { |
| 89 | int m_size; |
| 90 | int m_capacity; |
| 91 | }; |
| 92 | struct { |
| 93 | void* m_skippedFrame0; |
| 94 | void* m_skippedFrame1; |
| 95 | }; |
| 96 | }; |
| 97 | union { |
| 98 | void** m_borrowedStack; |
| 99 | void* m_stack[1]; |
| 100 | }; |
| 101 | }; |
| 102 | |
| 103 | } // namespace WTF |
| 104 | |
| 105 | using WTF::StackTrace; |
| 106 | |