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

Decode defmt logs

A firmware that logs with defmt writes compact frames, not text. nanoflash monitor --elf decodes them with the format strings of the ELF file of the firmware.

Requirements

  • The firmware frames each defmt frame as esp-println does: the two bytes ff 00, the frame, and one byte 00. A plain stream of frames, as defmt-serial writes it, is not supported.
  • The frames use the rzcobs encoding, the default of defmt. The raw encoding is refused.
  • The ELF file is the one that the board runs. Its .defmt section holds the table of the format strings.

Decode the output

$ nanoflash monitor --board boards/esp32-s3.toml --elf target/xtensa-esp32s3-none-elf/release/firmware
boot
[INFO ] temperature 21 (src/main.rs:12)
  • A frame is shown on its own line: [INFO ] message (file:line). The location is shown only when the ELF file has debug information for the statement. The timestamp of the firmware, if it defines one, comes first.
  • The text between frames is shown as it is.
  • A frame that cannot be decoded (a statement that the table does not have, or damaged bytes) is shown as [MALFORMED defmt frame].
  • When the device leaves, a frame that was not finished is malformed. The text after the device comes back is not hidden.

--elf with a file that has no defmt table, or that uses the raw encoding, is an input error. Without --elf, the bytes of a frame are shown as text.

nanoflash run decodes the frames by itself when its image is an ELF file with a defmt table. When the table cannot be used, run writes a note on standard error and does not decode.

Get JSON

With --json, a frame is a record:

{"timestamp":"2026-11-24T09:30:01.412Z","defmt":{"level":"info","message":"temperature 21","device_timestamp":null,"location":{"file":"src/main.rs","line":12,"module":"firmware"}}}

location is null without debug information. The text before a frame is emitted first, as a line with "complete":false when it has no line feed. A malformed frame is the event malformed_defmt_frame.

Limits

  • The framing finds a frame by its first bytes. A ff 00 in plain text (output at another baud rate, a binary dump) starts a frame: the bytes up to the next 00 are not text. A text-only firmware may print no 00, so its output is hidden until a 00 byte comes, and the monitor shows one [MALFORMED defmt frame].
  • A frame is at most 4096 bytes. A longer frame is malformed, and the bytes up to the next 00 are dropped.
  • defmt-decoder 1.2.0, the crate that decodes the frames, can end the process on bytes that claim a very long slice of Format values ({=[?]}). It asks for the memory of the whole count before it reads any element, and the monitor cannot catch that. See issue 8.

Troubleshooting: the terminal stays in raw mode

When the process ends on the defmt-decoder failure above, the terminal stays in raw mode, because the end skips the code that restores it. Run reset (or stty sane) to restore the terminal.

The serial monitor and defmt explains how the decoder works.