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>
38 #include <sys/ptrace.h>
39 #include <proc_service.h>
40 #include <thread_db.h>
42 #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, int type)
75 td_thragent_t *ta = __DECONST(td_thragent_t *, const_ta);
80 for (i = 1; i < ta->map_len; ++i) {
81 if (ta->map[i].type == PT_NONE) {
84 } else if (ta->map[i].type == type && ta->map[i].thr == pt) {
90 if (ta->map_len == 0) {
91 ta->map = calloc(20, sizeof(struct pt_map));
97 new = realloc(ta->map,
98 sizeof(struct pt_map) * ta->map_len * 2);
101 memset(new + ta->map_len, '\0', sizeof(struct pt_map) *
109 ta->map[first].type = type;
110 ta->map[first].thr = pt;
122 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
124 #define LOOKUP_SYM(proc, sym, addr) \
125 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \
127 TDBG("can not find symbol: %s\n", sym); \
128 ret = TD_NOLIBTHREAD; \
132 #define LOOKUP_VAL(proc, sym, val) \
133 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
135 TDBG("can not find symbol: %s\n", sym); \
136 ret = TD_NOLIBTHREAD; \
139 ret = ps_pread(proc, vaddr, val, sizeof(int)); \
141 TDBG("can not read value of %s\n", sym);\
142 ret = TD_NOLIBTHREAD; \
153 ta = malloc(sizeof(td_thragent_t));
158 ta->thread_activated = 0;
162 LOOKUP_SYM(ph, "_libkse_debug", &ta->libkse_debug_addr);
163 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr);
164 LOOKUP_SYM(ph, "_thread_activated", &ta->thread_activated_addr);
165 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
166 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr);
167 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv);
168 LOOKUP_VAL(ph, "_thread_off_kse_locklevel", &ta->thread_off_kse_locklevel);
169 LOOKUP_VAL(ph, "_thread_off_kse", &ta->thread_off_kse);
170 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex);
171 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags);
172 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key);
173 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb);
174 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap);
175 LOOKUP_VAL(ph, "_thread_off_tmbx", &ta->thread_off_tmbx);
176 LOOKUP_VAL(ph, "_thread_off_thr_locklevel", &ta->thread_off_thr_locklevel);
177 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next);
178 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state);
179 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys);
180 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
181 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
182 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
183 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
184 LOOKUP_VAL(ph, "_thread_off_sigmask", &ta->thread_off_sigmask);
185 LOOKUP_VAL(ph, "_thread_off_sigpend", &ta->thread_off_sigpend);
188 * If this fails it probably means we're debugging a core file and
191 ps_pwrite(ph, ta->libkse_debug_addr, &dbg, sizeof(int));
201 pt_ta_delete(td_thragent_t *ta)
209 * Error returns from this write are not really a problem;
210 * the process doesn't exist any more.
212 ps_pwrite(ta->ph, ta->libkse_debug_addr, &dbg, sizeof(int));
220 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
223 TAILQ_HEAD(, pthread) thread_list;
224 psaddr_t pt, tcb_addr;
230 if (id < 0 || id >= ta->map_len || ta->map[id].type == PT_NONE)
232 ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
233 sizeof(thread_list));
236 pt = (psaddr_t)thread_list.tqh_first;
237 if (ta->map[id].type == PT_LWP) {
239 * if we are referencing a lwp, make sure it was not already
240 * mapped to user thread.
243 ret = ps_pread(ta->ph,
244 pt + ta->thread_off_tcb,
245 &tcb_addr, sizeof(tcb_addr));
248 ret = ps_pread(ta->ph,
249 tcb_addr + ta->thread_off_tmbx +
250 offsetof(struct kse_thr_mailbox, tm_lwp),
255 * If the lwp was already mapped to userland thread,
256 * we shouldn't reference it directly in future.
258 if (lwp == ta->map[id].lwp) {
259 ta->map[id].type = PT_NONE;
262 /* get next thread */
263 ret = ps_pread(ta->ph,
264 pt + ta->thread_off_next,
270 ret = ps_lgetregs(ta->ph, ta->map[id].lwp, gregs);
272 /* no longer exists */
273 ta->map[id].type = PT_NONE;
277 while (pt != 0 && ta->map[id].thr != pt) {
278 ret = ps_pread(ta->ph,
279 pt + ta->thread_off_tcb,
280 &tcb_addr, sizeof(tcb_addr));
283 /* get next thread */
284 ret = ps_pread(ta->ph,
285 pt + ta->thread_off_next,
292 /* no longer exists */
293 ta->map[id].type = PT_NONE;
304 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
306 TAILQ_HEAD(, pthread) thread_list;
313 ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
314 sizeof(thread_list));
317 pt = (psaddr_t)thread_list.tqh_first;
319 ret = ps_pread(ta->ph, pt + ta->thread_off_tcb,
323 ptr += ta->thread_off_tmbx +
324 offsetof(struct kse_thr_mailbox, tm_lwp);
325 ret = ps_pread(ta->ph, ptr, &tmp_lwp, sizeof(lwpid_t));
328 if (tmp_lwp == lwp) {
330 th->th_tid = pt_map_thread(ta, pt, PT_USER);
331 if (th->th_tid == -1)
333 pt_unmap_lwp(ta, lwp);
338 /* get next thread */
339 ret = ps_pread(ta->ph,
340 pt + ta->thread_off_next,
350 pt_ta_thr_iter(const td_thragent_t *ta,
351 td_thr_iter_f *callback, void *cbdata_p,
352 td_thr_state_e state, int ti_pri,
353 sigset_t *ti_sigmask_p,
354 unsigned int ti_user_flags)
356 TAILQ_HEAD(, pthread) thread_list;
364 pserr = ps_pread(ta->ph, ta->thread_activated_addr, &activated,
371 pserr = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
372 sizeof(thread_list));
375 pt = (psaddr_t)thread_list.tqh_first;
378 th.th_tid = pt_map_thread(ta, pt, PT_USER);
380 /* should we unmap lwp here ? */
383 if ((*callback)(&th, cbdata_p))
385 /* get next thread */
386 pserr = ps_pread(ta->ph,
387 pt + ta->thread_off_next, &pt,
396 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
400 int i, ret, allocated;
404 keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
405 if (keytable == NULL)
407 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
408 ta->thread_max_keys * ta->thread_size_key);
413 for (i = 0; i < ta->thread_max_keys; i++) {
414 allocated = *(int *)(keytable + i * ta->thread_size_key +
415 ta->thread_off_key_allocated);
416 destructor = *(void **)(keytable + i * ta->thread_size_key +
417 ta->thread_off_key_destructor);
419 ret = (ki)(i, destructor, arg);
431 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
438 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
445 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
452 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
459 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
461 td_thragent_t *ta = (td_thragent_t *)th->th_ta;
462 psaddr_t tcb_addr, tmbx_addr, ptr;
465 int attrflags, locklevel, ret;
469 ret = pt_validate(th);
473 if (ta->map[th->th_tid].type == PT_LWP) {
475 ret = ps_lstop(ta->ph, ta->map[th->th_tid].lwp);
477 ret = ps_lcontinue(ta->ph, ta->map[th->th_tid].lwp);
481 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
482 ta->thread_off_attr_flags,
483 &attrflags, sizeof(attrflags));
486 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
488 &tcb_addr, sizeof(tcb_addr));
491 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
492 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
493 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
498 /* don't suspend signal thread */
499 if (attrflags & 0x200)
501 if (attrflags & PTHREAD_SCOPE_SYSTEM) {
503 * don't suspend system scope thread if it is holding
504 * some low level locks
506 ptr = ta->map[th->th_tid].thr + ta->thread_off_kse;
507 ret = ps_pread(ta->ph, ptr, &ptr, sizeof(ptr));
510 ret = ps_pread(ta->ph, ptr + ta->thread_off_kse_locklevel,
511 &locklevel, sizeof(int));
514 if (locklevel <= 0) {
515 ptr = ta->map[th->th_tid].thr +
516 ta->thread_off_thr_locklevel;
517 ret = ps_pread(ta->ph, ptr, &locklevel,
524 ret = ps_lstop(ta->ph, lwp);
526 ret = ps_lcontinue(ta->ph, lwp);
532 struct ptrace_lwpinfo pl;
534 if (ps_linfo(ta->ph, lwp, (caddr_t)&pl))
537 if (!(pl.pl_flags & PL_FLAG_BOUND))
538 ret = ps_lstop(ta->ph, lwp);
540 ret = ps_lcontinue(ta->ph, lwp);
548 ret = ps_pread(ta->ph,
549 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
550 &dflags, sizeof(dflags));
554 dflags |= TMDF_SUSPEND;
556 dflags &= ~TMDF_SUSPEND;
557 ret = ps_pwrite(ta->ph,
558 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
559 &dflags, sizeof(dflags));
564 pt_thr_dbresume(const td_thrhandle_t *th)
568 return pt_dbsuspend(th, 0);
572 pt_thr_dbsuspend(const td_thrhandle_t *th)
576 return pt_dbsuspend(th, 1);
580 pt_thr_validate(const td_thrhandle_t *th)
587 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid,
593 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
595 const td_thragent_t *ta = th->th_ta;
596 struct ptrace_lwpinfo linfo;
606 bzero(info, sizeof(*info));
607 ret = pt_validate(th);
611 memset(info, 0, sizeof(*info));
612 if (ta->map[th->th_tid].type == PT_LWP) {
613 info->ti_type = TD_THR_SYSTEM;
614 info->ti_lid = ta->map[th->th_tid].lwp;
615 info->ti_tid = th->th_tid;
616 info->ti_state = TD_THR_RUN;
617 info->ti_type = TD_THR_SYSTEM;
621 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
622 ta->thread_off_attr_flags,
623 &attrflags, sizeof(attrflags));
626 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
627 &tcb_addr, sizeof(tcb_addr));
630 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_state,
631 &state, sizeof(state));
632 ret = ps_pread(ta->ph,
633 tcb_addr + ta->thread_off_tmbx +
634 offsetof(struct kse_thr_mailbox, tm_lwp),
635 &info->ti_lid, sizeof(lwpid_t));
638 ret = ps_pread(ta->ph,
639 tcb_addr + ta->thread_off_tmbx +
640 offsetof(struct kse_thr_mailbox, tm_dflags),
641 &dflags, sizeof(dflags));
644 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_tmbx +
645 offsetof(struct kse_thr_mailbox, tm_lwp), &lwp, sizeof(lwpid_t));
648 info->ti_ta_p = th->th_ta;
649 info->ti_tid = th->th_tid;
651 if (attrflags & PTHREAD_SCOPE_SYSTEM) {
652 ret = ps_linfo(ta->ph, lwp, &linfo);
654 info->ti_sigmask = linfo.pl_sigmask;
655 info->ti_pending = linfo.pl_siglist;
659 ret = ps_pread(ta->ph,
660 ta->map[th->th_tid].thr + ta->thread_off_sigmask,
661 &info->ti_sigmask, sizeof(sigset_t));
664 ret = ps_pread(ta->ph,
665 ta->map[th->th_tid].thr + ta->thread_off_sigpend,
666 &info->ti_pending, sizeof(sigset_t));
671 if (state == ta->thread_state_running)
672 info->ti_state = TD_THR_RUN;
673 else if (state == ta->thread_state_zoombie)
674 info->ti_state = TD_THR_ZOMBIE;
676 info->ti_state = TD_THR_SLEEP;
677 info->ti_db_suspended = ((dflags & TMDF_SUSPEND) != 0);
678 info->ti_type = TD_THR_USER;
684 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
686 const td_thragent_t *ta = th->th_ta;
687 struct kse_thr_mailbox tmbx;
688 psaddr_t tcb_addr, tmbx_addr, ptr;
696 ret = pt_validate(th);
700 if (ta->map[th->th_tid].type == PT_LWP) {
701 ret = ps_lgetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
705 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
706 &tcb_addr, sizeof(tcb_addr));
709 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
710 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
711 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
715 ret = ps_lgetxmmregs(ta->ph, lwp, fxsave);
719 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
722 pt_ucontext_to_fxsave(&tmbx.tm_context, fxsave);
728 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
730 const td_thragent_t *ta = th->th_ta;
731 struct kse_thr_mailbox tmbx;
732 psaddr_t tcb_addr, tmbx_addr, ptr;
738 ret = pt_validate(th);
742 if (ta->map[th->th_tid].type == PT_LWP) {
743 ret = ps_lgetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
747 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
748 &tcb_addr, sizeof(tcb_addr));
751 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
752 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
753 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
757 ret = ps_lgetfpregs(ta->ph, lwp, fpregs);
761 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
764 pt_ucontext_to_fpreg(&tmbx.tm_context, fpregs);
769 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
771 const td_thragent_t *ta = th->th_ta;
772 struct kse_thr_mailbox tmbx;
773 psaddr_t tcb_addr, tmbx_addr, ptr;
779 ret = pt_validate(th);
783 if (ta->map[th->th_tid].type == PT_LWP) {
784 ret = ps_lgetregs(ta->ph,
785 ta->map[th->th_tid].lwp, gregs);
789 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
790 &tcb_addr, sizeof(tcb_addr));
793 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
794 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
795 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
799 ret = ps_lgetregs(ta->ph, lwp, gregs);
802 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
805 pt_ucontext_to_reg(&tmbx.tm_context, gregs);
811 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
813 const td_thragent_t *ta = th->th_ta;
814 struct kse_thr_mailbox tmbx;
815 psaddr_t tcb_addr, tmbx_addr, ptr;
823 ret = pt_validate(th);
827 if (ta->map[th->th_tid].type == PT_LWP) {
828 ret = ps_lsetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
832 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
834 &tcb_addr, sizeof(tcb_addr));
837 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
838 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
839 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
843 ret = ps_lsetxmmregs(ta->ph, lwp, fxsave);
847 * Read a copy of context, this makes sure that registers
848 * not covered by structure reg won't be clobbered
850 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
854 pt_fxsave_to_ucontext(fxsave, &tmbx.tm_context);
855 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
861 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
863 const td_thragent_t *ta = th->th_ta;
864 struct kse_thr_mailbox tmbx;
865 psaddr_t tcb_addr, tmbx_addr, ptr;
871 ret = pt_validate(th);
875 if (ta->map[th->th_tid].type == PT_LWP) {
876 ret = ps_lsetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
880 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
882 &tcb_addr, sizeof(tcb_addr));
885 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
886 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
887 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
891 ret = ps_lsetfpregs(ta->ph, lwp, fpregs);
895 * Read a copy of context, this makes sure that registers
896 * not covered by structure reg won't be clobbered
898 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
902 pt_fpreg_to_ucontext(fpregs, &tmbx.tm_context);
903 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
908 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
910 const td_thragent_t *ta = th->th_ta;
911 struct kse_thr_mailbox tmbx;
912 psaddr_t tcb_addr, tmbx_addr, ptr;
918 ret = pt_validate(th);
922 if (ta->map[th->th_tid].type == PT_LWP) {
923 ret = ps_lsetregs(ta->ph, ta->map[th->th_tid].lwp, gregs);
927 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
929 &tcb_addr, sizeof(tcb_addr));
932 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
933 ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
934 ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
938 ret = ps_lsetregs(ta->ph, lwp, gregs);
943 * Read a copy of context, make sure that registers
944 * not covered by structure reg won't be clobbered
946 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
949 pt_reg_to_ucontext(gregs, &tmbx.tm_context);
950 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
955 pt_thr_event_enable(const td_thrhandle_t *th, int en)
962 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
969 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
976 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
983 pt_thr_sstep(const td_thrhandle_t *th, int step)
985 const td_thragent_t *ta = th->th_ta;
986 struct kse_thr_mailbox tmbx;
988 psaddr_t tcb_addr, tmbx_addr;
995 ret = pt_validate(th);
999 if (ta->map[th->th_tid].type == PT_LWP)
1002 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
1004 &tcb_addr, sizeof(tcb_addr));
1008 /* Clear or set single step flag in thread mailbox */
1009 ret = ps_pread(ta->ph,
1010 tcb_addr + ta->thread_off_tmbx +
1011 offsetof(struct kse_thr_mailbox, tm_dflags),
1012 &dflags, sizeof(uint32_t));
1016 dflags |= TMDF_SSTEP;
1018 dflags &= ~TMDF_SSTEP;
1019 ret = ps_pwrite(ta->ph,
1020 tcb_addr + ta->thread_off_tmbx +
1021 offsetof(struct kse_thr_mailbox, tm_dflags),
1022 &dflags, sizeof(uint32_t));
1026 ret = ps_pread(ta->ph,
1027 tcb_addr + ta->thread_off_tmbx +
1028 offsetof(struct kse_thr_mailbox, tm_lwp),
1029 &lwp, sizeof(lwpid_t));
1035 tmbx_addr = tcb_addr + ta->thread_off_tmbx;
1037 * context is in userland, some architectures store
1038 * single step status in registers, we should change
1041 ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
1043 pt_ucontext_to_reg(&tmbx.tm_context, ®s);
1044 /* only write out if it is really changed. */
1045 if (pt_reg_sstep(®s, step) != 0) {
1046 pt_reg_to_ucontext(®s, &tmbx.tm_context);
1047 ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx,
1055 pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp)
1059 for (i = 0; i < ta->map_len; ++i) {
1060 if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) {
1061 ta->map[i].type = PT_NONE;
1068 pt_validate(const td_thrhandle_t *th)
1071 if (th->th_tid < 0 || th->th_tid >= th->th_ta->map_len ||
1072 th->th_ta->map[th->th_tid].type == PT_NONE)
1078 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
1082 const td_thragent_t *ta = th->th_ta;
1083 psaddr_t tcb_addr, *dtv_addr;
1086 /* linkmap is a member of Obj_Entry */
1087 obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
1089 /* get tlsindex of the object file */
1090 ret = ps_pread(ta->ph,
1091 obj_entry + ta->thread_off_tlsindex,
1092 &tls_index, sizeof(tls_index));
1096 /* get thread tcb */
1097 ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
1099 &tcb_addr, sizeof(tcb_addr));
1103 /* get dtv array address */
1104 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
1105 &dtv_addr, sizeof(dtv_addr));
1108 /* now get the object's tls block base address */
1109 ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
1118 struct ta_ops libpthread_db_ops = {
1120 .to_ta_clear_event = pt_ta_clear_event,
1121 .to_ta_delete = pt_ta_delete,
1122 .to_ta_event_addr = pt_ta_event_addr,
1123 .to_ta_event_getmsg = pt_ta_event_getmsg,
1124 .to_ta_map_id2thr = pt_ta_map_id2thr,
1125 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr,
1126 .to_ta_new = pt_ta_new,
1127 .to_ta_set_event = pt_ta_set_event,
1128 .to_ta_thr_iter = pt_ta_thr_iter,
1129 .to_ta_tsd_iter = pt_ta_tsd_iter,
1130 .to_thr_clear_event = pt_thr_clear_event,
1131 .to_thr_dbresume = pt_thr_dbresume,
1132 .to_thr_dbsuspend = pt_thr_dbsuspend,
1133 .to_thr_event_enable = pt_thr_event_enable,
1134 .to_thr_event_getmsg = pt_thr_event_getmsg,
1135 .to_thr_get_info = pt_thr_get_info,
1136 .to_thr_getfpregs = pt_thr_getfpregs,
1137 .to_thr_getgregs = pt_thr_getgregs,
1138 .to_thr_set_event = pt_thr_set_event,
1139 .to_thr_setfpregs = pt_thr_setfpregs,
1140 .to_thr_setgregs = pt_thr_setgregs,
1141 .to_thr_validate = pt_thr_validate,
1142 .to_thr_tls_get_addr = pt_thr_tls_get_addr,
1144 /* FreeBSD specific extensions. */
1145 .to_thr_sstep = pt_thr_sstep,
1147 .to_thr_getxmmregs = pt_thr_getxmmregs,
1148 .to_thr_setxmmregs = pt_thr_setxmmregs,
1152 DATA_SET(__ta_ops, libpthread_db_ops);