blob: cad95f8c549428e787f993ac6ba8ed8797d7e616 [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) {
Christopher Ferris01040b12018-12-10 11:13:23 -080037 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == -1) {
38 return false;
39 }
40
41 if (ptrace(PTRACE_INTERRUPT, pid, 0, 0) == -1) {
42 ptrace(PTRACE_DETACH, pid, 0, 0);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070043 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
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070058void DoUnwind(pid_t pid) {
Josh Gao0953ecd2017-08-25 13:55:06 -070059 unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070060 if (regs == nullptr) {
61 printf("Unable to get remote reg data\n");
62 return;
63 }
64
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070065 printf("ABI: ");
Christopher Ferrisd06001d2017-11-30 18:56:01 -080066 switch (regs->Arch()) {
67 case unwindstack::ARCH_ARM:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070068 printf("arm");
69 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080070 case unwindstack::ARCH_X86:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070071 printf("x86");
72 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080073 case unwindstack::ARCH_ARM64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070074 printf("arm64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070075 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080076 case unwindstack::ARCH_X86_64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070077 printf("x86_64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070078 break;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010079 case unwindstack::ARCH_MIPS:
80 printf("mips");
81 break;
82 case unwindstack::ARCH_MIPS64:
83 printf("mips64");
84 break;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070085 default:
86 printf("unknown\n");
87 return;
88 }
89 printf("\n");
90
Christopher Ferriseb0772f2018-12-05 15:57:02 -080091 unwindstack::UnwinderFromPid unwinder(1024, pid);
David Srbecky85b5fec2018-02-23 18:06:13 +000092 if (!unwinder.Init()) {
Christopher Ferriseb0772f2018-12-05 15:57:02 -080093 printf("Failed to init unwinder object.\n");
94 return;
95 }
Christopher Ferris7747b602018-01-31 19:05:19 -080096
Christopher Ferriseb0772f2018-12-05 15:57:02 -080097 unwinder.SetRegs(regs);
Christopher Ferrisae3b1362017-10-05 16:05:44 -070098 unwinder.Unwind();
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070099
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700100 // Print the frames.
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700101 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
102 printf("%s\n", unwinder.FormatFrame(i).c_str());
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700103 }
104}
105
106int main(int argc, char** argv) {
107 if (argc != 2) {
108 printf("Usage: unwind <PID>\n");
109 return 1;
110 }
111
112 pid_t pid = atoi(argv[1]);
113 if (!Attach(pid)) {
114 printf("Failed to attach to pid %d: %s\n", pid, strerror(errno));
115 return 1;
116 }
117
118 DoUnwind(pid);
119
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700120 ptrace(PTRACE_DETACH, pid, 0, 0);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700121
122 return 0;
123}