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 February 19, 2012 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, system calls, or stepping by the tracing 62process. 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 72The 73.Fa request 74argument specifies what operation is being performed; the meaning of 75the rest of the arguments depends on the operation, but except for one 76special case noted below, all 77.Fn ptrace 78calls are made by the tracing process, and the 79.Fa pid 80argument specifies the process ID of the traced process 81or a corresponding thread ID. 82The 83.Fa request 84argument 85can be: 86.Bl -tag -width 12n 87.It Dv PT_TRACE_ME 88This request is the only one used by the traced process; it declares 89that the process expects to be traced by its parent. 90All the other arguments are ignored. 91(If the parent process does not expect to trace the child, it will 92probably be rather confused by the results; once the traced process 93stops, it cannot be made to continue except via 94.Fn ptrace . ) 95When a process has used this request and calls 96.Xr execve 2 97or any of the routines built on it 98(such as 99.Xr execv 3 ) , 100it will stop before executing the first instruction of the new image. 101Also, any setuid or setgid bits on the executable being executed will 102be ignored. 103.It Dv PT_READ_I , Dv PT_READ_D 104These requests read a single 105.Vt int 106of data from the traced process's address space. 107Traditionally, 108.Fn ptrace 109has allowed for machines with distinct address spaces for instruction 110and data, which is why there are two requests: conceptually, 111.Dv PT_READ_I 112reads from the instruction space and 113.Dv PT_READ_D 114reads from the data space. 115In the current 116.Fx 117implementation, these two requests are completely identical. 118The 119.Fa addr 120argument specifies the address 121(in the traced process's virtual address space) 122at which the read is to be done. 123This address does not have to meet any alignment constraints. 124The value read is returned as the return value from 125.Fn ptrace . 126.It Dv PT_WRITE_I , Dv PT_WRITE_D 127These requests parallel 128.Dv PT_READ_I 129and 130.Dv PT_READ_D , 131except that they write rather than read. 132The 133.Fa data 134argument supplies the value to be written. 135.It Dv PT_IO 136This request allows reading and writing arbitrary amounts of data in 137the traced process's address space. 138The 139.Fa addr 140argument specifies a pointer to a 141.Vt "struct ptrace_io_desc" , 142which is defined as follows: 143.Bd -literal 144struct ptrace_io_desc { 145 int piod_op; /* I/O operation */ 146 void *piod_offs; /* child offset */ 147 void *piod_addr; /* parent offset */ 148 size_t piod_len; /* request length */ 149}; 150 151/* 152 * Operations in piod_op. 153 */ 154#define PIOD_READ_D 1 /* Read from D space */ 155#define PIOD_WRITE_D 2 /* Write to D space */ 156#define PIOD_READ_I 3 /* Read from I space */ 157#define PIOD_WRITE_I 4 /* Write to I space */ 158.Ed 159.Pp 160The 161.Fa data 162argument is ignored. 163The actual number of bytes read or written is stored in 164.Va piod_len 165upon return. 166.It Dv PT_CONTINUE 167The traced process continues execution. 168The 169.Fa addr 170argument 171is an address specifying the place where execution is to be resumed 172(a new value for the program counter), 173or 174.Po Vt caddr_t Pc Ns 1 175to indicate that execution is to pick up where it left off. 176The 177.Fa data 178argument 179provides a signal number to be delivered to the traced process as it 180resumes execution, or 0 if no signal is to be sent. 181.It Dv PT_STEP 182The traced process is single stepped one instruction. 183The 184.Fa addr 185argument 186should be passed 187.Po Vt caddr_t Pc Ns 1 . 188The 189.Fa data 190argument 191provides a signal number to be delivered to the traced process as it 192resumes execution, or 0 if no signal is to be sent. 193.It Dv PT_KILL 194The traced process terminates, as if 195.Dv PT_CONTINUE 196had been used with 197.Dv SIGKILL 198given as the signal to be delivered. 199.It Dv PT_ATTACH 200This request allows a process to gain control of an otherwise 201unrelated process and begin tracing it. 202It does not need any cooperation from the to-be-traced process. 203In 204this case, 205.Fa pid 206specifies the process ID of the to-be-traced process, and the other 207two arguments are ignored. 208This request requires that the target process must have the same real 209UID as the tracing process, and that it must not be executing a setuid 210or setgid executable. 211(If the tracing process is running as root, these restrictions do not 212apply.) 213The tracing process will see the newly-traced process stop and may 214then control it as if it had been traced all along. 215.It Dv PT_DETACH 216This request is like PT_CONTINUE, except that it does not allow 217specifying an alternate place to continue execution, and after it 218succeeds, the traced process is no longer traced and continues 219execution normally. 220.It Dv PT_GETREGS 221This request reads the traced process's machine registers into the 222.Do 223.Vt "struct reg" 224.Dc 225(defined in 226.In machine/reg.h ) 227pointed to by 228.Fa addr . 229.It Dv PT_SETREGS 230This request is the converse of 231.Dv PT_GETREGS ; 232it loads the traced process's machine registers from the 233.Do 234.Vt "struct reg" 235.Dc 236(defined in 237.In machine/reg.h ) 238pointed to by 239.Fa addr . 240.It Dv PT_GETFPREGS 241This request reads the traced process's floating-point registers into 242the 243.Do 244.Vt "struct fpreg" 245.Dc 246(defined in 247.In machine/reg.h ) 248pointed to by 249.Fa addr . 250.It Dv PT_SETFPREGS 251This request is the converse of 252.Dv PT_GETFPREGS ; 253it loads the traced process's floating-point registers from the 254.Do 255.Vt "struct fpreg" 256.Dc 257(defined in 258.In machine/reg.h ) 259pointed to by 260.Fa addr . 261.It Dv PT_GETDBREGS 262This request reads the traced process's debug registers into 263the 264.Do 265.Vt "struct dbreg" 266.Dc 267(defined in 268.In machine/reg.h ) 269pointed to by 270.Fa addr . 271.It Dv PT_SETDBREGS 272This request is the converse of 273.Dv PT_GETDBREGS ; 274it loads the traced process's debug registers from the 275.Do 276.Vt "struct dbreg" 277.Dc 278(defined in 279.In machine/reg.h ) 280pointed to by 281.Fa addr . 282.It Dv PT_LWPINFO 283This request can be used to obtain information about the kernel thread, 284also known as light-weight process, that caused the traced process to stop. 285The 286.Fa addr 287argument specifies a pointer to a 288.Vt "struct ptrace_lwpinfo" , 289which is defined as follows: 290.Bd -literal 291struct ptrace_lwpinfo { 292 lwpid_t pl_lwpid; 293 int pl_event; 294 int pl_flags; 295 sigset_t pl_sigmask; 296 sigset_t pl_siglist; 297 siginfo_t pl_siginfo; 298 char pl_tdname[MAXCOMLEN + 1]; 299 int pl_child_pid; 300}; 301.Ed 302.Pp 303The 304.Fa data 305argument is to be set to the size of the structure known to the caller. 306This allows the structure to grow without affecting older programs. 307.Pp 308The fields in the 309.Vt "struct ptrace_lwpinfo" 310have the following meaning: 311.Bl -tag -width indent -compact 312.It pl_lwpid 313LWP id of the thread 314.It pl_event 315Event that caused the stop. 316Currently defined events are 317.Bl -tag -width indent -compact 318.It PL_EVENT_NONE 319No reason given 320.It PL_EVENT_SIGNAL 321Thread stopped due to the pending signal 322.El 323.It pl_flags 324Flags that specify additional details about observed stop. 325Currently defined flags are: 326.Bl -tag -width indent -compact 327.It PL_FLAG_SCE 328The thread stopped due to system call entry, right after the kernel is entered. 329The debugger may examine syscall arguments that are stored in memory and 330registers according to the ABI of the current process, and modify them, 331if needed. 332.It PL_FLAG_SCX 333The thread is stopped immediately before syscall is returning to the usermode. 334The debugger may examine system call return values in the ABI-defined registers 335and/or memory. 336.It PL_FLAG_EXEC 337When 338.Dv PL_FLAG_SCX 339is set, this flag may be additionally specified to inform that the 340program being executed by debuggee process has been changed by successful 341execution of a system call from the 342.Fn execve 2 343family. 344.It PL_FLAG_SI 345Indicates that 346.Va pl_siginfo 347member of 348.Vt "struct ptrace_lwpinfo" 349contains valid information. 350.It PL_FLAG_FORKED 351Indicates that the process is returning from a call to 352.Fn fork 2 353that created a new child process. 354The process identifier of the new process is available in the 355.Va pl_child_pid 356member of 357.Vt "struct ptrace_lwpinfo" . 358.It PL_FLAG_CHILD 359The flag is set for first event reported from a new child, which is 360automatically attached due to 361.Dv PT_FOLLOW_FORK 362enabled. 363.El 364.It pl_sigmask 365The current signal mask of the LWP 366.It pl_siglist 367The current pending set of signals for the LWP. 368Note that signals that are delivered to the process would not appear 369on an LWP siglist until the thread is selected for delivery. 370.It pl_siginfo 371The siginfo that accompanies the signal pending. 372Only valid for 373.Dv PL_EVENT_SIGNAL 374stop when 375.Dv PL_FLAG_SI 376is set in 377.Va pl_flags . 378.It pl_tdname 379The name of the thread. 380.It pl_child_pid 381The process identifier of the new child process. 382Only valid for a 383.Dv PL_EVENT_SIGNAL 384stop when 385.Dv PL_FLAG_FORKED 386is set in 387.Va pl_flags . 388.El 389.It PT_GETNUMLWPS 390This request returns the number of kernel threads associated with the 391traced process. 392.It PT_GETLWPLIST 393This request can be used to get the current thread list. 394A pointer to an array of type 395.Vt lwpid_t 396should be passed in 397.Fa addr , 398with the array size specified by 399.Fa data . 400The return value from 401.Fn ptrace 402is the count of array entries filled in. 403.It PT_SETSTEP 404This request will turn on single stepping of the specified process. 405.It PT_CLEARSTEP 406This request will turn off single stepping of the specified process. 407.It PT_SUSPEND 408This request will suspend the specified thread. 409.It PT_RESUME 410This request will resume the specified thread. 411.It PT_TO_SCE 412This request will trace the specified process on each system call entry. 413.It PT_TO_SCX 414This request will trace the specified process on each system call exit. 415.It PT_SYSCALL 416This request will trace the specified process 417on each system call entry and exit. 418.It PT_FOLLOW_FORK 419This request controls tracing for new child processes of a traced process. 420If 421.Fa data 422is non-zero, 423then new child processes will enable tracing and stop before executing their 424first instruction. 425If 426.Fa data 427is zero, then new child processes will execute without tracing enabled. 428By default, tracing is not enabled for new child processes. 429Child processes do not inherit this property. 430The traced process will set the 431.Dv PL_FLAG_FORKED 432flag upon exit from a system call that creates a new process. 433.It PT_VM_TIMESTAMP 434This request returns the generation number or timestamp of the memory map of 435the traced process as the return value from 436.Fn ptrace . 437This provides a low-cost way for the tracing process to determine if the 438VM map changed since the last time this request was made. 439.It PT_VM_ENTRY 440This request is used to iterate over the entries of the VM map of the traced 441process. 442The 443.Fa addr 444argument specifies a pointer to a 445.Vt "struct ptrace_vm_entry" , 446which is defined as follows: 447.Bd -literal 448struct ptrace_vm_entry { 449 int pve_entry; 450 int pve_timestamp; 451 u_long pve_start; 452 u_long pve_end; 453 u_long pve_offset; 454 u_int pve_prot; 455 u_int pve_pathlen; 456 long pve_fileid; 457 uint32_t pve_fsid; 458 char *pve_path; 459}; 460.Ed 461.Pp 462The first entry is returned by setting 463.Va pve_entry 464to zero. 465Subsequent entries are returned by leaving 466.Va pve_entry 467unmodified from the value returned by previous requests. 468The 469.Va pve_timestamp 470field can be used to detect changes to the VM map while iterating over the 471entries. 472The tracing process can then take appropriate action, such as restarting. 473By setting 474.Va pve_pathlen 475to a non-zero value on entry, the pathname of the backing object is returned 476in the buffer pointed to by 477.Va pve_path , 478provided the entry is backed by a vnode. 479The 480.Va pve_pathlen 481field is updated with the actual length of the pathname (including the 482terminating null character). 483The 484.Va pve_offset 485field is the offset within the backing object at which the range starts. 486The range is located in the VM space at 487.Va pve_start 488and extends up to 489.Va pve_end 490(inclusive). 491.Pp 492The 493.Fa data 494argument is ignored. 495.El 496.Pp 497Additionally, machine-specific requests can exist. 498.Sh RETURN VALUES 499Some requests can cause 500.Fn ptrace 501to return 502\-1 503as a non-error value; to disambiguate, 504.Va errno 505can be set to 0 before the call and checked afterwards. 506.Sh ERRORS 507The 508.Fn ptrace 509system call may fail if: 510.Bl -tag -width Er 511.It Bq Er ESRCH 512.Bl -bullet -compact 513.It 514No process having the specified process ID exists. 515.El 516.It Bq Er EINVAL 517.Bl -bullet -compact 518.It 519A process attempted to use 520.Dv PT_ATTACH 521on itself. 522.It 523The 524.Fa request 525argument 526was not one of the legal requests. 527.It 528The signal number 529(in 530.Fa data ) 531to 532.Dv PT_CONTINUE 533was neither 0 nor a legal signal number. 534.It 535.Dv PT_GETREGS , 536.Dv PT_SETREGS , 537.Dv PT_GETFPREGS , 538.Dv PT_SETFPREGS , 539.Dv PT_GETDBREGS , 540or 541.Dv PT_SETDBREGS 542was attempted on a process with no valid register set. 543(This is normally true only of system processes.) 544.It 545.Dv PT_VM_ENTRY 546was given an invalid value for 547.Fa pve_entry . 548This can also be caused by changes to the VM map of the process. 549.El 550.It Bq Er EBUSY 551.Bl -bullet -compact 552.It 553.Dv PT_ATTACH 554was attempted on a process that was already being traced. 555.It 556A request attempted to manipulate a process that was being traced by 557some process other than the one making the request. 558.It 559A request 560(other than 561.Dv PT_ATTACH ) 562specified a process that was not stopped. 563.El 564.It Bq Er EPERM 565.Bl -bullet -compact 566.It 567A request 568(other than 569.Dv PT_ATTACH ) 570attempted to manipulate a process that was not being traced at all. 571.It 572An attempt was made to use 573.Dv PT_ATTACH 574on a process in violation of the requirements listed under 575.Dv PT_ATTACH 576above. 577.El 578.It Bq Er ENOENT 579.Bl -bullet -compact 580.It 581.Dv PT_VM_ENTRY 582previously returned the last entry of the memory map. 583No more entries exist. 584.El 585.It Bq Er ENAMETOOLONG 586.Bl -bullet -compact 587.It 588.Dv PT_VM_ENTRY 589cannot return the pathname of the backing object because the buffer is not big 590enough. 591.Fa pve_pathlen 592holds the minimum buffer size required on return. 593.El 594.El 595.Sh SEE ALSO 596.Xr execve 2 , 597.Xr sigaction 2 , 598.Xr wait 2 , 599.Xr execv 3 , 600.Xr i386_clr_watch 3 , 601.Xr i386_set_watch 3 602.Sh HISTORY 603The 604.Fn ptrace 605function appeared in 606.At v7 . 607