Files
wasmtime/crates/lightbeam/src/disassemble.rs
Alex Crichton d2daf5064e Get lightbeam compiling on stable Rust (#2370)
This will hopefully remove a small thorn in our side with periodic
nightly breakage due to nightly features changing. This commit moves
lightbeam to stable Rust, swapping out `staticvec` for `arrayvec` and
otherwise updating some dependencies (namely `dynasm`) to compile with
stable.

This then also updates CI appropriately to not use a pinned nightly and
instead us a floating `nightly` channel so we can head off any breakage
coming up ASAP.
2020-11-06 13:23:08 -06:00

58 lines
1.4 KiB
Rust

use capstone::prelude::*;
use dynasmrt::AssemblyOffset;
use std::error::Error;
use std::fmt::{Display, Write};
use std::io;
pub fn disassemble<D: Display>(
mem: &[u8],
mut ops: &[(AssemblyOffset, D)],
) -> Result<(), Box<dyn Error>> {
let cs = Capstone::new()
.x86()
.mode(arch::x86::ArchMode::Mode64)
.build()
.map_err(map_caperr)?;
println!("{} bytes:", mem.len());
let insns = cs.disasm_all(&mem, 0x0).map_err(map_caperr)?;
for i in insns.iter() {
let mut line = String::new();
let address = i.address();
while let Some((offset, op)) = ops.first() {
if offset.0 as u64 <= address {
ops = &ops[1..];
println!("{}", op);
} else {
break;
}
}
write!(&mut line, "{:4x}:\t", i.address())?;
let mut bytes_str = String::new();
for b in i.bytes() {
write!(&mut bytes_str, "{:02x} ", b)?;
}
write!(&mut line, "{:24}\t", bytes_str)?;
if let Some(s) = i.mnemonic() {
write!(&mut line, "{}\t", s)?;
}
if let Some(s) = i.op_str() {
write!(&mut line, "{}", s)?;
}
println!("{}", line);
}
Ok(())
}
fn map_caperr(err: capstone::Error) -> Box<dyn Error> {
Box::new(io::Error::new(io::ErrorKind::Other, err.to_string()))
}