blob: 97a6b9e3667a6b53b46007d9eb493bc9a845603e [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <errno.h>
20#include <signal.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <dirent.h>
26
27#include <sys/ptrace.h>
28#include <sys/wait.h>
29#include <sys/exec_elf.h>
30#include <sys/stat.h>
31
32#include <cutils/sockets.h>
33#include <cutils/logd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include <cutils/properties.h>
35
36#include <linux/input.h>
37
38#include <private/android_filesystem_config.h>
39
Bruce Beare84924902010-10-13 14:21:30 -070040#include "debuggerd.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#include "utility.h"
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#define ANDROID_LOG_INFO 4
44
45/* Log information onto the tombstone */
46void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
47{
Meng Huae7b91b2009-11-05 16:10:50 -060048 char buf[512];
Ben Cheng09e71372009-09-28 11:06:09 -070049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 va_list ap;
51 va_start(ap, fmt);
52
53 if (tfd >= 0) {
54 int len;
55 vsnprintf(buf, sizeof(buf), fmt, ap);
56 len = strlen(buf);
57 if(tfd >= 0) write(tfd, buf, len);
58 }
59
60 if (!in_tombstone_only)
61 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
62}
63
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
65// 012345678901234567890123456789012345678901234567890123456789
66// 0 1 2 3 4 5
67
68mapinfo *parse_maps_line(char *line)
69{
70 mapinfo *mi;
71 int len = strlen(line);
72
Andy McFadden136dcc52011-09-22 16:37:06 -070073 if (len < 1) return 0; /* not expected */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 line[--len] = 0;
Ben Cheng09e71372009-09-28 11:06:09 -070075
Andy McFadden136dcc52011-09-22 16:37:06 -070076 if (len < 50) {
77 mi = malloc(sizeof(mapinfo) + 1);
78 } else {
79 mi = malloc(sizeof(mapinfo) + (len - 47));
80 }
81 if (mi == 0) return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Andy McFadden136dcc52011-09-22 16:37:06 -070083 mi->isExecutable = (line[20] == 'x');
Ben Cheng09e71372009-09-28 11:06:09 -070084
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 mi->start = strtoul(line, 0, 16);
86 mi->end = strtoul(line + 9, 0, 16);
Meng Huae7b91b2009-11-05 16:10:50 -060087 /* To be filled in parse_elf_info if the mapped section starts with
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088 * elf_header
89 */
90 mi->exidx_start = mi->exidx_end = 0;
Meng Huae7b91b2009-11-05 16:10:50 -060091 mi->symbols = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 mi->next = 0;
Andy McFadden136dcc52011-09-22 16:37:06 -070093 if (len < 50) {
94 mi->name[0] = '\0';
95 } else {
96 strcpy(mi->name, line + 49);
97 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
99 return mi;
100}
101
102void dump_build_info(int tfd)
103{
104 char fingerprint[PROPERTY_VALUE_MAX];
105
106 property_get("ro.build.fingerprint", fingerprint, "unknown");
107
108 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
109}
110
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111const char *get_signame(int sig)
112{
113 switch(sig) {
114 case SIGILL: return "SIGILL";
115 case SIGABRT: return "SIGABRT";
116 case SIGBUS: return "SIGBUS";
117 case SIGFPE: return "SIGFPE";
118 case SIGSEGV: return "SIGSEGV";
119 case SIGSTKFLT: return "SIGSTKFLT";
120 default: return "?";
121 }
122}
123
Carl Shapiro83c6b052010-10-08 18:10:24 -0700124const char *get_sigcode(int signo, int code)
125{
126 switch (signo) {
127 case SIGILL:
128 switch (code) {
129 case ILL_ILLOPC: return "ILL_ILLOPC";
130 case ILL_ILLOPN: return "ILL_ILLOPN";
131 case ILL_ILLADR: return "ILL_ILLADR";
132 case ILL_ILLTRP: return "ILL_ILLTRP";
133 case ILL_PRVOPC: return "ILL_PRVOPC";
134 case ILL_PRVREG: return "ILL_PRVREG";
135 case ILL_COPROC: return "ILL_COPROC";
136 case ILL_BADSTK: return "ILL_BADSTK";
137 }
138 break;
139 case SIGBUS:
140 switch (code) {
141 case BUS_ADRALN: return "BUS_ADRALN";
142 case BUS_ADRERR: return "BUS_ADRERR";
143 case BUS_OBJERR: return "BUS_OBJERR";
144 }
145 break;
146 case SIGFPE:
147 switch (code) {
148 case FPE_INTDIV: return "FPE_INTDIV";
149 case FPE_INTOVF: return "FPE_INTOVF";
150 case FPE_FLTDIV: return "FPE_FLTDIV";
151 case FPE_FLTOVF: return "FPE_FLTOVF";
152 case FPE_FLTUND: return "FPE_FLTUND";
153 case FPE_FLTRES: return "FPE_FLTRES";
154 case FPE_FLTINV: return "FPE_FLTINV";
155 case FPE_FLTSUB: return "FPE_FLTSUB";
156 }
157 break;
158 case SIGSEGV:
159 switch (code) {
160 case SEGV_MAPERR: return "SEGV_MAPERR";
161 case SEGV_ACCERR: return "SEGV_ACCERR";
162 }
163 break;
164 }
165 return "?";
166}
167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168void dump_fault_addr(int tfd, int pid, int sig)
169{
170 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 memset(&si, 0, sizeof(si));
173 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
174 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
Andy McFadden136dcc52011-09-22 16:37:06 -0700175 } else if (signal_has_address(sig)) {
Carl Shapiro83c6b052010-10-08 18:10:24 -0700176 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
177 sig, get_signame(sig),
178 si.si_code, get_sigcode(sig, si.si_code),
179 si.si_addr);
Andy McFadden136dcc52011-09-22 16:37:06 -0700180 } else {
181 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr --------\n",
182 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 }
184}
185
186void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
187{
188 char data[1024];
189 char *x = 0;
190 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700191
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 sprintf(data, "/proc/%d/cmdline", pid);
193 fp = fopen(data, "r");
194 if(fp) {
195 x = fgets(data, 1024, fp);
196 fclose(fp);
197 }
Ben Cheng09e71372009-09-28 11:06:09 -0700198
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 _LOG(tfd, false,
200 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
201 dump_build_info(tfd);
202 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
203 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 if(sig) dump_fault_addr(tfd, tid, sig);
206}
207
Meng Huae7b91b2009-11-05 16:10:50 -0600208static void parse_elf_info(mapinfo *milist, pid_t pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209{
210 mapinfo *mi;
211 for (mi = milist; mi != NULL; mi = mi->next) {
Andy McFadden136dcc52011-09-22 16:37:06 -0700212 if (!mi->isExecutable)
213 continue;
214
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 Elf32_Ehdr ehdr;
216
217 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700218 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 * mapped section.
220 */
Ben Cheng09e71372009-09-28 11:06:09 -0700221 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 sizeof(Elf32_Ehdr));
223 /* Check if it has the matching magic words */
224 if (IS_ELF(ehdr)) {
225 Elf32_Phdr phdr;
226 Elf32_Phdr *ptr;
227 int i;
228
229 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
230 for (i = 0; i < ehdr.e_phnum; i++) {
231 /* Parse the program header */
Mike Dodd6b657472010-07-14 11:28:29 -0700232 get_remote_struct(pid, (char *) (ptr+i), &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 sizeof(Elf32_Phdr));
Bruce Beare84924902010-10-13 14:21:30 -0700234#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 /* Found a EXIDX segment? */
236 if (phdr.p_type == PT_ARM_EXIDX) {
237 mi->exidx_start = mi->start + phdr.p_offset;
238 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
239 break;
240 }
Bruce Beare84924902010-10-13 14:21:30 -0700241#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Meng Huae7b91b2009-11-05 16:10:50 -0600243
244 /* Try to load symbols from this file */
245 mi->symbols = symbol_table_create(mi->name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 }
247 }
248}
249
250void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
251{
252 char data[1024];
253 FILE *fp;
254 mapinfo *milist = 0;
255 unsigned int sp_list[STACK_CONTENT_DEPTH];
256 int stack_depth;
Bruce Beare84924902010-10-13 14:21:30 -0700257#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 int frame0_pc_sane = 1;
Bruce Beare84924902010-10-13 14:21:30 -0700259#endif
Ben Cheng09e71372009-09-28 11:06:09 -0700260
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 if (!at_fault) {
262 _LOG(tfd, true,
263 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
264 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
265 }
266
267 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700268
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 /* Clear stack pointer records */
270 memset(sp_list, 0, sizeof(sp_list));
271
272 sprintf(data, "/proc/%d/maps", pid);
273 fp = fopen(data, "r");
274 if(fp) {
275 while(fgets(data, 1024, fp)) {
276 mapinfo *mi = parse_maps_line(data);
277 if(mi) {
278 mi->next = milist;
279 milist = mi;
280 }
281 }
282 fclose(fp);
283 }
284
Meng Huae7b91b2009-11-05 16:10:50 -0600285 parse_elf_info(milist, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
Bruce Beare84924902010-10-13 14:21:30 -0700287#if __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 /* If stack unwinder fails, use the default solution to dump the stack
289 * content.
290 */
291 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
292 &frame0_pc_sane, at_fault);
293
294 /* The stack unwinder should at least unwind two levels of stack. If less
295 * level is seen we make sure at lease pc and lr are dumped.
296 */
297 if (stack_depth < 2) {
298 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
299 }
300
Ben Cheng2854db82010-01-28 10:00:03 -0800301 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
Bruce Beare6cc49232010-10-13 16:11:15 -0700302#elif __i386__
303 /* If stack unwinder fails, use the default solution to dump the stack
304 * content.
305 */
306 stack_depth = unwind_backtrace_with_ptrace_x86(tfd, tid, milist,at_fault);
307#else
308#error "Unsupported architecture"
Bruce Beare84924902010-10-13 14:21:30 -0700309#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
311 while(milist) {
312 mapinfo *next = milist->next;
Meng Huae7b91b2009-11-05 16:10:50 -0600313 symbol_table_free(milist->symbols);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 free(milist);
315 milist = next;
316 }
317}
318
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319#define MAX_TOMBSTONES 10
320
321#define typecheck(x,y) { \
322 typeof(x) __dummy1; \
323 typeof(y) __dummy2; \
324 (void)(&__dummy1 == &__dummy2); }
325
326#define TOMBSTONE_DIR "/data/tombstones"
327
328/*
329 * find_and_open_tombstone - find an available tombstone slot, if any, of the
330 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
331 * file is available, we reuse the least-recently-modified file.
332 */
333static int find_and_open_tombstone(void)
334{
335 unsigned long mtime = ULONG_MAX;
336 struct stat sb;
337 char path[128];
338 int fd, i, oldest = 0;
339
340 /*
341 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
342 * to, our logic breaks. This check will generate a warning if that happens.
343 */
344 typecheck(mtime, sb.st_mtime);
345
346 /*
347 * In a single wolf-like pass, find an available slot and, in case none
348 * exist, find and record the least-recently-modified file.
349 */
350 for (i = 0; i < MAX_TOMBSTONES; i++) {
351 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
352
353 if (!stat(path, &sb)) {
354 if (sb.st_mtime < mtime) {
355 oldest = i;
356 mtime = sb.st_mtime;
357 }
358 continue;
359 }
360 if (errno != ENOENT)
361 continue;
362
363 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
364 if (fd < 0)
365 continue; /* raced ? */
366
367 fchown(fd, AID_SYSTEM, AID_SYSTEM);
368 return fd;
369 }
370
371 /* we didn't find an available file, so we clobber the oldest one */
372 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
373 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
374 fchown(fd, AID_SYSTEM, AID_SYSTEM);
375
376 return fd;
377}
378
379/* Return true if some thread is not detached cleanly */
380static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
381{
382 char task_path[1024];
383
384 sprintf(task_path, "/proc/%d/task", pid);
385 DIR *d;
386 struct dirent *de;
387 int need_cleanup = 0;
388
389 d = opendir(task_path);
390 /* Bail early if cannot open the task directory */
391 if (d == NULL) {
392 XLOG("Cannot open /proc/%d/task\n", pid);
393 return false;
394 }
395 while ((de = readdir(d)) != NULL) {
396 unsigned new_tid;
397 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700398 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 continue;
400 new_tid = atoi(de->d_name);
401 /* The main thread at fault has been handled individually */
402 if (new_tid == tid)
403 continue;
404
405 /* Skip this thread if cannot ptrace it */
406 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
407 continue;
408
409 dump_crash_report(tfd, pid, new_tid, false);
410 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
411 }
412 closedir(d);
413 return need_cleanup != 0;
414}
415
416/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700417static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 int signal)
419{
420 int fd;
421 bool need_cleanup = false;
422
423 mkdir(TOMBSTONE_DIR, 0755);
424 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
425
426 fd = find_and_open_tombstone();
427 if (fd < 0)
428 return need_cleanup;
429
430 dump_crash_banner(fd, pid, tid, signal);
431 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700432 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 * If the user has requested to attach gdb, don't collect the per-thread
434 * information as it increases the chance to lose track of the process.
435 */
436 if ((signed)pid > debug_uid) {
437 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
438 }
439
440 close(fd);
441 return need_cleanup;
442}
443
444static int
445write_string(const char* file, const char* string)
446{
447 int len;
448 int fd;
449 ssize_t amt;
450 fd = open(file, O_RDWR);
451 len = strlen(string);
452 if (fd < 0)
453 return -errno;
454 amt = write(fd, string, len);
455 close(fd);
456 return amt >= 0 ? 0 : -errno;
457}
458
459static
460void init_debug_led(void)
461{
462 // trout leds
463 write_string("/sys/class/leds/red/brightness", "0");
464 write_string("/sys/class/leds/green/brightness", "0");
465 write_string("/sys/class/leds/blue/brightness", "0");
466 write_string("/sys/class/leds/red/device/blink", "0");
467 // sardine leds
468 write_string("/sys/class/leds/left/cadence", "0,0");
469}
470
471static
472void enable_debug_led(void)
473{
474 // trout leds
475 write_string("/sys/class/leds/red/brightness", "255");
476 // sardine leds
477 write_string("/sys/class/leds/left/cadence", "1,0");
478}
479
480static
481void disable_debug_led(void)
482{
483 // trout leds
484 write_string("/sys/class/leds/red/brightness", "0");
485 // sardine leds
486 write_string("/sys/class/leds/left/cadence", "0,0");
487}
488
489extern int init_getevent();
490extern void uninit_getevent();
491extern int get_event(struct input_event* event, int timeout);
492
493static void wait_for_user_action(unsigned tid, struct ucred* cr)
494{
495 (void)tid;
496 /* First log a helpful message */
497 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800498 "* Process %d has been suspended while crashing. To\n"
499 "* attach gdbserver for a gdb connection on port 5039:\n"
500 "*\n"
501 "* adb shell gdbserver :5039 --attach %d &\n"
502 "*\n"
503 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700504 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 cr->pid, cr->pid);
506
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800507 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 if (init_getevent() == 0) {
509 int ms = 1200 / 10;
510 int dit = 1;
511 int dah = 3*dit;
512 int _ = -dit;
513 int ___ = 3*_;
514 int _______ = 7*_;
515 const signed char codes[] = {
516 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
517 };
518 size_t s = 0;
519 struct input_event e;
520 int home = 0;
521 init_debug_led();
522 enable_debug_led();
523 do {
524 int timeout = abs((int)(codes[s])) * ms;
525 int res = get_event(&e, timeout);
526 if (res == 0) {
527 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
528 home = 1;
529 } else if (res == 1) {
530 if (++s >= sizeof(codes)/sizeof(*codes))
531 s = 0;
532 if (codes[s] > 0) {
533 enable_debug_led();
534 } else {
535 disable_debug_led();
536 }
537 }
Ben Cheng09e71372009-09-28 11:06:09 -0700538 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 uninit_getevent();
540 }
541
542 /* don't forget to turn debug led off */
543 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700544
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 /* close filedescriptor */
546 LOG("debuggerd resuming process %d", cr->pid);
547 }
548
549static void handle_crashing_process(int fd)
550{
551 char buf[64];
552 struct stat s;
553 unsigned tid;
554 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700555 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 int tid_attach_status = -1;
557 unsigned retry = 30;
558 bool need_cleanup = false;
559
560 char value[PROPERTY_VALUE_MAX];
561 property_get("debug.db.uid", value, "-1");
562 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700563
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700565
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566 len = sizeof(cr);
567 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
568 if(n != 0) {
569 LOG("cannot get credentials\n");
570 goto done;
571 }
572
Ben Cheng09e71372009-09-28 11:06:09 -0700573 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 fcntl(fd, F_SETFL, O_NONBLOCK);
575 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
576 if(errno == EINTR) continue;
577 if(errno == EWOULDBLOCK) {
578 if(retry-- > 0) {
579 usleep(100 * 1000);
580 continue;
581 }
582 LOG("timed out reading tid\n");
583 goto done;
584 }
585 LOG("read failure? %s\n", strerror(errno));
586 goto done;
587 }
588
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100589 snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800591 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592 tid, cr.pid);
593 close(fd);
594 return;
595 }
Ben Cheng09e71372009-09-28 11:06:09 -0700596
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
598
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100599 /* Note that at this point, the target thread's signal handler
600 * is blocked in a read() call. This gives us the time to PTRACE_ATTACH
601 * to it before it has a chance to really fault.
602 *
603 * After the attach, the thread is stopped, and we write to the file
604 * descriptor to ensure that it will run as soon as we call PTRACE_CONT
605 * below. See details in bionic/libc/linker/debugger.c, in function
606 * debugger_signal_handler().
607 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
Andy McFadden655835b2011-07-26 07:50:37 -0700609 int ptrace_error = errno;
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100610
Andy McFadden655835b2011-07-26 07:50:37 -0700611 if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) {
612 XLOG("failed responding to client: %s\n",
613 strerror(errno));
614 goto done;
615 }
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 if(tid_attach_status < 0) {
Andy McFadden655835b2011-07-26 07:50:37 -0700618 LOG("ptrace attach failed: %s\n", strerror(ptrace_error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 goto done;
620 }
621
622 close(fd);
623 fd = -1;
624
Andy McFadden655835b2011-07-26 07:50:37 -0700625 const int sleep_time_usec = 200000; /* 0.2 seconds */
626 const int max_total_sleep_usec = 3000000; /* 3 seconds */
627 int loop_limit = max_total_sleep_usec / sleep_time_usec;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 for(;;) {
Andy McFadden655835b2011-07-26 07:50:37 -0700629 if (loop_limit-- == 0) {
630 LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n",
631 cr.pid, tid, cr.uid);
632 goto done;
633 }
634 n = waitpid(tid, &status, __WALL | WNOHANG);
635
636 if (n == 0) {
637 /* not ready yet */
638 XLOG("not ready yet\n");
639 usleep(sleep_time_usec);
640 continue;
641 }
Ben Cheng09e71372009-09-28 11:06:09 -0700642
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 if(n < 0) {
644 if(errno == EAGAIN) continue;
645 LOG("waitpid failed: %s\n", strerror(errno));
646 goto done;
647 }
648
649 XLOG("waitpid: n=%d status=%08x\n", n, status);
650
651 if(WIFSTOPPED(status)){
652 n = WSTOPSIG(status);
653 switch(n) {
654 case SIGSTOP:
655 XLOG("stopped -- continuing\n");
656 n = ptrace(PTRACE_CONT, tid, 0, 0);
657 if(n) {
658 LOG("ptrace failed: %s\n", strerror(errno));
659 goto done;
660 }
661 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700662
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663 case SIGILL:
664 case SIGABRT:
665 case SIGBUS:
666 case SIGFPE:
667 case SIGSEGV:
668 case SIGSTKFLT: {
669 XLOG("stopped -- fatal signal\n");
670 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
671 kill(tid, SIGSTOP);
672 goto done;
673 }
674
675 default:
676 XLOG("stopped -- unexpected signal\n");
677 goto done;
678 }
679 } else {
680 XLOG("unexpected waitpid response\n");
681 goto done;
682 }
683 }
Ben Cheng09e71372009-09-28 11:06:09 -0700684
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685done:
686 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700687
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 /* stop the process so we can debug */
689 kill(cr.pid, SIGSTOP);
690
Ben Cheng09e71372009-09-28 11:06:09 -0700691 /*
692 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700694 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 if (tid_attach_status == 0) {
696 int detach_status;
697 /* detach so we can attach gdbserver */
698 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
699 need_cleanup |= (detach_status != 0);
700 }
701
702 /*
703 * if debug.db.uid is set, its value indicates if we should wait
704 * for user action for the crashing process.
705 * in this case, we log a message and turn the debug LED on
706 * waiting for a gdb connection (for instance)
707 */
708
709 if ((signed)cr.uid <= debug_uid) {
710 wait_for_user_action(tid, &cr);
711 }
712
713 /* resume stopped process (so it can crash in peace) */
714 kill(cr.pid, SIGCONT);
715
716 if (need_cleanup) {
717 LOG("debuggerd committing suicide to free the zombie!\n");
718 kill(getpid(), SIGKILL);
719 }
720
721 if(fd != -1) close(fd);
722}
723
Bruce Beare84924902010-10-13 14:21:30 -0700724
Ben Cheng09e71372009-09-28 11:06:09 -0700725int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726{
727 int s;
728 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700729 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700730
Andy McFadden44e12ec2011-07-29 12:36:47 -0700731 /*
732 * debuggerd crashes can't be reported to debuggerd. Reset all of the
733 * crash handlers.
734 */
735 signal(SIGILL, SIG_DFL);
736 signal(SIGABRT, SIG_DFL);
737 signal(SIGBUS, SIG_DFL);
738 signal(SIGFPE, SIG_DFL);
739 signal(SIGSEGV, SIG_DFL);
740 signal(SIGSTKFLT, SIG_DFL);
741 signal(SIGPIPE, SIG_DFL);
742
Ben Cheng09e71372009-09-28 11:06:09 -0700743 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
745 if(logsocket < 0) {
746 logsocket = -1;
747 } else {
748 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
749 }
750
751 act.sa_handler = SIG_DFL;
752 sigemptyset(&act.sa_mask);
753 sigaddset(&act.sa_mask,SIGCHLD);
754 act.sa_flags = SA_NOCLDWAIT;
755 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700756
757 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
759 if(s < 0) return -1;
760 fcntl(s, F_SETFD, FD_CLOEXEC);
761
762 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700763
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764 for(;;) {
765 struct sockaddr addr;
766 socklen_t alen;
767 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700768
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700770 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700772 if(fd < 0) {
773 XLOG("accept failed: %s\n", strerror(errno));
774 continue;
775 }
Ben Cheng09e71372009-09-28 11:06:09 -0700776
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777 fcntl(fd, F_SETFD, FD_CLOEXEC);
778
779 handle_crashing_process(fd);
780 }
781 return 0;
782}