I'm trying to read the register values of a running process. This is the code of the process I'm trying to trace:
#include <stdio.h>
int x=0;
int main(){
while(1){
x++;
printf("%d\n",x);
}
return 0;
}
and this is the ptrace program that I'm using to do it:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/wait.h>
void dumpRegs(int pid){
printf("------------------------\n");
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS,pid,NULL, ®s);
printf("rbx: %lx.\n", regs.rbx);
printf("r15: %lx.\n",regs.r15);
printf("r14: %lx.\n",regs.r14);
printf("r13: %lx.\n",regs.r13);
printf("r12: %lx.\n",regs.r12);
printf("rbp: %lx.\n",regs.rbp);
printf("rbx: %lx.\n",regs.rbx);
printf("r11: %lx.\n",regs.r11);
printf("r10: %lx.\n",regs.r10);
printf("r9: %lx.\n",regs.r9);
printf("r8: %lx.\n",regs.r8);
printf("rax: %lx.\n",regs.rax);
printf("rcx: %lx.\n",regs.rcx);
printf("rdx: %lx.\n",regs.rdx);
printf("rsi: %lx.\n",regs.rsi);
printf("rdi: %lx.\n",regs.rdi);
printf("orig_rax: %lx.\n",regs.orig_rax);
printf("rip: %lx.\n",regs.rip);
printf("cs: %lx.\n",regs.cs);
printf("eflags: %lx.\n",regs.eflags);
printf("rsp: %lx.\n",regs.rsp);
printf("ss: %lx.\n",regs.ss);
printf("fs_base: %lx.\n",regs.fs_base);
printf("gs_base: %lx.\n",regs.gs_base);
printf("ds: %lx.\n",regs.ds);
printf("es: %lx.\n",regs.es);
printf("fs: %lx.\n",regs.fs);
printf("gs: %lx.\n",regs.gs);
}
int main(int argc, char **argv){
printf("begin\n");
int pid=atoi(argv[1]);//the pid of the process
int status;
ptrace(PTRACE_ATTACH,pid);//attach to process
waitpid(pid,&status,0);
do{
ptrace(PTRACE_SINGLESTEP,pid);
dumpRegs(pid);
}while(getchar()!='q');
ptrace(PTRACE_DETACH,pid);//attach to process
printf("end\n");
return 0;
}
The code attaches to the process just fine, I can tell because it stops printing the value of x, and I can also see it in top and htop, but no matter how many iterations of the do...while loop it goes through, the output of dumpRegs stays the same. I would think that at least some of the registers would change their values even in such a simple process, so I think I must be doing something wrong. I've posted a sample of the output I'm getting below.
output:
rbx: 9.
r15: 9.
r14: 7ff23e124600.
r13: 1.
r12: 9.
rbp: 559d2ee03010.
rbx: 9.
r11: 246.
r10: 64.
r9: 8.
r8: 1.
rax: fffffffffffffe00.
rcx: 7ff23de65730.
rdx: 9.
rsi: 559d2ee03010.
rdi: 1.
orig_rax: 1.
rip: 7ff23de65730.
cs: 33.
eflags: 246.
rsp: 7ffcee0ce6f8.
ss: 2b.
fs_base: 7ff23e318700.
gs_base: 0.
ds: 0.
es: 0.
fs: 0.
gs: 0.
I'm running this on 64 bit Debian and compiling with gcc.