Last active
February 27, 2021 10:41
-
-
Save Mic92/49ee012d97d7e762740ecfd6b2282ca5 to your computer and use it in GitHub Desktop.
Attach gdb on demand in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod gdb_break; | |
pub fn main() { | |
// this will create a new tmux session with gdb | |
// To let the program continue run `detach` in gdb and close it | |
gdb_break(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::process::Command; | |
use libc::{syscall, SYS_gettid, pid_t}; | |
pub fn gdb_break() { | |
let tid = unsafe { syscall(SYS_gettid) as pid_t }.to_string(); | |
println!("GDB PROBE HIT, WAITING"); | |
// 1. finish: wait4 | |
// 2. finish: Process::wait | |
// 3. finish: gdb::break | |
// 4. finish: caller | |
let args = vec![ | |
"-c", "tmux new-window sudo -E gdb --pid \"$0\" -ex \"shell kill -9 $$\" -ex finish -ex finish -ex finish -ex finish; kill -STOP $$", &tid | |
]; | |
let _ = Command::new("sh").args(args.as_slice()).status(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment