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