Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From : http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1
- #include<stdio.h>
- #include<sys/reg.h>
- #include<sys/types.h>
- #include<sys/ptrace.h>
- #include<sys/wait.h>
- #include<unistd.h>
- #include<sys/user.h>
- #include<sys/syscall.h>
- void run_debugger(pid_t child_pid);
- void run_target(const char* programname);
- int main(int argc, char** argv)
- {
- pid_t child_pid;
- if (argc < 2) {
- printf("Expected a program name as argument\n");
- return -1;
- }
- child_pid = fork();
- if (child_pid == 0)
- run_target(argv[1]);
- else if (child_pid > 0)
- run_debugger(child_pid);
- else {
- printf("fork");
- return -1;
- }
- return 0;
- }
- void run_target(const char* programname)
- {
- printf("target started.#include<stdio.h>
- #include<sys/reg.h>
- #include<sys/types.h>
- #include<sys/ptrace.h>
- #include<sys/wait.h>
- #include<unistd.h>
- #include<sys/user.h>
- #include<sys/syscall.h>
- void run_debugger(pid_t child_pid);
- void run_target(const char* programname);
- int main(int argc, char** argv)
- {
- pid_t child_pid;
- if (argc < 2) {
- printf("Expected a program name as argument\n");
- return -1;
- }
- child_pid = fork();
- if (child_pid == 0)
- run_target(argv[1]);
- else if (child_pid > 0)
- run_debugger(child_pid);
- else {
- printf("fork");
- return -1;
- }
- return 0;
- }
- void run_target(const char* programname)
- {
- printf("target started.#include<stdio.h>
- #include<sys/reg.h>
- #include<sys/types.h>
- #include<sys/ptrace.h>
- #include<sys/wait.h>
- #include<unistd.h>
- #include<sys/user.h>
- #include<sys/syscall.h>
- void run_debugger(pid_t child_pid);
- void run_target(const char* programname);
- int main(int argc, char** argv)
- {
- pid_t child_pid;
- if (argc < 2) {
- printf("Expected a program name as argument\n");
- return -1;
- }
- child_pid = fork();
- if (child_pid == 0)
- run_target(argv[1]);
- else if (child_pid > 0)
- run_debugger(child_pid);
- else {
- printf("fork");
- return -1;
- }
- return 0;
- }
- void run_target(const char* programname)
- {
- printf("target started.will run '%s'\n", programname);
- /* Allow tracing of this process */
- if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
- printf("ptrace");
- return;
- }
- printf("replacing process image");
- /* Replace this process's image with the given program */
- execl(programname, programname, NULL);
- }
- void run_debugger(pid_t child_pid)
- {
- int wait_status;
- unsigned icounter = 0;
- printf("debugger started\n");
- /* Wait for child to stop on its first instruction */
- wait(&wait_status);
- while (WIFSTOPPED(wait_status)) {
- icounter++;
- struct user_regs_struct regs;
- ptrace(PTRACE_GETREGS, child_pid, 0, ®s);
- unsigned instr = ptrace(PTRACE_PEEKTEXT, child_pid, regs.rip, 0);
- /*printf("icounter = %u. EIP = 0x%08lld. instr = 0x%08x\n",
- icounter, regs.rip, instr);*/
- /* Make the child execute another instruction */
- if (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {
- printf("ptrace");
- return;
- }
- /* Wait for child to stop on its next instruction */
- wait(&wait_status);
- }
- printf("the child executed %u instructions\n", icounter);
- }
Add Comment
Please, Sign In to add comment