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, stepping by the tracing
63 or an event in the traced process.
64 The tracing process may choose to intercept the signal, using it to
65 observe process behavior (such as
67 or forward the signal to the process if appropriate.
71 is the mechanism by which all this happens.
73 A traced process may report additional signal stops corresponding to
74 events in the traced process.
75 These additional signal stops are reported as
80 The tracing process can use the
82 request to determine which events are associated with a
87 Note that multiple events may be associated with a single signal.
88 For example, events indicated by the
93 flags are also reported as a system call exit event
95 The signal stop for a new child process enabled via
100 All other additional signal stops use
102 .Sh DETACH AND TERMINATION
104 Normally, exiting tracing process should wait for all pending
105 debugging events and then detach from all alive traced processes
109 If tracing process exits without detaching, for instance due to abnormal
110 termination, the destiny of the traced children processes is determined
112 .Dv kern.kill_on_debugger_exit
115 If the control is set to the default value 1, such traced processes
117 If set to zero, kernel implicitly detaches traced processes.
118 Traced processes are un-stopped if needed, and then continue the execution
122 signals queued to the traced children, which could be either generated by
123 not yet consumed debug events, or sent by other means, the later should
127 Each traced process has a tracing event mask.
128 An event in the traced process only reports a
129 signal stop if the corresponding flag is set in the tracing event mask.
130 The current set of tracing event flags include:
131 .Bl -tag -width "Dv PTRACE_SYSCALL"
133 Report a stop for a successful invocation of
135 This event is indicated by the
140 .Vt "struct ptrace_lwpinfo" .
142 Report a stop on each system call entry.
143 This event is indicated by the
148 .Vt "struct ptrace_lwpinfo" .
150 Report a stop on each system call exit.
151 This event is indicated by the
156 .Vt "struct ptrace_lwpinfo" .
157 .It Dv PTRACE_SYSCALL
158 Report stops for both system call entry and exit.
160 This event flag controls tracing for new child processes of a traced process.
162 When this event flag is enabled,
163 new child processes will enable tracing and stop before executing their
165 The new child process will include the
170 .Vt "struct ptrace_lwpinfo" .
171 The traced process will report a stop that includes the
174 The process ID of the new child process will also be present in the
177 .Vt "struct ptrace_lwpinfo" .
178 If the new child process was created via
180 the traced process's stop will also include the
183 Note that new child processes will be attached with the default
185 they do not inherit the event mask of the traced process.
187 When this event flag is not enabled,
188 new child processes will execute without tracing enabled.
190 This event flag controls tracing of LWP
192 creation and destruction.
193 When this event is enabled,
194 new LWPs will stop and report an event with
196 set before executing their first instruction,
197 and exiting LWPs will stop and report an event with
199 set before completing their termination.
201 Note that new processes do not report an event for the creation of their
203 and exiting processes do not report an event for the termination of the
206 Report a stop event when a parent process resumes after a
209 When a thread in the traced process creates a new child process via
211 the stop that reports
215 occurs just after the child process is created,
216 but before the thread waits for the child process to stop sharing process
218 If a debugger is not tracing the new child process,
219 it must ensure that no breakpoints are enabled in the shared process
220 memory before detaching from the new child process.
221 This means that no breakpoints are enabled in the parent process either.
225 flag enables a new stop that indicates when the new child process stops
226 sharing the process memory of the parent process.
227 A debugger can reinsert breakpoints in the parent process and resume it
228 in response to this event.
229 This event is indicated by setting the
230 .Dv PL_FLAG_VFORK_DONE
234 The default tracing event mask when attaching to a process via
242 All other event flags are disabled.
247 argument specifies what operation is being performed; the meaning of
248 the rest of the arguments depends on the operation, but except for one
249 special case noted below, all
251 calls are made by the tracing process, and the
253 argument specifies the process ID of the traced process
254 or a corresponding thread ID.
259 .Bl -tag -width "Dv PT_GET_EVENT_MASK"
261 This request is the only one used by the traced process; it declares
262 that the process expects to be traced by its parent.
263 All the other arguments are ignored.
264 (If the parent process does not expect to trace the child, it will
265 probably be rather confused by the results; once the traced process
266 stops, it cannot be made to continue except via
268 When a process has used this request and calls
270 or any of the routines built on it
273 it will stop before executing the first instruction of the new image.
274 Also, any setuid or setgid bits on the executable being executed will
276 If the child was created by
282 flag specified, the debugging events are reported to the parent
286 .It Dv PT_READ_I , Dv PT_READ_D
287 These requests read a single
289 of data from the traced process's address space.
292 has allowed for machines with distinct address spaces for instruction
293 and data, which is why there are two requests: conceptually,
295 reads from the instruction space and
297 reads from the data space.
300 implementation, these two requests are completely identical.
303 argument specifies the address
304 (in the traced process's virtual address space)
305 at which the read is to be done.
306 This address does not have to meet any alignment constraints.
307 The value read is returned as the return value from
309 .It Dv PT_WRITE_I , Dv PT_WRITE_D
310 These requests parallel
314 except that they write rather than read.
317 argument supplies the value to be written.
319 This request allows reading and writing arbitrary amounts of data in
320 the traced process's address space.
323 argument specifies a pointer to a
324 .Vt "struct ptrace_io_desc" ,
325 which is defined as follows:
327 struct ptrace_io_desc {
328 int piod_op; /* I/O operation */
329 void *piod_offs; /* child offset */
330 void *piod_addr; /* parent offset */
331 size_t piod_len; /* request length */
335 * Operations in piod_op.
337 #define PIOD_READ_D 1 /* Read from D space */
338 #define PIOD_WRITE_D 2 /* Write to D space */
339 #define PIOD_READ_I 3 /* Read from I space */
340 #define PIOD_WRITE_I 4 /* Write to I space */
346 The actual number of bytes read or written is stored in
350 The traced process continues execution.
354 is an address specifying the place where execution is to be resumed
355 (a new value for the program counter),
357 .Po Vt caddr_t Pc Ns 1
358 to indicate that execution is to pick up where it left off.
362 provides a signal number to be delivered to the traced process as it
363 resumes execution, or 0 if no signal is to be sent.
365 The traced process is single stepped one instruction.
370 .Po Vt caddr_t Pc Ns 1 .
374 provides a signal number to be delivered to the traced process as it
375 resumes execution, or 0 if no signal is to be sent.
377 The traced process terminates, as if
381 given as the signal to be delivered.
383 This request allows a process to gain control of an otherwise
384 unrelated process and begin tracing it.
385 It does not need any cooperation from the to-be-traced process.
389 specifies the process ID of the to-be-traced process, and the other
390 two arguments are ignored.
391 This request requires that the target process must have the same real
392 UID as the tracing process, and that it must not be executing a setuid
393 or setgid executable.
394 (If the tracing process is running as root, these restrictions do not
396 The tracing process will see the newly-traced process stop and may
397 then control it as if it had been traced all along.
399 This request is like PT_CONTINUE, except that it does not allow
400 specifying an alternate place to continue execution, and after it
401 succeeds, the traced process is no longer traced and continues
404 This request reads the traced process's machine registers into the
413 This request is the converse of
415 it loads the traced process's machine registers from the
424 This request reads the traced process's floating-point registers into
434 This request is the converse of
436 it loads the traced process's floating-point registers from the
445 This request reads the traced process's debug registers into
455 This request is the converse of
457 it loads the traced process's debug registers from the
466 This request can be used to obtain information about the kernel thread,
467 also known as light-weight process, that caused the traced process to stop.
470 argument specifies a pointer to a
471 .Vt "struct ptrace_lwpinfo" ,
472 which is defined as follows:
474 struct ptrace_lwpinfo {
480 siginfo_t pl_siginfo;
481 char pl_tdname[MAXCOMLEN + 1];
483 u_int pl_syscall_code;
484 u_int pl_syscall_narg;
490 argument is to be set to the size of the structure known to the caller.
491 This allows the structure to grow without affecting older programs.
494 .Vt "struct ptrace_lwpinfo"
495 have the following meaning:
496 .Bl -tag -width indent -compact
500 Event that caused the stop.
501 Currently defined events are:
502 .Bl -tag -width "Dv PL_EVENT_SIGNAL" -compact
505 .It Dv PL_EVENT_SIGNAL
506 Thread stopped due to the pending signal
509 Flags that specify additional details about observed stop.
510 Currently defined flags are:
511 .Bl -tag -width indent -compact
513 The thread stopped due to system call entry, right after the kernel is entered.
514 The debugger may examine syscall arguments that are stored in memory and
515 registers according to the ABI of the current process, and modify them,
518 The thread is stopped immediately before syscall is returning to the usermode.
519 The debugger may examine system call return values in the ABI-defined registers
524 is set, this flag may be additionally specified to inform that the
525 program being executed by debuggee process has been changed by successful
526 execution of a system call from the
533 .Vt "struct ptrace_lwpinfo"
534 contains valid information.
535 .It Dv PL_FLAG_FORKED
536 Indicates that the process is returning from a call to
538 that created a new child process.
539 The process identifier of the new process is available in the
542 .Vt "struct ptrace_lwpinfo" .
544 The flag is set for first event reported from a new child which is
545 automatically attached when
549 This flag is set for the first event reported from a new LWP when
552 It is reported along with
554 .It Dv PL_FLAG_EXITED
555 This flag is set for the last event reported by an exiting LWP when
558 Note that this event is not reported when the last LWP in a process exits.
559 The termination of the last thread is reported via a normal process exit
561 .It Dv PL_FLAG_VFORKED
562 Indicates that the thread is returning from a call to
564 that created a new child process.
565 This flag is set in addition to
567 .It Dv PL_FLAG_VFORK_DONE
568 Indicates that the thread has resumed after a child process created via
570 has stopped sharing its address space with the traced process.
573 The current signal mask of the LWP
575 The current pending set of signals for the LWP.
576 Note that signals that are delivered to the process would not appear
577 on an LWP siglist until the thread is selected for delivery.
579 The siginfo that accompanies the signal pending.
587 The name of the thread.
589 The process identifier of the new child process.
596 .It Va pl_syscall_code
597 The ABI-specific identifier of the current system call.
598 Note that for indirect system calls this field reports the indirected
606 .It Va pl_syscall_narg
607 The number of arguments passed to the current system call not counting
608 the system call identifier.
609 Note that for indirect system calls this field reports the arguments
610 passed to the indirected system call.
619 This request returns the number of kernel threads associated with the
622 This request can be used to get the current thread list.
623 A pointer to an array of type
627 with the array size specified by
629 The return value from
631 is the count of array entries filled in.
633 This request will turn on single stepping of the specified process.
634 Stepping is automatically disabled when a single step trap is caught.
636 This request will turn off single stepping of the specified process.
638 This request will suspend the specified thread.
640 This request will resume the specified thread.
642 This request will set the
644 event flag to trace all future system call entries and continue the process.
649 arguments are used the same as for
652 This request will set the
654 event flag to trace all future system call exits and continue the process.
659 arguments are used the same as for
662 This request will set the
664 event flag to trace all future system call entries and exits and continue
670 arguments are used the same as for
672 .It Dv PT_GET_SC_ARGS
673 For the thread which is stopped in either
677 state, that is, on entry or exit to a syscall,
678 this request fetches the syscall arguments.
680 The arguments are copied out into the buffer pointed to by the
682 pointer, sequentially.
683 Each syscall argument is stored as the machine word.
684 Kernel copies out as many arguments as the syscall accepts,
688 .Vt struct ptrace_lwpinfo ,
689 but not more than the
691 bytes in total are copied.
693 Fetch the system call return values on exit from a syscall.
694 This request is only valid for threads stopped in a syscall
700 argument specifies a pointer to a
701 .Vt "struct ptrace_sc_ret" ,
702 which is defined as follows:
704 struct ptrace_sc_ret {
705 register_t sr_retval[2];
712 argument is set to the size of the structure.
714 If the system call completed successfully,
716 is set to zero and the return values of the system call are saved in
718 If the system call failed to execute,
720 field is set to a positive
723 If the system call completed in an unusual fashion,
725 is set to a negative value:
726 .Bl -tag -width Dv EJUSTRETURN -compact
728 System call will be restarted.
730 System call completed sucessfully but did not set a return value
737 .It Dv PT_FOLLOW_FORK
738 This request controls tracing for new child processes of a traced process.
743 is set in the traced process's event tracing mask.
748 is cleared from the traced process's event tracing mask.
750 This request controls tracing of LWP creation and destruction.
755 is set in the traced process's event tracing mask.
760 is cleared from the traced process's event tracing mask.
761 .It Dv PT_GET_EVENT_MASK
762 This request reads the traced process's event tracing mask into the
763 integer pointed to by
765 The size of the integer must be passed in
767 .It Dv PT_SET_EVENT_MASK
768 This request sets the traced process's event tracing mask from the
769 integer pointed to by
771 The size of the integer must be passed in
773 .It Dv PT_VM_TIMESTAMP
774 This request returns the generation number or timestamp of the memory map of
775 the traced process as the return value from
777 This provides a low-cost way for the tracing process to determine if the
778 VM map changed since the last time this request was made.
780 This request is used to iterate over the entries of the VM map of the traced
784 argument specifies a pointer to a
785 .Vt "struct ptrace_vm_entry" ,
786 which is defined as follows:
788 struct ptrace_vm_entry {
802 The first entry is returned by setting
805 Subsequent entries are returned by leaving
807 unmodified from the value returned by previous requests.
810 field can be used to detect changes to the VM map while iterating over the
812 The tracing process can then take appropriate action, such as restarting.
815 to a non-zero value on entry, the pathname of the backing object is returned
816 in the buffer pointed to by
818 provided the entry is backed by a vnode.
821 field is updated with the actual length of the pathname (including the
822 terminating null character).
825 field is the offset within the backing object at which the range starts.
826 The range is located in the VM space at
836 This request creates a coredump for the stopped program.
839 argument specifies a pointer to a
840 .Vt "struct ptrace_coredump" ,
841 which is defined as follows:
843 struct ptrace_coredump {
849 The fields of the structure are:
850 .Bl -tag -width pc_flags
852 File descriptor to write the dump to.
853 It must refer to a regular file, opened for writing.
856 The following flags are defined:
857 .Bl -tag -width PC_COMPRESS
859 Request compression of the dump.
861 Include non-dumpable entries into the dump.
864 flag of the process map entry, but device mappings are not dumped even with
869 Maximum size of the coredump.
870 Specify zero for no limit.
874 .Vt "struct ptrace_coredump"
878 The process must be stopped before dumping core.
879 A single thread in the target process is temporarily unsuspended
880 in kernel to write the dump.
883 call fails before a thread is unsuspended, there is no event to
886 If a thread was unsuspended, it will stop again before the
888 call returns, and the process must be waited upon using
890 to consume the new stop event.
891 Since it is hard to deduce whether a thread was unsuspended before
892 an error occurred, it is recommended to unconditionally perform
898 and silently accept zero result from it.
900 .Sh ARM MACHINE-SPECIFIC REQUESTS
901 .Bl -tag -width "Dv PT_SETVFPREGS"
905 machine state in the buffer pointed to by
914 machine state from the buffer pointed to by
921 .Sh x86 MACHINE-SPECIFIC REQUESTS
922 .Bl -tag -width "Dv PT_GETXSTATE_INFO"
924 Copy the XMM FPU state into the buffer pointed to by the
927 The buffer has the same layout as the 32-bit save buffer for the
931 This request is only valid for i386 programs, both on native 32-bit
932 systems and on amd64 kernels.
933 For 64-bit amd64 programs, the XMM state is reported as part of
934 the FPU state returned by the
942 Load the XMM FPU state for the thread from the buffer pointed to
945 The buffer has the same layout as the 32-bit load buffer for the
951 this request is only valid for i386 programs.
956 .It Dv PT_GETXSTATE_INFO
957 Report which XSAVE FPU extensions are supported by the CPU
958 and allowed in userspace programs.
961 argument must point to a variable of type
962 .Vt struct ptrace_xstate_info ,
963 which contains the information on the request return.
964 .Vt struct ptrace_xstate_info
965 is defined as follows:
967 struct ptrace_xstate_info {
974 field is a bitmask of the currently enabled extensions.
975 The meaning of the bits is defined in the Intel and AMD
976 processor documentation.
979 field reports the length of the XSAVE area for storing the hardware
980 state for currently enabled extensions in the format defined by the x86
986 argument value must be equal to the size of the
987 .Vt struct ptrace_xstate_info .
989 Return the content of the XSAVE area for the thread.
992 argument points to the buffer where the content is copied, and the
994 argument specifies the size of the buffer.
995 The kernel copies out as much content as allowed by the buffer size.
996 The buffer layout is specified by the layout of the save area for the
1000 Load the XSAVE state for the thread from the buffer specified by the
1003 The buffer size is passed in the
1006 The buffer must be at least as large as the
1010 to allow the complete x87 FPU and XMM state load.
1011 It must not be larger than the XSAVE state length, as reported by the
1014 .Vt struct ptrace_xstate_info
1016 .Dv PT_GETXSTATE_INFO
1018 Layout of the buffer is identical to the layout of the load area for the
1020 machine instruction.
1022 Return the value of the base used when doing segmented
1023 memory addressing using the %fs segment register.
1026 argument points to an
1028 variable where the base value is stored.
1032 argument is ignored.
1036 request, but returns the base for the %gs segment register.
1038 Set the base for the %fs segment register to the value pointed to
1045 variable containing the new base.
1049 argument is ignored.
1053 request, but sets the base for the %gs segment register.
1055 .Sh PowerPC MACHINE-SPECIFIC REQUESTS
1056 .Bl -tag -width "Dv PT_SETVRREGS"
1060 machine state in the buffer pointed to by
1065 argument is ignored.
1069 machine state from the buffer pointed to by
1074 argument is ignored.
1075 .It Dv PT_GETVSRREGS
1076 Return doubleword 1 of the thread's
1078 registers VSR0-VSR31 in the buffer pointed to by
1083 argument is ignored.
1084 .It Dv PT_SETVSRREGS
1085 Set doubleword 1 of the thread's
1087 registers VSR0-VSR31 from the buffer pointed to by
1092 argument is ignored.
1095 Additionally, other machine-specific requests can exist.
1097 Most requests return 0 on success and \-1 on error.
1098 Some requests can cause
1102 as a non-error value, among them are
1106 which return the value read from the process memory on success.
1109 can be set to 0 before the call and checked afterwards.
1113 implementation always sets
1115 to 0 before calling into the kernel, both for historic reasons and for
1116 consistency with other operating systems.
1117 It is recommended to assign zero to
1119 explicitly for forward compatibility.
1123 system call may fail if:
1126 .Bl -bullet -compact
1128 No process having the specified process ID exists.
1131 .Bl -bullet -compact
1133 A process attempted to use
1140 was not one of the legal requests.
1147 was neither 0 nor a legal signal number.
1156 was attempted on a process with no valid register set.
1157 (This is normally true only of system processes.)
1160 was given an invalid value for
1162 This can also be caused by changes to the VM map of the process.
1168 was less than or equal to zero, or larger than the
1170 structure known to the kernel.
1174 provided to the x86-specific
1175 .Dv PT_GETXSTATE_INFO
1176 request was not equal to the size of the
1177 .Vt struct ptrace_xstate_info .
1181 provided to the x86-specific
1183 request was less than the size of the x87 plus the XMM save area.
1187 provided to the x86-specific
1189 request was larger than returned in the
1192 .Vt struct ptrace_xstate_info
1194 .Dv PT_GETXSTATE_INFO
1197 The base value, provided to the amd64-specific requests
1201 pointed outside of the valid user address space.
1202 This error will not occur in 32-bit programs.
1205 .Bl -bullet -compact
1208 was attempted on a process that was already being traced.
1210 A request attempted to manipulate a process that was being traced by
1211 some process other than the one making the request.
1216 specified a process that was not stopped.
1219 .Bl -bullet -compact
1224 attempted to manipulate a process that was not being traced at all.
1226 An attempt was made to use
1228 on a process in violation of the requirements listed under
1233 .Bl -bullet -compact
1236 previously returned the last entry of the memory map.
1237 No more entries exist.
1240 .Bl -bullet -compact
1247 request attempted to access an invalid address, or a memory allocation failure
1248 occurred when accessing process memory.
1250 .It Bq Er ENAMETOOLONG
1251 .Bl -bullet -compact
1254 cannot return the pathname of the backing object because the buffer is not big
1257 holds the minimum buffer size required on return.
1265 .Xr i386_clr_watch 3 ,
1266 .Xr i386_set_watch 3
1270 function appeared in