Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active February 27, 2021 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/49ee012d97d7e762740ecfd6b2282ca5 to your computer and use it in GitHub Desktop.
Save Mic92/49ee012d97d7e762740ecfd6b2282ca5 to your computer and use it in GitHub Desktop.
Attach gdb on demand in rust
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();
}
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