2 * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/linker_set.h>
37 #include <sys/ptrace.h>
38 #include <proc_service.h>
39 #include <thread_db.h>
41 #include "libpthread_db.h"
44 #define P2T(c) ps2td(c)
46 static void pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp);
47 static int pt_validate(const td_thrhandle_t *th);
64 return TD_NOLIBTHREAD;
73 pt_map_thread(const td_thragent_t *const_ta, psaddr_t pt, enum pt_type type)
75 td_thragent_t *ta = __DECONST(td_thragent_t *, const_ta);
81 for (i = 1; i < ta->map_len; ++i) {
82 if (ta->map[i].type == PT_NONE) {
85 } else if (ta->map[i].type == type && ta->map[i].thr == pt) {
91 if (ta->map_len == 0) {
92 ta->map = calloc(20, sizeof(struct pt_map));
98 new = reallocarray(ta->map, ta->map_len,
99 2 * sizeof(struct pt_map));
102 memset(new + ta->map_len, '\0', ta->map_len *
103 sizeof(struct pt_map));
110 ta->map[first].type = type;
111 ta->map[first].thr = pt;
123 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
125 #define LOOKUP_SYM(proc, sym, addr) \
126 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \
128 TDBG("can not find symbol: %s\n", sym); \
129 ret = TD_NOLIBTHREAD; \
133 #define LOOKUP_VAL(proc, sym, val) \
134 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
136 TDBG("can not find symbol: %s\n", sym); \
137 ret = TD_NOLIBTHREAD; \
140 ret = ps_pread(proc, vaddr, val, sizeof(int)); \
142 TDBG("can not read value of %s\n", sym);\
143 ret = TD_NOLIBTHREAD; \
154 ta = malloc(sizeof(td_thragent_t));
159 ta->thread_activated = 0;
163 LOOKUP_SYM(ph, "_libkse_debug", &ta->libkse_debug_addr);
164 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr);
165 LOOKUP_SYM(ph, "_thread_activated", &ta->thread_activated_addr);
166 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
167 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr);
168 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv);
169 LOOKUP_VAL(ph, "_thread_off_kse_locklevel", &ta->thread_off_kse_locklevel);
170 LOOKUP_VAL(ph, "_thread_off_kse", &ta->thread_off_kse);
171 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex);
172 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags);
173 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key);
174 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb);
175 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap);
176 LOOKUP_VAL(ph, "_thread_off_tmbx", &ta->thread_off_tmbx);
177 LOOKUP_VAL(ph, "_thread_off_thr_locklevel", &ta->thread_off_thr_locklevel);
178 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next);
179 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state);
180 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys);
181 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
182 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
183 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
184 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
185 LOOKUP_VAL(ph, "_thread_off_sigmask", &ta->thread_off_sigmask);
186 LOOKUP_VAL(ph, "_thread_off_sigpend", &ta->thread_off_sigpend);
189 * If this fails it probably means we're debugging a core file and
192 ps_pwrite(ph, ta->libkse_debug_addr, &dbg, sizeof(int));
202 pt_ta_delete(td_thragent_t *ta)
210 * Error returns from this write are not really a problem;
211 * the process doesn't exist any more.
213 ps_pwrite(ta->ph, ta->libkse_debug_addr, &dbg, sizeof(int));
221 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
224 psaddr_t pt, tcb_addr;
230 if (id < 0 || id >= (long)ta->map_len || ta->map[id].type == PT_NONE)
233 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
236 if (ta->map[id].type == PT_LWP) {
238 * if we are referencing a lwp, make sure it was not already
239 * mapped to user thread.
242 ret = thr_pread_ptr(ta, pt + ta->thread_off_tcb,
246 ret = thr_pread_int(ta, tcb_addr + ta->thread_off_tmbx +
247 offsetof(struct kse_thr_mailbox, tm_lwp), &lwp);
251 * If the lwp was already mapped to userland thread,
252 * we shouldn't reference it directly in future.
254 if (lwp == ta->map[id].lwp) {
255 ta->map[id].type = PT_NONE;
258 /* get next thread */
259 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
264 ret = ps_lgetregs(ta->ph, ta->map[id].lwp, gregs);
266 /* no longer exists */
267 ta->map[id].type = PT_NONE;
271 while (pt != 0 && ta->map[id].thr != pt) {
272 ret = thr_pread_ptr(ta, pt + ta->thread_off_tcb,
276 /* get next thread */
277 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
283 /* no longer exists */
284 ta->map[id].type = PT_NONE;
295 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
297 psaddr_t pt, tcb_addr;
303 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
307 ret = thr_pread_ptr(ta, pt + ta->thread_off_tcb, &tcb_addr);
310 ret = thr_pread_int(ta, tcb_addr + ta->thread_off_tmbx +
311 offsetof(struct kse_thr_mailbox, tm_lwp), &lwp1);
316 th->th_tid = pt_map_thread(ta, pt, PT_USER);
317 if (th->th_tid == -1)
319 pt_unmap_lwp(ta, lwp);
324 /* get next thread */
325 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
334 pt_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
335 void *cbdata_p, td_thr_state_e state __unused, int ti_pri __unused,
336 sigset_t *ti_sigmask_p __unused, unsigned int ti_user_flags __unused)
345 pserr = ps_pread(ta->ph, ta->thread_activated_addr, &activated,
352 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
357 th.th_tid = pt_map_thread(ta, pt, PT_USER);
359 /* should we unmap lwp here ? */
362 if ((*callback)(&th, cbdata_p))
364 /* get next thread */
365 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
373 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
377 int i, ret, allocated;
381 keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
382 if (keytable == NULL)
384 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
385 ta->thread_max_keys * ta->thread_size_key);
390 for (i = 0; i < ta->thread_max_keys; i++) {
391 allocated = *(int *)(void *)((uintptr_t)keytable +
392 i * ta->thread_size_key + ta->thread_off_key_allocated);
393 destructor = *(void **)(void *)((uintptr_t)keytable +
394 i * ta->thread_size_key + ta->thread_off_key_destructor);
396 ret = (ki)(i, destructor, arg);
408 pt_ta_event_addr(const td_thragent_t *ta __unused, td_event_e event __unused,
409 td_notify_t *ptr __unused)
416 pt_ta_set_event(const td_thragent_t *ta __unused,
417 td_thr_events_t *events __unused)
424 pt_ta_clear_event(const td_thragent_t *ta __unused,
425 td_thr_events_t *events __unused)
432 pt_ta_event_getmsg(const td_thragent_t *ta __unused,
433 td_event_msg_t *msg __unused)
440 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
442 const td_thragent_t *ta = th->th_ta;
443 psaddr_t tcb_addr, tmbx_addr, ptr;
446 int attrflags, locklevel, ret;
450 ret = pt_validate(th);
454 if (ta->map[th->th_tid].type == PT_LWP) {
456 ret = ps_lstop(ta->ph, ta->map[th->th_tid].lwp);
458 ret = ps_lcontinue(ta->ph, ta->map[th->th_tid].lwp);
462 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
463 ta->thread_off_attr_flags,
464 &attrflags, sizeof(attrflags));
467 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
469 &tcb_addr, sizeof(tcb_addr));
472 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
473 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
474 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
479 /* don't suspend signal thread */
480 if (attrflags & 0x200)
482 if (attrflags & PTHREAD_SCOPE_SYSTEM) {
484 * don't suspend system scope thread if it is holding
485 * some low level locks
487 ptr = ta->map[th->th_tid].thr + ta->thread_off_kse;
488 ret = ps_pread(ta->ph, ptr, &ptr, sizeof(ptr));
491 ret = ps_pread(ta->ph, ptr + ta->thread_off_kse_locklevel,
492 &locklevel, sizeof(int));
495 if (locklevel <= 0) {
496 ptr = ta->map[th->th_tid].thr +
497 ta->thread_off_thr_locklevel;
498 ret = ps_pread(ta->ph, ptr, &locklevel,
505 ret = ps_lstop(ta->ph, lwp);
507 ret = ps_lcontinue(ta->ph, lwp);
513 struct ptrace_lwpinfo pl;
515 if (ps_linfo(ta->ph, lwp, (caddr_t)&pl))
518 if (!(pl.pl_flags & PL_FLAG_BOUND))
519 ret = ps_lstop(ta->ph, lwp);
521 ret = ps_lcontinue(ta->ph, lwp);
529 ret = ps_pread(ta->ph,
530 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
531 &dflags, sizeof(dflags));
535 dflags |= TMDF_SUSPEND;
537 dflags &= ~TMDF_SUSPEND;
538 ret = ps_pwrite(ta->ph,
539 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
540 &dflags, sizeof(dflags));
545 pt_thr_dbresume(const td_thrhandle_t *th)
549 return pt_dbsuspend(th, 0);
553 pt_thr_dbsuspend(const td_thrhandle_t *th)
557 return pt_dbsuspend(th, 1);
561 pt_thr_validate(const td_thrhandle_t *th)
568 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid,
574 pt_thr_old_get_info(const td_thrhandle_t *th, td_old_thrinfo_t *info)
576 const td_thragent_t *ta = th->th_ta;
577 struct ptrace_lwpinfo linfo;
587 bzero(info, sizeof(*info));
588 ret = pt_validate(th);
592 memset(info, 0, sizeof(*info));
593 if (ta->map[th->th_tid].type == PT_LWP) {
594 info->ti_type = TD_THR_SYSTEM;
595 info->ti_lid = ta->map[th->th_tid].lwp;
596 info->ti_tid = th->th_tid;
597 info->ti_state = TD_THR_RUN;
598 info->ti_type = TD_THR_SYSTEM;
602 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
603 ta->thread_off_attr_flags,
604 &attrflags, sizeof(attrflags));
607 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
608 &tcb_addr, sizeof(tcb_addr));
611 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_state,
612 &state, sizeof(state));
613 ret = ps_pread(ta->ph,
614 tcb_addr + ta->thread_off_tmbx +
615 offsetof(struct kse_thr_mailbox, tm_lwp),
616 &info->ti_lid, sizeof(lwpid_t));
619 ret = ps_pread(ta->ph,
620 tcb_addr + ta->thread_off_tmbx +
621 offsetof(struct kse_thr_mailbox, tm_dflags),
622 &dflags, sizeof(dflags));
625 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_tmbx +
626 offsetof(struct kse_thr_mailbox, tm_lwp), &lwp, sizeof(lwpid_t));
629 info->ti_ta_p = th->th_ta;
630 info->ti_tid = th->th_tid;
632 if (attrflags & PTHREAD_SCOPE_SYSTEM) {
633 ret = ps_linfo(ta->ph, lwp, &linfo);
635 info->ti_sigmask = linfo.pl_sigmask;
636 info->ti_pending = linfo.pl_siglist;
640 ret = ps_pread(ta->ph,
641 ta->map[th->th_tid].thr + ta->thread_off_sigmask,
642 &info->ti_sigmask, sizeof(sigset_t));
645 ret = ps_pread(ta->ph,
646 ta->map[th->th_tid].thr + ta->thread_off_sigpend,
647 &info->ti_pending, sizeof(sigset_t));
652 if (state == ta->thread_state_running)
653 info->ti_state = TD_THR_RUN;
654 else if (state == ta->thread_state_zoombie)
655 info->ti_state = TD_THR_ZOMBIE;
657 info->ti_state = TD_THR_SLEEP;
658 info->ti_db_suspended = ((dflags & TMDF_SUSPEND) != 0);
659 info->ti_type = TD_THR_USER;
664 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
668 e = pt_thr_old_get_info(th, (td_old_thrinfo_t *)info);
669 bzero(&info->ti_siginfo, sizeof(info->ti_siginfo));
675 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
677 const td_thragent_t *ta = th->th_ta;
678 struct kse_thr_mailbox tmbx;
679 psaddr_t tcb_addr, tmbx_addr, ptr;
687 ret = pt_validate(th);
691 if (ta->map[th->th_tid].type == PT_LWP) {
692 ret = ps_lgetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
696 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
697 &tcb_addr, sizeof(tcb_addr));
700 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
701 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
702 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
706 ret = ps_lgetxmmregs(ta->ph, lwp, fxsave);
710 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
713 pt_ucontext_to_fxsave(&tmbx.tm_context, fxsave);
719 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
721 const td_thragent_t *ta = th->th_ta;
722 struct kse_thr_mailbox tmbx;
723 psaddr_t tcb_addr, tmbx_addr, ptr;
729 ret = pt_validate(th);
733 if (ta->map[th->th_tid].type == PT_LWP) {
734 ret = ps_lgetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
738 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
739 &tcb_addr, sizeof(tcb_addr));
742 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
743 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
744 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
748 ret = ps_lgetfpregs(ta->ph, lwp, fpregs);
752 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
755 pt_ucontext_to_fpreg(&tmbx.tm_context, fpregs);
760 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
762 const td_thragent_t *ta = th->th_ta;
763 struct kse_thr_mailbox tmbx;
764 psaddr_t tcb_addr, tmbx_addr, ptr;
770 ret = pt_validate(th);
774 if (ta->map[th->th_tid].type == PT_LWP) {
775 ret = ps_lgetregs(ta->ph,
776 ta->map[th->th_tid].lwp, gregs);
780 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
781 &tcb_addr, sizeof(tcb_addr));
784 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
785 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
786 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
790 ret = ps_lgetregs(ta->ph, lwp, gregs);
793 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
796 pt_ucontext_to_reg(&tmbx.tm_context, gregs);
802 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
804 const td_thragent_t *ta = th->th_ta;
805 struct kse_thr_mailbox tmbx;
806 psaddr_t tcb_addr, tmbx_addr, ptr;
814 ret = pt_validate(th);
818 if (ta->map[th->th_tid].type == PT_LWP) {
819 ret = ps_lsetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
823 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
825 &tcb_addr, sizeof(tcb_addr));
828 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
829 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
830 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
834 ret = ps_lsetxmmregs(ta->ph, lwp, fxsave);
838 * Read a copy of context, this makes sure that registers
839 * not covered by structure reg won't be clobbered
841 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
845 pt_fxsave_to_ucontext(fxsave, &tmbx.tm_context);
846 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
852 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
854 const td_thragent_t *ta = th->th_ta;
855 struct kse_thr_mailbox tmbx;
856 psaddr_t tcb_addr, tmbx_addr, ptr;
862 ret = pt_validate(th);
866 if (ta->map[th->th_tid].type == PT_LWP) {
867 ret = ps_lsetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
871 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
873 &tcb_addr, sizeof(tcb_addr));
876 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
877 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
878 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
882 ret = ps_lsetfpregs(ta->ph, lwp, fpregs);
886 * Read a copy of context, this makes sure that registers
887 * not covered by structure reg won't be clobbered
889 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
893 pt_fpreg_to_ucontext(fpregs, &tmbx.tm_context);
894 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
899 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
901 const td_thragent_t *ta = th->th_ta;
902 struct kse_thr_mailbox tmbx;
903 psaddr_t tcb_addr, tmbx_addr, ptr;
909 ret = pt_validate(th);
913 if (ta->map[th->th_tid].type == PT_LWP) {
914 ret = ps_lsetregs(ta->ph, ta->map[th->th_tid].lwp, gregs);
918 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
920 &tcb_addr, sizeof(tcb_addr));
923 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
924 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
925 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
929 ret = ps_lsetregs(ta->ph, lwp, gregs);
934 * Read a copy of context, make sure that registers
935 * not covered by structure reg won't be clobbered
937 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
940 pt_reg_to_ucontext(gregs, &tmbx.tm_context);
941 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
946 pt_thr_event_enable(const td_thrhandle_t *th __unused, int en __unused)
953 pt_thr_set_event(const td_thrhandle_t *th __unused,
954 td_thr_events_t *setp __unused)
961 pt_thr_clear_event(const td_thrhandle_t *th __unused,
962 td_thr_events_t *setp __unused)
969 pt_thr_event_getmsg(const td_thrhandle_t *th __unused,
970 td_event_msg_t *msg __unused)
977 pt_thr_sstep(const td_thrhandle_t *th, int step)
979 const td_thragent_t *ta = th->th_ta;
980 struct kse_thr_mailbox tmbx;
982 psaddr_t tcb_addr, tmbx_addr;
989 ret = pt_validate(th);
993 if (ta->map[th->th_tid].type == PT_LWP)
996 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
998 &tcb_addr, sizeof(tcb_addr));
1002 /* Clear or set single step flag in thread mailbox */
1003 ret = ps_pread(ta->ph,
1004 tcb_addr + ta->thread_off_tmbx +
1005 offsetof(struct kse_thr_mailbox, tm_dflags),
1006 &dflags, sizeof(uint32_t));
1010 dflags |= TMDF_SSTEP;
1012 dflags &= ~TMDF_SSTEP;
1013 ret = ps_pwrite(ta->ph,
1014 tcb_addr + ta->thread_off_tmbx +
1015 offsetof(struct kse_thr_mailbox, tm_dflags),
1016 &dflags, sizeof(uint32_t));
1020 ret = ps_pread(ta->ph,
1021 tcb_addr + ta->thread_off_tmbx +
1022 offsetof(struct kse_thr_mailbox, tm_lwp),
1023 &lwp, sizeof(lwpid_t));
1029 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
1031 * context is in userland, some architectures store
1032 * single step status in registers, we should change
1035 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
1037 pt_ucontext_to_reg(&tmbx.tm_context, ®s);
1038 /* only write out if it is really changed. */
1039 if (pt_reg_sstep(®s, step) != 0) {
1040 pt_reg_to_ucontext(®s, &tmbx.tm_context);
1041 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx,
1049 pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp)
1053 for (i = 0; i < ta->map_len; ++i) {
1054 if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) {
1055 ta->map[i].type = PT_NONE;
1062 pt_validate(const td_thrhandle_t *th)
1065 if (th->th_tid < 0 || th->th_tid >= (long)th->th_ta->map_len ||
1066 th->th_ta->map[th->th_tid].type == PT_NONE)
1072 pt_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t _linkmap, size_t offset,
1075 const td_thragent_t *ta = th->th_ta;
1076 psaddr_t dtv_addr, obj_entry, tcb_addr;
1079 /* linkmap is a member of Obj_Entry */
1080 obj_entry = _linkmap - ta->thread_off_linkmap;
1082 /* get tlsindex of the object file */
1083 ret = ps_pread(ta->ph,
1084 obj_entry + ta->thread_off_tlsindex,
1085 &tls_index, sizeof(tls_index));
1089 /* get thread tcb */
1090 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
1092 &tcb_addr, sizeof(tcb_addr));
1096 /* get dtv array address */
1097 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
1098 &dtv_addr, sizeof(dtv_addr));
1101 /* now get the object's tls block base address */
1102 ret = ps_pread(ta->ph, dtv_addr + sizeof(void *) * (tls_index + 1),
1103 address, sizeof(*address));
1111 static struct ta_ops libpthread_db_ops = {
1113 .to_ta_clear_event = pt_ta_clear_event,
1114 .to_ta_delete = pt_ta_delete,
1115 .to_ta_event_addr = pt_ta_event_addr,
1116 .to_ta_event_getmsg = pt_ta_event_getmsg,
1117 .to_ta_map_id2thr = pt_ta_map_id2thr,
1118 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr,
1119 .to_ta_new = pt_ta_new,
1120 .to_ta_set_event = pt_ta_set_event,
1121 .to_ta_thr_iter = pt_ta_thr_iter,
1122 .to_ta_tsd_iter = pt_ta_tsd_iter,
1123 .to_thr_clear_event = pt_thr_clear_event,
1124 .to_thr_dbresume = pt_thr_dbresume,
1125 .to_thr_dbsuspend = pt_thr_dbsuspend,
1126 .to_thr_event_enable = pt_thr_event_enable,
1127 .to_thr_event_getmsg = pt_thr_event_getmsg,
1128 .to_thr_old_get_info = pt_thr_old_get_info,
1129 .to_thr_get_info = pt_thr_get_info,
1130 .to_thr_getfpregs = pt_thr_getfpregs,
1131 .to_thr_getgregs = pt_thr_getgregs,
1132 .to_thr_set_event = pt_thr_set_event,
1133 .to_thr_setfpregs = pt_thr_setfpregs,
1134 .to_thr_setgregs = pt_thr_setgregs,
1135 .to_thr_validate = pt_thr_validate,
1136 .to_thr_tls_get_addr = pt_thr_tls_get_addr,
1138 /* FreeBSD specific extensions. */
1139 .to_thr_sstep = pt_thr_sstep,
1141 .to_thr_getxmmregs = pt_thr_getxmmregs,
1142 .to_thr_setxmmregs = pt_thr_setxmmregs,
1146 DATA_SET(__ta_ops, libpthread_db_ops);