blob: fb0d6baa5ef8aa2c6034727ea7f62f9e70293623 [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
37#include "utility.h"
38
Andy McFaddenf2eae5a2011-10-18 15:42:03 -070039/* enable to dump memory pointed to by every register */
40#define DUMP_MEM_FOR_ALL_REGS 0
41
Bruce Beare84924902010-10-13 14:21:30 -070042#ifdef WITH_VFP
43#ifdef WITH_VFP_D32
44#define NUM_VFP_REGS 32
45#else
46#define NUM_VFP_REGS 16
47#endif
48#endif
49
50/* Main entry point to get the backtrace from the crashing process */
51extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
52 unsigned int sp_list[],
53 int *frame0_pc_sane,
54 bool at_fault);
55
Andy McFadden136dcc52011-09-22 16:37:06 -070056/*
57 * If this isn't clearly a null pointer dereference, dump the
58 * /proc/maps entries near the fault address.
59 */
60static void show_nearby_maps(int tfd, int pid, mapinfo *map)
61{
62 siginfo_t si;
63
64 memset(&si, 0, sizeof(si));
65 if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si)) {
66 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
67 return;
68 }
69 if (!signal_has_address(si.si_signo))
70 return;
71
72 uintptr_t addr = (uintptr_t) si.si_addr;
73 addr &= ~0xfff; /* round to 4K page boundary */
74 if (addr == 0) /* null-pointer deref */
75 return;
76
77 _LOG(tfd, false, "\nmemory map around addr %08x:\n", si.si_addr);
78
79 /*
80 * Search for a match, or for a hole where the match would be. The list
81 * is backward from the file content, so it starts at high addresses.
82 */
83 bool found = false;
84 mapinfo *next = NULL;
85 mapinfo *prev = NULL;
86 while (map != NULL) {
87 if (addr >= map->start && addr < map->end) {
88 found = true;
89 next = map->next;
90 break;
91 } else if (addr >= map->end) {
92 /* map would be between "prev" and this entry */
93 next = map;
94 map = NULL;
95 break;
96 }
97
98 prev = map;
99 map = map->next;
100 }
101
102 /*
103 * Show "next" then "match" then "prev" so that the addresses appear in
104 * ascending order (like /proc/pid/maps).
105 */
106 if (next != NULL) {
107 _LOG(tfd, false, "%08x-%08x %s\n", next->start, next->end, next->name);
108 } else {
109 _LOG(tfd, false, "(no map below)\n");
110 }
111 if (map != NULL) {
112 _LOG(tfd, false, "%08x-%08x %s\n", map->start, map->end, map->name);
113 } else {
114 _LOG(tfd, false, "(no map for address)\n");
115 }
116 if (prev != NULL) {
117 _LOG(tfd, false, "%08x-%08x %s\n", prev->start, prev->end, prev->name);
118 } else {
119 _LOG(tfd, false, "(no map above)\n");
120 }
121}
122
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700123/*
124 * Dumps a few bytes of memory, starting a bit before and ending a bit
125 * after the specified address.
126 */
127static void dump_memory(int tfd, int pid, uintptr_t addr,
128 bool only_in_tombstone)
129{
130 char code_buffer[64]; /* actual 8+1+((8+1)*4) + 1 == 45 */
131 char ascii_buffer[32]; /* actual 16 + 1 == 17 */
132 uintptr_t p, end;
133
134 p = addr & ~3;
135 p -= 32;
136 if (p > addr) {
137 /* catch underflow */
138 p = 0;
139 }
140 end = p + 80;
141 /* catch overflow; 'end - p' has to be multiples of 16 */
142 while (end < p)
143 end -= 16;
144
145 /* Dump the code around PC as:
146 * addr contents ascii
147 * 00008d34 ef000000 e8bd0090 e1b00000 512fff1e ............../Q
148 * 00008d44 ea00b1f9 e92d0090 e3a070fc ef000000 ......-..p......
149 */
150 while (p < end) {
151 char* asc_out = ascii_buffer;
152
153 sprintf(code_buffer, "%08x ", p);
154
155 int i;
156 for (i = 0; i < 4; i++) {
157 /*
158 * If we see (data == -1 && errno != 0), we know that the ptrace
159 * call failed, probably because we're dumping memory in an
160 * unmapped or inaccessible page. I don't know if there's
161 * value in making that explicit in the output -- it likely
162 * just complicates parsing and clarifies nothing for the
163 * enlightened reader.
164 */
165 long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
166 sprintf(code_buffer + strlen(code_buffer), "%08lx ", data);
167
168 int j;
169 for (j = 0; j < 4; j++) {
170 /*
171 * Our isprint() allows high-ASCII characters that display
172 * differently (often badly) in different viewers, so we
173 * just use a simpler test.
174 */
175 char val = (data >> (j*8)) & 0xff;
176 if (val >= 0x20 && val < 0x7f) {
177 *asc_out++ = val;
178 } else {
179 *asc_out++ = '.';
180 }
181 }
182 p += 4;
183 }
184 *asc_out = '\0';
185 _LOG(tfd, only_in_tombstone, "%s %s\n", code_buffer, ascii_buffer);
186 }
187
188}
Andy McFadden136dcc52011-09-22 16:37:06 -0700189
Bruce Beare84924902010-10-13 14:21:30 -0700190void dump_stack_and_code(int tfd, int pid, mapinfo *map,
191 int unwind_depth, unsigned int sp_list[],
192 bool at_fault)
193{
Bruce Beare84924902010-10-13 14:21:30 -0700194 struct pt_regs r;
195 int sp_depth;
196 bool only_in_tombstone = !at_fault;
Bruce Beare84924902010-10-13 14:21:30 -0700197
198 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return;
Bruce Beare84924902010-10-13 14:21:30 -0700199
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700200 if (DUMP_MEM_FOR_ALL_REGS && at_fault) {
201 /*
202 * If configured to do so, dump memory around *all* registers
203 * for the crashing thread.
204 *
205 * TODO: remove duplicates.
Bruce Beare84924902010-10-13 14:21:30 -0700206 */
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700207 static const char REG_NAMES[] = "R0R1R2R3R4R5R6R7R8R9SLFPIPSPLRPC";
Bruce Beare84924902010-10-13 14:21:30 -0700208
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700209 int reg;
210 for (reg = 0; reg < 16; reg++) {
211 /* this may not be a valid way to access, but it'll do for now */
212 uintptr_t addr = r.uregs[reg];
213
214 /*
215 * Don't bother if it looks like a small int or ~= null, or if
216 * it's in the kernel area.
217 */
218 if (addr < 4096 || addr >= 0xc0000000) {
219 continue;
Bruce Beare84924902010-10-13 14:21:30 -0700220 }
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700221
222 _LOG(tfd, only_in_tombstone, "\nmem near %.2s:\n",
223 &REG_NAMES[reg*2]);
224 dump_memory(tfd, pid, addr, false);
225 }
226 } else {
227 unsigned int pc, lr;
228 pc = r.ARM_pc;
229 lr = r.ARM_lr;
230
231 _LOG(tfd, only_in_tombstone, "\ncode around pc:\n");
232 dump_memory(tfd, pid, (uintptr_t) pc, only_in_tombstone);
233
234 if (lr != pc) {
235 _LOG(tfd, only_in_tombstone, "\ncode around lr:\n");
236 dump_memory(tfd, pid, (uintptr_t) lr, only_in_tombstone);
Bruce Beare84924902010-10-13 14:21:30 -0700237 }
238 }
239
Andy McFadden136dcc52011-09-22 16:37:06 -0700240 show_nearby_maps(tfd, pid, map);
241
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700242 unsigned int p, end;
243 unsigned int sp = r.ARM_sp;
244
Bruce Beare84924902010-10-13 14:21:30 -0700245 p = sp - 64;
Paul Eastham3227b5c2010-12-14 16:07:58 -0800246 if (p > sp)
247 p = 0;
Bruce Beare84924902010-10-13 14:21:30 -0700248 p &= ~3;
249 if (unwind_depth != 0) {
250 if (unwind_depth < STACK_CONTENT_DEPTH) {
251 end = sp_list[unwind_depth-1];
252 }
253 else {
254 end = sp_list[STACK_CONTENT_DEPTH-1];
255 }
256 }
257 else {
Ben Chengdce4d062011-02-24 16:37:52 -0800258 end = p + 256;
259 /* 'end - p' has to be multiples of 4 */
260 if (end < p)
261 end = ~7;
Bruce Beare84924902010-10-13 14:21:30 -0700262 }
263
264 _LOG(tfd, only_in_tombstone, "\nstack:\n");
265
266 /* If the crash is due to PC == 0, there will be two frames that
267 * have identical SP value.
268 */
269 if (sp_list[0] == sp_list[1]) {
270 sp_depth = 1;
271 }
272 else {
273 sp_depth = 0;
274 }
275
276 while (p <= end) {
277 char *prompt;
278 char level[16];
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700279 long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Bruce Beare84924902010-10-13 14:21:30 -0700280 if (p == sp_list[sp_depth]) {
281 sprintf(level, "#%02d", sp_depth++);
282 prompt = level;
283 }
284 else {
285 prompt = " ";
286 }
287
288 /* Print the stack content in the log for the first 3 frames. For the
289 * rest only print them in the tombstone file.
290 */
291 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
292 "%s %08x %08x %s\n", prompt, p, data,
293 map_to_name(map, data, ""));
294 p += 4;
295 }
296 /* print another 64-byte of stack data after the last frame */
297
298 end = p+64;
Ben Chengdce4d062011-02-24 16:37:52 -0800299 /* 'end - p' has to be multiples of 4 */
Paul Eastham3227b5c2010-12-14 16:07:58 -0800300 if (end < p)
Ben Chengdce4d062011-02-24 16:37:52 -0800301 end = ~7;
Paul Eastham3227b5c2010-12-14 16:07:58 -0800302
Bruce Beare84924902010-10-13 14:21:30 -0700303 while (p <= end) {
Andy McFaddenf2eae5a2011-10-18 15:42:03 -0700304 long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Bruce Beare84924902010-10-13 14:21:30 -0700305 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
306 " %08x %08x %s\n", p, data,
307 map_to_name(map, data, ""));
308 p += 4;
309 }
310}
311
312void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level,
313 bool at_fault)
314{
315 struct pt_regs r;
316
317 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
318 _LOG(tfd, !at_fault, "tid %d not responding!\n", pid);
319 return;
320 }
321
322 if (unwound_level == 0) {
323 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n", 0, r.ARM_pc,
324 map_to_name(map, r.ARM_pc, "<unknown>"));
325 }
326 _LOG(tfd, !at_fault, " #%02d lr %08x %s\n", 1, r.ARM_lr,
327 map_to_name(map, r.ARM_lr, "<unknown>"));
328}
329
330void dump_registers(int tfd, int pid, bool at_fault)
331{
332 struct pt_regs r;
333 bool only_in_tombstone = !at_fault;
334
335 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
336 _LOG(tfd, only_in_tombstone,
337 "cannot get registers: %s\n", strerror(errno));
338 return;
339 }
340
341 _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
342 r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3);
343 _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
344 r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7);
345 _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x 10 %08x fp %08x\n",
346 r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp);
347 _LOG(tfd, only_in_tombstone,
348 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
349 r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr);
350
351#ifdef WITH_VFP
352 struct user_vfp vfp_regs;
353 int i;
354
355 if(ptrace(PTRACE_GETVFPREGS, pid, 0, &vfp_regs)) {
356 _LOG(tfd, only_in_tombstone,
357 "cannot get registers: %s\n", strerror(errno));
358 return;
359 }
360
361 for (i = 0; i < NUM_VFP_REGS; i += 2) {
362 _LOG(tfd, only_in_tombstone,
363 " d%-2d %016llx d%-2d %016llx\n",
364 i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
365 }
366 _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr);
367#endif
368}