xref: /netbsd/sys/sys/ptrace.h (revision f6412eff)
1 /*	$NetBSD: ptrace.h,v 1.75 2022/06/08 23:12:27 andvar Exp $	*/
2 
3 /*-
4  * Copyright (c) 1984, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ptrace.h	8.2 (Berkeley) 1/4/94
32  */
33 
34 #ifndef	_SYS_PTRACE_H_
35 #define	_SYS_PTRACE_H_
36 
37 #include <sys/siginfo.h>
38 #include <sys/signal.h>
39 
40 #define	PT_TRACE_ME		0	/* child declares it's being traced */
41 #define	PT_READ_I		1	/* read word in child's I space */
42 #define	PT_READ_D		2	/* read word in child's D space */
43 #define	PT_WRITE_I		4	/* write word in child's I space */
44 #define	PT_WRITE_D		5	/* write word in child's D space */
45 #define	PT_CONTINUE		7	/* continue the child */
46 #define	PT_KILL			8	/* kill the child process */
47 #define	PT_ATTACH		9	/* attach to running process */
48 #define	PT_DETACH		10	/* detach from running process */
49 #define	PT_IO			11	/* do I/O to/from the stopped process */
50 #define	PT_DUMPCORE		12	/* make child generate a core dump */
51 #if defined(__LEGACY_PT_LWPINFO) || defined(_KERNEL)
52 #define	PT_LWPINFO		13	/* OBSOLETE: get info about the LWP */
53 #endif
54 #define	PT_SYSCALL		14	/* stop on syscall entry/exit */
55 #define	PT_SYSCALLEMU		15	/* cancel syscall, tracer emulates it */
56 #define	PT_SET_EVENT_MASK	16	/* set the event mask, defined below */
57 #define	PT_GET_EVENT_MASK	17	/* get the event mask, defined below */
58 #define	PT_GET_PROCESS_STATE	18	/* get process state, defined below */
59 #define	PT_SET_SIGINFO		19	/* set signal state, defined below */
60 #define	PT_GET_SIGINFO		20	/* get signal state, defined below */
61 #define	PT_RESUME		21	/* allow execution of the LWP */
62 #define	PT_SUSPEND		22	/* prevent execution of the LWP */
63 #define	PT_STOP			23	/* stop the child process */
64 #define	PT_LWPSTATUS		24	/* get info about the LWP */
65 #define	PT_LWPNEXT		25	/* get info about next LWP */
66 #define	PT_SET_SIGPASS		26	/* set signals to pass to debuggee */
67 #define	PT_GET_SIGPASS		27	/* get signals to pass to debuggee */
68 
69 #define	PT_FIRSTMACH		32	/* for machine-specific requests */
70 #include <machine/ptrace.h>		/* machine-specific requests, if any */
71 
72 #define PT_STRINGS \
73 /*  0 */    "PT_TRACE_ME", \
74 /*  1 */    "PT_READ_I", \
75 /*  2 */    "PT_READ_D", \
76 /*  3 */    "*PT_INVALID_3*", \
77 /*  4 */    "PT_WRITE_I", \
78 /*  5 */    "PT_WRITE_D", \
79 /*  6 */    "*PT_INVALID_6*", \
80 /*  7 */    "PT_CONTINUE", \
81 /*  8 */    "PT_KILL", \
82 /*  9 */    "PT_ATTACH", \
83 /* 10 */    "PT_DETACH", \
84 /* 11 */    "PT_IO", \
85 /* 12 */    "PT_DUMPCORE", \
86 /* 13 */    "PT_LWPINFO", \
87 /* 14 */    "PT_SYSCALL", \
88 /* 15 */    "PT_SYSCALLEMU", \
89 /* 16 */    "PT_SET_EVENT_MASK", \
90 /* 17 */    "PT_GET_EVENT_MASK", \
91 /* 18 */    "PT_GET_PROCESS_STATE", \
92 /* 19 */    "PT_SET_SIGINFO", \
93 /* 20 */    "PT_GET_SIGINFO", \
94 /* 21 */    "PT_RESUME", \
95 /* 22 */    "PT_SUSPEND", \
96 /* 23 */    "PT_STOP", \
97 /* 24 */    "PT_LWPSTATUS", \
98 /* 25 */    "PT_LWPNEXT", \
99 /* 26 */    "PT_SET_SIGPASS", \
100 /* 27 */    "PT_GET_SIGPASS"
101 
102 /* PT_{G,S}EVENT_MASK */
103 typedef struct ptrace_event {
104 	int	pe_set_event;
105 } ptrace_event_t;
106 
107 /* PT_GET_PROCESS_STATE */
108 typedef struct ptrace_state {
109 	int	pe_report_event;
110 	union {
111 		pid_t	_pe_other_pid;
112 		lwpid_t	_pe_lwp;
113 	} _option;
114 } ptrace_state_t;
115 
116 #define	pe_other_pid	_option._pe_other_pid
117 #define	pe_lwp		_option._pe_lwp
118 
119 #define	PTRACE_FORK		0x0001	/* Report forks */
120 #define	PTRACE_VFORK		0x0002	/* Report vforks */
121 #define	PTRACE_VFORK_DONE	0x0004	/* Report parent resumed from vforks */
122 #define	PTRACE_LWP_CREATE	0x0008	/* Report LWP creation */
123 #define	PTRACE_LWP_EXIT		0x0010	/* Report LWP termination */
124 #define	PTRACE_POSIX_SPAWN	0x0020	/* Report posix_spawn */
125 
126 /*
127  * Argument structure for PT_IO.
128  */
129 struct ptrace_io_desc {
130 	int	piod_op;	/* I/O operation (see below) */
131 	void	*piod_offs;	/* child offset */
132 	void	*piod_addr;	/* parent offset */
133 	size_t	piod_len;	/* request length (in)/actual count (out) */
134 };
135 
136 /* piod_op */
137 #define	PIOD_READ_D	1	/* read from D space */
138 #define	PIOD_WRITE_D	2	/* write to D space */
139 #define	PIOD_READ_I	3	/* read from I space */
140 #define	PIOD_WRITE_I	4	/* write to I space */
141 #define PIOD_READ_AUXV	5	/* Read from aux array */
142 
143 #if defined(__LEGACY_PT_LWPINFO) || defined(_KERNEL)
144 /*
145  * Argument structure for PT_LWPINFO.
146  *
147  * DEPRECATED: Use ptrace_lwpstatus.
148  */
149 struct ptrace_lwpinfo {
150 	lwpid_t	pl_lwpid;	/* LWP described */
151 	int	pl_event;	/* Event that stopped the LWP */
152 };
153 
154 #define PL_EVENT_NONE		0
155 #define PL_EVENT_SIGNAL		1
156 #define PL_EVENT_SUSPENDED	2
157 #endif
158 
159 /*
160  * Argument structure for PT_LWPSTATUS.
161  */
162 
163 #define PL_LNAMELEN	20	/* extra 4 for alignment */
164 
165 struct ptrace_lwpstatus {
166 	lwpid_t		pl_lwpid;		/* LWP described */
167 	sigset_t	pl_sigpend;		/* LWP signals pending */
168 	sigset_t	pl_sigmask;		/* LWP signal mask */
169 	char		pl_name[PL_LNAMELEN];	/* LWP name, may be empty */
170 	void		*pl_private;		/* LWP private data */
171 	/* Add fields at the end */
172 };
173 
174 /*
175  * Signal Information structure
176  */
177 typedef struct ptrace_siginfo {
178 	siginfo_t	psi_siginfo;	/* signal information structure */
179 	lwpid_t		psi_lwpid;	/* destination LWP of the signal
180 					 * value 0 means the whole process
181 					 * (route signal to all LWPs) */
182 } ptrace_siginfo_t;
183 
184 #ifdef _KERNEL
185 
186 #ifdef _KERNEL_OPT
187 #include "opt_compat_netbsd32.h"
188 #endif
189 
190 #ifdef COMPAT_NETBSD32
191 #include <compat/netbsd32/netbsd32.h>
192 #define process_read_lwpstatus32	netbsd32_read_lwpstatus
193 #define process_lwpstatus32		struct netbsd32_ptrace_lwpstatus
194 #endif
195 
196 #ifndef process_lwpstatus32
197 #define process_lwpstatus32 struct ptrace_lwpstatus
198 #endif
199 #ifndef process_lwpstatus64
200 #define process_lwpstatus64 struct ptrace_lwpstatus
201 #endif
202 
203 #if defined(PT_GETREGS) || defined(PT_SETREGS)
204 struct reg;
205 #ifndef process_reg32
206 #define process_reg32 struct reg
207 #endif
208 #ifndef process_reg64
209 #define process_reg64 struct reg
210 #endif
211 #endif
212 
213 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
214 struct fpreg;
215 #ifndef process_fpreg32
216 #define process_fpreg32 struct fpreg
217 #endif
218 #ifndef process_fpreg64
219 #define process_fpreg64 struct fpreg
220 #endif
221 #endif
222 
223 #if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
224 struct dbreg;
225 #ifndef process_dbreg32
226 #define process_dbreg32 struct dbreg
227 #endif
228 #ifndef process_dbreg64
229 #define process_dbreg64 struct dbreg
230 #endif
231 #endif
232 
233 struct ptrace_methods {
234 	int (*ptm_copyin_piod)(struct ptrace_io_desc *, const void *, size_t);
235 	int (*ptm_copyout_piod)(const struct ptrace_io_desc *, void *, size_t);
236 	int (*ptm_copyin_siginfo)(struct ptrace_siginfo *, const void *, size_t);
237 	int (*ptm_copyout_siginfo)(const struct ptrace_siginfo *, void *, size_t);
238 	int (*ptm_copyout_lwpstatus)(const struct ptrace_lwpstatus *, void *, size_t);
239 	int (*ptm_doregs)(struct lwp *, struct lwp *, struct uio *);
240 	int (*ptm_dofpregs)(struct lwp *, struct lwp *, struct uio *);
241 	int (*ptm_dodbregs)(struct lwp *, struct lwp *, struct uio *);
242 };
243 
244 int	ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid);
245 void	ptrace_hooks(void);
246 
247 int	process_doregs(struct lwp *, struct lwp *, struct uio *);
248 int	process_validregs(struct lwp *);
249 
250 int	process_dofpregs(struct lwp *, struct lwp *, struct uio *);
251 int	process_validfpregs(struct lwp *);
252 
253 int	process_dodbregs(struct lwp *, struct lwp *, struct uio *);
254 int	process_validdbregs(struct lwp *);
255 
256 int	process_domem(struct lwp *, struct lwp *, struct uio *);
257 
258 void	proc_stoptrace(int, int, const register_t[], const register_t *, int);
259 void	proc_reparent(struct proc *, struct proc *);
260 void	proc_changeparent(struct proc *, struct proc *);
261 
262 
263 int	do_ptrace(struct ptrace_methods *, struct lwp *, int, pid_t, void *,
264 	    int, register_t *);
265 
266 void	ptrace_read_lwpstatus(struct lwp *, struct ptrace_lwpstatus *);
267 
268 void	process_read_lwpstatus(struct lwp *, struct ptrace_lwpstatus *);
269 #ifndef process_read_lwpstatus32
270 #define process_read_lwpstatus32 process_read_lwpstatus
271 #endif
272 #ifndef process_read_lwpstatus64
273 #define process_read_lwpstatus64 process_read_lwpstatus
274 #endif
275 
276 /*
277  * 64bit architectures that support 32bit emulation (amd64 and sparc64)
278  * will #define process_read_regs32 to netbsd32_process_read_regs (etc).
279  * In all other cases these #defines drop the size suffix.
280  */
281 
282 #ifdef PT_GETDBREGS
283 int	process_read_dbregs(struct lwp *, struct dbreg *, size_t *);
284 #ifndef process_read_dbregs32
285 #define process_read_dbregs32	process_read_dbregs
286 #endif
287 #ifndef process_read_dbregs64
288 #define process_read_dbregs64	process_read_dbregs
289 #endif
290 #endif
291 #ifdef PT_GETFPREGS
292 int	process_read_fpregs(struct lwp *, struct fpreg *, size_t *);
293 #ifndef process_read_fpregs32
294 #define process_read_fpregs32	process_read_fpregs
295 #endif
296 #ifndef process_read_fpregs64
297 #define process_read_fpregs64	process_read_fpregs
298 #endif
299 #endif
300 #ifdef PT_GETREGS
301 int	process_read_regs(struct lwp *, struct reg *);
302 #ifndef process_read_regs32
303 #define process_read_regs32	process_read_regs
304 #endif
305 #ifndef process_read_regs64
306 #define process_read_regs64	process_read_regs
307 #endif
308 #endif
309 int	process_set_pc(struct lwp *, void *);
310 int	process_sstep(struct lwp *, int);
311 #ifdef PT_SETDBREGS
312 int	process_write_dbregs(struct lwp *, const struct dbreg *, size_t);
313 #ifndef process_write_dbregs32
314 #define process_write_dbregs32	process_write_dbregs
315 #endif
316 #ifndef process_write_dbregs64
317 #define process_write_dbregs64	process_write_dbregs
318 #endif
319 #endif
320 #ifdef PT_SETFPREGS
321 int	process_write_fpregs(struct lwp *, const struct fpreg *, size_t);
322 #ifndef process_write_fpregs32
323 #define process_write_fpregs32	process_write_fpregs
324 #endif
325 #ifndef process_write_fpregs64
326 #define process_write_fpregs64	process_write_fpregs
327 #endif
328 #endif
329 #ifdef PT_SETREGS
330 int	process_write_regs(struct lwp *, const struct reg *);
331 #ifndef process_write_regs32
332 #define process_write_regs32	process_write_regs
333 #endif
334 #ifndef process_write_regs64
335 #define process_write_regs64	process_write_regs
336 #endif
337 #endif
338 
339 int	ptrace_machdep_dorequest(struct lwp *, struct lwp **, int,
340 	    void *, int);
341 
342 #ifndef FIX_SSTEP
343 #define FIX_SSTEP(p)
344 #endif
345 
346 typedef int (*ptrace_regrfunc_t)(struct lwp *, void *, size_t *);
347 typedef int (*ptrace_regwfunc_t)(struct lwp *, void *, size_t);
348 
349 #if defined(PT_SETREGS) || defined(PT_GETREGS) || \
350     defined(PT_SETFPREGS) || defined(PT_GETFPREGS) || \
351     defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
352 # define PT_REGISTERS
353 #endif
354 
355 #else /* !_KERNEL */
356 
357 #include <sys/cdefs.h>
358 
359 __BEGIN_DECLS
360 int	ptrace(int _request, pid_t _pid, void *_addr, int _data);
361 __END_DECLS
362 
363 #endif /* !_KERNEL */
364 
365 #endif	/* !_SYS_PTRACE_H_ */
366