1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
extern crate nix;

use nix::unistd::{fork, ForkResult, setsid, execvp, getpid, getppid};
use nix::sys::signal::{Signal, kill};
use std::ffi::CString;

pub fn gdb_probe(){
    match fork() {
       Ok(ForkResult::Parent { child, .. }) => {
           println!("GDB PROBE HIT, SLEEPING");
           kill(getpid(), Signal::SIGSTOP );
           println!("GDB ATTACHED, CONTINUE");
       }
       Ok(ForkResult::Child) => {
           let gdb =  format!("sudo gdb --pid {}", getppid());
           let argv = vec!["urxvt", "-e", "sh", "-c", &gdb];
           let argv_c = argv.iter().map(|s| CString::new(*s).unwrap()).collect::<Vec<_>>();
           println!("execv: {:?}", argv);
           setsid().expect("could not setsid()");
           execvp(&CString::new("urxvt").unwrap(), &argv_c[..]).expect("could not execv terminal, maybe urxvt is missing?");
       }

       Err(_) => println!("Fork failed"),
    }
}

#[cfg(test)]
mod tests {
    use crate::gdb_probe;
    #[test]
    fn it_works() {
        let x = 3+4;
        gdb_probe();
        assert!(false);
    }
}