Skip to content
This repository was archived by the owner on Jan 30, 2024. It is now read-only.

use new stream decoder API #264

Merged
merged 2 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ addr2line = "0.15"
ansi_term = "0.12"
anyhow = "1.0"
colored = "2.0"
defmt-decoder = { version = "=0.2.2", features = ['unstable'] }
difference = "2.0"
either = "1.6"
gimli = "0.24"
Expand All @@ -29,6 +28,12 @@ rustc-demangle = "0.1"
signal-hook = "0.3"
structopt = "0.3"

[dependencies.defmt-decoder]
branch = "main"
features = ['unstable']
git = "https://github.com/knurling-rs/defmt"
# version = "0.3.0" # uncomment when defmt-decoder 0.3.0 is released

[dev-dependencies]
dirs = "3.0"
insta = "1.7"
Expand Down
33 changes: 17 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{

use anyhow::{anyhow, bail};
use colored::Colorize as _;
use defmt_decoder::Locations;
use defmt_decoder::{Locations, StreamDecoder};
use probe_rs::{
flashing::{self, Format},
Core,
Expand Down Expand Up @@ -220,12 +220,19 @@ fn extract_and_print_logs(
bail!("\"defmt\" RTT channel is in use, but the firmware binary contains no defmt data");
}

let mut stream_decoder = if use_defmt {
elf.defmt_table
.as_ref()
.map(|table| table.new_stream_decoder())
} else {
None
};

print_separator();

let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut read_buf = [0; 1024];
let mut defmt_buffer = vec![];
let mut was_halted = false;
while !exit.load(Ordering::Relaxed) {
if let Some(logging_channel) = &mut logging_channel {
Expand All @@ -238,13 +245,12 @@ fn extract_and_print_logs(
};

if num_bytes_read != 0 {
match &elf.defmt_table {
Some(table) if use_defmt => {
defmt_buffer.extend_from_slice(&read_buf[..num_bytes_read]);
match stream_decoder.as_mut() {
Some(stream_decoder) => {
stream_decoder.received(&read_buf[..num_bytes_read]);

decode_and_print_defmt_logs(
&mut defmt_buffer,
table,
&mut **stream_decoder,
elf.defmt_locations.as_ref(),
current_dir,
opts,
Expand Down Expand Up @@ -289,15 +295,14 @@ fn extract_and_print_logs(
}

fn decode_and_print_defmt_logs(
buffer: &mut Vec<u8>,
table: &defmt_decoder::Table,
stream_decoder: &mut dyn StreamDecoder,
locations: Option<&Locations>,
current_dir: &Path,
opts: &cli::Opts,
) -> anyhow::Result<()> {
loop {
match table.decode(buffer) {
Ok((frame, consumed)) => {
match stream_decoder.decode() {
Ok(frame) => {
// NOTE(`[]` indexing) all indices in `table` have already been verified to exist in
// the `locations` map
let (file, line, mod_path) = locations
Expand Down Expand Up @@ -325,16 +330,12 @@ fn decode_and_print_defmt_logs(

// Forward the defmt frame to our logger.
defmt_decoder::log::log_defmt(&frame, file.as_deref(), line, mod_path);

let num_bytes = buffer.len();
buffer.rotate_left(consumed);
buffer.truncate(num_bytes - consumed);
}

Err(defmt_decoder::DecodeError::UnexpectedEof) => break,

Err(defmt_decoder::DecodeError::Malformed) => {
log::error!("failed to decode defmt data: {:x?}", buffer);
log::error!("failed to decode defmt data");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, @Urhengulas this looked like the right place to modify to implement #249 (I think)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pointer!

return Err(defmt_decoder::DecodeError::Malformed.into());
}
}
Expand Down
14 changes: 4 additions & 10 deletions tests/snapshots/test__ctrl_c_by_user_is_reported_as_such.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ source: tests/test.rs
expression: run_result.output

---
(HOST) INFO flashing program (0.70 KiB)
(HOST) INFO flashing program (6.51 KiB)
(HOST) INFO success!
RTT logs not available; blocking until the device halts..
────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────
stack backtrace:
0: lib::inline::__nop
at ./asm/inline.rs:115:5
1: __nop
at ./asm/lib.rs:49:17
2: main
at /Users/lottesteenbrink/ferrous/my-app/src/bin/silent_loop.rs:8:1
3: ResetTrampoline
0: main
1: ResetTrampoline
at [cortex-m-rt-0.6.14]/src/lib.rs:547:26
4: Reset
2: Reset
at [cortex-m-rt-0.6.14]/src/lib.rs:550:13
(HOST) INFO device halted by user

17 changes: 8 additions & 9 deletions tests/snapshots/test__panic_is_reported_as_such.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
source: tests/test.rs
expression: run_output
expression: run_result.output

---
(HOST) INFO flashing program (5.82 KiB)
(HOST) INFO flashing program (6.57 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
INFO main
ERROR panicked at 'explicit panic'
ERROR panicked at 'explicit panic'
────────────────────────────────────────────────────────────────────────────────
stack backtrace:
0: HardFaultTrampoline
Expand All @@ -17,15 +16,15 @@ stack backtrace:
2: __udf
at ./asm/lib.rs:49:17
3: cortex_m::asm::udf
at [cortex-m-0.7.2]/src/asm.rs:43:5
at [cortex-m-0.7.3]/src/asm.rs:43:5
4: _defmt_panic
at /Users/lottesteenbrink/ferrous/my-app/src/lib.rs:13:5
at /tmp/app/src/lib.rs:11:5
5: defmt::export::panic
at [defmt-0.2.2]/src/export.rs:233:14
at /home/japaric/.cargo/git/checkouts/defmt-52fbd7917982cfac/e021e7d/src/export.rs:266:14
6: panic::__cortex_m_rt_main
at /Users/lottesteenbrink/ferrous/my-app/src/bin/panic.rs:10:5
at /tmp/app/src/bin/panic.rs:8:5
7: main
at /Users/lottesteenbrink/ferrous/my-app/src/bin/panic.rs:6:1
at /tmp/app/src/bin/panic.rs:6:1
8: ResetTrampoline
at [cortex-m-rt-0.6.14]/src/lib.rs:547:26
9: Reset
Expand Down
47 changes: 22 additions & 25 deletions tests/snapshots/test__panic_verbose.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,38 @@ source: tests/test.rs
expression: run_result.output

---
(HOST) DEBUG vector table: VectorTable { location: 0, initial_stack_pointer: 2003fbc8, reset: 101, hard_fault: 13e5 }
(HOST) DEBUG vector table: VectorTable { initial_stack_pointer: 2003fbc8, hard_fault: 15b1 }
(HOST) DEBUG RAM region: 0x20000000-0x2003FFFF
(HOST) DEBUG section `.data` is in RAM at 0x2003FBC8 ..= 0x2003FBF7
(HOST) DEBUG section `.bss` is in RAM at 0x2003FBF8 ..= 0x2003FBFB
(HOST) DEBUG section `.uninit` is in RAM at 0x2003FBFC ..= 0x2003FFFB
(HOST) DEBUG section `.bss` is in RAM at 0x2003FBF8 ..= 0x2003FBFF
(HOST) DEBUG section `.uninit` is in RAM at 0x2003FC00 ..= 0x2003FFFF
(HOST) DEBUG valid SP range: 0x20000000 ..= 0x2003FBC7
(HOST) DEBUG found 1 probes
(HOST) DEBUG opened probe
(HOST) DEBUG started session
(HOST) INFO flashing program (5.82 KiB)
(HOST) INFO flashing program (6.57 KiB)
(HOST) INFO success!
(HOST) DEBUG 261062 bytes of stack available (0x20000000 ..= 0x2003FBC7), using 1024 byte canary
(HOST) TRACE setting up canary took 0.013s (75.91 KiB/s)
(HOST) TRACE setting up canary took 0.012s (83.61 KiB/s)
(HOST) DEBUG starting device
(HOST) DEBUG Successfully attached RTT
────────────────────────────────────────────────────────────────────────────────
INFO main
ERROR panicked at 'explicit panic'
ERROR panicked at 'explicit panic'
────────────────────────────────────────────────────────────────────────────────
(HOST) TRACE reading canary took 0.012s (83.93 KiB/s)
(HOST) TRACE reading canary took 0.011s (92.16 KiB/s)
(HOST) DEBUG stack canary intact
(HOST) DEBUG LR=0xFFFFFFF9 PC=0x000013E4
(HOST) DEBUG LR=0x000006FF PC=0x0000071C
(HOST) DEBUG update_cfa: CFA changed Some(2003fb68) -> 2003fb70
(HOST) DEBUG LR=0x00000709 PC=0x000006FE
(HOST) DEBUG update_cfa: CFA changed Some(2003fb70) -> 2003fb78
(HOST) DEBUG LR=0x000005C5 PC=0x00000708
(HOST) DEBUG update_cfa: CFA changed Some(2003fb78) -> 2003fb80
(HOST) DEBUG LR=0x00000229 PC=0x000005C4
(HOST) DEBUG update_cfa: CFA changed Some(2003fb80) -> 2003fbb0
(HOST) DEBUG LR=0x00000195 PC=0x00000228
(HOST) DEBUG LR=0xFFFFFFF9 PC=0x000015B0
(HOST) DEBUG LR=0x000001C9 PC=0x000008DE
(HOST) DEBUG update_cfa: CFA changed Some(2003fb90) -> 2003fb98
(HOST) DEBUG LR=0x000001D3 PC=0x000001C8
(HOST) DEBUG update_cfa: CFA changed Some(2003fb98) -> 2003fba0
(HOST) DEBUG LR=0x000001BF PC=0x000001D2
(HOST) DEBUG update_cfa: CFA changed Some(2003fba0) -> 2003fbb0
(HOST) DEBUG LR=0x00000195 PC=0x000001BE
(HOST) DEBUG update_cfa: CFA changed Some(2003fbb0) -> 2003fbb8
(HOST) DEBUG LR=0x00000233 PC=0x00000194
(HOST) DEBUG LR=0x000001DD PC=0x00000194
(HOST) DEBUG update_cfa: CFA changed Some(2003fbb8) -> 2003fbc0
(HOST) DEBUG LR=0x0000018B PC=0x00000232
(HOST) DEBUG LR=0x0000018B PC=0x000001DC
(HOST) DEBUG update_cfa: CFA changed Some(2003fbc0) -> 2003fbc8
(HOST) DEBUG LR=0xFFFFFFFF PC=0x0000018A
stack backtrace:
Expand All @@ -48,15 +45,15 @@ stack backtrace:
2: __udf
at ./asm/lib.rs:49:17
3: cortex_m::asm::udf
at [cortex-m-0.7.2]/src/asm.rs:43:5
at [cortex-m-0.7.3]/src/asm.rs:43:5
4: _defmt_panic
at /Users/lottesteenbrink/ferrous/my-app/src/lib.rs:13:5
at /tmp/app/src/lib.rs:11:5
5: defmt::export::panic
at [defmt-0.2.2]/src/export.rs:233:14
at /home/japaric/.cargo/git/checkouts/defmt-52fbd7917982cfac/e021e7d/src/export.rs:266:14
6: panic::__cortex_m_rt_main
at /Users/lottesteenbrink/ferrous/my-app/src/bin/panic.rs:10:5
at /tmp/app/src/bin/panic.rs:8:5
7: main
at /Users/lottesteenbrink/ferrous/my-app/src/bin/panic.rs:6:1
at /tmp/app/src/bin/panic.rs:6:1
8: ResetTrampoline
at [cortex-m-rt-0.6.14]/src/lib.rs:547:26
9: Reset
Expand Down
12 changes: 12 additions & 0 deletions tests/snapshots/test__raw_encoding.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/test.rs
expression: run_result.output

---
(HOST) INFO flashing program (5.73 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
INFO Hello, world!
────────────────────────────────────────────────────────────────────────────────
(HOST) INFO device halted without error

Loading