mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 12:41:32 +00:00
Optimize backtraces.
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1559,6 +1559,7 @@ dependencies = [
|
||||
"bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"errno 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"field-offset 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -19,6 +19,7 @@ libc = "0.2.49"
|
||||
hex = "0.3.2"
|
||||
smallvec = "0.6.9"
|
||||
bincode = "1.1"
|
||||
colored = "1.8"
|
||||
|
||||
# Dependencies for caching.
|
||||
[dependencies.serde]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops::Bound::{Included, Unbounded};
|
||||
use std::ops::Bound::{Excluded, Included, Unbounded};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct RegisterIndex(pub usize);
|
||||
@ -233,6 +233,90 @@ impl ExecutionStateImage {
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_backtrace_if_needed(&self) {
|
||||
use std::env;
|
||||
|
||||
if let Ok(x) = env::var("WASMER_BACKTRACE") {
|
||||
if x == "1" {
|
||||
eprintln!("{}", self.colored_output());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("Run with `WASMER_BACKTRACE=1` environment variable to display a backtrace.");
|
||||
}
|
||||
|
||||
pub fn colored_output(&self) -> String {
|
||||
use colored::*;
|
||||
|
||||
fn join_strings(x: impl Iterator<Item = String>, sep: &str) -> String {
|
||||
let mut ret = String::new();
|
||||
let mut first = true;
|
||||
|
||||
for s in x {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
ret += sep;
|
||||
}
|
||||
ret += &s;
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
fn format_optional_u64_sequence(x: &[Option<u64>]) -> String {
|
||||
if x.len() == 0 {
|
||||
"(empty)".into()
|
||||
} else {
|
||||
join_strings(
|
||||
x.iter().enumerate().map(|(i, x)| {
|
||||
format!(
|
||||
"[{}] = {}",
|
||||
i,
|
||||
x.map(|x| format!("{}", x))
|
||||
.unwrap_or_else(|| "?".to_string())
|
||||
.bold()
|
||||
.cyan()
|
||||
)
|
||||
}),
|
||||
", ",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let mut ret = String::new();
|
||||
|
||||
if self.frames.len() == 0 {
|
||||
ret += &"Unknown fault address, cannot read stack.".yellow();
|
||||
ret += "\n";
|
||||
} else {
|
||||
ret += &"Backtrace:".bold();
|
||||
ret += "\n";
|
||||
for (i, f) in self.frames.iter().enumerate() {
|
||||
ret += &format!("* Frame {} @ Local function {}", i, f.local_function_id).bold();
|
||||
ret += "\n";
|
||||
ret += &format!(
|
||||
" {} {}\n",
|
||||
"Offset:".bold().yellow(),
|
||||
format!("{}", f.wasm_inst_offset).bold().cyan(),
|
||||
);
|
||||
ret += &format!(
|
||||
" {} {}\n",
|
||||
"Locals:".bold().yellow(),
|
||||
format_optional_u64_sequence(&f.locals)
|
||||
);
|
||||
ret += &format!(
|
||||
" {} {}\n\n",
|
||||
"Stack:".bold().yellow(),
|
||||
format_optional_u64_sequence(&f.stack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(unix, target_arch = "x86_64"))]
|
||||
@ -289,12 +373,15 @@ pub mod x64 {
|
||||
for f in image.execution_state.frames.iter().rev() {
|
||||
let fsm = local_functions_vec[f.local_function_id];
|
||||
let call_begin_offset = fsm.wasm_offset_to_target_offset[f.wasm_inst_offset];
|
||||
|
||||
// Left bound must be Excluded because it's possible that the previous instruction's (after-)call offset == call_begin_offset.
|
||||
let (after_call_inst, diff_id) = fsm
|
||||
.call_offsets
|
||||
.range((Included(&call_begin_offset), Unbounded))
|
||||
.range((Excluded(&call_begin_offset), Unbounded))
|
||||
.next()
|
||||
.map(|(k, v)| (*k, *v))
|
||||
.expect("instruction offset not found in call offsets");
|
||||
|
||||
let diff = &fsm.diffs[diff_id];
|
||||
let state = diff.build_state(fsm);
|
||||
|
||||
|
@ -92,6 +92,12 @@ unsafe extern "C" fn suspend(
|
||||
known_registers[X64Register::GPR(GPR::RBX).to_index().0] = Some(rbx);
|
||||
|
||||
let es_image = read_stack(&msm, code_base, stack, known_registers, None);
|
||||
|
||||
{
|
||||
use colored::*;
|
||||
eprintln!("\n{}", "Suspending instance.".green().bold());
|
||||
}
|
||||
es_image.print_backtrace_if_needed();
|
||||
let image = build_instance_image(ctx, es_image);
|
||||
let image_bin = serialize(&image).unwrap();
|
||||
let mut f = File::create(&config.image_path).unwrap();
|
||||
|
@ -4630,11 +4630,7 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
|
||||
);
|
||||
}
|
||||
Operator::Unreachable => {
|
||||
let state_diff_id =
|
||||
Self::get_state_diff(&self.machine, &mut self.fsm, &mut self.control_stack);
|
||||
let offset = a.get_offset().0;
|
||||
self.fsm.trappable_offsets.insert(offset, state_diff_id);
|
||||
|
||||
Self::mark_trappable(a, &self.machine, &mut self.fsm, &mut self.control_stack);
|
||||
a.emit_ud2();
|
||||
self.unreachable_depth = 1;
|
||||
}
|
||||
|
@ -25,44 +25,6 @@ use wasmer_runtime_core::state::x64::{read_stack, X64Register, GPR};
|
||||
use wasmer_runtime_core::typed_func::WasmTrapInfo;
|
||||
use wasmer_runtime_core::vm;
|
||||
|
||||
fn join_strings(x: impl Iterator<Item = String>, sep: &str) -> String {
|
||||
let mut ret = String::new();
|
||||
let mut first = true;
|
||||
|
||||
for s in x {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
ret += sep;
|
||||
}
|
||||
ret += &s;
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
fn format_optional_u64_sequence(x: &[Option<u64>]) -> String {
|
||||
use colored::*;
|
||||
|
||||
if x.len() == 0 {
|
||||
"(empty)".into()
|
||||
} else {
|
||||
join_strings(
|
||||
x.iter().enumerate().map(|(i, x)| {
|
||||
format!(
|
||||
"[{}] = {}",
|
||||
i,
|
||||
x.map(|x| format!("{}", x))
|
||||
.unwrap_or_else(|| "?".to_string())
|
||||
.bold()
|
||||
.cyan()
|
||||
)
|
||||
}),
|
||||
", ",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn signal_trap_handler(
|
||||
signum: ::nix::libc::c_int,
|
||||
siginfo: *mut siginfo_t,
|
||||
@ -104,40 +66,12 @@ extern "C" fn signal_trap_handler(
|
||||
|
||||
use colored::*;
|
||||
eprintln!(
|
||||
"\n{}\n",
|
||||
"\n{}",
|
||||
"Wasmer encountered an error while running your WebAssembly program."
|
||||
.bold()
|
||||
.red()
|
||||
);
|
||||
if image.frames.len() == 0 {
|
||||
eprintln!("{}", "Unknown fault address, cannot read stack.".yellow());
|
||||
} else {
|
||||
use colored::*;
|
||||
eprintln!("{}\n", "Backtrace:".bold());
|
||||
for (i, f) in image.frames.iter().enumerate() {
|
||||
eprintln!(
|
||||
"{}",
|
||||
format!("* Frame {} @ Local function {}", i, f.local_function_id).bold()
|
||||
);
|
||||
eprintln!(
|
||||
" {} {}",
|
||||
"Offset:".bold().yellow(),
|
||||
format!("{}", f.wasm_inst_offset).bold().cyan(),
|
||||
);
|
||||
eprintln!(
|
||||
" {} {}",
|
||||
"Locals:".bold().yellow(),
|
||||
format_optional_u64_sequence(&f.locals)
|
||||
);
|
||||
eprintln!(
|
||||
" {} {}",
|
||||
"Stack:".bold().yellow(),
|
||||
format_optional_u64_sequence(&f.stack)
|
||||
);
|
||||
eprintln!("");
|
||||
}
|
||||
}
|
||||
|
||||
image.print_backtrace_if_needed();
|
||||
do_unwind(signum, siginfo as _, ucontext);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user