blob: eda43cc9d26b4cee83a866e6ed44e730216e0f51 [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
117void dump_fault_addr(int tfd, int pid, int sig)
118{
119 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 memset(&si, 0, sizeof(si));
122 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
123 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
124 } else {
125 _LOG(tfd, false, "signal %d (%s), fault addr %08x\n",
126 sig, get_signame(sig), si.si_addr);
127 }
128}
129
130void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
131{
132 char data[1024];
133 char *x = 0;
134 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 sprintf(data, "/proc/%d/cmdline", pid);
137 fp = fopen(data, "r");
138 if(fp) {
139 x = fgets(data, 1024, fp);
140 fclose(fp);
141 }
Ben Cheng09e71372009-09-28 11:06:09 -0700142
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 _LOG(tfd, false,
144 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
145 dump_build_info(tfd);
146 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
147 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 if(sig) dump_fault_addr(tfd, tid, sig);
150}
151
Meng Huae7b91b2009-11-05 16:10:50 -0600152static void parse_elf_info(mapinfo *milist, pid_t pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153{
154 mapinfo *mi;
155 for (mi = milist; mi != NULL; mi = mi->next) {
156 Elf32_Ehdr ehdr;
157
158 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700159 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 * mapped section.
161 */
Ben Cheng09e71372009-09-28 11:06:09 -0700162 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 sizeof(Elf32_Ehdr));
164 /* Check if it has the matching magic words */
165 if (IS_ELF(ehdr)) {
166 Elf32_Phdr phdr;
167 Elf32_Phdr *ptr;
168 int i;
169
170 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
171 for (i = 0; i < ehdr.e_phnum; i++) {
172 /* Parse the program header */
Ben Cheng09e71372009-09-28 11:06:09 -0700173 get_remote_struct(pid, (char *) ptr+i, &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 sizeof(Elf32_Phdr));
Bruce Beare84924902010-10-13 14:21:30 -0700175#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 /* Found a EXIDX segment? */
177 if (phdr.p_type == PT_ARM_EXIDX) {
178 mi->exidx_start = mi->start + phdr.p_offset;
179 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
180 break;
181 }
Bruce Beare84924902010-10-13 14:21:30 -0700182#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 }
Meng Huae7b91b2009-11-05 16:10:50 -0600184
185 /* Try to load symbols from this file */
186 mi->symbols = symbol_table_create(mi->name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 }
188 }
189}
190
191void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
192{
193 char data[1024];
194 FILE *fp;
195 mapinfo *milist = 0;
196 unsigned int sp_list[STACK_CONTENT_DEPTH];
197 int stack_depth;
Bruce Beare84924902010-10-13 14:21:30 -0700198#ifdef __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 int frame0_pc_sane = 1;
Bruce Beare84924902010-10-13 14:21:30 -0700200#endif
Ben Cheng09e71372009-09-28 11:06:09 -0700201
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 if (!at_fault) {
203 _LOG(tfd, true,
204 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
205 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
206 }
207
208 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700209
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 /* Clear stack pointer records */
211 memset(sp_list, 0, sizeof(sp_list));
212
213 sprintf(data, "/proc/%d/maps", pid);
214 fp = fopen(data, "r");
215 if(fp) {
216 while(fgets(data, 1024, fp)) {
217 mapinfo *mi = parse_maps_line(data);
218 if(mi) {
219 mi->next = milist;
220 milist = mi;
221 }
222 }
223 fclose(fp);
224 }
225
Meng Huae7b91b2009-11-05 16:10:50 -0600226 parse_elf_info(milist, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Bruce Beare84924902010-10-13 14:21:30 -0700228#if __arm__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 /* If stack unwinder fails, use the default solution to dump the stack
230 * content.
231 */
232 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
233 &frame0_pc_sane, at_fault);
234
235 /* The stack unwinder should at least unwind two levels of stack. If less
236 * level is seen we make sure at lease pc and lr are dumped.
237 */
238 if (stack_depth < 2) {
239 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
240 }
241
Ben Cheng2854db82010-01-28 10:00:03 -0800242 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
Bruce Beare84924902010-10-13 14:21:30 -0700243#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
245 while(milist) {
246 mapinfo *next = milist->next;
Meng Huae7b91b2009-11-05 16:10:50 -0600247 symbol_table_free(milist->symbols);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 free(milist);
249 milist = next;
250 }
251}
252
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253#define MAX_TOMBSTONES 10
254
255#define typecheck(x,y) { \
256 typeof(x) __dummy1; \
257 typeof(y) __dummy2; \
258 (void)(&__dummy1 == &__dummy2); }
259
260#define TOMBSTONE_DIR "/data/tombstones"
261
262/*
263 * find_and_open_tombstone - find an available tombstone slot, if any, of the
264 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
265 * file is available, we reuse the least-recently-modified file.
266 */
267static int find_and_open_tombstone(void)
268{
269 unsigned long mtime = ULONG_MAX;
270 struct stat sb;
271 char path[128];
272 int fd, i, oldest = 0;
273
274 /*
275 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
276 * to, our logic breaks. This check will generate a warning if that happens.
277 */
278 typecheck(mtime, sb.st_mtime);
279
280 /*
281 * In a single wolf-like pass, find an available slot and, in case none
282 * exist, find and record the least-recently-modified file.
283 */
284 for (i = 0; i < MAX_TOMBSTONES; i++) {
285 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
286
287 if (!stat(path, &sb)) {
288 if (sb.st_mtime < mtime) {
289 oldest = i;
290 mtime = sb.st_mtime;
291 }
292 continue;
293 }
294 if (errno != ENOENT)
295 continue;
296
297 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
298 if (fd < 0)
299 continue; /* raced ? */
300
301 fchown(fd, AID_SYSTEM, AID_SYSTEM);
302 return fd;
303 }
304
305 /* we didn't find an available file, so we clobber the oldest one */
306 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
307 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
308 fchown(fd, AID_SYSTEM, AID_SYSTEM);
309
310 return fd;
311}
312
313/* Return true if some thread is not detached cleanly */
314static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
315{
316 char task_path[1024];
317
318 sprintf(task_path, "/proc/%d/task", pid);
319 DIR *d;
320 struct dirent *de;
321 int need_cleanup = 0;
322
323 d = opendir(task_path);
324 /* Bail early if cannot open the task directory */
325 if (d == NULL) {
326 XLOG("Cannot open /proc/%d/task\n", pid);
327 return false;
328 }
329 while ((de = readdir(d)) != NULL) {
330 unsigned new_tid;
331 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700332 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 continue;
334 new_tid = atoi(de->d_name);
335 /* The main thread at fault has been handled individually */
336 if (new_tid == tid)
337 continue;
338
339 /* Skip this thread if cannot ptrace it */
340 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
341 continue;
342
343 dump_crash_report(tfd, pid, new_tid, false);
344 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
345 }
346 closedir(d);
347 return need_cleanup != 0;
348}
349
350/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700351static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 int signal)
353{
354 int fd;
355 bool need_cleanup = false;
356
357 mkdir(TOMBSTONE_DIR, 0755);
358 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
359
360 fd = find_and_open_tombstone();
361 if (fd < 0)
362 return need_cleanup;
363
364 dump_crash_banner(fd, pid, tid, signal);
365 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700366 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 * If the user has requested to attach gdb, don't collect the per-thread
368 * information as it increases the chance to lose track of the process.
369 */
370 if ((signed)pid > debug_uid) {
371 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
372 }
373
374 close(fd);
375 return need_cleanup;
376}
377
378static int
379write_string(const char* file, const char* string)
380{
381 int len;
382 int fd;
383 ssize_t amt;
384 fd = open(file, O_RDWR);
385 len = strlen(string);
386 if (fd < 0)
387 return -errno;
388 amt = write(fd, string, len);
389 close(fd);
390 return amt >= 0 ? 0 : -errno;
391}
392
393static
394void init_debug_led(void)
395{
396 // trout leds
397 write_string("/sys/class/leds/red/brightness", "0");
398 write_string("/sys/class/leds/green/brightness", "0");
399 write_string("/sys/class/leds/blue/brightness", "0");
400 write_string("/sys/class/leds/red/device/blink", "0");
401 // sardine leds
402 write_string("/sys/class/leds/left/cadence", "0,0");
403}
404
405static
406void enable_debug_led(void)
407{
408 // trout leds
409 write_string("/sys/class/leds/red/brightness", "255");
410 // sardine leds
411 write_string("/sys/class/leds/left/cadence", "1,0");
412}
413
414static
415void disable_debug_led(void)
416{
417 // trout leds
418 write_string("/sys/class/leds/red/brightness", "0");
419 // sardine leds
420 write_string("/sys/class/leds/left/cadence", "0,0");
421}
422
423extern int init_getevent();
424extern void uninit_getevent();
425extern int get_event(struct input_event* event, int timeout);
426
427static void wait_for_user_action(unsigned tid, struct ucred* cr)
428{
429 (void)tid;
430 /* First log a helpful message */
431 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800432 "* Process %d has been suspended while crashing. To\n"
433 "* attach gdbserver for a gdb connection on port 5039:\n"
434 "*\n"
435 "* adb shell gdbserver :5039 --attach %d &\n"
436 "*\n"
437 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700438 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 cr->pid, cr->pid);
440
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800441 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 if (init_getevent() == 0) {
443 int ms = 1200 / 10;
444 int dit = 1;
445 int dah = 3*dit;
446 int _ = -dit;
447 int ___ = 3*_;
448 int _______ = 7*_;
449 const signed char codes[] = {
450 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
451 };
452 size_t s = 0;
453 struct input_event e;
454 int home = 0;
455 init_debug_led();
456 enable_debug_led();
457 do {
458 int timeout = abs((int)(codes[s])) * ms;
459 int res = get_event(&e, timeout);
460 if (res == 0) {
461 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
462 home = 1;
463 } else if (res == 1) {
464 if (++s >= sizeof(codes)/sizeof(*codes))
465 s = 0;
466 if (codes[s] > 0) {
467 enable_debug_led();
468 } else {
469 disable_debug_led();
470 }
471 }
Ben Cheng09e71372009-09-28 11:06:09 -0700472 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 uninit_getevent();
474 }
475
476 /* don't forget to turn debug led off */
477 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 /* close filedescriptor */
480 LOG("debuggerd resuming process %d", cr->pid);
481 }
482
483static void handle_crashing_process(int fd)
484{
485 char buf[64];
486 struct stat s;
487 unsigned tid;
488 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700489 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 int tid_attach_status = -1;
491 unsigned retry = 30;
492 bool need_cleanup = false;
493
494 char value[PROPERTY_VALUE_MAX];
495 property_get("debug.db.uid", value, "-1");
496 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700499
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 len = sizeof(cr);
501 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
502 if(n != 0) {
503 LOG("cannot get credentials\n");
504 goto done;
505 }
506
Ben Cheng09e71372009-09-28 11:06:09 -0700507 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 fcntl(fd, F_SETFL, O_NONBLOCK);
509 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
510 if(errno == EINTR) continue;
511 if(errno == EWOULDBLOCK) {
512 if(retry-- > 0) {
513 usleep(100 * 1000);
514 continue;
515 }
516 LOG("timed out reading tid\n");
517 goto done;
518 }
519 LOG("read failure? %s\n", strerror(errno));
520 goto done;
521 }
522
523 sprintf(buf,"/proc/%d/task/%d", cr.pid, tid);
524 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800525 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 tid, cr.pid);
527 close(fd);
528 return;
529 }
Ben Cheng09e71372009-09-28 11:06:09 -0700530
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
532
533 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
534 if(tid_attach_status < 0) {
535 LOG("ptrace attach failed: %s\n", strerror(errno));
536 goto done;
537 }
538
539 close(fd);
540 fd = -1;
541
542 for(;;) {
543 n = waitpid(tid, &status, __WALL);
Ben Cheng09e71372009-09-28 11:06:09 -0700544
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 if(n < 0) {
546 if(errno == EAGAIN) continue;
547 LOG("waitpid failed: %s\n", strerror(errno));
548 goto done;
549 }
550
551 XLOG("waitpid: n=%d status=%08x\n", n, status);
552
553 if(WIFSTOPPED(status)){
554 n = WSTOPSIG(status);
555 switch(n) {
556 case SIGSTOP:
557 XLOG("stopped -- continuing\n");
558 n = ptrace(PTRACE_CONT, tid, 0, 0);
559 if(n) {
560 LOG("ptrace failed: %s\n", strerror(errno));
561 goto done;
562 }
563 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700564
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 case SIGILL:
566 case SIGABRT:
567 case SIGBUS:
568 case SIGFPE:
569 case SIGSEGV:
570 case SIGSTKFLT: {
571 XLOG("stopped -- fatal signal\n");
572 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
573 kill(tid, SIGSTOP);
574 goto done;
575 }
576
577 default:
578 XLOG("stopped -- unexpected signal\n");
579 goto done;
580 }
581 } else {
582 XLOG("unexpected waitpid response\n");
583 goto done;
584 }
585 }
Ben Cheng09e71372009-09-28 11:06:09 -0700586
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587done:
588 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700589
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 /* stop the process so we can debug */
591 kill(cr.pid, SIGSTOP);
592
Ben Cheng09e71372009-09-28 11:06:09 -0700593 /*
594 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700596 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 if (tid_attach_status == 0) {
598 int detach_status;
599 /* detach so we can attach gdbserver */
600 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
601 need_cleanup |= (detach_status != 0);
602 }
603
604 /*
605 * if debug.db.uid is set, its value indicates if we should wait
606 * for user action for the crashing process.
607 * in this case, we log a message and turn the debug LED on
608 * waiting for a gdb connection (for instance)
609 */
610
611 if ((signed)cr.uid <= debug_uid) {
612 wait_for_user_action(tid, &cr);
613 }
614
615 /* resume stopped process (so it can crash in peace) */
616 kill(cr.pid, SIGCONT);
617
618 if (need_cleanup) {
619 LOG("debuggerd committing suicide to free the zombie!\n");
620 kill(getpid(), SIGKILL);
621 }
622
623 if(fd != -1) close(fd);
624}
625
Bruce Beare84924902010-10-13 14:21:30 -0700626
Ben Cheng09e71372009-09-28 11:06:09 -0700627int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628{
629 int s;
630 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700631 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700632
633 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
635 if(logsocket < 0) {
636 logsocket = -1;
637 } else {
638 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
639 }
640
641 act.sa_handler = SIG_DFL;
642 sigemptyset(&act.sa_mask);
643 sigaddset(&act.sa_mask,SIGCHLD);
644 act.sa_flags = SA_NOCLDWAIT;
645 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700646
647 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
649 if(s < 0) return -1;
650 fcntl(s, F_SETFD, FD_CLOEXEC);
651
652 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700653
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 for(;;) {
655 struct sockaddr addr;
656 socklen_t alen;
657 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700658
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 alen = sizeof(addr);
660 fd = accept(s, &addr, &alen);
661 if(fd < 0) continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700662
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663 fcntl(fd, F_SETFD, FD_CLOEXEC);
664
665 handle_crashing_process(fd);
666 }
667 return 0;
668}