blob: 248bc9ad2253db4ac87daf97f95670c761c0d7e7 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* system/debuggerd/debuggerd.c
2**
3** Copyright 2006, The Android Open Source Project
4**
Ben Cheng09e71372009-09-28 11:06:09 -07005** 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008**
Ben Cheng09e71372009-09-28 11:06:09 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010**
Ben Cheng09e71372009-09-28 11:06:09 -070011** 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <errno.h>
22#include <signal.h>
23#include <pthread.h>
24#include <stdarg.h>
25#include <fcntl.h>
26#include <sys/types.h>
27#include <dirent.h>
28
29#include <sys/ptrace.h>
30#include <sys/wait.h>
31#include <sys/exec_elf.h>
32#include <sys/stat.h>
33
34#include <cutils/sockets.h>
35#include <cutils/logd.h>
36#include <cutils/sockets.h>
37#include <cutils/properties.h>
38
39#include <linux/input.h>
40
41#include <private/android_filesystem_config.h>
42
Hristo Bojinovc031a3b2010-08-27 14:00:57 -070043#include <byteswap.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#include "utility.h"
45
Colin Crosse951f602010-03-08 19:21:07 -080046#ifdef WITH_VFP
47#ifdef WITH_VFP_D32
48#define NUM_VFP_REGS 32
49#else
50#define NUM_VFP_REGS 16
51#endif
52#endif
53
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054/* Main entry point to get the backtrace from the crashing process */
55extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
56 unsigned int sp_list[],
57 int *frame0_pc_sane,
58 bool at_fault);
59
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060static int logsocket = -1;
61
62#define ANDROID_LOG_INFO 4
63
64/* Log information onto the tombstone */
65void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
66{
Meng Huae7b91b2009-11-05 16:10:50 -060067 char buf[512];
Ben Cheng09e71372009-09-28 11:06:09 -070068
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 va_list ap;
70 va_start(ap, fmt);
71
72 if (tfd >= 0) {
73 int len;
74 vsnprintf(buf, sizeof(buf), fmt, ap);
75 len = strlen(buf);
76 if(tfd >= 0) write(tfd, buf, len);
77 }
78
79 if (!in_tombstone_only)
80 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
81}
82
83#define LOG(fmt...) _LOG(-1, 0, fmt)
84#if 0
85#define XLOG(fmt...) _LOG(-1, 0, fmt)
86#else
87#define XLOG(fmt...) do {} while(0)
88#endif
89
90// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
91// 012345678901234567890123456789012345678901234567890123456789
92// 0 1 2 3 4 5
93
94mapinfo *parse_maps_line(char *line)
95{
96 mapinfo *mi;
97 int len = strlen(line);
98
99 if(len < 1) return 0;
100 line[--len] = 0;
Ben Cheng09e71372009-09-28 11:06:09 -0700101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 if(len < 50) return 0;
103 if(line[20] != 'x') return 0;
104
105 mi = malloc(sizeof(mapinfo) + (len - 47));
106 if(mi == 0) return 0;
Ben Cheng09e71372009-09-28 11:06:09 -0700107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 mi->start = strtoul(line, 0, 16);
109 mi->end = strtoul(line + 9, 0, 16);
Meng Huae7b91b2009-11-05 16:10:50 -0600110 /* To be filled in parse_elf_info if the mapped section starts with
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 * elf_header
112 */
113 mi->exidx_start = mi->exidx_end = 0;
Meng Huae7b91b2009-11-05 16:10:50 -0600114 mi->symbols = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 mi->next = 0;
116 strcpy(mi->name, line + 49);
117
118 return mi;
119}
120
121void dump_build_info(int tfd)
122{
123 char fingerprint[PROPERTY_VALUE_MAX];
124
125 property_get("ro.build.fingerprint", fingerprint, "unknown");
126
127 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
128}
129
130
Ben Cheng09e71372009-09-28 11:06:09 -0700131void dump_stack_and_code(int tfd, int pid, mapinfo *map,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 int unwind_depth, unsigned int sp_list[],
Ben Cheng2854db82010-01-28 10:00:03 -0800133 bool at_fault)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134{
135 unsigned int sp, pc, p, end, data;
136 struct pt_regs r;
137 int sp_depth;
138 bool only_in_tombstone = !at_fault;
Ben Cheng09e71372009-09-28 11:06:09 -0700139 char code_buffer[80];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140
141 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return;
142 sp = r.ARM_sp;
143 pc = r.ARM_pc;
144
Ben Cheng2854db82010-01-28 10:00:03 -0800145 _LOG(tfd, only_in_tombstone, "\ncode around pc:\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
147 end = p = pc & ~3;
Ben Cheng2854db82010-01-28 10:00:03 -0800148 p -= 32;
149 end += 32;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150
Ben Cheng09e71372009-09-28 11:06:09 -0700151 /* Dump the code around PC as:
152 * addr contents
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
154 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
155 */
156 while (p <= end) {
157 int i;
158
Ben Cheng09e71372009-09-28 11:06:09 -0700159 sprintf(code_buffer, "%08x ", p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 for (i = 0; i < 4; i++) {
161 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700162 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 p += 4;
164 }
Ben Cheng09e71372009-09-28 11:06:09 -0700165 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
166 }
167
Ben Cheng2854db82010-01-28 10:00:03 -0800168 if ((unsigned) r.ARM_lr != pc) {
Ben Cheng09e71372009-09-28 11:06:09 -0700169 _LOG(tfd, only_in_tombstone, "\ncode around lr:\n");
170
171 end = p = r.ARM_lr & ~3;
Ben Cheng2854db82010-01-28 10:00:03 -0800172 p -= 32;
173 end += 32;
Ben Cheng09e71372009-09-28 11:06:09 -0700174
175 /* Dump the code around LR as:
176 * addr contents
177 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
178 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
179 */
180 while (p <= end) {
181 int i;
182
183 sprintf(code_buffer, "%08x ", p);
184 for (i = 0; i < 4; i++) {
185 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
186 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
187 p += 4;
188 }
189 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
192
193 p = sp - 64;
194 p &= ~3;
195 if (unwind_depth != 0) {
196 if (unwind_depth < STACK_CONTENT_DEPTH) {
197 end = sp_list[unwind_depth-1];
198 }
199 else {
200 end = sp_list[STACK_CONTENT_DEPTH-1];
201 }
202 }
203 else {
204 end = sp | 0x000000ff;
205 end += 0xff;
206 }
207
Ben Cheng09e71372009-09-28 11:06:09 -0700208 _LOG(tfd, only_in_tombstone, "\nstack:\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
210 /* If the crash is due to PC == 0, there will be two frames that
211 * have identical SP value.
212 */
213 if (sp_list[0] == sp_list[1]) {
214 sp_depth = 1;
215 }
216 else {
217 sp_depth = 0;
218 }
219
220 while (p <= end) {
Ben Cheng09e71372009-09-28 11:06:09 -0700221 char *prompt;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 char level[16];
223 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
224 if (p == sp_list[sp_depth]) {
225 sprintf(level, "#%02d", sp_depth++);
226 prompt = level;
227 }
228 else {
229 prompt = " ";
230 }
Ben Cheng09e71372009-09-28 11:06:09 -0700231
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 /* Print the stack content in the log for the first 3 frames. For the
233 * rest only print them in the tombstone file.
234 */
Ben Cheng09e71372009-09-28 11:06:09 -0700235 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
236 "%s %08x %08x %s\n", prompt, p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 map_to_name(map, data, ""));
238 p += 4;
239 }
240 /* print another 64-byte of stack data after the last frame */
241
242 end = p+64;
243 while (p <= end) {
244 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700245 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
246 " %08x %08x %s\n", p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 map_to_name(map, data, ""));
248 p += 4;
249 }
250}
251
Ben Cheng09e71372009-09-28 11:06:09 -0700252void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 bool at_fault)
254{
255 struct pt_regs r;
256
257 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
258 _LOG(tfd, !at_fault, "tid %d not responding!\n", pid);
259 return;
260 }
261
262 if (unwound_level == 0) {
263 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n", 0, r.ARM_pc,
264 map_to_name(map, r.ARM_pc, "<unknown>"));
265 }
266 _LOG(tfd, !at_fault, " #%02d lr %08x %s\n", 1, r.ARM_lr,
267 map_to_name(map, r.ARM_lr, "<unknown>"));
268}
269
Ben Cheng09e71372009-09-28 11:06:09 -0700270void dump_registers(int tfd, int pid, bool at_fault)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271{
272 struct pt_regs r;
273 bool only_in_tombstone = !at_fault;
274
275 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
Ben Cheng09e71372009-09-28 11:06:09 -0700276 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 "cannot get registers: %s\n", strerror(errno));
278 return;
279 }
Ben Cheng09e71372009-09-28 11:06:09 -0700280
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
282 r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3);
283 _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
284 r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7);
285 _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x 10 %08x fp %08x\n",
286 r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp);
Ben Cheng09e71372009-09-28 11:06:09 -0700287 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
Ben Cheng09e71372009-09-28 11:06:09 -0700289 r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr);
Ben Chengbdcff7d2009-12-17 12:50:58 -0800290
Colin Crosse951f602010-03-08 19:21:07 -0800291#ifdef WITH_VFP
Ben Chengbdcff7d2009-12-17 12:50:58 -0800292 struct user_vfp vfp_regs;
293 int i;
294
295 if(ptrace(PTRACE_GETVFPREGS, pid, 0, &vfp_regs)) {
296 _LOG(tfd, only_in_tombstone,
297 "cannot get registers: %s\n", strerror(errno));
298 return;
299 }
300
Colin Crosse951f602010-03-08 19:21:07 -0800301 for (i = 0; i < NUM_VFP_REGS; i += 2) {
Ben Chengbdcff7d2009-12-17 12:50:58 -0800302 _LOG(tfd, only_in_tombstone,
303 " d%-2d %016llx d%-2d %016llx\n",
304 i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
305 }
306 _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr);
307#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308}
309
310const char *get_signame(int sig)
311{
312 switch(sig) {
313 case SIGILL: return "SIGILL";
314 case SIGABRT: return "SIGABRT";
315 case SIGBUS: return "SIGBUS";
316 case SIGFPE: return "SIGFPE";
317 case SIGSEGV: return "SIGSEGV";
318 case SIGSTKFLT: return "SIGSTKFLT";
319 default: return "?";
320 }
321}
322
Carl Shapiro83c6b052010-10-08 18:10:24 -0700323const char *get_sigcode(int signo, int code)
324{
325 switch (signo) {
326 case SIGILL:
327 switch (code) {
328 case ILL_ILLOPC: return "ILL_ILLOPC";
329 case ILL_ILLOPN: return "ILL_ILLOPN";
330 case ILL_ILLADR: return "ILL_ILLADR";
331 case ILL_ILLTRP: return "ILL_ILLTRP";
332 case ILL_PRVOPC: return "ILL_PRVOPC";
333 case ILL_PRVREG: return "ILL_PRVREG";
334 case ILL_COPROC: return "ILL_COPROC";
335 case ILL_BADSTK: return "ILL_BADSTK";
336 }
337 break;
338 case SIGBUS:
339 switch (code) {
340 case BUS_ADRALN: return "BUS_ADRALN";
341 case BUS_ADRERR: return "BUS_ADRERR";
342 case BUS_OBJERR: return "BUS_OBJERR";
343 }
344 break;
345 case SIGFPE:
346 switch (code) {
347 case FPE_INTDIV: return "FPE_INTDIV";
348 case FPE_INTOVF: return "FPE_INTOVF";
349 case FPE_FLTDIV: return "FPE_FLTDIV";
350 case FPE_FLTOVF: return "FPE_FLTOVF";
351 case FPE_FLTUND: return "FPE_FLTUND";
352 case FPE_FLTRES: return "FPE_FLTRES";
353 case FPE_FLTINV: return "FPE_FLTINV";
354 case FPE_FLTSUB: return "FPE_FLTSUB";
355 }
356 break;
357 case SIGSEGV:
358 switch (code) {
359 case SEGV_MAPERR: return "SEGV_MAPERR";
360 case SEGV_ACCERR: return "SEGV_ACCERR";
361 }
362 break;
363 }
364 return "?";
365}
366
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367void dump_fault_addr(int tfd, int pid, int sig)
368{
369 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 memset(&si, 0, sizeof(si));
372 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
373 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
374 } else {
Carl Shapiro83c6b052010-10-08 18:10:24 -0700375 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
376 sig, get_signame(sig),
377 si.si_code, get_sigcode(sig, si.si_code),
378 si.si_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 }
380}
381
382void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
383{
384 char data[1024];
385 char *x = 0;
386 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 sprintf(data, "/proc/%d/cmdline", pid);
389 fp = fopen(data, "r");
390 if(fp) {
391 x = fgets(data, 1024, fp);
392 fclose(fp);
393 }
Ben Cheng09e71372009-09-28 11:06:09 -0700394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 _LOG(tfd, false,
396 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
397 dump_build_info(tfd);
398 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
399 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700400
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 if(sig) dump_fault_addr(tfd, tid, sig);
402}
403
Hristo Bojinovc031a3b2010-08-27 14:00:57 -0700404/* After randomization (ASLR), stack contents that point to randomized
405 * code become uninterpretable (e.g. can't be resolved to line numbers).
406 * Here, we bundle enough information so that stack analysis on the
407 * server side can still be performed. This means we are leaking some
408 * information about the device (its randomization base). We have to make
409 * sure an attacker has no way of intercepting the tombstone.
410 */
411
412typedef struct {
413 int32_t mmap_addr;
414 char tag[4]; /* 'P', 'R', 'E', ' ' */
415} prelink_info_t __attribute__((packed));
416
417static inline void set_prelink(long *prelink_addr,
418 prelink_info_t *info)
419{
420 // We will assume the binary is little-endian, and test the
421 // host endianness here.
422 unsigned long test_endianness = 0xFF;
423
424 if (sizeof(prelink_info_t) == 8 && prelink_addr) {
425 if (*(unsigned char *)&test_endianness)
426 *prelink_addr = info->mmap_addr;
427 else
428 *prelink_addr = bswap_32(info->mmap_addr);
429 }
430}
431
432static int check_prelinked(const char *fname,
433 long *prelink_addr)
434{
435 *prelink_addr = 0;
436 if (sizeof(prelink_info_t) != 8) return 0;
437
438 int fd = open(fname, O_RDONLY);
439 if (fd < 0) return 0;
440 off_t end = lseek(fd, 0, SEEK_END);
441 int nr = sizeof(prelink_info_t);
442
443 off_t sz = lseek(fd, -nr, SEEK_CUR);
444 if ((long)(end - sz) != (long)nr) return 0;
445 if (sz == (off_t)-1) return 0;
446
447 prelink_info_t info;
448 int num_read = read(fd, &info, nr);
449 if (num_read < 0) return 0;
450 if (num_read != sizeof(info)) return 0;
451
452 int prelinked = 0;
453 if (!strncmp(info.tag, "PRE ", 4)) {
454 set_prelink(prelink_addr, &info);
455 prelinked = 1;
456 }
457 if (close(fd) < 0) return 0;
458 return prelinked;
459}
460
461void dump_randomization_base(int tfd, bool at_fault) {
462 bool only_in_tombstone = !at_fault;
463 long prelink_addr;
464 check_prelinked("/system/lib/libc.so", &prelink_addr);
465 _LOG(tfd, only_in_tombstone,
466 "\nlibc base address: %08x\n", prelink_addr);
467}
468
469/* End of ASLR-related logic. */
470
Meng Huae7b91b2009-11-05 16:10:50 -0600471static void parse_elf_info(mapinfo *milist, pid_t pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472{
473 mapinfo *mi;
474 for (mi = milist; mi != NULL; mi = mi->next) {
475 Elf32_Ehdr ehdr;
476
477 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700478 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 * mapped section.
480 */
Ben Cheng09e71372009-09-28 11:06:09 -0700481 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 sizeof(Elf32_Ehdr));
483 /* Check if it has the matching magic words */
484 if (IS_ELF(ehdr)) {
485 Elf32_Phdr phdr;
486 Elf32_Phdr *ptr;
487 int i;
488
489 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
490 for (i = 0; i < ehdr.e_phnum; i++) {
491 /* Parse the program header */
Mike Dodd6b657472010-07-14 11:28:29 -0700492 get_remote_struct(pid, (char *) (ptr+i), &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 sizeof(Elf32_Phdr));
494 /* Found a EXIDX segment? */
495 if (phdr.p_type == PT_ARM_EXIDX) {
496 mi->exidx_start = mi->start + phdr.p_offset;
497 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
498 break;
499 }
500 }
Meng Huae7b91b2009-11-05 16:10:50 -0600501
502 /* Try to load symbols from this file */
503 mi->symbols = symbol_table_create(mi->name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505 }
506}
507
508void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
509{
510 char data[1024];
511 FILE *fp;
512 mapinfo *milist = 0;
513 unsigned int sp_list[STACK_CONTENT_DEPTH];
514 int stack_depth;
515 int frame0_pc_sane = 1;
Ben Cheng09e71372009-09-28 11:06:09 -0700516
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 if (!at_fault) {
518 _LOG(tfd, true,
519 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
520 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
521 }
522
523 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700524
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 /* Clear stack pointer records */
526 memset(sp_list, 0, sizeof(sp_list));
527
528 sprintf(data, "/proc/%d/maps", pid);
529 fp = fopen(data, "r");
530 if(fp) {
531 while(fgets(data, 1024, fp)) {
532 mapinfo *mi = parse_maps_line(data);
533 if(mi) {
534 mi->next = milist;
535 milist = mi;
536 }
537 }
538 fclose(fp);
539 }
540
Meng Huae7b91b2009-11-05 16:10:50 -0600541 parse_elf_info(milist, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542
543 /* If stack unwinder fails, use the default solution to dump the stack
544 * content.
545 */
546 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
547 &frame0_pc_sane, at_fault);
548
549 /* The stack unwinder should at least unwind two levels of stack. If less
550 * level is seen we make sure at lease pc and lr are dumped.
551 */
552 if (stack_depth < 2) {
553 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
554 }
555
Hristo Bojinovc031a3b2010-08-27 14:00:57 -0700556 dump_randomization_base(tfd, at_fault);
Ben Cheng2854db82010-01-28 10:00:03 -0800557 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558
559 while(milist) {
560 mapinfo *next = milist->next;
Meng Huae7b91b2009-11-05 16:10:50 -0600561 symbol_table_free(milist->symbols);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 free(milist);
563 milist = next;
564 }
565}
566
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567#define MAX_TOMBSTONES 10
568
569#define typecheck(x,y) { \
570 typeof(x) __dummy1; \
571 typeof(y) __dummy2; \
572 (void)(&__dummy1 == &__dummy2); }
573
574#define TOMBSTONE_DIR "/data/tombstones"
575
576/*
577 * find_and_open_tombstone - find an available tombstone slot, if any, of the
578 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
579 * file is available, we reuse the least-recently-modified file.
580 */
581static int find_and_open_tombstone(void)
582{
583 unsigned long mtime = ULONG_MAX;
584 struct stat sb;
585 char path[128];
586 int fd, i, oldest = 0;
587
588 /*
589 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
590 * to, our logic breaks. This check will generate a warning if that happens.
591 */
592 typecheck(mtime, sb.st_mtime);
593
594 /*
595 * In a single wolf-like pass, find an available slot and, in case none
596 * exist, find and record the least-recently-modified file.
597 */
598 for (i = 0; i < MAX_TOMBSTONES; i++) {
599 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
600
601 if (!stat(path, &sb)) {
602 if (sb.st_mtime < mtime) {
603 oldest = i;
604 mtime = sb.st_mtime;
605 }
606 continue;
607 }
608 if (errno != ENOENT)
609 continue;
610
611 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
612 if (fd < 0)
613 continue; /* raced ? */
614
615 fchown(fd, AID_SYSTEM, AID_SYSTEM);
616 return fd;
617 }
618
619 /* we didn't find an available file, so we clobber the oldest one */
620 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
621 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
622 fchown(fd, AID_SYSTEM, AID_SYSTEM);
623
624 return fd;
625}
626
627/* Return true if some thread is not detached cleanly */
628static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
629{
630 char task_path[1024];
631
632 sprintf(task_path, "/proc/%d/task", pid);
633 DIR *d;
634 struct dirent *de;
635 int need_cleanup = 0;
636
637 d = opendir(task_path);
638 /* Bail early if cannot open the task directory */
639 if (d == NULL) {
640 XLOG("Cannot open /proc/%d/task\n", pid);
641 return false;
642 }
643 while ((de = readdir(d)) != NULL) {
644 unsigned new_tid;
645 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700646 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 continue;
648 new_tid = atoi(de->d_name);
649 /* The main thread at fault has been handled individually */
650 if (new_tid == tid)
651 continue;
652
653 /* Skip this thread if cannot ptrace it */
654 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
655 continue;
656
657 dump_crash_report(tfd, pid, new_tid, false);
658 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
659 }
660 closedir(d);
661 return need_cleanup != 0;
662}
663
664/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700665static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 int signal)
667{
668 int fd;
669 bool need_cleanup = false;
670
671 mkdir(TOMBSTONE_DIR, 0755);
672 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
673
674 fd = find_and_open_tombstone();
675 if (fd < 0)
676 return need_cleanup;
677
678 dump_crash_banner(fd, pid, tid, signal);
679 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700680 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 * If the user has requested to attach gdb, don't collect the per-thread
682 * information as it increases the chance to lose track of the process.
683 */
684 if ((signed)pid > debug_uid) {
685 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
686 }
687
688 close(fd);
689 return need_cleanup;
690}
691
692static int
693write_string(const char* file, const char* string)
694{
695 int len;
696 int fd;
697 ssize_t amt;
698 fd = open(file, O_RDWR);
699 len = strlen(string);
700 if (fd < 0)
701 return -errno;
702 amt = write(fd, string, len);
703 close(fd);
704 return amt >= 0 ? 0 : -errno;
705}
706
707static
708void init_debug_led(void)
709{
710 // trout leds
711 write_string("/sys/class/leds/red/brightness", "0");
712 write_string("/sys/class/leds/green/brightness", "0");
713 write_string("/sys/class/leds/blue/brightness", "0");
714 write_string("/sys/class/leds/red/device/blink", "0");
715 // sardine leds
716 write_string("/sys/class/leds/left/cadence", "0,0");
717}
718
719static
720void enable_debug_led(void)
721{
722 // trout leds
723 write_string("/sys/class/leds/red/brightness", "255");
724 // sardine leds
725 write_string("/sys/class/leds/left/cadence", "1,0");
726}
727
728static
729void disable_debug_led(void)
730{
731 // trout leds
732 write_string("/sys/class/leds/red/brightness", "0");
733 // sardine leds
734 write_string("/sys/class/leds/left/cadence", "0,0");
735}
736
737extern int init_getevent();
738extern void uninit_getevent();
739extern int get_event(struct input_event* event, int timeout);
740
741static void wait_for_user_action(unsigned tid, struct ucred* cr)
742{
743 (void)tid;
744 /* First log a helpful message */
745 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800746 "* Process %d has been suspended while crashing. To\n"
747 "* attach gdbserver for a gdb connection on port 5039:\n"
748 "*\n"
749 "* adb shell gdbserver :5039 --attach %d &\n"
750 "*\n"
751 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700752 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 cr->pid, cr->pid);
754
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800755 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 if (init_getevent() == 0) {
757 int ms = 1200 / 10;
758 int dit = 1;
759 int dah = 3*dit;
760 int _ = -dit;
761 int ___ = 3*_;
762 int _______ = 7*_;
763 const signed char codes[] = {
764 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
765 };
766 size_t s = 0;
767 struct input_event e;
768 int home = 0;
769 init_debug_led();
770 enable_debug_led();
771 do {
772 int timeout = abs((int)(codes[s])) * ms;
773 int res = get_event(&e, timeout);
774 if (res == 0) {
775 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
776 home = 1;
777 } else if (res == 1) {
778 if (++s >= sizeof(codes)/sizeof(*codes))
779 s = 0;
780 if (codes[s] > 0) {
781 enable_debug_led();
782 } else {
783 disable_debug_led();
784 }
785 }
Ben Cheng09e71372009-09-28 11:06:09 -0700786 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 uninit_getevent();
788 }
789
790 /* don't forget to turn debug led off */
791 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700792
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 /* close filedescriptor */
794 LOG("debuggerd resuming process %d", cr->pid);
795 }
796
797static void handle_crashing_process(int fd)
798{
799 char buf[64];
800 struct stat s;
801 unsigned tid;
802 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700803 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 int tid_attach_status = -1;
805 unsigned retry = 30;
806 bool need_cleanup = false;
807
808 char value[PROPERTY_VALUE_MAX];
809 property_get("debug.db.uid", value, "-1");
810 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700811
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700813
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 len = sizeof(cr);
815 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
816 if(n != 0) {
817 LOG("cannot get credentials\n");
818 goto done;
819 }
820
Ben Cheng09e71372009-09-28 11:06:09 -0700821 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 fcntl(fd, F_SETFL, O_NONBLOCK);
823 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
824 if(errno == EINTR) continue;
825 if(errno == EWOULDBLOCK) {
826 if(retry-- > 0) {
827 usleep(100 * 1000);
828 continue;
829 }
830 LOG("timed out reading tid\n");
831 goto done;
832 }
833 LOG("read failure? %s\n", strerror(errno));
834 goto done;
835 }
836
837 sprintf(buf,"/proc/%d/task/%d", cr.pid, tid);
838 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800839 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 tid, cr.pid);
841 close(fd);
842 return;
843 }
Ben Cheng09e71372009-09-28 11:06:09 -0700844
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800845 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
846
847 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
848 if(tid_attach_status < 0) {
849 LOG("ptrace attach failed: %s\n", strerror(errno));
850 goto done;
851 }
852
853 close(fd);
854 fd = -1;
855
856 for(;;) {
857 n = waitpid(tid, &status, __WALL);
Ben Cheng09e71372009-09-28 11:06:09 -0700858
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 if(n < 0) {
860 if(errno == EAGAIN) continue;
861 LOG("waitpid failed: %s\n", strerror(errno));
862 goto done;
863 }
864
865 XLOG("waitpid: n=%d status=%08x\n", n, status);
866
867 if(WIFSTOPPED(status)){
868 n = WSTOPSIG(status);
869 switch(n) {
870 case SIGSTOP:
871 XLOG("stopped -- continuing\n");
872 n = ptrace(PTRACE_CONT, tid, 0, 0);
873 if(n) {
874 LOG("ptrace failed: %s\n", strerror(errno));
875 goto done;
876 }
877 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700878
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879 case SIGILL:
880 case SIGABRT:
881 case SIGBUS:
882 case SIGFPE:
883 case SIGSEGV:
884 case SIGSTKFLT: {
885 XLOG("stopped -- fatal signal\n");
886 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
887 kill(tid, SIGSTOP);
888 goto done;
889 }
890
891 default:
892 XLOG("stopped -- unexpected signal\n");
893 goto done;
894 }
895 } else {
896 XLOG("unexpected waitpid response\n");
897 goto done;
898 }
899 }
Ben Cheng09e71372009-09-28 11:06:09 -0700900
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901done:
902 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700903
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 /* stop the process so we can debug */
905 kill(cr.pid, SIGSTOP);
906
Ben Cheng09e71372009-09-28 11:06:09 -0700907 /*
908 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700910 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 if (tid_attach_status == 0) {
912 int detach_status;
913 /* detach so we can attach gdbserver */
914 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
915 need_cleanup |= (detach_status != 0);
916 }
917
918 /*
919 * if debug.db.uid is set, its value indicates if we should wait
920 * for user action for the crashing process.
921 * in this case, we log a message and turn the debug LED on
922 * waiting for a gdb connection (for instance)
923 */
924
925 if ((signed)cr.uid <= debug_uid) {
926 wait_for_user_action(tid, &cr);
927 }
928
929 /* resume stopped process (so it can crash in peace) */
930 kill(cr.pid, SIGCONT);
931
932 if (need_cleanup) {
933 LOG("debuggerd committing suicide to free the zombie!\n");
934 kill(getpid(), SIGKILL);
935 }
936
937 if(fd != -1) close(fd);
938}
939
Ben Cheng09e71372009-09-28 11:06:09 -0700940int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800941{
942 int s;
943 struct sigaction act;
Ben Cheng09e71372009-09-28 11:06:09 -0700944
945 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
947 if(logsocket < 0) {
948 logsocket = -1;
949 } else {
950 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
951 }
952
953 act.sa_handler = SIG_DFL;
954 sigemptyset(&act.sa_mask);
955 sigaddset(&act.sa_mask,SIGCHLD);
956 act.sa_flags = SA_NOCLDWAIT;
957 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700958
959 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
961 if(s < 0) return -1;
962 fcntl(s, F_SETFD, FD_CLOEXEC);
963
964 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700965
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800966 for(;;) {
967 struct sockaddr addr;
968 socklen_t alen;
969 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 alen = sizeof(addr);
972 fd = accept(s, &addr, &alen);
973 if(fd < 0) continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700974
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800975 fcntl(fd, F_SETFD, FD_CLOEXEC);
976
977 handle_crashing_process(fd);
978 }
979 return 0;
980}