blob: faac2eff6d276b67bd7c261bb866f2c8b7ce3b6e [file] [log] [blame]
Christopher Ferrisb5d7a872017-07-12 15:40:52 -07001/*
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 Ferrisd226a512017-07-14 10:37:19 -070017#include <elf.h>
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070018#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 Ferrisb9de87f2017-09-20 13:37:24 -070029#include <memory>
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070030#include <string>
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070031#include <vector>
32
33#include <android-base/stringprintf.h>
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070034
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#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 Ferrisb5d7a872017-07-12 15:40:52 -070040
41static 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
58static bool Detach(pid_t pid) {
59 return ptrace(PTRACE_DETACH, pid, 0, 0) == 0;
60}
61
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070062std::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 Ferrisb5d7a872017-07-12 15:40:52 -0700118void DoUnwind(pid_t pid) {
Christopher Ferrisd226a512017-07-14 10:37:19 -0700119 unwindstack::RemoteMaps remote_maps(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700120 if (!remote_maps.Parse()) {
121 printf("Failed to parse map data.\n");
122 return;
123 }
124
Josh Gao0953ecd2017-08-25 13:55:06 -0700125 unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700126 if (regs == nullptr) {
127 printf("Unable to get remote reg data\n");
128 return;
129 }
130
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700131 printf("ABI: ");
Josh Gao0953ecd2017-08-25 13:55:06 -0700132 switch (regs->MachineType()) {
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700133 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 Ferrisb5d7a872017-07-12 15:40:52 -0700141 break;
142 case EM_X86_64:
143 printf("x86_64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700144 break;
145 default:
146 printf("unknown\n");
147 return;
148 }
149 printf("\n");
150
Christopher Ferris5f118512017-09-01 11:17:16 -0700151 auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700152 bool return_address_attempt = false;
153 std::vector<std::string> frames;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700154 for (size_t frame_num = 0; frame_num < 64; frame_num++) {
Christopher Ferrisd226a512017-07-14 10:37:19 -0700155 unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc());
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700156 uint64_t rel_pc;
157 frames.push_back(GetFrameInfo(frame_num, regs, process_memory, map_info, &rel_pc));
158 bool stepped;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700159 if (map_info == nullptr) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700160 stepped = false;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700161 } else {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700162 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 Ferrisb5d7a872017-07-12 15:40:52 -0700168 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700169 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 Ferrisb5d7a872017-07-12 15:40:52 -0700185 }
186 } else {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700187 return_address_attempt = false;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700188 }
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700189 }
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700190
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700191 // Print the frames.
192 for (auto& frame : frames) {
193 printf("%s\n", frame.c_str());
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700194 }
195}
196
197int 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}