Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 1 | /* system/debuggerd/debuggerd.c |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <pthread.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <dirent.h> |
| 25 | |
| 26 | #include <sys/ptrace.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <sys/exec_elf.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #include <cutils/sockets.h> |
| 32 | #include <cutils/properties.h> |
| 33 | |
| 34 | #include <linux/input.h> |
David 'Digit' Turner | 2c25991 | 2011-01-26 15:11:04 +0100 | [diff] [blame] | 35 | #include <linux/user.h> |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 36 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 37 | #include "../utility.h" |
| 38 | #include "../machine.h" |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 39 | |
Andy McFadden | f2eae5a | 2011-10-18 15:42:03 -0700 | [diff] [blame] | 40 | /* enable to dump memory pointed to by every register */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 41 | #define DUMP_MEMORY_FOR_ALL_REGISTERS 1 |
Andy McFadden | f2eae5a | 2011-10-18 15:42:03 -0700 | [diff] [blame] | 42 | |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 43 | #ifdef WITH_VFP |
| 44 | #ifdef WITH_VFP_D32 |
| 45 | #define NUM_VFP_REGS 32 |
| 46 | #else |
| 47 | #define NUM_VFP_REGS 16 |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 51 | /* |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 52 | * If configured to do so, dump memory around *all* registers |
| 53 | * for the crashing thread. |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 54 | */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 55 | static void dump_memory_and_code(int tfd, pid_t tid, bool at_fault) { |
| 56 | struct pt_regs regs; |
| 57 | if(ptrace(PTRACE_GETREGS, tid, 0, ®s)) { |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 58 | return; |
| 59 | } |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 60 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 61 | if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) { |
| 62 | static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp"; |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 63 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 64 | for (int reg = 0; reg < 14; reg++) { |
Andy McFadden | f2eae5a | 2011-10-18 15:42:03 -0700 | [diff] [blame] | 65 | /* this may not be a valid way to access, but it'll do for now */ |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 66 | uintptr_t addr = regs.uregs[reg]; |
Andy McFadden | f2eae5a | 2011-10-18 15:42:03 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Don't bother if it looks like a small int or ~= null, or if |
| 70 | * it's in the kernel area. |
| 71 | */ |
| 72 | if (addr < 4096 || addr >= 0xc0000000) { |
| 73 | continue; |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 74 | } |
Andy McFadden | f2eae5a | 2011-10-18 15:42:03 -0700 | [diff] [blame] | 75 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 76 | _LOG(tfd, false, "\nmemory near %.2s:\n", ®_NAMES[reg * 2]); |
| 77 | dump_memory(tfd, tid, addr, at_fault); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 81 | _LOG(tfd, !at_fault, "\ncode around pc:\n"); |
| 82 | dump_memory(tfd, tid, (uintptr_t)regs.ARM_pc, at_fault); |
Andy McFadden | 136dcc5 | 2011-09-22 16:37:06 -0700 | [diff] [blame] | 83 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 84 | if (regs.ARM_pc != regs.ARM_lr) { |
| 85 | _LOG(tfd, !at_fault, "\ncode around lr:\n"); |
| 86 | dump_memory(tfd, tid, (uintptr_t)regs.ARM_lr, at_fault); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 90 | void dump_registers(ptrace_context_t* context __attribute((unused)), |
| 91 | int tfd, pid_t tid, bool at_fault) |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 92 | { |
| 93 | struct pt_regs r; |
| 94 | bool only_in_tombstone = !at_fault; |
| 95 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 96 | if(ptrace(PTRACE_GETREGS, tid, 0, &r)) { |
| 97 | _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno)); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 101 | _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n", |
| 102 | (uint32_t)r.ARM_r0, (uint32_t)r.ARM_r1, (uint32_t)r.ARM_r2, (uint32_t)r.ARM_r3); |
| 103 | _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n", |
| 104 | (uint32_t)r.ARM_r4, (uint32_t)r.ARM_r5, (uint32_t)r.ARM_r6, (uint32_t)r.ARM_r7); |
| 105 | _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x sl %08x fp %08x\n", |
| 106 | (uint32_t)r.ARM_r8, (uint32_t)r.ARM_r9, (uint32_t)r.ARM_r10, (uint32_t)r.ARM_fp); |
| 107 | _LOG(tfd, only_in_tombstone, " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n", |
| 108 | (uint32_t)r.ARM_ip, (uint32_t)r.ARM_sp, (uint32_t)r.ARM_lr, |
| 109 | (uint32_t)r.ARM_pc, (uint32_t)r.ARM_cpsr); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 110 | |
| 111 | #ifdef WITH_VFP |
| 112 | struct user_vfp vfp_regs; |
| 113 | int i; |
| 114 | |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 115 | if(ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) { |
| 116 | _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno)); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | for (i = 0; i < NUM_VFP_REGS; i += 2) { |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 121 | _LOG(tfd, only_in_tombstone, " d%-2d %016llx d%-2d %016llx\n", |
| 122 | i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 123 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 124 | _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 125 | #endif |
| 126 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame^] | 127 | |
| 128 | void dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) { |
| 129 | dump_registers(context, tfd, tid, at_fault); |
| 130 | |
| 131 | dump_backtrace_and_stack(context, tfd, tid, at_fault); |
| 132 | |
| 133 | if (at_fault) { |
| 134 | dump_memory_and_code(tfd, tid, at_fault); |
| 135 | dump_nearby_maps(context, tfd, tid); |
| 136 | } |
| 137 | } |