xref: /freebsd-13.1/lib/libc/sys/ptrace.2 (revision 2faefc48)
1.\" $FreeBSD$
2.\"	$NetBSD: ptrace.2,v 1.2 1995/02/27 12:35:37 cgd Exp $
3.\"
4.\" This file is in the public domain.
5.Dd January 22, 2022
6.Dt PTRACE 2
7.Os
8.Sh NAME
9.Nm ptrace
10.Nd process tracing and debugging
11.Sh LIBRARY
12.Lb libc
13.Sh SYNOPSIS
14.In sys/types.h
15.In sys/ptrace.h
16.Ft int
17.Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
18.Sh DESCRIPTION
19The
20.Fn ptrace
21system call
22provides tracing and debugging facilities.
23It allows one process
24(the
25.Em tracing
26process)
27to control another
28(the
29.Em traced
30process).
31The tracing process must first attach to the traced process, and then
32issue a series of
33.Fn ptrace
34system calls to control the execution of the process, as well as access
35process memory and register state.
36For the duration of the tracing session, the traced process will be
37.Dq re-parented ,
38with its parent process ID (and resulting behavior)
39changed to the tracing process.
40It is permissible for a tracing process to attach to more than one
41other process at a time.
42When the tracing process has completed its work, it must detach the
43traced process; if a tracing process exits without first detaching all
44processes it has attached, those processes will be killed.
45.Pp
46Most of the time, the traced process runs normally, but when it
47receives a signal
48(see
49.Xr sigaction 2 ) ,
50it stops.
51The tracing process is expected to notice this via
52.Xr wait 2
53or the delivery of a
54.Dv SIGCHLD
55signal, examine the state of the stopped process, and cause it to
56terminate or continue as appropriate.
57The signal may be a normal process signal, generated as a result of
58traced process behavior, or use of the
59.Xr kill 2
60system call; alternatively, it may be generated by the tracing facility
61as a result of attaching, stepping by the tracing
62process,
63or an event in the traced process.
64The tracing process may choose to intercept the signal, using it to
65observe process behavior (such as
66.Dv SIGTRAP ) ,
67or forward the signal to the process if appropriate.
68The
69.Fn ptrace
70system call
71is the mechanism by which all this happens.
72.Pp
73A traced process may report additional signal stops corresponding to
74events in the traced process.
75These additional signal stops are reported as
76.Dv SIGTRAP
77or
78.Dv SIGSTOP
79signals.
80The tracing process can use the
81.Dv PT_LWPINFO
82request to determine which events are associated with a
83.Dv SIGTRAP
84or
85.Dv SIGSTOP
86signal.
87Note that multiple events may be associated with a single signal.
88For example, events indicated by the
89.Dv PL_FLAG_BORN ,
90.Dv PL_FLAG_FORKED ,
91and
92.Dv PL_FLAG_EXEC
93flags are also reported as a system call exit event
94.Pq Dv PL_FLAG_SCX .
95The signal stop for a new child process enabled via
96.Dv PTRACE_FORK
97will report a
98.Dv SIGSTOP
99signal.
100All other additional signal stops use
101.Dv SIGTRAP .
102.Sh DETACH AND TERMINATION
103.Pp
104Normally, exiting tracing process should wait for all pending
105debugging events and then detach from all alive traced processes
106before exiting using
107.Dv PT_DETACH
108request.
109If tracing process exits without detaching, for instance due to abnormal
110termination, the destiny of the traced children processes is determined
111by the
112.Dv kern.kill_on_debugger_exit
113sysctl control.
114.Pp
115If the control is set to the default value 1, such traced processes
116are terminated.
117If set to zero, kernel implicitly detaches traced processes.
118Traced processes are un-stopped if needed, and then continue the execution
119without tracing.
120Kernel drops any
121.Dv SIGTRAP
122signals queued to the traced children, which could be either generated by
123not yet consumed debug events, or sent by other means, the later should
124not be done anyway.
125.Sh DISABLING PTRACE
126The
127.Nm
128subsystem provides rich facilities to manipulate other processes state.
129Sometimes it may be desirable to disallow it either completely, or limit
130its scope.
131The following controls are provided for this:
132.Bl -tag -width security.bsd.unprivileged_proc_debug
133.It Dv security.bsd.allow_ptrace
134Setting this sysctl to zero value makes
135.Xr ptrace 2
136return
137.Er ENOSYS
138always as if the syscall is not implemented by the kernel.
139.It Dv security.bsd.unprivileged_proc_debug
140Setting this sysctl to zero disallows use of
141.Fn ptrace
142by unprivileged processes.
143.It Dv security.bsd.see_other_uids
144Setting this sysctl to zero value disallows
145.Fn ptrace
146requests from targeting processes with the real user identifier different
147from the real user identifier of the caller.
148The requests return
149.Er ESRCH
150if policy is not met.
151.It Dv security.bsd.see_other_gids
152Setting this sysctl to zero value disallows
153.Fn ptrace
154requests from process belonging to a group that is not also one of
155the group of the target process.
156The requests return
157.Er ESRCH
158if policy is not met.
159.It Dv securelevel and init
160The
161.Xr init 1
162process can only be traced with
163.Nm
164if securelevel is zero.
165.It Dv procctl(2) PROC_TRACE_CTL
166Process can deny attempts to trace itself with
167.Xr procctl 2
168.Dv PROC_TRACE_CTL
169request.
170In this case requests return
171.Xr EPERM
172error.
173.El
174.Sh TRACING EVENTS
175.Pp
176Each traced process has a tracing event mask.
177An event in the traced process only reports a
178signal stop if the corresponding flag is set in the tracing event mask.
179The current set of tracing event flags include:
180.Bl -tag -width "Dv PTRACE_SYSCALL"
181.It Dv PTRACE_EXEC
182Report a stop for a successful invocation of
183.Xr execve 2 .
184This event is indicated by the
185.Dv PL_FLAG_EXEC
186flag in the
187.Va pl_flags
188member of
189.Vt "struct ptrace_lwpinfo" .
190.It Dv PTRACE_SCE
191Report a stop on each system call entry.
192This event is indicated by the
193.Dv PL_FLAG_SCE
194flag in the
195.Va pl_flags
196member of
197.Vt "struct ptrace_lwpinfo" .
198.It Dv PTRACE_SCX
199Report a stop on each system call exit.
200This event is indicated by the
201.Dv PL_FLAG_SCX
202flag in the
203.Va pl_flags
204member of
205.Vt "struct ptrace_lwpinfo" .
206.It Dv PTRACE_SYSCALL
207Report stops for both system call entry and exit.
208.It Dv PTRACE_FORK
209This event flag controls tracing for new child processes of a traced process.
210.Pp
211When this event flag is enabled,
212new child processes will enable tracing and stop before executing their
213first instruction.
214The new child process will include the
215.Dv PL_FLAG_CHILD
216flag in the
217.Va pl_flags
218member of
219.Vt "struct ptrace_lwpinfo" .
220The traced process will report a stop that includes the
221.Dv PL_FLAG_FORKED
222flag.
223The process ID of the new child process will also be present in the
224.Va pl_child_pid
225member of
226.Vt "struct ptrace_lwpinfo" .
227If the new child process was created via
228.Xr vfork 2 ,
229the traced process's stop will also include the
230.Dv PL_FLAG_VFORKED
231flag.
232Note that new child processes will be attached with the default
233tracing event mask;
234they do not inherit the event mask of the traced process.
235.Pp
236When this event flag is not enabled,
237new child processes will execute without tracing enabled.
238.It Dv PTRACE_LWP
239This event flag controls tracing of LWP
240.Pq kernel thread
241creation and destruction.
242When this event is enabled,
243new LWPs will stop and report an event with
244.Dv PL_FLAG_BORN
245set before executing their first instruction,
246and exiting LWPs will stop and report an event with
247.Dv PL_FLAG_EXITED
248set before completing their termination.
249.Pp
250Note that new processes do not report an event for the creation of their
251initial thread,
252and exiting processes do not report an event for the termination of the
253last thread.
254.It Dv PTRACE_VFORK
255Report a stop event when a parent process resumes after a
256.Xr vfork 2 .
257.Pp
258When a thread in the traced process creates a new child process via
259.Xr vfork 2 ,
260the stop that reports
261.Dv PL_FLAG_FORKED
262and
263.Dv PL_FLAG_SCX
264occurs just after the child process is created,
265but before the thread waits for the child process to stop sharing process
266memory.
267If a debugger is not tracing the new child process,
268it must ensure that no breakpoints are enabled in the shared process
269memory before detaching from the new child process.
270This means that no breakpoints are enabled in the parent process either.
271.Pp
272The
273.Dv PTRACE_VFORK
274flag enables a new stop that indicates when the new child process stops
275sharing the process memory of the parent process.
276A debugger can reinsert breakpoints in the parent process and resume it
277in response to this event.
278This event is indicated by setting the
279.Dv PL_FLAG_VFORK_DONE
280flag.
281.El
282.Pp
283The default tracing event mask when attaching to a process via
284.Dv PT_ATTACH ,
285.Dv PT_TRACE_ME ,
286or
287.Dv PTRACE_FORK
288includes only
289.Dv PTRACE_EXEC
290events.
291All other event flags are disabled.
292.Sh PTRACE REQUESTS
293.Pp
294The
295.Fa request
296argument specifies what operation is being performed; the meaning of
297the rest of the arguments depends on the operation, but except for one
298special case noted below, all
299.Fn ptrace
300calls are made by the tracing process, and the
301.Fa pid
302argument specifies the process ID of the traced process
303or a corresponding thread ID.
304The
305.Fa request
306argument
307can be:
308.Bl -tag -width "Dv PT_GET_EVENT_MASK"
309.It Dv PT_TRACE_ME
310This request is the only one used by the traced process; it declares
311that the process expects to be traced by its parent.
312All the other arguments are ignored.
313(If the parent process does not expect to trace the child, it will
314probably be rather confused by the results; once the traced process
315stops, it cannot be made to continue except via
316.Fn ptrace . )
317When a process has used this request and calls
318.Xr execve 2
319or any of the routines built on it
320(such as
321.Xr execv 3 ) ,
322it will stop before executing the first instruction of the new image.
323Also, any setuid or setgid bits on the executable being executed will
324be ignored.
325If the child was created by
326.Xr vfork 2
327system call or
328.Xr rfork 2
329call with the
330.Dv RFMEM
331flag specified, the debugging events are reported to the parent
332only after the
333.Xr execve 2
334is executed.
335.It Dv PT_READ_I , Dv PT_READ_D
336These requests read a single
337.Vt int
338of data from the traced process's address space.
339Traditionally,
340.Fn ptrace
341has allowed for machines with distinct address spaces for instruction
342and data, which is why there are two requests: conceptually,
343.Dv PT_READ_I
344reads from the instruction space and
345.Dv PT_READ_D
346reads from the data space.
347In the current
348.Fx
349implementation, these two requests are completely identical.
350The
351.Fa addr
352argument specifies the address
353(in the traced process's virtual address space)
354at which the read is to be done.
355This address does not have to meet any alignment constraints.
356The value read is returned as the return value from
357.Fn ptrace .
358.It Dv PT_WRITE_I , Dv PT_WRITE_D
359These requests parallel
360.Dv PT_READ_I
361and
362.Dv PT_READ_D ,
363except that they write rather than read.
364The
365.Fa data
366argument supplies the value to be written.
367.It Dv PT_IO
368This request allows reading and writing arbitrary amounts of data in
369the traced process's address space.
370The
371.Fa addr
372argument specifies a pointer to a
373.Vt "struct ptrace_io_desc" ,
374which is defined as follows:
375.Bd -literal
376struct ptrace_io_desc {
377	int	piod_op;	/* I/O operation */
378	void	*piod_offs;	/* child offset */
379	void	*piod_addr;	/* parent offset */
380	size_t	piod_len;	/* request length */
381};
382
383/*
384 * Operations in piod_op.
385 */
386#define PIOD_READ_D	1	/* Read from D space */
387#define PIOD_WRITE_D	2	/* Write to D space */
388#define PIOD_READ_I	3	/* Read from I space */
389#define PIOD_WRITE_I	4	/* Write to I space */
390.Ed
391.Pp
392The
393.Fa data
394argument is ignored.
395The actual number of bytes read or written is stored in
396.Va piod_len
397upon return.
398.It Dv PT_CONTINUE
399The traced process continues execution.
400The
401.Fa addr
402argument
403is an address specifying the place where execution is to be resumed
404(a new value for the program counter),
405or
406.Po Vt caddr_t Pc Ns 1
407to indicate that execution is to pick up where it left off.
408The
409.Fa data
410argument
411provides a signal number to be delivered to the traced process as it
412resumes execution, or 0 if no signal is to be sent.
413.It Dv PT_STEP
414The traced process is single stepped one instruction.
415The
416.Fa addr
417argument
418should be passed
419.Po Vt caddr_t Pc Ns 1 .
420The
421.Fa data
422argument
423provides a signal number to be delivered to the traced process as it
424resumes execution, or 0 if no signal is to be sent.
425.It Dv PT_KILL
426The traced process terminates, as if
427.Dv PT_CONTINUE
428had been used with
429.Dv SIGKILL
430given as the signal to be delivered.
431.It Dv PT_ATTACH
432This request allows a process to gain control of an otherwise
433unrelated process and begin tracing it.
434It does not need any cooperation from the to-be-traced process.
435In
436this case,
437.Fa pid
438specifies the process ID of the to-be-traced process, and the other
439two arguments are ignored.
440This request requires that the target process must have the same real
441UID as the tracing process, and that it must not be executing a setuid
442or setgid executable.
443(If the tracing process is running as root, these restrictions do not
444apply.)
445The tracing process will see the newly-traced process stop and may
446then control it as if it had been traced all along.
447.It Dv PT_DETACH
448This request is like PT_CONTINUE, except that it does not allow
449specifying an alternate place to continue execution, and after it
450succeeds, the traced process is no longer traced and continues
451execution normally.
452.It Dv PT_GETREGS
453This request reads the traced process's machine registers into the
454.Do
455.Vt "struct reg"
456.Dc
457(defined in
458.In machine/reg.h )
459pointed to by
460.Fa addr .
461.It Dv PT_SETREGS
462This request is the converse of
463.Dv PT_GETREGS ;
464it loads the traced process's machine registers from the
465.Do
466.Vt "struct reg"
467.Dc
468(defined in
469.In machine/reg.h )
470pointed to by
471.Fa addr .
472.It Dv PT_GETFPREGS
473This request reads the traced process's floating-point registers into
474the
475.Do
476.Vt "struct fpreg"
477.Dc
478(defined in
479.In machine/reg.h )
480pointed to by
481.Fa addr .
482.It Dv PT_SETFPREGS
483This request is the converse of
484.Dv PT_GETFPREGS ;
485it loads the traced process's floating-point registers from the
486.Do
487.Vt "struct fpreg"
488.Dc
489(defined in
490.In machine/reg.h )
491pointed to by
492.Fa addr .
493.It Dv PT_GETDBREGS
494This request reads the traced process's debug registers into
495the
496.Do
497.Vt "struct dbreg"
498.Dc
499(defined in
500.In machine/reg.h )
501pointed to by
502.Fa addr .
503.It Dv PT_SETDBREGS
504This request is the converse of
505.Dv PT_GETDBREGS ;
506it loads the traced process's debug registers from the
507.Do
508.Vt "struct dbreg"
509.Dc
510(defined in
511.In machine/reg.h )
512pointed to by
513.Fa addr .
514.It Dv PT_LWPINFO
515This request can be used to obtain information about the kernel thread,
516also known as light-weight process, that caused the traced process to stop.
517The
518.Fa addr
519argument specifies a pointer to a
520.Vt "struct ptrace_lwpinfo" ,
521which is defined as follows:
522.Bd -literal
523struct ptrace_lwpinfo {
524	lwpid_t pl_lwpid;
525	int	pl_event;
526	int	pl_flags;
527	sigset_t pl_sigmask;
528	sigset_t pl_siglist;
529	siginfo_t pl_siginfo;
530	char	pl_tdname[MAXCOMLEN + 1];
531	pid_t	pl_child_pid;
532	u_int	pl_syscall_code;
533	u_int	pl_syscall_narg;
534};
535.Ed
536.Pp
537The
538.Fa data
539argument is to be set to the size of the structure known to the caller.
540This allows the structure to grow without affecting older programs.
541.Pp
542The fields in the
543.Vt "struct ptrace_lwpinfo"
544have the following meaning:
545.Bl -tag -width indent -compact
546.It Va pl_lwpid
547LWP id of the thread
548.It Va pl_event
549Event that caused the stop.
550Currently defined events are:
551.Bl -tag -width "Dv PL_EVENT_SIGNAL" -compact
552.It Dv PL_EVENT_NONE
553No reason given
554.It Dv PL_EVENT_SIGNAL
555Thread stopped due to the pending signal
556.El
557.It Va pl_flags
558Flags that specify additional details about observed stop.
559Currently defined flags are:
560.Bl -tag -width indent -compact
561.It Dv PL_FLAG_SCE
562The thread stopped due to system call entry, right after the kernel is entered.
563The debugger may examine syscall arguments that are stored in memory and
564registers according to the ABI of the current process, and modify them,
565if needed.
566.It Dv PL_FLAG_SCX
567The thread is stopped immediately before syscall is returning to the usermode.
568The debugger may examine system call return values in the ABI-defined registers
569and/or memory.
570.It Dv PL_FLAG_EXEC
571When
572.Dv PL_FLAG_SCX
573is set, this flag may be additionally specified to inform that the
574program being executed by debuggee process has been changed by successful
575execution of a system call from the
576.Fn execve 2
577family.
578.It Dv PL_FLAG_SI
579Indicates that
580.Va pl_siginfo
581member of
582.Vt "struct ptrace_lwpinfo"
583contains valid information.
584.It Dv PL_FLAG_FORKED
585Indicates that the process is returning from a call to
586.Fn fork 2
587that created a new child process.
588The process identifier of the new process is available in the
589.Va pl_child_pid
590member of
591.Vt "struct ptrace_lwpinfo" .
592.It Dv PL_FLAG_CHILD
593The flag is set for first event reported from a new child which is
594automatically attached when
595.Dv PTRACE_FORK
596is enabled.
597.It Dv PL_FLAG_BORN
598This flag is set for the first event reported from a new LWP when
599.Dv PTRACE_LWP
600is enabled.
601It is reported along with
602.Dv PL_FLAG_SCX .
603.It Dv PL_FLAG_EXITED
604This flag is set for the last event reported by an exiting LWP when
605.Dv PTRACE_LWP
606is enabled.
607Note that this event is not reported when the last LWP in a process exits.
608The termination of the last thread is reported via a normal process exit
609event.
610.It Dv PL_FLAG_VFORKED
611Indicates that the thread is returning from a call to
612.Xr vfork 2
613that created a new child process.
614This flag is set in addition to
615.Dv PL_FLAG_FORKED .
616.It Dv PL_FLAG_VFORK_DONE
617Indicates that the thread has resumed after a child process created via
618.Xr vfork 2
619has stopped sharing its address space with the traced process.
620.El
621.It Va pl_sigmask
622The current signal mask of the LWP
623.It Va pl_siglist
624The current pending set of signals for the LWP.
625Note that signals that are delivered to the process would not appear
626on an LWP siglist until the thread is selected for delivery.
627.It Va pl_siginfo
628The siginfo that accompanies the signal pending.
629Only valid for
630.Dv PL_EVENT_SIGNAL
631stop when
632.Dv PL_FLAG_SI
633is set in
634.Va pl_flags .
635.It Va pl_tdname
636The name of the thread.
637.It Va pl_child_pid
638The process identifier of the new child process.
639Only valid for a
640.Dv PL_EVENT_SIGNAL
641stop when
642.Dv PL_FLAG_FORKED
643is set in
644.Va pl_flags .
645.It Va pl_syscall_code
646The ABI-specific identifier of the current system call.
647Note that for indirect system calls this field reports the indirected
648system call.
649Only valid when
650.Dv PL_FLAG_SCE
651or
652.Dv PL_FLAG_SCX
653is set in
654.Va pl_flags .
655.It Va pl_syscall_narg
656The number of arguments passed to the current system call not counting
657the system call identifier.
658Note that for indirect system calls this field reports the arguments
659passed to the indirected system call.
660Only valid when
661.Dv PL_FLAG_SCE
662or
663.Dv PL_FLAG_SCX
664is set in
665.Va pl_flags .
666.El
667.It Dv PT_GETNUMLWPS
668This request returns the number of kernel threads associated with the
669traced process.
670.It Dv PT_GETLWPLIST
671This request can be used to get the current thread list.
672A pointer to an array of type
673.Vt lwpid_t
674should be passed in
675.Fa addr ,
676with the array size specified by
677.Fa data .
678The return value from
679.Fn ptrace
680is the count of array entries filled in.
681.It Dv PT_SETSTEP
682This request will turn on single stepping of the specified process.
683Stepping is automatically disabled when a single step trap is caught.
684.It Dv PT_CLEARSTEP
685This request will turn off single stepping of the specified process.
686.It Dv PT_SUSPEND
687This request will suspend the specified thread.
688.It Dv PT_RESUME
689This request will resume the specified thread.
690.It Dv PT_TO_SCE
691This request will set the
692.Dv PTRACE_SCE
693event flag to trace all future system call entries and continue the process.
694The
695.Fa addr
696and
697.Fa data
698arguments are used the same as for
699.Dv PT_CONTINUE .
700.It Dv PT_TO_SCX
701This request will set the
702.Dv PTRACE_SCX
703event flag to trace all future system call exits and continue the process.
704The
705.Fa addr
706and
707.Fa data
708arguments are used the same as for
709.Dv PT_CONTINUE .
710.It Dv PT_SYSCALL
711This request will set the
712.Dv PTRACE_SYSCALL
713event flag to trace all future system call entries and exits and continue
714the process.
715The
716.Fa addr
717and
718.Fa data
719arguments are used the same as for
720.Dv PT_CONTINUE .
721.It Dv PT_GET_SC_ARGS
722For the thread which is stopped in either
723.Dv PL_FLAG_SCE
724or
725.Dv PL_FLAG_SCX
726state, that is, on entry or exit to a syscall,
727this request fetches the syscall arguments.
728.Pp
729The arguments are copied out into the buffer pointed to by the
730.Fa addr
731pointer, sequentially.
732Each syscall argument is stored as the machine word.
733Kernel copies out as many arguments as the syscall accepts,
734see the
735.Va pl_syscall_narg
736member of the
737.Vt struct ptrace_lwpinfo ,
738but not more than the
739.Fa data
740bytes in total are copied.
741.It Dv PT_GET_SC_RET
742Fetch the system call return values on exit from a syscall.
743This request is only valid for threads stopped in a syscall
744exit (the
745.Dv PL_FLAG_SCX
746state).
747The
748.Fa addr
749argument specifies a pointer to a
750.Vt "struct ptrace_sc_ret" ,
751which is defined as follows:
752.Bd -literal
753struct ptrace_sc_ret {
754	register_t	sr_retval[2];
755	int		sr_error;
756};
757.Ed
758.Pp
759The
760.Fa data
761argument is set to the size of the structure.
762.Pp
763If the system call completed successfully,
764.Va sr_error
765is set to zero and the return values of the system call are saved in
766.Va sr_retval .
767If the system call failed to execute,
768.Va sr_error
769field is set to a positive
770.Xr errno 2
771value.
772If the system call completed in an unusual fashion,
773.Va sr_error
774is set to a negative value:
775.Bl -tag -width Dv EJUSTRETURN -compact
776.It Dv ERESTART
777System call will be restarted.
778.It Dv EJUSTRETURN
779System call completed sucessfully but did not set a return value
780.Po for example,
781.Xr setcontext 2
782and
783.Xr sigreturn 2
784.Pc .
785.El
786.It Dv PT_FOLLOW_FORK
787This request controls tracing for new child processes of a traced process.
788If
789.Fa data
790is non-zero,
791.Dv PTRACE_FORK
792is set in the traced process's event tracing mask.
793If
794.Fa data
795is zero,
796.Dv PTRACE_FORK
797is cleared from the traced process's event tracing mask.
798.It Dv PT_LWP_EVENTS
799This request controls tracing of LWP creation and destruction.
800If
801.Fa data
802is non-zero,
803.Dv PTRACE_LWP
804is set in the traced process's event tracing mask.
805If
806.Fa data
807is zero,
808.Dv PTRACE_LWP
809is cleared from the traced process's event tracing mask.
810.It Dv PT_GET_EVENT_MASK
811This request reads the traced process's event tracing mask into the
812integer pointed to by
813.Fa addr .
814The size of the integer must be passed in
815.Fa data .
816.It Dv PT_SET_EVENT_MASK
817This request sets the traced process's event tracing mask from the
818integer pointed to by
819.Fa addr .
820The size of the integer must be passed in
821.Fa data .
822.It Dv PT_VM_TIMESTAMP
823This request returns the generation number or timestamp of the memory map of
824the traced process as the return value from
825.Fn ptrace .
826This provides a low-cost way for the tracing process to determine if the
827VM map changed since the last time this request was made.
828.It Dv PT_VM_ENTRY
829This request is used to iterate over the entries of the VM map of the traced
830process.
831The
832.Fa addr
833argument specifies a pointer to a
834.Vt "struct ptrace_vm_entry" ,
835which is defined as follows:
836.Bd -literal
837struct ptrace_vm_entry {
838	int		pve_entry;
839	int		pve_timestamp;
840	u_long		pve_start;
841	u_long		pve_end;
842	u_long		pve_offset;
843	u_int		pve_prot;
844	u_int		pve_pathlen;
845	long		pve_fileid;
846	uint32_t	pve_fsid;
847	char		*pve_path;
848};
849.Ed
850.Pp
851The first entry is returned by setting
852.Va pve_entry
853to zero.
854Subsequent entries are returned by leaving
855.Va pve_entry
856unmodified from the value returned by previous requests.
857The
858.Va pve_timestamp
859field can be used to detect changes to the VM map while iterating over the
860entries.
861The tracing process can then take appropriate action, such as restarting.
862By setting
863.Va pve_pathlen
864to a non-zero value on entry, the pathname of the backing object is returned
865in the buffer pointed to by
866.Va pve_path ,
867provided the entry is backed by a vnode.
868The
869.Va pve_pathlen
870field is updated with the actual length of the pathname (including the
871terminating null character).
872The
873.Va pve_offset
874field is the offset within the backing object at which the range starts.
875The range is located in the VM space at
876.Va pve_start
877and extends up to
878.Va pve_end
879(inclusive).
880.Pp
881The
882.Fa data
883argument is ignored.
884.It Dv PT_COREDUMP
885This request creates a coredump for the stopped program.
886The
887.Fa addr
888argument specifies a pointer to a
889.Vt "struct ptrace_coredump" ,
890which is defined as follows:
891.Bd -literal
892struct ptrace_coredump {
893	int		pc_fd;
894	uint32_t	pc_flags;
895	off_t		pc_limit;
896};
897.Ed
898The fields of the structure are:
899.Bl -tag -width pc_flags
900.It Dv pc_fd
901File descriptor to write the dump to.
902It must refer to a regular file, opened for writing.
903.It Dv pc_flags
904Flags.
905The following flags are defined:
906.Bl -tag -width PC_COMPRESS
907.It Dv PC_COMPRESS
908Request compression of the dump.
909.It Dv PC_ALL
910Include non-dumpable entries into the dump.
911The dumper ignores
912.Dv MAP_NOCORE
913flag of the process map entry, but device mappings are not dumped even with
914.Dv PC_ALL
915set.
916.El
917.It Dv pc_limit
918Maximum size of the coredump.
919Specify zero for no limit.
920.El
921.Pp
922The size of
923.Vt "struct ptrace_coredump"
924must be passed in
925.Fa data .
926.Pp
927The process must be stopped before dumping core.
928A single thread in the target process is temporarily unsuspended
929in kernel to write the dump.
930If the
931.Nm
932call fails before a thread is unsuspended, there is no event to
933.Xr waitpid 2
934for.
935If a thread was unsuspended, it will stop again before the
936.Nm
937call returns, and the process must be waited upon using
938.Xr waitpid 2
939to consume the new stop event.
940Since it is hard to deduce whether a thread was unsuspended before
941an error occurred, it is recommended to unconditionally perform
942.Xr waitpid 2
943with
944.Dv WNOHANG
945flag after
946.Dv PT_COREDUMP ,
947and silently accept zero result from it.
948.El
949.Sh ARM MACHINE-SPECIFIC REQUESTS
950.Bl -tag -width "Dv PT_SETVFPREGS"
951.It Dv PT_GETVFPREGS
952Return the thread's
953.Dv VFP
954machine state in the buffer pointed to by
955.Fa addr .
956.Pp
957The
958.Fa data
959argument is ignored.
960.It Dv PT_SETVFPREGS
961Set the thread's
962.Dv VFP
963machine state from the buffer pointed to by
964.Fa addr .
965.Pp
966The
967.Fa data
968argument is ignored.
969.El
970.Sh x86 MACHINE-SPECIFIC REQUESTS
971.Bl -tag -width "Dv PT_GETXSTATE_INFO"
972.It Dv PT_GETXMMREGS
973Copy the XMM FPU state into the buffer pointed to by the
974argument
975.Fa addr .
976The buffer has the same layout as the 32-bit save buffer for the
977machine instruction
978.Dv FXSAVE .
979.Pp
980This request is only valid for i386 programs, both on native 32-bit
981systems and on amd64 kernels.
982For 64-bit amd64 programs, the XMM state is reported as part of
983the FPU state returned by the
984.Dv PT_GETFPREGS
985request.
986.Pp
987The
988.Fa data
989argument is ignored.
990.It Dv PT_SETXMMREGS
991Load the XMM FPU state for the thread from the buffer pointed to
992by the argument
993.Fa addr .
994The buffer has the same layout as the 32-bit load buffer for the
995machine instruction
996.Dv FXRSTOR .
997.Pp
998As with
999.Dv PT_GETXMMREGS ,
1000this request is only valid for i386 programs.
1001.Pp
1002The
1003.Fa data
1004argument is ignored.
1005.It Dv PT_GETXSTATE_INFO
1006Report which XSAVE FPU extensions are supported by the CPU
1007and allowed in userspace programs.
1008The
1009.Fa addr
1010argument must point to a variable of type
1011.Vt struct ptrace_xstate_info ,
1012which contains the information on the request return.
1013.Vt struct ptrace_xstate_info
1014is defined as follows:
1015.Bd -literal
1016struct ptrace_xstate_info {
1017	uint64_t	xsave_mask;
1018	uint32_t	xsave_len;
1019};
1020.Ed
1021The
1022.Dv xsave_mask
1023field is a bitmask of the currently enabled extensions.
1024The meaning of the bits is defined in the Intel and AMD
1025processor documentation.
1026The
1027.Dv xsave_len
1028field reports the length of the XSAVE area for storing the hardware
1029state for currently enabled extensions in the format defined by the x86
1030.Dv XSAVE
1031machine instruction.
1032.Pp
1033The
1034.Fa data
1035argument value must be equal to the size of the
1036.Vt struct ptrace_xstate_info .
1037.It Dv PT_GETXSTATE
1038Return the content of the XSAVE area for the thread.
1039The
1040.Fa addr
1041argument points to the buffer where the content is copied, and the
1042.Fa data
1043argument specifies the size of the buffer.
1044The kernel copies out as much content as allowed by the buffer size.
1045The buffer layout is specified by the layout of the save area for the
1046.Dv XSAVE
1047machine instruction.
1048.It Dv PT_SETXSTATE
1049Load the XSAVE state for the thread from the buffer specified by the
1050.Fa addr
1051pointer.
1052The buffer size is passed in the
1053.Fa data
1054argument.
1055The buffer must be at least as large as the
1056.Vt struct savefpu
1057(defined in
1058.Pa x86/fpu.h )
1059to allow the complete x87 FPU and XMM state load.
1060It must not be larger than the XSAVE state length, as reported by the
1061.Dv xsave_len
1062field from the
1063.Vt struct ptrace_xstate_info
1064of the
1065.Dv PT_GETXSTATE_INFO
1066request.
1067Layout of the buffer is identical to the layout of the load area for the
1068.Dv XRSTOR
1069machine instruction.
1070.It Dv PT_GETFSBASE
1071Return the value of the base used when doing segmented
1072memory addressing using the %fs segment register.
1073The
1074.Fa addr
1075argument points to an
1076.Vt unsigned long
1077variable where the base value is stored.
1078.Pp
1079The
1080.Fa data
1081argument is ignored.
1082.It Dv PT_GETGSBASE
1083Like the
1084.Dv PT_GETFSBASE
1085request, but returns the base for the %gs segment register.
1086.It Dv PT_SETFSBASE
1087Set the base for the %fs segment register to the value pointed to
1088by the
1089.Fa addr
1090argument.
1091.Fa addr
1092must point to the
1093.Vt unsigned long
1094variable containing the new base.
1095.Pp
1096The
1097.Fa data
1098argument is ignored.
1099.It Dv PT_SETGSBASE
1100Like the
1101.Dv PT_SETFSBASE
1102request, but sets the base for the %gs segment register.
1103.El
1104.Sh PowerPC MACHINE-SPECIFIC REQUESTS
1105.Bl -tag -width "Dv PT_SETVRREGS"
1106.It Dv PT_GETVRREGS
1107Return the thread's
1108.Dv ALTIVEC
1109machine state in the buffer pointed to by
1110.Fa addr .
1111.Pp
1112The
1113.Fa data
1114argument is ignored.
1115.It Dv PT_SETVRREGS
1116Set the thread's
1117.Dv ALTIVEC
1118machine state from the buffer pointed to by
1119.Fa addr .
1120.Pp
1121The
1122.Fa data
1123argument is ignored.
1124.It Dv PT_GETVSRREGS
1125Return doubleword 1 of the thread's
1126.Dv VSX
1127registers VSR0-VSR31 in the buffer pointed to by
1128.Fa addr .
1129.Pp
1130The
1131.Fa data
1132argument is ignored.
1133.It Dv PT_SETVSRREGS
1134Set doubleword 1 of the thread's
1135.Dv VSX
1136registers VSR0-VSR31 from the buffer pointed to by
1137.Fa addr .
1138.Pp
1139The
1140.Fa data
1141argument is ignored.
1142.El
1143.Pp
1144Additionally, other machine-specific requests can exist.
1145.Sh RETURN VALUES
1146Most requests return 0 on success and \-1 on error.
1147Some requests can cause
1148.Fn ptrace
1149to return
1150\-1
1151as a non-error value, among them are
1152.Dv PT_READ_I
1153and
1154.Dv PT_READ_D ,
1155which return the value read from the process memory on success.
1156To disambiguate,
1157.Va errno
1158can be set to 0 before the call and checked afterwards.
1159.Pp
1160The current
1161.Fn ptrace
1162implementation always sets
1163.Va errno
1164to 0 before calling into the kernel, both for historic reasons and for
1165consistency with other operating systems.
1166It is recommended to assign zero to
1167.Va errno
1168explicitly for forward compatibility.
1169.Sh ERRORS
1170The
1171.Fn ptrace
1172system call may fail if:
1173.Bl -tag -width Er
1174.It Bq Er ESRCH
1175.Bl -bullet -compact
1176.It
1177No process having the specified process ID exists.
1178.El
1179.It Bq Er EINVAL
1180.Bl -bullet -compact
1181.It
1182A process attempted to use
1183.Dv PT_ATTACH
1184on itself.
1185.It
1186The
1187.Fa request
1188argument
1189was not one of the legal requests.
1190.It
1191The signal number
1192(in
1193.Fa data )
1194to
1195.Dv PT_CONTINUE
1196was neither 0 nor a legal signal number.
1197.It
1198.Dv PT_GETREGS ,
1199.Dv PT_SETREGS ,
1200.Dv PT_GETFPREGS ,
1201.Dv PT_SETFPREGS ,
1202.Dv PT_GETDBREGS ,
1203or
1204.Dv PT_SETDBREGS
1205was attempted on a process with no valid register set.
1206(This is normally true only of system processes.)
1207.It
1208.Dv PT_VM_ENTRY
1209was given an invalid value for
1210.Fa pve_entry .
1211This can also be caused by changes to the VM map of the process.
1212.It
1213The size (in
1214.Fa data )
1215provided to
1216.Dv PT_LWPINFO
1217was less than or equal to zero, or larger than the
1218.Vt ptrace_lwpinfo
1219structure known to the kernel.
1220.It
1221The size (in
1222.Fa data )
1223provided to the x86-specific
1224.Dv PT_GETXSTATE_INFO
1225request was not equal to the size of the
1226.Vt struct ptrace_xstate_info .
1227.It
1228The size (in
1229.Fa data )
1230provided to the x86-specific
1231.Dv PT_SETXSTATE
1232request was less than the size of the x87 plus the XMM save area.
1233.It
1234The size (in
1235.Fa data )
1236provided to the x86-specific
1237.Dv PT_SETXSTATE
1238request was larger than returned in the
1239.Dv xsave_len
1240member of the
1241.Vt struct ptrace_xstate_info
1242from the
1243.Dv PT_GETXSTATE_INFO
1244request.
1245.It
1246The base value, provided to the amd64-specific requests
1247.Dv PT_SETFSBASE
1248or
1249.Dv PT_SETGSBASE ,
1250pointed outside of the valid user address space.
1251This error will not occur in 32-bit programs.
1252.El
1253.It Bq Er EBUSY
1254.Bl -bullet -compact
1255.It
1256.Dv PT_ATTACH
1257was attempted on a process that was already being traced.
1258.It
1259A request attempted to manipulate a process that was being traced by
1260some process other than the one making the request.
1261.It
1262A request
1263(other than
1264.Dv PT_ATTACH )
1265specified a process that was not stopped.
1266.El
1267.It Bq Er EPERM
1268.Bl -bullet -compact
1269.It
1270A request
1271(other than
1272.Dv PT_ATTACH )
1273attempted to manipulate a process that was not being traced at all.
1274.It
1275An attempt was made to use
1276.Dv PT_ATTACH
1277on a process in violation of the requirements listed under
1278.Dv PT_ATTACH
1279above.
1280.El
1281.It Bq Er ENOENT
1282.Bl -bullet -compact
1283.It
1284.Dv PT_VM_ENTRY
1285previously returned the last entry of the memory map.
1286No more entries exist.
1287.El
1288.It Bq Er ENAMETOOLONG
1289.Bl -bullet -compact
1290.It
1291.Dv PT_VM_ENTRY
1292cannot return the pathname of the backing object because the buffer is not big
1293enough.
1294.Fa pve_pathlen
1295holds the minimum buffer size required on return.
1296.El
1297.El
1298.Sh SEE ALSO
1299.Xr execve 2 ,
1300.Xr sigaction 2 ,
1301.Xr wait 2 ,
1302.Xr execv 3 ,
1303.Xr i386_clr_watch 3 ,
1304.Xr i386_set_watch 3
1305.Sh HISTORY
1306The
1307.Fn ptrace
1308function appeared in
1309.At v6 .
1310