2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Marcel Moolenaar
5 * Copyright (c) 2005 David Xu
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <proc_service.h>
37 #include <sys/types.h>
38 #include <sys/linker_set.h>
39 #include <sys/ptrace.h>
40 #include <thread_db.h>
43 #include "thread_db_int.h"
49 psaddr_t libthr_debug_addr;
50 psaddr_t thread_list_addr;
51 psaddr_t thread_active_threads_addr;
52 psaddr_t thread_keytable_addr;
53 psaddr_t thread_last_event_addr;
54 psaddr_t thread_event_mask_addr;
55 psaddr_t thread_bp_create_addr;
56 psaddr_t thread_bp_death_addr;
58 int thread_off_tlsindex;
59 int thread_off_attr_flags;
62 int thread_off_linkmap;
67 int thread_off_key_allocated;
68 int thread_off_key_destructor;
69 int thread_off_report_events;
70 int thread_off_event_mask;
71 int thread_off_event_buf;
72 int thread_state_zoombie;
73 int thread_state_running;
76 #define P2T(c) ps2td(c)
78 static int pt_validate(const td_thrhandle_t *th);
95 return TD_NOLIBTHREAD;
110 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
112 #define LOOKUP_SYM(proc, sym, addr) \
113 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \
115 TDBG("can not find symbol: %s\n", sym); \
116 ret = TD_NOLIBTHREAD; \
120 #define LOOKUP_VAL(proc, sym, val) \
121 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
123 TDBG("can not find symbol: %s\n", sym); \
124 ret = TD_NOLIBTHREAD; \
127 ret = ps_pread(proc, vaddr, val, sizeof(int)); \
129 TDBG("can not read value of %s\n", sym);\
130 ret = TD_NOLIBTHREAD; \
141 ta = malloc(sizeof(td_thragent_t));
147 LOOKUP_SYM(ph, "_libthr_debug", &ta->libthr_debug_addr);
148 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr);
149 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
150 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr);
151 LOOKUP_SYM(ph, "_thread_last_event", &ta->thread_last_event_addr);
152 LOOKUP_SYM(ph, "_thread_event_mask", &ta->thread_event_mask_addr);
153 LOOKUP_SYM(ph, "_thread_bp_create", &ta->thread_bp_create_addr);
154 LOOKUP_SYM(ph, "_thread_bp_death", &ta->thread_bp_death_addr);
155 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv);
156 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex);
157 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags);
158 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key);
159 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb);
160 LOOKUP_VAL(ph, "_thread_off_tid", &ta->thread_off_tid);
161 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap);
162 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next);
163 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state);
164 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys);
165 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
166 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
167 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
168 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
169 LOOKUP_VAL(ph, "_thread_off_report_events", &ta->thread_off_report_events);
170 LOOKUP_VAL(ph, "_thread_off_event_mask", &ta->thread_off_event_mask);
171 LOOKUP_VAL(ph, "_thread_off_event_buf", &ta->thread_off_event_buf);
174 * If this fails it probably means we're debugging a core file and
177 ps_pwrite(ph, ta->libthr_debug_addr, &dbg, sizeof(int));
187 pt_ta_delete(td_thragent_t *ta)
195 * Error returns from this write are not really a problem;
196 * the process doesn't exist any more.
198 ps_pwrite(ta->ph, ta->libthr_debug_addr, &dbg, sizeof(int));
204 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
214 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
217 /* Iterate through thread list to find pthread */
219 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
224 /* get next thread */
225 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
238 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
240 return (pt_ta_map_id2thr(ta, lwp, th));
244 pt_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
245 void *cbdata_p, td_thr_state_e state __unused, int ti_pri __unused,
246 sigset_t *ti_sigmask_p __unused, unsigned int ti_user_flags __unused)
255 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
259 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
262 if (lwp != 0 && lwp != TERMINATED) {
264 th.th_tid = (thread_t)lwp;
266 if ((*callback)(&th, cbdata_p))
269 /* get next thread */
270 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
278 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
282 int i, ret, allocated;
286 keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
287 if (keytable == NULL)
289 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
290 ta->thread_max_keys * ta->thread_size_key);
295 for (i = 0; i < ta->thread_max_keys; i++) {
296 allocated = *(int *)(void *)((uintptr_t)keytable +
297 i * ta->thread_size_key + ta->thread_off_key_allocated);
298 destructor = *(void **)(void *)((uintptr_t)keytable +
299 i * ta->thread_size_key + ta->thread_off_key_destructor);
301 ret = (ki)(i, destructor, arg);
313 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
320 ptr->type = NOTIFY_BPT;
321 ptr->u.bptaddr = ta->thread_bp_create_addr;
324 ptr->type = NOTIFY_BPT;
325 ptr->u.bptaddr = ta->thread_bp_death_addr;
333 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
335 td_thr_events_t mask;
339 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
344 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
350 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
352 td_thr_events_t mask;
356 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
361 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
367 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
369 static td_thrhandle_t handle;
378 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt);
384 * Take the event pointer, at the time, libthr only reports event
385 * once a time, so it is not a link list.
387 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0);
389 /* Read event info */
390 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
397 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
400 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
405 handle.th_thread = pt;
406 msg->th_p = (uintptr_t)&handle;
411 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
413 const td_thragent_t *ta = th->th_ta;
418 ret = pt_validate(th);
423 ret = ps_lstop(ta->ph, th->th_tid);
425 ret = ps_lcontinue(ta->ph, th->th_tid);
430 pt_thr_dbresume(const td_thrhandle_t *th)
434 return pt_dbsuspend(th, 0);
438 pt_thr_dbsuspend(const td_thrhandle_t *th)
442 return pt_dbsuspend(th, 1);
446 pt_thr_validate(const td_thrhandle_t *th)
453 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, &temp);
458 pt_thr_get_info_common(const td_thrhandle_t *th, td_thrinfo_t *info, int old)
460 const td_thragent_t *ta = th->th_ta;
461 struct ptrace_lwpinfo linfo;
468 bzero(info, sizeof(*info));
469 ret = pt_validate(th);
472 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_state, &state);
475 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_report_events,
477 info->ti_traceme = traceme;
480 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
481 &info->ti_events, sizeof(td_thr_events_t));
484 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
485 &info->ti_tls, sizeof(void *));
486 info->ti_lid = th->th_tid;
487 info->ti_tid = th->th_tid;
488 info->ti_thread = th->th_thread;
489 info->ti_ta_p = th->th_ta;
490 ret = ps_linfo(ta->ph, th->th_tid, &linfo);
492 info->ti_sigmask = linfo.pl_sigmask;
493 info->ti_pending = linfo.pl_siglist;
495 if ((linfo.pl_flags & PL_FLAG_SI) != 0)
496 info->ti_siginfo = linfo.pl_siginfo;
498 bzero(&info->ti_siginfo,
499 sizeof(info->ti_siginfo));
503 if (state == ta->thread_state_running)
504 info->ti_state = TD_THR_RUN;
505 else if (state == ta->thread_state_zoombie)
506 info->ti_state = TD_THR_ZOMBIE;
508 info->ti_state = TD_THR_SLEEP;
509 info->ti_type = TD_THR_USER;
514 pt_thr_old_get_info(const td_thrhandle_t *th, td_old_thrinfo_t *info)
517 return (pt_thr_get_info_common(th, (td_thrinfo_t *)info, 1));
521 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
524 return (pt_thr_get_info_common(th, info, 0));
529 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
531 const td_thragent_t *ta = th->th_ta;
536 ret = pt_validate(th);
540 ret = ps_lgetxmmregs(ta->ph, th->th_tid, fxsave);
546 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
548 const td_thragent_t *ta = th->th_ta;
553 ret = pt_validate(th);
557 ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs);
562 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
564 const td_thragent_t *ta = th->th_ta;
569 ret = pt_validate(th);
573 ret = ps_lgetregs(ta->ph, th->th_tid, gregs);
579 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
581 const td_thragent_t *ta = th->th_ta;
586 ret = pt_validate(th);
590 ret = ps_lsetxmmregs(ta->ph, th->th_tid, fxsave);
596 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
598 const td_thragent_t *ta = th->th_ta;
603 ret = pt_validate(th);
607 ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs);
612 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
614 const td_thragent_t *ta = th->th_ta;
619 ret = pt_validate(th);
623 ret = ps_lsetregs(ta->ph, th->th_tid, gregs);
628 pt_thr_event_enable(const td_thrhandle_t *th, int en)
630 const td_thragent_t *ta = th->th_ta;
634 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events,
640 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
642 const td_thragent_t *ta = th->th_ta;
643 td_thr_events_t mask;
647 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
648 &mask, sizeof(mask));
650 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
651 &mask, sizeof(mask));
656 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
658 const td_thragent_t *ta = th->th_ta;
659 td_thr_events_t mask;
663 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
664 &mask, sizeof(mask));
666 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
667 &mask, sizeof(mask));
672 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
674 static td_thrhandle_t handle;
675 const td_thragent_t *ta = th->th_ta;
676 psaddr_t pt, pt_temp;
683 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt_temp);
687 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
693 * Take the event pointer, at the time, libthr only reports event
694 * once a time, so it is not a link list.
697 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0);
701 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
704 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
709 handle.th_thread = pt;
710 msg->th_p = (uintptr_t)&handle;
715 pt_thr_sstep(const td_thrhandle_t *th, int step __unused)
719 return pt_validate(th);
723 pt_validate(const td_thrhandle_t *th)
726 if (th->th_tid == 0 || th->th_thread == 0)
732 pt_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t _linkmap, size_t offset,
735 const td_thragent_t *ta = th->th_ta;
736 psaddr_t dtv_addr, obj_entry, tcb_addr;
739 /* linkmap is a member of Obj_Entry */
740 obj_entry = _linkmap - ta->thread_off_linkmap;
742 /* get tlsindex of the object file */
743 ret = ps_pread(ta->ph,
744 obj_entry + ta->thread_off_tlsindex,
745 &tls_index, sizeof(tls_index));
750 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
751 &tcb_addr, sizeof(tcb_addr));
755 /* get dtv array address */
756 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
757 &dtv_addr, sizeof(dtv_addr));
760 /* now get the object's tls block base address */
761 ret = ps_pread(ta->ph, dtv_addr + sizeof(void *) * (tls_index+1),
762 address, sizeof(*address));
770 static struct ta_ops libthr_db_ops = {
772 .to_ta_clear_event = pt_ta_clear_event,
773 .to_ta_delete = pt_ta_delete,
774 .to_ta_event_addr = pt_ta_event_addr,
775 .to_ta_event_getmsg = pt_ta_event_getmsg,
776 .to_ta_map_id2thr = pt_ta_map_id2thr,
777 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr,
778 .to_ta_new = pt_ta_new,
779 .to_ta_set_event = pt_ta_set_event,
780 .to_ta_thr_iter = pt_ta_thr_iter,
781 .to_ta_tsd_iter = pt_ta_tsd_iter,
782 .to_thr_clear_event = pt_thr_clear_event,
783 .to_thr_dbresume = pt_thr_dbresume,
784 .to_thr_dbsuspend = pt_thr_dbsuspend,
785 .to_thr_event_enable = pt_thr_event_enable,
786 .to_thr_event_getmsg = pt_thr_event_getmsg,
787 .to_thr_old_get_info = pt_thr_old_get_info,
788 .to_thr_get_info = pt_thr_get_info,
789 .to_thr_getfpregs = pt_thr_getfpregs,
790 .to_thr_getgregs = pt_thr_getgregs,
791 .to_thr_set_event = pt_thr_set_event,
792 .to_thr_setfpregs = pt_thr_setfpregs,
793 .to_thr_setgregs = pt_thr_setgregs,
794 .to_thr_validate = pt_thr_validate,
795 .to_thr_tls_get_addr = pt_thr_tls_get_addr,
797 /* FreeBSD specific extensions. */
798 .to_thr_sstep = pt_thr_sstep,
800 .to_thr_getxmmregs = pt_thr_getxmmregs,
801 .to_thr_setxmmregs = pt_thr_setxmmregs,
805 DATA_SET(__ta_ops, libthr_db_ops);