blob: c1077f8b6f57261ed8cf02de26dce31186c494a5 [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
29#include <string>
30
Christopher Ferrisd226a512017-07-14 10:37:19 -070031#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 Ferrisb5d7a872017-07-12 15:40:52 -070036
37static 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
54static bool Detach(pid_t pid) {
55 return ptrace(PTRACE_DETACH, pid, 0, 0) == 0;
56}
57
58void DoUnwind(pid_t pid) {
Christopher Ferrisd226a512017-07-14 10:37:19 -070059 unwindstack::RemoteMaps remote_maps(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070060 if (!remote_maps.Parse()) {
61 printf("Failed to parse map data.\n");
62 return;
63 }
64
Josh Gao0953ecd2017-08-25 13:55:06 -070065 unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070066 if (regs == nullptr) {
67 printf("Unable to get remote reg data\n");
68 return;
69 }
70
71 bool bits32 = true;
72 printf("ABI: ");
Josh Gao0953ecd2017-08-25 13:55:06 -070073 switch (regs->MachineType()) {
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070074 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 Ferrisd226a512017-07-14 10:37:19 -070094 unwindstack::MemoryRemote remote_memory(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070095 for (size_t frame_num = 0; frame_num < 64; frame_num++) {
96 if (regs->pc() == 0) {
97 break;
98 }
Christopher Ferrisd226a512017-07-14 10:37:19 -070099 unwindstack::MapInfo* map_info = remote_maps.Find(regs->pc());
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700100 if (map_info == nullptr) {
101 printf("Failed to find map data for the pc\n");
102 break;
103 }
104
Christopher Ferrisd226a512017-07-14 10:37:19 -0700105 unwindstack::Elf* elf = map_info->GetElf(pid, true);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700106
Christopher Ferrisd226a512017-07-14 10:37:19 -0700107 uint64_t rel_pc = elf->GetRelPc(regs->pc(), map_info);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700108 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
144int 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}