ptrace.2

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 2 代码 · 共 281 行

2
281
字号
.TH ptrace 2 RISC.SH Nameptrace \- process trace.SH Syntax.nf.ft B#include <signal.h>#include <sys/ptrace.h>.PP.ft Bptrace(request, pid, addr, data)int request, pid, *addr, data;.fi.SH DescriptionThe system call.PN ptraceprovides a means by which a processcan control the execution of another process,and examine and change its core image.Its primary use is for the implementation of breakpoint debugging.There are four arguments whose interpretationdepends on a.I requestargument.Generally,.I pidis the process ID of the traced process.A process being tracedbehaves normally until it encounters some signal,whether internally generated,like \*(lqillegal instruction,\*(rq or externallygenerated, like \*(lqinterrupt.\*(rqFor more information, see.MS sigvec 2 ..PPUpon encountering a signal, the traced process enters a stopped stateand its tracing process is notified by means of .PN wait .If the traced process stops with a SIGTRAP, the process might have beenstopped for a number of reasons.Two status words addressable as registersin the traced process's uarea qualify SIGTRAPs:TRAPCAUSE, which contains the cause of the trap, and TRAPINFO, whichcontains extra information concerning the trap..PPWhen the traced process is in the stopped state,its core image can be examined and modifiedusing.PN ptrace .If desired, another.PN ptracerequest can then cause the traced process either to terminateor to continue, possibly ignoring the signal..PPThe value of the.I requestargument determines the preciseaction of the call:.TP 40This request is the only one that can be used by a child process.The child process can declare that it is to be traced by its parent.All other arguments are ignored.Unexpected results occurif the parent process does not expect to trace the child process..TP 41,2Theword in the traced process's address spaceat.I addris returned.If I and D space are separated (for example, historicallyon a PDP-11), request 1 indicates I space,request 2 indicates D space.  The.I addrmust be 4-byte aligned.The traced process must be stopped.The input.I datais ignored..TP 43The wordof the system's per-process data area corresponding to.I addris returned.The.I addris a constant defined in ptrace.h.This space contains the registers and other information aboutthe process;the constants correspond to fields in the.I userstructure in the system..TP 44,5Thegiven.I datais written at the word in the process's address space corresponding to.I addr,which must be 4-byte aligned.The old value at the address is returned.If I and D space are separated, request 4 indicates I space, request 5 indicates D space.Attempts to write in pure procedurefail if another process is executing the same file..TP 46The process's system data is written,as it is read with request 3.Only a few locations can be written in this way:the general registers,the floating point status and registers,and certain bits of the processor status word.The old value at the address is returned..TP 47The.I dataargument is taken as a signal numberand the traced process's execution continuesat location.I addras if it had incurred that signal.Normally, the signal number is 0 to indicate that the signal causing the stop should be ignored. The signal number might be the value fetched out of theprocess's image, which identifies the signal that causedthe stop.If.I addris (int *)1, execution continues from where it stopped..TP 48The traced process terminates..TP 49Execution continues as in request 7;however, as soon as possible after execution of at least one instruction,execution stops again.The signal number from the stop isSIGTRAP.TRAPCAUSE contains CAUSESINGLE.This is part of the mechanism for implementing breakpoints..TP 420This is the same as zero, except it is executed by the tracing processand the pid field is nonzero.  The process with that pid stopsand becomes a traced process. On a signal, the traced process returns control to the tracing process, rather than the parent.  The tracing process must have the same uidas the traced process..TP 421,22Returns MAXREG general or MAXFREG floating registers, respectively.  Theirvalues are copied to the locations starting at the address in thetracing process specified by the.I addrargument..TP 424,25Same as 20 and 21, but writes the registers instead of reading them..TP 426Specifies a watchpoint in the data or stack segment of the tracedprocess.  If any byte address starting at the.I addrargument and continuing for the number of bytes specified by the.I dataargument is accessed in an instruction, the traced process stopsexecution with a SIGTRAP.  TRAPCAUSE contains CAUSEWATCH, andTRAPINFO contains the address causing the trap.  This ptracereturns a watchpoint identifier (wid).  MAXWIDS specifies themaximum number of watchpoints for each process..TP 427The data argument specifies a wid to delete..TP 428Turns off the tracing for the traced process with the specified pid..TP 429Returns an open file descriptor for the file attached to .I pid.This request is useful in accessing the symbol table of a processcreated with the.PN execve call. .PPAs indicated,these calls(except for request 0 and 20)can be used only when the subject process has stopped.The .I waitcall is used to determinewhen a process stops;in such a case, the \*(lqtermination\*(rq statusreturned by.I waithas the value 0177. This value indicates that the process has stopped, ratherthan terminated.  If multiple processes are being traced, .PN wait can be called multiple times,and it returns the status for the next stopped or terminatedchild or traced process..PPTo forestall possible fraud,.PN ptraceinhibits the set-user-id and set-group-id facilitieson subsequent.MS exec 2calls.If a traced process calls.PN execve ,it stops before executing the first instruction of the new imageshowing signal SIGTRAP. In this case, TRAPCAUSE contains CAUSEEXEC and TRAPINFO does not contain anything interesting.If a traced process calls.PN execveagain, the same thing occurs..PPIf a traced process forks, both parent and child are traced.Breakpoints from the parent are not copied into the child.At the time of the fork, thechild is stopped with a SIGTRAP. The tracing process can then terminatethe trace if desired.TRAPCAUSE contains CAUSEFORK and TRAPINFO contains the pid of its parent..PP.SH RestrictionsOn an ULTRIX system, the .PN ptracesystem call succeeds only if the user owns the binary being tracedor if the user is root. .PPThe request 0 call should be able to specifysignals that are to be treated normally and not cause a stop.In this way, for example,programs with simulated floating point, whichuse \*(lqillegal instruction\*(rq signals at a very high rate,can be efficiently debugged..PPThe error indication, \-1, is a legitimate function value; when an erroroccurs, the errno variable is set to explain the condition that caused theerror..PP  It should be possible to stop a process on occurrence of a systemcall;in this way, a completely controlled environment couldbe provided..SH Return ValuesA 0 value is returned if the call succeeds.  If the call fails,a \-1 is returned, and the global variable \fIerrno\fP isset to indicate the error..SH DiagnosticsThe.PN ptracecall fails under the following conditions:.TP 15[EIO]The request code is invalid..TP 15[ESRCH]The specified process does not exist..TP 15[EIO]          The given signal number is invalid..TP 15[EIO]The specified address is out of bounds..TP 15[EPERM]The specified process cannot be traced..SH See Alsodbx(1), wait(2), sigvec(2)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?