|
| 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 | +} |
0 commit comments