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"
44 #ifdef HAVE_LIB_GFLAGS
45 #include <gflags/gflags.h>
46 using namespace GFLAGS_NAMESPACE;
50 using namespace GOOGLE_NAMESPACE;
52 #if defined(HAVE_STACKTRACE)
56 // A wrapper function for Symbolize() to make the unit test simple.
57 static const char *TrySymbolize(void *pc) {
58 static char symbol[4096];
59 if (Symbolize(pc, symbol, sizeof(symbol))) {
68 // This unit tests make sense only with GCC.
69 // Uses lots of GCC specific features.
70 #if defined(__GNUC__) && !defined(__OPENCC__)
72 # define TEST_WITH_MODERN_GCC
73 # if __i386__ // always_inline isn't supported for x86_64 with GCC 4.1.0.
75 # define always_inline __attribute__((always_inline))
76 # define HAVE_ALWAYS_INLINE
79 # endif // __GNUC__ >= 4
80 # if defined(__i386__) || defined(__x86_64__)
81 # define TEST_X86_32_AND_64 1
82 # endif // defined(__i386__) || defined(__x86_64__)
85 // Make them C linkage to avoid mangled names.
87 void nonstatic_func() {
92 static void static_func() {
98 TEST(Symbolize, Symbolize) {
99 // We do C-style cast since GCC 2.95.3 doesn't allow
100 // reinterpret_cast<void *>(&func).
102 // Compilers should give us pointers to them.
103 EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
105 // The name of an internal linkage symbol is not specified; allow either a
106 // mangled or an unmangled name here.
107 const char *static_func_symbol = TrySymbolize((void *)(&static_func));
108 CHECK(NULL != static_func_symbol);
109 EXPECT_TRUE(strcmp("static_func", static_func_symbol) == 0 ||
110 strcmp("static_func()", static_func_symbol) == 0);
112 EXPECT_TRUE(NULL == TrySymbolize(NULL));
116 static void func(int x);
119 void ATTRIBUTE_NOINLINE Foo::func(int x) {
124 // With a modern GCC, Symbolize() should return demangled symbol
125 // names. Function parameters should be omitted.
126 #ifdef TEST_WITH_MODERN_GCC
127 TEST(Symbolize, SymbolizeWithDemangling) {
129 EXPECT_STREQ("Foo::func()", TrySymbolize((void *)(&Foo::func)));
133 // Tests that verify that Symbolize footprint is within some limit.
135 // To measure the stack footprint of the Symbolize function, we create
136 // a signal handler (for SIGUSR1 say) that calls the Symbolize function
137 // on an alternate stack. This alternate stack is initialized to some
138 // known pattern (0x55, 0x55, 0x55, ...). We then self-send this signal,
139 // and after the signal handler returns, look at the alternate stack
140 // buffer to see what portion has been touched.
142 // This trick gives us the the stack footprint of the signal handler.
143 // But the signal handler, even before the call to Symbolize, consumes
144 // some stack already. We however only want the stack usage of the
145 // Symbolize function. To measure this accurately, we install two signal
146 // handlers: one that does nothing and just returns, and another that
147 // calls Symbolize. The difference between the stack consumption of these
148 // two signals handlers should give us the Symbolize stack foorprint.
150 static void *g_pc_to_symbolize;
151 static char g_symbolize_buffer[4096];
152 static char *g_symbolize_result;
154 static void EmptySignalHandler(int signo) {}
156 static void SymbolizeSignalHandler(int signo) {
157 if (Symbolize(g_pc_to_symbolize, g_symbolize_buffer,
158 sizeof(g_symbolize_buffer))) {
159 g_symbolize_result = g_symbolize_buffer;
161 g_symbolize_result = NULL;
165 const int kAlternateStackSize = 8096;
166 const char kAlternateStackFillValue = 0x55;
168 // These helper functions look at the alternate stack buffer, and figure
169 // out what portion of this buffer has been touched - this is the stack
170 // consumption of the signal handler running on this alternate stack.
171 static ATTRIBUTE_NOINLINE bool StackGrowsDown(int *x) {
175 static int GetStackConsumption(const char* alt_stack) {
177 if (StackGrowsDown(&x)) {
178 for (int i = 0; i < kAlternateStackSize; i++) {
179 if (alt_stack[i] != kAlternateStackFillValue) {
180 return (kAlternateStackSize - i);
184 for (int i = (kAlternateStackSize - 1); i >= 0; i--) {
185 if (alt_stack[i] != kAlternateStackFillValue) {
193 #ifdef HAVE_SIGALTSTACK
195 // Call Symbolize and figure out the stack footprint of this call.
196 static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) {
198 g_pc_to_symbolize = pc;
200 // The alt-signal-stack cannot be heap allocated because there is a
201 // bug in glibc-2.2 where some signal handler setup code looks at the
202 // current stack pointer to figure out what thread is currently running.
203 // Therefore, the alternate stack must be allocated from the main stack
205 char altstack[kAlternateStackSize];
206 memset(altstack, kAlternateStackFillValue, kAlternateStackSize);
208 // Set up the alt-signal-stack (and save the older one).
210 memset(&sigstk, 0, sizeof(stack_t));
212 sigstk.ss_sp = altstack;
213 sigstk.ss_size = kAlternateStackSize;
215 CHECK_ERR(sigaltstack(&sigstk, &old_sigstk));
217 // Set up SIGUSR1 and SIGUSR2 signal handlers (and save the older ones).
219 memset(&sa, 0, sizeof(struct sigaction));
220 struct sigaction old_sa1, old_sa2;
221 sigemptyset(&sa.sa_mask);
222 sa.sa_flags = SA_ONSTACK;
224 // SIGUSR1 maps to EmptySignalHandler.
225 sa.sa_handler = EmptySignalHandler;
226 CHECK_ERR(sigaction(SIGUSR1, &sa, &old_sa1));
228 // SIGUSR2 maps to SymbolizeSignalHanlder.
229 sa.sa_handler = SymbolizeSignalHandler;
230 CHECK_ERR(sigaction(SIGUSR2, &sa, &old_sa2));
232 // Send SIGUSR1 signal and measure the stack consumption of the empty
234 CHECK_ERR(kill(getpid(), SIGUSR1));
235 int stack_consumption1 = GetStackConsumption(altstack);
237 // Send SIGUSR2 signal and measure the stack consumption of the symbolize
239 CHECK_ERR(kill(getpid(), SIGUSR2));
240 int stack_consumption2 = GetStackConsumption(altstack);
242 // The difference between the two stack consumption values is the
243 // stack footprint of the Symbolize function.
244 if (stack_consumption1 != -1 && stack_consumption2 != -1) {
245 *stack_consumed = stack_consumption2 - stack_consumption1;
247 *stack_consumed = -1;
250 // Log the stack consumption values.
251 LOG(INFO) << "Stack consumption of empty signal handler: "
252 << stack_consumption1;
253 LOG(INFO) << "Stack consumption of symbolize signal handler: "
254 << stack_consumption2;
255 LOG(INFO) << "Stack consumption of Symbolize: " << *stack_consumed;
257 // Now restore the old alt-signal-stack and signal handlers.
258 CHECK_ERR(sigaltstack(&old_sigstk, NULL));
259 CHECK_ERR(sigaction(SIGUSR1, &old_sa1, NULL));
260 CHECK_ERR(sigaction(SIGUSR2, &old_sa2, NULL));
262 return g_symbolize_result;
266 // Symbolize stack consumption should be within 4kB.
267 const int kStackConsumptionUpperLimit = 4096;
269 // Symbolize stack consumption should be within 2kB.
270 const int kStackConsumptionUpperLimit = 2048;
273 TEST(Symbolize, SymbolizeStackConsumption) {
277 symbol = SymbolizeStackConsumption((void *)(&nonstatic_func),
279 EXPECT_STREQ("nonstatic_func", symbol);
280 EXPECT_GT(stack_consumed, 0);
281 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
283 // The name of an internal linkage symbol is not specified; allow either a
284 // mangled or an unmangled name here.
285 symbol = SymbolizeStackConsumption((void *)(&static_func),
287 CHECK(NULL != symbol);
288 EXPECT_TRUE(strcmp("static_func", symbol) == 0 ||
289 strcmp("static_func()", symbol) == 0);
290 EXPECT_GT(stack_consumed, 0);
291 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
294 #ifdef TEST_WITH_MODERN_GCC
295 TEST(Symbolize, SymbolizeWithDemanglingStackConsumption) {
300 symbol = SymbolizeStackConsumption((void *)(&Foo::func), &stack_consumed);
302 EXPECT_STREQ("Foo::func()", symbol);
303 EXPECT_GT(stack_consumed, 0);
304 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
308 #endif // HAVE_SIGALTSTACK
310 // x86 specific tests. Uses some inline assembler.
312 inline void* always_inline inline_func() {
313 register void *pc = NULL;
314 #ifdef TEST_X86_32_AND_64
315 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
320 void* ATTRIBUTE_NOINLINE non_inline_func() {
321 register void *pc = NULL;
322 #ifdef TEST_X86_32_AND_64
323 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
328 void ATTRIBUTE_NOINLINE TestWithPCInsideNonInlineFunction() {
329 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ATTRIBUTE_NOINLINE)
330 void *pc = non_inline_func();
331 const char *symbol = TrySymbolize(pc);
332 CHECK(symbol != NULL);
333 CHECK_STREQ(symbol, "non_inline_func");
334 cout << "Test case TestWithPCInsideNonInlineFunction passed." << endl;
338 void ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() {
339 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ALWAYS_INLINE)
340 void *pc = inline_func(); // Must be inlined.
341 const char *symbol = TrySymbolize(pc);
342 CHECK(symbol != NULL);
343 CHECK_STREQ(symbol, __FUNCTION__);
344 cout << "Test case TestWithPCInsideInlineFunction passed." << endl;
349 // Test with a return address.
350 void ATTRIBUTE_NOINLINE TestWithReturnAddress() {
351 #if defined(HAVE_ATTRIBUTE_NOINLINE)
352 void *return_address = __builtin_return_address(0);
353 const char *symbol = TrySymbolize(return_address);
354 CHECK(symbol != NULL);
355 CHECK_STREQ(symbol, "main");
356 cout << "Test case TestWithReturnAddress passed." << endl;
360 # elif defined(OS_WINDOWS)
363 #pragma intrinsic(_ReturnAddress)
366 static void func(int x);
369 __declspec(noinline) void Foo::func(int x) {
374 TEST(Symbolize, SymbolizeWithDemangling) {
376 const char* ret = TrySymbolize((void *)(&Foo::func));
377 EXPECT_STREQ("public: static void __cdecl Foo::func(int)", ret);
380 __declspec(noinline) void TestWithReturnAddress() {
381 void *return_address = _ReturnAddress();
382 const char *symbol = TrySymbolize(return_address);
383 CHECK(symbol != NULL);
384 CHECK_STREQ(symbol, "main");
385 cout << "Test case TestWithReturnAddress passed." << endl;
388 #endif // HAVE_STACKTRACE
390 int main(int argc, char **argv) {
391 FLAGS_logtostderr = true;
392 InitGoogleLogging(argv[0]);
393 InitGoogleTest(&argc, argv);
394 #if defined(HAVE_SYMBOLIZE)
395 # if defined(__ELF__)
396 // We don't want to get affected by the callback interface, that may be
397 // used to install some callback function at InitGoogle() time.
398 InstallSymbolizeCallback(NULL);
400 TestWithPCInsideInlineFunction();
401 TestWithPCInsideNonInlineFunction();
402 TestWithReturnAddress();
403 return RUN_ALL_TESTS();
404 # elif defined(OS_WINDOWS)
405 TestWithReturnAddress();
406 return RUN_ALL_TESTS();
408 printf("PASS (no symbolize_unittest support)\n");
412 printf("PASS (no symbolize support)\n");
414 #endif // HAVE_SYMBOLIZE