xref: /dragonfly/lib/libthread_db/thread_db.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  *
14*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino  * SUCH DAMAGE.
25*86d7f5d3SJohn Marino  *
26*86d7f5d3SJohn Marino  * $DragonFly: src/lib/libthread_db/thread_db.c,v 1.1 2005/05/07 09:25:44 davidxu Exp $
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino #include <sys/cdefs.h>
30*86d7f5d3SJohn Marino #include <proc_service.h>
31*86d7f5d3SJohn Marino #include <stddef.h>
32*86d7f5d3SJohn Marino #include <thread_db.h>
33*86d7f5d3SJohn Marino #include <unistd.h>
34*86d7f5d3SJohn Marino 
35*86d7f5d3SJohn Marino #include "thread_db_int.h"
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino struct td_thragent
38*86d7f5d3SJohn Marino {
39*86d7f5d3SJohn Marino 	TD_THRAGENT_FIELDS;
40*86d7f5d3SJohn Marino };
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino static TAILQ_HEAD(, td_thragent) proclist = TAILQ_HEAD_INITIALIZER(proclist);
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino extern struct ta_ops libthread_xu_ops;
45*86d7f5d3SJohn Marino 
46*86d7f5d3SJohn Marino static struct ta_ops *ops[] = {
47*86d7f5d3SJohn Marino 	&libthread_xu_ops,
48*86d7f5d3SJohn Marino };
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino td_err_e
td_init(void)51*86d7f5d3SJohn Marino td_init(void)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino 	td_err_e ret, tmp;
54*86d7f5d3SJohn Marino 	size_t i;
55*86d7f5d3SJohn Marino 
56*86d7f5d3SJohn Marino 	ret = 0;
57*86d7f5d3SJohn Marino 	for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
58*86d7f5d3SJohn Marino 		if (ops[i]->to_init != NULL) {
59*86d7f5d3SJohn Marino 			tmp = ops[i]->to_init();
60*86d7f5d3SJohn Marino 			if (tmp != TD_OK)
61*86d7f5d3SJohn Marino 				ret = tmp;
62*86d7f5d3SJohn Marino 		}
63*86d7f5d3SJohn Marino 	}
64*86d7f5d3SJohn Marino 	return (ret);
65*86d7f5d3SJohn Marino }
66*86d7f5d3SJohn Marino 
67*86d7f5d3SJohn Marino td_err_e
td_ta_clear_event(const td_thragent_t * ta,td_thr_events_t * events)68*86d7f5d3SJohn Marino td_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
69*86d7f5d3SJohn Marino {
70*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_clear_event(ta, events));
71*86d7f5d3SJohn Marino }
72*86d7f5d3SJohn Marino 
73*86d7f5d3SJohn Marino td_err_e
td_ta_delete(td_thragent_t * ta)74*86d7f5d3SJohn Marino td_ta_delete(td_thragent_t *ta)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino 	TAILQ_REMOVE(&proclist, ta, ta_next);
77*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_delete(ta));
78*86d7f5d3SJohn Marino }
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino td_err_e
td_ta_event_addr(const td_thragent_t * ta,td_event_e event,td_notify_t * ptr)81*86d7f5d3SJohn Marino td_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
82*86d7f5d3SJohn Marino {
83*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_event_addr(ta, event, ptr));
84*86d7f5d3SJohn Marino }
85*86d7f5d3SJohn Marino 
86*86d7f5d3SJohn Marino td_err_e
td_ta_event_getmsg(const td_thragent_t * ta,td_event_msg_t * msg)87*86d7f5d3SJohn Marino td_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
88*86d7f5d3SJohn Marino {
89*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_event_getmsg(ta, msg));
90*86d7f5d3SJohn Marino }
91*86d7f5d3SJohn Marino 
92*86d7f5d3SJohn Marino td_err_e
td_ta_map_id2thr(const td_thragent_t * ta,thread_t id,td_thrhandle_t * th)93*86d7f5d3SJohn Marino td_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
94*86d7f5d3SJohn Marino {
95*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_map_id2thr(ta, id, th));
96*86d7f5d3SJohn Marino }
97*86d7f5d3SJohn Marino 
98*86d7f5d3SJohn Marino td_err_e
td_ta_map_lwp2thr(const td_thragent_t * ta,lwpid_t lwpid,td_thrhandle_t * th)99*86d7f5d3SJohn Marino td_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
100*86d7f5d3SJohn Marino {
101*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_map_lwp2thr(ta, lwpid, th));
102*86d7f5d3SJohn Marino }
103*86d7f5d3SJohn Marino 
104*86d7f5d3SJohn Marino td_err_e
td_ta_new(struct ps_prochandle * ph,td_thragent_t ** pta)105*86d7f5d3SJohn Marino td_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
106*86d7f5d3SJohn Marino {
107*86d7f5d3SJohn Marino 	size_t i;
108*86d7f5d3SJohn Marino 
109*86d7f5d3SJohn Marino 	for (i = 0; i < sizeof(ops)/sizeof(ops[0]); ++i) {
110*86d7f5d3SJohn Marino 		if (ops[i]->to_ta_new(ph, pta) == TD_OK) {
111*86d7f5d3SJohn Marino 			TAILQ_INSERT_HEAD(&proclist, *pta, ta_next);
112*86d7f5d3SJohn Marino 			(*pta)->ta_ops = ops[i];
113*86d7f5d3SJohn Marino 			return (TD_OK);
114*86d7f5d3SJohn Marino 		}
115*86d7f5d3SJohn Marino 	}
116*86d7f5d3SJohn Marino 	return (TD_NOLIBTHREAD);
117*86d7f5d3SJohn Marino }
118*86d7f5d3SJohn Marino 
119*86d7f5d3SJohn Marino td_err_e
td_ta_set_event(const td_thragent_t * ta,td_thr_events_t * events)120*86d7f5d3SJohn Marino td_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
121*86d7f5d3SJohn Marino {
122*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_set_event(ta, events));
123*86d7f5d3SJohn Marino }
124*86d7f5d3SJohn Marino 
125*86d7f5d3SJohn Marino td_err_e
td_ta_thr_iter(const td_thragent_t * ta,td_thr_iter_f * callback,void * cbdata_p,td_thr_state_e state,int ti_pri,sigset_t * ti_sigmask_p,unsigned int ti_user_flags)126*86d7f5d3SJohn Marino td_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
127*86d7f5d3SJohn Marino     void *cbdata_p, td_thr_state_e state, int ti_pri, sigset_t *ti_sigmask_p,
128*86d7f5d3SJohn Marino     unsigned int ti_user_flags)
129*86d7f5d3SJohn Marino {
130*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_thr_iter(ta, callback, cbdata_p, state,
131*86d7f5d3SJohn Marino 		    ti_pri, ti_sigmask_p, ti_user_flags));
132*86d7f5d3SJohn Marino }
133*86d7f5d3SJohn Marino 
134*86d7f5d3SJohn Marino td_err_e
td_ta_tsd_iter(const td_thragent_t * ta,td_key_iter_f * callback,void * cbdata_p)135*86d7f5d3SJohn Marino td_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *callback,
136*86d7f5d3SJohn Marino     void *cbdata_p)
137*86d7f5d3SJohn Marino {
138*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_ta_tsd_iter(ta, callback, cbdata_p));
139*86d7f5d3SJohn Marino }
140*86d7f5d3SJohn Marino 
141*86d7f5d3SJohn Marino td_err_e
td_thr_clear_event(const td_thrhandle_t * th,td_thr_events_t * events)142*86d7f5d3SJohn Marino td_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *events)
143*86d7f5d3SJohn Marino {
144*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
145*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_clear_event(th, events));
146*86d7f5d3SJohn Marino }
147*86d7f5d3SJohn Marino 
148*86d7f5d3SJohn Marino td_err_e
td_thr_dbresume(const td_thrhandle_t * th)149*86d7f5d3SJohn Marino td_thr_dbresume(const td_thrhandle_t *th)
150*86d7f5d3SJohn Marino {
151*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
152*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_dbresume(th));
153*86d7f5d3SJohn Marino }
154*86d7f5d3SJohn Marino 
155*86d7f5d3SJohn Marino td_err_e
td_thr_dbsuspend(const td_thrhandle_t * th)156*86d7f5d3SJohn Marino td_thr_dbsuspend(const td_thrhandle_t *th)
157*86d7f5d3SJohn Marino {
158*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
159*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_dbsuspend(th));
160*86d7f5d3SJohn Marino }
161*86d7f5d3SJohn Marino 
162*86d7f5d3SJohn Marino td_err_e
td_thr_event_enable(const td_thrhandle_t * th,int en)163*86d7f5d3SJohn Marino td_thr_event_enable(const td_thrhandle_t *th, int en)
164*86d7f5d3SJohn Marino {
165*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
166*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_event_enable(th, en));
167*86d7f5d3SJohn Marino }
168*86d7f5d3SJohn Marino 
169*86d7f5d3SJohn Marino td_err_e
td_thr_event_getmsg(const td_thrhandle_t * th,td_event_msg_t * msg)170*86d7f5d3SJohn Marino td_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
171*86d7f5d3SJohn Marino {
172*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
173*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_event_getmsg(th, msg));
174*86d7f5d3SJohn Marino }
175*86d7f5d3SJohn Marino 
176*86d7f5d3SJohn Marino td_err_e
td_thr_get_info(const td_thrhandle_t * th,td_thrinfo_t * info)177*86d7f5d3SJohn Marino td_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
178*86d7f5d3SJohn Marino {
179*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
180*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_get_info(th, info));
181*86d7f5d3SJohn Marino }
182*86d7f5d3SJohn Marino 
183*86d7f5d3SJohn Marino td_err_e
td_thr_getfpregs(const td_thrhandle_t * th,prfpregset_t * fpregset)184*86d7f5d3SJohn Marino td_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregset)
185*86d7f5d3SJohn Marino {
186*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
187*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_getfpregs(th, fpregset));
188*86d7f5d3SJohn Marino }
189*86d7f5d3SJohn Marino 
190*86d7f5d3SJohn Marino td_err_e
td_thr_getgregs(const td_thrhandle_t * th,prgregset_t gregs)191*86d7f5d3SJohn Marino td_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
192*86d7f5d3SJohn Marino {
193*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
194*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_getgregs(th, gregs));
195*86d7f5d3SJohn Marino }
196*86d7f5d3SJohn Marino 
197*86d7f5d3SJohn Marino td_err_e
td_thr_set_event(const td_thrhandle_t * th,td_thr_events_t * events)198*86d7f5d3SJohn Marino td_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *events)
199*86d7f5d3SJohn Marino {
200*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
201*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_set_event(th, events));
202*86d7f5d3SJohn Marino }
203*86d7f5d3SJohn Marino 
204*86d7f5d3SJohn Marino td_err_e
td_thr_setfpregs(const td_thrhandle_t * th,const prfpregset_t * fpregs)205*86d7f5d3SJohn Marino td_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
206*86d7f5d3SJohn Marino {
207*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
208*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_setfpregs(th, fpregs));
209*86d7f5d3SJohn Marino }
210*86d7f5d3SJohn Marino 
211*86d7f5d3SJohn Marino td_err_e
td_thr_setgregs(const td_thrhandle_t * th,const prgregset_t gregs)212*86d7f5d3SJohn Marino td_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
213*86d7f5d3SJohn Marino {
214*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
215*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_setgregs(th, gregs));
216*86d7f5d3SJohn Marino }
217*86d7f5d3SJohn Marino 
218*86d7f5d3SJohn Marino td_err_e
td_thr_validate(const td_thrhandle_t * th)219*86d7f5d3SJohn Marino td_thr_validate(const td_thrhandle_t *th)
220*86d7f5d3SJohn Marino {
221*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
222*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_validate(th));
223*86d7f5d3SJohn Marino }
224*86d7f5d3SJohn Marino 
225*86d7f5d3SJohn Marino td_err_e
td_thr_tls_get_addr(const td_thrhandle_t * th,void * linkmap,size_t offset,void ** address)226*86d7f5d3SJohn Marino td_thr_tls_get_addr(const td_thrhandle_t *th, void *linkmap, size_t offset,
227*86d7f5d3SJohn Marino 		    void **address)
228*86d7f5d3SJohn Marino {
229*86d7f5d3SJohn Marino 	const td_thragent_t *ta = th->th_ta;
230*86d7f5d3SJohn Marino 	return (ta->ta_ops->to_thr_tls_get_addr(th, linkmap, offset, address));
231*86d7f5d3SJohn Marino }
232