blob: 4ab6026bfbf2d0e381f919062ccdc8fc7f80a405 [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
Elliott Hughes9e7d2182013-11-26 18:01:29 -080018#include <errno.h>
Jeff Brown053b8652012-06-06 16:25:03 -070019#include <stdbool.h>
Elliott Hughes9e7d2182013-11-26 18:01:29 -080020#include <stddef.h>
21#include <stdio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <stdlib.h>
23#include <string.h>
Bruce Beare84924902010-10-13 14:21:30 -070024#include <sys/ptrace.h>
Elliott Hughes9e7d2182013-11-26 18:01:29 -080025#include <sys/types.h>
26#include <sys/user.h>
Bruce Beare84924902010-10-13 14:21:30 -070027
Jeff Brown13e715b2011-10-21 12:14:56 -070028#include "../utility.h"
29#include "../machine.h"
Bruce Beare84924902010-10-13 14:21:30 -070030
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070031/* enable to dump memory pointed to by every register */
Jeff Brown13e715b2011-10-21 12:14:56 -070032#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070033
Bruce Beare84924902010-10-13 14:21:30 -070034#ifdef WITH_VFP
35#ifdef WITH_VFP_D32
36#define NUM_VFP_REGS 32
37#else
38#define NUM_VFP_REGS 16
39#endif
40#endif
41
Christopher Ferris365e4ae2013-10-02 12:26:48 -070042static void dump_memory(log_t* log, pid_t tid, uintptr_t addr, int scope_flags) {
Jeff Brown053b8652012-06-06 16:25:03 -070043 char code_buffer[64]; /* actual 8+1+((8+1)*4) + 1 == 45 */
44 char ascii_buffer[32]; /* actual 16 + 1 == 17 */
45 uintptr_t p, end;
46
47 p = addr & ~3;
48 p -= 32;
49 if (p > addr) {
50 /* catch underflow */
51 p = 0;
52 }
Ben Chengc47b7722012-09-24 14:13:37 -070053 /* Dump more memory content for the crashing thread. */
54 end = p + 256;
Jeff Brown053b8652012-06-06 16:25:03 -070055 /* catch overflow; 'end - p' has to be multiples of 16 */
56 while (end < p)
57 end -= 16;
58
59 /* Dump the code around PC as:
60 * addr contents ascii
61 * 00008d34 ef000000 e8bd0090 e1b00000 512fff1e ............../Q
62 * 00008d44 ea00b1f9 e92d0090 e3a070fc ef000000 ......-..p......
63 */
64 while (p < end) {
65 char* asc_out = ascii_buffer;
66
67 sprintf(code_buffer, "%08x ", p);
68
69 int i;
70 for (i = 0; i < 4; i++) {
71 /*
72 * If we see (data == -1 && errno != 0), we know that the ptrace
73 * call failed, probably because we're dumping memory in an
74 * unmapped or inaccessible page. I don't know if there's
75 * value in making that explicit in the output -- it likely
76 * just complicates parsing and clarifies nothing for the
77 * enlightened reader.
78 */
79 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
80 sprintf(code_buffer + strlen(code_buffer), "%08lx ", data);
81
Ben Chengc47b7722012-09-24 14:13:37 -070082 /* Enable the following code blob to dump ASCII values */
83#if 0
Jeff Brown053b8652012-06-06 16:25:03 -070084 int j;
85 for (j = 0; j < 4; j++) {
86 /*
87 * Our isprint() allows high-ASCII characters that display
88 * differently (often badly) in different viewers, so we
89 * just use a simpler test.
90 */
91 char val = (data >> (j*8)) & 0xff;
92 if (val >= 0x20 && val < 0x7f) {
93 *asc_out++ = val;
94 } else {
95 *asc_out++ = '.';
96 }
97 }
Ben Chengc47b7722012-09-24 14:13:37 -070098#endif
Jeff Brown053b8652012-06-06 16:25:03 -070099 p += 4;
100 }
101 *asc_out = '\0';
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700102 _LOG(log, scope_flags, " %s %s\n", code_buffer, ascii_buffer);
Jeff Brown053b8652012-06-06 16:25:03 -0700103 }
104}
105
Andy McFadden136dcc52011-09-22 16:37:06 -0700106/*
Jeff Brown13e715b2011-10-21 12:14:56 -0700107 * If configured to do so, dump memory around *all* registers
108 * for the crashing thread.
Andy McFadden136dcc52011-09-22 16:37:06 -0700109 */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700110void dump_memory_and_code(log_t* log, pid_t tid, int scope_flags) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700111 struct pt_regs regs;
112 if(ptrace(PTRACE_GETREGS, tid, 0, &regs)) {
Andy McFadden136dcc52011-09-22 16:37:06 -0700113 return;
114 }
Andy McFadden136dcc52011-09-22 16:37:06 -0700115
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700116 if (IS_AT_FAULT(scope_flags) && DUMP_MEMORY_FOR_ALL_REGISTERS) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700117 static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
Andy McFadden136dcc52011-09-22 16:37:06 -0700118
Jeff Brown13e715b2011-10-21 12:14:56 -0700119 for (int reg = 0; reg < 14; reg++) {
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700120 /* this may not be a valid way to access, but it'll do for now */
Jeff Brown13e715b2011-10-21 12:14:56 -0700121 uintptr_t addr = regs.uregs[reg];
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700122
123 /*
124 * Don't bother if it looks like a small int or ~= null, or if
125 * it's in the kernel area.
126 */
127 if (addr < 4096 || addr >= 0xc0000000) {
128 continue;
Bruce Beare84924902010-10-13 14:21:30 -0700129 }
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700130
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700131 _LOG(log, scope_flags | SCOPE_SENSITIVE, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
132 dump_memory(log, tid, addr, scope_flags | SCOPE_SENSITIVE);
Bruce Beare84924902010-10-13 14:21:30 -0700133 }
134 }
135
Christopher Tate7716aef2013-04-02 14:00:27 -0700136 /* explicitly allow upload of code dump logging */
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700137 _LOG(log, scope_flags, "\ncode around pc:\n");
138 dump_memory(log, tid, (uintptr_t)regs.ARM_pc, scope_flags);
Andy McFadden136dcc52011-09-22 16:37:06 -0700139
Jeff Brown13e715b2011-10-21 12:14:56 -0700140 if (regs.ARM_pc != regs.ARM_lr) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700141 _LOG(log, scope_flags, "\ncode around lr:\n");
142 dump_memory(log, tid, (uintptr_t)regs.ARM_lr, scope_flags);
Bruce Beare84924902010-10-13 14:21:30 -0700143 }
144}
145
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700146void dump_registers(log_t* log, pid_t tid, int scope_flags)
Bruce Beare84924902010-10-13 14:21:30 -0700147{
148 struct pt_regs r;
Jeff Brown13e715b2011-10-21 12:14:56 -0700149 if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700150 _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
Bruce Beare84924902010-10-13 14:21:30 -0700151 return;
152 }
153
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700154 _LOG(log, scope_flags, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
Jeff Brown13e715b2011-10-21 12:14:56 -0700155 (uint32_t)r.ARM_r0, (uint32_t)r.ARM_r1, (uint32_t)r.ARM_r2, (uint32_t)r.ARM_r3);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700156 _LOG(log, scope_flags, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
Jeff Brown13e715b2011-10-21 12:14:56 -0700157 (uint32_t)r.ARM_r4, (uint32_t)r.ARM_r5, (uint32_t)r.ARM_r6, (uint32_t)r.ARM_r7);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700158 _LOG(log, scope_flags, " r8 %08x r9 %08x sl %08x fp %08x\n",
Jeff Brown13e715b2011-10-21 12:14:56 -0700159 (uint32_t)r.ARM_r8, (uint32_t)r.ARM_r9, (uint32_t)r.ARM_r10, (uint32_t)r.ARM_fp);
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700160 _LOG(log, scope_flags, " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
Jeff Brown13e715b2011-10-21 12:14:56 -0700161 (uint32_t)r.ARM_ip, (uint32_t)r.ARM_sp, (uint32_t)r.ARM_lr,
162 (uint32_t)r.ARM_pc, (uint32_t)r.ARM_cpsr);
Bruce Beare84924902010-10-13 14:21:30 -0700163
164#ifdef WITH_VFP
165 struct user_vfp vfp_regs;
166 int i;
167
Jeff Brown13e715b2011-10-21 12:14:56 -0700168 if(ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700169 _LOG(log, scope_flags, "cannot get registers: %s\n", strerror(errno));
Bruce Beare84924902010-10-13 14:21:30 -0700170 return;
171 }
172
173 for (i = 0; i < NUM_VFP_REGS; i += 2) {
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700174 _LOG(log, scope_flags, " d%-2d %016llx d%-2d %016llx\n",
Jeff Brown13e715b2011-10-21 12:14:56 -0700175 i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
Bruce Beare84924902010-10-13 14:21:30 -0700176 }
Christopher Ferris365e4ae2013-10-02 12:26:48 -0700177 _LOG(log, scope_flags, " scr %08lx\n", vfp_regs.fpscr);
Bruce Beare84924902010-10-13 14:21:30 -0700178#endif
179}