stack_unwinding_test.cpp revision 190dce9e56c750be6b8d113ffdd32a9c20c19e3d
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* 18 * Contributed by: Intel Corporation 19 */ 20 21#include <gtest/gtest.h> 22 23#include <dlfcn.h> 24#include <signal.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <sys/types.h> 29#include <unistd.h> 30#include <unwind.h> 31 32#include "ScopedSignalHandler.h" 33 34#define noinline __attribute__((noinline)) 35 36static _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) { 37 int* count_ptr = reinterpret_cast<int*>(arg); 38 39#if SHOW_FRAME_LOCATIONS 40 void* ip = reinterpret_cast<void*>(_Unwind_GetIP(ctx)); 41 42 const char* symbol = "<unknown>"; 43 int offset = 0; 44 45 Dl_info info; 46 memset(&info, 0, sizeof(info)); 47 if (dladdr(ip, &info) != 0) { 48 symbol = info.dli_sname; 49 if (info.dli_saddr != nullptr) { 50 offset = static_cast<int>(reinterpret_cast<char*>(ip) - reinterpret_cast<char*>(info.dli_saddr)); 51 } 52 } 53 54 fprintf(stderr, " #%02d %p %s%+d (%s)\n", *count_ptr, ip, symbol, offset, info.dli_fname ? info.dli_fname : "??"); 55 fflush(stderr); 56#endif 57 58 ++*count_ptr; 59 return _URC_NO_REASON; 60} 61 62static int noinline unwind_one_frame_deeper() { 63 int count = 0; 64 _Unwind_Backtrace(FrameCounter, &count); 65 return count; 66} 67 68TEST(stack_unwinding, easy) { 69 int count = 0; 70 _Unwind_Backtrace(FrameCounter, &count); 71 int deeper_count = unwind_one_frame_deeper(); 72 ASSERT_EQ(count + 1, deeper_count); 73} 74 75static int killer_count = 0; 76static int handler_count = 0; 77static int handler_one_deeper_count = 0; 78 79static void noinline UnwindSignalHandler(int) { 80 _Unwind_Backtrace(FrameCounter, &handler_count); 81 ASSERT_GT(handler_count, killer_count); 82 83 handler_one_deeper_count = unwind_one_frame_deeper(); 84 ASSERT_EQ(handler_count + 1, handler_one_deeper_count); 85} 86 87TEST(stack_unwinding, unwind_through_signal_frame) { 88 ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler); 89 90 _Unwind_Backtrace(FrameCounter, &killer_count); 91 92 ASSERT_EQ(0, kill(getpid(), SIGUSR1)); 93} 94 95extern "C" void unwind_through_frame_with_cleanup_function(); 96 97// We have to say "DeathTest" here so gtest knows to run this test (which exits) 98// in its own process. 99TEST(stack_unwinding_DeathTest, unwind_through_frame_with_cleanup_function) { 100 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; 101 ASSERT_EXIT(unwind_through_frame_with_cleanup_function(), ::testing::ExitedWithCode(42), ""); 102} 103