thread_db.h revision 132360
1132332Smarcel/*
2132360Smarcel * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3132332Smarcel * Copyright (c) 2004 Marcel Moolenaar
4132332Smarcel * All rights reserved.
5132332Smarcel *
6132332Smarcel * Redistribution and use in source and binary forms, with or without
7132332Smarcel * modification, are permitted provided that the following conditions
8132332Smarcel * are met:
9132332Smarcel *
10132332Smarcel * 1. Redistributions of source code must retain the above copyright
11132332Smarcel *    notice, this list of conditions and the following disclaimer.
12132332Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13132332Smarcel *    notice, this list of conditions and the following disclaimer in the
14132332Smarcel *    documentation and/or other materials provided with the distribution.
15132332Smarcel *
16132332Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17132332Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18132332Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19132332Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20132332Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21132332Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22132332Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23132332Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24132332Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25132332Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26132332Smarcel *
27132332Smarcel * $FreeBSD: head/lib/libthread_db/thread_db.h 132360 2004-07-18 19:29:38Z marcel $
28132332Smarcel */
29132332Smarcel
30132332Smarcel#ifndef _THREAD_DB_H_
31132332Smarcel#define	_THREAD_DB_H_
32132332Smarcel
33132332Smarcel#include <sys/procfs.h>
34132332Smarcel#include <pthread.h>
35132332Smarcel
36132332Smarceltypedef enum {
37132332Smarcel	TD_ERR = -1,		/* Unspecified error. */
38132332Smarcel	TD_OK = 0,		/* No error. */
39132332Smarcel	TD_BADKEY,
40132332Smarcel	TD_BADPH,
41132332Smarcel	TD_BADSH,
42132332Smarcel	TD_BADTA,
43132332Smarcel	TD_BADTH,
44132332Smarcel	TD_DBERR,
45132332Smarcel	TD_MALLOC,
46132332Smarcel	TD_NOAPLIC,
47132332Smarcel	TD_NOCAPAB,
48132332Smarcel	TD_NOEVENT,
49132332Smarcel	TD_NOFPREGS,
50132332Smarcel	TD_NOLIBTHREAD,
51132332Smarcel	TD_NOLWP,
52132332Smarcel	TD_NOMSG,
53132332Smarcel	TD_NOSV,
54132332Smarcel	TD_NOTHR,
55132332Smarcel	TD_NOTSD,
56132332Smarcel	TD_NOXREGS,
57132332Smarcel	TD_PARTIALREG
58132332Smarcel} td_err_e;
59132332Smarcel
60132332Smarceltypedef struct td_thragent td_thragent_t;
61132332Smarceltypedef long thread_t;			/* Must be an integral type. */
62132332Smarcel
63132332Smarceltypedef struct {
64132332Smarcel	const td_thragent_t *th_ta;
65132332Smarcel	psaddr_t	th_thread;
66132332Smarcel	thread_t	th_tid;
67132332Smarcel} td_thrhandle_t;			/* Used non-opaguely. */
68132332Smarcel
69132332Smarcel/*
70132332Smarcel * Events.
71132332Smarcel */
72132332Smarcel
73132332Smarceltypedef enum {
74132332Smarcel	TD_EVENT_NONE = 0,
75132360Smarcel	TD_CATCHSIG =	0x0001,
76132360Smarcel	TD_CONCURRENCY=	0x0002,
77132360Smarcel	TD_CREATE =	0x0004,
78132360Smarcel	TD_DEATH =	0x0008,
79132360Smarcel	TD_IDLE =	0x0010,
80132360Smarcel	TD_LOCK_TRY =	0x0020,
81132360Smarcel	TD_PREEMPT =	0x0040,
82132360Smarcel	TD_PRI_INHERIT=	0x0080,
83132360Smarcel	TD_READY =	0x0100,
84132360Smarcel	TD_REAP =	0x0200,
85132360Smarcel	TD_SLEEP =	0x0400,
86132360Smarcel	TD_SWITCHFROM =	0x0800,
87132360Smarcel	TD_SWITCHTO =	0x1000,
88132360Smarcel	TD_TIMEOUT =	0x2000,
89132332Smarcel	TD_ALL_EVENTS = ~0
90132332Smarcel} td_thr_events_e;
91132332Smarcel
92132332Smarcel/* Compatibility with Linux. */
93132332Smarcel#define	td_event_e	td_thr_events_e
94132332Smarcel
95132332Smarceltypedef struct {
96132332Smarcel	td_thr_events_e	event;
97132332Smarcel	const td_thrhandle_t *th_p;
98132332Smarcel	uintptr_t	data;
99132332Smarcel} td_event_msg_t;
100132332Smarcel
101132332Smarceltypedef unsigned int td_thr_events_t;
102132332Smarcel
103132360Smarceltypedef enum {
104132360Smarcel	NOTIFY_BPT,		/* User inserted breakpoint. */
105132360Smarcel	NOTIFY_AUTOBPT,		/* Automatic breakpoint. */
106132360Smarcel	NOTIFY_SYSCALL		/* Invocation of system call. */
107132360Smarcel} td_notify_e;
108132360Smarcel
109132332Smarceltypedef struct {
110132360Smarcel	td_notify_e	type;
111132332Smarcel	union {
112132332Smarcel		psaddr_t bptaddr;
113132360Smarcel		int syscallno;
114132332Smarcel	} u;
115132332Smarcel} td_notify_t;
116132332Smarcel
117132332Smarcelstatic __inline void
118132332Smarceltd_event_addset(td_thr_events_t *es, td_thr_events_e e)
119132332Smarcel{
120132332Smarcel	*es |= e;
121132332Smarcel}
122132332Smarcel
123132332Smarcelstatic __inline void
124132332Smarceltd_event_delset(td_thr_events_t *es, td_thr_events_e e)
125132332Smarcel{
126132332Smarcel	*es &= ~e;
127132332Smarcel}
128132332Smarcel
129132332Smarcelstatic __inline void
130132332Smarceltd_event_emptyset(td_thr_events_t *es)
131132332Smarcel{
132132332Smarcel	*es = TD_EVENT_NONE;
133132332Smarcel}
134132332Smarcel
135132332Smarcelstatic __inline void
136132332Smarceltd_event_fillset(td_thr_events_t *es)
137132332Smarcel{
138132332Smarcel	*es = TD_ALL_EVENTS;
139132332Smarcel}
140132332Smarcel
141132332Smarcelstatic __inline int
142132332Smarceltd_eventisempty(td_thr_events_t *es)
143132332Smarcel{
144132332Smarcel	return ((*es == TD_EVENT_NONE) ? 1 : 0);
145132332Smarcel}
146132332Smarcel
147132332Smarcelstatic __inline int
148132332Smarceltd_eventismember(td_thr_events_t *es, td_thr_events_e e)
149132332Smarcel{
150132332Smarcel	return ((*es & e) ? 1 : 0);
151132332Smarcel}
152132332Smarcel
153132332Smarcel/*
154132332Smarcel * Thread info.
155132332Smarcel */
156132332Smarcel
157132332Smarceltypedef enum {
158132332Smarcel	TD_THR_UNKNOWN = -1,
159132332Smarcel	TD_THR_ANY_STATE = 0,
160132332Smarcel	TD_THR_ACTIVE,
161132332Smarcel	TD_THR_RUN,
162132332Smarcel	TD_THR_SLEEP,
163132332Smarcel	TD_THR_STOPPED,
164132332Smarcel	TD_THR_STOPPED_ASLEEP,
165132332Smarcel	TD_THR_ZOMBIE
166132332Smarcel} td_thr_state_e;
167132332Smarcel
168132332Smarceltypedef enum
169132332Smarcel{
170132332Smarcel	TD_THR_SYSTEM = 1,
171132332Smarcel	TD_THR_USER
172132332Smarcel} td_thr_type_e;
173132332Smarcel
174132360Smarceltypedef pthread_key_t thread_key_t;
175132332Smarcel
176132332Smarceltypedef struct {
177132332Smarcel	const td_thragent_t *ti_ta_p;
178132332Smarcel	thread_t	ti_tid;
179132332Smarcel	td_thr_state_e	ti_state;
180132332Smarcel	td_thr_type_e	ti_type;
181132332Smarcel	td_thr_events_t	ti_events;
182132332Smarcel	int		ti_pri;
183132332Smarcel	lwpid_t		ti_lid;
184132332Smarcel	char		ti_db_suspended;
185132332Smarcel	char		ti_traceme;
186132332Smarcel	sigset_t	ti_sigmask;
187132332Smarcel	sigset_t	ti_pending;
188132332Smarcel	psaddr_t	ti_tls;
189132332Smarcel	psaddr_t	ti_startfunc;
190132332Smarcel	psaddr_t	ti_stkbase;
191132332Smarcel	size_t		ti_stksize;
192132332Smarcel} td_thrinfo_t;
193132332Smarcel
194132332Smarcel/*
195132332Smarcel * Prototypes.
196132332Smarcel */
197132332Smarcel
198132332Smarceltypedef int td_key_iter_f(thread_key_t, void (*)(void *), void *);
199132332Smarceltypedef int td_thr_iter_f(const td_thrhandle_t *, void *);
200132332Smarcel
201132332Smarcel/* Flags for `td_ta_thr_iter'. */
202132332Smarcel#define	TD_THR_ANY_USER_FLAGS	0xffffffff
203132332Smarcel#define	TD_THR_LOWEST_PRIORITY	-20
204132332Smarcel#define	TD_SIGNO_MASK		NULL
205132332Smarcel
206132332Smarcel__BEGIN_DECLS
207132332Smarceltd_err_e td_init(void);
208132332Smarcel
209132332Smarceltd_err_e td_ta_clear_event(const td_thragent_t *, td_thr_events_t *);
210132332Smarceltd_err_e td_ta_delete(td_thragent_t *);
211132332Smarceltd_err_e td_ta_event_addr(const td_thragent_t *, td_thr_events_e,
212132332Smarcel    td_notify_t *);
213132332Smarceltd_err_e td_ta_event_getmsg(const td_thragent_t *, td_event_msg_t *);
214132332Smarceltd_err_e td_ta_map_id2thr(const td_thragent_t *, thread_t, td_thrhandle_t *);
215132332Smarceltd_err_e td_ta_map_lwp2thr(const td_thragent_t *, lwpid_t, td_thrhandle_t *);
216132332Smarceltd_err_e td_ta_new(struct ps_prochandle *, td_thragent_t **);
217132332Smarceltd_err_e td_ta_set_event(const td_thragent_t *, td_thr_events_t *);
218132332Smarceltd_err_e td_ta_thr_iter(const td_thragent_t *, td_thr_iter_f *, void *,
219132332Smarcel    td_thr_state_e, int, sigset_t *, unsigned int);
220132332Smarceltd_err_e td_ta_tsd_iter(const td_thragent_t *, td_key_iter_f *, void *);
221132332Smarcel
222132332Smarceltd_err_e td_thr_clear_event(const td_thrhandle_t *, td_thr_events_t *);
223132332Smarceltd_err_e td_thr_dbresume(const td_thrhandle_t *);
224132332Smarceltd_err_e td_thr_dbsuspend(const td_thrhandle_t *);
225132332Smarceltd_err_e td_thr_event_enable(const td_thrhandle_t *, int);
226132332Smarceltd_err_e td_thr_event_getmsg(const td_thrhandle_t *, td_event_msg_t *);
227132332Smarceltd_err_e td_thr_get_info(const td_thrhandle_t *, td_thrinfo_t *);
228132332Smarceltd_err_e td_thr_getfpregs(const td_thrhandle_t *, prfpregset_t *);
229132332Smarceltd_err_e td_thr_getgregs(const td_thrhandle_t *, prgregset_t);
230132332Smarceltd_err_e td_thr_set_event(const td_thrhandle_t *, td_thr_events_t *);
231132332Smarceltd_err_e td_thr_setfpregs(const td_thrhandle_t *, const prfpregset_t *);
232132332Smarceltd_err_e td_thr_setgregs(const td_thrhandle_t *, const prgregset_t);
233132332Smarceltd_err_e td_thr_validate(const td_thrhandle_t *);
234132332Smarcel
235132332Smarcel/* FreeBSD specific extensions. */
236132332Smarceltd_err_e td_thr_sstep(const td_thrhandle_t *, int);
237132332Smarcel__END_DECLS
238132332Smarcel
239132332Smarcel#endif /* _THREAD_DB_H_ */
240