| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 17 | #include <elf.h> | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 18 | #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 Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 29 | #include <unwindstack/DexFiles.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 30 | #include <unwindstack/Elf.h> | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 31 | #include <unwindstack/JitDebug.h> | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 32 | #include <unwindstack/Maps.h> | 
|  | 33 | #include <unwindstack/Memory.h> | 
|  | 34 | #include <unwindstack/Regs.h> | 
| Christopher Ferris | ae3b136 | 2017-10-05 16:05:44 -0700 | [diff] [blame] | 35 | #include <unwindstack/Unwinder.h> | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | static bool Attach(pid_t pid) { | 
| Christopher Ferris | 01040b1 | 2018-12-10 11:13:23 -0800 | [diff] [blame] | 38 | if (ptrace(PTRACE_SEIZE, pid, 0, 0) == -1) { | 
|  | 39 | return false; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | if (ptrace(PTRACE_INTERRUPT, pid, 0, 0) == -1) { | 
|  | 43 | ptrace(PTRACE_DETACH, pid, 0, 0); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 44 | return false; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | // Allow at least 1 second to attach properly. | 
|  | 48 | for (size_t i = 0; i < 1000; i++) { | 
|  | 49 | siginfo_t si; | 
|  | 50 | if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { | 
|  | 51 | return true; | 
|  | 52 | } | 
|  | 53 | usleep(1000); | 
|  | 54 | } | 
|  | 55 | printf("%d: Failed to stop.\n", pid); | 
|  | 56 | return false; | 
|  | 57 | } | 
|  | 58 |  | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 59 | void DoUnwind(pid_t pid) { | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 60 | unwindstack::RemoteMaps remote_maps(pid); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 61 | if (!remote_maps.Parse()) { | 
|  | 62 | printf("Failed to parse map data.\n"); | 
|  | 63 | return; | 
|  | 64 | } | 
|  | 65 |  | 
| Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 66 | unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 67 | if (regs == nullptr) { | 
|  | 68 | printf("Unable to get remote reg data\n"); | 
|  | 69 | return; | 
|  | 70 | } | 
|  | 71 |  | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 72 | printf("ABI: "); | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 73 | switch (regs->Arch()) { | 
|  | 74 | case unwindstack::ARCH_ARM: | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 75 | printf("arm"); | 
|  | 76 | break; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 77 | case unwindstack::ARCH_X86: | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 78 | printf("x86"); | 
|  | 79 | break; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 80 | case unwindstack::ARCH_ARM64: | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 81 | printf("arm64"); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 82 | break; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 83 | case unwindstack::ARCH_X86_64: | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 84 | printf("x86_64"); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 85 | break; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 86 | case unwindstack::ARCH_MIPS: | 
|  | 87 | printf("mips"); | 
|  | 88 | break; | 
|  | 89 | case unwindstack::ARCH_MIPS64: | 
|  | 90 | printf("mips64"); | 
|  | 91 | break; | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 92 | default: | 
|  | 93 | printf("unknown\n"); | 
|  | 94 | return; | 
|  | 95 | } | 
|  | 96 | printf("\n"); | 
|  | 97 |  | 
| Christopher Ferris | 5f11851 | 2017-09-01 11:17:16 -0700 | [diff] [blame] | 98 | auto process_memory = unwindstack::Memory::CreateProcessMemory(pid); | 
| Christopher Ferris | ae3b136 | 2017-10-05 16:05:44 -0700 | [diff] [blame] | 99 | unwindstack::Unwinder unwinder(128, &remote_maps, regs, process_memory); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 100 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 101 | unwindstack::JitDebug jit_debug(process_memory); | 
|  | 102 | unwinder.SetJitDebug(&jit_debug, regs->Arch()); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 103 |  | 
|  | 104 | unwindstack::DexFiles dex_files(process_memory); | 
|  | 105 | unwinder.SetDexFiles(&dex_files, regs->Arch()); | 
|  | 106 |  | 
| Christopher Ferris | ae3b136 | 2017-10-05 16:05:44 -0700 | [diff] [blame] | 107 | unwinder.Unwind(); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 108 |  | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 109 | // Print the frames. | 
| Christopher Ferris | ae3b136 | 2017-10-05 16:05:44 -0700 | [diff] [blame] | 110 | for (size_t i = 0; i < unwinder.NumFrames(); i++) { | 
|  | 111 | printf("%s\n", unwinder.FormatFrame(i).c_str()); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 112 | } | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | int main(int argc, char** argv) { | 
|  | 116 | if (argc != 2) { | 
|  | 117 | printf("Usage: unwind <PID>\n"); | 
|  | 118 | return 1; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | pid_t pid = atoi(argv[1]); | 
|  | 122 | if (!Attach(pid)) { | 
|  | 123 | printf("Failed to attach to pid %d: %s\n", pid, strerror(errno)); | 
|  | 124 | return 1; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | DoUnwind(pid); | 
|  | 128 |  | 
| Christopher Ferris | ae3b136 | 2017-10-05 16:05:44 -0700 | [diff] [blame] | 129 | ptrace(PTRACE_DETACH, pid, 0, 0); | 
| Christopher Ferris | b5d7a87 | 2017-07-12 15:40:52 -0700 | [diff] [blame] | 130 |  | 
|  | 131 | return 0; | 
|  | 132 | } |