2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Contributed by: Intel Corporation
21 #include <gtest/gtest.h>
28 #include <sys/types.h>
32 #include "ScopedSignalHandler.h"
34 #define noinline __attribute__((__noinline__))
35 #define __unused __attribute__((__unused__))
37 _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) {
38 int* count_ptr = reinterpret_cast<int*>(arg);
40 #if SHOW_FRAME_LOCATIONS
41 void* ip = reinterpret_cast<void*>(_Unwind_GetIP(ctx));
43 const char* symbol = "<unknown>";
47 memset(&info, 0, sizeof(info));
48 if (dladdr(ip, &info) != 0) {
49 symbol = info.dli_sname;
50 if (info.dli_saddr != nullptr) {
51 offset = static_cast<int>(reinterpret_cast<char*>(ip) - reinterpret_cast<char*>(info.dli_saddr));
55 fprintf(stderr, " #%02d %p %s%+d (%s)\n", *count_ptr, ip, symbol, offset, info.dli_fname ? info.dli_fname : "??");
60 return _URC_NO_REASON;
63 static int noinline unwind_one_frame_deeper() {
65 _Unwind_Backtrace(FrameCounter, &count);
69 TEST(stack_unwinding, easy) {
71 _Unwind_Backtrace(FrameCounter, &count);
72 int deeper_count = unwind_one_frame_deeper();
73 ASSERT_EQ(count + 1, deeper_count);
77 volatile bool signal_handler_complete = false;
78 int expected_frame_count = 0;
79 int handler_frame_count = 0;
80 int handler_one_deeper_frame_count = 0;
83 static UnwindData g_unwind_data;
85 static void noinline UnwindSignalHandler(int) {
86 _Unwind_Backtrace(FrameCounter, &g_unwind_data.handler_frame_count);
88 g_unwind_data.handler_one_deeper_frame_count = unwind_one_frame_deeper();
89 g_unwind_data.signal_handler_complete = true;
92 static void verify_unwind_data(const UnwindData& unwind_data) {
93 // In order to avoid a false positive, the caller must have at least 2 frames
94 // outside of the signal handler. This avoids a case where the only frame
95 // right after the signal handler winds up being garbage.
96 EXPECT_GT(unwind_data.handler_frame_count, unwind_data.expected_frame_count + 1);
98 EXPECT_EQ(unwind_data.handler_frame_count + 1, unwind_data.handler_one_deeper_frame_count);
101 static void noinline UnwindTest() {
104 _Unwind_Backtrace(FrameCounter, &g_unwind_data.expected_frame_count);
105 ASSERT_LE(2, g_unwind_data.expected_frame_count)
106 << "The current call must contain at least 2 frames for the test to be valid.";
108 ASSERT_EQ(0, kill(getpid(), SIGUSR1));
109 while (!g_unwind_data.signal_handler_complete) {}
111 verify_unwind_data(g_unwind_data);
114 TEST(stack_unwinding, unwind_through_signal_frame) {
115 ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler);
120 // On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore.
121 TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) {
122 ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO);