blob: a0c45bdf37143eb6957f00616eaace7e4f741cb9 [file] [log] [blame]
Christopher Ferris2a25c4a2017-07-07 16:35:48 -07001/*
2 * Copyright (C) 2017 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#include <errno.h>
18#include <string.h>
19
20#include <signal.h>
21#include <stdint.h>
22#include <sys/ptrace.h>
23#include <sys/syscall.h>
24#include <unistd.h>
25
26#include <gtest/gtest.h>
27
28#include <atomic>
29#include <memory>
30#include <sstream>
31#include <string>
32#include <thread>
Christopher Ferrisa0196652017-07-18 16:09:20 -070033#include <vector>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070034
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#include <unwindstack/Elf.h>
36#include <unwindstack/MapInfo.h>
37#include <unwindstack/Maps.h>
38#include <unwindstack/Memory.h>
39#include <unwindstack/Regs.h>
40#include <unwindstack/RegsGetLocal.h>
41
42namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070043
44static std::atomic_bool g_ready(false);
45static volatile bool g_ready_for_remote = false;
Christopher Ferrisa0196652017-07-18 16:09:20 -070046static volatile bool g_signal_ready_for_remote = false;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070047static std::atomic_bool g_finish(false);
48static std::atomic_uintptr_t g_ucontext;
49
Christopher Ferrisa0196652017-07-18 16:09:20 -070050static std::vector<const char*> kFunctionOrder{"InnerFunction", "MiddleFunction", "OuterFunction"};
51
52static std::vector<const char*> kFunctionSignalOrder{"SignalInnerFunction", "SignalMiddleFunction",
53 "SignalOuterFunction", "InnerFunction",
54 "MiddleFunction", "OuterFunction"};
55
56static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070057 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
58 while (!g_finish.load()) {
59 }
60}
61
Christopher Ferrisa0196652017-07-18 16:09:20 -070062extern "C" void SignalInnerFunction() {
63 g_signal_ready_for_remote = true;
64 while (!g_finish.load()) {
65 }
66}
67
68extern "C" void SignalMiddleFunction() {
69 SignalInnerFunction();
70}
71
72extern "C" void SignalOuterFunction() {
73 SignalMiddleFunction();
74}
75
76static void SignalCallerHandler(int, siginfo_t*, void*) {
77 SignalOuterFunction();
78}
79
80static std::string ErrorMsg(const std::vector<const char*>& function_names, size_t index,
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070081 std::stringstream& unwind_stream) {
82 return std::string(
83 "Unwind completed without finding all frames\n"
84 " Looking for function: ") +
85 function_names[index] + "\n" + "Unwind data:\n" + unwind_stream.str();
86}
87
Christopher Ferris5f118512017-09-01 11:17:16 -070088static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
Christopher Ferrisa0196652017-07-18 16:09:20 -070089 std::vector<const char*>& function_names) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070090 size_t function_name_index = 0;
91
Christopher Ferris5f118512017-09-01 11:17:16 -070092 auto process_memory = Memory::CreateProcessMemory(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070093 std::stringstream unwind_stream;
94 unwind_stream << std::hex;
95 for (size_t frame_num = 0; frame_num < 64; frame_num++) {
96 ASSERT_NE(0U, regs->pc()) << ErrorMsg(function_names, function_name_index, unwind_stream);
97 MapInfo* map_info = maps->Find(regs->pc());
98 ASSERT_TRUE(map_info != nullptr) << ErrorMsg(function_names, function_name_index, unwind_stream);
99
Christopher Ferris5f118512017-09-01 11:17:16 -0700100 Elf* elf = map_info->GetElf(process_memory, true);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700101 uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700102 uint64_t adjusted_rel_pc = rel_pc;
103 if (frame_num != 0) {
104 adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf);
105 }
106 unwind_stream << " PC: 0x" << regs->pc() << " Rel: 0x" << adjusted_rel_pc;
107 unwind_stream << " Map: ";
108 if (!map_info->name.empty()) {
109 unwind_stream << map_info->name;
110 } else {
111 unwind_stream << " anonymous";
112 }
113 unwind_stream << "<" << map_info->start << "-" << map_info->end << ">";
114
115 std::string name;
116 uint64_t func_offset;
117 if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) {
118 if (name == function_names[function_name_index]) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700119 if (++function_name_index == function_names.size()) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700120 return;
121 }
122 }
123 unwind_stream << " " << name;
124 }
125 unwind_stream << "\n";
Christopher Ferris5f118512017-09-01 11:17:16 -0700126 ASSERT_TRUE(elf->Step(rel_pc + map_info->elf_offset, regs, process_memory.get()))
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700127 << ErrorMsg(function_names, function_name_index, unwind_stream);
128 }
129 ASSERT_TRUE(false) << ErrorMsg(function_names, function_name_index, unwind_stream);
130}
131
132// This test assumes that this code is compiled with optimizations turned
133// off. If this doesn't happen, then all of the calls will be optimized
134// away.
135extern "C" void InnerFunction(bool local) {
136 if (local) {
137 LocalMaps maps;
138 ASSERT_TRUE(maps.Parse());
139 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
140 RegsGetLocal(regs.get());
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700141
Christopher Ferris5f118512017-09-01 11:17:16 -0700142 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700143 } else {
144 g_ready_for_remote = true;
145 g_ready = true;
146 while (!g_finish.load()) {
147 }
148 }
149}
150
151extern "C" void MiddleFunction(bool local) {
152 InnerFunction(local);
153}
154
155extern "C" void OuterFunction(bool local) {
156 MiddleFunction(local);
157}
158
159TEST(UnwindTest, local) {
160 OuterFunction(true);
161}
162
Christopher Ferrisa0196652017-07-18 16:09:20 -0700163void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
164 *completed = false;
165 // Need to sleep before attempting first ptrace. Without this, on the
166 // host it becomes impossible to attach and ptrace set errno to EPERM.
167 usleep(1000);
168 for (size_t i = 0; i < 100; i++) {
169 ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid, 0, 0));
170 for (size_t j = 0; j < 100; j++) {
171 siginfo_t si;
172 if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
173 MemoryRemote memory(pid);
174 // Read the remote value to see if we are ready.
175 bool value;
176 if (memory.Read(addr, &value, sizeof(value)) && value) {
177 *completed = true;
178 break;
179 }
180 }
181 usleep(1000);
182 }
183 if (leave_attached && *completed) {
184 break;
185 }
186 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
187 if (*completed) {
188 break;
189 }
190 usleep(1000);
191 }
192}
193
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700194TEST(UnwindTest, remote) {
195 pid_t pid;
196 if ((pid = fork()) == 0) {
197 OuterFunction(false);
198 exit(0);
199 }
200 ASSERT_NE(-1, pid);
201
Christopher Ferrisa0196652017-07-18 16:09:20 -0700202 bool completed;
203 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
204 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700205
206 RemoteMaps maps(pid);
207 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700208 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700209 ASSERT_TRUE(regs.get() != nullptr);
210
Christopher Ferris5f118512017-09-01 11:17:16 -0700211 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700212
213 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
214
215 kill(pid, SIGKILL);
216 ASSERT_EQ(pid, wait(nullptr));
217}
218
219TEST(UnwindTest, from_context) {
220 std::atomic_int tid(0);
221 std::thread thread([&]() {
222 tid = syscall(__NR_gettid);
223 OuterFunction(false);
224 });
225
226 struct sigaction act, oldact;
227 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700228 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700229 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
230 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
231 // Wait for the tid to get set.
232 for (size_t i = 0; i < 100; i++) {
233 if (tid.load() != 0) {
234 break;
235 }
236 usleep(1000);
237 }
238 ASSERT_NE(0, tid.load());
239 // Portable tgkill method.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700240 ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700241
242 // Wait for context data.
243 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700244 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700245 ucontext = reinterpret_cast<void*>(g_ucontext.load());
246 if (ucontext != nullptr) {
247 break;
248 }
249 usleep(1000);
250 }
251 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
252
253 LocalMaps maps;
254 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700255 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentMachineType(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700256
Christopher Ferris5f118512017-09-01 11:17:16 -0700257 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700258
259 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
260
261 g_finish = true;
262 thread.join();
263}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700264
Christopher Ferrisa0196652017-07-18 16:09:20 -0700265static void RemoteThroughSignal(unsigned int sa_flags) {
266 g_ready = false;
267 g_signal_ready_for_remote = false;
268 g_finish = false;
269
270 pid_t pid;
271 if ((pid = fork()) == 0) {
272 struct sigaction act, oldact;
273 memset(&act, 0, sizeof(act));
274 act.sa_sigaction = SignalCallerHandler;
275 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
276 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
277
278 OuterFunction(false);
279 exit(0);
280 }
281 ASSERT_NE(-1, pid);
282
283 bool completed;
284 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
285 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
286 ASSERT_EQ(0, kill(pid, SIGUSR1));
287 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
288 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
289
290 RemoteMaps maps(pid);
291 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700292 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700293 ASSERT_TRUE(regs.get() != nullptr);
294
Christopher Ferris5f118512017-09-01 11:17:16 -0700295 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700296
297 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
298
299 kill(pid, SIGKILL);
300 ASSERT_EQ(pid, wait(nullptr));
301}
302
303TEST(UnwindTest, remote_through_signal) {
304 RemoteThroughSignal(0);
305}
306
307TEST(UnwindTest, remote_through_signal_sa_siginfo) {
308 RemoteThroughSignal(SA_SIGINFO);
309}
310
Christopher Ferrisd226a512017-07-14 10:37:19 -0700311} // namespace unwindstack