Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Debugger.h"
- #include <libgen.h>
- #include <string.h>
- #include <sys/reg.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/syscall.h>
- #include <stdlib.h>
- #include <sys/user.h>
- #include <iostream>
- #include "DebuggerListener.h"
- #define CLEARBIT(x, b) x &= ~b
- #define TESTBIT(x, b) x & b
- #define SETBIT(x, b) x |= b
- Debugger::Debugger(char *execName, bool prompt, int flag): inside(false), flag(flag), prompt(prompt)
- {
- this->execName = (char*)malloc(sizeof(char)*strlen(execName));
- strcpy(this->execName, execName);
- pathToExec = this->execName;
- execName = basename(this->execName);
- }
- void Debugger::DebugInfo(bool state, int type, pid_t pid)
- {
- user_regs_struct regs;
- ptrace(PTRACE_GETREGS, pid,
- NULL, ®s);
- //long int eax;
- //eax = ptrace(PTRACE_PEEKUSER,
- // pid, 4 * EAX,
- // NULL);
- switch(type)
- {
- case DebuggerListener::EXEC:
- {
- //if(!state)
- printf("Exec called with parameters: %ld.\n", regs.ebx);
- //else
- // printf("Exec returned: %ld.\n", eax);
- break;
- }
- case DebuggerListener::FORK:
- {
- if(!state)
- printf("Fork called.\n");
- else
- {
- printf("Fork returned: %ld\n", regs.eax);
- }
- break;
- }
- case DebuggerListener::WAIT4:
- {
- if(!state)
- printf("Wait4 called with parameters: %ld - %ld - %ld.\n", regs.ebx, regs.ecx, regs.edx);
- else
- printf("Wait4 returned: %ld.\n", regs.eax);
- break;
- }
- case DebuggerListener::KILL:
- {
- if(!state)
- printf("Kill called with parameters: %ld - %ld.\n", regs.ebx, regs.ecx);
- else
- printf("Kill returned: %ld.\n", regs.eax);
- break;
- }
- case DebuggerListener::OPEN:
- {
- if(!state)
- printf("Open called with parameters: %ld - %ld - %ld.\n", regs.ebx, regs.ecx, regs.edx);
- //std::cout << "Open called with parameters:" << regs.ebx << " - " << regs.ecx << " - " << regs.edx << std::endl;
- else
- printf("Open returned: %ld.\n", regs.eax);
- break;
- }
- case DebuggerListener::CLOSE:
- {
- if(!state)
- printf("Close called with parameters: %ld.\n", regs.ebx);
- else
- printf("Close returned: %ld.\n", regs.eax);
- break;
- }
- case DebuggerListener::WRITE:
- {
- if(!state)
- printf("Write called with parameters: %ld - %ld - %ld.\n", regs.ebx, regs.ecx, regs.edx);
- else
- printf("Write returned: %ld.\n", regs.eax);
- break;
- }
- case DebuggerListener::READ:
- {
- if(!state)
- printf("Read called with parameters: %ld - %ld - %ld.\n", regs.ebx, regs.ecx, regs.edx);
- else
- printf("Read returned: %ld.\n", regs.eax);
- break;
- }
- }
- }
- void Debugger::startDebugging()
- {
- pid_t child;
- long int orig_eax;
- int status;
- child = fork();
- if(child == 0)
- {
- ptrace(PTRACE_TRACEME, 0, NULL, NULL);
- if(execl(pathToExec, execName, NULL))
- printf("Exec failed.\n");
- }
- else
- {
- while(1)
- {
- wait(&status);
- if(WIFEXITED(status) || WIFSIGNALED(status))
- break;
- orig_eax = ptrace(PTRACE_PEEKUSER,
- child, 4 * ORIG_EAX,
- NULL);
- int type = -1;
- if(TESTBIT(flag, PROC))
- {
- if(orig_eax == SYS_execve)
- {
- inside = true;
- type = DebuggerListener::EXEC;
- }
- else if(orig_eax == SYS_fork)
- {
- type = DebuggerListener::FORK;
- }
- else if(orig_eax == SYS_wait4)
- {
- type = DebuggerListener::WAIT4;
- }
- else if(orig_eax == SYS_kill)
- {
- type = DebuggerListener::KILL;
- }
- }
- if(TESTBIT(flag, FILE))
- {
- if(orig_eax == SYS_open)
- {
- type = DebuggerListener::OPEN;
- }
- else if(orig_eax == SYS_close)
- {
- type = DebuggerListener::CLOSE;
- }
- else if(orig_eax == SYS_write)
- {
- type = DebuggerListener::WRITE;
- }
- else if(orig_eax == SYS_read)
- {
- type = DebuggerListener::READ;
- }
- }
- if(type != -1)
- {
- DebugInfo(inside, type, child);
- inside = !inside;
- if(prompt)
- {
- char answer;
- printf("Do you want to keep tracking the proccess?[y/n]: ");
- scanf("%c", &answer);
- printf("\n");
- if(answer == 'n')
- {
- kill(child, SIGTERM);
- break;
- }
- }
- }
- ptrace(PTRACE_SYSCALL, child, NULL, NULL);
- }
- }
- }
- Debugger::~Debugger()
- {
- free(pathToExec);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement