GDB (API)
/home/stan/gdb/src/gdb/linux-thread-db.c
Go to the documentation of this file.
00001 /* libthread_db assisted debugging support, generic parts.
00002 
00003    Copyright (C) 1999-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include "gdb_assert.h"
00023 #include <dlfcn.h>
00024 #include "gdb_proc_service.h"
00025 #include "gdb_thread_db.h"
00026 #include "gdb_vecs.h"
00027 #include "bfd.h"
00028 #include "command.h"
00029 #include "exceptions.h"
00030 #include "gdbcmd.h"
00031 #include "gdbthread.h"
00032 #include "inferior.h"
00033 #include "symfile.h"
00034 #include "objfiles.h"
00035 #include "target.h"
00036 #include "regcache.h"
00037 #include "solib.h"
00038 #include "solib-svr4.h"
00039 #include "gdbcore.h"
00040 #include "observer.h"
00041 #include "linux-nat.h"
00042 #include "linux-procfs.h"
00043 #include "linux-osdata.h"
00044 #include "auto-load.h"
00045 #include "cli/cli-utils.h"
00046 
00047 #include <signal.h>
00048 #include <ctype.h>
00049 
00050 /* GNU/Linux libthread_db support.
00051 
00052    libthread_db is a library, provided along with libpthread.so, which
00053    exposes the internals of the thread library to a debugger.  It
00054    allows GDB to find existing threads, new threads as they are
00055    created, thread IDs (usually, the result of pthread_self), and
00056    thread-local variables.
00057 
00058    The libthread_db interface originates on Solaris, where it is
00059    both more powerful and more complicated.  This implementation
00060    only works for LinuxThreads and NPTL, the two glibc threading
00061    libraries.  It assumes that each thread is permanently assigned
00062    to a single light-weight process (LWP).
00063 
00064    libthread_db-specific information is stored in the "private" field
00065    of struct thread_info.  When the field is NULL we do not yet have
00066    information about the new thread; this could be temporary (created,
00067    but the thread library's data structures do not reflect it yet)
00068    or permanent (created using clone instead of pthread_create).
00069 
00070    Process IDs managed by linux-thread-db.c match those used by
00071    linux-nat.c: a common PID for all processes, an LWP ID for each
00072    thread, and no TID.  We save the TID in private.  Keeping it out
00073    of the ptid_t prevents thread IDs changing when libpthread is
00074    loaded or unloaded.  */
00075 
00076 static char *libthread_db_search_path;
00077 
00078 /* Set to non-zero if thread_db auto-loading is enabled
00079    by the "set auto-load libthread-db" command.  */
00080 static int auto_load_thread_db = 1;
00081 
00082 /* "show" command for the auto_load_thread_db configuration variable.  */
00083 
00084 static void
00085 show_auto_load_thread_db (struct ui_file *file, int from_tty,
00086                           struct cmd_list_element *c, const char *value)
00087 {
00088   fprintf_filtered (file, _("Auto-loading of inferior specific libthread_db "
00089                             "is %s.\n"),
00090                     value);
00091 }
00092 
00093 static void
00094 set_libthread_db_search_path (char *ignored, int from_tty,
00095                               struct cmd_list_element *c)
00096 {
00097   if (*libthread_db_search_path == '\0')
00098     {
00099       xfree (libthread_db_search_path);
00100       libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
00101     }
00102 }
00103 
00104 /* If non-zero, print details of libthread_db processing.  */
00105 
00106 static unsigned int libthread_db_debug;
00107 
00108 static void
00109 show_libthread_db_debug (struct ui_file *file, int from_tty,
00110                          struct cmd_list_element *c, const char *value)
00111 {
00112   fprintf_filtered (file, _("libthread-db debugging is %s.\n"), value);
00113 }
00114 
00115 /* If we're running on GNU/Linux, we must explicitly attach to any new
00116    threads.  */
00117 
00118 /* This module's target vector.  */
00119 static struct target_ops thread_db_ops;
00120 
00121 /* Non-zero if we have determined the signals used by the threads
00122    library.  */
00123 static int thread_signals;
00124 static sigset_t thread_stop_set;
00125 static sigset_t thread_print_set;
00126 
00127 struct thread_db_info
00128 {
00129   struct thread_db_info *next;
00130 
00131   /* Process id this object refers to.  */
00132   int pid;
00133 
00134   /* Handle from dlopen for libthread_db.so.  */
00135   void *handle;
00136 
00137   /* Absolute pathname from gdb_realpath to disk file used for dlopen-ing
00138      HANDLE.  It may be NULL for system library.  */
00139   char *filename;
00140 
00141   /* Structure that identifies the child process for the
00142      <proc_service.h> interface.  */
00143   struct ps_prochandle proc_handle;
00144 
00145   /* Connection to the libthread_db library.  */
00146   td_thragent_t *thread_agent;
00147 
00148   /* True if we need to apply the workaround for glibc/BZ5983.  When
00149      we catch a PTRACE_O_TRACEFORK, and go query the child's thread
00150      list, nptl_db returns the parent's threads in addition to the new
00151      (single) child thread.  If this flag is set, we do extra work to
00152      be able to ignore such stale entries.  */
00153   int need_stale_parent_threads_check;
00154 
00155   /* Location of the thread creation event breakpoint.  The code at
00156      this location in the child process will be called by the pthread
00157      library whenever a new thread is created.  By setting a special
00158      breakpoint at this location, GDB can detect when a new thread is
00159      created.  We obtain this location via the td_ta_event_addr
00160      call.  */
00161   CORE_ADDR td_create_bp_addr;
00162 
00163   /* Location of the thread death event breakpoint.  */
00164   CORE_ADDR td_death_bp_addr;
00165 
00166   /* Pointers to the libthread_db functions.  */
00167 
00168   td_err_e (*td_init_p) (void);
00169 
00170   td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
00171                                 td_thragent_t **ta);
00172   td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
00173                                   td_thrhandle_t *__th);
00174   td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
00175                                    lwpid_t lwpid, td_thrhandle_t *th);
00176   td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
00177                                 td_thr_iter_f *callback, void *cbdata_p,
00178                                 td_thr_state_e state, int ti_pri,
00179                                 sigset_t *ti_sigmask_p,
00180                                 unsigned int ti_user_flags);
00181   td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
00182                                   td_event_e event, td_notify_t *ptr);
00183   td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
00184                                  td_thr_events_t *event);
00185   td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
00186                                    td_thr_events_t *event);
00187   td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
00188                                     td_event_msg_t *msg);
00189 
00190   td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
00191   td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
00192                                  td_thrinfo_t *infop);
00193   td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
00194                                      int event);
00195 
00196   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
00197                                      psaddr_t map_address,
00198                                      size_t offset, psaddr_t *address);
00199 };
00200 
00201 /* List of known processes using thread_db, and the required
00202    bookkeeping.  */
00203 struct thread_db_info *thread_db_list;
00204 
00205 static void thread_db_find_new_threads_1 (ptid_t ptid);
00206 static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
00207 
00208 /* Add the current inferior to the list of processes using libpthread.
00209    Return a pointer to the newly allocated object that was added to
00210    THREAD_DB_LIST.  HANDLE is the handle returned by dlopen'ing
00211    LIBTHREAD_DB_SO.  */
00212 
00213 static struct thread_db_info *
00214 add_thread_db_info (void *handle)
00215 {
00216   struct thread_db_info *info;
00217 
00218   info = xcalloc (1, sizeof (*info));
00219   info->pid = ptid_get_pid (inferior_ptid);
00220   info->handle = handle;
00221 
00222   /* The workaround works by reading from /proc/pid/status, so it is
00223      disabled for core files.  */
00224   if (target_has_execution)
00225     info->need_stale_parent_threads_check = 1;
00226 
00227   info->next = thread_db_list;
00228   thread_db_list = info;
00229 
00230   return info;
00231 }
00232 
00233 /* Return the thread_db_info object representing the bookkeeping
00234    related to process PID, if any; NULL otherwise.  */
00235 
00236 static struct thread_db_info *
00237 get_thread_db_info (int pid)
00238 {
00239   struct thread_db_info *info;
00240 
00241   for (info = thread_db_list; info; info = info->next)
00242     if (pid == info->pid)
00243       return info;
00244 
00245   return NULL;
00246 }
00247 
00248 /* When PID has exited or has been detached, we no longer want to keep
00249    track of it as using libpthread.  Call this function to discard
00250    thread_db related info related to PID.  Note that this closes
00251    LIBTHREAD_DB_SO's dlopen'ed handle.  */
00252 
00253 static void
00254 delete_thread_db_info (int pid)
00255 {
00256   struct thread_db_info *info, *info_prev;
00257 
00258   info_prev = NULL;
00259 
00260   for (info = thread_db_list; info; info_prev = info, info = info->next)
00261     if (pid == info->pid)
00262       break;
00263 
00264   if (info == NULL)
00265     return;
00266 
00267   if (info->handle != NULL)
00268     dlclose (info->handle);
00269 
00270   xfree (info->filename);
00271 
00272   if (info_prev)
00273     info_prev->next = info->next;
00274   else
00275     thread_db_list = info->next;
00276 
00277   xfree (info);
00278 }
00279 
00280 /* Prototypes for local functions.  */
00281 static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
00282                           const td_thrinfo_t *ti_p);
00283 static void detach_thread (ptid_t ptid);
00284 
00285 
00286 /* Use "struct private_thread_info" to cache thread state.  This is
00287    a substantial optimization.  */
00288 
00289 struct private_thread_info
00290 {
00291   /* Flag set when we see a TD_DEATH event for this thread.  */
00292   unsigned int dying:1;
00293 
00294   /* Cached thread state.  */
00295   td_thrhandle_t th;
00296   thread_t tid;
00297 };
00298 
00299 
00300 static char *
00301 thread_db_err_str (td_err_e err)
00302 {
00303   static char buf[64];
00304 
00305   switch (err)
00306     {
00307     case TD_OK:
00308       return "generic 'call succeeded'";
00309     case TD_ERR:
00310       return "generic error";
00311     case TD_NOTHR:
00312       return "no thread to satisfy query";
00313     case TD_NOSV:
00314       return "no sync handle to satisfy query";
00315     case TD_NOLWP:
00316       return "no LWP to satisfy query";
00317     case TD_BADPH:
00318       return "invalid process handle";
00319     case TD_BADTH:
00320       return "invalid thread handle";
00321     case TD_BADSH:
00322       return "invalid synchronization handle";
00323     case TD_BADTA:
00324       return "invalid thread agent";
00325     case TD_BADKEY:
00326       return "invalid key";
00327     case TD_NOMSG:
00328       return "no event message for getmsg";
00329     case TD_NOFPREGS:
00330       return "FPU register set not available";
00331     case TD_NOLIBTHREAD:
00332       return "application not linked with libthread";
00333     case TD_NOEVENT:
00334       return "requested event is not supported";
00335     case TD_NOCAPAB:
00336       return "capability not available";
00337     case TD_DBERR:
00338       return "debugger service failed";
00339     case TD_NOAPLIC:
00340       return "operation not applicable to";
00341     case TD_NOTSD:
00342       return "no thread-specific data for this thread";
00343     case TD_MALLOC:
00344       return "malloc failed";
00345     case TD_PARTIALREG:
00346       return "only part of register set was written/read";
00347     case TD_NOXREGS:
00348       return "X register set not available for this thread";
00349 #ifdef THREAD_DB_HAS_TD_NOTALLOC
00350     case TD_NOTALLOC:
00351       return "thread has not yet allocated TLS for given module";
00352 #endif
00353 #ifdef THREAD_DB_HAS_TD_VERSION
00354     case TD_VERSION:
00355       return "versions of libpthread and libthread_db do not match";
00356 #endif
00357 #ifdef THREAD_DB_HAS_TD_NOTLS
00358     case TD_NOTLS:
00359       return "there is no TLS segment in the given module";
00360 #endif
00361     default:
00362       snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
00363       return buf;
00364     }
00365 }
00366 
00367 /* Return 1 if any threads have been registered.  There may be none if
00368    the threading library is not fully initialized yet.  */
00369 
00370 static int
00371 have_threads_callback (struct thread_info *thread, void *args)
00372 {
00373   int pid = * (int *) args;
00374 
00375   if (ptid_get_pid (thread->ptid) != pid)
00376     return 0;
00377 
00378   return thread->private != NULL;
00379 }
00380 
00381 static int
00382 have_threads (ptid_t ptid)
00383 {
00384   int pid = ptid_get_pid (ptid);
00385 
00386   return iterate_over_threads (have_threads_callback, &pid) != NULL;
00387 }
00388 
00389 struct thread_get_info_inout
00390 {
00391   struct thread_info *thread_info;
00392   struct thread_db_info *thread_db_info;
00393 };
00394 
00395 /* A callback function for td_ta_thr_iter, which we use to map all
00396    threads to LWPs.
00397 
00398    THP is a handle to the current thread; if INFOP is not NULL, the
00399    struct thread_info associated with this thread is returned in
00400    *INFOP.
00401 
00402    If the thread is a zombie, TD_THR_ZOMBIE is returned.  Otherwise,
00403    zero is returned to indicate success.  */
00404 
00405 static int
00406 thread_get_info_callback (const td_thrhandle_t *thp, void *argp)
00407 {
00408   td_thrinfo_t ti;
00409   td_err_e err;
00410   ptid_t thread_ptid;
00411   struct thread_get_info_inout *inout;
00412   struct thread_db_info *info;
00413 
00414   inout = argp;
00415   info = inout->thread_db_info;
00416 
00417   err = info->td_thr_get_info_p (thp, &ti);
00418   if (err != TD_OK)
00419     error (_("thread_get_info_callback: cannot get thread info: %s"),
00420            thread_db_err_str (err));
00421 
00422   /* Fill the cache.  */
00423   thread_ptid = ptid_build (info->pid, ti.ti_lid, 0);
00424   inout->thread_info = find_thread_ptid (thread_ptid);
00425 
00426   if (inout->thread_info == NULL)
00427     {
00428       /* New thread.  Attach to it now (why wait?).  */
00429       if (!have_threads (thread_ptid))
00430         thread_db_find_new_threads_1 (thread_ptid);
00431       else
00432         attach_thread (thread_ptid, thp, &ti);
00433       inout->thread_info = find_thread_ptid (thread_ptid);
00434       gdb_assert (inout->thread_info != NULL);
00435     }
00436 
00437   return 0;
00438 }
00439 
00440 /* Fetch the user-level thread id of PTID.  */
00441 
00442 static void
00443 thread_from_lwp (ptid_t ptid)
00444 {
00445   td_thrhandle_t th;
00446   td_err_e err;
00447   struct thread_db_info *info;
00448   struct thread_get_info_inout io = {0};
00449 
00450   /* Just in case td_ta_map_lwp2thr doesn't initialize it completely.  */
00451   th.th_unique = 0;
00452 
00453   /* This ptid comes from linux-nat.c, which should always fill in the
00454      LWP.  */
00455   gdb_assert (ptid_get_lwp (ptid) != 0);
00456 
00457   info = get_thread_db_info (ptid_get_pid (ptid));
00458 
00459   /* Access an lwp we know is stopped.  */
00460   info->proc_handle.ptid = ptid;
00461   err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
00462                                    &th);
00463   if (err != TD_OK)
00464     error (_("Cannot find user-level thread for LWP %ld: %s"),
00465            ptid_get_lwp (ptid), thread_db_err_str (err));
00466 
00467   /* Long-winded way of fetching the thread info.  */
00468   io.thread_db_info = info;
00469   io.thread_info = NULL;
00470   thread_get_info_callback (&th, &io);
00471 }
00472 
00473 
00474 /* Attach to lwp PTID, doing whatever else is required to have this
00475    LWP under the debugger's control --- e.g., enabling event
00476    reporting.  Returns true on success.  */
00477 int
00478 thread_db_attach_lwp (ptid_t ptid)
00479 {
00480   td_thrhandle_t th;
00481   td_thrinfo_t ti;
00482   td_err_e err;
00483   struct thread_db_info *info;
00484 
00485   info = get_thread_db_info (ptid_get_pid (ptid));
00486 
00487   if (info == NULL)
00488     return 0;
00489 
00490   /* This ptid comes from linux-nat.c, which should always fill in the
00491      LWP.  */
00492   gdb_assert (ptid_get_lwp (ptid) != 0);
00493 
00494   /* Access an lwp we know is stopped.  */
00495   info->proc_handle.ptid = ptid;
00496 
00497   /* If we have only looked at the first thread before libpthread was
00498      initialized, we may not know its thread ID yet.  Make sure we do
00499      before we add another thread to the list.  */
00500   if (!have_threads (ptid))
00501     thread_db_find_new_threads_1 (ptid);
00502 
00503   err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
00504                                    &th);
00505   if (err != TD_OK)
00506     /* Cannot find user-level thread.  */
00507     return 0;
00508 
00509   err = info->td_thr_get_info_p (&th, &ti);
00510   if (err != TD_OK)
00511     {
00512       warning (_("Cannot get thread info: %s"), thread_db_err_str (err));
00513       return 0;
00514     }
00515 
00516   attach_thread (ptid, &th, &ti);
00517   return 1;
00518 }
00519 
00520 static void *
00521 verbose_dlsym (void *handle, const char *name)
00522 {
00523   void *sym = dlsym (handle, name);
00524   if (sym == NULL)
00525     warning (_("Symbol \"%s\" not found in libthread_db: %s"),
00526              name, dlerror ());
00527   return sym;
00528 }
00529 
00530 static td_err_e
00531 enable_thread_event (int event, CORE_ADDR *bp)
00532 {
00533   td_notify_t notify;
00534   td_err_e err;
00535   struct thread_db_info *info;
00536 
00537   info = get_thread_db_info (ptid_get_pid (inferior_ptid));
00538 
00539   /* Access an lwp we know is stopped.  */
00540   info->proc_handle.ptid = inferior_ptid;
00541 
00542   /* Get the breakpoint address for thread EVENT.  */
00543   err = info->td_ta_event_addr_p (info->thread_agent, event, &notify);
00544   if (err != TD_OK)
00545     return err;
00546 
00547   /* Set up the breakpoint.  */
00548   gdb_assert (exec_bfd);
00549   (*bp) = (gdbarch_convert_from_func_ptr_addr
00550            (target_gdbarch (),
00551             /* Do proper sign extension for the target.  */
00552             (bfd_get_sign_extend_vma (exec_bfd) > 0
00553              ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
00554              : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
00555             &current_target));
00556   create_thread_event_breakpoint (target_gdbarch (), *bp);
00557 
00558   return TD_OK;
00559 }
00560 
00561 /* Verify inferior's '\0'-terminated symbol VER_SYMBOL starts with "%d.%d" and
00562    return 1 if this version is lower (and not equal) to
00563    VER_MAJOR_MIN.VER_MINOR_MIN.  Return 0 in all other cases.  */
00564 
00565 static int
00566 inferior_has_bug (const char *ver_symbol, int ver_major_min, int ver_minor_min)
00567 {
00568   struct minimal_symbol *version_msym;
00569   CORE_ADDR version_addr;
00570   char *version;
00571   int err, got, retval = 0;
00572 
00573   version_msym = lookup_minimal_symbol (ver_symbol, NULL, NULL);
00574   if (version_msym == NULL)
00575     return 0;
00576 
00577   version_addr = SYMBOL_VALUE_ADDRESS (version_msym);
00578   got = target_read_string (version_addr, &version, 32, &err);
00579   if (err == 0 && memchr (version, 0, got) == &version[got -1])
00580     {
00581       int major, minor;
00582 
00583       retval = (sscanf (version, "%d.%d", &major, &minor) == 2
00584                 && (major < ver_major_min
00585                     || (major == ver_major_min && minor < ver_minor_min)));
00586     }
00587   xfree (version);
00588 
00589   return retval;
00590 }
00591 
00592 static void
00593 enable_thread_event_reporting (void)
00594 {
00595   td_thr_events_t events;
00596   td_err_e err;
00597   struct thread_db_info *info;
00598 
00599   info = get_thread_db_info (ptid_get_pid (inferior_ptid));
00600 
00601   /* We cannot use the thread event reporting facility if these
00602      functions aren't available.  */
00603   if (info->td_ta_event_addr_p == NULL
00604       || info->td_ta_set_event_p == NULL
00605       || info->td_ta_event_getmsg_p == NULL
00606       || info->td_thr_event_enable_p == NULL)
00607     return;
00608 
00609   /* Set the process wide mask saying which events we're interested in.  */
00610   td_event_emptyset (&events);
00611   td_event_addset (&events, TD_CREATE);
00612 
00613   /* There is a bug fixed between linuxthreads 2.1.3 and 2.2 by
00614        commit 2e4581e4fba917f1779cd0a010a45698586c190a
00615        * manager.c (pthread_exited): Correctly report event as TD_REAP
00616        instead of TD_DEATH.  Fix comments.
00617      where event reporting facility is broken for TD_DEATH events,
00618      so don't enable it if we have glibc but a lower version.  */
00619   if (!inferior_has_bug ("__linuxthreads_version", 2, 2))
00620     td_event_addset (&events, TD_DEATH);
00621 
00622   err = info->td_ta_set_event_p (info->thread_agent, &events);
00623   if (err != TD_OK)
00624     {
00625       warning (_("Unable to set global thread event mask: %s"),
00626                thread_db_err_str (err));
00627       return;
00628     }
00629 
00630   /* Delete previous thread event breakpoints, if any.  */
00631   remove_thread_event_breakpoints ();
00632   info->td_create_bp_addr = 0;
00633   info->td_death_bp_addr = 0;
00634 
00635   /* Set up the thread creation event.  */
00636   err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr);
00637   if (err != TD_OK)
00638     {
00639       warning (_("Unable to get location for thread creation breakpoint: %s"),
00640                thread_db_err_str (err));
00641       return;
00642     }
00643 
00644   /* Set up the thread death event.  */
00645   err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr);
00646   if (err != TD_OK)
00647     {
00648       warning (_("Unable to get location for thread death breakpoint: %s"),
00649                thread_db_err_str (err));
00650       return;
00651     }
00652 }
00653 
00654 /* Similar as thread_db_find_new_threads_1, but try to silently ignore errors
00655    if appropriate.
00656 
00657    Return 1 if the caller should abort libthread_db initialization.  Return 0
00658    otherwise.  */
00659 
00660 static int
00661 thread_db_find_new_threads_silently (ptid_t ptid)
00662 {
00663   volatile struct gdb_exception except;
00664 
00665   TRY_CATCH (except, RETURN_MASK_ERROR)
00666     {
00667       thread_db_find_new_threads_2 (ptid, 1);
00668     }
00669 
00670   if (except.reason < 0)
00671     {
00672       if (libthread_db_debug)
00673         exception_fprintf (gdb_stderr, except,
00674                            "Warning: thread_db_find_new_threads_silently: ");
00675 
00676       /* There is a bug fixed between nptl 2.6.1 and 2.7 by
00677            commit 7d9d8bd18906fdd17364f372b160d7ab896ce909
00678          where calls to td_thr_get_info fail with TD_ERR for statically linked
00679          executables if td_thr_get_info is called before glibc has initialized
00680          itself.
00681          
00682          If the nptl bug is NOT present in the inferior and still thread_db
00683          reports an error return 1.  It means the inferior has corrupted thread
00684          list and GDB should fall back only to LWPs.
00685 
00686          If the nptl bug is present in the inferior return 0 to silently ignore
00687          such errors, and let gdb enumerate threads again later.  In such case
00688          GDB cannot properly display LWPs if the inferior thread list is
00689          corrupted.  For core files it does not apply, no 'later enumeration'
00690          is possible.  */
00691 
00692       if (!target_has_execution || !inferior_has_bug ("nptl_version", 2, 7))
00693         {
00694           exception_fprintf (gdb_stderr, except,
00695                              _("Warning: couldn't activate thread debugging "
00696                                "using libthread_db: "));
00697           return 1;
00698         }
00699     }
00700   return 0;
00701 }
00702 
00703 /* Lookup a library in which given symbol resides.
00704    Note: this is looking in GDB process, not in the inferior.
00705    Returns library name, or NULL.  */
00706 
00707 static const char *
00708 dladdr_to_soname (const void *addr)
00709 {
00710   Dl_info info;
00711 
00712   if (dladdr (addr, &info) != 0)
00713     return info.dli_fname;
00714   return NULL;
00715 }
00716 
00717 /* Attempt to initialize dlopen()ed libthread_db, described by INFO.
00718    Return 1 on success.
00719    Failure could happen if libthread_db does not have symbols we expect,
00720    or when it refuses to work with the current inferior (e.g. due to
00721    version mismatch between libthread_db and libpthread).  */
00722 
00723 static int
00724 try_thread_db_load_1 (struct thread_db_info *info)
00725 {
00726   td_err_e err;
00727 
00728   /* Initialize pointers to the dynamic library functions we will use.
00729      Essential functions first.  */
00730 
00731   info->td_init_p = verbose_dlsym (info->handle, "td_init");
00732   if (info->td_init_p == NULL)
00733     return 0;
00734 
00735   err = info->td_init_p ();
00736   if (err != TD_OK)
00737     {
00738       warning (_("Cannot initialize libthread_db: %s"),
00739                thread_db_err_str (err));
00740       return 0;
00741     }
00742 
00743   info->td_ta_new_p = verbose_dlsym (info->handle, "td_ta_new");
00744   if (info->td_ta_new_p == NULL)
00745     return 0;
00746 
00747   /* Initialize the structure that identifies the child process.  */
00748   info->proc_handle.ptid = inferior_ptid;
00749 
00750   /* Now attempt to open a connection to the thread library.  */
00751   err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
00752   if (err != TD_OK)
00753     {
00754       if (libthread_db_debug)
00755         printf_unfiltered (_("td_ta_new failed: %s\n"),
00756                            thread_db_err_str (err));
00757       else
00758         switch (err)
00759           {
00760             case TD_NOLIBTHREAD:
00761 #ifdef THREAD_DB_HAS_TD_VERSION
00762             case TD_VERSION:
00763 #endif
00764               /* The errors above are not unexpected and silently ignored:
00765                  they just mean we haven't found correct version of
00766                  libthread_db yet.  */
00767               break;
00768             default:
00769               warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
00770           }
00771       return 0;
00772     }
00773 
00774   info->td_ta_map_id2thr_p = verbose_dlsym (info->handle, "td_ta_map_id2thr");
00775   if (info->td_ta_map_id2thr_p == NULL)
00776     return 0;
00777 
00778   info->td_ta_map_lwp2thr_p = verbose_dlsym (info->handle,
00779                                              "td_ta_map_lwp2thr");
00780   if (info->td_ta_map_lwp2thr_p == NULL)
00781     return 0;
00782 
00783   info->td_ta_thr_iter_p = verbose_dlsym (info->handle, "td_ta_thr_iter");
00784   if (info->td_ta_thr_iter_p == NULL)
00785     return 0;
00786 
00787   info->td_thr_validate_p = verbose_dlsym (info->handle, "td_thr_validate");
00788   if (info->td_thr_validate_p == NULL)
00789     return 0;
00790 
00791   info->td_thr_get_info_p = verbose_dlsym (info->handle, "td_thr_get_info");
00792   if (info->td_thr_get_info_p == NULL)
00793     return 0;
00794 
00795   /* These are not essential.  */
00796   info->td_ta_event_addr_p = dlsym (info->handle, "td_ta_event_addr");
00797   info->td_ta_set_event_p = dlsym (info->handle, "td_ta_set_event");
00798   info->td_ta_clear_event_p = dlsym (info->handle, "td_ta_clear_event");
00799   info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
00800   info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
00801   info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
00802 
00803   if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
00804     {
00805       /* Even if libthread_db initializes, if the thread list is
00806          corrupted, we'd not manage to list any threads.  Better reject this
00807          thread_db, and fall back to at least listing LWPs.  */
00808       return 0;
00809     }
00810 
00811   printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
00812 
00813   if (libthread_db_debug || *libthread_db_search_path)
00814     {
00815       const char *library;
00816 
00817       library = dladdr_to_soname (*info->td_ta_new_p);
00818       if (library == NULL)
00819         library = LIBTHREAD_DB_SO;
00820 
00821       printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
00822                          library);
00823     }
00824 
00825   /* The thread library was detected.  Activate the thread_db target
00826      if this is the first process using it.  */
00827   if (thread_db_list->next == NULL)
00828     push_target (&thread_db_ops);
00829 
00830   /* Enable event reporting, but not when debugging a core file.  */
00831   if (target_has_execution)
00832     enable_thread_event_reporting ();
00833 
00834   return 1;
00835 }
00836 
00837 /* Attempt to use LIBRARY as libthread_db.  LIBRARY could be absolute,
00838    relative, or just LIBTHREAD_DB.  */
00839 
00840 static int
00841 try_thread_db_load (const char *library)
00842 {
00843   void *handle;
00844   struct thread_db_info *info;
00845 
00846   if (libthread_db_debug)
00847     printf_unfiltered (_("Trying host libthread_db library: %s.\n"),
00848                        library);
00849   handle = dlopen (library, RTLD_NOW);
00850   if (handle == NULL)
00851     {
00852       if (libthread_db_debug)
00853         printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ());
00854       return 0;
00855     }
00856 
00857   if (libthread_db_debug && strchr (library, '/') == NULL)
00858     {
00859       void *td_init;
00860 
00861       td_init = dlsym (handle, "td_init");
00862       if (td_init != NULL)
00863         {
00864           const char *const libpath = dladdr_to_soname (td_init);
00865 
00866           if (libpath != NULL)
00867             printf_unfiltered (_("Host %s resolved to: %s.\n"),
00868                                library, libpath);
00869         }
00870     }
00871 
00872   info = add_thread_db_info (handle);
00873 
00874   /* Do not save system library name, that one is always trusted.  */
00875   if (strchr (library, '/') != NULL)
00876     info->filename = gdb_realpath (library);
00877 
00878   if (try_thread_db_load_1 (info))
00879     return 1;
00880 
00881   /* This library "refused" to work on current inferior.  */
00882   delete_thread_db_info (ptid_get_pid (inferior_ptid));
00883   return 0;
00884 }
00885 
00886 /* Subroutine of try_thread_db_load_from_pdir to simplify it.
00887    Try loading libthread_db in directory(OBJ)/SUBDIR.
00888    SUBDIR may be NULL.  It may also be something like "../lib64".
00889    The result is true for success.  */
00890 
00891 static int
00892 try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
00893 {
00894   struct cleanup *cleanup;
00895   char *path, *cp;
00896   int result;
00897   const char *obj_name = objfile_name (obj);
00898 
00899   if (obj_name[0] != '/')
00900     {
00901       warning (_("Expected absolute pathname for libpthread in the"
00902                  " inferior, but got %s."), obj_name);
00903       return 0;
00904     }
00905 
00906   path = xmalloc (strlen (obj_name) + (subdir ? strlen (subdir) + 1 : 0)
00907                   + 1 + strlen (LIBTHREAD_DB_SO) + 1);
00908   cleanup = make_cleanup (xfree, path);
00909 
00910   strcpy (path, obj_name);
00911   cp = strrchr (path, '/');
00912   /* This should at minimum hit the first character.  */
00913   gdb_assert (cp != NULL);
00914   cp[1] = '\0';
00915   if (subdir != NULL)
00916     {
00917       strcat (cp, subdir);
00918       strcat (cp, "/");
00919     }
00920   strcat (cp, LIBTHREAD_DB_SO);
00921 
00922   if (!file_is_auto_load_safe (path, _("auto-load: Loading libthread-db "
00923                                        "library \"%s\" from $pdir.\n"),
00924                                path))
00925     result = 0;
00926   else
00927     result = try_thread_db_load (path);
00928 
00929   do_cleanups (cleanup);
00930   return result;
00931 }
00932 
00933 /* Handle $pdir in libthread-db-search-path.
00934    Look for libthread_db in directory(libpthread)/SUBDIR.
00935    SUBDIR may be NULL.  It may also be something like "../lib64".
00936    The result is true for success.  */
00937 
00938 static int
00939 try_thread_db_load_from_pdir (const char *subdir)
00940 {
00941   struct objfile *obj;
00942 
00943   if (!auto_load_thread_db)
00944     return 0;
00945 
00946   ALL_OBJFILES (obj)
00947     if (libpthread_name_p (objfile_name (obj)))
00948       {
00949         if (try_thread_db_load_from_pdir_1 (obj, subdir))
00950           return 1;
00951 
00952         /* We may have found the separate-debug-info version of
00953            libpthread, and it may live in a directory without a matching
00954            libthread_db.  */
00955         if (obj->separate_debug_objfile_backlink != NULL)
00956           return try_thread_db_load_from_pdir_1 (obj->separate_debug_objfile_backlink,
00957                                                  subdir);
00958 
00959         return 0;
00960       }
00961 
00962   return 0;
00963 }
00964 
00965 /* Handle $sdir in libthread-db-search-path.
00966    Look for libthread_db in the system dirs, or wherever a plain
00967    dlopen(file_without_path) will look.
00968    The result is true for success.  */
00969 
00970 static int
00971 try_thread_db_load_from_sdir (void)
00972 {
00973   return try_thread_db_load (LIBTHREAD_DB_SO);
00974 }
00975 
00976 /* Try to load libthread_db from directory DIR of length DIR_LEN.
00977    The result is true for success.  */
00978 
00979 static int
00980 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
00981 {
00982   struct cleanup *cleanup;
00983   char *path;
00984   int result;
00985 
00986   if (!auto_load_thread_db)
00987     return 0;
00988 
00989   path = xmalloc (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1);
00990   cleanup = make_cleanup (xfree, path);
00991 
00992   memcpy (path, dir, dir_len);
00993   path[dir_len] = '/';
00994   strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
00995 
00996   if (!file_is_auto_load_safe (path, _("auto-load: Loading libthread-db "
00997                                        "library \"%s\" from explicit "
00998                                        "directory.\n"),
00999                                path))
01000     result = 0;
01001   else
01002     result = try_thread_db_load (path);
01003 
01004   do_cleanups (cleanup);
01005   return result;
01006 }
01007 
01008 /* Search libthread_db_search_path for libthread_db which "agrees"
01009    to work on current inferior.
01010    The result is true for success.  */
01011 
01012 static int
01013 thread_db_load_search (void)
01014 {
01015   VEC (char_ptr) *dir_vec;
01016   struct cleanup *cleanups;
01017   char *this_dir;
01018   int i, rc = 0;
01019 
01020   dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
01021   cleanups = make_cleanup_free_char_ptr_vec (dir_vec);
01022 
01023   for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
01024     {
01025       const int pdir_len = sizeof ("$pdir") - 1;
01026       size_t this_dir_len;
01027 
01028       this_dir_len = strlen (this_dir);
01029 
01030       if (strncmp (this_dir, "$pdir", pdir_len) == 0
01031           && (this_dir[pdir_len] == '\0'
01032               || this_dir[pdir_len] == '/'))
01033         {
01034           char *subdir = NULL;
01035           struct cleanup *free_subdir_cleanup
01036             = make_cleanup (null_cleanup, NULL);
01037 
01038           if (this_dir[pdir_len] == '/')
01039             {
01040               subdir = xmalloc (strlen (this_dir));
01041               make_cleanup (xfree, subdir);
01042               strcpy (subdir, this_dir + pdir_len + 1);
01043             }
01044           rc = try_thread_db_load_from_pdir (subdir);
01045           do_cleanups (free_subdir_cleanup);
01046           if (rc)
01047             break;
01048         }
01049       else if (strcmp (this_dir, "$sdir") == 0)
01050         {
01051           if (try_thread_db_load_from_sdir ())
01052             {
01053               rc = 1;
01054               break;
01055             }
01056         }
01057       else
01058         {
01059           if (try_thread_db_load_from_dir (this_dir, this_dir_len))
01060             {
01061               rc = 1;
01062               break;
01063             }
01064         }
01065     }
01066 
01067   do_cleanups (cleanups);
01068   if (libthread_db_debug)
01069     printf_unfiltered (_("thread_db_load_search returning %d\n"), rc);
01070   return rc;
01071 }
01072 
01073 /* Return non-zero if the inferior has a libpthread.  */
01074 
01075 static int
01076 has_libpthread (void)
01077 {
01078   struct objfile *obj;
01079 
01080   ALL_OBJFILES (obj)
01081     if (libpthread_name_p (objfile_name (obj)))
01082       return 1;
01083 
01084   return 0;
01085 }
01086 
01087 /* Attempt to load and initialize libthread_db.
01088    Return 1 on success.  */
01089 
01090 static int
01091 thread_db_load (void)
01092 {
01093   struct thread_db_info *info;
01094 
01095   info = get_thread_db_info (ptid_get_pid (inferior_ptid));
01096 
01097   if (info != NULL)
01098     return 1;
01099 
01100   /* Don't attempt to use thread_db on executables not running
01101      yet.  */
01102   if (!target_has_registers)
01103     return 0;
01104 
01105   /* Don't attempt to use thread_db for remote targets.  */
01106   if (!(target_can_run (&current_target) || core_bfd))
01107     return 0;
01108 
01109   if (thread_db_load_search ())
01110     return 1;
01111 
01112   /* We couldn't find a libthread_db.
01113      If the inferior has a libpthread warn the user.  */
01114   if (has_libpthread ())
01115     {
01116       warning (_("Unable to find libthread_db matching inferior's thread"
01117                  " library, thread debugging will not be available."));
01118       return 0;
01119     }
01120 
01121   /* Either this executable isn't using libpthread at all, or it is
01122      statically linked.  Since we can't easily distinguish these two cases,
01123      no warning is issued.  */
01124   return 0;
01125 }
01126 
01127 static void
01128 disable_thread_event_reporting (struct thread_db_info *info)
01129 {
01130   if (info->td_ta_clear_event_p != NULL)
01131     {
01132       td_thr_events_t events;
01133 
01134       /* Set the process wide mask saying we aren't interested in any
01135          events anymore.  */
01136       td_event_fillset (&events);
01137       info->td_ta_clear_event_p (info->thread_agent, &events);
01138     }
01139 
01140   info->td_create_bp_addr = 0;
01141   info->td_death_bp_addr = 0;
01142 }
01143 
01144 static void
01145 check_thread_signals (void)
01146 {
01147   if (!thread_signals)
01148     {
01149       sigset_t mask;
01150       int i;
01151 
01152       lin_thread_get_thread_signals (&mask);
01153       sigemptyset (&thread_stop_set);
01154       sigemptyset (&thread_print_set);
01155 
01156       for (i = 1; i < NSIG; i++)
01157         {
01158           if (sigismember (&mask, i))
01159             {
01160               if (signal_stop_update (gdb_signal_from_host (i), 0))
01161                 sigaddset (&thread_stop_set, i);
01162               if (signal_print_update (gdb_signal_from_host (i), 0))
01163                 sigaddset (&thread_print_set, i);
01164               thread_signals = 1;
01165             }
01166         }
01167     }
01168 }
01169 
01170 /* Check whether thread_db is usable.  This function is called when
01171    an inferior is created (or otherwise acquired, e.g. attached to)
01172    and when new shared libraries are loaded into a running process.  */
01173 
01174 void
01175 check_for_thread_db (void)
01176 {
01177   /* Do nothing if we couldn't load libthread_db.so.1.  */
01178   if (!thread_db_load ())
01179     return;
01180 }
01181 
01182 /* This function is called via the new_objfile observer.  */
01183 
01184 static void
01185 thread_db_new_objfile (struct objfile *objfile)
01186 {
01187   /* This observer must always be called with inferior_ptid set
01188      correctly.  */
01189 
01190   if (objfile != NULL
01191       /* libpthread with separate debug info has its debug info file already
01192          loaded (and notified without successful thread_db initialization)
01193          the time observer_notify_new_objfile is called for the library itself.
01194          Static executables have their separate debug info loaded already
01195          before the inferior has started.  */
01196       && objfile->separate_debug_objfile_backlink == NULL
01197       /* Only check for thread_db if we loaded libpthread,
01198          or if this is the main symbol file.
01199          We need to check OBJF_MAINLINE to handle the case of debugging
01200          a statically linked executable AND the symbol file is specified AFTER
01201          the exec file is loaded (e.g., gdb -c core ; file foo).
01202          For dynamically linked executables, libpthread can be near the end
01203          of the list of shared libraries to load, and in an app of several
01204          thousand shared libraries, this can otherwise be painful.  */
01205       && ((objfile->flags & OBJF_MAINLINE) != 0
01206           || libpthread_name_p (objfile_name (objfile))))
01207     check_for_thread_db ();
01208 }
01209 
01210 /* This function is called via the inferior_created observer.
01211    This handles the case of debugging statically linked executables.  */
01212 
01213 static void
01214 thread_db_inferior_created (struct target_ops *target, int from_tty)
01215 {
01216   check_for_thread_db ();
01217 }
01218 
01219 /* Attach to a new thread.  This function is called when we receive a
01220    TD_CREATE event or when we iterate over all threads and find one
01221    that wasn't already in our list.  Returns true on success.  */
01222 
01223 static int
01224 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
01225                const td_thrinfo_t *ti_p)
01226 {
01227   struct private_thread_info *private;
01228   struct thread_info *tp;
01229   td_err_e err;
01230   struct thread_db_info *info;
01231 
01232   /* If we're being called after a TD_CREATE event, we may already
01233      know about this thread.  There are two ways this can happen.  We
01234      may have iterated over all threads between the thread creation
01235      and the TD_CREATE event, for instance when the user has issued
01236      the `info threads' command before the SIGTRAP for hitting the
01237      thread creation breakpoint was reported.  Alternatively, the
01238      thread may have exited and a new one been created with the same
01239      thread ID.  In the first case we don't need to do anything; in
01240      the second case we should discard information about the dead
01241      thread and attach to the new one.  */
01242   tp = find_thread_ptid (ptid);
01243   if (tp != NULL)
01244     {
01245       /* If tp->private is NULL, then GDB is already attached to this
01246          thread, but we do not know anything about it.  We can learn
01247          about it here.  This can only happen if we have some other
01248          way besides libthread_db to notice new threads (i.e.
01249          PTRACE_EVENT_CLONE); assume the same mechanism notices thread
01250          exit, so this can not be a stale thread recreated with the
01251          same ID.  */
01252       if (tp->private != NULL)
01253         {
01254           if (!tp->private->dying)
01255             return 0;
01256 
01257           delete_thread (ptid);
01258           tp = NULL;
01259         }
01260     }
01261 
01262   if (target_has_execution)
01263     check_thread_signals ();
01264 
01265   /* Under GNU/Linux, we have to attach to each and every thread.  */
01266   if (target_has_execution
01267       && tp == NULL)
01268     {
01269       int res;
01270 
01271       res = lin_lwp_attach_lwp (ptid_build (ptid_get_pid (ptid),
01272                                             ti_p->ti_lid, 0));
01273       if (res < 0)
01274         {
01275           /* Error, stop iterating.  */
01276           return 0;
01277         }
01278       else if (res > 0)
01279         {
01280           /* Pretend this thread doesn't exist yet, and keep
01281              iterating.  */
01282           return 1;
01283         }
01284 
01285       /* Otherwise, we sucessfully attached to the thread.  */
01286     }
01287 
01288   /* Construct the thread's private data.  */
01289   private = xmalloc (sizeof (struct private_thread_info));
01290   memset (private, 0, sizeof (struct private_thread_info));
01291 
01292   /* A thread ID of zero may mean the thread library has not initialized
01293      yet.  But we shouldn't even get here if that's the case.  FIXME:
01294      if we change GDB to always have at least one thread in the thread
01295      list this will have to go somewhere else; maybe private == NULL
01296      until the thread_db target claims it.  */
01297   gdb_assert (ti_p->ti_tid != 0);
01298   private->th = *th_p;
01299   private->tid = ti_p->ti_tid;
01300   if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
01301     private->dying = 1;
01302 
01303   /* Add the thread to GDB's thread list.  */
01304   if (tp == NULL)
01305     add_thread_with_info (ptid, private);
01306   else
01307     tp->private = private;
01308 
01309   info = get_thread_db_info (ptid_get_pid (ptid));
01310 
01311   /* Enable thread event reporting for this thread, except when
01312      debugging a core file.  */
01313   if (target_has_execution)
01314     {
01315       err = info->td_thr_event_enable_p (th_p, 1);
01316       if (err != TD_OK)
01317         error (_("Cannot enable thread event reporting for %s: %s"),
01318                target_pid_to_str (ptid), thread_db_err_str (err));
01319     }
01320 
01321   return 1;
01322 }
01323 
01324 static void
01325 detach_thread (ptid_t ptid)
01326 {
01327   struct thread_info *thread_info;
01328 
01329   /* Don't delete the thread now, because it still reports as active
01330      until it has executed a few instructions after the event
01331      breakpoint - if we deleted it now, "info threads" would cause us
01332      to re-attach to it.  Just mark it as having had a TD_DEATH
01333      event.  This means that we won't delete it from our thread list
01334      until we notice that it's dead (via prune_threads), or until
01335      something re-uses its thread ID.  We'll report the thread exit
01336      when the underlying LWP dies.  */
01337   thread_info = find_thread_ptid (ptid);
01338   gdb_assert (thread_info != NULL && thread_info->private != NULL);
01339   thread_info->private->dying = 1;
01340 }
01341 
01342 static void
01343 thread_db_detach (struct target_ops *ops, char *args, int from_tty)
01344 {
01345   struct target_ops *target_beneath = find_target_beneath (ops);
01346   struct thread_db_info *info;
01347 
01348   info = get_thread_db_info (ptid_get_pid (inferior_ptid));
01349 
01350   if (info)
01351     {
01352       if (target_has_execution)
01353         {
01354           disable_thread_event_reporting (info);
01355 
01356           /* Delete the old thread event breakpoints.  Note that
01357              unlike when mourning, we can remove them here because
01358              there's still a live inferior to poke at.  In any case,
01359              GDB will not try to insert anything in the inferior when
01360              removing a breakpoint.  */
01361           remove_thread_event_breakpoints ();
01362         }
01363 
01364       delete_thread_db_info (ptid_get_pid (inferior_ptid));
01365     }
01366 
01367   target_beneath->to_detach (target_beneath, args, from_tty);
01368 
01369   /* NOTE: From this point on, inferior_ptid is null_ptid.  */
01370 
01371   /* If there are no more processes using libpthread, detach the
01372      thread_db target ops.  */
01373   if (!thread_db_list)
01374     unpush_target (&thread_db_ops);
01375 }
01376 
01377 /* Check if PID is currently stopped at the location of a thread event
01378    breakpoint location.  If it is, read the event message and act upon
01379    the event.  */
01380 
01381 static void
01382 check_event (ptid_t ptid)
01383 {
01384   struct regcache *regcache = get_thread_regcache (ptid);
01385   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01386   td_event_msg_t msg;
01387   td_thrinfo_t ti;
01388   td_err_e err;
01389   CORE_ADDR stop_pc;
01390   int loop = 0;
01391   struct thread_db_info *info;
01392 
01393   info = get_thread_db_info (ptid_get_pid (ptid));
01394 
01395   /* Bail out early if we're not at a thread event breakpoint.  */
01396   stop_pc = regcache_read_pc (regcache)
01397             - gdbarch_decr_pc_after_break (gdbarch);
01398   if (stop_pc != info->td_create_bp_addr
01399       && stop_pc != info->td_death_bp_addr)
01400     return;
01401 
01402   /* Access an lwp we know is stopped.  */
01403   info->proc_handle.ptid = ptid;
01404 
01405   /* If we have only looked at the first thread before libpthread was
01406      initialized, we may not know its thread ID yet.  Make sure we do
01407      before we add another thread to the list.  */
01408   if (!have_threads (ptid))
01409     thread_db_find_new_threads_1 (ptid);
01410 
01411   /* If we are at a create breakpoint, we do not know what new lwp
01412      was created and cannot specifically locate the event message for it.
01413      We have to call td_ta_event_getmsg() to get
01414      the latest message.  Since we have no way of correlating whether
01415      the event message we get back corresponds to our breakpoint, we must
01416      loop and read all event messages, processing them appropriately.
01417      This guarantees we will process the correct message before continuing
01418      from the breakpoint.
01419 
01420      Currently, death events are not enabled.  If they are enabled,
01421      the death event can use the td_thr_event_getmsg() interface to
01422      get the message specifically for that lwp and avoid looping
01423      below.  */
01424 
01425   loop = 1;
01426 
01427   do
01428     {
01429       err = info->td_ta_event_getmsg_p (info->thread_agent, &msg);
01430       if (err != TD_OK)
01431         {
01432           if (err == TD_NOMSG)
01433             return;
01434 
01435           error (_("Cannot get thread event message: %s"),
01436                  thread_db_err_str (err));
01437         }
01438 
01439       err = info->td_thr_get_info_p (msg.th_p, &ti);
01440       if (err != TD_OK)
01441         error (_("Cannot get thread info: %s"), thread_db_err_str (err));
01442 
01443       ptid = ptid_build (ptid_get_pid (ptid), ti.ti_lid, 0);
01444 
01445       switch (msg.event)
01446         {
01447         case TD_CREATE:
01448           /* Call attach_thread whether or not we already know about a
01449              thread with this thread ID.  */
01450           attach_thread (ptid, msg.th_p, &ti);
01451 
01452           break;
01453 
01454         case TD_DEATH:
01455 
01456           if (!in_thread_list (ptid))
01457             error (_("Spurious thread death event."));
01458 
01459           detach_thread (ptid);
01460 
01461           break;
01462 
01463         default:
01464           error (_("Spurious thread event."));
01465         }
01466     }
01467   while (loop);
01468 }
01469 
01470 static ptid_t
01471 thread_db_wait (struct target_ops *ops,
01472                 ptid_t ptid, struct target_waitstatus *ourstatus,
01473                 int options)
01474 {
01475   struct thread_db_info *info;
01476   struct target_ops *beneath = find_target_beneath (ops);
01477 
01478   ptid = beneath->to_wait (beneath, ptid, ourstatus, options);
01479 
01480   if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
01481     return ptid;
01482 
01483   if (ourstatus->kind == TARGET_WAITKIND_EXITED
01484       || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
01485     return ptid;
01486 
01487   info = get_thread_db_info (ptid_get_pid (ptid));
01488 
01489   /* If this process isn't using thread_db, we're done.  */
01490   if (info == NULL)
01491     return ptid;
01492 
01493   if (ourstatus->kind == TARGET_WAITKIND_EXECD)
01494     {
01495       /* New image, it may or may not end up using thread_db.  Assume
01496          not unless we find otherwise.  */
01497       delete_thread_db_info (ptid_get_pid (ptid));
01498       if (!thread_db_list)
01499         unpush_target (&thread_db_ops);
01500 
01501       /* Thread event breakpoints are deleted by
01502          update_breakpoints_after_exec.  */
01503 
01504       return ptid;
01505     }
01506 
01507   /* If we do not know about the main thread yet, this would be a good time to
01508      find it.  */
01509   if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads (ptid))
01510     thread_db_find_new_threads_1 (ptid);
01511 
01512   if (ourstatus->kind == TARGET_WAITKIND_STOPPED
01513       && ourstatus->value.sig == GDB_SIGNAL_TRAP)
01514     /* Check for a thread event.  */
01515     check_event (ptid);
01516 
01517   if (have_threads (ptid))
01518     {
01519       /* Fill in the thread's user-level thread id.  */
01520       thread_from_lwp (ptid);
01521     }
01522 
01523   return ptid;
01524 }
01525 
01526 static void
01527 thread_db_mourn_inferior (struct target_ops *ops)
01528 {
01529   struct target_ops *target_beneath = find_target_beneath (ops);
01530 
01531   delete_thread_db_info (ptid_get_pid (inferior_ptid));
01532 
01533   target_beneath->to_mourn_inferior (target_beneath);
01534 
01535   /* Delete the old thread event breakpoints.  Do this after mourning
01536      the inferior, so that we don't try to uninsert them.  */
01537   remove_thread_event_breakpoints ();
01538 
01539   /* Detach thread_db target ops.  */
01540   if (!thread_db_list)
01541     unpush_target (ops);
01542 }
01543 
01544 struct callback_data
01545 {
01546   struct thread_db_info *info;
01547   int new_threads;
01548 };
01549 
01550 static int
01551 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
01552 {
01553   td_thrinfo_t ti;
01554   td_err_e err;
01555   ptid_t ptid;
01556   struct thread_info *tp;
01557   struct callback_data *cb_data = data;
01558   struct thread_db_info *info = cb_data->info;
01559 
01560   err = info->td_thr_get_info_p (th_p, &ti);
01561   if (err != TD_OK)
01562     error (_("find_new_threads_callback: cannot get thread info: %s"),
01563            thread_db_err_str (err));
01564 
01565   if (ti.ti_tid == 0)
01566     {
01567       /* A thread ID of zero means that this is the main thread, but
01568          glibc has not yet initialized thread-local storage and the
01569          pthread library.  We do not know what the thread's TID will
01570          be yet.  Just enable event reporting and otherwise ignore
01571          it.  */
01572 
01573       /* In that case, we're not stopped in a fork syscall and don't
01574          need this glibc bug workaround.  */
01575       info->need_stale_parent_threads_check = 0;
01576 
01577       if (target_has_execution)
01578         {
01579           err = info->td_thr_event_enable_p (th_p, 1);
01580           if (err != TD_OK)
01581             error (_("Cannot enable thread event reporting for LWP %d: %s"),
01582                    (int) ti.ti_lid, thread_db_err_str (err));
01583         }
01584 
01585       return 0;
01586     }
01587 
01588   /* Ignore stale parent threads, caused by glibc/BZ5983.  This is a
01589      bit expensive, as it needs to open /proc/pid/status, so try to
01590      avoid doing the work if we know we don't have to.  */
01591   if (info->need_stale_parent_threads_check)
01592     {
01593       int tgid = linux_proc_get_tgid (ti.ti_lid);
01594 
01595       if (tgid != -1 && tgid != info->pid)
01596         return 0;
01597     }
01598 
01599   ptid = ptid_build (info->pid, ti.ti_lid, 0);
01600   tp = find_thread_ptid (ptid);
01601   if (tp == NULL || tp->private == NULL)
01602     {
01603       if (attach_thread (ptid, th_p, &ti))
01604         cb_data->new_threads += 1;
01605       else
01606         /* Problem attaching this thread; perhaps it exited before we
01607            could attach it?
01608            This could mean that the thread list inside glibc itself is in
01609            inconsistent state, and libthread_db could go on looping forever
01610            (observed with glibc-2.3.6).  To prevent that, terminate
01611            iteration: thread_db_find_new_threads_2 will retry.  */
01612         return 1;
01613     }
01614 
01615   return 0;
01616 }
01617 
01618 /* Helper for thread_db_find_new_threads_2.
01619    Returns number of new threads found.  */
01620 
01621 static int
01622 find_new_threads_once (struct thread_db_info *info, int iteration,
01623                        td_err_e *errp)
01624 {
01625   volatile struct gdb_exception except;
01626   struct callback_data data;
01627   td_err_e err = TD_ERR;
01628 
01629   data.info = info;
01630   data.new_threads = 0;
01631 
01632   TRY_CATCH (except, RETURN_MASK_ERROR)
01633     {
01634       /* Iterate over all user-space threads to discover new threads.  */
01635       err = info->td_ta_thr_iter_p (info->thread_agent,
01636                                     find_new_threads_callback,
01637                                     &data,
01638                                     TD_THR_ANY_STATE,
01639                                     TD_THR_LOWEST_PRIORITY,
01640                                     TD_SIGNO_MASK,
01641                                     TD_THR_ANY_USER_FLAGS);
01642     }
01643 
01644   if (libthread_db_debug)
01645     {
01646       if (except.reason < 0)
01647         exception_fprintf (gdb_stderr, except,
01648                            "Warning: find_new_threads_once: ");
01649 
01650       printf_filtered (_("Found %d new threads in iteration %d.\n"),
01651                        data.new_threads, iteration);
01652     }
01653 
01654   if (errp != NULL)
01655     *errp = err;
01656 
01657   return data.new_threads;
01658 }
01659 
01660 /* Search for new threads, accessing memory through stopped thread
01661    PTID.  If UNTIL_NO_NEW is true, repeat searching until several
01662    searches in a row do not discover any new threads.  */
01663 
01664 static void
01665 thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new)
01666 {
01667   td_err_e err = TD_OK;
01668   struct thread_db_info *info;
01669   int i, loop;
01670 
01671   info = get_thread_db_info (ptid_get_pid (ptid));
01672 
01673   /* Access an lwp we know is stopped.  */
01674   info->proc_handle.ptid = ptid;
01675 
01676   if (until_no_new)
01677     {
01678       /* Require 4 successive iterations which do not find any new threads.
01679          The 4 is a heuristic: there is an inherent race here, and I have
01680          seen that 2 iterations in a row are not always sufficient to
01681          "capture" all threads.  */
01682       for (i = 0, loop = 0; loop < 4 && err == TD_OK; ++i, ++loop)
01683         if (find_new_threads_once (info, i, &err) != 0)
01684           {
01685             /* Found some new threads.  Restart the loop from beginning.  */
01686             loop = -1;
01687           }
01688     }
01689   else
01690     find_new_threads_once (info, 0, &err);
01691 
01692   if (err != TD_OK)
01693     error (_("Cannot find new threads: %s"), thread_db_err_str (err));
01694 }
01695 
01696 static void
01697 thread_db_find_new_threads_1 (ptid_t ptid)
01698 {
01699   thread_db_find_new_threads_2 (ptid, 0);
01700 }
01701 
01702 static int
01703 update_thread_core (struct lwp_info *info, void *closure)
01704 {
01705   info->core = linux_common_core_of_thread (info->ptid);
01706   return 0;
01707 }
01708 
01709 static void
01710 thread_db_find_new_threads (struct target_ops *ops)
01711 {
01712   struct thread_db_info *info;
01713   struct inferior *inf;
01714 
01715   ALL_INFERIORS (inf)
01716     {
01717       struct thread_info *thread;
01718 
01719       if (inf->pid == 0)
01720         continue;
01721 
01722       info = get_thread_db_info (inf->pid);
01723       if (info == NULL)
01724         continue;
01725 
01726       thread = any_live_thread_of_process (inf->pid);
01727       if (thread == NULL || thread->executing)
01728         continue;
01729 
01730       thread_db_find_new_threads_1 (thread->ptid);
01731     }
01732 
01733   if (target_has_execution)
01734     iterate_over_lwps (minus_one_ptid /* iterate over all */,
01735                        update_thread_core, NULL);
01736 }
01737 
01738 static char *
01739 thread_db_pid_to_str (struct target_ops *ops, ptid_t ptid)
01740 {
01741   struct thread_info *thread_info = find_thread_ptid (ptid);
01742   struct target_ops *beneath;
01743 
01744   if (thread_info != NULL && thread_info->private != NULL)
01745     {
01746       static char buf[64];
01747       thread_t tid;
01748 
01749       tid = thread_info->private->tid;
01750       snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
01751                 tid, ptid_get_lwp (ptid));
01752 
01753       return buf;
01754     }
01755 
01756   beneath = find_target_beneath (ops);
01757   if (beneath->to_pid_to_str (beneath, ptid))
01758     return beneath->to_pid_to_str (beneath, ptid);
01759 
01760   return normal_pid_to_str (ptid);
01761 }
01762 
01763 /* Return a string describing the state of the thread specified by
01764    INFO.  */
01765 
01766 static char *
01767 thread_db_extra_thread_info (struct thread_info *info)
01768 {
01769   if (info->private == NULL)
01770     return NULL;
01771 
01772   if (info->private->dying)
01773     return "Exiting";
01774 
01775   return NULL;
01776 }
01777 
01778 /* Get the address of the thread local variable in load module LM which
01779    is stored at OFFSET within the thread local storage for thread PTID.  */
01780 
01781 static CORE_ADDR
01782 thread_db_get_thread_local_address (struct target_ops *ops,
01783                                     ptid_t ptid,
01784                                     CORE_ADDR lm,
01785                                     CORE_ADDR offset)
01786 {
01787   struct thread_info *thread_info;
01788   struct target_ops *beneath;
01789 
01790   /* If we have not discovered any threads yet, check now.  */
01791   if (!have_threads (ptid))
01792     thread_db_find_new_threads_1 (ptid);
01793 
01794   /* Find the matching thread.  */
01795   thread_info = find_thread_ptid (ptid);
01796 
01797   if (thread_info != NULL && thread_info->private != NULL)
01798     {
01799       td_err_e err;
01800       psaddr_t address;
01801       struct thread_db_info *info;
01802 
01803       info = get_thread_db_info (ptid_get_pid (ptid));
01804 
01805       /* glibc doesn't provide the needed interface.  */
01806       if (!info->td_thr_tls_get_addr_p)
01807         throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
01808                      _("No TLS library support"));
01809 
01810       /* Caller should have verified that lm != 0.  */
01811       gdb_assert (lm != 0);
01812 
01813       /* Finally, get the address of the variable.  */
01814       /* Note the cast through uintptr_t: this interface only works if
01815          a target address fits in a psaddr_t, which is a host pointer.
01816          So a 32-bit debugger can not access 64-bit TLS through this.  */
01817       err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
01818                                          (psaddr_t)(uintptr_t) lm,
01819                                          offset, &address);
01820 
01821 #ifdef THREAD_DB_HAS_TD_NOTALLOC
01822       /* The memory hasn't been allocated, yet.  */
01823       if (err == TD_NOTALLOC)
01824           /* Now, if libthread_db provided the initialization image's
01825              address, we *could* try to build a non-lvalue value from
01826              the initialization image.  */
01827         throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
01828                      _("TLS not allocated yet"));
01829 #endif
01830 
01831       /* Something else went wrong.  */
01832       if (err != TD_OK)
01833         throw_error (TLS_GENERIC_ERROR,
01834                      (("%s")), thread_db_err_str (err));
01835 
01836       /* Cast assuming host == target.  Joy.  */
01837       /* Do proper sign extension for the target.  */
01838       gdb_assert (exec_bfd);
01839       return (bfd_get_sign_extend_vma (exec_bfd) > 0
01840               ? (CORE_ADDR) (intptr_t) address
01841               : (CORE_ADDR) (uintptr_t) address);
01842     }
01843 
01844   beneath = find_target_beneath (ops);
01845   if (beneath->to_get_thread_local_address)
01846     return beneath->to_get_thread_local_address (beneath, ptid, lm, offset);
01847   else
01848     throw_error (TLS_GENERIC_ERROR,
01849                  _("TLS not supported on this target"));
01850 }
01851 
01852 /* Callback routine used to find a thread based on the TID part of
01853    its PTID.  */
01854 
01855 static int
01856 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
01857 {
01858   long *tid = (long *) data;
01859 
01860   if (thread->private->tid == *tid)
01861     return 1;
01862 
01863   return 0;
01864 }
01865 
01866 /* Implement the to_get_ada_task_ptid target method for this target.  */
01867 
01868 static ptid_t
01869 thread_db_get_ada_task_ptid (long lwp, long thread)
01870 {
01871   struct thread_info *thread_info;
01872 
01873   thread_db_find_new_threads_1 (inferior_ptid);
01874   thread_info = iterate_over_threads (thread_db_find_thread_from_tid, &thread);
01875 
01876   gdb_assert (thread_info != NULL);
01877 
01878   return (thread_info->ptid);
01879 }
01880 
01881 static void
01882 thread_db_resume (struct target_ops *ops,
01883                   ptid_t ptid, int step, enum gdb_signal signo)
01884 {
01885   struct target_ops *beneath = find_target_beneath (ops);
01886   struct thread_db_info *info;
01887 
01888   if (ptid_equal (ptid, minus_one_ptid))
01889     info = get_thread_db_info (ptid_get_pid (inferior_ptid));
01890   else
01891     info = get_thread_db_info (ptid_get_pid (ptid));
01892 
01893   /* This workaround is only needed for child fork lwps stopped in a
01894      PTRACE_O_TRACEFORK event.  When the inferior is resumed, the
01895      workaround can be disabled.  */
01896   if (info)
01897     info->need_stale_parent_threads_check = 0;
01898 
01899   beneath->to_resume (beneath, ptid, step, signo);
01900 }
01901 
01902 /* qsort helper function for info_auto_load_libthread_db, sort the
01903    thread_db_info pointers primarily by their FILENAME and secondarily by their
01904    PID, both in ascending order.  */
01905 
01906 static int
01907 info_auto_load_libthread_db_compare (const void *ap, const void *bp)
01908 {
01909   struct thread_db_info *a = *(struct thread_db_info **) ap;
01910   struct thread_db_info *b = *(struct thread_db_info **) bp;
01911   int retval;
01912 
01913   retval = strcmp (a->filename, b->filename);
01914   if (retval)
01915     return retval;
01916 
01917   return (a->pid > b->pid) - (a->pid - b->pid);
01918 }
01919 
01920 /* Implement 'info auto-load libthread-db'.  */
01921 
01922 static void
01923 info_auto_load_libthread_db (char *args, int from_tty)
01924 {
01925   struct ui_out *uiout = current_uiout;
01926   const char *cs = args ? args : "";
01927   struct thread_db_info *info, **array;
01928   unsigned info_count, unique_filenames;
01929   size_t max_filename_len, max_pids_len, pids_len;
01930   struct cleanup *back_to;
01931   char *pids;
01932   int i;
01933 
01934   cs = skip_spaces_const (cs);
01935   if (*cs)
01936     error (_("'info auto-load libthread-db' does not accept any parameters"));
01937 
01938   info_count = 0;
01939   for (info = thread_db_list; info; info = info->next)
01940     if (info->filename != NULL)
01941       info_count++;
01942 
01943   array = xmalloc (sizeof (*array) * info_count);
01944   back_to = make_cleanup (xfree, array);
01945 
01946   info_count = 0;
01947   for (info = thread_db_list; info; info = info->next)
01948     if (info->filename != NULL)
01949       array[info_count++] = info;
01950 
01951   /* Sort ARRAY by filenames and PIDs.  */
01952 
01953   qsort (array, info_count, sizeof (*array),
01954          info_auto_load_libthread_db_compare);
01955 
01956   /* Calculate the number of unique filenames (rows) and the maximum string
01957      length of PIDs list for the unique filenames (columns).  */
01958 
01959   unique_filenames = 0;
01960   max_filename_len = 0;
01961   max_pids_len = 0;
01962   pids_len = 0;
01963   for (i = 0; i < info_count; i++)
01964     {
01965       int pid = array[i]->pid;
01966       size_t this_pid_len;
01967 
01968       for (this_pid_len = 0; pid != 0; pid /= 10)
01969         this_pid_len++;
01970 
01971       if (i == 0 || strcmp (array[i - 1]->filename, array[i]->filename) != 0)
01972         {
01973           unique_filenames++;
01974           max_filename_len = max (max_filename_len,
01975                                   strlen (array[i]->filename));
01976 
01977           if (i > 0)
01978             {
01979               pids_len -= strlen (", ");
01980               max_pids_len = max (max_pids_len, pids_len);
01981             }
01982           pids_len = 0;
01983         }
01984       pids_len += this_pid_len + strlen (", ");
01985     }
01986   if (i)
01987     {
01988       pids_len -= strlen (", ");
01989       max_pids_len = max (max_pids_len, pids_len);
01990     }
01991 
01992   /* Table header shifted right by preceding "libthread-db:  " would not match
01993      its columns.  */
01994   if (info_count > 0 && args == auto_load_info_scripts_pattern_nl)
01995     ui_out_text (uiout, "\n");
01996 
01997   make_cleanup_ui_out_table_begin_end (uiout, 2, unique_filenames,
01998                                        "LinuxThreadDbTable");
01999 
02000   ui_out_table_header (uiout, max_filename_len, ui_left, "filename",
02001                        "Filename");
02002   ui_out_table_header (uiout, pids_len, ui_left, "PIDs", "Pids");
02003   ui_out_table_body (uiout);
02004 
02005   pids = xmalloc (max_pids_len + 1);
02006   make_cleanup (xfree, pids);
02007 
02008   /* Note I is incremented inside the cycle, not at its end.  */
02009   for (i = 0; i < info_count;)
02010     {
02011       struct cleanup *chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
02012       char *pids_end;
02013 
02014       info = array[i];
02015       ui_out_field_string (uiout, "filename", info->filename);
02016       pids_end = pids;
02017 
02018       while (i < info_count && strcmp (info->filename, array[i]->filename) == 0)
02019         {
02020           if (pids_end != pids)
02021             {
02022               *pids_end++ = ',';
02023               *pids_end++ = ' ';
02024             }
02025           pids_end += xsnprintf (pids_end, &pids[max_pids_len + 1] - pids_end,
02026                                  "%u", array[i]->pid);
02027           gdb_assert (pids_end < &pids[max_pids_len + 1]);
02028 
02029           i++;
02030         }
02031       *pids_end = '\0';
02032 
02033       ui_out_field_string (uiout, "pids", pids);
02034 
02035       ui_out_text (uiout, "\n");
02036       do_cleanups (chain);
02037     }
02038 
02039   do_cleanups (back_to);
02040 
02041   if (info_count == 0)
02042     ui_out_message (uiout, 0, _("No auto-loaded libthread-db.\n"));
02043 }
02044 
02045 static void
02046 init_thread_db_ops (void)
02047 {
02048   thread_db_ops.to_shortname = "multi-thread";
02049   thread_db_ops.to_longname = "multi-threaded child process.";
02050   thread_db_ops.to_doc = "Threads and pthreads support.";
02051   thread_db_ops.to_detach = thread_db_detach;
02052   thread_db_ops.to_wait = thread_db_wait;
02053   thread_db_ops.to_resume = thread_db_resume;
02054   thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
02055   thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
02056   thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
02057   thread_db_ops.to_stratum = thread_stratum;
02058   thread_db_ops.to_has_thread_control = tc_schedlock;
02059   thread_db_ops.to_get_thread_local_address
02060     = thread_db_get_thread_local_address;
02061   thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
02062   thread_db_ops.to_get_ada_task_ptid = thread_db_get_ada_task_ptid;
02063   thread_db_ops.to_magic = OPS_MAGIC;
02064 
02065   complete_target_initialization (&thread_db_ops);
02066 }
02067 
02068 /* Provide a prototype to silence -Wmissing-prototypes.  */
02069 extern initialize_file_ftype _initialize_thread_db;
02070 
02071 void
02072 _initialize_thread_db (void)
02073 {
02074   init_thread_db_ops ();
02075 
02076   /* Defer loading of libthread_db.so until inferior is running.
02077      This allows gdb to load correct libthread_db for a given
02078      executable -- there could be mutiple versions of glibc,
02079      compiled with LinuxThreads or NPTL, and until there is
02080      a running inferior, we can't tell which libthread_db is
02081      the correct one to load.  */
02082 
02083   libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
02084 
02085   add_setshow_optional_filename_cmd ("libthread-db-search-path",
02086                                      class_support,
02087                                      &libthread_db_search_path, _("\
02088 Set search path for libthread_db."), _("\
02089 Show the current search path or libthread_db."), _("\
02090 This path is used to search for libthread_db to be loaded into \
02091 gdb itself.\n\
02092 Its value is a colon (':') separate list of directories to search.\n\
02093 Setting the search path to an empty list resets it to its default value."),
02094                             set_libthread_db_search_path,
02095                             NULL,
02096                             &setlist, &showlist);
02097 
02098   add_setshow_zuinteger_cmd ("libthread-db", class_maintenance,
02099                              &libthread_db_debug, _("\
02100 Set libthread-db debugging."), _("\
02101 Show libthread-db debugging."), _("\
02102 When non-zero, libthread-db debugging is enabled."),
02103                              NULL,
02104                              show_libthread_db_debug,
02105                              &setdebuglist, &showdebuglist);
02106 
02107   add_setshow_boolean_cmd ("libthread-db", class_support,
02108                            &auto_load_thread_db, _("\
02109 Enable or disable auto-loading of inferior specific libthread_db."), _("\
02110 Show whether auto-loading inferior specific libthread_db is enabled."), _("\
02111 If enabled, libthread_db will be searched in 'set libthread-db-search-path'\n\
02112 locations to load libthread_db compatible with the inferior.\n\
02113 Standard system libthread_db still gets loaded even with this option off.\n\
02114 This options has security implications for untrusted inferiors."),
02115                            NULL, show_auto_load_thread_db,
02116                            auto_load_set_cmdlist_get (),
02117                            auto_load_show_cmdlist_get ());
02118 
02119   add_cmd ("libthread-db", class_info, info_auto_load_libthread_db,
02120            _("Print the list of loaded inferior specific libthread_db.\n\
02121 Usage: info auto-load libthread-db"),
02122            auto_load_info_cmdlist_get ());
02123 
02124   /* Add ourselves to objfile event chain.  */
02125   observer_attach_new_objfile (thread_db_new_objfile);
02126 
02127   /* Add ourselves to inferior_created event chain.
02128      This is needed to handle debugging statically linked programs where
02129      the new_objfile observer won't get called for libpthread.  */
02130   observer_attach_inferior_created (thread_db_inferior_created);
02131 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines