probe_rs/rtt.rs
1//! Host side implementation of the RTT (Real-Time Transfer) I/O protocol over probe-rs
2//!
3//! RTT implements input and output to/from a microcontroller using in-memory ring buffers and
4//! memory polling. This enables debug logging from the microcontroller with minimal delays and no
5//! blocking, making it usable even in real-time applications where e.g. semihosting delays cannot
6//! be tolerated.
7//!
8//! This crate enables you to read and write via RTT channels. It's also used as a building-block
9//! for probe-rs debugging tools.
10//!
11//! ## Example
12//!
13//! ```no_run
14//! use probe_rs::probe::list::Lister;
15//! use probe_rs::Permissions;
16//! use probe_rs::rtt::Rtt;
17//!
18//! // First obtain a probe-rs session (see probe-rs documentation for details)
19//! let lister = Lister::new();
20//!
21//! let probes = lister.list_all();
22//!
23//! let probe = probes[0].open()?;
24//! let mut session = probe.attach("somechip", Permissions::default())?;
25//! // Select a core.
26//! let mut core = session.core(0)?;
27//!
28//! // Attach to RTT
29//! let mut rtt = Rtt::attach(&mut core)?;
30//!
31//! // Read from a channel
32//! if let Some(input) = rtt.up_channel(0) {
33//! let mut buf = [0u8; 1024];
34//! let count = input.read(&mut core, &mut buf[..])?;
35//!
36//! println!("Read data: {:?}", &buf[..count]);
37//! }
38//!
39//! // Write to a channel
40//! if let Some(output) = rtt.down_channel(0) {
41//! output.write(&mut core, b"Hello, computer!\n")?;
42//! }
43//!
44//! # Ok::<(), Box<dyn std::error::Error>>(())
45//! ```
46
47mod channel;
48pub use channel::*;
49
50use crate::Session;
51use crate::{Core, MemoryInterface, config::MemoryRegion};
52use std::ops::Range;
53use std::thread;
54use std::time::Duration;
55use std::time::Instant;
56use zerocopy::{FromBytes, IntoBytes};
57
58/// The RTT interface.
59///
60/// Use [`Rtt::attach`] or [`Rtt::attach_region`] to attach to a probe-rs [`Core`] and detect the
61/// channels, as they were configured on the target. The timing of when this is called is really
62/// important, or else unexpected results can be expected.
63///
64/// ## Examples of how timing between host and target effects the results
65///
66/// 1. **Scenario: Ideal configuration**: The host RTT interface is created **AFTER** the target
67/// program has successfully executing the RTT initialization, by calling an api such as
68/// [`rtt_target::rtt_init_print!()`](https://docs.rs/rtt-target/0.5.0/rtt_target/macro.rtt_init_print.html).
69///
70/// At this point, both the RTT Control Block and the RTT Channel configurations are present in
71/// the target memory, and this RTT interface can be expected to work as expected.
72///
73/// 2. **Scenario: Failure to detect RTT Control Block**: The target has been configured correctly,
74/// **BUT** the host creates this interface **BEFORE** the target program has initialized RTT.
75///
76/// This most commonly occurs when the target halts processing before initializing RTT. For
77/// example, this could happen ...
78/// * During debugging, if the user sets a breakpoint in the code before the RTT
79/// initialization.
80/// * After flashing, if the user has configured `probe-rs` to `reset_after_flashing` AND
81/// `halt_after_reset`. On most targets, this will result in the target halting with
82/// reason `Exception` and will delay the subsequent RTT initialization.
83/// * If RTT initialization on the target is delayed because of time consuming processing or
84/// excessive interrupt handling. This can usually be prevented by moving the RTT
85/// initialization code to the very beginning of the target program logic.
86///
87/// The result of such a timing issue is that `probe-rs` will fail to initialize RTT with an
88/// [`Error::ControlBlockNotFound`]
89///
90/// 3. **Scenario: Incorrect Channel names and incorrect Channel buffer sizes**: This scenario
91/// usually occurs when two conditions coincide. Firstly, the same timing mismatch as described
92/// in point #2 above, and secondly, the target memory has NOT been cleared since a previous
93/// version of the binary program has been flashed to the target.
94///
95/// What happens here is that the RTT Control Block is validated by reading a previously
96/// initialized RTT ID from the target memory. The next step in the logic is then to read the
97/// Channel configuration from the RTT Control block which is usually contains unreliable data
98/// at this point. The symptoms will appear as:
99/// * RTT Channel names are incorrect and/or contain unprintable characters.
100/// * RTT Channel names are correct, but no data, or corrupted data, will be reported from
101/// RTT, because the buffer sizes are incorrect.
102#[derive(Debug)]
103pub struct Rtt {
104 /// The location of the control block in target memory.
105 ptr: u64,
106
107 /// The detected up (target to host) channels.
108 pub up_channels: Vec<UpChannel>,
109
110 /// The detected down (host to target) channels.
111 pub down_channels: Vec<DownChannel>,
112}
113
114#[repr(C)]
115#[derive(FromBytes)]
116struct RttControlBlockHeaderInner {
117 id: [u8; 16],
118 max_up_channels: u32,
119 max_down_channels: u32,
120}
121
122enum RttControlBlockHeader {
123 Header32(RttControlBlockHeaderInner),
124 Header64(RttControlBlockHeaderInner),
125}
126
127impl RttControlBlockHeader {
128 pub fn try_from_header(is_64_bit: bool, mem: &[u8]) -> Option<Self> {
129 if is_64_bit {
130 RttControlBlockHeaderInner::read_from_prefix(mem)
131 .map(|(header, _)| Self::Header64(header))
132 .ok()
133 } else {
134 RttControlBlockHeaderInner::read_from_prefix(mem)
135 .map(|(header, _)| Self::Header32(header))
136 .ok()
137 }
138 }
139
140 pub const fn minimal_header_size() -> usize {
141 std::mem::size_of::<RttControlBlockHeaderInner>()
142 }
143
144 pub const fn header_size(&self) -> usize {
145 std::mem::size_of::<RttControlBlockHeaderInner>()
146 }
147
148 pub fn id(&self) -> [u8; 16] {
149 match self {
150 RttControlBlockHeader::Header32(x) => x.id,
151 RttControlBlockHeader::Header64(x) => x.id,
152 }
153 }
154
155 pub fn max_up_channels(&self) -> usize {
156 match self {
157 RttControlBlockHeader::Header32(x) => x.max_up_channels as usize,
158 RttControlBlockHeader::Header64(x) => x.max_up_channels as usize,
159 }
160 }
161
162 pub fn max_down_channels(&self) -> usize {
163 match self {
164 RttControlBlockHeader::Header32(x) => x.max_down_channels as usize,
165 RttControlBlockHeader::Header64(x) => x.max_down_channels as usize,
166 }
167 }
168
169 pub fn channel_buffer_size(&self) -> usize {
170 match self {
171 RttControlBlockHeader::Header32(_x) => RttChannelBufferInner::<u32>::size(),
172 RttControlBlockHeader::Header64(_x) => RttChannelBufferInner::<u64>::size(),
173 }
174 }
175
176 pub fn total_rtt_buffer_size(&self) -> usize {
177 let total_number_of_channels = self.max_up_channels() + self.max_down_channels();
178 let channel_size = self.channel_buffer_size();
179
180 self.header_size() + channel_size * total_number_of_channels
181 }
182
183 pub fn parse_channel_buffers(&self, mem: &[u8]) -> Result<Vec<RttChannelBuffer>, Error> {
184 let buffers = match self {
185 RttControlBlockHeader::Header32(_) => {
186 <[RttChannelBufferInner<u32>]>::ref_from_bytes(mem)
187 .map_err(|_| Error::ControlBlockNotFound)?
188 .iter()
189 .cloned()
190 .map(RttChannelBuffer::from)
191 .collect::<Vec<RttChannelBuffer>>()
192 }
193 RttControlBlockHeader::Header64(_) => {
194 <[RttChannelBufferInner<u64>]>::ref_from_bytes(mem)
195 .map_err(|_| Error::ControlBlockNotFound)?
196 .iter()
197 .cloned()
198 .map(RttChannelBuffer::from)
199 .collect::<Vec<RttChannelBuffer>>()
200 }
201 };
202
203 Ok(buffers)
204 }
205}
206
207// Rtt must follow this data layout when reading/writing memory in order to be compatible with the
208// official RTT implementation.
209//
210// struct ControlBlock {
211// char id[16]; // Used to find/validate the control block.
212// // Maximum number of up (target to host) channels in following array
213// unsigned int max_up_channels;
214// // Maximum number of down (host to target) channels in following array.
215// unsigned int max_down_channels;
216// RttChannel up_channels[max_up_channels]; // Array of up (target to host) channels.
217// RttChannel down_channels[max_down_channels]; // array of down (host to target) channels.
218// }
219impl Rtt {
220 /// The magic string expected to be found at the beginning of the RTT control block.
221 pub const RTT_ID: [u8; 16] = *b"SEGGER RTT\0\0\0\0\0\0";
222
223 /// Tries to attach to an RTT control block at the specified memory address.
224 pub fn attach_at(
225 core: &mut Core,
226 // Pointer from which to scan
227 ptr: u64,
228 ) -> Result<Rtt, Error> {
229 let is_64_bit = core.is_64_bit();
230
231 let mut mem = [0u32; RttControlBlockHeader::minimal_header_size() / 4];
232 // Read the magic value first as unordered data, and read the subsequent pointers
233 // as ordered u32 values.
234 core.read(ptr, &mut mem.as_mut_bytes()[0..Self::RTT_ID.len()])?;
235 core.read_32(
236 ptr + Self::RTT_ID.len() as u64,
237 &mut mem[Self::RTT_ID.len() / 4..],
238 )?;
239
240 let rtt_header = RttControlBlockHeader::try_from_header(is_64_bit, mem.as_bytes())
241 .ok_or(Error::ControlBlockNotFound)?;
242
243 // Validate that the control block starts with the ID bytes
244 let rtt_id = rtt_header.id();
245 if rtt_id != Self::RTT_ID {
246 tracing::trace!(
247 "Expected control block to start with RTT ID: {:?}\n. Got instead: {:?}",
248 String::from_utf8_lossy(&Self::RTT_ID),
249 String::from_utf8_lossy(&rtt_id)
250 );
251 return Err(Error::ControlBlockNotFound);
252 }
253
254 let max_up_channels = rtt_header.max_up_channels();
255 let max_down_channels = rtt_header.max_down_channels();
256
257 // *Very* conservative sanity check, most people only use a handful of RTT channels
258 if max_up_channels > 255 || max_down_channels > 255 {
259 return Err(Error::ControlBlockCorrupted(format!(
260 "Unexpected array sizes at {ptr:#010x}: max_up_channels={max_up_channels} max_down_channels={max_down_channels}"
261 )));
262 }
263
264 // Read the rest of the control block
265 let channel_buffer_len = rtt_header.total_rtt_buffer_size() - rtt_header.header_size();
266 let mut mem = vec![0; channel_buffer_len / 4];
267 core.read_32(ptr + rtt_header.header_size() as u64, &mut mem)?;
268
269 let mut up_channels = Vec::new();
270 let mut down_channels = Vec::new();
271
272 let channel_buffer_size = rtt_header.channel_buffer_size();
273
274 let up_channels_start = 0;
275 let up_channels_len = max_up_channels * channel_buffer_size;
276 let up_channels_raw_buffer = &mem.as_bytes()[up_channels_start..][..up_channels_len];
277 let up_channels_buffer = rtt_header.parse_channel_buffers(up_channels_raw_buffer)?;
278
279 let down_channels_start = up_channels_start + up_channels_len;
280 let down_channels_len = max_down_channels * channel_buffer_size;
281 let down_channels_raw_buffer = &mem.as_bytes()[down_channels_start..][..down_channels_len];
282 let down_channels_buffer = rtt_header.parse_channel_buffers(down_channels_raw_buffer)?;
283
284 let mut offset = ptr + rtt_header.header_size() as u64 + up_channels_start as u64;
285 for (channel_index, buffer) in up_channels_buffer.into_iter().enumerate() {
286 let buffer_size = buffer.size() as u64;
287
288 if let Some(chan) = Channel::from(core, channel_index, offset, buffer)? {
289 up_channels.push(UpChannel(chan));
290 } else {
291 tracing::warn!("Buffer for up channel {channel_index} not initialized");
292 }
293 offset += buffer_size;
294 }
295
296 let mut offset = ptr + rtt_header.header_size() as u64 + down_channels_start as u64;
297 for (channel_index, buffer) in down_channels_buffer.into_iter().enumerate() {
298 let buffer_size = buffer.size() as u64;
299
300 if let Some(chan) = Channel::from(core, channel_index, offset, buffer)? {
301 down_channels.push(DownChannel(chan));
302 } else {
303 tracing::warn!("Buffer for down channel {channel_index} not initialized");
304 }
305 offset += buffer_size;
306 }
307
308 Ok(Rtt {
309 ptr,
310 up_channels,
311 down_channels,
312 })
313 }
314
315 /// Attempts to detect an RTT control block in the specified RAM region(s) and returns an
316 /// instance if a valid control block was found.
317 pub fn attach_region(core: &mut Core, region: &ScanRegion) -> Result<Rtt, Error> {
318 let ptr = Self::find_contol_block(core, region)?;
319 Self::attach_at(core, ptr)
320 }
321
322 /// Attempts to detect an RTT control block anywhere in the target RAM and returns an instance
323 /// if a valid control block was found.
324 pub fn attach(core: &mut Core) -> Result<Rtt, Error> {
325 Self::attach_region(core, &ScanRegion::default())
326 }
327
328 /// Attempts to detect an RTT control block in the specified RAM region(s) and returns an
329 /// address if a valid control block location was found.
330 pub fn find_contol_block(core: &mut Core, region: &ScanRegion) -> Result<u64, Error> {
331 let ranges = match region.clone() {
332 ScanRegion::Exact(addr) => {
333 tracing::debug!("Scanning at exact address: {:#010x}", addr);
334
335 return Ok(addr);
336 }
337 ScanRegion::Ram => {
338 tracing::debug!("Scanning whole RAM");
339
340 core.memory_regions()
341 .filter_map(MemoryRegion::as_ram_region)
342 .map(|r| r.range.clone())
343 .collect()
344 }
345 ScanRegion::Ranges(regions) if regions.is_empty() => {
346 // We have no regions to scan so we cannot initialize RTT.
347 tracing::debug!(
348 "ELF file has no RTT block symbol, and this target does not support automatic scanning"
349 );
350 return Err(Error::NoControlBlockLocation);
351 }
352 ScanRegion::Ranges(regions) => {
353 tracing::debug!("Scanning regions: {:#010x?}", region);
354 regions
355 }
356 };
357
358 let mut instances = ranges
359 .into_iter()
360 .filter_map(|range| {
361 let range_len = range.end.checked_sub(range.start)?;
362 let Ok(range_len) = usize::try_from(range_len) else {
363 // FIXME: This is not ideal because it means that we
364 // won't consider a >4GiB region if probe-rs is running
365 // on a 32-bit host, but it would be relatively unusual
366 // to use a 32-bit host to debug a 64-bit target.
367 tracing::warn!("Region too long ({} bytes), ignoring", range_len);
368 return None;
369 };
370
371 let mut mem = vec![0; range_len];
372 core.read(range.start, &mut mem).ok()?;
373
374 let offset = mem
375 .windows(Self::RTT_ID.len())
376 .position(|w| w == Self::RTT_ID)?;
377
378 let target_ptr = range.start + offset as u64;
379
380 Some(target_ptr)
381 })
382 .collect::<Vec<_>>();
383
384 match instances.len() {
385 0 => Err(Error::ControlBlockNotFound),
386 1 => Ok(instances.remove(0)),
387 _ => Err(Error::MultipleControlBlocksFound(instances)),
388 }
389 }
390
391 /// Returns the memory address of the control block in target memory.
392 pub fn ptr(&self) -> u64 {
393 self.ptr
394 }
395
396 /// Returns a reference to the detected up channels.
397 pub fn up_channels(&mut self) -> &mut [UpChannel] {
398 &mut self.up_channels
399 }
400
401 /// Returns a reference to the detected down channels.
402 pub fn down_channels(&mut self) -> &mut [DownChannel] {
403 &mut self.down_channels
404 }
405
406 /// Returns a particular up channel.
407 pub fn up_channel(&mut self, channel: usize) -> Option<&mut UpChannel> {
408 self.up_channels.get_mut(channel)
409 }
410
411 /// Returns a particular down channel.
412 pub fn down_channel(&mut self, channel: usize) -> Option<&mut DownChannel> {
413 self.down_channels.get_mut(channel)
414 }
415
416 /// Returns the size of the RTT control block.
417 pub fn control_block_size() -> usize {
418 RttControlBlockHeader::minimal_header_size()
419 }
420}
421
422/// Used to specify which memory regions to scan for the RTT control block.
423#[derive(Clone, Debug, Default)]
424pub enum ScanRegion {
425 /// Scans all RAM regions known to probe-rs. This is the default and should always work, however
426 /// if your device has a lot of RAM, scanning all of it is slow.
427 #[default]
428 Ram,
429
430 /// Limit scanning to the memory addresses covered by all of the given ranges. It is up to the
431 /// user to ensure that reading from this range will not read from undefined memory.
432 Ranges(Vec<Range<u64>>),
433
434 /// Tries to find the control block starting at this exact address. It is up to the user to
435 /// ensure that reading the necessary bytes after the pointer will no read from undefined
436 /// memory.
437 Exact(u64),
438}
439
440impl ScanRegion {
441 /// Creates a new `ScanRegion` that scans the given memory range.
442 ///
443 /// The memory range should be in a single memory block of the target.
444 pub fn range(range: Range<u64>) -> Self {
445 Self::Ranges(vec![range])
446 }
447}
448
449/// Error type for RTT operations.
450#[derive(thiserror::Error, Debug, docsplay::Display)]
451pub enum Error {
452 /// There is no control block location given. This usually means RTT is not present in the
453 /// firmware.
454 NoControlBlockLocation,
455
456 /// RTT control block not found in target memory.
457 /// - Make sure RTT is initialized on the target, AND that there are NO target breakpoints before RTT initialization.
458 /// - For VSCode and probe-rs-debugger users, using `halt_after_reset:true` in your `launch.json` file will prevent RTT
459 /// initialization from happening on time.
460 /// - Depending on the target, sleep modes can interfere with RTT.
461 ControlBlockNotFound,
462
463 /// Multiple control blocks found in target memory: {display_list(_0)}.
464 MultipleControlBlocksFound(Vec<u64>),
465
466 /// The control block has been corrupted: {0}
467 ControlBlockCorrupted(String),
468
469 /// Attempted an RTT operation against a Core number that is different from the Core number against which RTT was initialized. Expected {0}, found {1}
470 IncorrectCoreSpecified(usize, usize),
471
472 /// Error communicating with the probe.
473 Probe(#[from] crate::Error),
474
475 /// Unexpected error while reading {0} from target memory. Please report this as a bug.
476 MemoryRead(String),
477
478 /// Some uncategorized error occurred.
479 Other(#[from] anyhow::Error),
480
481 /// The read pointer changed unexpectedly.
482 ReadPointerChanged,
483
484 /// Channel {0} does not exist.
485 MissingChannel(usize),
486}
487
488fn display_list(list: &[u64]) -> String {
489 list.iter()
490 .map(|ptr| format!("{ptr:#010x}"))
491 .collect::<Vec<_>>()
492 .join(", ")
493}
494
495fn try_attach_to_rtt_inner(
496 mut try_attach_once: impl FnMut() -> Result<Rtt, Error>,
497 timeout: Duration,
498) -> Result<Rtt, Error> {
499 let t = Instant::now();
500 let mut attempt = 1;
501 loop {
502 tracing::debug!("Initializing RTT (attempt {attempt})...");
503
504 match try_attach_once() {
505 err @ Err(Error::NoControlBlockLocation) => return err,
506 Err(_) if t.elapsed() < timeout => {
507 attempt += 1;
508 tracing::debug!("Failed to initialize RTT. Retrying until timeout.");
509 thread::sleep(Duration::from_millis(50));
510 }
511 other => return other,
512 }
513 }
514}
515
516/// Try to attach to RTT, with the given timeout.
517pub fn try_attach_to_rtt(
518 core: &mut Core<'_>,
519 timeout: Duration,
520 rtt_region: &ScanRegion,
521) -> Result<Rtt, Error> {
522 try_attach_to_rtt_inner(|| Rtt::attach_region(core, rtt_region), timeout)
523}
524
525/// Try to attach to RTT, with the given timeout.
526pub fn try_attach_to_rtt_shared(
527 session: &parking_lot::FairMutex<Session>,
528 core_id: usize,
529 timeout: Duration,
530 rtt_region: &ScanRegion,
531) -> Result<Rtt, Error> {
532 try_attach_to_rtt_inner(
533 || {
534 let mut session_handle = session.lock();
535 let mut core = session_handle.core(core_id)?;
536 Rtt::attach_region(&mut core, rtt_region)
537 },
538 timeout,
539 )
540}
541
542#[cfg(test)]
543mod test {
544 use super::*;
545
546 #[test]
547 fn test_how_control_block_list_looks() {
548 let error = Error::MultipleControlBlocksFound(vec![0x2000, 0x3000]);
549 assert_eq!(
550 error.to_string(),
551 "Multiple control blocks found in target memory: 0x00002000, 0x00003000."
552 );
553 }
554}