examine_stack.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Copyright 2018 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "y_absl/debugging/internal/examine_stack.h"
  17. #ifndef _WIN32
  18. #include <unistd.h>
  19. #endif
  20. #include "y_absl/base/config.h"
  21. #ifdef Y_ABSL_HAVE_MMAP
  22. #include <sys/mman.h>
  23. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  24. #define MAP_ANONYMOUS MAP_ANON
  25. #endif
  26. #endif
  27. #if defined(__linux__) || defined(__APPLE__)
  28. #include <sys/ucontext.h>
  29. #endif
  30. #include <csignal>
  31. #include <cstdio>
  32. #include "y_absl/base/attributes.h"
  33. #include "y_absl/base/internal/raw_logging.h"
  34. #include "y_absl/base/macros.h"
  35. #include "y_absl/debugging/stacktrace.h"
  36. #include "y_absl/debugging/symbolize.h"
  37. namespace y_absl {
  38. Y_ABSL_NAMESPACE_BEGIN
  39. namespace debugging_internal {
  40. namespace {
  41. constexpr int kDefaultDumpStackFramesLimit = 64;
  42. // The %p field width for printf() functions is two characters per byte,
  43. // and two extra for the leading "0x".
  44. constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
  45. Y_ABSL_CONST_INIT SymbolizeUrlEmitter debug_stack_trace_hook = nullptr;
  46. // Async-signal safe mmap allocator.
  47. void* Allocate(size_t num_bytes) {
  48. #ifdef Y_ABSL_HAVE_MMAP
  49. void* p = ::mmap(nullptr, num_bytes, PROT_READ | PROT_WRITE,
  50. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  51. return p == MAP_FAILED ? nullptr : p;
  52. #else
  53. (void)num_bytes;
  54. return nullptr;
  55. #endif // Y_ABSL_HAVE_MMAP
  56. }
  57. void Deallocate(void* p, size_t size) {
  58. #ifdef Y_ABSL_HAVE_MMAP
  59. ::munmap(p, size);
  60. #else
  61. (void)p;
  62. (void)size;
  63. #endif // Y_ABSL_HAVE_MMAP
  64. }
  65. // Print a program counter only.
  66. void DumpPC(OutputWriter* writer, void* writer_arg, void* const pc,
  67. const char* const prefix) {
  68. char buf[100];
  69. snprintf(buf, sizeof(buf), "%s@ %*p\n", prefix, kPrintfPointerFieldWidth, pc);
  70. writer(buf, writer_arg);
  71. }
  72. // Print a program counter and the corresponding stack frame size.
  73. void DumpPCAndFrameSize(OutputWriter* writer, void* writer_arg, void* const pc,
  74. int framesize, const char* const prefix) {
  75. char buf[100];
  76. if (framesize <= 0) {
  77. snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n", prefix,
  78. kPrintfPointerFieldWidth, pc);
  79. } else {
  80. snprintf(buf, sizeof(buf), "%s@ %*p %9d\n", prefix,
  81. kPrintfPointerFieldWidth, pc, framesize);
  82. }
  83. writer(buf, writer_arg);
  84. }
  85. // Print a program counter and the corresponding symbol.
  86. void DumpPCAndSymbol(OutputWriter* writer, void* writer_arg, void* const pc,
  87. const char* const prefix) {
  88. char tmp[1024];
  89. const char* symbol = "(unknown)";
  90. // Symbolizes the previous address of pc because pc may be in the
  91. // next function. The overrun happens when the function ends with
  92. // a call to a function annotated noreturn (e.g. CHECK).
  93. // If symbolization of pc-1 fails, also try pc on the off-chance
  94. // that we crashed on the first instruction of a function (that
  95. // actually happens very often for e.g. __restore_rt).
  96. const uintptr_t prev_pc = reinterpret_cast<uintptr_t>(pc) - 1;
  97. if (y_absl::Symbolize(reinterpret_cast<const char*>(prev_pc), tmp,
  98. sizeof(tmp)) ||
  99. y_absl::Symbolize(pc, tmp, sizeof(tmp))) {
  100. symbol = tmp;
  101. }
  102. char buf[1024];
  103. snprintf(buf, sizeof(buf), "%s@ %*p %s\n", prefix, kPrintfPointerFieldWidth,
  104. pc, symbol);
  105. writer(buf, writer_arg);
  106. }
  107. // Print a program counter, its stack frame size, and its symbol name.
  108. // Note that there is a separate symbolize_pc argument. Return addresses may be
  109. // at the end of the function, and this allows the caller to back up from pc if
  110. // appropriate.
  111. void DumpPCAndFrameSizeAndSymbol(OutputWriter* writer, void* writer_arg,
  112. void* const pc, void* const symbolize_pc,
  113. int framesize, const char* const prefix) {
  114. char tmp[1024];
  115. const char* symbol = "(unknown)";
  116. if (y_absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
  117. symbol = tmp;
  118. }
  119. char buf[1024];
  120. if (framesize <= 0) {
  121. snprintf(buf, sizeof(buf), "%s@ %*p (unknown) %s\n", prefix,
  122. kPrintfPointerFieldWidth, pc, symbol);
  123. } else {
  124. snprintf(buf, sizeof(buf), "%s@ %*p %9d %s\n", prefix,
  125. kPrintfPointerFieldWidth, pc, framesize, symbol);
  126. }
  127. writer(buf, writer_arg);
  128. }
  129. } // namespace
  130. void RegisterDebugStackTraceHook(SymbolizeUrlEmitter hook) {
  131. debug_stack_trace_hook = hook;
  132. }
  133. SymbolizeUrlEmitter GetDebugStackTraceHook() { return debug_stack_trace_hook; }
  134. // Returns the program counter from signal context, nullptr if
  135. // unknown. vuc is a ucontext_t*. We use void* to avoid the use of
  136. // ucontext_t on non-POSIX systems.
  137. void* GetProgramCounter(void* const vuc) {
  138. #ifdef __linux__
  139. if (vuc != nullptr) {
  140. ucontext_t* context = reinterpret_cast<ucontext_t*>(vuc);
  141. #if defined(__aarch64__)
  142. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  143. #elif defined(__alpha__)
  144. return reinterpret_cast<void*>(context->uc_mcontext.sc_pc);
  145. #elif defined(__arm__)
  146. return reinterpret_cast<void*>(context->uc_mcontext.arm_pc);
  147. #elif defined(__hppa__)
  148. return reinterpret_cast<void*>(context->uc_mcontext.sc_iaoq[0]);
  149. #elif defined(__i386__)
  150. if (14 < Y_ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
  151. return reinterpret_cast<void*>(context->uc_mcontext.gregs[14]);
  152. #elif defined(__ia64__)
  153. return reinterpret_cast<void*>(context->uc_mcontext.sc_ip);
  154. #elif defined(__m68k__)
  155. return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
  156. #elif defined(__mips__)
  157. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  158. #elif defined(__powerpc64__)
  159. return reinterpret_cast<void*>(context->uc_mcontext.gp_regs[32]);
  160. #elif defined(__powerpc__)
  161. return reinterpret_cast<void*>(context->uc_mcontext.uc_regs->gregs[32]);
  162. #elif defined(__riscv)
  163. return reinterpret_cast<void*>(context->uc_mcontext.__gregs[REG_PC]);
  164. #elif defined(__s390__) && !defined(__s390x__)
  165. return reinterpret_cast<void*>(context->uc_mcontext.psw.addr & 0x7fffffff);
  166. #elif defined(__s390__) && defined(__s390x__)
  167. return reinterpret_cast<void*>(context->uc_mcontext.psw.addr);
  168. #elif defined(__sh__)
  169. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  170. #elif defined(__sparc__) && !defined(__arch64__)
  171. return reinterpret_cast<void*>(context->uc_mcontext.gregs[19]);
  172. #elif defined(__sparc__) && defined(__arch64__)
  173. return reinterpret_cast<void*>(context->uc_mcontext.mc_gregs[19]);
  174. #elif defined(__x86_64__)
  175. if (16 < Y_ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
  176. return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
  177. #elif defined(__e2k__)
  178. return reinterpret_cast<void*>(context->uc_mcontext.cr0_hi);
  179. #elif defined(__loongarch__)
  180. return reinterpret_cast<void*>(context->uc_mcontext.__pc);
  181. #else
  182. #error "Undefined Architecture."
  183. #endif
  184. }
  185. #elif defined(__APPLE__)
  186. if (vuc != nullptr) {
  187. ucontext_t* signal_ucontext = reinterpret_cast<ucontext_t*>(vuc);
  188. #if defined(__aarch64__)
  189. return reinterpret_cast<void*>(
  190. __darwin_arm_thread_state64_get_pc(signal_ucontext->uc_mcontext->__ss));
  191. #elif defined(__arm__)
  192. #if __DARWIN_UNIX03
  193. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__pc);
  194. #else
  195. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.pc);
  196. #endif
  197. #elif defined(__i386__)
  198. #if __DARWIN_UNIX03
  199. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__eip);
  200. #else
  201. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.eip);
  202. #endif
  203. #elif defined(__x86_64__)
  204. #if __DARWIN_UNIX03
  205. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__rip);
  206. #else
  207. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.rip);
  208. #endif
  209. #endif
  210. }
  211. #elif defined(__akaros__)
  212. auto* ctx = reinterpret_cast<struct user_context*>(vuc);
  213. return reinterpret_cast<void*>(get_user_ctx_pc(ctx));
  214. #endif
  215. static_cast<void>(vuc);
  216. return nullptr;
  217. }
  218. void DumpPCAndFrameSizesAndStackTrace(void* const pc, void* const stack[],
  219. int frame_sizes[], int depth,
  220. int min_dropped_frames,
  221. bool symbolize_stacktrace,
  222. OutputWriter* writer, void* writer_arg) {
  223. if (pc != nullptr) {
  224. // We don't know the stack frame size for PC, use 0.
  225. if (symbolize_stacktrace) {
  226. DumpPCAndFrameSizeAndSymbol(writer, writer_arg, pc, pc, 0, "PC: ");
  227. } else {
  228. DumpPCAndFrameSize(writer, writer_arg, pc, 0, "PC: ");
  229. }
  230. }
  231. for (int i = 0; i < depth; i++) {
  232. if (symbolize_stacktrace) {
  233. // Pass the previous address of pc as the symbol address because pc is a
  234. // return address, and an overrun may occur when the function ends with a
  235. // call to a function annotated noreturn (e.g. CHECK). Note that we don't
  236. // do this for pc above, as the adjustment is only correct for return
  237. // addresses.
  238. DumpPCAndFrameSizeAndSymbol(writer, writer_arg, stack[i],
  239. reinterpret_cast<char*>(stack[i]) - 1,
  240. frame_sizes[i], " ");
  241. } else {
  242. DumpPCAndFrameSize(writer, writer_arg, stack[i], frame_sizes[i], " ");
  243. }
  244. }
  245. if (min_dropped_frames > 0) {
  246. char buf[100];
  247. snprintf(buf, sizeof(buf), " @ ... and at least %d more frames\n",
  248. min_dropped_frames);
  249. writer(buf, writer_arg);
  250. }
  251. }
  252. // Dump current stack trace as directed by writer.
  253. // Make sure this function is not inlined to avoid skipping too many top frames.
  254. Y_ABSL_ATTRIBUTE_NOINLINE
  255. void DumpStackTrace(int min_dropped_frames, int max_num_frames,
  256. bool symbolize_stacktrace, OutputWriter* writer,
  257. void* writer_arg) {
  258. // Print stack trace
  259. void* stack_buf[kDefaultDumpStackFramesLimit];
  260. void** stack = stack_buf;
  261. int num_stack = kDefaultDumpStackFramesLimit;
  262. size_t allocated_bytes = 0;
  263. if (num_stack >= max_num_frames) {
  264. // User requested fewer frames than we already have space for.
  265. num_stack = max_num_frames;
  266. } else {
  267. const size_t needed_bytes =
  268. static_cast<size_t>(max_num_frames) * sizeof(stack[0]);
  269. void* p = Allocate(needed_bytes);
  270. if (p != nullptr) { // We got the space.
  271. num_stack = max_num_frames;
  272. stack = reinterpret_cast<void**>(p);
  273. allocated_bytes = needed_bytes;
  274. }
  275. }
  276. int depth = y_absl::GetStackTrace(stack, num_stack, min_dropped_frames + 1);
  277. for (int i = 0; i < depth; i++) {
  278. if (symbolize_stacktrace) {
  279. DumpPCAndSymbol(writer, writer_arg, stack[static_cast<size_t>(i)],
  280. " ");
  281. } else {
  282. DumpPC(writer, writer_arg, stack[static_cast<size_t>(i)], " ");
  283. }
  284. }
  285. auto hook = GetDebugStackTraceHook();
  286. if (hook != nullptr) {
  287. (*hook)(stack, depth, writer, writer_arg);
  288. }
  289. if (allocated_bytes != 0) Deallocate(stack, allocated_bytes);
  290. }
  291. } // namespace debugging_internal
  292. Y_ABSL_NAMESPACE_END
  293. } // namespace y_absl