1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use crate::*;
use anyhow::{anyhow, Result};
use defmt_decoder::DecodeError;
use num_traits::Zero;
use probe_rs::config::MemoryRegion;
pub use probe_rs::rtt::ChannelMode;
use probe_rs::rtt::{DownChannel, Rtt, ScanRegion, UpChannel};
use probe_rs::Core;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::{
    fmt,
    fmt::Write,
    fs,
    io::{Read, Seek},
    str::FromStr,
};
use time::{OffsetDateTime, UtcOffset};

pub fn attach_to_rtt(
    core: &mut Core,
    memory_map: &[MemoryRegion],
    elf_file: &Path,
    rtt_config: &RttConfig,
    timestamp_offset: UtcOffset,
) -> Result<crate::rtt::RttActiveTarget, anyhow::Error> {
    log::info!("Initializing RTT");
    let rtt_header_address = if let Ok(mut file) = File::open(elf_file) {
        if let Some(address) = RttActiveTarget::get_rtt_symbol(&mut file) {
            ScanRegion::Exact(address as u32)
        } else {
            ScanRegion::Ram
        }
    } else {
        ScanRegion::Ram
    };

    match Rtt::attach_region(core, memory_map, &rtt_header_address) {
        Ok(rtt) => {
            log::info!("RTT initialized.");
            let app = RttActiveTarget::new(rtt, elf_file, rtt_config, timestamp_offset)?;
            Ok(app)
        }
        Err(err) => Err(anyhow!("Error attempting to attach to RTT: {}", err)),
    }
}

/// Used by serde to provide defaults for `RttConfig`
fn default_channel_formats() -> Vec<RttChannelConfig> {
    vec![]
}

/// Used by serde to provide defaults for `RttChannelConfig::show_location`
fn default_include_location() -> bool {
    // Setting this to true to allow compatibility with behaviour prior to when this option was introduced.
    true
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum DataFormat {
    String,
    BinaryLE,
    Defmt,
}
impl FromStr for DataFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let src = s.to_ascii_lowercase();
        match &src.to_ascii_lowercase()[..] {
            // A forgiving/case-insensitive match
            "string" => Ok(Self::String),
            "binaryle" => Ok(Self::BinaryLE),
            "defmt" => Ok(Self::Defmt),
            _ => Err(format!("{src} is not a valid format")),
        }
    }
}
impl Default for DataFormat {
    fn default() -> Self {
        DataFormat::String
    }
}

/// The initial configuration for RTT (Real Time Transfer). This configuration is complimented with the additional information specified for each of the channels in `RttChannel`.
#[derive(clap::Parser, Debug, Clone, Deserialize, Default)]
pub struct RttConfig {
    #[structopt(skip)]
    #[serde(default, rename = "rttEnabled")]
    pub enabled: bool,
    /// Configure data_format and show_timestamps for select channels
    #[structopt(skip)]
    #[serde(default = "default_channel_formats", rename = "rttChannelFormats")]
    pub channels: Vec<RttChannelConfig>,
}

/// The User specified configuration for each active RTT Channel. The configuration is passed via a DAP Client configuration (`launch.json`). If no configuration is specified, the defaults will be `Dataformat::String` and `show_timestamps=false`.
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct RttChannelConfig {
    pub channel_number: Option<usize>,
    pub channel_name: Option<String>,
    #[serde(default)]
    pub data_format: DataFormat,
    #[structopt(skip)]
    #[serde(default)]
    // Control the inclusion of timestamps for DataFormat::String.
    pub show_timestamps: bool,
    #[structopt(skip)]
    #[serde(default = "default_include_location")]
    // Control the inclusion of source location information for DataFormat::Defmt.
    pub show_location: bool,
}

/// This is the primary interface through which RTT channel data is read and written. Every actual RTT channel has a configuration and buffer that is used for this purpose.
#[derive(Debug)]
pub struct RttActiveChannel {
    pub up_channel: Option<UpChannel>,
    pub down_channel: Option<DownChannel>,
    pub channel_name: String,
    pub data_format: DataFormat,
    /// Data that will be written to the down_channel (host to target)
    _input_data: String,
    rtt_buffer: RttBuffer,
    show_timestamps: bool,
    show_location: bool,

    /// UTC offset used for creating timestamps
    ///
    /// Getting the offset can fail in multi-threaded programs,
    /// so it needs to be stored.
    timestamp_offset: UtcOffset,
}

/// A fully configured RttActiveChannel. The configuration will always try to 'default' based on information read from the RTT control block in the binary. Where insufficient information is available, it will use the supplied configuration, with final hardcoded defaults where no other information was available.
impl RttActiveChannel {
    pub fn new(
        up_channel: Option<UpChannel>,
        down_channel: Option<DownChannel>,
        channel_config: Option<RttChannelConfig>,
        timestamp_offset: UtcOffset,
    ) -> Self {
        let full_config = match &channel_config {
            Some(channel_config) => channel_config.clone(),
            None => RttChannelConfig {
                ..Default::default() // Will set intelligent defaults below ...
            },
        };
        let buffer_size: usize = up_channel
            .as_ref()
            .map(|up| up.buffer_size())
            .or_else(|| down_channel.as_ref().map(|down| down.buffer_size()))
            .unwrap_or(1024); // If no explicit config is requested, assign a default
        let defmt_enabled: bool = up_channel
            .as_ref()
            .map(|up| up.name() == Some("defmt"))
            .or_else(|| {
                down_channel
                    .as_ref()
                    .map(|down| down.name() == Some("defmt"))
            })
            .unwrap_or(false); // If no explicit config is requested, assign a default
        let (data_format, show_location) = if defmt_enabled {
            let show_location = if let Some(channel_config) = channel_config {
                channel_config.show_location
            } else {
                true
            };
            (DataFormat::Defmt, show_location)
        } else {
            (full_config.data_format, false)
        };
        let name = up_channel
            .as_ref()
            .and_then(|up| up.name().map(Into::into))
            .or_else(|| {
                down_channel
                    .as_ref()
                    .and_then(|down| down.name().map(Into::into))
            })
            .or_else(|| full_config.clone().channel_name)
            .unwrap_or_else(|| {
                format!(
                    "Unnamed {:?} RTT channel - {}",
                    data_format,
                    full_config.channel_number.unwrap_or(0)
                )
            });
        Self {
            up_channel,
            down_channel,
            channel_name: name,
            data_format,
            _input_data: String::new(),
            rtt_buffer: RttBuffer::new(buffer_size),
            show_timestamps: full_config.show_timestamps,
            show_location,
            timestamp_offset,
        }
    }

    /// Returns the number of the `UpChannel`.
    pub fn number(&self) -> Option<usize> {
        self.up_channel.as_ref().map(|uc| uc.number())
    }

    /// Polls the RTT target for new data on the channel represented by `self`.
    /// Processes all the new data into the channel internal buffer and returns the number of bytes that was read.
    pub fn poll_rtt(&mut self, core: &mut Core) -> Option<usize> {
        if let Some(channel) = self.up_channel.as_mut() {
            // Retry loop, in case the probe is temporarily unavailable, e.g. user pressed the `reset` button.
            for _loop_count in 0..10 {
                match channel.read(core, self.rtt_buffer.0.as_mut()) {
                    Ok(count) => {
                        if count.is_zero() {
                            return None;
                        } else {
                            return Some(count);
                        }
                    }
                    Err(err) => {
                        if matches!(err, probe_rs::rtt::Error::Probe(_)) {
                            std::thread::sleep(std::time::Duration::from_millis(50));
                        } else {
                            log::error!("\nError reading from RTT: {}", err);
                            return None;
                        }
                    }
                }
            }
        }
        None
    }

    /// Retrieves available data from the channel and if available, returns `Some(channel_number:String, formatted_data:String)`.
    /// If no data is available, or we encounter a recoverable error, it returns `None` value fore `formatted_data`.
    /// Non-recoverable errors are propagated to the caller.
    pub fn get_rtt_data(
        &mut self,
        core: &mut Core,
        defmt_state: Option<&(defmt_decoder::Table, Option<defmt_decoder::Locations>)>,
    ) -> Result<Option<(String, String)>, anyhow::Error> {
        self
            .poll_rtt(core)
            .map(|bytes_read| {
                Ok((
                    self.number().unwrap_or(0).to_string(), // If the Channel doesn't have a number, then send the output to channel 0
                    {
                        let mut formatted_data = String::new();
                        match self.data_format {
                            DataFormat::String => {
                                let incoming = String::from_utf8_lossy(&self.rtt_buffer.0[..bytes_read]).to_string();
                                for (_i, line) in incoming.split_terminator('\n').enumerate() {
                                    if self.show_timestamps {
                                        write!(formatted_data, "{} :", OffsetDateTime::now_utc().to_offset(self.timestamp_offset))
                                            .map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                    }
                                    writeln!(formatted_data, "{line}").map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                }
                            }
                            DataFormat::BinaryLE => {
                                for element in &self.rtt_buffer.0[..bytes_read] {
                                    // Width of 4 allows 0xFF to be printed.
                                    write!(formatted_data, "{element:#04x}").map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                }
                            }
                            DataFormat::Defmt => {
                                match defmt_state {
                                    Some((table, locs)) => {
                                        let mut stream_decoder = table.new_stream_decoder();
                                        stream_decoder.received(&self.rtt_buffer.0[..bytes_read]);
                                        loop {
                                            match stream_decoder.decode() {
                                                Ok(frame) => {
                                                    let loc = locs.as_ref().and_then(|locs| locs.get(&frame.index()) );
                                                    writeln!(formatted_data, "{}", frame.display(false)).map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                                    if self.show_location {
                                                        if let Some(loc) = loc {
                                                            let relpath = if let Ok(relpath) =
                                                                loc.file.strip_prefix(&std::env::current_dir().unwrap())
                                                            {
                                                                relpath
                                                            } else {
                                                                // not relative; use full path
                                                                &loc.file
                                                            };
                                                            writeln!(formatted_data,
                                                                "└─ {}:{}",
                                                                relpath.display(),
                                                                loc.line
                                                            ).map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                                        } else {
                                                            writeln!(formatted_data, "└─ <invalid location: defmt frame-index: {}>", frame.index()).map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                                        }
                                                    }
                                                    continue;
                                                },
                                                Err(DecodeError::UnexpectedEof) => break,
                                                Err(DecodeError::Malformed) => match table.encoding().can_recover() {
                                                    // If recovery is impossible, break out of here and propagate the error.
                                                    false => {
                                                        return Err(anyhow!("Unrecoverable error while decoding Defmt data and some data may have been lost: {:?}", DecodeError::Malformed));
                                                    },
                                                    // If recovery is possible, skip the current frame and continue with new data.
                                                    true => continue,
                                                },

                                            }
                                        }
                                    }
                                    None => {
                                        write!(formatted_data, "Running rtt in defmt mode but table or locations could not be loaded.")
                                            .map_or_else(|err| log::error!("Failed to format RTT data - {:?}", err), |r|r);
                                    }
                                }
                            }
                        };
                        formatted_data
                    }
                ))
            }).transpose()
    }

    pub fn _push_rtt(&mut self, core: &mut Core) {
        if let Some(down_channel) = self.down_channel.as_mut() {
            self._input_data += "\n";
            down_channel
                .write(core, self._input_data.as_bytes())
                .unwrap();
            self._input_data.clear();
        }
    }
}

/// Once an active connection with the Target RTT control block has been established, we configure each of the active channels, and hold essential state information for successfull communication.
#[derive(Debug)]
pub struct RttActiveTarget {
    pub active_channels: Vec<RttActiveChannel>,
    pub defmt_state: Option<(defmt_decoder::Table, Option<defmt_decoder::Locations>)>,
}

impl RttActiveTarget {
    /// RttActiveTarget collects references to all the `RttActiveChannel`s, for latter polling/pushing of data.
    pub fn new(
        mut rtt: probe_rs::rtt::Rtt,
        elf_file: &Path,
        rtt_config: &RttConfig,
        timestamp_offset: UtcOffset,
    ) -> Result<Self> {
        let mut active_channels = Vec::new();
        // For each channel configured in the RTT Control Block (`Rtt`), check if there are additional user configuration in a `RttChannelConfig`. If not, apply defaults.
        let up_channels = rtt.up_channels().drain();
        let down_channels = rtt.down_channels().drain();
        for channel in up_channels {
            let number = channel.number();
            let channel_config = rtt_config
                .channels
                .clone()
                .into_iter()
                .find(|channel| channel.channel_number == Some(number));
            active_channels.push(RttActiveChannel::new(
                Some(channel),
                None,
                channel_config,
                timestamp_offset,
            ));
        }

        for channel in down_channels {
            let number = channel.number();
            let channel_config = rtt_config
                .channels
                .clone()
                .into_iter()
                .find(|channel| channel.channel_number == Some(number));
            active_channels.push(RttActiveChannel::new(
                None,
                Some(channel),
                channel_config,
                timestamp_offset,
            ));
        }

        // It doesn't make sense to pretend RTT is active, if there are no active channels
        if active_channels.is_empty() {
            return Err(anyhow!(
                "RTT Initialized correctly, but there were no active channels configured"
            ));
        }

        let defmt_enabled = active_channels
            .iter()
            .any(|elem| elem.data_format == DataFormat::Defmt);
        let defmt_state = if defmt_enabled {
            let elf = fs::read(elf_file).map_err(|err| {
                anyhow!(
                    "Error reading program binary while initalizing RTT: {}",
                    err
                )
            })?;
            if let Some(table) = defmt_decoder::Table::parse(&elf)? {
                let locs = {
                    let locs = table.get_locations(&elf)?;

                    if !table.is_empty() && locs.is_empty() {
                        log::warn!("Insufficient DWARF info; compile your program with `debug = 2` to enable location info.");
                        None
                    } else if table.indices().all(|idx| locs.contains_key(&(idx as u64))) {
                        Some(locs)
                    } else {
                        log::warn!(
                            "Location info is incomplete; it will be omitted from the output."
                        );
                        None
                    }
                };
                Some((table, locs))
            } else {
                log::warn!("No `Table` definition in DWARF info; compile your program with `debug = 2` to enable location info.");
                None
            }
        } else {
            None
        };

        Ok(Self {
            active_channels,
            defmt_state,
        })
    }

    pub fn get_rtt_symbol<T: Read + Seek>(file: &mut T) -> Option<u64> {
        let mut buffer = Vec::new();
        if file.read_to_end(&mut buffer).is_ok() {
            if let Ok(binary) = goblin::elf::Elf::parse(buffer.as_slice()) {
                for sym in &binary.syms {
                    if let Some(name) = binary.strtab.get_at(sym.st_name) {
                        if name == "_SEGGER_RTT" {
                            return Some(sym.st_value);
                        }
                    }
                }
            }
        }

        log::warn!("No RTT header info was present in the ELF file. Does your firmware run RTT?");
        None
    }

    /// Polls the RTT target on all channels and returns available data.
    /// Errors on any channel will be ignored and the data (even if incomplete) from the other channels will be returned.
    #[deprecated(
        since = "0.14.0",
        note = "This function is deprecated and will be removed in a future version. Please use `poll_rtt_fallible` instead."
    )]
    pub fn poll_rtt(&mut self, core: &mut Core) -> HashMap<String, String> {
        let defmt_state = self.defmt_state.as_ref();
        self.active_channels
            .iter_mut()
            .filter_map(|active_channel| {
                active_channel
                    .get_rtt_data(core, defmt_state)
                    .unwrap_or_default()
            })
            .collect::<HashMap<_, _>>()
    }

    /// Polls the RTT target on all channels and returns available data.
    /// An error on any channel will return an error instead of incomplete data.
    pub fn poll_rtt_fallible(
        &mut self,
        core: &mut Core,
    ) -> Result<HashMap<String, String>, anyhow::Error> {
        let defmt_state = self.defmt_state.as_ref();
        let mut data = HashMap::new();
        for channel in self.active_channels.iter_mut() {
            if let Some((channel, formatted_data)) = channel.get_rtt_data(core, defmt_state)? {
                data.insert(channel, formatted_data);
            }
        }
        Ok(data)
    }

    // pub fn push_rtt(&mut self) {
    //     self.tabs[self.current_tab].push_rtt();
    // }
}

struct RttBuffer(Vec<u8>);
impl RttBuffer {
    /// Initialize the buffer and ensure it has enough capacity to match the size of the RTT channel on the target at the time of instantiation. Doing this now prevents later performance impact if the buffer capacity has to be grown dynamically.
    pub fn new(mut buffer_size: usize) -> RttBuffer {
        let mut rtt_buffer = vec![0u8; 1];
        while buffer_size > 0 {
            buffer_size -= 1;
            rtt_buffer.push(0u8);
        }
        RttBuffer(rtt_buffer)
    }
}
impl fmt::Debug for RttBuffer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}