[9] | 1 | .\" Copyright (c) 1980 Regents of the University of California.
|
---|
| 2 | .\" All rights reserved. The Berkeley software License Agreement
|
---|
| 3 | .\" specifies the terms and conditions for redistribution.
|
---|
| 4 | .\"
|
---|
| 5 | .\" @(#)ptrace.2 6.4 (Berkeley) 5/23/86
|
---|
| 6 | .\"
|
---|
| 7 | .TH PTRACE 2 "May 23, 1986"
|
---|
| 8 | .UC 4
|
---|
| 9 | .SH NAME
|
---|
| 10 | ptrace \- process trace
|
---|
| 11 | .SH SYNOPSIS
|
---|
| 12 | .nf
|
---|
| 13 | .ft B
|
---|
| 14 | #include <sys/types.h>
|
---|
| 15 | #include <sys/signal.h>
|
---|
| 16 | #include <sys/ptrace.h>
|
---|
| 17 |
|
---|
| 18 | int ptrace(int \fIrequest\fP, pid_t \fIpid\fP, long \fIaddr\fP, long \fIdata\fP)
|
---|
| 19 | .ft R
|
---|
| 20 | .fi
|
---|
| 21 | .SH DESCRIPTION
|
---|
| 22 | .ft B
|
---|
| 23 | Note: This manual page has no relation to MINIX 3. Someone who knows ptrace()
|
---|
| 24 | has to check, or rewrite, this page. (kjb)
|
---|
| 25 | .ft R
|
---|
| 26 | .PP
|
---|
| 27 | .B Ptrace
|
---|
| 28 | provides a means by which a parent process
|
---|
| 29 | may control the execution of a child process,
|
---|
| 30 | and examine and change its core image.
|
---|
| 31 | Its primary use is for the implementation of breakpoint debugging.
|
---|
| 32 | There are four arguments whose interpretation
|
---|
| 33 | depends on a
|
---|
| 34 | .I request
|
---|
| 35 | argument.
|
---|
| 36 | Generally,
|
---|
| 37 | .I pid
|
---|
| 38 | is the process ID of the traced process,
|
---|
| 39 | which must be a child (no more distant descendant)
|
---|
| 40 | of the tracing process.
|
---|
| 41 | A process being traced
|
---|
| 42 | behaves normally until it encounters some signal
|
---|
| 43 | whether internally generated
|
---|
| 44 | like \*(lqillegal instruction\*(rq or externally
|
---|
| 45 | generated like \*(lqinterrupt\*(rq.
|
---|
| 46 | See
|
---|
| 47 | .BR sigaction (2)
|
---|
| 48 | for the list.
|
---|
| 49 | Then the traced process enters a stopped state
|
---|
| 50 | and its parent is notified via
|
---|
| 51 | .BR wait (2).
|
---|
| 52 | When the child is in the stopped state,
|
---|
| 53 | its core image can be examined and modified
|
---|
| 54 | using
|
---|
| 55 | .BR ptrace .
|
---|
| 56 | If desired, another
|
---|
| 57 | .B ptrace
|
---|
| 58 | request can then cause the child either to terminate
|
---|
| 59 | or to continue, possibly ignoring the signal.
|
---|
| 60 | .PP
|
---|
| 61 | The value of the
|
---|
| 62 | .I request
|
---|
| 63 | argument determines the precise
|
---|
| 64 | action of the call:
|
---|
| 65 | .TP 4
|
---|
| 66 | PT_TRACE_ME
|
---|
| 67 | This request is the only one used by the child process;
|
---|
| 68 | it declares that the process is to be traced by its parent.
|
---|
| 69 | All the other arguments are ignored.
|
---|
| 70 | Peculiar results will ensue
|
---|
| 71 | if the parent does not expect to trace the child.
|
---|
| 72 | .TP 4
|
---|
| 73 | PT_READ_I, PT_READ_D
|
---|
| 74 | The
|
---|
| 75 | word in the child process's address space
|
---|
| 76 | at
|
---|
| 77 | .I addr
|
---|
| 78 | is returned.
|
---|
| 79 | If I and D space are separated (e.g. historically
|
---|
| 80 | on a pdp-11), request PT_READ_I indicates I space,
|
---|
| 81 | PT_READ_D D space.
|
---|
| 82 | .I Addr
|
---|
| 83 | must be even on some machines.
|
---|
| 84 | The child must be stopped.
|
---|
| 85 | The input
|
---|
| 86 | .I data
|
---|
| 87 | is ignored.
|
---|
| 88 | .TP 4
|
---|
| 89 | PT_READ_U
|
---|
| 90 | The word
|
---|
| 91 | of the system's per-process data area corresponding to
|
---|
| 92 | .I addr
|
---|
| 93 | is returned.
|
---|
| 94 | .I Addr
|
---|
| 95 | must be even on some machines and less than 512.
|
---|
| 96 | This space contains the registers and other information about
|
---|
| 97 | the process;
|
---|
| 98 | its layout corresponds to the
|
---|
| 99 | .I user
|
---|
| 100 | structure in the system.
|
---|
| 101 | .TP 4
|
---|
| 102 | PT_WRITE_I, PT_WRITE_D
|
---|
| 103 | The
|
---|
| 104 | given
|
---|
| 105 | .I data
|
---|
| 106 | is written at the word in the process's address space corresponding to
|
---|
| 107 | .I addr,
|
---|
| 108 | which must be even on some machines.
|
---|
| 109 | No useful value is returned.
|
---|
| 110 | If I and D space are separated, request PT_WRITE_I indicates I space,
|
---|
| 111 | PT_WRITE_D D space.
|
---|
| 112 | Attempts to write in pure procedure
|
---|
| 113 | fail if another process is executing the same file.
|
---|
| 114 | .TP 4
|
---|
| 115 | PT_WRITE_U
|
---|
| 116 | The process's system data is written,
|
---|
| 117 | as it is read with request PT_READ_U.
|
---|
| 118 | Only a few locations can be written in this way:
|
---|
| 119 | the general registers,
|
---|
| 120 | the floating point status and registers,
|
---|
| 121 | and certain bits of the processor status word.
|
---|
| 122 | .TP 4
|
---|
| 123 | PT_CONTINUE
|
---|
| 124 | The
|
---|
| 125 | .I data
|
---|
| 126 | argument is taken as a signal number
|
---|
| 127 | and the child's execution continues
|
---|
| 128 | at location
|
---|
| 129 | .I addr
|
---|
| 130 | as if it had incurred that signal.
|
---|
| 131 | Normally the signal number will be
|
---|
| 132 | either 0 to indicate that the signal that caused the stop
|
---|
| 133 | should be ignored,
|
---|
| 134 | or that value fetched out of the
|
---|
| 135 | process's image indicating which signal caused
|
---|
| 136 | the stop.
|
---|
| 137 | If
|
---|
| 138 | .I addr
|
---|
| 139 | is (int *)1 then execution continues from where it stopped.
|
---|
| 140 | .TP 4
|
---|
| 141 | PT_KILL
|
---|
| 142 | The traced process terminates.
|
---|
| 143 | .TP 4
|
---|
| 144 | PT_STEP
|
---|
| 145 | Execution continues as in request PT_CONTINUE;
|
---|
| 146 | however, as soon as possible after execution of at least one instruction,
|
---|
| 147 | execution stops again.
|
---|
| 148 | The signal number from the stop is
|
---|
| 149 | SIGTRAP.
|
---|
| 150 | (On the VAX-11 the T-bit is used and just one instruction
|
---|
| 151 | is executed.)
|
---|
| 152 | This is part of the mechanism for implementing breakpoints.
|
---|
| 153 | .PP
|
---|
| 154 | As indicated,
|
---|
| 155 | these calls
|
---|
| 156 | (except for request PT_TRACE_ME)
|
---|
| 157 | can be used only when the subject process has stopped.
|
---|
| 158 | The
|
---|
| 159 | .B wait
|
---|
| 160 | call is used to determine
|
---|
| 161 | when a process stops;
|
---|
| 162 | in such a case the \*(lqtermination\*(rq status
|
---|
| 163 | returned by
|
---|
| 164 | .B wait
|
---|
| 165 | has the value 0177 to indicate stoppage rather
|
---|
| 166 | than genuine termination.
|
---|
| 167 | .PP
|
---|
| 168 | To forestall possible fraud,
|
---|
| 169 | .B ptrace
|
---|
| 170 | inhibits the set-user-id and set-group-id facilities
|
---|
| 171 | on subsequent
|
---|
| 172 | .BR execve (2)
|
---|
| 173 | calls.
|
---|
| 174 | If a traced process calls
|
---|
| 175 | .BR execve ,
|
---|
| 176 | it will stop before executing the first instruction of the new image
|
---|
| 177 | showing signal SIGTRAP.
|
---|
| 178 | .PP
|
---|
| 179 | On a VAX-11, \*(lqword\*(rq also means a 32-bit integer,
|
---|
| 180 | but the \*(lqeven\*(rq
|
---|
| 181 | restriction does not apply.
|
---|
| 182 | .SH "RETURN VALUE
|
---|
| 183 | A 0 value is returned if the call succeeds. If the call fails
|
---|
| 184 | then a \-1 is returned and the global variable \fIerrno\fP is
|
---|
| 185 | set to indicate the error.
|
---|
| 186 | .SH "ERRORS
|
---|
| 187 | .TP 15
|
---|
| 188 | [EIO]
|
---|
| 189 | The request code is invalid.
|
---|
| 190 | .TP 15
|
---|
| 191 | [ESRCH]
|
---|
| 192 | The specified process does not exist.
|
---|
| 193 | .TP 15
|
---|
| 194 | [EIO]
|
---|
| 195 | The given signal number is invalid.
|
---|
| 196 | .TP 15
|
---|
| 197 | [EIO]
|
---|
| 198 | The specified address is out of bounds.
|
---|
| 199 | .TP 15
|
---|
| 200 | [EPERM]
|
---|
| 201 | The specified process cannot be traced.
|
---|
| 202 | .SH "SEE ALSO"
|
---|
| 203 | .BR wait (2),
|
---|
| 204 | .BR sigaction (2),
|
---|
| 205 | .BR mdb (1).
|
---|
| 206 | .SH BUGS
|
---|
| 207 | .B Ptrace
|
---|
| 208 | is unique and arcane; it should be replaced with a special file that
|
---|
| 209 | can be opened and read and written. The control functions could then
|
---|
| 210 | be implemented with
|
---|
| 211 | .BR ioctl (2)
|
---|
| 212 | calls on this file. This would be simpler to understand and have much
|
---|
| 213 | higher performance.
|
---|
| 214 | .PP
|
---|
| 215 | The request PT_TRACE_ME call should be able to specify
|
---|
| 216 | signals that are to be treated normally and not cause a stop.
|
---|
| 217 | In this way, for example,
|
---|
| 218 | programs with simulated floating point (which
|
---|
| 219 | use \*(lqillegal instruction\*(rq signals at a very high rate)
|
---|
| 220 | could be efficiently debugged.
|
---|
| 221 | .PP
|
---|
| 222 | The error indication, \-1, is a legitimate function value;
|
---|
| 223 | .BR errno ,
|
---|
| 224 | (see
|
---|
| 225 | .BR intro (2)),
|
---|
| 226 | can be used to disambiguate.
|
---|
| 227 | .PP
|
---|
| 228 | It should be possible to stop a process on occurrence of a system
|
---|
| 229 | call;
|
---|
| 230 | in this way a completely controlled environment could
|
---|
| 231 | be provided.
|
---|