2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <machine/setjmp.h>
31 #include <proc_service.h>
34 #include <thread_db.h>
36 #include "thread_db_int.h"
38 void libc_r_md_getfpregs(jmp_buf jb, prfpregset_t *);
39 void libc_r_md_getgregs(jmp_buf jb, prgregset_t);
43 struct ps_prochandle *ta_ph;
44 psaddr_t ta_thread_initial;
45 psaddr_t ta_thread_list;
46 psaddr_t ta_thread_run;
59 libc_r_db_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *ev)
65 libc_r_db_ta_delete(td_thragent_t *ta)
72 libc_r_db_ta_event_addr(const td_thragent_t *ta, td_thr_events_e ev,
79 libc_r_db_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
85 libc_r_db_ta_map_id2thr(const td_thragent_t *ta, thread_t tid,
92 libc_r_db_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid,
99 err = ps_pread(ta->ta_ph, ta->ta_thread_initial, &addr, sizeof(addr));
104 err = ps_pread(ta->ta_ph, ta->ta_thread_run, &th->th_thread,
106 return ((err == PS_OK) ? TD_OK : TD_ERR);
110 libc_r_db_ta_new(struct ps_prochandle *ph, td_thragent_t **ta_p)
116 ta = malloc(sizeof(td_thragent_t));
122 err = ps_pglobal_lookup(ph, NULL, "_thread_initial",
123 &ta->ta_thread_initial);
126 err = ps_pglobal_lookup(ph, NULL, "_thread_list", &ta->ta_thread_list);
129 err = ps_pglobal_lookup(ph, NULL, "_thread_run", &ta->ta_thread_run);
132 err = ps_pglobal_lookup(ph, NULL, "_thread_ctx_offset", &addr);
135 err = ps_pread(ph, addr, &ta->ta_ofs_ctx, sizeof(int));
138 err = ps_pglobal_lookup(ph, NULL, "_thread_next_offset", &addr);
141 err = ps_pread(ph, addr, &ta->ta_ofs_next, sizeof(int));
144 err = ps_pglobal_lookup(ph, NULL, "_thread_uniqueid_offset", &addr);
147 err = ps_pread(ph, addr, &ta->ta_ofs_uniqueid, sizeof(int));
161 libc_r_db_ta_set_event(const td_thragent_t *ta, td_thr_events_t *ev)
167 libc_r_db_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *cb, void *data,
168 td_thr_state_e state, int pri, sigset_t *mask, unsigned int flags)
176 err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th.th_thread,
177 sizeof(th.th_thread));
180 while (th.th_thread != NULL) {
181 if (cb(&th, data) != 0)
183 addr = (psaddr_t)((uintptr_t)th.th_thread + ta->ta_ofs_next);
184 err = ps_pread(ta->ta_ph, addr, &th.th_thread,
185 sizeof(th.th_thread));
193 libc_r_db_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *ev)
199 libc_r_db_thr_event_enable(const td_thrhandle_t *th, int oo)
205 libc_r_db_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
211 libc_r_db_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *ti)
213 const td_thragent_t *ta;
214 psaddr_t addr, current;
219 err = ps_pread(ta->ta_ph, ta->ta_thread_run, ¤t,
223 ti->ti_lid = (th->th_thread == current) ? -1 : 0;
224 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_uniqueid);
225 err = ps_pread(ta->ta_ph, addr, &ti->ti_tid, sizeof(thread_t));
226 /* libc_r numbers its threads starting with 0. Not smart. */
228 return ((err == PS_OK) ? TD_OK : TD_ERR);
233 libc_r_db_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
235 return (TD_NOFPREGS);
240 libc_r_db_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *r)
243 const td_thragent_t *ta;
248 err = ps_lgetfpregs(ta->ta_ph, -1, r);
251 err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
254 if (th->th_thread == addr)
256 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
257 err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
260 libc_r_md_getfpregs(jb, r);
265 libc_r_db_thr_getgregs(const td_thrhandle_t *th, prgregset_t r)
268 const td_thragent_t *ta;
273 err = ps_lgetregs(ta->ta_ph, -1, r);
276 err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
279 if (th->th_thread == addr)
281 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
282 err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
285 libc_r_md_getgregs(jb, r);
290 libc_r_db_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *ev)
297 libc_r_db_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
299 return (TD_NOFPREGS);
304 libc_r_db_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *r)
310 libc_r_db_thr_setgregs(const td_thrhandle_t *th, const prgregset_t r)
316 libc_r_db_thr_validate(const td_thrhandle_t *th)
321 struct ta_ops libc_r_db_ops = {
322 .to_init = libc_r_db_init,
324 .to_ta_clear_event = libc_r_db_ta_clear_event,
325 .to_ta_delete = libc_r_db_ta_delete,
326 .to_ta_event_addr = libc_r_db_ta_event_addr,
327 .to_ta_event_getmsg = libc_r_db_ta_event_getmsg,
328 .to_ta_map_id2thr = libc_r_db_ta_map_id2thr,
329 .to_ta_map_lwp2thr = libc_r_db_ta_map_lwp2thr,
330 .to_ta_new = libc_r_db_ta_new,
331 .to_ta_set_event = libc_r_db_ta_set_event,
332 .to_ta_thr_iter = libc_r_db_ta_thr_iter,
334 .to_thr_clear_event = libc_r_db_thr_clear_event,
335 .to_thr_event_enable = libc_r_db_thr_event_enable,
336 .to_thr_event_getmsg = libc_r_db_thr_event_getmsg,
337 .to_thr_get_info = libc_r_db_thr_get_info,
338 .to_thr_getfpregs = libc_r_db_thr_getfpregs,
339 .to_thr_getgregs = libc_r_db_thr_getgregs,
340 .to_thr_set_event = libc_r_db_thr_set_event,
341 .to_thr_setfpregs = libc_r_db_thr_setfpregs,
342 .to_thr_setgregs = libc_r_db_thr_setgregs,
343 .to_thr_validate = libc_r_db_thr_validate,
345 .to_thr_getxmmregs = libc_r_db_thr_getxmmregs,
346 .to_thr_setxmmregs = libc_r_db_thr_setxmmregs,