1/* $NetBSD: ptrace.h,v 1.65 2019/06/11 23:18:55 kamil 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
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#define PT_WRITE_I 4 /* write word in child's I space */
43#define PT_WRITE_D 5 /* write word in child's D space */
44#define PT_CONTINUE 7 /* continue the child */
45#define PT_KILL 8 /* kill the child process */
46#define PT_ATTACH 9 /* attach to running process */
47#define PT_DETACH 10 /* detach from running process */
48#define PT_IO 11 /* do I/O to/from the stopped process */
49#define PT_DUMPCORE 12 /* make child generate a core dump */
50#define PT_LWPINFO 13 /* get info about the LWP */
51#define PT_SYSCALL 14 /* stop on syscall entry/exit */
52#define PT_SYSCALLEMU 15 /* cancel syscall, tracer emulates it */
53#define PT_SET_EVENT_MASK 16 /* set the event mask, defined below */
54#define PT_GET_EVENT_MASK 17 /* get the event mask, defined below */
55#define PT_GET_PROCESS_STATE 18 /* get process state, defined below */
56#define PT_SET_SIGINFO 19 /* set signal state, defined below */
57#define PT_GET_SIGINFO 20 /* get signal state, defined below */
58#define PT_RESUME 21 /* allow execution of the LWP */
59#define PT_SUSPEND 22 /* prevent execution of the LWP */
60
61#define PT_FIRSTMACH 32 /* for machine-specific requests */
62#include <machine/ptrace.h> /* machine-specific requests, if any */
63
64#define PT_STRINGS \
65/* 0 */ "PT_TRACE_ME", \
66/* 1 */ "PT_READ_I", \
67/* 2 */ "PT_READ_D", \
68/* 3 */ "*PT_INVALID_3*", \
69/* 4 */ "PT_WRITE_I", \
70/* 5 */ "PT_WRITE_D", \
71/* 6 */ "*PT_INVALID_6*", \
72/* 7 */ "PT_CONTINUE", \
73/* 8 */ "PT_KILL", \
74/* 9 */ "PT_ATTACH", \
75/* 10 */ "PT_DETACH", \
76/* 11 */ "PT_IO", \
77/* 12 */ "PT_DUMPCORE", \
78/* 13 */ "PT_LWPINFO", \
79/* 14 */ "PT_SYSCALL", \
80/* 15 */ "PT_SYSCALLEMU", \
81/* 16 */ "PT_SET_EVENT_MASK", \
82/* 17 */ "PT_GET_EVENT_MASK", \
83/* 18 */ "PT_GET_PROCESS_STATE", \
84/* 19 */ "PT_SET_SIGINFO", \
85/* 20 */ "PT_GET_SIGINFO", \
86/* 21 */ "PT_RESUME", \
87/* 22 */ "PT_SUSPEND",
88
89/* PT_{G,S}EVENT_MASK */
90typedef struct ptrace_event {
91 int pe_set_event;
92} ptrace_event_t;
93
94/* PT_GET_PROCESS_STATE */
95typedef struct ptrace_state {
96 int pe_report_event;
97 union {
98 pid_t _pe_other_pid;
99 lwpid_t _pe_lwp;
100 } _option;
101} ptrace_state_t;
102
103#define pe_other_pid _option._pe_other_pid
104#define pe_lwp _option._pe_lwp
105
106#define PTRACE_FORK 0x0001 /* Report forks */
107#define PTRACE_VFORK 0x0002 /* Report vforks */
108#define PTRACE_VFORK_DONE 0x0004 /* Report parent resumed from vforks */
109#define PTRACE_LWP_CREATE 0x0008 /* Report LWP creation */
110#define PTRACE_LWP_EXIT 0x0010 /* Report LWP termination */
111#define PTRACE_POSIX_SPAWN 0x0020 /* Report posix_spawn */
112
113/*
114 * Argument structure for PT_IO.
115 */
116struct ptrace_io_desc {
117 int piod_op; /* I/O operation (see below) */
118 void *piod_offs; /* child offset */
119 void *piod_addr; /* parent offset */
120 size_t piod_len; /* request length (in)/actual count (out) */
121};
122
123/* piod_op */
124#define PIOD_READ_D 1 /* read from D space */
125#define PIOD_WRITE_D 2 /* write to D spcae */
126#define PIOD_READ_I 3 /* read from I space */
127#define PIOD_WRITE_I 4 /* write to I space */
128#define PIOD_READ_AUXV 5 /* Read from aux array */
129
130/*
131 * Argument structure for PT_LWPINFO.
132 */
133struct ptrace_lwpinfo {
134 lwpid_t pl_lwpid; /* LWP described */
135 int pl_event; /* Event that stopped the LWP */
136 /* Add fields at the end */
137};
138
139#define PL_EVENT_NONE 0
140#define PL_EVENT_SIGNAL 1
141#define PL_EVENT_SUSPENDED 2
142
143/*
144 * Signal Information structure
145 */
146typedef struct ptrace_siginfo {
147 siginfo_t psi_siginfo; /* signal information structure */
148 lwpid_t psi_lwpid; /* destination LWP of the signal
149 * value 0 means the whole process
150 * (route signal to all LWPs) */
151} ptrace_siginfo_t;
152
153#ifdef _KERNEL
154
155#if defined(PT_GETREGS) || defined(PT_SETREGS)
156struct reg;
157#ifndef process_reg32
158#define process_reg32 struct reg
159#endif
160#ifndef process_reg64
161#define process_reg64 struct reg
162#endif
163#endif
164
165#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
166struct fpreg;
167#ifndef process_fpreg32
168#define process_fpreg32 struct fpreg
169#endif
170#ifndef process_fpreg64
171#define process_fpreg64 struct fpreg
172#endif
173#endif
174
175#if defined(PT_GETDBREGS) || defined(PT_SETDBREGS)
176struct dbreg;
177#ifndef process_dbreg32
178#define process_dbreg32 struct dbreg
179#endif
180#ifndef process_dbreg64
181#define process_dbreg64 struct dbreg
182#endif
183#endif
184
185struct ptrace_methods {
186 int (*ptm_copyin_piod)(struct ptrace_io_desc *, const void *, size_t);
187 int (*ptm_copyout_piod)(const struct ptrace_io_desc *, void *, size_t);
188 int (*ptm_copyin_siginfo)(struct ptrace_siginfo *, const void *, size_t);
189 int (*ptm_copyout_siginfo)(const struct ptrace_siginfo *, void *, size_t);
190 int (*ptm_doregs)(struct lwp *, struct lwp *, struct uio *);
191 int (*ptm_dofpregs)(struct lwp *, struct lwp *, struct uio *);
192 int (*ptm_dodbregs)(struct lwp *, struct lwp *, struct uio *);
193};
194
195int ptrace_init(void);
196int ptrace_fini(void);
197void ptrace_hooks(void);
198
199int process_doregs(struct lwp *, struct lwp *, struct uio *);
200int process_validregs(struct lwp *);
201
202int process_dofpregs(struct lwp *, struct lwp *, struct uio *);
203int process_validfpregs(struct lwp *);
204
205int process_dodbregs(struct lwp *, struct lwp *, struct uio *);
206int process_validdbregs(struct lwp *);
207
208int process_domem(struct lwp *, struct lwp *, struct uio *);
209
210void proc_stoptrace(int, int, const register_t[], const register_t *, int);
211void proc_reparent(struct proc *, struct proc *);
212void proc_changeparent(struct proc *, struct proc *);
213
214
215int do_ptrace(struct ptrace_methods *, struct lwp *, int, pid_t, void *,
216 int, register_t *);
217
218/*
219 * 64bit architectures that support 32bit emulation (amd64 and sparc64)
220 * will #define process_read_regs32 to netbsd32_process_read_regs (etc).
221 * In all other cases these #defines drop the size suffix.
222 */
223#ifdef PT_GETDBREGS
224int process_read_dbregs(struct lwp *, struct dbreg *, size_t *);
225#ifndef process_read_dbregs32
226#define process_read_dbregs32 process_read_dbregs
227#endif
228#ifndef process_read_dbregs64
229#define process_read_dbregs64 process_read_dbregs
230#endif
231#endif
232#ifdef PT_GETFPREGS
233int process_read_fpregs(struct lwp *, struct fpreg *, size_t *);
234#ifndef process_read_fpregs32
235#define process_read_fpregs32 process_read_fpregs
236#endif
237#ifndef process_read_fpregs64
238#define process_read_fpregs64 process_read_fpregs
239#endif
240#endif
241#ifdef PT_GETREGS
242int process_read_regs(struct lwp *, struct reg *);
243#ifndef process_read_regs32
244#define process_read_regs32 process_read_regs
245#endif
246#ifndef process_read_regs64
247#define process_read_regs64 process_read_regs
248#endif
249#endif
250int process_set_pc(struct lwp *, void *);
251int process_sstep(struct lwp *, int);
252#ifdef PT_SETDBREGS
253int process_write_dbregs(struct lwp *, const struct dbreg *, size_t);
254#ifndef process_write_dbregs32
255#define process_write_dbregs32 process_write_dbregs
256#endif
257#ifndef process_write_dbregs64
258#define process_write_dbregs64 process_write_dbregs
259#endif
260#endif
261#ifdef PT_SETFPREGS
262int process_write_fpregs(struct lwp *, const struct fpreg *, size_t);
263#ifndef process_write_fpregs32
264#define process_write_fpregs32 process_write_fpregs
265#endif
266#ifndef process_write_fpregs64
267#define process_write_fpregs64 process_write_fpregs
268#endif
269#endif
270#ifdef PT_SETREGS
271int process_write_regs(struct lwp *, const struct reg *);
272#ifndef process_write_regs32
273#define process_write_regs32 process_write_regs
274#endif
275#ifndef process_write_regs64
276#define process_write_regs64 process_write_regs
277#endif
278#endif
279
280int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int,
281 void *, int);
282
283#ifndef FIX_SSTEP
284#define FIX_SSTEP(p)
285#endif
286
287#else /* !_KERNEL */
288
289#include <sys/cdefs.h>
290
291__BEGIN_DECLS
292int ptrace(int _request, pid_t _pid, void *_addr, int _data);
293__END_DECLS
294
295#endif /* !_KERNEL */
296
297#endif /* !_SYS_PTRACE_H_ */
298