Advertisement
Guest User

Ptrace

a guest
Mar 5th, 2012
2,469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/ptrace.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/user.h>
  8. #include <sys/syscall.h>
  9. #include <unistd.h>
  10.  
  11. int main()
  12. {   pid_t child;
  13.     const int long_size = sizeof(long);
  14.     child = fork();
  15.     if(child == 0) {
  16.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);
  17.         execl("./gen", "./gen", "abcd", "efgh", NULL);
  18.     }
  19.     else {
  20.         int status;
  21.         union u {
  22.             long val;
  23.             char chars[long_size];
  24.         }data;
  25.         struct user_regs_struct regs;
  26.         int start = 0;
  27.         long ins;
  28.         while(1) {
  29.             wait(&status);
  30.             if(WIFEXITED(status))
  31.                 break;
  32.             ptrace(PTRACE_GETREGS,
  33.                    child, NULL, &regs);
  34.  
  35.             ins = ptrace(PTRACE_PEEKTEXT,
  36.                              child, regs.rip,
  37.                              NULL);
  38.             printf("EIP: %lx Instruction "
  39.                   "executed: %lx\n",
  40.                   regs.rip, ins);
  41.  
  42.             ptrace(PTRACE_SINGLESTEP, child,
  43.                       NULL, NULL);
  44.         }
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement