Skip to content

Commit

Permalink
rust runner: only breakpoint under a debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
tombl committed Mar 26, 2024
1 parent b853fce commit dc3f953
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
1 change: 0 additions & 1 deletion arch/wasm/include/asm/bug.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
pr_crit("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, \
__func__); \
barrier_before_unreachable(); \
wasm_breakpoint(); \
__builtin_trap(); \
} while (0)

Expand Down
51 changes: 36 additions & 15 deletions tools/wasm-runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Result;
use clap::Parser;
use rand::Rng;
use std::{
arch::asm,
io::{stdout, Write},
fs::File,
io::{stdout, BufRead, BufReader, Write},
path::PathBuf,
time::Instant,
};
Expand All @@ -27,7 +29,7 @@ struct Args {
#[clap(short, long, default_value_t = 1024)]
memory: u32,

/// enable debugging
/// enable debug info
#[clap(short, long)]
debug: bool,
}
Expand All @@ -41,14 +43,13 @@ struct State {
instance_pre: Option<InstancePre<State>>,
}

fn add_imports(linker: &mut Linker<State>, is_debug: bool) -> anyhow::Result<()> {
fn add_imports(linker: &mut Linker<State>) -> Result<()> {
linker.func_wrap("kernel", "breakpoint", move || {
if !is_debug {
return;
};
unsafe {
#[cfg(target_arch = "x86_64")]
asm!("int3");
if let Ok(true) = is_under_debugger() {
unsafe {
#[cfg(target_arch = "x86_64")]
asm!("int3");
}
}
})?;
linker.func_wrap("kernel", "halt", || {
Expand Down Expand Up @@ -132,9 +133,7 @@ fn add_imports(linker: &mut Linker<State>, is_debug: bool) -> anyhow::Result<()>
|mut caller: Caller<'_, State>, buf: u32, len: u32| {
let memory = caller.data_mut().memory.data();

let trace = std::backtrace::Backtrace::force_capture()
.to_string()
.into_bytes();
let trace = b"stack traces are unsupported";

let buf = buf as usize;
let len = (len as usize).min(trace.len());
Expand Down Expand Up @@ -217,7 +216,7 @@ fn add_imports(linker: &mut Linker<State>, is_debug: bool) -> anyhow::Result<()>
Ok(())
}

fn create_devicetree(cmdline: &str, memory_pages: u32) -> anyhow::Result<Vec<u8>> {
fn create_devicetree(cmdline: &str, memory_pages: u32) -> Result<Vec<u8>> {
let mut fdt = FdtWriter::new()?;
let mut rng_seed = [0u64; 8];
rand::thread_rng().fill(&mut rng_seed);
Expand All @@ -242,7 +241,29 @@ fn create_devicetree(cmdline: &str, memory_pages: u32) -> anyhow::Result<Vec<u8>
Ok(fdt.finish()?)
}

fn main() -> anyhow::Result<()> {
#[cfg(not(target_os = "linux"))]
fn is_under_debugger() -> Result<bool> {
Ok(false)
}

#[cfg(target_os = "linux")]
fn is_under_debugger() -> Result<bool> {
let file = File::open("/proc/self/status")?;
let reader = BufReader::new(file);

for line in reader.lines() {
let line = line?;
if line.starts_with("TracerPid:") {
if let Some(pid) = line.split_whitespace().nth(1) {
return Ok(pid != "0");
}
}
}

Ok(false)
}

fn main() -> Result<()> {
let args = Args::parse();

let engine = Engine::new(
Expand Down Expand Up @@ -272,7 +293,7 @@ fn main() -> anyhow::Result<()> {
);

let mut linker = Linker::new(&engine);
add_imports(&mut linker, args.debug)?;
add_imports(&mut linker)?;
linker.define(&store, "env", "memory", memory)?;

let instance_pre = linker.instantiate_pre(&module)?;
Expand Down

0 comments on commit dc3f953

Please sign in to comment.