#include<sys/ptrace.h>#include<sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<sys/reg.h>intmain(){pid_tchild;longorig_rax;child=fork();if(child==0){ptrace(PTRACE_TRACEME,0,NULL,NULL);execl("/bin/ls","ls",NULL);}else{wait(NULL);orig_rax=ptrace(PTRACE_PEEKUSER,child,8*ORIG_RAX,NULL);printf("The child made a ""system call %ld\n",orig_rax);ptrace(PTRACE_CONT,child,NULL,NULL);}return0;}
#include<sys/ptrace.h>#include<sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<sys/reg.h>#include<sys/syscall.h>#include<sys/user.h>#include<stdio.h>intmain(){pid_tchild;longorig_rax,rax;longparams[3];intstatus;intinsyscall=0;structuser_regs_structregs;child=fork();if(child==0){ptrace(PTRACE_TRACEME,0,NULL,NULL);execl("/bin/ls","ls",NULL);}else{while(1){wait(&status);if(WIFEXITED(status))break;orig_rax=ptrace(PTRACE_PEEKUSER,child,8*ORIG_RAX,NULL);if(orig_rax==SYS_write){if(insyscall==0){/* Syscall entry */insyscall=1;ptrace(PTRACE_GETREGS,child,NULL,®s);printf("Write called with ""%ld, %ld, %ld\n",regs.rdi,regs.rsi,regs.rdx);}else{/* Syscall exit */rax=ptrace(PTRACE_PEEKUSER,child,8*RAX,NULL);printf("Write returned ""with %ld\n",rax);insyscall=0;}}ptrace(PTRACE_SYSCALL,child,NULL,NULL);}}return0;}
Write called with 1, 9348640, 99
a.out foo.c libbar.so libnice.a mod1.cpp nice.cpp rtsched test_reg.s wrapjack
Write returned with 99
Write called with 1, 9348640, 103
bar.c foo.map libdemo.a libtom.so mod1.o nice.o rtsched.cpp tlpi-dist wrapjack2
Write returned with 103
// trace_counter
#include<stdio.h>#include<stdlib.h>#include<sys/ptrace.h>#include<sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<sys/reg.h>#include<sys/user.h>#include<sys/syscall.h>#include<string.h>intlong_size=sizeof(long);voidgetdata(pid_tpid,longaddr,char*str,intlen){char*laddr;inti,j;unionu{longval;charchars[long_size];}data;i=0;j=len/long_size;laddr=str;while(i<j){data.val=ptrace(PTRACE_PEEKDATA,pid,addr+i*8,NULL);memcpy(laddr,data.chars,long_size);++i;laddr+=long_size;}j=len%long_size;if(j!=0){data.val=ptrace(PTRACE_PEEKDATA,pid,addr+i*8,NULL);memcpy(laddr,data.chars,j);}str[len]='\0';}intmain(intargc,char*argv[]){longorig_rax,rax;intstatus;intinsyscall=0;pid_ttraced_process;structuser_regs_structregs;longins;if(argc!=2){printf("Usage: %s <pid to be traced>\n",argv[0]);exit(1);}traced_process=atoi(argv[1]);ptrace(PTRACE_ATTACH,traced_process,NULL,NULL);while(1){wait(&status);if(WIFEXITED(status))break;orig_rax=ptrace(PTRACE_PEEKUSER,traced_process,8*ORIG_RAX,NULL);if(orig_rax==SYS_write){if(insyscall==0){/* Syscall entry */insyscall=1;ptrace(PTRACE_GETREGS,traced_process,NULL,®s);printf("Write called with %lld, %lld, %lld\n",regs.rdi,regs.rsi,regs.rdx);char*str=(char*)calloc((regs.rdx+1),sizeof(char));getdata(traced_process,regs.rsi,str,regs.rdx);printf("write string is : %s",str);}else{/* Syscall exit */rax=ptrace(PTRACE_PEEKUSER,traced_process,8*RAX,NULL);printf("Write returned with %ld\n",rax);insyscall=0;}}ptrace(PTRACE_SYSCALL,traced_process,NULL,NULL);}ptrace(PTRACE_DETACH,traced_process,NULL,NULL);return0;}
Write called with 1, 28184592, 11write string is : Counter: 7Write returned with 11Write called with 1, 28184592, 11write string is : Counter: 8Write returned with 11Write called with 1, 28184592, 11write string is : Counter: 9Write returned with 11Write called with 1, 28184592, 12write string is : Counter: 10Write returned with 12...
#include<stdio.h>#include<stdlib.h>#include<sys/ptrace.h>#include<sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<sys/reg.h>#include<sys/user.h>#include<sys/syscall.h>#include<string.h>constintlong_size=sizeof(long);voidgetdata(pid_tpid,longaddr,char*str,intlen){char*laddr;inti,j;unionu{longval;charchars[long_size];}data;i=0;j=len/long_size;laddr=str;while(i<j){data.val=ptrace(PTRACE_PEEKDATA,pid,addr+i*long_size,NULL);memcpy(laddr,data.chars,long_size);++i;laddr+=long_size;}j=len%long_size;if(j!=0){data.val=ptrace(PTRACE_PEEKDATA,pid,addr+i*long_size,NULL);memcpy(laddr,data.chars,j);}str[len]='\0';}voidputdata(pid_tchild,longaddr,char*str,intlen){char*laddr;inti,j;unionu{longval;charchars[long_size];}data;i=0;j=len/long_size;laddr=str;while(i<j){memcpy(data.chars,laddr,long_size);ptrace(PTRACE_POKEDATA,child,addr+i*long_size,data.val);++i;laddr+=long_size;}j=len%long_size;if(j!=0){memcpy(data.chars,laddr,j);ptrace(PTRACE_POKEDATA,child,addr+i*long_size,data.val);}}intmain(intargc,char*argv[]){pid_ttraced_process;structuser_regs_structregs,newregs;longins;/* int 0x80, int3 */charcode[]={0xcd,0x80,0xcc,0};charbackup[long_size];if(argc!=2){printf("Usage: %s <pid to be traced>\n",argv[0]);exit(1);}traced_process=atoi(argv[1]);ptrace(PTRACE_ATTACH,traced_process,NULL,NULL);wait(NULL);ptrace(PTRACE_GETREGS,traced_process,NULL,®s);/* Copy instructions into a backup variable */getdata(traced_process,regs.rip,backup,long_size);/* Put the breakpoint */putdata(traced_process,regs.rip,code,3);/* Let the process continue and execute
the int 3 instruction */ptrace(PTRACE_CONT,traced_process,NULL,NULL);wait(NULL);printf("The process stopped, putting back ""the original instructions\n");printf("Press <enter> to continue\n");getchar();putdata(traced_process,regs.rip,backup,long_size);/* Setting the eip back to the original
instruction to let the process continue */ptrace(PTRACE_SETREGS,traced_process,NULL,®s);ptrace(PTRACE_DETACH,traced_process,NULL,NULL);return0;}