xref: /dragonfly/lib/libthread_db/thread_db.h (revision 86d7f5d3)
1 /*
2  * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3  * Copyright (c) 2004 Marcel Moolenaar
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  * $DragonFly: src/lib/libthread_db/thread_db.h,v 1.1 2005/05/07 09:25:44 davidxu Exp $
28  */
29 
30 #ifndef _THREAD_DB_H_
31 #define	_THREAD_DB_H_
32 
33 #include <sys/procfs.h>
34 #include <pthread.h>
35 
36 typedef enum {
37 	TD_ERR = -1,		/* Unspecified error. */
38 	TD_OK = 0,		/* No error. */
39 	TD_BADKEY,
40 	TD_BADPH,
41 	TD_BADSH,
42 	TD_BADTA,
43 	TD_BADTH,
44 	TD_DBERR,
45 	TD_MALLOC,
46 	TD_NOAPLIC,
47 	TD_NOCAPAB,
48 	TD_NOEVENT,
49 	TD_NOFPREGS,
50 	TD_NOLIBTHREAD,
51 	TD_NOLWP,
52 	TD_NOMSG,
53 	TD_NOSV,
54 	TD_NOTHR,
55 	TD_NOTSD,
56 	TD_NOXREGS,
57 	TD_PARTIALREG
58 } td_err_e;
59 
60 struct ps_prochandle;
61 typedef struct td_thragent td_thragent_t;
62 typedef long thread_t;			/* Must be an integral type. */
63 
64 typedef struct {
65 	const td_thragent_t *th_ta;
66 	psaddr_t	th_thread;
67 	thread_t	th_tid;
68 } td_thrhandle_t;			/* Used non-opaguely. */
69 
70 /*
71  * Events.
72  */
73 
74 typedef enum {
75 	TD_EVENT_NONE = 0,
76 	TD_CATCHSIG =	0x0001,
77 	TD_CONCURRENCY=	0x0002,
78 	TD_CREATE =	0x0004,
79 	TD_DEATH =	0x0008,
80 	TD_IDLE =	0x0010,
81 	TD_LOCK_TRY =	0x0020,
82 	TD_PREEMPT =	0x0040,
83 	TD_PRI_INHERIT=	0x0080,
84 	TD_READY =	0x0100,
85 	TD_REAP =	0x0200,
86 	TD_SLEEP =	0x0400,
87 	TD_SWITCHFROM =	0x0800,
88 	TD_SWITCHTO =	0x1000,
89 	TD_TIMEOUT =	0x2000,
90 	TD_ALL_EVENTS = ~0
91 } td_thr_events_e;
92 
93 /* Compatibility with Linux. */
94 #define	td_event_e	td_thr_events_e
95 
96 typedef struct {
97 	td_thr_events_e	event;
98 	const td_thrhandle_t *th_p;
99 	uintptr_t	data;
100 } td_event_msg_t;
101 
102 typedef unsigned int td_thr_events_t;
103 
104 typedef enum {
105 	NOTIFY_BPT,		/* User inserted breakpoint. */
106 	NOTIFY_AUTOBPT,		/* Automatic breakpoint. */
107 	NOTIFY_SYSCALL		/* Invocation of system call. */
108 } td_notify_e;
109 
110 typedef struct {
111 	td_notify_e	type;
112 	union {
113 		psaddr_t bptaddr;
114 		int syscallno;
115 	} u;
116 } td_notify_t;
117 
118 static __inline void
td_event_addset(td_thr_events_t * es,td_thr_events_e e)119 td_event_addset(td_thr_events_t *es, td_thr_events_e e)
120 {
121 	*es |= e;
122 }
123 
124 static __inline void
td_event_delset(td_thr_events_t * es,td_thr_events_e e)125 td_event_delset(td_thr_events_t *es, td_thr_events_e e)
126 {
127 	*es &= ~e;
128 }
129 
130 static __inline void
td_event_emptyset(td_thr_events_t * es)131 td_event_emptyset(td_thr_events_t *es)
132 {
133 	*es = TD_EVENT_NONE;
134 }
135 
136 static __inline void
td_event_fillset(td_thr_events_t * es)137 td_event_fillset(td_thr_events_t *es)
138 {
139 	*es = TD_ALL_EVENTS;
140 }
141 
142 static __inline int
td_eventisempty(td_thr_events_t * es)143 td_eventisempty(td_thr_events_t *es)
144 {
145 	return ((*es == TD_EVENT_NONE) ? 1 : 0);
146 }
147 
148 static __inline int
td_eventismember(td_thr_events_t * es,td_thr_events_e e)149 td_eventismember(td_thr_events_t *es, td_thr_events_e e)
150 {
151 	return ((*es & e) ? 1 : 0);
152 }
153 
154 /*
155  * Thread info.
156  */
157 
158 typedef enum {
159 	TD_THR_UNKNOWN = -1,
160 	TD_THR_ANY_STATE = 0,
161 	TD_THR_ACTIVE,
162 	TD_THR_RUN,
163 	TD_THR_SLEEP,
164 	TD_THR_STOPPED,
165 	TD_THR_STOPPED_ASLEEP,
166 	TD_THR_ZOMBIE
167 } td_thr_state_e;
168 
169 typedef enum
170 {
171 	TD_THR_SYSTEM = 1,
172 	TD_THR_USER
173 } td_thr_type_e;
174 
175 typedef pthread_key_t thread_key_t;
176 
177 typedef struct {
178 	const td_thragent_t *ti_ta_p;
179 	thread_t	ti_tid;
180 	psaddr_t	ti_thread;
181 	td_thr_state_e	ti_state;
182 	td_thr_type_e	ti_type;
183 	td_thr_events_t	ti_events;
184 	int		ti_pri;
185 	lwpid_t		ti_lid;
186 	char		ti_db_suspended;
187 	char		ti_traceme;
188 	sigset_t	ti_sigmask;
189 	sigset_t	ti_pending;
190 	psaddr_t	ti_tls;
191 	psaddr_t	ti_startfunc;
192 	psaddr_t	ti_stkbase;
193 	size_t		ti_stksize;
194 } td_thrinfo_t;
195 
196 /*
197  * Prototypes.
198  */
199 
200 typedef int td_key_iter_f(thread_key_t, void (*)(void *), void *);
201 typedef int td_thr_iter_f(const td_thrhandle_t *, void *);
202 
203 /* Flags for `td_ta_thr_iter'. */
204 #define	TD_THR_ANY_USER_FLAGS	0xffffffff
205 #define	TD_THR_LOWEST_PRIORITY	-20
206 #define	TD_SIGNO_MASK		NULL
207 
208 __BEGIN_DECLS
209 td_err_e td_init(void);
210 td_err_e td_ta_clear_event(const td_thragent_t *, td_thr_events_t *);
211 td_err_e td_ta_delete(td_thragent_t *);
212 td_err_e td_ta_event_addr(const td_thragent_t *, td_thr_events_e,
213     td_notify_t *);
214 td_err_e td_ta_event_getmsg(const td_thragent_t *, td_event_msg_t *);
215 td_err_e td_ta_map_id2thr(const td_thragent_t *, thread_t, td_thrhandle_t *);
216 td_err_e td_ta_map_lwp2thr(const td_thragent_t *, lwpid_t, td_thrhandle_t *);
217 td_err_e td_ta_new(struct ps_prochandle *, td_thragent_t **);
218 td_err_e td_ta_set_event(const td_thragent_t *, td_thr_events_t *);
219 td_err_e td_ta_thr_iter(const td_thragent_t *, td_thr_iter_f *, void *,
220     td_thr_state_e, int, sigset_t *, unsigned int);
221 td_err_e td_ta_tsd_iter(const td_thragent_t *, td_key_iter_f *, void *);
222 td_err_e td_thr_clear_event(const td_thrhandle_t *, td_thr_events_t *);
223 td_err_e td_thr_dbresume(const td_thrhandle_t *);
224 td_err_e td_thr_dbsuspend(const td_thrhandle_t *);
225 td_err_e td_thr_event_enable(const td_thrhandle_t *, int);
226 td_err_e td_thr_event_getmsg(const td_thrhandle_t *, td_event_msg_t *);
227 td_err_e td_thr_get_info(const td_thrhandle_t *, td_thrinfo_t *);
228 td_err_e td_thr_getfpregs(const td_thrhandle_t *, prfpregset_t *);
229 td_err_e td_thr_getgregs(const td_thrhandle_t *, prgregset_t);
230 td_err_e td_thr_set_event(const td_thrhandle_t *, td_thr_events_t *);
231 td_err_e td_thr_setfpregs(const td_thrhandle_t *, const prfpregset_t *);
232 td_err_e td_thr_setgregs(const td_thrhandle_t *, const prgregset_t);
233 td_err_e td_thr_validate(const td_thrhandle_t *);
234 td_err_e td_thr_tls_get_addr(const td_thrhandle_t *, void *, size_t, void **);
235 
236 __END_DECLS
237 
238 #endif /* _THREAD_DB_H_ */
239