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