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#[derive(Debug)]
48pub struct Session {
49 target: Target,
50 interfaces: ArchitectureInterface,
51 cores: Vec<CombinedCoreState>,
52 configured_trace_sink: Option<TraceSink>,
53}
54
55#[derive(Default, Debug)]
63pub struct SessionConfig {
64 pub permissions: Permissions,
66 pub speed: Option<u32>,
68 pub protocol: Option<WireProtocol>,
70}
71
72enum JtagInterface {
73 Riscv(RiscvDebugInterfaceState),
74 Xtensa(XtensaDebugInterfaceState),
75 Unknown,
76}
77
78impl JtagInterface {
79 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
99enum 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 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 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 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 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 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 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 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 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 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 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 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 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 fn auto_probe(session_config: &SessionConfig) -> Result<Probe, Error> {
432 let lister = Lister::new();
434
435 let probes = lister.list_all();
436
437 let mut probe = probes
439 .first()
440 .ok_or(Error::Probe(DebugProbeError::ProbeCouldNotBeCreated(
441 ProbeCreationError::NotFound,
442 )))?
443 .open()?;
444
445 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 #[tracing::instrument(skip(target))]
458 pub fn auto_attach(
459 target: impl Into<TargetSelector>,
460 session_config: SessionConfig,
461 ) -> Result<Session, Error> {
462 Self::auto_probe(&session_config)?.attach(target, session_config.permissions)
464 }
465
466 #[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 Self::auto_probe(&session_config)?.attach_with_registry(
476 target,
477 session_config.permissions,
478 registry,
479 )
480 }
481
482 pub fn list_cores(&self) -> Vec<(usize, CoreType)> {
484 self.cores.iter().map(|t| (t.id(), t.core_type())).collect()
485 }
486
487 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 #[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 Error::CoreDisabled(core_index)
558 } else {
559 e
560 }
561 })
562 }
563
564 #[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 pub fn swo_reader(&mut self) -> Result<SwoReader<'_>, Error> {
602 let interface = self.get_arm_interface()?;
603 Ok(SwoReader::new(interface))
604 }
605
606 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 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 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 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 std::mem::swap(interface, &mut new_interface);
684
685 tracing::debug!("Probe re-attached");
686 Ok(())
687 }
688
689 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 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 _ => false,
707 }
708 }
709
710 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 Err(ArmError::ReAttachRequired) => {
740 Self::reattach_arm_interface(interface, debug_sequence)?;
741 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 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 pub fn target(&self) -> &Target {
767 &self.target
768 }
769
770 pub fn setup_tracing(
772 &mut self,
773 core_index: usize,
774 destination: TraceSink,
775 ) -> Result<(), Error> {
776 {
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 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 #[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 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 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 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 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 pub fn resume_all_cores(&mut self) -> Result<(), Error> {
866 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
883const _: 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 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
911fn 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 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 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#[non_exhaustive]
965#[derive(Debug, Clone, Default)]
966pub struct Permissions {
967 erase_all: bool,
969}
970
971impl Permissions {
972 pub fn new() -> Self {
974 Self::default()
975 }
976
977 #[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);