probe_rs/
session.rs

1use crate::{
2    Core, CoreType, Error,
3    architecture::{
4        arm::{
5            ArmError, SwoReader,
6            communication_interface::ArmDebugInterface,
7            component::{TraceSink, get_arm_components},
8            dp::DpAddress,
9            memory::CoresightComponent,
10            sequences::{ArmDebugSequence, DefaultArmSequence},
11        },
12        riscv::communication_interface::{
13            RiscvCommunicationInterface, RiscvDebugInterfaceState, RiscvError,
14        },
15        xtensa::communication_interface::{
16            XtensaCommunicationInterface, XtensaDebugInterfaceState, XtensaError,
17        },
18    },
19    config::{CoreExt, DebugSequence, RegistryError, Target, TargetSelector, registry::Registry},
20    core::{Architecture, CombinedCoreState},
21    probe::{
22        AttachMethod, DebugProbeError, Probe, ProbeCreationError, WireProtocol,
23        fake_probe::FakeProbe, list::Lister,
24    },
25};
26use std::ops::DerefMut;
27use std::{fmt, sync::Arc, time::Duration};
28
29/// The `Session` struct represents an active debug session.
30///
31/// ## Creating a session
32/// The session can be created by calling the [Session::auto_attach()] function,
33/// which tries to automatically select a probe, and then connect to the target.
34///
35/// For more control, the [Probe::attach()] and [Probe::attach_under_reset()]
36/// methods can be used to open a `Session` from a specific [Probe].
37///
38/// # Usage
39/// The Session is the common handle that gives a user exclusive access to an active probe.
40/// You can create and share a session between threads to enable multiple stakeholders (e.g. GDB and RTT) to access the target taking turns, by using `Arc<FairMutex<Session>>`.
41///
42/// If you do so, make sure that both threads sleep in between tasks such that other stakeholders may take their turn.
43///
44/// To get access to a single [Core] from the `Session`, the [Session::core()] method can be used.
45/// Please see the [Session::core()] method for more usage guidelines.
46///
47#[derive(Debug)]
48pub struct Session {
49    target: Target,
50    interfaces: ArchitectureInterface,
51    cores: Vec<CombinedCoreState>,
52    configured_trace_sink: Option<TraceSink>,
53}
54
55/// The `SessionConfig` struct is used to configure a new `Session` during auto-attach.
56///
57/// ## Configuring auto attach
58/// The SessionConfig can be used to control the behavior of the auto-attach function.
59/// It should be used in the [Session::auto_attach()] method.
60/// This includes setting the speed of the probe and the protocol to use, as well as the permissions.
61///
62#[derive(Default, Debug)]
63pub struct SessionConfig {
64    /// Debug permissions
65    pub permissions: Permissions,
66    /// Speed of the WireProtocol in kHz
67    pub speed: Option<u32>,
68    /// WireProtocol to use
69    pub protocol: Option<WireProtocol>,
70}
71
72enum JtagInterface {
73    Riscv(RiscvDebugInterfaceState),
74    Xtensa(XtensaDebugInterfaceState),
75    Unknown,
76}
77
78impl JtagInterface {
79    /// Returns the debug module's intended architecture.
80    fn architecture(&self) -> Option<Architecture> {
81        match self {
82            JtagInterface::Riscv(_) => Some(Architecture::Riscv),
83            JtagInterface::Xtensa(_) => Some(Architecture::Xtensa),
84            JtagInterface::Unknown => None,
85        }
86    }
87}
88
89impl fmt::Debug for JtagInterface {
90    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91        match self {
92            JtagInterface::Riscv(_) => f.write_str("Riscv(..)"),
93            JtagInterface::Xtensa(_) => f.write_str("Xtensa(..)"),
94            JtagInterface::Unknown => f.write_str("Unknown"),
95        }
96    }
97}
98
99// TODO: this is somewhat messy because I omitted separating the Probe out of the ARM interface.
100enum ArchitectureInterface {
101    Arm(Box<dyn ArmDebugInterface + 'static>),
102    Jtag(Probe, Vec<JtagInterface>),
103}
104
105impl fmt::Debug for ArchitectureInterface {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        match self {
108            ArchitectureInterface::Arm(_) => f.write_str("ArchitectureInterface::Arm(..)"),
109            ArchitectureInterface::Jtag(_, ifaces) => f
110                .debug_tuple("ArchitectureInterface::Jtag(..)")
111                .field(ifaces)
112                .finish(),
113        }
114    }
115}
116
117impl ArchitectureInterface {
118    fn attach<'probe, 'target: 'probe>(
119        &'probe mut self,
120        target: &'probe Target,
121        combined_state: &'probe mut CombinedCoreState,
122    ) -> Result<Core<'probe>, Error> {
123        match self {
124            ArchitectureInterface::Arm(interface) => combined_state.attach_arm(target, interface),
125            ArchitectureInterface::Jtag(probe, ifaces) => {
126                let idx = combined_state.jtag_tap_index();
127                if let Some(probe) = probe.try_as_jtag_probe() {
128                    probe.select_target(idx)?;
129                }
130                match &mut ifaces[idx] {
131                    JtagInterface::Riscv(state) => {
132                        let factory = probe.try_get_riscv_interface_builder()?;
133                        let iface = factory.attach_auto(target, state)?;
134                        combined_state.attach_riscv(target, iface)
135                    }
136                    JtagInterface::Xtensa(state) => {
137                        let iface = probe.try_get_xtensa_interface(state)?;
138                        combined_state.attach_xtensa(target, iface)
139                    }
140                    JtagInterface::Unknown => {
141                        unreachable!(
142                            "Tried to attach to unknown interface {idx}. This should never happen."
143                        )
144                    }
145                }
146            }
147        }
148    }
149}
150
151impl Session {
152    /// Open a new session with a given debug target.
153    pub(crate) fn new(
154        probe: Probe,
155        target: TargetSelector,
156        attach_method: AttachMethod,
157        permissions: Permissions,
158        registry: &Registry,
159    ) -> Result<Self, Error> {
160        let (probe, target) = get_target_from_selector(target, attach_method, probe, registry)?;
161
162        let cores = target
163            .cores
164            .iter()
165            .enumerate()
166            .map(|(id, core)| {
167                Core::create_state(
168                    id,
169                    core.core_access_options.clone(),
170                    &target,
171                    core.core_type,
172                )
173            })
174            .collect();
175
176        let mut session = if let Architecture::Arm = target.architecture() {
177            Self::attach_arm_debug_interface(probe, target, attach_method, permissions, cores)?
178        } else {
179            Self::attach_jtag(probe, target, attach_method, permissions, cores)?
180        };
181
182        session.clear_all_hw_breakpoints()?;
183
184        Ok(session)
185    }
186
187    fn attach_arm_debug_interface(
188        mut probe: Probe,
189        target: Target,
190        attach_method: AttachMethod,
191        permissions: Permissions,
192        cores: Vec<CombinedCoreState>,
193    ) -> Result<Self, Error> {
194        let default_core = target.default_core();
195
196        let default_memory_ap = default_core.memory_ap().ok_or_else(|| {
197            Error::Other(format!(
198                "Unable to connect to core {default_core:?}, no memory AP configured"
199            ))
200        })?;
201
202        let default_dp = default_memory_ap.dp();
203
204        let sequence_handle = match &target.debug_sequence {
205            DebugSequence::Arm(sequence) => sequence.clone(),
206            _ => unreachable!("Mismatch between architecture and sequence type!"),
207        };
208
209        if AttachMethod::UnderReset == attach_method {
210            let _span = tracing::debug_span!("Asserting hardware reset").entered();
211
212            if let Some(dap_probe) = probe.try_as_dap_probe() {
213                sequence_handle.reset_hardware_assert(dap_probe)?;
214            } else {
215                tracing::info!(
216                    "Custom reset sequences are not supported on {}.",
217                    probe.get_name()
218                );
219                tracing::info!("Falling back to standard probe reset.");
220                probe.target_reset_assert()?;
221            }
222        }
223
224        if let Some(jtag) = target.jtag.as_ref()
225            && let Some(scan_chain) = jtag.scan_chain.clone()
226            && let Some(probe) = probe.try_as_jtag_probe()
227        {
228            probe.set_scan_chain(&scan_chain)?;
229        }
230
231        probe.attach_to_unspecified()?;
232        if probe.protocol() == Some(WireProtocol::Jtag)
233            && let Some(probe) = probe.try_as_jtag_probe()
234            && let Ok(chain) = probe.scan_chain()
235            && !chain.is_empty()
236        {
237            for core in &cores {
238                probe.select_target(core.jtag_tap_index())?;
239            }
240        }
241
242        let mut interface = probe
243            .try_into_arm_debug_interface(sequence_handle.clone())
244            .map_err(|(_, err)| err)?;
245
246        interface.select_debug_port(default_dp)?;
247
248        let unlock_span = tracing::debug_span!("debug_device_unlock").entered();
249
250        // Enable debug mode
251        let unlock_res =
252            sequence_handle.debug_device_unlock(&mut *interface, &default_memory_ap, &permissions);
253        drop(unlock_span);
254
255        match unlock_res {
256            Ok(()) => (),
257            // In case this happens after unlock. Try to re-attach the probe once.
258            Err(ArmError::ReAttachRequired) => {
259                Self::reattach_arm_interface(&mut interface, &sequence_handle)?;
260            }
261            Err(e) => return Err(Error::Arm(e)),
262        }
263
264        if attach_method == AttachMethod::UnderReset {
265            {
266                for core in &cores {
267                    core.arm_reset_catch_set(&mut *interface)?;
268                }
269
270                let reset_hardware_deassert =
271                    tracing::debug_span!("reset_hardware_deassert").entered();
272
273                // A timeout here indicates that the reset pin is probably not properly connected.
274                if let Err(e) =
275                    sequence_handle.reset_hardware_deassert(&mut *interface, &default_memory_ap)
276                {
277                    if matches!(e, ArmError::Timeout) {
278                        tracing::warn!(
279                            "Timeout while deasserting hardware reset pin. This indicates that the reset pin is not properly connected. Please check your hardware setup."
280                        );
281                    }
282
283                    return Err(e.into());
284                }
285                drop(reset_hardware_deassert);
286            }
287
288            // Now that hardware reset is de-asserted, for each core, setup debugging.
289            // (Some chips keep cores in power-down under hardware-reset.)
290            for core in &cores {
291                core.enable_arm_debug(&mut *interface)?;
292            }
293
294            let mut session = Session {
295                target,
296                interfaces: ArchitectureInterface::Arm(interface),
297                cores,
298                configured_trace_sink: None,
299            };
300
301            {
302                // Wait for the core to be halted. The core should be
303                // halted because we set the `reset_catch` earlier, which
304                // means that the core should stop when coming out of reset.
305
306                for core_id in 0..session.cores.len() {
307                    let mut core = session.core(core_id)?;
308
309                    core.wait_for_core_halted(Duration::from_millis(100))?;
310
311                    core.reset_catch_clear()?;
312                }
313            }
314
315            Ok(session)
316        } else {
317            // For each core, setup debugging
318            for core in &cores {
319                core.enable_arm_debug(&mut *interface)?;
320            }
321
322            Ok(Session {
323                target,
324                interfaces: ArchitectureInterface::Arm(interface),
325                cores,
326                configured_trace_sink: None,
327            })
328        }
329    }
330
331    fn attach_jtag(
332        mut probe: Probe,
333        target: Target,
334        _attach_method: AttachMethod,
335        _permissions: Permissions,
336        cores: Vec<CombinedCoreState>,
337    ) -> Result<Self, Error> {
338        // While we still don't support mixed architectures
339        // (they'd need per-core debug sequences), we can at least
340        // handle most of the setup in the same way.
341        if let Some(jtag) = target.jtag.as_ref()
342            && let Some(scan_chain) = jtag.scan_chain.clone()
343            && let Some(probe) = probe.try_as_jtag_probe()
344        {
345            probe.set_scan_chain(&scan_chain)?;
346        }
347
348        probe.attach_to_unspecified()?;
349
350        // We try to guess the TAP number. Normally we trust the scan chain, but some probes are
351        // only quasi-JTAG (wch-link), so we'll have to work with at least 1, but if we're guessing
352        // we can also use the highest number specified in the target YAML.
353
354        // FIXME: This is terribly JTAG-specific. Since we don't really support anything else yet,
355        // it should be fine for now.
356        let highest_idx = cores.iter().map(|c| c.jtag_tap_index()).max().unwrap_or(0);
357        let tap_count = if let Some(probe) = probe.try_as_jtag_probe() {
358            match probe.scan_chain() {
359                Ok(scan_chain) => scan_chain.len().max(highest_idx + 1),
360                Err(_) => highest_idx + 1,
361            }
362        } else {
363            highest_idx + 1
364        };
365        let mut interfaces = std::iter::repeat_with(|| JtagInterface::Unknown)
366            .take(tap_count)
367            .collect::<Vec<_>>();
368
369        // Create a new interface by walking through the cores and initialising the TAPs that
370        // we find mentioned.
371        for core in cores.iter() {
372            let iface_idx = core.jtag_tap_index();
373
374            let core_arch = core.core_type().architecture();
375
376            if let Some(debug_arch) = interfaces[iface_idx].architecture() {
377                if core_arch == debug_arch {
378                    // Already initialised.
379                    continue;
380                }
381                return Err(Error::Probe(DebugProbeError::Other(format!(
382                    "{core_arch:?} core can not be mixed with a {debug_arch:?} debug module.",
383                ))));
384            }
385
386            interfaces[iface_idx] = match core_arch {
387                Architecture::Riscv => {
388                    let factory = probe.try_get_riscv_interface_builder()?;
389                    let mut state = factory.create_state();
390                    {
391                        let mut interface = factory.attach_auto(&target, &mut state)?;
392                        interface.enter_debug_mode()?;
393                    }
394
395                    JtagInterface::Riscv(state)
396                }
397                Architecture::Xtensa => JtagInterface::Xtensa(XtensaDebugInterfaceState::default()),
398                _ => {
399                    return Err(Error::Probe(DebugProbeError::Other(format!(
400                        "Unsupported core architecture {core_arch:?}",
401                    ))));
402                }
403            };
404        }
405
406        let interfaces = ArchitectureInterface::Jtag(probe, interfaces);
407
408        let mut session = Session {
409            target,
410            interfaces,
411            cores,
412            configured_trace_sink: None,
413        };
414
415        // Connect to the cores
416        match session.target.debug_sequence.clone() {
417            DebugSequence::Xtensa(_) => {}
418
419            DebugSequence::Riscv(sequence) => {
420                for core_id in 0..session.cores.len() {
421                    sequence.on_connect(&mut session.get_riscv_interface(core_id)?)?;
422                }
423            }
424            _ => unreachable!("Other architectures should have already been handled"),
425        };
426
427        Ok(session)
428    }
429
430    /// Automatically open a probe with the given session config.
431    fn auto_probe(session_config: &SessionConfig) -> Result<Probe, Error> {
432        // Get a list of all available debug probes.
433        let lister = Lister::new();
434
435        let probes = lister.list_all();
436
437        // Use the first probe found.
438        let mut probe = probes
439            .first()
440            .ok_or(Error::Probe(DebugProbeError::ProbeCouldNotBeCreated(
441                ProbeCreationError::NotFound,
442            )))?
443            .open()?;
444
445        // If the caller has specified speed or protocol in SessionConfig, set them
446        if let Some(speed) = session_config.speed {
447            probe.set_speed(speed)?;
448        }
449
450        if let Some(protocol) = session_config.protocol {
451            probe.select_protocol(protocol)?;
452        }
453        Ok(probe)
454    }
455
456    /// Automatically creates a session with the first connected probe found.
457    #[tracing::instrument(skip(target))]
458    pub fn auto_attach(
459        target: impl Into<TargetSelector>,
460        session_config: SessionConfig,
461    ) -> Result<Session, Error> {
462        // Attach to a chip.
463        Self::auto_probe(&session_config)?.attach(target, session_config.permissions)
464    }
465
466    /// Automatically creates a session with the first connected probe found
467    /// using the registry that was provided.
468    #[tracing::instrument(skip(target, registry))]
469    pub fn auto_attach_with_registry(
470        target: impl Into<TargetSelector>,
471        session_config: SessionConfig,
472        registry: &Registry,
473    ) -> Result<Session, Error> {
474        // Attach to a chip.
475        Self::auto_probe(&session_config)?.attach_with_registry(
476            target,
477            session_config.permissions,
478            registry,
479        )
480    }
481
482    /// Lists the available cores with their number and their type.
483    pub fn list_cores(&self) -> Vec<(usize, CoreType)> {
484        self.cores.iter().map(|t| (t.id(), t.core_type())).collect()
485    }
486
487    /// Get access to the session when all cores are halted.
488    ///
489    /// Any previously running cores will be resumed once the closure is executed.
490    pub fn halted_access<R>(
491        &mut self,
492        f: impl FnOnce(&mut Self) -> Result<R, Error>,
493    ) -> Result<R, Error> {
494        let mut resume_state = vec![];
495        for (core, _) in self.list_cores() {
496            let mut c = match self.core(core) {
497                Err(Error::CoreDisabled(_)) => continue,
498                other => other?,
499            };
500            if c.core_halted()? {
501                tracing::info!("Core {core} already halted");
502            } else {
503                tracing::info!("Halting core {core}...");
504                resume_state.push(core);
505                c.halt(Duration::from_millis(100))?;
506            }
507        }
508
509        let r = f(self);
510
511        for core in resume_state {
512            tracing::debug!("Resuming core...");
513            self.core(core)?.run()?;
514        }
515
516        r
517    }
518
519    fn interface_idx(&self, core: usize) -> Result<usize, Error> {
520        self.cores
521            .get(core)
522            .map(|c| c.jtag_tap_index())
523            .ok_or(Error::CoreNotFound(core))
524    }
525
526    /// Attaches to the core with the given number.
527    ///
528    /// ## Usage
529    /// Every time you want to perform an operation on the chip, you need to get the Core handle with the [Session::core()] method. This [Core] handle is merely a view into the core and provides a convenient API surface.
530    ///
531    /// All the state is stored in the [Session] handle.
532    ///
533    /// The first time you call [Session::core()] for a specific core, it will run the attach/init sequences and return a handle to the [Core].
534    ///
535    /// Every subsequent call is a no-op. It simply returns the handle for the user to use in further operations without calling any int sequences again.
536    ///
537    /// It is strongly advised to never store the [Core] handle for any significant duration! Free it as fast as possible such that other stakeholders can have access to the [Core] too.
538    ///
539    /// The idea behind this is: You need the smallest common denominator which you can share between threads. Since you sometimes need the [Core], sometimes the [Probe] or sometimes the [Target], the [Session] is the only common ground and the only handle you should actively store in your code.
540    //
541    // By design, this is called frequently in a session, therefore we limit tracing level to "trace" to avoid spamming the logs.
542    #[tracing::instrument(level = "trace", skip(self), name = "attach_to_core")]
543    pub fn core(&mut self, core_index: usize) -> Result<Core<'_>, Error> {
544        let combined_state = self
545            .cores
546            .get_mut(core_index)
547            .ok_or(Error::CoreNotFound(core_index))?;
548
549        self.interfaces
550            .attach(&self.target, combined_state)
551            .map_err(|e| {
552                if matches!(e, Error::Xtensa(XtensaError::CoreDisabled)) {
553                    // If the core is disabled, we can't attach to it.
554                    // We can't do anything about it, so we just translate
555                    // and return the error.
556                    // We'll retry at the next call.
557                    Error::CoreDisabled(core_index)
558                } else {
559                    e
560                }
561            })
562    }
563
564    /// Read available trace data from the specified data sink.
565    ///
566    /// This method is only supported for ARM-based targets, and will
567    /// return [ArmError::ArchitectureRequired] otherwise.
568    #[tracing::instrument(skip(self))]
569    pub fn read_trace_data(&mut self) -> Result<Vec<u8>, ArmError> {
570        let sink = self
571            .configured_trace_sink
572            .as_ref()
573            .ok_or(ArmError::TracingUnconfigured)?;
574
575        match sink {
576            TraceSink::Swo(_) => {
577                let interface = self.get_arm_interface()?;
578                interface.read_swo()
579            }
580
581            TraceSink::Tpiu(_) => {
582                panic!("Probe-rs does not yet support reading parallel trace ports");
583            }
584
585            TraceSink::TraceMemory => {
586                let components = self.get_arm_components(DpAddress::Default)?;
587                let interface = self.get_arm_interface()?;
588                crate::architecture::arm::component::read_trace_memory(interface, &components)
589            }
590        }
591    }
592
593    /// Returns an implementation of [std::io::Read] that wraps [SwoAccess::read_swo].
594    ///
595    /// The implementation buffers all available bytes from
596    /// [SwoAccess::read_swo] on each [std::io::Read::read],
597    /// minimizing the chance of a target-side overflow event on which
598    /// trace packets are lost.
599    ///
600    /// [SwoAccess::read_swo]: crate::architecture::arm::swo::SwoAccess
601    pub fn swo_reader(&mut self) -> Result<SwoReader<'_>, Error> {
602        let interface = self.get_arm_interface()?;
603        Ok(SwoReader::new(interface))
604    }
605
606    /// Get the Arm probe interface.
607    pub fn get_arm_interface(&mut self) -> Result<&mut dyn ArmDebugInterface, ArmError> {
608        let interface = match &mut self.interfaces {
609            ArchitectureInterface::Arm(state) => state.deref_mut(),
610            _ => return Err(ArmError::NoArmTarget),
611        };
612
613        Ok(interface)
614    }
615
616    /// Get the RISC-V probe interface.
617    pub fn get_riscv_interface(
618        &mut self,
619        core_id: usize,
620    ) -> Result<RiscvCommunicationInterface<'_>, Error> {
621        let tap_idx = self.interface_idx(core_id)?;
622        if let ArchitectureInterface::Jtag(probe, ifaces) = &mut self.interfaces {
623            if let Some(probe) = probe.try_as_jtag_probe() {
624                probe.select_target(tap_idx)?;
625            }
626            if let JtagInterface::Riscv(state) = &mut ifaces[tap_idx] {
627                let factory = probe.try_get_riscv_interface_builder()?;
628                return Ok(factory.attach_auto(&self.target, state)?);
629            }
630        }
631        Err(RiscvError::NoRiscvTarget.into())
632    }
633
634    /// Get the Xtensa probe interface.
635    pub fn get_xtensa_interface(
636        &mut self,
637        core_id: usize,
638    ) -> Result<XtensaCommunicationInterface<'_>, Error> {
639        let tap_idx = self.interface_idx(core_id)?;
640        if let ArchitectureInterface::Jtag(probe, ifaces) = &mut self.interfaces {
641            if let Some(probe) = probe.try_as_jtag_probe() {
642                probe.select_target(tap_idx)?;
643            }
644            if let JtagInterface::Xtensa(state) = &mut ifaces[tap_idx] {
645                return Ok(probe.try_get_xtensa_interface(state)?);
646            }
647        }
648        Err(XtensaError::NoXtensaTarget.into())
649    }
650
651    #[tracing::instrument(skip_all)]
652    fn reattach_arm_interface(
653        interface: &mut Box<dyn ArmDebugInterface>,
654        debug_sequence: &Arc<dyn ArmDebugSequence>,
655    ) -> Result<(), Error> {
656        use crate::probe::DebugProbe;
657
658        let current_dp = interface.current_debug_port();
659
660        // In order to re-attach we need an owned instance to the interface
661        // but we only have &mut. We can work around that by first creating
662        // an instance of a Dummy and then swapping it out for the real one.
663        // perform the re-attach and then swap it back.
664        let mut tmp_interface = Box::<FakeProbe>::default()
665            .try_get_arm_debug_interface(DefaultArmSequence::create())
666            .unwrap();
667
668        std::mem::swap(interface, &mut tmp_interface);
669
670        tracing::debug!("Re-attaching Probe");
671        let mut probe = tmp_interface.close();
672        probe.detach()?;
673        probe.attach_to_unspecified()?;
674
675        let mut new_interface = probe
676            .try_into_arm_debug_interface(debug_sequence.clone())
677            .map_err(|(_, err)| err)?;
678
679        if let Some(current_dp) = current_dp {
680            new_interface.select_debug_port(current_dp)?;
681        }
682        // swap it back
683        std::mem::swap(interface, &mut new_interface);
684
685        tracing::debug!("Probe re-attached");
686        Ok(())
687    }
688
689    /// This function can be used to set up an application which was flashed to RAM.
690    pub fn prepare_running_on_ram(&mut self, vector_table_addr: u64) -> Result<(), crate::Error> {
691        match &self.target.debug_sequence.clone() {
692            crate::config::DebugSequence::Arm(arm) => {
693                arm.prepare_running_on_ram(vector_table_addr, self)
694            }
695            _ => Err(crate::Error::NotImplemented(
696                "ram flash non-ARM architectures",
697            )),
698        }
699    }
700
701    /// Check if the connected device has a debug erase sequence defined
702    pub fn has_sequence_erase_all(&self) -> bool {
703        match &self.target.debug_sequence {
704            DebugSequence::Arm(seq) => seq.debug_erase_sequence().is_some(),
705            // Currently, debug_erase_sequence is ARM (and ATSAM) specific
706            _ => false,
707        }
708    }
709
710    /// Erase all flash memory using the Device's Debug Erase Sequence if any
711    ///
712    /// # Returns
713    /// Ok(()) if the device provides a custom erase sequence and it succeeded.
714    ///
715    /// # Errors
716    /// NotImplemented if no custom erase sequence exists
717    /// Err(e) if the custom erase sequence failed
718    pub fn sequence_erase_all(&mut self) -> Result<(), Error> {
719        let ArchitectureInterface::Arm(ref mut interface) = self.interfaces else {
720            return Err(Error::NotImplemented(
721                "Debug Erase Sequence is not implemented for non-ARM targets.",
722            ));
723        };
724
725        let DebugSequence::Arm(ref debug_sequence) = self.target.debug_sequence else {
726            unreachable!("This should never happen. Please file a bug if it does.");
727        };
728
729        let erase_sequence = debug_sequence
730            .debug_erase_sequence()
731            .ok_or(Error::Arm(ArmError::NotImplemented("Debug Erase Sequence")))?;
732
733        tracing::info!("Trying Debug Erase Sequence");
734        let erase_result = erase_sequence.erase_all(interface.deref_mut());
735
736        match erase_result {
737            Ok(()) => (),
738            // In case this happens after unlock. Try to re-attach the probe once.
739            Err(ArmError::ReAttachRequired) => {
740                Self::reattach_arm_interface(interface, debug_sequence)?;
741                // For re-setup debugging on all cores
742                for core_state in &self.cores {
743                    core_state.enable_arm_debug(interface.deref_mut())?;
744                }
745            }
746            Err(e) => return Err(Error::Arm(e)),
747        }
748        tracing::info!("Device Erased Successfully");
749        Ok(())
750    }
751
752    /// Reads all the available ARM CoresightComponents of the currently attached target.
753    ///
754    /// This will recursively parse the Romtable of the attached target
755    /// and create a list of all the contained components.
756    pub fn get_arm_components(
757        &mut self,
758        dp: DpAddress,
759    ) -> Result<Vec<CoresightComponent>, ArmError> {
760        let interface = self.get_arm_interface()?;
761
762        get_arm_components(interface, dp)
763    }
764
765    /// Get the target description of the connected target.
766    pub fn target(&self) -> &Target {
767        &self.target
768    }
769
770    /// Configure the target and probe for serial wire view (SWV) tracing.
771    pub fn setup_tracing(
772        &mut self,
773        core_index: usize,
774        destination: TraceSink,
775    ) -> Result<(), Error> {
776        // Enable tracing on the target
777        {
778            let mut core = self.core(core_index)?;
779            crate::architecture::arm::component::enable_tracing(&mut core)?;
780        }
781
782        let sequence_handle = match &self.target.debug_sequence {
783            DebugSequence::Arm(sequence) => sequence.clone(),
784            _ => unreachable!("Mismatch between architecture and sequence type!"),
785        };
786
787        let components = self.get_arm_components(DpAddress::Default)?;
788        let interface = self.get_arm_interface()?;
789
790        // Configure SWO on the probe when the trace sink is configured for a serial output. Note
791        // that on some architectures, the TPIU is configured to drive SWO.
792        match destination {
793            TraceSink::Swo(ref config) => {
794                interface.enable_swo(config)?;
795            }
796            TraceSink::Tpiu(ref config) => {
797                interface.enable_swo(config)?;
798            }
799            TraceSink::TraceMemory => {}
800        }
801
802        sequence_handle.trace_start(interface, &components, &destination)?;
803        crate::architecture::arm::component::setup_tracing(interface, &components, &destination)?;
804
805        self.configured_trace_sink.replace(destination);
806
807        Ok(())
808    }
809
810    /// Configure the target to stop emitting SWV trace data.
811    #[tracing::instrument(skip(self))]
812    pub fn disable_swv(&mut self, core_index: usize) -> Result<(), Error> {
813        crate::architecture::arm::component::disable_swv(&mut self.core(core_index)?)
814    }
815
816    /// Begin tracing a memory address over SWV.
817    pub fn add_swv_data_trace(&mut self, unit: usize, address: u32) -> Result<(), ArmError> {
818        let components = self.get_arm_components(DpAddress::Default)?;
819        let interface = self.get_arm_interface()?;
820        crate::architecture::arm::component::add_swv_data_trace(
821            interface,
822            &components,
823            unit,
824            address,
825        )
826    }
827
828    /// Stop tracing from a given SWV unit
829    pub fn remove_swv_data_trace(&mut self, unit: usize) -> Result<(), ArmError> {
830        let components = self.get_arm_components(DpAddress::Default)?;
831        let interface = self.get_arm_interface()?;
832        crate::architecture::arm::component::remove_swv_data_trace(interface, &components, unit)
833    }
834
835    /// Return the `Architecture` of the currently connected chip.
836    pub fn architecture(&self) -> Architecture {
837        match &self.interfaces {
838            ArchitectureInterface::Arm(_) => Architecture::Arm,
839            ArchitectureInterface::Jtag(_, ifaces) => {
840                if let JtagInterface::Riscv(_) = &ifaces[0] {
841                    Architecture::Riscv
842                } else {
843                    Architecture::Xtensa
844                }
845            }
846        }
847    }
848
849    /// Clears all hardware breakpoints on all cores
850    pub fn clear_all_hw_breakpoints(&mut self) -> Result<(), Error> {
851        self.halted_access(|session| {
852            { 0..session.cores.len() }.try_for_each(|core| {
853                tracing::info!("Clearing breakpoints for core {core}");
854
855                match session.core(core) {
856                    Ok(mut core) => core.clear_all_hw_breakpoints(),
857                    Err(Error::CoreDisabled(_)) => Ok(()),
858                    Err(err) => Err(err),
859                }
860            })
861        })
862    }
863
864    /// Resume all cores
865    pub fn resume_all_cores(&mut self) -> Result<(), Error> {
866        // Resume cores
867        for core_id in 0..self.cores.len() {
868            match self.core(core_id) {
869                Ok(mut core) => {
870                    if core.core_halted()? {
871                        core.run()?;
872                    }
873                }
874                Err(Error::CoreDisabled(i)) => tracing::debug!("Core {i} is disabled"),
875                Err(error) => return Err(error),
876            }
877        }
878
879        Ok(())
880    }
881}
882
883// This test ensures that [Session] is fully [Send] + [Sync].
884const _: fn() = || {
885    fn assert_impl_all<T: ?Sized + Send>() {}
886
887    assert_impl_all::<Session>();
888};
889
890impl Drop for Session {
891    #[tracing::instrument(name = "session_drop", skip(self))]
892    fn drop(&mut self) {
893        if let Err(err) = self.clear_all_hw_breakpoints() {
894            tracing::warn!(
895                "Could not clear all hardware breakpoints: {:?}",
896                anyhow::anyhow!(err)
897            );
898        }
899
900        // Call any necessary deconfiguration/shutdown hooks.
901        if let Err(err) = { 0..self.cores.len() }.try_for_each(|core| match self.core(core) {
902            Ok(mut core) => core.debug_core_stop(),
903            Err(Error::CoreDisabled(_)) => Ok(()),
904            Err(err) => Err(err),
905        }) {
906            tracing::warn!("Failed to deconfigure device during shutdown: {:?}", err);
907        }
908    }
909}
910
911/// Determine the [Target] from a [TargetSelector].
912///
913/// If the selector is [TargetSelector::Unspecified], the target will be looked up in the registry.
914/// If it its [TargetSelector::Auto], probe-rs will try to determine the target automatically, based on
915/// information read from the chip.
916fn get_target_from_selector(
917    target: TargetSelector,
918    attach_method: AttachMethod,
919    mut probe: Probe,
920    registry: &Registry,
921) -> Result<(Probe, Target), Error> {
922    let target = match target {
923        TargetSelector::Unspecified(name) => registry.get_target_by_name(name)?,
924        TargetSelector::Specified(target) => target,
925        TargetSelector::Auto => {
926            // At this point we do not know what the target is, so we cannot use the chip specific reset sequence.
927            // Thus, we try just using a normal reset for target detection if we want to do so under reset.
928            // This can of course fail, but target detection is a best effort, not a guarantee!
929            if AttachMethod::UnderReset == attach_method {
930                probe.target_reset_assert()?;
931            }
932            probe.attach_to_unspecified()?;
933
934            let (returned_probe, found_target) =
935                crate::vendor::auto_determine_target(registry, probe)?;
936            probe = returned_probe;
937
938            if AttachMethod::UnderReset == attach_method {
939                // Now we can deassert reset in case we asserted it before.
940                probe.target_reset_deassert()?;
941            }
942
943            if let Some(target) = found_target {
944                target
945            } else {
946                return Err(Error::ChipNotFound(RegistryError::ChipAutodetectFailed));
947            }
948        }
949    };
950
951    Ok((probe, target))
952}
953
954/// The `Permissions` struct represents what a [Session] is allowed to do with a target.
955/// Some operations can be irreversible, so need to be explicitly allowed by the user.
956///
957/// # Example
958///
959/// ```
960/// use probe_rs::Permissions;
961///
962/// let permissions = Permissions::new().allow_erase_all();
963/// ```
964#[non_exhaustive]
965#[derive(Debug, Clone, Default)]
966pub struct Permissions {
967    /// When set to true, all memory of the chip may be erased or reset to factory default
968    erase_all: bool,
969}
970
971impl Permissions {
972    /// Constructs a new permissions object with the default values
973    pub fn new() -> Self {
974        Self::default()
975    }
976
977    /// Allow the session to erase all memory of the chip or reset it to factory default.
978    ///
979    /// # Warning
980    /// This may irreversibly remove otherwise read-protected data from the device like security keys and 3rd party firmware.
981    /// What happens exactly may differ per device and per probe-rs version.
982    #[must_use]
983    pub fn allow_erase_all(self) -> Self {
984        Self {
985            erase_all: true,
986            ..self
987        }
988    }
989
990    pub(crate) fn erase_all(&self) -> Result<(), MissingPermissions> {
991        if self.erase_all {
992            Ok(())
993        } else {
994            Err(MissingPermissions("erase_all".into()))
995        }
996    }
997}
998
999#[derive(Debug, Clone, thiserror::Error)]
1000#[error("An operation could not be performed because it lacked the permission to do so: {0}")]
1001pub struct MissingPermissions(pub String);