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
| Feature | Default | Adds |
|---|---|---|
cli | yes | the sbe-inspect filter |
http | no | schemas 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
| Step | Call | Fails with |
|---|---|---|
| Load schemas | Catalogue::builder().path("schemas/")?.xml("name", xml)?.build()? | CatalogueError |
| Make an inspector | Inspector::new(&catalogue) | never |
| Read bytes | inspector.inspect(bytes) returns a Reading | never: unknown bytes are a reading |
reading.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
| Source | Call | A 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 deep | is 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
Reading | Meaning |
|---|---|
Message | An SBE message of a loaded schema: its match class, fields, evidence and length |
Text | Valid UTF-8 whose characters are printable, tab, newline or carriage return |
Framed | Records a framing cut out of the bytes (Framing detection) |
Ambiguous | Several readings equal on every ranking rule, each with its evidence |
Padding | All zero bytes |
Unknown | Bytes 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.
| Class | When | How far to trust it |
|---|---|---|
Exact | The header’s schemaId and templateId are both in the catalogue | The identity is certain; the values are what the bytes say |
TemplateOnly | The templateId exists in a loaded schema with another id | A schema-id mismatch: the right layout, the wrong schema, or chance |
Inferred | No usable header: the bytes were read as a bare message body | A 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:
| Value | JsonPrinter | json |
|---|---|---|
| A set | { "sportsPack": true, "sunRoof": false } | ["sportsPack"]: the bits set |
| An optional field holding null | the type’s raw null value | null |
| A field newer than the writer’s version | the type’s raw null value | {"absent":true} |
| A constant newer than the writer’s version | its value | {"absent":true} |
| Binary var data | "ff007f" | "0xff007f": not a string of text |
A uint64 above i64::MAX | a negative number | the unsigned number |
A float constant such as 0.1 | its digits as a double: 0.10000000149011612 | 0.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:
| Value | Generated Display | compact |
|---|---|---|
| A composite or group entry | Engine { .. }, FuelFiguresEntryDecoder { .. } | { .. } |
| Binary var data that is not UTF-8 | "<3 bytes>" | 0xff007f |
| An enum value the schema does not define | NullVal | invalid(7) |
| Set bits the schema does not define | dropped | bit9 |
| An optional field holding null | None | null |
| A field newer than the writer’s version | None | absent |
| A NaN other than the canonical quiet one (a payload, or the sign set) | NaN | NaN(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
- The
sbe-inspectfilter: the same reading in a pipe, with unknown input passed through unchanged. - How a reading is chosen: the hard rules, the evidence, the comparison order and the limits.
- Framing detection: how messages are found in a buffer of many.