stacktrace_arm-inl.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // This is inspired by Craig Silverstein's PowerPC stacktrace code.
  16. #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
  17. #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_
  18. #include <cstdint>
  19. #include "absl/debugging/stacktrace.h"
  20. // WARNING:
  21. // This only works if all your code is in either ARM or THUMB mode. With
  22. // interworking, the frame pointer of the caller can either be in r11 (ARM
  23. // mode) or r7 (THUMB mode). A callee only saves the frame pointer of its
  24. // mode in a fixed location on its stack frame. If the caller is a different
  25. // mode, there is no easy way to find the frame pointer. It can either be
  26. // still in the designated register or saved on stack along with other callee
  27. // saved registers.
  28. // Given a pointer to a stack frame, locate and return the calling
  29. // stackframe, or return nullptr if no stackframe can be found. Perform sanity
  30. // checks (the strictness of which is controlled by the boolean parameter
  31. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  32. template<bool STRICT_UNWINDING>
  33. static void **NextStackFrame(void **old_sp) {
  34. void **new_sp = (void**) old_sp[-1];
  35. // Check that the transition from frame pointer old_sp to frame
  36. // pointer new_sp isn't clearly bogus
  37. if (STRICT_UNWINDING) {
  38. // With the stack growing downwards, older stack frame must be
  39. // at a greater address that the current one.
  40. if (new_sp <= old_sp) return nullptr;
  41. // Assume stack frames larger than 100,000 bytes are bogus.
  42. if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return nullptr;
  43. } else {
  44. // In the non-strict mode, allow discontiguous stack frames.
  45. // (alternate-signal-stacks for example).
  46. if (new_sp == old_sp) return nullptr;
  47. // And allow frames upto about 1MB.
  48. if ((new_sp > old_sp)
  49. && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return nullptr;
  50. }
  51. if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return nullptr;
  52. return new_sp;
  53. }
  54. // This ensures that absl::GetStackTrace sets up the Link Register properly.
  55. #ifdef __GNUC__
  56. void StacktraceArmDummyFunction() __attribute__((noinline));
  57. void StacktraceArmDummyFunction() { __asm__ volatile(""); }
  58. #else
  59. # error StacktraceArmDummyFunction() needs to be ported to this platform.
  60. #endif
  61. template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
  62. static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
  63. const void * /* ucp */, int *min_dropped_frames) {
  64. #ifdef __GNUC__
  65. void **sp = reinterpret_cast<void**>(__builtin_frame_address(0));
  66. #else
  67. # error reading stack point not yet supported on this platform.
  68. #endif
  69. // On ARM, the return address is stored in the link register (r14).
  70. // This is not saved on the stack frame of a leaf function. To
  71. // simplify code that reads return addresses, we call a dummy
  72. // function so that the return address of this function is also
  73. // stored in the stack frame. This works at least for gcc.
  74. StacktraceArmDummyFunction();
  75. int n = 0;
  76. while (sp && n < max_depth) {
  77. // The absl::GetStackFrames routine is called when we are in some
  78. // informational context (the failure signal handler for example).
  79. // Use the non-strict unwinding rules to produce a stack trace
  80. // that is as complete as possible (even if it contains a few bogus
  81. // entries in some rare cases).
  82. void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
  83. if (skip_count > 0) {
  84. skip_count--;
  85. } else {
  86. result[n] = *sp;
  87. if (IS_STACK_FRAMES) {
  88. if (next_sp > sp) {
  89. sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
  90. } else {
  91. // A frame-size of 0 is used to indicate unknown frame size.
  92. sizes[n] = 0;
  93. }
  94. }
  95. n++;
  96. }
  97. sp = next_sp;
  98. }
  99. if (min_dropped_frames != nullptr) {
  100. // Implementation detail: we clamp the max of frames we are willing to
  101. // count, so as not to spend too much time in the loop below.
  102. const int kMaxUnwind = 200;
  103. int num_dropped_frames = 0;
  104. for (int j = 0; sp != nullptr && j < kMaxUnwind; j++) {
  105. if (skip_count > 0) {
  106. skip_count--;
  107. } else {
  108. num_dropped_frames++;
  109. }
  110. sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
  111. }
  112. *min_dropped_frames = num_dropped_frames;
  113. }
  114. return n;
  115. }
  116. namespace absl {
  117. ABSL_NAMESPACE_BEGIN
  118. namespace debugging_internal {
  119. bool StackTraceWorksForTest() {
  120. return false;
  121. }
  122. } // namespace debugging_internal
  123. ABSL_NAMESPACE_END
  124. } // namespace absl
  125. #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_ARM_INL_H_