xref: /freebsd/sys/sys/ptrace.h (revision 68d4b311270ecca80bbb887a5e502e2caacebc98)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 
32 #ifndef	_SYS_PTRACE_H_
33 #define	_SYS_PTRACE_H_
34 
35 #include <sys/signal.h>
36 #include <sys/param.h>
37 #include <machine/reg.h>
38 
39 #define	PT_TRACE_ME	0	/* child declares it's being traced */
40 #define	PT_READ_I	1	/* read word in child's I space */
41 #define	PT_READ_D	2	/* read word in child's D space */
42 /* was	PT_READ_U	3	 * read word in child's user structure */
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 /* was	PT_WRITE_U	6	 * write word in child's user structure */
46 #define	PT_CONTINUE	7	/* continue the child */
47 #define	PT_KILL		8	/* kill the child process */
48 #define	PT_STEP		9	/* single step the child */
49 
50 #define	PT_ATTACH	10	/* trace some running process */
51 #define	PT_DETACH	11	/* stop tracing a process */
52 #define PT_IO		12	/* do I/O to/from stopped process. */
53 #define	PT_LWPINFO	13	/* Info about the LWP that stopped. */
54 #define PT_GETNUMLWPS	14	/* get total number of threads */
55 #define PT_GETLWPLIST	15	/* get thread list */
56 #define PT_CLEARSTEP	16	/* turn off single step */
57 #define PT_SETSTEP	17	/* turn on single step */
58 #define PT_SUSPEND	18	/* suspend a thread */
59 #define PT_RESUME	19	/* resume a thread */
60 
61 #define	PT_TO_SCE	20
62 #define	PT_TO_SCX	21
63 #define	PT_SYSCALL	22
64 
65 #define	PT_FOLLOW_FORK	23
66 #define	PT_LWP_EVENTS	24	/* report LWP birth and exit */
67 
68 #define	PT_GET_EVENT_MASK 25	/* get mask of optional events */
69 #define	PT_SET_EVENT_MASK 26	/* set mask of optional events */
70 
71 #define	PT_GET_SC_ARGS	27	/* fetch syscall args */
72 #define	PT_GET_SC_RET	28	/* fetch syscall results */
73 
74 #define PT_COREDUMP	29	/* create a coredump */
75 
76 #define PT_GETREGS      33	/* get general-purpose registers */
77 #define PT_SETREGS      34	/* set general-purpose registers */
78 #define PT_GETFPREGS    35	/* get floating-point registers */
79 #define PT_SETFPREGS    36	/* set floating-point registers */
80 #define PT_GETDBREGS    37	/* get debugging registers */
81 #define PT_SETDBREGS    38	/* set debugging registers */
82 
83 #define	PT_VM_TIMESTAMP	40	/* Get VM version (timestamp) */
84 #define	PT_VM_ENTRY	41	/* Get VM map (entry) */
85 #define	PT_GETREGSET	42	/* Get a target register set */
86 #define	PT_SETREGSET	43	/* Set a target register set */
87 #define	PT_SC_REMOTE	44	/* Execute a syscall */
88 #define	PT_SET_SC_RET	45	/* Set (fake) syscall results */
89 #define	PT_GET_CHILDREN	46	/* Report children */
90 #define	PT_GET_ABI_NAME	47	/* Report ABI name */
91 
92 #define PT_FIRSTMACH    64	/* for machine-specific requests */
93 #define	PT_LASTMACH     127
94 #include <machine/ptrace.h>	/* machine-specific requests, if any */
95 
96 #ifdef _KERNEL
97 /* Space for ptrace commands not exposed directly to userspace. */
98 #define	PTINTERNAL_FIRST	128
99 #define	PTINTERNAL_LAST		191
100 #define	PTLINUX_GET_SC_ARGS	(PTINTERNAL_FIRST + 0)
101 #endif
102 
103 /* Events used with PT_GET_EVENT_MASK and PT_SET_EVENT_MASK */
104 #define	PTRACE_EXEC	0x0001
105 #define	PTRACE_SCE	0x0002
106 #define	PTRACE_SCX	0x0004
107 #define	PTRACE_SYSCALL	(PTRACE_SCE | PTRACE_SCX)
108 #define	PTRACE_FORK	0x0008
109 #define	PTRACE_LWP	0x0010
110 #define	PTRACE_VFORK	0x0020
111 
112 #define	PTRACE_DEFAULT	(PTRACE_EXEC)
113 
114 struct ptrace_io_desc {
115 	int	piod_op;	/* I/O operation */
116 	void	*piod_offs;	/* child offset */
117 	void	*piod_addr;	/* parent offset */
118 	size_t	piod_len;	/* request length */
119 };
120 
121 /*
122  * Operations in piod_op.
123  */
124 #define PIOD_READ_D	1	/* Read from D space */
125 #define PIOD_WRITE_D	2	/* Write to D space */
126 #define PIOD_READ_I	3	/* Read from I space */
127 #define PIOD_WRITE_I	4	/* Write to I space */
128 
129 /* Argument structure for PT_LWPINFO. */
130 struct ptrace_lwpinfo {
131 	lwpid_t	pl_lwpid;	/* LWP described. */
132 	int	pl_event;	/* Event that stopped the LWP. */
133 #define	PL_EVENT_NONE	0
134 #define	PL_EVENT_SIGNAL	1
135 	int	pl_flags;	/* LWP flags. */
136 #define	PL_FLAG_SA	0x01	/* M:N thread */
137 #define	PL_FLAG_BOUND	0x02	/* M:N bound thread */
138 #define	PL_FLAG_SCE	0x04	/* syscall enter point */
139 #define	PL_FLAG_SCX	0x08	/* syscall leave point */
140 #define	PL_FLAG_EXEC	0x10	/* exec(2) succeeded */
141 #define	PL_FLAG_SI	0x20	/* siginfo is valid */
142 #define	PL_FLAG_FORKED	0x40	/* new child */
143 #define	PL_FLAG_CHILD	0x80	/* I am from child */
144 #define	PL_FLAG_BORN	0x100	/* new LWP */
145 #define	PL_FLAG_EXITED	0x200	/* exiting LWP */
146 #define	PL_FLAG_VFORKED	0x400	/* new child via vfork */
147 #define	PL_FLAG_VFORK_DONE 0x800 /* vfork parent has resumed */
148 	sigset_t	pl_sigmask;	/* LWP signal mask */
149 	sigset_t	pl_siglist;	/* LWP pending signal */
150 	struct __siginfo pl_siginfo;	/* siginfo for signal */
151 	char		pl_tdname[MAXCOMLEN + 1]; /* LWP name */
152 	pid_t		pl_child_pid;	/* New child pid */
153 	u_int		pl_syscall_code;
154 	u_int		pl_syscall_narg;
155 };
156 
157 #if defined(_WANT_LWPINFO32) || (defined(_KERNEL) && defined(__LP64__))
158 struct ptrace_lwpinfo32 {
159 	lwpid_t	pl_lwpid;	/* LWP described. */
160 	int	pl_event;	/* Event that stopped the LWP. */
161 	int	pl_flags;	/* LWP flags. */
162 	sigset_t	pl_sigmask;	/* LWP signal mask */
163 	sigset_t	pl_siglist;	/* LWP pending signal */
164 	struct __siginfo32 pl_siginfo;	/* siginfo for signal */
165 	char		pl_tdname[MAXCOMLEN + 1]; /* LWP name. */
166 	pid_t		pl_child_pid;	/* New child pid */
167 	u_int		pl_syscall_code;
168 	u_int		pl_syscall_narg;
169 };
170 #endif
171 
172 /* Argument structure for PT_GET_SC_RET and PT_SET_SC_RET. */
173 struct ptrace_sc_ret {
174 	syscallarg_t	sr_retval[2];	/* Only valid if sr_error == 0. */
175 	int		sr_error;
176 };
177 
178 /* Argument structure for PT_VM_ENTRY. */
179 struct ptrace_vm_entry {
180 	int		pve_entry;	/* Entry number used for iteration. */
181 	int		pve_timestamp;	/* Generation number of VM map. */
182 	u_long		pve_start;	/* Start VA of range. */
183 	u_long		pve_end;	/* End VA of range (incl). */
184 	u_long		pve_offset;	/* Offset in backing object. */
185 	u_int		pve_prot;	/* Protection of memory range. */
186 	u_int		pve_pathlen;	/* Size of path. */
187 	long		pve_fileid;	/* File ID. */
188 	uint32_t	pve_fsid;	/* File system ID. */
189 	char		*pve_path;	/* Path name of object. */
190 };
191 
192 /* Argument structure for PT_COREDUMP */
193 struct ptrace_coredump {
194 	int		pc_fd;		/* File descriptor to write dump to. */
195 	uint32_t	pc_flags;	/* Flags PC_* */
196 	off_t		pc_limit;	/* Maximum size of the coredump,
197 					   0 for no limit. */
198 };
199 
200 /* Flags for PT_COREDUMP pc_flags */
201 #define	PC_COMPRESS	0x00000001	/* Allow compression */
202 #define	PC_ALL		0x00000002	/* Include non-dumpable entries */
203 
204 struct ptrace_sc_remote {
205 	struct ptrace_sc_ret pscr_ret;
206 	u_int	pscr_syscall;
207 	u_int	pscr_nargs;
208 	syscallarg_t	*pscr_args;
209 };
210 
211 #define	PTCHLD_TRACED		0x00000001
212 #define	PTCHLD_TRACED_BY_ME	0x00000002
213 #define	PTCHLD_EXITED		0x00000004
214 #define	PTCHLD_ORPHAN		0x00000008
215 
216 struct ptrace_child {
217 	pid_t	pid;
218 	int	flags;
219 	uint64ptr_t rsrv[3];
220 };
221 
222 #ifdef _KERNEL
223 
224 #include <sys/proc.h>
225 
226 struct thr_coredump_req {
227 	struct vnode	*tc_vp;		/* vnode to write coredump to. */
228 	off_t		tc_limit;	/* max coredump file size. */
229 	int		tc_flags;	/* user flags */
230 	int		tc_error;	/* request result */
231 };
232 
233 struct thr_syscall_req {
234 	struct ptrace_sc_ret ts_ret;
235 	u_int	ts_nargs;
236 	struct syscall_args ts_sa;
237 };
238 
239 int	ptrace_set_pc(struct thread *_td, unsigned long _addr);
240 int	ptrace_single_step(struct thread *_td);
241 int	ptrace_clear_single_step(struct thread *_td);
242 
243 #ifdef __HAVE_PTRACE_MACHDEP
244 int	cpu_ptrace(struct thread *_td, int _req, void *_addr, int _data);
245 #endif
246 
247 /*
248  * These are prototypes for functions that implement some of the
249  * debugging functionality exported by procfs / linprocfs and by the
250  * ptrace(2) syscall.  They used to be part of procfs, but they don't
251  * really belong there.
252  */
253 struct reg;
254 struct fpreg;
255 struct dbreg;
256 struct uio;
257 int	proc_read_regs(struct thread *_td, struct reg *_reg);
258 int	proc_write_regs(struct thread *_td, struct reg *_reg);
259 int	proc_read_fpregs(struct thread *_td, struct fpreg *_fpreg);
260 int	proc_write_fpregs(struct thread *_td, struct fpreg *_fpreg);
261 int	proc_read_dbregs(struct thread *_td, struct dbreg *_dbreg);
262 int	proc_write_dbregs(struct thread *_td, struct dbreg *_dbreg);
263 int	proc_sstep(struct thread *_td);
264 
265 #define	PRVM_BLOCK_EXEC		0x00000001
266 #define	PRVM_CHECK_VISIBILITY	0x00000002
267 #define	PRVM_CHECK_DEBUG	0x00000004
268 
269 #include <sys/_uio.h>
270 struct vmspace;
271 int	proc_vmspace_ref(struct thread *_td, struct proc *_p, int _flags,
272 	    struct vmspace **_vmp);
273 void	proc_vmspace_unref(struct thread *_td, struct proc *_p, int _flags,
274 	    struct vmspace *_vm);
275 ssize_t	vmspace_iop(struct thread *td, struct vmspace *vm, vm_offset_t va,
276 	    void *buf, size_t len, enum uio_rw rw);
277 int	proc_rwmem(struct proc *_p, struct uio *_uio, int _flags);
278 ssize_t	proc_readmem(struct thread *_td, struct proc *_p, vm_offset_t _va,
279 	    void *_buf, size_t _len);
280 ssize_t	proc_writemem(struct thread *_td, struct proc *_p, vm_offset_t _va,
281 	    void *_buf, size_t _len);
282 #ifdef COMPAT_FREEBSD32
283 struct reg32;
284 struct fpreg32;
285 struct dbreg32;
286 int	proc_read_regs32(struct thread *_td, struct reg32 *_reg32);
287 int	proc_write_regs32(struct thread *_td, struct reg32 *_reg32);
288 int	proc_read_fpregs32(struct thread *_td, struct fpreg32 *_fpreg32);
289 int	proc_write_fpregs32(struct thread *_td, struct fpreg32 *_fpreg32);
290 int	proc_read_dbregs32(struct thread *_td, struct dbreg32 *_dbreg32);
291 int	proc_write_dbregs32(struct thread *_td, struct dbreg32 *_dbreg32);
292 #endif
293 
294 void	ptrace_unsuspend(struct proc *p);
295 int	ptrace_action(struct thread *td, int req, bool pd_mode, pid_t pid,
296 	    int pfd, lwpid_t lwpid, void *addr, int data);
297 
298 extern bool allow_ptrace;
299 extern bool allow_ptrace_in_cap_mode;
300 
301 #else /* !_KERNEL */
302 
303 #include <sys/cdefs.h>
304 
305 __BEGIN_DECLS
306 int	ptrace(int _request, pid_t _pid, caddr_t _addr, int _data);
307 __END_DECLS
308 
309 #endif /* !_KERNEL */
310 
311 #endif	/* !_SYS_PTRACE_H_ */
312