| 6 | #define BASE_DEBUG_STACK_TRACE_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include <iosfwd> |
| 11 | #include <string> |
| 12 |
| 13 | #include "base/base_export.h" |
| 14 | #include "base/debug/debugging_buildflags.h" |
| 15 | #include "base/macros.h" |
| 16 | #include "build/build_config.h" |
| 17 |
| 18 | #if defined(OS_POSIX) |
| 19 | #if !defined(OS_NACL) |
| 20 | #include <signal.h> |
| 21 | #endif |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 |
| 25 | #if defined(OS_WIN) |
| 26 | struct _EXCEPTION_POINTERS; |
| 27 | struct _CONTEXT; |
| 28 | #endif |
| 29 |
| 30 | namespace base { |
| 31 | namespace debug { |
| 32 |
| 33 | // Enables stack dump to console output on exception and signals. |
| 34 | // When enabled, the process will quit immediately. This is meant to be used in |
| 35 | // unit_tests only! This is not thread-safe: only call from main thread. |
| 36 | // In sandboxed processes, this has to be called before the sandbox is turned |
| 37 | // on. |
| 38 | // Calling this function on Linux opens /proc/self/maps and caches its |
| 39 | // contents. In non-official builds, this function also opens the object files |
| 40 | // that are loaded in memory and caches their file descriptors (this cannot be |
| 41 | // done in official builds because it has security implications). |
| 42 | BASE_EXPORT bool EnableInProcessStackDumping(); |
| 43 |
| 44 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 45 | // Sets a first-chance callback for the stack dump signal handler. This callback |
| 46 | // is called at the beginning of the signal handler to handle special kinds of |
| 47 | // signals, like out-of-bounds memory accesses in WebAssembly (WebAssembly Trap |
| 48 | // Handler). |
| 49 | // {SetStackDumpFirstChanceCallback} returns {true} if the callback |
| 50 | // has been set correctly. It returns {false} if the stack dump signal handler |
| 51 | // has not been registered with the OS, e.g. because of ASAN. |
| 52 | BASE_EXPORT bool SetStackDumpFirstChanceCallback(bool (*handler)(int, |
| 53 | siginfo_t*, |
| 54 | void*)); |
| 55 | #endif |
| 56 |
| 57 | // Returns end of the stack, or 0 if we couldn't get it. |
| 58 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 59 | BASE_EXPORT uintptr_t GetStackEnd(); |
| 60 | #endif |
| 61 |
| 62 | // A stacktrace can be helpful in debugging. For example, you can include a |
| 63 | // stacktrace member in a object (probably around #ifndef NDEBUG) so that you |
| 64 | // can later see where the given object was created from. |
| 65 | class BASE_EXPORT StackTrace { |
| 66 | public: |
| 67 | // Creates a stacktrace from the current location. |
| 68 | StackTrace(); |
| 69 |
| 70 | // Creates a stacktrace from the current location, of up to |count| entries. |
| 71 | // |count| will be limited to at most |kMaxTraces|. |
| 72 | explicit StackTrace(size_t count); |
| 73 |
| 74 | // Creates a stacktrace from an existing array of instruction |
| 75 | // pointers (such as returned by Addresses()). |count| will be |
| 76 | // limited to at most |kMaxTraces|. |
| 77 | StackTrace(const void* const* trace, size_t count); |
| 78 |
| 79 | #if defined(OS_WIN) |
| 80 | // Creates a stacktrace for an exception. |
| 81 | // Note: this function will throw an import not found (StackWalk64) exception |
| 82 | // on system without dbghelp 5.1. |
| 83 | StackTrace(_EXCEPTION_POINTERS* exception_pointers); |
| 84 | StackTrace(const _CONTEXT* context); |
| 85 | #endif |
| 86 |
| 87 | // Copying and assignment are allowed with the default functions. |
| 88 |
| 89 | // Gets an array of instruction pointer values. |*count| will be set to the |
| 90 | // number of elements in the returned array. Addresses()[0] will contain an |
| 91 | // address from the leaf function, and Addresses()[count-1] will contain an |
| 92 | // address from the root function (i.e.; the thread's entry point). |
| 93 | const void* const* Addresses(size_t* count) const; |
| 94 |
| 95 | // Prints the stack trace to stderr. |
| 96 | void Print() const; |
| 97 |
| 98 | // Prints the stack trace to stderr, prepending the given string before |
| 99 | // each output line. |
| 100 | void PrintWithPrefix(const char* prefix_string) const; |
| 101 |
| 102 | #if !defined(__UCLIBC__) & !defined(_AIX) |
| 103 | // Resolves backtrace to symbols and write to stream. |
| 104 | void OutputToStream(std::ostream* os) const; |
| 105 | // Resolves backtrace to symbols and write to stream, with the provided |
| 106 | // prefix string prepended to each line. |
| 107 | void OutputToStreamWithPrefix(std::ostream* os, |
| 108 | const char* prefix_string) const; |
| 109 | #endif |
| 110 |
| 111 | // Resolves backtrace to symbols and returns as string. |
| 112 | std::string ToString() const; |
| 113 |
| 114 | // Resolves backtrace to symbols and returns as string, prepending the |
| 115 | // provided prefix string to each line. |
| 116 | std::string ToStringWithPrefix(const char* prefix_string) const; |
| 117 |
| 118 | private: |
| 119 | #if defined(OS_WIN) |
| 120 | void InitTrace(const _CONTEXT* context_record); |
| 121 | #endif |
| 122 |
| 123 | #if defined(OS_ANDROID) |
| 124 | // TODO(https://crbug.com/925525): Testing indicates that Android has issues |
| 125 | // with a larger value here, so leave Android at 62. |
| 126 | static constexpr int kMaxTraces = 62; |
| 127 | #else |
| 128 | // For other platforms, use 250. This seems reasonable without |
| 129 | // being huge. |
| 130 | static constexpr int kMaxTraces = 250; |
| 131 | #endif |
| 132 |
| 133 | void* trace_[kMaxTraces]; |
| 134 |
| 135 | // The number of valid frames in |trace_|. |
| 136 | size_t count_; |
| 137 | }; |
| 138 |
| 139 | // Forwards to StackTrace::OutputToStream(). |
| 140 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, const StackTrace& s); |
| 141 |
| 142 | // Record a stack trace with up to |count| frames into |trace|. Returns the |
| 143 | // number of frames read. |
| 144 | BASE_EXPORT size_t CollectStackTrace(void** trace, size_t count); |
| 145 |
| 146 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 147 | // Traces the stack by using frame pointers. This function is faster but less |
| 148 | // reliable than StackTrace. It should work for debug and profiling builds, |
| 149 | // but not for release builds (although there are some exceptions). |
| 150 | // |
| 151 | // Writes at most |max_depth| frames (instruction pointers) into |out_trace| |
| 152 | // after skipping |skip_initial| frames. Note that the function itself is not |
| 153 | // added to the trace so |skip_initial| should be 0 in most cases. |
| 154 | // Returns number of frames written. |
| 155 | BASE_EXPORT size_t TraceStackFramePointers(const void** out_trace, |
| 156 | size_t max_depth, |
| 157 | size_t skip_initial); |
| 158 |
| 159 | // Links stack frame |fp| to |parent_fp|, so that during stack unwinding |
| 160 | // TraceStackFramePointers() visits |parent_fp| after visiting |fp|. |
| 161 | // Both frame pointers must come from __builtin_frame_address(). |
| 162 | // Destructor restores original linkage of |fp| to avoid corrupting caller's |
| 163 | // frame register on return. |
| 164 | // |
| 165 | // This class can be used to repair broken stack frame chain in cases |
| 166 | // when execution flow goes into code built without frame pointers: |
| 167 | // |
| 168 | // void DoWork() { |
| 169 | // Call_SomeLibrary(); |
| 170 | // } |
| 171 | // static __thread void* g_saved_fp; |
| 172 | // void Call_SomeLibrary() { |
| 173 | // g_saved_fp = __builtin_frame_address(0); |
| 174 | // some_library_call(...); // indirectly calls SomeLibrary_Callback() |
| 175 | // } |
| 176 | // void SomeLibrary_Callback() { |
| 177 | // ScopedStackFrameLinker linker(__builtin_frame_address(0), g_saved_fp); |
| 178 | // ... |
| 179 | // TraceStackFramePointers(...); |
| 180 | // } |
| 181 | // |
| 182 | // This produces the following trace: |
| 183 | // |
| 184 | // #0 SomeLibrary_Callback() |
| 185 | // #1 <address of the code inside SomeLibrary that called #0> |
| 186 | // #2 DoWork() |
| 187 | // ...rest of the trace... |
| 188 | // |
| 189 | // SomeLibrary doesn't use frame pointers, so when SomeLibrary_Callback() |
| 190 | // is called, stack frame register contains bogus value that becomes callback' |
| 191 | // parent frame address. Without ScopedStackFrameLinker unwinding would've |
| 192 | // stopped at that bogus frame address yielding just two first frames (#0, #1). |
| 193 | // ScopedStackFrameLinker overwrites callback's parent frame address with |
| 194 | // Call_SomeLibrary's frame, so unwinder produces full trace without even |
| 195 | // noticing that stack frame chain was broken. |
| 196 | class BASE_EXPORT ScopedStackFrameLinker { |
| 197 | public: |
| 198 | ScopedStackFrameLinker(void* fp, void* parent_fp); |
| 199 | ~ScopedStackFrameLinker(); |
| 200 |
| 201 | private: |
| 202 | void* fp_; |
| 203 | void* parent_fp_; |
| 204 | void* original_parent_fp_; |
| 205 |
| 206 | DISALLOW_COPY_AND_ASSIGN(ScopedStackFrameLinker); |
| 207 | }; |
| 208 |
| 209 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 210 |
| 211 | namespace internal { |
| 212 |
| 213 | #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 214 | // POSIX doesn't define any async-signal safe function for converting |
| 215 | // an integer to ASCII. We'll have to define our own version. |
| 216 | // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
| 217 | // conversion was successful or NULL otherwise. It never writes more than "sz" |
| 218 | // bytes. Output will be truncated as needed, and a NUL character is always |
| 219 | // appended. |
| 220 | BASE_EXPORT char *itoa_r(intptr_t i, |
| 221 | char *buf, |
| 222 | size_t sz, |
| 223 | int base, |
| 224 | size_t padding); |
| 225 | #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 226 |
| 227 | } // namespace internal |
| 228 |
| 229 | } // namespace debug |
| 230 | } // namespace base |
| 231 |
| 232 | #endif // BASE_DEBUG_STACK_TRACE_H_ |
| 233 |