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