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