Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 17 | #include <elf.h> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/ptrace.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 29 | #include <memory> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 30 | #include <string> |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 31 | #include <vector> |
| 32 | |
| 33 | #include <android-base/stringprintf.h> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | #include <unwindstack/Elf.h> |
| 36 | #include <unwindstack/MapInfo.h> |
| 37 | #include <unwindstack/Maps.h> |
| 38 | #include <unwindstack/Memory.h> |
| 39 | #include <unwindstack/Regs.h> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 40 | |
| 41 | static bool Attach(pid_t pid) { |
| 42 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // Allow at least 1 second to attach properly. |
| 47 | for (size_t i = 0; i < 1000; i++) { |
| 48 | siginfo_t si; |
| 49 | if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { |
| 50 | return true; |
| 51 | } |
| 52 | usleep(1000); |
| 53 | } |
| 54 | printf("%d: Failed to stop.\n", pid); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | static bool Detach(pid_t pid) { |
| 59 | return ptrace(PTRACE_DETACH, pid, 0, 0) == 0; |
| 60 | } |
| 61 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 62 | std::string GetFrameInfo(size_t frame_num, unwindstack::Regs* regs, |
| 63 | const std::shared_ptr<unwindstack::Memory>& process_memory, |
| 64 | unwindstack::MapInfo* map_info, uint64_t* rel_pc) { |
| 65 | bool bits32; |
| 66 | switch (regs->MachineType()) { |
| 67 | case EM_ARM: |
| 68 | case EM_386: |
| 69 | bits32 = true; |
| 70 | break; |
| 71 | |
| 72 | default: |
| 73 | bits32 = false; |
| 74 | } |
| 75 | |
| 76 | if (map_info == nullptr) { |
| 77 | if (bits32) { |
| 78 | return android::base::StringPrintf(" #%02zu pc %08" PRIx64, frame_num, regs->pc()); |
| 79 | } else { |
| 80 | return android::base::StringPrintf(" #%02zu pc %016" PRIx64, frame_num, regs->pc()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | unwindstack::Elf* elf = map_info->GetElf(process_memory, true); |
| 85 | *rel_pc = elf->GetRelPc(regs->pc(), map_info); |
| 86 | uint64_t adjusted_rel_pc = *rel_pc; |
| 87 | // Don't need to adjust the first frame pc. |
| 88 | if (frame_num != 0) { |
| 89 | adjusted_rel_pc = regs->GetAdjustedPc(*rel_pc, elf); |
| 90 | } |
| 91 | |
| 92 | std::string line; |
| 93 | if (bits32) { |
| 94 | line = android::base::StringPrintf(" #%02zu pc %08" PRIx64, frame_num, adjusted_rel_pc); |
| 95 | } else { |
| 96 | line = android::base::StringPrintf(" #%02zu pc %016" PRIx64, frame_num, adjusted_rel_pc); |
| 97 | } |
| 98 | if (!map_info->name.empty()) { |
| 99 | line += " " + map_info->name; |
| 100 | if (map_info->elf_offset != 0) { |
| 101 | line += android::base::StringPrintf(" (offset 0x%" PRIx64 ")", map_info->elf_offset); |
| 102 | } |
| 103 | } else { |
| 104 | line += android::base::StringPrintf(" <anonymous:%" PRIx64 ">", map_info->offset); |
| 105 | } |
| 106 | uint64_t func_offset; |
| 107 | std::string func_name; |
| 108 | if (elf->GetFunctionName(adjusted_rel_pc, &func_name, &func_offset)) { |
| 109 | line += " (" + func_name; |
| 110 | if (func_offset != 0) { |
| 111 | line += android::base::StringPrintf("+%" PRId64, func_offset); |
| 112 | } |
| 113 | line += ')'; |
| 114 | } |
| 115 | return line; |
| 116 | } |
| 117 | |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 118 | void DoUnwind(pid_t pid) { |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 119 | unwindstack::RemoteMaps remote_maps(pid); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 120 | if (!remote_maps.Parse()) { |
| 121 | printf("Failed to parse map data.\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 125 | unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 126 | if (regs == nullptr) { |
| 127 | printf("Unable to get remote reg data\n"); |
| 128 | return; |
| 129 | } |
| 130 | |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 131 | printf("ABI: "); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 132 | switch (regs->MachineType()) { |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 133 | case EM_ARM: |
| 134 | printf("arm"); |
| 135 | break; |
| 136 | case EM_386: |
| 137 | printf("x86"); |
| 138 | break; |
| 139 | case EM_AARCH64: |
| 140 | printf("arm64"); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 141 | break; |
| 142 | case EM_X86_64: |
| 143 | printf("x86_64"); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
| 146 | printf("unknown\n"); |
| 147 | return; |
| 148 | } |
| 149 | printf("\n"); |
| 150 | |
Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 151 | auto process_memory = unwindstack::Memory::CreateProcessMemory(pid); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 152 | bool return_address_attempt = false; |
| 153 | std::vector<std::string> frames; |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 154 | for (size_t frame_num = 0; frame_num < 64; frame_num++) { |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 155 | unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc()); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 156 | uint64_t rel_pc; |
| 157 | frames.push_back(GetFrameInfo(frame_num, regs, process_memory, map_info, &rel_pc)); |
| 158 | bool stepped; |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 159 | if (map_info == nullptr) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 160 | stepped = false; |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 161 | } else { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 162 | bool finished; |
| 163 | stepped = |
| 164 | map_info->elf->Step(rel_pc + map_info->elf_offset, regs, process_memory.get(), &finished); |
| 165 | if (stepped && finished) { |
| 166 | break; |
| 167 | } |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 168 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 169 | if (!stepped) { |
| 170 | if (return_address_attempt) { |
| 171 | // We tried the return address and it didn't work, remove the last |
| 172 | // two frames. If this bad frame is the only frame, only remove |
| 173 | // the last frame. |
| 174 | frames.pop_back(); |
| 175 | if (frame_num != 1) { |
| 176 | frames.pop_back(); |
| 177 | } |
| 178 | break; |
| 179 | } else { |
| 180 | // Steping didn't work, try this secondary method. |
| 181 | if (!regs->SetPcFromReturnAddress(process_memory.get())) { |
| 182 | break; |
| 183 | } |
| 184 | return_address_attempt = true; |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 185 | } |
| 186 | } else { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 187 | return_address_attempt = false; |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 188 | } |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 189 | } |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 190 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame^] | 191 | // Print the frames. |
| 192 | for (auto& frame : frames) { |
| 193 | printf("%s\n", frame.c_str()); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | int main(int argc, char** argv) { |
| 198 | if (argc != 2) { |
| 199 | printf("Usage: unwind <PID>\n"); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | pid_t pid = atoi(argv[1]); |
| 204 | if (!Attach(pid)) { |
| 205 | printf("Failed to attach to pid %d: %s\n", pid, strerror(errno)); |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | DoUnwind(pid); |
| 210 | |
| 211 | Detach(pid); |
| 212 | |
| 213 | return 0; |
| 214 | } |