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