1 .\" $NetBSD: ptrace.2,v 1.29 2007/03/06 16:07:22 drochner Exp $
3 .\" This file is in the public domain.
9 .Nd process tracing and debugging
16 .Fn ptrace "int request" "pid_t pid" "void *addr" "int data"
19 provides tracing and debugging facilities.
20 It allows one process (the
22 process) to control another (the
25 Most of the time, the traced process runs normally, but when
32 The tracing process is expected to notice this via
36 signal, examine the state of the stopped process, and cause it to
37 terminate or continue as appropriate.
39 is the mechanism by which all this happens.
43 argument specifies what operation is being performed; the meaning of
44 the rest of the arguments depends on the operation, but except for one
45 special case noted below, all
47 calls are made by the tracing process, and the
49 argument specifies the process ID of the traced process.
54 This request is the only one used by the traced process; it declares
55 that the process expects to be traced by its parent.
56 All the other arguments are ignored.
57 (If the parent process does not expect to trace
58 the child, it will probably be rather confused by the results; once the
59 traced process stops, it cannot be made to continue except via
61 When a process has used this request and calls
63 or any of the routines built on it
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
71 .It Dv PT_READ_I , Dv PT_READ_D
72 These requests read a single
74 of data from the traced process' address space.
77 has allowed for machines with distinct address spaces for instruction
78 and data, which is why there are two requests: conceptually,
80 reads from the instruction space and
82 reads from the data space.
86 two requests are completely identical.
89 argument specifies the address (in the traced process' virtual address
90 space) 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
96 .It Dv PT_WRITE_I , Dv PT_WRITE_D
97 These requests parallel
101 except that they write rather than read.
104 argument supplies the value to be written.
106 .\" This request reads an
108 .\" from the traced process' user structure.
111 .\" argument specifies the location of the int relative to the base of the
112 .\" user structure; it will usually be an integer value cast to
114 .\" either explicitly or via the presence of a prototype for
123 .\" must be aligned on an
126 .\" The value read is returned as the return value from
130 .\" .It Dv PT_WRITE_U
131 .\" This request writes an
133 .\" into the traced process' user structure.
135 .\" specifies the offset, just as for
139 .\" specifies the value to be written, just as for
144 The traced process continues execution.
146 is an address specifying the place where execution is to be resumed (a
147 new value for the program counter), or
149 to indicate that execution is to pick up where it left off.
151 provides a signal number to be delivered to the traced process as it
152 resumes execution, or 0 if no signal is to be sent.
154 The traced process terminates, as if
158 given as the signal to be delivered.
160 This request allows a process to gain control of an otherwise unrelated
161 process and begin tracing it.
162 It does not need any cooperation from the to-be-traced process.
165 specifies the process ID of the to-be-traced process, and the other two
166 arguments are ignored.
167 This request requires that the target process
168 must have the same real UID as the tracing process, and that it must
169 not be executing a setuid or setgid executable.
170 (If the tracing process is running as root,
171 these restrictions do not apply.)
172 The tracing process will see the newly-traced process stop and may then
173 control it as if it had been traced all along.
175 Three other restrictions apply to all tracing processes, even those
177 First, no process may trace a system process.
178 Second, no process may trace the process running
180 Third, if a process has its root directory set with
182 it may not trace another process unless that process's root directory
183 is at or below the tracing process's root.
185 This request is like PT_CONTINUE, except that after it
186 succeeds, the traced process is no longer traced and continues
189 This request is a more general interface that can be used instead of
195 The I/O request is encoded in a
196 .Dq Li "struct ptrace_io_desc"
198 .Bd -literal -offset indent
199 struct ptrace_io_desc {
209 is the offset within the traced process where the I/O operation should
212 is the buffer in the tracing process, and
214 is the length of the I/O request.
217 field specifies which type of I/O operation to perform.
227 See the description of
229 for the difference between I and D spaces.
230 A pointer to the I/O descriptor is passed in the
236 field in the I/O descriptor will be updated with the actual number of
238 If the requested I/O could not be successfully performed,
245 Makes the process specified in the
247 pid generate a core dump.
250 argument should contain the name of the core file to be generated
253 argument should contain the length of the core filename.
256 call currently does not stop the child process so it can generate
259 Returns information about the specific thread from the process specified
265 argument should contain a
266 .Dq Li "struct ptrace_lwpinfo"
268 .Bd -literal -offset indent
269 struct ptrace_lwpinfo {
277 contains the thread for which to get info.
280 contains the event that stopped the thread.
285 .It Dv PL_EVENT_SIGNAL
290 argument should contain
291 .Dq Li "sizeof(struct ptrace_lwpinfo)" .
293 Stops a process before and after executing each system call.
296 Additionally, the following requests exist but are
297 not available on all machine architectures.
299 .Aq Pa machine/ptrace.h
300 lists which requests exist on a given machine.
303 Execution continues as in request PT_CONTINUE; however
304 as soon as possible after execution of at least one
305 instruction, execution stops again.
307 This request reads the traced process' machine registers into the
310 .Aq Pa machine/reg.h )
314 This request is the converse of
316 it loads the traced process' machine registers from the
319 .Aq Pa machine/reg.h )
323 This request reads the traced process' floating-point registers into
325 .Dq Li "struct fpreg"
327 .Aq Pa machine/reg.h )
331 This request is the converse of
333 it loads the traced process' floating-point registers from the
334 .Dq Li "struct fpreg"
336 .Aq Pa machine/reg.h )
339 .\" .It Dv PT_SYSCALL
340 .\" This request is like
342 .\" except that the process will stop next time it executes any system
343 .\" call. Information about the system call can be examined with
345 .\" and potentially modified with
348 .\" .Li u_kproc.kp_proc.p_md
349 .\" element of the user structure (see below). If the process is continued
352 .\" request, it will stop again on exit from the syscall, at which point
353 .\" the return values can be examined and potentially changed. The
354 .\" .Li u_kproc.kp_proc.p_md
355 .\" element is of type
356 .\" .Dq Li "struct mdproc" ,
357 .\" which should be declared by including
358 .\" .Aq Pa sys/param.h ,
359 .\" .Aq Pa sys/user.h ,
361 .\" .Aq Pa machine/proc.h ,
362 .\" and contains the following fields (among others):
363 .\" .Bl -item -compact -offset indent
367 .\" .Li syscall_nargs
369 .\" .Li syscall_args[8]
373 .\" .Li syscall_rv[2]
375 .\" When a process stops on entry to a syscall,
377 .\" holds the number of the syscall,
378 .\" .Li syscall_nargs
379 .\" holds the number of arguments it expects, and
381 .\" holds the arguments themselves.
383 .\" .Li syscall_nargs
386 .\" are guaranteed to be useful.)
387 .\" When a process stops on exit from a syscall,
394 .\" holds the error number
399 .\" or 0 if no error occurred, and
401 .\" holds the return values.
402 .\" (If the syscall returns only one value, only
403 .\" .Li syscall_rv[0]
405 .\" The tracing process can modify any of these with
407 .\" only some modifications are useful.
409 .\" On entry to a syscall,
411 .\" can be changed, and the syscall actually performed will correspond to
412 .\" the new number (it is the responsibility of the tracing process to fill
415 .\" appropriately for the new call, but there is no need to modify
417 .\" .Li syscall_nargs
419 .\" If the new syscall number is 0, no syscall is actually performed;
424 .\" are passed back to the traced process directly (and therefore should be
426 .\" If the syscall number is otherwise out of range, a dummy
427 .\" syscall which simply produces an
429 .\" error is effectively performed.
431 .\" On exit from a syscall, only
435 .\" can usefully be changed; they are set to the values returned by the
436 .\" syscall and will be passed back to the traced process by the normal
437 .\" syscall return mechanism.
439 Cause the traced process to dump core.
444 it is taken to be the pathname of the core file to be generated and the
446 argument should contain the length of the pathname.
447 The pathname may contain
449 patterns that are expanded as described in
455 the default core file path generation rules are followed.
458 Some requests can cause
462 as a non-error value; to disambiguate,
464 can be set to 0 before the call and checked afterwards.
465 The possible errors are:
468 Process is currently exec'ing and cannot be traced.
470 No process having the specified process ID exists.
474 A process attempted to use
480 was not a legal request on this machine architecture.
489 .\" .Li int Ns \&-aligned.
491 The signal number (in
497 was neither 0 nor a legal signal number.
504 was attempted on a process with no valid register set.
505 (This is normally true only of system processes.)
511 was attempted on a process that was already being traced.
513 A request attempted to manipulate a process that was being traced by
514 some process other than the one making the request.
516 A request (other than
518 specified a process that wasn't stopped.
523 A request (other than
525 attempted to manipulate a process that wasn't being traced at all.
527 An attempt was made to use
529 on a process in violation of the requirements listed under
538 On the SPARC, the PC is set to the provided PC value for
541 but the NPC is set willy-nilly to 4 greater than the PC value.
546 to modify the PC, passing
552 should be able to sidestep this.
556 .\" there is no easy way to tell whether the traced process stopped because
557 .\" it made a syscall or because a signal was sent at a moment that it just
558 .\" happened to have valid-looking garbage in its
559 .\" .Dq Li "struct mdproc" .