blob: b11d213f6b0f0f5b640c11fd3733fd8da9f2a259 [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());
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800173 unwinder_from_pid->SetRegs(regs.get());
174 unwinder.reset(unwinder_from_pid);
175 }
176 VerifyUnwind(unwinder.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700177}
178
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800179extern "C" void MiddleFunction(TestTypeEnum test_type) {
180 InnerFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700181}
182
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800183extern "C" void OuterFunction(TestTypeEnum test_type) {
184 MiddleFunction(test_type);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700185}
186
Christopher Ferrisedccd842017-09-06 14:15:28 -0700187class UnwindTest : public ::testing::Test {
188 public:
189 void SetUp() override { ResetGlobals(); }
190};
191
192TEST_F(UnwindTest, local) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800193 OuterFunction(TEST_TYPE_LOCAL_UNWINDER);
194}
195
196TEST_F(UnwindTest, local_use_from_pid) {
197 OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700198}
199
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800200static void LocalUnwind(void* data) {
201 TestTypeEnum* test_type = reinterpret_cast<TestTypeEnum*>(data);
202 OuterFunction(*test_type);
203}
204
205TEST_F(UnwindTest, local_check_for_leak) {
206 TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER;
207 TestCheckForLeaks(LocalUnwind, &test_type);
208}
209
210TEST_F(UnwindTest, local_use_from_pid_check_for_leak) {
211 TestTypeEnum test_type = TEST_TYPE_LOCAL_UNWINDER_FROM_PID;
212 TestCheckForLeaks(LocalUnwind, &test_type);
213}
214
Christopher Ferrisa0196652017-07-18 16:09:20 -0700215void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
216 *completed = false;
217 // Need to sleep before attempting first ptrace. Without this, on the
Christopher Ferrisedccd842017-09-06 14:15:28 -0700218 // host it becomes impossible to attach and ptrace sets errno to EPERM.
Christopher Ferrisa0196652017-07-18 16:09:20 -0700219 usleep(1000);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700220 for (size_t i = 0; i < 1000; i++) {
221 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) {
222 ASSERT_TRUE(TestQuiescePid(pid))
223 << "Waiting for process to quiesce failed: " << strerror(errno);
224
225 MemoryRemote memory(pid);
226 // Read the remote value to see if we are ready.
227 bool value;
Josh Gaoef35aa52017-10-18 11:44:51 -0700228 if (memory.ReadFully(addr, &value, sizeof(value)) && value) {
Christopher Ferrisedccd842017-09-06 14:15:28 -0700229 *completed = true;
Christopher Ferrisa0196652017-07-18 16:09:20 -0700230 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700231 if (!*completed || !leave_attached) {
232 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0));
233 }
234 if (*completed) {
235 break;
236 }
237 } else {
238 ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700239 }
Christopher Ferrisedccd842017-09-06 14:15:28 -0700240 usleep(5000);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700241 }
242}
243
Christopher Ferrisedccd842017-09-06 14:15:28 -0700244TEST_F(UnwindTest, remote) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700245 pid_t pid;
246 if ((pid = fork()) == 0) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800247 OuterFunction(TEST_TYPE_REMOTE);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700248 exit(0);
249 }
250 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700251 TestScopedPidReaper reap(pid);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700252
Christopher Ferrisa0196652017-07-18 16:09:20 -0700253 bool completed;
254 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
255 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700256
257 RemoteMaps maps(pid);
258 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700259 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700260 ASSERT_TRUE(regs.get() != nullptr);
261
Christopher Ferris5f118512017-09-01 11:17:16 -0700262 VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700263
Christopher Ferrisedccd842017-09-06 14:15:28 -0700264 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
265 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700266}
267
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800268TEST_F(UnwindTest, unwind_from_pid_remote) {
269 pid_t pid;
270 if ((pid = fork()) == 0) {
271 OuterFunction(TEST_TYPE_REMOTE);
272 exit(0);
273 }
274 ASSERT_NE(-1, pid);
275 TestScopedPidReaper reap(pid);
276
277 bool completed;
278 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
279 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
280
281 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
282 ASSERT_TRUE(regs.get() != nullptr);
283
284 UnwinderFromPid unwinder(512, pid);
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800285 unwinder.SetRegs(regs.get());
286
287 VerifyUnwind(&unwinder, kFunctionOrder);
288
289 // Verify that calling the same object works again.
290
291 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
292 << "ptrace detach failed with unexpected error: " << strerror(errno);
293}
294
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800295static void RemoteCheckForLeaks(void (*unwind_func)(void*)) {
296 pid_t pid;
297 if ((pid = fork()) == 0) {
298 OuterFunction(TEST_TYPE_REMOTE);
299 exit(0);
300 }
301 ASSERT_NE(-1, pid);
302 TestScopedPidReaper reap(pid);
303
304 bool completed;
305 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
306 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
307
308 TestCheckForLeaks(unwind_func, &pid);
309
310 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
311 << "ptrace detach failed with unexpected error: " << strerror(errno);
312}
313
314static void RemoteUnwind(void* data) {
315 pid_t* pid = reinterpret_cast<pid_t*>(data);
316
317 RemoteMaps maps(*pid);
318 ASSERT_TRUE(maps.Parse());
319 std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
320 ASSERT_TRUE(regs.get() != nullptr);
321
322 VerifyUnwind(*pid, &maps, regs.get(), kFunctionOrder);
323}
324
325TEST_F(UnwindTest, remote_check_for_leaks) {
326 RemoteCheckForLeaks(RemoteUnwind);
327}
328
329static void RemoteUnwindFromPid(void* data) {
330 pid_t* pid = reinterpret_cast<pid_t*>(data);
331
332 std::unique_ptr<Regs> regs(Regs::RemoteGet(*pid));
333 ASSERT_TRUE(regs.get() != nullptr);
334
335 UnwinderFromPid unwinder(512, *pid);
Christopher Ferrise1f7a632019-01-24 12:22:03 -0800336 unwinder.SetRegs(regs.get());
337
338 VerifyUnwind(&unwinder, kFunctionOrder);
339}
340
341TEST_F(UnwindTest, remote_unwind_for_pid_check_for_leaks) {
342 RemoteCheckForLeaks(RemoteUnwindFromPid);
343}
344
Christopher Ferrisedccd842017-09-06 14:15:28 -0700345TEST_F(UnwindTest, from_context) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700346 std::atomic_int tid(0);
347 std::thread thread([&]() {
348 tid = syscall(__NR_gettid);
Christopher Ferrisbc6a7e52019-01-18 16:33:27 -0800349 OuterFunction(TEST_TYPE_LOCAL_WAIT_FOR_FINISH);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700350 });
351
352 struct sigaction act, oldact;
353 memset(&act, 0, sizeof(act));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700354 act.sa_sigaction = SignalHandler;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700355 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
356 ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
357 // Wait for the tid to get set.
358 for (size_t i = 0; i < 100; i++) {
359 if (tid.load() != 0) {
360 break;
361 }
362 usleep(1000);
363 }
364 ASSERT_NE(0, tid.load());
Elliott Hughes38488902018-07-11 11:13:16 -0700365 ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700366
367 // Wait for context data.
368 void* ucontext;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700369 for (size_t i = 0; i < 2000; i++) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700370 ucontext = reinterpret_cast<void*>(g_ucontext.load());
371 if (ucontext != nullptr) {
372 break;
373 }
374 usleep(1000);
375 }
376 ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal.";
377
378 LocalMaps maps;
379 ASSERT_TRUE(maps.Parse());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800380 std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext));
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700381
Christopher Ferris5f118512017-09-01 11:17:16 -0700382 VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700383
384 ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
385
386 g_finish = true;
387 thread.join();
388}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700389
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700390static void RemoteThroughSignal(int signal, unsigned int sa_flags) {
Christopher Ferrisa0196652017-07-18 16:09:20 -0700391 pid_t pid;
392 if ((pid = fork()) == 0) {
393 struct sigaction act, oldact;
394 memset(&act, 0, sizeof(act));
395 act.sa_sigaction = SignalCallerHandler;
396 act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700397 ASSERT_EQ(0, sigaction(signal, &act, &oldact));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700398
Christopher Ferriseb0772f2018-12-05 15:57:02 -0800399 OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700400 exit(0);
401 }
402 ASSERT_NE(-1, pid);
Christopher Ferrisedccd842017-09-06 14:15:28 -0700403 TestScopedPidReaper reap(pid);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700404
405 bool completed;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700406 if (signal != SIGSEGV) {
407 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed);
408 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
409 ASSERT_EQ(0, kill(pid, SIGUSR1));
410 }
Christopher Ferrisa0196652017-07-18 16:09:20 -0700411 WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed);
412 ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler.";
413
414 RemoteMaps maps(pid);
415 ASSERT_TRUE(maps.Parse());
Josh Gao0953ecd2017-08-25 13:55:06 -0700416 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700417 ASSERT_TRUE(regs.get() != nullptr);
418
Christopher Ferris5f118512017-09-01 11:17:16 -0700419 VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700420
Christopher Ferrisedccd842017-09-06 14:15:28 -0700421 ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
422 << "ptrace detach failed with unexpected error: " << strerror(errno);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700423}
424
Christopher Ferrisedccd842017-09-06 14:15:28 -0700425TEST_F(UnwindTest, remote_through_signal) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700426 RemoteThroughSignal(SIGUSR1, 0);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700427}
428
Christopher Ferrisedccd842017-09-06 14:15:28 -0700429TEST_F(UnwindTest, remote_through_signal_sa_siginfo) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700430 RemoteThroughSignal(SIGUSR1, SA_SIGINFO);
431}
432
433TEST_F(UnwindTest, remote_through_signal_with_invalid_func) {
434 RemoteThroughSignal(SIGSEGV, 0);
435}
436
437TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) {
438 RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700439}
440
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800441// Verify that using the same map while unwinding multiple threads at the
442// same time doesn't cause problems.
443TEST_F(UnwindTest, multiple_threads_unwind_same_map) {
444 static constexpr size_t kNumConcurrentThreads = 100;
445
446 LocalMaps maps;
447 ASSERT_TRUE(maps.Parse());
448 auto process_memory(Memory::CreateProcessMemory(getpid()));
449
450 std::vector<std::thread*> threads;
451
452 std::atomic_bool wait;
453 wait = true;
454 size_t frames[kNumConcurrentThreads];
455 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
456 std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() {
457 while (wait)
458 ;
459 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
460 RegsGetLocal(regs.get());
461
462 Unwinder unwinder(512, &maps, regs.get(), process_memory);
463 unwinder.Unwind();
464 frames[i] = unwinder.NumFrames();
465 ASSERT_LE(3U, frames[i]) << "Failed for thread " << i;
466 });
467 threads.push_back(thread);
468 }
469 wait = false;
470 for (auto thread : threads) {
471 thread->join();
472 delete thread;
473 }
474}
475
Christopher Ferrisd226a512017-07-14 10:37:19 -0700476} // namespace unwindstack