blob: 22ca7bf1daf2a2bde29e93cc18ee53ecf933c20a [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 Ferris7747b602018-01-31 19:05:19 -080029#include <unwindstack/DexFiles.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070030#include <unwindstack/Elf.h>
Christopher Ferris150db122017-12-20 18:49:01 -080031#include <unwindstack/JitDebug.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070032#include <unwindstack/Maps.h>
33#include <unwindstack/Memory.h>
34#include <unwindstack/Regs.h>
Christopher Ferrisae3b1362017-10-05 16:05:44 -070035#include <unwindstack/Unwinder.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
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070054void DoUnwind(pid_t pid) {
Christopher Ferrisd226a512017-07-14 10:37:19 -070055 unwindstack::RemoteMaps remote_maps(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070056 if (!remote_maps.Parse()) {
57 printf("Failed to parse map data.\n");
58 return;
59 }
60
Josh Gao0953ecd2017-08-25 13:55:06 -070061 unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070062 if (regs == nullptr) {
63 printf("Unable to get remote reg data\n");
64 return;
65 }
66
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070067 printf("ABI: ");
Christopher Ferrisd06001d2017-11-30 18:56:01 -080068 switch (regs->Arch()) {
69 case unwindstack::ARCH_ARM:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070070 printf("arm");
71 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080072 case unwindstack::ARCH_X86:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070073 printf("x86");
74 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080075 case unwindstack::ARCH_ARM64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070076 printf("arm64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070077 break;
Christopher Ferrisd06001d2017-11-30 18:56:01 -080078 case unwindstack::ARCH_X86_64:
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070079 printf("x86_64");
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070080 break;
Douglas Leung61b1a1a2017-11-08 10:53:53 +010081 case unwindstack::ARCH_MIPS:
82 printf("mips");
83 break;
84 case unwindstack::ARCH_MIPS64:
85 printf("mips64");
86 break;
Christopher Ferrisb5d7a872017-07-12 15:40:52 -070087 default:
88 printf("unknown\n");
89 return;
90 }
91 printf("\n");
92
Christopher Ferris5f118512017-09-01 11:17:16 -070093 auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
Christopher Ferrisae3b1362017-10-05 16:05:44 -070094 unwindstack::Unwinder unwinder(128, &remote_maps, regs, process_memory);
Christopher Ferris7747b602018-01-31 19:05:19 -080095
Christopher Ferris150db122017-12-20 18:49:01 -080096 unwindstack::JitDebug jit_debug(process_memory);
97 unwinder.SetJitDebug(&jit_debug, regs->Arch());
Christopher Ferris7747b602018-01-31 19:05:19 -080098
99 unwindstack::DexFiles dex_files(process_memory);
100 unwinder.SetDexFiles(&dex_files, regs->Arch());
101
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700102 unwinder.Unwind();
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700103
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700104 // Print the frames.
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700105 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
106 printf("%s\n", unwinder.FormatFrame(i).c_str());
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700107 }
108}
109
110int main(int argc, char** argv) {
111 if (argc != 2) {
112 printf("Usage: unwind <PID>\n");
113 return 1;
114 }
115
116 pid_t pid = atoi(argv[1]);
117 if (!Attach(pid)) {
118 printf("Failed to attach to pid %d: %s\n", pid, strerror(errno));
119 return 1;
120 }
121
122 DoUnwind(pid);
123
Christopher Ferrisae3b1362017-10-05 16:05:44 -0700124 ptrace(PTRACE_DETACH, pid, 0, 0);
Christopher Ferrisb5d7a872017-07-12 15:40:52 -0700125
126 return 0;
127}