use crate::Result;
use color_eyre::eyre::WrapErr;
use std::{
path::Path,
process::{Command, ExitStatus},
};
#[tracing::instrument(level = "debug")]
pub fn run_gdb(binary: &Path, gdb_port: u16) -> Result<ExitStatus> {
let gdb_path = wheres_gdb().context("failed to find gdb executable")?;
tracing::info!("Found {}", gdb_path);
let mut gdb = Command::new(gdb_path);
gdb.arg("-ex").arg(format!("file {}", binary.display()));
gdb.arg("-ex").arg(format!("target remote :{gdb_port}"));
gdb.arg("-ex").arg("tbreak mycelium_kernel::kernel_main");
gdb.arg("-ex").arg("continue");
tracing::info!("Running {gdb:?}");
let status = gdb.status().context("failed to run gdb")?;
tracing::debug!("gdb exited with status {:?}", status);
Ok(status)
}
fn wheres_gdb() -> Result<String> {
let output = Command::new("which")
.arg("rust-gdb")
.output()
.context("running `which rust-gdb` failed")?;
let mut which_gdb =
String::from_utf8(output.stdout).context("`which rust-gdb` output was not a string")?;
which_gdb.truncate(which_gdb.trim_end().len());
if which_gdb.ends_with("not found") {
return Ok(String::from("gdb"));
}
Ok(which_gdb)
}