xref: /dragonfly/lib/libc/sys/ptrace.2 (revision 896f2e3a)
1.\" $FreeBSD: src/lib/libc/sys/ptrace.2,v 1.12.2.12 2001/12/14 18:34:01 ru Exp $
2.\" $DragonFly: src/lib/libc/sys/ptrace.2,v 1.4 2006/05/26 19:39:37 swildner Exp $
3.\"	$NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
4.\"
5.\" This file is in the public domain.
6.Dd January 18, 2015
7.Dt PTRACE 2
8.Os
9.Sh NAME
10.Nm ptrace
11.Nd process tracing and debugging
12.Sh LIBRARY
13.Lb libc
14.Sh SYNOPSIS
15.In sys/types.h
16.In sys/ptrace.h
17.Ft int
18.Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
19.Sh DESCRIPTION
20.Fn ptrace
21provides tracing and debugging facilities.  It allows one process (the
22.Em tracing
23process) to control another (the
24.Em traced
25process).  Most of the time, the traced process runs normally, but when
26it receives a signal
27(see
28.Xr sigaction 2 ) ,
29it stops.  The tracing process is expected to notice this via
30.Xr wait 2
31or the delivery of a
32.Dv SIGCHLD
33signal, examine the state of the stopped process, and cause it to
34terminate or continue as appropriate.
35.Fn ptrace
36is the mechanism by which all this happens.
37.Pp
38The
39.Fa request
40argument specifies what operation is being performed; the meaning of
41the rest of the arguments depends on the operation, but except for one
42special case noted below, all
43.Fn ptrace
44calls are made by the tracing process, and the
45.Fa pid
46argument specifies the process ID of the traced process.
47.Fa request
48can be:
49.Bl -tag -width 12n
50.It Dv PT_TRACE_ME
51This request is the only one used by the traced process; it declares
52that the process expects to be traced by its parent.  All the other
53arguments are ignored.  (If the parent process does not expect to trace
54the child, it will probably be rather confused by the results; once the
55traced process stops, it cannot be made to continue except via
56.Fn ptrace . )
57When a process has used this request and calls
58.Xr execve 2
59or any of the routines built on it
60(such as
61.Xr execv 3 ) ,
62it will stop before executing the first instruction of the new image.
63Also, any setuid or setgid bits on the executable being executed will
64be ignored.
65.It Dv PT_READ_I , Dv PT_READ_D
66These requests read a single
67.Vt int
68of data from the traced process' address space.  Traditionally,
69.Fn ptrace
70has allowed for machines with distinct address spaces for instruction
71and data, which is why there are two requests: conceptually,
72.Dv PT_READ_I
73reads from the instruction space and
74.Dv PT_READ_D
75reads from the data space.  In the current
76.Dx
77implementation, these
78two requests are completely identical.  The
79.Fa addr
80argument specifies the address (in the traced process' virtual address
81space) at which the read is to be done.  This address does not have to
82meet any alignment constraints.  The value read is returned as the
83return value from
84.Eo \&
85.Fn ptrace
86.Ec .
87.It Dv PT_WRITE_I , Dv PT_WRITE_D
88These requests parallel
89.Dv PT_READ_I
90and
91.Dv PT_READ_D ,
92except that they write rather than read.  The
93.Fa data
94argument supplies the value to be written.
95.It Dv PT_IO
96This request allows reading and writing arbitrary amounts of data in
97the traced process's address space.
98The
99.Fa addr
100argument specifies a pointer to a
101.Vt "struct ptrace_io_desc" ,
102which is defined as follows:
103.Bd -literal
104struct ptrace_io_desc {
105	int	piod_op;	/* I/O operation */
106	void	*piod_offs;	/* child offset */
107	void	*piod_addr;	/* parent offset */
108	size_t	piod_len;	/* request length */
109};
110
111/*
112 * Operations in piod_op.
113 */
114#define PIOD_READ_D	1	/* Read from D space */
115#define PIOD_WRITE_D	2	/* Write to D space */
116#define PIOD_READ_I	3	/* Read from I space */
117#define PIOD_WRITE_I	4	/* Write to I space */
118.Ed
119.Pp
120The
121.Fa data
122argument is ignored.
123The actual number of bytes read or written is stored in
124.Va piod_len
125upon return.
126.It Dv PT_CONTINUE
127The traced process continues execution.
128.Fa addr
129is an address specifying the place where execution is to be resumed (a
130new value for the program counter), or
131.Po Vt caddr_t Pc Ns 1
132to indicate that execution is to pick up where it left off.
133.Fa data
134provides a signal number to be delivered to the traced process as it
135resumes execution, or 0 if no signal is to be sent.
136.It Dv PT_STEP
137The traced process is single stepped one instruction.
138.Fa addr
139should be passed
140.Po Vt caddr_t Pc Ns 1 .
141.Fa data
142is not used.
143.It Dv PT_KILL
144The traced process terminates, as if
145.Dv PT_CONTINUE
146had been used with
147.Dv SIGKILL
148given as the signal to be delivered.
149.It Dv PT_ATTACH
150This request allows a process to gain control of an otherwise unrelated
151process and begin tracing it.  It does not need any cooperation from
152the to-be-traced process.  In this case,
153.Fa pid
154specifies the process ID of the to-be-traced process, and the other two
155arguments are ignored.  This request requires that the target process
156must have the same real UID as the tracing process, and that it must
157not be executing a setuid or setgid executable.  (If the tracing
158process is running as root, these restrictions do not apply.)  The
159tracing process will see the newly-traced process stop and may then
160control it as if it had been traced all along.
161.It Dv PT_DETACH
162This request is like PT_CONTINUE, except that it does not allow
163specifying an alternate place to continue execution, and after it
164succeeds, the traced process is no longer traced and continues
165execution normally.
166.El
167.Pp
168Additionally, machine-specific requests can exist.  On the i386, these
169are:
170.Bl -tag -width 12n
171.It Dv PT_GETREGS
172This request reads the traced process' machine registers into the
173.Do
174.Vt "struct reg"
175.Dc
176(defined in
177.In machine/reg.h )
178pointed to by
179.Fa addr .
180.It Dv PT_SETREGS
181This request is the converse of
182.Dv PT_GETREGS ;
183it loads the traced process' machine registers from the
184.Do
185.Vt "struct reg"
186.Dc
187(defined in
188.In machine/reg.h )
189pointed to by
190.Fa addr .
191.It Dv PT_GETFPREGS
192This request reads the traced process' floating-point registers into
193the
194.Do
195.Vt "struct fpreg"
196.Dc
197(defined in
198.In machine/reg.h )
199pointed to by
200.Fa addr .
201.It Dv PT_SETFPREGS
202This request is the converse of
203.Dv PT_GETFPREGS ;
204it loads the traced process' floating-point registers from the
205.Do
206.Vt "struct fpreg"
207.Dc
208(defined in
209.In machine/reg.h )
210pointed to by
211.Fa addr .
212.It Dv PT_GETDBREGS
213This request reads the traced process' debug registers into
214the
215.Do
216.Vt "struct dbreg"
217.Dc
218(defined in
219.In machine/reg.h )
220pointed to by
221.Fa addr .
222.It Dv PT_SETDBREGS
223This request is the converse of
224.Dv PT_GETDBREGS ;
225it loads the traced process' debug registers from the
226.Do
227.Vt "struct dbreg"
228.Dc
229(defined in
230.In machine/reg.h )
231pointed to by
232.Fa addr .
233.El
234.Sh RETURN VALUES
235Some requests can cause
236.Fn ptrace
237to return
238.Li -1
239as a non-error value; to disambiguate,
240.Va errno
241can be set to 0 before the call and checked afterwards.
242.Sh ERRORS
243The
244.Fn ptrace
245function may fail if:
246.Bl -tag -width Er
247.It Bq Er ESRCH
248.Bl -bullet -compact
249.It
250No process having the specified process ID exists.
251.El
252.It Bq Er EINVAL
253.Bl -bullet -compact
254.It
255A process attempted to use
256.Dv PT_ATTACH
257on itself.
258.It
259The
260.Fa request
261was not one of the legal requests.
262.It
263The signal number (in
264.Fa data )
265to
266.Dv PT_CONTINUE
267was neither 0 nor a legal signal number.
268.It
269The
270.Fa pid
271represents a system process.
272.It
273.Dv PT_GETREGS ,
274.Dv PT_SETREGS ,
275.Dv PT_GETFPREGS ,
276.Dv PT_SETFPREGS ,
277.Dv PT_GETDBREGS ,
278or
279.Dv PT_SETDBREGS
280was attempted on a process with no valid register set.  (This is
281normally true only of system processes.)
282.El
283.It Bq Er EBUSY
284.Bl -bullet -compact
285.It
286.Dv PT_ATTACH
287was attempted on a process that was already being traced.
288.It
289A request attempted to manipulate a process that was being traced by
290some process other than the one making the request.
291.It
292A request (other than
293.Dv PT_ATTACH )
294specified a process that wasn't stopped.
295.El
296.It Bq Er EPERM
297.Bl -bullet -compact
298.It
299A request (other than
300.Dv PT_ATTACH )
301attempted to manipulate a process that wasn't being traced at all.
302.It
303An attempt was made to use
304.Dv PT_ATTACH
305on a process in violation of the requirements listed under
306.Dv PT_ATTACH
307above.
308.El
309.El
310.Sh SEE ALSO
311.Xr execve 2 ,
312.Xr sigaction 2 ,
313.Xr wait 2 ,
314.Xr execv 3 ,
315.Xr i386_clr_watch 3 ,
316.Xr i386_set_watch 3
317.Sh HISTORY
318A
319.Fn ptrace
320function call appeared in
321.At v7 .
322