xref: /freebsd/lib/libthread_db/libthr_db.c (revision d36dd0b78e967af8cfa003ac39385b87dc558551)
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 	int state;
473 	int ret;
474 
475 	TDBG_FUNC();
476 
477 	ret = pt_validate(th);
478 	if (ret)
479 		return (ret);
480 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_state,
481 	               &state, sizeof(state));
482 	if (ret != 0)
483 		return (P2T(ret));
484 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_report_events,
485 		&info->ti_traceme, sizeof(int));
486 	if (ret != 0)
487 		return (P2T(ret));
488 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
489 		&info->ti_events, sizeof(td_thr_events_t));
490 	if (ret != 0)
491 		return (P2T(ret));
492 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
493 		&info->ti_tls, sizeof(void *));
494 	info->ti_lid = th->th_tid;
495 	info->ti_tid = th->th_tid;
496 	info->ti_thread = th->th_thread;
497 	info->ti_ta_p = th->th_ta;
498 	if (state == ta->thread_state_running)
499 		info->ti_state = TD_THR_RUN;
500 	else if (state == ta->thread_state_zoombie)
501 		info->ti_state = TD_THR_ZOMBIE;
502 	else
503 		info->ti_state = TD_THR_SLEEP;
504 	info->ti_type = TD_THR_USER;
505 	return (0);
506 }
507 
508 static td_err_e
509 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
510 {
511 	const td_thragent_t *ta = th->th_ta;
512 	int ret;
513 
514 	TDBG_FUNC();
515 
516 	ret = pt_validate(th);
517 	if (ret)
518 		return (ret);
519 
520 	ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs);
521 	return (P2T(ret));
522 }
523 
524 static td_err_e
525 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
526 {
527 	const td_thragent_t *ta = th->th_ta;
528 	int ret;
529 
530 	TDBG_FUNC();
531 
532 	ret = pt_validate(th);
533 	if (ret)
534 		return (ret);
535 
536 	ret = ps_lgetregs(ta->ph, th->th_tid, gregs);
537 	return (P2T(ret));
538 }
539 
540 static td_err_e
541 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
542 {
543 	const td_thragent_t *ta = th->th_ta;
544 	int ret;
545 
546 	TDBG_FUNC();
547 
548 	ret = pt_validate(th);
549 	if (ret)
550 		return (ret);
551 
552 	ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs);
553 	return (P2T(ret));
554 }
555 
556 static td_err_e
557 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
558 {
559 	const td_thragent_t *ta = th->th_ta;
560 	int ret;
561 
562 	TDBG_FUNC();
563 
564 	ret = pt_validate(th);
565 	if (ret)
566 		return (ret);
567 
568 	ret = ps_lsetregs(ta->ph, th->th_tid, gregs);
569 	return (P2T(ret));
570 }
571 
572 static td_err_e
573 pt_thr_event_enable(const td_thrhandle_t *th, int en)
574 {
575 	const td_thragent_t *ta = th->th_ta;
576 	int ret;
577 
578 	TDBG_FUNC();
579 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events,
580 		&en, sizeof(int));
581 	return (P2T(ret));
582 }
583 
584 static td_err_e
585 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
586 {
587 	const td_thragent_t *ta = th->th_ta;
588 	td_thr_events_t mask;
589 	int ret;
590 
591 	TDBG_FUNC();
592 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
593 			&mask, sizeof(mask));
594 	mask |= *setp;
595 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
596 			&mask, sizeof(mask));
597 	return (P2T(ret));
598 }
599 
600 static td_err_e
601 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
602 {
603 	const td_thragent_t *ta = th->th_ta;
604 	td_thr_events_t mask;
605 	int ret;
606 
607 	TDBG_FUNC();
608 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
609 			&mask, sizeof(mask));
610 	mask &= ~*setp;
611 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
612 			&mask, sizeof(mask));
613 	return (P2T(ret));
614 }
615 
616 static td_err_e
617 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
618 {
619 	static td_thrhandle_t handle;
620 	td_thragent_t *ta = (td_thragent_t *)th->th_ta;
621 	psaddr_t pt, pt_temp;
622 	long lwp;
623 	int ret;
624 	td_thr_events_e	tmp;
625 
626 	TDBG_FUNC();
627 	pt = th->th_thread;
628 	ret = ps_pread(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
629 	if (ret != 0)
630 		return (P2T(ret));
631 	/* Get event */
632 	ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
633 	if (ret != 0)
634 		return (P2T(ret));
635 	if (msg->event == 0)
636 		return (TD_NOMSG);
637 	/*
638 	 * Take the event pointer, at the time, libthr only reports event
639 	 * once a time, so it is not a link list.
640 	 */
641 	if (pt == pt_temp) {
642 		pt_temp = NULL;
643 		ps_pwrite(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
644 	}
645 	/* Clear event */
646 	tmp = 0;
647 	ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
648 	/* Convert event */
649 	pt = (psaddr_t)msg->th_p;
650 	ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp, sizeof(lwp));
651 	if (ret != 0)
652 		return (P2T(ret));
653 	handle.th_ta = ta;
654 	handle.th_tid = lwp;
655 	handle.th_thread = pt;
656 	msg->th_p = &handle;
657 	return (0);
658 }
659 
660 static td_err_e
661 pt_thr_sstep(const td_thrhandle_t *th, int step)
662 {
663 	TDBG_FUNC();
664 
665 	return pt_validate(th);
666 }
667 
668 static int
669 pt_validate(const td_thrhandle_t *th)
670 {
671 
672 	if (th->th_tid == 0 || th->th_thread == NULL)
673 		return (TD_ERR);
674 	return (TD_OK);
675 }
676 
677 static td_err_e
678 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
679 		    void **address)
680 {
681 	char *obj_entry;
682 	const td_thragent_t *ta = th->th_ta;
683 	psaddr_t tcb_addr, *dtv_addr;
684 	int tls_index, ret;
685 
686 	/* linkmap is a member of Obj_Entry */
687 	obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
688 
689 	/* get tlsindex of the object file */
690 	ret = ps_pread(ta->ph,
691 		obj_entry + ta->thread_off_tlsindex,
692 		&tls_index, sizeof(tls_index));
693 	if (ret != 0)
694 		return (P2T(ret));
695 
696 	/* get thread tcb */
697 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
698 		&tcb_addr, sizeof(tcb_addr));
699 	if (ret != 0)
700 		return (P2T(ret));
701 
702 	/* get dtv array address */
703 	ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
704 		&dtv_addr, sizeof(dtv_addr));
705 	if (ret != 0)
706 		return (P2T(ret));
707 	/* now get the object's tls block base address */
708 	ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
709 		sizeof(*address));
710 	if (ret != 0)
711 		return (P2T(ret));
712 
713 	*address += offset;
714 	return (TD_OK);
715 }
716 
717 struct ta_ops libthr_db_ops = {
718 	.to_init		= pt_init,
719 	.to_ta_clear_event	= pt_ta_clear_event,
720 	.to_ta_delete		= pt_ta_delete,
721 	.to_ta_event_addr	= pt_ta_event_addr,
722 	.to_ta_event_getmsg	= pt_ta_event_getmsg,
723 	.to_ta_map_id2thr	= pt_ta_map_id2thr,
724 	.to_ta_map_lwp2thr	= pt_ta_map_lwp2thr,
725 	.to_ta_new		= pt_ta_new,
726 	.to_ta_set_event	= pt_ta_set_event,
727 	.to_ta_thr_iter		= pt_ta_thr_iter,
728 	.to_ta_tsd_iter		= pt_ta_tsd_iter,
729 	.to_thr_clear_event	= pt_thr_clear_event,
730 	.to_thr_dbresume	= pt_thr_dbresume,
731 	.to_thr_dbsuspend	= pt_thr_dbsuspend,
732 	.to_thr_event_enable	= pt_thr_event_enable,
733 	.to_thr_event_getmsg	= pt_thr_event_getmsg,
734 	.to_thr_get_info	= pt_thr_get_info,
735 	.to_thr_getfpregs	= pt_thr_getfpregs,
736 	.to_thr_getgregs	= pt_thr_getgregs,
737 	.to_thr_set_event	= pt_thr_set_event,
738 	.to_thr_setfpregs	= pt_thr_setfpregs,
739 	.to_thr_setgregs	= pt_thr_setgregs,
740 	.to_thr_validate	= pt_thr_validate,
741 	.to_thr_tls_get_addr	= pt_thr_tls_get_addr,
742 
743 	/* FreeBSD specific extensions. */
744 	.to_thr_sstep		= pt_thr_sstep,
745 };
746