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