1 // Copyright (c) 2004, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "utilities.h"
34 #include "base/commandlineflags.h"
35 #include "glog/logging.h"
36 #include "stacktrace.h"
38 #ifdef HAVE_EXECINFO_H
39 # include <execinfo.h>
42 using namespace GOOGLE_NAMESPACE;
44 #ifdef HAVE_STACKTRACE
46 // Obtain a backtrace, verify that the expected callers are present in the
47 // backtrace, and maybe print the backtrace to stdout.
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 //-----------------------------------------------------------------------//
58 // The sequence of functions whose return addresses we expect to see in the
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,
70 // Depending on the architecture/compiler/libraries, (not sure which)
71 // the current function may or may not appear in the backtrace.
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
82 // stack[0] is ret addr within CheckStackTraceLeaf
83 // stack[1] is ret addr within CheckStackTrace4
85 // stack[5] is ret addr within CheckStackTrace
87 //-----------------------------------------------------------------------//
89 const int kMaxFnLen = 0x40; // assume relevant functions are only this long
91 void CheckRetAddrIsInFunction( void * ret_addr, void * function_start_addr)
93 CHECK_GE(ret_addr, function_start_addr);
94 CHECK_LE(ret_addr, (void *) ((char *) function_start_addr + kMaxFnLen));
97 //-----------------------------------------------------------------------//
99 void CheckStackTraceLeaf(void) {
100 const int STACK_LEN = 10;
101 void *stack[STACK_LEN];
104 size = GetStackTrace(stack, STACK_LEN, 0);
105 printf("Obtained %d stack frames.\n", size);
106 CHECK_LE(size, STACK_LEN);
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);
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]);
126 // Check if the second stacktrace returns the same size.
127 CHECK_EQ(size, GetStackTrace(stack, STACK_LEN, 0));
130 //-----------------------------------------------------------------------//
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); }
139 //-----------------------------------------------------------------------//
141 int main(int argc, char ** argv) {
142 FLAGS_logtostderr = true;
143 InitGoogleLogging(argv[0]);
153 printf("PASS (no stacktrace support)\n");
156 #endif // HAVE_STACKTRACE