GDBserver
/home/stan/gdb/src/gdb/gdbserver/thread-db.c
Go to the documentation of this file.
00001 /* Thread management interface, for the remote server for GDB.
00002    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00003 
00004    Contributed by MontaVista Software.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "server.h"
00022 
00023 #include "linux-low.h"
00024 
00025 extern int debug_threads;
00026 
00027 static int thread_db_use_events;
00028 
00029 #include "gdb_proc_service.h"
00030 #include "gdb_thread_db.h"
00031 #include "gdb_vecs.h"
00032 
00033 #ifndef USE_LIBTHREAD_DB_DIRECTLY
00034 #include <dlfcn.h>
00035 #endif
00036 
00037 #include <stdint.h>
00038 #include <limits.h>
00039 #include <ctype.h>
00040 
00041 struct thread_db
00042 {
00043   /* Structure that identifies the child process for the
00044      <proc_service.h> interface.  */
00045   struct ps_prochandle proc_handle;
00046 
00047   /* Connection to the libthread_db library.  */
00048   td_thragent_t *thread_agent;
00049 
00050   /* If this flag has been set, we've already asked GDB for all
00051      symbols we might need; assume symbol cache misses are
00052      failures.  */
00053   int all_symbols_looked_up;
00054 
00055 #ifndef USE_LIBTHREAD_DB_DIRECTLY
00056   /* Handle of the libthread_db from dlopen.  */
00057   void *handle;
00058 #endif
00059 
00060   /* Thread creation event breakpoint.  The code at this location in
00061      the child process will be called by the pthread library whenever
00062      a new thread is created.  By setting a special breakpoint at this
00063      location, GDB can detect when a new thread is created.  We obtain
00064      this location via the td_ta_event_addr call.  Note that if the
00065      running kernel supports tracing clones, then we don't need to use
00066      (and in fact don't use) this magic thread event breakpoint to
00067      learn about threads.  */
00068   struct breakpoint *td_create_bp;
00069 
00070   /* Addresses of libthread_db functions.  */
00071   td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta);
00072   td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
00073                                     td_event_msg_t *msg);
00074   td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
00075                                  td_thr_events_t *event);
00076   td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
00077                                   td_event_e event, td_notify_t *ptr);
00078   td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
00079                                    td_thrhandle_t *th);
00080   td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
00081                                  td_thrinfo_t *infop);
00082   td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
00083   td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
00084                                 td_thr_iter_f *callback, void *cbdata_p,
00085                                 td_thr_state_e state, int ti_pri,
00086                                 sigset_t *ti_sigmask_p,
00087                                 unsigned int ti_user_flags);
00088   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
00089                                      psaddr_t map_address,
00090                                      size_t offset, psaddr_t *address);
00091   const char ** (*td_symbol_list_p) (void);
00092 };
00093 
00094 static char *libthread_db_search_path;
00095 
00096 static int find_one_thread (ptid_t);
00097 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
00098 
00099 static const char *
00100 thread_db_err_str (td_err_e err)
00101 {
00102   static char buf[64];
00103 
00104   switch (err)
00105     {
00106     case TD_OK:
00107       return "generic 'call succeeded'";
00108     case TD_ERR:
00109       return "generic error";
00110     case TD_NOTHR:
00111       return "no thread to satisfy query";
00112     case TD_NOSV:
00113       return "no sync handle to satisfy query";
00114     case TD_NOLWP:
00115       return "no LWP to satisfy query";
00116     case TD_BADPH:
00117       return "invalid process handle";
00118     case TD_BADTH:
00119       return "invalid thread handle";
00120     case TD_BADSH:
00121       return "invalid synchronization handle";
00122     case TD_BADTA:
00123       return "invalid thread agent";
00124     case TD_BADKEY:
00125       return "invalid key";
00126     case TD_NOMSG:
00127       return "no event message for getmsg";
00128     case TD_NOFPREGS:
00129       return "FPU register set not available";
00130     case TD_NOLIBTHREAD:
00131       return "application not linked with libthread";
00132     case TD_NOEVENT:
00133       return "requested event is not supported";
00134     case TD_NOCAPAB:
00135       return "capability not available";
00136     case TD_DBERR:
00137       return "debugger service failed";
00138     case TD_NOAPLIC:
00139       return "operation not applicable to";
00140     case TD_NOTSD:
00141       return "no thread-specific data for this thread";
00142     case TD_MALLOC:
00143       return "malloc failed";
00144     case TD_PARTIALREG:
00145       return "only part of register set was written/read";
00146     case TD_NOXREGS:
00147       return "X register set not available for this thread";
00148 #ifdef HAVE_TD_VERSION
00149     case TD_VERSION:
00150       return "version mismatch between libthread_db and libpthread";
00151 #endif
00152     default:
00153       xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
00154       return buf;
00155     }
00156 }
00157 
00158 #if 0
00159 static char *
00160 thread_db_state_str (td_thr_state_e state)
00161 {
00162   static char buf[64];
00163 
00164   switch (state)
00165     {
00166     case TD_THR_STOPPED:
00167       return "stopped by debugger";
00168     case TD_THR_RUN:
00169       return "runnable";
00170     case TD_THR_ACTIVE:
00171       return "active";
00172     case TD_THR_ZOMBIE:
00173       return "zombie";
00174     case TD_THR_SLEEP:
00175       return "sleeping";
00176     case TD_THR_STOPPED_ASLEEP:
00177       return "stopped by debugger AND blocked";
00178     default:
00179       xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
00180       return buf;
00181     }
00182 }
00183 #endif
00184 
00185 static int
00186 thread_db_create_event (CORE_ADDR where)
00187 {
00188   td_event_msg_t msg;
00189   td_err_e err;
00190   struct lwp_info *lwp;
00191   struct thread_db *thread_db = current_process ()->private->thread_db;
00192 
00193   if (thread_db->td_ta_event_getmsg_p == NULL)
00194     fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
00195 
00196   if (debug_threads)
00197     fprintf (stderr, "Thread creation event.\n");
00198 
00199   /* FIXME: This assumes we don't get another event.
00200      In the LinuxThreads implementation, this is safe,
00201      because all events come from the manager thread
00202      (except for its own creation, of course).  */
00203   err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
00204   if (err != TD_OK)
00205     fprintf (stderr, "thread getmsg err: %s\n",
00206              thread_db_err_str (err));
00207 
00208   /* If we do not know about the main thread yet, this would be a good time to
00209      find it.  We need to do this to pick up the main thread before any newly
00210      created threads.  */
00211   lwp = get_thread_lwp (current_inferior);
00212   if (lwp->thread_known == 0)
00213     find_one_thread (lwp->head.id);
00214 
00215   /* msg.event == TD_EVENT_CREATE */
00216 
00217   find_new_threads_callback (msg.th_p, NULL);
00218 
00219   return 0;
00220 }
00221 
00222 static int
00223 thread_db_enable_reporting (void)
00224 {
00225   td_thr_events_t events;
00226   td_notify_t notify;
00227   td_err_e err;
00228   struct thread_db *thread_db = current_process ()->private->thread_db;
00229 
00230   if (thread_db->td_ta_set_event_p == NULL
00231       || thread_db->td_ta_event_addr_p == NULL
00232       || thread_db->td_ta_event_getmsg_p == NULL)
00233     /* This libthread_db is missing required support.  */
00234     return 0;
00235 
00236   /* Set the process wide mask saying which events we're interested in.  */
00237   td_event_emptyset (&events);
00238   td_event_addset (&events, TD_CREATE);
00239 
00240   err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
00241   if (err != TD_OK)
00242     {
00243       warning ("Unable to set global thread event mask: %s",
00244                thread_db_err_str (err));
00245       return 0;
00246     }
00247 
00248   /* Get address for thread creation breakpoint.  */
00249   err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
00250                                        &notify);
00251   if (err != TD_OK)
00252     {
00253       warning ("Unable to get location for thread creation breakpoint: %s",
00254                thread_db_err_str (err));
00255       return 0;
00256     }
00257   thread_db->td_create_bp
00258     = set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
00259                          thread_db_create_event);
00260 
00261   return 1;
00262 }
00263 
00264 static int
00265 find_one_thread (ptid_t ptid)
00266 {
00267   td_thrhandle_t th;
00268   td_thrinfo_t ti;
00269   td_err_e err;
00270   struct thread_info *inferior;
00271   struct lwp_info *lwp;
00272   struct thread_db *thread_db = current_process ()->private->thread_db;
00273   int lwpid = ptid_get_lwp (ptid);
00274 
00275   inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
00276   lwp = get_thread_lwp (inferior);
00277   if (lwp->thread_known)
00278     return 1;
00279 
00280   /* Get information about this thread.  */
00281   err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
00282   if (err != TD_OK)
00283     error ("Cannot get thread handle for LWP %d: %s",
00284            lwpid, thread_db_err_str (err));
00285 
00286   err = thread_db->td_thr_get_info_p (&th, &ti);
00287   if (err != TD_OK)
00288     error ("Cannot get thread info for LWP %d: %s",
00289            lwpid, thread_db_err_str (err));
00290 
00291   if (debug_threads)
00292     fprintf (stderr, "Found thread %ld (LWP %d)\n",
00293              ti.ti_tid, ti.ti_lid);
00294 
00295   if (lwpid != ti.ti_lid)
00296     {
00297       warning ("PID mismatch!  Expected %ld, got %ld",
00298                (long) lwpid, (long) ti.ti_lid);
00299       return 0;
00300     }
00301 
00302   if (thread_db_use_events)
00303     {
00304       err = thread_db->td_thr_event_enable_p (&th, 1);
00305       if (err != TD_OK)
00306         error ("Cannot enable thread event reporting for %d: %s",
00307                ti.ti_lid, thread_db_err_str (err));
00308     }
00309 
00310   /* If the new thread ID is zero, a final thread ID will be available
00311      later.  Do not enable thread debugging yet.  */
00312   if (ti.ti_tid == 0)
00313     return 0;
00314 
00315   lwp->thread_known = 1;
00316   lwp->th = th;
00317 
00318   return 1;
00319 }
00320 
00321 /* Attach a thread.  Return true on success.  */
00322 
00323 static int
00324 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
00325 {
00326   struct lwp_info *lwp;
00327 
00328   if (debug_threads)
00329     fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
00330              ti_p->ti_tid, ti_p->ti_lid);
00331   linux_attach_lwp (ti_p->ti_lid);
00332   lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
00333   if (lwp == NULL)
00334     {
00335       warning ("Could not attach to thread %ld (LWP %d)\n",
00336                ti_p->ti_tid, ti_p->ti_lid);
00337       return 0;
00338     }
00339 
00340   lwp->thread_known = 1;
00341   lwp->th = *th_p;
00342 
00343   if (thread_db_use_events)
00344     {
00345       td_err_e err;
00346       struct thread_db *thread_db = current_process ()->private->thread_db;
00347 
00348       err = thread_db->td_thr_event_enable_p (th_p, 1);
00349       if (err != TD_OK)
00350         error ("Cannot enable thread event reporting for %d: %s",
00351                ti_p->ti_lid, thread_db_err_str (err));
00352     }
00353 
00354   return 1;
00355 }
00356 
00357 /* Attach thread if we haven't seen it yet.
00358    Increment *COUNTER if we have attached a new thread.
00359    Return false on failure.  */
00360 
00361 static int
00362 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
00363                      int *counter)
00364 {
00365   struct lwp_info *lwp;
00366 
00367   lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
00368   if (lwp != NULL)
00369     return 1;
00370 
00371   if (!attach_thread (th_p, ti_p))
00372     return 0;
00373 
00374   if (counter != NULL)
00375     *counter += 1;
00376 
00377   return 1;
00378 }
00379 
00380 static int
00381 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
00382 {
00383   td_thrinfo_t ti;
00384   td_err_e err;
00385   struct thread_db *thread_db = current_process ()->private->thread_db;
00386 
00387   err = thread_db->td_thr_get_info_p (th_p, &ti);
00388   if (err != TD_OK)
00389     error ("Cannot get thread info: %s", thread_db_err_str (err));
00390 
00391   /* Check for zombies.  */
00392   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
00393     return 0;
00394 
00395   if (!maybe_attach_thread (th_p, &ti, (int *) data))
00396     {
00397       /* Terminate iteration early: we might be looking at stale data in
00398          the inferior.  The thread_db_find_new_threads will retry.  */
00399       return 1;
00400     }
00401 
00402   return 0;
00403 }
00404 
00405 static void
00406 thread_db_find_new_threads (void)
00407 {
00408   td_err_e err;
00409   ptid_t ptid = current_ptid;
00410   struct thread_db *thread_db = current_process ()->private->thread_db;
00411   int loop, iteration;
00412 
00413   /* This function is only called when we first initialize thread_db.
00414      First locate the initial thread.  If it is not ready for
00415      debugging yet, then stop.  */
00416   if (find_one_thread (ptid) == 0)
00417     return;
00418 
00419   /* Require 4 successive iterations which do not find any new threads.
00420      The 4 is a heuristic: there is an inherent race here, and I have
00421      seen that 2 iterations in a row are not always sufficient to
00422      "capture" all threads.  */
00423   for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
00424     {
00425       int new_thread_count = 0;
00426 
00427       /* Iterate over all user-space threads to discover new threads.  */
00428       err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
00429                                          find_new_threads_callback,
00430                                          &new_thread_count,
00431                                          TD_THR_ANY_STATE,
00432                                          TD_THR_LOWEST_PRIORITY,
00433                                          TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
00434       if (debug_threads)
00435         fprintf (stderr, "Found %d threads in iteration %d.\n",
00436                  new_thread_count, iteration);
00437 
00438       if (new_thread_count != 0)
00439         {
00440           /* Found new threads.  Restart iteration from beginning.  */
00441           loop = -1;
00442         }
00443     }
00444   if (err != TD_OK)
00445     error ("Cannot find new threads: %s", thread_db_err_str (err));
00446 }
00447 
00448 /* Cache all future symbols that thread_db might request.  We can not
00449    request symbols at arbitrary states in the remote protocol, only
00450    when the client tells us that new symbols are available.  So when
00451    we load the thread library, make sure to check the entire list.  */
00452 
00453 static void
00454 thread_db_look_up_symbols (void)
00455 {
00456   struct thread_db *thread_db = current_process ()->private->thread_db;
00457   const char **sym_list;
00458   CORE_ADDR unused;
00459 
00460   for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
00461     look_up_one_symbol (*sym_list, &unused, 1);
00462 
00463   /* We're not interested in any other libraries loaded after this
00464      point, only in symbols in libpthread.so.  */
00465   thread_db->all_symbols_looked_up = 1;
00466 }
00467 
00468 int
00469 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
00470 {
00471   struct thread_db *thread_db = current_process ()->private->thread_db;
00472   int may_ask_gdb = !thread_db->all_symbols_looked_up;
00473 
00474   /* If we've passed the call to thread_db_look_up_symbols, then
00475      anything not in the cache must not exist; we're not interested
00476      in any libraries loaded after that point, only in symbols in
00477      libpthread.so.  It might not be an appropriate time to look
00478      up a symbol, e.g. while we're trying to fetch registers.  */
00479   return look_up_one_symbol (name, addrp, may_ask_gdb);
00480 }
00481 
00482 int
00483 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
00484                            CORE_ADDR load_module, CORE_ADDR *address)
00485 {
00486   psaddr_t addr;
00487   td_err_e err;
00488   struct lwp_info *lwp;
00489   struct thread_info *saved_inferior;
00490   struct process_info *proc;
00491   struct thread_db *thread_db;
00492 
00493   proc = get_thread_process (thread);
00494   thread_db = proc->private->thread_db;
00495 
00496   /* If the thread layer is not (yet) initialized, fail.  */
00497   if (thread_db == NULL || !thread_db->all_symbols_looked_up)
00498     return TD_ERR;
00499 
00500   if (thread_db->td_thr_tls_get_addr_p == NULL)
00501     return -1;
00502 
00503   lwp = get_thread_lwp (thread);
00504   if (!lwp->thread_known)
00505     find_one_thread (lwp->head.id);
00506   if (!lwp->thread_known)
00507     return TD_NOTHR;
00508 
00509   saved_inferior = current_inferior;
00510   current_inferior = thread;
00511   /* Note the cast through uintptr_t: this interface only works if
00512      a target address fits in a psaddr_t, which is a host pointer.
00513      So a 32-bit debugger can not access 64-bit TLS through this.  */
00514   err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
00515                                           (psaddr_t) (uintptr_t) load_module,
00516                                           offset, &addr);
00517   current_inferior = saved_inferior;
00518   if (err == TD_OK)
00519     {
00520       *address = (CORE_ADDR) (uintptr_t) addr;
00521       return 0;
00522     }
00523   else
00524     return err;
00525 }
00526 
00527 #ifdef USE_LIBTHREAD_DB_DIRECTLY
00528 
00529 static int
00530 thread_db_load_search (void)
00531 {
00532   td_err_e err;
00533   struct thread_db *tdb;
00534   struct process_info *proc = current_process ();
00535 
00536   if (proc->private->thread_db != NULL)
00537     fatal ("unexpected: proc->private->thread_db != NULL");
00538 
00539   tdb = xcalloc (1, sizeof (*tdb));
00540   proc->private->thread_db = tdb;
00541 
00542   tdb->td_ta_new_p = &td_ta_new;
00543 
00544   /* Attempt to open a connection to the thread library.  */
00545   err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
00546   if (err != TD_OK)
00547     {
00548       if (debug_threads)
00549         fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
00550       free (tdb);
00551       proc->private->thread_db = NULL;
00552       return 0;
00553     }
00554 
00555   tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
00556   tdb->td_thr_get_info_p = &td_thr_get_info;
00557   tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
00558   tdb->td_symbol_list_p = &td_symbol_list;
00559 
00560   /* This is required only when thread_db_use_events is on.  */
00561   tdb->td_thr_event_enable_p = &td_thr_event_enable;
00562 
00563   /* These are not essential.  */
00564   tdb->td_ta_event_addr_p = &td_ta_event_addr;
00565   tdb->td_ta_set_event_p = &td_ta_set_event;
00566   tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
00567   tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
00568 
00569   return 1;
00570 }
00571 
00572 #else
00573 
00574 static int
00575 try_thread_db_load_1 (void *handle)
00576 {
00577   td_err_e err;
00578   struct thread_db *tdb;
00579   struct process_info *proc = current_process ();
00580 
00581   if (proc->private->thread_db != NULL)
00582     fatal ("unexpected: proc->private->thread_db != NULL");
00583 
00584   tdb = xcalloc (1, sizeof (*tdb));
00585   proc->private->thread_db = tdb;
00586 
00587   tdb->handle = handle;
00588 
00589   /* Initialize pointers to the dynamic library functions we will use.
00590      Essential functions first.  */
00591 
00592 #define CHK(required, a)                                        \
00593   do                                                            \
00594     {                                                           \
00595       if ((a) == NULL)                                          \
00596         {                                                       \
00597           if (debug_threads)                                    \
00598             fprintf (stderr, "dlsym: %s\n", dlerror ());        \
00599           if (required)                                         \
00600             {                                                   \
00601               free (tdb);                                       \
00602               proc->private->thread_db = NULL;                  \
00603               return 0;                                         \
00604             }                                                   \
00605         }                                                       \
00606     }                                                           \
00607   while (0)
00608 
00609   CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new"));
00610 
00611   /* Attempt to open a connection to the thread library.  */
00612   err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
00613   if (err != TD_OK)
00614     {
00615       if (debug_threads)
00616         fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
00617       free (tdb);
00618       proc->private->thread_db = NULL;
00619       return 0;
00620     }
00621 
00622   CHK (1, tdb->td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
00623   CHK (1, tdb->td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
00624   CHK (1, tdb->td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
00625   CHK (1, tdb->td_symbol_list_p = dlsym (handle, "td_symbol_list"));
00626 
00627   /* This is required only when thread_db_use_events is on.  */
00628   CHK (thread_db_use_events,
00629        tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
00630 
00631   /* These are not essential.  */
00632   CHK (0, tdb->td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
00633   CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
00634   CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
00635   CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
00636 
00637 #undef CHK
00638 
00639   return 1;
00640 }
00641 
00642 #ifdef HAVE_DLADDR
00643 
00644 /* Lookup a library in which given symbol resides.
00645    Note: this is looking in the GDBSERVER process, not in the inferior.
00646    Returns library name, or NULL.  */
00647 
00648 static const char *
00649 dladdr_to_soname (const void *addr)
00650 {
00651   Dl_info info;
00652 
00653   if (dladdr (addr, &info) != 0)
00654     return info.dli_fname;
00655   return NULL;
00656 }
00657 
00658 #endif
00659 
00660 static int
00661 try_thread_db_load (const char *library)
00662 {
00663   void *handle;
00664 
00665   if (debug_threads)
00666     fprintf (stderr, "Trying host libthread_db library: %s.\n",
00667              library);
00668   handle = dlopen (library, RTLD_NOW);
00669   if (handle == NULL)
00670     {
00671       if (debug_threads)
00672         fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
00673       return 0;
00674     }
00675 
00676 #ifdef HAVE_DLADDR
00677   if (debug_threads && strchr (library, '/') == NULL)
00678     {
00679       void *td_init;
00680 
00681       td_init = dlsym (handle, "td_init");
00682       if (td_init != NULL)
00683         {
00684           const char *const libpath = dladdr_to_soname (td_init);
00685 
00686           if (libpath != NULL)
00687             fprintf (stderr, "Host %s resolved to: %s.\n",
00688                      library, libpath);
00689         }
00690     }
00691 #endif
00692 
00693   if (try_thread_db_load_1 (handle))
00694     return 1;
00695 
00696   /* This library "refused" to work on current inferior.  */
00697   dlclose (handle);
00698   return 0;
00699 }
00700 
00701 /* Handle $sdir in libthread-db-search-path.
00702    Look for libthread_db in the system dirs, or wherever a plain
00703    dlopen(file_without_path) will look.
00704    The result is true for success.  */
00705 
00706 static int
00707 try_thread_db_load_from_sdir (void)
00708 {
00709   return try_thread_db_load (LIBTHREAD_DB_SO);
00710 }
00711 
00712 /* Try to load libthread_db from directory DIR of length DIR_LEN.
00713    The result is true for success.  */
00714 
00715 static int
00716 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
00717 {
00718   char path[PATH_MAX];
00719 
00720   if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
00721     {
00722       char *cp = xmalloc (dir_len + 1);
00723 
00724       memcpy (cp, dir, dir_len);
00725       cp[dir_len] = '\0';
00726       warning (_("libthread-db-search-path component too long,"
00727                  " ignored: %s."), cp);
00728       free (cp);
00729       return 0;
00730     }
00731 
00732   memcpy (path, dir, dir_len);
00733   path[dir_len] = '/';
00734   strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
00735   return try_thread_db_load (path);
00736 }
00737 
00738 /* Search libthread_db_search_path for libthread_db which "agrees"
00739    to work on current inferior.
00740    The result is true for success.  */
00741 
00742 static int
00743 thread_db_load_search (void)
00744 {
00745   VEC (char_ptr) *dir_vec;
00746   char *this_dir;
00747   int i, rc = 0;
00748 
00749   if (libthread_db_search_path == NULL)
00750     libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
00751 
00752   dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
00753 
00754   for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
00755     {
00756       const int pdir_len = sizeof ("$pdir") - 1;
00757       size_t this_dir_len;
00758 
00759       this_dir_len = strlen (this_dir);
00760 
00761       if (strncmp (this_dir, "$pdir", pdir_len) == 0
00762           && (this_dir[pdir_len] == '\0'
00763               || this_dir[pdir_len] == '/'))
00764         {
00765           /* We don't maintain a list of loaded libraries so we don't know
00766              where libpthread lives.  We *could* fetch the info, but we don't
00767              do that yet.  Ignore it.  */
00768         }
00769       else if (strcmp (this_dir, "$sdir") == 0)
00770         {
00771           if (try_thread_db_load_from_sdir ())
00772             {
00773               rc = 1;
00774               break;
00775             }
00776         }
00777       else
00778         {
00779           if (try_thread_db_load_from_dir (this_dir, this_dir_len))
00780             {
00781               rc = 1;
00782               break;
00783             }
00784         }
00785     }
00786 
00787   free_char_ptr_vec (dir_vec);
00788   if (debug_threads)
00789     fprintf (stderr, "thread_db_load_search returning %d\n", rc);
00790   return rc;
00791 }
00792 
00793 #endif  /* USE_LIBTHREAD_DB_DIRECTLY */
00794 
00795 int
00796 thread_db_init (int use_events)
00797 {
00798   struct process_info *proc = current_process ();
00799 
00800   /* FIXME drow/2004-10-16: This is the "overall process ID", which
00801      GNU/Linux calls tgid, "thread group ID".  When we support
00802      attaching to threads, the original thread may not be the correct
00803      thread.  We would have to get the process ID from /proc for NPTL.
00804      For LinuxThreads we could do something similar: follow the chain
00805      of parent processes until we find the highest one we're attached
00806      to, and use its tgid.
00807 
00808      This isn't the only place in gdbserver that assumes that the first
00809      process in the list is the thread group leader.  */
00810 
00811   thread_db_use_events = use_events;
00812 
00813   if (thread_db_load_search ())
00814     {
00815       if (use_events && thread_db_enable_reporting () == 0)
00816         {
00817           /* Keep trying; maybe event reporting will work later.  */
00818           thread_db_mourn (proc);
00819           return 0;
00820         }
00821       thread_db_find_new_threads ();
00822       thread_db_look_up_symbols ();
00823       return 1;
00824     }
00825 
00826   return 0;
00827 }
00828 
00829 static int
00830 any_thread_of (struct inferior_list_entry *entry, void *args)
00831 {
00832   int *pid_p = args;
00833 
00834   if (ptid_get_pid (entry->id) == *pid_p)
00835     return 1;
00836 
00837   return 0;
00838 }
00839 
00840 static void
00841 switch_to_process (struct process_info *proc)
00842 {
00843   int pid = pid_of (proc);
00844 
00845   current_inferior =
00846     (struct thread_info *) find_inferior (&all_threads,
00847                                           any_thread_of, &pid);
00848 }
00849 
00850 /* Disconnect from libthread_db and free resources.  */
00851 
00852 static void
00853 disable_thread_event_reporting (struct process_info *proc)
00854 {
00855   struct thread_db *thread_db = proc->private->thread_db;
00856   if (thread_db)
00857     {
00858       td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
00859                                        td_thr_events_t *event);
00860 
00861 #ifndef USE_LIBTHREAD_DB_DIRECTLY
00862       td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
00863 #else
00864       td_ta_clear_event_p = &td_ta_clear_event;
00865 #endif
00866 
00867       if (td_ta_clear_event_p != NULL)
00868         {
00869           struct thread_info *saved_inferior = current_inferior;
00870           td_thr_events_t events;
00871 
00872           switch_to_process (proc);
00873 
00874           /* Set the process wide mask saying we aren't interested
00875              in any events anymore.  */
00876           td_event_fillset (&events);
00877           (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
00878 
00879           current_inferior = saved_inferior;
00880         }
00881     }
00882 }
00883 
00884 static void
00885 remove_thread_event_breakpoints (struct process_info *proc)
00886 {
00887   struct thread_db *thread_db = proc->private->thread_db;
00888 
00889   if (thread_db->td_create_bp != NULL)
00890     {
00891       struct thread_info *saved_inferior = current_inferior;
00892 
00893       switch_to_process (proc);
00894 
00895       delete_breakpoint (thread_db->td_create_bp);
00896       thread_db->td_create_bp = NULL;
00897 
00898       current_inferior = saved_inferior;
00899     }
00900 }
00901 
00902 void
00903 thread_db_detach (struct process_info *proc)
00904 {
00905   struct thread_db *thread_db = proc->private->thread_db;
00906 
00907   if (thread_db)
00908     {
00909       disable_thread_event_reporting (proc);
00910       remove_thread_event_breakpoints (proc);
00911     }
00912 }
00913 
00914 /* Disconnect from libthread_db and free resources.  */
00915 
00916 void
00917 thread_db_mourn (struct process_info *proc)
00918 {
00919   struct thread_db *thread_db = proc->private->thread_db;
00920   if (thread_db)
00921     {
00922       td_err_e (*td_ta_delete_p) (td_thragent_t *);
00923 
00924 #ifndef USE_LIBTHREAD_DB_DIRECTLY
00925       td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
00926 #else
00927       td_ta_delete_p = &td_ta_delete;
00928 #endif
00929 
00930       if (td_ta_delete_p != NULL)
00931         (*td_ta_delete_p) (thread_db->thread_agent);
00932 
00933 #ifndef USE_LIBTHREAD_DB_DIRECTLY
00934       dlclose (thread_db->handle);
00935 #endif  /* USE_LIBTHREAD_DB_DIRECTLY  */
00936 
00937       free (thread_db);
00938       proc->private->thread_db = NULL;
00939     }
00940 }
00941 
00942 /* Handle "set libthread-db-search-path" monitor command and return 1.
00943    For any other command, return 0.  */
00944 
00945 int
00946 thread_db_handle_monitor_command (char *mon)
00947 {
00948   const char *cmd = "set libthread-db-search-path";
00949   size_t cmd_len = strlen (cmd);
00950 
00951   if (strncmp (mon, cmd, cmd_len) == 0
00952       && (mon[cmd_len] == '\0'
00953           || mon[cmd_len] == ' '))
00954     {
00955       const char *cp = mon + cmd_len;
00956 
00957       if (libthread_db_search_path != NULL)
00958         free (libthread_db_search_path);
00959 
00960       /* Skip leading space (if any).  */
00961       while (isspace (*cp))
00962         ++cp;
00963 
00964       if (*cp == '\0')
00965         cp = LIBTHREAD_DB_SEARCH_PATH;
00966       libthread_db_search_path = xstrdup (cp);
00967 
00968       monitor_output ("libthread-db-search-path set to `");
00969       monitor_output (libthread_db_search_path);
00970       monitor_output ("'\n");
00971       return 1;
00972     }
00973 
00974   /* Tell server.c to perform default processing.  */
00975   return 0;
00976 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines