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)
1303 if (!fbsd_thread_active || !IS_THREAD(inferior_ptid))
1306 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
1310 err = td_thr_get_info_p (&th, &ti);
1314 printf_filtered("signal mask:\n");
1315 fbsd_print_sigset(&ti.ti_sigmask);
1316 printf_filtered("signal pending:\n");
1317 fbsd_print_sigset(&ti.ti_pending);
1321 ignore (CORE_ADDR addr, char *contents)
1327 fbsd_core_open (char *filename, int from_tty)
1331 fbsd_thread_core = 1;
1333 orig_core_ops.to_open (filename, from_tty);
1335 if (fbsd_thread_present)
1337 err = td_ta_new_p (&proc_handle, &thread_agent);
1340 proc_handle.pid = elf_tdata (core_bfd)->core_pid;
1341 fbsd_thread_activate ();
1344 error ("fbsd_core_open: td_ta_new: %s", thread_db_err_str (err));
1349 fbsd_core_close (int quitting)
1351 orig_core_ops.to_close (quitting);
1355 fbsd_core_detach (char *args, int from_tty)
1357 if (fbsd_thread_active)
1358 fbsd_thread_deactivate ();
1359 unpush_target (&fbsd_thread_ops);
1360 orig_core_ops.to_detach (args, from_tty);
1362 /* Clear gdb solib information and symbol file
1363 cache, so that after detach and re-attach, new_objfile
1364 hook will be called */
1366 symbol_file_clear(0);
1370 fbsd_core_files_info (struct target_ops *ignore)
1372 orig_core_ops.to_files_info (ignore);
1376 init_fbsd_core_ops (void)
1378 fbsd_core_ops.to_shortname = "FreeBSD-core";
1379 fbsd_core_ops.to_longname = "FreeBSD multithreaded core dump file";
1380 fbsd_core_ops.to_doc =
1381 "Use a core file as a target. Specify the filename of the core file.";
1382 fbsd_core_ops.to_open = fbsd_core_open;
1383 fbsd_core_ops.to_close = fbsd_core_close;
1384 fbsd_core_ops.to_attach = 0;
1385 fbsd_core_ops.to_post_attach = 0;
1386 fbsd_core_ops.to_detach = fbsd_core_detach;
1387 /* fbsd_core_ops.to_resume = 0; */
1388 /* fbsd_core_ops.to_wait = 0; */
1389 fbsd_core_ops.to_fetch_registers = fbsd_thread_fetch_registers;
1390 /* fbsd_core_ops.to_store_registers = 0; */
1391 /* fbsd_core_ops.to_prepare_to_store = 0; */
1392 fbsd_core_ops.to_xfer_memory = fbsd_thread_xfer_memory;
1393 fbsd_core_ops.to_files_info = fbsd_core_files_info;
1394 fbsd_core_ops.to_insert_breakpoint = ignore;
1395 fbsd_core_ops.to_remove_breakpoint = ignore;
1396 /* fbsd_core_ops.to_lookup_symbol = 0; */
1397 fbsd_core_ops.to_create_inferior = fbsd_thread_create_inferior;
1398 fbsd_core_ops.to_stratum = core_stratum;
1399 fbsd_core_ops.to_has_all_memory = 0;
1400 fbsd_core_ops.to_has_memory = 1;
1401 fbsd_core_ops.to_has_stack = 1;
1402 fbsd_core_ops.to_has_registers = 1;
1403 fbsd_core_ops.to_has_execution = 0;
1404 fbsd_core_ops.to_has_thread_control = tc_none;
1405 fbsd_core_ops.to_thread_alive = fbsd_thread_alive;
1406 fbsd_core_ops.to_pid_to_str = fbsd_thread_pid_to_str;
1407 fbsd_core_ops.to_find_new_threads = fbsd_thread_find_new_threads;
1408 fbsd_core_ops.to_sections = 0;
1409 fbsd_core_ops.to_sections_end = 0;
1410 fbsd_core_ops.to_magic = OPS_MAGIC;
1414 init_fbsd_thread_ops (void)
1416 fbsd_thread_ops.to_shortname = "freebsd-threads";
1417 fbsd_thread_ops.to_longname = "FreeBSD multithreaded child process.";
1418 fbsd_thread_ops.to_doc = "FreeBSD threads support.";
1419 fbsd_thread_ops.to_attach = fbsd_thread_attach;
1420 fbsd_thread_ops.to_detach = fbsd_thread_detach;
1421 fbsd_thread_ops.to_post_attach = fbsd_thread_post_attach;
1422 fbsd_thread_ops.to_resume = fbsd_thread_resume;
1423 fbsd_thread_ops.to_wait = fbsd_thread_wait;
1424 fbsd_thread_ops.to_fetch_registers = fbsd_thread_fetch_registers;
1425 fbsd_thread_ops.to_store_registers = fbsd_thread_store_registers;
1426 fbsd_thread_ops.to_xfer_memory = fbsd_thread_xfer_memory;
1427 fbsd_thread_ops.to_files_info = fbsd_thread_files_info;
1428 fbsd_thread_ops.to_kill = fbsd_thread_kill;
1429 fbsd_thread_ops.to_create_inferior = fbsd_thread_create_inferior;
1430 fbsd_thread_ops.to_post_startup_inferior = fbsd_thread_post_startup_inferior;
1431 fbsd_thread_ops.to_mourn_inferior = fbsd_thread_mourn_inferior;
1432 fbsd_thread_ops.to_can_run = fbsd_thread_can_run;
1433 fbsd_thread_ops.to_thread_alive = fbsd_thread_alive;
1434 fbsd_thread_ops.to_find_new_threads = fbsd_thread_find_new_threads;
1435 fbsd_thread_ops.to_pid_to_str = fbsd_thread_pid_to_str;
1436 fbsd_thread_ops.to_stratum = thread_stratum;
1437 fbsd_thread_ops.to_has_thread_control = tc_none;
1438 fbsd_thread_ops.to_has_all_memory = 1;
1439 fbsd_thread_ops.to_has_memory = 1;
1440 fbsd_thread_ops.to_has_stack = 1;
1441 fbsd_thread_ops.to_has_registers = 1;
1442 fbsd_thread_ops.to_has_execution = 1;
1443 fbsd_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
1444 fbsd_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
1445 fbsd_thread_ops.to_get_thread_local_address = fbsd_thread_get_local_address;
1446 fbsd_thread_ops.to_magic = OPS_MAGIC;
1450 thread_db_load (void)
1455 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
1459 #define resolve(X) \
1460 if (!(X##_p = dlsym (handle, #X))) \
1465 resolve(td_ta_delete);
1466 resolve(td_ta_map_id2thr);
1467 resolve(td_ta_map_lwp2thr);
1468 resolve(td_ta_thr_iter);
1469 resolve(td_thr_get_info);
1470 #ifdef PT_GETXMMREGS
1471 resolve(td_thr_getxmmregs);
1473 resolve(td_thr_getfpregs);
1474 resolve(td_thr_getgregs);
1475 #ifdef PT_GETXMMREGS
1476 resolve(td_thr_setxmmregs);
1478 resolve(td_thr_setfpregs);
1479 resolve(td_thr_setgregs);
1480 resolve(td_thr_sstep);
1481 resolve(td_ta_tsd_iter);
1482 resolve(td_thr_dbsuspend);
1483 resolve(td_thr_dbresume);
1484 resolve(td_thr_tls_get_addr);
1486 /* Initialize the library. */
1490 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err));
1494 /* These are not essential. */
1495 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
1496 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
1497 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
1498 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
1499 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
1504 /* we suppress the call to add_target of core_ops in corelow because
1505 if there are two targets in the stratum core_stratum, find_core_target
1506 won't know which one to return. see corelow.c for an additonal
1507 comment on coreops_suppress_target. */
1509 int coreops_suppress_target = 1;
1511 /* similarly we allow this target to be completely skipped. This is used
1512 by kgdb which uses its own core target. */
1514 int fbsdcoreops_suppress_target;
1517 _initialize_thread_db (void)
1520 if (fbsdcoreops_suppress_target)
1522 init_fbsd_thread_ops ();
1523 init_fbsd_core_ops ();
1525 if (thread_db_load ())
1527 add_target (&fbsd_thread_ops);
1529 /* "thread tsd" command */
1530 add_cmd ("tsd", class_run, fbsd_thread_tsd_cmd,
1531 "Show the thread-specific data keys and destructors "
1532 "for the process.\n",
1535 add_cmd ("signal", class_run, fbsd_thread_signal_cmd,
1536 "Show the thread signal info.\n",
1539 memcpy (&orig_core_ops, &core_ops, sizeof (struct target_ops));
1540 memcpy (&core_ops, &fbsd_core_ops, sizeof (struct target_ops));
1541 add_target (&core_ops);
1543 /* Add ourselves to objfile event chain. */
1544 target_new_objfile_chain = target_new_objfile_hook;
1545 target_new_objfile_hook = fbsd_thread_new_objfile;
1547 child_suppress_run = 1;
1551 fprintf_unfiltered (gdb_stderr,
1552 "[GDB will not be able to debug user-mode threads: %s]\n", dlerror());
1554 /* allow the user to debug non-threaded core files */
1555 add_target (&core_ops);
1559 /* proc service functions */
1561 ps_plog (const char *fmt, ...)
1565 va_start (args, fmt);
1566 vfprintf_filtered (gdb_stderr, fmt, args);
1571 ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj,
1572 const char *name, psaddr_t *sym_addr)
1574 struct minimal_symbol *ms;
1577 ms = lookup_minimal_symbol (name, NULL, NULL);
1581 addr = SYMBOL_VALUE_ADDRESS (ms);
1582 store_typed_address(sym_addr, builtin_type_void_data_ptr, addr);
1587 ps_pread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len)
1589 int err = target_read_memory (
1590 extract_typed_address(&addr, builtin_type_void_data_ptr), buf, len);
1591 return (err == 0 ? PS_OK : PS_ERR);
1595 ps_pwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf,
1598 int err = target_write_memory (
1599 extract_typed_address(&addr, builtin_type_void_data_ptr), (void *)buf, len);
1600 return (err == 0 ? PS_OK : PS_ERR);
1604 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
1606 struct cleanup *old_chain;
1608 old_chain = save_inferior_ptid ();
1610 /* XXX: Target operation isn't lwp aware: replace pid with lwp */
1611 inferior_ptid = BUILD_LWP (0, lwpid);
1613 target_fetch_registers (-1);
1614 fill_gregset (gregset, -1);
1615 do_cleanups (old_chain);
1620 ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
1622 struct cleanup *old_chain;
1624 old_chain = save_inferior_ptid ();
1625 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1626 supply_gregset (gregset);
1627 target_store_registers (-1);
1628 do_cleanups (old_chain);
1633 ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset)
1635 struct cleanup *old_chain;
1637 old_chain = save_inferior_ptid ();
1638 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1639 target_fetch_registers (-1);
1640 fill_fpregset (fpregset, -1);
1641 do_cleanups (old_chain);
1646 ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
1647 const prfpregset_t *fpregset)
1649 struct cleanup *old_chain;
1651 old_chain = save_inferior_ptid ();
1652 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1653 supply_fpregset (fpregset);
1654 target_store_registers (-1);
1655 do_cleanups (old_chain);
1659 #ifdef PT_GETXMMREGS
1661 ps_lgetxmmregs (struct ps_prochandle *ph, lwpid_t lwpid, char *xmmregs)
1663 struct cleanup *old_chain;
1665 old_chain = save_inferior_ptid ();
1666 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1667 target_fetch_registers (-1);
1668 i387_fill_fxsave (xmmregs, -1);
1669 do_cleanups (old_chain);
1674 ps_lsetxmmregs (struct ps_prochandle *ph, lwpid_t lwpid,
1675 const char *xmmregs)
1677 struct cleanup *old_chain;
1679 old_chain = save_inferior_ptid ();
1680 inferior_ptid = BUILD_LWP (lwpid, PIDGET (inferior_ptid));
1681 i387_supply_fxsave (current_regcache, -1, xmmregs);
1682 target_store_registers (-1);
1683 do_cleanups (old_chain);
1689 ps_lstop(struct ps_prochandle *ph, lwpid_t lwpid)
1691 if (ptrace (PT_SUSPEND, lwpid, 0, 0) == -1)
1697 ps_lcontinue(struct ps_prochandle *ph, lwpid_t lwpid)
1699 if (ptrace (PT_RESUME, lwpid, 0, 0) == -1)
1705 ps_linfo(struct ps_prochandle *ph, lwpid_t lwpid, void *info)
1707 if (fbsd_thread_core) {
1708 /* XXX should verify lwpid and make a pseudo lwp info */
1709 memset(info, 0, sizeof(struct ptrace_lwpinfo));
1713 if (ptrace (PT_LWPINFO, lwpid, info, sizeof(struct ptrace_lwpinfo)) == -1)