Skip to content

Commit 8889754

Browse files
committed
Refactor breakpoint class
1 parent b211067 commit 8889754

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

breakpoint.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ using namespace std;
77

88
class breakpoint{
99
public:
10-
uint64_t pid; uint64_t addr;
11-
uint64_t orig_data;
12-
breakpoint(){}
13-
breakpoint(uint64_t _pid, uint64_t _addr){pid = _pid; addr = _addr;}
14-
void set_breakpoint(){
15-
orig_data = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
10+
static int64_t set_breakpoint(int pid, uint64_t addr){
11+
uint64_t orig_data = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
1612
printf("Address : %lx Orig Data : %lx\n", addr, orig_data);
1713
uint64_t new_data = (orig_data&~0xFF)|0xCC; //Insert a int3 = 0xcc
1814
printf("New Data : %lx\n", new_data);
1915
ptrace(PTRACE_POKETEXT, pid, addr, new_data);
16+
return orig_data;
2017
}
2118

22-
void unset_breakpoint(uint64_t in_addr, uint64_t in_data)
19+
static void unset_breakpoint(uint64_t pid, uint64_t in_addr, uint64_t in_data)
2320
{
2421
//uint64_t data = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
2522
printf("New Data : %lx \n", in_data);
26-
uint64_t new_data = in_data; //Insert a int3 = 0xcc
23+
uint64_t new_data = in_data; //Insert a int3 = 0xcc
2724
printf("Restore Address : %ld Orig Data : %lx\n", in_addr, new_data);
28-
ptrace(PTRACE_POKETEXT, pid, in_addr, new_data);
25+
ptrace(PTRACE_POKETEXT, pid, in_addr, new_data);
2926
}
3027
};

debugger.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void run_target(string prog)
2929
}
3030

3131
map<uint64_t, uint64_t> originalInst;
32-
bool continue_execution(int pid, breakpoint* bp)
32+
bool continue_execution(int pid)
3333
{
3434
// Lets continue execution
3535
ptrace(PTRACE_CONT, pid, 0, 0);
@@ -48,15 +48,14 @@ bool continue_execution(int pid, breakpoint* bp)
4848
ptrace(PTRACE_SETREGS, pid, NULL, &regs);
4949

5050
// Replace the original instruction
51-
bp->unset_breakpoint(regs.rip, originalInst[regs.rip]);
51+
breakpoint::unset_breakpoint(pid, regs.rip, originalInst[regs.rip]);
5252
return true;
5353
}
5454

5555
void run_debugger(int pid)
5656
{
5757
wait(NULL);
5858

59-
breakpoint* bp = new breakpoint();
6059
while(1)
6160
{
6261
cout << "\n>";
@@ -67,14 +66,12 @@ void run_debugger(int pid)
6766

6867
if(iaddr == 1)
6968
{
70-
if(!continue_execution(pid, bp))return;
69+
if(!continue_execution(pid))return;
7170
}
7271
else
7372
{
74-
bp->pid = pid; bp->addr = iaddr;
75-
bp->set_breakpoint();
7673
// Save the original instruction data
77-
originalInst[bp->addr] = bp->orig_data;
74+
originalInst[iaddr] = breakpoint::set_breakpoint(pid, iaddr);
7875
}
7976
}
8077
}

0 commit comments

Comments
 (0)