]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libthread_db/libc_r_db.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libthread_db / libc_r_db.c
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <machine/setjmp.h>
31 #include <sys/linker_set.h>
32 #include <proc_service.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <thread_db.h>
36
37 #include "thread_db_int.h"
38
39 void libc_r_md_getfpregs(jmp_buf jb, prfpregset_t *);
40 void libc_r_md_getgregs(jmp_buf jb, prgregset_t);
41
42 struct td_thragent {
43         TD_THRAGENT_FIELDS;
44         struct ps_prochandle    *ta_ph;
45         psaddr_t ta_thread_initial;
46         psaddr_t ta_thread_list;
47         psaddr_t ta_thread_run;
48         int     ta_ofs_ctx;
49         int     ta_ofs_next;
50         int     ta_ofs_uniqueid;
51 };
52
53 static td_err_e
54 libc_r_db_init()
55 {
56         return (TD_OK);
57 }
58
59 static td_err_e
60 libc_r_db_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *ev)
61 {
62         return (0);
63 }
64
65 static td_err_e
66 libc_r_db_ta_delete(td_thragent_t *ta)
67 {
68         free(ta);
69         return (TD_OK);
70 }
71
72 static td_err_e
73 libc_r_db_ta_event_addr(const td_thragent_t *ta, td_thr_events_e ev,
74     td_notify_t *n)
75 {
76         return (TD_ERR);
77 }
78
79 static td_err_e
80 libc_r_db_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
81 {
82         return (TD_ERR);
83 }
84
85 static td_err_e
86 libc_r_db_ta_map_id2thr(const td_thragent_t *ta, thread_t tid,
87     td_thrhandle_t *th)
88 {
89         return (TD_ERR);
90 }
91
92 static td_err_e
93 libc_r_db_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid,
94     td_thrhandle_t *th)
95 {
96         psaddr_t addr;
97         ps_err_e err;
98
99         th->th_ta = ta;
100         err = ps_pread(ta->ta_ph, ta->ta_thread_initial, &addr, sizeof(addr));
101         if (err != PS_OK)
102                 return (TD_ERR);
103         if (addr == NULL)
104                 return (TD_NOLWP);
105         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &th->th_thread,
106             sizeof(psaddr_t));
107         return ((err == PS_OK) ? TD_OK : TD_ERR);
108 }
109
110 static td_err_e
111 libc_r_db_ta_new(struct ps_prochandle *ph, td_thragent_t **ta_p)
112 {
113         td_thragent_t *ta;
114         psaddr_t addr;
115         ps_err_e err;
116
117         ta = malloc(sizeof(td_thragent_t));
118         if (ta == NULL)
119                 return (TD_MALLOC);
120
121         ta->ta_ph = ph;
122
123         err = ps_pglobal_lookup(ph, NULL, "_thread_initial",
124             &ta->ta_thread_initial);
125         if (err != PS_OK)
126                 goto fail;
127         err = ps_pglobal_lookup(ph, NULL, "_thread_list", &ta->ta_thread_list);
128         if (err != PS_OK)
129                 goto fail;
130         err = ps_pglobal_lookup(ph, NULL, "_thread_run", &ta->ta_thread_run);
131         if (err != PS_OK)
132                 goto fail;
133         err = ps_pglobal_lookup(ph, NULL, "_thread_ctx_offset", &addr);
134         if (err != PS_OK)
135                 goto fail;
136         err = ps_pread(ph, addr, &ta->ta_ofs_ctx, sizeof(int));
137         if (err != PS_OK)
138                 goto fail;
139         err = ps_pglobal_lookup(ph, NULL, "_thread_next_offset", &addr);
140         if (err != PS_OK)
141                 goto fail;
142         err = ps_pread(ph, addr, &ta->ta_ofs_next, sizeof(int));
143         if (err != PS_OK)
144                 goto fail;
145         err = ps_pglobal_lookup(ph, NULL, "_thread_uniqueid_offset", &addr);
146         if (err != PS_OK)
147                 goto fail;
148         err = ps_pread(ph, addr, &ta->ta_ofs_uniqueid, sizeof(int));
149         if (err != PS_OK)
150                 goto fail;
151
152         *ta_p = ta;
153         return (TD_OK);
154
155  fail:
156         free(ta);
157         *ta_p = NULL;
158         return (TD_ERR);
159 }
160
161 static td_err_e
162 libc_r_db_ta_set_event(const td_thragent_t *ta, td_thr_events_t *ev)
163 {
164         return (0);
165 }
166
167 static td_err_e
168 libc_r_db_ta_thr_iter(const td_thragent_t *ta, td_thr_iter_f *cb, void *data,
169     td_thr_state_e state, int pri, sigset_t *mask, unsigned int flags)
170 {
171         td_thrhandle_t th;
172         psaddr_t addr;
173         ps_err_e err;
174
175         th.th_ta = ta;
176
177         err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th.th_thread,
178             sizeof(th.th_thread));
179         if (err != PS_OK)
180                 return (TD_ERR);
181         while (th.th_thread != NULL) {
182                 if (cb(&th, data) != 0)
183                         return (TD_OK);
184                 addr = (psaddr_t)((uintptr_t)th.th_thread + ta->ta_ofs_next);
185                 err = ps_pread(ta->ta_ph, addr, &th.th_thread,
186                     sizeof(th.th_thread));
187                 if (err != PS_OK)
188                         return (TD_ERR);
189         }
190         return (TD_OK);
191 }
192
193 static td_err_e
194 libc_r_db_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *ev)
195 {
196         return (0);
197 }
198
199 static td_err_e
200 libc_r_db_thr_event_enable(const td_thrhandle_t *th, int oo)
201 {
202         return (0);
203 }
204
205 static td_err_e
206 libc_r_db_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
207 {
208         return (TD_ERR);
209 }
210
211 static td_err_e
212 libc_r_db_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *ti)
213 {
214         const td_thragent_t *ta;
215         psaddr_t addr, current;
216         ps_err_e err;
217
218         ta = th->th_ta;
219         ti->ti_ta_p = ta;
220         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &current,
221             sizeof(psaddr_t));
222         if (err != PS_OK)
223                 return (TD_ERR);
224         ti->ti_lid = (th->th_thread == current) ? -1 : 0;
225         addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_uniqueid);
226         err = ps_pread(ta->ta_ph, addr, &ti->ti_tid, sizeof(thread_t));
227         /* libc_r numbers its threads starting with 0. Not smart. */
228         ti->ti_tid++;
229         return ((err == PS_OK) ? TD_OK : TD_ERR);
230 }
231
232 #ifdef __i386__
233 static td_err_e
234 libc_r_db_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
235 {
236         return (TD_NOFPREGS);
237 }
238 #endif
239
240 static td_err_e
241 libc_r_db_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *r)
242 {
243         jmp_buf jb;
244         const td_thragent_t *ta;
245         psaddr_t addr;
246         ps_err_e err;
247
248         ta = th->th_ta;
249         err = ps_lgetfpregs(ta->ta_ph, -1, r);
250         if (err != PS_OK)
251                 return (TD_ERR);
252         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
253         if (err != PS_OK)
254                 return (TD_ERR);
255         if (th->th_thread == addr)
256                 return (TD_OK);
257         addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
258         err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
259         if (err != PS_OK)
260                 return (TD_ERR);
261         libc_r_md_getfpregs(jb, r);
262         return (TD_OK);
263 }
264
265 static td_err_e
266 libc_r_db_thr_getgregs(const td_thrhandle_t *th, prgregset_t r)
267 {
268         jmp_buf jb;
269         const td_thragent_t *ta;
270         psaddr_t addr;
271         ps_err_e err;
272
273         ta = th->th_ta;
274         err = ps_lgetregs(ta->ta_ph, -1, r);
275         if (err != PS_OK)
276                 return (TD_ERR);
277         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
278         if (err != PS_OK)
279                 return (TD_ERR);
280         if (th->th_thread == addr)
281                 return (TD_OK);
282         addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
283         err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
284         if (err != PS_OK)
285                 return (TD_ERR);
286         libc_r_md_getgregs(jb, r);
287         return (TD_OK);
288 }
289
290 static td_err_e
291 libc_r_db_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *ev)
292 {
293         return (0);
294 }
295
296 #ifdef __i386__
297 static td_err_e
298 libc_r_db_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
299 {
300         return (TD_NOFPREGS);
301 }
302 #endif
303
304 static td_err_e
305 libc_r_db_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *r)
306 {
307         return (TD_ERR);
308 }
309
310 static td_err_e
311 libc_r_db_thr_setgregs(const td_thrhandle_t *th, const prgregset_t r)
312 {
313         return (TD_ERR);
314 }
315
316 static td_err_e
317 libc_r_db_thr_validate(const td_thrhandle_t *th)
318 {
319         return (TD_ERR);
320 }
321
322 struct ta_ops libc_r_db_ops = {
323         .to_init                = libc_r_db_init,
324
325         .to_ta_clear_event      = libc_r_db_ta_clear_event,
326         .to_ta_delete           = libc_r_db_ta_delete,
327         .to_ta_event_addr       = libc_r_db_ta_event_addr,
328         .to_ta_event_getmsg     = libc_r_db_ta_event_getmsg,
329         .to_ta_map_id2thr       = libc_r_db_ta_map_id2thr,
330         .to_ta_map_lwp2thr      = libc_r_db_ta_map_lwp2thr,
331         .to_ta_new              = libc_r_db_ta_new,
332         .to_ta_set_event        = libc_r_db_ta_set_event,
333         .to_ta_thr_iter         = libc_r_db_ta_thr_iter,
334
335         .to_thr_clear_event     = libc_r_db_thr_clear_event,
336         .to_thr_event_enable    = libc_r_db_thr_event_enable,
337         .to_thr_event_getmsg    = libc_r_db_thr_event_getmsg,
338         .to_thr_get_info        = libc_r_db_thr_get_info,
339         .to_thr_getfpregs       = libc_r_db_thr_getfpregs,
340         .to_thr_getgregs        = libc_r_db_thr_getgregs,
341         .to_thr_set_event       = libc_r_db_thr_set_event,
342         .to_thr_setfpregs       = libc_r_db_thr_setfpregs,
343         .to_thr_setgregs        = libc_r_db_thr_setgregs,
344         .to_thr_validate        = libc_r_db_thr_validate,
345 #ifdef __i386__
346         .to_thr_getxmmregs      = libc_r_db_thr_getxmmregs,
347         .to_thr_setxmmregs      = libc_r_db_thr_setxmmregs,
348 #endif
349 };
350
351 DATA_SET(__ta_ops, libc_r_db_ops);