1 // Copyright (c) 2006, 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 // Author: Satoru Takabayashi
32 // Unit tests for functions in symbolize.cc.
34 #include "utilities.h"
39 #include "glog/logging.h"
40 #include "symbolize.h"
41 #include "googletest.h"
45 using namespace GOOGLE_NAMESPACE;
47 #if defined(HAVE_STACKTRACE) && defined(__ELF__)
51 // This unit tests make sense only with GCC.
52 // Uses lots of GCC specific features.
53 #if defined(__GNUC__) && !defined(__OPENCC__)
55 # define TEST_WITH_MODERN_GCC
56 # if __i386__ // always_inline isn't supported for x86_64 with GCC 4.1.0.
58 # define always_inline __attribute__((always_inline))
59 # define HAVE_ALWAYS_INLINE
62 # endif // __GNUC__ >= 4
63 # if defined(__i386__) || defined(__x86_64__)
64 # define TEST_X86_32_AND_64 1
65 # endif // defined(__i386__) || defined(__x86_64__)
68 // A wrapper function for Symbolize() to make the unit test simple.
69 static const char *TrySymbolize(void *pc) {
70 static char symbol[4096];
71 if (Symbolize(pc, symbol, sizeof(symbol))) {
78 // Make them C linkage to avoid mangled names.
80 void nonstatic_func() {
85 static void static_func() {
91 TEST(Symbolize, Symbolize) {
92 // We do C-style cast since GCC 2.95.3 doesn't allow
93 // reinterpret_cast<void *>(&func).
95 // Compilers should give us pointers to them.
96 EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
97 EXPECT_STREQ("static_func", TrySymbolize((void *)(&static_func)));
99 EXPECT_TRUE(NULL == TrySymbolize(NULL));
103 static void func(int x);
106 void ATTRIBUTE_NOINLINE Foo::func(int x) {
111 // With a modern GCC, Symbolize() should return demangled symbol
112 // names. Function parameters should be omitted.
113 #ifdef TEST_WITH_MODERN_GCC
114 TEST(Symbolize, SymbolizeWithDemangling) {
116 EXPECT_STREQ("Foo::func()", TrySymbolize((void *)(&Foo::func)));
120 // Tests that verify that Symbolize footprint is within some limit.
122 // To measure the stack footprint of the Symbolize function, we create
123 // a signal handler (for SIGUSR1 say) that calls the Symbolize function
124 // on an alternate stack. This alternate stack is initialized to some
125 // known pattern (0x55, 0x55, 0x55, ...). We then self-send this signal,
126 // and after the signal handler returns, look at the alternate stack
127 // buffer to see what portion has been touched.
129 // This trick gives us the the stack footprint of the signal handler.
130 // But the signal handler, even before the call to Symbolize, consumes
131 // some stack already. We however only want the stack usage of the
132 // Symbolize function. To measure this accurately, we install two signal
133 // handlers: one that does nothing and just returns, and another that
134 // calls Symbolize. The difference between the stack consumption of these
135 // two signals handlers should give us the Symbolize stack foorprint.
137 static void *g_pc_to_symbolize;
138 static char g_symbolize_buffer[4096];
139 static char *g_symbolize_result;
141 static void EmptySignalHandler(int signo) {}
143 static void SymbolizeSignalHandler(int signo) {
144 if (Symbolize(g_pc_to_symbolize, g_symbolize_buffer,
145 sizeof(g_symbolize_buffer))) {
146 g_symbolize_result = g_symbolize_buffer;
148 g_symbolize_result = NULL;
152 const int kAlternateStackSize = 8096;
153 const char kAlternateStackFillValue = 0x55;
155 // These helper functions look at the alternate stack buffer, and figure
156 // out what portion of this buffer has been touched - this is the stack
157 // consumption of the signal handler running on this alternate stack.
158 static ATTRIBUTE_NOINLINE bool StackGrowsDown(int *x) {
162 static int GetStackConsumption(const char* alt_stack) {
164 if (StackGrowsDown(&x)) {
165 for (int i = 0; i < kAlternateStackSize; i++) {
166 if (alt_stack[i] != kAlternateStackFillValue) {
167 return (kAlternateStackSize - i);
171 for (int i = (kAlternateStackSize - 1); i >= 0; i--) {
172 if (alt_stack[i] != kAlternateStackFillValue) {
180 #ifdef HAVE_SIGALTSTACK
182 // Call Symbolize and figure out the stack footprint of this call.
183 static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) {
185 g_pc_to_symbolize = pc;
187 // The alt-signal-stack cannot be heap allocated because there is a
188 // bug in glibc-2.2 where some signal handler setup code looks at the
189 // current stack pointer to figure out what thread is currently running.
190 // Therefore, the alternate stack must be allocated from the main stack
192 char altstack[kAlternateStackSize];
193 memset(altstack, kAlternateStackFillValue, kAlternateStackSize);
195 // Set up the alt-signal-stack (and save the older one).
196 stack_t sigstk = {}; // Zero-clear.
198 sigstk.ss_sp = altstack;
199 sigstk.ss_size = kAlternateStackSize;
201 CHECK_ERR(sigaltstack(&sigstk, &old_sigstk));
203 // Set up SIGUSR1 and SIGUSR2 signal handlers (and save the older ones).
204 struct sigaction sa = {}; // Zero-clear;
205 struct sigaction old_sa1, old_sa2;
206 sigemptyset(&sa.sa_mask);
207 sa.sa_flags = SA_ONSTACK;
209 // SIGUSR1 maps to EmptySignalHandler.
210 sa.sa_handler = EmptySignalHandler;
211 CHECK_ERR(sigaction(SIGUSR1, &sa, &old_sa1));
213 // SIGUSR2 maps to SymbolizeSignalHanlder.
214 sa.sa_handler = SymbolizeSignalHandler;
215 CHECK_ERR(sigaction(SIGUSR2, &sa, &old_sa2));
217 // Send SIGUSR1 signal and measure the stack consumption of the empty
219 CHECK_ERR(kill(getpid(), SIGUSR1));
220 int stack_consumption1 = GetStackConsumption(altstack);
222 // Send SIGUSR2 signal and measure the stack consumption of the symbolize
224 CHECK_ERR(kill(getpid(), SIGUSR2));
225 int stack_consumption2 = GetStackConsumption(altstack);
227 // The difference between the two stack consumption values is the
228 // stack footprint of the Symbolize function.
229 if (stack_consumption1 != -1 && stack_consumption2 != -1) {
230 *stack_consumed = stack_consumption2 - stack_consumption1;
232 *stack_consumed = -1;
235 // Log the stack consumption values.
236 LOG(INFO) << "Stack consumption of empty signal handler: "
237 << stack_consumption1;
238 LOG(INFO) << "Stack consumption of symbolize signal handler: "
239 << stack_consumption2;
240 LOG(INFO) << "Stack consumption of Symbolize: " << *stack_consumed;
242 // Now restore the old alt-signal-stack and signal handlers.
243 CHECK_ERR(sigaltstack(&old_sigstk, NULL));
244 CHECK_ERR(sigaction(SIGUSR1, &old_sa1, NULL));
245 CHECK_ERR(sigaction(SIGUSR2, &old_sa2, NULL));
247 return g_symbolize_result;
250 // Symbolize stack consumption should be within 2kB.
251 const int kStackConsumptionUpperLimit = 2048;
253 TEST(Symbolize, SymbolizeStackConsumption) {
257 symbol = SymbolizeStackConsumption((void *)(&nonstatic_func),
259 EXPECT_STREQ("nonstatic_func", symbol);
260 EXPECT_GT(stack_consumed, 0);
261 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
263 symbol = SymbolizeStackConsumption((void *)(&static_func),
265 EXPECT_STREQ("static_func", symbol);
266 EXPECT_GT(stack_consumed, 0);
267 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
270 #ifdef TEST_WITH_MODERN_GCC
271 TEST(Symbolize, SymbolizeWithDemanglingStackConsumption) {
276 symbol = SymbolizeStackConsumption((void *)(&Foo::func), &stack_consumed);
278 EXPECT_STREQ("Foo::func()", symbol);
279 EXPECT_GT(stack_consumed, 0);
280 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
284 #endif // HAVE_SIGALTSTACK
286 // x86 specific tests. Uses some inline assembler.
288 inline void* always_inline inline_func() {
289 register void *pc = NULL;
290 #ifdef TEST_X86_32_AND_64
291 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
296 void* ATTRIBUTE_NOINLINE non_inline_func() {
297 register void *pc = NULL;
298 #ifdef TEST_X86_32_AND_64
299 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
304 void ATTRIBUTE_NOINLINE TestWithPCInsideNonInlineFunction() {
305 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ATTRIBUTE_NOINLINE)
306 void *pc = non_inline_func();
307 const char *symbol = TrySymbolize(pc);
308 CHECK(symbol != NULL);
309 CHECK_STREQ(symbol, "non_inline_func");
310 cout << "Test case TestWithPCInsideNonInlineFunction passed." << endl;
314 void ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() {
315 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ALWAYS_INLINE)
316 void *pc = inline_func(); // Must be inlined.
317 const char *symbol = TrySymbolize(pc);
318 CHECK(symbol != NULL);
319 CHECK_STREQ(symbol, __FUNCTION__);
320 cout << "Test case TestWithPCInsideInlineFunction passed." << endl;
325 // Test with a return address.
326 void ATTRIBUTE_NOINLINE TestWithReturnAddress() {
327 #if defined(HAVE_ATTRIBUTE_NOINLINE)
328 void *return_address = __builtin_return_address(0);
329 const char *symbol = TrySymbolize(return_address);
330 CHECK(symbol != NULL);
331 CHECK_STREQ(symbol, "main");
332 cout << "Test case TestWithReturnAddress passed." << endl;
336 int main(int argc, char **argv) {
337 FLAGS_logtostderr = true;
338 InitGoogleLogging(argv[0]);
339 InitGoogleTest(&argc, argv);
340 #ifdef HAVE_SYMBOLIZE
341 // We don't want to get affected by the callback interface, that may be
342 // used to install some callback function at InitGoogle() time.
343 InstallSymbolizeCallback(NULL);
345 TestWithPCInsideInlineFunction();
346 TestWithPCInsideNonInlineFunction();
347 TestWithReturnAddress();
348 return RUN_ALL_TESTS();
356 #ifdef HAVE_SYMBOLIZE
357 printf("PASS (no symbolize_unittest support)\n");
359 printf("PASS (no symbolize support)\n");
363 #endif // HAVE_STACKTRACE