stack_trace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/memory/raw_ptr.h"
  12. #include "build/build_config.h"
  13. #if BUILDFLAG(IS_POSIX)
  14. #if !BUILDFLAG(IS_NACL)
  15. #include <signal.h>
  16. #endif
  17. #include <unistd.h>
  18. #endif
  19. #if BUILDFLAG(IS_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 BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_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 BUILDFLAG(IS_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. // Returns true if this current test environment is expected to have
  73. // symbolized frames when printing a stack trace.
  74. static bool WillSymbolizeToStreamForTesting();
  75. // Copying and assignment are allowed with the default functions.
  76. // Gets an array of instruction pointer values. |*count| will be set to the
  77. // number of elements in the returned array. Addresses()[0] will contain an
  78. // address from the leaf function, and Addresses()[count-1] will contain an
  79. // address from the root function (i.e.; the thread's entry point).
  80. const void* const* Addresses(size_t* count) const;
  81. // Prints the stack trace to stderr.
  82. void Print() const;
  83. // Prints the stack trace to stderr, prepending the given string before
  84. // each output line.
  85. void PrintWithPrefix(const char* prefix_string) const;
  86. #if !defined(__UCLIBC__) && !defined(_AIX)
  87. // Resolves backtrace to symbols and write to stream.
  88. void OutputToStream(std::ostream* os) const;
  89. // Resolves backtrace to symbols and write to stream, with the provided
  90. // prefix string prepended to each line.
  91. void OutputToStreamWithPrefix(std::ostream* os,
  92. const char* prefix_string) const;
  93. #endif
  94. // Resolves backtrace to symbols and returns as string.
  95. std::string ToString() const;
  96. // Resolves backtrace to symbols and returns as string, prepending the
  97. // provided prefix string to each line.
  98. std::string ToStringWithPrefix(const char* prefix_string) const;
  99. private:
  100. #if BUILDFLAG(IS_WIN)
  101. void InitTrace(const _CONTEXT* context_record);
  102. #endif
  103. #if BUILDFLAG(IS_ANDROID)
  104. // TODO(https://crbug.com/925525): Testing indicates that Android has issues
  105. // with a larger value here, so leave Android at 62.
  106. static constexpr int kMaxTraces = 62;
  107. #else
  108. // For other platforms, use 250. This seems reasonable without
  109. // being huge.
  110. static constexpr int kMaxTraces = 250;
  111. #endif
  112. void* trace_[kMaxTraces];
  113. // The number of valid frames in |trace_|.
  114. size_t count_;
  115. };
  116. // Forwards to StackTrace::OutputToStream().
  117. BASE_EXPORT std::ostream& operator<<(std::ostream& os, const StackTrace& s);
  118. // Record a stack trace with up to |count| frames into |trace|. Returns the
  119. // number of frames read.
  120. BASE_EXPORT size_t CollectStackTrace(void** trace, size_t count);
  121. #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  122. // For stack scanning to be efficient it's very important for the thread to
  123. // be started by Chrome. In that case we naturally terminate unwinding once
  124. // we reach the origin of the stack (i.e. GetStackEnd()). If the thread is
  125. // not started by Chrome (e.g. Android's main thread), then we end up always
  126. // scanning area at the origin of the stack, wasting time and not finding any
  127. // frames (since Android libraries don't have frame pointers). Scanning is not
  128. // enabled on other posix platforms due to legacy reasons.
  129. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  130. constexpr bool kEnableScanningByDefault = true;
  131. #else
  132. constexpr bool kEnableScanningByDefault = false;
  133. #endif
  134. // Traces the stack by using frame pointers. This function is faster but less
  135. // reliable than StackTrace. It should work for debug and profiling builds,
  136. // but not for release builds (although there are some exceptions).
  137. //
  138. // Writes at most |max_depth| frames (instruction pointers) into |out_trace|
  139. // after skipping |skip_initial| frames. Note that the function itself is not
  140. // added to the trace so |skip_initial| should be 0 in most cases.
  141. // Returns number of frames written. |enable_scanning| enables scanning on
  142. // platforms that do not enable scanning by default.
  143. BASE_EXPORT size_t
  144. TraceStackFramePointers(const void** out_trace,
  145. size_t max_depth,
  146. size_t skip_initial,
  147. bool enable_scanning = kEnableScanningByDefault);
  148. // Same as above function, but allows to pass in frame pointer and stack end
  149. // address for unwinding. This is useful when unwinding based on a copied stack
  150. // segment. Note that the client has to take care of rewriting all the pointers
  151. // in the stack pointing within the stack to point to the copied addresses.
  152. BASE_EXPORT size_t TraceStackFramePointersFromBuffer(
  153. uintptr_t fp,
  154. uintptr_t stack_end,
  155. const void** out_trace,
  156. size_t max_depth,
  157. size_t skip_initial,
  158. bool enable_scanning = kEnableScanningByDefault);
  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(const ScopedStackFrameLinker&) = delete;
  200. ScopedStackFrameLinker& operator=(const ScopedStackFrameLinker&) = delete;
  201. ~ScopedStackFrameLinker();
  202. private:
  203. raw_ptr<void> fp_;
  204. raw_ptr<void> parent_fp_;
  205. raw_ptr<void> original_parent_fp_;
  206. };
  207. #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  208. namespace internal {
  209. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
  210. // POSIX doesn't define any async-signal safe function for converting
  211. // an integer to ASCII. We'll have to define our own version.
  212. // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
  213. // conversion was successful or NULL otherwise. It never writes more than "sz"
  214. // bytes. Output will be truncated as needed, and a NUL character is always
  215. // appended.
  216. BASE_EXPORT char *itoa_r(intptr_t i,
  217. char *buf,
  218. size_t sz,
  219. int base,
  220. size_t padding);
  221. #endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
  222. } // namespace internal
  223. } // namespace debug
  224. } // namespace base
  225. #endif // BASE_DEBUG_STACK_TRACE_H_