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