]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthread_db/libc_r_db.c
This commit was generated by cvs2svn to compensate for changes in r161863,
[FreeBSD/FreeBSD.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 <proc_service.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <thread_db.h>
35
36 #include "thread_db_int.h"
37
38 void libc_r_md_getfpregs(jmp_buf jb, prfpregset_t *);
39 void libc_r_md_getgregs(jmp_buf jb, prgregset_t);
40
41 struct td_thragent {
42         TD_THRAGENT_FIELDS;
43         struct ps_prochandle    *ta_ph;
44         psaddr_t ta_thread_initial;
45         psaddr_t ta_thread_list;
46         psaddr_t ta_thread_run;
47         int     ta_ofs_ctx;
48         int     ta_ofs_next;
49         int     ta_ofs_uniqueid;
50 };
51
52 static td_err_e
53 libc_r_db_init()
54 {
55         return (TD_OK);
56 }
57
58 static td_err_e
59 libc_r_db_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *ev)
60 {
61         return (0);
62 }
63
64 static td_err_e
65 libc_r_db_ta_delete(td_thragent_t *ta)
66 {
67         free(ta);
68         return (TD_OK);
69 }
70
71 static td_err_e
72 libc_r_db_ta_event_addr(const td_thragent_t *ta, td_thr_events_e ev,
73     td_notify_t *n)
74 {
75         return (TD_ERR);
76 }
77
78 static td_err_e
79 libc_r_db_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
80 {
81         return (TD_ERR);
82 }
83
84 static td_err_e
85 libc_r_db_ta_map_id2thr(const td_thragent_t *ta, thread_t tid,
86     td_thrhandle_t *th)
87 {
88         return (TD_ERR);
89 }
90
91 static td_err_e
92 libc_r_db_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwpid,
93     td_thrhandle_t *th)
94 {
95         psaddr_t addr;
96         ps_err_e err;
97
98         th->th_ta = ta;
99         err = ps_pread(ta->ta_ph, ta->ta_thread_initial, &addr, sizeof(addr));
100         if (err != PS_OK)
101                 return (TD_ERR);
102         if (addr == NULL)
103                 return (TD_NOLWP);
104         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &th->th_thread,
105             sizeof(psaddr_t));
106         return ((err == PS_OK) ? TD_OK : TD_ERR);
107 }
108
109 static td_err_e
110 libc_r_db_ta_new(struct ps_prochandle *ph, td_thragent_t **ta_p)
111 {
112         td_thragent_t *ta;
113         psaddr_t addr;
114         ps_err_e err;
115
116         ta = malloc(sizeof(td_thragent_t));
117         if (ta == NULL)
118                 return (TD_MALLOC);
119
120         ta->ta_ph = ph;
121
122         err = ps_pglobal_lookup(ph, NULL, "_thread_initial",
123             &ta->ta_thread_initial);
124         if (err != PS_OK)
125                 goto fail;
126         err = ps_pglobal_lookup(ph, NULL, "_thread_list", &ta->ta_thread_list);
127         if (err != PS_OK)
128                 goto fail;
129         err = ps_pglobal_lookup(ph, NULL, "_thread_run", &ta->ta_thread_run);
130         if (err != PS_OK)
131                 goto fail;
132         err = ps_pglobal_lookup(ph, NULL, "_thread_ctx_offset", &addr);
133         if (err != PS_OK)
134                 goto fail;
135         err = ps_pread(ph, addr, &ta->ta_ofs_ctx, sizeof(int));
136         if (err != PS_OK)
137                 goto fail;
138         err = ps_pglobal_lookup(ph, NULL, "_thread_next_offset", &addr);
139         if (err != PS_OK)
140                 goto fail;
141         err = ps_pread(ph, addr, &ta->ta_ofs_next, sizeof(int));
142         if (err != PS_OK)
143                 goto fail;
144         err = ps_pglobal_lookup(ph, NULL, "_thread_uniqueid_offset", &addr);
145         if (err != PS_OK)
146                 goto fail;
147         err = ps_pread(ph, addr, &ta->ta_ofs_uniqueid, sizeof(int));
148         if (err != PS_OK)
149                 goto fail;
150
151         *ta_p = ta;
152         return (TD_OK);
153
154  fail:
155         free(ta);
156         *ta_p = NULL;
157         return (TD_ERR);
158 }
159
160 static td_err_e
161 libc_r_db_ta_set_event(const td_thragent_t *ta, td_thr_events_t *ev)
162 {
163         return (0);
164 }
165
166 static td_err_e
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)
169 {
170         td_thrhandle_t th;
171         psaddr_t addr;
172         ps_err_e err;
173
174         th.th_ta = ta;
175
176         err = ps_pread(ta->ta_ph, ta->ta_thread_list, &th.th_thread,
177             sizeof(th.th_thread));
178         if (err != PS_OK)
179                 return (TD_ERR);
180         while (th.th_thread != NULL) {
181                 if (cb(&th, data) != 0)
182                         return (TD_OK);
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));
186                 if (err != PS_OK)
187                         return (TD_ERR);
188         }
189         return (TD_OK);
190 }
191
192 static td_err_e
193 libc_r_db_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *ev)
194 {
195         return (0);
196 }
197
198 static td_err_e
199 libc_r_db_thr_event_enable(const td_thrhandle_t *th, int oo)
200 {
201         return (0);
202 }
203
204 static td_err_e
205 libc_r_db_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
206 {
207         return (TD_ERR);
208 }
209
210 static td_err_e
211 libc_r_db_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *ti)
212 {
213         const td_thragent_t *ta;
214         psaddr_t addr, current;
215         ps_err_e err;
216
217         ta = th->th_ta;
218         ti->ti_ta_p = ta;
219         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &current,
220             sizeof(psaddr_t));
221         if (err != PS_OK)
222                 return (TD_ERR);
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. */
227         ti->ti_tid++;
228         return ((err == PS_OK) ? TD_OK : TD_ERR);
229 }
230
231 #ifdef __i386__
232 static td_err_e
233 libc_r_db_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
234 {
235         return (TD_NOFPREGS);
236 }
237 #endif
238
239 static td_err_e
240 libc_r_db_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *r)
241 {
242         jmp_buf jb;
243         const td_thragent_t *ta;
244         psaddr_t addr;
245         ps_err_e err;
246
247         ta = th->th_ta;
248         err = ps_lgetfpregs(ta->ta_ph, -1, r);
249         if (err != PS_OK)
250                 return (TD_ERR);
251         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
252         if (err != PS_OK)
253                 return (TD_ERR);
254         if (th->th_thread == addr)
255                 return (TD_OK);
256         addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
257         err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
258         if (err != PS_OK)
259                 return (TD_ERR);
260         libc_r_md_getfpregs(jb, r);
261         return (TD_OK);
262 }
263
264 static td_err_e
265 libc_r_db_thr_getgregs(const td_thrhandle_t *th, prgregset_t r)
266 {
267         jmp_buf jb;
268         const td_thragent_t *ta;
269         psaddr_t addr;
270         ps_err_e err;
271
272         ta = th->th_ta;
273         err = ps_lgetregs(ta->ta_ph, -1, r);
274         if (err != PS_OK)
275                 return (TD_ERR);
276         err = ps_pread(ta->ta_ph, ta->ta_thread_run, &addr, sizeof(psaddr_t));
277         if (err != PS_OK)
278                 return (TD_ERR);
279         if (th->th_thread == addr)
280                 return (TD_OK);
281         addr = (psaddr_t)((uintptr_t)th->th_thread + ta->ta_ofs_ctx);
282         err = ps_pread(ta->ta_ph, addr, jb, sizeof(jb));
283         if (err != PS_OK)
284                 return (TD_ERR);
285         libc_r_md_getgregs(jb, r);
286         return (TD_OK);
287 }
288
289 static td_err_e
290 libc_r_db_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *ev)
291 {
292         return (0);
293 }
294
295 #ifdef __i386__
296 static td_err_e
297 libc_r_db_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
298 {
299         return (TD_NOFPREGS);
300 }
301 #endif
302
303 static td_err_e
304 libc_r_db_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *r)
305 {
306         return (TD_ERR);
307 }
308
309 static td_err_e
310 libc_r_db_thr_setgregs(const td_thrhandle_t *th, const prgregset_t r)
311 {
312         return (TD_ERR);
313 }
314
315 static td_err_e
316 libc_r_db_thr_validate(const td_thrhandle_t *th)
317 {
318         return (TD_ERR);
319 }
320
321 struct ta_ops libc_r_db_ops = {
322         .to_init                = libc_r_db_init,
323
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,
333
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,
344 #ifdef __i386__
345         .to_thr_getxmmregs      = libc_r_db_thr_getxmmregs,
346         .to_thr_setxmmregs      = libc_r_db_thr_setxmmregs,
347 #endif
348 };