Skip to content

Commit 911609f

Browse files
committed
Started to organize code a bit for library use
1 parent ef0e0b3 commit 911609f

19 files changed

+492
-225
lines changed

Cargo.lock

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ version = "0.1.0"
44
authors = ["bfalk"]
55

66
[dependencies]
7-
winapi = { version = "0.3.5", features = ["debugapi", "winbase", "memoryapi", "processthreadsapi", "errhandlingapi", "handleapi", "securitybaseapi", "consoleapi", "winerror", "wow64apiset", "psapi"] }
7+
debugger = { path = "libs/debugger" }
88

99
[profile.release]
1010
debug = true
11-

examples/benchmark/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+

examples/benchmark/Cargo.lock

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/benchmark/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "benchmark"
3+
version = "0.1.0"
4+
authors = ["Brandon Falk <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
debugger = { path = "../../libs/debugger" }
9+
10+
[profile.release]
11+
debug = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.obj
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all:
2+
nasm -f win64 -o program.obj program.asm
3+
link /nologo /SUBSYSTEM:CONSOLE /OUT:program.exe program.obj
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[bits 64]
2+
3+
section .code
4+
5+
global mainCRTStartup
6+
mainCRTStartup:
7+
; This should be at program.exe+0x1000
8+
nop
9+
10+
; This should be at program.exe+0x1001
11+
jmp mainCRTStartup
12+
1.5 KB
Binary file not shown.

examples/benchmark/src/main.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::process::Command;
2+
use std::sync::Arc;
3+
use std::time::Instant;
4+
use debugger::{Debugger, BreakpointType};
5+
6+
const NUM_BREAKPOINTS_TO_HIT: u64 = 100000;
7+
8+
/// Get elapsed time in seconds
9+
fn elapsed_from(start: &Instant) -> f64 {
10+
let dur = start.elapsed();
11+
dur.as_secs() as f64 + dur.subsec_nanos() as f64 / 1_000_000_000.0
12+
}
13+
14+
fn bp_handler(_dbg: &mut Debugger, _tid: u32, _rip: usize, freq: u64) -> bool {
15+
if freq == NUM_BREAKPOINTS_TO_HIT {
16+
return false;
17+
}
18+
19+
true
20+
}
21+
22+
fn benchmark_bp_creation() {
23+
const BREAKPOINTS_TO_APPLY: usize = 1000000;
24+
25+
// Create new fake process to test performance on
26+
let mut process = Command::new("fake_program\\program.exe").spawn()
27+
.expect("Failed to run program");
28+
29+
// Attach to program process
30+
let mut dbg = Debugger::attach(process.id());
31+
32+
let modname = Arc::new(String::from("program.exe"));
33+
let name = Arc::new(String::from("wootboot"));
34+
35+
// Register breakpoints
36+
let start = Instant::now();
37+
for offset in 0..BREAKPOINTS_TO_APPLY {
38+
// These breakpoints are just bogus and don't matter
39+
let offset = offset + 0x10000;
40+
dbg.register_breakpoint(modname.clone(), offset, name.clone(),
41+
offset, BreakpointType::Single, None);
42+
}
43+
let elapsed = elapsed_from(&start);
44+
45+
print!("Registered {:10} breakpoints in {:10.6} seconds | {:10.1} / second\n",
46+
BREAKPOINTS_TO_APPLY, elapsed, BREAKPOINTS_TO_APPLY as f64 / elapsed);
47+
48+
// Kill process
49+
let _ = process.kill();
50+
51+
// Wait until process exits, which will require all breakpoints to be
52+
// applied
53+
let start = Instant::now();
54+
dbg.run();
55+
let elapsed = elapsed_from(&start);
56+
57+
print!("Applied {:10} breakpoints in {:10.6} seconds | {:10.1} / second\n",
58+
BREAKPOINTS_TO_APPLY, elapsed, BREAKPOINTS_TO_APPLY as f64 / elapsed);
59+
60+
// Drop the debugger, clearing breakpoints and detaching
61+
let start = Instant::now();
62+
std::mem::drop(dbg);
63+
let elapsed = elapsed_from(&start);
64+
65+
print!("Cleared {:10} breakpoints in {:10.6} seconds | {:10.1} / second\n",
66+
BREAKPOINTS_TO_APPLY, elapsed, BREAKPOINTS_TO_APPLY as f64 / elapsed);
67+
}
68+
69+
fn benchmark_bp_hit() {
70+
// Create new fake process to test performance on
71+
let process = Command::new("fake_program\\program.exe").spawn()
72+
.expect("Failed to run program");
73+
74+
// Attach to program process
75+
let mut dbg = Debugger::attach(process.id());
76+
77+
let modname = Arc::new(String::from("program.exe"));
78+
let name = Arc::new(String::from("wootboot"));
79+
80+
// Register a real breakpoint, this will get hit in a loop in the fake
81+
// program we made
82+
dbg.register_breakpoint(modname.clone(), 0x1000,
83+
name.clone(), 0, BreakpointType::Freq, Some(bp_handler));
84+
85+
// Run
86+
let start = Instant::now();
87+
dbg.run();
88+
let elapsed = elapsed_from(&start);
89+
90+
print!("Hit {:10} breakpoints in {:10.6} seconds | {:10.1} / second\n",
91+
NUM_BREAKPOINTS_TO_HIT, elapsed,
92+
NUM_BREAKPOINTS_TO_HIT as f64 / elapsed);
93+
}
94+
95+
fn main() {
96+
benchmark_bp_creation();
97+
benchmark_bp_hit();
98+
}

libs/debugger/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "debugger"
3+
version = "0.1.0"
4+
authors = ["Brandon Falk <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
winapi = { version = "0.3.5", features = ["debugapi", "winbase", "memoryapi", "processthreadsapi", "errhandlingapi", "handleapi", "securitybaseapi", "consoleapi", "winerror", "wow64apiset", "psapi"] }

0 commit comments

Comments
 (0)