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 | |
| 29 | #include <string> |
| 30 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 | #include <unwindstack/Elf.h> |
| 32 | #include <unwindstack/MapInfo.h> |
| 33 | #include <unwindstack/Maps.h> |
| 34 | #include <unwindstack/Memory.h> |
| 35 | #include <unwindstack/Regs.h> |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 36 | |
| 37 | static bool Attach(pid_t pid) { |
| 38 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | // Allow at least 1 second to attach properly. |
| 43 | for (size_t i = 0; i < 1000; i++) { |
| 44 | siginfo_t si; |
| 45 | if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { |
| 46 | return true; |
| 47 | } |
| 48 | usleep(1000); |
| 49 | } |
| 50 | printf("%d: Failed to stop.\n", pid); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | static bool Detach(pid_t pid) { |
| 55 | return ptrace(PTRACE_DETACH, pid, 0, 0) == 0; |
| 56 | } |
| 57 | |
| 58 | void DoUnwind(pid_t pid) { |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 59 | unwindstack::RemoteMaps remote_maps(pid); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 60 | if (!remote_maps.Parse()) { |
| 61 | printf("Failed to parse map data.\n"); |
| 62 | return; |
| 63 | } |
| 64 | |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 65 | unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 66 | if (regs == nullptr) { |
| 67 | printf("Unable to get remote reg data\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | bool bits32 = true; |
| 72 | printf("ABI: "); |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame^] | 73 | switch (regs->MachineType()) { |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 74 | case EM_ARM: |
| 75 | printf("arm"); |
| 76 | break; |
| 77 | case EM_386: |
| 78 | printf("x86"); |
| 79 | break; |
| 80 | case EM_AARCH64: |
| 81 | printf("arm64"); |
| 82 | bits32 = false; |
| 83 | break; |
| 84 | case EM_X86_64: |
| 85 | printf("x86_64"); |
| 86 | bits32 = false; |
| 87 | break; |
| 88 | default: |
| 89 | printf("unknown\n"); |
| 90 | return; |
| 91 | } |
| 92 | printf("\n"); |
| 93 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 94 | unwindstack::MemoryRemote remote_memory(pid); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 95 | for (size_t frame_num = 0; frame_num < 64; frame_num++) { |
| 96 | if (regs->pc() == 0) { |
| 97 | break; |
| 98 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 99 | unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc()); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 100 | if (map_info == nullptr) { |
| 101 | printf("Failed to find map data for the pc\n"); |
| 102 | break; |
| 103 | } |
| 104 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 105 | unwindstack::Elf* elf = map_info->GetElf(pid, true); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 106 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 107 | uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info); |
Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 108 | uint64_t adjusted_rel_pc = rel_pc; |
| 109 | // Don't need to adjust the first frame pc. |
| 110 | if (frame_num != 0) { |
| 111 | adjusted_rel_pc = regs->GetAdjustedPc(rel_pc, elf); |
| 112 | } |
| 113 | |
| 114 | std::string name; |
| 115 | if (bits32) { |
| 116 | printf(" #%02zu pc %08" PRIx64, frame_num, adjusted_rel_pc); |
| 117 | } else { |
| 118 | printf(" #%02zu pc %016" PRIx64, frame_num, adjusted_rel_pc); |
| 119 | } |
| 120 | if (!map_info->name.empty()) { |
| 121 | printf(" %s", map_info->name.c_str()); |
| 122 | if (map_info->elf_offset != 0) { |
| 123 | printf(" (offset 0x%" PRIx64 ")", map_info->elf_offset); |
| 124 | } |
| 125 | } else { |
| 126 | printf(" <anonymous:%" PRIx64 ">", map_info->offset); |
| 127 | } |
| 128 | uint64_t func_offset; |
| 129 | if (elf->GetFunctionName(adjusted_rel_pc, &name, &func_offset)) { |
| 130 | printf(" (%s", name.c_str()); |
| 131 | if (func_offset != 0) { |
| 132 | printf("+%" PRId64, func_offset); |
| 133 | } |
| 134 | printf(")"); |
| 135 | } |
| 136 | printf("\n"); |
| 137 | |
| 138 | if (!elf->Step(rel_pc + map_info->elf_offset, regs, &remote_memory)) { |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | int main(int argc, char** argv) { |
| 145 | if (argc != 2) { |
| 146 | printf("Usage: unwind <PID>\n"); |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | pid_t pid = atoi(argv[1]); |
| 151 | if (!Attach(pid)) { |
| 152 | printf("Failed to attach to pid %d: %s\n", pid, strerror(errno)); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | DoUnwind(pid); |
| 157 | |
| 158 | Detach(pid); |
| 159 | |
| 160 | return 0; |
| 161 | } |