<s>
ptrace	B-Operating_System
is	O
a	O
system	B-Operating_System
call	I-Operating_System
found	O
in	O
Unix	B-Application
and	O
several	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
By	O
using	O
ptrace	B-Operating_System
(	O
the	O
name	O
is	O
an	O
abbreviation	O
of	O
"	O
process	B-Operating_System
trace	O
"	O
)	O
one	O
process	B-Operating_System
can	O
control	O
another	O
,	O
enabling	O
the	O
controller	O
to	O
inspect	O
and	O
manipulate	O
the	O
internal	O
state	O
of	O
its	O
target	O
.	O
</s>
<s>
ptrace	B-Operating_System
is	O
used	O
by	O
debuggers	B-Application
and	O
other	O
code-analysis	O
tools	O
,	O
mostly	O
as	O
aids	O
to	O
software	O
development	O
.	O
</s>
<s>
ptrace	B-Operating_System
is	O
used	O
by	O
debuggers	B-Application
(	O
such	O
as	O
gdb	B-Language
and	O
dbx	B-Application
)	O
,	O
by	O
tracing	O
tools	O
like	O
strace	B-Language
and	O
ltrace	B-Device
,	O
and	O
by	O
code	B-Application
coverage	I-Application
tools	O
.	O
</s>
<s>
ptrace	B-Operating_System
is	O
also	O
used	O
by	O
specialized	O
programs	O
to	O
patch	O
running	O
programs	O
,	O
to	O
avoid	O
unfixed	O
bugs	O
or	O
to	O
overcome	O
security	O
features	O
.	O
</s>
<s>
It	O
can	O
further	O
be	O
used	O
as	O
a	O
sandbox	O
and	O
as	O
a	O
run-time	O
environment	O
simulator	O
(	O
like	O
emulating	O
root	B-Application
access	I-Application
for	O
non-root	O
software	O
)	O
.	O
</s>
<s>
By	O
attaching	O
to	O
another	O
process	B-Operating_System
using	O
the	O
ptrace	B-Operating_System
call	O
,	O
a	O
tool	O
has	O
extensive	O
control	O
over	O
the	O
operation	O
of	O
its	O
target	O
.	O
</s>
<s>
This	O
includes	O
manipulation	O
of	O
its	O
file	B-Application
descriptors	I-Application
,	O
memory	O
,	O
and	O
registers	B-General_Concept
.	O
</s>
<s>
It	O
can	O
single-step	O
through	O
the	O
target	O
's	O
code	O
,	O
can	O
observe	O
and	O
intercept	O
system	B-Operating_System
calls	I-Operating_System
and	O
their	O
results	O
,	O
and	O
can	O
manipulate	O
the	O
target	O
's	O
signal	B-Operating_System
handlers	I-Operating_System
and	O
both	O
receive	O
and	O
send	O
signals	O
on	O
its	O
behalf	O
.	O
</s>
<s>
The	O
ability	O
to	O
write	O
into	O
the	O
target	O
's	O
memory	O
allows	O
not	O
only	O
its	O
data	O
store	O
to	O
be	O
changed	O
,	O
but	O
also	O
the	O
application	O
's	O
own	O
code	B-General_Concept
segment	I-General_Concept
,	O
allowing	O
the	O
controller	O
to	O
install	O
breakpoints	O
and	O
patch	O
the	O
running	O
code	O
of	O
the	O
target	O
.	O
</s>
<s>
As	O
the	O
ability	O
to	O
inspect	O
and	O
alter	O
another	O
process	B-Operating_System
is	O
very	O
powerful	O
,	O
ptrace	B-Operating_System
can	O
attach	O
only	O
to	O
processes	O
that	O
the	O
owner	O
can	O
send	O
signals	O
to	O
(	O
typically	O
only	O
their	O
own	O
processes	O
)	O
;	O
the	O
superuser	B-Application
account	O
can	O
ptrace	B-Operating_System
almost	O
any	O
process	B-Operating_System
(	O
except	O
init	B-Operating_System
on	O
kernels	O
before	O
2.6.26	O
)	O
.	O
</s>
<s>
In	O
Linux	B-Application
systems	O
that	O
feature	O
capabilities-based	O
security	O
,	O
the	O
ability	O
to	O
ptrace	B-Operating_System
is	O
further	O
limited	O
by	O
the	O
CAP_SYS_PTRACE	O
capability	O
or	O
by	O
the	O
YAMA	O
Linux	B-Application
Security	I-Application
Module	I-Application
.	O
</s>
<s>
In	O
FreeBSD	B-Operating_System
,	O
it	O
is	O
limited	O
by	O
FreeBSD	B-Language
jails	I-Language
and	O
Mandatory	O
Access	O
Control	O
policies	O
.	O
</s>
<s>
Communications	O
between	O
the	O
controller	O
and	O
target	O
take	O
place	O
using	O
repeated	O
calls	O
of	O
ptrace	B-Operating_System
,	O
passing	O
a	O
small	O
fixed-size	O
block	O
of	O
memory	O
between	O
the	O
two	O
(	O
necessitating	O
two	O
context	B-Operating_System
switches	I-Operating_System
per	O
call	O
)	O
;	O
this	O
is	O
acutely	O
inefficient	O
when	O
accessing	O
large	O
amounts	O
of	O
the	O
target	O
's	O
memory	O
,	O
as	O
this	O
can	O
only	O
be	O
done	O
in	O
word	O
sized	O
blocks	O
(	O
with	O
a	O
ptrace	B-Operating_System
call	O
for	O
each	O
word	O
)	O
.	O
</s>
<s>
For	O
this	O
reason	O
the	O
8th	O
edition	O
of	O
Unix	B-Application
introduced	O
procfs	B-Application
,	O
which	O
allows	O
permitted	O
processes	O
direct	O
access	O
to	O
the	O
memory	O
of	O
another	O
process	B-Operating_System
-	O
4.4BSD	O
followed	O
,	O
and	O
the	O
use	O
of	O
/proc	B-Application
for	O
debugger	B-Application
support	O
was	O
inherited	O
by	O
Solaris	B-Application
,	O
BSD	O
,	O
and	O
AIX	B-Application
,	O
and	O
mostly	O
copied	O
by	O
Linux	B-Application
.	O
</s>
<s>
Some	O
,	O
such	O
as	O
Solaris	B-Application
,	O
have	O
removed	O
ptrace	B-Operating_System
as	O
a	O
system	B-Operating_System
call	I-Operating_System
altogether	O
,	O
retaining	O
it	O
as	O
a	O
library	O
call	O
that	O
reinterprets	O
calls	O
to	O
ptrace	B-Operating_System
in	O
terms	O
of	O
the	O
platform	O
's	O
procfs	B-Application
.	O
</s>
<s>
Such	O
systems	O
use	O
ioctls	B-Operating_System
on	O
the	O
file	B-Application
descriptor	I-Application
of	O
the	O
opened	O
/proc	B-Application
file	O
to	O
issue	O
commands	O
to	O
the	O
controlled	O
process	B-Operating_System
.	O
</s>
<s>
FreeBSD	B-Operating_System
,	O
on	O
the	O
other	O
hand	O
,	O
extended	O
ptrace	B-Operating_System
to	O
remove	O
mentioned	O
problems	O
,	O
and	O
declared	O
procfs	B-Application
obsolete	O
due	O
to	O
its	O
inherent	O
design	O
problems	O
.	O
</s>
<s>
ptrace	B-Operating_System
only	O
provides	O
the	O
most	O
basic	O
interface	O
necessary	O
to	O
support	O
debuggers	B-Application
and	O
similar	O
tools	O
.	O
</s>
<s>
Programs	O
using	O
it	O
must	O
have	O
intimate	O
knowledge	O
of	O
the	O
specifics	O
of	O
the	O
OS	O
and	O
architecture	O
,	O
including	O
stack	O
layout	O
,	O
application	B-Operating_System
binary	I-Operating_System
interface	I-Operating_System
,	O
system	B-Operating_System
call	I-Operating_System
mechanism	O
,	O
name	B-Language
mangling	I-Language
,	O
the	O
format	B-General_Concept
of	I-General_Concept
any	I-General_Concept
debug	I-General_Concept
data	I-General_Concept
,	O
and	O
are	O
responsible	O
for	O
understanding	O
and	O
disassembling	O
machine	B-Language
code	I-Language
themselves	O
.	O
</s>
<s>
Further	O
,	O
programs	O
that	O
inject	O
executable	O
code	O
into	O
the	O
target	O
process	B-Operating_System
or	O
(	O
like	O
gdb	B-Language
)	O
allow	O
the	O
user	O
to	O
enter	O
commands	O
that	O
are	O
executed	O
in	O
the	O
context	O
of	O
the	O
target	O
must	O
generate	O
and	O
load	O
that	O
code	O
themselves	O
,	O
generally	O
without	O
the	O
help	O
of	O
the	O
program	B-Operating_System
loader	I-Operating_System
.	O
</s>
<s>
ptrace	B-Operating_System
was	O
first	O
implemented	O
in	O
Version	B-Operating_System
6	I-Operating_System
Unix	I-Operating_System
,	O
and	O
was	O
present	O
in	O
both	O
the	O
SVr4	O
and	O
4.3BSD	B-Operating_System
branches	O
of	O
Unix	B-Application
.	O
</s>
<s>
ptrace	B-Operating_System
is	O
available	O
as	O
a	O
system	B-Operating_System
call	I-Operating_System
on	O
IRIX	B-Operating_System
,	O
IBM	B-Application
AIX	I-Application
,	O
NetBSD	B-Device
,	O
FreeBSD	B-Operating_System
,	O
OpenBSD	B-Operating_System
,	O
and	O
Linux	B-Application
.	O
</s>
<s>
ptrace	B-Operating_System
is	O
implemented	O
as	O
a	O
library	O
call	O
on	O
Solaris	B-Application
,	O
built	O
on	O
the	O
Solaris	B-Application
kernel	I-Application
's	O
procfs	B-Application
filesystem	O
;	O
Sun	O
notes	O
that	O
ptrace	B-Operating_System
on	O
Solaris	B-Application
is	O
intended	O
for	O
compatibility	O
,	O
and	O
recommends	O
that	O
new	O
implementations	O
use	O
the	O
richer	O
interface	O
that	O
proc	O
supplies	O
instead	O
.	O
</s>
<s>
UnixWare	B-Operating_System
also	O
features	O
a	O
limited	O
ptrace	B-Operating_System
but	O
like	O
Sun	O
,	O
SCO	O
recommends	O
implementers	O
use	O
the	O
underlying	O
procfs	B-Application
features	O
instead	O
.	O
</s>
<s>
HP-UX	B-Application
supported	O
ptrace	B-Operating_System
until	O
release	O
11i	O
v3	O
(	O
it	O
was	O
deprecated	O
in	O
favour	O
of	O
,	O
a	O
similar	O
OS-specific	O
call	O
,	O
in	O
11i	O
v1	O
)	O
.	O
</s>
<s>
Apple	O
's	O
macOS	B-Application
also	O
implements	O
ptrace	B-Operating_System
as	O
a	O
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
Apple	O
's	O
version	O
adds	O
a	O
special	O
option	O
PT_DENY_ATTACH	O
–	O
if	O
a	O
process	B-Operating_System
invokes	O
this	O
option	O
on	O
itself	O
,	O
subsequent	O
attempts	O
to	O
ptrace	B-Operating_System
the	O
process	B-Operating_System
will	O
fail	O
.	O
</s>
<s>
Apple	O
uses	O
this	O
feature	O
to	O
limit	O
the	O
use	O
of	O
debuggers	B-Application
on	O
programs	O
that	O
manipulate	O
DRM-ed	O
content	O
,	O
including	O
iTunes	B-Operating_System
.	O
</s>
<s>
PT_DENY_ATTACH	O
on	O
also	O
disables	O
DTrace	B-Application
's	O
ability	O
to	O
monitor	O
the	O
process	B-Operating_System
.	O
</s>
<s>
Debuggers	B-Application
on	O
OS	O
X	O
typically	O
use	O
a	O
combination	O
of	O
ptrace	B-Operating_System
and	O
the	O
Mach	B-Operating_System
VM	O
and	O
thread	O
APIs	O
.	O
</s>
<s>
ptrace	B-Operating_System
(	O
again	O
with	O
PT_DENY_ATTACH	O
)	O
is	O
available	O
to	O
developers	O
for	O
the	O
Apple	O
iPhone	B-Device
.	O
</s>
<s>
Linux	B-Application
also	O
gives	O
processes	O
the	O
ability	O
to	O
prevent	O
other	O
processes	O
from	O
attaching	O
to	O
them	O
.	O
</s>
<s>
Processes	O
can	O
call	O
the	O
prctl	O
syscall	B-Operating_System
and	O
clear	O
their	O
PR_SET_DUMPABLE	O
flag	O
;	O
in	O
later	O
kernels	O
this	O
prevents	O
non-root	O
processes	O
from	O
ptracing	O
the	O
calling	O
process	B-Operating_System
;	O
the	O
OpenSSH	B-Language
authentication	O
agent	O
uses	O
this	O
mechanism	O
to	O
prevent	O
ssh	O
session	O
hijacking	O
via	O
ptrace	B-Operating_System
.	O
</s>
<s>
Later	O
Ubuntu	O
versions	O
ship	O
with	O
a	O
Linux	B-Application
kernel	O
configured	O
to	O
prevent	O
ptrace	B-Operating_System
attaches	O
from	O
processes	O
other	O
than	O
the	O
traced	O
process	B-Operating_System
 '	O
parent	O
;	O
this	O
allows	O
gdb	B-Language
and	O
strace	B-Language
to	O
continue	O
to	O
work	O
when	O
running	O
a	O
target	O
process	B-Operating_System
,	O
but	O
prevents	O
them	O
from	O
attaching	O
to	O
an	O
unrelated	O
running	O
process	B-Operating_System
.	O
</s>
<s>
Control	O
of	O
this	O
feature	O
is	O
performed	O
via	O
the	O
/proc/sys/kernel/yama/ptrace_scope	O
setting	O
.	O
</s>
<s>
On	O
systems	O
where	O
this	O
feature	O
is	O
enabled	O
,	O
commands	O
like	O
"	O
gdb	B-Language
--attach	O
"	O
and	O
"	O
strace	B-Language
-p	O
"	O
will	O
not	O
work	O
.	O
</s>
<s>
Starting	O
in	O
Ubuntu	O
10.10	O
,	O
ptrace	B-Operating_System
is	O
only	O
allowed	O
to	O
be	O
called	O
on	O
child	O
processes	O
.	O
</s>
<s>
For	O
some	O
Android	B-Application
phones	I-Application
with	O
a	O
locked	O
boot	O
loader	B-Operating_System
,	O
ptrace	B-Operating_System
is	O
used	O
to	O
gain	O
control	O
over	O
the	O
init	B-Operating_System
process	B-Operating_System
to	O
enable	O
a	O
'	O
2nd	O
boot	O
 '	O
and	O
replace	O
the	O
system	O
files	O
.	O
</s>
