Home | History | Annotate | Download | only in debuggerd
      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 <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>
     34 #include <cutils/properties.h>
     35 
     36 #include <linux/input.h>
     37 
     38 #include <private/android_filesystem_config.h>
     39 
     40 #include "debuggerd.h"
     41 #include "utility.h"
     42 
     43 #define ANDROID_LOG_INFO 4
     44 
     45 /* Log information onto the tombstone */
     46 void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
     47 {
     48     char buf[512];
     49 
     50     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 
     64 // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so
     65 // 012345678901234567890123456789012345678901234567890123456789
     66 // 0         1         2         3         4         5
     67 
     68 mapinfo *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;
     75 
     76     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;
     81 
     82     mi->start = strtoul(line, 0, 16);
     83     mi->end = strtoul(line + 9, 0, 16);
     84     /* To be filled in parse_elf_info if the mapped section starts with
     85      * elf_header
     86      */
     87     mi->exidx_start = mi->exidx_end = 0;
     88     mi->symbols = 0;
     89     mi->next = 0;
     90     strcpy(mi->name, line + 49);
     91 
     92     return mi;
     93 }
     94 
     95 void 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 
    104 const 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 
    117 const 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 
    161 void dump_fault_addr(int tfd, int pid, int sig)
    162 {
    163     siginfo_t si;
    164 
    165     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 {
    169         _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);
    173     }
    174 }
    175 
    176 void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
    177 {
    178     char data[1024];
    179     char *x = 0;
    180     FILE *fp;
    181 
    182     sprintf(data, "/proc/%d/cmdline", pid);
    183     fp = fopen(data, "r");
    184     if(fp) {
    185         x = fgets(data, 1024, fp);
    186         fclose(fp);
    187     }
    188 
    189     _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");
    194 
    195     if(sig) dump_fault_addr(tfd, tid, sig);
    196 }
    197 
    198 static void parse_elf_info(mapinfo *milist, pid_t pid)
    199 {
    200     mapinfo *mi;
    201     for (mi = milist; mi != NULL; mi = mi->next) {
    202         Elf32_Ehdr ehdr;
    203 
    204         memset(&ehdr, 0, sizeof(Elf32_Ehdr));
    205         /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
    206          * mapped section.
    207          */
    208         get_remote_struct(pid, (void *) (mi->start), &ehdr,
    209                           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 */
    219                 get_remote_struct(pid, (char *) (ptr+i), &phdr,
    220                                   sizeof(Elf32_Phdr));
    221 #ifdef __arm__
    222                 /* 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                 }
    228 #endif
    229             }
    230 
    231             /* Try to load symbols from this file */
    232             mi->symbols = symbol_table_create(mi->name);
    233         }
    234     }
    235 }
    236 
    237 void 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;
    244 #ifdef __arm__
    245     int frame0_pc_sane = 1;
    246 #endif
    247 
    248     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);
    255 
    256     /* 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 
    272     parse_elf_info(milist, tid);
    273 
    274 #if __arm__
    275     /* 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 
    288     dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
    289 #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"
    296 #endif
    297 
    298     while(milist) {
    299         mapinfo *next = milist->next;
    300         symbol_table_free(milist->symbols);
    301         free(milist);
    302         milist = next;
    303     }
    304 }
    305 
    306 #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  */
    320 static 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 */
    367 static 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 ".." */
    385         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
    386             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 */
    404 static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
    405                               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);
    419     /*
    420      * 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 
    431 static int
    432 write_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 
    446 static
    447 void 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 
    458 static
    459 void 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 
    467 static
    468 void 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 
    476 extern int init_getevent();
    477 extern void uninit_getevent();
    478 extern int get_event(struct input_event* event, int timeout);
    479 
    480 static void wait_for_user_action(unsigned tid, struct ucred* cr)
    481 {
    482     (void)tid;
    483     /* First log a helpful message */
    484     LOG(    "********************************************************\n"
    485             "* 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"
    491             "********************************************************\n",
    492             cr->pid, cr->pid);
    493 
    494     /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
    495     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             }
    525         } while (!home);
    526         uninit_getevent();
    527     }
    528 
    529     /* don't forget to turn debug led off */
    530     disable_debug_led();
    531 
    532     /* close filedescriptor */
    533     LOG("debuggerd resuming process %d", cr->pid);
    534  }
    535 
    536 static void handle_crashing_process(int fd)
    537 {
    538     char buf[64];
    539     struct stat s;
    540     unsigned tid;
    541     struct ucred cr;
    542     int n, len, status;
    543     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);
    550 
    551     XLOG("handle_crashing_process(%d)\n", fd);
    552 
    553     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 
    560     XLOG("reading tid\n");
    561     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 
    576     snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid);
    577     if(stat(buf, &s)) {
    578         LOG("tid %d does not exist in pid %d. ignoring debug request\n",
    579             tid, cr.pid);
    580         close(fd);
    581         return;
    582     }
    583 
    584     XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
    585 
    586     /* 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      */
    595     tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
    596     int ptrace_error = errno;
    597 
    598     if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) {
    599         XLOG("failed responding to client: %s\n",
    600             strerror(errno));
    601         goto done;
    602     }
    603 
    604     if(tid_attach_status < 0) {
    605         LOG("ptrace attach failed: %s\n", strerror(ptrace_error));
    606         goto done;
    607     }
    608 
    609     close(fd);
    610     fd = -1;
    611 
    612     const int sleep_time_usec = 200000;         /* 0.2 seconds */
    613     const int max_total_sleep_usec = 3000000;   /* 3 seconds */
    614     int loop_limit = max_total_sleep_usec / sleep_time_usec;
    615     for(;;) {
    616         if (loop_limit-- == 0) {
    617             LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n",
    618                 cr.pid, tid, cr.uid);
    619             goto done;
    620         }
    621         n = waitpid(tid, &status, __WALL | WNOHANG);
    622 
    623         if (n == 0) {
    624             /* not ready yet */
    625             XLOG("not ready yet\n");
    626             usleep(sleep_time_usec);
    627             continue;
    628         }
    629 
    630         if(n < 0) {
    631             if(errno == EAGAIN) continue;
    632             LOG("waitpid failed: %s\n", strerror(errno));
    633             goto done;
    634         }
    635 
    636         XLOG("waitpid: n=%d status=%08x\n", n, status);
    637 
    638         if(WIFSTOPPED(status)){
    639             n = WSTOPSIG(status);
    640             switch(n) {
    641             case SIGSTOP:
    642                 XLOG("stopped -- continuing\n");
    643                 n = ptrace(PTRACE_CONT, tid, 0, 0);
    644                 if(n) {
    645                     LOG("ptrace failed: %s\n", strerror(errno));
    646                     goto done;
    647                 }
    648                 continue;
    649 
    650             case SIGILL:
    651             case SIGABRT:
    652             case SIGBUS:
    653             case SIGFPE:
    654             case SIGSEGV:
    655             case SIGSTKFLT: {
    656                 XLOG("stopped -- fatal signal\n");
    657                 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
    658                 kill(tid, SIGSTOP);
    659                 goto done;
    660             }
    661 
    662             default:
    663                 XLOG("stopped -- unexpected signal\n");
    664                 goto done;
    665             }
    666         } else {
    667             XLOG("unexpected waitpid response\n");
    668             goto done;
    669         }
    670     }
    671 
    672 done:
    673     XLOG("detaching\n");
    674 
    675     /* stop the process so we can debug */
    676     kill(cr.pid, SIGSTOP);
    677 
    678     /*
    679      * If a thread has been attached by ptrace, make sure it is detached
    680      * successfully otherwise we will get a zombie.
    681      */
    682     if (tid_attach_status == 0) {
    683         int detach_status;
    684         /* detach so we can attach gdbserver */
    685         detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
    686         need_cleanup |= (detach_status != 0);
    687     }
    688 
    689     /*
    690      * if debug.db.uid is set, its value indicates if we should wait
    691      * for user action for the crashing process.
    692      * in this case, we log a message and turn the debug LED on
    693      * waiting for a gdb connection (for instance)
    694      */
    695 
    696     if ((signed)cr.uid <= debug_uid) {
    697         wait_for_user_action(tid, &cr);
    698     }
    699 
    700     /* resume stopped process (so it can crash in peace) */
    701     kill(cr.pid, SIGCONT);
    702 
    703     if (need_cleanup) {
    704         LOG("debuggerd committing suicide to free the zombie!\n");
    705         kill(getpid(), SIGKILL);
    706     }
    707 
    708     if(fd != -1) close(fd);
    709 }
    710 
    711 
    712 int main()
    713 {
    714     int s;
    715     struct sigaction act;
    716     int logsocket = -1;
    717 
    718     /*
    719      * debuggerd crashes can't be reported to debuggerd.  Reset all of the
    720      * crash handlers.
    721      */
    722     signal(SIGILL, SIG_DFL);
    723     signal(SIGABRT, SIG_DFL);
    724     signal(SIGBUS, SIG_DFL);
    725     signal(SIGFPE, SIG_DFL);
    726     signal(SIGSEGV, SIG_DFL);
    727     signal(SIGSTKFLT, SIG_DFL);
    728     signal(SIGPIPE, SIG_DFL);
    729 
    730     logsocket = socket_local_client("logd",
    731             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
    732     if(logsocket < 0) {
    733         logsocket = -1;
    734     } else {
    735         fcntl(logsocket, F_SETFD, FD_CLOEXEC);
    736     }
    737 
    738     act.sa_handler = SIG_DFL;
    739     sigemptyset(&act.sa_mask);
    740     sigaddset(&act.sa_mask,SIGCHLD);
    741     act.sa_flags = SA_NOCLDWAIT;
    742     sigaction(SIGCHLD, &act, 0);
    743 
    744     s = socket_local_server("android:debuggerd",
    745             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
    746     if(s < 0) return -1;
    747     fcntl(s, F_SETFD, FD_CLOEXEC);
    748 
    749     LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
    750 
    751     for(;;) {
    752         struct sockaddr addr;
    753         socklen_t alen;
    754         int fd;
    755 
    756         alen = sizeof(addr);
    757         XLOG("waiting for connection\n");
    758         fd = accept(s, &addr, &alen);
    759         if(fd < 0) {
    760             XLOG("accept failed: %s\n", strerror(errno));
    761             continue;
    762         }
    763 
    764         fcntl(fd, F_SETFD, FD_CLOEXEC);
    765 
    766         handle_crashing_process(fd);
    767     }
    768     return 0;
    769 }
    770