xref: /freebsd/lib/libthread_db/libpthread_db.c (revision a80845ea)
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 static td_err_e
645 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
646 {
647 	const td_thragent_t *ta = th->th_ta;
648 	struct kse_thr_mailbox tmbx;
649 	psaddr_t tcb_addr, tmbx_addr, ptr;
650 	lwpid_t lwp;
651 	int ret;
652 
653 	TDBG_FUNC();
654 
655 	ret = pt_validate(th);
656 	if (ret)
657 		return (ret);
658 
659 	if (ta->map[th->th_tid].type == PT_LWP) {
660 		ret = ps_lgetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
661 		return (P2T(ret));
662 	}
663 
664 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
665 	               &tcb_addr, sizeof(tcb_addr));
666 	if (ret != 0)
667 		return (P2T(ret));
668 	tmbx_addr = tcb_addr + ta->thread_off_tmbx;
669 	ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
670 	ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
671 	if (ret != 0)
672 		return (P2T(ret));
673 	if (lwp != 0) {
674 		ret = ps_lgetfpregs(ta->ph, lwp, fpregs);
675 		return (P2T(ret));
676 	}
677 
678 	ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
679 	if (ret != 0)
680 		return (P2T(ret));
681 	pt_ucontext_to_fpreg(&tmbx.tm_context, fpregs);
682 	return (0);
683 }
684 
685 static td_err_e
686 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
687 {
688 	const td_thragent_t *ta = th->th_ta;
689 	struct kse_thr_mailbox tmbx;
690 	psaddr_t tcb_addr, tmbx_addr, ptr;
691 	lwpid_t lwp;
692 	int ret;
693 
694 	TDBG_FUNC();
695 
696 	ret = pt_validate(th);
697 	if (ret)
698 		return (ret);
699 
700 	if (ta->map[th->th_tid].type == PT_LWP) {
701 		ret = ps_lgetregs(ta->ph,
702 		                  ta->map[th->th_tid].lwp, gregs);
703 		return (P2T(ret));
704 	}
705 
706 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr + ta->thread_off_tcb,
707 			&tcb_addr, sizeof(tcb_addr));
708 	if (ret != 0)
709 		return (P2T(ret));
710 	tmbx_addr = tcb_addr + ta->thread_off_tmbx;
711 	ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
712 	ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
713 	if (ret != 0)
714 		return (P2T(ret));
715 	if (lwp != 0) {
716 		ret = ps_lgetregs(ta->ph, lwp, gregs);
717 		return (P2T(ret));
718 	}
719 	ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
720 	if (ret != 0)
721 		return (P2T(ret));
722 	pt_ucontext_to_reg(&tmbx.tm_context, gregs);
723 	return (0);
724 }
725 
726 static td_err_e
727 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
728 {
729 	const td_thragent_t *ta = th->th_ta;
730 	struct kse_thr_mailbox tmbx;
731 	psaddr_t tcb_addr, tmbx_addr, ptr;
732 	lwpid_t lwp;
733 	int ret;
734 
735 	TDBG_FUNC();
736 
737 	ret = pt_validate(th);
738 	if (ret)
739 		return (ret);
740 
741 	if (ta->map[th->th_tid].type == PT_LWP) {
742 		ret = ps_lsetfpregs(ta->ph, ta->map[th->th_tid].lwp, fpregs);
743 		return (P2T(ret));
744 	}
745 
746 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
747 	                ta->thread_off_tcb,
748                         &tcb_addr, sizeof(tcb_addr));
749 	if (ret != 0)
750 		return (P2T(ret));
751 	tmbx_addr = tcb_addr + ta->thread_off_tmbx;
752 	ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
753 	ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
754 	if (ret != 0)
755 		return (P2T(ret));
756 	if (lwp != 0) {
757 		ret = ps_lsetfpregs(ta->ph, lwp, fpregs);
758 		return (P2T(ret));
759 	}
760 	/*
761 	 * Read a copy of context, this makes sure that registers
762 	 * not covered by structure reg won't be clobbered
763 	 */
764 	ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
765 	if (ret != 0)
766 		return (P2T(ret));
767 
768 	pt_fpreg_to_ucontext(fpregs, &tmbx.tm_context);
769 	ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
770 	return (P2T(ret));
771 }
772 
773 static td_err_e
774 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
775 {
776 	const td_thragent_t *ta = th->th_ta;
777 	struct kse_thr_mailbox tmbx;
778 	psaddr_t tcb_addr, tmbx_addr, ptr;
779 	lwpid_t lwp;
780 	int ret;
781 
782 	TDBG_FUNC();
783 
784 	ret = pt_validate(th);
785 	if (ret)
786 		return (ret);
787 
788 	if (ta->map[th->th_tid].type == PT_LWP) {
789 		ret = ps_lsetregs(ta->ph, ta->map[th->th_tid].lwp, gregs);
790 		return (P2T(ret));
791 	}
792 
793 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
794 	                ta->thread_off_tcb,
795 	                &tcb_addr, sizeof(tcb_addr));
796 	if (ret != 0)
797 		return (P2T(ret));
798 	tmbx_addr = tcb_addr + ta->thread_off_tmbx;
799 	ptr = tmbx_addr + offsetof(struct kse_thr_mailbox, tm_lwp);
800 	ret = ps_pread(ta->ph, ptr, &lwp, sizeof(lwpid_t));
801 	if (ret != 0)
802 		return (P2T(ret));
803 	if (lwp != 0) {
804 		ret = ps_lsetregs(ta->ph, lwp, gregs);
805 		return (P2T(ret));
806 	}
807 
808 	/*
809 	 * Read a copy of context, make 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 	pt_reg_to_ucontext(gregs, &tmbx.tm_context);
816 	ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
817 	return (P2T(ret));
818 }
819 
820 static td_err_e
821 pt_thr_event_enable(const td_thrhandle_t *th, int en)
822 {
823 	TDBG_FUNC();
824 	return (0);
825 }
826 
827 static td_err_e
828 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
829 {
830 	TDBG_FUNC();
831 	return (0);
832 }
833 
834 static td_err_e
835 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
836 {
837 	TDBG_FUNC();
838 	return (0);
839 }
840 
841 static td_err_e
842 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
843 {
844 	TDBG_FUNC();
845 	return (TD_NOMSG);
846 }
847 
848 static td_err_e
849 pt_thr_sstep(const td_thrhandle_t *th, int step)
850 {
851 	const td_thragent_t *ta = th->th_ta;
852 	struct kse_thr_mailbox tmbx;
853 	struct reg regs;
854 	psaddr_t tcb_addr, tmbx_addr;
855 	uint32_t dflags;
856 	lwpid_t lwp;
857 	int ret;
858 
859 	TDBG_FUNC();
860 
861 	ret = pt_validate(th);
862 	if (ret)
863 		return (ret);
864 
865 	if (ta->map[th->th_tid].type == PT_LWP)
866 		return (TD_BADTH);
867 
868 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
869 	                ta->thread_off_tcb,
870 	                &tcb_addr, sizeof(tcb_addr));
871 	if (ret != 0)
872 		return (P2T(ret));
873 
874 	/* Clear or set single step flag in thread mailbox */
875 	ret = ps_pread(ta->ph,
876 		tcb_addr + ta->thread_off_tmbx +
877 		 offsetof(struct kse_thr_mailbox, tm_dflags),
878 		&dflags, sizeof(uint32_t));
879 	if (ret != 0)
880 		return (P2T(ret));
881 	if (step != 0)
882 		dflags |= TMDF_SSTEP;
883 	else
884 		dflags &= ~TMDF_SSTEP;
885 	ret = ps_pwrite(ta->ph,
886 		tcb_addr + ta->thread_off_tmbx +
887 		 offsetof(struct kse_thr_mailbox, tm_dflags),
888 	        &dflags, sizeof(uint32_t));
889 	if (ret != 0)
890 		return (P2T(ret));
891 	/* Get lwp */
892 	ret = ps_pread(ta->ph,
893 		tcb_addr + ta->thread_off_tmbx +
894 		 offsetof(struct kse_thr_mailbox, tm_lwp),
895 		&lwp, sizeof(lwpid_t));
896 	if (ret != 0)
897 		return (P2T(ret));
898 	if (lwp != 0)
899 		return (0);
900 
901 	tmbx_addr = tcb_addr + ta->thread_off_tmbx;
902 	/*
903 	 * context is in userland, some architectures store
904 	 * single step status in registers, we should change
905 	 * these registers.
906 	 */
907 	ret = ps_pread(ta->ph, tmbx_addr, &tmbx, sizeof(tmbx));
908 	if (ret == 0) {
909 		pt_ucontext_to_reg(&tmbx.tm_context, &regs);
910 		/* only write out if it is really changed. */
911 		if (pt_reg_sstep(&regs, step) != 0) {
912 			pt_reg_to_ucontext(&regs, &tmbx.tm_context);
913 			ret = ps_pwrite(ta->ph, tmbx_addr, &tmbx,
914 			                 sizeof(tmbx));
915 		}
916 	}
917 	return (P2T(ret));
918 }
919 
920 static void
921 pt_unmap_lwp(const td_thragent_t *ta, lwpid_t lwp)
922 {
923 	int i;
924 
925 	for (i = 0; i < ta->map_len; ++i) {
926 		if (ta->map[i].type == PT_LWP && ta->map[i].lwp == lwp) {
927 			ta->map[i].type = PT_NONE;
928 			return;
929 		}
930 	}
931 }
932 
933 static int
934 pt_validate(const td_thrhandle_t *th)
935 {
936 
937 	if (th->th_tid < 0 || th->th_tid >= th->th_ta->map_len ||
938 	    th->th_ta->map[th->th_tid].type == PT_NONE)
939 		return (TD_NOTHR);
940 	return (TD_OK);
941 }
942 
943 td_err_e
944 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
945 		    void **address)
946 {
947 	char *obj_entry;
948 	const td_thragent_t *ta = th->th_ta;
949 	psaddr_t tcb_addr, *dtv_addr, tcb_tp;
950 	int tls_index, ret;
951 
952 	/* linkmap is a member of Obj_Entry */
953 	obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
954 
955 	/* get tlsindex of the object file */
956 	ret = ps_pread(ta->ph,
957 		obj_entry + ta->thread_off_tlsindex,
958 		&tls_index, sizeof(tls_index));
959 	if (ret != 0)
960 		return (P2T(ret));
961 
962 	/* get thread tcb */
963 	ret = ps_pread(ta->ph, ta->map[th->th_tid].thr +
964 		ta->thread_off_tcb,
965 		&tcb_addr, sizeof(tcb_addr));
966 	if (ret != 0)
967 		return (P2T(ret));
968 
969 	/* get dtv array address */
970 	ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
971 		&dtv_addr, sizeof(dtv_addr));
972 	if (ret != 0)
973 		return (P2T(ret));
974 	/* now get the object's tls block base address */
975 	ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
976 		sizeof(*address));
977 	if (ret != 0)
978 		return (P2T(ret));
979 
980 	*address += offset;
981 	return (TD_OK);
982 }
983 
984 struct ta_ops libpthread_db_ops = {
985 	.to_init		= pt_init,
986 	.to_ta_clear_event	= pt_ta_clear_event,
987 	.to_ta_delete		= pt_ta_delete,
988 	.to_ta_event_addr	= pt_ta_event_addr,
989 	.to_ta_event_getmsg	= pt_ta_event_getmsg,
990 	.to_ta_map_id2thr	= pt_ta_map_id2thr,
991 	.to_ta_map_lwp2thr	= pt_ta_map_lwp2thr,
992 	.to_ta_new		= pt_ta_new,
993 	.to_ta_set_event	= pt_ta_set_event,
994 	.to_ta_thr_iter		= pt_ta_thr_iter,
995 	.to_ta_tsd_iter		= pt_ta_tsd_iter,
996 	.to_thr_clear_event	= pt_thr_clear_event,
997 	.to_thr_dbresume	= pt_thr_dbresume,
998 	.to_thr_dbsuspend	= pt_thr_dbsuspend,
999 	.to_thr_event_enable	= pt_thr_event_enable,
1000 	.to_thr_event_getmsg	= pt_thr_event_getmsg,
1001 	.to_thr_get_info	= pt_thr_get_info,
1002 	.to_thr_getfpregs	= pt_thr_getfpregs,
1003 	.to_thr_getgregs	= pt_thr_getgregs,
1004 	.to_thr_set_event	= pt_thr_set_event,
1005 	.to_thr_setfpregs	= pt_thr_setfpregs,
1006 	.to_thr_setgregs	= pt_thr_setgregs,
1007 	.to_thr_validate	= pt_thr_validate,
1008 	.to_thr_tls_get_addr	= pt_thr_tls_get_addr,
1009 
1010 	/* FreeBSD specific extensions. */
1011 	.to_thr_sstep		= pt_thr_sstep,
1012 };
1013