]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthread_db/libpthread_db.c
This commit was generated by cvs2svn to compensate for changes in r154439,
[FreeBSD/FreeBSD.git] / lib / libthread_db / libpthread_db.c
1 /*
2  * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <pthread.h>
35 #include <sys/types.h>
36 #include <sys/kse.h>
37 #include <sys/ptrace.h>
38 #include <proc_service.h>
39 #include <thread_db.h>
40
41 #include "libpthread_db.h"
42
43 #define P2T(c) ps2td(c)
44
45 static void pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp);
46 static int pt_validate(const td_thrhandle_t *th);
47
48 static int
49 ps2td(int c)
50 {
51         switch (c) {
52         case PS_OK:
53                 return TD_OK;
54         case PS_ERR:
55                 return TD_ERR;
56         case PS_BADPID:
57                 return TD_BADPH;
58         case PS_BADLID:
59                 return TD_NOLWP;
60         case PS_BADADDR:
61                 return TD_ERR;
62         case PS_NOSYM:
63                 return TD_NOLIBTHREAD;
64         case PS_NOFREGS:
65                 return TD_NOFPREGS;
66         default:
67                 return TD_ERR;
68         }
69 }
70
71 static long
72 pt_map_thread(const td_thragent_t *const_ta, psaddr_t pt, int type)
73 {
74         td_thragent_t *ta = __DECONST(td_thragent_t *, const_ta);
75         struct pt_map *new;
76         int i, first = -1;
77
78         /* leave zero out */
79         for (i = 1; i < ta->map_len; ++i) {
80                 if (ta->map[i].type == PT_NONE) {
81                         if (first == -1)
82                                 first = i;
83                 } else if (ta->map[i].type == type && ta->map[i].thr == pt) {
84                                 return (i);
85                 }
86         }
87
88         if (first == -1) {
89                 if (ta->map_len == 0) {
90                         ta->map = calloc(20, sizeof(struct pt_map));
91                         if (ta->map == NULL)
92                                 return (-1);
93                         ta->map_len = 20;
94                         first = 1;
95                 } else {
96                         new = realloc(ta->map,
97                                       sizeof(struct pt_map) * ta->map_len * 2);
98                         if (new == NULL)
99                                 return (-1);
100                         memset(new + ta->map_len, '\0', sizeof(struct pt_map) *
101                                ta->map_len);
102                         first = ta->map_len;
103                         ta->map = new;
104                         ta->map_len *= 2;
105                 }
106         }
107
108         ta->map[first].type = type;
109         ta->map[first].thr = pt;
110         return (first);
111 }
112
113 static td_err_e
114 pt_init(void)
115 {
116         pt_md_init();
117         return (0);
118 }
119
120 static td_err_e
121 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
122 {
123 #define LOOKUP_SYM(proc, sym, addr)                     \
124         ret = ps_pglobal_lookup(proc, NULL, sym, addr); \
125         if (ret != 0) {                                 \
126                 TDBG("can not find symbol: %s\n", sym); \
127                 ret = TD_NOLIBTHREAD;                   \
128                 goto error;                             \
129         }
130
131 #define LOOKUP_VAL(proc, sym, val)                      \
132         ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
133         if (ret != 0) {                                 \
134                 TDBG("can not find symbol: %s\n", sym); \
135                 ret = TD_NOLIBTHREAD;                   \
136                 goto error;                             \
137         }                                               \
138         ret = ps_pread(proc, vaddr, val, sizeof(int));  \
139         if (ret != 0) {                                 \
140                 TDBG("can not read value of %s\n", sym);\
141                 ret = TD_NOLIBTHREAD;                   \
142                 goto error;                             \
143         }
144
145         td_thragent_t *ta;
146         psaddr_t vaddr;
147         int dbg;
148         int ret;
149
150         TDBG_FUNC();
151
152         ta = malloc(sizeof(td_thragent_t));
153         if (ta == NULL)
154                 return (TD_MALLOC);
155
156         ta->ph = ph;
157         ta->thread_activated = 0;
158         ta->map = NULL;
159         ta->map_len = 0;
160
161         LOOKUP_SYM(ph, "_libkse_debug",         &ta->libkse_debug_addr);
162         LOOKUP_SYM(ph, "_thread_list",          &ta->thread_list_addr);
163         LOOKUP_SYM(ph, "_thread_activated",     &ta->thread_activated_addr);
164         LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
165         LOOKUP_SYM(ph, "_thread_keytable",      &ta->thread_keytable_addr);
166         LOOKUP_VAL(ph, "_thread_off_dtv",       &ta->thread_off_dtv);
167         LOOKUP_VAL(ph, "_thread_off_kse_locklevel", &ta->thread_off_kse_locklevel);
168         LOOKUP_VAL(ph, "_thread_off_kse",       &ta->thread_off_kse);
169         LOOKUP_VAL(ph, "_thread_off_tlsindex",  &ta->thread_off_tlsindex);
170         LOOKUP_VAL(ph, "_thread_off_attr_flags",        &ta->thread_off_attr_flags);
171         LOOKUP_VAL(ph, "_thread_size_key",      &ta->thread_size_key);
172         LOOKUP_VAL(ph, "_thread_off_tcb",       &ta->thread_off_tcb);
173         LOOKUP_VAL(ph, "_thread_off_linkmap",   &ta->thread_off_linkmap);
174         LOOKUP_VAL(ph, "_thread_off_tmbx",      &ta->thread_off_tmbx);
175         LOOKUP_VAL(ph, "_thread_off_thr_locklevel",     &ta->thread_off_thr_locklevel);
176         LOOKUP_VAL(ph, "_thread_off_next",      &ta->thread_off_next);
177         LOOKUP_VAL(ph, "_thread_off_state",     &ta->thread_off_state);
178         LOOKUP_VAL(ph, "_thread_max_keys",      &ta->thread_max_keys);
179         LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
180         LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
181         LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
182         LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
183         dbg = getpid();
184         /*
185          * If this fails it probably means we're debugging a core file and
186          * can't write to it.
187          */
188         ps_pwrite(ph, ta->libkse_debug_addr, &dbg, sizeof(int));
189         *pta = ta;
190         return (0);
191
192 error:
193         free(ta);
194         return (ret);
195 }
196
197 static td_err_e
198 pt_ta_delete(td_thragent_t *ta)
199 {
200         int dbg;
201
202         TDBG_FUNC();
203
204         dbg = 0;
205         /*
206          * Error returns from this write are not really a problem;
207          * the process doesn't exist any more.
208          */
209         ps_pwrite(ta->ph, ta->libkse_debug_addr, &dbg, sizeof(int));
210         if (ta->map)
211                 free(ta->map);
212         free(ta);
213         return (TD_OK);
214 }
215
216 static td_err_e
217 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
218 {
219         prgregset_t gregs;
220         TAILQ_HEAD(, pthread) thread_list;
221         psaddr_t pt, tcb_addr;
222         lwpid_t lwp;
223         int ret;
224
225         TDBG_FUNC();
226
227         if (id < 0 || id >= ta->map_len || ta->map[id].type == PT_NONE)
228                 return (TD_NOTHR);
229         ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
230                         sizeof(thread_list));
231         if (ret != 0)
232                 return (P2T(ret));
233         pt = (psaddr_t)thread_list.tqh_first;
234         if (ta->map[id].type == PT_LWP) {
235                 /*
236                  * if we are referencing a lwp, make sure it was not already
237                  * mapped to user thread.
238                  */
239                 while (pt != 0) {
240                         ret = ps_pread(ta->ph,
241                                 pt + ta->thread_off_tcb,
242                                 &tcb_addr, sizeof(tcb_addr));
243                         if (ret != 0)
244                                 return (P2T(ret));
245                         ret = ps_pread(ta->ph,
246                                 tcb_addr + ta->thread_off_tmbx + 
247                                 offsetof(struct kse_thr_mailbox, tm_lwp),
248                                 &lwp, sizeof(lwp));
249                         if (ret != 0)
250                                 return (P2T(ret));
251                         /*
252                          * If the lwp was already mapped to userland thread,
253                          * we shouldn't reference it directly in future.
254                          */
255                         if (lwp == ta->map[id].lwp) {
256                                 ta->map[id].type = PT_NONE;
257                                 return (TD_NOTHR);
258                         }
259                         /* get next thread */
260                         ret = ps_pread(ta->ph,
261                                 pt + ta->thread_off_next,
262                                 &pt, sizeof(pt));
263                         if (ret != 0)
264                                 return (P2T(ret));
265                 }
266                 /* check lwp */
267                 ret = ptrace(PT_GETREGS, ta->map[id].lwp, (caddr_t)&gregs, 0);
268                 if (ret != 0) {
269                         /* no longer exists */
270                         ta->map[id].type = PT_NONE;
271                         return (TD_NOTHR);
272                 }
273         } else {
274                 while (pt != 0 && ta->map[id].thr != pt) {
275                         ret = ps_pread(ta->ph,
276                                 pt + ta->thread_off_tcb,
277                                 &tcb_addr, sizeof(tcb_addr));
278                         if (ret != 0)
279                                 return (P2T(ret));
280                         /* get next thread */
281                         ret = ps_pread(ta->ph,
282                                 pt + ta->thread_off_next,
283                                 &pt, sizeof(pt));
284                         if (ret != 0)
285                                 return (P2T(ret));
286                 }
287
288                 if (pt == 0) {
289                         /* no longer exists */
290                         ta->map[id].type = PT_NONE;
291                         return (TD_NOTHR);
292                 }
293         }
294         th->th_ta = ta;
295         th->th_tid = id;
296         th->th_thread = pt;
297         return (TD_OK);
298 }
299
300 static td_err_e
301 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
302 {
303         TAILQ_HEAD(, pthread) thread_list;
304         psaddr_t pt, ptr;
305         lwpid_t tmp_lwp;
306         int ret;
307         
308         TDBG_FUNC();
309
310         ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
311                         sizeof(thread_list));
312         if (ret != 0)
313                 return (P2T(ret));
314         pt = (psaddr_t)thread_list.tqh_first;
315         while (pt != 0) {
316                 ret = ps_pread(ta->ph, pt + ta->thread_off_tcb,
317                                 &ptr, sizeof(ptr));
318                 if (ret != 0)
319                         return (P2T(ret));
320                 ptr += ta->thread_off_tmbx +
321                        offsetof(struct kse_thr_mailbox, tm_lwp);
322                 ret = ps_pread(ta->ph, ptr, &tmp_lwp, sizeof(lwpid_t));
323                 if (ret != 0)
324                         return (P2T(ret));
325                 if (tmp_lwp == lwp) {
326                         th->th_ta = ta;
327                         th->th_tid = pt_map_thread(ta, pt, PT_USER);
328                         if (th->th_tid == -1)
329                                 return (TD_MALLOC);
330                         pt_unmap_lwp(ta, lwp);
331                         th->th_thread = pt;
332                         return (TD_OK);
333                 }
334
335                 /* get next thread */
336                 ret = ps_pread(ta->ph,
337                            pt + ta->thread_off_next,
338                            &pt, sizeof(pt));
339                 if (ret != 0)
340                         return (P2T(ret));
341         }
342
343         return (TD_NOTHR);
344 }
345
346 static td_err_e
347 pt_ta_thr_iter(const td_thragent_t *ta,
348                td_thr_iter_f *callback, void *cbdata_p,
349                td_thr_state_e state, int ti_pri,
350                sigset_t *ti_sigmask_p,
351                unsigned int ti_user_flags)
352 {
353         TAILQ_HEAD(, pthread) thread_list;
354         td_thrhandle_t th;
355         psaddr_t pt;
356         ps_err_e pserr;
357         int activated;
358
359         TDBG_FUNC();
360
361         pserr = ps_pread(ta->ph, ta->thread_activated_addr, &activated,
362             sizeof(int));
363         if (pserr != PS_OK)
364                 return (P2T(pserr));
365         if (!activated)
366                 return (TD_OK);
367
368         pserr = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
369             sizeof(thread_list));
370         if (pserr != 0)
371                 return (P2T(pserr));
372         pt = (psaddr_t)thread_list.tqh_first;
373         while (pt != 0) {
374                 th.th_ta = ta;
375                 th.th_tid = pt_map_thread(ta, pt, PT_USER);
376                 th.th_thread = pt;
377                 /* should we unmap lwp here ? */
378                 if (th.th_tid == -1)
379                         return (TD_MALLOC);
380                 if ((*callback)(&th, cbdata_p))
381                         return (TD_DBERR);
382                 /* get next thread */
383                 pserr = ps_pread(ta->ph,
384                     pt + ta->thread_off_next, &pt,
385                     sizeof(pt));
386                 if (pserr != PS_OK)
387                         return (P2T(pserr));
388         }
389         return (TD_OK);
390 }
391
392 static td_err_e
393 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
394 {
395         char *keytable;
396         void *destructor;
397         int i, ret, allocated;
398
399         TDBG_FUNC();
400
401         keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
402         if (keytable == NULL)
403                 return (TD_MALLOC);
404         ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
405                        ta->thread_max_keys * ta->thread_size_key);
406         if (ret != 0) {
407                 free(keytable);
408                 return (P2T(ret));
409         }       
410         for (i = 0; i < ta->thread_max_keys; i++) {
411                 allocated = *(int *)(keytable + i * ta->thread_size_key +
412                         ta->thread_off_key_allocated);
413                 destructor = *(void **)(keytable + i * ta->thread_size_key +
414                         ta->thread_off_key_destructor);
415                 if (allocated) {
416                         ret = (ki)(i, destructor, arg);
417                         if (ret != 0) {
418                                 free(keytable);
419                                 return (TD_DBERR);
420                         }
421                 }
422         }
423         free(keytable);
424         return (TD_OK);
425 }
426
427 static td_err_e
428 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
429 {
430         TDBG_FUNC();
431         return (TD_ERR);
432 }
433
434 static td_err_e
435 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
436 {
437         TDBG_FUNC();
438         return (0);
439 }
440
441 static td_err_e
442 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
443 {
444         TDBG_FUNC();
445         return (0);
446 }
447
448 static td_err_e
449 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
450 {
451         TDBG_FUNC();
452         return (TD_NOMSG);
453 }
454
455 static td_err_e
456 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
457 {
458         td_thragent_t *ta = (td_thragent_t *)th->th_ta;
459         psaddr_t tcb_addr, tmbx_addr, ptr;
460         lwpid_t lwp;
461         uint32_t dflags;
462         int attrflags, locklevel, ret;
463
464         TDBG_FUNC();
465
466         ret = pt_validate(th);
467         if (ret)
468                 return (ret);
469
470         if (ta->map[th->th_tid].type == PT_LWP) {
471                 if (suspend)
472                         ret = ps_lstop(ta->ph, ta->map[th->th_tid].lwp);
473                 else
474                         ret = ps_lcontinue(ta->ph, ta->map[th->th_tid].lwp);
475                 return (P2T(ret));
476         }
477
478         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
479                 ta->thread_off_attr_flags,
480                 &attrflags, sizeof(attrflags));
481         if (ret != 0)
482                 return (P2T(ret));
483         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
484                        ta->thread_off_tcb,
485                        &tcb_addr, sizeof(tcb_addr));
486         if (ret != 0)
487                 return (P2T(ret));
488         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
489         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
490         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
491         if (ret != 0)
492                 return (P2T(ret));
493
494         if (lwp != 0) {
495                 /* don't suspend signal thread */
496                 if (attrflags & 0x200)
497                         return (0);
498                 if (attrflags & PTHREAD_SCOPE_SYSTEM) {
499                         /*
500                          * don't suspend system scope thread if it is holding
501                          * some low level locks
502                          */
503                         ptr = ta->map[th->th_tid].thr + ta->thread_off_kse;
504                         ret = ps_pread(ta->ph, ptr, &ptr, sizeof(ptr));
505                         if (ret != 0)
506                                 return (P2T(ret));
507                         ret = ps_pread(ta->ph, ptr + ta->thread_off_kse_locklevel,
508                                 &locklevel, sizeof(int));
509                         if (ret != 0)
510                                 return (P2T(ret));
511                         if (locklevel <= 0) {
512                                 ptr = ta->map[th->th_tid].thr +
513                                         ta->thread_off_thr_locklevel;
514                                 ret = ps_pread(ta->ph, ptr, &locklevel,
515                                         sizeof(int));
516                                 if (ret != 0)
517                                         return (P2T(ret));
518                         }
519                         if (suspend) {
520                                 if (locklevel <= 0)
521                                         ret = ps_lstop(ta->ph, lwp);
522                         } else {
523                                 ret = ps_lcontinue(ta->ph, lwp);
524                         }
525                         if (ret != 0)
526                                 return (P2T(ret));
527                         /* FALLTHROUGH */
528                 } else {
529                         struct ptrace_lwpinfo pl;
530
531                         if (ptrace(PT_LWPINFO, lwp, (caddr_t) &pl, sizeof(pl)))
532                                 return (TD_ERR);
533                         if (suspend) {
534                                 if (!(pl.pl_flags & PL_FLAG_BOUND))
535                                         ret = ps_lstop(ta->ph, lwp);
536                         } else {
537                                 ret = ps_lcontinue(ta->ph, lwp);
538                         }
539                         if (ret != 0)
540                                 return (P2T(ret));
541                         /* FALLTHROUGH */
542                 }
543         }
544         /* read tm_dflags */
545         ret = ps_pread(ta->ph,
546                 tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
547                 &dflags, sizeof(dflags));
548         if (ret != 0)
549                 return (P2T(ret));
550         if (suspend)
551                 dflags |= TMDF_SUSPEND;
552         else
553                 dflags &= ~TMDF_SUSPEND;
554         ret = ps_pwrite(ta->ph,
555                tmbx_addr + offsetof(struct kse_thr_mailbox, tm_dflags),
556                &dflags, sizeof(dflags));
557         return (P2T(ret));
558 }
559
560 static td_err_e
561 pt_thr_dbresume(const td_thrhandle_t *th)
562 {
563         TDBG_FUNC();
564
565         return pt_dbsuspend(th, 0);
566 }
567
568 static td_err_e
569 pt_thr_dbsuspend(const td_thrhandle_t *th)
570 {
571         TDBG_FUNC();
572
573         return pt_dbsuspend(th, 1);
574 }
575
576 static td_err_e
577 pt_thr_validate(const td_thrhandle_t *th)
578 {
579         td_thrhandle_t temp;
580         int ret;
581
582         TDBG_FUNC();
583
584         ret = pt_ta_map_id2thr(th->th_ta, th->th_tid,
585                                &temp);
586         return (ret);
587 }
588
589 static td_err_e
590 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
591 {
592         const td_thragent_t *ta = th->th_ta;
593         psaddr_t tcb_addr;
594         uint32_t dflags;
595         int state;
596         int ret;
597
598         TDBG_FUNC();
599
600         ret = pt_validate(th);
601         if (ret)
602                 return (ret);
603
604         memset(info, 0, sizeof(*info));
605         if (ta->map[th->th_tid].type == PT_LWP) {
606                 info->ti_type = TD_THR_SYSTEM;
607                 info->ti_lid = ta->map[th->th_tid].lwp;
608                 info->ti_tid = th->th_tid;
609                 info->ti_state = TD_THR_RUN;
610                 info->ti_type = TD_THR_SYSTEM;
611                 return (TD_OK);
612         }
613         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
614                        &tcb_addr, sizeof(tcb_addr));
615         if (ret != 0)
616                 return (P2T(ret));
617         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_state,
618                        &state, sizeof(state));
619         ret = ps_pread(ta->ph,
620                 tcb_addr + ta->thread_off_tmbx +
621                  offsetof(struct kse_thr_mailbox, tm_lwp),
622                 &info->ti_lid, sizeof(lwpid_t));
623         if (ret != 0)
624                 return (P2T(ret));
625         ret = ps_pread(ta->ph,
626                 tcb_addr + ta->thread_off_tmbx +
627                  offsetof(struct kse_thr_mailbox, tm_dflags),
628                 &dflags, sizeof(dflags));
629         if (ret != 0)
630                 return (P2T(ret));
631         info->ti_ta_p = th->th_ta;
632         info->ti_tid = th->th_tid;
633         if (state == ta->thread_state_running)
634                 info->ti_state = TD_THR_RUN;
635         else if (state == ta->thread_state_zoombie)
636                 info->ti_state = TD_THR_ZOMBIE;
637         else
638                 info->ti_state = TD_THR_SLEEP;
639         info->ti_db_suspended = ((dflags & TMDF_SUSPEND) != 0);
640         info->ti_type = TD_THR_USER;
641         return (0);
642 }
643
644 #ifdef __i386__
645 static td_err_e
646 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
647 {
648         const td_thragent_t *ta = th->th_ta;
649         struct kse_thr_mailbox tmbx;
650         psaddr_t tcb_addr, tmbx_addr, ptr;
651         lwpid_t lwp;
652         int ret;
653
654         return TD_ERR;
655
656         TDBG_FUNC();
657
658         ret = pt_validate(th);
659         if (ret)
660                 return (ret);
661
662         if (ta->map[th->th_tid].type == PT_LWP) {
663                 ret = ps_lgetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
664                 return (P2T(ret));
665         }
666
667         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
668                        &tcb_addr, sizeof(tcb_addr));
669         if (ret != 0)
670                 return (P2T(ret));
671         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
672         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
673         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
674         if (ret != 0)
675                 return (P2T(ret));
676         if (lwp != 0) {
677                 ret = ps_lgetxmmregs(ta->ph, lwp, fxsave);
678                 return (P2T(ret));
679         }
680
681         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
682         if (ret != 0)
683                 return (P2T(ret));
684         pt_ucontext_to_fxsave(&tmbx.tm_context, fxsave);
685         return (0);
686 }
687 #endif
688
689 static td_err_e
690 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
691 {
692         const td_thragent_t *ta = th->th_ta;
693         struct kse_thr_mailbox tmbx;
694         psaddr_t tcb_addr, tmbx_addr, ptr;
695         lwpid_t lwp;
696         int ret;
697
698         TDBG_FUNC();
699
700         ret = pt_validate(th);
701         if (ret)
702                 return (ret);
703
704         if (ta->map[th->th_tid].type == PT_LWP) {
705                 ret = ps_lgetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
706                 return (P2T(ret));
707         }
708
709         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
710                        &tcb_addr, sizeof(tcb_addr));
711         if (ret != 0)
712                 return (P2T(ret));
713         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
714         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
715         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
716         if (ret != 0)
717                 return (P2T(ret));
718         if (lwp != 0) {
719                 ret = ps_lgetfpregs(ta->ph, lwp, fpregs);
720                 return (P2T(ret));
721         }
722
723         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
724         if (ret != 0)
725                 return (P2T(ret));
726         pt_ucontext_to_fpreg(&tmbx.tm_context, fpregs);
727         return (0);
728 }
729
730 static td_err_e
731 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
732 {
733         const td_thragent_t *ta = th->th_ta;
734         struct kse_thr_mailbox tmbx;
735         psaddr_t tcb_addr, tmbx_addr, ptr;
736         lwpid_t lwp;
737         int ret;
738
739         TDBG_FUNC();
740
741         ret = pt_validate(th);
742         if (ret)
743                 return (ret);
744
745         if (ta->map[th->th_tid].type == PT_LWP) {
746                 ret = ps_lgetregs(ta->ph,
747                                   ta->map[th->th_tid].lwp, gregs);
748                 return (P2T(ret));
749         }
750
751         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
752                         &tcb_addr, sizeof(tcb_addr));
753         if (ret != 0)
754                 return (P2T(ret));
755         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
756         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
757         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
758         if (ret != 0)
759                 return (P2T(ret));
760         if (lwp != 0) {
761                 ret = ps_lgetregs(ta->ph, lwp, gregs);
762                 return (P2T(ret));
763         }
764         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
765         if (ret != 0)
766                 return (P2T(ret));
767         pt_ucontext_to_reg(&tmbx.tm_context, gregs);
768         return (0);
769 }
770
771 #ifdef __i386__
772 static td_err_e
773 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
774 {
775         const td_thragent_t *ta = th->th_ta;
776         struct kse_thr_mailbox tmbx;
777         psaddr_t tcb_addr, tmbx_addr, ptr;
778         lwpid_t lwp;
779         int ret;
780
781         return TD_ERR;
782
783         TDBG_FUNC();
784
785         ret = pt_validate(th);
786         if (ret)
787                 return (ret);
788
789         if (ta->map[th->th_tid].type == PT_LWP) {
790                 ret = ps_lsetxmmregs(ta->ph, ta->map[th->th_tid].lwp, fxsave);
791                 return (P2T(ret));
792         }
793
794         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
795                         ta->thread_off_tcb,
796                         &tcb_addr, sizeof(tcb_addr));
797         if (ret != 0)
798                 return (P2T(ret));
799         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
800         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
801         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
802         if (ret != 0)
803                 return (P2T(ret));
804         if (lwp != 0) {
805                 ret = ps_lsetxmmregs(ta->ph, lwp, fxsave);
806                 return (P2T(ret));
807         }
808         /*
809          * Read a copy of context, this makes sure that registers
810          * not covered by structure reg won't be clobbered
811          */
812         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
813         if (ret != 0)
814                 return (P2T(ret));
815
816         pt_fxsave_to_ucontext(fxsave, &tmbx.tm_context);
817         ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
818         return (P2T(ret));
819 }
820 #endif
821
822 static td_err_e
823 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
824 {
825         const td_thragent_t *ta = th->th_ta;
826         struct kse_thr_mailbox tmbx;
827         psaddr_t tcb_addr, tmbx_addr, ptr;
828         lwpid_t lwp;
829         int ret;
830
831         TDBG_FUNC();
832
833         ret = pt_validate(th);
834         if (ret)
835                 return (ret);
836
837         if (ta->map[th->th_tid].type == PT_LWP) {
838                 ret = ps_lsetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
839                 return (P2T(ret));
840         }
841
842         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
843                         ta->thread_off_tcb,
844                         &tcb_addr, sizeof(tcb_addr));
845         if (ret != 0)
846                 return (P2T(ret));
847         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
848         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
849         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
850         if (ret != 0)
851                 return (P2T(ret));
852         if (lwp != 0) {
853                 ret = ps_lsetfpregs(ta->ph, lwp, fpregs);
854                 return (P2T(ret));
855         }
856         /*
857          * Read a copy of context, this makes sure that registers
858          * not covered by structure reg won't be clobbered
859          */
860         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
861         if (ret != 0)
862                 return (P2T(ret));
863
864         pt_fpreg_to_ucontext(fpregs, &tmbx.tm_context);
865         ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
866         return (P2T(ret));
867 }
868
869 static td_err_e
870 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
871 {
872         const td_thragent_t *ta = th->th_ta;
873         struct kse_thr_mailbox tmbx;
874         psaddr_t tcb_addr, tmbx_addr, ptr;
875         lwpid_t lwp;
876         int ret;
877
878         TDBG_FUNC();
879
880         ret = pt_validate(th);
881         if (ret)
882                 return (ret);
883
884         if (ta->map[th->th_tid].type == PT_LWP) {
885                 ret = ps_lsetregs(ta->ph, ta->map[th->th_tid].lwp, gregs);
886                 return (P2T(ret));
887         }
888
889         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
890                         ta->thread_off_tcb,
891                         &tcb_addr, sizeof(tcb_addr));
892         if (ret != 0)
893                 return (P2T(ret));
894         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
895         ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
896         ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
897         if (ret != 0)
898                 return (P2T(ret));
899         if (lwp != 0) {
900                 ret = ps_lsetregs(ta->ph, lwp, gregs);
901                 return (P2T(ret));
902         }
903
904         /*
905          * Read a copy of context, make sure that registers
906          * not covered by structure reg won't be clobbered
907          */
908         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
909         if (ret != 0)
910                 return (P2T(ret));
911         pt_reg_to_ucontext(gregs, &tmbx.tm_context);
912         ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
913         return (P2T(ret));
914 }
915
916 static td_err_e
917 pt_thr_event_enable(const td_thrhandle_t *th, int en)
918 {
919         TDBG_FUNC();
920         return (0);
921 }
922
923 static td_err_e
924 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
925 {
926         TDBG_FUNC();
927         return (0);
928 }
929
930 static td_err_e
931 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
932 {
933         TDBG_FUNC();
934         return (0);
935 }
936
937 static td_err_e
938 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
939 {
940         TDBG_FUNC();
941         return (TD_NOMSG);
942 }
943
944 static td_err_e
945 pt_thr_sstep(const td_thrhandle_t *th, int step)
946 {
947         const td_thragent_t *ta = th->th_ta;
948         struct kse_thr_mailbox tmbx;
949         struct reg regs;
950         psaddr_t tcb_addr, tmbx_addr;
951         uint32_t dflags;
952         lwpid_t lwp;
953         int ret;
954
955         TDBG_FUNC();
956
957         ret = pt_validate(th);
958         if (ret)
959                 return (ret);
960
961         if (ta->map[th->th_tid].type == PT_LWP)
962                 return (TD_BADTH);
963
964         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + 
965                         ta->thread_off_tcb,
966                         &tcb_addr, sizeof(tcb_addr));
967         if (ret != 0)
968                 return (P2T(ret));
969
970         /* Clear or set single step flag in thread mailbox */
971         ret = ps_pread(ta->ph,
972                 tcb_addr + ta->thread_off_tmbx +
973                  offsetof(struct kse_thr_mailbox, tm_dflags),
974                 &dflags, sizeof(uint32_t));
975         if (ret != 0)
976                 return (P2T(ret));
977         if (step != 0)
978                 dflags |= TMDF_SSTEP;
979         else
980                 dflags &= ~TMDF_SSTEP;
981         ret = ps_pwrite(ta->ph,
982                 tcb_addr + ta->thread_off_tmbx +
983                  offsetof(struct kse_thr_mailbox, tm_dflags),
984                 &dflags, sizeof(uint32_t));
985         if (ret != 0)
986                 return (P2T(ret));
987         /* Get lwp */
988         ret = ps_pread(ta->ph,
989                 tcb_addr + ta->thread_off_tmbx +
990                  offsetof(struct kse_thr_mailbox, tm_lwp),
991                 &lwp, sizeof(lwpid_t));
992         if (ret != 0)
993                 return (P2T(ret));
994         if (lwp != 0)
995                 return (0);
996
997         tmbx_addr = tcb_addr + ta->thread_off_tmbx;
998         /*
999          * context is in userland, some architectures store
1000          * single step status in registers, we should change
1001          * these registers.
1002          */
1003         ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
1004         if (ret == 0) {
1005                 pt_ucontext_to_reg(&tmbx.tm_context, &regs);
1006                 /* only write out if it is really changed. */
1007                 if (pt_reg_sstep(&regs, step) != 0) {
1008                         pt_reg_to_ucontext(&regs, &tmbx.tm_context);
1009                         ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx,
1010                                          sizeof(tmbx));
1011                 }
1012         }
1013         return (P2T(ret));
1014 }
1015
1016 static void
1017 pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp)
1018 {
1019         int i;
1020
1021         for (i = 0; i < ta->map_len; ++i) {
1022                 if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) {
1023                         ta->map[i].type = PT_NONE;
1024                         return;
1025                 }
1026         }
1027 }
1028
1029 static int
1030 pt_validate(const td_thrhandle_t *th)
1031 {
1032
1033         if (th->th_tid < 0 || th->th_tid >= th->th_ta->map_len ||
1034             th->th_ta->map[th->th_tid].type == PT_NONE)
1035                 return (TD_NOTHR);
1036         return (TD_OK);
1037 }
1038
1039 td_err_e
1040 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
1041                     void **address)
1042 {
1043         char *obj_entry;
1044         const td_thragent_t *ta = th->th_ta;
1045         psaddr_t tcb_addr, *dtv_addr;
1046         int tls_index, ret;
1047
1048         /* linkmap is a member of Obj_Entry */
1049         obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
1050
1051         /* get tlsindex of the object file */
1052         ret = ps_pread(ta->ph,
1053                 obj_entry + ta->thread_off_tlsindex,
1054                 &tls_index, sizeof(tls_index));
1055         if (ret != 0)
1056                 return (P2T(ret));
1057
1058         /* get thread tcb */
1059         ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
1060                 ta->thread_off_tcb,
1061                 &tcb_addr, sizeof(tcb_addr));
1062         if (ret != 0)
1063                 return (P2T(ret));
1064
1065         /* get dtv array address */
1066         ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
1067                 &dtv_addr, sizeof(dtv_addr));
1068         if (ret != 0)
1069                 return (P2T(ret));
1070         /* now get the object's tls block base address */
1071         ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
1072                 sizeof(*address));
1073         if (ret != 0)
1074                 return (P2T(ret));
1075
1076         *address += offset;
1077         return (TD_OK);
1078 }
1079
1080 struct ta_ops libpthread_db_ops = {
1081         .to_init                = pt_init,
1082         .to_ta_clear_event      = pt_ta_clear_event,
1083         .to_ta_delete           = pt_ta_delete,
1084         .to_ta_event_addr       = pt_ta_event_addr,
1085         .to_ta_event_getmsg     = pt_ta_event_getmsg,
1086         .to_ta_map_id2thr       = pt_ta_map_id2thr,
1087         .to_ta_map_lwp2thr      = pt_ta_map_lwp2thr,
1088         .to_ta_new              = pt_ta_new,
1089         .to_ta_set_event        = pt_ta_set_event,
1090         .to_ta_thr_iter         = pt_ta_thr_iter,
1091         .to_ta_tsd_iter         = pt_ta_tsd_iter,
1092         .to_thr_clear_event     = pt_thr_clear_event,
1093         .to_thr_dbresume        = pt_thr_dbresume,
1094         .to_thr_dbsuspend       = pt_thr_dbsuspend,
1095         .to_thr_event_enable    = pt_thr_event_enable,
1096         .to_thr_event_getmsg    = pt_thr_event_getmsg,
1097         .to_thr_get_info        = pt_thr_get_info,
1098         .to_thr_getfpregs       = pt_thr_getfpregs,
1099         .to_thr_getgregs        = pt_thr_getgregs,
1100         .to_thr_set_event       = pt_thr_set_event,
1101         .to_thr_setfpregs       = pt_thr_setfpregs,
1102         .to_thr_setgregs        = pt_thr_setgregs,
1103         .to_thr_validate        = pt_thr_validate,
1104         .to_thr_tls_get_addr    = pt_thr_tls_get_addr,
1105
1106         /* FreeBSD specific extensions. */
1107         .to_thr_sstep           = pt_thr_sstep,
1108 #ifdef __i386__
1109         .to_thr_getxmmregs      = pt_thr_getxmmregs,
1110         .to_thr_setxmmregs      = pt_thr_setxmmregs,
1111 #endif
1112 };