1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: Satoru Takabayashi
4 // Unit tests for functions in symbolize.cc.
11 #include "glog/logging.h"
12 #include "symbolize.h"
13 #include "googletest.h"
17 using namespace GOOGLE_NAMESPACE;
19 #if defined(HAVE_STACKTRACE) && defined(__ELF__)
23 // This unit tests make sense only with GCC.
24 // Uses lots of GCC specific features.
25 #if defined(__GNUC__) && !defined(__OPENCC__)
27 # define TEST_WITH_MODERN_GCC
28 # if __i386__ // always_inline isn't supported for x86_64 with GCC 4.1.0.
30 # define always_inline __attribute__((always_inline))
31 # define HAVE_ALWAYS_INLINE
34 # endif // __GNUC__ >= 4
35 # if defined(__i386__) || defined(__x86_64__)
36 # define TEST_X86_32_AND_64 1
37 # endif // defined(__i386__) || defined(__x86_64__)
40 // A wrapper function for Symbolize() to make the unit test simple.
41 static const char *TrySymbolize(void *pc) {
42 static char symbol[4096];
43 if (Symbolize(pc, symbol, sizeof(symbol))) {
50 // Make them C linkage to avoid mangled names.
52 void nonstatic_func() {
57 static void static_func() {
63 TEST(Symbolize, Symbolize) {
64 // We do C-style cast since GCC 2.95.3 doesn't allow
65 // reinterpret_cast<void *>(&func).
67 // Compilers should give us pointers to them.
68 EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
69 EXPECT_STREQ("static_func", TrySymbolize((void *)(&static_func)));
71 EXPECT_TRUE(NULL == TrySymbolize(NULL));
75 static void func(int x);
78 void ATTRIBUTE_NOINLINE Foo::func(int x) {
83 // With a modern GCC, Symbolize() should return demangled symbol
84 // names. Function parameters should be omitted.
85 #ifdef TEST_WITH_MODERN_GCC
86 TEST(Symbolize, SymbolizeWithDemangling) {
88 EXPECT_STREQ("Foo::func()", TrySymbolize((void *)(&Foo::func)));
92 // Tests that verify that Symbolize footprint is within some limit.
94 // To measure the stack footprint of the Symbolize function, we create
95 // a signal handler (for SIGUSR1 say) that calls the Symbolize function
96 // on an alternate stack. This alternate stack is initialized to some
97 // known pattern (0x55, 0x55, 0x55, ...). We then self-send this signal,
98 // and after the signal handler returns, look at the alternate stack
99 // buffer to see what portion has been touched.
101 // This trick gives us the the stack footprint of the signal handler.
102 // But the signal handler, even before the call to Symbolize, consumes
103 // some stack already. We however only want the stack usage of the
104 // Symbolize function. To measure this accurately, we install two signal
105 // handlers: one that does nothing and just returns, and another that
106 // calls Symbolize. The difference between the stack consumption of these
107 // two signals handlers should give us the Symbolize stack foorprint.
109 static void *g_pc_to_symbolize;
110 static char g_symbolize_buffer[4096];
111 static char *g_symbolize_result;
113 static void EmptySignalHandler(int signo) {}
115 static void SymbolizeSignalHandler(int signo) {
116 if (Symbolize(g_pc_to_symbolize, g_symbolize_buffer,
117 sizeof(g_symbolize_buffer))) {
118 g_symbolize_result = g_symbolize_buffer;
120 g_symbolize_result = NULL;
124 const int kAlternateStackSize = 8096;
125 const char kAlternateStackFillValue = 0x55;
127 // These helper functions look at the alternate stack buffer, and figure
128 // out what portion of this buffer has been touched - this is the stack
129 // consumption of the signal handler running on this alternate stack.
130 static ATTRIBUTE_NOINLINE bool StackGrowsDown(int *x) {
134 static int GetStackConsumption(const char* alt_stack) {
136 if (StackGrowsDown(&x)) {
137 for (int i = 0; i < kAlternateStackSize; i++) {
138 if (alt_stack[i] != kAlternateStackFillValue) {
139 return (kAlternateStackSize - i);
143 for (int i = (kAlternateStackSize - 1); i >= 0; i--) {
144 if (alt_stack[i] != kAlternateStackFillValue) {
152 #ifdef HAVE_SIGALTSTACK
154 // Call Symbolize and figure out the stack footprint of this call.
155 static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) {
157 g_pc_to_symbolize = pc;
159 // The alt-signal-stack cannot be heap allocated because there is a
160 // bug in glibc-2.2 where some signal handler setup code looks at the
161 // current stack pointer to figure out what thread is currently running.
162 // Therefore, the alternate stack must be allocated from the main stack
164 char altstack[kAlternateStackSize];
165 memset(altstack, kAlternateStackFillValue, kAlternateStackSize);
167 // Set up the alt-signal-stack (and save the older one).
168 stack_t sigstk = {}; // Zero-clear.
170 sigstk.ss_sp = altstack;
171 sigstk.ss_size = kAlternateStackSize;
173 CHECK_ERR(sigaltstack(&sigstk, &old_sigstk));
175 // Set up SIGUSR1 and SIGUSR2 signal handlers (and save the older ones).
176 struct sigaction sa = {}; // Zero-clear;
177 struct sigaction old_sa1, old_sa2;
178 sigemptyset(&sa.sa_mask);
179 sa.sa_flags = SA_ONSTACK;
181 // SIGUSR1 maps to EmptySignalHandler.
182 sa.sa_handler = EmptySignalHandler;
183 CHECK_ERR(sigaction(SIGUSR1, &sa, &old_sa1));
185 // SIGUSR2 maps to SymbolizeSignalHanlder.
186 sa.sa_handler = SymbolizeSignalHandler;
187 CHECK_ERR(sigaction(SIGUSR2, &sa, &old_sa2));
189 // Send SIGUSR1 signal and measure the stack consumption of the empty
191 CHECK_ERR(kill(getpid(), SIGUSR1));
192 int stack_consumption1 = GetStackConsumption(altstack);
194 // Send SIGUSR2 signal and measure the stack consumption of the symbolize
196 CHECK_ERR(kill(getpid(), SIGUSR2));
197 int stack_consumption2 = GetStackConsumption(altstack);
199 // The difference between the two stack consumption values is the
200 // stack footprint of the Symbolize function.
201 if (stack_consumption1 != -1 && stack_consumption2 != -1) {
202 *stack_consumed = stack_consumption2 - stack_consumption1;
204 *stack_consumed = -1;
207 // Log the stack consumption values.
208 LOG(INFO) << "Stack consumption of empty signal handler: "
209 << stack_consumption1;
210 LOG(INFO) << "Stack consumption of symbolize signal handler: "
211 << stack_consumption2;
212 LOG(INFO) << "Stack consumption of Symbolize: " << *stack_consumed;
214 // Now restore the old alt-signal-stack and signal handlers.
215 CHECK_ERR(sigaltstack(&old_sigstk, NULL));
216 CHECK_ERR(sigaction(SIGUSR1, &old_sa1, NULL));
217 CHECK_ERR(sigaction(SIGUSR2, &old_sa2, NULL));
219 return g_symbolize_result;
222 // Symbolize stack consumption should be within 2kB.
223 const int kStackConsumptionUpperLimit = 2048;
225 TEST(Symbolize, SymbolizeStackConsumption) {
229 symbol = SymbolizeStackConsumption((void *)(&nonstatic_func),
231 EXPECT_STREQ("nonstatic_func", symbol);
232 EXPECT_GT(stack_consumed, 0);
233 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
235 symbol = SymbolizeStackConsumption((void *)(&static_func),
237 EXPECT_STREQ("static_func", symbol);
238 EXPECT_GT(stack_consumed, 0);
239 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
242 #ifdef TEST_WITH_MODERN_GCC
243 TEST(Symbolize, SymbolizeWithDemanglingStackConsumption) {
248 symbol = SymbolizeStackConsumption((void *)(&Foo::func), &stack_consumed);
250 EXPECT_STREQ("Foo::func()", symbol);
251 EXPECT_GT(stack_consumed, 0);
252 EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
256 #endif // HAVE_SIGALTSTACK
258 // x86 specific tests. Uses some inline assembler.
260 inline void* always_inline inline_func() {
261 register void *pc = NULL;
262 #ifdef TEST_X86_32_AND_64
263 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
268 void* ATTRIBUTE_NOINLINE non_inline_func() {
269 register void *pc = NULL;
270 #ifdef TEST_X86_32_AND_64
271 __asm__ __volatile__("call 1f; 1: pop %0" : "=r"(pc));
276 void ATTRIBUTE_NOINLINE TestWithPCInsideNonInlineFunction() {
277 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ATTRIBUTE_NOINLINE)
278 void *pc = non_inline_func();
279 const char *symbol = TrySymbolize(pc);
280 CHECK(symbol != NULL);
281 CHECK_STREQ(symbol, "non_inline_func");
282 cout << "Test case TestWithPCInsideNonInlineFunction passed." << endl;
286 void ATTRIBUTE_NOINLINE TestWithPCInsideInlineFunction() {
287 #if defined(TEST_X86_32_AND_64) && defined(HAVE_ALWAYS_INLINE)
288 void *pc = inline_func(); // Must be inlined.
289 const char *symbol = TrySymbolize(pc);
290 CHECK(symbol != NULL);
291 CHECK_STREQ(symbol, __FUNCTION__);
292 cout << "Test case TestWithPCInsideInlineFunction passed." << endl;
297 // Test with a return address.
298 void ATTRIBUTE_NOINLINE TestWithReturnAddress() {
299 #if defined(HAVE_ATTRIBUTE_NOINLINE)
300 void *return_address = __builtin_return_address(0);
301 const char *symbol = TrySymbolize(return_address);
302 CHECK(symbol != NULL);
303 CHECK_STREQ(symbol, "main");
304 cout << "Test case TestWithReturnAddress passed." << endl;
308 int main(int argc, char **argv) {
309 FLAGS_logtostderr = true;
310 InitGoogleLogging(argv[0]);
311 InitGoogleTest(&argc, argv);
312 #ifdef HAVE_SYMBOLIZE
313 // We don't want to get affected by the callback interface, that may be
314 // used to install some callback function at InitGoogle() time.
315 InstallSymbolizeCallback(NULL);
317 TestWithPCInsideInlineFunction();
318 TestWithPCInsideNonInlineFunction();
319 TestWithReturnAddress();
320 return RUN_ALL_TESTS();
328 #ifdef HAVE_SYMBOLIZE
329 printf("PASS (no symbolize_unittest support)\n");
331 printf("PASS (no symbolize support)\n");
335 #endif // HAVE_STACKTRACE