blob: 892bc99697fe41dd7fdc720945a0d35a25857713 [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
73 if(len < 1) return 0;
74 line[--len] = 0;
Ben Cheng09e71372009-09-28 11:06:09 -070075
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 if(len < 50) return 0;
77 if(line[20] != 'x') return 0;
78
79 mi = malloc(sizeof(mapinfo) + (len - 47));
80 if(mi == 0) return 0;
Ben Cheng09e71372009-09-28 11:06:09 -070081
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 mi->start = strtoul(line, 0, 16);
83 mi->end = strtoul(line + 9, 0, 16);
Meng Huae7b91b2009-11-05 16:10:50 -060084 /* To be filled in parse_elf_info if the mapped section starts with
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 * elf_header
86 */
87 mi->exidx_start = mi->exidx_end = 0;
Meng Huae7b91b2009-11-05 16:10:50 -060088 mi->symbols = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 mi->next = 0;
90 strcpy(mi->name, line + 49);
91
92 return mi;
93}
94
95void dump_build_info(int tfd)
96{
97 char fingerprint[PROPERTY_VALUE_MAX];
98
99 property_get("ro.build.fingerprint", fingerprint, "unknown");
100
101 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
102}
103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104const char *get_signame(int sig)
105{
106 switch(sig) {
107 case SIGILL: return "SIGILL";
108 case SIGABRT: return "SIGABRT";
109 case SIGBUS: return "SIGBUS";
110 case SIGFPE: return "SIGFPE";
111 case SIGSEGV: return "SIGSEGV";
112 case SIGSTKFLT: return "SIGSTKFLT";
113 default: return "?";
114 }
115}
116
Carl Shapiro83c6b052010-10-08 18:10:24 -0700117const char *get_sigcode(int signo, int code)
118{
119 switch (signo) {
120 case SIGILL:
121 switch (code) {
122 case ILL_ILLOPC: return "ILL_ILLOPC";
123 case ILL_ILLOPN: return "ILL_ILLOPN";
124 case ILL_ILLADR: return "ILL_ILLADR";
125 case ILL_ILLTRP: return "ILL_ILLTRP";
126 case ILL_PRVOPC: return "ILL_PRVOPC";
127 case ILL_PRVREG: return "ILL_PRVREG";
128 case ILL_COPROC: return "ILL_COPROC";
129 case ILL_BADSTK: return "ILL_BADSTK";
130 }
131 break;
132 case SIGBUS:
133 switch (code) {
134 case BUS_ADRALN: return "BUS_ADRALN";
135 case BUS_ADRERR: return "BUS_ADRERR";
136 case BUS_OBJERR: return "BUS_OBJERR";
137 }
138 break;
139 case SIGFPE:
140 switch (code) {
141 case FPE_INTDIV: return "FPE_INTDIV";
142 case FPE_INTOVF: return "FPE_INTOVF";
143 case FPE_FLTDIV: return "FPE_FLTDIV";
144 case FPE_FLTOVF: return "FPE_FLTOVF";
145 case FPE_FLTUND: return "FPE_FLTUND";
146 case FPE_FLTRES: return "FPE_FLTRES";
147 case FPE_FLTINV: return "FPE_FLTINV";
148 case FPE_FLTSUB: return "FPE_FLTSUB";
149 }
150 break;
151 case SIGSEGV:
152 switch (code) {
153 case SEGV_MAPERR: return "SEGV_MAPERR";
154 case SEGV_ACCERR: return "SEGV_ACCERR";
155 }
156 break;
157 }
158 return "?";
159}
160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161void dump_fault_addr(int tfd, int pid, int sig)
162{
163 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700164
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 memset(&si, 0, sizeof(si));
166 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
167 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
168 } else {
Carl Shapiro83c6b052010-10-08 18:10:24 -0700169 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
170 sig, get_signame(sig),
171 si.si_code, get_sigcode(sig, si.si_code),
172 si.si_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 }
174}
175
176void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
177{
178 char data[1024];
179 char *x = 0;
180 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 sprintf(data, "/proc/%d/cmdline", pid);
183 fp = fopen(data, "r");
184 if(fp) {
185 x = fgets(data, 1024, fp);
186 fclose(fp);
187 }
Ben Cheng09e71372009-09-28 11:06:09 -0700188
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 _LOG(tfd, false,
190 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
191 dump_build_info(tfd);
192 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
193 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 if(sig) dump_fault_addr(tfd, tid, sig);
196}
197
Meng Huae7b91b2009-11-05 16:10:50 -0600198static void parse_elf_info(mapinfo *milist, pid_t pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199{
200 mapinfo *mi;
201 for (mi = milist; mi != NULL; mi = mi->next) {
202 Elf32_Ehdr ehdr;
203
204 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700205 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 * mapped section.
207 */
Ben Cheng09e71372009-09-28 11:06:09 -0700208 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 sizeof(Elf32_Ehdr));
210 /* Check if it has the matching magic words */
211 if (IS_ELF(ehdr)) {
212 Elf32_Phdr phdr;
213 Elf32_Phdr *ptr;
214 int i;
215
216 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
217 for (i = 0; i < ehdr.e_phnum; i++) {
218 /* Parse the program header */
Mike Dodd6b657472010-07-14 11:28:29 -0700219 get_remote_struct(pid, (char *) (ptr+i), &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 sizeof(Elf32_Phdr));
Bruce Beare84924902010-10-13 14:21:30 -0700221#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 /* Found a EXIDX segment? */
223 if (phdr.p_type == PT_ARM_EXIDX) {
224 mi->exidx_start = mi->start + phdr.p_offset;
225 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
226 break;
227 }
Bruce Beare84924902010-10-13 14:21:30 -0700228#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 }
Meng Huae7b91b2009-11-05 16:10:50 -0600230
231 /* Try to load symbols from this file */
232 mi->symbols = symbol_table_create(mi->name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234 }
235}
236
237void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
238{
239 char data[1024];
240 FILE *fp;
241 mapinfo *milist = 0;
242 unsigned int sp_list[STACK_CONTENT_DEPTH];
243 int stack_depth;
Bruce Beare84924902010-10-13 14:21:30 -0700244#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 int frame0_pc_sane = 1;
Bruce Beare84924902010-10-13 14:21:30 -0700246#endif
Ben Cheng09e71372009-09-28 11:06:09 -0700247
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 if (!at_fault) {
249 _LOG(tfd, true,
250 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
251 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
252 }
253
254 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700255
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 /* Clear stack pointer records */
257 memset(sp_list, 0, sizeof(sp_list));
258
259 sprintf(data, "/proc/%d/maps", pid);
260 fp = fopen(data, "r");
261 if(fp) {
262 while(fgets(data, 1024, fp)) {
263 mapinfo *mi = parse_maps_line(data);
264 if(mi) {
265 mi->next = milist;
266 milist = mi;
267 }
268 }
269 fclose(fp);
270 }
271
Meng Huae7b91b2009-11-05 16:10:50 -0600272 parse_elf_info(milist, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
Bruce Beare84924902010-10-13 14:21:30 -0700274#if __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 /* If stack unwinder fails, use the default solution to dump the stack
276 * content.
277 */
278 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
279 &frame0_pc_sane, at_fault);
280
281 /* The stack unwinder should at least unwind two levels of stack. If less
282 * level is seen we make sure at lease pc and lr are dumped.
283 */
284 if (stack_depth < 2) {
285 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
286 }
287
Ben Cheng2854db82010-01-28 10:00:03 -0800288 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
Bruce Beare6cc49232010-10-13 16:11:15 -0700289#elif __i386__
290 /* If stack unwinder fails, use the default solution to dump the stack
291 * content.
292 */
293 stack_depth = unwind_backtrace_with_ptrace_x86(tfd, tid, milist,at_fault);
294#else
295#error "Unsupported architecture"
Bruce Beare84924902010-10-13 14:21:30 -0700296#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297
298 while(milist) {
299 mapinfo *next = milist->next;
Meng Huae7b91b2009-11-05 16:10:50 -0600300 symbol_table_free(milist->symbols);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 free(milist);
302 milist = next;
303 }
304}
305
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306#define MAX_TOMBSTONES 10
307
308#define typecheck(x,y) { \
309 typeof(x) __dummy1; \
310 typeof(y) __dummy2; \
311 (void)(&__dummy1 == &__dummy2); }
312
313#define TOMBSTONE_DIR "/data/tombstones"
314
315/*
316 * find_and_open_tombstone - find an available tombstone slot, if any, of the
317 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
318 * file is available, we reuse the least-recently-modified file.
319 */
320static int find_and_open_tombstone(void)
321{
322 unsigned long mtime = ULONG_MAX;
323 struct stat sb;
324 char path[128];
325 int fd, i, oldest = 0;
326
327 /*
328 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
329 * to, our logic breaks. This check will generate a warning if that happens.
330 */
331 typecheck(mtime, sb.st_mtime);
332
333 /*
334 * In a single wolf-like pass, find an available slot and, in case none
335 * exist, find and record the least-recently-modified file.
336 */
337 for (i = 0; i < MAX_TOMBSTONES; i++) {
338 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
339
340 if (!stat(path, &sb)) {
341 if (sb.st_mtime < mtime) {
342 oldest = i;
343 mtime = sb.st_mtime;
344 }
345 continue;
346 }
347 if (errno != ENOENT)
348 continue;
349
350 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
351 if (fd < 0)
352 continue; /* raced ? */
353
354 fchown(fd, AID_SYSTEM, AID_SYSTEM);
355 return fd;
356 }
357
358 /* we didn't find an available file, so we clobber the oldest one */
359 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
360 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
361 fchown(fd, AID_SYSTEM, AID_SYSTEM);
362
363 return fd;
364}
365
366/* Return true if some thread is not detached cleanly */
367static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
368{
369 char task_path[1024];
370
371 sprintf(task_path, "/proc/%d/task", pid);
372 DIR *d;
373 struct dirent *de;
374 int need_cleanup = 0;
375
376 d = opendir(task_path);
377 /* Bail early if cannot open the task directory */
378 if (d == NULL) {
379 XLOG("Cannot open /proc/%d/task\n", pid);
380 return false;
381 }
382 while ((de = readdir(d)) != NULL) {
383 unsigned new_tid;
384 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700385 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 continue;
387 new_tid = atoi(de->d_name);
388 /* The main thread at fault has been handled individually */
389 if (new_tid == tid)
390 continue;
391
392 /* Skip this thread if cannot ptrace it */
393 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
394 continue;
395
396 dump_crash_report(tfd, pid, new_tid, false);
397 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
398 }
399 closedir(d);
400 return need_cleanup != 0;
401}
402
403/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700404static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 int signal)
406{
407 int fd;
408 bool need_cleanup = false;
409
410 mkdir(TOMBSTONE_DIR, 0755);
411 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
412
413 fd = find_and_open_tombstone();
414 if (fd < 0)
415 return need_cleanup;
416
417 dump_crash_banner(fd, pid, tid, signal);
418 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700419 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 * If the user has requested to attach gdb, don't collect the per-thread
421 * information as it increases the chance to lose track of the process.
422 */
423 if ((signed)pid > debug_uid) {
424 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
425 }
426
427 close(fd);
428 return need_cleanup;
429}
430
431static int
432write_string(const char* file, const char* string)
433{
434 int len;
435 int fd;
436 ssize_t amt;
437 fd = open(file, O_RDWR);
438 len = strlen(string);
439 if (fd < 0)
440 return -errno;
441 amt = write(fd, string, len);
442 close(fd);
443 return amt >= 0 ? 0 : -errno;
444}
445
446static
447void init_debug_led(void)
448{
449 // trout leds
450 write_string("/sys/class/leds/red/brightness", "0");
451 write_string("/sys/class/leds/green/brightness", "0");
452 write_string("/sys/class/leds/blue/brightness", "0");
453 write_string("/sys/class/leds/red/device/blink", "0");
454 // sardine leds
455 write_string("/sys/class/leds/left/cadence", "0,0");
456}
457
458static
459void enable_debug_led(void)
460{
461 // trout leds
462 write_string("/sys/class/leds/red/brightness", "255");
463 // sardine leds
464 write_string("/sys/class/leds/left/cadence", "1,0");
465}
466
467static
468void disable_debug_led(void)
469{
470 // trout leds
471 write_string("/sys/class/leds/red/brightness", "0");
472 // sardine leds
473 write_string("/sys/class/leds/left/cadence", "0,0");
474}
475
476extern int init_getevent();
477extern void uninit_getevent();
478extern int get_event(struct input_event* event, int timeout);
479
480static void wait_for_user_action(unsigned tid, struct ucred* cr)
481{
482 (void)tid;
483 /* First log a helpful message */
484 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800485 "* Process %d has been suspended while crashing. To\n"
486 "* attach gdbserver for a gdb connection on port 5039:\n"
487 "*\n"
488 "* adb shell gdbserver :5039 --attach %d &\n"
489 "*\n"
490 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700491 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 cr->pid, cr->pid);
493
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800494 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 if (init_getevent() == 0) {
496 int ms = 1200 / 10;
497 int dit = 1;
498 int dah = 3*dit;
499 int _ = -dit;
500 int ___ = 3*_;
501 int _______ = 7*_;
502 const signed char codes[] = {
503 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
504 };
505 size_t s = 0;
506 struct input_event e;
507 int home = 0;
508 init_debug_led();
509 enable_debug_led();
510 do {
511 int timeout = abs((int)(codes[s])) * ms;
512 int res = get_event(&e, timeout);
513 if (res == 0) {
514 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
515 home = 1;
516 } else if (res == 1) {
517 if (++s >= sizeof(codes)/sizeof(*codes))
518 s = 0;
519 if (codes[s] > 0) {
520 enable_debug_led();
521 } else {
522 disable_debug_led();
523 }
524 }
Ben Cheng09e71372009-09-28 11:06:09 -0700525 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 uninit_getevent();
527 }
528
529 /* don't forget to turn debug led off */
530 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700531
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 /* close filedescriptor */
533 LOG("debuggerd resuming process %d", cr->pid);
534 }
535
536static void handle_crashing_process(int fd)
537{
538 char buf[64];
539 struct stat s;
540 unsigned tid;
541 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700542 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 int tid_attach_status = -1;
544 unsigned retry = 30;
545 bool need_cleanup = false;
546
547 char value[PROPERTY_VALUE_MAX];
548 property_get("debug.db.uid", value, "-1");
549 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700550
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700552
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 len = sizeof(cr);
554 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
555 if(n != 0) {
556 LOG("cannot get credentials\n");
557 goto done;
558 }
559
Ben Cheng09e71372009-09-28 11:06:09 -0700560 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 fcntl(fd, F_SETFL, O_NONBLOCK);
562 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
563 if(errno == EINTR) continue;
564 if(errno == EWOULDBLOCK) {
565 if(retry-- > 0) {
566 usleep(100 * 1000);
567 continue;
568 }
569 LOG("timed out reading tid\n");
570 goto done;
571 }
572 LOG("read failure? %s\n", strerror(errno));
573 goto done;
574 }
575
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100576 snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800578 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 tid, cr.pid);
580 close(fd);
581 return;
582 }
Ben Cheng09e71372009-09-28 11:06:09 -0700583
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
585
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100586 /* Note that at this point, the target thread's signal handler
587 * is blocked in a read() call. This gives us the time to PTRACE_ATTACH
588 * to it before it has a chance to really fault.
589 *
590 * After the attach, the thread is stopped, and we write to the file
591 * descriptor to ensure that it will run as soon as we call PTRACE_CONT
592 * below. See details in bionic/libc/linker/debugger.c, in function
593 * debugger_signal_handler().
594 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100596
597 TEMP_FAILURE_RETRY(write(fd, &tid, 1));
598
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 if(tid_attach_status < 0) {
600 LOG("ptrace attach failed: %s\n", strerror(errno));
601 goto done;
602 }
603
604 close(fd);
605 fd = -1;
606
607 for(;;) {
608 n = waitpid(tid, &status, __WALL);
Ben Cheng09e71372009-09-28 11:06:09 -0700609
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 if(n < 0) {
611 if(errno == EAGAIN) continue;
612 LOG("waitpid failed: %s\n", strerror(errno));
613 goto done;
614 }
615
616 XLOG("waitpid: n=%d status=%08x\n", n, status);
617
618 if(WIFSTOPPED(status)){
619 n = WSTOPSIG(status);
620 switch(n) {
621 case SIGSTOP:
622 XLOG("stopped -- continuing\n");
623 n = ptrace(PTRACE_CONT, tid, 0, 0);
624 if(n) {
625 LOG("ptrace failed: %s\n", strerror(errno));
626 goto done;
627 }
628 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700629
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 case SIGILL:
631 case SIGABRT:
632 case SIGBUS:
633 case SIGFPE:
634 case SIGSEGV:
635 case SIGSTKFLT: {
636 XLOG("stopped -- fatal signal\n");
637 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
638 kill(tid, SIGSTOP);
639 goto done;
640 }
641
642 default:
643 XLOG("stopped -- unexpected signal\n");
644 goto done;
645 }
646 } else {
647 XLOG("unexpected waitpid response\n");
648 goto done;
649 }
650 }
Ben Cheng09e71372009-09-28 11:06:09 -0700651
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652done:
653 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700654
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 /* stop the process so we can debug */
656 kill(cr.pid, SIGSTOP);
657
Ben Cheng09e71372009-09-28 11:06:09 -0700658 /*
659 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700661 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 if (tid_attach_status == 0) {
663 int detach_status;
664 /* detach so we can attach gdbserver */
665 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
666 need_cleanup |= (detach_status != 0);
667 }
668
669 /*
670 * if debug.db.uid is set, its value indicates if we should wait
671 * for user action for the crashing process.
672 * in this case, we log a message and turn the debug LED on
673 * waiting for a gdb connection (for instance)
674 */
675
676 if ((signed)cr.uid <= debug_uid) {
677 wait_for_user_action(tid, &cr);
678 }
679
680 /* resume stopped process (so it can crash in peace) */
681 kill(cr.pid, SIGCONT);
682
683 if (need_cleanup) {
684 LOG("debuggerd committing suicide to free the zombie!\n");
685 kill(getpid(), SIGKILL);
686 }
687
688 if(fd != -1) close(fd);
689}
690
Bruce Beare84924902010-10-13 14:21:30 -0700691
Ben Cheng09e71372009-09-28 11:06:09 -0700692int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693{
694 int s;
695 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700696 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700697
Andy McFadden44e12ec2011-07-29 12:36:47 -0700698 /*
699 * debuggerd crashes can't be reported to debuggerd. Reset all of the
700 * crash handlers.
701 */
702 signal(SIGILL, SIG_DFL);
703 signal(SIGABRT, SIG_DFL);
704 signal(SIGBUS, SIG_DFL);
705 signal(SIGFPE, SIG_DFL);
706 signal(SIGSEGV, SIG_DFL);
707 signal(SIGSTKFLT, SIG_DFL);
708 signal(SIGPIPE, SIG_DFL);
709
Ben Cheng09e71372009-09-28 11:06:09 -0700710 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
712 if(logsocket < 0) {
713 logsocket = -1;
714 } else {
715 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
716 }
717
718 act.sa_handler = SIG_DFL;
719 sigemptyset(&act.sa_mask);
720 sigaddset(&act.sa_mask,SIGCHLD);
721 act.sa_flags = SA_NOCLDWAIT;
722 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700723
724 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
726 if(s < 0) return -1;
727 fcntl(s, F_SETFD, FD_CLOEXEC);
728
729 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700730
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 for(;;) {
732 struct sockaddr addr;
733 socklen_t alen;
734 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700735
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 alen = sizeof(addr);
737 fd = accept(s, &addr, &alen);
738 if(fd < 0) continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700739
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 fcntl(fd, F_SETFD, FD_CLOEXEC);
741
742 handle_crashing_process(fd);
743 }
744 return 0;
745}