stacktrace_powerpc-inl.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. I'm guessing (hoping!) the code is much like
  34. // for x86. For apple machines, at least, it seems to be; see
  35. // http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html
  36. // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK
  37. // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882
  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. struct layout_ppc {
  46. struct layout_ppc *next;
  47. #if defined(__APPLE__) || (defined(__linux) && defined(__PPC64__))
  48. long condition_register;
  49. #endif
  50. void *return_addr;
  51. };
  52. // Given a pointer to a stack frame, locate and return the calling
  53. // stackframe, or return NULL if no stackframe can be found. Perform sanity
  54. // checks (the strictness of which is controlled by the boolean parameter
  55. // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
  56. template<bool STRICT_UNWINDING>
  57. static layout_ppc *NextStackFrame(layout_ppc *current) {
  58. uintptr_t old_sp = (uintptr_t)(current);
  59. uintptr_t new_sp = (uintptr_t)(current->next);
  60. // Check that the transition from frame pointer old_sp to frame
  61. // pointer new_sp isn't clearly bogus
  62. if (STRICT_UNWINDING) {
  63. // With the stack growing downwards, older stack frame must be
  64. // at a greater address that the current one.
  65. if (new_sp <= old_sp)
  66. return NULL;
  67. // Assume stack frames larger than 100,000 bytes are bogus.
  68. if (new_sp - old_sp > 100000)
  69. return NULL;
  70. } else {
  71. // In the non-strict mode, allow discontiguous stack frames.
  72. // (alternate-signal-stacks for example).
  73. if (new_sp == old_sp)
  74. return NULL;
  75. // And allow frames upto about 1MB.
  76. if ((new_sp > old_sp) && (new_sp - old_sp > 1000000))
  77. return NULL;
  78. }
  79. if (new_sp & (sizeof(void *) - 1))
  80. return NULL;
  81. return current->next;
  82. }
  83. // This ensures that GetStackTrace stes up the Link Register properly.
  84. void StacktracePowerPCDummyFunction() __attribute__((noinline));
  85. void StacktracePowerPCDummyFunction() { __asm__ volatile(""); }
  86. #endif // BASE_STACKTRACE_POWERPC_INL_H_
  87. // Note: this part of the file is included several times.
  88. // Do not put globals below.
  89. // Load instruction used on top-of-stack get.
  90. #if defined(__PPC64__) || defined(__LP64__)
  91. # define LOAD "ld"
  92. #else
  93. # define LOAD "lwz"
  94. #endif
  95. #if defined(__linux__) && defined(__PPC__)
  96. # define TOP_STACK "%0,0(1)"
  97. #elif defined(__MACH__) && defined(__APPLE__)
  98. // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther)
  99. // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a
  100. // different asm syntax. I don't know quite the best way to discriminate
  101. // systems using the old as from the new one; I've gone with __APPLE__.
  102. // TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2
  103. # define TOP_STACK "%0,0(r1)"
  104. #endif
  105. // The following 4 functions are generated from the code below:
  106. // GetStack{Trace,Frames}()
  107. // GetStack{Trace,Frames}WithContext()
  108. //
  109. // These functions take the following args:
  110. // void** result: the stack-trace, as an array
  111. // int* sizes: the size of each stack frame, as an array
  112. // (GetStackFrames* only)
  113. // int max_depth: the size of the result (and sizes) array(s)
  114. // int skip_count: how many stack pointers to skip before storing in result
  115. // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
  116. static int GET_STACK_TRACE_OR_FRAMES {
  117. layout_ppc *current;
  118. int n;
  119. // Force GCC to spill LR.
  120. asm volatile ("" : "=l"(current));
  121. // Get the address on top-of-stack
  122. asm volatile (LOAD " " TOP_STACK : "=r"(current));
  123. StacktracePowerPCDummyFunction();
  124. n = 0;
  125. skip_count++; // skip parent's frame due to indirection in
  126. // stacktrace.cc
  127. while (current && n < max_depth) {
  128. // The GetStackFrames routine is called when we are in some
  129. // informational context (the failure signal handler for example).
  130. // Use the non-strict unwinding rules to produce a stack trace
  131. // that is as complete as possible (even if it contains a few
  132. // bogus entries in some rare cases).
  133. layout_ppc *next = NextStackFrame<!IS_STACK_FRAMES>(current);
  134. if (skip_count > 0) {
  135. skip_count--;
  136. } else {
  137. result[n] = current->return_addr;
  138. #if IS_STACK_FRAMES
  139. if (next > current) {
  140. sizes[n] = (uintptr_t)next - (uintptr_t)current;
  141. } else {
  142. // A frame-size of 0 is used to indicate unknown frame size.
  143. sizes[n] = 0;
  144. }
  145. #endif
  146. n++;
  147. }
  148. current = next;
  149. }
  150. // It's possible the second-last stack frame can't return
  151. // (that is, it's __libc_start_main), in which case
  152. // the CRT startup code will have set its LR to 'NULL'.
  153. if (n > 0 && result[n-1] == NULL)
  154. n--;
  155. return n;
  156. }