stacktrace_powerpc-linux-inl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, 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: Craig Silverstein
  32. //
  33. // Produce stack trace. ABI documentation reference can be found at:
  34. // * PowerPC32 ABI: https://www.power.org/documentation/
  35. // power-architecture-32-bit-abi-supplement-1-0-embeddedlinuxunified/
  36. // * PowerPC64 ABI:
  37. // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
  38. #ifndef BASE_STACKTRACE_POWERPC_INL_H_
  39. #define BASE_STACKTRACE_POWERPC_INL_H_
  40. // Note: this file is included into stacktrace.cc more than once.
  41. // Anything that should only be defined once should be here:
  42. #include <stdint.h> // for uintptr_t
  43. #include <stdlib.h> // for NULL
  44. #include <gperftools/stacktrace.h>
  45. #include <base/vdso_support.h>
  46. #if defined(HAVE_SYS_UCONTEXT_H)
  47. #include <sys/ucontext.h>
  48. #elif defined(HAVE_UCONTEXT_H)
  49. #include <ucontext.h> // for ucontext_t
  50. #endif
  51. typedef ucontext ucontext_t;
  52. // PowerPC64 Little Endian follows BE wrt. backchain, condition register,
  53. // and LR save area, so no need to adjust the reading struct.
  54. struct layout_ppc {
  55. struct layout_ppc *next;
  56. #ifdef __PPC64__
  57. long condition_register;
  58. #endif
  59. void *return_addr;
  60. };
  61. // Signal callbacks are handled by the vDSO symbol:
  62. //
  63. // * PowerPC64 Linux (arch/powerpc/kernel/vdso64/sigtramp.S):
  64. // __kernel_sigtramp_rt64
  65. // * PowerPC32 Linux (arch/powerpc/kernel/vdso32/sigtramp.S):
  66. // __kernel_sigtramp32
  67. // __kernel_sigtramp_rt32
  68. //
  69. // So a backtrace may need to specially handling if the symbol readed is
  70. // the signal trampoline.
  71. // Given a pointer to a stack frame, locate and return the calling
  72. // stackframe, or return NULL if no stackframe can be found. Perform sanity
  73. // checks (the strictness of which is controlled by the boolean parameter
  74. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  75. template<bool STRICT_UNWINDING>
  76. static layout_ppc *NextStackFrame(layout_ppc *current) {
  77. uintptr_t old_sp = (uintptr_t)(current);
  78. uintptr_t new_sp = (uintptr_t)(current->next);
  79. // Check that the transition from frame pointer old_sp to frame
  80. // pointer new_sp isn't clearly bogus
  81. if (STRICT_UNWINDING) {
  82. // With the stack growing downwards, older stack frame must be
  83. // at a greater address that the current one.
  84. if (new_sp <= old_sp)
  85. return NULL;
  86. // Assume stack frames larger than 100,000 bytes are bogus.
  87. if (new_sp - old_sp > 100000)
  88. return NULL;
  89. } else {
  90. // In the non-strict mode, allow discontiguous stack frames.
  91. // (alternate-signal-stacks for example).
  92. if (new_sp == old_sp)
  93. return NULL;
  94. // And allow frames upto about 1MB.
  95. if ((new_sp > old_sp) && (new_sp - old_sp > 1000000))
  96. return NULL;
  97. }
  98. if (new_sp & (sizeof(void *) - 1))
  99. return NULL;
  100. return current->next;
  101. }
  102. // This ensures that GetStackTrace stes up the Link Register properly.
  103. void StacktracePowerPCDummyFunction() __attribute__((noinline));
  104. void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
  105. #endif // BASE_STACKTRACE_POWERPC_INL_H_
  106. // Note: this part of the file is included several times.
  107. // Do not put globals below.
  108. // Load instruction used on top-of-stack get.
  109. #if defined(__PPC64__) || defined(__LP64__)
  110. # define LOAD "ld"
  111. #else
  112. # define LOAD "lwz"
  113. #endif
  114. // The following 4 functions are generated from the code below:
  115. // GetStack{Trace,Frames}()
  116. // GetStack{Trace,Frames}WithContext()
  117. //
  118. // These functions take the following args:
  119. // void** result: the stack-trace, as an array
  120. // int* sizes: the size of each stack frame, as an array
  121. // (GetStackFrames* only)
  122. // int max_depth: the size of the result (and sizes) array(s)
  123. // int skip_count: how many stack pointers to skip before storing in result
  124. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  125. static int GET_STACK_TRACE_OR_FRAMES {
  126. layout_ppc *current;
  127. int n;
  128. // Get the address on top-of-stack
  129. current = reinterpret_cast<layout_ppc*> (__builtin_frame_address (0));
  130. // And ignore the current symbol
  131. current = current->next;
  132. StacktracePowerPCDummyFunction();
  133. n = 0;
  134. skip_count++; // skip parent's frame due to indirection in
  135. // stacktrace.cc
  136. base::VDSOSupport vdso;
  137. base::ElfMemImage::SymbolInfo rt_sigreturn_symbol_info;
  138. #ifdef __PPC64__
  139. const void *sigtramp64_vdso = 0;
  140. if (vdso.LookupSymbol("__kernel_sigtramp_rt64", "LINUX_2.6.15", STT_NOTYPE,
  141. &rt_sigreturn_symbol_info))
  142. sigtramp64_vdso = rt_sigreturn_symbol_info.address;
  143. #else
  144. const void *sigtramp32_vdso = 0;
  145. if (vdso.LookupSymbol("__kernel_sigtramp32", "LINUX_2.6.15", STT_NOTYPE,
  146. &rt_sigreturn_symbol_info))
  147. sigtramp32_vdso = rt_sigreturn_symbol_info.address;
  148. const void *sigtramp32_rt_vdso = 0;
  149. if (vdso.LookupSymbol("__kernel_sigtramp_rt32", "LINUX_2.6.15", STT_NOTYPE,
  150. &rt_sigreturn_symbol_info))
  151. sigtramp32_rt_vdso = rt_sigreturn_symbol_info.address;
  152. #endif
  153. while (current && n < max_depth) {
  154. // The GetStackFrames routine is called when we are in some
  155. // informational context (the failure signal handler for example).
  156. // Use the non-strict unwinding rules to produce a stack trace
  157. // that is as complete as possible (even if it contains a few
  158. // bogus entries in some rare cases).
  159. layout_ppc *next = NextStackFrame<!IS_STACK_FRAMES>(current);
  160. if (skip_count > 0) {
  161. skip_count--;
  162. } else {
  163. result[n] = current->return_addr;
  164. #ifdef __PPC64__
  165. if (sigtramp64_vdso && (sigtramp64_vdso == current->return_addr)) {
  166. struct signal_frame_64 {
  167. char dummy[128];
  168. ucontext_t uc;
  169. // We don't care about the rest, since the IP value is at 'uc' field.
  170. } *sigframe = reinterpret_cast<signal_frame_64*>(current);
  171. result[n] = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_NIP];
  172. }
  173. #else
  174. if (sigtramp32_vdso && (sigtramp32_vdso == current->return_addr)) {
  175. struct signal_frame_32 {
  176. char dummy[64];
  177. struct sigcontext sctx;
  178. mcontext_t mctx;
  179. // We don't care about the rest, since IP value is at 'mctx' field.
  180. } *sigframe = reinterpret_cast<signal_frame_32*>(current);
  181. result[n] = (void*) sigframe->mctx.gregs[PT_NIP];
  182. } else if (sigtramp32_rt_vdso && (sigtramp32_rt_vdso == current->return_addr)) {
  183. struct rt_signal_frame_32 {
  184. char dummy[64 + 16];
  185. siginfo_t info;
  186. struct ucontext uc;
  187. // We don't care about the rest, since IP value is at 'uc' field.A
  188. } *sigframe = reinterpret_cast<rt_signal_frame_32*>(current);
  189. result[n] = (void*) sigframe->uc.uc_mcontext.uc_regs->gregs[PT_NIP];
  190. }
  191. #endif
  192. #if IS_STACK_FRAMES
  193. if (next > current) {
  194. sizes[n] = (uintptr_t)next - (uintptr_t)current;
  195. } else {
  196. // A frame-size of 0 is used to indicate unknown frame size.
  197. sizes[n] = 0;
  198. }
  199. #endif
  200. n++;
  201. }
  202. current = next;
  203. }
  204. // It's possible the second-last stack frame can't return
  205. // (that is, it's __libc_start_main), in which case
  206. // the CRT startup code will have set its LR to 'NULL'.
  207. if (n > 0 && result[n-1] == NULL)
  208. n--;
  209. return n;
  210. }