stacktrace_arm-inl.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2011, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Doug Kwan
  32. // This is inspired by Craig Silverstein's PowerPC stacktrace code.
  33. //
  34. #ifndef BASE_STACKTRACE_ARM_INL_H_
  35. #define BASE_STACKTRACE_ARM_INL_H_
  36. // Note: this file is included into stacktrace.cc more than once.
  37. // Anything that should only be defined once should be here:
  38. #include <stdint.h> // for uintptr_t
  39. #include "base/basictypes.h" // for NULL
  40. #include <gperftools/stacktrace.h>
  41. // WARNING:
  42. // This only works if all your code is in either ARM or THUMB mode. With
  43. // interworking, the frame pointer of the caller can either be in r11 (ARM
  44. // mode) or r7 (THUMB mode). A callee only saves the frame pointer of its
  45. // mode in a fixed location on its stack frame. If the caller is a different
  46. // mode, there is no easy way to find the frame pointer. It can either be
  47. // still in the designated register or saved on stack along with other callee
  48. // saved registers.
  49. // Given a pointer to a stack frame, locate and return the calling
  50. // stackframe, or return NULL if no stackframe can be found. Perform sanity
  51. // checks (the strictness of which is controlled by the boolean parameter
  52. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  53. template<bool STRICT_UNWINDING>
  54. static void **NextStackFrame(void **old_sp) {
  55. void **new_sp = (void**) old_sp[-1];
  56. // Check that the transition from frame pointer old_sp to frame
  57. // pointer new_sp isn't clearly bogus
  58. if (STRICT_UNWINDING) {
  59. // With the stack growing downwards, older stack frame must be
  60. // at a greater address that the current one.
  61. if (new_sp <= old_sp) return NULL;
  62. // Assume stack frames larger than 100,000 bytes are bogus.
  63. if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
  64. } else {
  65. // In the non-strict mode, allow discontiguous stack frames.
  66. // (alternate-signal-stacks for example).
  67. if (new_sp == old_sp) return NULL;
  68. // And allow frames upto about 1MB.
  69. if ((new_sp > old_sp)
  70. && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
  71. }
  72. if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
  73. return new_sp;
  74. }
  75. // This ensures that GetStackTrace stes up the Link Register properly.
  76. #ifdef __GNUC__
  77. void StacktraceArmDummyFunction() __attribute__((noinline));
  78. void StacktraceArmDummyFunction() { __asm__ volatile(""); }
  79. #else
  80. # error StacktraceArmDummyFunction() needs to be ported to this platform.
  81. #endif
  82. #endif // BASE_STACKTRACE_ARM_INL_H_
  83. // Note: this part of the file is included several times.
  84. // Do not put globals below.
  85. // The following 4 functions are generated from the code below:
  86. // GetStack{Trace,Frames}()
  87. // GetStack{Trace,Frames}WithContext()
  88. //
  89. // These functions take the following args:
  90. // void** result: the stack-trace, as an array
  91. // int* sizes: the size of each stack frame, as an array
  92. // (GetStackFrames* only)
  93. // int max_depth: the size of the result (and sizes) array(s)
  94. // int skip_count: how many stack pointers to skip before storing in result
  95. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  96. static int GET_STACK_TRACE_OR_FRAMES {
  97. #ifdef __GNUC__
  98. void **sp = reinterpret_cast<void**>(__builtin_frame_address(0));
  99. #else
  100. # error reading stack point not yet supported on this platform.
  101. #endif
  102. // On ARM, the return address is stored in the link register (r14).
  103. // This is not saved on the stack frame of a leaf function. To
  104. // simplify code that reads return addresses, we call a dummy
  105. // function so that the return address of this function is also
  106. // stored in the stack frame. This works at least for gcc.
  107. StacktraceArmDummyFunction();
  108. skip_count++; // skip parent frame due to indirection in stacktrace.cc
  109. int n = 0;
  110. while (sp && n < max_depth) {
  111. // The GetStackFrames routine is called when we are in some
  112. // informational context (the failure signal handler for example).
  113. // Use the non-strict unwinding rules to produce a stack trace
  114. // that is as complete as possible (even if it contains a few bogus
  115. // entries in some rare cases).
  116. void **next_sp = NextStackFrame<IS_STACK_FRAMES == 0>(sp);
  117. if (skip_count > 0) {
  118. skip_count--;
  119. } else {
  120. result[n] = *sp;
  121. #if IS_STACK_FRAMES
  122. if (next_sp > sp) {
  123. sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
  124. } else {
  125. // A frame-size of 0 is used to indicate unknown frame size.
  126. sizes[n] = 0;
  127. }
  128. #endif
  129. n++;
  130. }
  131. sp = next_sp;
  132. }
  133. return n;
  134. }