1 /* Multi-threaded debugging support for the thread_db interface,
2 used on operating systems such as Solaris and Linux.
3 Copyright 1999, 2000, 2001 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"
104 #include "regcache.h"
106 #include "gdb_wait.h"
110 #if defined(USE_PROC_FS) || defined(HAVE_GREGSET_T)
111 #include <sys/procfs.h>
114 #include "gdb_proc_service.h"
116 #if defined HAVE_STDINT_H /* Pre-5.2 systems don't have this header */
117 #if defined (HAVE_THREAD_DB_H)
118 #include <thread_db.h> /* defines outgoing API (td_thr_* calls) */
120 #include "gdb_thread_db.h"
123 #include <dlfcn.h> /* dynamic library interface */
125 /* Prototypes for supply_gregset etc. */
128 /* Macros for superimposing PID and TID into inferior_ptid. */
129 #define GET_PID(ptid) ptid_get_pid (ptid)
130 #define GET_LWP(ptid) ptid_get_lwp (ptid)
131 #define GET_THREAD(ptid) ptid_get_tid (ptid)
133 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
134 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
136 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
137 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
139 /* From linux-thread.c. FIXME: These should go in a separate header
140 file, but I'm told that the life expectancy of lin-thread.c and
141 linux-thread.c isn't very long... */
143 extern int linux_child_wait (int, int *, int *);
144 extern void check_all_signal_numbers (void);
145 extern void linuxthreads_discard_global_state (void);
146 extern void attach_thread (int);
149 * target_beneath is a pointer to the target_ops underlying this one.
152 static struct target_ops *target_beneath;
156 * target vector defined in this module:
159 static struct target_ops thread_db_ops;
162 * Typedefs required to resolve differences between the thread_db
163 * and proc_service API defined on different versions of Solaris:
166 #if defined(PROC_SERVICE_IS_OLD)
167 typedef const struct ps_prochandle *gdb_ps_prochandle_t;
168 typedef char *gdb_ps_read_buf_t;
169 typedef char *gdb_ps_write_buf_t;
170 typedef int gdb_ps_size_t;
172 typedef struct ps_prochandle *gdb_ps_prochandle_t;
173 typedef void *gdb_ps_read_buf_t;
174 typedef const void *gdb_ps_write_buf_t;
175 typedef size_t gdb_ps_size_t;
179 * proc_service callback functions, called by thread_db.
183 ps_pstop (gdb_ps_prochandle_t ph) /* Process stop */
189 ps_pcontinue (gdb_ps_prochandle_t ph) /* Process continue */
195 ps_lstop (gdb_ps_prochandle_t ph, /* LWP stop */
202 ps_lcontinue (gdb_ps_prochandle_t ph, /* LWP continue */
209 ps_lgetxregsize (gdb_ps_prochandle_t ph, /* Get XREG size */
217 ps_lgetxregs (gdb_ps_prochandle_t ph, /* Get XREGS */
225 ps_lsetxregs (gdb_ps_prochandle_t ph, /* Set XREGS */
233 ps_plog (const char *fmt, ...)
237 va_start (args, fmt);
238 vfprintf_filtered (gdb_stderr, fmt, args);
241 /* Look up a symbol in GDB's global symbol table.
242 Return the symbol's address.
243 FIXME: it would be more correct to look up the symbol in the context
244 of the LD_OBJECT_NAME provided. However we're probably fairly safe
245 as long as there aren't name conflicts with other libraries. */
248 ps_pglobal_lookup (gdb_ps_prochandle_t ph,
249 const char *ld_object_name, /* the library name */
250 const char *ld_symbol_name, /* the symbol name */
251 paddr_t *ld_symbol_addr) /* return the symbol addr */
253 struct minimal_symbol *ms;
255 ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);
260 *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);
265 /* Worker function for all memory reads and writes: */
266 static ps_err_e rw_common (const struct ps_prochandle *ph,
272 /* target_xfer_memory direction consts */
273 enum {PS_READ = 0, PS_WRITE = 1};
276 ps_pdread (gdb_ps_prochandle_t ph, /* read from data segment */
278 gdb_ps_read_buf_t buf,
281 return rw_common (ph, addr, buf, size, PS_READ);
285 ps_pdwrite (gdb_ps_prochandle_t ph, /* write to data segment */
287 gdb_ps_write_buf_t buf,
290 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
294 ps_ptread (gdb_ps_prochandle_t ph, /* read from text segment */
296 gdb_ps_read_buf_t buf,
299 return rw_common (ph, addr, buf, size, PS_READ);
303 ps_ptwrite (gdb_ps_prochandle_t ph, /* write to text segment */
305 gdb_ps_write_buf_t buf,
308 return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
311 static char *thr_err_string (td_err_e);
312 static char *thr_state_string (td_thr_state_e);
314 struct ps_prochandle main_prochandle;
315 td_thragent_t * main_threadagent;
318 * Common proc_service routine for reading and writing memory.
321 /* FIXME: once we've munged the inferior_ptid, why can't we
322 simply call target_read/write_memory and return? */
325 rw_common (const struct ps_prochandle *ph,
331 struct cleanup *old_chain = save_inferior_ptid ();
335 inferior_ptid = pid_to_ptid (main_prochandle.pid);
339 done = current_target.to_xfer_memory (addr, buf, size, write_p,
343 if (write_p == PS_READ)
344 print_sys_errmsg ("rw_common (): read", errno);
346 print_sys_errmsg ("rw_common (): write", errno);
353 do_cleanups (old_chain);
357 /* Cleanup functions used by the register callbacks
358 (which have to manipulate the global inferior_ptid). */
361 ps_lgetregs (gdb_ps_prochandle_t ph, /* Get LWP general regs */
365 struct cleanup *old_chain = save_inferior_ptid ();
367 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
368 current_target.to_fetch_registers (-1);
370 fill_gregset ((gdb_gregset_t *) gregset, -1);
371 do_cleanups (old_chain);
377 ps_lsetregs (gdb_ps_prochandle_t ph, /* Set LWP general regs */
379 const prgregset_t gregset)
381 struct cleanup *old_chain = save_inferior_ptid ();
383 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
384 supply_gregset ((gdb_gregset_t *) gregset);
385 current_target.to_store_registers (-1);
386 do_cleanups (old_chain);
391 ps_lgetfpregs (gdb_ps_prochandle_t ph, /* Get LWP float regs */
393 gdb_prfpregset_t *fpregset)
395 struct cleanup *old_chain = save_inferior_ptid ();
397 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
398 current_target.to_fetch_registers (-1);
399 fill_fpregset (fpregset, -1);
400 do_cleanups (old_chain);
405 ps_lsetfpregs (gdb_ps_prochandle_t ph, /* Set LWP float regs */
407 const gdb_prfpregset_t *fpregset)
409 struct cleanup *old_chain = save_inferior_ptid ();
411 inferior_ptid = BUILD_LWP (lwpid, main_prochandle.pid);
412 supply_fpregset (fpregset);
413 current_target.to_store_registers (-1);
414 do_cleanups (old_chain);
421 * return the main pid for the child process
422 * (special for Linux -- not used on Solaris)
426 ps_getpid (gdb_ps_prochandle_t ph)
433 /* Reads the local descriptor table of a LWP. */
436 ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
439 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
440 extern struct ssd *procfs_find_LDT_entry (int);
443 ret = procfs_find_LDT_entry (BUILD_LWP (lwpid,
444 PIDGET (main_prochandle.pid)));
447 memcpy (pldt, ret, sizeof (struct ssd));
450 else /* LDT not found. */
453 #endif /* TM_I386SOL2_H */
456 * Pointers to thread_db functions:
458 * These are a dynamic library mechanism.
459 * The dlfcn.h interface will be used to initialize these
460 * so that they point to the appropriate functions in the
461 * thread_db dynamic library. This is done dynamically
462 * so that GDB can still run on systems that lack thread_db.
465 static td_err_e (*p_td_init) (void);
467 static td_err_e (*p_td_ta_new) (const struct ps_prochandle *ph_p,
468 td_thragent_t **ta_pp);
470 static td_err_e (*p_td_ta_delete) (td_thragent_t *ta_p);
472 static td_err_e (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p,
476 static td_err_e (*p_td_ta_thr_iter) (const td_thragent_t *ta_p,
479 td_thr_state_e state,
481 sigset_t *ti_sigmask_p,
482 unsigned ti_user_flags);
484 static td_err_e (*p_td_ta_event_addr) (const td_thragent_t *ta_p,
486 td_notify_t *notify_p);
488 static td_err_e (*p_td_ta_event_getmsg) (const td_thragent_t *ta_p,
489 td_event_msg_t *msg);
491 static td_err_e (*p_td_ta_set_event) (const td_thragent_t *ta_p,
492 td_thr_events_t *events);
494 static td_err_e (*p_td_thr_validate) (const td_thrhandle_t *th_p);
496 static td_err_e (*p_td_thr_event_enable) (const td_thrhandle_t *th_p,
499 static td_err_e (*p_td_thr_get_info) (const td_thrhandle_t *th_p,
502 static td_err_e (*p_td_thr_getgregs) (const td_thrhandle_t *th_p,
505 static td_err_e (*p_td_thr_setgregs) (const td_thrhandle_t *th_p,
506 const prgregset_t regset);
508 static td_err_e (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p,
509 gdb_prfpregset_t *fpregset);
511 static td_err_e (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p,
512 const gdb_prfpregset_t *fpregset);
514 static td_err_e (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p,
516 td_thrhandle_t *th_p);
518 static td_err_e (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p,
520 td_thrhandle_t *th_p);
523 * API and target vector initialization function: thread_db_initialize.
525 * NOTE: this function is deliberately NOT named with the GDB convention
526 * of module initializer function names that begin with "_initialize".
527 * This module is NOT intended to be auto-initialized at GDB startup.
528 * Rather, it will only be initialized when a multi-threaded child
529 * process is detected.
534 * Initializer for thread_db library interface.
535 * This function does the dynamic library stuff (dlopen, dlsym),
536 * and then calls the thread_db library's one-time initializer
537 * function (td_init). If everything succeeds, this function
538 * returns true; otherwise it returns false, and this module
543 init_thread_db_library (void)
548 /* Open a handle to the "thread_db" dynamic library. */
549 if ((dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW)) == NULL)
552 /* Initialize pointers to the dynamic library functions we will use.
553 * Note that we are not calling the functions here -- we are only
554 * establishing pointers to them.
557 /* td_init: initialize thread_db library. */
558 if ((p_td_init = dlsym (dlhandle, "td_init")) == NULL)
560 /* td_ta_new: register a target process with thread_db. */
561 if ((p_td_ta_new = dlsym (dlhandle, "td_ta_new")) == NULL)
563 /* td_ta_delete: un-register a target process with thread_db. */
564 if ((p_td_ta_delete = dlsym (dlhandle, "td_ta_delete")) == NULL)
567 /* td_ta_map_id2thr: get thread handle from thread id. */
568 if ((p_td_ta_map_id2thr = dlsym (dlhandle, "td_ta_map_id2thr")) == NULL)
570 /* td_ta_map_lwp2thr: get thread handle from lwp id. */
571 if ((p_td_ta_map_lwp2thr = dlsym (dlhandle, "td_ta_map_lwp2thr")) == NULL)
573 /* td_ta_get_nthreads: get number of threads in target process. */
574 if ((p_td_ta_get_nthreads = dlsym (dlhandle, "td_ta_get_nthreads")) == NULL)
576 /* td_ta_thr_iter: iterate over all thread handles. */
577 if ((p_td_ta_thr_iter = dlsym (dlhandle, "td_ta_thr_iter")) == NULL)
580 /* td_thr_validate: make sure a thread handle is real and alive. */
581 if ((p_td_thr_validate = dlsym (dlhandle, "td_thr_validate")) == NULL)
583 /* td_thr_get_info: get a bunch of info about a thread. */
584 if ((p_td_thr_get_info = dlsym (dlhandle, "td_thr_get_info")) == NULL)
586 /* td_thr_getgregs: get general registers for thread. */
587 if ((p_td_thr_getgregs = dlsym (dlhandle, "td_thr_getgregs")) == NULL)
589 /* td_thr_setgregs: set general registers for thread. */
590 if ((p_td_thr_setgregs = dlsym (dlhandle, "td_thr_setgregs")) == NULL)
592 /* td_thr_getfpregs: get floating point registers for thread. */
593 if ((p_td_thr_getfpregs = dlsym (dlhandle, "td_thr_getfpregs")) == NULL)
595 /* td_thr_setfpregs: set floating point registers for thread. */
596 if ((p_td_thr_setfpregs = dlsym (dlhandle, "td_thr_setfpregs")) == NULL)
602 warning ("init_thread_db: td_init: %s", thr_err_string (ret));
606 /* Optional functions:
607 We can still debug even if the following functions are not found. */
609 /* td_ta_event_addr: get the breakpoint address for specified event. */
610 p_td_ta_event_addr = dlsym (dlhandle, "td_ta_event_addr");
612 /* td_ta_event_getmsg: get the next event message for the process. */
613 p_td_ta_event_getmsg = dlsym (dlhandle, "td_ta_event_getmsg");
615 /* td_ta_set_event: request notification of an event. */
616 p_td_ta_set_event = dlsym (dlhandle, "td_ta_set_event");
618 /* td_thr_event_enable: enable event reporting in a thread. */
619 p_td_thr_event_enable = dlsym (dlhandle, "td_thr_event_enable");
621 return 1; /* success */
625 * Local utility functions:
632 thr_err_string - Convert a thread_db error code to a string
636 char * thr_err_string (errcode)
640 Return a string description of the thread_db errcode. If errcode
641 is unknown, then return an <unknown> message.
646 thr_err_string (td_err_e errcode)
651 case TD_OK: return "generic 'call succeeded'";
652 case TD_ERR: return "generic error";
653 case TD_NOTHR: return "no thread to satisfy query";
654 case TD_NOSV: return "no sync handle to satisfy query";
655 case TD_NOLWP: return "no lwp to satisfy query";
656 case TD_BADPH: return "invalid process handle";
657 case TD_BADTH: return "invalid thread handle";
658 case TD_BADSH: return "invalid synchronization handle";
659 case TD_BADTA: return "invalid thread agent";
660 case TD_BADKEY: return "invalid key";
661 case TD_NOMSG: return "no event message for getmsg";
662 case TD_NOFPREGS: return "FPU register set not available";
663 case TD_NOLIBTHREAD: return "application not linked with libthread";
664 case TD_NOEVENT: return "requested event is not supported";
665 case TD_NOCAPAB: return "capability not available";
666 case TD_DBERR: return "debugger service failed";
667 case TD_NOAPLIC: return "operation not applicable to";
668 case TD_NOTSD: return "no thread-specific data for this thread";
669 case TD_MALLOC: return "malloc failed";
670 case TD_PARTIALREG: return "only part of register set was written/read";
671 case TD_NOXREGS: return "X register set not available for this thread";
673 sprintf (buf, "unknown thread_db error '%d'", errcode);
682 thr_state_string - Convert a thread_db state code to a string
686 char *thr_state_string (statecode)
690 Return the thread_db state string associated with statecode.
691 If statecode is unknown, then return an <unknown> message.
696 thr_state_string (td_thr_state_e statecode)
701 case TD_THR_STOPPED: return "stopped by debugger";
702 case TD_THR_RUN: return "runnable";
703 case TD_THR_ACTIVE: return "active";
704 case TD_THR_ZOMBIE: return "zombie";
705 case TD_THR_SLEEP: return "sleeping";
706 case TD_THR_STOPPED_ASLEEP: return "stopped by debugger AND blocked";
708 sprintf (buf, "unknown thread_db state %d", statecode);
714 * Local thread/event list.
715 * This data structure will be used to hold a list of threads and
716 * pending/deliverable events.
719 typedef struct THREADINFO {
720 thread_t tid; /* thread ID */
721 pid_t lid; /* process/lwp ID */
722 td_thr_state_e state; /* thread state (a la thread_db) */
723 td_thr_type_e type; /* thread type (a la thread_db) */
724 int pending; /* true if holding a pending event */
725 int status; /* wait status of any interesting event */
728 threadinfo * threadlist;
729 int threadlist_max = 0; /* current size of table */
730 int threadlist_top = 0; /* number of threads now in table */
731 #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */
734 insert_thread (int tid, int lid, td_thr_state_e state, td_thr_type_e type)
736 if (threadlist_top >= threadlist_max)
738 threadlist_max += THREADLIST_ALLOC;
739 threadlist = xrealloc (threadlist,
740 threadlist_max * sizeof (threadinfo));
741 if (threadlist == NULL)
744 threadlist[threadlist_top].tid = tid;
745 threadlist[threadlist_top].lid = lid;
746 threadlist[threadlist_top].state = state;
747 threadlist[threadlist_top].type = type;
748 threadlist[threadlist_top].pending = 0;
749 threadlist[threadlist_top].status = 0;
751 return &threadlist[threadlist_top++];
755 empty_threadlist (void)
761 next_pending_event (void)
765 for (i = 0; i < threadlist_top; i++)
766 if (threadlist[i].pending)
767 return &threadlist[i];
773 threadlist_iter (int (*func) (), void *data, td_thr_state_e state,
778 for (i = 0; i < threadlist_top; i++)
779 if ((state == TD_THR_ANY_STATE || state == threadlist[i].state) &&
780 (type == TD_THR_ANY_TYPE || type == threadlist[i].type))
781 if ((*func) (&threadlist[i], data) != 0)
790 * Here we keep state information all collected in one place.
793 /* This flag is set when we activate, so that we don't do it twice.
794 Defined in linux-thread.c and used for inter-target syncronization. */
795 extern int using_thread_db;
797 /* The process id for which we've stopped.
798 * This is only set when we actually stop all threads.
799 * Otherwise it's zero.
801 static int event_pid;
804 * The process id for a new thread to which we've just attached.
805 * This process needs special handling at resume time.
807 static int attach_pid;
811 * thread_db event handling:
813 * The mechanism for event notification via the thread_db API.
814 * These events are implemented as breakpoints. The thread_db
815 * library gives us an address where we can set a breakpoint.
816 * When the breakpoint is hit, it represents an event of interest
823 /* Location of the thread creation event breakpoint. The code at this
824 location in the child process will be called by the pthread library
825 whenever a new thread is created. By setting a special breakpoint
826 at this location, GDB can detect when a new thread is created. We
827 obtain this location via the td_ta_event_addr call. */
829 static CORE_ADDR thread_creation_bkpt_address;
831 /* Location of the thread death event breakpoint. The code at this
832 location in the child process will be called by the pthread library
833 whenever a thread is destroyed. By setting a special breakpoint at
834 this location, GDB can detect when a new thread is created. We
835 obtain this location via the td_ta_event_addr call. */
837 static CORE_ADDR thread_death_bkpt_address;
839 /* This function handles the global parts of enabling thread events.
840 The thread-specific enabling is handled per-thread elsewhere. */
843 enable_thread_event_reporting (td_thragent_t *ta)
845 td_thr_events_t events;
849 if (p_td_ta_set_event == NULL ||
850 p_td_ta_event_addr == NULL ||
851 p_td_ta_event_getmsg == NULL ||
852 p_td_thr_event_enable == NULL)
853 return; /* can't do thread event reporting without these funcs */
855 /* set process wide mask saying which events we are interested in */
856 td_event_emptyset (&events);
857 td_event_addset (&events, TD_CREATE);
858 td_event_addset (&events, TD_DEATH);
860 if (p_td_ta_set_event (ta, &events) != TD_OK)
862 warning ("unable to set global thread event mask");
866 /* Delete previous thread event breakpoints, if any. */
867 remove_thread_event_breakpoints ();
869 /* create breakpoints -- thread creation and death */
870 /* thread creation */
871 /* get breakpoint location */
872 if (p_td_ta_event_addr (ta, TD_CREATE, ¬ify) != TD_OK)
874 warning ("unable to get location for thread creation breakpoint");
878 /* Set up the breakpoint. */
879 create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr);
881 /* Save it's location. */
882 thread_creation_bkpt_address = (CORE_ADDR) notify.u.bptaddr;
885 /* get breakpoint location */
886 if (p_td_ta_event_addr (ta, TD_DEATH, ¬ify) != TD_OK)
888 warning ("unable to get location for thread death breakpoint");
891 /* Set up the breakpoint. */
892 create_thread_event_breakpoint ((CORE_ADDR) notify.u.bptaddr);
894 /* Save it's location. */
895 thread_death_bkpt_address = (CORE_ADDR) notify.u.bptaddr;
898 /* This function handles the global parts of disabling thread events.
899 The thread-specific enabling is handled per-thread elsewhere. */
902 disable_thread_event_reporting (td_thragent_t *ta)
904 td_thr_events_t events;
906 /* set process wide mask saying we aren't interested in any events */
907 td_event_emptyset (&events);
908 p_td_ta_set_event (main_threadagent, &events);
910 /* Delete thread event breakpoints, if any. */
911 remove_thread_event_breakpoints ();
912 thread_creation_bkpt_address = 0;
913 thread_death_bkpt_address = 0;
916 /* check_for_thread_event
918 if it's a thread event we recognize (currently
919 we only recognize creation and destruction
920 events), return 1; else return 0. */
924 check_for_thread_event (struct target_waitstatus *tws, int event_pid)
926 /* FIXME: to be more efficient, we should keep a static
927 list of threads, and update it only here (with td_ta_thr_iter). */
932 thread_db_push_target (void)
934 /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */
936 /* Push this target vector */
937 push_target (&thread_db_ops);
938 /* Find the underlying process-layer target for calling later. */
939 target_beneath = find_target_beneath (&thread_db_ops);
941 /* Turn on thread_db event-reporting API. */
942 enable_thread_event_reporting (main_threadagent);
946 thread_db_unpush_target (void)
948 /* Must be called whenever we remove ourself from the target stack! */
951 target_beneath = NULL;
953 /* delete local list of threads */
955 /* Turn off the thread_db API. */
956 p_td_ta_delete (main_threadagent);
957 /* Unpush this target vector */
958 unpush_target (&thread_db_ops);
959 /* Reset linuxthreads module. */
960 linuxthreads_discard_global_state ();
964 * New objfile hook function:
965 * Called for each new objfile (image, shared lib) in the target process.
967 * The purpose of this function is to detect that the target process
968 * is linked with the (appropriate) thread library. So every time a
969 * new target shared library is detected, we will call td_ta_new.
970 * If it succeeds, we know we have a multi-threaded target process
971 * that we can debug using the thread_db API.
975 * new_objfile function:
977 * connected to target_new_objfile_hook, this function gets called
978 * every time a new binary image is loaded.
980 * At each call, we attempt to open the thread_db connection to the
981 * child process. If it succeeds, we know we have a libthread process
982 * and we can debug it with this target vector. Therefore we push
983 * ourself onto the target stack.
986 static void (*target_new_objfile_chain) (struct objfile *objfile);
987 static int stop_or_attach_thread_callback (const td_thrhandle_t *th,
989 static int wait_thread_callback (const td_thrhandle_t *th,
993 thread_db_new_objfile (struct objfile *objfile)
997 if (using_thread_db) /* libthread already detected, and */
998 goto quit; /* thread target vector activated. */
1000 if (objfile == NULL)
1001 goto quit; /* un-interesting object file */
1003 /* Initialize our "main prochandle" with the main inferior pid. */
1004 main_prochandle.pid = PIDGET (inferior_ptid);
1006 /* Now attempt to open a thread_db connection to the
1007 thread library running in the child process. */
1008 ret = p_td_ta_new (&main_prochandle, &main_threadagent);
1011 warning ("Unexpected error initializing thread_db: %s",
1012 thr_err_string (ret));
1014 case TD_NOLIBTHREAD: /* expected: no libthread in child process (yet) */
1016 case TD_OK: /* libthread detected in child: we go live now! */
1017 thread_db_push_target ();
1018 event_pid = PIDGET (inferior_ptid); /* for resume */
1020 /* Now stop everyone else, and attach any new threads you find. */
1021 p_td_ta_thr_iter (main_threadagent,
1022 stop_or_attach_thread_callback,
1025 TD_THR_LOWEST_PRIORITY,
1027 TD_THR_ANY_USER_FLAGS);
1029 /* Now go call wait on all the threads you've stopped:
1030 This allows us to absorb the SIGKILL event, and to make sure
1031 that the thread knows that it is stopped (Linux peculiarity). */
1032 p_td_ta_thr_iter (main_threadagent,
1033 wait_thread_callback,
1036 TD_THR_LOWEST_PRIORITY,
1038 TD_THR_ANY_USER_FLAGS);
1043 if (target_new_objfile_chain)
1044 target_new_objfile_chain (objfile);
1052 thread_db_alive - test thread for "aliveness"
1056 static bool thread_db_alive (int pid);
1060 returns true if thread still active in inferior.
1065 thread_db_alive (ptid_t ptid)
1067 if (is_thread (ptid)) /* user-space (non-kernel) thread */
1071 int pid = GET_THREAD (ptid);
1073 if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK)
1074 return 0; /* thread not found */
1075 if ((ret = p_td_thr_validate (&th)) != TD_OK)
1076 return 0; /* thread not valid */
1077 return 1; /* known thread: return true */
1079 else if (target_beneath->to_thread_alive)
1080 return target_beneath->to_thread_alive (ptid);
1082 return 0; /* default to "not alive" (shouldn't happen anyway) */
1086 * get_lwp_from_thread_handle
1089 static int /* lwpid_t or pid_t */
1090 get_lwp_from_thread_handle (td_thrhandle_t *th)
1095 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1096 error ("get_lwp_from_thread_handle: thr_get_info failed: %s",
1097 thr_err_string (ret));
1103 * get_lwp_from_thread_id
1106 static int /* lwpid_t or pid_t */
1107 get_lwp_from_thread_id (int tid /* thread_t? */)
1112 if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK)
1113 error ("get_lwp_from_thread_id: map_id2thr failed: %s",
1114 thr_err_string (ret));
1116 return get_lwp_from_thread_handle (&th);
1120 * pid_to_str has to handle user-space threads.
1121 * If not a user-space thread, then pass the request on to the
1122 * underlying stratum if it can handle it: else call normal_pid_to_str.
1126 thread_db_pid_to_str (ptid_t ptid)
1128 static char buf[100];
1133 if (is_thread (ptid))
1135 if ((ret = p_td_ta_map_id2thr (main_threadagent,
1138 error ("thread_db: map_id2thr failed: %s", thr_err_string (ret));
1140 if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK)
1141 error ("thread_db: thr_get_info failed: %s", thr_err_string (ret));
1143 if (ti.ti_state == TD_THR_ACTIVE &&
1145 sprintf (buf, "Thread %ld (LWP %d)", ti.ti_tid, ti.ti_lid);
1147 sprintf (buf, "Thread %ld (%s)", ti.ti_tid,
1148 thr_state_string (ti.ti_state));
1150 else if (GET_LWP (ptid))
1151 sprintf (buf, "LWP %ld", GET_LWP (ptid));
1152 else return normal_pid_to_str (ptid);
1158 * thread_db target vector functions:
1162 thread_db_files_info (struct target_ops *tgt_vector)
1164 /* This function will be unnecessary in real life. */
1165 printf_filtered ("thread_db stratum:\n");
1166 target_beneath->to_files_info (tgt_vector);
1170 * xfer_memory has to munge the inferior_ptid before passing the call
1171 * down to the target layer.
1175 thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
1176 struct mem_attrib *attrib,
1177 struct target_ops *target)
1179 struct cleanup *old_chain;
1182 old_chain = save_inferior_ptid ();
1184 if (is_thread (inferior_ptid) ||
1185 !target_thread_alive (inferior_ptid))
1187 /* FIXME: use the LID/LWP, so that underlying process layer
1188 can read memory from specific threads? */
1189 inferior_ptid = pid_to_ptid (main_prochandle.pid);
1192 ret = target_beneath->to_xfer_memory (memaddr, myaddr, len,
1193 dowrite, attrib, target);
1194 do_cleanups (old_chain);
1199 * fetch_registers has to determine if inferior_ptid is a user-space thread.
1200 * If so, we use the thread_db API to get the registers.
1201 * And if not, we call the underlying process stratum.
1205 thread_db_fetch_registers (int regno)
1207 td_thrhandle_t thandle;
1208 gdb_prfpregset_t fpregset;
1209 prgregset_t gregset;
1213 if (!is_thread (inferior_ptid)) /* kernel thread */
1214 { /* pass the request on to the target underneath. */
1215 target_beneath->to_fetch_registers (regno);
1219 /* convert inferior_ptid into a td_thrhandle_t */
1221 if ((thread = GET_THREAD (inferior_ptid)) == 0)
1222 error ("fetch_registers: thread == 0");
1224 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1225 error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret));
1227 /* Get the integer regs:
1228 For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7,
1229 pc and sp are saved (by a thread context switch). */
1230 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK &&
1231 ret != TD_PARTIALREG)
1232 error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret));
1234 /* And, now the fp regs */
1235 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK &&
1237 error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret));
1239 /* Note that we must call supply_{g fp}regset *after* calling the td routines
1240 because the td routines call ps_lget* which affect the values stored in the
1243 supply_gregset ((gdb_gregset_t *) gregset);
1244 supply_fpregset (&fpregset);
1249 * store_registers has to determine if inferior_ptid is a user-space thread.
1250 * If so, we use the thread_db API to get the registers.
1251 * And if not, we call the underlying process stratum.
1255 thread_db_store_registers (int regno)
1257 td_thrhandle_t thandle;
1258 gdb_prfpregset_t fpregset;
1259 prgregset_t gregset;
1263 if (!is_thread (inferior_ptid)) /* Kernel thread: */
1264 { /* pass the request on to the underlying target vector. */
1265 target_beneath->to_store_registers (regno);
1269 /* convert inferior_ptid into a td_thrhandle_t */
1271 if ((thread = GET_THREAD (inferior_ptid)) == 0)
1272 error ("store_registers: thread == 0");
1274 if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
1275 error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret));
1278 { /* Not writing all the regs */
1279 /* save new register value */
1280 /* MVS: I don't understand this... */
1281 char old_value[REGISTER_SIZE];
1283 memcpy (old_value, ®isters[REGISTER_BYTE (regno)], REGISTER_SIZE);
1285 if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK)
1286 error ("store_registers: td_thr_getgregs %s", thr_err_string (ret));
1287 if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK)
1288 error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret));
1290 /* restore new register value */
1291 memcpy (®isters[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);
1295 fill_gregset ((gdb_gregset_t *) gregset, regno);
1296 fill_fpregset (&fpregset, regno);
1298 if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK)
1299 error ("store_registers: td_thr_setgregs %s", thr_err_string (ret));
1300 if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK &&
1302 error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret));
1306 handle_new_thread (int tid, /* user thread id */
1307 int lid, /* kernel thread id */
1310 ptid_t gdb_ptid = BUILD_THREAD (tid, main_prochandle.pid);
1311 int wait_pid, wait_status;
1314 printf_filtered ("[New %s]\n", target_pid_to_str (gdb_ptid));
1315 add_thread (gdb_ptid);
1317 if (lid != main_prochandle.pid)
1319 attach_thread (lid);
1320 /* According to the Eric Paire model, we now have to send
1321 the restart signal to the new thread -- however, empirically,
1322 I do not find that to be necessary. */
1328 test_for_new_thread (int tid, int lid, int verbose)
1330 if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid)))
1331 handle_new_thread (tid, lid, verbose);
1335 * Callback function that gets called once per USER thread
1336 * (i.e., not kernel) thread by td_ta_thr_iter.
1340 find_new_threads_callback (const td_thrhandle_t *th, void *ignored)
1345 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1347 warning ("find_new_threads_callback: %s", thr_err_string (ret));
1348 return -1; /* bail out, get_info failed. */
1352 As things now stand, this should never detect a new thread.
1353 But if it does, we could be in trouble because we aren't calling
1354 wait_thread_callback for it. */
1355 test_for_new_thread (ti.ti_tid, ti.ti_lid, 0);
1360 * find_new_threads uses the thread_db iterator function to discover
1361 * user-space threads. Then if the underlying process stratum has a
1362 * find_new_threads method, we call that too.
1366 thread_db_find_new_threads (void)
1368 if (PIDGET (inferior_ptid) == -1) /* FIXME: still necessary? */
1370 printf_filtered ("No process.\n");
1373 p_td_ta_thr_iter (main_threadagent,
1374 find_new_threads_callback,
1377 TD_THR_LOWEST_PRIORITY,
1379 TD_THR_ANY_USER_FLAGS);
1380 if (target_beneath->to_find_new_threads)
1381 target_beneath->to_find_new_threads ();
1385 * Resume all threads, or resume a single thread.
1386 * If step is true, then single-step the appropriate thread
1387 * (or single-step inferior_ptid, but continue everyone else).
1388 * If signo is true, then send that signal to at least one thread.
1392 * This function is called once for each thread before resuming.
1393 * It sends continue (no step, and no signal) to each thread except
1394 * the main thread, and
1395 * the event thread (the one that stopped at a breakpoint etc.)
1397 * The event thread is handled separately so that it can be sent
1398 * the stepping and signal args with which target_resume was called.
1400 * The main thread is resumed last, so that the thread_db proc_service
1401 * callbacks will still work during the iterator function.
1405 resume_thread_callback (const td_thrhandle_t *th, void *data)
1410 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1412 warning ("resume_thread_callback: %s", thr_err_string (ret));
1413 return -1; /* bail out, get_info failed. */
1416 As things now stand, this should never detect a new thread.
1417 But if it does, we could be in trouble because we aren't calling
1418 wait_thread_callback for it. */
1419 test_for_new_thread (ti.ti_tid, ti.ti_lid, 1);
1421 if (ti.ti_lid != main_prochandle.pid &&
1422 ti.ti_lid != event_pid)
1424 /* Unconditionally continue the thread with no signal.
1425 Only the event thread will get a signal of any kind. */
1427 target_beneath->to_resume (pid_to_ptid (ti.ti_lid), 0, 0);
1433 new_resume_thread_callback (threadinfo *thread, void *data)
1435 if (thread->lid != event_pid &&
1436 thread->lid != main_prochandle.pid)
1438 /* Unconditionally continue the thread with no signal (for now). */
1440 target_beneath->to_resume (pid_to_ptid (thread->lid), 0, 0);
1445 static int last_resume_pid;
1446 static int last_resume_step;
1447 static int last_resume_signo;
1450 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
1452 last_resume_pid = PIDGET (ptid);
1453 last_resume_step = step;
1454 last_resume_signo = signo;
1456 /* resuming a specific pid? */
1457 if (PIDGET (ptid) != -1)
1459 if (is_thread (ptid))
1460 ptid = pid_to_ptid (get_lwp_from_thread_id (GET_THREAD (ptid)));
1461 else if (GET_LWP (ptid))
1462 ptid = pid_to_ptid (GET_LWP (ptid));
1465 /* Apparently the interpretation of 'pid' is dependent on 'step':
1466 If step is true, then a specific pid means 'step only this pid'.
1467 But if step is not true, then pid means 'continue ALL pids, but
1468 give the signal only to this one'. */
1469 if (PIDGET (ptid) != -1 && step)
1471 /* FIXME: is this gonna work in all circumstances? */
1472 target_beneath->to_resume (ptid, step, signo);
1476 /* 1) Continue all threads except the event thread and the main thread.
1477 2) resume the event thread with step and signo.
1478 3) If event thread != main thread, continue the main thread.
1480 Note: order of 2 and 3 may need to be reversed. */
1482 threadlist_iter (new_resume_thread_callback,
1486 /* now resume event thread, and if necessary also main thread. */
1489 target_beneath->to_resume (pid_to_ptid (event_pid), step, signo);
1491 if (event_pid != main_prochandle.pid)
1493 target_beneath->to_resume (pid_to_ptid (main_prochandle.pid), 0, 0);
1498 /* All new threads will be attached.
1499 All previously known threads will be stopped using kill (SIGKILL). */
1502 stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data)
1509 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1511 warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret));
1512 return -1; /* bail out, get_info failed. */
1515 /* First add it to our internal list.
1516 We build this list anew at every wait event. */
1517 insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type);
1518 /* Now: if we've already seen it, stop it, else add it and attach it. */
1519 gdb_ptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
1520 if (!in_thread_list (gdb_ptid)) /* new thread */
1522 handle_new_thread (ti.ti_tid, ti.ti_lid, 1);
1523 /* Enable thread events */
1524 if (p_td_thr_event_enable)
1525 if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK)
1526 warning ("stop_or_attach_thread: %s", thr_err_string (ret));
1528 else if (ti.ti_lid != event_pid &&
1529 ti.ti_lid != main_prochandle.pid)
1531 ret = (td_err_e) kill (ti.ti_lid, SIGSTOP);
1538 * Wait for signal N from pid PID.
1539 * If wait returns any other signals, put them back before returning.
1543 wait_for_stop (int pid)
1549 /* Array of wait/signal status */
1550 /* FIXME: wrong data structure, we need a queue.
1551 Realtime signals may be delivered more than once.
1552 And at that, we really can't handle them (see below). */
1554 static int wstatus [NSIG];
1555 #elif defined (_NSIG)
1556 static int wstatus [_NSIG];
1558 #error No definition for number of signals!
1561 /* clear wait/status list */
1562 memset (&wstatus, 0, sizeof (wstatus));
1564 /* Now look for SIGSTOP event on all threads except event thread. */
1567 if (pid == main_prochandle.pid)
1568 retpid = waitpid (pid, &status, 0);
1570 retpid = waitpid (pid, &status, __WCLONE);
1573 if (WSTOPSIG (status) == SIGSTOP)
1575 /* Got the SIGSTOP event we're looking for.
1576 Throw it away, and throw any other events back! */
1577 for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++)
1583 break; /* all done */
1588 /* Oops, got an event other than SIGSTOP.
1589 Save it, and throw it back after we find the SIGSTOP event. */
1591 /* FIXME (how?) This method is going to fail for realtime
1592 signals, which cannot be put back simply by using kill. */
1594 if (WIFEXITED (status))
1595 error ("Ack! Thread Exited event. What do I do now???");
1596 else if (WIFSTOPPED (status))
1597 signo = WSTOPSIG (status);
1599 signo = WTERMSIG (status);
1601 /* If a thread other than the event thread has hit a GDB
1602 breakpoint (as opposed to some random trap signal), then
1603 just arrange for it to hit it again later. Back up the
1604 PC if necessary. Don't forward the SIGTRAP signal to
1605 the thread. We will handle the current event, eventually
1606 we will resume all the threads, and this one will get
1607 it's breakpoint trap again.
1609 If we do not do this, then we run the risk that the user
1610 will delete or disable the breakpoint, but the thread will
1611 have already tripped on it. */
1613 if (retpid != event_pid &&
1615 breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (retpid)) -
1616 DECR_PC_AFTER_BREAK))
1618 /* Set the pc to before the trap and DO NOT re-send the signal */
1619 if (DECR_PC_AFTER_BREAK)
1620 write_pc_pid (read_pc_pid (pid_to_ptid (retpid))
1621 - DECR_PC_AFTER_BREAK,
1622 pid_to_ptid (retpid));
1625 /* Since SIGINT gets forwarded to the entire process group
1626 (in the case where ^C is typed at the tty / console),
1627 just ignore all SIGINTs from other than the event thread. */
1628 else if (retpid != event_pid && signo == SIGINT)
1629 { /* do nothing. Signal will disappear into oblivion! */
1633 else /* This is some random signal other than a breakpoint. */
1635 wstatus [signo] = 1;
1637 child_resume (pid_to_ptid (retpid), 0, TARGET_SIGNAL_0);
1641 } while (errno == 0 || errno == EINTR);
1645 * wait_thread_callback
1647 * Calls waitpid for each thread, repeatedly if necessary, until
1648 * SIGSTOP is returned. Afterward, if any other signals were returned
1649 * by waitpid, return them to the thread's pending queue by calling kill.
1653 wait_thread_callback (const td_thrhandle_t *th, void *data)
1658 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1660 warning ("wait_thread_callback: %s", thr_err_string (ret));
1661 return -1; /* bail out, get_info failed. */
1664 /* This callback to act on all threads except the event thread: */
1665 if (ti.ti_lid == event_pid || /* no need to wait (no sigstop) */
1666 ti.ti_lid == main_prochandle.pid) /* no need to wait (already waited) */
1667 return 0; /* don't wait on the event thread. */
1669 wait_for_stop (ti.ti_lid);
1670 return 0; /* finished: next thread. */
1674 new_wait_thread_callback (threadinfo *thread, void *data)
1676 /* don't wait on the event thread -- it's already stopped and waited.
1677 Ditto the main thread. */
1678 if (thread->lid != event_pid &&
1679 thread->lid != main_prochandle.pid)
1681 wait_for_stop (thread->lid);
1687 * Wait for any thread to stop, by calling the underlying wait method.
1688 * The PID returned by the underlying target may be a kernel thread,
1689 * in which case we will want to convert it to the corresponding
1690 * user-space thread.
1694 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
1696 td_thrhandle_t thandle;
1705 /* OK, we're about to wait for an event from the running inferior.
1706 Make sure we're ignoring the right signals. */
1708 check_all_signal_numbers (); /* see if magic signals changed. */
1713 /* FIXME: should I do the wait right here inline? */
1715 if (PIDGET (ptid) == -1)
1718 lwp = get_lwp_from_thread_id (GET_THREAD (ptid));
1722 save_errno = linux_child_wait (-1, &retpid, &status);
1723 store_waitstatus (ourstatus, status);
1725 /* Thread ID is irrelevant if the target process exited.
1726 FIXME: do I have any killing to do?
1727 Can I get this event mistakenly from a thread? */
1728 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
1729 return pid_to_ptid (retpid);
1731 /* OK, we got an event of interest.
1732 Go stop all threads and look for new ones.
1733 FIXME: maybe don't do this for the restart signal? Optimization... */
1736 /* If the last call to resume was for a specific thread, then we don't
1737 need to stop everyone else: they should already be stopped. */
1738 if (last_resume_step == 0 || last_resume_pid == -1)
1740 /* Main thread must be stopped before calling the iterator. */
1741 if (retpid != main_prochandle.pid)
1743 kill (main_prochandle.pid, SIGSTOP);
1744 wait_for_stop (main_prochandle.pid);
1747 empty_threadlist ();
1748 /* Now stop everyone else, and attach any new threads you find. */
1749 p_td_ta_thr_iter (main_threadagent,
1750 stop_or_attach_thread_callback,
1753 TD_THR_LOWEST_PRIORITY,
1755 TD_THR_ANY_USER_FLAGS);
1757 /* Now go call wait on all the threads we've stopped:
1758 This allows us to absorb the SIGKILL event, and to make sure
1759 that the thread knows that it is stopped (Linux peculiarity). */
1761 threadlist_iter (new_wait_thread_callback,
1767 /* Convert the kernel thread id to the corresponding thread id. */
1769 /* If the process layer does not furnish an lwp,
1770 then perhaps the returned pid IS the lwp... */
1771 #if 0 /* Always true (if it'd compile...) */
1772 if ((lwp = GET_LWP (pid_to_ptid (retpid))) == 0)
1776 if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK)
1777 return pid_to_ptid (retpid); /* LWP is not mapped onto a user-space thread. */
1779 if ((ret = p_td_thr_validate (&thandle)) != TD_OK)
1780 return pid_to_ptid (retpid); /* LWP is not mapped onto a valid thread. */
1782 if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK)
1784 warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret));
1785 return pid_to_ptid (retpid);
1788 retptid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
1789 /* If this is a new user thread, notify GDB about it. */
1790 if (!in_thread_list (retptid))
1792 printf_filtered ("[New %s]\n", target_pid_to_str (retptid));
1793 add_thread (retptid);
1797 /* Now detect if this is a thread creation/deletion event: */
1798 check_for_thread_event (ourstatus, retpid);
1804 * kill has to call the underlying kill.
1805 * FIXME: I'm not sure if it's necessary to check inferior_ptid any more,
1806 * but we might need to fix inferior_ptid up if it's a user thread.
1810 kill_thread_callback (const td_thrhandle_t *th, void *data)
1816 For Linux, threads may need to be waited. */
1817 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1819 warning ("kill_thread_callback: %s", thr_err_string (ret));
1820 return -1; /* bail out, get_info failed. */
1823 if (ti.ti_lid != main_prochandle.pid)
1825 kill (ti.ti_lid, SIGKILL);
1831 static void thread_db_kill (void)
1837 For Linux, threads may need to be waited. */
1838 if (! ptid_equal (inferior_ptid, null_ptid))
1840 /* Go kill the children first. Save the main thread for last. */
1841 p_td_ta_thr_iter (main_threadagent,
1842 kill_thread_callback,
1845 TD_THR_LOWEST_PRIORITY,
1847 TD_THR_ANY_USER_FLAGS);
1849 /* Turn off thread_db event-reporting API *before* killing the
1850 main thread, since this operation requires child memory access.
1851 Can't move this into thread_db_unpush target because then
1852 detach would not work. */
1853 disable_thread_event_reporting (main_threadagent);
1855 inferior_ptid = pid_to_ptid (main_prochandle.pid);
1858 * Since both procfs_kill and ptrace_kill call target_mourn,
1859 * it should be sufficient for me to call one of them.
1860 * That will result in my mourn being called, which will both
1861 * unpush me and call the underlying mourn.
1863 target_beneath->to_kill ();
1866 /* Wait for all threads. */
1867 /* FIXME: need a universal wait_for_signal func? */
1870 rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
1872 while (rpid > 0 || errno == EINTR);
1876 rpid = waitpid (-1, &status, WNOHANG);
1878 while (rpid > 0 || errno == EINTR);
1882 * Mourn has to remove us from the target stack,
1883 * and then call the underlying mourn.
1886 static void thread_db_mourn_inferior (void)
1888 thread_db_unpush_target ();
1889 target_mourn_inferior (); /* call the underlying mourn */
1893 * Detach has to remove us from the target stack,
1894 * and then call the underlying detach.
1896 * But first, it has to detach all the cloned threads!
1900 detach_thread_callback (const td_thrhandle_t *th, void *data)
1902 /* Called once per thread. */
1906 if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
1908 warning ("detach_thread_callback: %s", thr_err_string (ret));
1909 return -1; /* bail out, get_info failed. */
1912 if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid)))
1913 return 0; /* apparently we don't know this one. */
1915 /* Save main thread for last, or the iterator will fail! */
1916 if (ti.ti_lid != main_prochandle.pid)
1918 struct cleanup *old_chain;
1921 /* Time to detach this thread.
1922 First disable thread_db event reporting for the thread. */
1923 if (p_td_thr_event_enable &&
1924 (ret = p_td_thr_event_enable (th, off)) != TD_OK)
1926 warning ("detach_thread_callback: %s\n", thr_err_string (ret));
1930 /* Now cancel any pending SIGTRAPS. FIXME! */
1932 /* Call underlying detach method. FIXME just detach it. */
1933 old_chain = save_inferior_ptid ();
1934 inferior_ptid = pid_to_ptid (ti.ti_lid);
1935 detach (TARGET_SIGNAL_0);
1936 do_cleanups (old_chain);
1942 thread_db_detach (char *args, int from_tty)
1946 if ((ret = p_td_ta_thr_iter (main_threadagent,
1947 detach_thread_callback,
1950 TD_THR_LOWEST_PRIORITY,
1952 TD_THR_ANY_USER_FLAGS))
1954 warning ("detach (thr_iter): %s", thr_err_string (ret));
1956 /* Turn off thread_db event-reporting API
1957 (before detaching the main thread) */
1958 disable_thread_event_reporting (main_threadagent);
1960 thread_db_unpush_target ();
1962 /* above call nullifies target_beneath, so don't use that! */
1963 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1964 target_detach (args, from_tty);
1969 * We never want to actually create the inferior!
1971 * If this is ever called, it means we were on the target stack
1972 * when the user said "run". But we don't want to be on the new
1973 * inferior's target stack until the thread_db / libthread
1974 * connection is ready to be made.
1976 * So, what shall we do?
1977 * Unpush ourselves from the stack, and then invoke
1978 * find_default_create_inferior, which will invoke the
1979 * appropriate process_stratum target to do the create.
1983 thread_db_create_inferior (char *exec_file, char *allargs, char **env)
1985 thread_db_unpush_target ();
1986 find_default_create_inferior (exec_file, allargs, env);
1990 * Thread_db target vector initializer.
1994 init_thread_db_ops (void)
1996 thread_db_ops.to_shortname = "multi-thread";
1997 thread_db_ops.to_longname = "multi-threaded child process.";
1998 thread_db_ops.to_doc = "Threads and pthreads support.";
1999 thread_db_ops.to_files_info = thread_db_files_info;
2000 thread_db_ops.to_create_inferior = thread_db_create_inferior;
2001 thread_db_ops.to_detach = thread_db_detach;
2002 thread_db_ops.to_wait = thread_db_wait;
2003 thread_db_ops.to_resume = thread_db_resume;
2004 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
2005 thread_db_ops.to_kill = thread_db_kill;
2006 thread_db_ops.to_xfer_memory = thread_db_xfer_memory;
2007 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
2008 thread_db_ops.to_store_registers = thread_db_store_registers;
2009 thread_db_ops.to_thread_alive = thread_db_alive;
2010 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
2011 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
2012 thread_db_ops.to_stratum = thread_stratum;
2013 thread_db_ops.to_has_thread_control = tc_schedlock;
2014 thread_db_ops.to_magic = OPS_MAGIC;
2016 #endif /* HAVE_STDINT_H */
2019 * Module constructor / initializer function.
2020 * If connection to thread_db dynamic library is successful,
2021 * then initialize this module's target vectors and the
2027 _initialize_thread_db (void)
2029 #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */
2030 if (init_thread_db_library ())
2032 init_thread_db_ops ();
2033 add_target (&thread_db_ops);
2035 * Hook up to the new_objfile event.
2036 * If someone is already there, arrange for him to be called
2039 target_new_objfile_chain = target_new_objfile_hook;
2040 target_new_objfile_hook = thread_db_new_objfile;
2042 #endif /* HAVE_STDINT_H */