]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/ptrace.2
getgrent.3, getgrnam.3, getpwent.3, getpwnam.3: Note that returned pointer should...
[thirdparty/man-pages.git] / man2 / ptrace.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (c) 1993 Michael Haardt
4.\" (michael@moria.de),
5.\" Fri Apr 2 11:32:09 MET DST 1993
6.\"
7.\" changes Copyright 1999 Mike Coleman (mkc@acm.org)
8.\" -- major revision to fully document ptrace semantics per recent Linux
c13182ef 9.\" kernel (2.2.10) and glibc (2.1.2)
fea681da
MK
10.\" Sun Nov 7 03:18:35 CST 1999
11.\"
12.\" This is free documentation; you can redistribute it and/or
13.\" modify it under the terms of the GNU General Public License as
14.\" published by the Free Software Foundation; either version 2 of
15.\" the License, or (at your option) any later version.
16.\"
17.\" The GNU General Public License's references to "object code"
18.\" and "executables" are to be interpreted as the output of any
19.\" document formatting or typesetting system, including
20.\" intermediate and printed output.
21.\"
22.\" This manual is distributed in the hope that it will be useful,
23.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
24.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25.\" GNU General Public License for more details.
26.\"
27.\" You should have received a copy of the GNU General Public
28.\" License along with this manual; if not, write to the Free
29.\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
30.\" USA.
31.\"
32.\" Modified Fri Jul 23 23:47:18 1993 by Rik Faith <faith@cs.unc.edu>
33.\" Modified Fri Jan 31 16:46:30 1997 by Eric S. Raymond <esr@thyrsus.com>
34.\" Modified Thu Oct 7 17:28:49 1999 by Andries Brouwer <aeb@cwi.nl>
c11b1abf 35.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
36.\" Added notes on capability requirements
37.\"
44b35ee0
MK
38.\" 2006-03-24, Chuck Ebbert <76306.1226@compuserve.com>
39.\" Added PTRACE_SETOPTIONS, PTRACE_GETEVENTMSG, PTRACE_GETSIGINFO,
40.\" PTRACE_SETSIGINFO, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP
41.\" (Thanks to Blaisorblade, Daniel Jacobowitz and others who helped.)
42.\"
c71df6fb 43.TH PTRACE 2 2009-03-30 "Linux" "Linux Programmer's Manual"
fea681da
MK
44.SH NAME
45ptrace \- process trace
46.SH SYNOPSIS
44b35ee0 47.nf
fea681da
MK
48.B #include <sys/ptrace.h>
49.sp
44b35ee0
MK
50.BI "long ptrace(enum __ptrace_request " request ", pid_t " pid ", "
51.BI " void *" addr ", void *" data );
52.fi
fea681da
MK
53.SH DESCRIPTION
54The
e511ffb6 55.BR ptrace ()
c13182ef
MK
56system call provides a means by which a parent process may observe
57and control the execution of another process,
58and examine and change its core image and registers.
e63ad01d 59It is primarily used to implement breakpoint debugging and system
fea681da
MK
60call tracing.
61.LP
62The parent can initiate a trace by calling
c13182ef 63.BR fork (2)
8bd58774
MK
64and having the resulting child do a
65.BR PTRACE_TRACEME ,
e63ad01d 66followed (typically) by an
fea681da
MK
67.BR exec (3).
68Alternatively, the parent may commence trace of an existing process using
8bd58774 69.BR PTRACE_ATTACH .
fea681da 70.LP
c13182ef
MK
71While being traced, the child will stop each time a signal is delivered,
72even if the signal is being ignored.
8bd58774
MK
73(The exception is
74.BR SIGKILL ,
75which has its usual effect.)
e63ad01d 76The parent will be notified at its next
fea681da 77.BR wait (2)
c13182ef
MK
78and may inspect and modify the child process while it is stopped.
79The parent then causes the child to continue,
e63ad01d 80optionally ignoring the delivered signal
fea681da
MK
81(or even delivering a different signal instead).
82.LP
83When the parent is finished tracing, it can terminate the child with
8bd58774
MK
84.B PTRACE_KILL
85or cause it to continue executing in a normal, untraced mode
86via
87.BR PTRACE_DETACH .
fea681da
MK
88.LP
89The value of \fIrequest\fP determines the action to be performed:
90.TP
8bd58774 91.B PTRACE_TRACEME
c13182ef
MK
92Indicates that this process is to be traced by its parent.
93Any signal
8bd58774
MK
94(except
95.BR SIGKILL )
96delivered to this process will cause it to stop and its
fea681da 97parent to be notified via
0bfa087b 98.BR wait (2).
fea681da 99Also, all subsequent calls to
0bfa087b 100.BR execve (2)
8bd58774
MK
101by this process will cause a
102.B SIGTRAP
103to be sent to it,
c13182ef
MK
104giving the parent a chance to gain control before the new program
105begins execution.
106A process probably shouldn't make this request if its parent
107isn't expecting to trace it.
e63ad01d 108(\fIpid\fP, \fIaddr\fP, and \fIdata\fP are ignored.)
fea681da 109.LP
c13182ef
MK
110The above request is used only by the child process;
111the rest are used only by the parent.
e63ad01d 112In the following requests, \fIpid\fP specifies the child process
c13182ef 113to be acted on.
8bd58774
MK
114For requests other than
115.BR PTRACE_KILL ,
116the child process must
fea681da
MK
117be stopped.
118.TP
8bd58774 119.BR PTRACE_PEEKTEXT ", " PTRACE_PEEKDATA
fea681da 120Reads a word at the location
0daa9e92 121.I addr
fea681da 122in the child's memory, returning the word as the result of the
e511ffb6 123.BR ptrace ()
c13182ef 124call.
e63ad01d 125Linux does not have separate text and data address spaces, so the two
c13182ef 126requests are currently equivalent.
e63ad01d 127(The argument \fIdata\fP is ignored.)
fea681da 128.TP
428d3520 129.B PTRACE_PEEKUSER
254255af
MK
130.\" PTRACE_PEEKUSR in kernel source, but glibc uses PTRACE_PEEKUSER,
131.\" and that is the name that seems common on other systems.
fea681da
MK
132Reads a word at offset
133.I addr
8bd58774
MK
134in the child's USER area,
135which holds the registers and other information about the process
c71df6fb 136(see \fI<sys/user.h>\fP).
e63ad01d 137The word is returned as the result of the
e511ffb6 138.BR ptrace ()
c13182ef 139call.
e63ad01d 140Typically the offset must be word-aligned, though this might vary by
8660aec0
MK
141architecture.
142See NOTES.
143(\fIdata\fP is ignored.)
fea681da 144.TP
8bd58774 145.BR PTRACE_POKETEXT ", " PTRACE_POKEDATA
fea681da 146Copies the word
0daa9e92 147.I data
fea681da 148to location
0daa9e92 149.I addr
c13182ef
MK
150in the child's memory.
151As above, the two requests are currently equivalent.
fea681da 152.TP
428d3520 153.B PTRACE_POKEUSER
254255af
MK
154.\" PTRACE_POKEUSR in kernel source, but glibc uses PTRACE_POKEUSER,
155.\" and that is the name that seems common on other systems.
fea681da 156Copies the word
0daa9e92 157.I data
fea681da
MK
158to offset
159.I addr
8bd58774 160in the child's USER area.
c13182ef
MK
161As above, the offset must typically be word-aligned.
162In order to maintain the integrity of the kernel,
8bd58774 163some modifications to the USER area are disallowed.
fea681da 164.TP
8bd58774 165.BR PTRACE_GETREGS ", " PTRACE_GETFPREGS
c13182ef
MK
166Copies the child's general purpose or floating-point registers,
167respectively, to location \fIdata\fP in the parent.
c71df6fb 168See \fI<sys/user.h>\fP for information on
67da5267
MK
169the format of this data.
170(\fIaddr\fP is ignored.)
fea681da 171.TP
8bd58774 172.BR PTRACE_GETSIGINFO " (since Linux 2.3.99-pre6)"
44b35ee0
MK
173Retrieve information about the signal that caused the stop.
174Copies a \fIsiginfo_t\fP structure (see
175.BR sigaction (2))
176from the child to location \fIdata\fP in the parent.
177(\fIaddr\fP is ignored.)
178.TP
6beb1671 179.BR PTRACE_SETREGS ", " PTRACE_SETFPREGS
c13182ef
MK
180Copies the child's general purpose or floating-point registers,
181respectively, from location \fIdata\fP in the parent.
8bd58774
MK
182As for
183.BR PTRACE_POKEUSER ,
184some general
c13182ef 185purpose register modifications may be disallowed.
e63ad01d 186(\fIaddr\fP is ignored.)
fea681da 187.TP
8bd58774 188.BR PTRACE_SETSIGINFO " (since Linux 2.3.99-pre6)"
44b35ee0 189Set signal information.
c13182ef 190Copies a \fIsiginfo_t\fP structure from location \fIdata\fP in the
e63ad01d 191parent to the child.
44b35ee0 192This will only affect signals that would normally be delivered to
c13182ef
MK
193the child and were caught by the tracer.
194It may be difficult to tell
44b35ee0
MK
195these normal signals from synthetic signals generated by
196.BR ptrace ()
8660aec0
MK
197itself.
198(\fIaddr\fP is ignored.)
44b35ee0 199.TP
8bd58774 200.BR PTRACE_SETOPTIONS " (since Linux 2.4.6; see BUGS for caveats)"
c13182ef 201Sets ptrace options from \fIdata\fP in the parent.
44b35ee0
MK
202(\fIaddr\fP is ignored.)
203\fIdata\fP is interpreted
10f5f294 204as a bit mask of options, which are specified by the following flags:
44b35ee0
MK
205.RS
206.TP
8bd58774 207.BR PTRACE_O_TRACESYSGOOD " (since Linux 2.4.6)"
c13182ef 208When delivering syscall traps, set bit 7 in the signal number
8bd58774 209(i.e., deliver \fI(SIGTRAP | 0x80)\fP
e63ad01d 210This makes it easy for the tracer to tell the difference
c13182ef 211between normal traps and those caused by a syscall.
8bd58774
MK
212.RB ( PTRACE_O_TRACESYSGOOD
213may not work on all architectures.)
44b35ee0 214.TP
8bd58774 215.BR PTRACE_O_TRACEFORK " (since Linux 2.5.46)"
44b35ee0 216Stop the child at the next
0bfa087b 217.BR fork (2)
8bd58774 218call with \fISIGTRAP | PTRACE_EVENT_FORK\ <<\ 8\fP and automatically
c13182ef 219start tracing the newly forked process,
8bd58774
MK
220which will start with a
221.BR SIGSTOP .
222The PID for the new process can be retrieved with
223.BR PTRACE_GETEVENTMSG .
44b35ee0 224.TP
8bd58774 225.BR PTRACE_O_TRACEVFORK " (since Linux 2.5.46)"
44b35ee0 226Stop the child at the next
0bfa087b 227.BR vfork (2)
8bd58774
MK
228call with \fISIGTRAP | PTRACE_EVENT_VFORK\ <<\ 8\fP and automatically start
229tracing the newly vforked process, which will start with a
230.BR SIGSTOP .
231The PID for the new process can be retrieved with
232.BR PTRACE_GETEVENTMSG .
44b35ee0 233.TP
8bd58774 234.BR PTRACE_O_TRACECLONE " (since Linux 2.5.46)"
44b35ee0 235Stop the child at the next
0bfa087b 236.BR clone (2)
8bd58774
MK
237call with \fISIGTRAP | PTRACE_EVENT_CLONE\ <<\ 8\fP and automatically start
238tracing the newly cloned process, which will start with a
239.BR SIGSTOP .
240The PID for the new process can be retrieved with
241.BR PTRACE_GETEVENTMSG .
44b35ee0 242This option may not catch
0bfa087b 243.BR clone (2)
c13182ef
MK
244calls in all cases.
245If the child calls
0bfa087b 246.BR clone (2)
8bd58774 247with the
0daa9e92 248.B CLONE_VFORK
8bd58774
MK
249flag,
250.B PTRACE_EVENT_VFORK
251will be delivered instead
252if
253.B PTRACE_O_TRACEVFORK
254is set; otherwise if the child calls
0bfa087b 255.BR clone (2)
8bd58774
MK
256with the exit signal set to
257.BR SIGCHLD ,
258.B PTRACE_EVENT_FORK
259will be delivered
260if
261.B PTRACE_O_TRACEFORK
262is set.
44b35ee0 263.TP
8bd58774 264.BR PTRACE_O_TRACEEXEC " (since Linux 2.5.46)"
44b35ee0 265Stop the child at the next
0bfa087b 266.BR execve (2)
8bd58774 267call with \fISIGTRAP | PTRACE_EVENT_EXEC\ <<\ 8\fP.
44b35ee0 268.TP
8bd58774 269.BR PTRACE_O_TRACEVFORKDONE " (since Linux 2.5.60)"
44b35ee0 270Stop the child at the completion of the next
0bfa087b 271.BR vfork (2)
8bd58774 272call with \fISIGTRAP | PTRACE_EVENT_VFORK_DONE\ <<\ 8\fP.
44b35ee0 273.TP
8bd58774
MK
274.BR PTRACE_O_TRACEEXIT " (since Linux 2.5.60)"
275Stop the child at exit with \fISIGTRAP | PTRACE_EVENT_EXIT\ <<\ 8\fP.
276The child's exit status can be retrieved with
277.BR PTRACE_GETEVENTMSG .
c13182ef
MK
278This stop will be done early during process exit when registers
279are still available, allowing the tracer to see where the exit occurred,
280whereas the normal exit notification is done after the process
e63ad01d 281is finished exiting.
44b35ee0
MK
282Even though context is available, the tracer cannot prevent the exit from
283happening at this point.
284.RE
285.TP
8bd58774 286.BR PTRACE_GETEVENTMSG " (since Linux 2.5.46)"
c13182ef
MK
287Retrieve a message (as an
288.IR "unsigned long" )
44b35ee0
MK
289about the ptrace event
290that just happened, placing it in the location \fIdata\fP in the parent.
8bd58774 291For
0daa9e92 292.B PTRACE_EVENT_EXIT
8bd58774
MK
293this is the child's exit status.
294For
295.BR PTRACE_EVENT_FORK ,
0daa9e92 296.B PTRACE_EVENT_VFORK
8bd58774
MK
297and
298.B PTRACE_EVENT_CLONE
299this
c13182ef 300is the PID of the new process.
53e4da48 301Since Linux 2.6.18, the PID of the new process is also available
8bd58774
MK
302for
303.BR PTRACE_EVENT_VFORK_DONE .
53e4da48 304(\fIaddr\fP is ignored.)
44b35ee0 305.TP
8bd58774 306.B PTRACE_CONT
c13182ef 307Restarts the stopped child process.
eba72288 308If \fIdata\fP is non-zero and not
8bd58774
MK
309.BR SIGSTOP ,
310it is interpreted as a signal to be delivered to the child;
c13182ef 311otherwise, no signal is delivered.
e63ad01d 312Thus, for example, the parent can control
c13182ef 313whether a signal sent to the child is delivered or not.
e63ad01d 314(\fIaddr\fP is ignored.)
fea681da 315.TP
8bd58774
MK
316.BR PTRACE_SYSCALL ", " PTRACE_SINGLESTEP
317Restarts the stopped child as for
318.BR PTRACE_CONT ,
319but arranges for
c13182ef
MK
320the child to be stopped at the next entry to or exit from a system call,
321or after execution of a single instruction, respectively.
322(The child will also, as usual, be stopped upon receipt of a signal.)
323From the parent's perspective, the child will appear to have been
8bd58774
MK
324stopped by receipt of a
325.BR SIGTRAP .
326So, for
327.BR PTRACE_SYSCALL ,
328for example, the idea is to inspect
c13182ef 329the arguments to the system call at the first stop,
8bd58774
MK
330then do another
331.B PTRACE_SYSCALL
332and inspect the return value of
c13182ef 333the system call at the second stop.
e63ad01d 334(\fIaddr\fP is ignored.)
fea681da 335.TP
6beb1671 336.BR PTRACE_SYSEMU ", " PTRACE_SYSEMU_SINGLESTEP " (since Linux 2.6.14)"
8bd58774
MK
337For
338.BR PTRACE_SYSEMU ,
339continue and stop on entry to the next syscall,
c13182ef 340which will not be executed.
8bd58774
MK
341For
342.BR PTRACE_SYSEMU_SINGLESTEP ,
343do the same
c13182ef
MK
344but also singlestep if not a syscall.
345This call is used by programs like
a832b2d8 346User Mode Linux that want to emulate all the child's system calls.
c13182ef 347(\fIaddr\fP and \fIdata\fP are ignored;
e63ad01d 348not supported on all architectures.)
44b35ee0 349.TP
8bd58774
MK
350.B PTRACE_KILL
351Sends the child a
352.B SIGKILL
353to terminate it.
e63ad01d 354(\fIaddr\fP and \fIdata\fP are ignored.)
fea681da 355.TP
8bd58774 356.B PTRACE_ATTACH
fea681da
MK
357Attaches to the process specified in
358.IR pid ,
a1ffe9f5 359making it a traced "child" of the calling process;
8bd58774
MK
360the behavior of the child is as if it had done a
361.BR PTRACE_TRACEME .
a1ffe9f5 362The calling process actually becomes the parent of the child
e63ad01d 363process for most purposes (e.g., it will receive
fea681da
MK
364notification of child events and appears in
365.BR ps (1)
366output as the child's parent), but a
367.BR getppid (2)
c13182ef 368by the child will still return the PID of the original parent.
8bd58774
MK
369The child is sent a
370.BR SIGSTOP ,
371but will not necessarily have stopped
e63ad01d 372by the completion of this call; use
0bfa087b 373.BR wait (2)
c13182ef 374to wait for the child to stop.
e63ad01d 375(\fIaddr\fP and \fIdata\fP are ignored.)
fea681da 376.TP
8bd58774
MK
377.B PTRACE_DETACH
378Restarts the stopped child as for
379.BR PTRACE_CONT ,
380but first detaches
381from the process, undoing the reparenting effect of
382.BR PTRACE_ATTACH ,
383and the effects of
384.BR PTRACE_TRACEME .
c13182ef 385Although perhaps not intended, under Linux a traced child can be
e63ad01d 386detached in this way regardless of which method was used to initiate
c13182ef 387tracing.
e63ad01d 388(\fIaddr\fP is ignored.)
2b2581ee 389.SH "RETURN VALUE"
988db661 390On success,
0daa9e92 391.B PTRACE_PEEK*
8bd58774 392requests return the requested data,
2b2581ee
MK
393while other requests return zero.
394On error, all requests return \-1, and
395.I errno
396is set appropriately.
8bd58774 397Since the value returned by a successful
0daa9e92 398.B PTRACE_PEEK*
2b2581ee
MK
399request may be \-1, the caller must check
400.I errno
401after such requests to determine whether or not an error occurred.
402.SH ERRORS
403.TP
404.B EBUSY
405(i386 only) There was an error with allocating or freeing a debug
406register.
407.TP
408.B EFAULT
409There was an attempt to read from or write to an invalid area in
410the parent's or child's memory,
411probably because the area wasn't mapped or accessible.
412Unfortunately, under Linux, different variations of this fault
2f0af33b
MK
413will return
414.B EIO
415or
416.B EFAULT
417more or less arbitrarily.
2b2581ee
MK
418.TP
419.B EINVAL
420An attempt was made to set an invalid option.
421.TP
422.B EIO
423\fIrequest\fP is invalid, or an attempt was made to read from or
424write to an invalid area in the parent's or child's memory,
425or there was a word-alignment violation,
426or an invalid signal was specified during a restart request.
427.TP
428.B EPERM
429The specified process cannot be traced.
430This could be because the
431parent has insufficient privileges (the required capability is
432.BR CAP_SYS_PTRACE );
433non-root processes cannot trace processes that they
434cannot send signals to or those running
435set-user-ID/set-group-ID programs, for obvious reasons.
436Alternatively, the process may already be being traced, or be
66ee0c7e 437.BR init (8)
2b2581ee
MK
438(PID 1).
439.TP
440.B ESRCH
441The specified process does not exist, or is not currently being traced
442by the caller, or is not stopped (for requests that require that).
443.SH "CONFORMING TO"
44a2c328 444SVr4, 4.3BSD.
fea681da
MK
445.SH NOTES
446Although arguments to
e511ffb6 447.BR ptrace ()
c13182ef 448are interpreted according to the prototype given,
5260fe08 449glibc currently declares
e511ffb6 450.BR ptrace ()
c13182ef
MK
451as a variadic function with only the \fIrequest\fP argument fixed.
452This means that unneeded trailing arguments may be omitted,
e63ad01d 453though doing so makes use of undocumented
197889d0 454.BR gcc (1)
fea681da
MK
455behavior.
456.LP
457.BR init (8),
e360c3a9 458the process with PID 1, may not be traced.
fea681da
MK
459.LP
460The layout of the contents of memory and the USER area are quite OS- and
461architecture-specific.
8660aec0
MK
462The offset supplied, and the data returned,
463might not entirely match with the definition of
464.IR "struct user" .
465.\" See http://lkml.org/lkml/2008/5/8/375
fea681da 466.LP
c13182ef 467The size of a "word" is determined by the OS variant
3758f6da 468(e.g., for 32-bit Linux it is 32 bits, etc.).
fea681da 469.LP
c13182ef 470Tracing causes a few subtle differences in the semantics of
e63ad01d 471traced processes.
8bd58774
MK
472For example, if a process is attached to with
473.BR PTRACE_ATTACH ,
c13182ef 474its original parent can no longer receive notification via
0bfa087b 475.BR wait (2)
c13182ef 476when it stops, and there is no way for the new parent to
e63ad01d 477effectively simulate this notification.
fea681da 478.LP
8bd58774 479When the parent receives an event with
0daa9e92 480.B PTRACE_EVENT_*
8bd58774 481set,
cd270bc0
MK
482the child is not in the normal signal delivery path.
483This means the parent cannot do
9110c53a 484.BR ptrace (PTRACE_CONT)
6d3925ac 485with a signal or
9110c53a 486.BR ptrace (PTRACE_KILL).
0bfa087b 487.BR kill (2)
8bd58774 488with a
0daa9e92 489.B SIGKILL
8bd58774 490signal can be used instead to kill the child process
6d3925ac
MK
491after receiving one of these messages.
492.LP
fea681da 493This page documents the way the
e511ffb6 494.BR ptrace ()
c13182ef
MK
495call works currently in Linux.
496Its behavior differs noticeably on other flavors of Unix.
e63ad01d 497In any case, use of
e511ffb6 498.BR ptrace ()
fea681da
MK
499is highly OS- and architecture-specific.
500.LP
c13182ef 501The SunOS man page describes
e511ffb6 502.BR ptrace ()
c13182ef 503as "unique and arcane", which it is.
e63ad01d 504The proc-based debugging interface
fea681da 505present in Solaris 2 implements a superset of
e511ffb6 506.BR ptrace ()
fea681da 507functionality in a more powerful and uniform way.
a1d5f77c 508.SH BUGS
8bd58774 509On hosts with 2.6 kernel headers,
0daa9e92 510.B PTRACE_SETOPTIONS
8bd58774 511is declared
a1d5f77c
MK
512with a different value than the one for 2.4.
513This leads to applications compiled with such
514headers failing when run on 2.4 kernels.
8bd58774 515This can be worked around by redefining
0daa9e92 516.B PTRACE_SETOPTIONS
8bd58774
MK
517to
518.BR PTRACE_OLDSETOPTIONS ,
519if that is defined.
fea681da
MK
520.SH "SEE ALSO"
521.BR gdb (1),
522.BR strace (1),
523.BR execve (2),
524.BR fork (2),
525.BR signal (2),
526.BR wait (2),
527.BR exec (3),
528.BR capabilities (7)