binrw icon indicating copy to clipboard operation
binrw copied to clipboard

Add `dbg` directive

Open jam1garner opened this issue 3 years ago • 1 comments

Purely out of curiosity of how an implementation would feel, I implemented a dbg directive.

To explain, here's the documentation for it:


The dbg directive prints out information to stderr to help with quick-and-dirty debugging.

The information printed out:

  • Source filename
  • Line number in source file
  • Offset in reader (in hex)
  • Contents of parsed value (using hex format when available)
#[derive(BinRead)]
struct DbgExample {
    first_value: u32,
    #[br(dbg)]
    second_value: u32,
}

Which will output something like...

[src/main.rs:5 | offset 0x4] second_value = 0xFFFE76

This is not necessarily something we need to add, just putting this here to (a) collect thoughts and (b) have an implementation if ever desired.

jam1garner avatar Mar 21 '21 05:03 jam1garner

Just had to figure out a bug and dumping thoughts here about things a dbg implementation probably should have beyond what is in this PR:

  • Show the position of the reader both before and after the read (if there is an IO error indicate that in place of the end-read position). This would be implicit in the start position of the next field, except that they can be padded/aligned and also the last field doesn’t have a next field
  • Show padding/alignment operations when they occur
  • Show errors when they occur
  • Show any applied directive values, e.g. if someone has if(foo) show the value of foo, if someone has pad_size_to(foo) show the foo and what actually happened
  • Recursion through inner types?

csnover avatar Mar 24 '21 16:03 csnover

I do believe this was superseded by https://github.com/jam1garner/binrw/pull/162.

csnover avatar Oct 25 '22 05:10 csnover