* linux-thread-db.c: Include "linux-nat.h".
[deliverable/binutils-gdb.git] / gdb / linux-thread-db.c
1 /* libthread_db assisted debugging support, generic parts.
2
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24
25 #include "gdb_assert.h"
26 #include <dlfcn.h>
27 #include "gdb_proc_service.h"
28 #include "gdb_thread_db.h"
29
30 #include "bfd.h"
31 #include "exceptions.h"
32 #include "gdbthread.h"
33 #include "inferior.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "target.h"
37 #include "regcache.h"
38 #include "solib-svr4.h"
39 #include "gdbcore.h"
40 #include "linux-nat.h"
41
42 #ifdef HAVE_GNU_LIBC_VERSION_H
43 #include <gnu/libc-version.h>
44 #endif
45
46 #ifndef LIBTHREAD_DB_SO
47 #define LIBTHREAD_DB_SO "libthread_db.so.1"
48 #endif
49
50 /* If we're running on GNU/Linux, we must explicitly attach to any new
51 threads. */
52
53 /* FIXME: There is certainly some room for improvements:
54 - Cache LWP ids.
55 - Bypass libthread_db when fetching or storing registers for
56 threads bound to a LWP. */
57
58 /* This module's target vector. */
59 static struct target_ops thread_db_ops;
60
61 /* The target vector that we call for things this module can't handle. */
62 static struct target_ops *target_beneath;
63
64 /* Pointer to the next function on the objfile event chain. */
65 static void (*target_new_objfile_chain) (struct objfile * objfile);
66
67 /* Non-zero if we're using this module's target vector. */
68 static int using_thread_db;
69
70 /* Non-zero if we have determined the signals used by the threads
71 library. */
72 static int thread_signals;
73 static sigset_t thread_stop_set;
74 static sigset_t thread_print_set;
75
76 /* Structure that identifies the child process for the
77 <proc_service.h> interface. */
78 static struct ps_prochandle proc_handle;
79
80 /* Connection to the libthread_db library. */
81 static td_thragent_t *thread_agent;
82
83 /* Pointers to the libthread_db functions. */
84
85 static td_err_e (*td_init_p) (void);
86
87 static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
88 td_thragent_t **ta);
89 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
90 td_thrhandle_t *__th);
91 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
92 lwpid_t lwpid, td_thrhandle_t *th);
93 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
94 td_thr_iter_f *callback, void *cbdata_p,
95 td_thr_state_e state, int ti_pri,
96 sigset_t *ti_sigmask_p,
97 unsigned int ti_user_flags);
98 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
99 td_event_e event, td_notify_t *ptr);
100 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
101 td_thr_events_t *event);
102 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
103 td_event_msg_t *msg);
104
105 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
106 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
107 td_thrinfo_t *infop);
108 static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
109 gdb_prfpregset_t *regset);
110 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
111 prgregset_t gregs);
112 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
113 const gdb_prfpregset_t *fpregs);
114 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
115 prgregset_t gregs);
116 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
117 int event);
118
119 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
120 void *map_address,
121 size_t offset, void **address);
122
123 /* Location of the thread creation event breakpoint. The code at this
124 location in the child process will be called by the pthread library
125 whenever a new thread is created. By setting a special breakpoint
126 at this location, GDB can detect when a new thread is created. We
127 obtain this location via the td_ta_event_addr call. */
128 static CORE_ADDR td_create_bp_addr;
129
130 /* Location of the thread death event breakpoint. */
131 static CORE_ADDR td_death_bp_addr;
132
133 /* Prototypes for local functions. */
134 static void thread_db_find_new_threads (void);
135 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
136 const td_thrinfo_t *ti_p, int verbose);
137 static void detach_thread (ptid_t ptid, int verbose);
138 \f
139
140 /* Building process ids. */
141
142 #define GET_PID(ptid) ptid_get_pid (ptid)
143 #define GET_LWP(ptid) ptid_get_lwp (ptid)
144 #define GET_THREAD(ptid) ptid_get_tid (ptid)
145
146 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
147 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
148
149 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
150 \f
151
152 /* Use "struct private_thread_info" to cache thread state. This is
153 a substantial optimization. */
154
155 struct private_thread_info
156 {
157 /* Flag set when we see a TD_DEATH event for this thread. */
158 unsigned int dying:1;
159
160 /* Cached thread state. */
161 unsigned int th_valid:1;
162 unsigned int ti_valid:1;
163
164 td_thrhandle_t th;
165 td_thrinfo_t ti;
166 };
167 \f
168
169 static char *
170 thread_db_err_str (td_err_e err)
171 {
172 static char buf[64];
173
174 switch (err)
175 {
176 case TD_OK:
177 return "generic 'call succeeded'";
178 case TD_ERR:
179 return "generic error";
180 case TD_NOTHR:
181 return "no thread to satisfy query";
182 case TD_NOSV:
183 return "no sync handle to satisfy query";
184 case TD_NOLWP:
185 return "no LWP to satisfy query";
186 case TD_BADPH:
187 return "invalid process handle";
188 case TD_BADTH:
189 return "invalid thread handle";
190 case TD_BADSH:
191 return "invalid synchronization handle";
192 case TD_BADTA:
193 return "invalid thread agent";
194 case TD_BADKEY:
195 return "invalid key";
196 case TD_NOMSG:
197 return "no event message for getmsg";
198 case TD_NOFPREGS:
199 return "FPU register set not available";
200 case TD_NOLIBTHREAD:
201 return "application not linked with libthread";
202 case TD_NOEVENT:
203 return "requested event is not supported";
204 case TD_NOCAPAB:
205 return "capability not available";
206 case TD_DBERR:
207 return "debugger service failed";
208 case TD_NOAPLIC:
209 return "operation not applicable to";
210 case TD_NOTSD:
211 return "no thread-specific data for this thread";
212 case TD_MALLOC:
213 return "malloc failed";
214 case TD_PARTIALREG:
215 return "only part of register set was written/read";
216 case TD_NOXREGS:
217 return "X register set not available for this thread";
218 default:
219 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
220 return buf;
221 }
222 }
223
224 static char *
225 thread_db_state_str (td_thr_state_e state)
226 {
227 static char buf[64];
228
229 switch (state)
230 {
231 case TD_THR_STOPPED:
232 return "stopped by debugger";
233 case TD_THR_RUN:
234 return "runnable";
235 case TD_THR_ACTIVE:
236 return "active";
237 case TD_THR_ZOMBIE:
238 return "zombie";
239 case TD_THR_SLEEP:
240 return "sleeping";
241 case TD_THR_STOPPED_ASLEEP:
242 return "stopped by debugger AND blocked";
243 default:
244 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
245 return buf;
246 }
247 }
248 \f
249 /* A callback function for td_ta_thr_iter, which we use to map all
250 threads to LWPs.
251
252 THP is a handle to the current thread; if INFOP is not NULL, the
253 struct thread_info associated with this thread is returned in
254 *INFOP.
255
256 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
257 zero is returned to indicate success. */
258
259 static int
260 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
261 {
262 td_thrinfo_t ti;
263 td_err_e err;
264 struct thread_info *thread_info;
265 ptid_t thread_ptid;
266
267 err = td_thr_get_info_p (thp, &ti);
268 if (err != TD_OK)
269 error (_("thread_get_info_callback: cannot get thread info: %s"),
270 thread_db_err_str (err));
271
272 /* Fill the cache. */
273 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
274 thread_info = find_thread_pid (thread_ptid);
275
276 /* In the case of a zombie thread, don't continue. We don't want to
277 attach to it thinking it is a new thread. */
278 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
279 {
280 if (infop != NULL)
281 *(struct thread_info **) infop = thread_info;
282 if (thread_info != NULL)
283 {
284 memcpy (&thread_info->private->th, thp, sizeof (*thp));
285 thread_info->private->th_valid = 1;
286 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
287 thread_info->private->ti_valid = 1;
288 }
289 return TD_THR_ZOMBIE;
290 }
291
292 if (thread_info == NULL)
293 {
294 /* New thread. Attach to it now (why wait?). */
295 attach_thread (thread_ptid, thp, &ti, 1);
296 thread_info = find_thread_pid (thread_ptid);
297 gdb_assert (thread_info != NULL);
298 }
299
300 memcpy (&thread_info->private->th, thp, sizeof (*thp));
301 thread_info->private->th_valid = 1;
302 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
303 thread_info->private->ti_valid = 1;
304
305 if (infop != NULL)
306 *(struct thread_info **) infop = thread_info;
307
308 return 0;
309 }
310
311 /* Accessor functions for the thread_db information, with caching. */
312
313 static void
314 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
315 {
316 td_err_e err;
317
318 if (thread_info->private->th_valid)
319 return;
320
321 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
322 &thread_info->private->th);
323 if (err != TD_OK)
324 {
325 if (fatal)
326 error (_("Cannot find thread %ld: %s"),
327 (long) GET_THREAD (thread_info->ptid),
328 thread_db_err_str (err));
329 }
330 else
331 thread_info->private->th_valid = 1;
332 }
333
334 static td_thrinfo_t *
335 thread_db_get_info (struct thread_info *thread_info)
336 {
337 td_err_e err;
338
339 if (thread_info->private->ti_valid)
340 return &thread_info->private->ti;
341
342 if (!thread_info->private->th_valid)
343 thread_db_map_id2thr (thread_info, 1);
344
345 err =
346 td_thr_get_info_p (&thread_info->private->th, &thread_info->private->ti);
347 if (err != TD_OK)
348 error (_("thread_db_get_info: cannot get thread info: %s"),
349 thread_db_err_str (err));
350
351 thread_info->private->ti_valid = 1;
352 return &thread_info->private->ti;
353 }
354 \f
355 /* Convert between user-level thread ids and LWP ids. */
356
357 static ptid_t
358 thread_from_lwp (ptid_t ptid)
359 {
360 td_thrhandle_t th;
361 td_err_e err;
362 struct thread_info *thread_info;
363 ptid_t thread_ptid;
364
365 if (GET_LWP (ptid) == 0)
366 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
367
368 gdb_assert (is_lwp (ptid));
369
370 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
371 if (err != TD_OK)
372 error (_("Cannot find user-level thread for LWP %ld: %s"),
373 GET_LWP (ptid), thread_db_err_str (err));
374
375 thread_info = NULL;
376
377 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
378 event thread has already died. If another gdb interface has called
379 thread_alive() previously, the thread won't be found on the thread list
380 anymore. In that case, we don't want to process this ptid anymore
381 to avoid the possibility of later treating it as a newly
382 discovered thread id that we should add to the list. Thus,
383 we return a -1 ptid which is also how the thread list marks a
384 dead thread. */
385 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
386 && thread_info == NULL)
387 return pid_to_ptid (-1);
388
389 gdb_assert (thread_info && thread_info->private->ti_valid);
390
391 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
392 thread_info->private->ti.ti_tid);
393 }
394
395 static ptid_t
396 lwp_from_thread (ptid_t ptid)
397 {
398 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
399 }
400 \f
401
402 void
403 thread_db_init (struct target_ops *target)
404 {
405 target_beneath = target;
406 }
407
408 static void *
409 verbose_dlsym (void *handle, const char *name)
410 {
411 void *sym = dlsym (handle, name);
412 if (sym == NULL)
413 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
414 return sym;
415 }
416
417 static int
418 thread_db_load (void)
419 {
420 void *handle;
421 td_err_e err;
422
423 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
424 if (handle == NULL)
425 {
426 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
427 LIBTHREAD_DB_SO, dlerror ());
428 fprintf_filtered (gdb_stderr,
429 "GDB will not be able to debug pthreads.\n\n");
430 return 0;
431 }
432
433 /* Initialize pointers to the dynamic library functions we will use.
434 Essential functions first. */
435
436 td_init_p = verbose_dlsym (handle, "td_init");
437 if (td_init_p == NULL)
438 return 0;
439
440 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
441 if (td_ta_new_p == NULL)
442 return 0;
443
444 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
445 if (td_ta_map_id2thr_p == NULL)
446 return 0;
447
448 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
449 if (td_ta_map_lwp2thr_p == NULL)
450 return 0;
451
452 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
453 if (td_ta_thr_iter_p == NULL)
454 return 0;
455
456 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
457 if (td_thr_validate_p == NULL)
458 return 0;
459
460 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
461 if (td_thr_get_info_p == NULL)
462 return 0;
463
464 td_thr_getfpregs_p = verbose_dlsym (handle, "td_thr_getfpregs");
465 if (td_thr_getfpregs_p == NULL)
466 return 0;
467
468 td_thr_getgregs_p = verbose_dlsym (handle, "td_thr_getgregs");
469 if (td_thr_getgregs_p == NULL)
470 return 0;
471
472 td_thr_setfpregs_p = verbose_dlsym (handle, "td_thr_setfpregs");
473 if (td_thr_setfpregs_p == NULL)
474 return 0;
475
476 td_thr_setgregs_p = verbose_dlsym (handle, "td_thr_setgregs");
477 if (td_thr_setgregs_p == NULL)
478 return 0;
479
480 /* Initialize the library. */
481 err = td_init_p ();
482 if (err != TD_OK)
483 {
484 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
485 return 0;
486 }
487
488 /* These are not essential. */
489 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
490 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
491 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
492 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
493 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
494
495 return 1;
496 }
497
498 static td_err_e
499 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
500 {
501 td_notify_t notify;
502 td_err_e err;
503
504 /* Get the breakpoint address for thread EVENT. */
505 err = td_ta_event_addr_p (thread_agent, event, &notify);
506 if (err != TD_OK)
507 return err;
508
509 /* Set up the breakpoint. */
510 gdb_assert (exec_bfd);
511 (*bp) = (gdbarch_convert_from_func_ptr_addr
512 (current_gdbarch,
513 /* Do proper sign extension for the target. */
514 (bfd_get_sign_extend_vma (exec_bfd) > 0
515 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
516 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
517 &current_target));
518 create_thread_event_breakpoint ((*bp));
519
520 return TD_OK;
521 }
522
523 static void
524 enable_thread_event_reporting (void)
525 {
526 td_thr_events_t events;
527 td_notify_t notify;
528 td_err_e err;
529 #ifdef HAVE_GNU_LIBC_VERSION_H
530 const char *libc_version;
531 int libc_major, libc_minor;
532 #endif
533
534 /* We cannot use the thread event reporting facility if these
535 functions aren't available. */
536 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
537 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
538 return;
539
540 /* Set the process wide mask saying which events we're interested in. */
541 td_event_emptyset (&events);
542 td_event_addset (&events, TD_CREATE);
543
544 #ifdef HAVE_GNU_LIBC_VERSION_H
545 /* FIXME: kettenis/2000-04-23: The event reporting facility is
546 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
547 now. */
548 libc_version = gnu_get_libc_version ();
549 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
550 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
551 #endif
552 td_event_addset (&events, TD_DEATH);
553
554 err = td_ta_set_event_p (thread_agent, &events);
555 if (err != TD_OK)
556 {
557 warning (_("Unable to set global thread event mask: %s"),
558 thread_db_err_str (err));
559 return;
560 }
561
562 /* Delete previous thread event breakpoints, if any. */
563 remove_thread_event_breakpoints ();
564 td_create_bp_addr = 0;
565 td_death_bp_addr = 0;
566
567 /* Set up the thread creation event. */
568 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
569 if (err != TD_OK)
570 {
571 warning (_("Unable to get location for thread creation breakpoint: %s"),
572 thread_db_err_str (err));
573 return;
574 }
575
576 /* Set up the thread death event. */
577 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
578 if (err != TD_OK)
579 {
580 warning (_("Unable to get location for thread death breakpoint: %s"),
581 thread_db_err_str (err));
582 return;
583 }
584 }
585
586 static void
587 disable_thread_event_reporting (void)
588 {
589 td_thr_events_t events;
590
591 /* Set the process wide mask saying we aren't interested in any
592 events anymore. */
593 td_event_emptyset (&events);
594 td_ta_set_event_p (thread_agent, &events);
595
596 /* Delete thread event breakpoints, if any. */
597 remove_thread_event_breakpoints ();
598 td_create_bp_addr = 0;
599 td_death_bp_addr = 0;
600 }
601
602 static void
603 check_thread_signals (void)
604 {
605 #ifdef GET_THREAD_SIGNALS
606 if (!thread_signals)
607 {
608 sigset_t mask;
609 int i;
610
611 GET_THREAD_SIGNALS (&mask);
612 sigemptyset (&thread_stop_set);
613 sigemptyset (&thread_print_set);
614
615 for (i = 1; i < NSIG; i++)
616 {
617 if (sigismember (&mask, i))
618 {
619 if (signal_stop_update (target_signal_from_host (i), 0))
620 sigaddset (&thread_stop_set, i);
621 if (signal_print_update (target_signal_from_host (i), 0))
622 sigaddset (&thread_print_set, i);
623 thread_signals = 1;
624 }
625 }
626 }
627 #endif
628 }
629
630 /* Check whether thread_db is usable. This function is called when
631 an inferior is created (or otherwise acquired, e.g. attached to)
632 and when new shared libraries are loaded into a running process. */
633
634 void
635 check_for_thread_db (void)
636 {
637 td_err_e err;
638 static int already_loaded;
639
640 /* First time through, report that libthread_db was successfuly
641 loaded. Can't print this in in thread_db_load as, at that stage,
642 the interpreter and it's console haven't started. */
643
644 if (!already_loaded)
645 {
646 Dl_info info;
647 const char *library = NULL;
648 if (dladdr ((*td_ta_new_p), &info) != 0)
649 library = info.dli_fname;
650
651 /* Try dlinfo? */
652
653 if (library == NULL)
654 /* Paranoid - don't let a NULL path slip through. */
655 library = LIBTHREAD_DB_SO;
656
657 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
658 library);
659 already_loaded = 1;
660 }
661
662 if (using_thread_db)
663 /* Nothing to do. The thread library was already detected and the
664 target vector was already activated. */
665 return;
666
667 /* Don't attempt to use thread_db on targets which can not run
668 (executables not running yet, core files) for now. */
669 if (!target_has_execution)
670 return;
671
672 /* Initialize the structure that identifies the child process. */
673 proc_handle.pid = GET_PID (inferior_ptid);
674
675 /* Now attempt to open a connection to the thread library. */
676 err = td_ta_new_p (&proc_handle, &thread_agent);
677 switch (err)
678 {
679 case TD_NOLIBTHREAD:
680 /* No thread library was detected. */
681 break;
682
683 case TD_OK:
684 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
685
686 /* The thread library was detected. Activate the thread_db target. */
687 push_target (&thread_db_ops);
688 using_thread_db = 1;
689
690 enable_thread_event_reporting ();
691 thread_db_find_new_threads ();
692 break;
693
694 default:
695 warning (_("Cannot initialize thread debugging library: %s"),
696 thread_db_err_str (err));
697 break;
698 }
699 }
700
701 static void
702 thread_db_new_objfile (struct objfile *objfile)
703 {
704 if (objfile != NULL)
705 check_for_thread_db ();
706
707 if (target_new_objfile_chain)
708 target_new_objfile_chain (objfile);
709 }
710
711 /* Attach to a new thread. This function is called when we receive a
712 TD_CREATE event or when we iterate over all threads and find one
713 that wasn't already in our list. */
714
715 static void
716 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
717 const td_thrinfo_t *ti_p, int verbose)
718 {
719 struct thread_info *tp;
720 td_err_e err;
721
722 /* If we're being called after a TD_CREATE event, we may already
723 know about this thread. There are two ways this can happen. We
724 may have iterated over all threads between the thread creation
725 and the TD_CREATE event, for instance when the user has issued
726 the `info threads' command before the SIGTRAP for hitting the
727 thread creation breakpoint was reported. Alternatively, the
728 thread may have exited and a new one been created with the same
729 thread ID. In the first case we don't need to do anything; in
730 the second case we should discard information about the dead
731 thread and attach to the new one. */
732 if (in_thread_list (ptid))
733 {
734 tp = find_thread_pid (ptid);
735 gdb_assert (tp != NULL);
736
737 if (!tp->private->dying)
738 return;
739
740 delete_thread (ptid);
741 }
742
743 check_thread_signals ();
744
745 /* Add the thread to GDB's thread list. */
746 tp = add_thread (ptid);
747 tp->private = xmalloc (sizeof (struct private_thread_info));
748 memset (tp->private, 0, sizeof (struct private_thread_info));
749
750 if (verbose)
751 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
752
753 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
754 return; /* A zombie thread -- do not attach. */
755
756 /* Under GNU/Linux, we have to attach to each and every thread. */
757 #ifdef ATTACH_LWP
758 ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0);
759 #endif
760
761 /* Enable thread event reporting for this thread. */
762 err = td_thr_event_enable_p (th_p, 1);
763 if (err != TD_OK)
764 error (_("Cannot enable thread event reporting for %s: %s"),
765 target_pid_to_str (ptid), thread_db_err_str (err));
766 }
767
768 static void
769 thread_db_attach (char *args, int from_tty)
770 {
771 target_beneath->to_attach (args, from_tty);
772
773 /* Destroy thread info; it's no longer valid. */
774 init_thread_list ();
775
776 /* The child process is now the actual multi-threaded
777 program. Snatch its process ID... */
778 proc_handle.pid = GET_PID (inferior_ptid);
779
780 /* ...and perform the remaining initialization steps. */
781 enable_thread_event_reporting ();
782 thread_db_find_new_threads ();
783 }
784
785 static void
786 detach_thread (ptid_t ptid, int verbose)
787 {
788 struct thread_info *thread_info;
789
790 if (verbose)
791 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
792
793 /* Don't delete the thread now, because it still reports as active
794 until it has executed a few instructions after the event
795 breakpoint - if we deleted it now, "info threads" would cause us
796 to re-attach to it. Just mark it as having had a TD_DEATH
797 event. This means that we won't delete it from our thread list
798 until we notice that it's dead (via prune_threads), or until
799 something re-uses its thread ID. */
800 thread_info = find_thread_pid (ptid);
801 gdb_assert (thread_info != NULL);
802 thread_info->private->dying = 1;
803 }
804
805 static void
806 thread_db_detach (char *args, int from_tty)
807 {
808 disable_thread_event_reporting ();
809
810 /* There's no need to save & restore inferior_ptid here, since the
811 inferior is supposed to be survive this function call. */
812 inferior_ptid = lwp_from_thread (inferior_ptid);
813
814 /* Forget about the child's process ID. We shouldn't need it
815 anymore. */
816 proc_handle.pid = 0;
817
818 target_beneath->to_detach (args, from_tty);
819 }
820
821 static int
822 clear_lwpid_callback (struct thread_info *thread, void *dummy)
823 {
824 /* If we know that our thread implementation is 1-to-1, we could save
825 a certain amount of information; it's not clear how much, so we
826 are always conservative. */
827
828 thread->private->th_valid = 0;
829 thread->private->ti_valid = 0;
830
831 return 0;
832 }
833
834 static void
835 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
836 {
837 struct cleanup *old_chain = save_inferior_ptid ();
838
839 if (GET_PID (ptid) == -1)
840 inferior_ptid = lwp_from_thread (inferior_ptid);
841 else if (is_thread (ptid))
842 ptid = lwp_from_thread (ptid);
843
844 /* Clear cached data which may not be valid after the resume. */
845 iterate_over_threads (clear_lwpid_callback, NULL);
846
847 target_beneath->to_resume (ptid, step, signo);
848
849 do_cleanups (old_chain);
850 }
851
852 /* Check if PID is currently stopped at the location of a thread event
853 breakpoint location. If it is, read the event message and act upon
854 the event. */
855
856 static void
857 check_event (ptid_t ptid)
858 {
859 td_event_msg_t msg;
860 td_thrinfo_t ti;
861 td_err_e err;
862 CORE_ADDR stop_pc;
863 int loop = 0;
864
865 /* Bail out early if we're not at a thread event breakpoint. */
866 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
867 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
868 return;
869
870 /* If we are at a create breakpoint, we do not know what new lwp
871 was created and cannot specifically locate the event message for it.
872 We have to call td_ta_event_getmsg() to get
873 the latest message. Since we have no way of correlating whether
874 the event message we get back corresponds to our breakpoint, we must
875 loop and read all event messages, processing them appropriately.
876 This guarantees we will process the correct message before continuing
877 from the breakpoint.
878
879 Currently, death events are not enabled. If they are enabled,
880 the death event can use the td_thr_event_getmsg() interface to
881 get the message specifically for that lwp and avoid looping
882 below. */
883
884 loop = 1;
885
886 do
887 {
888 err = td_ta_event_getmsg_p (thread_agent, &msg);
889 if (err != TD_OK)
890 {
891 if (err == TD_NOMSG)
892 return;
893
894 error (_("Cannot get thread event message: %s"),
895 thread_db_err_str (err));
896 }
897
898 err = td_thr_get_info_p (msg.th_p, &ti);
899 if (err != TD_OK)
900 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
901
902 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
903
904 switch (msg.event)
905 {
906 case TD_CREATE:
907 /* Call attach_thread whether or not we already know about a
908 thread with this thread ID. */
909 attach_thread (ptid, msg.th_p, &ti, 1);
910
911 break;
912
913 case TD_DEATH:
914
915 if (!in_thread_list (ptid))
916 error (_("Spurious thread death event."));
917
918 detach_thread (ptid, 1);
919
920 break;
921
922 default:
923 error (_("Spurious thread event."));
924 }
925 }
926 while (loop);
927 }
928
929 static ptid_t
930 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
931 {
932 extern ptid_t trap_ptid;
933
934 if (GET_PID (ptid) != -1 && is_thread (ptid))
935 ptid = lwp_from_thread (ptid);
936
937 ptid = target_beneath->to_wait (ptid, ourstatus);
938
939 if (proc_handle.pid == 0)
940 /* The current child process isn't the actual multi-threaded
941 program yet, so don't try to do any special thread-specific
942 post-processing and bail out early. */
943 return ptid;
944
945 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
946 return pid_to_ptid (-1);
947
948 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
949 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
950 /* Check for a thread event. */
951 check_event (ptid);
952
953 if (!ptid_equal (trap_ptid, null_ptid))
954 trap_ptid = thread_from_lwp (trap_ptid);
955
956 /* Change the ptid back into the higher level PID + TID format.
957 If the thread is dead and no longer on the thread list, we will
958 get back a dead ptid. This can occur if the thread death event
959 gets postponed by other simultaneous events. In such a case,
960 we want to just ignore the event and continue on. */
961 ptid = thread_from_lwp (ptid);
962 if (GET_PID (ptid) == -1)
963 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
964
965 return ptid;
966 }
967
968 static LONGEST
969 thread_db_xfer_partial (struct target_ops *ops, enum target_object object,
970 const char *annex, gdb_byte *readbuf,
971 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
972 {
973 struct cleanup *old_chain = save_inferior_ptid ();
974 LONGEST xfer;
975
976 if (is_thread (inferior_ptid))
977 {
978 /* FIXME: This seems to be necessary to make sure breakpoints
979 are removed. */
980 if (!target_thread_alive (inferior_ptid))
981 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
982 else
983 inferior_ptid = lwp_from_thread (inferior_ptid);
984 }
985
986 xfer = target_beneath->to_xfer_partial (ops, object, annex,
987 readbuf, writebuf, offset, len);
988
989 do_cleanups (old_chain);
990 return xfer;
991 }
992
993 static void
994 thread_db_fetch_registers (int regno)
995 {
996 struct thread_info *thread_info;
997 prgregset_t gregset;
998 gdb_prfpregset_t fpregset;
999 td_err_e err;
1000
1001 if (!is_thread (inferior_ptid))
1002 {
1003 /* Pass the request to the target beneath us. */
1004 target_beneath->to_fetch_registers (regno);
1005 return;
1006 }
1007
1008 thread_info = find_thread_pid (inferior_ptid);
1009 thread_db_map_id2thr (thread_info, 1);
1010
1011 err = td_thr_getgregs_p (&thread_info->private->th, gregset);
1012 if (err != TD_OK)
1013 error (_("Cannot fetch general-purpose registers for thread %ld: %s"),
1014 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1015
1016 err = td_thr_getfpregs_p (&thread_info->private->th, &fpregset);
1017 if (err != TD_OK)
1018 error (_("Cannot get floating-point registers for thread %ld: %s"),
1019 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1020
1021 /* Note that we must call supply_gregset after calling the thread_db
1022 routines because the thread_db routines call ps_lgetgregs and
1023 friends which clobber GDB's register cache. */
1024 supply_gregset ((gdb_gregset_t *) gregset);
1025 supply_fpregset (&fpregset);
1026 }
1027
1028 static void
1029 thread_db_store_registers (int regno)
1030 {
1031 prgregset_t gregset;
1032 gdb_prfpregset_t fpregset;
1033 td_err_e err;
1034 struct thread_info *thread_info;
1035
1036 if (!is_thread (inferior_ptid))
1037 {
1038 /* Pass the request to the target beneath us. */
1039 target_beneath->to_store_registers (regno);
1040 return;
1041 }
1042
1043 thread_info = find_thread_pid (inferior_ptid);
1044 thread_db_map_id2thr (thread_info, 1);
1045
1046 if (regno != -1)
1047 {
1048 gdb_byte raw[MAX_REGISTER_SIZE];
1049
1050 deprecated_read_register_gen (regno, raw);
1051 thread_db_fetch_registers (-1);
1052 regcache_raw_supply (current_regcache, regno, raw);
1053 }
1054
1055 fill_gregset ((gdb_gregset_t *) gregset, -1);
1056 fill_fpregset (&fpregset, -1);
1057
1058 err = td_thr_setgregs_p (&thread_info->private->th, gregset);
1059 if (err != TD_OK)
1060 error (_("Cannot store general-purpose registers for thread %ld: %s"),
1061 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1062 err = td_thr_setfpregs_p (&thread_info->private->th, &fpregset);
1063 if (err != TD_OK)
1064 error (_("Cannot store floating-point registers for thread %ld: %s"),
1065 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1066 }
1067
1068 static void
1069 thread_db_kill (void)
1070 {
1071 /* There's no need to save & restore inferior_ptid here, since the
1072 inferior isn't supposed to survive this function call. */
1073 inferior_ptid = lwp_from_thread (inferior_ptid);
1074 target_beneath->to_kill ();
1075 }
1076
1077 static void
1078 thread_db_create_inferior (char *exec_file, char *allargs, char **env,
1079 int from_tty)
1080 {
1081 unpush_target (&thread_db_ops);
1082 using_thread_db = 0;
1083 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
1084 }
1085
1086 static void
1087 thread_db_post_startup_inferior (ptid_t ptid)
1088 {
1089 if (proc_handle.pid == 0)
1090 {
1091 /* The child process is now the actual multi-threaded
1092 program. Snatch its process ID... */
1093 proc_handle.pid = GET_PID (ptid);
1094
1095 /* ...and perform the remaining initialization steps. */
1096 enable_thread_event_reporting ();
1097 thread_db_find_new_threads ();
1098 }
1099 }
1100
1101 static void
1102 thread_db_mourn_inferior (void)
1103 {
1104 /* Forget about the child's process ID. We shouldn't need it
1105 anymore. */
1106 proc_handle.pid = 0;
1107
1108 target_beneath->to_mourn_inferior ();
1109
1110 /* Delete the old thread event breakpoints. Do this after mourning
1111 the inferior, so that we don't try to uninsert them. */
1112 remove_thread_event_breakpoints ();
1113
1114 /* Detach thread_db target ops. */
1115 unpush_target (&thread_db_ops);
1116 using_thread_db = 0;
1117 }
1118
1119 static int
1120 thread_db_thread_alive (ptid_t ptid)
1121 {
1122 td_thrhandle_t th;
1123 td_err_e err;
1124
1125 if (is_thread (ptid))
1126 {
1127 struct thread_info *thread_info;
1128 thread_info = find_thread_pid (ptid);
1129
1130 thread_db_map_id2thr (thread_info, 0);
1131 if (!thread_info->private->th_valid)
1132 return 0;
1133
1134 err = td_thr_validate_p (&thread_info->private->th);
1135 if (err != TD_OK)
1136 return 0;
1137
1138 if (!thread_info->private->ti_valid)
1139 {
1140 err =
1141 td_thr_get_info_p (&thread_info->private->th,
1142 &thread_info->private->ti);
1143 if (err != TD_OK)
1144 return 0;
1145 thread_info->private->ti_valid = 1;
1146 }
1147
1148 if (thread_info->private->ti.ti_state == TD_THR_UNKNOWN
1149 || thread_info->private->ti.ti_state == TD_THR_ZOMBIE)
1150 return 0; /* A zombie thread. */
1151
1152 return 1;
1153 }
1154
1155 if (target_beneath->to_thread_alive)
1156 return target_beneath->to_thread_alive (ptid);
1157
1158 return 0;
1159 }
1160
1161 static int
1162 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1163 {
1164 td_thrinfo_t ti;
1165 td_err_e err;
1166 ptid_t ptid;
1167
1168 err = td_thr_get_info_p (th_p, &ti);
1169 if (err != TD_OK)
1170 error (_("find_new_threads_callback: cannot get thread info: %s"),
1171 thread_db_err_str (err));
1172
1173 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1174 return 0; /* A zombie -- ignore. */
1175
1176 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
1177
1178 if (!in_thread_list (ptid))
1179 attach_thread (ptid, th_p, &ti, 1);
1180
1181 return 0;
1182 }
1183
1184 static void
1185 thread_db_find_new_threads (void)
1186 {
1187 td_err_e err;
1188
1189 /* Iterate over all user-space threads to discover new threads. */
1190 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1191 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1192 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1193 if (err != TD_OK)
1194 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1195 }
1196
1197 static char *
1198 thread_db_pid_to_str (ptid_t ptid)
1199 {
1200 if (is_thread (ptid))
1201 {
1202 static char buf[64];
1203 td_thrinfo_t *ti_p;
1204 td_err_e err;
1205 struct thread_info *thread_info;
1206
1207 thread_info = find_thread_pid (ptid);
1208 thread_db_map_id2thr (thread_info, 0);
1209 if (!thread_info->private->th_valid)
1210 {
1211 snprintf (buf, sizeof (buf), "Thread %ld (Missing)",
1212 GET_THREAD (ptid));
1213 return buf;
1214 }
1215
1216 ti_p = thread_db_get_info (thread_info);
1217
1218 if (ti_p->ti_state == TD_THR_ACTIVE && ti_p->ti_lid != 0)
1219 {
1220 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)",
1221 (long) ti_p->ti_tid, ti_p->ti_lid);
1222 }
1223 else
1224 {
1225 snprintf (buf, sizeof (buf), "Thread %ld (%s)",
1226 (long) ti_p->ti_tid,
1227 thread_db_state_str (ti_p->ti_state));
1228 }
1229
1230 return buf;
1231 }
1232
1233 if (target_beneath->to_pid_to_str (ptid))
1234 return target_beneath->to_pid_to_str (ptid);
1235
1236 return normal_pid_to_str (ptid);
1237 }
1238
1239 /* Get the address of the thread local variable in load module LM which
1240 is stored at OFFSET within the thread local storage for thread PTID. */
1241
1242 static CORE_ADDR
1243 thread_db_get_thread_local_address (ptid_t ptid,
1244 CORE_ADDR lm,
1245 CORE_ADDR offset)
1246 {
1247 if (is_thread (ptid))
1248 {
1249 td_err_e err;
1250 void *address;
1251 struct thread_info *thread_info;
1252
1253 /* glibc doesn't provide the needed interface. */
1254 if (!td_thr_tls_get_addr_p)
1255 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1256 _("No TLS library support"));
1257
1258 /* Caller should have verified that lm != 0. */
1259 gdb_assert (lm != 0);
1260
1261 /* Get info about the thread. */
1262 thread_info = find_thread_pid (ptid);
1263 thread_db_map_id2thr (thread_info, 1);
1264
1265 /* Finally, get the address of the variable. */
1266 err = td_thr_tls_get_addr_p (&thread_info->private->th, (void *) lm,
1267 offset, &address);
1268
1269 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1270 /* The memory hasn't been allocated, yet. */
1271 if (err == TD_NOTALLOC)
1272 /* Now, if libthread_db provided the initialization image's
1273 address, we *could* try to build a non-lvalue value from
1274 the initialization image. */
1275 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1276 _("TLS not allocated yet"));
1277 #endif
1278
1279 /* Something else went wrong. */
1280 if (err != TD_OK)
1281 throw_error (TLS_GENERIC_ERROR,
1282 (("%s")), thread_db_err_str (err));
1283
1284 /* Cast assuming host == target. Joy. */
1285 /* Do proper sign extension for the target. */
1286 gdb_assert (exec_bfd);
1287 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1288 ? (CORE_ADDR) (intptr_t) address
1289 : (CORE_ADDR) (uintptr_t) address);
1290 }
1291
1292 if (target_beneath->to_get_thread_local_address)
1293 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1294 else
1295 throw_error (TLS_GENERIC_ERROR,
1296 _("TLS not supported on this target"));
1297 }
1298
1299 static void
1300 init_thread_db_ops (void)
1301 {
1302 thread_db_ops.to_shortname = "multi-thread";
1303 thread_db_ops.to_longname = "multi-threaded child process.";
1304 thread_db_ops.to_doc = "Threads and pthreads support.";
1305 thread_db_ops.to_attach = thread_db_attach;
1306 thread_db_ops.to_detach = thread_db_detach;
1307 thread_db_ops.to_resume = thread_db_resume;
1308 thread_db_ops.to_wait = thread_db_wait;
1309 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
1310 thread_db_ops.to_store_registers = thread_db_store_registers;
1311 thread_db_ops.to_xfer_partial = thread_db_xfer_partial;
1312 thread_db_ops.to_kill = thread_db_kill;
1313 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1314 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1315 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1316 thread_db_ops.to_thread_alive = thread_db_thread_alive;
1317 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1318 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1319 thread_db_ops.to_stratum = thread_stratum;
1320 thread_db_ops.to_has_thread_control = tc_schedlock;
1321 thread_db_ops.to_get_thread_local_address
1322 = thread_db_get_thread_local_address;
1323 thread_db_ops.to_magic = OPS_MAGIC;
1324 }
1325
1326 void
1327 _initialize_thread_db (void)
1328 {
1329 /* Only initialize the module if we can load libthread_db. */
1330 if (thread_db_load ())
1331 {
1332 init_thread_db_ops ();
1333 add_target (&thread_db_ops);
1334
1335 /* Add ourselves to objfile event chain. */
1336 target_new_objfile_chain = deprecated_target_new_objfile_hook;
1337 deprecated_target_new_objfile_hook = thread_db_new_objfile;
1338 }
1339 }
This page took 0.057243 seconds and 4 git commands to generate.