stacktrace_powerpc-darwin-inl.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // Produce stack trace. ABI documentation reference can be found at:
  31. // * PowerPC32 ABI: https://www.power.org/documentation/
  32. // power-architecture-32-bit-abi-supplement-1-0-embeddedlinuxunified/
  33. // * PowerPC64 ABI:
  34. // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
  35. #ifndef BASE_STACKTRACE_POWERPC_INL_H_
  36. #define BASE_STACKTRACE_POWERPC_INL_H_
  37. // Note: this file is included into stacktrace.cc more than once.
  38. // Anything that should only be defined once should be here:
  39. #include <stdint.h> // for uintptr_t
  40. #include <stdlib.h> // for NULL
  41. #include <gperftools/stacktrace.h>
  42. // Given a pointer to a stack frame, locate and return the calling
  43. // stackframe, or return NULL if no stackframe can be found. Perform sanity
  44. // checks (the strictness of which is controlled by the boolean parameter
  45. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  46. template<bool STRICT_UNWINDING>
  47. static void **NextStackFrame(void **old_sp) {
  48. void **new_sp = (void **) *old_sp;
  49. // Check that the transition from frame pointer old_sp to frame
  50. // pointer new_sp isn't clearly bogus
  51. if (STRICT_UNWINDING) {
  52. // With the stack growing downwards, older stack frame must be
  53. // at a greater address that the current one.
  54. if (new_sp <= old_sp) return NULL;
  55. // Assume stack frames larger than 100,000 bytes are bogus.
  56. if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
  57. } else {
  58. // In the non-strict mode, allow discontiguous stack frames.
  59. // (alternate-signal-stacks for example).
  60. if (new_sp == old_sp) return NULL;
  61. // And allow frames upto about 1MB.
  62. if ((new_sp > old_sp)
  63. && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL;
  64. }
  65. if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
  66. return new_sp;
  67. }
  68. // This ensures that GetStackTrace stes up the Link Register properly.
  69. void StacktracePowerPCDummyFunction() __attribute__((noinline));
  70. void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
  71. #endif // BASE_STACKTRACE_POWERPC_INL_H_
  72. // Note: this part of the file is included several times.
  73. // Do not put globals below.
  74. // The following 4 functions are generated from the code below:
  75. // GetStack{Trace,Frames}()
  76. // GetStack{Trace,Frames}WithContext()
  77. //
  78. // These functions take the following args:
  79. // void** result: the stack-trace, as an array
  80. // int* sizes: the size of each stack frame, as an array
  81. // (GetStackFrames* only)
  82. // int max_depth: the size of the result (and sizes) array(s)
  83. // int skip_count: how many stack pointers to skip before storing in result
  84. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  85. int GET_STACK_TRACE_OR_FRAMES {
  86. void **sp;
  87. // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
  88. // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
  89. // different asm syntax. I don't know quite the best way to discriminate
  90. // systems using the old as from the new one; I've gone with __APPLE__.
  91. // TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2
  92. __asm__ volatile ("mr %0,r1" : "=r" (sp));
  93. // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack
  94. // entry that holds the return address of the subroutine call (what
  95. // instruction we run after our function finishes). This is the
  96. // same as the stack-pointer of our parent routine, which is what we
  97. // want here. While the compiler will always(?) set up LR for
  98. // subroutine calls, it may not for leaf functions (such as this one).
  99. // This routine forces the compiler (at least gcc) to push it anyway.
  100. StacktracePowerPCDummyFunction();
  101. #if IS_STACK_FRAMES
  102. // Note we do *not* increment skip_count here for the SYSV ABI. If
  103. // we did, the list of stack frames wouldn't properly match up with
  104. // the list of return addresses. Note this means the top pc entry
  105. // is probably bogus for linux/ppc (and other SYSV-ABI systems).
  106. #else
  107. // The LR save area is used by the callee, so the top entry is bogus.
  108. skip_count++;
  109. #endif
  110. int n = 0;
  111. while (sp && n < max_depth) {
  112. // The GetStackFrames routine is called when we are in some
  113. // informational context (the failure signal handler for example).
  114. // Use the non-strict unwinding rules to produce a stack trace
  115. // that is as complete as possible (even if it contains a few
  116. // bogus entries in some rare cases).
  117. void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp);
  118. if (skip_count > 0) {
  119. skip_count--;
  120. } else {
  121. // PowerPC has 3 main ABIs, which say where in the stack the
  122. // Link Register is. For DARWIN and AIX (used by apple and
  123. // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc),
  124. // it's in sp[1].
  125. #if defined(__PPC64__)
  126. // This check is in case the compiler doesn't define _CALL_AIX/etc.
  127. result[n] = *(sp+2);
  128. #elif defined(__linux)
  129. // This check is in case the compiler doesn't define _CALL_SYSV.
  130. result[n] = *(sp+1);
  131. #endif
  132. #if IS_STACK_FRAMES
  133. if (next_sp > sp) {
  134. sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
  135. } else {
  136. // A frame-size of 0 is used to indicate unknown frame size.
  137. sizes[n] = 0;
  138. }
  139. #endif
  140. n++;
  141. }
  142. sp = next_sp;
  143. }
  144. return n;
  145. }