5 #include "base/commandlineflags.h"
6 #include "glog/logging.h"
7 #include "stacktrace.h"
10 # include <execinfo.h>
13 using namespace GOOGLE_NAMESPACE;
15 #ifdef HAVE_STACKTRACE
17 // Obtain a backtrace, verify that the expected callers are present in the
18 // backtrace, and maybe print the backtrace to stdout.
20 //-----------------------------------------------------------------------//
21 void CheckStackTraceLeaf();
22 void CheckStackTrace4(int i);
23 void CheckStackTrace3(int i);
24 void CheckStackTrace2(int i);
25 void CheckStackTrace1(int i);
26 void CheckStackTrace(int i);
27 //-----------------------------------------------------------------------//
29 // The sequence of functions whose return addresses we expect to see in the
31 const int BACKTRACE_STEPS = 6;
32 void * expected_stack[BACKTRACE_STEPS] = {
33 (void *) &CheckStackTraceLeaf,
34 (void *) &CheckStackTrace4,
35 (void *) &CheckStackTrace3,
36 (void *) &CheckStackTrace2,
37 (void *) &CheckStackTrace1,
38 (void *) &CheckStackTrace,
41 // Depending on the architecture/compiler/libraries, (not sure which)
42 // the current function may or may not appear in the backtrace.
45 // stack[0] is ret addr within CheckStackTrace4
46 // stack[1] is ret addr within CheckStackTrace3
47 // stack[2] is ret addr within CheckStackTrace2
48 // stack[3] is ret addr within CheckStackTrace1
49 // stack[4] is ret addr within CheckStackTrace
53 // stack[0] is ret addr within CheckStackTraceLeaf
54 // stack[1] is ret addr within CheckStackTrace4
56 // stack[5] is ret addr within CheckStackTrace
58 //-----------------------------------------------------------------------//
60 const int kMaxFnLen = 0x40; // assume relevant functions are only this long
62 void CheckRetAddrIsInFunction( void * ret_addr, void * function_start_addr)
64 CHECK_GE(ret_addr, function_start_addr);
65 CHECK_LE(ret_addr, (void *) ((char *) function_start_addr + kMaxFnLen));
68 //-----------------------------------------------------------------------//
70 void CheckStackTraceLeaf(void) {
71 const int STACK_LEN = 10;
72 void *stack[STACK_LEN];
75 size = GetStackTrace(stack, STACK_LEN, 0);
76 printf("Obtained %d stack frames.\n", size);
77 CHECK_LE(size, STACK_LEN);
80 #ifdef HAVE_EXECINFO_H
81 char **strings = backtrace_symbols(stack, size);
82 printf("Obtained %d stack frames.\n", size);
83 for (int i = 0; i < size; i++)
84 printf("%s %p\n", strings[i], stack[i]);
85 printf("CheckStackTrace() addr: %p\n", &CheckStackTrace);
89 for (int i = 0; i < BACKTRACE_STEPS; i++) {
90 printf("Backtrace %d: expected: %p..%p actual: %p ... ",
92 reinterpret_cast<char*>(expected_stack[i]) + kMaxFnLen, stack[i]);
93 CheckRetAddrIsInFunction(stack[i], expected_stack[i]);
97 // Check if the second stacktrace returns the same size.
98 CHECK_EQ(size, GetStackTrace(stack, STACK_LEN, 0));
101 //-----------------------------------------------------------------------//
103 /* Dummy functions to make the backtrace more interesting. */
104 void CheckStackTrace4(int i) { for (int j = i; j >= 0; j--) CheckStackTraceLeaf(); }
105 void CheckStackTrace3(int i) { for (int j = i; j >= 0; j--) CheckStackTrace4(j); }
106 void CheckStackTrace2(int i) { for (int j = i; j >= 0; j--) CheckStackTrace3(j); }
107 void CheckStackTrace1(int i) { for (int j = i; j >= 0; j--) CheckStackTrace2(j); }
108 void CheckStackTrace(int i) { for (int j = i; j >= 0; j--) CheckStackTrace1(j); }
110 //-----------------------------------------------------------------------//
112 int main(int argc, char ** argv) {
113 FLAGS_logtostderr = true;
114 InitGoogleLogging(argv[0]);
124 printf("PASS (no stacktrace support)\n");
127 #endif // HAVE_STACKTRACE