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