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