2 .\" $NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
4 .\" This file is in the public domain.
10 .Nd process tracing and debugging
17 .Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
22 provides tracing and debugging facilities.
31 The tracing process must first attach to the traced process, and then
34 system calls to control the execution of the process, as well as access
35 process memory and register state.
36 For the duration of the tracing session, the traced process will be
38 with its parent process ID (and resulting behavior)
39 changed to the tracing process.
40 It is permissible for a tracing process to attach to more than one
41 other process at a time.
42 When the tracing process has completed its work, it must detach the
43 traced process; if a tracing process exits without first detaching all
44 processes it has attached, those processes will be killed.
46 Most of the time, the traced process runs normally, but when it
51 The tracing process is expected to notice this via
55 signal, examine the state of the stopped process, and cause it to
56 terminate or continue as appropriate.
57 The signal may be a normal process signal, generated as a result of
58 traced process behavior, or use of the
60 system call; alternatively, it may be generated by the tracing facility
61 as a result of attaching, system calls, or stepping by the tracing
63 The tracing process may choose to intercept the signal, using it to
64 observe process behavior (such as
66 or forward the signal to the process if appropriate.
70 is the mechanism by which all this happens.
74 argument specifies what operation is being performed; the meaning of
75 the rest of the arguments depends on the operation, but except for one
76 special case noted below, all
78 calls are made by the tracing process, and the
80 argument specifies the process ID of the traced process
81 or a corresponding thread ID.
88 This request is the only one used by the traced process; it declares
89 that the process expects to be traced by its parent.
90 All the other arguments are ignored.
91 (If the parent process does not expect to trace the child, it will
92 probably be rather confused by the results; once the traced process
93 stops, it cannot be made to continue except via
95 When a process has used this request and calls
97 or any of the routines built on it
100 it will stop before executing the first instruction of the new image.
101 Also, any setuid or setgid bits on the executable being executed will
103 If the child was created by
109 flag specified, the debugging events are reported to the parent
113 .It Dv PT_READ_I , Dv PT_READ_D
114 These requests read a single
116 of data from the traced process's address space.
119 has allowed for machines with distinct address spaces for instruction
120 and data, which is why there are two requests: conceptually,
122 reads from the instruction space and
124 reads from the data space.
127 implementation, these two requests are completely identical.
130 argument specifies the address
131 (in the traced process's virtual address space)
132 at which the read is to be done.
133 This address does not have to meet any alignment constraints.
134 The value read is returned as the return value from
136 .It Dv PT_WRITE_I , Dv PT_WRITE_D
137 These requests parallel
141 except that they write rather than read.
144 argument supplies the value to be written.
146 This request allows reading and writing arbitrary amounts of data in
147 the traced process's address space.
150 argument specifies a pointer to a
151 .Vt "struct ptrace_io_desc" ,
152 which is defined as follows:
154 struct ptrace_io_desc {
155 int piod_op; /* I/O operation */
156 void *piod_offs; /* child offset */
157 void *piod_addr; /* parent offset */
158 size_t piod_len; /* request length */
162 * Operations in piod_op.
164 #define PIOD_READ_D 1 /* Read from D space */
165 #define PIOD_WRITE_D 2 /* Write to D space */
166 #define PIOD_READ_I 3 /* Read from I space */
167 #define PIOD_WRITE_I 4 /* Write to I space */
173 The actual number of bytes read or written is stored in
177 The traced process continues execution.
181 is an address specifying the place where execution is to be resumed
182 (a new value for the program counter),
184 .Po Vt caddr_t Pc Ns 1
185 to indicate that execution is to pick up where it left off.
189 provides a signal number to be delivered to the traced process as it
190 resumes execution, or 0 if no signal is to be sent.
192 The traced process is single stepped one instruction.
197 .Po Vt caddr_t Pc Ns 1 .
201 provides a signal number to be delivered to the traced process as it
202 resumes execution, or 0 if no signal is to be sent.
204 The traced process terminates, as if
208 given as the signal to be delivered.
210 This request allows a process to gain control of an otherwise
211 unrelated process and begin tracing it.
212 It does not need any cooperation from the to-be-traced process.
216 specifies the process ID of the to-be-traced process, and the other
217 two arguments are ignored.
218 This request requires that the target process must have the same real
219 UID as the tracing process, and that it must not be executing a setuid
220 or setgid executable.
221 (If the tracing process is running as root, these restrictions do not
223 The tracing process will see the newly-traced process stop and may
224 then control it as if it had been traced all along.
226 This request is like PT_CONTINUE, except that it does not allow
227 specifying an alternate place to continue execution, and after it
228 succeeds, the traced process is no longer traced and continues
231 This request reads the traced process's machine registers into the
240 This request is the converse of
242 it loads the traced process's machine registers from the
251 This request reads the traced process's floating-point registers into
261 This request is the converse of
263 it loads the traced process's floating-point registers from the
272 This request reads the traced process's debug registers into
282 This request is the converse of
284 it loads the traced process's debug registers from the
293 This request can be used to obtain information about the kernel thread,
294 also known as light-weight process, that caused the traced process to stop.
297 argument specifies a pointer to a
298 .Vt "struct ptrace_lwpinfo" ,
299 which is defined as follows:
301 struct ptrace_lwpinfo {
307 siginfo_t pl_siginfo;
308 char pl_tdname[MAXCOMLEN + 1];
310 u_int pl_syscall_code;
311 u_int pl_syscall_narg;
317 argument is to be set to the size of the structure known to the caller.
318 This allows the structure to grow without affecting older programs.
321 .Vt "struct ptrace_lwpinfo"
322 have the following meaning:
323 .Bl -tag -width indent -compact
327 Event that caused the stop.
328 Currently defined events are
329 .Bl -tag -width indent -compact
333 Thread stopped due to the pending signal
336 Flags that specify additional details about observed stop.
337 Currently defined flags are:
338 .Bl -tag -width indent -compact
340 The thread stopped due to system call entry, right after the kernel is entered.
341 The debugger may examine syscall arguments that are stored in memory and
342 registers according to the ABI of the current process, and modify them,
345 The thread is stopped immediately before syscall is returning to the usermode.
346 The debugger may examine system call return values in the ABI-defined registers
351 is set, this flag may be additionally specified to inform that the
352 program being executed by debuggee process has been changed by successful
353 execution of a system call from the
360 .Vt "struct ptrace_lwpinfo"
361 contains valid information.
363 Indicates that the process is returning from a call to
365 that created a new child process.
366 The process identifier of the new process is available in the
369 .Vt "struct ptrace_lwpinfo" .
371 The flag is set for first event reported from a new child, which is
372 automatically attached due to
376 This flag is set for the first event reported from a new LWP when LWP
377 events are enabled via
379 It is reported along with
381 and is always reported if LWP events are enabled.
383 This flag is set for the last event reported by an exiting LWP when
384 LWP events are enabled via
386 Note that this event is not reported when the last LWP in a process exits.
387 The termination of the last thread is reported via a normal process exit
391 The current signal mask of the LWP
393 The current pending set of signals for the LWP.
394 Note that signals that are delivered to the process would not appear
395 on an LWP siglist until the thread is selected for delivery.
397 The siginfo that accompanies the signal pending.
405 The name of the thread.
407 The process identifier of the new child process.
415 The ABI-specific identifier of the current system call.
416 Note that for indirect system calls this field reports the indirected
425 The number of arguments passed to the current system call not counting
426 the system call identifier.
427 Note that for indirect system calls this field reports the arguments
428 passed to the indirected system call.
437 This request returns the number of kernel threads associated with the
440 This request can be used to get the current thread list.
441 A pointer to an array of type
445 with the array size specified by
447 The return value from
449 is the count of array entries filled in.
451 This request will turn on single stepping of the specified process.
453 This request will turn off single stepping of the specified process.
455 This request will suspend the specified thread.
457 This request will resume the specified thread.
459 This request will trace the specified process on each system call entry.
461 This request will trace the specified process on each system call exit.
463 This request will trace the specified process
464 on each system call entry and exit.
466 This request controls tracing for new child processes of a traced process.
470 then new child processes will enable tracing and stop before executing their
474 is zero, then new child processes will execute without tracing enabled.
475 By default, tracing is not enabled for new child processes.
476 Child processes do not inherit this property.
477 The traced process will set the
479 flag upon exit from a system call that creates a new process.
481 This request controls tracing of LWP creation and destruction.
485 then LWPs will stop to report creation and destruction events.
489 then LWP creation and destruction events will not be reported.
490 By default, tracing is not enabled for LWP events.
491 Child processes do not inherit this property.
492 New LWPs will stop to report an event with
494 set before executing their first instruction.
495 Exiting LWPs will stop to report an event with
497 set before completing their termination.
499 Note that new processes do not report an event for the creation of their
501 and exiting processes do not report an event for the termination of the
504 This request returns the generation number or timestamp of the memory map of
505 the traced process as the return value from
507 This provides a low-cost way for the tracing process to determine if the
508 VM map changed since the last time this request was made.
510 This request is used to iterate over the entries of the VM map of the traced
514 argument specifies a pointer to a
515 .Vt "struct ptrace_vm_entry" ,
516 which is defined as follows:
518 struct ptrace_vm_entry {
532 The first entry is returned by setting
535 Subsequent entries are returned by leaving
537 unmodified from the value returned by previous requests.
540 field can be used to detect changes to the VM map while iterating over the
542 The tracing process can then take appropriate action, such as restarting.
545 to a non-zero value on entry, the pathname of the backing object is returned
546 in the buffer pointed to by
548 provided the entry is backed by a vnode.
551 field is updated with the actual length of the pathname (including the
552 terminating null character).
555 field is the offset within the backing object at which the range starts.
556 The range is located in the VM space at
566 .Sh x86 MACHINE-SPECIFIC REQUESTS
567 .Bl -tag -width "Dv PT_GETXSTATE_INFO"
569 Copy the XMM FPU state into the buffer pointed to by the
572 The buffer has the same layout as the 32-bit save buffer for the
576 This request is only valid for i386 programs, both on native 32-bit
577 systems and on amd64 kernels.
578 For 64-bit amd64 programs, the XMM state is reported as part of
579 the FPU state returned by the
587 Load the XMM FPU state for the thread from the buffer pointed to
590 The buffer has the same layout as the 32-bit load buffer for the
596 this request is only valid for i386 programs.
601 .It Dv PT_GETXSTATE_INFO
602 Report which XSAVE FPU extensions are supported by the CPU
603 and allowed in userspace programs.
606 argument must point to a variable of type
607 .Vt struct ptrace_xstate_info ,
608 which contains the information on the request return.
609 .Vt struct ptrace_xstate_info
610 is defined as follows:
612 struct ptrace_xstate_info {
619 field is a bitmask of the currently enabled extensions.
620 The meaning of the bits is defined in the Intel and AMD
621 processor documentation.
624 field reports the length of the XSAVE area for storing the hardware
625 state for currently enabled extensions in the format defined by the x86
631 argument value must be equal to the size of the
632 .Vt struct ptrace_xstate_info .
634 Return the content of the XSAVE area for the thread.
637 argument points to the buffer where the content is copied, and the
639 argument specifies the size of the buffer.
640 The kernel copies out as much content as allowed by the buffer size.
641 The buffer layout is specified by the layout of the save area for the
645 Load the XSAVE state for the thread from the buffer specified by the
648 The buffer size is passed in the
651 The buffer must be at least as large as the
655 to allow the complete x87 FPU and XMM state load.
656 It must not be larger than the XSAVE state length, as reported by the
659 .Vt struct ptrace_xstate_info
661 .Dv PT_GETXSTATE_INFO
663 Layout of the buffer is identical to the layout of the load area for the
667 Return the value of the base used when doing segmented
668 memory addressing using the %fs segment register.
671 argument points to an
673 variable where the base value is stored.
681 request, but returns the base for the %gs segment register.
683 Set the base for the %fs segment register to the value pointed to
690 variable containing the new base.
698 request, but sets the base for the %gs segment register.
700 .Sh PowerPC MACHINE-SPECIFIC REQUESTS
701 .Bl -tag -width "Dv PT_SETVRREGS"
705 machine state in the buffer pointed to by
714 machine state from the buffer pointed to by
722 Additionally, other machine-specific requests can exist.
724 Some requests can cause
728 as a non-error value; to disambiguate,
730 can be set to 0 before the call and checked afterwards.
734 system call may fail if:
739 No process having the specified process ID exists.
744 A process attempted to use
751 was not one of the legal requests.
758 was neither 0 nor a legal signal number.
767 was attempted on a process with no valid register set.
768 (This is normally true only of system processes.)
771 was given an invalid value for
773 This can also be caused by changes to the VM map of the process.
779 was less than or equal to zero, or larger than the
781 structure known to the kernel.
785 provided to the x86-specific
786 .Dv PT_GETXSTATE_INFO
787 request was not equal to the size of the
788 .Vt struct ptrace_xstate_info .
792 provided to the x86-specific
794 request was less than the size of the x87 plus the XMM save area.
798 provided to the x86-specific
800 request was larger than returned in the
803 .Vt struct ptrace_xstate_info
805 .Dv PT_GETXSTATE_INFO
808 The base value, provided to the amd64-specific requests
812 pointed outside of the valid user address space.
813 This error will not occur in 32-bit programs.
819 was attempted on a process that was already being traced.
821 A request attempted to manipulate a process that was being traced by
822 some process other than the one making the request.
827 specified a process that was not stopped.
835 attempted to manipulate a process that was not being traced at all.
837 An attempt was made to use
839 on a process in violation of the requirements listed under
847 previously returned the last entry of the memory map.
848 No more entries exist.
850 .It Bq Er ENAMETOOLONG
854 cannot return the pathname of the backing object because the buffer is not big
857 holds the minimum buffer size required on return.
865 .Xr i386_clr_watch 3 ,