/thirdparty/breakpad/processor/stackwalker_selftest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 425 lines · 236 code · 62 blank · 127 comment · 11 complexity · 23215b834268bf369611c9d4b14fba4d MD5 · raw file

  1. // Copyright (c) 2006, 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. // stackwalker_selftest.cc: Tests StackwalkerX86 or StackwalkerPPC using the
  30. // running process' stack as test data, if running on an x86 or ppc and
  31. // compiled with gcc. This test is not enabled in the "make check" suite
  32. // by default, because certain optimizations interfere with its proper
  33. // operation. To turn it on, configure with --enable-selftest.
  34. //
  35. // Optimizations that cause problems:
  36. // - stack frame reuse. The Recursor function here calls itself with
  37. // |return Recursor|. When the caller's frame is reused, it will cause
  38. // CountCallerFrames to correctly return the same number of frames
  39. // in both the caller and callee. This is considered an unexpected
  40. // condition in the test, which expects a callee to have one more
  41. // caller frame in the stack than its caller.
  42. // - frame pointer omission. Even with a stackwalker that understands
  43. // this optimization, the code to harness debug information currently
  44. // only exists to retrieve it from minidumps, not the current process.
  45. //
  46. // This test can also serve as a developmental and debugging aid if
  47. // PRINT_STACKS is defined.
  48. //
  49. // Author: Mark Mentovai
  50. #include "processor/logging.h"
  51. #if defined(__i386) && !defined(__i386__)
  52. #define __i386__
  53. #endif
  54. #if defined(__sparc) && !defined(__sparc__)
  55. #define __sparc__
  56. #endif
  57. #if (defined(__SUNPRO_CC) || defined(__GNUC__)) && \
  58. (defined(__i386__) || defined(__ppc__) || defined(__sparc__))
  59. #include <stdio.h>
  60. #include "google_breakpad/common/breakpad_types.h"
  61. #include "google_breakpad/common/minidump_format.h"
  62. #include "google_breakpad/processor/basic_source_line_resolver.h"
  63. #include "google_breakpad/processor/call_stack.h"
  64. #include "google_breakpad/processor/memory_region.h"
  65. #include "google_breakpad/processor/stack_frame.h"
  66. #include "google_breakpad/processor/stack_frame_cpu.h"
  67. #include "processor/scoped_ptr.h"
  68. using google_breakpad::BasicSourceLineResolver;
  69. using google_breakpad::CallStack;
  70. using google_breakpad::MemoryRegion;
  71. using google_breakpad::scoped_ptr;
  72. using google_breakpad::StackFrame;
  73. using google_breakpad::StackFramePPC;
  74. using google_breakpad::StackFrameX86;
  75. using google_breakpad::StackFrameSPARC;
  76. #if defined(__i386__)
  77. #include "processor/stackwalker_x86.h"
  78. using google_breakpad::StackwalkerX86;
  79. #elif defined(__ppc__)
  80. #include "processor/stackwalker_ppc.h"
  81. using google_breakpad::StackwalkerPPC;
  82. #elif defined(__sparc__)
  83. #include "processor/stackwalker_sparc.h"
  84. using google_breakpad::StackwalkerSPARC;
  85. #endif // __i386__ || __ppc__ || __sparc__
  86. #define RECURSION_DEPTH 100
  87. // A simple MemoryRegion subclass that provides direct access to this
  88. // process' memory space by pointer.
  89. class SelfMemoryRegion : public MemoryRegion {
  90. public:
  91. virtual u_int64_t GetBase() { return 0; }
  92. virtual u_int32_t GetSize() { return 0xffffffff; }
  93. bool GetMemoryAtAddress(u_int64_t address, u_int8_t* value) {
  94. return GetMemoryAtAddressInternal(address, value); }
  95. bool GetMemoryAtAddress(u_int64_t address, u_int16_t* value) {
  96. return GetMemoryAtAddressInternal(address, value); }
  97. bool GetMemoryAtAddress(u_int64_t address, u_int32_t* value) {
  98. return GetMemoryAtAddressInternal(address, value); }
  99. bool GetMemoryAtAddress(u_int64_t address, u_int64_t* value) {
  100. return GetMemoryAtAddressInternal(address, value); }
  101. private:
  102. template<typename T> bool GetMemoryAtAddressInternal(u_int64_t address,
  103. T* value) {
  104. // Without knowing what addresses are actually mapped, just assume that
  105. // everything low is not mapped. This helps the stackwalker catch the
  106. // end of a stack when it tries to dereference a null or low pointer
  107. // in an attempt to find the caller frame. Other unmapped accesses will
  108. // cause the program to crash, but that would properly be a test failure.
  109. if (address < 0x100)
  110. return false;
  111. u_int8_t* memory = 0;
  112. *value = *reinterpret_cast<const T*>(&memory[address]);
  113. return true;
  114. }
  115. };
  116. #if defined(__GNUC__)
  117. #if defined(__i386__)
  118. // GetEBP returns the current value of the %ebp register. Because it's
  119. // implemented as a function, %ebp itself contains GetEBP's frame pointer
  120. // and not the caller's frame pointer. Dereference %ebp to obtain the
  121. // caller's frame pointer, which the compiler-generated preamble stored
  122. // on the stack (provided frame pointers are not being omitted.) Because
  123. // this function depends on the compiler-generated preamble, inlining is
  124. // disabled.
  125. static u_int32_t GetEBP() __attribute__((noinline));
  126. static u_int32_t GetEBP() {
  127. u_int32_t ebp;
  128. __asm__ __volatile__(
  129. "movl (%%ebp), %0"
  130. : "=a" (ebp)
  131. );
  132. return ebp;
  133. }
  134. // The caller's %esp is 8 higher than the value of %ebp in this function,
  135. // assuming that it's not inlined and that the standard prolog is used.
  136. // The CALL instruction places a 4-byte return address on the stack above
  137. // the caller's %esp, and this function's prolog will save the caller's %ebp
  138. // on the stack as well, for another 4 bytes, before storing %esp in %ebp.
  139. static u_int32_t GetESP() __attribute__((noinline));
  140. static u_int32_t GetESP() {
  141. u_int32_t ebp;
  142. __asm__ __volatile__(
  143. "movl %%ebp, %0"
  144. : "=a" (ebp)
  145. );
  146. return ebp + 8;
  147. }
  148. // GetEIP returns the instruction pointer identifying the next instruction
  149. // to execute after GetEIP returns. It obtains this information from the
  150. // stack, where it was placed by the call instruction that called GetEIP.
  151. // This function depends on frame pointers not being omitted. It is possible
  152. // to write a pure asm version of this routine that has no compiler-generated
  153. // preamble and uses %esp instead of %ebp; that would function in the
  154. // absence of frame pointers. However, the simpler approach is used here
  155. // because GetEBP and stackwalking necessarily depends on access to frame
  156. // pointers. Because this function depends on a call instruction and the
  157. // compiler-generated preamble, inlining is disabled.
  158. static u_int32_t GetEIP() __attribute__((noinline));
  159. static u_int32_t GetEIP() {
  160. u_int32_t eip;
  161. __asm__ __volatile__(
  162. "movl 4(%%ebp), %0"
  163. : "=a" (eip)
  164. );
  165. return eip;
  166. }
  167. #elif defined(__ppc__)
  168. // GetSP returns the current value of the %r1 register, which by convention,
  169. // is the stack pointer on ppc. Because it's implemented as a function,
  170. // %r1 itself contains GetSP's own stack pointer and not the caller's stack
  171. // pointer. Dereference %r1 to obtain the caller's stack pointer, which the
  172. // compiler-generated prolog stored on the stack. Because this function
  173. // depends on the compiler-generated prolog, inlining is disabled.
  174. static u_int32_t GetSP() __attribute__((noinline));
  175. static u_int32_t GetSP() {
  176. u_int32_t sp;
  177. __asm__ __volatile__(
  178. "lwz %0, 0(r1)"
  179. : "=r" (sp)
  180. );
  181. return sp;
  182. }
  183. // GetPC returns the program counter identifying the next instruction to
  184. // execute after GetPC returns. It obtains this information from the
  185. // link register, where it was placed by the branch instruction that called
  186. // GetPC. Because this function depends on the caller's use of a branch
  187. // instruction, inlining is disabled.
  188. static u_int32_t GetPC() __attribute__((noinline));
  189. static u_int32_t GetPC() {
  190. u_int32_t lr;
  191. __asm__ __volatile__(
  192. "mflr %0"
  193. : "=r" (lr)
  194. );
  195. return lr;
  196. }
  197. #elif defined(__sparc__)
  198. // GetSP returns the current value of the %sp/%o6/%g_r[14] register, which
  199. // by convention, is the stack pointer on sparc. Because it's implemented
  200. // as a function, %sp itself contains GetSP's own stack pointer and not
  201. // the caller's stack pointer. Dereference to obtain the caller's stack
  202. // pointer, which the compiler-generated prolog stored on the stack.
  203. // Because this function depends on the compiler-generated prolog, inlining
  204. // is disabled.
  205. static u_int32_t GetSP() __attribute__((noinline));
  206. static u_int32_t GetSP() {
  207. u_int32_t sp;
  208. __asm__ __volatile__(
  209. "mov %%fp, %0"
  210. : "=r" (sp)
  211. );
  212. return sp;
  213. }
  214. // GetFP returns the current value of the %fp register. Because it's
  215. // implemented as a function, %fp itself contains GetFP's frame pointer
  216. // and not the caller's frame pointer. Dereference %fp to obtain the
  217. // caller's frame pointer, which the compiler-generated preamble stored
  218. // on the stack (provided frame pointers are not being omitted.) Because
  219. // this function depends on the compiler-generated preamble, inlining is
  220. // disabled.
  221. static u_int32_t GetFP() __attribute__((noinline));
  222. static u_int32_t GetFP() {
  223. u_int32_t fp;
  224. __asm__ __volatile__(
  225. "ld [%%fp+56], %0"
  226. : "=r" (fp)
  227. );
  228. return fp;
  229. }
  230. // GetPC returns the program counter identifying the next instruction to
  231. // execute after GetPC returns. It obtains this information from the
  232. // link register, where it was placed by the branch instruction that called
  233. // GetPC. Because this function depends on the caller's use of a branch
  234. // instruction, inlining is disabled.
  235. static u_int32_t GetPC() __attribute__((noinline));
  236. static u_int32_t GetPC() {
  237. u_int32_t pc;
  238. __asm__ __volatile__(
  239. "mov %%i7, %0"
  240. : "=r" (pc)
  241. );
  242. return pc + 8;
  243. }
  244. #endif // __i386__ || __ppc__ || __sparc__
  245. #elif defined(__SUNPRO_CC)
  246. #if defined(__i386__)
  247. extern "C" {
  248. extern u_int32_t GetEIP();
  249. extern u_int32_t GetEBP();
  250. extern u_int32_t GetESP();
  251. }
  252. #elif defined(__sparc__)
  253. extern "C" {
  254. extern u_int32_t GetPC();
  255. extern u_int32_t GetFP();
  256. extern u_int32_t GetSP();
  257. }
  258. #endif // __i386__ || __sparc__
  259. #endif // __GNUC__ || __SUNPRO_CC
  260. // CountCallerFrames returns the number of stack frames beneath the function
  261. // that called CountCallerFrames. Because this function's return value
  262. // is dependent on the size of the stack beneath it, inlining is disabled,
  263. // and any function that calls this should not be inlined either.
  264. #if defined(__GNUC__)
  265. static unsigned int CountCallerFrames() __attribute__((noinline));
  266. #elif defined(__SUNPRO_CC)
  267. static unsigned int CountCallerFrames();
  268. #endif
  269. static unsigned int CountCallerFrames() {
  270. SelfMemoryRegion memory;
  271. BasicSourceLineResolver resolver;
  272. #if defined(__i386__)
  273. MDRawContextX86 context = MDRawContextX86();
  274. context.eip = GetEIP();
  275. context.ebp = GetEBP();
  276. context.esp = GetESP();
  277. StackwalkerX86 stackwalker = StackwalkerX86(NULL, &context, &memory, NULL,
  278. NULL, &resolver);
  279. #elif defined(__ppc__)
  280. MDRawContextPPC context = MDRawContextPPC();
  281. context.srr0 = GetPC();
  282. context.gpr[1] = GetSP();
  283. StackwalkerPPC stackwalker = StackwalkerPPC(NULL, &context, &memory, NULL,
  284. NULL, &resolver);
  285. #elif defined(__sparc__)
  286. MDRawContextSPARC context = MDRawContextSPARC();
  287. context.pc = GetPC();
  288. context.g_r[14] = GetSP();
  289. context.g_r[30] = GetFP();
  290. StackwalkerSPARC stackwalker = StackwalkerSPARC(NULL, &context, &memory,
  291. NULL, NULL, &resolver);
  292. #endif // __i386__ || __ppc__ || __sparc__
  293. CallStack stack;
  294. stackwalker.Walk(&stack);
  295. #ifdef PRINT_STACKS
  296. printf("\n");
  297. for (unsigned int frame_index = 0;
  298. frame_index < stack.frames()->size();
  299. ++frame_index) {
  300. StackFrame *frame = stack.frames()->at(frame_index);
  301. printf("frame %-3d instruction = 0x%08" PRIx64,
  302. frame_index, frame->instruction);
  303. #if defined(__i386__)
  304. StackFrameX86 *frame_x86 = reinterpret_cast<StackFrameX86*>(frame);
  305. printf(" esp = 0x%08x ebp = 0x%08x\n",
  306. frame_x86->context.esp, frame_x86->context.ebp);
  307. #elif defined(__ppc__)
  308. StackFramePPC *frame_ppc = reinterpret_cast<StackFramePPC*>(frame);
  309. printf(" gpr[1] = 0x%08x\n", frame_ppc->context.gpr[1]);
  310. #elif defined(__sparc__)
  311. StackFrameSPARC *frame_sparc = reinterpret_cast<StackFrameSPARC*>(frame);
  312. printf(" sp = 0x%08x fp = 0x%08x\n",
  313. frame_sparc->context.g_r[14], frame_sparc->context.g_r[30]);
  314. #endif // __i386__ || __ppc__ || __sparc__
  315. }
  316. #endif // PRINT_STACKS
  317. // Subtract 1 because the caller wants the number of frames beneath
  318. // itself. Because the caller called us, subract two for our frame and its
  319. // frame, which are included in stack.size().
  320. return stack.frames()->size() - 2;
  321. }
  322. // Recursor verifies that the number stack frames beneath itself is one more
  323. // than the number of stack frames beneath its parent. When depth frames
  324. // have been reached, Recursor stops checking and returns success. If the
  325. // frame count check fails at any depth, Recursor will stop and return false.
  326. // Because this calls CountCallerFrames, inlining is disabled.
  327. #if defined(__GNUC__)
  328. static bool Recursor(unsigned int depth, unsigned int parent_callers)
  329. __attribute__((noinline));
  330. #elif defined(__SUNPRO_CC)
  331. static bool Recursor(unsigned int depth, unsigned int parent_callers);
  332. #endif
  333. static bool Recursor(unsigned int depth, unsigned int parent_callers) {
  334. unsigned int callers = CountCallerFrames();
  335. if (callers != parent_callers + 1)
  336. return false;
  337. if (depth)
  338. return Recursor(depth - 1, callers);
  339. // depth == 0
  340. return true;
  341. }
  342. // Because this calls CountCallerFrames, inlining is disabled - but because
  343. // it's main (and nobody calls it other than the entry point), it wouldn't
  344. // be inlined anyway.
  345. #if defined(__GNUC__)
  346. int main(int argc, char** argv) __attribute__((noinline));
  347. #elif defined(__SUNPRO_CC)
  348. int main(int argc, char** argv);
  349. #endif
  350. int main(int argc, char** argv) {
  351. BPLOG_INIT(&argc, &argv);
  352. return Recursor(RECURSION_DEPTH, CountCallerFrames()) ? 0 : 1;
  353. }
  354. #else
  355. // Not i386 or ppc or sparc? We can only test stacks we know how to walk.
  356. int main(int argc, char **argv) {
  357. BPLOG_INIT(&argc, &argv);
  358. // "make check" interprets an exit status of 77 to mean that the test is
  359. // not supported.
  360. BPLOG(ERROR) << "Selftest not supported here";
  361. return 77;
  362. }
  363. #endif // (__GNUC__ || __SUNPRO_CC) && (__i386__ || __ppc__ || __sparc__)