2 /* FreeBSD libthread_db assisted debugging support.
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. */
23 #include <sys/types.h>
24 #include <sys/ptrace.h>
27 #include "proc_service.h"
28 #include "thread_db.h"
33 #include "gdb_assert.h"
35 #include "gdbthread.h"
43 #include "solib-svr4.h"
46 #define LIBTHREAD_DB_SO "libthread_db.so"
53 extern int child_suppress_run;
55 extern struct target_ops child_ops;
57 /* This module's target vectors. */
58 static struct target_ops fbsd_thread_ops;
59 static struct target_ops fbsd_core_ops;
61 /* Saved copy of orignal core_ops. */
62 static struct target_ops orig_core_ops;
63 extern struct target_ops core_ops;
65 /* Pointer to the next function on the objfile event chain. */
66 static void (*target_new_objfile_chain) (struct objfile *objfile);
68 /* Non-zero if there is a thread module */
69 static int fbsd_thread_present;
71 /* Non-zero if we're using this module's target vector. */
72 static int fbsd_thread_active;
74 /* Non-zero if core_open is called */
75 static int fbsd_thread_core = 0;
77 /* Non-zero if we have to keep this module's target vector active
79 static int keep_thread_db;
81 /* Structure that identifies the child process for the
82 <proc_service.h> interface. */
83 static struct ps_prochandle proc_handle;
85 /* Connection to the libthread_db library. */
86 static td_thragent_t *thread_agent;
88 /* The last thread we are single stepping */
89 static ptid_t last_single_step_thread;
91 /* Pointers to the libthread_db functions. */
93 static td_err_e (*td_init_p) (void);
95 static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta);
96 static td_err_e (*td_ta_delete_p) (td_thragent_t *);
97 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
98 td_thrhandle_t *__th);
99 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
101 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
102 td_thr_iter_f *callback,
103 void *cbdata_p, td_thr_state_e state,
104 int ti_pri, sigset_t *ti_sigmask_p,
105 unsigned int ti_user_flags);
106 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
107 td_event_e event, td_notify_t *ptr);
108 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
109 td_thr_events_t *event);
110 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
111 td_event_msg_t *msg);
112 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
113 td_thrinfo_t *infop);
115 static td_err_e (*td_thr_getxmmregs_p) (const td_thrhandle_t *th,
118 static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
119 prfpregset_t *regset);
120 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
123 static td_err_e (*td_thr_setxmmregs_p) (const td_thrhandle_t *th,
126 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
127 const prfpregset_t *fpregs);
128 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
130 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
132 static td_err_e (*td_thr_sstep_p) (td_thrhandle_t *th, int step);
134 static td_err_e (*td_ta_tsd_iter_p) (const td_thragent_t *ta,
135 td_key_iter_f *func, void *data);
136 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
138 size_t offset, void **address);
139 static td_err_e (*td_thr_dbsuspend_p) (const td_thrhandle_t *);
140 static td_err_e (*td_thr_dbresume_p) (const td_thrhandle_t *);
142 static CORE_ADDR td_create_bp_addr;
144 /* Location of the thread death event breakpoint. */
145 static CORE_ADDR td_death_bp_addr;
147 /* Prototypes for local functions. */
148 static void fbsd_thread_find_new_threads (void);
149 static int fbsd_thread_alive (ptid_t ptid);
150 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
151 const td_thrinfo_t *ti_p, int verbose);
152 static void fbsd_thread_detach (char *args, int from_tty);
154 /* Building process ids. */
156 #define GET_PID(ptid) ptid_get_pid (ptid)
157 #define GET_LWP(ptid) ptid_get_lwp (ptid)
158 #define GET_THREAD(ptid) ptid_get_tid (ptid)
160 #define IS_LWP(ptid) (GET_LWP (ptid) != 0)
161 #define IS_THREAD(ptid) (GET_THREAD (ptid) != 0)
163 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
164 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
167 thread_db_err_str (td_err_e err)
174 return "generic 'call succeeded'";
176 return "generic error";
178 return "no thread to satisfy query";
180 return "no sync handle to satisfy query";
182 return "no LWP to satisfy query";
184 return "invalid process handle";
186 return "invalid thread handle";
188 return "invalid synchronization handle";
190 return "invalid thread agent";
192 return "invalid key";
194 return "no event message for getmsg";
196 return "FPU register set not available";
198 return "application not linked with libthread";
200 return "requested event is not supported";
202 return "capability not available";
204 return "debugger service failed";
206 return "operation not applicable to";
208 return "no thread-specific data for this thread";
210 return "malloc failed";
212 return "only part of register set was written/read";
214 return "X register set not available for this thread";
216 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
222 thread_db_state_str (td_thr_state_e state)
229 return "stopped by debugger";
238 case TD_THR_STOPPED_ASLEEP:
239 return "stopped by debugger AND blocked";
241 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
246 /* Convert LWP to user-level thread id. */
248 thread_from_lwp (ptid_t ptid, td_thrhandle_t *th, td_thrinfo_t *ti)
252 gdb_assert (IS_LWP (ptid));
254 if (fbsd_thread_active)
256 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), th);
259 err = td_thr_get_info_p (th, ti);
261 error ("Cannot get thread info: %s", thread_db_err_str (err));
262 return BUILD_THREAD (ti->ti_tid, GET_PID (ptid));
266 /* the LWP is not mapped to user thread */
267 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
271 fbsd_core_get_first_lwp (bfd *abfd, asection *asect, void *obj)
273 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
276 if (*(lwpid_t *)obj != 0)
279 *(lwpid_t *)obj = atoi (bfd_section_name (abfd, asect) + 5);
283 get_current_lwp (int pid)
285 struct ptrace_lwpinfo pl;
288 if (!target_has_execution)
291 bfd_map_over_sections (core_bfd, fbsd_core_get_first_lwp, &lwpid);
294 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof(pl)))
295 perror_with_name("PT_LWPINFO");
297 return (long)pl.pl_lwpid;
301 get_current_thread ()
308 lwp = get_current_lwp (proc_handle.pid);
309 tmp = BUILD_LWP (lwp, proc_handle.pid);
310 ptid = thread_from_lwp (tmp, &th, &ti);
311 if (!in_thread_list (ptid))
313 attach_thread (ptid, &th, &ti, 1);
315 inferior_ptid = ptid;
319 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
324 /* Get the breakpoint address for thread EVENT. */
325 err = td_ta_event_addr_p (thread_agent, event, ¬ify);
329 /* Set up the breakpoint. */
330 (*bp) = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
331 extract_typed_address(¬ify.u.bptaddr, builtin_type_void_func_ptr),
333 create_thread_event_breakpoint ((*bp));
339 enable_thread_event_reporting (void)
341 td_thr_events_t events;
345 /* We cannot use the thread event reporting facility if these
346 functions aren't available. */
347 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
348 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
351 /* Set the process wide mask saying which events we're interested in. */
352 td_event_emptyset (&events);
353 td_event_addset (&events, TD_CREATE);
354 td_event_addset (&events, TD_DEATH);
356 err = td_ta_set_event_p (thread_agent, &events);
359 warning ("Unable to set global thread event mask: %s",
360 thread_db_err_str (err));
364 /* Delete previous thread event breakpoints, if any. */
365 remove_thread_event_breakpoints ();
366 td_create_bp_addr = 0;
367 td_death_bp_addr = 0;
369 /* Set up the thread creation event. */
370 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
373 warning ("Unable to get location for thread creation breakpoint: %s",
374 thread_db_err_str (err));
378 /* Set up the thread death event. */
379 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
382 warning ("Unable to get location for thread death breakpoint: %s",
383 thread_db_err_str (err));
389 disable_thread_event_reporting (void)
391 td_thr_events_t events;
393 /* Set the process wide mask saying we aren't interested in any
395 td_event_emptyset (&events);
396 td_ta_set_event_p (thread_agent, &events);
398 /* Delete thread event breakpoints, if any. */
399 remove_thread_event_breakpoints ();
400 td_create_bp_addr = 0;
401 td_death_bp_addr = 0;
405 fbsd_thread_activate (void)
407 fbsd_thread_active = 1;
409 if (fbsd_thread_core == 0)
410 enable_thread_event_reporting ();
411 fbsd_thread_find_new_threads ();
412 get_current_thread ();
416 fbsd_thread_deactivate (void)
418 if (fbsd_thread_core == 0)
419 disable_thread_event_reporting();
420 td_ta_delete_p (thread_agent);
422 inferior_ptid = pid_to_ptid (proc_handle.pid);
424 fbsd_thread_active = 0;
425 fbsd_thread_present = 0;
430 fbsd_thread_new_objfile (struct objfile *objfile)
436 /* All symbols have been discarded. If the thread_db target is
437 active, deactivate it now. */
438 if (fbsd_thread_active)
440 gdb_assert (proc_handle.pid == 0);
441 fbsd_thread_active = 0;
447 if (!child_suppress_run)
450 /* Nothing to do. The thread library was already detected and the
451 target vector was already activated. */
452 if (fbsd_thread_active)
455 /* Initialize the structure that identifies the child process. Note
456 that at this point there is no guarantee that we actually have a
458 proc_handle.pid = GET_PID (inferior_ptid);
460 /* Now attempt to open a connection to the thread library. */
461 err = td_ta_new_p (&proc_handle, &thread_agent);
465 /* No thread library was detected. */
469 /* The thread library was detected. Activate the thread_db target. */
470 fbsd_thread_present = 1;
472 /* We can only poke around if there actually is a child process.
473 If there is no child process alive, postpone the steps below
474 until one has been created. */
475 if (fbsd_thread_core == 0 && proc_handle.pid != 0)
477 push_target(&fbsd_thread_ops);
478 fbsd_thread_activate();
482 td_ta_delete_p(thread_agent);
488 warning ("Cannot initialize thread debugging library: %s",
489 thread_db_err_str (err));
494 if (target_new_objfile_chain)
495 target_new_objfile_chain (objfile);
499 fbsd_thread_attach (char *args, int from_tty)
501 fbsd_thread_core = 0;
503 child_ops.to_attach (args, from_tty);
505 /* Must get symbols from solibs before libthread_db can run! */
506 SOLIB_ADD ((char *) 0, from_tty, (struct target_ops *) 0, auto_solib_add);
508 if (fbsd_thread_present && !fbsd_thread_active)
509 push_target(&fbsd_thread_ops);
513 fbsd_thread_post_attach (int pid)
515 child_ops.to_post_attach (pid);
517 if (fbsd_thread_present && !fbsd_thread_active)
519 proc_handle.pid = GET_PID (inferior_ptid);
520 fbsd_thread_activate ();
525 fbsd_thread_detach (char *args, int from_tty)
527 fbsd_thread_deactivate ();
528 unpush_target (&fbsd_thread_ops);
530 /* Clear gdb solib information and symbol file
531 cache, so that after detach and re-attach, new_objfile
532 hook will be called */
535 symbol_file_clear(0);
537 child_ops.to_detach (args, from_tty);
541 suspend_thread_callback (const td_thrhandle_t *th_p, void *data)
543 int err = td_thr_dbsuspend_p (th_p);
545 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err));
550 resume_thread_callback (const td_thrhandle_t *th_p, void *data)
552 int err = td_thr_dbresume_p (th_p);
554 fprintf_filtered(gdb_stderr, "%s %s\n", __func__, thread_db_err_str (err));
559 fbsd_thread_resume (ptid_t ptid, int step, enum target_signal signo)
565 long lwp, thvalid = 0;
567 if (!fbsd_thread_active)
569 child_ops.to_resume (ptid, step, signo);
573 if (GET_PID(ptid) != -1 && step != 0)
581 work_ptid = inferior_ptid;
584 lwp = GET_LWP (work_ptid);
587 /* check user thread */
588 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(work_ptid), &th);
590 error (thread_db_err_str (ret));
592 /* For M:N thread, we need to tell UTS to set/unset single step
593 flag at context switch time, the flag will be written into
594 thread mailbox. This becauses some architecture may not have
595 machine single step flag in ucontext, so we put the flag in mailbox,
596 when the thread switches back, kse_switchin restores the single step
598 ret = td_thr_sstep_p (&th, step);
600 error (thread_db_err_str (ret));
601 ret = td_thr_get_info_p (&th, &ti);
603 error (thread_db_err_str (ret));
610 int req = step ? PT_SETSTEP : PT_CLEARSTEP;
611 if (ptrace (req, (pid_t) lwp, (caddr_t) 1, target_signal_to_host(signo)))
612 perror_with_name ("PT_SETSTEP/PT_CLEARSTEP");
615 if (!ptid_equal (last_single_step_thread, null_ptid))
617 ret = td_ta_thr_iter_p (thread_agent, resume_thread_callback, NULL,
618 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
619 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
621 error ("resume error: %s", thread_db_err_str (ret));
626 ret = td_ta_thr_iter_p (thread_agent, suspend_thread_callback, NULL,
627 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
628 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
630 error ("suspend error: %s", thread_db_err_str (ret));
631 last_single_step_thread = work_ptid;
634 last_single_step_thread = null_ptid;
638 ret = td_thr_dbresume_p (&th);
640 error ("resume error: %s", thread_db_err_str (ret));
644 /* it is not necessary, put it here for completness */
645 ret = ptrace(PT_RESUME, lwp, 0, 0);
648 /* now continue the process, suspended thread wont run */
649 if (ptrace (PT_CONTINUE, proc_handle.pid , (caddr_t)1,
650 target_signal_to_host(signo)))
651 perror_with_name ("PT_CONTINUE");
655 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
656 const td_thrinfo_t *ti_p, int verbose)
660 /* Add the thread to GDB's thread list. */
661 if (!in_thread_list (ptid)) {
664 printf_unfiltered ("[New %s]\n", target_pid_to_str (ptid));
667 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
668 return; /* A zombie thread -- do not attach. */
670 if (! IS_THREAD(ptid))
672 if (fbsd_thread_core != 0)
674 /* Enable thread event reporting for this thread. */
675 err = td_thr_event_enable_p (th_p, 1);
677 error ("Cannot enable thread event reporting for %s: %s",
678 target_pid_to_str (ptid), thread_db_err_str (err));
682 detach_thread (ptid_t ptid, int verbose)
685 printf_unfiltered ("[%s exited]\n", target_pid_to_str (ptid));
689 check_event (ptid_t ptid)
697 /* Bail out early if we're not at a thread event breakpoint. */
698 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
699 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
705 err = td_ta_event_getmsg_p (thread_agent, &msg);
710 error ("Cannot get thread event message: %s",
711 thread_db_err_str (err));
713 err = td_thr_get_info_p ((void *)(uintptr_t)msg.th_p, &ti);
715 error ("Cannot get thread info: %s", thread_db_err_str (err));
716 ptid = BUILD_THREAD (ti.ti_tid, GET_PID (ptid));
720 /* We may already know about this thread, for instance when the
721 user has issued the `info threads' command before the SIGTRAP
722 for hitting the thread creation breakpoint was reported. */
723 attach_thread (ptid, (void *)(uintptr_t)msg.th_p, &ti, 1);
726 if (!in_thread_list (ptid))
727 error ("Spurious thread death event.");
728 detach_thread (ptid, 1);
731 error ("Spurious thread event.");
738 fbsd_thread_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
746 ret = child_ops.to_wait (ptid, ourstatus);
747 if (GET_PID(ret) >= 0 && ourstatus->kind == TARGET_WAITKIND_STOPPED)
749 lwp = get_current_lwp (GET_PID(ret));
750 ret = thread_from_lwp (BUILD_LWP(lwp, GET_PID(ret)),
752 if (!in_thread_list(ret)) {
754 * We have to enable event reporting for initial thread
755 * which was not mapped before.
757 attach_thread(ret, &th, &ti, 1);
759 if (ourstatus->value.sig == TARGET_SIGNAL_TRAP)
761 /* this is a hack, if an event won't cause gdb to stop, for example,
762 SIGARLM, gdb resumes the process immediatly without setting
763 inferior_ptid to the new thread returned here, this is a bug
764 because inferior_ptid may already not exist there, and passing
765 a none existing thread to fbsd_thread_resume causes error. */
766 if (!fbsd_thread_alive (inferior_ptid))
768 delete_thread (inferior_ptid);
777 fbsd_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
778 struct mem_attrib *attrib, struct target_ops *target)
782 if (target_has_execution)
783 err = child_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib,
786 err = orig_core_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib,
793 fbsd_lwp_fetch_registers (int regno)
802 if (!target_has_execution)
804 orig_core_ops.to_fetch_registers (-1);
808 /* XXX: We've replaced the pid with the lwpid for GDB's benefit. */
809 lwp = GET_PID (inferior_ptid);
811 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1)
812 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno));
813 supply_gregset (&gregs);
816 if (ptrace (PT_GETXMMREGS, lwp, xmmregs, 0) == 0)
818 i387_supply_fxsave (current_regcache, -1, xmmregs);
823 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
824 error ("Cannot get lwp %d registers: %s\n ", lwp, safe_strerror (errno));
825 supply_fpregset (&fpregs);
832 fbsd_thread_fetch_registers (int regno)
835 prfpregset_t fpregset;
842 if (!IS_THREAD (inferior_ptid))
844 fbsd_lwp_fetch_registers (regno);
848 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
850 error ("Cannot find thread %d: Thread ID=%ld, %s",
851 pid_to_thread_id (inferior_ptid),
852 GET_THREAD (inferior_ptid), thread_db_err_str (err));
854 err = td_thr_getgregs_p (&th, gregset);
856 error ("Cannot fetch general-purpose registers for thread %d: Thread ID=%ld, %s",
857 pid_to_thread_id (inferior_ptid),
858 GET_THREAD (inferior_ptid), thread_db_err_str (err));
860 err = td_thr_getxmmregs_p (&th, xmmregs);
863 i387_supply_fxsave (current_regcache, -1, xmmregs);
868 err = td_thr_getfpregs_p (&th, &fpregset);
870 error ("Cannot get floating-point registers for thread %d: Thread ID=%ld, %s",
871 pid_to_thread_id (inferior_ptid),
872 GET_THREAD (inferior_ptid), thread_db_err_str (err));
873 supply_fpregset (&fpregset);
878 supply_gregset (gregset);
882 fbsd_lwp_store_registers (int regno)
891 /* FIXME, is it possible ? */
892 if (!IS_LWP (inferior_ptid))
894 child_ops.to_store_registers (regno);
898 lwp = GET_LWP (inferior_ptid);
900 if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1)
901 error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno));
903 fill_gregset (&gregs, regno);
904 if (ptrace (PT_SETREGS, lwp, (caddr_t) &gregs, 0) == -1)
905 error ("Cannot set lwp %d registers: %s\n", lwp, safe_strerror (errno));
909 if (ptrace (PT_GETXMMREGS, lwp, xmmregs, 0) == -1)
912 i387_fill_fxsave (xmmregs, regno);
913 if (ptrace (PT_SETXMMREGS, lwp, xmmregs, 0) == -1)
922 if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
923 error ("Cannot get lwp %d float registers: %s\n", lwp,
924 safe_strerror (errno));
926 fill_fpregset (&fpregs, regno);
927 if (ptrace (PT_SETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
928 error ("Cannot set lwp %d float registers: %s\n", lwp,
929 safe_strerror (errno));
933 fbsd_thread_store_registers (int regno)
936 prfpregset_t fpregset;
943 if (!IS_THREAD (inferior_ptid))
945 fbsd_lwp_store_registers (regno);
949 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
951 error ("Cannot find thread %d: Thread ID=%ld, %s",
952 pid_to_thread_id (inferior_ptid),
953 GET_THREAD (inferior_ptid),
954 thread_db_err_str (err));
958 char old_value[MAX_REGISTER_SIZE];
960 regcache_collect (regno, old_value);
961 err = td_thr_getgregs_p (&th, gregset);
963 error ("%s: td_thr_getgregs %s", __func__, thread_db_err_str (err));
965 err = td_thr_getxmmregs_p (&th, xmmregs);
969 err = td_thr_getfpregs_p (&th, &fpregset);
971 error ("%s: td_thr_getfpgregs %s", __func__, thread_db_err_str (err));
975 supply_register (regno, old_value);
978 fill_gregset (gregset, regno);
979 err = td_thr_setgregs_p (&th, gregset);
981 error ("Cannot store general-purpose registers for thread %d: Thread ID=%d, %s",
982 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid),
983 thread_db_err_str (err));
986 i387_fill_fxsave (xmmregs, regno);
987 err = td_thr_setxmmregs_p (&th, xmmregs);
992 fill_fpregset (&fpregset, regno);
993 err = td_thr_setfpregs_p (&th, &fpregset);
995 error ("Cannot store floating-point registers for thread %d: Thread ID=%d, %s",
996 pid_to_thread_id (inferior_ptid), GET_THREAD (inferior_ptid),
997 thread_db_err_str (err));
1001 fbsd_thread_kill (void)
1003 child_ops.to_kill();
1007 fbsd_thread_can_run (void)
1009 return child_suppress_run;
1013 fbsd_thread_create_inferior (char *exec_file, char *allargs, char **env)
1015 if (fbsd_thread_present && !fbsd_thread_active)
1016 push_target(&fbsd_thread_ops);
1018 child_ops.to_create_inferior (exec_file, allargs, env);
1022 fbsd_thread_post_startup_inferior (ptid_t ptid)
1024 if (fbsd_thread_present && !fbsd_thread_active)
1026 /* The child process is now the actual multi-threaded
1027 program. Snatch its process ID... */
1028 proc_handle.pid = GET_PID (ptid);
1029 td_ta_new_p (&proc_handle, &thread_agent);
1030 fbsd_thread_activate();
1035 fbsd_thread_mourn_inferior (void)
1037 if (fbsd_thread_active)
1038 fbsd_thread_deactivate ();
1040 unpush_target (&fbsd_thread_ops);
1042 child_ops.to_mourn_inferior ();
1046 fbsd_core_check_lwp (bfd *abfd, asection *asect, void *obj)
1050 if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
1054 if (*(lwpid_t *)obj == 0)
1057 lwp = atoi (bfd_section_name (abfd, asect) + 5);
1058 if (*(lwpid_t *)obj == lwp)
1059 *(lwpid_t *)obj = 0;
1063 fbsd_thread_alive (ptid_t ptid)
1071 if (IS_THREAD (ptid))
1073 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th);
1077 err = td_thr_get_info_p (&th, &ti);
1081 /* A zombie thread. */
1082 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1087 else if (GET_LWP (ptid) == 0)
1089 /* we sometimes are called with lwp == 0 */
1093 if (fbsd_thread_active)
1095 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
1098 * if the lwp was already mapped to user thread, don't use it
1099 * directly, please use user thread id instead.
1105 if (!target_has_execution)
1107 lwp = GET_LWP (ptid);
1108 bfd_map_over_sections (core_bfd, fbsd_core_check_lwp, &lwp);
1112 /* check lwp in kernel */
1113 return ptrace (PT_GETREGS, GET_LWP (ptid), (caddr_t)&gregs, 0) == 0;
1117 fbsd_thread_files_info (struct target_ops *ignore)
1119 child_ops.to_files_info (ignore);
1123 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1129 err = td_thr_get_info_p (th_p, &ti);
1131 error ("Cannot get thread info: %s", thread_db_err_str (err));
1134 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1137 ptid = BUILD_THREAD (ti.ti_tid, proc_handle.pid);
1138 attach_thread (ptid, th_p, &ti, 1);
1143 fbsd_thread_find_new_threads (void)
1147 if (!fbsd_thread_active)
1150 /* Iterate over all user-space threads to discover new threads. */
1151 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1152 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1153 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1155 error ("Cannot find new threads: %s", thread_db_err_str (err));
1159 fbsd_thread_pid_to_str (ptid_t ptid)
1161 static char buf[64];
1163 if (IS_THREAD (ptid))
1169 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th);
1171 error ("Cannot find thread, Thread ID=%ld, %s",
1172 GET_THREAD (ptid), thread_db_err_str (err));
1174 err = td_thr_get_info_p (&th, &ti);
1176 error ("Cannot get thread info, Thread ID=%ld, %s",
1177 GET_THREAD (ptid), thread_db_err_str (err));
1181 snprintf (buf, sizeof (buf), "Thread %llx (LWP %d)",
1182 (unsigned long long)th.th_thread, ti.ti_lid);
1186 snprintf (buf, sizeof (buf), "Thread %llx (%s)",
1187 (unsigned long long)th.th_thread,
1188 thread_db_state_str (ti.ti_state));
1193 else if (IS_LWP (ptid))
1195 snprintf (buf, sizeof (buf), "LWP %d", (int) GET_LWP (ptid));
1198 return normal_pid_to_str (ptid);
1202 fbsd_thread_get_local_address(ptid_t ptid, struct objfile *objfile,
1209 int ret, is_library = (objfile->flags & OBJF_SHARED);
1211 if (IS_THREAD (ptid))
1213 if (!td_thr_tls_get_addr_p)
1214 error ("Cannot find thread-local interface in thread_db library.");
1216 /* Get the address of the link map for this objfile. */
1217 lm = svr4_fetch_objfile_link_map (objfile);
1219 /* Couldn't find link map. Bail out. */
1223 error ("Cannot find shared library `%s' link_map in dynamic"
1224 " linker's module list", objfile->name);
1226 error ("Cannot find executable file `%s' link_map in dynamic"
1227 " linker's module list", objfile->name);
1230 ret = td_ta_map_id2thr_p (thread_agent, GET_THREAD(ptid), &th);
1232 /* get the address of the variable. */
1233 store_typed_address(&lm2, builtin_type_void_data_ptr, lm);
1234 ret = td_thr_tls_get_addr_p (&th, lm2, offset, &address);
1239 error ("Cannot find thread-local storage for thread %ld, "
1240 "shared library %s:\n%s",
1241 (long) GET_THREAD (ptid),
1242 objfile->name, thread_db_err_str (ret));
1244 error ("Cannot find thread-local storage for thread %ld, "
1245 "executable file %s:\n%s",
1246 (long) GET_THREAD (ptid),
1247 objfile->name, thread_db_err_str (ret));
1250 /* Cast assuming host == target. */
1251 return extract_typed_address(&address, builtin_type_void_data_ptr);
1257 tsd_cb (thread_key_t key, void (*destructor)(void *), void *ignore)
1259 struct minimal_symbol *ms;
1262 ms = lookup_minimal_symbol_by_pc (
1263 extract_typed_address(&destructor, builtin_type_void_func_ptr));
1267 name = DEPRECATED_SYMBOL_NAME (ms);
1269 printf_filtered ("Destructor %p <%s>\n", destructor, name);
1274 fbsd_thread_tsd_cmd (char *exp, int from_tty)
1276 if (fbsd_thread_active)
1277 td_ta_tsd_iter_p (thread_agent, tsd_cb, NULL);
1281 fbsd_print_sigset (sigset_t *set)
1285 for (i = 1; i <= _SIG_MAXSIG; ++i) {
1286 if (sigismember(set, i)) {
1287 if (i < sizeof(sys_signame)/sizeof(sys_signame[0]))
1288 printf_filtered("%s ", sys_signame[i]);
1290 printf_filtered("sig%d ", i);
1293 printf_filtered("\n");
1297 fbsd_thread_signal_cmd (char *exp, int from_tty)
1304 if (!fbsd_thread_active || !IS_THREAD(inferior_ptid))
1307 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
1311 err = td_thr_get_info_p (&th, &ti);
1315 printf_filtered("signal mask:\n");
1316 fbsd_print_sigset(&ti.ti_sigmask);
1317 printf_filtered("signal pending:\n");
1318 fbsd_print_sigset(&ti.ti_pending);
1319 if (ti.ti_siginfo.si_signo != 0) {
1320 printf_filtered("si_signo %d si_errno %d", ti.ti_siginfo.si_signo,
1321 ti.ti_siginfo.si_errno);
1322 if (ti.ti_siginfo.si_errno != 0)
1323 printf_filtered(" (%s)", strerror(ti.ti_siginfo.si_errno));
1324 printf_filtered("\n");
1325 switch (ti.ti_siginfo.si_code) {
1351 printf_filtered("si_code %s si_pid %d si_uid %d si_status %x si_addr %p\n",
1352 code, ti.ti_siginfo.si_pid, ti.ti_siginfo.si_uid, ti.ti_siginfo.si_status,
1353 ti.ti_siginfo.si_addr);
1358 ignore (CORE_ADDR addr, char *contents)
1364 fbsd_core_open (char *filename, int from_tty)
1368 fbsd_thread_core = 1;
1370 orig_core_ops.to_open (filename, from_tty);
1372 if (fbsd_thread_present)
1374 err = td_ta_new_p (&proc_handle, &thread_agent);
1377 proc_handle.pid = elf_tdata (core_bfd)->core_pid;
1378 fbsd_thread_activate ();
1381 error ("fbsd_core_open: td_ta_new: %s", thread_db_err_str (err));
1386 fbsd_core_close (int quitting)
1388 orig_core_ops.to_close (quitting);
1392 fbsd_core_detach (char *args, int from_tty)
1394 if (fbsd_thread_active)
1395 fbsd_thread_deactivate ();
1396 unpush_target (&fbsd_thread_ops);
1397 orig_core_ops.to_detach (args, from_tty);
1399 /* Clear gdb solib information and symbol file
1400 cache, so that after detach and re-attach, new_objfile
1401 hook will be called */
1403 symbol_file_clear(0);
1407 fbsd_core_files_info (struct target_ops *ignore)
1409 orig_core_ops.to_files_info (ignore);
1413 init_fbsd_core_ops (void)
1415 fbsd_core_ops.to_shortname = "FreeBSD-core";
1416 fbsd_core_ops.to_longname = "FreeBSD multithreaded core dump file";
1417 fbsd_core_ops.to_doc =
1418 "Use a core file as a target. Specify the filename of the core file.";
1419 fbsd_core_ops.to_open = fbsd_core_open;
1420 fbsd_core_ops.to_close = fbsd_core_close;
1421 fbsd_core_ops.to_attach = 0;
1422 fbsd_core_ops.to_post_attach = 0;
1423 fbsd_core_ops.to_detach = fbsd_core_detach;
1424 /* fbsd_core_ops.to_resume = 0; */
1425 /* fbsd_core_ops.to_wait = 0; */
1426 fbsd_core_ops.to_fetch_registers = fbsd_thread_fetch_registers;
1427 /* fbsd_core_ops.to_store_registers = 0; */
1428 /* fbsd_core_ops.to_prepare_to_store = 0; */
1429 fbsd_core_ops.to_xfer_memory = fbsd_thread_xfer_memory;
1430 fbsd_core_ops.to_files_info = fbsd_core_files_info;
1431 fbsd_core_ops.to_insert_breakpoint = ignore;
1432 fbsd_core_ops.to_remove_breakpoint = ignore;
1433 /* fbsd_core_ops.to_lookup_symbol = 0; */
1434 fbsd_core_ops.to_create_inferior = fbsd_thread_create_inferior;
1435 fbsd_core_ops.to_stratum = core_stratum;
1436 fbsd_core_ops.to_has_all_memory = 0;
1437 fbsd_core_ops.to_has_memory = 1;
1438 fbsd_core_ops.to_has_stack = 1;
1439 fbsd_core_ops.to_has_registers = 1;
1440 fbsd_core_ops.to_has_execution = 0;
1441 fbsd_core_ops.to_has_thread_control = tc_none;
1442 fbsd_core_ops.to_thread_alive = fbsd_thread_alive;
1443 fbsd_core_ops.to_pid_to_str = fbsd_thread_pid_to_str;
1444 fbsd_core_ops.to_find_new_threads = fbsd_thread_find_new_threads;
1445 fbsd_core_ops.to_sections = 0;
1446 fbsd_core_ops.to_sections_end = 0;
1447 fbsd_core_ops.to_magic = OPS_MAGIC;
1451 init_fbsd_thread_ops (void)
1453 fbsd_thread_ops.to_shortname = "freebsd-threads";
1454 fbsd_thread_ops.to_longname = "FreeBSD multithreaded child process.";
1455 fbsd_thread_ops.to_doc = "FreeBSD threads support.";
1456 fbsd_thread_ops.to_attach = fbsd_thread_attach;
1457 fbsd_thread_ops.to_detach = fbsd_thread_detach;
1458 fbsd_thread_ops.to_post_attach = fbsd_thread_post_attach;
1459 fbsd_thread_ops.to_resume = fbsd_thread_resume;
1460 fbsd_thread_ops.to_wait = fbsd_thread_wait;
1461 fbsd_thread_ops.to_fetch_registers = fbsd_thread_fetch_registers;
1462 fbsd_thread_ops.to_store_registers = fbsd_thread_store_registers;
1463 fbsd_thread_ops.to_xfer_memory = fbsd_thread_xfer_memory;
1464 fbsd_thread_ops.to_files_info = fbsd_thread_files_info;
1465 fbsd_thread_ops.to_kill = fbsd_thread_kill;
1466 fbsd_thread_ops.to_create_inferior = fbsd_thread_create_inferior;
1467 fbsd_thread_ops.to_post_startup_inferior = fbsd_thread_post_startup_inferior;
1468 fbsd_thread_ops.to_mourn_inferior = fbsd_thread_mourn_inferior;
1469 fbsd_thread_ops.to_can_run = fbsd_thread_can_run;
1470 fbsd_thread_ops.to_thread_alive = fbsd_thread_alive;
1471 fbsd_thread_ops.to_find_new_threads = fbsd_thread_find_new_threads;
1472 fbsd_thread_ops.to_pid_to_str = fbsd_thread_pid_to_str;
1473 fbsd_thread_ops.to_stratum = thread_stratum;
1474 fbsd_thread_ops.to_has_thread_control = tc_none;
1475 fbsd_thread_ops.to_has_all_memory = 1;
1476 fbsd_thread_ops.to_has_memory = 1;
1477 fbsd_thread_ops.to_has_stack = 1;
1478 fbsd_thread_ops.to_has_registers = 1;
1479 fbsd_thread_ops.to_has_execution = 1;
1480 fbsd_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1481 fbsd_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1482 fbsd_thread_ops.to_get_thread_local_address = fbsd_thread_get_local_address;
1483 fbsd_thread_ops.to_magic = OPS_MAGIC;
1487 thread_db_load (void)
1492 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
1496 #define resolve(X) \
1497 if (!(X##_p = dlsym (handle, #X))) \
1502 resolve(td_ta_delete);
1503 resolve(td_ta_map_id2thr);
1504 resolve(td_ta_map_lwp2thr);
1505 resolve(td_ta_thr_iter);
1506 resolve(td_thr_get_info);
1507 #ifdef PT_GETXMMREGS
1508 resolve(td_thr_getxmmregs);
1510 resolve(td_thr_getfpregs);
1511 resolve(td_thr_getgregs);
1512 #ifdef PT_GETXMMREGS
1513 resolve(td_thr_setxmmregs);
1515 resolve(td_thr_setfpregs);
1516 resolve(td_thr_setgregs);
1517 resolve(td_thr_sstep);
1518 resolve(td_ta_tsd_iter);
1519 resolve(td_thr_dbsuspend);
1520 resolve(td_thr_dbresume);
1521 resolve(td_thr_tls_get_addr);
1523 /* Initialize the library. */
1527 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err));
1531 /* These are not essential. */
1532 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
1533 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
1534 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
1535 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
1536 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
1541 /* we suppress the call to add_target of core_ops in corelow because
1542 if there are two targets in the stratum core_stratum, find_core_target
1543 won't know which one to return. see corelow.c for an additonal
1544 comment on coreops_suppress_target. */
1546 int coreops_suppress_target = 1;
1548 /* similarly we allow this target to be completely skipped. This is used
1549 by kgdb which uses its own core target. */
1551 int fbsdcoreops_suppress_target;
1554 _initialize_thread_db (void)
1557 if (fbsdcoreops_suppress_target)
1559 init_fbsd_thread_ops ();
1560 init_fbsd_core_ops ();
1562 if (thread_db_load ())
1564 add_target (&fbsd_thread_ops);
1566 /* "thread tsd" command */
1567 add_cmd ("tsd", class_run, fbsd_thread_tsd_cmd,
1568 "Show the thread-specific data keys and destructors "
1569 "for the process.\n",
1572 add_cmd ("signal", class_run, fbsd_thread_signal_cmd,
1573 "Show the thread signal info.\n",
1576 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1577 memcpy (&core_ops, &fbsd_core_ops, sizeof (struct target_ops));
1578 add_target (&core_ops);
1580 /* Add ourselves to objfile event chain. */
1581 target_new_objfile_chain = target_new_objfile_hook;
1582 target_new_objfile_hook = fbsd_thread_new_objfile;
1584 child_suppress_run = 1;
1588 fprintf_unfiltered (gdb_stderr,
1589 "[GDB will not be able to debug user-mode threads: %s]\n", dlerror());
1591 /* allow the user to debug non-threaded core files */
1592 add_target (&core_ops);
1596 /* proc service functions */
1598 ps_plog (const char *fmt, ...)
1602 va_start (args, fmt);
1603 vfprintf_filtered (gdb_stderr, fmt, args);
1608 ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj,
1609 const char *name, psaddr_t *sym_addr)
1611 struct minimal_symbol *ms;
1614 ms = lookup_minimal_symbol (name, NULL, NULL);
1618 addr = SYMBOL_VALUE_ADDRESS (ms);
1619 store_typed_address(sym_addr, builtin_type_void_data_ptr, addr);
1624 ps_pread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len)
1626 int err = target_read_memory (
1627 extract_typed_address(&addr, builtin_type_void_data_ptr), buf, len);
1628 return (err == 0 ? PS_OK : PS_ERR);
1632 ps_pwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf,
1635 int err = target_write_memory (
1636 extract_typed_address(&addr, builtin_type_void_data_ptr), (void *)buf, len);
1637 return (err == 0 ? PS_OK : PS_ERR);
1641 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
1643 struct cleanup *old_chain;
1645 old_chain = save_inferior_ptid ();
1647 /* XXX: Target operation isn't lwp aware: replace pid with lwp */
1648 inferior_ptid = BUILD_LWP (0, lwpid);
1650 target_fetch_registers (-1);
1651 fill_gregset (gregset, -1);
1652 do_cleanups (old_chain);
1657 ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
1659 struct cleanup *old_chain;
1661 old_chain = save_inferior_ptid ();
1662 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1663 supply_gregset (gregset);
1664 target_store_registers (-1);
1665 do_cleanups (old_chain);
1670 ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset)
1672 struct cleanup *old_chain;
1674 old_chain = save_inferior_ptid ();
1675 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1676 target_fetch_registers (-1);
1677 fill_fpregset (fpregset, -1);
1678 do_cleanups (old_chain);
1683 ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
1684 const prfpregset_t *fpregset)
1686 struct cleanup *old_chain;
1688 old_chain = save_inferior_ptid ();
1689 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1690 supply_fpregset (fpregset);
1691 target_store_registers (-1);
1692 do_cleanups (old_chain);
1696 #ifdef PT_GETXMMREGS
1698 ps_lgetxmmregs (struct ps_prochandle *ph, lwpid_t lwpid, char *xmmregs)
1700 struct cleanup *old_chain;
1702 old_chain = save_inferior_ptid ();
1703 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1704 target_fetch_registers (-1);
1705 i387_fill_fxsave (xmmregs, -1);
1706 do_cleanups (old_chain);
1711 ps_lsetxmmregs (struct ps_prochandle *ph, lwpid_t lwpid,
1712 const char *xmmregs)
1714 struct cleanup *old_chain;
1716 old_chain = save_inferior_ptid ();
1717 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1718 i387_supply_fxsave (current_regcache, -1, xmmregs);
1719 target_store_registers (-1);
1720 do_cleanups (old_chain);
1726 ps_lstop(struct ps_prochandle *ph, lwpid_t lwpid)
1728 if (ptrace (PT_SUSPEND, lwpid, 0, 0) == -1)
1734 ps_lcontinue(struct ps_prochandle *ph, lwpid_t lwpid)
1736 if (ptrace (PT_RESUME, lwpid, 0, 0) == -1)
1742 ps_linfo(struct ps_prochandle *ph, lwpid_t lwpid, void *info)
1744 if (fbsd_thread_core) {
1745 /* XXX should verify lwpid and make a pseudo lwp info */
1746 memset(info, 0, sizeof(struct ptrace_lwpinfo));
1750 if (ptrace (PT_LWPINFO, lwpid, info, sizeof(struct ptrace_lwpinfo)) == -1)