2009-10-29 Paul Pluzhnikov <ppluzhnikov@google.com>
[external/binutils.git] / gdb / gdbserver / thread-db.c
1 /* Thread management interface, for the remote server for GDB.
2    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4
5    Contributed by MontaVista Software.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "server.h"
23
24 #include "linux-low.h"
25
26 extern int debug_threads;
27
28 static int thread_db_use_events;
29
30 #include "gdb_proc_service.h"
31 #include "../gdb_thread_db.h"
32
33 #ifndef USE_LIBTHREAD_DB_DIRECTLY
34 #include <dlfcn.h>
35 #endif
36
37 #include <stdint.h>
38 #include <limits.h>
39 #include <ctype.h>
40
41 struct thread_db
42 {
43   /* Structure that identifies the child process for the
44      <proc_service.h> interface.  */
45   struct ps_prochandle proc_handle;
46
47   /* Connection to the libthread_db library.  */
48   td_thragent_t *thread_agent;
49
50 #ifndef USE_LIBTHREAD_DB_DIRECTLY
51   /* Handle of the libthread_db from dlopen.  */
52   void *handle;
53 #endif
54
55   /* Addresses of libthread_db functions.  */
56   td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta);
57   td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
58                                     td_event_msg_t *msg);
59   td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
60                                  td_thr_events_t *event);
61   td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
62                                   td_event_e event, td_notify_t *ptr);
63   td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
64                                    td_thrhandle_t *th);
65   td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
66                                  td_thrinfo_t *infop);
67   td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
68   td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
69                                 td_thr_iter_f *callback, void *cbdata_p,
70                                 td_thr_state_e state, int ti_pri,
71                                 sigset_t *ti_sigmask_p,
72                                 unsigned int ti_user_flags);
73   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
74                                      void *map_address,
75                                      size_t offset, void **address);
76   const char ** (*td_symbol_list_p) (void);
77 };
78
79 static char *libthread_db_search_path;
80
81 static int find_one_thread (ptid_t);
82 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
83
84 static const char *
85 thread_db_err_str (td_err_e err)
86 {
87   static char buf[64];
88
89   switch (err)
90     {
91     case TD_OK:
92       return "generic 'call succeeded'";
93     case TD_ERR:
94       return "generic error";
95     case TD_NOTHR:
96       return "no thread to satisfy query";
97     case TD_NOSV:
98       return "no sync handle to satisfy query";
99     case TD_NOLWP:
100       return "no LWP to satisfy query";
101     case TD_BADPH:
102       return "invalid process handle";
103     case TD_BADTH:
104       return "invalid thread handle";
105     case TD_BADSH:
106       return "invalid synchronization handle";
107     case TD_BADTA:
108       return "invalid thread agent";
109     case TD_BADKEY:
110       return "invalid key";
111     case TD_NOMSG:
112       return "no event message for getmsg";
113     case TD_NOFPREGS:
114       return "FPU register set not available";
115     case TD_NOLIBTHREAD:
116       return "application not linked with libthread";
117     case TD_NOEVENT:
118       return "requested event is not supported";
119     case TD_NOCAPAB:
120       return "capability not available";
121     case TD_DBERR:
122       return "debugger service failed";
123     case TD_NOAPLIC:
124       return "operation not applicable to";
125     case TD_NOTSD:
126       return "no thread-specific data for this thread";
127     case TD_MALLOC:
128       return "malloc failed";
129     case TD_PARTIALREG:
130       return "only part of register set was written/read";
131     case TD_NOXREGS:
132       return "X register set not available for this thread";
133 #ifdef HAVE_TD_VERSION
134     case TD_VERSION:
135       return "version mismatch between libthread_db and libpthread";
136 #endif
137     default:
138       snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
139       return buf;
140     }
141 }
142
143 #if 0
144 static char *
145 thread_db_state_str (td_thr_state_e state)
146 {
147   static char buf[64];
148
149   switch (state)
150     {
151     case TD_THR_STOPPED:
152       return "stopped by debugger";
153     case TD_THR_RUN:
154       return "runnable";
155     case TD_THR_ACTIVE:
156       return "active";
157     case TD_THR_ZOMBIE:
158       return "zombie";
159     case TD_THR_SLEEP:
160       return "sleeping";
161     case TD_THR_STOPPED_ASLEEP:
162       return "stopped by debugger AND blocked";
163     default:
164       snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
165       return buf;
166     }
167 }
168 #endif
169
170 static int
171 thread_db_create_event (CORE_ADDR where)
172 {
173   td_event_msg_t msg;
174   td_err_e err;
175   struct lwp_info *lwp;
176   struct thread_db *thread_db = current_process ()->private->thread_db;
177
178   if (thread_db->td_ta_event_getmsg_p == NULL)
179     fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
180
181   if (debug_threads)
182     fprintf (stderr, "Thread creation event.\n");
183
184   /* FIXME: This assumes we don't get another event.
185      In the LinuxThreads implementation, this is safe,
186      because all events come from the manager thread
187      (except for its own creation, of course).  */
188   err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
189   if (err != TD_OK)
190     fprintf (stderr, "thread getmsg err: %s\n",
191              thread_db_err_str (err));
192
193   /* If we do not know about the main thread yet, this would be a good time to
194      find it.  We need to do this to pick up the main thread before any newly
195      created threads.  */
196   lwp = get_thread_lwp (current_inferior);
197   if (lwp->thread_known == 0)
198     find_one_thread (lwp->head.id);
199
200   /* msg.event == TD_EVENT_CREATE */
201
202   find_new_threads_callback (msg.th_p, NULL);
203
204   return 0;
205 }
206
207 static int
208 thread_db_enable_reporting ()
209 {
210   td_thr_events_t events;
211   td_notify_t notify;
212   td_err_e err;
213   struct thread_db *thread_db = current_process ()->private->thread_db;
214
215   if (thread_db->td_ta_set_event_p == NULL
216       || thread_db->td_ta_event_addr_p == NULL
217       || thread_db->td_ta_event_getmsg_p == NULL)
218     /* This libthread_db is missing required support.  */
219     return 0;
220
221   /* Set the process wide mask saying which events we're interested in.  */
222   td_event_emptyset (&events);
223   td_event_addset (&events, TD_CREATE);
224
225   err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
226   if (err != TD_OK)
227     {
228       warning ("Unable to set global thread event mask: %s",
229                thread_db_err_str (err));
230       return 0;
231     }
232
233   /* Get address for thread creation breakpoint.  */
234   err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
235                                        &notify);
236   if (err != TD_OK)
237     {
238       warning ("Unable to get location for thread creation breakpoint: %s",
239                thread_db_err_str (err));
240       return 0;
241     }
242   set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
243                      thread_db_create_event);
244
245   return 1;
246 }
247
248 static int
249 find_one_thread (ptid_t ptid)
250 {
251   td_thrhandle_t th;
252   td_thrinfo_t ti;
253   td_err_e err;
254   struct thread_info *inferior;
255   struct lwp_info *lwp;
256   struct thread_db *thread_db = current_process ()->private->thread_db;
257   int lwpid = ptid_get_lwp (ptid);
258
259   inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
260   lwp = get_thread_lwp (inferior);
261   if (lwp->thread_known)
262     return 1;
263
264   /* Get information about this thread.  */
265   err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
266   if (err != TD_OK)
267     error ("Cannot get thread handle for LWP %d: %s",
268            lwpid, thread_db_err_str (err));
269
270   err = thread_db->td_thr_get_info_p (&th, &ti);
271   if (err != TD_OK)
272     error ("Cannot get thread info for LWP %d: %s",
273            lwpid, thread_db_err_str (err));
274
275   if (debug_threads)
276     fprintf (stderr, "Found thread %ld (LWP %d)\n",
277              ti.ti_tid, ti.ti_lid);
278
279   if (lwpid != ti.ti_lid)
280     {
281       warning ("PID mismatch!  Expected %ld, got %ld",
282                (long) lwpid, (long) ti.ti_lid);
283       return 0;
284     }
285
286   if (thread_db_use_events)
287     {
288       err = thread_db->td_thr_event_enable_p (&th, 1);
289       if (err != TD_OK)
290         error ("Cannot enable thread event reporting for %d: %s",
291                ti.ti_lid, thread_db_err_str (err));
292     }
293
294   /* If the new thread ID is zero, a final thread ID will be available
295      later.  Do not enable thread debugging yet.  */
296   if (ti.ti_tid == 0)
297     return 0;
298
299   lwp->thread_known = 1;
300   lwp->th = th;
301
302   return 1;
303 }
304
305 /* Attach a thread.  Return true on success.  */
306
307 static int
308 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
309 {
310   struct lwp_info *lwp;
311
312   if (debug_threads)
313     fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
314              ti_p->ti_tid, ti_p->ti_lid);
315   linux_attach_lwp (ti_p->ti_lid);
316   lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
317   if (lwp == NULL)
318     {
319       warning ("Could not attach to thread %ld (LWP %d)\n",
320                ti_p->ti_tid, ti_p->ti_lid);
321       return 0;
322     }
323
324   lwp->thread_known = 1;
325   lwp->th = *th_p;
326
327   if (thread_db_use_events)
328     {
329       td_err_e err;
330       struct thread_db *thread_db = current_process ()->private->thread_db;
331
332       err = thread_db->td_thr_event_enable_p (th_p, 1);
333       if (err != TD_OK)
334         error ("Cannot enable thread event reporting for %d: %s",
335                ti_p->ti_lid, thread_db_err_str (err));
336     }
337
338   return 1;
339 }
340
341 /* Attach thread if we haven't seen it yet.
342    Increment *COUNTER if we have attached a new thread.
343    Return false on failure.  */
344
345 static int
346 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
347                      int *counter)
348 {
349   struct lwp_info *lwp;
350
351   lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
352   if (lwp != NULL)
353     return 1;
354
355   if (!attach_thread (th_p, ti_p))
356     return 0;
357
358   if (counter != NULL)
359     *counter += 1;
360
361   return 1;
362 }
363
364 static int
365 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
366 {
367   td_thrinfo_t ti;
368   td_err_e err;
369   struct thread_db *thread_db = current_process ()->private->thread_db;
370
371   err = thread_db->td_thr_get_info_p (th_p, &ti);
372   if (err != TD_OK)
373     error ("Cannot get thread info: %s", thread_db_err_str (err));
374
375   /* Check for zombies.  */
376   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
377     return 0;
378
379   if (!maybe_attach_thread (th_p, &ti, (int *) data))
380     {
381       /* Terminate iteration early: we might be looking at stale data in
382          the inferior.  The thread_db_find_new_threads will retry.  */
383       return 1;
384     }
385
386   return 0;
387 }
388
389 static void
390 thread_db_find_new_threads (void)
391 {
392   td_err_e err;
393   ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
394   struct thread_db *thread_db = current_process ()->private->thread_db;
395   int loop, iteration;
396
397   /* This function is only called when we first initialize thread_db.
398      First locate the initial thread.  If it is not ready for
399      debugging yet, then stop.  */
400   if (find_one_thread (ptid) == 0)
401     return;
402
403   /* Require 4 successive iterations which do not find any new threads.
404      The 4 is a heuristic: there is an inherent race here, and I have
405      seen that 2 iterations in a row are not always sufficient to
406      "capture" all threads.  */
407   for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
408     {
409       int new_thread_count = 0;
410
411       /* Iterate over all user-space threads to discover new threads.  */
412       err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
413                                          find_new_threads_callback,
414                                          &new_thread_count,
415                                          TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
416                                          TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
417       if (debug_threads)
418         fprintf (stderr, "Found %d threads in iteration %d.\n",
419                  new_thread_count, iteration);
420
421       if (new_thread_count != 0)
422         {
423           /* Found new threads.  Restart iteration from beginning.  */
424           loop = -1;
425         }
426     }
427   if (err != TD_OK)
428     error ("Cannot find new threads: %s", thread_db_err_str (err));
429 }
430
431 /* Cache all future symbols that thread_db might request.  We can not
432    request symbols at arbitrary states in the remote protocol, only
433    when the client tells us that new symbols are available.  So when
434    we load the thread library, make sure to check the entire list.  */
435
436 static void
437 thread_db_look_up_symbols (void)
438 {
439   struct thread_db *thread_db = current_process ()->private->thread_db;
440   const char **sym_list;
441   CORE_ADDR unused;
442
443   for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
444     look_up_one_symbol (*sym_list, &unused);
445 }
446
447 int
448 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
449                            CORE_ADDR load_module, CORE_ADDR *address)
450 {
451   psaddr_t addr;
452   td_err_e err;
453   struct lwp_info *lwp;
454   struct thread_info *saved_inferior;
455   struct process_info *proc;
456   struct thread_db *thread_db;
457
458   proc = get_thread_process (thread);
459   thread_db = proc->private->thread_db;
460
461   /* If the thread layer is not (yet) initialized, fail.  */
462   if (!proc->all_symbols_looked_up)
463     return TD_ERR;
464
465   if (thread_db->td_thr_tls_get_addr_p == NULL)
466     return -1;
467
468   lwp = get_thread_lwp (thread);
469   if (!lwp->thread_known)
470     find_one_thread (lwp->head.id);
471   if (!lwp->thread_known)
472     return TD_NOTHR;
473
474   saved_inferior = current_inferior;
475   current_inferior = thread;
476   /* Note the cast through uintptr_t: this interface only works if
477      a target address fits in a psaddr_t, which is a host pointer.
478      So a 32-bit debugger can not access 64-bit TLS through this.  */
479   err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
480                                           (psaddr_t) (uintptr_t) load_module,
481                                           offset, &addr);
482   current_inferior = saved_inferior;
483   if (err == TD_OK)
484     {
485       *address = (CORE_ADDR) (uintptr_t) addr;
486       return 0;
487     }
488   else
489     return err;
490 }
491
492 #ifdef USE_LIBTHREAD_DB_DIRECTLY
493
494 static int
495 thread_db_load_search (void)
496 {
497   td_err_e err;
498   struct thread_db tdb;
499   struct process_info *proc = current_process ();
500
501   if (proc->private->thread_db != NULL)
502     fatal ("unexpected: proc->private->thread_db != NULL");
503
504   tdb.td_ta_new_p = &td_ta_new;
505
506   /* Attempt to open a connection to the thread library.  */
507   err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
508   if (err != TD_OK)
509     {
510       if (debug_threads)
511         fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
512       return 0;
513     }
514
515   tdb.td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
516   tdb.td_thr_get_info_p = &td_thr_get_info;
517   tdb.td_ta_thr_iter_p = &td_ta_thr_iter;
518   tdb.td_symbol_list_p = &td_symbol_list;
519
520   /* This is required only when thread_db_use_events is on.  */
521   tdb.td_thr_event_enable_p = &td_thr_event_enable;
522
523   /* These are not essential.  */
524   tdb.td_ta_event_addr_p = &td_ta_event_addr;
525   tdb.td_ta_set_event_p = &td_ta_set_event;
526   tdb.td_ta_event_getmsg_p = &td_ta_event_getmsg;
527   tdb.td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
528
529   proc->private->thread_db = xmalloc (sizeof (tdb));
530   memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
531
532   return 1;
533 }
534
535 #else
536
537 static int
538 try_thread_db_load_1 (void *handle)
539 {
540   td_err_e err;
541   struct thread_db tdb;
542   struct process_info *proc = current_process ();
543
544   if (proc->private->thread_db != NULL)
545     fatal ("unexpected: proc->private->thread_db != NULL");
546
547   tdb.handle = handle;
548
549   /* Initialize pointers to the dynamic library functions we will use.
550      Essential functions first.  */
551
552 #define CHK(required, a)                                        \
553   do                                                            \
554     {                                                           \
555       if ((a) == NULL)                                          \
556         {                                                       \
557           if (debug_threads)                                    \
558             fprintf (stderr, "dlsym: %s\n", dlerror ());        \
559           if (required)                                         \
560             return 0;                                           \
561         }                                                       \
562     }                                                           \
563   while (0)
564
565   CHK (1, tdb.td_ta_new_p = dlsym (handle, "td_ta_new"));
566
567   /* Attempt to open a connection to the thread library.  */
568   err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
569   if (err != TD_OK)
570     {
571       if (debug_threads)
572         fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
573       return 0;
574     }
575
576   CHK (1, tdb.td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
577   CHK (1, tdb.td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
578   CHK (1, tdb.td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
579   CHK (1, tdb.td_symbol_list_p = dlsym (handle, "td_symbol_list"));
580
581   /* This is required only when thread_db_use_events is on.  */
582   CHK (thread_db_use_events,
583        tdb.td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
584
585   /* These are not essential.  */
586   CHK (0, tdb.td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
587   CHK (0, tdb.td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
588   CHK (0, tdb.td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
589   CHK (0, tdb.td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
590
591 #undef CHK
592
593   proc->private->thread_db = xmalloc (sizeof (tdb));
594   memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
595
596   return 1;
597 }
598
599 /* Lookup a library in which given symbol resides.
600    Note: this is looking in the GDBSERVER process, not in the inferior.
601    Returns library name, or NULL.  */
602
603 static const char *
604 dladdr_to_soname (const void *addr)
605 {
606   Dl_info info;
607
608   if (dladdr (addr, &info) != 0)
609     return info.dli_fname;
610   return NULL;
611 }
612
613 static int
614 try_thread_db_load (const char *library)
615 {
616   void *handle;
617
618   if (debug_threads)
619     fprintf (stderr, "Trying host libthread_db library: %s.\n",
620              library);
621   handle = dlopen (library, RTLD_NOW);
622   if (handle == NULL)
623     {
624       if (debug_threads)
625         fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
626       return 0;
627     }
628
629   if (debug_threads && strchr (library, '/') == NULL)
630     {
631       void *td_init;
632
633       td_init = dlsym (handle, "td_init");
634       if (td_init != NULL)
635         {
636           const char *const libpath = dladdr_to_soname (td_init);
637
638           if (libpath != NULL)
639             fprintf (stderr, "Host %s resolved to: %s.\n",
640                      library, libpath);
641         }
642     }
643
644   if (try_thread_db_load_1 (handle))
645     return 1;
646
647   /* This library "refused" to work on current inferior.  */
648   dlclose (handle);
649   return 0;
650 }
651
652 static int
653 thread_db_load_search (void)
654 {
655   char path[PATH_MAX];
656   const char *search_path;
657   int rc = 0;
658
659   if (libthread_db_search_path == NULL)
660     libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
661
662   search_path = libthread_db_search_path;
663   while (*search_path)
664     {
665       const char *end = strchr (search_path, ':');
666       if (end)
667         {
668           size_t len = end - search_path;
669           if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
670             {
671               char *cp = xmalloc (len + 1);
672               memcpy (cp, search_path, len);
673               cp[len] = '\0';
674               warning ("libthread_db_search_path component too long, "
675                        "ignored: %s.", cp);
676               free (cp);
677               search_path += len + 1;
678               continue;
679             }
680           memcpy (path, search_path, len);
681           path[len] = '\0';
682           search_path += len + 1;
683         }
684       else
685         {
686           size_t len = strlen (search_path);
687
688           if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
689             {
690               warning ("libthread_db_search_path component too long,"
691                        " ignored: %s.", search_path);
692               break;
693             }
694           memcpy (path, search_path, len + 1);
695           search_path += len;
696         }
697       strcat (path, "/");
698       strcat (path, LIBTHREAD_DB_SO);
699       if (debug_threads)
700         fprintf (stderr, "thread_db_load_search trying %s\n", path);
701       if (try_thread_db_load (path))
702         {
703           rc = 1;
704           break;
705         }
706     }
707   if (rc == 0)
708     rc = try_thread_db_load (LIBTHREAD_DB_SO);
709
710   if (debug_threads)
711     fprintf (stderr, "thread_db_load_search returning %d\n", rc);
712   return rc;
713 }
714
715 #endif  /* USE_LIBTHREAD_DB_DIRECTLY */
716
717 int
718 thread_db_init (int use_events)
719 {
720   struct process_info *proc = current_process ();
721
722   /* FIXME drow/2004-10-16: This is the "overall process ID", which
723      GNU/Linux calls tgid, "thread group ID".  When we support
724      attaching to threads, the original thread may not be the correct
725      thread.  We would have to get the process ID from /proc for NPTL.
726      For LinuxThreads we could do something similar: follow the chain
727      of parent processes until we find the highest one we're attached
728      to, and use its tgid.
729
730      This isn't the only place in gdbserver that assumes that the first
731      process in the list is the thread group leader.  */
732
733   thread_db_use_events = use_events;
734
735   if (thread_db_load_search ())
736     {
737       if (use_events && thread_db_enable_reporting () == 0)
738         {
739           /* Keep trying; maybe event reporting will work later.  */
740           thread_db_free (proc);
741           return 0;
742         }
743       thread_db_find_new_threads ();
744       thread_db_look_up_symbols ();
745       proc->all_symbols_looked_up = 1;
746       return 1;
747     }
748
749   return 0;
750 }
751
752 /* Disconnect from libthread_db and free resources.  */
753
754 void
755 thread_db_free (struct process_info *proc)
756 {
757   struct thread_db *thread_db = proc->private->thread_db;
758   if (thread_db)
759     {
760 #ifndef USE_LIBTHREAD_DB_DIRECTLY
761       td_err_e (*td_ta_delete_p) (td_thragent_t *);
762
763       td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
764       if (td_ta_delete_p != NULL)
765         (*td_ta_delete_p) (thread_db->thread_agent);
766
767       dlclose (thread_db->handle);
768 #else
769       td_ta_delete (thread_db->thread_agent);
770 #endif  /* USE_LIBTHREAD_DB_DIRECTLY  */
771
772       free (thread_db);
773       proc->private->thread_db = NULL;
774     }
775 }
776
777 /* Handle "set libthread-db-search-path" monitor command and return 1.
778    For any other command, return 0.  */
779
780 int
781 thread_db_handle_monitor_command (char *mon)
782 {
783   if (strncmp (mon, "set libthread-db-search-path ", 29) == 0)
784     {
785       const char *cp = mon + 29;
786
787       if (libthread_db_search_path != NULL)
788         free (libthread_db_search_path);
789
790       /* Skip leading space (if any).  */
791       while (isspace (*cp))
792         ++cp;
793
794       libthread_db_search_path = xstrdup (cp);
795
796       monitor_output ("libthread-db-search-path set to `");
797       monitor_output (libthread_db_search_path);
798       monitor_output ("'\n");
799       return 1;
800     }
801
802   /* Tell server.c to perform default processing.  */
803   return 0;
804 }