examine_stack.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "absl/debugging/internal/examine_stack.h"
  17. #ifndef _WIN32
  18. #include <unistd.h>
  19. #endif
  20. #ifdef __APPLE__
  21. #include <sys/ucontext.h>
  22. #endif
  23. #include <csignal>
  24. #include <cstdio>
  25. #include "absl/base/attributes.h"
  26. #include "absl/base/internal/raw_logging.h"
  27. #include "absl/base/macros.h"
  28. #include "absl/debugging/stacktrace.h"
  29. #include "absl/debugging/symbolize.h"
  30. namespace absl {
  31. ABSL_NAMESPACE_BEGIN
  32. namespace debugging_internal {
  33. // Returns the program counter from signal context, nullptr if
  34. // unknown. vuc is a ucontext_t*. We use void* to avoid the use of
  35. // ucontext_t on non-POSIX systems.
  36. void* GetProgramCounter(void* vuc) {
  37. #ifdef __linux__
  38. if (vuc != nullptr) {
  39. ucontext_t* context = reinterpret_cast<ucontext_t*>(vuc);
  40. #if defined(__aarch64__)
  41. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  42. #elif defined(__alpha__)
  43. return reinterpret_cast<void*>(context->uc_mcontext.sc_pc);
  44. #elif defined(__arm__)
  45. return reinterpret_cast<void*>(context->uc_mcontext.arm_pc);
  46. #elif defined(__hppa__)
  47. return reinterpret_cast<void*>(context->uc_mcontext.sc_iaoq[0]);
  48. #elif defined(__i386__)
  49. if (14 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
  50. return reinterpret_cast<void*>(context->uc_mcontext.gregs[14]);
  51. #elif defined(__ia64__)
  52. return reinterpret_cast<void*>(context->uc_mcontext.sc_ip);
  53. #elif defined(__m68k__)
  54. return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
  55. #elif defined(__mips__)
  56. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  57. #elif defined(__powerpc64__)
  58. return reinterpret_cast<void*>(context->uc_mcontext.gp_regs[32]);
  59. #elif defined(__powerpc__)
  60. return reinterpret_cast<void*>(context->uc_mcontext.uc_regs->gregs[32]);
  61. #elif defined(__riscv)
  62. return reinterpret_cast<void*>(context->uc_mcontext.__gregs[REG_PC]);
  63. #elif defined(__s390__) && !defined(__s390x__)
  64. return reinterpret_cast<void*>(context->uc_mcontext.psw.addr & 0x7fffffff);
  65. #elif defined(__s390__) && defined(__s390x__)
  66. return reinterpret_cast<void*>(context->uc_mcontext.psw.addr);
  67. #elif defined(__sh__)
  68. return reinterpret_cast<void*>(context->uc_mcontext.pc);
  69. #elif defined(__sparc__) && !defined(__arch64__)
  70. return reinterpret_cast<void*>(context->uc_mcontext.gregs[19]);
  71. #elif defined(__sparc__) && defined(__arch64__)
  72. return reinterpret_cast<void*>(context->uc_mcontext.mc_gregs[19]);
  73. #elif defined(__x86_64__)
  74. if (16 < ABSL_ARRAYSIZE(context->uc_mcontext.gregs))
  75. return reinterpret_cast<void*>(context->uc_mcontext.gregs[16]);
  76. #elif defined(__e2k__)
  77. return reinterpret_cast<void*>(context->uc_mcontext.cr0_hi);
  78. #else
  79. #error "Undefined Architecture."
  80. #endif
  81. }
  82. #elif defined(__APPLE__)
  83. if (vuc != nullptr) {
  84. ucontext_t* signal_ucontext = reinterpret_cast<ucontext_t*>(vuc);
  85. #if defined(__aarch64__)
  86. return reinterpret_cast<void*>(
  87. __darwin_arm_thread_state64_get_pc(signal_ucontext->uc_mcontext->__ss));
  88. #elif defined(__arm__)
  89. #if __DARWIN_UNIX03
  90. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__pc);
  91. #else
  92. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.pc);
  93. #endif
  94. #elif defined(__i386__)
  95. #if __DARWIN_UNIX03
  96. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__eip);
  97. #else
  98. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.eip);
  99. #endif
  100. #elif defined(__x86_64__)
  101. #if __DARWIN_UNIX03
  102. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->__ss.__rip);
  103. #else
  104. return reinterpret_cast<void*>(signal_ucontext->uc_mcontext->ss.rip);
  105. #endif
  106. #endif
  107. }
  108. #elif defined(__akaros__)
  109. auto* ctx = reinterpret_cast<struct user_context*>(vuc);
  110. return reinterpret_cast<void*>(get_user_ctx_pc(ctx));
  111. #endif
  112. static_cast<void>(vuc);
  113. return nullptr;
  114. }
  115. // The %p field width for printf() functions is two characters per byte,
  116. // and two extra for the leading "0x".
  117. static constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
  118. // Print a program counter, its stack frame size, and its symbol name.
  119. // Note that there is a separate symbolize_pc argument. Return addresses may be
  120. // at the end of the function, and this allows the caller to back up from pc if
  121. // appropriate.
  122. static void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*),
  123. void* writerfn_arg, void* pc,
  124. void* symbolize_pc, int framesize,
  125. const char* const prefix) {
  126. char tmp[1024];
  127. const char* symbol = "(unknown)";
  128. if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
  129. symbol = tmp;
  130. }
  131. char buf[1024];
  132. if (framesize <= 0) {
  133. snprintf(buf, sizeof(buf), "%s@ %*p (unknown) %s\n", prefix,
  134. kPrintfPointerFieldWidth, pc, symbol);
  135. } else {
  136. snprintf(buf, sizeof(buf), "%s@ %*p %9d %s\n", prefix,
  137. kPrintfPointerFieldWidth, pc, framesize, symbol);
  138. }
  139. writerfn(buf, writerfn_arg);
  140. }
  141. // Print a program counter and the corresponding stack frame size.
  142. static void DumpPCAndFrameSize(void (*writerfn)(const char*, void*),
  143. void* writerfn_arg, void* pc, int framesize,
  144. const char* const prefix) {
  145. char buf[100];
  146. if (framesize <= 0) {
  147. snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n", prefix,
  148. kPrintfPointerFieldWidth, pc);
  149. } else {
  150. snprintf(buf, sizeof(buf), "%s@ %*p %9d\n", prefix,
  151. kPrintfPointerFieldWidth, pc, framesize);
  152. }
  153. writerfn(buf, writerfn_arg);
  154. }
  155. void DumpPCAndFrameSizesAndStackTrace(
  156. void* pc, void* const stack[], int frame_sizes[], int depth,
  157. int min_dropped_frames, bool symbolize_stacktrace,
  158. void (*writerfn)(const char*, void*), void* writerfn_arg) {
  159. if (pc != nullptr) {
  160. // We don't know the stack frame size for PC, use 0.
  161. if (symbolize_stacktrace) {
  162. DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, pc, pc, 0, "PC: ");
  163. } else {
  164. DumpPCAndFrameSize(writerfn, writerfn_arg, pc, 0, "PC: ");
  165. }
  166. }
  167. for (int i = 0; i < depth; i++) {
  168. if (symbolize_stacktrace) {
  169. // Pass the previous address of pc as the symbol address because pc is a
  170. // return address, and an overrun may occur when the function ends with a
  171. // call to a function annotated noreturn (e.g. CHECK). Note that we don't
  172. // do this for pc above, as the adjustment is only correct for return
  173. // addresses.
  174. DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i],
  175. reinterpret_cast<char*>(stack[i]) - 1,
  176. frame_sizes[i], " ");
  177. } else {
  178. DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i],
  179. " ");
  180. }
  181. }
  182. if (min_dropped_frames > 0) {
  183. char buf[100];
  184. snprintf(buf, sizeof(buf), " @ ... and at least %d more frames\n",
  185. min_dropped_frames);
  186. writerfn(buf, writerfn_arg);
  187. }
  188. }
  189. } // namespace debugging_internal
  190. ABSL_NAMESPACE_END
  191. } // namespace absl