Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inspect bytes with no codec

ergo-sbe-inspect reads SBE bytes at run time. Load any number of schemas; give it bytes; it says which message of which schema the bytes most likely hold, how sure that is, and prints them. Nothing is generated or compiled: it walks the token IR that ergo-sbe parses, so any schema ergo-sbe accepts can be read.

Use it to look at a recording, a capture or a log line without knowing in advance which schema, message or framing it uses. For a codec you compile in, the generated decoder’s Display is faster (Display and Debug).

It is the crate ergo-sbe-inspect (sbe/inspect):

ergo-sbe-inspect = { version = "0.1", default-features = false }  # the library
FeatureDefaultAdds
cliyesthe sbe-inspect filter
httpnoschemas from HTTPS URLs

AI assistance: like the rest of ergon, this crate was written with heavy AI assistance (disclosure). It is not production-ready.

In four calls

StepCallFails with
Load schemasCatalogue::builder().path("schemas/")?.xml("name", xml)?.build()?CatalogueError
Make an inspectorInspector::new(&catalogue)never
Read bytesinspector.inspect(bytes) returns a Readingnever: unknown bytes are a reading
Printreading.compact(), .pretty() or .json() (each Display)never

All four, reading a capture file with a schema file or directory (the example is compiled with the crate; cargo run -p ergo-sbe-inspect --example read_capture -- <schemas> <capture>):

fn main() -> Result<(), Box<dyn Error>> {
    let args: Vec<String> = std::env::args().collect();
    let (schemas, capture) = (
        args.get(1).ok_or("schema path")?,
        args.get(2).ok_or("capture path")?,
    );
    let catalogue = Catalogue::builder().path(schemas)?.build()?;
    let bytes = std::fs::read(capture)?;
    print!("{}", Inspector::new(&catalogue).inspect(&bytes).pretty());
    Ok(())
}

On the car schema and the car capture the tests write, it prints the pretty output shown under The three outputs. The crate documentation’s example does the same on an inline schema and is compiled and run as a doctest.

Loading schemas

SourceCallA file that is not an SBE schema
One file.path("car.xml")fails the load, naming the file
A directory.path("schemas/"): every *.xml, sorted, at most 16 levels deepis skipped and listed in Catalogue::skipped()
Inline XML.xml("name", text)fails the load, naming name

CatalogueError has four cases: Io (a path cannot be read), Schema (ergo-sbe rejected it, with its parser error), Unsupported (it parsed but the walker cannot use it, for example groups or composites nested deeper than 32 levels) and Empty (nothing loaded). Two schemas with the same id are both kept; a reading that cannot choose between them lists both.

What a reading can be

ReadingMeaning
MessageAn SBE message of a loaded schema: its match class, fields, evidence and length
TextValid UTF-8 whose characters are printable, tab, newline or carriage return
FramedRecords a framing cut out of the bytes (Framing detection)
AmbiguousSeveral readings equal on every ranking rule, each with its evidence
PaddingAll zero bytes
UnknownBytes nothing explains, shown in full

A var-data field with no characterEncoding is read again as bytes of its own, so a message inside a message is decoded too, up to a depth limit. The bytes after a message are read again the same way.

Match classes

Every message is printed with its match class first, so a guess never looks like a fact.

ClassWhenHow far to trust it
ExactThe header’s schemaId and templateId are both in the catalogueThe identity is certain; the values are what the bytes say
TemplateOnlyThe templateId exists in a loaded schema with another idA schema-id mismatch: the right layout, the wrong schema, or chance
InferredNo usable header: the bytes were read as a bare message bodyA guess: kept only when it uses every byte and nothing counts against it

A car message with its 8-byte header removed reads as Inferred:

Inferred Car { serialNumber: 1234, modelYear: 2013, available: T, code: A, someNumbers: [10, 20, 30, 40, 50], vehicleCode: "ABCDEF", extras: sportsPack|cruiseControl, engine: { capacity: 2000, numCylinders: 4, maxRpm: 9000, manufacturerCode: "123", fuel: "Petrol" }, fuelFigures: [{ speed: 30, mpg: 35.9 }, { speed: 60, mpg: 25.0 }], performanceFigures: [{ octaneRating: 95, acceleration: [{ mph: 30, seconds: 4.0 }, { mph: 60, seconds: 7.5 }] }], manufacturer: "Honda", model: "Civic VTi" }

A message cut short is still shown, with the rule that stopped the walk (hard rules), and the bytes after the cut follow it as unknown bytes, so none is dropped. The car message cut to 60 bytes: its fuelFigures group declares 2 entries that no longer fit:

Exact Car { serialNumber: 1234, modelYear: 2013, available: T, code: A, someNumbers: [10, 20, 30, 40, 50], vehicleCode: "ABCDEF", extras: sportsPack|cruiseControl, engine: { capacity: 2000, numCylinders: 4, maxRpm: 9000, manufacturerCode: "123", fuel: "Petrol" }, fuelFigures: [(0 of 2 read)] } truncated at 53 (H4)
Unknown(7) 0x1e009a990f423c

The three outputs

compact is one line per message, in the generated decoder’s Display style:

Exact Car { serialNumber: 1234, modelYear: 2013, available: T, code: A, someNumbers: [10, 20, 30, 40, 50], vehicleCode: "ABCDEF", extras: sportsPack|cruiseControl, engine: { capacity: 2000, numCylinders: 4, maxRpm: 9000, manufacturerCode: "123", fuel: "Petrol" }, fuelFigures: [{ speed: 30, mpg: 35.9 }, { speed: 60, mpg: 25.0 }], performanceFigures: [{ octaneRating: 95, acceleration: [{ mph: 30, seconds: 4.0 }, { mph: 60, seconds: 7.5 }] }], manufacturer: "Honda", model: "Civic VTi" }

pretty is for people: the match class, schema, wire version, length and evidence first, then one field per line:

Car  Exact  schema 1 v1 (uk.co.real_logic.sbe.benchmarks)  template 1  wire v1 blockLength 41  108 bytes  complete  evidence: +enums valid, +chars printable, +text valid, +header ids found, +version equal, +ends exactly
  serialNumber: 1234
  modelYear: 2013
  available: T
  code: A
  someNumbers: [10, 20, 30, 40, 50]
  vehicleCode: "ABCDEF"
  extras: sportsPack|cruiseControl
  engine:
    capacity: 2000
    numCylinders: 4
    maxRpm: 9000
    manufacturerCode: "123"
    fuel: "Petrol"
  fuelFigures: 2 entries
    [0]
      speed: 30
      mpg: 35.9
    [1]
      speed: 60
      mpg: 25.0
  performanceFigures: 1 entries
    [0]
      octaneRating: 95
      acceleration: 2 entries
        [0]
          mph: 30
          seconds: 4.0
        [1]
          mph: 60
          seconds: 7.5
  manufacturer: "Honda"
  model: "Civic VTi"

json is one object per record per line, for other programs:

{"type":"message","class":"Exact","message":"Car","schema":{"package":"uk.co.real_logic.sbe.benchmarks","id":1,"version":1},"template":1,"header":{"blockLength":41,"templateId":1,"schemaId":1,"version":1},"actingVersion":1,"actingBlockLength":41,"consumed":108,"complete":true,"truncated":null,"evidence":[{"kind":"enums valid","against":false,"at":""},{"kind":"chars printable","against":false,"at":""},{"kind":"text valid","against":false,"at":""},{"kind":"header ids found","against":false,"at":""},{"kind":"version equal","against":false,"at":""},{"kind":"ends exactly","against":false,"at":""}],"fields":{"serialNumber":1234,"modelYear":2013,"available":"T","code":"A","someNumbers":[10,20,30,40,50],"vehicleCode":"ABCDEF","extras":["sportsPack","cruiseControl"],"engine":{"capacity":2000,"numCylinders":4,"maxRpm":9000,"manufacturerCode":"123","fuel":"Petrol"},"fuelFigures":[{"speed":30,"mpg":35.9},{"speed":60,"mpg":25.0}],"performanceFigures":[{"octaneRating":95,"acceleration":[{"mph":30,"seconds":4.0},{"mph":60,"seconds":7.5}]}],"manufacturer":"Honda","model":"Civic VTi"},"trailing":null}

A single char field prints in json as a string of one char, a zero byte as "\u0000"; as in JsonPrinter, a byte above 0x7f is sign-extended (0xe9 is U+FFE9). A constant prints as a number, or as a string for char; a constant enum field as the value’s name alone ("C"); a valueRef on a number or char field as the value it refers to. fields is shaped as the output of sbe-tool’s JsonPrinter, and differs from it on purpose here:

ValueJsonPrinterjson
A set{ "sportsPack": true, "sunRoof": false }["sportsPack"]: the bits set
An optional field holding nullthe type’s raw null valuenull
A field newer than the writer’s versionthe type’s raw null value{"absent":true}
A constant newer than the writer’s versionits value{"absent":true}
Binary var data"ff007f""0xff007f": not a string of text
A uint64 above i64::MAXa negative numberthe unsigned number
A float constant such as 0.1its digits as a double: 0.100000001490116120.1
A constant that is not finite"0/0", "1/0", "-1/0""NaN", "inf", "-inf"; a -NaN as its bits ("NaN(0xffc00000)" for a float)

compact prints every field the generated Display prints, with the same name and value text, and more: the fields Display skips (constants such as maxRpm, and fixed arrays such as someNumbers), and the raw value wherever Display would lose it. It differs on purpose in a few places:

ValueGenerated Displaycompact
A composite or group entryEngine { .. }, FuelFiguresEntryDecoder { .. }{ .. }
Binary var data that is not UTF-8"<3 bytes>"0xff007f
An enum value the schema does not defineNullValinvalid(7)
Set bits the schema does not definedroppedbit9
An optional field holding nullNonenull
A field newer than the writer’s versionNoneabsent
A NaN other than the canonical quiet one (a payload, or the sign set)NaNNaN(0x7fc00001): its bits
A group the walk stopped inside(not printed)[(0 of 2 read)]: entries read, of the count on the wire
A group or var data whose prefix does not fit(not printed)cut

Every printed string is escaped (Rust Debug escaping in compact and pretty, JSON escaping of every control character in json), payload text and schema text (names, the package, constants) alike, so neither a payload nor a schema can send control characters to a terminal.

Next