Use struct instead of class for forward declaration of CrashReason as we define it...
[platform/upstream/glog.git] / src / stacktrace_unittest.cc
1 // Copyright (c) 2004, 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
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "config.h"
33 #include "utilities.h"
34 #include "base/commandlineflags.h"
35 #include "glog/logging.h"
36 #include "stacktrace.h"
37
38 #ifdef HAVE_EXECINFO_H
39 # include <execinfo.h>
40 #endif
41
42 using namespace GOOGLE_NAMESPACE;
43
44 #ifdef HAVE_STACKTRACE
45
46 // Obtain a backtrace, verify that the expected callers are present in the
47 // backtrace, and maybe print the backtrace to stdout.
48
49 //-----------------------------------------------------------------------//
50 void CheckStackTraceLeaf();
51 void CheckStackTrace4(int i);
52 void CheckStackTrace3(int i);
53 void CheckStackTrace2(int i);
54 void CheckStackTrace1(int i);
55 void CheckStackTrace(int i);
56 //-----------------------------------------------------------------------//
57
58 // The sequence of functions whose return addresses we expect to see in the
59 // backtrace.
60 const int BACKTRACE_STEPS = 6;
61 void * expected_stack[BACKTRACE_STEPS] = {
62   (void *) &CheckStackTraceLeaf,
63   (void *) &CheckStackTrace4,
64   (void *) &CheckStackTrace3,
65   (void *) &CheckStackTrace2,
66   (void *) &CheckStackTrace1,
67   (void *) &CheckStackTrace,
68 };
69
70 // Depending on the architecture/compiler/libraries, (not sure which)
71 // the current function may or may not appear in the backtrace.
72 // For gcc-2:
73 //
74 // stack[0] is ret addr within CheckStackTrace4
75 // stack[1] is ret addr within CheckStackTrace3
76 // stack[2] is ret addr within CheckStackTrace2
77 // stack[3] is ret addr within CheckStackTrace1
78 // stack[4] is ret addr within CheckStackTrace
79 //
80 // For gcc3-k8:
81 //
82 // stack[0] is ret addr within CheckStackTraceLeaf
83 // stack[1] is ret addr within CheckStackTrace4
84 // ...
85 // stack[5] is ret addr within CheckStackTrace
86
87 //-----------------------------------------------------------------------//
88
89 const int kMaxFnLen = 0x40; // assume relevant functions are only this long
90
91 void CheckRetAddrIsInFunction( void * ret_addr, void * function_start_addr)
92 {
93   CHECK_GE(ret_addr, function_start_addr);
94   CHECK_LE(ret_addr, (void *) ((char *) function_start_addr + kMaxFnLen));
95 }
96
97 //-----------------------------------------------------------------------//
98
99 void CheckStackTraceLeaf(void) {
100   const int STACK_LEN = 10;
101   void *stack[STACK_LEN];
102   int size;
103
104   size = GetStackTrace(stack, STACK_LEN, 0);
105   printf("Obtained %d stack frames.\n", size);
106   CHECK_LE(size, STACK_LEN);
107
108   if (1) {
109 #ifdef HAVE_EXECINFO_H
110     char **strings = backtrace_symbols(stack, size);
111     printf("Obtained %d stack frames.\n", size);
112     for (int i = 0; i < size; i++)
113       printf("%s %p\n", strings[i], stack[i]);
114     printf("CheckStackTrace() addr: %p\n", &CheckStackTrace);
115     free(strings);
116 #endif
117   }
118   for (int i = 0; i < BACKTRACE_STEPS; i++) {
119     printf("Backtrace %d: expected: %p..%p  actual: %p ... ",
120            i, expected_stack[i],
121            reinterpret_cast<char*>(expected_stack[i]) + kMaxFnLen, stack[i]);
122     CheckRetAddrIsInFunction(stack[i], expected_stack[i]);
123     printf("OK\n");
124   }
125
126   // Check if the second stacktrace returns the same size.
127   CHECK_EQ(size, GetStackTrace(stack, STACK_LEN, 0));
128 }
129
130 //-----------------------------------------------------------------------//
131
132 /* Dummy functions to make the backtrace more interesting. */
133 void CheckStackTrace4(int i) { for (int j = i; j >= 0; j--) CheckStackTraceLeaf(); }
134 void CheckStackTrace3(int i) { for (int j = i; j >= 0; j--) CheckStackTrace4(j); }
135 void CheckStackTrace2(int i) { for (int j = i; j >= 0; j--) CheckStackTrace3(j); }
136 void CheckStackTrace1(int i) { for (int j = i; j >= 0; j--) CheckStackTrace2(j); }
137 void CheckStackTrace(int i)  { for (int j = i; j >= 0; j--) CheckStackTrace1(j); }
138
139 //-----------------------------------------------------------------------//
140
141 int main(int argc, char ** argv) {
142   FLAGS_logtostderr = true;
143   InitGoogleLogging(argv[0]);
144   
145   CheckStackTrace(0);
146   
147   printf("PASS\n");
148   return 0;
149 }
150
151 #else
152 int main() {
153   printf("PASS (no stacktrace support)\n");
154   return 0;
155 }
156 #endif  // HAVE_STACKTRACE