@@ -190,17 +190,24 @@ pub async fn attach_rtt(
190
190
let rtt_scan_region = rtt_scan_region. clone ( ) ;
191
191
192
192
let result = spawn_blocking ( move || {
193
- let _halt_guard = if halt_on_access {
194
- Some ( CoreHaltGuard :: new ( session. clone ( ) ) . map_err ( AttachRttError :: HaltCore ) ?)
193
+ let mut session = session. lock ( ) . unwrap ( ) ;
194
+ let memory_map = session. target ( ) . memory_map . clone ( ) ;
195
+ let mut core = session. core ( 0 ) . map_err ( AttachRttError :: HaltCore ) ?;
196
+ let halt_guard;
197
+ let core = if halt_on_access {
198
+ halt_guard = CoreHaltGuard :: new ( & mut core) . map_err ( AttachRttError :: HaltCore ) ?;
199
+ & mut * halt_guard. core
195
200
} else {
196
- None
201
+ & mut core
197
202
} ;
198
203
199
- match probe_rs_rtt:: Rtt :: attach_region ( session, & rtt_scan_region) {
200
- Ok ( rtt) => Ok ( Some ( rtt) ) ,
201
- Err ( probe_rs_rtt:: Error :: ControlBlockNotFound ) => Ok ( None ) ,
202
- Err ( e) => Err ( AttachRttError :: AttachRtt ( e) ) ,
203
- }
204
+ let result = match probe_rs_rtt:: Rtt :: attach_region ( core, & memory_map, & rtt_scan_region)
205
+ {
206
+ Ok ( rtt) => Some ( rtt) ,
207
+ Err ( probe_rs_rtt:: Error :: ControlBlockNotFound ) => None ,
208
+ Err ( e) => return Err ( AttachRttError :: AttachRtt ( e) ) ,
209
+ } ;
210
+ Ok ( result)
204
211
} )
205
212
. await
206
213
. unwrap ( ) ?;
@@ -244,34 +251,20 @@ fn find_rtt_symbol(elf_bytes: &[u8]) -> Option<u64> {
244
251
}
245
252
246
253
/// Halts the first core while this RAII guard is held.
247
- struct CoreHaltGuard ( Arc < Mutex < probe_rs:: Session > > ) ;
248
-
249
- impl CoreHaltGuard {
250
- fn new ( session : Arc < Mutex < probe_rs:: Session > > ) -> Result < Self , probe_rs:: Error > {
251
- {
252
- let mut session = session. lock ( ) . unwrap ( ) ;
253
- let mut core = session. core ( 0 ) ?;
254
- core. halt ( std:: time:: Duration :: from_millis ( 100 ) ) ?;
255
- }
254
+ struct CoreHaltGuard < ' a , ' probe > {
255
+ core : & ' a mut probe_rs:: Core < ' probe > ,
256
+ }
256
257
257
- Ok ( Self ( session) )
258
+ impl < ' a , ' probe > CoreHaltGuard < ' a , ' probe > {
259
+ fn new ( core : & ' a mut probe_rs:: Core < ' probe > ) -> Result < Self , probe_rs:: Error > {
260
+ core. halt ( std:: time:: Duration :: from_millis ( 100 ) ) ?;
261
+ Ok ( Self { core } )
258
262
}
259
263
}
260
264
261
- impl Drop for CoreHaltGuard {
265
+ impl Drop for CoreHaltGuard < ' _ , ' _ > {
262
266
fn drop ( & mut self ) {
263
- let mut session = self . 0 . lock ( ) . unwrap ( ) ;
264
- let mut core = match session. core ( 0 ) {
265
- Ok ( x) => x,
266
- Err ( e) => {
267
- log:: warn!(
268
- "Failed to get the core object while restarting the core (ignored): {:?}" ,
269
- e
270
- ) ;
271
- return ;
272
- }
273
- } ;
274
- if let Err ( e) = core. run ( ) {
267
+ if let Err ( e) = self . core . run ( ) {
275
268
log:: warn!( "Failed to restart the core (ignored): {:?}" , e) ;
276
269
}
277
270
}
@@ -448,20 +441,27 @@ impl ReadRtt {
448
441
buf : & mut [ u8 ] ,
449
442
halt_on_access : bool ,
450
443
) -> tokio:: io:: Result < usize > {
451
- let _halt_guard = if halt_on_access {
452
- Some (
453
- CoreHaltGuard :: new ( session)
454
- . map_err ( |e| tokio:: io:: Error :: new ( tokio:: io:: ErrorKind :: Other , e) ) ?,
455
- )
444
+ // FIXME: Hold the lock in `ReadRtt`; it's pointless to be able to have
445
+ // two instances of `ReadRtt` pointing to the same `Session` and
446
+ // performing interleaved reads
447
+ let mut session = session. lock ( ) . unwrap ( ) ;
448
+ let mut core = session
449
+ . core ( 0 )
450
+ . map_err ( |e| tokio:: io:: Error :: new ( tokio:: io:: ErrorKind :: Other , e) ) ?;
451
+ let halt_guard;
452
+ let core = if halt_on_access {
453
+ halt_guard = CoreHaltGuard :: new ( & mut core)
454
+ . map_err ( |e| tokio:: io:: Error :: new ( tokio:: io:: ErrorKind :: Other , e) ) ?;
455
+ & mut * halt_guard. core
456
456
} else {
457
- None
457
+ & mut core
458
458
} ;
459
459
460
460
let mut num_read_bytes = 0 ;
461
461
462
462
for ( i, channel) in rtt. up_channels ( ) . iter ( ) . enumerate ( ) {
463
463
let num_ch_read_bytes = channel
464
- . read ( buf)
464
+ . read ( core , buf)
465
465
. map_err ( |e| tokio:: io:: Error :: new ( tokio:: io:: ErrorKind :: Other , e) ) ?;
466
466
467
467
if num_ch_read_bytes != 0 {
0 commit comments