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