blob: 3757cd5f925327e5814f88983f8f8ef108190b00 [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
43#include "utility.h"
44
45/* Main entry point to get the backtrace from the crashing process */
46extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
47 unsigned int sp_list[],
48 int *frame0_pc_sane,
49 bool at_fault);
50
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051static int logsocket = -1;
52
53#define ANDROID_LOG_INFO 4
54
55/* Log information onto the tombstone */
56void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
57{
58 char buf[128];
Ben Cheng09e71372009-09-28 11:06:09 -070059
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060 va_list ap;
61 va_start(ap, fmt);
62
63 if (tfd >= 0) {
64 int len;
65 vsnprintf(buf, sizeof(buf), fmt, ap);
66 len = strlen(buf);
67 if(tfd >= 0) write(tfd, buf, len);
68 }
69
70 if (!in_tombstone_only)
71 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
72}
73
74#define LOG(fmt...) _LOG(-1, 0, fmt)
75#if 0
76#define XLOG(fmt...) _LOG(-1, 0, fmt)
77#else
78#define XLOG(fmt...) do {} while(0)
79#endif
80
81// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
82// 012345678901234567890123456789012345678901234567890123456789
83// 0 1 2 3 4 5
84
85mapinfo *parse_maps_line(char *line)
86{
87 mapinfo *mi;
88 int len = strlen(line);
89
90 if(len < 1) return 0;
91 line[--len] = 0;
Ben Cheng09e71372009-09-28 11:06:09 -070092
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 if(len < 50) return 0;
94 if(line[20] != 'x') return 0;
95
96 mi = malloc(sizeof(mapinfo) + (len - 47));
97 if(mi == 0) return 0;
Ben Cheng09e71372009-09-28 11:06:09 -070098
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 mi->start = strtoul(line, 0, 16);
100 mi->end = strtoul(line + 9, 0, 16);
Ben Cheng09e71372009-09-28 11:06:09 -0700101 /* To be filled in parse_exidx_info if the mapped section starts with
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 * elf_header
103 */
104 mi->exidx_start = mi->exidx_end = 0;
105 mi->next = 0;
106 strcpy(mi->name, line + 49);
107
108 return mi;
109}
110
111void dump_build_info(int tfd)
112{
113 char fingerprint[PROPERTY_VALUE_MAX];
114
115 property_get("ro.build.fingerprint", fingerprint, "unknown");
116
117 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
118}
119
120
Ben Cheng09e71372009-09-28 11:06:09 -0700121void dump_stack_and_code(int tfd, int pid, mapinfo *map,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 int unwind_depth, unsigned int sp_list[],
123 int frame0_pc_sane, bool at_fault)
124{
125 unsigned int sp, pc, p, end, data;
126 struct pt_regs r;
127 int sp_depth;
128 bool only_in_tombstone = !at_fault;
Ben Cheng09e71372009-09-28 11:06:09 -0700129 char code_buffer[80];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
131 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return;
132 sp = r.ARM_sp;
133 pc = r.ARM_pc;
134
135 /* Died because calling the weeds - dump
136 * the code around the PC in the next frame instead.
137 */
138 if (frame0_pc_sane == 0) {
139 pc = r.ARM_lr;
140 }
141
Ben Cheng09e71372009-09-28 11:06:09 -0700142 _LOG(tfd, only_in_tombstone,
143 "\ncode around %s:\n", frame0_pc_sane ? "pc" : "lr");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144
145 end = p = pc & ~3;
146 p -= 16;
Ben Cheng09e71372009-09-28 11:06:09 -0700147 end += 16;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Ben Cheng09e71372009-09-28 11:06:09 -0700149 /* Dump the code around PC as:
150 * addr contents
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
152 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
153 */
154 while (p <= end) {
155 int i;
156
Ben Cheng09e71372009-09-28 11:06:09 -0700157 sprintf(code_buffer, "%08x ", p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 for (i = 0; i < 4; i++) {
159 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700160 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 p += 4;
162 }
Ben Cheng09e71372009-09-28 11:06:09 -0700163 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
164 }
165
166 if (frame0_pc_sane) {
167 _LOG(tfd, only_in_tombstone, "\ncode around lr:\n");
168
169 end = p = r.ARM_lr & ~3;
170 p -= 16;
171 end += 16;
172
173 /* Dump the code around LR as:
174 * addr contents
175 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
176 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
177 */
178 while (p <= end) {
179 int i;
180
181 sprintf(code_buffer, "%08x ", p);
182 for (i = 0; i < 4; i++) {
183 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
184 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
185 p += 4;
186 }
187 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 }
190
191 p = sp - 64;
192 p &= ~3;
193 if (unwind_depth != 0) {
194 if (unwind_depth < STACK_CONTENT_DEPTH) {
195 end = sp_list[unwind_depth-1];
196 }
197 else {
198 end = sp_list[STACK_CONTENT_DEPTH-1];
199 }
200 }
201 else {
202 end = sp | 0x000000ff;
203 end += 0xff;
204 }
205
Ben Cheng09e71372009-09-28 11:06:09 -0700206 _LOG(tfd, only_in_tombstone, "\nstack:\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
208 /* If the crash is due to PC == 0, there will be two frames that
209 * have identical SP value.
210 */
211 if (sp_list[0] == sp_list[1]) {
212 sp_depth = 1;
213 }
214 else {
215 sp_depth = 0;
216 }
217
218 while (p <= end) {
Ben Cheng09e71372009-09-28 11:06:09 -0700219 char *prompt;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 char level[16];
221 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
222 if (p == sp_list[sp_depth]) {
223 sprintf(level, "#%02d", sp_depth++);
224 prompt = level;
225 }
226 else {
227 prompt = " ";
228 }
Ben Cheng09e71372009-09-28 11:06:09 -0700229
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 /* Print the stack content in the log for the first 3 frames. For the
231 * rest only print them in the tombstone file.
232 */
Ben Cheng09e71372009-09-28 11:06:09 -0700233 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
234 "%s %08x %08x %s\n", prompt, p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 map_to_name(map, data, ""));
236 p += 4;
237 }
238 /* print another 64-byte of stack data after the last frame */
239
240 end = p+64;
241 while (p <= end) {
242 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700243 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
244 " %08x %08x %s\n", p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 map_to_name(map, data, ""));
246 p += 4;
247 }
248}
249
Ben Cheng09e71372009-09-28 11:06:09 -0700250void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 bool at_fault)
252{
253 struct pt_regs r;
254
255 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
256 _LOG(tfd, !at_fault, "tid %d not responding!\n", pid);
257 return;
258 }
259
260 if (unwound_level == 0) {
261 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n", 0, r.ARM_pc,
262 map_to_name(map, r.ARM_pc, "<unknown>"));
263 }
264 _LOG(tfd, !at_fault, " #%02d lr %08x %s\n", 1, r.ARM_lr,
265 map_to_name(map, r.ARM_lr, "<unknown>"));
266}
267
Ben Cheng09e71372009-09-28 11:06:09 -0700268void dump_registers(int tfd, int pid, bool at_fault)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269{
270 struct pt_regs r;
271 bool only_in_tombstone = !at_fault;
272
273 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
Ben Cheng09e71372009-09-28 11:06:09 -0700274 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 "cannot get registers: %s\n", strerror(errno));
276 return;
277 }
Ben Cheng09e71372009-09-28 11:06:09 -0700278
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
280 r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3);
281 _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
282 r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7);
283 _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x 10 %08x fp %08x\n",
284 r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp);
Ben Cheng09e71372009-09-28 11:06:09 -0700285 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
Ben Cheng09e71372009-09-28 11:06:09 -0700287 r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr);
Ben Chengbdcff7d2009-12-17 12:50:58 -0800288
289#if __VFP_FP__
290 struct user_vfp vfp_regs;
291 int i;
292
293 if(ptrace(PTRACE_GETVFPREGS, pid, 0, &vfp_regs)) {
294 _LOG(tfd, only_in_tombstone,
295 "cannot get registers: %s\n", strerror(errno));
296 return;
297 }
298
299 for (i = 0; i < 32; i += 2) {
300 _LOG(tfd, only_in_tombstone,
301 " d%-2d %016llx d%-2d %016llx\n",
302 i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
303 }
304 _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr);
305#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306}
307
308const char *get_signame(int sig)
309{
310 switch(sig) {
311 case SIGILL: return "SIGILL";
312 case SIGABRT: return "SIGABRT";
313 case SIGBUS: return "SIGBUS";
314 case SIGFPE: return "SIGFPE";
315 case SIGSEGV: return "SIGSEGV";
316 case SIGSTKFLT: return "SIGSTKFLT";
317 default: return "?";
318 }
319}
320
321void dump_fault_addr(int tfd, int pid, int sig)
322{
323 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700324
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 memset(&si, 0, sizeof(si));
326 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
327 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
328 } else {
329 _LOG(tfd, false, "signal %d (%s), fault addr %08x\n",
330 sig, get_signame(sig), si.si_addr);
331 }
332}
333
334void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
335{
336 char data[1024];
337 char *x = 0;
338 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700339
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 sprintf(data, "/proc/%d/cmdline", pid);
341 fp = fopen(data, "r");
342 if(fp) {
343 x = fgets(data, 1024, fp);
344 fclose(fp);
345 }
Ben Cheng09e71372009-09-28 11:06:09 -0700346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 _LOG(tfd, false,
348 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
349 dump_build_info(tfd);
350 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
351 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700352
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 if(sig) dump_fault_addr(tfd, tid, sig);
354}
355
356static void parse_exidx_info(mapinfo *milist, pid_t pid)
357{
358 mapinfo *mi;
359 for (mi = milist; mi != NULL; mi = mi->next) {
360 Elf32_Ehdr ehdr;
361
362 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700363 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 * mapped section.
365 */
Ben Cheng09e71372009-09-28 11:06:09 -0700366 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 sizeof(Elf32_Ehdr));
368 /* Check if it has the matching magic words */
369 if (IS_ELF(ehdr)) {
370 Elf32_Phdr phdr;
371 Elf32_Phdr *ptr;
372 int i;
373
374 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
375 for (i = 0; i < ehdr.e_phnum; i++) {
376 /* Parse the program header */
Ben Cheng09e71372009-09-28 11:06:09 -0700377 get_remote_struct(pid, (char *) ptr+i, &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 sizeof(Elf32_Phdr));
379 /* Found a EXIDX segment? */
380 if (phdr.p_type == PT_ARM_EXIDX) {
381 mi->exidx_start = mi->start + phdr.p_offset;
382 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
383 break;
384 }
385 }
386 }
387 }
388}
389
390void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
391{
392 char data[1024];
393 FILE *fp;
394 mapinfo *milist = 0;
395 unsigned int sp_list[STACK_CONTENT_DEPTH];
396 int stack_depth;
397 int frame0_pc_sane = 1;
Ben Cheng09e71372009-09-28 11:06:09 -0700398
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 if (!at_fault) {
400 _LOG(tfd, true,
401 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
402 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
403 }
404
405 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 /* Clear stack pointer records */
408 memset(sp_list, 0, sizeof(sp_list));
409
410 sprintf(data, "/proc/%d/maps", pid);
411 fp = fopen(data, "r");
412 if(fp) {
413 while(fgets(data, 1024, fp)) {
414 mapinfo *mi = parse_maps_line(data);
415 if(mi) {
416 mi->next = milist;
417 milist = mi;
418 }
419 }
420 fclose(fp);
421 }
422
423 parse_exidx_info(milist, tid);
424
425 /* If stack unwinder fails, use the default solution to dump the stack
426 * content.
427 */
428 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
429 &frame0_pc_sane, at_fault);
430
431 /* The stack unwinder should at least unwind two levels of stack. If less
432 * level is seen we make sure at lease pc and lr are dumped.
433 */
434 if (stack_depth < 2) {
435 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
436 }
437
438 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, frame0_pc_sane,
439 at_fault);
440
441 while(milist) {
442 mapinfo *next = milist->next;
443 free(milist);
444 milist = next;
445 }
446}
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448#define MAX_TOMBSTONES 10
449
450#define typecheck(x,y) { \
451 typeof(x) __dummy1; \
452 typeof(y) __dummy2; \
453 (void)(&__dummy1 == &__dummy2); }
454
455#define TOMBSTONE_DIR "/data/tombstones"
456
457/*
458 * find_and_open_tombstone - find an available tombstone slot, if any, of the
459 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
460 * file is available, we reuse the least-recently-modified file.
461 */
462static int find_and_open_tombstone(void)
463{
464 unsigned long mtime = ULONG_MAX;
465 struct stat sb;
466 char path[128];
467 int fd, i, oldest = 0;
468
469 /*
470 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
471 * to, our logic breaks. This check will generate a warning if that happens.
472 */
473 typecheck(mtime, sb.st_mtime);
474
475 /*
476 * In a single wolf-like pass, find an available slot and, in case none
477 * exist, find and record the least-recently-modified file.
478 */
479 for (i = 0; i < MAX_TOMBSTONES; i++) {
480 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
481
482 if (!stat(path, &sb)) {
483 if (sb.st_mtime < mtime) {
484 oldest = i;
485 mtime = sb.st_mtime;
486 }
487 continue;
488 }
489 if (errno != ENOENT)
490 continue;
491
492 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
493 if (fd < 0)
494 continue; /* raced ? */
495
496 fchown(fd, AID_SYSTEM, AID_SYSTEM);
497 return fd;
498 }
499
500 /* we didn't find an available file, so we clobber the oldest one */
501 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
502 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
503 fchown(fd, AID_SYSTEM, AID_SYSTEM);
504
505 return fd;
506}
507
508/* Return true if some thread is not detached cleanly */
509static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
510{
511 char task_path[1024];
512
513 sprintf(task_path, "/proc/%d/task", pid);
514 DIR *d;
515 struct dirent *de;
516 int need_cleanup = 0;
517
518 d = opendir(task_path);
519 /* Bail early if cannot open the task directory */
520 if (d == NULL) {
521 XLOG("Cannot open /proc/%d/task\n", pid);
522 return false;
523 }
524 while ((de = readdir(d)) != NULL) {
525 unsigned new_tid;
526 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700527 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 continue;
529 new_tid = atoi(de->d_name);
530 /* The main thread at fault has been handled individually */
531 if (new_tid == tid)
532 continue;
533
534 /* Skip this thread if cannot ptrace it */
535 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
536 continue;
537
538 dump_crash_report(tfd, pid, new_tid, false);
539 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
540 }
541 closedir(d);
542 return need_cleanup != 0;
543}
544
545/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700546static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 int signal)
548{
549 int fd;
550 bool need_cleanup = false;
551
552 mkdir(TOMBSTONE_DIR, 0755);
553 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
554
555 fd = find_and_open_tombstone();
556 if (fd < 0)
557 return need_cleanup;
558
559 dump_crash_banner(fd, pid, tid, signal);
560 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700561 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 * If the user has requested to attach gdb, don't collect the per-thread
563 * information as it increases the chance to lose track of the process.
564 */
565 if ((signed)pid > debug_uid) {
566 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
567 }
568
569 close(fd);
570 return need_cleanup;
571}
572
573static int
574write_string(const char* file, const char* string)
575{
576 int len;
577 int fd;
578 ssize_t amt;
579 fd = open(file, O_RDWR);
580 len = strlen(string);
581 if (fd < 0)
582 return -errno;
583 amt = write(fd, string, len);
584 close(fd);
585 return amt >= 0 ? 0 : -errno;
586}
587
588static
589void init_debug_led(void)
590{
591 // trout leds
592 write_string("/sys/class/leds/red/brightness", "0");
593 write_string("/sys/class/leds/green/brightness", "0");
594 write_string("/sys/class/leds/blue/brightness", "0");
595 write_string("/sys/class/leds/red/device/blink", "0");
596 // sardine leds
597 write_string("/sys/class/leds/left/cadence", "0,0");
598}
599
600static
601void enable_debug_led(void)
602{
603 // trout leds
604 write_string("/sys/class/leds/red/brightness", "255");
605 // sardine leds
606 write_string("/sys/class/leds/left/cadence", "1,0");
607}
608
609static
610void disable_debug_led(void)
611{
612 // trout leds
613 write_string("/sys/class/leds/red/brightness", "0");
614 // sardine leds
615 write_string("/sys/class/leds/left/cadence", "0,0");
616}
617
618extern int init_getevent();
619extern void uninit_getevent();
620extern int get_event(struct input_event* event, int timeout);
621
622static void wait_for_user_action(unsigned tid, struct ucred* cr)
623{
624 (void)tid;
625 /* First log a helpful message */
626 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800627 "* Process %d has been suspended while crashing. To\n"
628 "* attach gdbserver for a gdb connection on port 5039:\n"
629 "*\n"
630 "* adb shell gdbserver :5039 --attach %d &\n"
631 "*\n"
632 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700633 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 cr->pid, cr->pid);
635
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800636 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 if (init_getevent() == 0) {
638 int ms = 1200 / 10;
639 int dit = 1;
640 int dah = 3*dit;
641 int _ = -dit;
642 int ___ = 3*_;
643 int _______ = 7*_;
644 const signed char codes[] = {
645 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
646 };
647 size_t s = 0;
648 struct input_event e;
649 int home = 0;
650 init_debug_led();
651 enable_debug_led();
652 do {
653 int timeout = abs((int)(codes[s])) * ms;
654 int res = get_event(&e, timeout);
655 if (res == 0) {
656 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
657 home = 1;
658 } else if (res == 1) {
659 if (++s >= sizeof(codes)/sizeof(*codes))
660 s = 0;
661 if (codes[s] > 0) {
662 enable_debug_led();
663 } else {
664 disable_debug_led();
665 }
666 }
Ben Cheng09e71372009-09-28 11:06:09 -0700667 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 uninit_getevent();
669 }
670
671 /* don't forget to turn debug led off */
672 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700673
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 /* close filedescriptor */
675 LOG("debuggerd resuming process %d", cr->pid);
676 }
677
678static void handle_crashing_process(int fd)
679{
680 char buf[64];
681 struct stat s;
682 unsigned tid;
683 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700684 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 int tid_attach_status = -1;
686 unsigned retry = 30;
687 bool need_cleanup = false;
688
689 char value[PROPERTY_VALUE_MAX];
690 property_get("debug.db.uid", value, "-1");
691 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700692
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700694
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 len = sizeof(cr);
696 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
697 if(n != 0) {
698 LOG("cannot get credentials\n");
699 goto done;
700 }
701
Ben Cheng09e71372009-09-28 11:06:09 -0700702 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 fcntl(fd, F_SETFL, O_NONBLOCK);
704 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
705 if(errno == EINTR) continue;
706 if(errno == EWOULDBLOCK) {
707 if(retry-- > 0) {
708 usleep(100 * 1000);
709 continue;
710 }
711 LOG("timed out reading tid\n");
712 goto done;
713 }
714 LOG("read failure? %s\n", strerror(errno));
715 goto done;
716 }
717
718 sprintf(buf,"/proc/%d/task/%d", cr.pid, tid);
719 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800720 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 tid, cr.pid);
722 close(fd);
723 return;
724 }
Ben Cheng09e71372009-09-28 11:06:09 -0700725
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
727
728 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
729 if(tid_attach_status < 0) {
730 LOG("ptrace attach failed: %s\n", strerror(errno));
731 goto done;
732 }
733
734 close(fd);
735 fd = -1;
736
737 for(;;) {
738 n = waitpid(tid, &status, __WALL);
Ben Cheng09e71372009-09-28 11:06:09 -0700739
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 if(n < 0) {
741 if(errno == EAGAIN) continue;
742 LOG("waitpid failed: %s\n", strerror(errno));
743 goto done;
744 }
745
746 XLOG("waitpid: n=%d status=%08x\n", n, status);
747
748 if(WIFSTOPPED(status)){
749 n = WSTOPSIG(status);
750 switch(n) {
751 case SIGSTOP:
752 XLOG("stopped -- continuing\n");
753 n = ptrace(PTRACE_CONT, tid, 0, 0);
754 if(n) {
755 LOG("ptrace failed: %s\n", strerror(errno));
756 goto done;
757 }
758 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700759
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 case SIGILL:
761 case SIGABRT:
762 case SIGBUS:
763 case SIGFPE:
764 case SIGSEGV:
765 case SIGSTKFLT: {
766 XLOG("stopped -- fatal signal\n");
767 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
768 kill(tid, SIGSTOP);
769 goto done;
770 }
771
772 default:
773 XLOG("stopped -- unexpected signal\n");
774 goto done;
775 }
776 } else {
777 XLOG("unexpected waitpid response\n");
778 goto done;
779 }
780 }
Ben Cheng09e71372009-09-28 11:06:09 -0700781
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782done:
783 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700784
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 /* stop the process so we can debug */
786 kill(cr.pid, SIGSTOP);
787
Ben Cheng09e71372009-09-28 11:06:09 -0700788 /*
789 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700791 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 if (tid_attach_status == 0) {
793 int detach_status;
794 /* detach so we can attach gdbserver */
795 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
796 need_cleanup |= (detach_status != 0);
797 }
798
799 /*
800 * if debug.db.uid is set, its value indicates if we should wait
801 * for user action for the crashing process.
802 * in this case, we log a message and turn the debug LED on
803 * waiting for a gdb connection (for instance)
804 */
805
806 if ((signed)cr.uid <= debug_uid) {
807 wait_for_user_action(tid, &cr);
808 }
809
810 /* resume stopped process (so it can crash in peace) */
811 kill(cr.pid, SIGCONT);
812
813 if (need_cleanup) {
814 LOG("debuggerd committing suicide to free the zombie!\n");
815 kill(getpid(), SIGKILL);
816 }
817
818 if(fd != -1) close(fd);
819}
820
Ben Cheng09e71372009-09-28 11:06:09 -0700821int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822{
823 int s;
824 struct sigaction act;
Ben Cheng09e71372009-09-28 11:06:09 -0700825
826 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
828 if(logsocket < 0) {
829 logsocket = -1;
830 } else {
831 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
832 }
833
834 act.sa_handler = SIG_DFL;
835 sigemptyset(&act.sa_mask);
836 sigaddset(&act.sa_mask,SIGCHLD);
837 act.sa_flags = SA_NOCLDWAIT;
838 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700839
840 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
842 if(s < 0) return -1;
843 fcntl(s, F_SETFD, FD_CLOEXEC);
844
845 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700846
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800847 for(;;) {
848 struct sockaddr addr;
849 socklen_t alen;
850 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700851
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 alen = sizeof(addr);
853 fd = accept(s, &addr, &alen);
854 if(fd < 0) continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700855
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 fcntl(fd, F_SETFD, FD_CLOEXEC);
857
858 handle_crashing_process(fd);
859 }
860 return 0;
861}