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. */
129 #define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
130 #define PIDGET0(PID) (((PID) & 0xffff))
131 #define PIDGET(PID) ((PIDGET0 (PID) == 0xffff) ? -1 : PIDGET0 (PID))
132 #define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
135 /* Macros for superimposing PID and TID into inferior_ptid. */
136 #define THREAD_FLAG 0x80000000
137 #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0)
138 #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0)
139 #define GET_LWP(PID) TIDGET (PID)
140 #define GET_THREAD(PID) TIDGET (PID)
141 #define BUILD_LWP(TID, PID) MERGEPID (PID, TID)
142 #define BUILD_THREAD(TID, PID) (MERGEPID (PID, TID) | THREAD_FLAG)
145 * target_beneath is a pointer to the target_ops underlying this one.
148 static struct target_ops
*target_beneath
;
152 * target vector defined in this module:
155 static struct target_ops thread_db_ops
;
158 * Typedefs required to resolve differences between the thread_db
159 * and proc_service API defined on different versions of Solaris:
162 #if defined(PROC_SERVICE_IS_OLD)
163 typedef const struct ps_prochandle
*gdb_ps_prochandle_t
;
164 typedef char *gdb_ps_read_buf_t
;
165 typedef char *gdb_ps_write_buf_t
;
166 typedef int gdb_ps_size_t
;
168 typedef struct ps_prochandle
*gdb_ps_prochandle_t
;
169 typedef void *gdb_ps_read_buf_t
;
170 typedef const void *gdb_ps_write_buf_t
;
171 typedef size_t gdb_ps_size_t
;
175 * proc_service callback functions, called by thread_db.
179 ps_pstop (gdb_ps_prochandle_t ph
) /* Process stop */
185 ps_pcontinue (gdb_ps_prochandle_t ph
) /* Process continue */
191 ps_lstop (gdb_ps_prochandle_t ph
, /* LWP stop */
198 ps_lcontinue (gdb_ps_prochandle_t ph
, /* LWP continue */
205 ps_lgetxregsize (gdb_ps_prochandle_t ph
, /* Get XREG size */
213 ps_lgetxregs (gdb_ps_prochandle_t ph
, /* Get XREGS */
221 ps_lsetxregs (gdb_ps_prochandle_t ph
, /* Set XREGS */
229 ps_plog (const char *fmt
, ...)
233 va_start (args
, fmt
);
234 vfprintf_filtered (gdb_stderr
, fmt
, args
);
237 /* Look up a symbol in GDB's global symbol table.
238 Return the symbol's address.
239 FIXME: it would be more correct to look up the symbol in the context
240 of the LD_OBJECT_NAME provided. However we're probably fairly safe
241 as long as there aren't name conflicts with other libraries. */
244 ps_pglobal_lookup (gdb_ps_prochandle_t ph
,
245 const char *ld_object_name
, /* the library name */
246 const char *ld_symbol_name
, /* the symbol name */
247 paddr_t
*ld_symbol_addr
) /* return the symbol addr */
249 struct minimal_symbol
*ms
;
251 ms
= lookup_minimal_symbol (ld_symbol_name
, NULL
, NULL
);
256 *ld_symbol_addr
= SYMBOL_VALUE_ADDRESS (ms
);
261 /* Worker function for all memory reads and writes: */
262 static ps_err_e
rw_common (const struct ps_prochandle
*ph
,
268 /* target_xfer_memory direction consts */
269 enum {PS_READ
= 0, PS_WRITE
= 1};
272 ps_pdread (gdb_ps_prochandle_t ph
, /* read from data segment */
274 gdb_ps_read_buf_t buf
,
277 return rw_common (ph
, addr
, buf
, size
, PS_READ
);
281 ps_pdwrite (gdb_ps_prochandle_t ph
, /* write to data segment */
283 gdb_ps_write_buf_t buf
,
286 return rw_common (ph
, addr
, (char *) buf
, size
, PS_WRITE
);
290 ps_ptread (gdb_ps_prochandle_t ph
, /* read from text segment */
292 gdb_ps_read_buf_t buf
,
295 return rw_common (ph
, addr
, buf
, size
, PS_READ
);
299 ps_ptwrite (gdb_ps_prochandle_t ph
, /* write to text segment */
301 gdb_ps_write_buf_t buf
,
304 return rw_common (ph
, addr
, (char *) buf
, size
, PS_WRITE
);
307 static char *thr_err_string (td_err_e
);
308 static char *thr_state_string (td_thr_state_e
);
310 struct ps_prochandle main_prochandle
;
311 td_thragent_t
* main_threadagent
;
314 * Common proc_service routine for reading and writing memory.
317 /* FIXME: once we've munged the inferior_ptid, why can't we
318 simply call target_read/write_memory and return? */
321 rw_common (const struct ps_prochandle
*ph
,
327 struct cleanup
*old_chain
= save_inferior_ptid ();
331 inferior_ptid
= pid_to_ptid (main_prochandle
.pid
);
335 done
= current_target
.to_xfer_memory (addr
, buf
, size
, write_p
,
339 if (write_p
== PS_READ
)
340 print_sys_errmsg ("rw_common (): read", errno
);
342 print_sys_errmsg ("rw_common (): write", errno
);
349 do_cleanups (old_chain
);
353 /* Cleanup functions used by the register callbacks
354 (which have to manipulate the global inferior_ptid). */
357 ps_lgetregs (gdb_ps_prochandle_t ph
, /* Get LWP general regs */
361 struct cleanup
*old_chain
= save_inferior_ptid ();
363 inferior_ptid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
364 current_target
.to_fetch_registers (-1);
366 fill_gregset ((gdb_gregset_t
*) gregset
, -1);
367 do_cleanups (old_chain
);
373 ps_lsetregs (gdb_ps_prochandle_t ph
, /* Set LWP general regs */
375 const prgregset_t gregset
)
377 struct cleanup
*old_chain
= save_inferior_ptid ();
379 inferior_ptid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
380 supply_gregset ((gdb_gregset_t
*) gregset
);
381 current_target
.to_store_registers (-1);
382 do_cleanups (old_chain
);
387 ps_lgetfpregs (gdb_ps_prochandle_t ph
, /* Get LWP float regs */
389 gdb_prfpregset_t
*fpregset
)
391 struct cleanup
*old_chain
= save_inferior_ptid ();
393 inferior_ptid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
394 current_target
.to_fetch_registers (-1);
395 fill_fpregset (fpregset
, -1);
396 do_cleanups (old_chain
);
401 ps_lsetfpregs (gdb_ps_prochandle_t ph
, /* Set LWP float regs */
403 const gdb_prfpregset_t
*fpregset
)
405 struct cleanup
*old_chain
= save_inferior_ptid ();
407 inferior_ptid
= BUILD_LWP (lwpid
, main_prochandle
.pid
);
408 supply_fpregset (fpregset
);
409 current_target
.to_store_registers (-1);
410 do_cleanups (old_chain
);
417 * return the main pid for the child process
418 * (special for Linux -- not used on Solaris)
422 ps_getpid (gdb_ps_prochandle_t ph
)
429 /* Reads the local descriptor table of a LWP. */
432 ps_lgetLDT (gdb_ps_prochandle_t ph
, lwpid_t lwpid
,
435 /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
436 extern struct ssd
*procfs_find_LDT_entry (int);
439 ret
= procfs_find_LDT_entry (BUILD_LWP (lwpid
,
440 PIDGET (main_prochandle
.pid
)));
443 memcpy (pldt
, ret
, sizeof (struct ssd
));
446 else /* LDT not found. */
449 #endif /* TM_I386SOL2_H */
452 * Pointers to thread_db functions:
454 * These are a dynamic library mechanism.
455 * The dlfcn.h interface will be used to initialize these
456 * so that they point to the appropriate functions in the
457 * thread_db dynamic library. This is done dynamically
458 * so that GDB can still run on systems that lack thread_db.
461 static td_err_e (*p_td_init
) (void);
463 static td_err_e (*p_td_ta_new
) (const struct ps_prochandle
*ph_p
,
464 td_thragent_t
**ta_pp
);
466 static td_err_e (*p_td_ta_delete
) (td_thragent_t
*ta_p
);
468 static td_err_e (*p_td_ta_get_nthreads
) (const td_thragent_t
*ta_p
,
472 static td_err_e (*p_td_ta_thr_iter
) (const td_thragent_t
*ta_p
,
475 td_thr_state_e state
,
477 sigset_t
*ti_sigmask_p
,
478 unsigned ti_user_flags
);
480 static td_err_e (*p_td_ta_event_addr
) (const td_thragent_t
*ta_p
,
482 td_notify_t
*notify_p
);
484 static td_err_e (*p_td_ta_event_getmsg
) (const td_thragent_t
*ta_p
,
485 td_event_msg_t
*msg
);
487 static td_err_e (*p_td_ta_set_event
) (const td_thragent_t
*ta_p
,
488 td_thr_events_t
*events
);
490 static td_err_e (*p_td_thr_validate
) (const td_thrhandle_t
*th_p
);
492 static td_err_e (*p_td_thr_event_enable
) (const td_thrhandle_t
*th_p
,
495 static td_err_e (*p_td_thr_get_info
) (const td_thrhandle_t
*th_p
,
498 static td_err_e (*p_td_thr_getgregs
) (const td_thrhandle_t
*th_p
,
501 static td_err_e (*p_td_thr_setgregs
) (const td_thrhandle_t
*th_p
,
502 const prgregset_t regset
);
504 static td_err_e (*p_td_thr_getfpregs
) (const td_thrhandle_t
*th_p
,
505 gdb_prfpregset_t
*fpregset
);
507 static td_err_e (*p_td_thr_setfpregs
) (const td_thrhandle_t
*th_p
,
508 const gdb_prfpregset_t
*fpregset
);
510 static td_err_e (*p_td_ta_map_id2thr
) (const td_thragent_t
*ta_p
,
512 td_thrhandle_t
*th_p
);
514 static td_err_e (*p_td_ta_map_lwp2thr
) (const td_thragent_t
*ta_p
,
516 td_thrhandle_t
*th_p
);
519 * API and target vector initialization function: thread_db_initialize.
521 * NOTE: this function is deliberately NOT named with the GDB convention
522 * of module initializer function names that begin with "_initialize".
523 * This module is NOT intended to be auto-initialized at GDB startup.
524 * Rather, it will only be initialized when a multi-threaded child
525 * process is detected.
530 * Initializer for thread_db library interface.
531 * This function does the dynamic library stuff (dlopen, dlsym),
532 * and then calls the thread_db library's one-time initializer
533 * function (td_init). If everything succeeds, this function
534 * returns true; otherwise it returns false, and this module
539 init_thread_db_library (void)
544 /* Open a handle to the "thread_db" dynamic library. */
545 if ((dlhandle
= dlopen ("libthread_db.so.1", RTLD_NOW
)) == NULL
)
548 /* Initialize pointers to the dynamic library functions we will use.
549 * Note that we are not calling the functions here -- we are only
550 * establishing pointers to them.
553 /* td_init: initialize thread_db library. */
554 if ((p_td_init
= dlsym (dlhandle
, "td_init")) == NULL
)
556 /* td_ta_new: register a target process with thread_db. */
557 if ((p_td_ta_new
= dlsym (dlhandle
, "td_ta_new")) == NULL
)
559 /* td_ta_delete: un-register a target process with thread_db. */
560 if ((p_td_ta_delete
= dlsym (dlhandle
, "td_ta_delete")) == NULL
)
563 /* td_ta_map_id2thr: get thread handle from thread id. */
564 if ((p_td_ta_map_id2thr
= dlsym (dlhandle
, "td_ta_map_id2thr")) == NULL
)
566 /* td_ta_map_lwp2thr: get thread handle from lwp id. */
567 if ((p_td_ta_map_lwp2thr
= dlsym (dlhandle
, "td_ta_map_lwp2thr")) == NULL
)
569 /* td_ta_get_nthreads: get number of threads in target process. */
570 if ((p_td_ta_get_nthreads
= dlsym (dlhandle
, "td_ta_get_nthreads")) == NULL
)
572 /* td_ta_thr_iter: iterate over all thread handles. */
573 if ((p_td_ta_thr_iter
= dlsym (dlhandle
, "td_ta_thr_iter")) == NULL
)
576 /* td_thr_validate: make sure a thread handle is real and alive. */
577 if ((p_td_thr_validate
= dlsym (dlhandle
, "td_thr_validate")) == NULL
)
579 /* td_thr_get_info: get a bunch of info about a thread. */
580 if ((p_td_thr_get_info
= dlsym (dlhandle
, "td_thr_get_info")) == NULL
)
582 /* td_thr_getgregs: get general registers for thread. */
583 if ((p_td_thr_getgregs
= dlsym (dlhandle
, "td_thr_getgregs")) == NULL
)
585 /* td_thr_setgregs: set general registers for thread. */
586 if ((p_td_thr_setgregs
= dlsym (dlhandle
, "td_thr_setgregs")) == NULL
)
588 /* td_thr_getfpregs: get floating point registers for thread. */
589 if ((p_td_thr_getfpregs
= dlsym (dlhandle
, "td_thr_getfpregs")) == NULL
)
591 /* td_thr_setfpregs: set floating point registers for thread. */
592 if ((p_td_thr_setfpregs
= dlsym (dlhandle
, "td_thr_setfpregs")) == NULL
)
598 warning ("init_thread_db: td_init: %s", thr_err_string (ret
));
602 /* Optional functions:
603 We can still debug even if the following functions are not found. */
605 /* td_ta_event_addr: get the breakpoint address for specified event. */
606 p_td_ta_event_addr
= dlsym (dlhandle
, "td_ta_event_addr");
608 /* td_ta_event_getmsg: get the next event message for the process. */
609 p_td_ta_event_getmsg
= dlsym (dlhandle
, "td_ta_event_getmsg");
611 /* td_ta_set_event: request notification of an event. */
612 p_td_ta_set_event
= dlsym (dlhandle
, "td_ta_set_event");
614 /* td_thr_event_enable: enable event reporting in a thread. */
615 p_td_thr_event_enable
= dlsym (dlhandle
, "td_thr_event_enable");
617 return 1; /* success */
621 * Local utility functions:
628 thr_err_string - Convert a thread_db error code to a string
632 char * thr_err_string (errcode)
636 Return a string description of the thread_db errcode. If errcode
637 is unknown, then return an <unknown> message.
642 thr_err_string (td_err_e errcode
)
647 case TD_OK
: return "generic 'call succeeded'";
648 case TD_ERR
: return "generic error";
649 case TD_NOTHR
: return "no thread to satisfy query";
650 case TD_NOSV
: return "no sync handle to satisfy query";
651 case TD_NOLWP
: return "no lwp to satisfy query";
652 case TD_BADPH
: return "invalid process handle";
653 case TD_BADTH
: return "invalid thread handle";
654 case TD_BADSH
: return "invalid synchronization handle";
655 case TD_BADTA
: return "invalid thread agent";
656 case TD_BADKEY
: return "invalid key";
657 case TD_NOMSG
: return "no event message for getmsg";
658 case TD_NOFPREGS
: return "FPU register set not available";
659 case TD_NOLIBTHREAD
: return "application not linked with libthread";
660 case TD_NOEVENT
: return "requested event is not supported";
661 case TD_NOCAPAB
: return "capability not available";
662 case TD_DBERR
: return "debugger service failed";
663 case TD_NOAPLIC
: return "operation not applicable to";
664 case TD_NOTSD
: return "no thread-specific data for this thread";
665 case TD_MALLOC
: return "malloc failed";
666 case TD_PARTIALREG
: return "only part of register set was written/read";
667 case TD_NOXREGS
: return "X register set not available for this thread";
669 sprintf (buf
, "unknown thread_db error '%d'", errcode
);
678 thr_state_string - Convert a thread_db state code to a string
682 char *thr_state_string (statecode)
686 Return the thread_db state string associated with statecode.
687 If statecode is unknown, then return an <unknown> message.
692 thr_state_string (td_thr_state_e statecode
)
697 case TD_THR_STOPPED
: return "stopped by debugger";
698 case TD_THR_RUN
: return "runnable";
699 case TD_THR_ACTIVE
: return "active";
700 case TD_THR_ZOMBIE
: return "zombie";
701 case TD_THR_SLEEP
: return "sleeping";
702 case TD_THR_STOPPED_ASLEEP
: return "stopped by debugger AND blocked";
704 sprintf (buf
, "unknown thread_db state %d", statecode
);
710 * Local thread/event list.
711 * This data structure will be used to hold a list of threads and
712 * pending/deliverable events.
715 typedef struct THREADINFO
{
716 thread_t tid
; /* thread ID */
717 pid_t lid
; /* process/lwp ID */
718 td_thr_state_e state
; /* thread state (a la thread_db) */
719 td_thr_type_e type
; /* thread type (a la thread_db) */
720 int pending
; /* true if holding a pending event */
721 int status
; /* wait status of any interesting event */
724 threadinfo
* threadlist
;
725 int threadlist_max
= 0; /* current size of table */
726 int threadlist_top
= 0; /* number of threads now in table */
727 #define THREADLIST_ALLOC 100 /* chunk size by which to expand table */
730 insert_thread (int tid
, int lid
, td_thr_state_e state
, td_thr_type_e type
)
732 if (threadlist_top
>= threadlist_max
)
734 threadlist_max
+= THREADLIST_ALLOC
;
735 threadlist
= xrealloc (threadlist
,
736 threadlist_max
* sizeof (threadinfo
));
737 if (threadlist
== NULL
)
740 threadlist
[threadlist_top
].tid
= tid
;
741 threadlist
[threadlist_top
].lid
= lid
;
742 threadlist
[threadlist_top
].state
= state
;
743 threadlist
[threadlist_top
].type
= type
;
744 threadlist
[threadlist_top
].pending
= 0;
745 threadlist
[threadlist_top
].status
= 0;
747 return &threadlist
[threadlist_top
++];
751 empty_threadlist (void)
757 next_pending_event (void)
761 for (i
= 0; i
< threadlist_top
; i
++)
762 if (threadlist
[i
].pending
)
763 return &threadlist
[i
];
769 threadlist_iter (int (*func
) (), void *data
, td_thr_state_e state
,
774 for (i
= 0; i
< threadlist_top
; i
++)
775 if ((state
== TD_THR_ANY_STATE
|| state
== threadlist
[i
].state
) &&
776 (type
== TD_THR_ANY_TYPE
|| type
== threadlist
[i
].type
))
777 if ((*func
) (&threadlist
[i
], data
) != 0)
786 * Here we keep state information all collected in one place.
789 /* This flag is set when we activate, so that we don't do it twice.
790 Defined in linux-thread.c and used for inter-target syncronization. */
791 extern int using_thread_db
;
793 /* The process id for which we've stopped.
794 * This is only set when we actually stop all threads.
795 * Otherwise it's zero.
797 static int event_pid
;
800 * The process id for a new thread to which we've just attached.
801 * This process needs special handling at resume time.
803 static int attach_pid
;
807 * thread_db event handling:
809 * The mechanism for event notification via the thread_db API.
810 * These events are implemented as breakpoints. The thread_db
811 * library gives us an address where we can set a breakpoint.
812 * When the breakpoint is hit, it represents an event of interest
819 /* Location of the thread creation event breakpoint. The code at this
820 location in the child process will be called by the pthread library
821 whenever a new thread is created. By setting a special breakpoint
822 at this location, GDB can detect when a new thread is created. We
823 obtain this location via the td_ta_event_addr call. */
825 static CORE_ADDR thread_creation_bkpt_address
;
827 /* Location of the thread death event breakpoint. The code at this
828 location in the child process will be called by the pthread library
829 whenever a thread is destroyed. By setting a special breakpoint at
830 this location, GDB can detect when a new thread is created. We
831 obtain this location via the td_ta_event_addr call. */
833 static CORE_ADDR thread_death_bkpt_address
;
835 /* This function handles the global parts of enabling thread events.
836 The thread-specific enabling is handled per-thread elsewhere. */
839 enable_thread_event_reporting (td_thragent_t
*ta
)
841 td_thr_events_t events
;
845 if (p_td_ta_set_event
== NULL
||
846 p_td_ta_event_addr
== NULL
||
847 p_td_ta_event_getmsg
== NULL
||
848 p_td_thr_event_enable
== NULL
)
849 return; /* can't do thread event reporting without these funcs */
851 /* set process wide mask saying which events we are interested in */
852 td_event_emptyset (&events
);
853 td_event_addset (&events
, TD_CREATE
);
854 td_event_addset (&events
, TD_DEATH
);
856 if (p_td_ta_set_event (ta
, &events
) != TD_OK
)
858 warning ("unable to set global thread event mask");
862 /* Delete previous thread event breakpoints, if any. */
863 remove_thread_event_breakpoints ();
865 /* create breakpoints -- thread creation and death */
866 /* thread creation */
867 /* get breakpoint location */
868 if (p_td_ta_event_addr (ta
, TD_CREATE
, ¬ify
) != TD_OK
)
870 warning ("unable to get location for thread creation breakpoint");
874 /* Set up the breakpoint. */
875 create_thread_event_breakpoint ((CORE_ADDR
) notify
.u
.bptaddr
);
877 /* Save it's location. */
878 thread_creation_bkpt_address
= (CORE_ADDR
) notify
.u
.bptaddr
;
881 /* get breakpoint location */
882 if (p_td_ta_event_addr (ta
, TD_DEATH
, ¬ify
) != TD_OK
)
884 warning ("unable to get location for thread death breakpoint");
887 /* Set up the breakpoint. */
888 create_thread_event_breakpoint ((CORE_ADDR
) notify
.u
.bptaddr
);
890 /* Save it's location. */
891 thread_death_bkpt_address
= (CORE_ADDR
) notify
.u
.bptaddr
;
894 /* This function handles the global parts of disabling thread events.
895 The thread-specific enabling is handled per-thread elsewhere. */
898 disable_thread_event_reporting (td_thragent_t
*ta
)
900 td_thr_events_t events
;
902 /* set process wide mask saying we aren't interested in any events */
903 td_event_emptyset (&events
);
904 p_td_ta_set_event (main_threadagent
, &events
);
906 /* Delete thread event breakpoints, if any. */
907 remove_thread_event_breakpoints ();
908 thread_creation_bkpt_address
= 0;
909 thread_death_bkpt_address
= 0;
912 /* check_for_thread_event
914 if it's a thread event we recognize (currently
915 we only recognize creation and destruction
916 events), return 1; else return 0. */
920 check_for_thread_event (struct target_waitstatus
*tws
, int event_pid
)
922 /* FIXME: to be more efficient, we should keep a static
923 list of threads, and update it only here (with td_ta_thr_iter). */
928 thread_db_push_target (void)
930 /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */
932 /* Push this target vector */
933 push_target (&thread_db_ops
);
934 /* Find the underlying process-layer target for calling later. */
935 target_beneath
= find_target_beneath (&thread_db_ops
);
937 /* Turn on thread_db event-reporting API. */
938 enable_thread_event_reporting (main_threadagent
);
942 thread_db_unpush_target (void)
944 /* Must be called whenever we remove ourself from the target stack! */
947 target_beneath
= NULL
;
949 /* delete local list of threads */
951 /* Turn off the thread_db API. */
952 p_td_ta_delete (main_threadagent
);
953 /* Unpush this target vector */
954 unpush_target (&thread_db_ops
);
955 /* Reset linuxthreads module. */
956 linuxthreads_discard_global_state ();
960 * New objfile hook function:
961 * Called for each new objfile (image, shared lib) in the target process.
963 * The purpose of this function is to detect that the target process
964 * is linked with the (appropriate) thread library. So every time a
965 * new target shared library is detected, we will call td_ta_new.
966 * If it succeeds, we know we have a multi-threaded target process
967 * that we can debug using the thread_db API.
971 * new_objfile function:
973 * connected to target_new_objfile_hook, this function gets called
974 * every time a new binary image is loaded.
976 * At each call, we attempt to open the thread_db connection to the
977 * child process. If it succeeds, we know we have a libthread process
978 * and we can debug it with this target vector. Therefore we push
979 * ourself onto the target stack.
982 static void (*target_new_objfile_chain
) (struct objfile
*objfile
);
983 static int stop_or_attach_thread_callback (const td_thrhandle_t
*th
,
985 static int wait_thread_callback (const td_thrhandle_t
*th
,
989 thread_db_new_objfile (struct objfile
*objfile
)
993 if (using_thread_db
) /* libthread already detected, and */
994 goto quit
; /* thread target vector activated. */
997 goto quit
; /* un-interesting object file */
999 /* Initialize our "main prochandle" with the main inferior pid. */
1000 main_prochandle
.pid
= PIDGET (inferior_ptid
);
1002 /* Now attempt to open a thread_db connection to the
1003 thread library running in the child process. */
1004 ret
= p_td_ta_new (&main_prochandle
, &main_threadagent
);
1007 warning ("Unexpected error initializing thread_db: %s",
1008 thr_err_string (ret
));
1010 case TD_NOLIBTHREAD
: /* expected: no libthread in child process (yet) */
1012 case TD_OK
: /* libthread detected in child: we go live now! */
1013 thread_db_push_target ();
1014 event_pid
= PIDGET (inferior_ptid
); /* for resume */
1016 /* Now stop everyone else, and attach any new threads you find. */
1017 p_td_ta_thr_iter (main_threadagent
,
1018 stop_or_attach_thread_callback
,
1021 TD_THR_LOWEST_PRIORITY
,
1023 TD_THR_ANY_USER_FLAGS
);
1025 /* Now go call wait on all the threads you've stopped:
1026 This allows us to absorb the SIGKILL event, and to make sure
1027 that the thread knows that it is stopped (Linux peculiarity). */
1028 p_td_ta_thr_iter (main_threadagent
,
1029 wait_thread_callback
,
1032 TD_THR_LOWEST_PRIORITY
,
1034 TD_THR_ANY_USER_FLAGS
);
1039 if (target_new_objfile_chain
)
1040 target_new_objfile_chain (objfile
);
1048 thread_db_alive - test thread for "aliveness"
1052 static bool thread_db_alive (int pid);
1056 returns true if thread still active in inferior.
1061 thread_db_alive (ptid_t ptid
)
1063 if (is_thread (ptid
)) /* user-space (non-kernel) thread */
1067 int pid
= GET_THREAD (ptid
);
1069 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, pid
, &th
)) != TD_OK
)
1070 return 0; /* thread not found */
1071 if ((ret
= p_td_thr_validate (&th
)) != TD_OK
)
1072 return 0; /* thread not valid */
1073 return 1; /* known thread: return true */
1075 else if (target_beneath
->to_thread_alive
)
1076 return target_beneath
->to_thread_alive (ptid
);
1078 return 0; /* default to "not alive" (shouldn't happen anyway) */
1082 * get_lwp_from_thread_handle
1085 static int /* lwpid_t or pid_t */
1086 get_lwp_from_thread_handle (td_thrhandle_t
*th
)
1091 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1092 error ("get_lwp_from_thread_handle: thr_get_info failed: %s",
1093 thr_err_string (ret
));
1099 * get_lwp_from_thread_id
1102 static int /* lwpid_t or pid_t */
1103 get_lwp_from_thread_id (int tid
/* thread_t? */)
1108 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, tid
, &th
)) != TD_OK
)
1109 error ("get_lwp_from_thread_id: map_id2thr failed: %s",
1110 thr_err_string (ret
));
1112 return get_lwp_from_thread_handle (&th
);
1116 * pid_to_str has to handle user-space threads.
1117 * If not a user-space thread, then pass the request on to the
1118 * underlying stratum if it can handle it: else call normal_pid_to_str.
1122 thread_db_pid_to_str (ptid_t ptid
)
1124 static char buf
[100];
1129 if (is_thread (ptid
))
1131 if ((ret
= p_td_ta_map_id2thr (main_threadagent
,
1134 error ("thread_db: map_id2thr failed: %s", thr_err_string (ret
));
1136 if ((ret
= p_td_thr_get_info (&th
, &ti
)) != TD_OK
)
1137 error ("thread_db: thr_get_info failed: %s", thr_err_string (ret
));
1139 if (ti
.ti_state
== TD_THR_ACTIVE
&&
1141 sprintf (buf
, "Thread %ld (LWP %d)", ti
.ti_tid
, ti
.ti_lid
);
1143 sprintf (buf
, "Thread %ld (%s)", ti
.ti_tid
,
1144 thr_state_string (ti
.ti_state
));
1146 else if (GET_LWP (ptid
))
1147 sprintf (buf
, "LWP %ld", GET_LWP (ptid
));
1148 else return normal_pid_to_str (ptid
);
1154 * thread_db target vector functions:
1158 thread_db_files_info (struct target_ops
*tgt_vector
)
1160 /* This function will be unnecessary in real life. */
1161 printf_filtered ("thread_db stratum:\n");
1162 target_beneath
->to_files_info (tgt_vector
);
1166 * xfer_memory has to munge the inferior_ptid before passing the call
1167 * down to the target layer.
1171 thread_db_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
1172 struct mem_attrib
*attrib
,
1173 struct target_ops
*target
)
1175 struct cleanup
*old_chain
;
1178 old_chain
= save_inferior_ptid ();
1180 if (is_thread (inferior_ptid
) ||
1181 !target_thread_alive (inferior_ptid
))
1183 /* FIXME: use the LID/LWP, so that underlying process layer
1184 can read memory from specific threads? */
1185 inferior_ptid
= pid_to_ptid (main_prochandle
.pid
);
1188 ret
= target_beneath
->to_xfer_memory (memaddr
, myaddr
, len
,
1189 dowrite
, attrib
, target
);
1190 do_cleanups (old_chain
);
1195 * fetch_registers has to determine if inferior_ptid is a user-space thread.
1196 * If so, we use the thread_db API to get the registers.
1197 * And if not, we call the underlying process stratum.
1201 thread_db_fetch_registers (int regno
)
1203 td_thrhandle_t thandle
;
1204 gdb_prfpregset_t fpregset
;
1205 prgregset_t gregset
;
1209 if (!is_thread (inferior_ptid
)) /* kernel thread */
1210 { /* pass the request on to the target underneath. */
1211 target_beneath
->to_fetch_registers (regno
);
1215 /* convert inferior_ptid into a td_thrhandle_t */
1217 if ((thread
= GET_THREAD (inferior_ptid
)) == 0)
1218 error ("fetch_registers: thread == 0");
1220 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, thread
, &thandle
)) != TD_OK
)
1221 error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret
));
1223 /* Get the integer regs:
1224 For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7,
1225 pc and sp are saved (by a thread context switch). */
1226 if ((ret
= p_td_thr_getgregs (&thandle
, gregset
)) != TD_OK
&&
1227 ret
!= TD_PARTIALREG
)
1228 error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret
));
1230 /* And, now the fp regs */
1231 if ((ret
= p_td_thr_getfpregs (&thandle
, &fpregset
)) != TD_OK
&&
1233 error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret
));
1235 /* Note that we must call supply_{g fp}regset *after* calling the td routines
1236 because the td routines call ps_lget* which affect the values stored in the
1239 supply_gregset ((gdb_gregset_t
*) gregset
);
1240 supply_fpregset (&fpregset
);
1245 * store_registers has to determine if inferior_ptid is a user-space thread.
1246 * If so, we use the thread_db API to get the registers.
1247 * And if not, we call the underlying process stratum.
1251 thread_db_store_registers (int regno
)
1253 td_thrhandle_t thandle
;
1254 gdb_prfpregset_t fpregset
;
1255 prgregset_t gregset
;
1259 if (!is_thread (inferior_ptid
)) /* Kernel thread: */
1260 { /* pass the request on to the underlying target vector. */
1261 target_beneath
->to_store_registers (regno
);
1265 /* convert inferior_ptid into a td_thrhandle_t */
1267 if ((thread
= GET_THREAD (inferior_ptid
)) == 0)
1268 error ("store_registers: thread == 0");
1270 if ((ret
= p_td_ta_map_id2thr (main_threadagent
, thread
, &thandle
)) != TD_OK
)
1271 error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret
));
1274 { /* Not writing all the regs */
1275 /* save new register value */
1276 /* MVS: I don't understand this... */
1277 char old_value
[REGISTER_SIZE
];
1279 memcpy (old_value
, ®isters
[REGISTER_BYTE (regno
)], REGISTER_SIZE
);
1281 if ((ret
= p_td_thr_getgregs (&thandle
, gregset
)) != TD_OK
)
1282 error ("store_registers: td_thr_getgregs %s", thr_err_string (ret
));
1283 if ((ret
= p_td_thr_getfpregs (&thandle
, &fpregset
)) != TD_OK
)
1284 error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret
));
1286 /* restore new register value */
1287 memcpy (®isters
[REGISTER_BYTE (regno
)], old_value
, REGISTER_SIZE
);
1291 fill_gregset ((gdb_gregset_t
*) gregset
, regno
);
1292 fill_fpregset (&fpregset
, regno
);
1294 if ((ret
= p_td_thr_setgregs (&thandle
, gregset
)) != TD_OK
)
1295 error ("store_registers: td_thr_setgregs %s", thr_err_string (ret
));
1296 if ((ret
= p_td_thr_setfpregs (&thandle
, &fpregset
)) != TD_OK
&&
1298 error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret
));
1302 handle_new_thread (int tid
, /* user thread id */
1303 int lid
, /* kernel thread id */
1306 ptid_t gdb_ptid
= BUILD_THREAD (tid
, main_prochandle
.pid
);
1307 int wait_pid
, wait_status
;
1310 printf_filtered ("[New %s]\n", target_pid_to_str (gdb_ptid
));
1311 add_thread (gdb_ptid
);
1313 if (lid
!= main_prochandle
.pid
)
1315 attach_thread (lid
);
1316 /* According to the Eric Paire model, we now have to send
1317 the restart signal to the new thread -- however, empirically,
1318 I do not find that to be necessary. */
1324 test_for_new_thread (int tid
, int lid
, int verbose
)
1326 if (!in_thread_list (BUILD_THREAD (tid
, main_prochandle
.pid
)))
1327 handle_new_thread (tid
, lid
, verbose
);
1331 * Callback function that gets called once per USER thread
1332 * (i.e., not kernel) thread by td_ta_thr_iter.
1336 find_new_threads_callback (const td_thrhandle_t
*th
, void *ignored
)
1341 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1343 warning ("find_new_threads_callback: %s", thr_err_string (ret
));
1344 return -1; /* bail out, get_info failed. */
1348 As things now stand, this should never detect a new thread.
1349 But if it does, we could be in trouble because we aren't calling
1350 wait_thread_callback for it. */
1351 test_for_new_thread (ti
.ti_tid
, ti
.ti_lid
, 0);
1356 * find_new_threads uses the thread_db iterator function to discover
1357 * user-space threads. Then if the underlying process stratum has a
1358 * find_new_threads method, we call that too.
1362 thread_db_find_new_threads (void)
1364 if (PIDGET (inferior_ptid
) == -1) /* FIXME: still necessary? */
1366 printf_filtered ("No process.\n");
1369 p_td_ta_thr_iter (main_threadagent
,
1370 find_new_threads_callback
,
1373 TD_THR_LOWEST_PRIORITY
,
1375 TD_THR_ANY_USER_FLAGS
);
1376 if (target_beneath
->to_find_new_threads
)
1377 target_beneath
->to_find_new_threads ();
1381 * Resume all threads, or resume a single thread.
1382 * If step is true, then single-step the appropriate thread
1383 * (or single-step inferior_ptid, but continue everyone else).
1384 * If signo is true, then send that signal to at least one thread.
1388 * This function is called once for each thread before resuming.
1389 * It sends continue (no step, and no signal) to each thread except
1390 * the main thread, and
1391 * the event thread (the one that stopped at a breakpoint etc.)
1393 * The event thread is handled separately so that it can be sent
1394 * the stepping and signal args with which target_resume was called.
1396 * The main thread is resumed last, so that the thread_db proc_service
1397 * callbacks will still work during the iterator function.
1401 resume_thread_callback (const td_thrhandle_t
*th
, void *data
)
1406 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1408 warning ("resume_thread_callback: %s", thr_err_string (ret
));
1409 return -1; /* bail out, get_info failed. */
1412 As things now stand, this should never detect a new thread.
1413 But if it does, we could be in trouble because we aren't calling
1414 wait_thread_callback for it. */
1415 test_for_new_thread (ti
.ti_tid
, ti
.ti_lid
, 1);
1417 if (ti
.ti_lid
!= main_prochandle
.pid
&&
1418 ti
.ti_lid
!= event_pid
)
1420 /* Unconditionally continue the thread with no signal.
1421 Only the event thread will get a signal of any kind. */
1423 target_beneath
->to_resume (pid_to_ptid (ti
.ti_lid
), 0, 0);
1429 new_resume_thread_callback (threadinfo
*thread
, void *data
)
1431 if (thread
->lid
!= event_pid
&&
1432 thread
->lid
!= main_prochandle
.pid
)
1434 /* Unconditionally continue the thread with no signal (for now). */
1436 target_beneath
->to_resume (pid_to_ptid (thread
->lid
), 0, 0);
1441 static int last_resume_pid
;
1442 static int last_resume_step
;
1443 static int last_resume_signo
;
1446 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
1448 last_resume_pid
= PIDGET (ptid
);
1449 last_resume_step
= step
;
1450 last_resume_signo
= signo
;
1452 /* resuming a specific pid? */
1453 if (PIDGET (ptid
) != -1)
1455 if (is_thread (ptid
))
1456 ptid
= pid_to_ptid (get_lwp_from_thread_id (GET_THREAD (ptid
)));
1457 else if (GET_LWP (ptid
))
1458 ptid
= pid_to_ptid (GET_LWP (ptid
));
1461 /* Apparently the interpretation of 'pid' is dependent on 'step':
1462 If step is true, then a specific pid means 'step only this pid'.
1463 But if step is not true, then pid means 'continue ALL pids, but
1464 give the signal only to this one'. */
1465 if (PIDGET (ptid
) != -1 && step
)
1467 /* FIXME: is this gonna work in all circumstances? */
1468 target_beneath
->to_resume (ptid
, step
, signo
);
1472 /* 1) Continue all threads except the event thread and the main thread.
1473 2) resume the event thread with step and signo.
1474 3) If event thread != main thread, continue the main thread.
1476 Note: order of 2 and 3 may need to be reversed. */
1478 threadlist_iter (new_resume_thread_callback
,
1482 /* now resume event thread, and if necessary also main thread. */
1485 target_beneath
->to_resume (pid_to_ptid (event_pid
), step
, signo
);
1487 if (event_pid
!= main_prochandle
.pid
)
1489 target_beneath
->to_resume (pid_to_ptid (main_prochandle
.pid
), 0, 0);
1494 /* All new threads will be attached.
1495 All previously known threads will be stopped using kill (SIGKILL). */
1498 stop_or_attach_thread_callback (const td_thrhandle_t
*th
, void *data
)
1505 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1507 warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret
));
1508 return -1; /* bail out, get_info failed. */
1511 /* First add it to our internal list.
1512 We build this list anew at every wait event. */
1513 insert_thread (ti
.ti_tid
, ti
.ti_lid
, ti
.ti_state
, ti
.ti_type
);
1514 /* Now: if we've already seen it, stop it, else add it and attach it. */
1515 gdb_ptid
= BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
);
1516 if (!in_thread_list (gdb_ptid
)) /* new thread */
1518 handle_new_thread (ti
.ti_tid
, ti
.ti_lid
, 1);
1519 /* Enable thread events */
1520 if (p_td_thr_event_enable
)
1521 if ((ret
= p_td_thr_event_enable (th
, on_off
)) != TD_OK
)
1522 warning ("stop_or_attach_thread: %s", thr_err_string (ret
));
1524 else if (ti
.ti_lid
!= event_pid
&&
1525 ti
.ti_lid
!= main_prochandle
.pid
)
1527 ret
= (td_err_e
) kill (ti
.ti_lid
, SIGSTOP
);
1534 * Wait for signal N from pid PID.
1535 * If wait returns any other signals, put them back before returning.
1539 wait_for_stop (int pid
)
1545 /* Array of wait/signal status */
1546 /* FIXME: wrong data structure, we need a queue.
1547 Realtime signals may be delivered more than once.
1548 And at that, we really can't handle them (see below). */
1550 static int wstatus
[NSIG
];
1551 #elif defined (_NSIG)
1552 static int wstatus
[_NSIG
];
1554 #error No definition for number of signals!
1557 /* clear wait/status list */
1558 memset (&wstatus
, 0, sizeof (wstatus
));
1560 /* Now look for SIGSTOP event on all threads except event thread. */
1563 if (pid
== main_prochandle
.pid
)
1564 retpid
= waitpid (pid
, &status
, 0);
1566 retpid
= waitpid (pid
, &status
, __WCLONE
);
1569 if (WSTOPSIG (status
) == SIGSTOP
)
1571 /* Got the SIGSTOP event we're looking for.
1572 Throw it away, and throw any other events back! */
1573 for (i
= 0; i
< sizeof(wstatus
) / sizeof (wstatus
[0]); i
++)
1579 break; /* all done */
1584 /* Oops, got an event other than SIGSTOP.
1585 Save it, and throw it back after we find the SIGSTOP event. */
1587 /* FIXME (how?) This method is going to fail for realtime
1588 signals, which cannot be put back simply by using kill. */
1590 if (WIFEXITED (status
))
1591 error ("Ack! Thread Exited event. What do I do now???");
1592 else if (WIFSTOPPED (status
))
1593 signo
= WSTOPSIG (status
);
1595 signo
= WTERMSIG (status
);
1597 /* If a thread other than the event thread has hit a GDB
1598 breakpoint (as opposed to some random trap signal), then
1599 just arrange for it to hit it again later. Back up the
1600 PC if necessary. Don't forward the SIGTRAP signal to
1601 the thread. We will handle the current event, eventually
1602 we will resume all the threads, and this one will get
1603 it's breakpoint trap again.
1605 If we do not do this, then we run the risk that the user
1606 will delete or disable the breakpoint, but the thread will
1607 have already tripped on it. */
1609 if (retpid
!= event_pid
&&
1611 breakpoint_inserted_here_p (read_pc_pid (pid_to_ptid (retpid
)) -
1612 DECR_PC_AFTER_BREAK
))
1614 /* Set the pc to before the trap and DO NOT re-send the signal */
1615 if (DECR_PC_AFTER_BREAK
)
1616 write_pc_pid (read_pc_pid (pid_to_ptid (retpid
))
1617 - DECR_PC_AFTER_BREAK
,
1618 pid_to_ptid (retpid
));
1621 /* Since SIGINT gets forwarded to the entire process group
1622 (in the case where ^C is typed at the tty / console),
1623 just ignore all SIGINTs from other than the event thread. */
1624 else if (retpid
!= event_pid
&& signo
== SIGINT
)
1625 { /* do nothing. Signal will disappear into oblivion! */
1629 else /* This is some random signal other than a breakpoint. */
1631 wstatus
[signo
] = 1;
1633 child_resume (pid_to_ptid (retpid
), 0, TARGET_SIGNAL_0
);
1637 } while (errno
== 0 || errno
== EINTR
);
1641 * wait_thread_callback
1643 * Calls waitpid for each thread, repeatedly if necessary, until
1644 * SIGSTOP is returned. Afterward, if any other signals were returned
1645 * by waitpid, return them to the thread's pending queue by calling kill.
1649 wait_thread_callback (const td_thrhandle_t
*th
, void *data
)
1654 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1656 warning ("wait_thread_callback: %s", thr_err_string (ret
));
1657 return -1; /* bail out, get_info failed. */
1660 /* This callback to act on all threads except the event thread: */
1661 if (ti
.ti_lid
== event_pid
|| /* no need to wait (no sigstop) */
1662 ti
.ti_lid
== main_prochandle
.pid
) /* no need to wait (already waited) */
1663 return 0; /* don't wait on the event thread. */
1665 wait_for_stop (ti
.ti_lid
);
1666 return 0; /* finished: next thread. */
1670 new_wait_thread_callback (threadinfo
*thread
, void *data
)
1672 /* don't wait on the event thread -- it's already stopped and waited.
1673 Ditto the main thread. */
1674 if (thread
->lid
!= event_pid
&&
1675 thread
->lid
!= main_prochandle
.pid
)
1677 wait_for_stop (thread
->lid
);
1683 * Wait for any thread to stop, by calling the underlying wait method.
1684 * The PID returned by the underlying target may be a kernel thread,
1685 * in which case we will want to convert it to the corresponding
1686 * user-space thread.
1690 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1692 td_thrhandle_t thandle
;
1701 /* OK, we're about to wait for an event from the running inferior.
1702 Make sure we're ignoring the right signals. */
1704 check_all_signal_numbers (); /* see if magic signals changed. */
1709 /* FIXME: should I do the wait right here inline? */
1711 if (PIDGET (ptid
) == -1)
1714 lwp
= get_lwp_from_thread_id (GET_THREAD (ptid
));
1718 save_errno
= linux_child_wait (-1, &retpid
, &status
);
1719 store_waitstatus (ourstatus
, status
);
1721 /* Thread ID is irrelevant if the target process exited.
1722 FIXME: do I have any killing to do?
1723 Can I get this event mistakenly from a thread? */
1724 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
1725 return pid_to_ptid (retpid
);
1727 /* OK, we got an event of interest.
1728 Go stop all threads and look for new ones.
1729 FIXME: maybe don't do this for the restart signal? Optimization... */
1732 /* If the last call to resume was for a specific thread, then we don't
1733 need to stop everyone else: they should already be stopped. */
1734 if (last_resume_step
== 0 || last_resume_pid
== -1)
1736 /* Main thread must be stopped before calling the iterator. */
1737 if (retpid
!= main_prochandle
.pid
)
1739 kill (main_prochandle
.pid
, SIGSTOP
);
1740 wait_for_stop (main_prochandle
.pid
);
1743 empty_threadlist ();
1744 /* Now stop everyone else, and attach any new threads you find. */
1745 p_td_ta_thr_iter (main_threadagent
,
1746 stop_or_attach_thread_callback
,
1749 TD_THR_LOWEST_PRIORITY
,
1751 TD_THR_ANY_USER_FLAGS
);
1753 /* Now go call wait on all the threads we've stopped:
1754 This allows us to absorb the SIGKILL event, and to make sure
1755 that the thread knows that it is stopped (Linux peculiarity). */
1757 threadlist_iter (new_wait_thread_callback
,
1763 /* Convert the kernel thread id to the corresponding thread id. */
1765 /* If the process layer does not furnish an lwp,
1766 then perhaps the returned pid IS the lwp... */
1767 #if 0 /* Always true (if it'd compile...) */
1768 if ((lwp
= GET_LWP (pid_to_ptid (retpid
))) == 0)
1772 if ((ret
= p_td_ta_map_lwp2thr (main_threadagent
, lwp
, &thandle
)) != TD_OK
)
1773 return pid_to_ptid (retpid
); /* LWP is not mapped onto a user-space thread. */
1775 if ((ret
= p_td_thr_validate (&thandle
)) != TD_OK
)
1776 return pid_to_ptid (retpid
); /* LWP is not mapped onto a valid thread. */
1778 if ((ret
= p_td_thr_get_info (&thandle
, &ti
)) != TD_OK
)
1780 warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret
));
1781 return pid_to_ptid (retpid
);
1784 retptid
= BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
);
1785 /* If this is a new user thread, notify GDB about it. */
1786 if (!in_thread_list (retptid
))
1788 printf_filtered ("[New %s]\n", target_pid_to_str (retptid
));
1789 add_thread (retptid
);
1793 /* Now detect if this is a thread creation/deletion event: */
1794 check_for_thread_event (ourstatus
, retpid
);
1800 * kill has to call the underlying kill.
1801 * FIXME: I'm not sure if it's necessary to check inferior_ptid any more,
1802 * but we might need to fix inferior_ptid up if it's a user thread.
1806 kill_thread_callback (const td_thrhandle_t
*th
, void *data
)
1812 For Linux, threads may need to be waited. */
1813 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1815 warning ("kill_thread_callback: %s", thr_err_string (ret
));
1816 return -1; /* bail out, get_info failed. */
1819 if (ti
.ti_lid
!= main_prochandle
.pid
)
1821 kill (ti
.ti_lid
, SIGKILL
);
1827 static void thread_db_kill (void)
1833 For Linux, threads may need to be waited. */
1834 if (! ptid_equal (inferior_ptid
, null_ptid
))
1836 /* Go kill the children first. Save the main thread for last. */
1837 p_td_ta_thr_iter (main_threadagent
,
1838 kill_thread_callback
,
1841 TD_THR_LOWEST_PRIORITY
,
1843 TD_THR_ANY_USER_FLAGS
);
1845 /* Turn off thread_db event-reporting API *before* killing the
1846 main thread, since this operation requires child memory access.
1847 Can't move this into thread_db_unpush target because then
1848 detach would not work. */
1849 disable_thread_event_reporting (main_threadagent
);
1851 inferior_ptid
= pid_to_ptid (main_prochandle
.pid
);
1854 * Since both procfs_kill and ptrace_kill call target_mourn,
1855 * it should be sufficient for me to call one of them.
1856 * That will result in my mourn being called, which will both
1857 * unpush me and call the underlying mourn.
1859 target_beneath
->to_kill ();
1862 /* Wait for all threads. */
1863 /* FIXME: need a universal wait_for_signal func? */
1866 rpid
= waitpid (-1, &status
, __WCLONE
| WNOHANG
);
1868 while (rpid
> 0 || errno
== EINTR
);
1872 rpid
= waitpid (-1, &status
, WNOHANG
);
1874 while (rpid
> 0 || errno
== EINTR
);
1878 * Mourn has to remove us from the target stack,
1879 * and then call the underlying mourn.
1882 static void thread_db_mourn_inferior (void)
1884 thread_db_unpush_target ();
1885 target_mourn_inferior (); /* call the underlying mourn */
1889 * Detach has to remove us from the target stack,
1890 * and then call the underlying detach.
1892 * But first, it has to detach all the cloned threads!
1896 detach_thread_callback (const td_thrhandle_t
*th
, void *data
)
1898 /* Called once per thread. */
1902 if ((ret
= p_td_thr_get_info (th
, &ti
)) != TD_OK
)
1904 warning ("detach_thread_callback: %s", thr_err_string (ret
));
1905 return -1; /* bail out, get_info failed. */
1908 if (!in_thread_list (BUILD_THREAD (ti
.ti_tid
, main_prochandle
.pid
)))
1909 return 0; /* apparently we don't know this one. */
1911 /* Save main thread for last, or the iterator will fail! */
1912 if (ti
.ti_lid
!= main_prochandle
.pid
)
1914 struct cleanup
*old_chain
;
1917 /* Time to detach this thread.
1918 First disable thread_db event reporting for the thread. */
1919 if (p_td_thr_event_enable
&&
1920 (ret
= p_td_thr_event_enable (th
, off
)) != TD_OK
)
1922 warning ("detach_thread_callback: %s\n", thr_err_string (ret
));
1926 /* Now cancel any pending SIGTRAPS. FIXME! */
1928 /* Call underlying detach method. FIXME just detach it. */
1929 old_chain
= save_inferior_ptid ();
1930 inferior_ptid
= pid_to_ptid (ti
.ti_lid
);
1931 detach (TARGET_SIGNAL_0
);
1932 do_cleanups (old_chain
);
1938 thread_db_detach (char *args
, int from_tty
)
1942 if ((ret
= p_td_ta_thr_iter (main_threadagent
,
1943 detach_thread_callback
,
1946 TD_THR_LOWEST_PRIORITY
,
1948 TD_THR_ANY_USER_FLAGS
))
1950 warning ("detach (thr_iter): %s", thr_err_string (ret
));
1952 /* Turn off thread_db event-reporting API
1953 (before detaching the main thread) */
1954 disable_thread_event_reporting (main_threadagent
);
1956 thread_db_unpush_target ();
1958 /* above call nullifies target_beneath, so don't use that! */
1959 inferior_ptid
= pid_to_ptid (PIDGET (inferior_ptid
));
1960 target_detach (args
, from_tty
);
1965 * We never want to actually create the inferior!
1967 * If this is ever called, it means we were on the target stack
1968 * when the user said "run". But we don't want to be on the new
1969 * inferior's target stack until the thread_db / libthread
1970 * connection is ready to be made.
1972 * So, what shall we do?
1973 * Unpush ourselves from the stack, and then invoke
1974 * find_default_create_inferior, which will invoke the
1975 * appropriate process_stratum target to do the create.
1979 thread_db_create_inferior (char *exec_file
, char *allargs
, char **env
)
1981 thread_db_unpush_target ();
1982 find_default_create_inferior (exec_file
, allargs
, env
);
1986 * Thread_db target vector initializer.
1990 init_thread_db_ops (void)
1992 thread_db_ops
.to_shortname
= "multi-thread";
1993 thread_db_ops
.to_longname
= "multi-threaded child process.";
1994 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1995 thread_db_ops
.to_files_info
= thread_db_files_info
;
1996 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
1997 thread_db_ops
.to_detach
= thread_db_detach
;
1998 thread_db_ops
.to_wait
= thread_db_wait
;
1999 thread_db_ops
.to_resume
= thread_db_resume
;
2000 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
2001 thread_db_ops
.to_kill
= thread_db_kill
;
2002 thread_db_ops
.to_xfer_memory
= thread_db_xfer_memory
;
2003 thread_db_ops
.to_fetch_registers
= thread_db_fetch_registers
;
2004 thread_db_ops
.to_store_registers
= thread_db_store_registers
;
2005 thread_db_ops
.to_thread_alive
= thread_db_alive
;
2006 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
2007 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
2008 thread_db_ops
.to_stratum
= thread_stratum
;
2009 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
2010 thread_db_ops
.to_magic
= OPS_MAGIC
;
2012 #endif /* HAVE_STDINT_H */
2015 * Module constructor / initializer function.
2016 * If connection to thread_db dynamic library is successful,
2017 * then initialize this module's target vectors and the
2023 _initialize_thread_db (void)
2025 #ifdef HAVE_STDINT_H /* stub out entire module, leave initializer empty */
2026 if (init_thread_db_library ())
2028 init_thread_db_ops ();
2029 add_target (&thread_db_ops
);
2031 * Hook up to the new_objfile event.
2032 * If someone is already there, arrange for him to be called
2035 target_new_objfile_chain
= target_new_objfile_hook
;
2036 target_new_objfile_hook
= thread_db_new_objfile
;
2038 #endif /* HAVE_STDINT_H */