The sbe-inspect filter
sbe-inspect decodes the SBE messages in its input with the schemas named on
its command line, and passes through, byte for byte, whatever it cannot
decode. It is ergo-sbe-inspect as a pipe filter, in the
manner of c++filt: put it in any pipeline and only what it recognises
changes.
Install it (Rust 1.99 or newer):
cargo install ergo-sbe-inspect # schema files and directories
cargo install ergo-sbe-inspect --features http # also https:// schema URLs
cargo install --path sbe/inspect # from a checkout of the repository
The http feature uses the platform’s TLS and certificate store; on Linux it
needs the OpenSSL headers to build.
usage: sbe-inspect [SCHEMA_FILE|DIR|URL]... [OPTIONS] < INPUT
Decodes the SBE messages on stdin with the given schemas (files, directories
walked for *.xml, and https:// URLs). Input it cannot decode is written
unchanged when stdout is not a terminal, and shown as text or hex on one.
options:
--format pretty|compact|json how a decoded message is printed (default:
pretty on a terminal, compact otherwise)
--input auto|bin|lines auto (default): lines when the input's first
line is text, else bin
--framing auto|NAME fix the framing of bin input and of decoded
runs, by the name it is printed with, e.g.
'aeron-frame', 'back-to-back', 'sofh big-endian',
'length-prefix u32 little-endian payload-only',
'skip-then 10 + single'
--strict exit 1 when any input was not recognised
--max-buffer BYTES most bytes held for an incomplete record
(default 4194304)
--allow-http fetch http:// schema URLs too (http feature)
--offline read schema URLs from the cache only (http
feature)
-h, --help print this text
exit status: 0 done, even when nothing decoded; 1 with --strict when some input
was not recognised; 2 bad arguments, a schema that does not load, or an I/O
error.
Binary input
A capture of length-prefixed records, two car messages around a record that
is not SBE, decoded with the car schema. The passthrough bytes include NULs,
so the output goes through cat -v:
sbe-inspect sbe/tests/fixtures/schemas/car.xml --format compact \
< sbe/inspect/tests/book/filter-capture.bin | cat -v
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" }
^R^@^@^@not an SBE messageExact 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" }
The framing (a little-endian u32 length before each record) was detected
and each car printed on its own line. The middle record, its length
(^R^@^@^@, 18) and its 18 bytes, came out exactly as it went in, so the
second car’s line starts straight after it. Nothing is dropped and nothing
is changed: input with no SBE in it comes out identical, which the tests
check byte for byte.
| Input | Written to a pipe or file | Shown on a terminal |
|---|---|---|
Recognised: an Exact, complete message (or a run of them) that explains every byte of its record, and whose decoded text shows every byte the walk read | The decoded text in --format | The same |
Anything else: TemplateOnly and Inferred guesses, text, unknown bytes, a record cut short, a message whose block holds bytes no decoded field shows (a writer whose block is longer than the schema’s, or an older writer with bytes under fields newer than its version, zeros included) | The input bytes, unchanged | The reading: labelled guesses, text or hex, every control character escaped |
--format defaults to pretty on a terminal and compact elsewhere; json
writes one object per message. Zero padding counts as recognised, and is
printed as Padding(n), only where the framing explains it: between two
recognised records, or after a recognised message in Aeron frames (the term’s
unwritten tail, however long). Zeros anywhere else, such as after a single
message or among unknown bytes, come out unchanged. Zero-filled Aeron padding
frames and alignment are framing, like a length prefix, and print nothing;
Aeron alignment bytes or padding-frame bodies that are not zero come out
unchanged.
“Every byte the walk read” has two exceptions, both the format rather than
data: the message header’s four standard fields, which compact reduces to
the message name and match class (pretty and json print them), and
padding the schema itself declares (a gap left by explicit offsets, or a
blockLength longer than the fields), which the generated decoders do not
show either. A header or group dimension with any other member (an SBE 2.0
numGroups, say) is read but shown by no output, so such a message passes
through unchanged.
--framing fixes the framing instead of detecting it, by the name the
framing is printed with (Framing detection): --framing aeron-frame for a term buffer or an archive segment, --framing 'length-prefix u32 little-endian payload-only' for the capture above.
Streaming
The filter reads its input as it arrives and flushes whenever it writes:
-
A recognised record is written as soon as it is complete.
-
An incomplete record waits for more input; at the end of the input it is written as it stands.
-
In a framing whose own structure marks every record (a length prefix, a SOFH, Aeron frames, a skipped header before one of those), a record that is not recognised is written once a later record is. Bytes only content marks (
scan,back-to-back, a single payload) wait for the end of the input. -
So the output mostly does not depend on how the input arrived in reads: the tests cut streams in every framing at every byte and compare. Known exceptions, where a prefix is explained by a framing that the whole input is not, so the whole input is
scanned instead:- a skipped leading header followed by a record that is not SBE;
- length-prefixed records followed by zeros;
- Aeron frames followed by zeros and then bytes that are not zero (with
--framing aeron-frame, the whole input is then not recognised at all).
The same messages are decoded either way; what differs is whether the framing bytes (length prefixes, frame headers) come out raw or hidden, and whether the zeros print as
Padding(n). -
Nothing waits beyond
--max-bufferbytes (4 MiB by default): past it, what is held is written as it stands. Such a flush can write raw what a larger buffer would decode (a record longer than the buffer, zeros between two recognised records). The one case the other way: zeros after Aeron frames are printed as the term’s tail up to the flush, even if bytes that are not zero follow later, which a larger buffer would write raw. With a small buffer, where the reads fall can also decide whether a message held at a flush is decoded or written raw, and where thePadding(n)lines of a long Aeron tail break.
Log lines
With --input lines, or by default when the input’s first line (within its
first 4 KiB) is text with at least one character that is not white space, the
filter reads lines; the choice is made on that line alone. So binary input
whose first byte is a newline (a length of 10) stays binary; one whose first
bytes happen to be printable characters and a newline (a little-endian length
such as 0x0A41 or 0x0A4141) reads as log lines, unless --input bin is
given. Each run of hex digits (optionally after
0x) or of standard base64 that decodes to a recognised message is replaced
in place; the rest of the line, and every run that does not decode, stays
byte for byte as it was. A log with the car message as hex and as base64:
12:00:01.481 INFO gateway rx len=108 payload=2900010001000100d2040000dd0701410a000000140000001e000000280000003200000041424344454606d00704313233060002001e009a990f423c000000c841010001005f060002001e00000080403c000000f04005000000486f6e646109000000436976696320565469
12:00:01.502 INFO gateway rx payload_b64=KQABAAEAAQDSBAAA3QcBQQoAAAAUAAAAHgAAACgAAAAyAAAAQUJDREVGBtAHBDEyMwYAAgAeAJqZD0I8AAAAyEEBAAEAXwYAAgAeAAAAgEA8AAAA8EAFAAAASG9uZGEJAAAAQ2l2aWMgVlRp
12:00:01.503 WARN gateway slow consumer, 3 messages behind
sbe-inspect sbe/tests/fixtures/schemas/car.xml < sbe/inspect/tests/book/filter-log.txt
12:00:01.481 INFO gateway rx len=108 payload=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" }
12:00:01.502 INFO gateway rx payload_b64=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" }
12:00:01.503 WARN gateway slow consumer, 3 messages behind
A decoded run stays on its line: pretty is written as compact, json as
one line, and several messages in one run are joined by |. A run must
decode to at least 8 bytes, one message header, and every byte of it must be
recognised: a run holding a message and zeros before or after it stays as it
is. Line endings, a missing last
newline and bytes that are not UTF-8 are kept; on a terminal, control
characters in the line are escaped too. A text with no message in it comes out
unchanged: the tests run the repository’s changelog through every fixture
schema.
Exit status and --strict
| Exit | When |
|---|---|
| 0 | Done, even when nothing was decoded; also when the reader closes the pipe (| head) |
| 1 | --strict, and some input was not recognised: bytes in binary input, lines with nothing decoded in log lines |
| 2 | Bad arguments, a schema that does not load, or an I/O error |
Diagnostics go to standard error only: files skipped in a schema directory,
and the count --strict found. On the capture above, --strict exits 1 and
writes:
sbe-inspect sbe/tests/fixtures/schemas/car.xml --strict \
< sbe/inspect/tests/book/filter-capture.bin > /dev/null
sbe-inspect: --strict: 22 bytes not recognised
The 22 bytes are the middle record and its length.
Schemas from a URL
With the http feature, a schema argument may be an https:// URL. Schemas
are fetched before any input is read, never while decoding.
| Rule | Detail |
|---|---|
| HTTPS only | An http:// URL, or a redirect to one, is refused unless --allow-http is given; that holds for the cache too |
| Size | At most 4 MiB |
| Time | 10 s for the whole request, redirects included |
| Redirects | At most 3 |
| Self-contained | A schema that includes other files is refused: an include would be read from this machine |
| DTDs | Refused by the XML parser, so an entity bomb fails cleanly |
| Cache | One file per URL (a hash, with the URL beside it to rule out a collision) under $XDG_CACHE_HOME/sbe-inspect, else ~/.cache/sbe-inspect; read back with the same checks |
| Offline | --offline reads the cache only and never connects |
| Certificates | The platform’s store: Security.framework on macOS, OpenSSL’s default paths on Linux, SChannel on Windows |
sbe-inspect https://<host>/schemas/car.xml < capture.bin
From a program
The filter is ergo_sbe_inspect::filter::run over any reader and writer, with
the same options; CatalogueBuilder::url fetches a schema with the limits of
a Fetch.
Next
- Framing detection: the framings and their names.
- How a reading is chosen: why a message is
Exact,TemplateOnlyorInferred.