]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/ptrace.2
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / man / man2 / ptrace.2
CommitLineData
9bccf70c
A
1.\" $OpenBSD: ptrace.2,v 1.3 1996/10/08 01:20:12 michaels Exp $
2.\" $NetBSD: ptrace.2,v 1.3 1996/02/23 01:39:41 jtc Exp $
3.\"
4.\" This file is in the public domain.
3e170ce0 5.Dd March 25, 2015
9bccf70c
A
6.Dt PTRACE 2
7.Os
8.Sh NAME
9.Nm ptrace
10.Nd process tracing and debugging
11.Sh SYNOPSIS
12.Fd #include <sys/types.h>
13.Fd #include <sys/ptrace.h>
14.Ft int
15.Fn ptrace "int request" "pid_t pid" "caddr_t addr" "int data"
16.Sh DESCRIPTION
17.Fn ptrace
18provides tracing and debugging facilities. It allows one process (the
19.Em tracing
20process) to control another (the
21.Em traced
22process). Most of the time, the traced process runs normally, but when
23it receives a signal
24.Po
25see
26.Xr sigaction 2
27.Pc ,
28it stops. The tracing process is expected to notice this via
29.Xr wait 2
30or the delivery of a
31.Dv SIGCHLD
32signal, examine the state of the stopped process, and cause it to
33terminate or continue as appropriate.
34.Fn ptrace
35is the mechanism by which all this happens.
36.Pp
37The
38.Fa request
39argument specifies what operation is being performed; the meaning of
40the rest of the arguments depends on the operation, but except for one
41special case noted below, all
42.Fn ptrace
43calls are made by the tracing process, and the
44.Fa pid
45argument specifies the process ID of the traced process.
46.Fa request
47can be:
48.Bl -tag -width 12n
49.It Dv PT_TRACE_ME
91447636 50This request is one of two used by the traced process; it declares
9bccf70c
A
51that the process expects to be traced by its parent. All the other
52arguments are ignored. (If the parent process does not expect to trace
53the child, it will probably be rather confused by the results; once the
54traced process stops, it cannot be made to continue except via
55.Eo \&
56.Fn ptrace
57.Ec \&.)
58When a process has used this request and calls
59.Xr execve 2
60or any of the routines built on it
61.Po
62such as
63.Xr execv 3
64.Pc ,
65it will stop before executing the first instruction of the new image.
66Also, any setuid or setgid bits on the executable being executed will
67be ignored.
91447636
A
68.It Dv PT_DENY_ATTACH
69This request is the other operation used by the traced process; it allows
70a process that is not currently being traced to deny future traces by its
71parent. All other arguments are ignored. If the process is currently
72being traced, it will exit with the exit status of ENOTSUP; otherwise,
73it sets a flag that denies future traces. An attempt by the parent to
74trace a process which has set this flag will result in a segmentation violation
75in the parent.
9bccf70c
A
76.It Dv PT_CONTINUE
77The traced process continues execution.
78.Fa addr
79is an address specifying the place where execution is to be resumed (a
80new value for the program counter), or
81.Li (caddr_t)1
82to indicate that execution is to pick up where it left off.
83.Fa data
84provides a signal number to be delivered to the traced process as it
85resumes execution, or 0 if no signal is to be sent.
91447636
A
86.It Dv PT_STEP
87The traced process continues execution for a single step. The
88parameters are identical to those passed to
89.Dv PT_CONTINUE.
9bccf70c
A
90.It Dv PT_KILL
91The traced process terminates, as if
92.Dv PT_CONTINUE
93had been used with
94.Dv SIGKILL
95given as the signal to be delivered.
96.It Dv PT_ATTACH
3e170ce0
A
97This call has been replaced with PT_ATTACHEXC.
98.It Dv PT_ATTACHEXC
9bccf70c
A
99This request allows a process to gain control of an otherwise unrelated
100process and begin tracing it. It does not need any cooperation from
101the to-be-traced process. In this case,
102.Fa pid
103specifies the process ID of the to-be-traced process, and the other two
104arguments are ignored. This request requires that the target process
105must have the same real UID as the tracing process, and that it must
106not be executing a setuid or setgid executable. (If the tracing
107process is running as root, these restrictions do not apply.) The
108tracing process will see the newly-traced process stop and may then
3e170ce0
A
109control it as if it had been traced all along. Note that this call differs
110from the prior call (
111.Dv PT_ATTACH )
112in that signals from the child are delivered to the parent as Mach exceptions (see EXC_SOFT_SIGNAL).
9bccf70c
A
113.It Dv PT_DETACH
114This request is like PT_CONTINUE, except that it does not allow
115specifying an alternate place to continue execution, and after it
116succeeds, the traced process is no longer traced and continues
117execution normally.
118.El
119.Pp
9bccf70c
A
120.Sh ERRORS
121Some requests can cause
122.Fn ptrace
123to return
124.Li -1
125as a non-error value; to disambiguate,
126.Va errno
127can be set to 0 before the call and checked afterwards. The possible
128errors are:
129.Bl -tag -width 4n
130.It Bq Er ESRCH
131No process having the specified process ID exists.
132.It Bq Er EINVAL
133.Bl -bullet -compact
134.It
135A process attempted to use
3e170ce0 136.Dv PT_ATTACHEXC
9bccf70c
A
137on itself.
138.It
139The
140.Fa request
141was not one of the legal requests.
9bccf70c
A
142.It
143The signal number (in
144.Fa data )
145to
146.Dv PT_CONTINUE
9bccf70c
A
147was neither 0 nor a legal signal number.
148.It
149.Dv PT_GETREGS ,
150.Dv PT_SETREGS ,
151.Dv PT_GETFPREGS ,
152or
153.Dv PT_SETFPREGS
154was attempted on a process with no valid register set. (This is
155normally true only of system processes.)
156.El
157.It Bq Er EBUSY
158.Bl -bullet -compact
159.It
3e170ce0 160.Dv PT_ATTACHEXC
9bccf70c
A
161was attempted on a process that was already being traced.
162.It
163A request attempted to manipulate a process that was being traced by
164some process other than the one making the request.
165.It
166A request (other than
3e170ce0 167.Dv PT_ATTACHEXC )
9bccf70c
A
168specified a process that wasn't stopped.
169.El
170.It Bq Er EPERM
171.Bl -bullet -compact
172.It
173A request (other than
3e170ce0 174.Dv PT_ATTACHEXC )
9bccf70c
A
175attempted to manipulate a process that wasn't being traced at all.
176.It
177An attempt was made to use
3e170ce0 178.Dv PT_ATTACHEXC
9bccf70c 179on a process in violation of the requirements listed under
3e170ce0 180.Dv PT_ATTACHEXC
9bccf70c
A
181above.
182.El
55e303ae 183.El