Home | History | Annotate | Download | only in gdb-6.6
      1 /*
      2  * Copyright 2006 The Android Open Source Project
      3  */
      4 
      5 #include <dirent.h>
      6 #include <sys/ptrace.h>
      7 #include <stdint.h>
      8 #include <thread_db.h>
      9 #include <stdlib.h>
     10 #include <stdio.h>
     11 #include <unistd.h>
     12 #include <fcntl.h>
     13 #include <errno.h>
     14 
     15 #define DEBUG 0
     16 #if DEBUG
     17 #  include <string.h>  /* for strerror() */
     18 #  define D(...)  fprintf(stderr, "libthread_db:%s: ", __FUNCTION__), fprintf(stderr, __VA_ARGS__)
     19 #else
     20 #  define D(...)  do{}while(0)
     21 #endif
     22 
     23 extern int ps_pglobal_lookup (void *, const char *obj, const char *name, void **sym_addr);
     24 
     25 struct ps_prochandle
     26 {
     27     pid_t pid;
     28 };
     29 
     30 
     31 /*
     32  * This is the list of "special" symbols we care about whose addresses are
     33  * cached by gdbserver from the host at init time.
     34  */
     35 enum {
     36     SYM_TD_CREATE,
     37     SYM_THREAD_LIST,
     38     NUM_SYMS
     39 };
     40 
     41 static char const * gSymbols[] = {
     42     [SYM_TD_CREATE] = "_thread_created_hook",
     43     NULL
     44 };
     45 
     46 
     47 char const **
     48 td_symbol_list(void)
     49 {
     50     return gSymbols;
     51 }
     52 
     53 
     54 /* Extract the permitted capabilities of a given task */
     55 static int
     56 _get_task_permitted_caps(int pid, int tid, uint64_t *cap)
     57 {
     58     char path[64];
     59     char buff[1024];
     60     int  len;
     61     int  fd;
     62     int  result = -1;
     63     char*  perm;
     64     char*  end;
     65 
     66     /* Open task status file */
     67     snprintf(path, sizeof path, "/proc/%d/task/%d/status", pid, tid);
     68     fd = open(path, O_RDONLY);
     69     if (fd < 0) {
     70         D("Could not open %s: %s\n", path, strerror(errno));
     71         return -1;
     72     }
     73 
     74     /* Read its content, up to sizeof buff-1, then zero-terminate */
     75     do {
     76         len = read(fd, buff, sizeof buff-1);
     77     } while (len < 0 && errno == EINTR);
     78 
     79     if (len < 0) {
     80         D("Could not read %s: %s\n", path, strerror(errno));
     81         goto EXIT;
     82     }
     83 
     84     buff[len] = 0;
     85 
     86     /* Look for "CapPrm: " in it */
     87     perm = strstr(buff, "CapPrm:");
     88     if (perm == NULL) {
     89         D("Could not find CapPrm in %s!\n---- cut here ----\n%.*s\n----- cut here -----\n",
     90           path, len, buff);
     91         errno = EINVAL;
     92         goto EXIT;
     93     }
     94 
     95     /* Now read the hexadecimal value after 'CapPrm: ' */
     96     errno = 0;
     97     *cap = (uint64_t) strtoull(perm+8, &end, 16);
     98     if (errno == 0) {
     99         D("Found CapPerm of %lld in %s\n", *cap, path);
    100         result = 0;
    101     } else {
    102         D("Cannot read CapPerm from %s: '%.*s'\n", path, 24, perm);
    103     }
    104 EXIT:
    105     close(fd);
    106     return result;
    107 }
    108 
    109 td_err_e
    110 td_ta_new(struct ps_prochandle const * proc_handle, td_thragent_t ** agent_out)
    111 {
    112     td_thragent_t * agent;
    113 
    114     /* Platforms before Android 2.3 contain a system bug that prevents
    115      * gdbserver to attach to all threads in a target process when
    116      * it is run as the same userID than the target (works fine if
    117      * run as root).
    118      *
    119      * Due to the way gdbserver is coded, this makes gdbserver exit()
    120      * immediately (see linux_attach_lwp in linux-low.c). Even if we
    121      * modify the source code to not exit(), then signals will not
    122      * be properly rerouted to gdbserver, preventing breakpoints from
    123      * working correctly.
    124      *
    125      * The following code is here to test for this problematic condition.
    126      * If it is detected, we return TD_NOLIBTHREAD to indicate that there
    127      * are no threads to attach to (gdbserver will attach to the main thread
    128      * though).
    129      */
    130     do {
    131         char  path[64];
    132         DIR*  dir;
    133         struct dirent *entry;
    134         pid_t     my_pid = getpid();
    135         int       target_pid = proc_handle->pid;
    136         uint64_t  my_caps, tid_caps;
    137 
    138         D("Probing system for platform bug.\n");
    139 
    140         /* nothing to do if we run as root */
    141         if (geteuid() == 0) {
    142             D("Running as root, nothing to do.\n");
    143             break;
    144         }
    145 
    146         /* First, get our own permitted capabilities */
    147         if (_get_task_permitted_caps(my_pid, my_pid, &my_caps) < 0) {
    148             /* something is really fishy here */
    149             D("Could not get gdbserver permitted caps!\n");
    150             return TD_NOLIBTHREAD;
    151         }
    152 
    153         /* Now, for each thread in the target process, compare the
    154          * permitted capabilities set to our own. If they differ,
    155          * the thread attach will fail. Booo...
    156          */
    157         snprintf(path, sizeof path, "/proc/%d/task", target_pid);
    158         dir = opendir(path);
    159         if (!dir) {
    160             D("Could not open %s: %s\n", path, strerror(errno));
    161             break;
    162         }
    163         while ((entry = readdir(dir)) != NULL) {
    164             int  tid;
    165 
    166             if (entry->d_name[0] == '.')   /* skip . and .. */
    167                 continue;
    168 
    169             tid = atoi(entry->d_name);
    170             if (tid == 0)  /* should not happen - be safe */
    171                 continue;
    172 
    173             if (_get_task_permitted_caps(target_pid, tid, &tid_caps) < 0) {
    174                 /* again, something is fishy */
    175                 D("Could not get permitted caps for thread %d\n", tid);
    176                 closedir(dir);
    177                 return TD_NOLIBTHREAD;
    178             }
    179 
    180             if (tid_caps != my_caps) {
    181                 /* AAAARGH !! The permitted capabilities set differ. */
    182                 D("AAAAAH, Can't debug threads!\n");
    183                 fprintf(stderr, "Thread debugging is unsupported on this Android platform!\n");
    184                 closedir(dir);
    185                 return TD_NOLIBTHREAD;
    186             }
    187         }
    188         closedir(dir);
    189         D("Victory: We can debug theads!\n");
    190     } while (0);
    191 
    192     /* We now return to our regularly scheduled program */
    193 
    194     agent = (td_thragent_t *)malloc(sizeof(td_thragent_t));
    195     if (!agent) {
    196         return TD_MALLOC;
    197     }
    198 
    199     agent->pid = proc_handle->pid;
    200     *agent_out = agent;
    201 
    202     return TD_OK;
    203 }
    204 
    205 
    206 td_err_e
    207 td_ta_set_event(td_thragent_t const * agent, td_thr_events_t * events)
    208 {
    209     return TD_OK;
    210 }
    211 
    212 
    213 static td_thrhandle_t gEventMsgHandle;
    214 
    215 static int
    216 _event_getmsg_helper(td_thrhandle_t const * handle, void * bkpt_addr)
    217 {
    218     void * pc;
    219 
    220     pc = (void *)ptrace(PTRACE_PEEKUSR, handle->tid, (void *)60 /* r15/pc */, NULL);
    221 
    222     if (pc == bkpt_addr) {
    223         // The hook function takes the id of the new thread as it's first param,
    224         // so grab it from r0.
    225         gEventMsgHandle.pid = ptrace(PTRACE_PEEKUSR, handle->tid, (void *)0 /* r0 */, NULL);
    226         gEventMsgHandle.tid = gEventMsgHandle.pid;
    227         return 0x42;
    228     }
    229     return 0;
    230 }
    231 
    232 td_err_e
    233 td_ta_event_getmsg(td_thragent_t const * agent, td_event_msg_t * event)
    234 {
    235     td_err_e err;
    236     void * bkpt_addr;
    237 
    238     err = ps_pglobal_lookup(NULL, NULL, gSymbols[SYM_TD_CREATE], &bkpt_addr);
    239     if (err) {
    240         return err;
    241     }
    242 
    243     err = td_ta_thr_iter(agent, _event_getmsg_helper, bkpt_addr, 0, 0, NULL, 0);
    244     if (err != 0x42) {
    245         return TD_NOMSG;
    246     }
    247 
    248     event->event = TD_CREATE;
    249     event->th_p = &gEventMsgHandle; // Nasty hack, but it's the only way!
    250 
    251     return TD_OK;
    252 }
    253 
    254 
    255 td_err_e
    256 td_thr_get_info(td_thrhandle_t const * handle, td_thrinfo_t * info)
    257 {
    258     info->ti_tid = handle->tid;
    259     info->ti_lid = handle->tid; // Our pthreads uses kernel ids for tids
    260     info->ti_state = TD_THR_SLEEP; /* XXX this needs to be read from /proc/<pid>/task/<tid>.
    261                                       This is only used to see if the thread is a zombie or not */
    262     return TD_OK;
    263 }
    264 
    265 
    266 td_err_e
    267 td_thr_event_enable(td_thrhandle_t const * handle, td_event_e event)
    268 {
    269     // I don't think we need to do anything here...
    270     return TD_OK;
    271 }
    272 
    273 
    274 td_err_e
    275 td_ta_event_addr(td_thragent_t const * agent, td_event_e event, td_notify_t * notify_out)
    276 {
    277     int32_t err;
    278 
    279     /*
    280      * This is nasty, ps_pglobal_lookup is implemented in gdbserver and looks up
    281      * the symbol from it's cache, which is populated at start time with the
    282      * symbols returned from td_symbol_list via calls back to the host.
    283      */
    284 
    285     switch (event) {
    286         case TD_CREATE:
    287             err = ps_pglobal_lookup(NULL, NULL, gSymbols[SYM_TD_CREATE], &notify_out->u.bptaddr);
    288             if (err) {
    289                 return TD_NOEVENT;
    290             }
    291             return TD_OK;
    292     }
    293     return TD_NOEVENT;
    294 }
    295 
    296 
    297 td_err_e
    298 td_ta_thr_iter(td_thragent_t const * agent, td_thr_iter_f * func, void * cookie,
    299                td_thr_state_e state, int32_t prio, sigset_t * sigmask, uint32_t user_flags)
    300 {
    301     td_err_e err = TD_OK;
    302     char path[32];
    303     DIR * dir;
    304     struct dirent * entry;
    305     td_thrhandle_t handle;
    306 
    307     snprintf(path, sizeof(path), "/proc/%d/task/", agent->pid);
    308     dir = opendir(path);
    309     if (!dir) {
    310         return TD_NOEVENT;
    311     }
    312 
    313     handle.pid = agent->pid;
    314     while ((entry = readdir(dir)) != NULL) {
    315         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
    316             continue;
    317         }
    318         handle.tid = atoi(entry->d_name);
    319         err = func(&handle, cookie);
    320         if (err) {
    321             break;
    322         }
    323     }
    324 
    325     closedir(dir);
    326 
    327     return err;
    328 }
    329 
    330