2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2005 David Xu
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <proc_service.h>
35 #include <sys/types.h>
36 #include <sys/linker_set.h>
37 #include <sys/ptrace.h>
38 #include <thread_db.h>
41 #include "thread_db_int.h"
47 psaddr_t libthr_debug_addr;
48 psaddr_t thread_list_addr;
49 psaddr_t thread_active_threads_addr;
50 psaddr_t thread_keytable_addr;
51 psaddr_t thread_last_event_addr;
52 psaddr_t thread_event_mask_addr;
53 psaddr_t thread_bp_create_addr;
54 psaddr_t thread_bp_death_addr;
56 int thread_off_tlsindex;
57 int thread_off_attr_flags;
60 int thread_off_linkmap;
65 int thread_off_key_allocated;
66 int thread_off_key_destructor;
67 int thread_off_report_events;
68 int thread_off_event_mask;
69 int thread_off_event_buf;
70 int thread_state_zoombie;
71 int thread_state_running;
74 #define P2T(c) ps2td(c)
76 static int pt_validate(const td_thrhandle_t *th);
93 return TD_NOLIBTHREAD;
108 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
110 #define LOOKUP_SYM(proc, sym, addr) \
111 ret = ps_pglobal_lookup(proc, NULL, sym, addr); \
113 TDBG("can not find symbol: %s\n", sym); \
114 ret = TD_NOLIBTHREAD; \
118 #define LOOKUP_VAL(proc, sym, val) \
119 ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
121 TDBG("can not find symbol: %s\n", sym); \
122 ret = TD_NOLIBTHREAD; \
125 ret = ps_pread(proc, vaddr, val, sizeof(int)); \
127 TDBG("can not read value of %s\n", sym);\
128 ret = TD_NOLIBTHREAD; \
139 ta = malloc(sizeof(td_thragent_t));
145 LOOKUP_SYM(ph, "_libthr_debug", &ta->libthr_debug_addr);
146 LOOKUP_SYM(ph, "_thread_list", &ta->thread_list_addr);
147 LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
148 LOOKUP_SYM(ph, "_thread_keytable", &ta->thread_keytable_addr);
149 LOOKUP_SYM(ph, "_thread_last_event", &ta->thread_last_event_addr);
150 LOOKUP_SYM(ph, "_thread_event_mask", &ta->thread_event_mask_addr);
151 LOOKUP_SYM(ph, "_thread_bp_create", &ta->thread_bp_create_addr);
152 LOOKUP_SYM(ph, "_thread_bp_death", &ta->thread_bp_death_addr);
153 LOOKUP_VAL(ph, "_thread_off_dtv", &ta->thread_off_dtv);
154 LOOKUP_VAL(ph, "_thread_off_tlsindex", &ta->thread_off_tlsindex);
155 LOOKUP_VAL(ph, "_thread_off_attr_flags", &ta->thread_off_attr_flags);
156 LOOKUP_VAL(ph, "_thread_size_key", &ta->thread_size_key);
157 LOOKUP_VAL(ph, "_thread_off_tcb", &ta->thread_off_tcb);
158 LOOKUP_VAL(ph, "_thread_off_tid", &ta->thread_off_tid);
159 LOOKUP_VAL(ph, "_thread_off_linkmap", &ta->thread_off_linkmap);
160 LOOKUP_VAL(ph, "_thread_off_next", &ta->thread_off_next);
161 LOOKUP_VAL(ph, "_thread_off_state", &ta->thread_off_state);
162 LOOKUP_VAL(ph, "_thread_max_keys", &ta->thread_max_keys);
163 LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
164 LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
165 LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
166 LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
167 LOOKUP_VAL(ph, "_thread_off_report_events", &ta->thread_off_report_events);
168 LOOKUP_VAL(ph, "_thread_off_event_mask", &ta->thread_off_event_mask);
169 LOOKUP_VAL(ph, "_thread_off_event_buf", &ta->thread_off_event_buf);
172 * If this fails it probably means we're debugging a core file and
175 ps_pwrite(ph, ta->libthr_debug_addr, &dbg, sizeof(int));
185 pt_ta_delete(td_thragent_t *ta)
193 * Error returns from this write are not really a problem;
194 * the process doesn't exist any more.
196 ps_pwrite(ta->ph, ta->libthr_debug_addr, &dbg, sizeof(int));
202 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
212 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
215 /* Iterate through thread list to find pthread */
217 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
222 /* get next thread */
223 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
236 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
238 return (pt_ta_map_id2thr(ta, lwp, th));
242 pt_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
243 void *cbdata_p, td_thr_state_e state __unused, int ti_pri __unused,
244 sigset_t *ti_sigmask_p __unused, unsigned int ti_user_flags __unused)
253 ret = thr_pread_ptr(ta, ta->thread_list_addr, &pt);
257 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
260 if (lwp != 0 && lwp != TERMINATED) {
262 th.th_tid = (thread_t)lwp;
264 if ((*callback)(&th, cbdata_p))
267 /* get next thread */
268 ret = thr_pread_ptr(ta, pt + ta->thread_off_next, &pt);
276 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
280 int i, ret, allocated;
284 keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
285 if (keytable == NULL)
287 ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
288 ta->thread_max_keys * ta->thread_size_key);
293 for (i = 0; i < ta->thread_max_keys; i++) {
294 allocated = *(int *)(void *)((uintptr_t)keytable +
295 i * ta->thread_size_key + ta->thread_off_key_allocated);
296 destructor = *(void **)(void *)((uintptr_t)keytable +
297 i * ta->thread_size_key + ta->thread_off_key_destructor);
299 ret = (ki)(i, destructor, arg);
311 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
318 ptr->type = NOTIFY_BPT;
319 ptr->u.bptaddr = ta->thread_bp_create_addr;
322 ptr->type = NOTIFY_BPT;
323 ptr->u.bptaddr = ta->thread_bp_death_addr;
331 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
333 td_thr_events_t mask;
337 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
342 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
348 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
350 td_thr_events_t mask;
354 ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
359 ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
365 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
367 static td_thrhandle_t handle;
376 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt);
382 * Take the event pointer, at the time, libthr only reports event
383 * once a time, so it is not a link list.
385 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0);
387 /* Read event info */
388 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
395 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
398 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
403 handle.th_thread = pt;
404 msg->th_p = (uintptr_t)&handle;
409 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
411 const td_thragent_t *ta = th->th_ta;
416 ret = pt_validate(th);
421 ret = ps_lstop(ta->ph, th->th_tid);
423 ret = ps_lcontinue(ta->ph, th->th_tid);
428 pt_thr_dbresume(const td_thrhandle_t *th)
432 return pt_dbsuspend(th, 0);
436 pt_thr_dbsuspend(const td_thrhandle_t *th)
440 return pt_dbsuspend(th, 1);
444 pt_thr_validate(const td_thrhandle_t *th)
451 ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, &temp);
456 pt_thr_get_info_common(const td_thrhandle_t *th, td_thrinfo_t *info, int old)
458 const td_thragent_t *ta = th->th_ta;
459 struct ptrace_lwpinfo linfo;
466 bzero(info, sizeof(*info));
467 ret = pt_validate(th);
470 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_state, &state);
473 ret = thr_pread_int(ta, th->th_thread + ta->thread_off_report_events,
475 info->ti_traceme = traceme;
478 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
479 &info->ti_events, sizeof(td_thr_events_t));
482 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
483 &info->ti_tls, sizeof(void *));
484 info->ti_lid = th->th_tid;
485 info->ti_tid = th->th_tid;
486 info->ti_thread = th->th_thread;
487 info->ti_ta_p = th->th_ta;
488 ret = ps_linfo(ta->ph, th->th_tid, &linfo);
490 info->ti_sigmask = linfo.pl_sigmask;
491 info->ti_pending = linfo.pl_siglist;
493 if ((linfo.pl_flags & PL_FLAG_SI) != 0)
494 info->ti_siginfo = linfo.pl_siginfo;
496 bzero(&info->ti_siginfo,
497 sizeof(info->ti_siginfo));
501 if (state == ta->thread_state_running)
502 info->ti_state = TD_THR_RUN;
503 else if (state == ta->thread_state_zoombie)
504 info->ti_state = TD_THR_ZOMBIE;
506 info->ti_state = TD_THR_SLEEP;
507 info->ti_type = TD_THR_USER;
512 pt_thr_old_get_info(const td_thrhandle_t *th, td_old_thrinfo_t *info)
515 return (pt_thr_get_info_common(th, (td_thrinfo_t *)info, 1));
519 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
522 return (pt_thr_get_info_common(th, info, 0));
527 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
529 const td_thragent_t *ta = th->th_ta;
534 ret = pt_validate(th);
538 ret = ps_lgetxmmregs(ta->ph, th->th_tid, fxsave);
544 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
546 const td_thragent_t *ta = th->th_ta;
551 ret = pt_validate(th);
555 ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs);
560 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
562 const td_thragent_t *ta = th->th_ta;
567 ret = pt_validate(th);
571 ret = ps_lgetregs(ta->ph, th->th_tid, gregs);
577 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
579 const td_thragent_t *ta = th->th_ta;
584 ret = pt_validate(th);
588 ret = ps_lsetxmmregs(ta->ph, th->th_tid, fxsave);
594 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
596 const td_thragent_t *ta = th->th_ta;
601 ret = pt_validate(th);
605 ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs);
610 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
612 const td_thragent_t *ta = th->th_ta;
617 ret = pt_validate(th);
621 ret = ps_lsetregs(ta->ph, th->th_tid, gregs);
626 pt_thr_event_enable(const td_thrhandle_t *th, int en)
628 const td_thragent_t *ta = th->th_ta;
632 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events,
638 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
640 const td_thragent_t *ta = th->th_ta;
641 td_thr_events_t mask;
645 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
646 &mask, sizeof(mask));
648 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
649 &mask, sizeof(mask));
654 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
656 const td_thragent_t *ta = th->th_ta;
657 td_thr_events_t mask;
661 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
662 &mask, sizeof(mask));
664 ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
665 &mask, sizeof(mask));
670 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
672 static td_thrhandle_t handle;
673 const td_thragent_t *ta = th->th_ta;
674 psaddr_t pt, pt_temp;
681 ret = thr_pread_ptr(ta, ta->thread_last_event_addr, &pt_temp);
685 ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
691 * Take the event pointer, at the time, libthr only reports event
692 * once a time, so it is not a link list.
695 thr_pwrite_ptr(ta, ta->thread_last_event_addr, 0);
699 ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
702 ret = thr_pread_long(ta, pt + ta->thread_off_tid, &lwp);
707 handle.th_thread = pt;
708 msg->th_p = (uintptr_t)&handle;
713 pt_thr_sstep(const td_thrhandle_t *th, int step __unused)
717 return pt_validate(th);
721 pt_validate(const td_thrhandle_t *th)
724 if (th->th_tid == 0 || th->th_thread == 0)
730 pt_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t _linkmap, size_t offset,
733 const td_thragent_t *ta = th->th_ta;
734 psaddr_t dtv_addr, obj_entry, tcb_addr;
737 /* linkmap is a member of Obj_Entry */
738 obj_entry = _linkmap - ta->thread_off_linkmap;
740 /* get tlsindex of the object file */
741 ret = ps_pread(ta->ph,
742 obj_entry + ta->thread_off_tlsindex,
743 &tls_index, sizeof(tls_index));
748 ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
749 &tcb_addr, sizeof(tcb_addr));
753 /* get dtv array address */
754 ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
755 &dtv_addr, sizeof(dtv_addr));
758 /* now get the object's tls block base address */
759 ret = ps_pread(ta->ph, dtv_addr + sizeof(void *) * (tls_index+1),
760 address, sizeof(*address));
768 static struct ta_ops libthr_db_ops = {
770 .to_ta_clear_event = pt_ta_clear_event,
771 .to_ta_delete = pt_ta_delete,
772 .to_ta_event_addr = pt_ta_event_addr,
773 .to_ta_event_getmsg = pt_ta_event_getmsg,
774 .to_ta_map_id2thr = pt_ta_map_id2thr,
775 .to_ta_map_lwp2thr = pt_ta_map_lwp2thr,
776 .to_ta_new = pt_ta_new,
777 .to_ta_set_event = pt_ta_set_event,
778 .to_ta_thr_iter = pt_ta_thr_iter,
779 .to_ta_tsd_iter = pt_ta_tsd_iter,
780 .to_thr_clear_event = pt_thr_clear_event,
781 .to_thr_dbresume = pt_thr_dbresume,
782 .to_thr_dbsuspend = pt_thr_dbsuspend,
783 .to_thr_event_enable = pt_thr_event_enable,
784 .to_thr_event_getmsg = pt_thr_event_getmsg,
785 .to_thr_old_get_info = pt_thr_old_get_info,
786 .to_thr_get_info = pt_thr_get_info,
787 .to_thr_getfpregs = pt_thr_getfpregs,
788 .to_thr_getgregs = pt_thr_getgregs,
789 .to_thr_set_event = pt_thr_set_event,
790 .to_thr_setfpregs = pt_thr_setfpregs,
791 .to_thr_setgregs = pt_thr_setgregs,
792 .to_thr_validate = pt_thr_validate,
793 .to_thr_tls_get_addr = pt_thr_tls_get_addr,
795 /* FreeBSD specific extensions. */
796 .to_thr_sstep = pt_thr_sstep,
798 .to_thr_getxmmregs = pt_thr_getxmmregs,
799 .to_thr_setxmmregs = pt_thr_setxmmregs,
803 DATA_SET(__ta_ops, libthr_db_ops);