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.
30 #include "utilities.h"
35 #include "base/commandlineflags.h"
36 #include "glog/logging.h"
37 #include "stacktrace.h"
39 #ifdef HAVE_EXECINFO_H
40 # include <execinfo.h>
43 using namespace GOOGLE_NAMESPACE;
45 #ifdef HAVE_STACKTRACE
47 // Obtain a backtrace, verify that the expected callers are present in the
48 // backtrace, and maybe print the backtrace to stdout.
50 // The sequence of functions whose return addresses we expect to see in the
52 const int BACKTRACE_STEPS = 6;
55 const void *start, *end;
58 // Expected function [start,end] range.
59 AddressRange expected_range[BACKTRACE_STEPS];
62 // Using GCC extension: address of a label can be taken with '&&label'.
63 // Start should be a label somewhere before recursive call, end somewhere
65 #define INIT_ADDRESS_RANGE(fn, start_label, end_label, prange) \
67 (prange)->start = &&start_label; \
68 (prange)->end = &&end_label; \
69 CHECK_LT((prange)->start, (prange)->end); \
71 // This macro expands into "unmovable" code (opaque to GCC), and that
72 // prevents GCC from moving a_label up or down in the code.
73 // Without it, there is no code following the 'end' label, and GCC
74 // (4.3.1, 4.4.0) thinks it safe to assign &&end an address that is before
75 // the recursive call.
76 #define DECLARE_ADDRESS_LABEL(a_label) \
77 a_label: do { __asm__ __volatile__(""); } while (0)
78 // Gcc 4.4.0 may split function into multiple chunks, and the chunk
79 // performing recursive call may end up later in the code then the return
80 // instruction (this actually happens with FDO).
81 // Adjust function range from __builtin_return_address.
82 #define ADJUST_ADDRESS_RANGE_FROM_RA(prange) \
84 void *ra = __builtin_return_address(0); \
85 CHECK_LT((prange)->start, ra); \
86 if (ra > (prange)->end) { \
87 printf("Adjusting range from %p..%p to %p..%p\n", \
88 (prange)->start, (prange)->end, \
89 (prange)->start, ra); \
94 // Assume the Check* functions below are not longer than 256 bytes.
95 #define INIT_ADDRESS_RANGE(fn, start_label, end_label, prange) \
97 (prange)->start = reinterpret_cast<const void *>(&fn); \
98 (prange)->end = reinterpret_cast<const char *>(&fn) + 256; \
100 #define DECLARE_ADDRESS_LABEL(a_label) do { } while (0)
101 #define ADJUST_ADDRESS_RANGE_FROM_RA(prange) do { } while (0)
104 //-----------------------------------------------------------------------//
106 void CheckRetAddrIsInFunction(void *ret_addr, const AddressRange &range)
108 CHECK_GE(ret_addr, range.start);
109 CHECK_LE(ret_addr, range.end);
112 //-----------------------------------------------------------------------//
114 void ATTRIBUTE_NOINLINE CheckStackTrace(int);
115 void ATTRIBUTE_NOINLINE CheckStackTraceLeaf(void) {
116 const int STACK_LEN = 10;
117 void *stack[STACK_LEN];
120 ADJUST_ADDRESS_RANGE_FROM_RA(&expected_range[1]);
121 INIT_ADDRESS_RANGE(CheckStackTraceLeaf, start, end, &expected_range[0]);
122 DECLARE_ADDRESS_LABEL(start);
123 size = GetStackTrace(stack, STACK_LEN, 0);
124 printf("Obtained %d stack frames.\n", size);
126 CHECK_LE(size, STACK_LEN);
129 #ifdef HAVE_EXECINFO_H
130 char **strings = backtrace_symbols(stack, size);
131 printf("Obtained %d stack frames.\n", size);
132 for (int i = 0; i < size; i++)
133 printf("%s %p\n", strings[i], stack[i]);
134 printf("CheckStackTrace() addr: %p\n", &CheckStackTrace);
138 for (int i = 0; i < BACKTRACE_STEPS; i++) {
139 printf("Backtrace %d: expected: %p..%p actual: %p ... ",
140 i, expected_range[i].start, expected_range[i].end, stack[i]);
142 CheckRetAddrIsInFunction(stack[i], expected_range[i]);
145 DECLARE_ADDRESS_LABEL(end);
148 //-----------------------------------------------------------------------//
150 /* Dummy functions to make the backtrace more interesting. */
151 void ATTRIBUTE_NOINLINE CheckStackTrace4(int i) {
152 ADJUST_ADDRESS_RANGE_FROM_RA(&expected_range[2]);
153 INIT_ADDRESS_RANGE(CheckStackTrace4, start, end, &expected_range[1]);
154 DECLARE_ADDRESS_LABEL(start);
155 for (int j = i; j >= 0; j--)
156 CheckStackTraceLeaf();
157 DECLARE_ADDRESS_LABEL(end);
159 void ATTRIBUTE_NOINLINE CheckStackTrace3(int i) {
160 ADJUST_ADDRESS_RANGE_FROM_RA(&expected_range[3]);
161 INIT_ADDRESS_RANGE(CheckStackTrace3, start, end, &expected_range[2]);
162 DECLARE_ADDRESS_LABEL(start);
163 for (int j = i; j >= 0; j--)
165 DECLARE_ADDRESS_LABEL(end);
167 void ATTRIBUTE_NOINLINE CheckStackTrace2(int i) {
168 ADJUST_ADDRESS_RANGE_FROM_RA(&expected_range[4]);
169 INIT_ADDRESS_RANGE(CheckStackTrace2, start, end, &expected_range[3]);
170 DECLARE_ADDRESS_LABEL(start);
171 for (int j = i; j >= 0; j--)
173 DECLARE_ADDRESS_LABEL(end);
175 void ATTRIBUTE_NOINLINE CheckStackTrace1(int i) {
176 ADJUST_ADDRESS_RANGE_FROM_RA(&expected_range[5]);
177 INIT_ADDRESS_RANGE(CheckStackTrace1, start, end, &expected_range[4]);
178 DECLARE_ADDRESS_LABEL(start);
179 for (int j = i; j >= 0; j--)
181 DECLARE_ADDRESS_LABEL(end);
183 void ATTRIBUTE_NOINLINE CheckStackTrace(int i) {
184 INIT_ADDRESS_RANGE(CheckStackTrace, start, end, &expected_range[5]);
185 DECLARE_ADDRESS_LABEL(start);
186 for (int j = i; j >= 0; j--)
188 DECLARE_ADDRESS_LABEL(end);
191 //-----------------------------------------------------------------------//
193 int main(int, char ** argv) {
194 FLAGS_logtostderr = true;
195 InitGoogleLogging(argv[0]);
205 printf("PASS (no stacktrace support)\n");
208 #endif // HAVE_STACKTRACE