c - Why does ptrace not recognize the function? -
i can't figure out why function returns "no such process" error message every time run it, using same instructions inline produces required output.
#include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/user.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> void getregs(pid_t proc, struct user_regs_struct *regs); int main() { pid_t proc = fork(); if(proc == 0) { if(ptrace(ptrace_traceme, 0, null, null) == -1) { perror("traceme"); exit(0); } if(execl("child", "child", null) == -1) { perror("execl"); exit(0); } } else { wait(&proc); struct user_regs_struct regs; ptrace(ptrace_getregs, proc, null, ®s); printf("eax: %08x\n", (unsigned int)regs.eax); getregs(proc, ®s); ptrace(ptrace_cont, proc, null, null); } return 0; } void getregs(pid_t proc, struct user_regs_struct *regs) { if(ptrace(ptrace_getregs, proc, null, ®s) == -1) { perror("getregs"); exit(1); } printf("eax: %08x\n", (unsigned int)regs->eax); }
when run get
~$ ./tracer eax: 0000002f getregs: no such process
i don't why getregs()
returns error. it's outside scope of something?
also, little unrelated: eax set 0000002f
no matter process try execl()
. natural? don't know if i'm forking child process or not. need make new question on this?
you hit error because modifying value of process identifier (pid) contained in variable proc
passing address wait(2)
syscall.
the wait
syscall change value of proc
return status of child process upon termination. when reference child process in ptrace
using proc
, value invalid , referencing no existing processes.
and @lornix noticed, make sure pass right pointer ptrace
in getregs
function.
Comments
Post a Comment