xref: /freebsd/lib/libthread_db/libthr_db.c (revision 263d6a7ece4f357d01b3152c39e5d36043f877bb)
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 	prgregset_t gregs;
204 	TAILQ_HEAD(, pthread) thread_list;
205 	psaddr_t pt;
206 	long lwp;
207 	int ret;
208 
209 	TDBG_FUNC();
210 
211 	if (id == 0)
212 		return (TD_NOTHR);
213 	ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
214 		sizeof(thread_list));
215 	if (ret != 0)
216 		return (P2T(ret));
217 	/* Iterate through thread list to find pthread */
218 	pt = (psaddr_t)thread_list.tqh_first;
219 	while (pt != NULL) {
220 		ret = ps_pread(ta->ph, pt + ta->thread_off_tid,
221 			       &lwp, sizeof(lwp));
222 		if (ret != 0)
223 			return (P2T(ret));
224 		if (lwp == id)
225 			break;
226 		/* get next thread */
227 		ret = ps_pread(ta->ph,
228 				pt + ta->thread_off_next,
229 				&pt, sizeof(pt));
230 		if (ret != 0)
231 			return (P2T(ret));
232 	}
233 	if (pt == NULL)
234 		return (TD_NOTHR);
235 	th->th_ta = ta;
236 	th->th_tid = id;
237 	th->th_thread = pt;
238 	return (TD_OK);
239 }
240 
241 static td_err_e
242 pt_ta_map_lwp2thr(const td_thragent_t *ta, lwpid_t lwp, td_thrhandle_t *th)
243 {
244 	return (pt_ta_map_id2thr(ta, lwp, th));
245 }
246 
247 static td_err_e
248 pt_ta_thr_iter(const td_thragent_t *ta,
249                td_thr_iter_f *callback, void *cbdata_p,
250                td_thr_state_e state, int ti_pri,
251                sigset_t *ti_sigmask_p,
252                unsigned int ti_user_flags)
253 {
254 	TAILQ_HEAD(, pthread) thread_list;
255 	td_thrhandle_t th;
256 	psaddr_t pt;
257 	long lwp;
258 	int ret;
259 
260 	TDBG_FUNC();
261 
262 	ret = ps_pread(ta->ph, ta->thread_list_addr, &thread_list,
263 		       sizeof(thread_list));
264 	if (ret != 0)
265 		return (P2T(ret));
266 	pt = (psaddr_t)thread_list.tqh_first;
267 	while (pt != 0) {
268 		ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp,
269 			      sizeof(lwp));
270 		if (ret != 0)
271 			return (P2T(ret));
272 		if (lwp != 0 && lwp != TERMINATED) {
273 			th.th_ta = ta;
274 			th.th_tid = (thread_t)lwp;
275 			th.th_thread = pt;
276 			if ((*callback)(&th, cbdata_p))
277 				return (TD_DBERR);
278 		}
279 		/* get next thread */
280 		ret = ps_pread(ta->ph, pt + ta->thread_off_next, &pt,
281 			       sizeof(pt));
282 		if (ret != 0)
283 			return (P2T(ret));
284 	}
285 	return (TD_OK);
286 }
287 
288 static td_err_e
289 pt_ta_tsd_iter(const td_thragent_t *ta, td_key_iter_f *ki, void *arg)
290 {
291 	char *keytable;
292 	void *destructor;
293 	int i, ret, allocated;
294 
295 	TDBG_FUNC();
296 
297 	keytable = malloc(ta->thread_max_keys * ta->thread_size_key);
298 	if (keytable == NULL)
299 		return (TD_MALLOC);
300 	ret = ps_pread(ta->ph, (psaddr_t)ta->thread_keytable_addr, keytable,
301 	               ta->thread_max_keys * ta->thread_size_key);
302 	if (ret != 0) {
303 		free(keytable);
304 		return (P2T(ret));
305 	}
306 	for (i = 0; i < ta->thread_max_keys; i++) {
307 		allocated = *(int *)(keytable + i * ta->thread_size_key +
308 			ta->thread_off_key_allocated);
309 		destructor = *(void **)(keytable + i * ta->thread_size_key +
310 			ta->thread_off_key_destructor);
311 		if (allocated) {
312 			ret = (ki)(i, destructor, arg);
313 			if (ret != 0) {
314 				free(keytable);
315 				return (TD_DBERR);
316 			}
317 		}
318 	}
319 	free(keytable);
320 	return (TD_OK);
321 }
322 
323 static td_err_e
324 pt_ta_event_addr(const td_thragent_t *ta, td_event_e event, td_notify_t *ptr)
325 {
326 
327 	TDBG_FUNC();
328 
329 	switch (event) {
330 	case TD_CREATE:
331 		ptr->type = NOTIFY_BPT;
332 		ptr->u.bptaddr = ta->thread_bp_create_addr;
333 		return (0);
334 	case TD_DEATH:
335 		ptr->type = NOTIFY_BPT;
336 		ptr->u.bptaddr = ta->thread_bp_death_addr;
337 		return (0);
338 	default:
339 		return (TD_ERR);
340 	}
341 }
342 
343 static td_err_e
344 pt_ta_set_event(const td_thragent_t *ta, td_thr_events_t *events)
345 {
346 	td_thr_events_t mask;
347 	int ret;
348 
349 	TDBG_FUNC();
350 	ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
351 		sizeof(mask));
352 	if (ret != 0)
353 		return (P2T(ret));
354 	mask |= *events;
355 	ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
356 		sizeof(mask));
357 	return (P2T(ret));
358 }
359 
360 static td_err_e
361 pt_ta_clear_event(const td_thragent_t *ta, td_thr_events_t *events)
362 {
363 	td_thr_events_t mask;
364 	int ret;
365 
366 	TDBG_FUNC();
367 	ret = ps_pread(ta->ph, ta->thread_event_mask_addr, &mask,
368 		sizeof(mask));
369 	if (ret != 0)
370 		return (P2T(ret));
371 	mask &= ~*events;
372 	ret = ps_pwrite(ta->ph, ta->thread_event_mask_addr, &mask,
373 		sizeof(mask));
374 	return (P2T(ret));
375 }
376 
377 static td_err_e
378 pt_ta_event_getmsg(const td_thragent_t *ta, td_event_msg_t *msg)
379 {
380 	static td_thrhandle_t handle;
381 
382 	psaddr_t pt, pt_temp;
383 	td_thr_events_e	tmp;
384 	long lwp;
385 	int ret;
386 
387 	TDBG_FUNC();
388 
389 	ret = ps_pread(ta->ph, ta->thread_last_event_addr, &pt, sizeof(pt));
390 	if (ret != 0)
391 		return (P2T(ret));
392 	if (pt == NULL)
393 		return (TD_NOMSG);
394 	/*
395 	 * Take the event pointer, at the time, libthr only reports event
396 	 * once a time, so it is not a link list.
397 	 */
398 	pt_temp = NULL;
399 	ps_pwrite(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
400 
401 	/* Read event info */
402 	ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
403 	if (ret != 0)
404 		return (P2T(ret));
405 	if (msg->event == 0)
406 		return (TD_NOMSG);
407 	/* Clear event */
408 	tmp = 0;
409 	ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
410 	/* Convert event */
411 	pt = (psaddr_t)msg->th_p;
412 	ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp, sizeof(lwp));
413 	if (ret != 0)
414 		return (P2T(ret));
415 	handle.th_ta = ta;
416 	handle.th_tid = lwp;
417 	handle.th_thread = pt;
418 	msg->th_p = &handle;
419 	return (0);
420 }
421 
422 static td_err_e
423 pt_dbsuspend(const td_thrhandle_t *th, int suspend)
424 {
425 	td_thragent_t *ta = (td_thragent_t *)th->th_ta;
426 	int ret;
427 
428 	TDBG_FUNC();
429 
430 	ret = pt_validate(th);
431 	if (ret)
432 		return (ret);
433 
434 	if (suspend)
435 		ret = ps_lstop(ta->ph, th->th_tid);
436 	else
437 		ret = ps_lcontinue(ta->ph, th->th_tid);
438 	return (P2T(ret));
439 }
440 
441 static td_err_e
442 pt_thr_dbresume(const td_thrhandle_t *th)
443 {
444 	TDBG_FUNC();
445 
446 	return pt_dbsuspend(th, 0);
447 }
448 
449 static td_err_e
450 pt_thr_dbsuspend(const td_thrhandle_t *th)
451 {
452 	TDBG_FUNC();
453 
454 	return pt_dbsuspend(th, 1);
455 }
456 
457 static td_err_e
458 pt_thr_validate(const td_thrhandle_t *th)
459 {
460 	td_thrhandle_t temp;
461 	int ret;
462 
463 	TDBG_FUNC();
464 
465 	ret = pt_ta_map_id2thr(th->th_ta, th->th_tid, &temp);
466 	return (ret);
467 }
468 
469 static td_err_e
470 pt_thr_get_info(const td_thrhandle_t *th, td_thrinfo_t *info)
471 {
472 	const td_thragent_t *ta = th->th_ta;
473 	int state;
474 	int ret;
475 
476 	TDBG_FUNC();
477 
478 	ret = pt_validate(th);
479 	if (ret)
480 		return (ret);
481 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_state,
482 	               &state, sizeof(state));
483 	if (ret != 0)
484 		return (P2T(ret));
485 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_report_events,
486 		&info->ti_traceme, sizeof(int));
487 	if (ret != 0)
488 		return (P2T(ret));
489 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
490 		&info->ti_events, sizeof(td_thr_events_t));
491 	if (ret != 0)
492 		return (P2T(ret));
493 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
494 		&info->ti_tls, sizeof(void *));
495 	info->ti_lid = th->th_tid;
496 	info->ti_tid = th->th_tid;
497 	info->ti_thread = th->th_thread;
498 	info->ti_ta_p = th->th_ta;
499 	if (state == ta->thread_state_running)
500 		info->ti_state = TD_THR_RUN;
501 	else if (state == ta->thread_state_zoombie)
502 		info->ti_state = TD_THR_ZOMBIE;
503 	else
504 		info->ti_state = TD_THR_SLEEP;
505 	info->ti_type = TD_THR_USER;
506 	return (0);
507 }
508 
509 static td_err_e
510 pt_thr_getfpregs(const td_thrhandle_t *th, prfpregset_t *fpregs)
511 {
512 	const td_thragent_t *ta = th->th_ta;
513 	int ret;
514 
515 	TDBG_FUNC();
516 
517 	ret = pt_validate(th);
518 	if (ret)
519 		return (ret);
520 
521 	ret = ps_lgetfpregs(ta->ph, th->th_tid, fpregs);
522 	return (P2T(ret));
523 }
524 
525 static td_err_e
526 pt_thr_getgregs(const td_thrhandle_t *th, prgregset_t gregs)
527 {
528 	const td_thragent_t *ta = th->th_ta;
529 	int ret;
530 
531 	TDBG_FUNC();
532 
533 	ret = pt_validate(th);
534 	if (ret)
535 		return (ret);
536 
537 	ret = ps_lgetregs(ta->ph, th->th_tid, gregs);
538 	return (P2T(ret));
539 }
540 
541 static td_err_e
542 pt_thr_setfpregs(const td_thrhandle_t *th, const prfpregset_t *fpregs)
543 {
544 	const td_thragent_t *ta = th->th_ta;
545 	int ret;
546 
547 	TDBG_FUNC();
548 
549 	ret = pt_validate(th);
550 	if (ret)
551 		return (ret);
552 
553 	ret = ps_lsetfpregs(ta->ph, th->th_tid, fpregs);
554 	return (P2T(ret));
555 }
556 
557 static td_err_e
558 pt_thr_setgregs(const td_thrhandle_t *th, const prgregset_t gregs)
559 {
560 	const td_thragent_t *ta = th->th_ta;
561 	int ret;
562 
563 	TDBG_FUNC();
564 
565 	ret = pt_validate(th);
566 	if (ret)
567 		return (ret);
568 
569 	ret = ps_lsetregs(ta->ph, th->th_tid, gregs);
570 	return (P2T(ret));
571 }
572 
573 static td_err_e
574 pt_thr_event_enable(const td_thrhandle_t *th, int en)
575 {
576 	const td_thragent_t *ta = th->th_ta;
577 	int ret;
578 
579 	TDBG_FUNC();
580 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_report_events,
581 		&en, sizeof(int));
582 	return (P2T(ret));
583 }
584 
585 static td_err_e
586 pt_thr_set_event(const td_thrhandle_t *th, td_thr_events_t *setp)
587 {
588 	const td_thragent_t *ta = th->th_ta;
589 	td_thr_events_t mask;
590 	int ret;
591 
592 	TDBG_FUNC();
593 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
594 			&mask, sizeof(mask));
595 	mask |= *setp;
596 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
597 			&mask, sizeof(mask));
598 	return (P2T(ret));
599 }
600 
601 static td_err_e
602 pt_thr_clear_event(const td_thrhandle_t *th, td_thr_events_t *setp)
603 {
604 	const td_thragent_t *ta = th->th_ta;
605 	td_thr_events_t mask;
606 	int ret;
607 
608 	TDBG_FUNC();
609 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_event_mask,
610 			&mask, sizeof(mask));
611 	mask &= ~*setp;
612 	ret = ps_pwrite(ta->ph, th->th_thread + ta->thread_off_event_mask,
613 			&mask, sizeof(mask));
614 	return (P2T(ret));
615 }
616 
617 static td_err_e
618 pt_thr_event_getmsg(const td_thrhandle_t *th, td_event_msg_t *msg)
619 {
620 	static td_thrhandle_t handle;
621 	td_thragent_t *ta = (td_thragent_t *)th->th_ta;
622 	psaddr_t pt, pt_temp;
623 	long lwp;
624 	int ret;
625 	td_thr_events_e	tmp;
626 
627 	TDBG_FUNC();
628 	pt = th->th_thread;
629 	ret = ps_pread(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
630 	if (ret != 0)
631 		return (P2T(ret));
632 	/* Get event */
633 	ret = ps_pread(ta->ph, pt + ta->thread_off_event_buf, msg, sizeof(*msg));
634 	if (ret != 0)
635 		return (P2T(ret));
636 	if (msg->event == 0)
637 		return (TD_NOMSG);
638 	/*
639 	 * Take the event pointer, at the time, libthr only reports event
640 	 * once a time, so it is not a link list.
641 	 */
642 	if (pt == pt_temp) {
643 		pt_temp = NULL;
644 		ps_pwrite(ta->ph, ta->thread_last_event_addr, &pt_temp, sizeof(pt_temp));
645 	}
646 	/* Clear event */
647 	tmp = 0;
648 	ps_pwrite(ta->ph, pt + ta->thread_off_event_buf, &tmp, sizeof(tmp));
649 	/* Convert event */
650 	pt = (psaddr_t)msg->th_p;
651 	ret = ps_pread(ta->ph, pt + ta->thread_off_tid, &lwp, sizeof(lwp));
652 	if (ret != 0)
653 		return (P2T(ret));
654 	handle.th_ta = ta;
655 	handle.th_tid = lwp;
656 	handle.th_thread = pt;
657 	msg->th_p = &handle;
658 	return (0);
659 }
660 
661 static td_err_e
662 pt_thr_sstep(const td_thrhandle_t *th, int step)
663 {
664 	TDBG_FUNC();
665 
666 	return pt_validate(th);
667 }
668 
669 static int
670 pt_validate(const td_thrhandle_t *th)
671 {
672 
673 	if (th->th_tid == 0 || th->th_thread == NULL)
674 		return (TD_ERR);
675 	return (TD_OK);
676 }
677 
678 static td_err_e
679 pt_thr_tls_get_addr(const td_thrhandle_t *th, void *_linkmap, size_t offset,
680 		    void **address)
681 {
682 	char *obj_entry;
683 	const td_thragent_t *ta = th->th_ta;
684 	psaddr_t tcb_addr, *dtv_addr, tcb_tp;
685 	int tls_index, ret;
686 
687 	/* linkmap is a member of Obj_Entry */
688 	obj_entry = (char *)_linkmap - ta->thread_off_linkmap;
689 
690 	/* get tlsindex of the object file */
691 	ret = ps_pread(ta->ph,
692 		obj_entry + ta->thread_off_tlsindex,
693 		&tls_index, sizeof(tls_index));
694 	if (ret != 0)
695 		return (P2T(ret));
696 
697 	/* get thread tcb */
698 	ret = ps_pread(ta->ph, th->th_thread + ta->thread_off_tcb,
699 		&tcb_addr, sizeof(tcb_addr));
700 	if (ret != 0)
701 		return (P2T(ret));
702 
703 	/* get dtv array address */
704 	ret = ps_pread(ta->ph, tcb_addr + ta->thread_off_dtv,
705 		&dtv_addr, sizeof(dtv_addr));
706 	if (ret != 0)
707 		return (P2T(ret));
708 	/* now get the object's tls block base address */
709 	ret = ps_pread(ta->ph, &dtv_addr[tls_index+1], address,
710 		sizeof(*address));
711 	if (ret != 0)
712 		return (P2T(ret));
713 
714 	*address += offset;
715 	return (TD_OK);
716 }
717 
718 struct ta_ops libthr_db_ops = {
719 	.to_init		= pt_init,
720 	.to_ta_clear_event	= pt_ta_clear_event,
721 	.to_ta_delete		= pt_ta_delete,
722 	.to_ta_event_addr	= pt_ta_event_addr,
723 	.to_ta_event_getmsg	= pt_ta_event_getmsg,
724 	.to_ta_map_id2thr	= pt_ta_map_id2thr,
725 	.to_ta_map_lwp2thr	= pt_ta_map_lwp2thr,
726 	.to_ta_new		= pt_ta_new,
727 	.to_ta_set_event	= pt_ta_set_event,
728 	.to_ta_thr_iter		= pt_ta_thr_iter,
729 	.to_ta_tsd_iter		= pt_ta_tsd_iter,
730 	.to_thr_clear_event	= pt_thr_clear_event,
731 	.to_thr_dbresume	= pt_thr_dbresume,
732 	.to_thr_dbsuspend	= pt_thr_dbsuspend,
733 	.to_thr_event_enable	= pt_thr_event_enable,
734 	.to_thr_event_getmsg	= pt_thr_event_getmsg,
735 	.to_thr_get_info	= pt_thr_get_info,
736 	.to_thr_getfpregs	= pt_thr_getfpregs,
737 	.to_thr_getgregs	= pt_thr_getgregs,
738 	.to_thr_set_event	= pt_thr_set_event,
739 	.to_thr_setfpregs	= pt_thr_setfpregs,
740 	.to_thr_setgregs	= pt_thr_setgregs,
741 	.to_thr_validate	= pt_thr_validate,
742 	.to_thr_tls_get_addr	= pt_thr_tls_get_addr,
743 
744 	/* FreeBSD specific extensions. */
745 	.to_thr_sstep		= pt_thr_sstep,
746 };
747