blob: 07e48af60a423757a8eebca9be948fb369ce2bcc [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 Ferrisd226a512017-07-14 10:37:19 -070029#include <unwindstack/Elf.h>
Christopher Ferris150db122017-12-20 18:49:01 -080030#include <unwindstack/JitDebug.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070031#include <unwindstack/Maps.h>
32#include <unwindstack/Memory.h>
33#include <unwindstack/Regs.h>
Christopher Ferrisae3b1362017-10-05 16:05:44 -070034#include <unwindstack/Unwinder.h>
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070035
36static bool Attach(pid_t pid) {
37 if (ptrace(PTRACE_ATTACH, pid, 0, 0) == -1) {
38 return false;
39 }
40
41 // Allow at least 1 second to attach properly.
42 for (size_t i = 0; i < 1000; i++) {
43 siginfo_t si;
44 if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
45 return true;
46 }
47 usleep(1000);
48 }
49 printf("%d: Failed to stop.\n", pid);
50 return false;
51}
52
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070053void DoUnwind(pid_t pid) {
Christopher Ferrisd226a512017-07-14 10:37:19 -070054 unwindstack::RemoteMaps remote_maps(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070055 if (!remote_maps.Parse()) {
56 printf("Failed to parse map data.\n");
57 return;
58 }
59
Josh Gao0953ecd2017-08-25 13:55:06 -070060 unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070061 if (regs == nullptr) {
62 printf("Unable to get remote reg data\n");
63 return;
64 }
65
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070066 printf("ABI: ");
Christopher Ferrisd06001d2017-11-30 18:56:01 -080067 switch (regs->Arch()) {
68 case unwindstack::ARCH_ARM:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070069 printf("arm");
70 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080071 case unwindstack::ARCH_X86:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070072 printf("x86");
73 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080074 case unwindstack::ARCH_ARM64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070075 printf("arm64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070076 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080077 case unwindstack::ARCH_X86_64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070078 printf("x86_64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070079 break;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010080 case unwindstack::ARCH_MIPS:
81 printf("mips");
82 break;
83 case unwindstack::ARCH_MIPS64:
84 printf("mips64");
85 break;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070086 default:
87 printf("unknown\n");
88 return;
89 }
90 printf("\n");
91
Christopher Ferris5f118512017-09-01 11:17:16 -070092 auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
Christopher Ferrisae3b1362017-10-05 16:05:44 -070093 unwindstack::Unwinder unwinder(128, &remote_maps, regs, process_memory);
Christopher Ferris150db122017-12-20 18:49:01 -080094 unwindstack::JitDebug jit_debug(process_memory);
95 unwinder.SetJitDebug(&jit_debug, regs->Arch());
Christopher Ferrisae3b1362017-10-05 16:05:44 -070096 unwinder.Unwind();
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070097
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070098 // Print the frames.
Christopher Ferrisae3b1362017-10-05 16:05:44 -070099 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
100 printf("%s\n", unwinder.FormatFrame(i).c_str());
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700101 }
102}
103
104int main(int argc, char** argv) {
105 if (argc != 2) {
106 printf("Usage: unwind <PID>\n");
107 return 1;
108 }
109
110 pid_t pid = atoi(argv[1]);
111 if (!Attach(pid)) {
112 printf("Failed to attach to pid %d: %s\n", pid, strerror(errno));
113 return 1;
114 }
115
116 DoUnwind(pid);
117
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700118 ptrace(PTRACE_DETACH, pid, 0, 0);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700119
120 return 0;
121}