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