xref: /dragonfly/lib/libc/sys/ptrace.2 (revision 0bb9290e)
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 20, 1996
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_CONTINUE
96The traced process continues execution.
97.Fa addr
98is an address specifying the place where execution is to be resumed (a
99new value for the program counter), or
100.Po Vt caddr_t Pc Ns 1
101to indicate that execution is to pick up where it left off.
102.Fa data
103provides a signal number to be delivered to the traced process as it
104resumes execution, or 0 if no signal is to be sent.
105.It Dv PT_STEP
106The traced process is single stepped one instruction.
107.Fa addr
108should be passed
109.Po Vt caddr_t Pc Ns 1 .
110.Fa data
111is not used.
112.It Dv PT_KILL
113The traced process terminates, as if
114.Dv PT_CONTINUE
115had been used with
116.Dv SIGKILL
117given as the signal to be delivered.
118.It Dv PT_ATTACH
119This request allows a process to gain control of an otherwise unrelated
120process and begin tracing it.  It does not need any cooperation from
121the to-be-traced process.  In this case,
122.Fa pid
123specifies the process ID of the to-be-traced process, and the other two
124arguments are ignored.  This request requires that the target process
125must have the same real UID as the tracing process, and that it must
126not be executing a setuid or setgid executable.  (If the tracing
127process is running as root, these restrictions do not apply.)  The
128tracing process will see the newly-traced process stop and may then
129control it as if it had been traced all along.
130.It Dv PT_DETACH
131This request is like PT_CONTINUE, except that it does not allow
132specifying an alternate place to continue execution, and after it
133succeeds, the traced process is no longer traced and continues
134execution normally.
135.El
136.Pp
137Additionally, machine-specific requests can exist.  On the i386, these
138are:
139.Bl -tag -width 12n
140.It Dv PT_GETREGS
141This request reads the traced process' machine registers into the
142.Do
143.Vt "struct reg"
144.Dc
145(defined in
146.In machine/reg.h )
147pointed to by
148.Fa addr .
149.It Dv PT_SETREGS
150This request is the converse of
151.Dv PT_GETREGS ;
152it loads the traced process' machine registers from the
153.Do
154.Vt "struct reg"
155.Dc
156(defined in
157.In machine/reg.h )
158pointed to by
159.Fa addr .
160.It Dv PT_GETFPREGS
161This request reads the traced process' floating-point registers into
162the
163.Do
164.Vt "struct fpreg"
165.Dc
166(defined in
167.In machine/reg.h )
168pointed to by
169.Fa addr .
170.It Dv PT_SETFPREGS
171This request is the converse of
172.Dv PT_GETFPREGS ;
173it loads the traced process' floating-point registers from the
174.Do
175.Vt "struct fpreg"
176.Dc
177(defined in
178.In machine/reg.h )
179pointed to by
180.Fa addr .
181.It Dv PT_GETDBREGS
182This request reads the traced process' debug registers into
183the
184.Do
185.Vt "struct dbreg"
186.Dc
187(defined in
188.In machine/reg.h )
189pointed to by
190.Fa addr .
191.It Dv PT_SETDBREGS
192This request is the converse of
193.Dv PT_GETDBREGS ;
194it loads the traced process' debug registers from the
195.Do
196.Vt "struct dbreg"
197.Dc
198(defined in
199.In machine/reg.h )
200pointed to by
201.Fa addr .
202.El
203.Sh RETURN VALUES
204Some requests can cause
205.Fn ptrace
206to return
207.Li -1
208as a non-error value; to disambiguate,
209.Va errno
210can be set to 0 before the call and checked afterwards.
211.Sh ERRORS
212The
213.Fn ptrace
214function may fail if:
215.Bl -tag -width Er
216.It Bq Er ESRCH
217.Bl -bullet -compact
218.It
219No process having the specified process ID exists.
220.El
221.It Bq Er EINVAL
222.Bl -bullet -compact
223.It
224A process attempted to use
225.Dv PT_ATTACH
226on itself.
227.It
228The
229.Fa request
230was not one of the legal requests.
231.It
232The signal number (in
233.Fa data )
234to
235.Dv PT_CONTINUE
236was neither 0 nor a legal signal number.
237.It
238.Dv PT_GETREGS ,
239.Dv PT_SETREGS ,
240.Dv PT_GETFPREGS ,
241.Dv PT_SETFPREGS ,
242.Dv PT_GETDBREGS ,
243or
244.Dv PT_SETDBREGS
245was attempted on a process with no valid register set.  (This is
246normally true only of system processes.)
247.El
248.It Bq Er EBUSY
249.Bl -bullet -compact
250.It
251.Dv PT_ATTACH
252was attempted on a process that was already being traced.
253.It
254A request attempted to manipulate a process that was being traced by
255some process other than the one making the request.
256.It
257A request (other than
258.Dv PT_ATTACH )
259specified a process that wasn't stopped.
260.El
261.It Bq Er EPERM
262.Bl -bullet -compact
263.It
264A request (other than
265.Dv PT_ATTACH )
266attempted to manipulate a process that wasn't being traced at all.
267.It
268An attempt was made to use
269.Dv PT_ATTACH
270on a process in violation of the requirements listed under
271.Dv PT_ATTACH
272above.
273.El
274.El
275.Sh SEE ALSO
276.Xr execve 2 ,
277.Xr sigaction 2 ,
278.Xr wait 2 ,
279.Xr execv 3 ,
280.Xr i386_clr_watch 3 ,
281.Xr i386_set_watch 3
282.Sh HISTORY
283A
284.Fn ptrace
285function call appeared in
286.At v7 .
287