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> |
| 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> |
| 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 | |
| 41 | namespace unwindstack { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 42 | |
| 43 | static std::atomic_bool g_ready(false); |
| 44 | static volatile bool g_ready_for_remote = false; |
| 45 | static std::atomic_bool g_finish(false); |
| 46 | static std::atomic_uintptr_t g_ucontext; |
| 47 | |
| 48 | static void Signal(int, siginfo_t*, void* sigcontext) { |
| 49 | g_ucontext = reinterpret_cast<uintptr_t>(sigcontext); |
| 50 | while (!g_finish.load()) { |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static std::string ErrorMsg(const char** function_names, size_t index, |
| 55 | std::stringstream& unwind_stream) { |
| 56 | return std::string( |
| 57 | "Unwind completed without finding all frames\n" |
| 58 | " Looking for function: ") + |
| 59 | function_names[index] + "\n" + "Unwind data:\n" + unwind_stream.str(); |
| 60 | } |
| 61 | |
| 62 | static void VerifyUnwind(pid_t pid, Memory* memory, Maps* maps, Regs* regs) { |
| 63 | const char* function_names[] = { |
| 64 | "InnerFunction", "MiddleFunction", "OuterFunction", |
| 65 | }; |
| 66 | size_t function_name_index = 0; |
| 67 | |
| 68 | std::stringstream unwind_stream; |
| 69 | unwind_stream << std::hex; |
| 70 | for (size_t frame_num = 0; frame_num < 64; frame_num++) { |
| 71 | ASSERT_NE(0U, regs->pc()) << ErrorMsg(function_names, function_name_index, unwind_stream); |
| 72 | MapInfo* map_info = maps->Find(regs->pc()); |
| 73 | ASSERT_TRUE(map_info != nullptr) << ErrorMsg(function_names, function_name_index, unwind_stream); |
| 74 | |
| 75 | Elf* elf = map_info->GetElf(pid, true); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame^] | 76 | uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 77 | uint64_t adjusted_rel_pc = rel_pc; |
| 78 | if (frame_num != 0) { |
| 79 | adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf); |
| 80 | } |
| 81 | unwind_stream << " PC: 0x" << regs->pc() << " Rel: 0x" << adjusted_rel_pc; |
| 82 | unwind_stream << " Map: "; |
| 83 | if (!map_info->name.empty()) { |
| 84 | unwind_stream << map_info->name; |
| 85 | } else { |
| 86 | unwind_stream << " anonymous"; |
| 87 | } |
| 88 | unwind_stream << "<" << map_info->start << "-" << map_info->end << ">"; |
| 89 | |
| 90 | std::string name; |
| 91 | uint64_t func_offset; |
| 92 | if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) { |
| 93 | if (name == function_names[function_name_index]) { |
| 94 | function_name_index++; |
| 95 | if (function_name_index == sizeof(function_names) / sizeof(const char*)) { |
| 96 | return; |
| 97 | } |
| 98 | } |
| 99 | unwind_stream << " " << name; |
| 100 | } |
| 101 | unwind_stream << "\n"; |
| 102 | ASSERT_TRUE(elf->Step(rel_pc + map_info->elf_offset, regs, memory)) |
| 103 | << ErrorMsg(function_names, function_name_index, unwind_stream); |
| 104 | } |
| 105 | ASSERT_TRUE(false) << ErrorMsg(function_names, function_name_index, unwind_stream); |
| 106 | } |
| 107 | |
| 108 | // This test assumes that this code is compiled with optimizations turned |
| 109 | // off. If this doesn't happen, then all of the calls will be optimized |
| 110 | // away. |
| 111 | extern "C" void InnerFunction(bool local) { |
| 112 | if (local) { |
| 113 | LocalMaps maps; |
| 114 | ASSERT_TRUE(maps.Parse()); |
| 115 | std::unique_ptr<Regs> regs(Regs::CreateFromLocal()); |
| 116 | RegsGetLocal(regs.get()); |
| 117 | MemoryLocal memory; |
| 118 | |
| 119 | VerifyUnwind(getpid(), &memory, &maps, regs.get()); |
| 120 | } else { |
| 121 | g_ready_for_remote = true; |
| 122 | g_ready = true; |
| 123 | while (!g_finish.load()) { |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | extern "C" void MiddleFunction(bool local) { |
| 129 | InnerFunction(local); |
| 130 | } |
| 131 | |
| 132 | extern "C" void OuterFunction(bool local) { |
| 133 | MiddleFunction(local); |
| 134 | } |
| 135 | |
| 136 | TEST(UnwindTest, local) { |
| 137 | OuterFunction(true); |
| 138 | } |
| 139 | |
| 140 | TEST(UnwindTest, remote) { |
| 141 | pid_t pid; |
| 142 | if ((pid = fork()) == 0) { |
| 143 | OuterFunction(false); |
| 144 | exit(0); |
| 145 | } |
| 146 | ASSERT_NE(-1, pid); |
| 147 | |
| 148 | bool ready = false; |
| 149 | uint64_t addr = reinterpret_cast<uint64_t>(&g_ready_for_remote); |
| 150 | for (size_t i = 0; i < 100; i++) { |
| 151 | ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid, 0, 0)); |
| 152 | for (size_t j = 0; j < 100; j++) { |
| 153 | siginfo_t si; |
| 154 | if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { |
| 155 | // Check to see if process is ready to be unwound. |
| 156 | MemoryRemote memory(pid); |
| 157 | // Read the remote value to see if we are ready. |
| 158 | bool value; |
| 159 | if (memory.Read(addr, &value, sizeof(value)) && value) { |
| 160 | ready = true; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | usleep(1000); |
| 165 | } |
| 166 | if (ready) { |
| 167 | break; |
| 168 | } |
| 169 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)); |
| 170 | usleep(1000); |
| 171 | } |
| 172 | ASSERT_TRUE(read) << "Timed out waiting for remote process to be ready."; |
| 173 | |
| 174 | RemoteMaps maps(pid); |
| 175 | ASSERT_TRUE(maps.Parse()); |
| 176 | MemoryRemote memory(pid); |
| 177 | uint32_t machine_type; |
| 178 | std::unique_ptr<Regs> regs(Regs::RemoteGet(pid, &machine_type)); |
| 179 | ASSERT_TRUE(regs.get() != nullptr); |
| 180 | |
| 181 | VerifyUnwind(pid, &memory, &maps, regs.get()); |
| 182 | |
| 183 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0)); |
| 184 | |
| 185 | kill(pid, SIGKILL); |
| 186 | ASSERT_EQ(pid, wait(nullptr)); |
| 187 | } |
| 188 | |
| 189 | TEST(UnwindTest, from_context) { |
| 190 | std::atomic_int tid(0); |
| 191 | std::thread thread([&]() { |
| 192 | tid = syscall(__NR_gettid); |
| 193 | OuterFunction(false); |
| 194 | }); |
| 195 | |
| 196 | struct sigaction act, oldact; |
| 197 | memset(&act, 0, sizeof(act)); |
| 198 | act.sa_sigaction = Signal; |
| 199 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 200 | ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact)); |
| 201 | // Wait for the tid to get set. |
| 202 | for (size_t i = 0; i < 100; i++) { |
| 203 | if (tid.load() != 0) { |
| 204 | break; |
| 205 | } |
| 206 | usleep(1000); |
| 207 | } |
| 208 | ASSERT_NE(0, tid.load()); |
| 209 | // Portable tgkill method. |
| 210 | ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Failed because " |
| 211 | << strerror(errno); |
| 212 | |
| 213 | // Wait for context data. |
| 214 | void* ucontext; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame^] | 215 | for (size_t i = 0; i < 2000; i++) { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 216 | ucontext = reinterpret_cast<void*>(g_ucontext.load()); |
| 217 | if (ucontext != nullptr) { |
| 218 | break; |
| 219 | } |
| 220 | usleep(1000); |
| 221 | } |
| 222 | ASSERT_TRUE(ucontext != nullptr) << "Timed out waiting for thread to respond to signal."; |
| 223 | |
| 224 | LocalMaps maps; |
| 225 | ASSERT_TRUE(maps.Parse()); |
| 226 | std::unique_ptr<Regs> regs(Regs::CreateFromUcontext(Regs::GetMachineType(), ucontext)); |
| 227 | MemoryLocal memory; |
| 228 | |
| 229 | VerifyUnwind(tid.load(), &memory, &maps, regs.get()); |
| 230 | |
| 231 | ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr)); |
| 232 | |
| 233 | g_finish = true; |
| 234 | thread.join(); |
| 235 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame^] | 236 | |
| 237 | } // namespace unwindstack |