2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * 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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <proc_service.h>
34 #include <thread_db.h>
36 #include <sys/cdefs.h>
37 #include <sys/endian.h>
38 #include <sys/errno.h>
39 #include <sys/linker_set.h>
41 #include "thread_db_int.h"
48 static TAILQ_HEAD(, td_thragent) proclist = TAILQ_HEAD_INITIALIZER(proclist);
50 SET_DECLARE(__ta_ops, struct ta_ops);
56 struct ta_ops *ops_p, **ops_pp;
59 SET_FOREACH(ops_pp, __ta_ops) {
61 if (ops_p->to_init != NULL) {
62 tmp = ops_p->to_init();
71 td_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
73 return (ta->ta_ops->to_ta_clear_event(ta, events));
77 td_ta_delete(td_thragent_t *ta)
79 TAILQ_REMOVE(&proclist, ta, ta_next);
80 return (ta->ta_ops->to_ta_delete(ta));
84 td_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
86 return (ta->ta_ops->to_ta_event_addr(ta, event, ptr));
90 td_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
92 return (ta->ta_ops->to_ta_event_getmsg(ta, msg));
96 td_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
98 return (ta->ta_ops->to_ta_map_id2thr(ta, id, th));
102 td_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid, td_thrhandle_t *th)
104 return (ta->ta_ops->to_ta_map_lwp2thr(ta, lwpid, th));
108 td_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
110 struct ta_ops *ops_p, **ops_pp;
112 SET_FOREACH(ops_pp, __ta_ops) {
114 if (ops_p->to_ta_new(ph, pta) == TD_OK) {
115 TAILQ_INSERT_HEAD(&proclist, *pta, ta_next);
116 (*pta)->ta_ops = ops_p;
120 return (TD_NOLIBTHREAD);
124 td_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
126 return (ta->ta_ops->to_ta_set_event(ta, events));
130 td_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *callback,
131 void *cbdata_p, td_thr_state_e state, int ti_pri, sigset_t *ti_sigmask_p,
132 unsigned int ti_user_flags)
134 return (ta->ta_ops->to_ta_thr_iter(ta, callback, cbdata_p, state,
135 ti_pri, ti_sigmask_p, ti_user_flags));
139 td_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *callback,
142 return (ta->ta_ops->to_ta_tsd_iter(ta, callback, cbdata_p));
146 td_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *events)
148 const td_thragent_t *ta = th->th_ta;
149 return (ta->ta_ops->to_thr_clear_event(th, events));
153 td_thr_dbresume(const td_thrhandle_t *th)
155 const td_thragent_t *ta = th->th_ta;
156 return (ta->ta_ops->to_thr_dbresume(th));
160 td_thr_dbsuspend(const td_thrhandle_t *th)
162 const td_thragent_t *ta = th->th_ta;
163 return (ta->ta_ops->to_thr_dbsuspend(th));
167 td_thr_event_enable(const td_thrhandle_t *th, int en)
169 const td_thragent_t *ta = th->th_ta;
170 return (ta->ta_ops->to_thr_event_enable(th, en));
174 td_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
176 const td_thragent_t *ta = th->th_ta;
177 return (ta->ta_ops->to_thr_event_getmsg(th, msg));
181 td_thr_old_get_info(const td_thrhandle_t *th, td_old_thrinfo_t *info)
183 const td_thragent_t *ta = th->th_ta;
184 return (ta->ta_ops->to_thr_old_get_info(th, info));
186 __sym_compat(td_thr_get_info, td_thr_old_get_info, FBSD_1.0);
189 td_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
191 const td_thragent_t *ta = th->th_ta;
192 return (ta->ta_ops->to_thr_get_info(th, info));
197 td_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
199 const td_thragent_t *ta = th->th_ta;
200 return (ta->ta_ops->to_thr_getxmmregs(th, fxsave));
206 td_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregset)
208 const td_thragent_t *ta = th->th_ta;
209 return (ta->ta_ops->to_thr_getfpregs(th, fpregset));
213 td_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
215 const td_thragent_t *ta = th->th_ta;
216 return (ta->ta_ops->to_thr_getgregs(th, gregs));
220 td_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *events)
222 const td_thragent_t *ta = th->th_ta;
223 return (ta->ta_ops->to_thr_set_event(th, events));
228 td_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
230 const td_thragent_t *ta = th->th_ta;
231 return (ta->ta_ops->to_thr_setxmmregs(th, fxsave));
236 td_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
238 const td_thragent_t *ta = th->th_ta;
239 return (ta->ta_ops->to_thr_setfpregs(th, fpregs));
243 td_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
245 const td_thragent_t *ta = th->th_ta;
246 return (ta->ta_ops->to_thr_setgregs(th, gregs));
250 td_thr_validate(const td_thrhandle_t *th)
252 const td_thragent_t *ta = th->th_ta;
253 return (ta->ta_ops->to_thr_validate(th));
257 td_thr_tls_get_addr(const td_thrhandle_t *th, psaddr_t linkmap, size_t offset,
260 const td_thragent_t *ta = th->th_ta;
261 return (ta->ta_ops->to_thr_tls_get_addr(th, linkmap, offset, address));
264 /* FreeBSD specific extensions. */
267 td_thr_sstep(const td_thrhandle_t *th, int step)
269 const td_thragent_t *ta = th->th_ta;
270 return (ta->ta_ops->to_thr_sstep(th, step));
274 * Support functions for reading from and writing to the target
279 thr_pread(struct ps_prochandle *ph, psaddr_t addr, uint64_t *val,
280 u_int size, u_int byteorder)
282 uint8_t buf[sizeof(*val)];
285 if (size > sizeof(buf))
288 err = ps_pread(ph, addr, buf, size);
337 thr_pread_int(const struct td_thragent *ta, psaddr_t addr, uint32_t *val)
342 error = thr_pread(ta->ph, addr, &tmp, sizeof(int), BYTE_ORDER);
350 thr_pread_long(const struct td_thragent *ta, psaddr_t addr, uint64_t *val)
353 return (thr_pread(ta->ph, addr, val, sizeof(long), BYTE_ORDER));
357 thr_pread_ptr(const struct td_thragent *ta, psaddr_t addr, psaddr_t *val)
362 error = thr_pread(ta->ph, addr, &tmp, sizeof(void *), BYTE_ORDER);
370 thr_pwrite(struct ps_prochandle *ph, psaddr_t addr, uint64_t val,
371 u_int size, u_int byteorder)
373 uint8_t buf[sizeof(val)];
376 if (size > sizeof(buf))
383 buf[0] = (uint8_t)val;
386 be16enc(buf, (uint16_t)val);
389 be32enc(buf, (uint32_t)val);
392 be64enc(buf, (uint64_t)val);
401 buf[0] = (uint8_t)val;
404 le16enc(buf, (uint16_t)val);
407 le32enc(buf, (uint32_t)val);
410 le64enc(buf, (uint64_t)val);
420 err = ps_pwrite(ph, addr, buf, size);
421 return ((err != PS_OK) ? EFAULT : 0);
425 thr_pwrite_int(const struct td_thragent *ta, psaddr_t addr, uint32_t val)
428 return (thr_pwrite(ta->ph, addr, val, sizeof(int), BYTE_ORDER));
432 thr_pwrite_long(const struct td_thragent *ta, psaddr_t addr, uint64_t val)
435 return (thr_pwrite(ta->ph, addr, val, sizeof(long), BYTE_ORDER));
439 thr_pwrite_ptr(const struct td_thragent *ta, psaddr_t addr, psaddr_t val)
442 return (thr_pwrite(ta->ph, addr, val, sizeof(void *), BYTE_ORDER));