blob: f76a101df255d9c881b32dc5a5c7fbdd76f89aa2 [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>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070018#include <signal.h>
19#include <stdint.h>
Christopher Ferrisedccd842017-09-06 14:15:28 -070020#include <string.h>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070021#include <sys/ptrace.h>
22#include <sys/syscall.h>
23#include <unistd.h>
24
25#include <gtest/gtest.h>
26
27#include <atomic>
28#include <memory>
29#include <sstream>
30#include <string>
31#include <thread>
Christopher Ferrisa0196652017-07-18 16:09:20 -070032#include <vector>
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070033
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070034#include <android-base/stringprintf.h>
Elliott Hughes38488902018-07-11 11:13:16 -070035#include <android-base/threads.h>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070036
Christopher Ferrisd226a512017-07-14 10:37:19 -070037#include <unwindstack/Maps.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070038#include <unwindstack/Regs.h>
39#include <unwindstack/RegsGetLocal.h>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070040#include <unwindstack/Unwinder.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070041
Casey Dahlin6b95a0e2019-03-12 17:50:52 -070042#include "MemoryRemote.h"
Christopher Ferrisedccd842017-09-06 14:15:28 -070043#include "TestUtils.h"
44
Christopher Ferrisd226a512017-07-14 10:37:19 -070045namespace unwindstack {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070046
Christopher Ferriseb0772f2018-12-05 15:57:02 -080047enum TestTypeEnum : uint8_t {
48 TEST_TYPE_LOCAL_UNWINDER = 0,
49 TEST_TYPE_LOCAL_UNWINDER_FROM_PID,
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -080050 TEST_TYPE_LOCAL_WAIT_FOR_FINISH,
Christopher Ferriseb0772f2018-12-05 15:57:02 -080051 TEST_TYPE_REMOTE,
52 TEST_TYPE_REMOTE_WITH_INVALID_CALL,
53};
54
Christopher Ferrisedccd842017-09-06 14:15:28 -070055static std::atomic_bool g_ready;
56static volatile bool g_ready_for_remote;
57static volatile bool g_signal_ready_for_remote;
58static std::atomic_bool g_finish;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070059static std::atomic_uintptr_t g_ucontext;
60
Christopher Ferrisedccd842017-09-06 14:15:28 -070061static void ResetGlobals() {
62 g_ready = false;
63 g_ready_for_remote = false;
64 g_signal_ready_for_remote = false;
65 g_finish = false;
66 g_ucontext = 0;
67}
68
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070069static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070070
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070071static std::vector<const char*> kFunctionSignalOrder{"OuterFunction", "MiddleFunction",
72 "InnerFunction", "SignalOuterFunction",
73 "SignalMiddleFunction", "SignalInnerFunction"};
Christopher Ferrisa0196652017-07-18 16:09:20 -070074
75static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -070076 g_ucontext = reinterpret_cast<uintptr_t>(sigcontext);
77 while (!g_finish.load()) {
78 }
79}
80
Christopher Ferrisa0196652017-07-18 16:09:20 -070081extern "C" void SignalInnerFunction() {
82 g_signal_ready_for_remote = true;
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -080083 // Avoid any function calls because not every instruction will be
84 // unwindable.
85 // This method of looping is only used when testing a remote unwind.
86 while (true) {
Christopher Ferrisa0196652017-07-18 16:09:20 -070087 }
88}
89
90extern "C" void SignalMiddleFunction() {
91 SignalInnerFunction();
92}
93
94extern "C" void SignalOuterFunction() {
95 SignalMiddleFunction();
96}
97
98static void SignalCallerHandler(int, siginfo_t*, void*) {
99 SignalOuterFunction();
100}
101
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800102static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder* unwinder) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700103 std::string unwind;
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800104 for (size_t i = 0; i < unwinder->NumFrames(); i++) {
105 unwind += unwinder->FormatFrame(i) + '\n';
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700106 }
107
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700108 return std::string(
109 "Unwind completed without finding all frames\n"
110 " Looking for function: ") +
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700111 function_names.front() + "\n" + "Unwind data:\n" + unwind;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700112}
113
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800114static void VerifyUnwind(Unwinder* unwinder, std::vector<const char*> expected_function_names) {
115 unwinder->Unwind();
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700116
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800117 for (auto& frame : unwinder->frames()) {
Christopher Ferrisca9a54b2018-04-05 11:15:00 -0700118 if (frame.function_name == expected_function_names.back()) {
119 expected_function_names.pop_back();
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700120 if (expected_function_names.empty()) {
121 break;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700122 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700123 }
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700124 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700125
126 ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700127}
128
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800129static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
130 std::vector<const char*> expected_function_names) {
131 auto process_memory(Memory::CreateProcessMemory(pid));
132
133 Unwinder unwinder(512, maps, regs, process_memory);
134 VerifyUnwind(&unwinder, expected_function_names);
135}
136
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700137// This test assumes that this code is compiled with optimizations turned
138// off. If this doesn't happen, then all of the calls will be optimized
139// away.
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800140extern "C" void InnerFunction(TestTypeEnum test_type) {
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -0800141 if (test_type == TEST_TYPE_LOCAL_WAIT_FOR_FINISH) {
142 while (!g_finish.load()) {
143 }
144 return;
145 }
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800146 if (test_type == TEST_TYPE_REMOTE || test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700147 g_ready_for_remote = true;
148 g_ready = true;
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800149 if (test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700150 void (*crash_func)() = nullptr;
151 crash_func();
152 }
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -0800153 // Avoid any function calls because not every instruction will be
154 // unwindable.
155 // This method of looping is only used when testing a remote unwind.
156 while (true) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700157 }
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800158 return;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700159 }
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800160
161 std::unique_ptr<Unwinder> unwinder;
162 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
163 RegsGetLocal(regs.get());
164 std::unique_ptr<Maps> maps;
165
166 if (test_type == TEST_TYPE_LOCAL_UNWINDER) {
167 maps.reset(new LocalMaps());
168 ASSERT_TRUE(maps->Parse());
169 auto process_memory(Memory::CreateProcessMemory(getpid()));
170 unwinder.reset(new Unwinder(512, maps.get(), regs.get(), process_memory));
171 } else {
172 UnwinderFromPid* unwinder_from_pid = new UnwinderFromPid(512, getpid());
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000173 ASSERT_TRUE(unwinder_from_pid->Init(regs->Arch()));
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800174 unwinder_from_pid->SetRegs(regs.get());
175 unwinder.reset(unwinder_from_pid);
176 }
177 VerifyUnwind(unwinder.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700178}
179
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800180extern "C" void MiddleFunction(TestTypeEnum test_type) {
181 InnerFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700182}
183
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800184extern "C" void OuterFunction(TestTypeEnum test_type) {
185 MiddleFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700186}
187
Christopher Ferrisedccd842017-09-06 14:15:28 -0700188class UnwindTest : public ::testing::Test {
189 public:
190 void SetUp() override { ResetGlobals(); }
191};
192
193TEST_F(UnwindTest, local) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800194 OuterFunction(TEST_TYPE_LOCAL_UNWINDER);
195}
196
197TEST_F(UnwindTest, local_use_from_pid) {
198 OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700199}
200
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800201static void LocalUnwind(void* data) {
202 TestTypeEnum* test_type = reinterpret_cast<TestTypeEnum*>(data);
203 OuterFunction(*test_type);
204}
205
206TEST_F(UnwindTest, local_check_for_leak) {
207 TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER;
208 TestCheckForLeaks(LocalUnwind, &test_type);
209}
210
211TEST_F(UnwindTest, local_use_from_pid_check_for_leak) {
212 TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER_FROM_PID;
213 TestCheckForLeaks(LocalUnwind, &test_type);
214}
215
Christopher Ferrisa0196652017-07-18 16:09:20 -0700216void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
217 *completed = false;
218 // Need to sleep before attempting first ptrace. Without this, on the
Christopher Ferrisedccd842017-09-06 14:15:28 -0700219 // host it becomes impossible to attach and ptrace sets errno to EPERM.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700220 usleep(1000);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700221 for (size_t i = 0; i < 1000; i++) {
222 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
223 ASSERT_TRUE(TestQuiescePid(pid))
224 << "Waiting for process to quiesce failed: " << strerror(errno);
225
226 MemoryRemote memory(pid);
227 // Read the remote value to see if we are ready.
228 bool value;
Josh Gaoef35aa52017-10-18 11:44:51 -0700229 if (memory.ReadFully(addr, &value, sizeof(value)) && value) {
Christopher Ferrisedccd842017-09-06 14:15:28 -0700230 *completed = true;
Christopher Ferrisa0196652017-07-18 16:09:20 -0700231 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700232 if (!*completed || !leave_attached) {
233 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
234 }
235 if (*completed) {
236 break;
237 }
238 } else {
239 ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700240 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700241 usleep(5000);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700242 }
243}
244
Christopher Ferrisedccd842017-09-06 14:15:28 -0700245TEST_F(UnwindTest, remote) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700246 pid_t pid;
247 if ((pid = fork()) == 0) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800248 OuterFunction(TEST_TYPE_REMOTE);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700249 exit(0);
250 }
251 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700252 TestScopedPidReaper reap(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700253
Christopher Ferrisa0196652017-07-18 16:09:20 -0700254 bool completed;
255 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
256 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700257
258 RemoteMaps maps(pid);
259 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700260 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700261 ASSERT_TRUE(regs.get() != nullptr);
262
Christopher Ferris5f118512017-09-01 11:17:16 -0700263 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700264
Christopher Ferrisedccd842017-09-06 14:15:28 -0700265 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
266 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700267}
268
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800269TEST_F(UnwindTest, unwind_from_pid_remote) {
270 pid_t pid;
271 if ((pid = fork()) == 0) {
272 OuterFunction(TEST_TYPE_REMOTE);
273 exit(0);
274 }
275 ASSERT_NE(-1, pid);
276 TestScopedPidReaper reap(pid);
277
278 bool completed;
279 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
280 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
281
282 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
283 ASSERT_TRUE(regs.get() != nullptr);
284
285 UnwinderFromPid unwinder(512, pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000286 ASSERT_TRUE(unwinder.Init(regs->Arch()));
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800287 unwinder.SetRegs(regs.get());
288
289 VerifyUnwind(&unwinder, kFunctionOrder);
290
291 // Verify that calling the same object works again.
292
293 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
294 << "ptrace detach failed with unexpected error: " << strerror(errno);
295}
296
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800297static void RemoteCheckForLeaks(void (*unwind_func)(void*)) {
298 pid_t pid;
299 if ((pid = fork()) == 0) {
300 OuterFunction(TEST_TYPE_REMOTE);
301 exit(0);
302 }
303 ASSERT_NE(-1, pid);
304 TestScopedPidReaper reap(pid);
305
306 bool completed;
307 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
308 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
309
310 TestCheckForLeaks(unwind_func, &pid);
311
312 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
313 << "ptrace detach failed with unexpected error: " << strerror(errno);
314}
315
316static void RemoteUnwind(void* data) {
317 pid_t* pid = reinterpret_cast<pid_t*>(data);
318
319 RemoteMaps maps(*pid);
320 ASSERT_TRUE(maps.Parse());
321 std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
322 ASSERT_TRUE(regs.get() != nullptr);
323
324 VerifyUnwind(*pid, &maps, regs.get(), kFunctionOrder);
325}
326
327TEST_F(UnwindTest, remote_check_for_leaks) {
328 RemoteCheckForLeaks(RemoteUnwind);
329}
330
331static void RemoteUnwindFromPid(void* data) {
332 pid_t* pid = reinterpret_cast<pid_t*>(data);
333
334 std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
335 ASSERT_TRUE(regs.get() != nullptr);
336
337 UnwinderFromPid unwinder(512, *pid);
David Srbeckyb9cc4fb2019-04-05 18:23:32 +0000338 ASSERT_TRUE(unwinder.Init(regs->Arch()));
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800339 unwinder.SetRegs(regs.get());
340
341 VerifyUnwind(&unwinder, kFunctionOrder);
342}
343
344TEST_F(UnwindTest, remote_unwind_for_pid_check_for_leaks) {
345 RemoteCheckForLeaks(RemoteUnwindFromPid);
346}
347
Christopher Ferrisedccd842017-09-06 14:15:28 -0700348TEST_F(UnwindTest, from_context) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700349 std::atomic_int tid(0);
350 std::thread thread([&]() {
351 tid = syscall(__NR_gettid);
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -0800352 OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700353 });
354
355 struct sigaction act, oldact;
356 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700357 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700358 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
359 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
360 // Wait for the tid to get set.
361 for (size_t i = 0; i < 100; i++) {
362 if (tid.load() != 0) {
363 break;
364 }
365 usleep(1000);
366 }
367 ASSERT_NE(0, tid.load());
Elliott Hughes38488902018-07-11 11:13:16 -0700368 ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700369
370 // Wait for context data.
371 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700372 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700373 ucontext = reinterpret_cast<void*>(g_ucontext.load());
374 if (ucontext != nullptr) {
375 break;
376 }
377 usleep(1000);
378 }
379 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
380
381 LocalMaps maps;
382 ASSERT_TRUE(maps.Parse());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800383 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700384
Christopher Ferris5f118512017-09-01 11:17:16 -0700385 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700386
387 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
388
389 g_finish = true;
390 thread.join();
391}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700392
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700393static void RemoteThroughSignal(int signal, unsigned int sa_flags) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700394 pid_t pid;
395 if ((pid = fork()) == 0) {
396 struct sigaction act, oldact;
397 memset(&act, 0, sizeof(act));
398 act.sa_sigaction = SignalCallerHandler;
399 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700400 ASSERT_EQ(0, sigaction(signal, &act, &oldact));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700401
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800402 OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700403 exit(0);
404 }
405 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700406 TestScopedPidReaper reap(pid);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700407
408 bool completed;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700409 if (signal != SIGSEGV) {
410 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
411 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
412 ASSERT_EQ(0, kill(pid, SIGUSR1));
413 }
Christopher Ferrisa0196652017-07-18 16:09:20 -0700414 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
415 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
416
417 RemoteMaps maps(pid);
418 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700419 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700420 ASSERT_TRUE(regs.get() != nullptr);
421
Christopher Ferris5f118512017-09-01 11:17:16 -0700422 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700423
Christopher Ferrisedccd842017-09-06 14:15:28 -0700424 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
425 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700426}
427
Christopher Ferrisedccd842017-09-06 14:15:28 -0700428TEST_F(UnwindTest, remote_through_signal) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700429 RemoteThroughSignal(SIGUSR1, 0);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700430}
431
Christopher Ferrisedccd842017-09-06 14:15:28 -0700432TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700433 RemoteThroughSignal(SIGUSR1, SA_SIGINFO);
434}
435
436TEST_F(UnwindTest, remote_through_signal_with_invalid_func) {
437 RemoteThroughSignal(SIGSEGV, 0);
438}
439
440TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) {
441 RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700442}
443
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800444// Verify that using the same map while unwinding multiple threads at the
445// same time doesn't cause problems.
446TEST_F(UnwindTest, multiple_threads_unwind_same_map) {
447 static constexpr size_t kNumConcurrentThreads = 100;
448
449 LocalMaps maps;
450 ASSERT_TRUE(maps.Parse());
451 auto process_memory(Memory::CreateProcessMemory(getpid()));
452
453 std::vector<std::thread*> threads;
454
455 std::atomic_bool wait;
456 wait = true;
457 size_t frames[kNumConcurrentThreads];
458 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
459 std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() {
460 while (wait)
461 ;
462 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
463 RegsGetLocal(regs.get());
464
465 Unwinder unwinder(512, &maps, regs.get(), process_memory);
466 unwinder.Unwind();
467 frames[i] = unwinder.NumFrames();
468 ASSERT_LE(3U, frames[i]) << "Failed for thread " << i;
469 });
470 threads.push_back(thread);
471 }
472 wait = false;
473 for (auto thread : threads) {
474 thread->join();
475 delete thread;
476 }
477}
478
Christopher Ferrisd226a512017-07-14 10:37:19 -0700479} // namespace unwindstack