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$");
30 #include <proc_service.h>
32 #include <thread_db.h>
34 #include <sys/cdefs.h>
35 #include <sys/endian.h>
36 #include <sys/errno.h>
37 #include <sys/linker_set.h>
39 #include "thread_db_int.h"
46 static TAILQ_HEAD(, td_thragent) proclist = TAILQ_HEAD_INITIALIZER(proclist);
48 SET_DECLARE(__ta_ops, struct ta_ops);
54 struct ta_ops *ops_p, **ops_pp;
57 SET_FOREACH(ops_pp, __ta_ops) {
59 if (ops_p->to_init != NULL) {
60 tmp = ops_p->to_init();
69 td_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
71 return (ta->ta_ops->to_ta_clear_event(ta, events));
75 td_ta_delete(td_thragent_t *ta)
77 TAILQ_REMOVE(&proclist, ta, ta_next);
78 return (ta->ta_ops->to_ta_delete(ta));
82 td_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
84 return (ta->ta_ops->to_ta_event_addr(ta, event, ptr));
88 td_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
90 return (ta->ta_ops->to_ta_event_getmsg(ta, msg));
94 td_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
96 return (ta->ta_ops->to_ta_map_id2thr(ta, id, th));
100 td_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
102 return (ta->ta_ops->to_ta_map_lwp2thr(ta, lwpid, th));
106 td_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
108 struct ta_ops *ops_p, **ops_pp;
110 SET_FOREACH(ops_pp, __ta_ops) {
112 if (ops_p->to_ta_new(ph, pta) == TD_OK) {
113 TAILQ_INSERT_HEAD(&proclist, *pta, ta_next);
114 (*pta)->ta_ops = ops_p;
118 return (TD_NOLIBTHREAD);
122 td_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
124 return (ta->ta_ops->to_ta_set_event(ta, events));
128 td_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
129 void *cbdata_p, td_thr_state_e state, int ti_pri, sigset_t *ti_sigmask_p,
130 unsigned int ti_user_flags)
132 return (ta->ta_ops->to_ta_thr_iter(ta, callback, cbdata_p, state,
133 ti_pri, ti_sigmask_p, ti_user_flags));
137 td_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *callback,
140 return (ta->ta_ops->to_ta_tsd_iter(ta, callback, cbdata_p));
144 td_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *events)
146 const td_thragent_t *ta = th->th_ta;
147 return (ta->ta_ops->to_thr_clear_event(th, events));
151 td_thr_dbresume(const td_thrhandle_t *th)
153 const td_thragent_t *ta = th->th_ta;
154 return (ta->ta_ops->to_thr_dbresume(th));
158 td_thr_dbsuspend(const td_thrhandle_t *th)
160 const td_thragent_t *ta = th->th_ta;
161 return (ta->ta_ops->to_thr_dbsuspend(th));
165 td_thr_event_enable(const td_thrhandle_t *th, int en)
167 const td_thragent_t *ta = th->th_ta;
168 return (ta->ta_ops->to_thr_event_enable(th, en));
172 td_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
174 const td_thragent_t *ta = th->th_ta;
175 return (ta->ta_ops->to_thr_event_getmsg(th, msg));
179 td_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
181 const td_thragent_t *ta = th->th_ta;
182 return (ta->ta_ops->to_thr_get_info(th, info));
187 td_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
189 const td_thragent_t *ta = th->th_ta;
190 return (ta->ta_ops->to_thr_getxmmregs(th, fxsave));
196 td_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregset)
198 const td_thragent_t *ta = th->th_ta;
199 return (ta->ta_ops->to_thr_getfpregs(th, fpregset));
203 td_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
205 const td_thragent_t *ta = th->th_ta;
206 return (ta->ta_ops->to_thr_getgregs(th, gregs));
210 td_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *events)
212 const td_thragent_t *ta = th->th_ta;
213 return (ta->ta_ops->to_thr_set_event(th, events));
218 td_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
220 const td_thragent_t *ta = th->th_ta;
221 return (ta->ta_ops->to_thr_setxmmregs(th, fxsave));
226 td_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
228 const td_thragent_t *ta = th->th_ta;
229 return (ta->ta_ops->to_thr_setfpregs(th, fpregs));
233 td_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
235 const td_thragent_t *ta = th->th_ta;
236 return (ta->ta_ops->to_thr_setgregs(th, gregs));
240 td_thr_validate(const td_thrhandle_t *th)
242 const td_thragent_t *ta = th->th_ta;
243 return (ta->ta_ops->to_thr_validate(th));
247 td_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t linkmap, size_t offset,
250 const td_thragent_t *ta = th->th_ta;
251 return (ta->ta_ops->to_thr_tls_get_addr(th, linkmap, offset, address));
254 /* FreeBSD specific extensions. */
257 td_thr_sstep(const td_thrhandle_t *th, int step)
259 const td_thragent_t *ta = th->th_ta;
260 return (ta->ta_ops->to_thr_sstep(th, step));
264 * Support functions for reading from and writing to the target
269 thr_pread(struct ps_prochandle *ph, psaddr_t addr, uint64_t *val,
270 u_int size, u_int byteorder)
272 uint8_t buf[sizeof(*val)];
275 if (size > sizeof(buf))
278 err = ps_pread(ph, addr, buf, size);
327 thr_pread_int(const struct td_thragent *ta, psaddr_t addr, uint32_t *val)
332 error = thr_pread(ta->ph, addr, &tmp, sizeof(int), BYTE_ORDER);
340 thr_pread_long(const struct td_thragent *ta, psaddr_t addr, uint64_t *val)
343 return (thr_pread(ta->ph, addr, val, sizeof(long), BYTE_ORDER));
347 thr_pread_ptr(const struct td_thragent *ta, psaddr_t addr, psaddr_t *val)
352 error = thr_pread(ta->ph, addr, &tmp, sizeof(void *), BYTE_ORDER);
360 thr_pwrite(struct ps_prochandle *ph, psaddr_t addr, uint64_t val,
361 u_int size, u_int byteorder)
363 uint8_t buf[sizeof(val)];
366 if (size > sizeof(buf))
373 buf[0] = (uint8_t)val;
376 be16enc(buf, (uint16_t)val);
379 be32enc(buf, (uint32_t)val);
382 be64enc(buf, (uint64_t)val);
391 buf[0] = (uint8_t)val;
394 le16enc(buf, (uint16_t)val);
397 le32enc(buf, (uint32_t)val);
400 le64enc(buf, (uint64_t)val);
410 err = ps_pwrite(ph, addr, buf, size);
411 return ((err != PS_OK) ? EFAULT : 0);
415 thr_pwrite_int(const struct td_thragent *ta, psaddr_t addr, uint32_t val)
418 return (thr_pwrite(ta->ph, addr, val, sizeof(int), BYTE_ORDER));
422 thr_pwrite_long(const struct td_thragent *ta, psaddr_t addr, uint64_t val)
425 return (thr_pwrite(ta->ph, addr, val, sizeof(long), BYTE_ORDER));
429 thr_pwrite_ptr(const struct td_thragent *ta, psaddr_t addr, psaddr_t val)
432 return (thr_pwrite(ta->ph, addr, val, sizeof(void *), BYTE_ORDER));