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 */
128 /* Prototypes for supply_gregset etc. */
132 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
133 #define PIDGET(PID) (((PID) & 0xffff))
134 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
137 /* Macros for superimposing PID and TID into inferior_pid. */
138 #define THREAD_FLAG 0x80000000
139 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
140 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
141 #define GET_LWP(PID) TIDGET (PID)
142 #define GET_THREAD(PID) TIDGET (PID)
143 #define BUILD_LWP(TID, PID) MERGEPID (PID, TID)
144 #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG)
147 * target_beneath is a pointer to the target_ops underlying this one.
150 static struct target_ops
*target_beneath
;
154 * target vector defined in this module:
157 static struct target_ops thread_db_ops
;
160 * Typedefs required to resolve differences between the thread_db
161 * and proc_service API defined on different versions of Solaris:
164 #if defined(PROC_SERVICE_IS_OLD)
165 typedef const struct ps_prochandle
*gdb_ps_prochandle_t
;
166 typedef char *gdb_ps_read_buf_t
;
167 typedef char *gdb_ps_write_buf_t
;
168 typedef int gdb_ps_size_t
;
170 typedef struct ps_prochandle
*gdb_ps_prochandle_t
;
171 typedef void *gdb_ps_read_buf_t
;
172 typedef const void *gdb_ps_write_buf_t
;
173 typedef size_t gdb_ps_size_t
;
176 /* Unfortunately glibc 2.1.3 was released with a broken prfpregset_t
177 type. We let configure check for this lossage, and make
178 appropriate typedefs here. */
180 #ifdef PRFPREGSET_T_BROKEN
181 typedef elf_fpregset_t gdb_prfpregset_t
;
183 typedef prfpregset_t gdb_prfpregset_t
;
187 * proc_service callback functions, called by thread_db.
191 ps_pstop (gdb_ps_prochandle_t ph
) /* Process stop */
197 ps_pcontinue (gdb_ps_prochandle_t ph
) /* Process continue */
203 ps_lstop (gdb_ps_prochandle_t ph
, /* LWP stop */
210 ps_lcontinue (gdb_ps_prochandle_t ph
, /* LWP continue */
217 ps_lgetxregsize (gdb_ps_prochandle_t ph
, /* Get XREG size */
225 ps_lgetxregs (gdb_ps_prochandle_t ph
, /* Get XREGS */
233 ps_lsetxregs (gdb_ps_prochandle_t ph
, /* Set XREGS */
241 ps_plog (const char *fmt
, ...)
245 va_start (args
, fmt
);
246 vfprintf_filtered (gdb_stderr
, fmt
, args
);
249 /* Look up a symbol in GDB's global symbol table.
250 Return the symbol's address.
251 FIXME: it would be more correct to look up the symbol in the context
252 of the LD_OBJECT_NAME provided. However we're probably fairly safe
253 as long as there aren't name conflicts with other libraries. */
256 ps_pglobal_lookup (gdb_ps_prochandle_t ph
,
257 const char *ld_object_name
, /* the library name */
258 const char *ld_symbol_name
, /* the symbol name */
259 paddr_t
*ld_symbol_addr
) /* return the symbol addr */
261 struct minimal_symbol
*ms
;
263 ms
= lookup_minimal_symbol (ld_symbol_name
, NULL
, NULL
);
268 *ld_symbol_addr
= SYMBOL_VALUE_ADDRESS (ms
);
273 /* Worker function for all memory reads and writes: */
274 static ps_err_e
rw_common (const struct ps_prochandle
*ph
,
280 /* target_xfer_memory direction consts */
281 enum {PS_READ
= 0, PS_WRITE
= 1};
284 ps_pdread (gdb_ps_prochandle_t ph
, /* read from data segment */
286 gdb_ps_read_buf_t buf
,
289 return rw_common (ph
, addr
, buf
, size
, PS_READ
);
293 ps_pdwrite (gdb_ps_prochandle_t ph
, /* write to data segment */
295 gdb_ps_write_buf_t buf
,
298 return rw_common (ph
, addr
, (char *) buf
, size
, PS_WRITE
);
302 ps_ptread (gdb_ps_prochandle_t ph
, /* read from text segment */
304 gdb_ps_read_buf_t buf
,
307 return rw_common (ph
, addr
, buf
, size
, PS_READ
);
311 ps_ptwrite (gdb_ps_prochandle_t ph
, /* write to text segment */
313 gdb_ps_write_buf_t buf
,
316 return rw_common (ph
, addr
, (char *) buf
, size
, PS_WRITE
);
319 static struct cleanup
*save_inferior_pid (void);
320 static void restore_inferior_pid (void *saved_pid
);
321 static char *thr_err_string (td_err_e
);
322 static char *thr_state_string (td_thr_state_e
);
324 struct ps_prochandle
{
328 struct ps_prochandle main_prochandle
;
329 td_thragent_t
* main_threadagent
;
332 * Common proc_service routine for reading and writing memory.
335 /* FIXME: once we've munged the inferior_pid, why can't we
336 simply call target_read/write_memory and return? */
340 rw_common (const struct ps_prochandle
*ph
,
346 struct cleanup
*old_chain
= save_inferior_pid ();
350 inferior_pid
= main_prochandle
.pid
;
354 done
= current_target
.to_xfer_memory (addr
, buf
, size
, write_p
,
358 if (write_p
== PS_READ
)
359 print_sys_errmsg ("rw_common (): read", errno
);
361 print_sys_errmsg ("rw_common (): write", errno
);
368 do_cleanups (old_chain
);
372 /* Cleanup functions used by the register callbacks
373 (which have to manipulate the global inferior_pid). */
376 ps_lgetregs (gdb_ps_prochandle_t ph
, /* Get LWP general regs */
380 struct cleanup
*old_chain
= save_inferior_pid ();
382 inferior_pid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
383 current_target
.to_fetch_registers (-1);
385 fill_gregset (gregset
, -1);
386 do_cleanups (old_chain
);
392 ps_lsetregs (gdb_ps_prochandle_t ph
, /* Set LWP general regs */
394 const prgregset_t gregset
)
396 struct cleanup
*old_chain
= save_inferior_pid ();
398 inferior_pid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
399 supply_gregset (gregset
);
400 current_target
.to_store_registers (-1);
401 do_cleanups (old_chain
);
406 ps_lgetfpregs (gdb_ps_prochandle_t ph
, /* Get LWP float regs */
408 gdb_prfpregset_t
*fpregset
)
410 struct cleanup
*old_chain
= save_inferior_pid ();
412 inferior_pid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
413 current_target
.to_fetch_registers (-1);
414 fill_fpregset (fpregset
, -1);
415 do_cleanups (old_chain
);
420 ps_lsetfpregs (gdb_ps_prochandle_t ph
, /* Set LWP float regs */
422 const gdb_prfpregset_t
*fpregset
)
424 struct cleanup
*old_chain
= save_inferior_pid ();
426 inferior_pid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
427 supply_fpregset (fpregset
);
428 current_target
.to_store_registers (-1);
429 do_cleanups (old_chain
);
436 * return the main pid for the child process
437 * (special for Linux -- not used on Solaris)
441 ps_getpid (gdb_ps_prochandle_t ph
)
448 /* Reads the local descriptor table of a LWP. */
451 ps_lgetLDT (gdb_ps_prochandle_t ph
, lwpid_t lwpid
,
454 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
455 extern struct ssd
*procfs_find_LDT_entry (int);
458 ret
= procfs_find_LDT_entry (BUILD_LWP (lwpid
,
459 PIDGET (main_prochandle
.pid
)));
462 memcpy (pldt
, ret
, sizeof (struct ssd
));
465 else /* LDT not found. */
468 #endif /* TM_I386SOL2_H */
471 * Pointers to thread_db functions:
473 * These are a dynamic library mechanism.
474 * The dlfcn.h interface will be used to initialize these
475 * so that they point to the appropriate functions in the
476 * thread_db dynamic library. This is done dynamically
477 * so that GDB can still run on systems that lack thread_db.
480 static td_err_e (*p_td_init
) (void);
482 static td_err_e (*p_td_ta_new
) (const struct ps_prochandle
*ph_p
,
483 td_thragent_t
**ta_pp
);
485 static td_err_e (*p_td_ta_delete
) (td_thragent_t
*ta_p
);
487 static td_err_e (*p_td_ta_get_nthreads
) (const td_thragent_t
*ta_p
,
491 static td_err_e (*p_td_ta_thr_iter
) (const td_thragent_t
*ta_p
,
494 td_thr_state_e state
,
496 sigset_t
*ti_sigmask_p
,
497 unsigned ti_user_flags
);
499 static td_err_e (*p_td_ta_event_addr
) (const td_thragent_t
*ta_p
,
501 td_notify_t
*notify_p
);
503 static td_err_e (*p_td_ta_event_getmsg
) (const td_thragent_t
*ta_p
,
504 td_event_msg_t
*msg
);
506 static td_err_e (*p_td_ta_set_event
) (const td_thragent_t
*ta_p
,
507 td_thr_events_t
*events
);
509 static td_err_e (*p_td_thr_validate
) (const td_thrhandle_t
*th_p
);
511 static td_err_e (*p_td_thr_event_enable
) (const td_thrhandle_t
*th_p
,
514 static td_err_e (*p_td_thr_get_info
) (const td_thrhandle_t
*th_p
,
517 static td_err_e (*p_td_thr_getgregs
) (const td_thrhandle_t
*th_p
,
520 static td_err_e (*p_td_thr_setgregs
) (const td_thrhandle_t
*th_p
,
521 const prgregset_t regset
);
523 static td_err_e (*p_td_thr_getfpregs
) (const td_thrhandle_t
*th_p
,
524 gdb_prfpregset_t
*fpregset
);
526 static td_err_e (*p_td_thr_setfpregs
) (const td_thrhandle_t
*th_p
,
527 const gdb_prfpregset_t
*fpregset
);
529 static td_err_e (*p_td_ta_map_id2thr
) (const td_thragent_t
*ta_p
,
531 td_thrhandle_t
*th_p
);
533 static td_err_e (*p_td_ta_map_lwp2thr
) (const td_thragent_t
*ta_p
,
535 td_thrhandle_t
*th_p
);
538 * API and target vector initialization function: thread_db_initialize.
540 * NOTE: this function is deliberately NOT named with the GDB convention
541 * of module initializer function names that begin with "_initialize".
542 * This module is NOT intended to be auto-initialized at GDB startup.
543 * Rather, it will only be initialized when a multi-threaded child
544 * process is detected.
549 * Initializer for thread_db library interface.
550 * This function does the dynamic library stuff (dlopen, dlsym),
551 * and then calls the thread_db library's one-time initializer
552 * function (td_init). If everything succeeds, this function
553 * returns true; otherwise it returns false, and this module
558 init_thread_db_library ()
563 /* Open a handle to the "thread_db" dynamic library. */
564 if ((dlhandle
= dlopen ("libthread_db.so.1", RTLD_NOW
)) == NULL
)
567 /* Initialize pointers to the dynamic library functions we will use.
568 * Note that we are not calling the functions here -- we are only
569 * establishing pointers to them.
572 /* td_init: initialize thread_db library. */
573 if ((p_td_init
= dlsym (dlhandle
, "td_init")) == NULL
)
575 /* td_ta_new: register a target process with thread_db. */
576 if ((p_td_ta_new
= dlsym (dlhandle
, "td_ta_new")) == NULL
)
578 /* td_ta_delete: un-register a target process with thread_db. */
579 if ((p_td_ta_delete
= dlsym (dlhandle
, "td_ta_delete")) == NULL
)
582 /* td_ta_map_id2thr: get thread handle from thread id. */
583 if ((p_td_ta_map_id2thr
= dlsym (dlhandle
, "td_ta_map_id2thr")) == NULL
)
585 /* td_ta_map_lwp2thr: get thread handle from lwp id. */
586 if ((p_td_ta_map_lwp2thr
= dlsym (dlhandle
, "td_ta_map_lwp2thr")) == NULL
)
588 /* td_ta_get_nthreads: get number of threads in target process. */
589 if ((p_td_ta_get_nthreads
= dlsym (dlhandle
, "td_ta_get_nthreads")) == NULL
)
591 /* td_ta_thr_iter: iterate over all thread handles. */
592 if ((p_td_ta_thr_iter
= dlsym (dlhandle
, "td_ta_thr_iter")) == NULL
)
595 /* td_thr_validate: make sure a thread handle is real and alive. */
596 if ((p_td_thr_validate
= dlsym (dlhandle
, "td_thr_validate")) == NULL
)
598 /* td_thr_get_info: get a bunch of info about a thread. */
599 if ((p_td_thr_get_info
= dlsym (dlhandle
, "td_thr_get_info")) == NULL
)
601 /* td_thr_getgregs: get general registers for thread. */
602 if ((p_td_thr_getgregs
= dlsym (dlhandle
, "td_thr_getgregs")) == NULL
)
604 /* td_thr_setgregs: set general registers for thread. */
605 if ((p_td_thr_setgregs
= dlsym (dlhandle
, "td_thr_setgregs")) == NULL
)
607 /* td_thr_getfpregs: get floating point registers for thread. */
608 if ((p_td_thr_getfpregs
= dlsym (dlhandle
, "td_thr_getfpregs")) == NULL
)
610 /* td_thr_setfpregs: set floating point registers for thread. */
611 if ((p_td_thr_setfpregs
= dlsym (dlhandle
, "td_thr_setfpregs")) == NULL
)
617 warning ("init_thread_db: td_init: %s", thr_err_string (ret
));
621 /* Optional functions:
622 We can still debug even if the following functions are not found. */
624 /* td_ta_event_addr: get the breakpoint address for specified event. */
625 p_td_ta_event_addr
= dlsym (dlhandle
, "td_ta_event_addr");
627 /* td_ta_event_getmsg: get the next event message for the process. */
628 p_td_ta_event_getmsg
= dlsym (dlhandle
, "td_ta_event_getmsg");
630 /* td_ta_set_event: request notification of an event. */
631 p_td_ta_set_event
= dlsym (dlhandle
, "td_ta_set_event");
633 /* td_thr_event_enable: enable event reporting in a thread. */
634 p_td_thr_event_enable
= dlsym (dlhandle
, "td_thr_event_enable");
636 return 1; /* success */
640 * Local utility functions:
648 save_inferior_pid - Save inferior_pid on the cleanup list
649 restore_inferior_pid - Restore inferior_pid from the cleanup list
653 struct cleanup *save_inferior_pid (void);
654 void restore_inferior_pid (void *saved_pid);
658 These two functions act in unison to restore inferior_pid in
663 inferior_pid is a global variable that needs to be changed by many
664 of these routines before calling functions in procfs.c. In order
665 to guarantee that inferior_pid gets restored (in case of errors),
666 you need to call save_inferior_pid before changing it. At the end
667 of the function, you should invoke do_cleanups to restore it.
671 static struct cleanup
*
672 save_inferior_pid (void)
676 saved_pid_ptr
= xmalloc (sizeof (int));
677 *saved_pid_ptr
= inferior_pid
;
678 return make_cleanup (restore_inferior_pid
, saved_pid_ptr
);
682 restore_inferior_pid (void *arg
)
684 int *saved_pid_ptr
= arg
;
685 inferior_pid
= *saved_pid_ptr
;
693 thr_err_string - Convert a thread_db error code to a string
697 char * thr_err_string (errcode)
701 Return a string description of the thread_db errcode. If errcode
702 is unknown, then return an <unknown> message.
707 thr_err_string (errcode
)
713 case TD_OK
: return "generic 'call succeeded'";
714 case TD_ERR
: return "generic error";
715 case TD_NOTHR
: return "no thread to satisfy query";
716 case TD_NOSV
: return "no sync handle to satisfy query";
717 case TD_NOLWP
: return "no lwp to satisfy query";
718 case TD_BADPH
: return "invalid process handle";
719 case TD_BADTH
: return "invalid thread handle";
720 case TD_BADSH
: return "invalid synchronization handle";
721 case TD_BADTA
: return "invalid thread agent";
722 case TD_BADKEY
: return "invalid key";
723 case TD_NOMSG
: return "no event message for getmsg";
724 case TD_NOFPREGS
: return "FPU register set not available";
725 case TD_NOLIBTHREAD
: return "application not linked with libthread";
726 case TD_NOEVENT
: return "requested event is not supported";
727 case TD_NOCAPAB
: return "capability not available";
728 case TD_DBERR
: return "debugger service failed";
729 case TD_NOAPLIC
: return "operation not applicable to";
730 case TD_NOTSD
: return "no thread-specific data for this thread";
731 case TD_MALLOC
: return "malloc failed";
732 case TD_PARTIALREG
: return "only part of register set was written/read";
733 case TD_NOXREGS
: return "X register set not available for this thread";
735 sprintf (buf
, "unknown thread_db error '%d'", errcode
);
744 thr_state_string - Convert a thread_db state code to a string
748 char *thr_state_string (statecode)
752 Return the thread_db state string associated with statecode.
753 If statecode is unknown, then return an <unknown> message.
758 thr_state_string (statecode
)
759 td_thr_state_e statecode
;
764 case TD_THR_STOPPED
: return "stopped by debugger";
765 case TD_THR_RUN
: return "runnable";
766 case TD_THR_ACTIVE
: return "active";
767 case TD_THR_ZOMBIE
: return "zombie";
768 case TD_THR_SLEEP
: return "sleeping";
769 case TD_THR_STOPPED_ASLEEP
: return "stopped by debugger AND blocked";
771 sprintf (buf
, "unknown thread_db state %d", statecode
);
777 * Local thread/event list.
778 * This data structure will be used to hold a list of threads and
779 * pending/deliverable events.
782 typedef struct THREADINFO
{
783 thread_t tid
; /* thread ID */
784 pid_t lid
; /* process/lwp ID */
785 td_thr_state_e state
; /* thread state (a la thread_db) */
786 td_thr_type_e type
; /* thread type (a la thread_db) */
787 int pending
; /* true if holding a pending event */
788 int status
; /* wait status of any interesting event */
791 threadinfo
* threadlist
;
792 int threadlist_max
= 0; /* current size of table */
793 int threadlist_top
= 0; /* number of threads now in table */
794 #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */
797 insert_thread (tid
, lid
, state
, type
)
800 td_thr_state_e state
;
803 if (threadlist_top
>= threadlist_max
)
805 threadlist_max
+= THREADLIST_ALLOC
;
806 threadlist
= realloc (threadlist
,
807 threadlist_max
* sizeof (threadinfo
));
808 if (threadlist
== NULL
)
811 threadlist
[threadlist_top
].tid
= tid
;
812 threadlist
[threadlist_top
].lid
= lid
;
813 threadlist
[threadlist_top
].state
= state
;
814 threadlist
[threadlist_top
].type
= type
;
815 threadlist
[threadlist_top
].pending
= 0;
816 threadlist
[threadlist_top
].status
= 0;
818 return &threadlist
[threadlist_top
++];
828 next_pending_event ()
832 for (i
= 0; i
< threadlist_top
; i
++)
833 if (threadlist
[i
].pending
)
834 return &threadlist
[i
];
840 threadlist_iter (func
, data
, state
, type
)
843 td_thr_state_e state
;
848 for (i
= 0; i
< threadlist_top
; i
++)
849 if ((state
== TD_THR_ANY_STATE
|| state
== threadlist
[i
].state
) &&
850 (type
== TD_THR_ANY_TYPE
|| type
== threadlist
[i
].type
))
851 if ((*func
) (&threadlist
[i
], data
) != 0)
860 * Here we keep state information all collected in one place.
863 /* This flag is set when we activate, so that we don't do it twice.
864 Defined in linux-thread.c and used for inter-target syncronization. */
865 extern int using_thread_db
;
867 /* The process id for which we've stopped.
868 * This is only set when we actually stop all threads.
869 * Otherwise it's zero.
871 static int event_pid
;
874 * The process id for a new thread to which we've just attached.
875 * This process needs special handling at resume time.
877 static int attach_pid
;
881 * thread_db event handling:
883 * The mechanism for event notification via the thread_db API.
884 * These events are implemented as breakpoints. The thread_db
885 * library gives us an address where we can set a breakpoint.
886 * When the breakpoint is hit, it represents an event of interest
893 /* Location of the thread creation event breakpoint. The code at this
894 location in the child process will be called by the pthread library
895 whenever a new thread is created. By setting a special breakpoint
896 at this location, GDB can detect when a new thread is created. We
897 obtain this location via the td_ta_event_addr call. */
899 static CORE_ADDR thread_creation_bkpt_address
;
901 /* Location of the thread death event breakpoint. The code at this
902 location in the child process will be called by the pthread library
903 whenever a thread is destroyed. By setting a special breakpoint at
904 this location, GDB can detect when a new thread is created. We
905 obtain this location via the td_ta_event_addr call. */
907 static CORE_ADDR thread_death_bkpt_address
;
909 /* This function handles the global parts of enabling thread events.
910 The thread-specific enabling is handled per-thread elsewhere. */
913 enable_thread_event_reporting (ta
)
916 td_thr_events_t events
;
920 if (p_td_ta_set_event
== NULL
||
921 p_td_ta_event_addr
== NULL
||
922 p_td_ta_event_getmsg
== NULL
||
923 p_td_thr_event_enable
== NULL
)
924 return; /* can't do thread event reporting without these funcs */
926 /* set process wide mask saying which events we are interested in */
927 td_event_emptyset (&events
);
928 td_event_addset (&events
, TD_CREATE
);
929 td_event_addset (&events
, TD_DEATH
);
931 if (p_td_ta_set_event (ta
, &events
) != TD_OK
)
933 warning ("unable to set global thread event mask");
937 /* Delete previous thread event breakpoints, if any. */
938 remove_thread_event_breakpoints ();
940 /* create breakpoints -- thread creation and death */
941 /* thread creation */
942 /* get breakpoint location */
943 if (p_td_ta_event_addr (ta
, TD_CREATE
, ¬ify
) != TD_OK
)
945 warning ("unable to get location for thread creation breakpoint");
949 /* Set up the breakpoint. */
950 create_thread_event_breakpoint (notify
.u
.bptaddr
);
952 /* Save it's location. */
953 thread_creation_bkpt_address
= notify
.u
.bptaddr
;
956 /* get breakpoint location */
957 if (p_td_ta_event_addr (ta
, TD_DEATH
, ¬ify
) != TD_OK
)
959 warning ("unable to get location for thread death breakpoint");
962 /* Set up the breakpoint. */
963 create_thread_event_breakpoint (notify
.u
.bptaddr
);
965 /* Save it's location. */
966 thread_death_bkpt_address
= notify
.u
.bptaddr
;
969 /* This function handles the global parts of disabling thread events.
970 The thread-specific enabling is handled per-thread elsewhere. */
973 disable_thread_event_reporting (ta
)
976 td_thr_events_t events
;
978 /* set process wide mask saying we aren't interested in any events */
979 td_event_emptyset (&events
);
980 p_td_ta_set_event (main_threadagent
, &events
);
982 /* Delete thread event breakpoints, if any. */
983 remove_thread_event_breakpoints ();
984 thread_creation_bkpt_address
= 0;
985 thread_death_bkpt_address
= 0;
988 /* check_for_thread_event
990 if it's a thread event we recognize (currently
991 we only recognize creation and destruction
992 events), return 1; else return 0. */
996 check_for_thread_event (struct target_waitstatus
*tws
, int event_pid
)
998 /* FIXME: to be more efficient, we should keep a static
999 list of threads, and update it only here (with td_ta_thr_iter). */
1003 thread_db_push_target (void)
1005 /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */
1007 /* Push this target vector */
1008 push_target (&thread_db_ops
);
1009 /* Find the underlying process-layer target for calling later. */
1010 target_beneath
= find_target_beneath (&thread_db_ops
);
1011 using_thread_db
= 1;
1012 /* Turn on thread_db event-reporting API. */
1013 enable_thread_event_reporting (main_threadagent
);
1017 thread_db_unpush_target (void)
1019 /* Must be called whenever we remove ourself from the target stack! */
1021 using_thread_db
= 0;
1022 target_beneath
= NULL
;
1024 /* delete local list of threads */
1025 empty_threadlist ();
1026 /* Turn off the thread_db API. */
1027 p_td_ta_delete (main_threadagent
);
1028 /* Unpush this target vector */
1029 unpush_target (&thread_db_ops
);
1030 /* Reset linuxthreads module. */
1031 linuxthreads_discard_global_state ();
1035 * New objfile hook function:
1036 * Called for each new objfile (image, shared lib) in the target process.
1038 * The purpose of this function is to detect that the target process
1039 * is linked with the (appropriate) thread library. So every time a
1040 * new target shared library is detected, we will call td_ta_new.
1041 * If it succeeds, we know we have a multi-threaded target process
1042 * that we can debug using the thread_db API.
1046 * new_objfile function:
1048 * connected to target_new_objfile_hook, this function gets called
1049 * every time a new binary image is loaded.
1051 * At each call, we attempt to open the thread_db connection to the
1052 * child process. If it succeeds, we know we have a libthread process
1053 * and we can debug it with this target vector. Therefore we push
1054 * ourself onto the target stack.
1057 static void (*target_new_objfile_chain
) (struct objfile
*objfile
);
1058 static int stop_or_attach_thread_callback (const td_thrhandle_t
*th
,
1060 static int wait_thread_callback (const td_thrhandle_t
*th
,
1064 thread_db_new_objfile (struct objfile
*objfile
)
1068 if (using_thread_db
) /* libthread already detected, and */
1069 goto quit
; /* thread target vector activated. */
1071 if (objfile
== NULL
)
1072 goto quit
; /* un-interesting object file */
1074 /* Initialize our "main prochandle" with the main inferior pid. */
1075 main_prochandle
.pid
= PIDGET (inferior_pid
);
1077 /* Now attempt to open a thread_db connection to the
1078 thread library running in the child process. */
1079 ret
= p_td_ta_new (&main_prochandle
, &main_threadagent
);
1082 warning ("Unexpected error initializing thread_db: %s",
1083 thr_err_string (ret
));
1085 case TD_NOLIBTHREAD
: /* expected: no libthread in child process (yet) */
1087 case TD_OK
: /* libthread detected in child: we go live now! */
1088 thread_db_push_target ();
1089 event_pid
= inferior_pid
; /* for resume */
1091 /* Now stop everyone else, and attach any new threads you find. */
1092 p_td_ta_thr_iter (main_threadagent
,
1093 stop_or_attach_thread_callback
,
1096 TD_THR_LOWEST_PRIORITY
,
1098 TD_THR_ANY_USER_FLAGS
);
1100 /* Now go call wait on all the threads you've stopped:
1101 This allows us to absorb the SIGKILL event, and to make sure
1102 that the thread knows that it is stopped (Linux peculiarity). */
1103 p_td_ta_thr_iter (main_threadagent
,
1104 wait_thread_callback
,
1107 TD_THR_LOWEST_PRIORITY
,
1109 TD_THR_ANY_USER_FLAGS
);
1114 if (target_new_objfile_chain
)
1115 target_new_objfile_chain (objfile
);
1123 thread_db_alive - test thread for "aliveness"
1127 static bool thread_db_alive (int pid);
1131 returns true if thread still active in inferior.
1136 thread_db_alive (pid
)
1139 if (is_thread (pid
)) /* user-space (non-kernel) thread */
1144 pid
= GET_THREAD (pid
);
1145 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, pid
, &th
)) != TD_OK
)
1146 return 0; /* thread not found */
1147 if ((ret
= p_td_thr_validate (&th
)) != TD_OK
)
1148 return 0; /* thread not valid */
1149 return 1; /* known thread: return true */
1151 else if (target_beneath
->to_thread_alive
)
1152 return target_beneath
->to_thread_alive (pid
);
1154 return 0; /* default to "not alive" (shouldn't happen anyway) */
1158 * get_lwp_from_thread_handle
1161 static int /* lwpid_t or pid_t */
1162 get_lwp_from_thread_handle (th
)
1168 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1169 error ("get_lwp_from_thread_handle: thr_get_info failed: %s",
1170 thr_err_string (ret
));
1176 * get_lwp_from_thread_id
1179 static int /* lwpid_t or pid_t */
1180 get_lwp_from_thread_id (tid
)
1181 int tid
; /* thread_t? */
1186 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, tid
, &th
)) != TD_OK
)
1187 error ("get_lwp_from_thread_id: map_id2thr failed: %s",
1188 thr_err_string (ret
));
1190 return get_lwp_from_thread_handle (&th
);
1194 * pid_to_str has to handle user-space threads.
1195 * If not a user-space thread, then pass the request on to the
1196 * underlying stratum if it can handle it: else call normal_pid_to_str.
1200 thread_db_pid_to_str (int pid
)
1202 static char buf
[100];
1207 if (is_thread (pid
))
1209 if ((ret
= p_td_ta_map_id2thr (main_threadagent
,
1212 error ("thread_db: map_id2thr failed: %s", thr_err_string (ret
));
1214 if ((ret
= p_td_thr_get_info (&th
, &ti
)) != TD_OK
)
1215 error ("thread_db: thr_get_info failed: %s", thr_err_string (ret
));
1217 if (ti
.ti_state
== TD_THR_ACTIVE
&&
1219 sprintf (buf
, "Thread %d (LWP %d)", ti
.ti_tid
, ti
.ti_lid
);
1221 sprintf (buf
, "Thread %d (%s)", ti
.ti_tid
,
1222 thr_state_string (ti
.ti_state
));
1224 else if (GET_LWP (pid
))
1225 sprintf (buf
, "LWP %d", GET_LWP (pid
));
1226 else return normal_pid_to_str (pid
);
1232 * thread_db target vector functions:
1236 thread_db_files_info (struct target_ops
*tgt_vector
)
1238 /* This function will be unnecessary in real life. */
1239 printf_filtered ("thread_db stratum:\n");
1240 target_beneath
->to_files_info (tgt_vector
);
1244 * xfer_memory has to munge the inferior_pid before passing the call
1245 * down to the target layer.
1249 thread_db_xfer_memory (memaddr
, myaddr
, len
, dowrite
, target
)
1254 struct target_ops
*target
; /* ignored */
1256 struct cleanup
*old_chain
;
1259 old_chain
= save_inferior_pid ();
1261 if (is_thread (inferior_pid
) ||
1262 !target_thread_alive (inferior_pid
))
1264 /* FIXME: use the LID/LWP, so that underlying process layer
1265 can read memory from specific threads? */
1266 inferior_pid
= main_prochandle
.pid
;
1269 ret
= target_beneath
->to_xfer_memory (memaddr
, myaddr
, len
,
1271 do_cleanups (old_chain
);
1276 * fetch_registers has to determine if inferior_pid is a user-space thread.
1277 * If so, we use the thread_db API to get the registers.
1278 * And if not, we call the underlying process stratum.
1282 thread_db_fetch_registers (regno
)
1285 td_thrhandle_t thandle
;
1286 gdb_prfpregset_t fpregset
;
1287 prgregset_t gregset
;
1291 if (!is_thread (inferior_pid
)) /* kernel thread */
1292 { /* pass the request on to the target underneath. */
1293 target_beneath
->to_fetch_registers (regno
);
1297 /* convert inferior_pid into a td_thrhandle_t */
1299 if ((thread
= GET_THREAD (inferior_pid
)) == 0)
1300 error ("fetch_registers: thread == 0");
1302 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, thread
, &thandle
)) != TD_OK
)
1303 error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret
));
1305 /* Get the integer regs:
1306 For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7,
1307 pc and sp are saved (by a thread context switch). */
1308 if ((ret
= p_td_thr_getgregs (&thandle
, gregset
)) != TD_OK
&&
1309 ret
!= TD_PARTIALREG
)
1310 error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret
));
1312 /* And, now the fp regs */
1313 if ((ret
= p_td_thr_getfpregs (&thandle
, &fpregset
)) != TD_OK
&&
1315 error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret
));
1317 /* Note that we must call supply_{g fp}regset *after* calling the td routines
1318 because the td routines call ps_lget* which affect the values stored in the
1321 supply_gregset (gregset
);
1322 supply_fpregset (&fpregset
);
1327 * store_registers has to determine if inferior_pid is a user-space thread.
1328 * If so, we use the thread_db API to get the registers.
1329 * And if not, we call the underlying process stratum.
1333 thread_db_store_registers (regno
)
1336 td_thrhandle_t thandle
;
1337 gdb_prfpregset_t fpregset
;
1338 prgregset_t gregset
;
1342 if (!is_thread (inferior_pid
)) /* Kernel thread: */
1343 { /* pass the request on to the underlying target vector. */
1344 target_beneath
->to_store_registers (regno
);
1348 /* convert inferior_pid into a td_thrhandle_t */
1350 if ((thread
= GET_THREAD (inferior_pid
)) == 0)
1351 error ("store_registers: thread == 0");
1353 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, thread
, &thandle
)) != TD_OK
)
1354 error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret
));
1357 { /* Not writing all the regs */
1358 /* save new register value */
1359 /* MVS: I don't understand this... */
1360 char old_value
[REGISTER_SIZE
];
1362 memcpy (old_value
, ®isters
[REGISTER_BYTE (regno
)], REGISTER_SIZE
);
1364 if ((ret
= p_td_thr_getgregs (&thandle
, gregset
)) != TD_OK
)
1365 error ("store_registers: td_thr_getgregs %s", thr_err_string (ret
));
1366 if ((ret
= p_td_thr_getfpregs (&thandle
, &fpregset
)) != TD_OK
)
1367 error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret
));
1369 /* restore new register value */
1370 memcpy (®isters
[REGISTER_BYTE (regno
)], old_value
, REGISTER_SIZE
);
1374 fill_gregset (gregset
, regno
);
1375 fill_fpregset (&fpregset
, regno
);
1377 if ((ret
= p_td_thr_setgregs (&thandle
, gregset
)) != TD_OK
)
1378 error ("store_registers: td_thr_setgregs %s", thr_err_string (ret
));
1379 if ((ret
= p_td_thr_setfpregs (&thandle
, &fpregset
)) != TD_OK
&&
1381 error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret
));
1385 handle_new_thread (tid
, lid
, verbose
)
1386 int tid
; /* user thread id */
1387 int lid
; /* kernel thread id */
1390 int gdb_pid
= BUILD_THREAD (tid
, main_prochandle
.pid
);
1391 int wait_pid
, wait_status
;
1394 printf_filtered ("[New %s]\n", target_pid_to_str (gdb_pid
));
1395 add_thread (gdb_pid
);
1397 if (lid
!= main_prochandle
.pid
)
1399 attach_thread (lid
);
1400 /* According to the Eric Paire model, we now have to send
1401 the restart signal to the new thread -- however, empirically,
1402 I do not find that to be necessary. */
1408 test_for_new_thread (tid
, lid
, verbose
)
1413 if (!in_thread_list (BUILD_THREAD (tid
, main_prochandle
.pid
)))
1414 handle_new_thread (tid
, lid
, verbose
);
1418 * Callback function that gets called once per USER thread
1419 * (i.e., not kernel) thread by td_ta_thr_iter.
1423 find_new_threads_callback (th
, ignored
)
1424 const td_thrhandle_t
*th
;
1430 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1432 warning ("find_new_threads_callback: %s", thr_err_string (ret
));
1433 return -1; /* bail out, get_info failed. */
1437 As things now stand, this should never detect a new thread.
1438 But if it does, we could be in trouble because we aren't calling
1439 wait_thread_callback for it. */
1440 test_for_new_thread (ti
.ti_tid
, ti
.ti_lid
, 0);
1445 * find_new_threads uses the thread_db iterator function to discover
1446 * user-space threads. Then if the underlying process stratum has a
1447 * find_new_threads method, we call that too.
1451 thread_db_find_new_threads ()
1453 if (inferior_pid
== -1) /* FIXME: still necessary? */
1455 printf_filtered ("No process.\n");
1458 p_td_ta_thr_iter (main_threadagent
,
1459 find_new_threads_callback
,
1462 TD_THR_LOWEST_PRIORITY
,
1464 TD_THR_ANY_USER_FLAGS
);
1465 if (target_beneath
->to_find_new_threads
)
1466 target_beneath
->to_find_new_threads ();
1470 * Resume all threads, or resume a single thread.
1471 * If step is true, then single-step the appropriate thread
1472 * (or single-step inferior_pid, but continue everyone else).
1473 * If signo is true, then send that signal to at least one thread.
1477 * This function is called once for each thread before resuming.
1478 * It sends continue (no step, and no signal) to each thread except
1479 * the main thread, and
1480 * the event thread (the one that stopped at a breakpoint etc.)
1482 * The event thread is handled separately so that it can be sent
1483 * the stepping and signal args with which target_resume was called.
1485 * The main thread is resumed last, so that the thread_db proc_service
1486 * callbacks will still work during the iterator function.
1490 resume_thread_callback (th
, data
)
1491 const td_thrhandle_t
*th
;
1497 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1499 warning ("resume_thread_callback: %s", thr_err_string (ret
));
1500 return -1; /* bail out, get_info failed. */
1503 As things now stand, this should never detect a new thread.
1504 But if it does, we could be in trouble because we aren't calling
1505 wait_thread_callback for it. */
1506 test_for_new_thread (ti
.ti_tid
, ti
.ti_lid
, 1);
1508 if (ti
.ti_lid
!= main_prochandle
.pid
&&
1509 ti
.ti_lid
!= event_pid
)
1511 /* Unconditionally continue the thread with no signal.
1512 Only the event thread will get a signal of any kind. */
1514 target_beneath
->to_resume (ti
.ti_lid
, 0, 0);
1520 new_resume_thread_callback (thread
, data
)
1524 if (thread
->lid
!= event_pid
&&
1525 thread
->lid
!= main_prochandle
.pid
)
1527 /* Unconditionally continue the thread with no signal (for now). */
1529 target_beneath
->to_resume (thread
->lid
, 0, 0);
1534 static int last_resume_pid
;
1535 static int last_resume_step
;
1536 static int last_resume_signo
;
1539 thread_db_resume (pid
, step
, signo
)
1542 enum target_signal signo
;
1544 last_resume_pid
= pid
;
1545 last_resume_step
= step
;
1546 last_resume_signo
= signo
;
1548 /* resuming a specific pid? */
1551 if (is_thread (pid
))
1552 pid
= get_lwp_from_thread_id (GET_THREAD (pid
));
1553 else if (GET_LWP (pid
))
1554 pid
= GET_LWP (pid
);
1557 /* Apparently the interpretation of 'pid' is dependent on 'step':
1558 If step is true, then a specific pid means 'step only this pid'.
1559 But if step is not true, then pid means 'continue ALL pids, but
1560 give the signal only to this one'. */
1561 if (pid
!= -1 && step
)
1563 /* FIXME: is this gonna work in all circumstances? */
1564 target_beneath
->to_resume (pid
, step
, signo
);
1568 /* 1) Continue all threads except the event thread and the main thread.
1569 2) resume the event thread with step and signo.
1570 3) If event thread != main thread, continue the main thread.
1572 Note: order of 2 and 3 may need to be reversed. */
1574 threadlist_iter (new_resume_thread_callback
,
1578 /* now resume event thread, and if necessary also main thread. */
1581 target_beneath
->to_resume (event_pid
, step
, signo
);
1583 if (event_pid
!= main_prochandle
.pid
)
1585 target_beneath
->to_resume (main_prochandle
.pid
, 0, 0);
1590 /* All new threads will be attached.
1591 All previously known threads will be stopped using kill (SIGKILL). */
1594 stop_or_attach_thread_callback (const td_thrhandle_t
*th
, void *data
)
1601 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1603 warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret
));
1604 return -1; /* bail out, get_info failed. */
1607 /* First add it to our internal list.
1608 We build this list anew at every wait event. */
1609 insert_thread (ti
.ti_tid
, ti
.ti_lid
, ti
.ti_state
, ti
.ti_type
);
1610 /* Now: if we've already seen it, stop it, else add it and attach it. */
1611 gdb_pid
= BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
);
1612 if (!in_thread_list (gdb_pid
)) /* new thread */
1614 handle_new_thread (ti
.ti_tid
, ti
.ti_lid
, 1);
1615 /* Enable thread events */
1616 if (p_td_thr_event_enable
)
1617 if ((ret
= p_td_thr_event_enable (th
, on_off
)) != TD_OK
)
1618 warning ("stop_or_attach_thread: %s", thr_err_string (ret
));
1620 else if (ti
.ti_lid
!= event_pid
&&
1621 ti
.ti_lid
!= main_prochandle
.pid
)
1623 ret
= (td_err_e
) kill (ti
.ti_lid
, SIGSTOP
);
1630 * Wait for signal N from pid PID.
1631 * If wait returns any other signals, put them back before returning.
1642 /* Array of wait/signal status */
1643 /* FIXME: wrong data structure, we need a queue.
1644 Realtime signals may be delivered more than once.
1645 And at that, we really can't handle them (see below). */
1647 static int wstatus
[NSIG
];
1648 #elif defined (_NSIG)
1649 static int wstatus
[_NSIG
];
1651 #error No definition for number of signals!
1654 /* clear wait/status list */
1655 memset (&wstatus
, 0, sizeof (wstatus
));
1657 /* Now look for SIGSTOP event on all threads except event thread. */
1660 if (pid
== main_prochandle
.pid
)
1661 retpid
= waitpid (pid
, &status
, 0);
1663 retpid
= waitpid (pid
, &status
, __WCLONE
);
1666 if (WSTOPSIG (status
) == SIGSTOP
)
1668 /* Got the SIGSTOP event we're looking for.
1669 Throw it away, and throw any other events back! */
1670 for (i
= 0; i
< sizeof(wstatus
) / sizeof (wstatus
[0]); i
++)
1676 break; /* all done */
1681 /* Oops, got an event other than SIGSTOP.
1682 Save it, and throw it back after we find the SIGSTOP event. */
1684 /* FIXME (how?) This method is going to fail for realtime
1685 signals, which cannot be put back simply by using kill. */
1687 if (WIFEXITED (status
))
1688 error ("Ack! Thread Exited event. What do I do now???");
1689 else if (WIFSTOPPED (status
))
1690 signo
= WSTOPSIG (status
);
1692 signo
= WTERMSIG (status
);
1694 /* If a thread other than the event thread has hit a GDB
1695 breakpoint (as opposed to some random trap signal), then
1696 just arrange for it to hit it again later. Back up the
1697 PC if necessary. Don't forward the SIGTRAP signal to
1698 the thread. We will handle the current event, eventually
1699 we will resume all the threads, and this one will get
1700 it's breakpoint trap again.
1702 If we do not do this, then we run the risk that the user
1703 will delete or disable the breakpoint, but the thread will
1704 have already tripped on it. */
1706 if (retpid
!= event_pid
&&
1708 breakpoint_inserted_here_p (read_pc_pid (retpid
) -
1709 DECR_PC_AFTER_BREAK
))
1711 /* Set the pc to before the trap and DO NOT re-send the signal */
1712 if (DECR_PC_AFTER_BREAK
)
1713 write_pc_pid (read_pc_pid (retpid
) - DECR_PC_AFTER_BREAK
,
1717 /* Since SIGINT gets forwarded to the entire process group
1718 (in the case where ^C is typed at the tty / console),
1719 just ignore all SIGINTs from other than the event thread. */
1720 else if (retpid
!= event_pid
&& signo
== SIGINT
)
1721 { /* do nothing. Signal will disappear into oblivion! */
1725 else /* This is some random signal other than a breakpoint. */
1727 wstatus
[signo
] = 1;
1729 child_resume (retpid
, 0, TARGET_SIGNAL_0
);
1733 } while (errno
== 0 || errno
== EINTR
);
1737 * wait_thread_callback
1739 * Calls waitpid for each thread, repeatedly if necessary, until
1740 * SIGSTOP is returned. Afterward, if any other signals were returned
1741 * by waitpid, return them to the thread's pending queue by calling kill.
1745 wait_thread_callback (const td_thrhandle_t
*th
, void *data
)
1750 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1752 warning ("wait_thread_callback: %s", thr_err_string (ret
));
1753 return -1; /* bail out, get_info failed. */
1756 /* This callback to act on all threads except the event thread: */
1757 if (ti
.ti_lid
== event_pid
|| /* no need to wait (no sigstop) */
1758 ti
.ti_lid
== main_prochandle
.pid
) /* no need to wait (already waited) */
1759 return 0; /* don't wait on the event thread. */
1761 wait_for_stop (ti
.ti_lid
);
1762 return 0; /* finished: next thread. */
1766 new_wait_thread_callback (thread
, data
)
1770 /* don't wait on the event thread -- it's already stopped and waited.
1771 Ditto the main thread. */
1772 if (thread
->lid
!= event_pid
&&
1773 thread
->lid
!= main_prochandle
.pid
)
1775 wait_for_stop (thread
->lid
);
1781 * Wait for any thread to stop, by calling the underlying wait method.
1782 * The PID returned by the underlying target may be a kernel thread,
1783 * in which case we will want to convert it to the corresponding
1784 * user-space thread.
1788 thread_db_wait (int pid
, struct target_waitstatus
*ourstatus
)
1790 td_thrhandle_t thandle
;
1798 /* OK, we're about to wait for an event from the running inferior.
1799 Make sure we're ignoring the right signals. */
1801 check_all_signal_numbers (); /* see if magic signals changed. */
1806 /* FIXME: should I do the wait right here inline? */
1811 lwp
= get_lwp_from_thread_id (GET_THREAD (pid
));
1815 save_errno
= linux_child_wait (-1, &retpid
, &status
);
1816 store_waitstatus (ourstatus
, status
);
1818 /* Thread ID is irrelevant if the target process exited.
1819 FIXME: do I have any killing to do?
1820 Can I get this event mistakenly from a thread? */
1821 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
1824 /* OK, we got an event of interest.
1825 Go stop all threads and look for new ones.
1826 FIXME: maybe don't do this for the restart signal? Optimization... */
1829 /* If the last call to resume was for a specific thread, then we don't
1830 need to stop everyone else: they should already be stopped. */
1831 if (last_resume_step
== 0 || last_resume_pid
== -1)
1833 /* Main thread must be stopped before calling the iterator. */
1834 if (retpid
!= main_prochandle
.pid
)
1836 kill (main_prochandle
.pid
, SIGSTOP
);
1837 wait_for_stop (main_prochandle
.pid
);
1840 empty_threadlist ();
1841 /* Now stop everyone else, and attach any new threads you find. */
1842 p_td_ta_thr_iter (main_threadagent
,
1843 stop_or_attach_thread_callback
,
1846 TD_THR_LOWEST_PRIORITY
,
1848 TD_THR_ANY_USER_FLAGS
);
1850 /* Now go call wait on all the threads we've stopped:
1851 This allows us to absorb the SIGKILL event, and to make sure
1852 that the thread knows that it is stopped (Linux peculiarity). */
1854 threadlist_iter (new_wait_thread_callback
,
1860 /* Convert the kernel thread id to the corresponding thread id. */
1862 /* If the process layer does not furnish an lwp,
1863 then perhaps the returned pid IS the lwp... */
1864 if ((lwp
= GET_LWP (retpid
)) == 0)
1867 if ((ret
= p_td_ta_map_lwp2thr (main_threadagent
, lwp
, &thandle
)) != TD_OK
)
1868 return retpid
; /* LWP is not mapped onto a user-space thread. */
1870 if ((ret
= p_td_thr_validate (&thandle
)) != TD_OK
)
1871 return retpid
; /* LWP is not mapped onto a valid thread. */
1873 if ((ret
= p_td_thr_get_info (&thandle
, &ti
)) != TD_OK
)
1875 warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret
));
1879 retpid
= BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
);
1880 /* If this is a new user thread, notify GDB about it. */
1881 if (!in_thread_list (retpid
))
1883 printf_filtered ("[New %s]\n", target_pid_to_str (retpid
));
1884 add_thread (retpid
);
1888 /* Now detect if this is a thread creation/deletion event: */
1889 check_for_thread_event (ourstatus
, retpid
);
1895 * kill has to call the underlying kill.
1896 * FIXME: I'm not sure if it's necessary to check inferior_pid any more,
1897 * but we might need to fix inferior_pid up if it's a user thread.
1901 kill_thread_callback (th
, data
)
1909 For Linux, threads may need to be waited. */
1910 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1912 warning ("kill_thread_callback: %s", thr_err_string (ret
));
1913 return -1; /* bail out, get_info failed. */
1916 if (ti
.ti_lid
!= main_prochandle
.pid
)
1918 kill (ti
.ti_lid
, SIGKILL
);
1924 static void thread_db_kill (void)
1930 For Linux, threads may need to be waited. */
1931 if (inferior_pid
!= 0)
1933 /* Go kill the children first. Save the main thread for last. */
1934 p_td_ta_thr_iter (main_threadagent
,
1935 kill_thread_callback
,
1938 TD_THR_LOWEST_PRIORITY
,
1940 TD_THR_ANY_USER_FLAGS
);
1942 /* Turn off thread_db event-reporting API *before* killing the
1943 main thread, since this operation requires child memory access.
1944 Can't move this into thread_db_unpush target because then
1945 detach would not work. */
1946 disable_thread_event_reporting (main_threadagent
);
1948 inferior_pid
= main_prochandle
.pid
;
1951 * Since both procfs_kill and ptrace_kill call target_mourn,
1952 * it should be sufficient for me to call one of them.
1953 * That will result in my mourn being called, which will both
1954 * unpush me and call the underlying mourn.
1956 target_beneath
->to_kill ();
1959 /* Wait for all threads. */
1960 /* FIXME: need a universal wait_for_signal func? */
1963 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1965 while (rpid
> 0 || errno
== EINTR
);
1969 rpid
= waitpid (-1, &status
, WNOHANG
);
1971 while (rpid
> 0 || errno
== EINTR
);
1975 * Mourn has to remove us from the target stack,
1976 * and then call the underlying mourn.
1979 static void thread_db_mourn_inferior (void)
1981 thread_db_unpush_target ();
1982 target_mourn_inferior (); /* call the underlying mourn */
1986 * Detach has to remove us from the target stack,
1987 * and then call the underlying detach.
1989 * But first, it has to detach all the cloned threads!
1993 detach_thread_callback (th
, data
)
1997 /* Called once per thread. */
2001 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
2003 warning ("detach_thread_callback: %s", thr_err_string (ret
));
2004 return -1; /* bail out, get_info failed. */
2007 if (!in_thread_list (BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
)))
2008 return 0; /* apparently we don't know this one. */
2010 /* Save main thread for last, or the iterator will fail! */
2011 if (ti
.ti_lid
!= main_prochandle
.pid
)
2013 struct cleanup
*old_chain
;
2016 /* Time to detach this thread.
2017 First disable thread_db event reporting for the thread. */
2018 if (p_td_thr_event_enable
&&
2019 (ret
= p_td_thr_event_enable (th
, off
)) != TD_OK
)
2021 warning ("detach_thread_callback: %s\n", thr_err_string (ret
));
2025 /* Now cancel any pending SIGTRAPS. FIXME! */
2027 /* Call underlying detach method. FIXME just detach it. */
2028 old_chain
= save_inferior_pid ();
2029 inferior_pid
= ti
.ti_lid
;
2030 detach (TARGET_SIGNAL_0
);
2031 do_cleanups (old_chain
);
2037 thread_db_detach (char *args
, int from_tty
)
2041 if ((ret
= p_td_ta_thr_iter (main_threadagent
,
2042 detach_thread_callback
,
2045 TD_THR_LOWEST_PRIORITY
,
2047 TD_THR_ANY_USER_FLAGS
))
2049 warning ("detach (thr_iter): %s", thr_err_string (ret
));
2051 /* Turn off thread_db event-reporting API
2052 (before detaching the main thread) */
2053 disable_thread_event_reporting (main_threadagent
);
2055 thread_db_unpush_target ();
2057 /* above call nullifies target_beneath, so don't use that! */
2058 inferior_pid
= PIDGET (inferior_pid
);
2059 target_detach (args
, from_tty
);
2064 * We never want to actually create the inferior!
2066 * If this is ever called, it means we were on the target stack
2067 * when the user said "run". But we don't want to be on the new
2068 * inferior's target stack until the thread_db / libthread
2069 * connection is ready to be made.
2071 * So, what shall we do?
2072 * Unpush ourselves from the stack, and then invoke
2073 * find_default_create_inferior, which will invoke the
2074 * appropriate process_stratum target to do the create.
2078 thread_db_create_inferior (exec_file
, allargs
, env
)
2083 thread_db_unpush_target ();
2084 find_default_create_inferior (exec_file
, allargs
, env
);
2088 * Thread_db target vector initializer.
2092 init_thread_db_ops ()
2094 thread_db_ops
.to_shortname
= "multi-thread";
2095 thread_db_ops
.to_longname
= "multi-threaded child process.";
2096 thread_db_ops
.to_doc
= "Threads and pthreads support.";
2097 thread_db_ops
.to_files_info
= thread_db_files_info
;
2098 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
2099 thread_db_ops
.to_detach
= thread_db_detach
;
2100 thread_db_ops
.to_wait
= thread_db_wait
;
2101 thread_db_ops
.to_resume
= thread_db_resume
;
2102 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
2103 thread_db_ops
.to_kill
= thread_db_kill
;
2104 thread_db_ops
.to_xfer_memory
= thread_db_xfer_memory
;
2105 thread_db_ops
.to_fetch_registers
= thread_db_fetch_registers
;
2106 thread_db_ops
.to_store_registers
= thread_db_store_registers
;
2107 thread_db_ops
.to_thread_alive
= thread_db_alive
;
2108 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
2109 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
2110 thread_db_ops
.to_stratum
= thread_stratum
;
2111 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
2112 thread_db_ops
.to_magic
= OPS_MAGIC
;
2114 #endif /* HAVE_STDINT_H */
2117 * Module constructor / initializer function.
2118 * If connection to thread_db dynamic library is successful,
2119 * then initialize this module's target vectors and the
2125 _initialize_thread_db ()
2127 #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */
2128 if (init_thread_db_library ())
2130 init_thread_db_ops ();
2131 add_target (&thread_db_ops
);
2133 * Hook up to the new_objfile event.
2134 * If someone is already there, arrange for him to be called
2137 target_new_objfile_chain
= target_new_objfile_hook
;
2138 target_new_objfile_hook
= thread_db_new_objfile
;
2140 #endif /* HAVE_STDINT_H */