Introduce mock-log.h for unittests.
[platform/upstream/glog.git] / src / stacktrace_unittest.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "config.h"
4 #include "utilities.h"
5 #include "base/commandlineflags.h"
6 #include "glog/logging.h"
7 #include "stacktrace.h"
8
9 #ifdef HAVE_EXECINFO_H
10 # include <execinfo.h>
11 #endif
12
13 using namespace GOOGLE_NAMESPACE;
14
15 #ifdef HAVE_STACKTRACE
16
17 // Obtain a backtrace, verify that the expected callers are present in the
18 // backtrace, and maybe print the backtrace to stdout.
19
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 //-----------------------------------------------------------------------//
28
29 // The sequence of functions whose return addresses we expect to see in the
30 // backtrace.
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,
39 };
40
41 // Depending on the architecture/compiler/libraries, (not sure which)
42 // the current function may or may not appear in the backtrace.
43 // For gcc-2:
44 //
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
50 //
51 // For gcc3-k8:
52 //
53 // stack[0] is ret addr within CheckStackTraceLeaf
54 // stack[1] is ret addr within CheckStackTrace4
55 // ...
56 // stack[5] is ret addr within CheckStackTrace
57
58 //-----------------------------------------------------------------------//
59
60 const int kMaxFnLen = 0x40; // assume relevant functions are only this long
61
62 void CheckRetAddrIsInFunction( void * ret_addr, void * function_start_addr)
63 {
64   CHECK_GE(ret_addr, function_start_addr);
65   CHECK_LE(ret_addr, (void *) ((char *) function_start_addr + kMaxFnLen));
66 }
67
68 //-----------------------------------------------------------------------//
69
70 void CheckStackTraceLeaf(void) {
71   const int STACK_LEN = 10;
72   void *stack[STACK_LEN];
73   int size;
74
75   size = GetStackTrace(stack, STACK_LEN, 0);
76   printf("Obtained %d stack frames.\n", size);
77   CHECK_LE(size, STACK_LEN);
78
79   if (1) {
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);
86     free(strings);
87 #endif
88   }
89   for (int i = 0; i < BACKTRACE_STEPS; i++) {
90     printf("Backtrace %d: expected: %p..%p  actual: %p ... ",
91            i, expected_stack[i],
92            reinterpret_cast<char*>(expected_stack[i]) + kMaxFnLen, stack[i]);
93     CheckRetAddrIsInFunction(stack[i], expected_stack[i]);
94     printf("OK\n");
95   }
96
97   // Check if the second stacktrace returns the same size.
98   CHECK_EQ(size, GetStackTrace(stack, STACK_LEN, 0));
99 }
100
101 //-----------------------------------------------------------------------//
102
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); }
109
110 //-----------------------------------------------------------------------//
111
112 int main(int argc, char ** argv) {
113   FLAGS_logtostderr = true;
114   InitGoogleLogging(argv[0]);
115   
116   CheckStackTrace(0);
117   
118   printf("PASS\n");
119   return 0;
120 }
121
122 #else
123 int main() {
124   printf("PASS (no stacktrace support)\n");
125   return 0;
126 }
127 #endif  // HAVE_STACKTRACE