1 .\" $NetBSD: ptrace.2,v 1.37 2015/07/02 03:50:21 christos 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.
153 If a negative value is supplied, that is the negative of the LWP
154 ID of the thread to be resumed, and only that thread executes.
156 The traced process terminates, as if
160 given as the signal to be delivered.
162 This request allows a process to gain control of an otherwise unrelated
163 process and begin tracing it.
164 It does not need any cooperation from the to-be-traced process.
167 specifies the process ID of the to-be-traced process, and the other two
168 arguments are ignored.
169 This request requires that the target process
170 must have the same real UID as the tracing process, and that it must
171 not be executing a setuid or setgid executable.
172 (If the tracing process is running as root,
173 these restrictions do not apply.)
174 The tracing process will see the newly-traced process stop and may then
175 control it as if it had been traced all along.
177 Three other restrictions apply to all tracing processes, even those
179 First, no process may trace a system process.
180 Second, no process may trace the process running
182 Third, if a process has its root directory set with
184 it may not trace another process unless that process's root directory
185 is at or below the tracing process's root.
187 This request is like PT_CONTINUE, except that after it
188 succeeds, the traced process is no longer traced and continues
191 This request is a more general interface that can be used instead of
197 The I/O request is encoded in a
198 .Dq Li "struct ptrace_io_desc"
200 .Bd -literal -offset indent
201 struct ptrace_io_desc {
211 is the offset within the traced process where the I/O operation should
214 is the buffer in the tracing process, and
216 is the length of the I/O request.
219 field specifies which type of I/O operation to perform.
222 .Bl -tag -width 18n -offset indent -compact
227 .It Dv PIOD_READ_AUXV
230 See the description of
232 for the difference between I and D spaces.
235 operation can be used to read from the ELF auxiliary vector.
236 A pointer to the I/O descriptor is passed in the
242 field in the I/O descriptor will be updated with the actual number of
244 If the requested I/O could not be successfully performed,
251 Makes the process specified in the
253 pid generate a core dump.
256 argument should contain the name of the core file to be generated
259 argument should contain the length of the core filename.
262 call currently does not stop the child process so it can generate
265 Returns information about a thread from the list of threads for the
266 process specified in the
271 argument should contain a
272 .Dq Li "struct ptrace_lwpinfo"
274 .Bd -literal -offset indent
275 struct ptrace_lwpinfo {
283 contains a thread LWP ID.
284 Information is returned for the thread following the one with the
285 specified ID in the process thread list, or for the first thread
291 contains the LWP ID of the thread that was found, or 0 if there is
292 no thread after the one whose LWP ID was supplied in the call.
294 contains the event that stopped the thread.
297 .Bl -tag -width 30n -offset indent -compact
299 .It Dv PL_EVENT_SIGNAL
304 argument should contain
305 .Dq Li "sizeof(struct ptrace_lwpinfo)" .
307 Stops a process before and after executing each system call.
309 Intercept and ignore a system call before it has been executed, for use with
313 Additionally, the following requests exist but are
314 not available on all machine architectures.
317 lists which requests exist on a given machine.
320 Execution continues as in request PT_CONTINUE; however
321 as soon as possible after execution of at least one
322 instruction, execution stops again.
325 argument is greater than 0, it contains the LWP ID of the thread to be
326 stepped, and any other threads are continued.
329 argument is less than zero, it contains the negative of the LWP ID of
330 the thread to be stepped, and only that thread executes.
332 This request reads the traced process' machine registers into the
340 argument contains the LWP ID of the thread whose registers are to
342 If zero is supplied, the first thread of the process is read.
344 This request is the converse of
346 it loads the traced process' machine registers from the
354 argument contains the LWP ID of the thread whose registers are to
356 If zero is supplied, the first thread of the process is written.
358 This request reads the traced process' floating-point registers into
360 .Dq Li "struct fpreg"
367 argument contains the LWP ID of the thread whose registers are to
369 If zero is supplied, the first thread of the process is read.
371 This request is the converse of
373 it loads the traced process' floating-point registers from the
374 .Dq Li "struct fpreg"
381 argument contains the LWP ID of the thread whose registers are to
383 If zero is supplied, the first thread of the process is written.
384 .\" .It Dv PT_SYSCALL
385 .\" This request is like
387 .\" except that the process will stop next time it executes any system
389 .\" Information about the system call can be examined with
391 .\" and potentially modified with
394 .\" .Li u_kproc.kp_proc.p_md
395 .\" element of the user structure (see below).
396 .\" If the process is continued
399 .\" request, it will stop again on exit from the syscall, at which point
400 .\" the return values can be examined and potentially changed.
402 .\" .Li u_kproc.kp_proc.p_md
403 .\" element is of type
404 .\" .Dq Li "struct mdproc" ,
405 .\" which should be declared by including
406 .\" .In sys/param.h ,
409 .\" .In machine/proc.h ,
410 .\" and contains the following fields (among others):
411 .\" .Bl -item -compact -offset indent
415 .\" .Li syscall_nargs
417 .\" .Li syscall_args[8]
421 .\" .Li syscall_rv[2]
423 .\" When a process stops on entry to a syscall,
425 .\" holds the number of the syscall,
426 .\" .Li syscall_nargs
427 .\" holds the number of arguments it expects, and
429 .\" holds the arguments themselves.
431 .\" .Li syscall_nargs
434 .\" are guaranteed to be useful.)
435 .\" When a process stops on exit from a syscall,
442 .\" holds the error number
447 .\" or 0 if no error occurred, and
449 .\" holds the return values.
450 .\" (If the syscall returns only one value, only
451 .\" .Li syscall_rv[0]
453 .\" The tracing process can modify any of these with
455 .\" only some modifications are useful.
457 .\" On entry to a syscall,
459 .\" can be changed, and the syscall actually performed will correspond to
460 .\" the new number (it is the responsibility of the tracing process to fill
463 .\" appropriately for the new call, but there is no need to modify
465 .\" .Li syscall_nargs
467 .\" If the new syscall number is 0, no syscall is actually performed;
472 .\" are passed back to the traced process directly (and therefore should be
474 .\" If the syscall number is otherwise out of range, a dummy
475 .\" syscall which simply produces an
477 .\" error is effectively performed.
479 .\" On exit from a syscall, only
483 .\" can usefully be changed; they are set to the values returned by the
484 .\" syscall and will be passed back to the traced process by the normal
485 .\" syscall return mechanism.
487 Cause the traced process to dump core.
492 it is taken to be the pathname of the core file to be generated and the
494 argument should contain the length of the pathname.
495 The pathname may contain
497 patterns that are expanded as described in
503 the default core file path generation rules are followed.
506 Some requests can cause
510 as a non-error value; to disambiguate,
512 can be set to 0 before the call and checked afterwards.
513 The possible errors are:
514 .Bl -tag -width "[EINVAL]"
516 Process is currently exec'ing and cannot be traced.
521 was attempted on a process that was already being traced.
523 A request attempted to manipulate a process that was being traced by
524 some process other than the one making the request.
526 A request (other than
528 specified a process that wasn't stopped.
533 A process attempted to use
539 was not a legal request on this machine architecture.
548 .\" .Li int Ns \&-aligned.
550 The signal number (in
556 was neither 0 nor a legal signal number.
563 was attempted on a process with no valid register set.
564 (This is normally true only of system processes.)
569 A request (other than
571 attempted to manipulate a process that wasn't being traced at all.
573 An attempt was made to use
575 on a process in violation of the requirements listed under
580 No process having the specified process ID exists.
586 On the SPARC, the PC is set to the provided PC value for
589 but the NPC is set willy-nilly to 4 greater than the PC value.
594 to modify the PC, passing
600 should be able to sidestep this.
604 .\" there is no easy way to tell whether the traced process stopped because
605 .\" it made a syscall or because a signal was sent at a moment that it just
606 .\" happened to have valid-looking garbage in its
607 .\" .Dq Li "struct mdproc" .