1 /* Multi-threaded debugging support for the thread_db interface,
2 used on operating systems such as Solaris and Linux.
3 Copyright 1999 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This module implements a thread_stratum target that sits on top of
23 a normal process_stratum target (such as procfs or ptrace). The
24 process_stratum target must install this thread_stratum target when
25 it detects the presence of the thread_db shared library.
27 This module will then use the thread_db API to add thread-awareness
28 to the functionality provided by the process_stratum target (or in
29 some cases, to add user-level thread awareness on top of the
30 kernel-level thread awareness that is already provided by the
31 process_stratum target).
33 Solaris threads (for instance) are a multi-level thread implementation;
34 the kernel provides a Light Weight Process (LWP) which the procfs
35 process_stratum module is aware of. This module must then mediate
36 the relationship between kernel LWP threads and user (eg. posix)
39 Linux threads are likely to be different -- but the thread_db
40 library API should make the difference largely transparent to GDB.
44 /* The thread_db API provides a number of functions that give the caller
45 access to the inner workings of the child process's thread library.
46 We will be using the following (others may be added):
48 td_thr_validate Confirm valid "live" thread
49 td_thr_get_info Get info about a thread
50 td_thr_getgregs Get thread's general registers
51 td_thr_getfpregs Get thread's floating point registers
52 td_thr_setgregs Set thread's general registers
53 td_thr_setfpregs Set thread's floating point registers
54 td_ta_map_id2thr Get thread handle from thread id
55 td_ta_map_lwp2thr Get thread handle from LWP id
56 td_ta_thr_iter Iterate over all threads (with callback)
58 In return, the debugger has to provide certain services to the
59 thread_db library. Some of these aren't actually required to do
60 anything in practice. For instance, the thread_db expects to be
61 able to stop the child process and start it again: but in our
62 context, the child process will always be stopped already when we
63 invoke the thread_db library, so the functions that we provide for
64 the library to stop and start the child process are no-ops.
66 Here is the list of functions which we export to the thread_db
67 library, divided into no-op functions vs. functions that actually
72 ps_pstop Stop the child process
73 ps_pcontinue Continue the child process
74 ps_lstop Stop a specific LWP (kernel thread)
75 ps_lcontinue Continue an LWP
76 ps_lgetxregsize Get size of LWP's xregs (sparc)
77 ps_lgetxregs Get LWP's xregs (sparc)
78 ps_lsetxregs Set LWP's xregs (sparc)
80 Functions that have to do useful work:
82 ps_pglobal_lookup Get the address of a global symbol
83 ps_pdread Read memory, data segment
84 ps_ptread Read memory, text segment
85 ps_pdwrite Write memory, data segment
86 ps_ptwrite Write memory, text segment
87 ps_lgetregs Get LWP's general registers
88 ps_lgetfpregs Get LWP's floating point registers
89 ps_lsetregs Set LWP's general registers
90 ps_lsetfpregs Set LWP's floating point registers
91 ps_lgetLDT Get LWP's Local Descriptor Table (x86)
93 Thus, if we ask the thread_db library to give us the general registers
94 for user thread X, thread_db may figure out that user thread X is
95 actually mapped onto kernel thread Y. Thread_db does not know how
96 to obtain the registers for kernel thread Y, but GDB does, so thread_db
97 turns the request right back to us via the ps_lgetregs callback. */
100 #include "gdbthread.h"
102 #include "inferior.h"
105 #include "gdb_wait.h"
109 #if defined(USE_PROC_FS) || defined(HAVE_GREGSET_T)
110 #include <sys/procfs.h>
113 #if defined (HAVE_PROC_SERVICE_H)
114 #include <proc_service.h> /* defines incoming API (ps_* callbacks) */
116 #include "gdb_proc_service.h"
119 #if defined HAVE_STDINT_H /* Pre-5.2 systems don't have this header */
120 #if defined (HAVE_THREAD_DB_H)
121 #include <thread_db.h> /* defines outgoing API (td_thr_* calls) */
123 #include "gdb_thread_db.h"
126 #include <dlfcn.h> /* dynamic library interface */
129 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
130 #define PIDGET(PID) (((PID) & 0xffff))
131 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
134 /* Macros for superimposing PID and TID into inferior_pid. */
135 #define THREAD_FLAG 0x80000000
136 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
137 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
138 #define GET_LWP(PID) TIDGET (PID)
139 #define GET_THREAD(PID) TIDGET (PID)
140 #define BUILD_LWP(TID, PID) MERGEPID (PID, TID)
141 #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG)
144 * target_beneath is a pointer to the target_ops underlying this one.
147 static struct target_ops *target_beneath;
151 * target vector defined in this module:
154 static struct target_ops thread_db_ops;
157 * Typedefs required to resolve differences between the thread_db
158 * and proc_service API defined on different versions of Solaris:
161 #if defined(PROC_SERVICE_IS_OLD)
162 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
163 typedef char *gdb_ps_read_buf_t;
164 typedef char *gdb_ps_write_buf_t;
165 typedef int gdb_ps_size_t;
167 typedef struct ps_prochandle *gdb_ps_prochandle_t;
168 typedef void *gdb_ps_read_buf_t;
169 typedef const void *gdb_ps_write_buf_t;
170 typedef size_t gdb_ps_size_t;
173 /* Unfortunately glibc 2.1.3 was released with a broken prfpregset_t
174 type. We let configure check for this lossage, and make
175 appropriate typedefs here. */
177 #ifdef PRFPREGSET_T_BROKEN
178 typedef elf_fpregset_t gdb_prfpregset_t;
180 typedef prfpregset_t gdb_prfpregset_t;
184 * proc_service callback functions, called by thread_db.
188 ps_pstop (gdb_ps_prochandle_t ph) /* Process stop */
194 ps_pcontinue (gdb_ps_prochandle_t ph) /* Process continue */
200 ps_lstop (gdb_ps_prochandle_t ph, /* LWP stop */
207 ps_lcontinue (gdb_ps_prochandle_t ph, /* LWP continue */
214 ps_lgetxregsize (gdb_ps_prochandle_t ph, /* Get XREG size */
222 ps_lgetxregs (gdb_ps_prochandle_t ph, /* Get XREGS */
230 ps_lsetxregs (gdb_ps_prochandle_t ph, /* Set XREGS */
238 ps_plog (const char *fmt, ...)
242 va_start (args, fmt);
243 vfprintf_filtered (gdb_stderr, fmt, args);
246 /* Look up a symbol in GDB's global symbol table.
247 Return the symbol's address.
248 FIXME: it would be more correct to look up the symbol in the context
249 of the LD_OBJECT_NAME provided. However we're probably fairly safe
250 as long as there aren't name conflicts with other libraries. */
253 ps_pglobal_lookup (gdb_ps_prochandle_t ph,
254 const char *ld_object_name, /* the library name */
255 const char *ld_symbol_name, /* the symbol name */
256 paddr_t *ld_symbol_addr) /* return the symbol addr */
258 struct minimal_symbol *ms;
260 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
265 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
270 /* Worker function for all memory reads and writes: */
271 static ps_err_e rw_common (const struct ps_prochandle *ph,
277 /* target_xfer_memory direction consts */
278 enum {PS_READ = 0, PS_WRITE = 1};
281 ps_pdread (gdb_ps_prochandle_t ph, /* read from data segment */
283 gdb_ps_read_buf_t buf,
286 return rw_common (ph, addr, buf, size, PS_READ);
290 ps_pdwrite (gdb_ps_prochandle_t ph, /* write to data segment */
292 gdb_ps_write_buf_t buf,
295 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
299 ps_ptread (gdb_ps_prochandle_t ph, /* read from text segment */
301 gdb_ps_read_buf_t buf,
304 return rw_common (ph, addr, buf, size, PS_READ);
308 ps_ptwrite (gdb_ps_prochandle_t ph, /* write to text segment */
310 gdb_ps_write_buf_t buf,
313 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
316 static struct cleanup *save_inferior_pid (void);
317 static void restore_inferior_pid (void *saved_pid);
318 static char *thr_err_string (td_err_e);
319 static char *thr_state_string (td_thr_state_e);
321 struct ps_prochandle {
325 struct ps_prochandle main_prochandle;
326 td_thragent_t * main_threadagent;
329 * Common proc_service routine for reading and writing memory.
332 /* FIXME: once we've munged the inferior_pid, why can't we
333 simply call target_read/write_memory and return? */
337 rw_common (const struct ps_prochandle *ph,
343 struct cleanup *old_chain = save_inferior_pid ();
347 inferior_pid = main_prochandle.pid;
351 done = current_target.to_xfer_memory (addr, buf, size, write_p,
355 if (write_p == PS_READ)
356 print_sys_errmsg ("rw_common (): read", errno);
358 print_sys_errmsg ("rw_common (): write", errno);
365 do_cleanups (old_chain);
369 /* Cleanup functions used by the register callbacks
370 (which have to manipulate the global inferior_pid). */
373 ps_lgetregs (gdb_ps_prochandle_t ph, /* Get LWP general regs */
377 struct cleanup *old_chain = save_inferior_pid ();
379 inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
380 current_target.to_fetch_registers (-1);
382 fill_gregset (gregset, -1);
383 do_cleanups (old_chain);
389 ps_lsetregs (gdb_ps_prochandle_t ph, /* Set LWP general regs */
391 const prgregset_t gregset)
393 struct cleanup *old_chain = save_inferior_pid ();
395 inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
396 supply_gregset (gregset);
397 current_target.to_store_registers (-1);
398 do_cleanups (old_chain);
403 ps_lgetfpregs (gdb_ps_prochandle_t ph, /* Get LWP float regs */
405 gdb_prfpregset_t *fpregset)
407 struct cleanup *old_chain = save_inferior_pid ();
409 inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
410 current_target.to_fetch_registers (-1);
411 fill_fpregset (fpregset, -1);
412 do_cleanups (old_chain);
417 ps_lsetfpregs (gdb_ps_prochandle_t ph, /* Set LWP float regs */
419 const gdb_prfpregset_t *fpregset)
421 struct cleanup *old_chain = save_inferior_pid ();
423 inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
424 supply_fpregset (fpregset);
425 current_target.to_store_registers (-1);
426 do_cleanups (old_chain);
433 * return the main pid for the child process
434 * (special for Linux -- not used on Solaris)
438 ps_getpid (gdb_ps_prochandle_t ph)
445 /* Reads the local descriptor table of a LWP. */
448 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
451 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
452 extern struct ssd *procfs_find_LDT_entry (int);
455 ret = procfs_find_LDT_entry (BUILD_LWP (lwpid,
456 PIDGET (main_prochandle.pid)));
459 memcpy (pldt, ret, sizeof (struct ssd));
462 else /* LDT not found. */
465 #endif /* TM_I386SOL2_H */
468 * Pointers to thread_db functions:
470 * These are a dynamic library mechanism.
471 * The dlfcn.h interface will be used to initialize these
472 * so that they point to the appropriate functions in the
473 * thread_db dynamic library. This is done dynamically
474 * so that GDB can still run on systems that lack thread_db.
477 static td_err_e (*p_td_init) (void);
479 static td_err_e (*p_td_ta_new) (const struct ps_prochandle *ph_p,
480 td_thragent_t **ta_pp);
482 static td_err_e (*p_td_ta_delete) (td_thragent_t *ta_p);
484 static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p,
488 static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t *ta_p,
491 td_thr_state_e state,
493 sigset_t *ti_sigmask_p,
494 unsigned ti_user_flags);
496 static td_err_e (*p_td_ta_event_addr) (const td_thragent_t *ta_p,
498 td_notify_t *notify_p);
500 static td_err_e (*p_td_ta_event_getmsg) (const td_thragent_t *ta_p,
501 td_event_msg_t *msg);
503 static td_err_e (*p_td_ta_set_event) (const td_thragent_t *ta_p,
504 td_thr_events_t *events);
506 static td_err_e (*p_td_thr_validate) (const td_thrhandle_t *th_p);
508 static td_err_e (*p_td_thr_event_enable) (const td_thrhandle_t *th_p,
511 static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t *th_p,
514 static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t *th_p,
517 static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t *th_p,
518 const prgregset_t regset);
520 static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p,
521 gdb_prfpregset_t *fpregset);
523 static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p,
524 const gdb_prfpregset_t *fpregset);
526 static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p,
528 td_thrhandle_t *th_p);
530 static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p,
532 td_thrhandle_t *th_p);
535 * API and target vector initialization function: thread_db_initialize.
537 * NOTE: this function is deliberately NOT named with the GDB convention
538 * of module initializer function names that begin with "_initialize".
539 * This module is NOT intended to be auto-initialized at GDB startup.
540 * Rather, it will only be initialized when a multi-threaded child
541 * process is detected.
546 * Initializer for thread_db library interface.
547 * This function does the dynamic library stuff (dlopen, dlsym),
548 * and then calls the thread_db library's one-time initializer
549 * function (td_init). If everything succeeds, this function
550 * returns true; otherwise it returns false, and this module
555 init_thread_db_library ()
560 /* Open a handle to the "thread_db" dynamic library. */
561 if ((dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW)) == NULL)
564 /* Initialize pointers to the dynamic library functions we will use.
565 * Note that we are not calling the functions here -- we are only
566 * establishing pointers to them.
569 /* td_init: initialize thread_db library. */
570 if ((p_td_init = dlsym (dlhandle, "td_init")) == NULL)
572 /* td_ta_new: register a target process with thread_db. */
573 if ((p_td_ta_new = dlsym (dlhandle, "td_ta_new")) == NULL)
575 /* td_ta_delete: un-register a target process with thread_db. */
576 if ((p_td_ta_delete = dlsym (dlhandle, "td_ta_delete")) == NULL)
579 /* td_ta_map_id2thr: get thread handle from thread id. */
580 if ((p_td_ta_map_id2thr = dlsym (dlhandle, "td_ta_map_id2thr")) == NULL)
582 /* td_ta_map_lwp2thr: get thread handle from lwp id. */
583 if ((p_td_ta_map_lwp2thr = dlsym (dlhandle, "td_ta_map_lwp2thr")) == NULL)
585 /* td_ta_get_nthreads: get number of threads in target process. */
586 if ((p_td_ta_get_nthreads = dlsym (dlhandle, "td_ta_get_nthreads")) == NULL)
588 /* td_ta_thr_iter: iterate over all thread handles. */
589 if ((p_td_ta_thr_iter = dlsym (dlhandle, "td_ta_thr_iter")) == NULL)
592 /* td_thr_validate: make sure a thread handle is real and alive. */
593 if ((p_td_thr_validate = dlsym (dlhandle, "td_thr_validate")) == NULL)
595 /* td_thr_get_info: get a bunch of info about a thread. */
596 if ((p_td_thr_get_info = dlsym (dlhandle, "td_thr_get_info")) == NULL)
598 /* td_thr_getgregs: get general registers for thread. */
599 if ((p_td_thr_getgregs = dlsym (dlhandle, "td_thr_getgregs")) == NULL)
601 /* td_thr_setgregs: set general registers for thread. */
602 if ((p_td_thr_setgregs = dlsym (dlhandle, "td_thr_setgregs")) == NULL)
604 /* td_thr_getfpregs: get floating point registers for thread. */
605 if ((p_td_thr_getfpregs = dlsym (dlhandle, "td_thr_getfpregs")) == NULL)
607 /* td_thr_setfpregs: set floating point registers for thread. */
608 if ((p_td_thr_setfpregs = dlsym (dlhandle, "td_thr_setfpregs")) == NULL)
614 warning ("init_thread_db: td_init: %s", thr_err_string (ret));
618 /* Optional functions:
619 We can still debug even if the following functions are not found. */
621 /* td_ta_event_addr: get the breakpoint address for specified event. */
622 p_td_ta_event_addr = dlsym (dlhandle, "td_ta_event_addr");
624 /* td_ta_event_getmsg: get the next event message for the process. */
625 p_td_ta_event_getmsg = dlsym (dlhandle, "td_ta_event_getmsg");
627 /* td_ta_set_event: request notification of an event. */
628 p_td_ta_set_event = dlsym (dlhandle, "td_ta_set_event");
630 /* td_thr_event_enable: enable event reporting in a thread. */
631 p_td_thr_event_enable = dlsym (dlhandle, "td_thr_event_enable");
633 return 1; /* success */
637 * Local utility functions:
645 save_inferior_pid - Save inferior_pid on the cleanup list
646 restore_inferior_pid - Restore inferior_pid from the cleanup list
650 struct cleanup *save_inferior_pid (void);
651 void restore_inferior_pid (void *saved_pid);
655 These two functions act in unison to restore inferior_pid in
660 inferior_pid is a global variable that needs to be changed by many
661 of these routines before calling functions in procfs.c. In order
662 to guarantee that inferior_pid gets restored (in case of errors),
663 you need to call save_inferior_pid before changing it. At the end
664 of the function, you should invoke do_cleanups to restore it.
668 static struct cleanup *
669 save_inferior_pid (void)
673 saved_pid_ptr = xmalloc (sizeof (int));
674 *saved_pid_ptr = inferior_pid;
675 return make_cleanup (restore_inferior_pid, saved_pid_ptr);
679 restore_inferior_pid (void *arg)
681 int *saved_pid_ptr = arg;
682 inferior_pid = *saved_pid_ptr;
690 thr_err_string - Convert a thread_db error code to a string
694 char * thr_err_string (errcode)
698 Return a string description of the thread_db errcode. If errcode
699 is unknown, then return an <unknown> message.
704 thr_err_string (errcode)
710 case TD_OK: return "generic 'call succeeded'";
711 case TD_ERR: return "generic error";
712 case TD_NOTHR: return "no thread to satisfy query";
713 case TD_NOSV: return "no sync handle to satisfy query";
714 case TD_NOLWP: return "no lwp to satisfy query";
715 case TD_BADPH: return "invalid process handle";
716 case TD_BADTH: return "invalid thread handle";
717 case TD_BADSH: return "invalid synchronization handle";
718 case TD_BADTA: return "invalid thread agent";
719 case TD_BADKEY: return "invalid key";
720 case TD_NOMSG: return "no event message for getmsg";
721 case TD_NOFPREGS: return "FPU register set not available";
722 case TD_NOLIBTHREAD: return "application not linked with libthread";
723 case TD_NOEVENT: return "requested event is not supported";
724 case TD_NOCAPAB: return "capability not available";
725 case TD_DBERR: return "debugger service failed";
726 case TD_NOAPLIC: return "operation not applicable to";
727 case TD_NOTSD: return "no thread-specific data for this thread";
728 case TD_MALLOC: return "malloc failed";
729 case TD_PARTIALREG: return "only part of register set was written/read";
730 case TD_NOXREGS: return "X register set not available for this thread";
732 sprintf (buf, "unknown thread_db error '%d'", errcode);
741 thr_state_string - Convert a thread_db state code to a string
745 char *thr_state_string (statecode)
749 Return the thread_db state string associated with statecode.
750 If statecode is unknown, then return an <unknown> message.
755 thr_state_string (statecode)
756 td_thr_state_e statecode;
761 case TD_THR_STOPPED: return "stopped by debugger";
762 case TD_THR_RUN: return "runnable";
763 case TD_THR_ACTIVE: return "active";
764 case TD_THR_ZOMBIE: return "zombie";
765 case TD_THR_SLEEP: return "sleeping";
766 case TD_THR_STOPPED_ASLEEP: return "stopped by debugger AND blocked";
768 sprintf (buf, "unknown thread_db state %d", statecode);
774 * Local thread/event list.
775 * This data structure will be used to hold a list of threads and
776 * pending/deliverable events.
779 typedef struct THREADINFO {
780 thread_t tid; /* thread ID */
781 pid_t lid; /* process/lwp ID */
782 td_thr_state_e state; /* thread state (a la thread_db) */
783 td_thr_type_e type; /* thread type (a la thread_db) */
784 int pending; /* true if holding a pending event */
785 int status; /* wait status of any interesting event */
788 threadinfo * threadlist;
789 int threadlist_max = 0; /* current size of table */
790 int threadlist_top = 0; /* number of threads now in table */
791 #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */
794 insert_thread (tid, lid, state, type)
797 td_thr_state_e state;
800 if (threadlist_top >= threadlist_max)
802 threadlist_max += THREADLIST_ALLOC;
803 threadlist = realloc (threadlist,
804 threadlist_max * sizeof (threadinfo));
805 if (threadlist == NULL)
808 threadlist[threadlist_top].tid = tid;
809 threadlist[threadlist_top].lid = lid;
810 threadlist[threadlist_top].state = state;
811 threadlist[threadlist_top].type = type;
812 threadlist[threadlist_top].pending = 0;
813 threadlist[threadlist_top].status = 0;
815 return &threadlist[threadlist_top++];
825 next_pending_event ()
829 for (i = 0; i < threadlist_top; i++)
830 if (threadlist[i].pending)
831 return &threadlist[i];
837 threadlist_iter (func, data, state, type)
840 td_thr_state_e state;
845 for (i = 0; i < threadlist_top; i++)
846 if ((state == TD_THR_ANY_STATE || state == threadlist[i].state) &&
847 (type == TD_THR_ANY_TYPE || type == threadlist[i].type))
848 if ((*func) (&threadlist[i], data) != 0)
857 * Here we keep state information all collected in one place.
860 /* This flag is set when we activate, so that we don't do it twice.
861 Defined in linux-thread.c and used for inter-target syncronization. */
862 extern int using_thread_db;
864 /* The process id for which we've stopped.
865 * This is only set when we actually stop all threads.
866 * Otherwise it's zero.
868 static int event_pid;
871 * The process id for a new thread to which we've just attached.
872 * This process needs special handling at resume time.
874 static int attach_pid;
878 * thread_db event handling:
880 * The mechanism for event notification via the thread_db API.
881 * These events are implemented as breakpoints. The thread_db
882 * library gives us an address where we can set a breakpoint.
883 * When the breakpoint is hit, it represents an event of interest
890 /* Location of the thread creation event breakpoint. The code at this
891 location in the child process will be called by the pthread library
892 whenever a new thread is created. By setting a special breakpoint
893 at this location, GDB can detect when a new thread is created. We
894 obtain this location via the td_ta_event_addr call. */
896 static CORE_ADDR thread_creation_bkpt_address;
898 /* Location of the thread death event breakpoint. The code at this
899 location in the child process will be called by the pthread library
900 whenever a thread is destroyed. By setting a special breakpoint at
901 this location, GDB can detect when a new thread is created. We
902 obtain this location via the td_ta_event_addr call. */
904 static CORE_ADDR thread_death_bkpt_address;
906 /* This function handles the global parts of enabling thread events.
907 The thread-specific enabling is handled per-thread elsewhere. */
910 enable_thread_event_reporting (ta)
913 td_thr_events_t events;
917 if (p_td_ta_set_event == NULL ||
918 p_td_ta_event_addr == NULL ||
919 p_td_ta_event_getmsg == NULL ||
920 p_td_thr_event_enable == NULL)
921 return; /* can't do thread event reporting without these funcs */
923 /* set process wide mask saying which events we are interested in */
924 td_event_emptyset (&events);
925 td_event_addset (&events, TD_CREATE);
926 td_event_addset (&events, TD_DEATH);
928 if (p_td_ta_set_event (ta, &events) != TD_OK)
930 warning ("unable to set global thread event mask");
934 /* Delete previous thread event breakpoints, if any. */
935 remove_thread_event_breakpoints ();
937 /* create breakpoints -- thread creation and death */
938 /* thread creation */
939 /* get breakpoint location */
940 if (p_td_ta_event_addr (ta, TD_CREATE, ¬ify) != TD_OK)
942 warning ("unable to get location for thread creation breakpoint");
946 /* Set up the breakpoint. */
947 create_thread_event_breakpoint (notify.u.bptaddr);
949 /* Save it's location. */
950 thread_creation_bkpt_address = notify.u.bptaddr;
953 /* get breakpoint location */
954 if (p_td_ta_event_addr (ta, TD_DEATH, ¬ify) != TD_OK)
956 warning ("unable to get location for thread death breakpoint");
959 /* Set up the breakpoint. */
960 create_thread_event_breakpoint (notify.u.bptaddr);
962 /* Save it's location. */
963 thread_death_bkpt_address = notify.u.bptaddr;
966 /* This function handles the global parts of disabling thread events.
967 The thread-specific enabling is handled per-thread elsewhere. */
970 disable_thread_event_reporting (ta)
973 td_thr_events_t events;
975 /* set process wide mask saying we aren't interested in any events */
976 td_event_emptyset (&events);
977 p_td_ta_set_event (main_threadagent, &events);
979 /* Delete thread event breakpoints, if any. */
980 remove_thread_event_breakpoints ();
981 thread_creation_bkpt_address = 0;
982 thread_death_bkpt_address = 0;
985 /* check_for_thread_event
987 if it's a thread event we recognize (currently
988 we only recognize creation and destruction
989 events), return 1; else return 0. */
993 check_for_thread_event (struct target_waitstatus *tws, int event_pid)
995 /* FIXME: to be more efficient, we should keep a static
996 list of threads, and update it only here (with td_ta_thr_iter). */
1000 thread_db_push_target (void)
1002 /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */
1004 /* Push this target vector */
1005 push_target (&thread_db_ops);
1006 /* Find the underlying process-layer target for calling later. */
1007 target_beneath = find_target_beneath (&thread_db_ops);
1008 using_thread_db = 1;
1009 /* Turn on thread_db event-reporting API. */
1010 enable_thread_event_reporting (main_threadagent);
1014 thread_db_unpush_target (void)
1016 /* Must be called whenever we remove ourself from the target stack! */
1018 using_thread_db = 0;
1019 target_beneath = NULL;
1021 /* delete local list of threads */
1022 empty_threadlist ();
1023 /* Turn off the thread_db API. */
1024 p_td_ta_delete (main_threadagent);
1025 /* Unpush this target vector */
1026 unpush_target (&thread_db_ops);
1027 /* Reset linuxthreads module. */
1028 linuxthreads_discard_global_state ();
1032 * New objfile hook function:
1033 * Called for each new objfile (image, shared lib) in the target process.
1035 * The purpose of this function is to detect that the target process
1036 * is linked with the (appropriate) thread library. So every time a
1037 * new target shared library is detected, we will call td_ta_new.
1038 * If it succeeds, we know we have a multi-threaded target process
1039 * that we can debug using the thread_db API.
1043 * new_objfile function:
1045 * connected to target_new_objfile_hook, this function gets called
1046 * every time a new binary image is loaded.
1048 * At each call, we attempt to open the thread_db connection to the
1049 * child process. If it succeeds, we know we have a libthread process
1050 * and we can debug it with this target vector. Therefore we push
1051 * ourself onto the target stack.
1054 static void (*target_new_objfile_chain) (struct objfile *objfile);
1055 static int stop_or_attach_thread_callback (const td_thrhandle_t *th,
1057 static int wait_thread_callback (const td_thrhandle_t *th,
1061 thread_db_new_objfile (struct objfile *objfile)
1065 if (using_thread_db) /* libthread already detected, and */
1066 goto quit; /* thread target vector activated. */
1068 if (objfile == NULL)
1069 goto quit; /* un-interesting object file */
1071 /* Initialize our "main prochandle" with the main inferior pid. */
1072 main_prochandle.pid = PIDGET (inferior_pid);
1074 /* Now attempt to open a thread_db connection to the
1075 thread library running in the child process. */
1076 ret = p_td_ta_new (&main_prochandle, &main_threadagent);
1079 warning ("Unexpected error initializing thread_db: %s",
1080 thr_err_string (ret));
1082 case TD_NOLIBTHREAD: /* expected: no libthread in child process (yet) */
1084 case TD_OK: /* libthread detected in child: we go live now! */
1085 thread_db_push_target ();
1086 event_pid = inferior_pid; /* for resume */
1088 /* Now stop everyone else, and attach any new threads you find. */
1089 p_td_ta_thr_iter (main_threadagent,
1090 stop_or_attach_thread_callback,
1093 TD_THR_LOWEST_PRIORITY,
1095 TD_THR_ANY_USER_FLAGS);
1097 /* Now go call wait on all the threads you've stopped:
1098 This allows us to absorb the SIGKILL event, and to make sure
1099 that the thread knows that it is stopped (Linux peculiarity). */
1100 p_td_ta_thr_iter (main_threadagent,
1101 wait_thread_callback,
1104 TD_THR_LOWEST_PRIORITY,
1106 TD_THR_ANY_USER_FLAGS);
1111 if (target_new_objfile_chain)
1112 target_new_objfile_chain (objfile);
1120 thread_db_alive - test thread for "aliveness"
1124 static bool thread_db_alive (int pid);
1128 returns true if thread still active in inferior.
1133 thread_db_alive (pid)
1136 if (is_thread (pid)) /* user-space (non-kernel) thread */
1141 pid = GET_THREAD (pid);
1142 if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK)
1143 return 0; /* thread not found */
1144 if ((ret = p_td_thr_validate (&th)) != TD_OK)
1145 return 0; /* thread not valid */
1146 return 1; /* known thread: return true */
1148 else if (target_beneath->to_thread_alive)
1149 return target_beneath->to_thread_alive (pid);
1151 return 0; /* default to "not alive" (shouldn't happen anyway) */
1155 * get_lwp_from_thread_handle
1158 static int /* lwpid_t or pid_t */
1159 get_lwp_from_thread_handle (th)
1165 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1166 error ("get_lwp_from_thread_handle: thr_get_info failed: %s",
1167 thr_err_string (ret));
1173 * get_lwp_from_thread_id
1176 static int /* lwpid_t or pid_t */
1177 get_lwp_from_thread_id (tid)
1178 int tid; /* thread_t? */
1183 if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK)
1184 error ("get_lwp_from_thread_id: map_id2thr failed: %s",
1185 thr_err_string (ret));
1187 return get_lwp_from_thread_handle (&th);
1191 * pid_to_str has to handle user-space threads.
1192 * If not a user-space thread, then pass the request on to the
1193 * underlying stratum if it can handle it: else call normal_pid_to_str.
1197 thread_db_pid_to_str (int pid)
1199 static char buf[100];
1204 if (is_thread (pid))
1206 if ((ret = p_td_ta_map_id2thr (main_threadagent,
1209 error ("thread_db: map_id2thr failed: %s", thr_err_string (ret));
1211 if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK)
1212 error ("thread_db: thr_get_info failed: %s", thr_err_string (ret));
1214 if (ti.ti_state == TD_THR_ACTIVE &&
1216 sprintf (buf, "Thread %d (LWP %d)", ti.ti_tid, ti.ti_lid);
1218 sprintf (buf, "Thread %d (%s)", ti.ti_tid,
1219 thr_state_string (ti.ti_state));
1221 else if (GET_LWP (pid))
1222 sprintf (buf, "LWP %d", GET_LWP (pid));
1223 else return normal_pid_to_str (pid);
1229 * thread_db target vector functions:
1233 thread_db_files_info (struct target_ops *tgt_vector)
1235 /* This function will be unnecessary in real life. */
1236 printf_filtered ("thread_db stratum:\n");
1237 target_beneath->to_files_info (tgt_vector);
1241 * xfer_memory has to munge the inferior_pid before passing the call
1242 * down to the target layer.
1246 thread_db_xfer_memory (memaddr, myaddr, len, dowrite, target)
1251 struct target_ops *target; /* ignored */
1253 struct cleanup *old_chain;
1256 old_chain = save_inferior_pid ();
1258 if (is_thread (inferior_pid) ||
1259 !target_thread_alive (inferior_pid))
1261 /* FIXME: use the LID/LWP, so that underlying process layer
1262 can read memory from specific threads? */
1263 inferior_pid = main_prochandle.pid;
1266 ret = target_beneath->to_xfer_memory (memaddr, myaddr, len,
1268 do_cleanups (old_chain);
1273 * fetch_registers has to determine if inferior_pid is a user-space thread.
1274 * If so, we use the thread_db API to get the registers.
1275 * And if not, we call the underlying process stratum.
1279 thread_db_fetch_registers (regno)
1282 td_thrhandle_t thandle;
1283 gdb_prfpregset_t fpregset;
1284 prgregset_t gregset;
1288 if (!is_thread (inferior_pid)) /* kernel thread */
1289 { /* pass the request on to the target underneath. */
1290 target_beneath->to_fetch_registers (regno);
1294 /* convert inferior_pid into a td_thrhandle_t */
1296 if ((thread = GET_THREAD (inferior_pid)) == 0)
1297 error ("fetch_registers: thread == 0");
1299 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1300 error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret));
1302 /* Get the integer regs:
1303 For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7,
1304 pc and sp are saved (by a thread context switch). */
1305 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK &&
1306 ret != TD_PARTIALREG)
1307 error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret));
1309 /* And, now the fp regs */
1310 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK &&
1312 error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret));
1314 /* Note that we must call supply_{g fp}regset *after* calling the td routines
1315 because the td routines call ps_lget* which affect the values stored in the
1318 supply_gregset (gregset);
1319 supply_fpregset (&fpregset);
1324 * store_registers has to determine if inferior_pid is a user-space thread.
1325 * If so, we use the thread_db API to get the registers.
1326 * And if not, we call the underlying process stratum.
1330 thread_db_store_registers (regno)
1333 td_thrhandle_t thandle;
1334 gdb_prfpregset_t fpregset;
1335 prgregset_t gregset;
1339 if (!is_thread (inferior_pid)) /* Kernel thread: */
1340 { /* pass the request on to the underlying target vector. */
1341 target_beneath->to_store_registers (regno);
1345 /* convert inferior_pid into a td_thrhandle_t */
1347 if ((thread = GET_THREAD (inferior_pid)) == 0)
1348 error ("store_registers: thread == 0");
1350 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1351 error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret));
1354 { /* Not writing all the regs */
1355 /* save new register value */
1356 /* MVS: I don't understand this... */
1357 char old_value[REGISTER_SIZE];
1359 memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE);
1361 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK)
1362 error ("store_registers: td_thr_getgregs %s", thr_err_string (ret));
1363 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK)
1364 error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret));
1366 /* restore new register value */
1367 memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
1371 fill_gregset (gregset, regno);
1372 fill_fpregset (&fpregset, regno);
1374 if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK)
1375 error ("store_registers: td_thr_setgregs %s", thr_err_string (ret));
1376 if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK &&
1378 error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret));
1382 handle_new_thread (tid, lid, verbose)
1383 int tid; /* user thread id */
1384 int lid; /* kernel thread id */
1387 int gdb_pid = BUILD_THREAD (tid, main_prochandle.pid);
1388 int wait_pid, wait_status;
1391 printf_filtered ("[New %s]\n", target_pid_to_str (gdb_pid));
1392 add_thread (gdb_pid);
1394 if (lid != main_prochandle.pid)
1396 attach_thread (lid);
1397 /* According to the Eric Paire model, we now have to send
1398 the restart signal to the new thread -- however, empirically,
1399 I do not find that to be necessary. */
1405 test_for_new_thread (tid, lid, verbose)
1410 if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid)))
1411 handle_new_thread (tid, lid, verbose);
1415 * Callback function that gets called once per USER thread
1416 * (i.e., not kernel) thread by td_ta_thr_iter.
1420 find_new_threads_callback (th, ignored)
1421 const td_thrhandle_t *th;
1427 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1429 warning ("find_new_threads_callback: %s", thr_err_string (ret));
1430 return -1; /* bail out, get_info failed. */
1434 As things now stand, this should never detect a new thread.
1435 But if it does, we could be in trouble because we aren't calling
1436 wait_thread_callback for it. */
1437 test_for_new_thread (ti.ti_tid, ti.ti_lid, 0);
1442 * find_new_threads uses the thread_db iterator function to discover
1443 * user-space threads. Then if the underlying process stratum has a
1444 * find_new_threads method, we call that too.
1448 thread_db_find_new_threads ()
1450 if (inferior_pid == -1) /* FIXME: still necessary? */
1452 printf_filtered ("No process.\n");
1455 p_td_ta_thr_iter (main_threadagent,
1456 find_new_threads_callback,
1459 TD_THR_LOWEST_PRIORITY,
1461 TD_THR_ANY_USER_FLAGS);
1462 if (target_beneath->to_find_new_threads)
1463 target_beneath->to_find_new_threads ();
1467 * Resume all threads, or resume a single thread.
1468 * If step is true, then single-step the appropriate thread
1469 * (or single-step inferior_pid, but continue everyone else).
1470 * If signo is true, then send that signal to at least one thread.
1474 * This function is called once for each thread before resuming.
1475 * It sends continue (no step, and no signal) to each thread except
1476 * the main thread, and
1477 * the event thread (the one that stopped at a breakpoint etc.)
1479 * The event thread is handled separately so that it can be sent
1480 * the stepping and signal args with which target_resume was called.
1482 * The main thread is resumed last, so that the thread_db proc_service
1483 * callbacks will still work during the iterator function.
1487 resume_thread_callback (th, data)
1488 const td_thrhandle_t *th;
1494 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1496 warning ("resume_thread_callback: %s", thr_err_string (ret));
1497 return -1; /* bail out, get_info failed. */
1500 As things now stand, this should never detect a new thread.
1501 But if it does, we could be in trouble because we aren't calling
1502 wait_thread_callback for it. */
1503 test_for_new_thread (ti.ti_tid, ti.ti_lid, 1);
1505 if (ti.ti_lid != main_prochandle.pid &&
1506 ti.ti_lid != event_pid)
1508 /* Unconditionally continue the thread with no signal.
1509 Only the event thread will get a signal of any kind. */
1511 target_beneath->to_resume (ti.ti_lid, 0, 0);
1517 new_resume_thread_callback (thread, data)
1521 if (thread->lid != event_pid &&
1522 thread->lid != main_prochandle.pid)
1524 /* Unconditionally continue the thread with no signal (for now). */
1526 target_beneath->to_resume (thread->lid, 0, 0);
1531 static int last_resume_pid;
1532 static int last_resume_step;
1533 static int last_resume_signo;
1536 thread_db_resume (pid, step, signo)
1539 enum target_signal signo;
1541 last_resume_pid = pid;
1542 last_resume_step = step;
1543 last_resume_signo = signo;
1545 /* resuming a specific pid? */
1548 if (is_thread (pid))
1549 pid = get_lwp_from_thread_id (GET_THREAD (pid));
1550 else if (GET_LWP (pid))
1551 pid = GET_LWP (pid);
1554 /* Apparently the interpretation of 'pid' is dependent on 'step':
1555 If step is true, then a specific pid means 'step only this pid'.
1556 But if step is not true, then pid means 'continue ALL pids, but
1557 give the signal only to this one'. */
1558 if (pid != -1 && step)
1560 /* FIXME: is this gonna work in all circumstances? */
1561 target_beneath->to_resume (pid, step, signo);
1565 /* 1) Continue all threads except the event thread and the main thread.
1566 2) resume the event thread with step and signo.
1567 3) If event thread != main thread, continue the main thread.
1569 Note: order of 2 and 3 may need to be reversed. */
1571 threadlist_iter (new_resume_thread_callback,
1575 /* now resume event thread, and if necessary also main thread. */
1578 target_beneath->to_resume (event_pid, step, signo);
1580 if (event_pid != main_prochandle.pid)
1582 target_beneath->to_resume (main_prochandle.pid, 0, 0);
1587 /* All new threads will be attached.
1588 All previously known threads will be stopped using kill (SIGKILL). */
1591 stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data)
1598 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1600 warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret));
1601 return -1; /* bail out, get_info failed. */
1604 /* First add it to our internal list.
1605 We build this list anew at every wait event. */
1606 insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type);
1607 /* Now: if we've already seen it, stop it, else add it and attach it. */
1608 gdb_pid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
1609 if (!in_thread_list (gdb_pid)) /* new thread */
1611 handle_new_thread (ti.ti_tid, ti.ti_lid, 1);
1612 /* Enable thread events */
1613 if (p_td_thr_event_enable)
1614 if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK)
1615 warning ("stop_or_attach_thread: %s", thr_err_string (ret));
1617 else if (ti.ti_lid != event_pid &&
1618 ti.ti_lid != main_prochandle.pid)
1620 ret = (td_err_e) kill (ti.ti_lid, SIGSTOP);
1627 * Wait for signal N from pid PID.
1628 * If wait returns any other signals, put them back before returning.
1639 /* Array of wait/signal status */
1640 /* FIXME: wrong data structure, we need a queue.
1641 Realtime signals may be delivered more than once.
1642 And at that, we really can't handle them (see below). */
1644 static int wstatus [NSIG];
1645 #elif defined (_NSIG)
1646 static int wstatus [_NSIG];
1648 #error No definition for number of signals!
1651 /* clear wait/status list */
1652 memset (&wstatus, 0, sizeof (wstatus));
1654 /* Now look for SIGSTOP event on all threads except event thread. */
1657 if (pid == main_prochandle.pid)
1658 retpid = waitpid (pid, &status, 0);
1660 retpid = waitpid (pid, &status, __WCLONE);
1663 if (WSTOPSIG (status) == SIGSTOP)
1665 /* Got the SIGSTOP event we're looking for.
1666 Throw it away, and throw any other events back! */
1667 for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++)
1673 break; /* all done */
1678 /* Oops, got an event other than SIGSTOP.
1679 Save it, and throw it back after we find the SIGSTOP event. */
1681 /* FIXME (how?) This method is going to fail for realtime
1682 signals, which cannot be put back simply by using kill. */
1684 if (WIFEXITED (status))
1685 error ("Ack! Thread Exited event. What do I do now???");
1686 else if (WIFSTOPPED (status))
1687 signo = WSTOPSIG (status);
1689 signo = WTERMSIG (status);
1691 /* If a thread other than the event thread has hit a GDB
1692 breakpoint (as opposed to some random trap signal), then
1693 just arrange for it to hit it again later. Back up the
1694 PC if necessary. Don't forward the SIGTRAP signal to
1695 the thread. We will handle the current event, eventually
1696 we will resume all the threads, and this one will get
1697 it's breakpoint trap again.
1699 If we do not do this, then we run the risk that the user
1700 will delete or disable the breakpoint, but the thread will
1701 have already tripped on it. */
1703 if (retpid != event_pid &&
1705 breakpoint_inserted_here_p (read_pc_pid (retpid) -
1706 DECR_PC_AFTER_BREAK))
1708 /* Set the pc to before the trap and DO NOT re-send the signal */
1709 if (DECR_PC_AFTER_BREAK)
1710 write_pc_pid (read_pc_pid (retpid) - DECR_PC_AFTER_BREAK,
1714 /* Since SIGINT gets forwarded to the entire process group
1715 (in the case where ^C is typed at the tty / console),
1716 just ignore all SIGINTs from other than the event thread. */
1717 else if (retpid != event_pid && signo == SIGINT)
1718 { /* do nothing. Signal will disappear into oblivion! */
1722 else /* This is some random signal other than a breakpoint. */
1724 wstatus [signo] = 1;
1726 child_resume (retpid, 0, TARGET_SIGNAL_0);
1730 } while (errno == 0 || errno == EINTR);
1734 * wait_thread_callback
1736 * Calls waitpid for each thread, repeatedly if necessary, until
1737 * SIGSTOP is returned. Afterward, if any other signals were returned
1738 * by waitpid, return them to the thread's pending queue by calling kill.
1742 wait_thread_callback (const td_thrhandle_t *th, void *data)
1747 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1749 warning ("wait_thread_callback: %s", thr_err_string (ret));
1750 return -1; /* bail out, get_info failed. */
1753 /* This callback to act on all threads except the event thread: */
1754 if (ti.ti_lid == event_pid || /* no need to wait (no sigstop) */
1755 ti.ti_lid == main_prochandle.pid) /* no need to wait (already waited) */
1756 return 0; /* don't wait on the event thread. */
1758 wait_for_stop (ti.ti_lid);
1759 return 0; /* finished: next thread. */
1763 new_wait_thread_callback (thread, data)
1767 /* don't wait on the event thread -- it's already stopped and waited.
1768 Ditto the main thread. */
1769 if (thread->lid != event_pid &&
1770 thread->lid != main_prochandle.pid)
1772 wait_for_stop (thread->lid);
1778 * Wait for any thread to stop, by calling the underlying wait method.
1779 * The PID returned by the underlying target may be a kernel thread,
1780 * in which case we will want to convert it to the corresponding
1781 * user-space thread.
1785 thread_db_wait (int pid, struct target_waitstatus *ourstatus)
1787 td_thrhandle_t thandle;
1795 /* OK, we're about to wait for an event from the running inferior.
1796 Make sure we're ignoring the right signals. */
1798 check_all_signal_numbers (); /* see if magic signals changed. */
1803 /* FIXME: should I do the wait right here inline? */
1808 lwp = get_lwp_from_thread_id (GET_THREAD (pid));
1812 save_errno = linux_child_wait (-1, &retpid, &status);
1813 store_waitstatus (ourstatus, status);
1815 /* Thread ID is irrelevant if the target process exited.
1816 FIXME: do I have any killing to do?
1817 Can I get this event mistakenly from a thread? */
1818 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
1821 /* OK, we got an event of interest.
1822 Go stop all threads and look for new ones.
1823 FIXME: maybe don't do this for the restart signal? Optimization... */
1826 /* If the last call to resume was for a specific thread, then we don't
1827 need to stop everyone else: they should already be stopped. */
1828 if (last_resume_step == 0 || last_resume_pid == -1)
1830 /* Main thread must be stopped before calling the iterator. */
1831 if (retpid != main_prochandle.pid)
1833 kill (main_prochandle.pid, SIGSTOP);
1834 wait_for_stop (main_prochandle.pid);
1837 empty_threadlist ();
1838 /* Now stop everyone else, and attach any new threads you find. */
1839 p_td_ta_thr_iter (main_threadagent,
1840 stop_or_attach_thread_callback,
1843 TD_THR_LOWEST_PRIORITY,
1845 TD_THR_ANY_USER_FLAGS);
1847 /* Now go call wait on all the threads we've stopped:
1848 This allows us to absorb the SIGKILL event, and to make sure
1849 that the thread knows that it is stopped (Linux peculiarity). */
1851 threadlist_iter (new_wait_thread_callback,
1857 /* Convert the kernel thread id to the corresponding thread id. */
1859 /* If the process layer does not furnish an lwp,
1860 then perhaps the returned pid IS the lwp... */
1861 if ((lwp = GET_LWP (retpid)) == 0)
1864 if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK)
1865 return retpid; /* LWP is not mapped onto a user-space thread. */
1867 if ((ret = p_td_thr_validate (&thandle)) != TD_OK)
1868 return retpid; /* LWP is not mapped onto a valid thread. */
1870 if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK)
1872 warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret));
1876 retpid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
1877 /* If this is a new user thread, notify GDB about it. */
1878 if (!in_thread_list (retpid))
1880 printf_filtered ("[New %s]\n", target_pid_to_str (retpid));
1881 add_thread (retpid);
1885 /* Now detect if this is a thread creation/deletion event: */
1886 check_for_thread_event (ourstatus, retpid);
1892 * kill has to call the underlying kill.
1893 * FIXME: I'm not sure if it's necessary to check inferior_pid any more,
1894 * but we might need to fix inferior_pid up if it's a user thread.
1898 kill_thread_callback (th, data)
1906 For Linux, threads may need to be waited. */
1907 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1909 warning ("kill_thread_callback: %s", thr_err_string (ret));
1910 return -1; /* bail out, get_info failed. */
1913 if (ti.ti_lid != main_prochandle.pid)
1915 kill (ti.ti_lid, SIGKILL);
1921 static void thread_db_kill (void)
1927 For Linux, threads may need to be waited. */
1928 if (inferior_pid != 0)
1930 /* Go kill the children first. Save the main thread for last. */
1931 p_td_ta_thr_iter (main_threadagent,
1932 kill_thread_callback,
1935 TD_THR_LOWEST_PRIORITY,
1937 TD_THR_ANY_USER_FLAGS);
1939 /* Turn off thread_db event-reporting API *before* killing the
1940 main thread, since this operation requires child memory access.
1941 Can't move this into thread_db_unpush target because then
1942 detach would not work. */
1943 disable_thread_event_reporting (main_threadagent);
1945 inferior_pid = main_prochandle.pid;
1948 * Since both procfs_kill and ptrace_kill call target_mourn,
1949 * it should be sufficient for me to call one of them.
1950 * That will result in my mourn being called, which will both
1951 * unpush me and call the underlying mourn.
1953 target_beneath->to_kill ();
1956 /* Wait for all threads. */
1957 /* FIXME: need a universal wait_for_signal func? */
1960 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1962 while (rpid > 0 || errno == EINTR);
1966 rpid = waitpid (-1, &status, WNOHANG);
1968 while (rpid > 0 || errno == EINTR);
1972 * Mourn has to remove us from the target stack,
1973 * and then call the underlying mourn.
1976 static void thread_db_mourn_inferior (void)
1978 thread_db_unpush_target ();
1979 target_mourn_inferior (); /* call the underlying mourn */
1983 * Detach has to remove us from the target stack,
1984 * and then call the underlying detach.
1986 * But first, it has to detach all the cloned threads!
1990 detach_thread_callback (th, data)
1994 /* Called once per thread. */
1998 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
2000 warning ("detach_thread_callback: %s", thr_err_string (ret));
2001 return -1; /* bail out, get_info failed. */
2004 if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid)))
2005 return 0; /* apparently we don't know this one. */
2007 /* Save main thread for last, or the iterator will fail! */
2008 if (ti.ti_lid != main_prochandle.pid)
2010 struct cleanup *old_chain;
2013 /* Time to detach this thread.
2014 First disable thread_db event reporting for the thread. */
2015 if (p_td_thr_event_enable &&
2016 (ret = p_td_thr_event_enable (th, off)) != TD_OK)
2018 warning ("detach_thread_callback: %s\n", thr_err_string (ret));
2022 /* Now cancel any pending SIGTRAPS. FIXME! */
2024 /* Call underlying detach method. FIXME just detach it. */
2025 old_chain = save_inferior_pid ();
2026 inferior_pid = ti.ti_lid;
2027 detach (TARGET_SIGNAL_0);
2028 do_cleanups (old_chain);
2034 thread_db_detach (char *args, int from_tty)
2038 if ((ret = p_td_ta_thr_iter (main_threadagent,
2039 detach_thread_callback,
2042 TD_THR_LOWEST_PRIORITY,
2044 TD_THR_ANY_USER_FLAGS))
2046 warning ("detach (thr_iter): %s", thr_err_string (ret));
2048 /* Turn off thread_db event-reporting API
2049 (before detaching the main thread) */
2050 disable_thread_event_reporting (main_threadagent);
2052 thread_db_unpush_target ();
2054 /* above call nullifies target_beneath, so don't use that! */
2055 inferior_pid = PIDGET (inferior_pid);
2056 target_detach (args, from_tty);
2061 * We never want to actually create the inferior!
2063 * If this is ever called, it means we were on the target stack
2064 * when the user said "run". But we don't want to be on the new
2065 * inferior's target stack until the thread_db / libthread
2066 * connection is ready to be made.
2068 * So, what shall we do?
2069 * Unpush ourselves from the stack, and then invoke
2070 * find_default_create_inferior, which will invoke the
2071 * appropriate process_stratum target to do the create.
2075 thread_db_create_inferior (exec_file, allargs, env)
2080 thread_db_unpush_target ();
2081 find_default_create_inferior (exec_file, allargs, env);
2085 * Thread_db target vector initializer.
2089 init_thread_db_ops ()
2091 thread_db_ops.to_shortname = "multi-thread";
2092 thread_db_ops.to_longname = "multi-threaded child process.";
2093 thread_db_ops.to_doc = "Threads and pthreads support.";
2094 thread_db_ops.to_files_info = thread_db_files_info;
2095 thread_db_ops.to_create_inferior = thread_db_create_inferior;
2096 thread_db_ops.to_detach = thread_db_detach;
2097 thread_db_ops.to_wait = thread_db_wait;
2098 thread_db_ops.to_resume = thread_db_resume;
2099 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
2100 thread_db_ops.to_kill = thread_db_kill;
2101 thread_db_ops.to_xfer_memory = thread_db_xfer_memory;
2102 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
2103 thread_db_ops.to_store_registers = thread_db_store_registers;
2104 thread_db_ops.to_thread_alive = thread_db_alive;
2105 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
2106 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
2107 thread_db_ops.to_stratum = thread_stratum;
2108 thread_db_ops.to_has_thread_control = tc_schedlock;
2109 thread_db_ops.to_magic = OPS_MAGIC;
2111 #endif /* HAVE_STDINT_H */
2114 * Module constructor / initializer function.
2115 * If connection to thread_db dynamic library is successful,
2116 * then initialize this module's target vectors and the
2122 _initialize_thread_db ()
2124 #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */
2125 if (init_thread_db_library ())
2127 init_thread_db_ops ();
2128 add_target (&thread_db_ops);
2130 * Hook up to the new_objfile event.
2131 * If someone is already there, arrange for him to be called
2134 target_new_objfile_chain = target_new_objfile_hook;
2135 target_new_objfile_hook = thread_db_new_objfile;
2137 #endif /* HAVE_STDINT_H */