1 .\" $NetBSD: ptrace.2,v 1.35 2011/08/31 23:04:33 jmcneill 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
229 See the description of
231 for the difference between I and D spaces.
232 A pointer to the I/O descriptor is passed in the
238 field in the I/O descriptor will be updated with the actual number of
240 If the requested I/O could not be successfully performed,
247 Makes the process specified in the
249 pid generate a core dump.
252 argument should contain the name of the core file to be generated
255 argument should contain the length of the core filename.
258 call currently does not stop the child process so it can generate
261 Returns information about a thread from the list of threads for the
262 process specified in the
267 argument should contain a
268 .Dq Li "struct ptrace_lwpinfo"
270 .Bd -literal -offset indent
271 struct ptrace_lwpinfo {
279 contains a thread LWP ID.
280 Information is returned for the thread following the one with the
281 specified ID in the process thread list, or for the first thread
287 contains the LWP ID of the thread that was found, or 0 if there is
288 no thread after the one whose LWP ID was supplied in the call.
290 contains the event that stopped the thread.
293 .Bl -tag -width 30n -offset indent -compact
295 .It Dv PL_EVENT_SIGNAL
300 argument should contain
301 .Dq Li "sizeof(struct ptrace_lwpinfo)" .
303 Stops a process before and after executing each system call.
305 Intercept and ignore a system call before it has been executed, for use with
309 Additionally, the following requests exist but are
310 not available on all machine architectures.
313 lists which requests exist on a given machine.
316 Execution continues as in request PT_CONTINUE; however
317 as soon as possible after execution of at least one
318 instruction, execution stops again.
321 argument is greater than 0, it contains the LWP ID of the thread to be
322 stepped, and any other threads are continued.
325 argument is less than zero, it contains the negative of the LWP ID of
326 the thread to be stepped, and only that thread executes.
328 This request reads the traced process' machine registers into the
336 argument contains the LWP ID of the thread whose registers are to
338 If zero is supplied, the first thread of the process is read.
340 This request is the converse of
342 it loads the traced process' machine registers from the
350 argument contains the LWP ID of the thread whose registers are to
352 If zero is supplied, the first thread of the process is written.
354 This request reads the traced process' floating-point registers into
356 .Dq Li "struct fpreg"
363 argument contains the LWP ID of the thread whose registers are to
365 If zero is supplied, the first thread of the process is read.
367 This request is the converse of
369 it loads the traced process' floating-point registers from the
370 .Dq Li "struct fpreg"
377 argument contains the LWP ID of the thread whose registers are to
379 If zero is supplied, the first thread of the process is written.
380 .\" .It Dv PT_SYSCALL
381 .\" This request is like
383 .\" except that the process will stop next time it executes any system
385 .\" Information about the system call can be examined with
387 .\" and potentially modified with
390 .\" .Li u_kproc.kp_proc.p_md
391 .\" element of the user structure (see below).
392 .\" If the process is continued
395 .\" request, it will stop again on exit from the syscall, at which point
396 .\" the return values can be examined and potentially changed.
398 .\" .Li u_kproc.kp_proc.p_md
399 .\" element is of type
400 .\" .Dq Li "struct mdproc" ,
401 .\" which should be declared by including
402 .\" .In sys/param.h ,
405 .\" .In machine/proc.h ,
406 .\" and contains the following fields (among others):
407 .\" .Bl -item -compact -offset indent
411 .\" .Li syscall_nargs
413 .\" .Li syscall_args[8]
417 .\" .Li syscall_rv[2]
419 .\" When a process stops on entry to a syscall,
421 .\" holds the number of the syscall,
422 .\" .Li syscall_nargs
423 .\" holds the number of arguments it expects, and
425 .\" holds the arguments themselves.
427 .\" .Li syscall_nargs
430 .\" are guaranteed to be useful.)
431 .\" When a process stops on exit from a syscall,
438 .\" holds the error number
443 .\" or 0 if no error occurred, and
445 .\" holds the return values.
446 .\" (If the syscall returns only one value, only
447 .\" .Li syscall_rv[0]
449 .\" The tracing process can modify any of these with
451 .\" only some modifications are useful.
453 .\" On entry to a syscall,
455 .\" can be changed, and the syscall actually performed will correspond to
456 .\" the new number (it is the responsibility of the tracing process to fill
459 .\" appropriately for the new call, but there is no need to modify
461 .\" .Li syscall_nargs
463 .\" If the new syscall number is 0, no syscall is actually performed;
468 .\" are passed back to the traced process directly (and therefore should be
470 .\" If the syscall number is otherwise out of range, a dummy
471 .\" syscall which simply produces an
473 .\" error is effectively performed.
475 .\" On exit from a syscall, only
479 .\" can usefully be changed; they are set to the values returned by the
480 .\" syscall and will be passed back to the traced process by the normal
481 .\" syscall return mechanism.
483 Cause the traced process to dump core.
488 it is taken to be the pathname of the core file to be generated and the
490 argument should contain the length of the pathname.
491 The pathname may contain
493 patterns that are expanded as described in
499 the default core file path generation rules are followed.
502 Some requests can cause
506 as a non-error value; to disambiguate,
508 can be set to 0 before the call and checked afterwards.
509 The possible errors are:
510 .Bl -tag -width "[EINVAL]"
512 Process is currently exec'ing and cannot be traced.
517 was attempted on a process that was already being traced.
519 A request attempted to manipulate a process that was being traced by
520 some process other than the one making the request.
522 A request (other than
524 specified a process that wasn't stopped.
529 A process attempted to use
535 was not a legal request on this machine architecture.
544 .\" .Li int Ns \&-aligned.
546 The signal number (in
552 was neither 0 nor a legal signal number.
559 was attempted on a process with no valid register set.
560 (This is normally true only of system processes.)
565 A request (other than
567 attempted to manipulate a process that wasn't being traced at all.
569 An attempt was made to use
571 on a process in violation of the requirements listed under
576 No process having the specified process ID exists.
582 On the SPARC, the PC is set to the provided PC value for
585 but the NPC is set willy-nilly to 4 greater than the PC value.
590 to modify the PC, passing
596 should be able to sidestep this.
600 .\" there is no easy way to tell whether the traced process stopped because
601 .\" it made a syscall or because a signal was sent at a moment that it just
602 .\" happened to have valid-looking garbage in its
603 .\" .Dq Li "struct mdproc" .