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