debuggerd.c revision 2854db8969cd061d83a931769dfaf4e57108d9df
1/* system/debuggerd/debuggerd.c 2** 3** Copyright 2006, The Android Open Source Project 4** 5** 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 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** 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 15** 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 51static 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]; 59 60 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; 92 93 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; 98 99 mi->start = strtoul(line, 0, 16); 100 mi->end = strtoul(line + 9, 0, 16); 101 /* To be filled in parse_exidx_info if the mapped section starts with 102 * 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 121void dump_stack_and_code(int tfd, int pid, mapinfo *map, 122 int unwind_depth, unsigned int sp_list[], 123 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; 129 char code_buffer[80]; 130 131 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return; 132 sp = r.ARM_sp; 133 pc = r.ARM_pc; 134 135 _LOG(tfd, only_in_tombstone, "\ncode around pc:\n"); 136 137 end = p = pc & ~3; 138 p -= 32; 139 end += 32; 140 141 /* Dump the code around PC as: 142 * addr contents 143 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c 144 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00 145 */ 146 while (p <= end) { 147 int i; 148 149 sprintf(code_buffer, "%08x ", p); 150 for (i = 0; i < 4; i++) { 151 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL); 152 sprintf(code_buffer + strlen(code_buffer), "%08x ", data); 153 p += 4; 154 } 155 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer); 156 } 157 158 if ((unsigned) r.ARM_lr != pc) { 159 _LOG(tfd, only_in_tombstone, "\ncode around lr:\n"); 160 161 end = p = r.ARM_lr & ~3; 162 p -= 32; 163 end += 32; 164 165 /* Dump the code around LR as: 166 * addr contents 167 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c 168 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00 169 */ 170 while (p <= end) { 171 int i; 172 173 sprintf(code_buffer, "%08x ", p); 174 for (i = 0; i < 4; i++) { 175 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL); 176 sprintf(code_buffer + strlen(code_buffer), "%08x ", data); 177 p += 4; 178 } 179 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer); 180 } 181 } 182 183 p = sp - 64; 184 p &= ~3; 185 if (unwind_depth != 0) { 186 if (unwind_depth < STACK_CONTENT_DEPTH) { 187 end = sp_list[unwind_depth-1]; 188 } 189 else { 190 end = sp_list[STACK_CONTENT_DEPTH-1]; 191 } 192 } 193 else { 194 end = sp | 0x000000ff; 195 end += 0xff; 196 } 197 198 _LOG(tfd, only_in_tombstone, "\nstack:\n"); 199 200 /* If the crash is due to PC == 0, there will be two frames that 201 * have identical SP value. 202 */ 203 if (sp_list[0] == sp_list[1]) { 204 sp_depth = 1; 205 } 206 else { 207 sp_depth = 0; 208 } 209 210 while (p <= end) { 211 char *prompt; 212 char level[16]; 213 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL); 214 if (p == sp_list[sp_depth]) { 215 sprintf(level, "#%02d", sp_depth++); 216 prompt = level; 217 } 218 else { 219 prompt = " "; 220 } 221 222 /* Print the stack content in the log for the first 3 frames. For the 223 * rest only print them in the tombstone file. 224 */ 225 _LOG(tfd, (sp_depth > 2) || only_in_tombstone, 226 "%s %08x %08x %s\n", prompt, p, data, 227 map_to_name(map, data, "")); 228 p += 4; 229 } 230 /* print another 64-byte of stack data after the last frame */ 231 232 end = p+64; 233 while (p <= end) { 234 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL); 235 _LOG(tfd, (sp_depth > 2) || only_in_tombstone, 236 " %08x %08x %s\n", p, data, 237 map_to_name(map, data, "")); 238 p += 4; 239 } 240} 241 242void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level, 243 bool at_fault) 244{ 245 struct pt_regs r; 246 247 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) { 248 _LOG(tfd, !at_fault, "tid %d not responding!\n", pid); 249 return; 250 } 251 252 if (unwound_level == 0) { 253 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n", 0, r.ARM_pc, 254 map_to_name(map, r.ARM_pc, "<unknown>")); 255 } 256 _LOG(tfd, !at_fault, " #%02d lr %08x %s\n", 1, r.ARM_lr, 257 map_to_name(map, r.ARM_lr, "<unknown>")); 258} 259 260void dump_registers(int tfd, int pid, bool at_fault) 261{ 262 struct pt_regs r; 263 bool only_in_tombstone = !at_fault; 264 265 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) { 266 _LOG(tfd, only_in_tombstone, 267 "cannot get registers: %s\n", strerror(errno)); 268 return; 269 } 270 271 _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n", 272 r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3); 273 _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n", 274 r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7); 275 _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x 10 %08x fp %08x\n", 276 r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp); 277 _LOG(tfd, only_in_tombstone, 278 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n", 279 r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr); 280 281#if __ARM_NEON__ 282 struct user_vfp vfp_regs; 283 int i; 284 285 if(ptrace(PTRACE_GETVFPREGS, pid, 0, &vfp_regs)) { 286 _LOG(tfd, only_in_tombstone, 287 "cannot get registers: %s\n", strerror(errno)); 288 return; 289 } 290 291 for (i = 0; i < 32; i += 2) { 292 _LOG(tfd, only_in_tombstone, 293 " d%-2d %016llx d%-2d %016llx\n", 294 i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]); 295 } 296 _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr); 297#endif 298} 299 300const char *get_signame(int sig) 301{ 302 switch(sig) { 303 case SIGILL: return "SIGILL"; 304 case SIGABRT: return "SIGABRT"; 305 case SIGBUS: return "SIGBUS"; 306 case SIGFPE: return "SIGFPE"; 307 case SIGSEGV: return "SIGSEGV"; 308 case SIGSTKFLT: return "SIGSTKFLT"; 309 default: return "?"; 310 } 311} 312 313void dump_fault_addr(int tfd, int pid, int sig) 314{ 315 siginfo_t si; 316 317 memset(&si, 0, sizeof(si)); 318 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){ 319 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno)); 320 } else { 321 _LOG(tfd, false, "signal %d (%s), fault addr %08x\n", 322 sig, get_signame(sig), si.si_addr); 323 } 324} 325 326void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig) 327{ 328 char data[1024]; 329 char *x = 0; 330 FILE *fp; 331 332 sprintf(data, "/proc/%d/cmdline", pid); 333 fp = fopen(data, "r"); 334 if(fp) { 335 x = fgets(data, 1024, fp); 336 fclose(fp); 337 } 338 339 _LOG(tfd, false, 340 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); 341 dump_build_info(tfd); 342 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n", 343 pid, tid, x ? x : "UNKNOWN"); 344 345 if(sig) dump_fault_addr(tfd, tid, sig); 346} 347 348static void parse_exidx_info(mapinfo *milist, pid_t pid) 349{ 350 mapinfo *mi; 351 for (mi = milist; mi != NULL; mi = mi->next) { 352 Elf32_Ehdr ehdr; 353 354 memset(&ehdr, 0, sizeof(Elf32_Ehdr)); 355 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of 356 * mapped section. 357 */ 358 get_remote_struct(pid, (void *) (mi->start), &ehdr, 359 sizeof(Elf32_Ehdr)); 360 /* Check if it has the matching magic words */ 361 if (IS_ELF(ehdr)) { 362 Elf32_Phdr phdr; 363 Elf32_Phdr *ptr; 364 int i; 365 366 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff); 367 for (i = 0; i < ehdr.e_phnum; i++) { 368 /* Parse the program header */ 369 get_remote_struct(pid, (char *) ptr+i, &phdr, 370 sizeof(Elf32_Phdr)); 371 /* Found a EXIDX segment? */ 372 if (phdr.p_type == PT_ARM_EXIDX) { 373 mi->exidx_start = mi->start + phdr.p_offset; 374 mi->exidx_end = mi->exidx_start + phdr.p_filesz; 375 break; 376 } 377 } 378 } 379 } 380} 381 382void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault) 383{ 384 char data[1024]; 385 FILE *fp; 386 mapinfo *milist = 0; 387 unsigned int sp_list[STACK_CONTENT_DEPTH]; 388 int stack_depth; 389 int frame0_pc_sane = 1; 390 391 if (!at_fault) { 392 _LOG(tfd, true, 393 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); 394 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid); 395 } 396 397 dump_registers(tfd, tid, at_fault); 398 399 /* Clear stack pointer records */ 400 memset(sp_list, 0, sizeof(sp_list)); 401 402 sprintf(data, "/proc/%d/maps", pid); 403 fp = fopen(data, "r"); 404 if(fp) { 405 while(fgets(data, 1024, fp)) { 406 mapinfo *mi = parse_maps_line(data); 407 if(mi) { 408 mi->next = milist; 409 milist = mi; 410 } 411 } 412 fclose(fp); 413 } 414 415 parse_exidx_info(milist, tid); 416 417 /* If stack unwinder fails, use the default solution to dump the stack 418 * content. 419 */ 420 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list, 421 &frame0_pc_sane, at_fault); 422 423 /* The stack unwinder should at least unwind two levels of stack. If less 424 * level is seen we make sure at lease pc and lr are dumped. 425 */ 426 if (stack_depth < 2) { 427 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault); 428 } 429 430 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault); 431 432 while(milist) { 433 mapinfo *next = milist->next; 434 free(milist); 435 milist = next; 436 } 437} 438 439#define MAX_TOMBSTONES 10 440 441#define typecheck(x,y) { \ 442 typeof(x) __dummy1; \ 443 typeof(y) __dummy2; \ 444 (void)(&__dummy1 == &__dummy2); } 445 446#define TOMBSTONE_DIR "/data/tombstones" 447 448/* 449 * find_and_open_tombstone - find an available tombstone slot, if any, of the 450 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no 451 * file is available, we reuse the least-recently-modified file. 452 */ 453static int find_and_open_tombstone(void) 454{ 455 unsigned long mtime = ULONG_MAX; 456 struct stat sb; 457 char path[128]; 458 int fd, i, oldest = 0; 459 460 /* 461 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought 462 * to, our logic breaks. This check will generate a warning if that happens. 463 */ 464 typecheck(mtime, sb.st_mtime); 465 466 /* 467 * In a single wolf-like pass, find an available slot and, in case none 468 * exist, find and record the least-recently-modified file. 469 */ 470 for (i = 0; i < MAX_TOMBSTONES; i++) { 471 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i); 472 473 if (!stat(path, &sb)) { 474 if (sb.st_mtime < mtime) { 475 oldest = i; 476 mtime = sb.st_mtime; 477 } 478 continue; 479 } 480 if (errno != ENOENT) 481 continue; 482 483 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); 484 if (fd < 0) 485 continue; /* raced ? */ 486 487 fchown(fd, AID_SYSTEM, AID_SYSTEM); 488 return fd; 489 } 490 491 /* we didn't find an available file, so we clobber the oldest one */ 492 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest); 493 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); 494 fchown(fd, AID_SYSTEM, AID_SYSTEM); 495 496 return fd; 497} 498 499/* Return true if some thread is not detached cleanly */ 500static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid) 501{ 502 char task_path[1024]; 503 504 sprintf(task_path, "/proc/%d/task", pid); 505 DIR *d; 506 struct dirent *de; 507 int need_cleanup = 0; 508 509 d = opendir(task_path); 510 /* Bail early if cannot open the task directory */ 511 if (d == NULL) { 512 XLOG("Cannot open /proc/%d/task\n", pid); 513 return false; 514 } 515 while ((de = readdir(d)) != NULL) { 516 unsigned new_tid; 517 /* Ignore "." and ".." */ 518 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) 519 continue; 520 new_tid = atoi(de->d_name); 521 /* The main thread at fault has been handled individually */ 522 if (new_tid == tid) 523 continue; 524 525 /* Skip this thread if cannot ptrace it */ 526 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) 527 continue; 528 529 dump_crash_report(tfd, pid, new_tid, false); 530 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0); 531 } 532 closedir(d); 533 return need_cleanup != 0; 534} 535 536/* Return true if some thread is not detached cleanly */ 537static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid, 538 int signal) 539{ 540 int fd; 541 bool need_cleanup = false; 542 543 mkdir(TOMBSTONE_DIR, 0755); 544 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM); 545 546 fd = find_and_open_tombstone(); 547 if (fd < 0) 548 return need_cleanup; 549 550 dump_crash_banner(fd, pid, tid, signal); 551 dump_crash_report(fd, pid, tid, true); 552 /* 553 * If the user has requested to attach gdb, don't collect the per-thread 554 * information as it increases the chance to lose track of the process. 555 */ 556 if ((signed)pid > debug_uid) { 557 need_cleanup = dump_sibling_thread_report(fd, pid, tid); 558 } 559 560 close(fd); 561 return need_cleanup; 562} 563 564static int 565write_string(const char* file, const char* string) 566{ 567 int len; 568 int fd; 569 ssize_t amt; 570 fd = open(file, O_RDWR); 571 len = strlen(string); 572 if (fd < 0) 573 return -errno; 574 amt = write(fd, string, len); 575 close(fd); 576 return amt >= 0 ? 0 : -errno; 577} 578 579static 580void init_debug_led(void) 581{ 582 // trout leds 583 write_string("/sys/class/leds/red/brightness", "0"); 584 write_string("/sys/class/leds/green/brightness", "0"); 585 write_string("/sys/class/leds/blue/brightness", "0"); 586 write_string("/sys/class/leds/red/device/blink", "0"); 587 // sardine leds 588 write_string("/sys/class/leds/left/cadence", "0,0"); 589} 590 591static 592void enable_debug_led(void) 593{ 594 // trout leds 595 write_string("/sys/class/leds/red/brightness", "255"); 596 // sardine leds 597 write_string("/sys/class/leds/left/cadence", "1,0"); 598} 599 600static 601void disable_debug_led(void) 602{ 603 // trout leds 604 write_string("/sys/class/leds/red/brightness", "0"); 605 // sardine leds 606 write_string("/sys/class/leds/left/cadence", "0,0"); 607} 608 609extern int init_getevent(); 610extern void uninit_getevent(); 611extern int get_event(struct input_event* event, int timeout); 612 613static void wait_for_user_action(unsigned tid, struct ucred* cr) 614{ 615 (void)tid; 616 /* First log a helpful message */ 617 LOG( "********************************************************\n" 618 "* Process %d has been suspended while crashing. To\n" 619 "* attach gdbserver for a gdb connection on port 5039:\n" 620 "*\n" 621 "* adb shell gdbserver :5039 --attach %d &\n" 622 "*\n" 623 "* Press HOME key to let the process continue crashing.\n" 624 "********************************************************\n", 625 cr->pid, cr->pid); 626 627 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */ 628 if (init_getevent() == 0) { 629 int ms = 1200 / 10; 630 int dit = 1; 631 int dah = 3*dit; 632 int _ = -dit; 633 int ___ = 3*_; 634 int _______ = 7*_; 635 const signed char codes[] = { 636 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______ 637 }; 638 size_t s = 0; 639 struct input_event e; 640 int home = 0; 641 init_debug_led(); 642 enable_debug_led(); 643 do { 644 int timeout = abs((int)(codes[s])) * ms; 645 int res = get_event(&e, timeout); 646 if (res == 0) { 647 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0) 648 home = 1; 649 } else if (res == 1) { 650 if (++s >= sizeof(codes)/sizeof(*codes)) 651 s = 0; 652 if (codes[s] > 0) { 653 enable_debug_led(); 654 } else { 655 disable_debug_led(); 656 } 657 } 658 } while (!home); 659 uninit_getevent(); 660 } 661 662 /* don't forget to turn debug led off */ 663 disable_debug_led(); 664 665 /* close filedescriptor */ 666 LOG("debuggerd resuming process %d", cr->pid); 667 } 668 669static void handle_crashing_process(int fd) 670{ 671 char buf[64]; 672 struct stat s; 673 unsigned tid; 674 struct ucred cr; 675 int n, len, status; 676 int tid_attach_status = -1; 677 unsigned retry = 30; 678 bool need_cleanup = false; 679 680 char value[PROPERTY_VALUE_MAX]; 681 property_get("debug.db.uid", value, "-1"); 682 int debug_uid = atoi(value); 683 684 XLOG("handle_crashing_process(%d)\n", fd); 685 686 len = sizeof(cr); 687 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); 688 if(n != 0) { 689 LOG("cannot get credentials\n"); 690 goto done; 691 } 692 693 XLOG("reading tid\n"); 694 fcntl(fd, F_SETFL, O_NONBLOCK); 695 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) { 696 if(errno == EINTR) continue; 697 if(errno == EWOULDBLOCK) { 698 if(retry-- > 0) { 699 usleep(100 * 1000); 700 continue; 701 } 702 LOG("timed out reading tid\n"); 703 goto done; 704 } 705 LOG("read failure? %s\n", strerror(errno)); 706 goto done; 707 } 708 709 sprintf(buf,"/proc/%d/task/%d", cr.pid, tid); 710 if(stat(buf, &s)) { 711 LOG("tid %d does not exist in pid %d. ignoring debug request\n", 712 tid, cr.pid); 713 close(fd); 714 return; 715 } 716 717 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid); 718 719 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0); 720 if(tid_attach_status < 0) { 721 LOG("ptrace attach failed: %s\n", strerror(errno)); 722 goto done; 723 } 724 725 close(fd); 726 fd = -1; 727 728 for(;;) { 729 n = waitpid(tid, &status, __WALL); 730 731 if(n < 0) { 732 if(errno == EAGAIN) continue; 733 LOG("waitpid failed: %s\n", strerror(errno)); 734 goto done; 735 } 736 737 XLOG("waitpid: n=%d status=%08x\n", n, status); 738 739 if(WIFSTOPPED(status)){ 740 n = WSTOPSIG(status); 741 switch(n) { 742 case SIGSTOP: 743 XLOG("stopped -- continuing\n"); 744 n = ptrace(PTRACE_CONT, tid, 0, 0); 745 if(n) { 746 LOG("ptrace failed: %s\n", strerror(errno)); 747 goto done; 748 } 749 continue; 750 751 case SIGILL: 752 case SIGABRT: 753 case SIGBUS: 754 case SIGFPE: 755 case SIGSEGV: 756 case SIGSTKFLT: { 757 XLOG("stopped -- fatal signal\n"); 758 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n); 759 kill(tid, SIGSTOP); 760 goto done; 761 } 762 763 default: 764 XLOG("stopped -- unexpected signal\n"); 765 goto done; 766 } 767 } else { 768 XLOG("unexpected waitpid response\n"); 769 goto done; 770 } 771 } 772 773done: 774 XLOG("detaching\n"); 775 776 /* stop the process so we can debug */ 777 kill(cr.pid, SIGSTOP); 778 779 /* 780 * If a thread has been attached by ptrace, make sure it is detached 781 * successfully otherwise we will get a zombie. 782 */ 783 if (tid_attach_status == 0) { 784 int detach_status; 785 /* detach so we can attach gdbserver */ 786 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0); 787 need_cleanup |= (detach_status != 0); 788 } 789 790 /* 791 * if debug.db.uid is set, its value indicates if we should wait 792 * for user action for the crashing process. 793 * in this case, we log a message and turn the debug LED on 794 * waiting for a gdb connection (for instance) 795 */ 796 797 if ((signed)cr.uid <= debug_uid) { 798 wait_for_user_action(tid, &cr); 799 } 800 801 /* resume stopped process (so it can crash in peace) */ 802 kill(cr.pid, SIGCONT); 803 804 if (need_cleanup) { 805 LOG("debuggerd committing suicide to free the zombie!\n"); 806 kill(getpid(), SIGKILL); 807 } 808 809 if(fd != -1) close(fd); 810} 811 812int main() 813{ 814 int s; 815 struct sigaction act; 816 817 logsocket = socket_local_client("logd", 818 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM); 819 if(logsocket < 0) { 820 logsocket = -1; 821 } else { 822 fcntl(logsocket, F_SETFD, FD_CLOEXEC); 823 } 824 825 act.sa_handler = SIG_DFL; 826 sigemptyset(&act.sa_mask); 827 sigaddset(&act.sa_mask,SIGCHLD); 828 act.sa_flags = SA_NOCLDWAIT; 829 sigaction(SIGCHLD, &act, 0); 830 831 s = socket_local_server("android:debuggerd", 832 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); 833 if(s < 0) return -1; 834 fcntl(s, F_SETFD, FD_CLOEXEC); 835 836 LOG("debuggerd: " __DATE__ " " __TIME__ "\n"); 837 838 for(;;) { 839 struct sockaddr addr; 840 socklen_t alen; 841 int fd; 842 843 alen = sizeof(addr); 844 fd = accept(s, &addr, &alen); 845 if(fd < 0) continue; 846 847 fcntl(fd, F_SETFD, FD_CLOEXEC); 848 849 handle_crashing_process(fd); 850 } 851 return 0; 852} 853