Currently, for a project, I need to code some kind of debugger using ptrace(). At the end, it should show every function/syscall entered/exited in the program to trace.
Right now, I'm pretty stuck. I made a small program that should try to trace a given program, and print if it finds a call or a syscall based on the opcode (retreived with the registers). Here it is:
#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>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t child;
const int long_size = sizeof(long);
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("./bin", "bin", NULL);
} else {
int status;
unsigned ins;
struct user_regs_struct regs;
unsigned char prim, sec;
while (1) {
wait(&status);
if (WIFEXITED(status))
break;
ptrace(PTRACE_GETREGS, child, NULL, ®s);
ins = ptrace(PTRACE_PEEKTEXT, child, regs.rip, NULL);
prim = (unsigned)0xFF & ins;
sec = ((unsigned)0xFF00 & ins) >> 8;
if (prim == 0xE8 && sec == 0xCD)
printf("call found!\n");
if (prim == 0x80 && sec == 0xCD)
printf("syscall found!\n");
ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
}
}
return 0;
}
And here's the code for the "bin" binary :
#include <unistd.h>
void toto()
{
write(1, "hello\n", 6);
}
int main()
{
toto();
toto();
return (1);
}
When I'm looking the output of my mini debugger, it seems to find only one syscall and one call... I tried messing with the registers and the offset, but every tutorial I found on internet seems to be for a 32bits machine, which won't work in my case :/
Can someone give me a small hint to help me continue?
Thanks and have a nice day !