Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <stdint.h> |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 20 | #include <string.h> |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 21 | #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 Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 32 | #include <vector> |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 33 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame^] | 35 | #include <android-base/threads.h> |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 37 | #include <unwindstack/Maps.h> |
| 38 | #include <unwindstack/Memory.h> |
| 39 | #include <unwindstack/Regs.h> |
| 40 | #include <unwindstack/RegsGetLocal.h> |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 41 | #include <unwindstack/Unwinder.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 42 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 43 | #include "TestUtils.h" |
| 44 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 45 | namespace unwindstack { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 46 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 47 | static std::atomic_bool g_ready; |
| 48 | static volatile bool g_ready_for_remote; |
| 49 | static volatile bool g_signal_ready_for_remote; |
| 50 | static std::atomic_bool g_finish; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 51 | static std::atomic_uintptr_t g_ucontext; |
| 52 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 53 | static void ResetGlobals() { |
| 54 | g_ready = false; |
| 55 | g_ready_for_remote = false; |
| 56 | g_signal_ready_for_remote = false; |
| 57 | g_finish = false; |
| 58 | g_ucontext = 0; |
| 59 | } |
| 60 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 61 | static std::vector<const char*> kFunctionOrder{"OuterFunction", "MiddleFunction", "InnerFunction"}; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 62 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 63 | static std::vector<const char*> kFunctionSignalOrder{"OuterFunction", "MiddleFunction", |
| 64 | "InnerFunction", "SignalOuterFunction", |
| 65 | "SignalMiddleFunction", "SignalInnerFunction"}; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 66 | |
| 67 | static void SignalHandler(int, siginfo_t*, void* sigcontext) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 68 | g_ucontext = reinterpret_cast<uintptr_t>(sigcontext); |
| 69 | while (!g_finish.load()) { |
| 70 | } |
| 71 | } |
| 72 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 73 | extern "C" void SignalInnerFunction() { |
| 74 | g_signal_ready_for_remote = true; |
| 75 | while (!g_finish.load()) { |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | extern "C" void SignalMiddleFunction() { |
| 80 | SignalInnerFunction(); |
| 81 | } |
| 82 | |
| 83 | extern "C" void SignalOuterFunction() { |
| 84 | SignalMiddleFunction(); |
| 85 | } |
| 86 | |
| 87 | static void SignalCallerHandler(int, siginfo_t*, void*) { |
| 88 | SignalOuterFunction(); |
| 89 | } |
| 90 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 91 | static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder& unwinder) { |
| 92 | std::string unwind; |
| 93 | for (size_t i = 0; i < unwinder.NumFrames(); i++) { |
| 94 | unwind += unwinder.FormatFrame(i) + '\n'; |
| 95 | } |
| 96 | |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 97 | return std::string( |
| 98 | "Unwind completed without finding all frames\n" |
| 99 | " Looking for function: ") + |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 100 | function_names.front() + "\n" + "Unwind data:\n" + unwind; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 103 | static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs, |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 104 | std::vector<const char*> expected_function_names) { |
| 105 | auto process_memory(Memory::CreateProcessMemory(pid)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 106 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 107 | Unwinder unwinder(512, maps, regs, process_memory); |
| 108 | unwinder.Unwind(); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 109 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 110 | for (auto& frame : unwinder.frames()) { |
Christopher Ferris | ca9a54b | 2018-04-05 11:15:00 -0700 | [diff] [blame] | 111 | if (frame.function_name == expected_function_names.back()) { |
| 112 | expected_function_names.pop_back(); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 113 | if (expected_function_names.empty()) { |
| 114 | break; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 115 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 116 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 117 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 118 | |
| 119 | ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // This test assumes that this code is compiled with optimizations turned |
| 123 | // off. If this doesn't happen, then all of the calls will be optimized |
| 124 | // away. |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 125 | extern "C" void InnerFunction(bool local, bool trigger_invalid_call) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 126 | if (local) { |
| 127 | LocalMaps maps; |
| 128 | ASSERT_TRUE(maps.Parse()); |
| 129 | std::unique_ptr<Regs> regs(Regs::CreateFromLocal()); |
| 130 | RegsGetLocal(regs.get()); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 131 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 132 | VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 133 | } else { |
| 134 | g_ready_for_remote = true; |
| 135 | g_ready = true; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 136 | if (trigger_invalid_call) { |
| 137 | void (*crash_func)() = nullptr; |
| 138 | crash_func(); |
| 139 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 140 | while (!g_finish.load()) { |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 145 | extern "C" void MiddleFunction(bool local, bool trigger_invalid_call) { |
| 146 | InnerFunction(local, trigger_invalid_call); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 149 | extern "C" void OuterFunction(bool local, bool trigger_invalid_call) { |
| 150 | MiddleFunction(local, trigger_invalid_call); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 153 | class UnwindTest : public ::testing::Test { |
| 154 | public: |
| 155 | void SetUp() override { ResetGlobals(); } |
| 156 | }; |
| 157 | |
| 158 | TEST_F(UnwindTest, local) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 159 | OuterFunction(true, false); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 162 | void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) { |
| 163 | *completed = false; |
| 164 | // Need to sleep before attempting first ptrace. Without this, on the |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 165 | // host it becomes impossible to attach and ptrace sets errno to EPERM. |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 166 | usleep(1000); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 167 | for (size_t i = 0; i < 1000; i++) { |
| 168 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == 0) { |
| 169 | ASSERT_TRUE(TestQuiescePid(pid)) |
| 170 | << "Waiting for process to quiesce failed: " << strerror(errno); |
| 171 | |
| 172 | MemoryRemote memory(pid); |
| 173 | // Read the remote value to see if we are ready. |
| 174 | bool value; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 175 | if (memory.ReadFully(addr, &value, sizeof(value)) && value) { |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 176 | *completed = true; |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 177 | } |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 178 | if (!*completed || !leave_attached) { |
| 179 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)); |
| 180 | } |
| 181 | if (*completed) { |
| 182 | break; |
| 183 | } |
| 184 | } else { |
| 185 | ASSERT_EQ(ESRCH, errno) << "ptrace attach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 186 | } |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 187 | usleep(5000); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 191 | TEST_F(UnwindTest, remote) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 192 | pid_t pid; |
| 193 | if ((pid = fork()) == 0) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 194 | OuterFunction(false, false); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 195 | exit(0); |
| 196 | } |
| 197 | ASSERT_NE(-1, pid); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 198 | TestScopedPidReaper reap(pid); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 199 | |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 200 | bool completed; |
| 201 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed); |
| 202 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 203 | |
| 204 | RemoteMaps maps(pid); |
| 205 | ASSERT_TRUE(maps.Parse()); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 206 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 207 | ASSERT_TRUE(regs.get() != nullptr); |
| 208 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 209 | VerifyUnwind(pid, &maps, regs.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 210 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 211 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 212 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 215 | TEST_F(UnwindTest, from_context) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 216 | std::atomic_int tid(0); |
| 217 | std::thread thread([&]() { |
| 218 | tid = syscall(__NR_gettid); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 219 | OuterFunction(false, false); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 220 | }); |
| 221 | |
| 222 | struct sigaction act, oldact; |
| 223 | memset(&act, 0, sizeof(act)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 224 | act.sa_sigaction = SignalHandler; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 225 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 226 | ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact)); |
| 227 | // Wait for the tid to get set. |
| 228 | for (size_t i = 0; i < 100; i++) { |
| 229 | if (tid.load() != 0) { |
| 230 | break; |
| 231 | } |
| 232 | usleep(1000); |
| 233 | } |
| 234 | ASSERT_NE(0, tid.load()); |
Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame^] | 235 | ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 236 | |
| 237 | // Wait for context data. |
| 238 | void* ucontext; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 239 | for (size_t i = 0; i < 2000; i++) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 240 | ucontext = reinterpret_cast<void*>(g_ucontext.load()); |
| 241 | if (ucontext != nullptr) { |
| 242 | break; |
| 243 | } |
| 244 | usleep(1000); |
| 245 | } |
| 246 | ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal."; |
| 247 | |
| 248 | LocalMaps maps; |
| 249 | ASSERT_TRUE(maps.Parse()); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 250 | std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::CurrentArch(), ucontext)); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 251 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 252 | VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 253 | |
| 254 | ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr)); |
| 255 | |
| 256 | g_finish = true; |
| 257 | thread.join(); |
| 258 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 259 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 260 | static void RemoteThroughSignal(int signal, unsigned int sa_flags) { |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 261 | pid_t pid; |
| 262 | if ((pid = fork()) == 0) { |
| 263 | struct sigaction act, oldact; |
| 264 | memset(&act, 0, sizeof(act)); |
| 265 | act.sa_sigaction = SignalCallerHandler; |
| 266 | act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 267 | ASSERT_EQ(0, sigaction(signal, &act, &oldact)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 268 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 269 | OuterFunction(false, signal == SIGSEGV); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 270 | exit(0); |
| 271 | } |
| 272 | ASSERT_NE(-1, pid); |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 273 | TestScopedPidReaper reap(pid); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 274 | |
| 275 | bool completed; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 276 | if (signal != SIGSEGV) { |
| 277 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), false, &completed); |
| 278 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready."; |
| 279 | ASSERT_EQ(0, kill(pid, SIGUSR1)); |
| 280 | } |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 281 | WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_signal_ready_for_remote), true, &completed); |
| 282 | ASSERT_TRUE(completed) << "Timed out waiting for remote process to be in signal handler."; |
| 283 | |
| 284 | RemoteMaps maps(pid); |
| 285 | ASSERT_TRUE(maps.Parse()); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 286 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 287 | ASSERT_TRUE(regs.get() != nullptr); |
| 288 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 289 | VerifyUnwind(pid, &maps, regs.get(), kFunctionSignalOrder); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 290 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 291 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)) |
| 292 | << "ptrace detach failed with unexpected error: " << strerror(errno); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 295 | TEST_F(UnwindTest, remote_through_signal) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 296 | RemoteThroughSignal(SIGUSR1, 0); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Christopher Ferris | edccd84 | 2017-09-06 14:15:28 -0700 | [diff] [blame] | 299 | TEST_F(UnwindTest, remote_through_signal_sa_siginfo) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 300 | RemoteThroughSignal(SIGUSR1, SA_SIGINFO); |
| 301 | } |
| 302 | |
| 303 | TEST_F(UnwindTest, remote_through_signal_with_invalid_func) { |
| 304 | RemoteThroughSignal(SIGSEGV, 0); |
| 305 | } |
| 306 | |
| 307 | TEST_F(UnwindTest, remote_through_signal_sa_siginfo_with_invalid_func) { |
| 308 | RemoteThroughSignal(SIGSEGV, SA_SIGINFO); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 311 | // Verify that using the same map while unwinding multiple threads at the |
| 312 | // same time doesn't cause problems. |
| 313 | TEST_F(UnwindTest, multiple_threads_unwind_same_map) { |
| 314 | static constexpr size_t kNumConcurrentThreads = 100; |
| 315 | |
| 316 | LocalMaps maps; |
| 317 | ASSERT_TRUE(maps.Parse()); |
| 318 | auto process_memory(Memory::CreateProcessMemory(getpid())); |
| 319 | |
| 320 | std::vector<std::thread*> threads; |
| 321 | |
| 322 | std::atomic_bool wait; |
| 323 | wait = true; |
| 324 | size_t frames[kNumConcurrentThreads]; |
| 325 | for (size_t i = 0; i < kNumConcurrentThreads; i++) { |
| 326 | std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() { |
| 327 | while (wait) |
| 328 | ; |
| 329 | std::unique_ptr<Regs> regs(Regs::CreateFromLocal()); |
| 330 | RegsGetLocal(regs.get()); |
| 331 | |
| 332 | Unwinder unwinder(512, &maps, regs.get(), process_memory); |
| 333 | unwinder.Unwind(); |
| 334 | frames[i] = unwinder.NumFrames(); |
| 335 | ASSERT_LE(3U, frames[i]) << "Failed for thread " << i; |
| 336 | }); |
| 337 | threads.push_back(thread); |
| 338 | } |
| 339 | wait = false; |
| 340 | for (auto thread : threads) { |
| 341 | thread->join(); |
| 342 | delete thread; |
| 343 | } |
| 344 | } |
| 345 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 346 | } // namespace unwindstack |