Skip to content

Commit

Permalink
Instruction debug fmt improvements (#546)
Browse files Browse the repository at this point in the history
* Decode instruction field in PanicInstruction dbg

* Dbg-format Instruction register arguments in hexadecimal

* Changelog

* Change "bytes " => "bytes: "
  • Loading branch information
Dentosal authored Aug 10, 2023
1 parent 92bbca1 commit a364fc8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- [#546](https://github.com/FuelLabs/fuel-vm/pull/546): Improve debug formatting of instruction in panic receipts.

## [Version 0.36.0]

### Changed
Expand Down
32 changes: 16 additions & 16 deletions fuel-asm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,26 +903,26 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let ra = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.finish()
}
};
($Op:ident[$ra:ident : RegId $rb:ident : RegId]) => {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.finish()
}
};
($Op:ident[$ra:ident : RegId $rb:ident : RegId $rc:ident : RegId]) => {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.finish()
}
};
Expand All @@ -932,10 +932,10 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc, rd) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($rd), &u8::from(rd))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.field(stringify!($rd), &format_args!("{:#02x}", u8::from(rd)))
.finish()
}
};
Expand All @@ -945,9 +945,9 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.field(stringify!($imm), &u8::from(imm))
.finish()
}
Expand All @@ -956,8 +956,8 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($imm), &u16::from(imm))
.finish()
}
Expand All @@ -966,7 +966,7 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($imm), &u32::from(imm))
.finish()
}
Expand Down
33 changes: 32 additions & 1 deletion fuel-asm/src/panic_instruction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::fmt;

use crate::{
Instruction,
PanicReason,
RawInstruction,
Word,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "typescript", wasm_bindgen::prelude::wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -35,6 +37,35 @@ impl PanicInstruction {
}
}

/// Helper struct to debug-format a `RawInstruction` in `PanicInstruction::fmt`.
struct InstructionDbg(RawInstruction);
impl fmt::Debug for InstructionDbg {
/// Formats like this: `MOVI { dst: 32, val: 32 } (bytes: 72 80 00 20)`}`
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match Instruction::try_from(self.0) {
Ok(instr) => write!(f, "{:?}", instr)?,
Err(_) => write!(f, "Unknown")?,
};
write!(f, " (bytes: ")?;
for (i, byte) in self.0.to_be_bytes().iter().enumerate() {
if i != 0 {
write!(f, " ")?;
}
write!(f, "{:02x}", byte)?;
}
write!(f, ")")
}
}

impl fmt::Debug for PanicInstruction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PanicInstruction")
.field("reason", &self.reason)
.field("instruction", &InstructionDbg(self.instruction))
.finish()
}
}

#[cfg(feature = "typescript")]
#[wasm_bindgen::prelude::wasm_bindgen]
impl PanicInstruction {
Expand Down

0 comments on commit a364fc8

Please sign in to comment.