Skip to content

Commit f065263

Browse files
authored
Update readme (#8)
1 parent 215c0a3 commit f065263

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ Create an instance of SimpleDebugger like so:
1313
SimpleDebugger *debugger = new SimpleDebugger();
1414
```
1515

16+
## Hook functions
17+
18+
Hook functions using the `hookFunction(void *originalFunc, void *newFunc)` method. The originalFunction must be at
19+
least 5 instructions long, if not you will get undefined behavior.
20+
21+
After the hook is added all calls to originalFunc will go to newFunc. Make sure the signature for newFunc exactly
22+
matches originalFunc. Once a hook is added it is active for the lifetime of the process. There is not a way to
23+
call the original function from the hooked function.
24+
1625
## Set breakpoints
1726

1827
Set breakpoints using the `setBreakpoint(vm_address_t address)` method. The provided address must be in the __TEXT/__text section (the memory region containing executable code).
@@ -50,6 +59,25 @@ __attribute__((constructor)) void setup() {
5059
}
5160
```
5261
62+
This example hooks the `gettimeofday` function:
63+
64+
```c++
65+
#include <SimpleDebugger.h>
66+
67+
SimpleDebugger *handler;
68+
69+
int gettimeofday_new(struct timeval *t, void *a) {
70+
t->tv_sec = 1723532400;
71+
t->tv_usec = 0;
72+
return 0;
73+
}
74+
75+
void hookTime() {
76+
handler = new SimpleDebugger();
77+
handler->hookFunction((void *) &gettimeofday, (void *) &gettimeofday_new);
78+
}
79+
```
80+
5381
# How it works
5482

5583
SimpleDebugger overwrites instructions with a break instruction by modifying the vm protection of the memory address to be writeable. The original instruction is stored in a table and written back after the breakpoint is hit. Break instructions are handled with a mach exception server.

0 commit comments

Comments
 (0)