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