HP-UX Reference (11i v2 07/12) - 2 System Calls (vol 5)
p
ptrace(2) ptrace(2)
(PA-RISC Only; TO BE OBSOLETED)
/*
* Wait for the traced child to stop after the exec system call in
* response to an exec event set in its ptrace_event structure.
*/
if (pid != (npid = wait(&status))) /* wait failed */
printf("Parent: wait() failed with wrong status\n");
if (!WIFSTOPPED(status))
printf("Parent: invalid wait() completion\n");
/*
* Child has stopped; fetch its process state and examine state
* information.
*/
if (ptrace(PT_GET_PROCESS_STATE, pid, state_addr, state_len) < 0)
printf("Parent: PT_GET_PROCESS_STATE ptrace request failed\n");
if (pid != wait(&status)) /* wait failed */
printf("Parent: wait() failed with wrong wait status\n");
/* Check if the pathlength value returned is non-zero */
if ((pathlength = state_addr->pe_path_len) == 0)
printf("Parent: zero length pathname returned\n");
/* Fetch exec’d file pathname and store it in the buffer. */
if (ptrace(PT_GET_PROCESS_PATHNAME, pid, buf_addr, (pathlength+1))
< 0){
printf("Parent: Failed to get exec pathname\n");
} else {
printf("Parent: the exec pathname is %s\n", buf_addr);
if (pid != wait(&status)) /* wait failed */
printf("Parent: wait() failed with wrong status\n");
}
}
main()
{
event_len = sizeof(ptrace_event_t);
state_len = sizeof(ptrace_state_t);
event_addr = calloc(event_len, 1);
state_addr = calloc(state_len, 1);
buf_addr = calloc(MAXPATH, 1);
pipe(filed);
switch (pid = fork()) {
case -1:
exit(1);
case 0:
child();
break;
default:
parent();
break;
}
}
ERRORS
If the ptrace() call fails, errno is set to one of the following values:
[EACCES] The executable image of the process being attached resides across an interruptible
NFS mount.
[EIO] Argument request contains an illegal number.
[EIO] The PT_SETTRC request is called with a data argument that is less than zero, not a
multiple of four or contains an address that is not word-aligned.
[EIO] Attempting to write to a memory segment of the traced process that is not writable,
attempting to write to page 0 or the request argument is out of range.
306 Hewlett-Packard Company − 7 − HP-UX 11i Version 2: December 2007 Update