blob: 891b1efaeb5cb70e9e84251b2d5542d749b8ce5a [file] [log] [blame]
Bruce Beare84924902010-10-13 14:21:30 -07001/* 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' Turner2c259912011-01-26 15:11:04 +010035#include <linux/user.h>
Bruce Beare84924902010-10-13 14:21:30 -070036
Jeff Brown13e715b2011-10-21 12:14:56 -070037#include "../utility.h"
38#include "../machine.h"
Bruce Beare84924902010-10-13 14:21:30 -070039
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070040/* enable to dump memory pointed to by every register */
Jeff Brown13e715b2011-10-21 12:14:56 -070041#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070042
Bruce Beare84924902010-10-13 14:21:30 -070043#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 McFadden136dcc52011-09-22 16:37:06 -070051/*
Jeff Brown13e715b2011-10-21 12:14:56 -070052 * If configured to do so, dump memory around *all* registers
53 * for the crashing thread.
Andy McFadden136dcc52011-09-22 16:37:06 -070054 */
Jeff Brown13e715b2011-10-21 12:14:56 -070055static 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, &regs)) {
Andy McFadden136dcc52011-09-22 16:37:06 -070058 return;
59 }
Andy McFadden136dcc52011-09-22 16:37:06 -070060
Jeff Brown13e715b2011-10-21 12:14:56 -070061 if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) {
62 static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
Andy McFadden136dcc52011-09-22 16:37:06 -070063
Jeff Brown13e715b2011-10-21 12:14:56 -070064 for (int reg = 0; reg < 14; reg++) {
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070065 /* this may not be a valid way to access, but it'll do for now */
Jeff Brown13e715b2011-10-21 12:14:56 -070066 uintptr_t addr = regs.uregs[reg];
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070067
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 Beare84924902010-10-13 14:21:30 -070074 }
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070075
Jeff Brown13e715b2011-10-21 12:14:56 -070076 _LOG(tfd, false, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
77 dump_memory(tfd, tid, addr, at_fault);
Bruce Beare84924902010-10-13 14:21:30 -070078 }
79 }
80
Jeff Brown13e715b2011-10-21 12:14:56 -070081 _LOG(tfd, !at_fault, "\ncode around pc:\n");
82 dump_memory(tfd, tid, (uintptr_t)regs.ARM_pc, at_fault);
Andy McFadden136dcc52011-09-22 16:37:06 -070083
Jeff Brown13e715b2011-10-21 12:14:56 -070084 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 Beare84924902010-10-13 14:21:30 -070087 }
88}
89
Jeff Brown13e715b2011-10-21 12:14:56 -070090void dump_registers(ptrace_context_t* context __attribute((unused)),
91 int tfd, pid_t tid, bool at_fault)
Bruce Beare84924902010-10-13 14:21:30 -070092{
93 struct pt_regs r;
94 bool only_in_tombstone = !at_fault;
95
Jeff Brown13e715b2011-10-21 12:14:56 -070096 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
97 _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
Bruce Beare84924902010-10-13 14:21:30 -070098 return;
99 }
100
Jeff Brown13e715b2011-10-21 12:14:56 -0700101 _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 Beare84924902010-10-13 14:21:30 -0700110
111#ifdef WITH_VFP
112 struct user_vfp vfp_regs;
113 int i;
114
Jeff Brown13e715b2011-10-21 12:14:56 -0700115 if(ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) {
116 _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
Bruce Beare84924902010-10-13 14:21:30 -0700117 return;
118 }
119
120 for (i = 0; i < NUM_VFP_REGS; i += 2) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700121 _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 Beare84924902010-10-13 14:21:30 -0700123 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700124 _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr);
Bruce Beare84924902010-10-13 14:21:30 -0700125#endif
126}
Jeff Brown13e715b2011-10-21 12:14:56 -0700127
128void 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}