stack_trace.h 9.7 KB

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