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 <proc_service.h>
32 #include <thread_db.h>
34 #include "thread_db_int.h"
38 struct ps_prochandle *ta_ph;
39 psaddr_t ta_thread_list;
52 libthr_db_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *ev)
58 libthr_db_ta_delete(td_thragent_t *ta)
65 libthr_db_ta_event_addr(const td_thragent_t *ta, td_thr_events_e event,
72 libthr_db_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
78 libthr_db_ta_map_id2thr(const td_thragent_t *ta, thread_t tid,
87 err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th->th_thread,
88 sizeof(th->th_thread));
91 while (th->th_thread != NULL) {
92 addr = (psaddr_t)((uintptr_t)th->th_thread +
94 err = ps_pread(ta->ta_ph, addr, &lwpid, sizeof(thread_t));
101 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_next);
102 err = ps_pread(ta->ta_ph, addr, &th->th_thread,
103 sizeof(th->th_thread));
112 libthr_db_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid,
121 err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th->th_thread,
122 sizeof(th->th_thread));
125 while (th->th_thread != NULL) {
126 addr = (psaddr_t)((uintptr_t)th->th_thread +
128 err = ps_pread(ta->ta_ph, addr, &tid, sizeof(thread_t));
135 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_next);
136 err = ps_pread(ta->ta_ph, addr, &th->th_thread,
137 sizeof(th->th_thread));
145 libthr_db_ta_new(struct ps_prochandle *ph, td_thragent_t **ta_p)
151 err = ps_pglobal_lookup(ph, NULL, "_libthr_debug", &addr);
153 return (TD_NOLIBTHREAD);
155 ta = malloc(sizeof(td_thragent_t));
161 err = ps_pglobal_lookup(ph, NULL, "_thread_list", &ta->ta_thread_list);
164 err = ps_pglobal_lookup(ph, NULL, "_thread_ctx_offset", &addr);
167 err = ps_pread(ph, addr, &ta->ta_ofs_ctx, sizeof(int));
170 err = ps_pglobal_lookup(ph, NULL, "_thread_next_offset", &addr);
173 err = ps_pread(ph, addr, &ta->ta_ofs_next, sizeof(int));
176 err = ps_pglobal_lookup(ph, NULL, "_thread_thr_id_offset", &addr);
179 err = ps_pread(ph, addr, &ta->ta_ofs_thr_id, sizeof(int));
193 libthr_db_ta_set_event(const td_thragent_t *ta, td_thr_events_t *ev)
199 libthr_db_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *cb, void *data,
200 td_thr_state_e state, int pri, sigset_t *mask, unsigned int flags)
208 err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th.th_thread,
209 sizeof(th.th_thread));
212 while (th.th_thread != NULL) {
213 addr = (psaddr_t)((uintptr_t)th.th_thread +
215 err = ps_pread(ta->ta_ph, addr, &th.th_tid, sizeof(thread_t));
218 if (cb(&th, data) != 0)
220 addr = (psaddr_t)((uintptr_t)th.th_thread + ta->ta_ofs_next);
221 err = ps_pread(ta->ta_ph, addr, &th.th_thread,
222 sizeof(th.th_thread));
230 libthr_db_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *ev)
236 libthr_dbresume(const td_thrhandle_t *th)
240 err = ps_lcontinue(th->th_ta->ta_ph, (lwpid_t)th->th_tid);
241 return ((err == PS_OK) ? TD_OK : TD_ERR);
245 libthr_dbsuspend(const td_thrhandle_t *th)
249 err = ps_lstop(th->th_ta->ta_ph, (lwpid_t)th->th_tid);
250 return ((err == PS_OK) ? TD_OK : TD_ERR);
254 libthr_db_thr_event_enable(const td_thrhandle_t *th, int oo)
260 libthr_db_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
266 libthr_db_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *ti)
268 const td_thragent_t *ta;
275 addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_thr_id);
276 err = ps_pread(ta->ta_ph, addr, &tid, sizeof(thread_t));
279 return ((err == PS_OK) ? TD_OK : TD_ERR);
283 libthr_db_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *r)
285 const td_thragent_t *ta;
289 err = ps_lgetfpregs(ta->ta_ph, (lwpid_t)th->th_tid, r);
290 return ((err == PS_OK) ? TD_OK : TD_ERR);
294 libthr_db_thr_getgregs(const td_thrhandle_t *th, prgregset_t r)
296 const td_thragent_t *ta;
301 err = ps_lgetregs(ta->ta_ph, (lwpid_t)th->th_tid, r);
302 return ((err == PS_OK) ? TD_OK : TD_ERR);
306 libthr_db_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *ev)
312 libthr_db_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *r)
316 err = ps_lsetfpregs(th->th_ta->ta_ph, (lwpid_t)th->th_tid, r);
317 return ((err == PS_OK) ? TD_OK : TD_ERR);
321 libthr_db_thr_setgregs(const td_thrhandle_t *th, const prgregset_t r)
325 err = ps_lsetregs(th->th_ta->ta_ph, (lwpid_t)th->th_tid, r);
326 return ((err == PS_OK) ? TD_OK : TD_ERR);
330 libthr_db_thr_validate(const td_thrhandle_t *th)
336 libthr_db_sstep(const td_thrhandle_t *th, int step)
341 struct ta_ops libthr_db_ops = {
342 .to_init = libthr_db_init,
344 .to_ta_clear_event = libthr_db_ta_clear_event,
345 .to_ta_delete = libthr_db_ta_delete,
346 .to_ta_event_addr = libthr_db_ta_event_addr,
347 .to_ta_event_getmsg = libthr_db_ta_event_getmsg,
348 .to_ta_map_id2thr = libthr_db_ta_map_id2thr,
349 .to_ta_map_lwp2thr = libthr_db_ta_map_lwp2thr,
350 .to_ta_new = libthr_db_ta_new,
351 .to_ta_set_event = libthr_db_ta_set_event,
352 .to_ta_thr_iter = libthr_db_ta_thr_iter,
353 .to_thr_clear_event = libthr_db_thr_clear_event,
354 .to_thr_dbresume = libthr_dbresume,
355 .to_thr_dbsuspend = libthr_dbsuspend,
356 .to_thr_event_enable = libthr_db_thr_event_enable,
357 .to_thr_event_getmsg = libthr_db_thr_event_getmsg,
358 .to_thr_get_info = libthr_db_thr_get_info,
359 .to_thr_getfpregs = libthr_db_thr_getfpregs,
360 .to_thr_getgregs = libthr_db_thr_getgregs,
361 .to_thr_set_event = libthr_db_thr_set_event,
362 .to_thr_setfpregs = libthr_db_thr_setfpregs,
363 .to_thr_setgregs = libthr_db_thr_setgregs,
364 .to_thr_validate = libthr_db_thr_validate,
366 /* FreeBSD specific extensions. */
367 .to_thr_sstep = libthr_db_sstep