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