xref: /freebsd/lib/libthread_db/libthr_db.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2005 David Xu
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <proc_service.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/ptrace.h>
37 #include <thread_db.h>
38 #include <unistd.h>
39 
40 #include "thread_db_int.h"
41 
42 #define	TERMINATED	1
43 
44 struct td_thragent {
45 	TD_THRAGENT_FIELDS;
46 	psaddr_t	libthr_debug_addr;
47 	psaddr_t	thread_list_addr;
48 	psaddr_t	thread_active_threads_addr;
49 	psaddr_t	thread_keytable_addr;
50 	psaddr_t	thread_last_event_addr;
51 	psaddr_t	thread_event_mask_addr;
52 	psaddr_t	thread_bp_create_addr;
53 	psaddr_t	thread_bp_death_addr;
54 	int		thread_off_dtv;
55 	int		thread_off_tlsindex;
56 	int		thread_off_attr_flags;
57 	int		thread_size_key;
58 	int		thread_off_tcb;
59 	int		thread_off_linkmap;
60 	int		thread_off_next;
61 	int		thread_off_state;
62 	int		thread_off_tid;
63 	int		thread_max_keys;
64 	int		thread_off_key_allocated;
65 	int		thread_off_key_destructor;
66 	int		thread_off_report_events;
67 	int		thread_off_event_mask;
68 	int		thread_off_event_buf;
69 	int		thread_state_zoombie;
70 	int		thread_state_running;
71 };
72 
73 #define P2T(c) ps2td(c)
74 
75 static int pt_validate(const td_thrhandle_t *th);
76 
77 static int
78 ps2td(int c)
79 {
80 	switch (c) {
81 	case PS_OK:
82 		return TD_OK;
83 	case PS_ERR:
84 		return TD_ERR;
85 	case PS_BADPID:
86 		return TD_BADPH;
87 	case PS_BADLID:
88 		return TD_NOLWP;
89 	case PS_BADADDR:
90 		return TD_ERR;
91 	case PS_NOSYM:
92 		return TD_NOLIBTHREAD;
93 	case PS_NOFREGS:
94 		return TD_NOFPREGS;
95 	default:
96 		return TD_ERR;
97 	}
98 }
99 
100 static td_err_e
101 pt_init(void)
102 {
103 	return (0);
104 }
105 
106 static td_err_e
107 pt_ta_new(struct ps_prochandle *ph, td_thragent_t **pta)
108 {
109 #define LOOKUP_SYM(proc, sym, addr) 			\
110 	ret = ps_pglobal_lookup(proc, NULL, sym, addr);	\
111 	if (ret != 0) {					\
112 		TDBG("can not find symbol: %s\n", sym);	\
113 		ret = TD_NOLIBTHREAD;			\
114 		goto error;				\
115 	}
116 
117 #define	LOOKUP_VAL(proc, sym, val)			\
118 	ret = ps_pglobal_lookup(proc, NULL, sym, &vaddr);\
119 	if (ret != 0) {					\
120 		TDBG("can not find symbol: %s\n", sym);	\
121 		ret = TD_NOLIBTHREAD;			\
122 		goto error;				\
123 	}						\
124 	ret = ps_pread(proc, vaddr, val, sizeof(int));	\
125 	if (ret != 0) {					\
126 		TDBG("can not read value of %s\n", sym);\
127 		ret = TD_NOLIBTHREAD;			\
128 		goto error;				\
129 	}
130 
131 	td_thragent_t *ta;
132 	psaddr_t vaddr;
133 	int dbg;
134 	int ret;
135 
136 	TDBG_FUNC();
137 
138 	ta = malloc(sizeof(td_thragent_t));
139 	if (ta == NULL)
140 		return (TD_MALLOC);
141 
142 	ta->ph = ph;
143 
144 	LOOKUP_SYM(ph, "_libthr_debug",		&ta->libthr_debug_addr);
145 	LOOKUP_SYM(ph, "_thread_list",		&ta->thread_list_addr);
146 	LOOKUP_SYM(ph, "_thread_active_threads",&ta->thread_active_threads_addr);
147 	LOOKUP_SYM(ph, "_thread_keytable",	&ta->thread_keytable_addr);
148 	LOOKUP_SYM(ph, "_thread_last_event",	&ta->thread_last_event_addr);
149 	LOOKUP_SYM(ph, "_thread_event_mask",	&ta->thread_event_mask_addr);
150 	LOOKUP_SYM(ph, "_thread_bp_create",	&ta->thread_bp_create_addr);
151 	LOOKUP_SYM(ph, "_thread_bp_death",	&ta->thread_bp_death_addr);
152 	LOOKUP_VAL(ph, "_thread_off_dtv",	&ta->thread_off_dtv);
153 	LOOKUP_VAL(ph, "_thread_off_tlsindex",	&ta->thread_off_tlsindex);
154 	LOOKUP_VAL(ph, "_thread_off_attr_flags",	&ta->thread_off_attr_flags);
155 	LOOKUP_VAL(ph, "_thread_size_key",	&ta->thread_size_key);
156 	LOOKUP_VAL(ph, "_thread_off_tcb",	&ta->thread_off_tcb);
157 	LOOKUP_VAL(ph, "_thread_off_tid",	&ta->thread_off_tid);
158 	LOOKUP_VAL(ph, "_thread_off_linkmap",	&ta->thread_off_linkmap);
159 	LOOKUP_VAL(ph, "_thread_off_next",	&ta->thread_off_next);
160 	LOOKUP_VAL(ph, "_thread_off_state",	&ta->thread_off_state);
161 	LOOKUP_VAL(ph, "_thread_max_keys",	&ta->thread_max_keys);
162 	LOOKUP_VAL(ph, "_thread_off_key_allocated", &ta->thread_off_key_allocated);
163 	LOOKUP_VAL(ph, "_thread_off_key_destructor", &ta->thread_off_key_destructor);
164 	LOOKUP_VAL(ph, "_thread_state_running", &ta->thread_state_running);
165 	LOOKUP_VAL(ph, "_thread_state_zoombie", &ta->thread_state_zoombie);
166 	LOOKUP_VAL(ph, "_thread_off_report_events", &ta->thread_off_report_events);
167 	LOOKUP_VAL(ph, "_thread_off_event_mask", &ta->thread_off_event_mask);
168 	LOOKUP_VAL(ph, "_thread_off_event_buf", &ta->thread_off_event_buf);
169 	dbg = getpid();
170 	/*
171 	 * If this fails it probably means we're debugging a core file and
172 	 * can't write to it.
173 	 */
174 	ps_pwrite(ph, ta->libthr_debug_addr, &dbg, sizeof(int));
175 	*pta = ta;
176 	return (0);
177 
178 error:
179 	free(ta);
180 	return (ret);
181 }
182 
183 static td_err_e
184 pt_ta_delete(td_thragent_t *ta)
185 {
186 	int dbg;
187 
188 	TDBG_FUNC();
189 
190 	dbg = 0;
191 	/*
192 	 * Error returns from this write are not really a problem;
193 	 * the process doesn't exist any more.
194 	 */
195 	ps_pwrite(ta->ph, ta->libthr_debug_addr, &dbg, sizeof(int));
196 	free(ta);
197 	return (TD_OK);
198 }
199 
200 static td_err_e
201 pt_ta_map_id2thr(const td_thragent_t *ta, thread_t id, td_thrhandle_t *th)
202 {
203 	TAILQ_HEAD(, pthread) thread_list;
204 	psaddr_t pt;
205 	long lwp;
206 	int ret;
207 
208 	TDBG_FUNC();
209 
210 	if (id == 0)
211 		return (TD_NOTHR);
212 	ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
213 		sizeof(thread_list));
214 	if (ret != 0)
215 		return (P2T(ret));
216 	/* Iterate through thread list to find pthread */
217 	pt = (psaddr_t)thread_list.tqh_first;
218 	while (pt != NULL) {
219 		ret = ps_pread(ta->ph, pt + ta->thread_off_tid,
220 			       &lwp, sizeof(lwp));
221 		if (ret != 0)
222 			return (P2T(ret));
223 		if (lwp == id)
224 			break;
225 		/* get next thread */
226 		ret = ps_pread(ta->ph,
227 				pt + ta->thread_off_next,
228 				&pt, sizeof(pt));
229 		if (ret != 0)
230 			return (P2T(ret));
231 	}
232 	if (pt == NULL)
233 		return (TD_NOTHR);
234 	th->th_ta = ta;
235 	th->th_tid = id;
236 	th->th_thread = pt;
237 	return (TD_OK);
238 }
239 
240 static td_err_e
241 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
242 {
243 	return (pt_ta_map_id2thr(ta, lwp, th));
244 }
245 
246 static td_err_e
247 pt_ta_thr_iter(const td_thragent_t *ta,
248                td_thr_iter_f *callback, void *cbdata_p,
249                td_thr_state_e state, int ti_pri,
250                sigset_t *ti_sigmask_p,
251                unsigned int ti_user_flags)
252 {
253 	TAILQ_HEAD(, pthread) thread_list;
254 	td_thrhandle_t th;
255 	psaddr_t pt;
256 	long lwp;
257 	int ret;
258 
259 	TDBG_FUNC();
260 
261 	ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
262 		       sizeof(thread_list));
263 	if (ret != 0)
264 		return (P2T(ret));
265 	pt = (psaddr_t)thread_list.tqh_first;
266 	while (pt != 0) {
267 		ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp,
268 			      sizeof(lwp));
269 		if (ret != 0)
270 			return (P2T(ret));
271 		if (lwp != 0 && lwp != TERMINATED) {
272 			th.th_ta = ta;
273 			th.th_tid = (thread_t)lwp;
274 			th.th_thread = pt;
275 			if ((*callback)(&th, cbdata_p))
276 				return (TD_DBERR);
277 		}
278 		/* get next thread */
279 		ret = ps_pread(ta->ph, pt + ta->thread_off_next, &pt,
280 			       sizeof(pt));
281 		if (ret != 0)
282 			return (P2T(ret));
283 	}
284 	return (TD_OK);
285 }
286 
287 static td_err_e
288 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
289 {
290 	char *keytable;
291 	void *destructor;
292 	int i, ret, allocated;
293 
294 	TDBG_FUNC();
295 
296 	keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
297 	if (keytable == NULL)
298 		return (TD_MALLOC);
299 	ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
300 	               ta->thread_max_keys * ta->thread_size_key);
301 	if (ret != 0) {
302 		free(keytable);
303 		return (P2T(ret));
304 	}
305 	for (i = 0; i < ta->thread_max_keys; i++) {
306 		allocated = *(int *)(keytable + i * ta->thread_size_key +
307 			ta->thread_off_key_allocated);
308 		destructor = *(void **)(keytable + i * ta->thread_size_key +
309 			ta->thread_off_key_destructor);
310 		if (allocated) {
311 			ret = (ki)(i, destructor, arg);
312 			if (ret != 0) {
313 				free(keytable);
314 				return (TD_DBERR);
315 			}
316 		}
317 	}
318 	free(keytable);
319 	return (TD_OK);
320 }
321 
322 static td_err_e
323 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
324 {
325 
326 	TDBG_FUNC();
327 
328 	switch (event) {
329 	case TD_CREATE:
330 		ptr->type = NOTIFY_BPT;
331 		ptr->u.bptaddr = ta->thread_bp_create_addr;
332 		return (0);
333 	case TD_DEATH:
334 		ptr->type = NOTIFY_BPT;
335 		ptr->u.bptaddr = ta->thread_bp_death_addr;
336 		return (0);
337 	default:
338 		return (TD_ERR);
339 	}
340 }
341 
342 static td_err_e
343 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
344 {
345 	td_thr_events_t mask;
346 	int ret;
347 
348 	TDBG_FUNC();
349 	ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
350 		sizeof(mask));
351 	if (ret != 0)
352 		return (P2T(ret));
353 	mask |= *events;
354 	ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
355 		sizeof(mask));
356 	return (P2T(ret));
357 }
358 
359 static td_err_e
360 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
361 {
362 	td_thr_events_t mask;
363 	int ret;
364 
365 	TDBG_FUNC();
366 	ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
367 		sizeof(mask));
368 	if (ret != 0)
369 		return (P2T(ret));
370 	mask &= ~*events;
371 	ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
372 		sizeof(mask));
373 	return (P2T(ret));
374 }
375 
376 static td_err_e
377 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
378 {
379 	static td_thrhandle_t handle;
380 
381 	psaddr_t pt, pt_temp;
382 	td_thr_events_e	tmp;
383 	long lwp;
384 	int ret;
385 
386 	TDBG_FUNC();
387 
388 	ret = ps_pread(ta->ph, ta->thread_last_event_addr, &pt, sizeof(pt));
389 	if (ret != 0)
390 		return (P2T(ret));
391 	if (pt == NULL)
392 		return (TD_NOMSG);
393 	/*
394 	 * Take the event pointer, at the time, libthr only reports event
395 	 * once a time, so it is not a link list.
396 	 */
397 	pt_temp = NULL;
398 	ps_pwrite(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
399 
400 	/* Read event info */
401 	ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
402 	if (ret != 0)
403 		return (P2T(ret));
404 	if (msg->event == 0)
405 		return (TD_NOMSG);
406 	/* Clear event */
407 	tmp = 0;
408 	ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
409 	/* Convert event */
410 	pt = (psaddr_t)msg->th_p;
411 	ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp, sizeof(lwp));
412 	if (ret != 0)
413 		return (P2T(ret));
414 	handle.th_ta = ta;
415 	handle.th_tid = lwp;
416 	handle.th_thread = pt;
417 	msg->th_p = &handle;
418 	return (0);
419 }
420 
421 static td_err_e
422 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
423 {
424 	td_thragent_t *ta = (td_thragent_t *)th->th_ta;
425 	int ret;
426 
427 	TDBG_FUNC();
428 
429 	ret = pt_validate(th);
430 	if (ret)
431 		return (ret);
432 
433 	if (suspend)
434 		ret = ps_lstop(ta->ph, th->th_tid);
435 	else
436 		ret = ps_lcontinue(ta->ph, th->th_tid);
437 	return (P2T(ret));
438 }
439 
440 static td_err_e
441 pt_thr_dbresume(const td_thrhandle_t *th)
442 {
443 	TDBG_FUNC();
444 
445 	return pt_dbsuspend(th, 0);
446 }
447 
448 static td_err_e
449 pt_thr_dbsuspend(const td_thrhandle_t *th)
450 {
451 	TDBG_FUNC();
452 
453 	return pt_dbsuspend(th, 1);
454 }
455 
456 static td_err_e
457 pt_thr_validate(const td_thrhandle_t *th)
458 {
459 	td_thrhandle_t temp;
460 	int ret;
461 
462 	TDBG_FUNC();
463 
464 	ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, &temp);
465 	return (ret);
466 }
467 
468 static td_err_e
469 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
470 {
471 	const td_thragent_t *ta = th->th_ta;
472 	struct ptrace_lwpinfo linfo;
473 	int state;
474 	int ret;
475 
476 	TDBG_FUNC();
477 
478 	bzero(info, sizeof(*info));
479 	ret = pt_validate(th);
480 	if (ret)
481 		return (ret);
482 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_state,
483 	               &state, sizeof(state));
484 	if (ret != 0)
485 		return (P2T(ret));
486 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_report_events,
487 		&info->ti_traceme, sizeof(int));
488 	if (ret != 0)
489 		return (P2T(ret));
490 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
491 		&info->ti_events, sizeof(td_thr_events_t));
492 	if (ret != 0)
493 		return (P2T(ret));
494 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
495 		&info->ti_tls, sizeof(void *));
496 	info->ti_lid = th->th_tid;
497 	info->ti_tid = th->th_tid;
498 	info->ti_thread = th->th_thread;
499 	info->ti_ta_p = th->th_ta;
500 	ret = ps_linfo(ta->ph, th->th_tid, &linfo);
501 	if (ret == PS_OK) {
502 		info->ti_sigmask = linfo.pl_sigmask;
503 		info->ti_pending = linfo.pl_siglist;
504 	} else
505 		return (ret);
506 	if (state == ta->thread_state_running)
507 		info->ti_state = TD_THR_RUN;
508 	else if (state == ta->thread_state_zoombie)
509 		info->ti_state = TD_THR_ZOMBIE;
510 	else
511 		info->ti_state = TD_THR_SLEEP;
512 	info->ti_type = TD_THR_USER;
513 	return (0);
514 }
515 
516 #ifdef __i386__
517 static td_err_e
518 pt_thr_getxmmregs(const td_thrhandle_t *th, char *fxsave)
519 {
520 	const td_thragent_t *ta = th->th_ta;
521 	int ret;
522 
523 	TDBG_FUNC();
524 
525 	ret = pt_validate(th);
526 	if (ret)
527 		return (ret);
528 
529 	ret = ps_lgetxmmregs(ta->ph, th->th_tid, fxsave);
530 	return (P2T(ret));
531 }
532 #endif
533 
534 static td_err_e
535 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
536 {
537 	const td_thragent_t *ta = th->th_ta;
538 	int ret;
539 
540 	TDBG_FUNC();
541 
542 	ret = pt_validate(th);
543 	if (ret)
544 		return (ret);
545 
546 	ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs);
547 	return (P2T(ret));
548 }
549 
550 static td_err_e
551 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
552 {
553 	const td_thragent_t *ta = th->th_ta;
554 	int ret;
555 
556 	TDBG_FUNC();
557 
558 	ret = pt_validate(th);
559 	if (ret)
560 		return (ret);
561 
562 	ret = ps_lgetregs(ta->ph, th->th_tid, gregs);
563 	return (P2T(ret));
564 }
565 
566 #ifdef __i386__
567 static td_err_e
568 pt_thr_setxmmregs(const td_thrhandle_t *th, const char *fxsave)
569 {
570 	const td_thragent_t *ta = th->th_ta;
571 	int ret;
572 
573 	TDBG_FUNC();
574 
575 	ret = pt_validate(th);
576 	if (ret)
577 		return (ret);
578 
579 	ret = ps_lsetxmmregs(ta->ph, th->th_tid, fxsave);
580 	return (P2T(ret));
581 }
582 #endif
583 
584 static td_err_e
585 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
586 {
587 	const td_thragent_t *ta = th->th_ta;
588 	int ret;
589 
590 	TDBG_FUNC();
591 
592 	ret = pt_validate(th);
593 	if (ret)
594 		return (ret);
595 
596 	ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs);
597 	return (P2T(ret));
598 }
599 
600 static td_err_e
601 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
602 {
603 	const td_thragent_t *ta = th->th_ta;
604 	int ret;
605 
606 	TDBG_FUNC();
607 
608 	ret = pt_validate(th);
609 	if (ret)
610 		return (ret);
611 
612 	ret = ps_lsetregs(ta->ph, th->th_tid, gregs);
613 	return (P2T(ret));
614 }
615 
616 static td_err_e
617 pt_thr_event_enable(const td_thrhandle_t *th, int en)
618 {
619 	const td_thragent_t *ta = th->th_ta;
620 	int ret;
621 
622 	TDBG_FUNC();
623 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events,
624 		&en, sizeof(int));
625 	return (P2T(ret));
626 }
627 
628 static td_err_e
629 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
630 {
631 	const td_thragent_t *ta = th->th_ta;
632 	td_thr_events_t mask;
633 	int ret;
634 
635 	TDBG_FUNC();
636 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
637 			&mask, sizeof(mask));
638 	mask |= *setp;
639 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
640 			&mask, sizeof(mask));
641 	return (P2T(ret));
642 }
643 
644 static td_err_e
645 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
646 {
647 	const td_thragent_t *ta = th->th_ta;
648 	td_thr_events_t mask;
649 	int ret;
650 
651 	TDBG_FUNC();
652 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
653 			&mask, sizeof(mask));
654 	mask &= ~*setp;
655 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
656 			&mask, sizeof(mask));
657 	return (P2T(ret));
658 }
659 
660 static td_err_e
661 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
662 {
663 	static td_thrhandle_t handle;
664 	td_thragent_t *ta = (td_thragent_t *)th->th_ta;
665 	psaddr_t pt, pt_temp;
666 	long lwp;
667 	int ret;
668 	td_thr_events_e	tmp;
669 
670 	TDBG_FUNC();
671 	pt = th->th_thread;
672 	ret = ps_pread(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
673 	if (ret != 0)
674 		return (P2T(ret));
675 	/* Get event */
676 	ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
677 	if (ret != 0)
678 		return (P2T(ret));
679 	if (msg->event == 0)
680 		return (TD_NOMSG);
681 	/*
682 	 * Take the event pointer, at the time, libthr only reports event
683 	 * once a time, so it is not a link list.
684 	 */
685 	if (pt == pt_temp) {
686 		pt_temp = NULL;
687 		ps_pwrite(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
688 	}
689 	/* Clear event */
690 	tmp = 0;
691 	ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
692 	/* Convert event */
693 	pt = (psaddr_t)msg->th_p;
694 	ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp, sizeof(lwp));
695 	if (ret != 0)
696 		return (P2T(ret));
697 	handle.th_ta = ta;
698 	handle.th_tid = lwp;
699 	handle.th_thread = pt;
700 	msg->th_p = &handle;
701 	return (0);
702 }
703 
704 static td_err_e
705 pt_thr_sstep(const td_thrhandle_t *th, int step)
706 {
707 	TDBG_FUNC();
708 
709 	return pt_validate(th);
710 }
711 
712 static int
713 pt_validate(const td_thrhandle_t *th)
714 {
715 
716 	if (th->th_tid == 0 || th->th_thread == NULL)
717 		return (TD_ERR);
718 	return (TD_OK);
719 }
720 
721 static td_err_e
722 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
723 		    void **address)
724 {
725 	char *obj_entry;
726 	const td_thragent_t *ta = th->th_ta;
727 	psaddr_t tcb_addr, *dtv_addr;
728 	int tls_index, ret;
729 
730 	/* linkmap is a member of Obj_Entry */
731 	obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
732 
733 	/* get tlsindex of the object file */
734 	ret = ps_pread(ta->ph,
735 		obj_entry + ta->thread_off_tlsindex,
736 		&tls_index, sizeof(tls_index));
737 	if (ret != 0)
738 		return (P2T(ret));
739 
740 	/* get thread tcb */
741 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
742 		&tcb_addr, sizeof(tcb_addr));
743 	if (ret != 0)
744 		return (P2T(ret));
745 
746 	/* get dtv array address */
747 	ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
748 		&dtv_addr, sizeof(dtv_addr));
749 	if (ret != 0)
750 		return (P2T(ret));
751 	/* now get the object's tls block base address */
752 	ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
753 		sizeof(*address));
754 	if (ret != 0)
755 		return (P2T(ret));
756 
757 	*address += offset;
758 	return (TD_OK);
759 }
760 
761 struct ta_ops libthr_db_ops = {
762 	.to_init		= pt_init,
763 	.to_ta_clear_event	= pt_ta_clear_event,
764 	.to_ta_delete		= pt_ta_delete,
765 	.to_ta_event_addr	= pt_ta_event_addr,
766 	.to_ta_event_getmsg	= pt_ta_event_getmsg,
767 	.to_ta_map_id2thr	= pt_ta_map_id2thr,
768 	.to_ta_map_lwp2thr	= pt_ta_map_lwp2thr,
769 	.to_ta_new		= pt_ta_new,
770 	.to_ta_set_event	= pt_ta_set_event,
771 	.to_ta_thr_iter		= pt_ta_thr_iter,
772 	.to_ta_tsd_iter		= pt_ta_tsd_iter,
773 	.to_thr_clear_event	= pt_thr_clear_event,
774 	.to_thr_dbresume	= pt_thr_dbresume,
775 	.to_thr_dbsuspend	= pt_thr_dbsuspend,
776 	.to_thr_event_enable	= pt_thr_event_enable,
777 	.to_thr_event_getmsg	= pt_thr_event_getmsg,
778 	.to_thr_get_info	= pt_thr_get_info,
779 	.to_thr_getfpregs	= pt_thr_getfpregs,
780 	.to_thr_getgregs	= pt_thr_getgregs,
781 	.to_thr_set_event	= pt_thr_set_event,
782 	.to_thr_setfpregs	= pt_thr_setfpregs,
783 	.to_thr_setgregs	= pt_thr_setgregs,
784 	.to_thr_validate	= pt_thr_validate,
785 	.to_thr_tls_get_addr	= pt_thr_tls_get_addr,
786 
787 	/* FreeBSD specific extensions. */
788 	.to_thr_sstep		= pt_thr_sstep,
789 #ifdef __i386__
790 	.to_thr_getxmmregs	= pt_thr_getxmmregs,
791 	.to_thr_setxmmregs	= pt_thr_setxmmregs,
792 #endif
793 };
794