probe_rs_debug/debug_step.rs
1use super::{DebugError, VerifiedBreakpoint, debug_info::DebugInfo};
2use probe_rs::{
3 CoreInterface, CoreStatus, HaltReason,
4 architecture::{
5 arm::ArmError, riscv::communication_interface::RiscvError,
6 xtensa::communication_interface::XtensaError,
7 },
8};
9use std::{ops::RangeInclusive, time::Duration};
10
11/// Stepping granularity for stepping through a program during debug.
12#[derive(Clone, Debug)]
13pub enum SteppingMode {
14 /// Special case, where we aren't stepping, but we are trying to find the next valid breakpoint.
15 /// - The validity of halt locations are defined as target instructions that live between the end of the prologue, and the start of the end sequence of a [`gimli::read::LineRow`].
16 BreakPoint,
17 /// Advance one machine instruction at a time.
18 StepInstruction,
19 /// Step Over the current statement, and halt at the start of the next statement.
20 OverStatement,
21 /// Use best efforts to determine the location of any function calls in this statement, and step into them.
22 IntoStatement,
23 /// Step to the calling statement, immediately after the current function returns.
24 OutOfStatement,
25}
26
27impl SteppingMode {
28 /// Determine the program counter location where the SteppingMode is aimed, and step to it.
29 /// Return the new CoreStatus and program_counter value.
30 ///
31 /// Implementation Notes for stepping at statement granularity:
32 /// - If a hardware breakpoint is available, we will set it at the desired location, run to it, and release it.
33 /// - If no hardware breakpoints are available, we will do repeated instruction steps until we reach the desired location.
34 ///
35 /// Usage Note:
36 /// - Currently, no special provision is made for the effect of interrupts that get triggered
37 /// during stepping. The user must ensure that interrupts are disabled during stepping, or
38 /// accept that stepping may be diverted by the interrupt processing on the core.
39 pub fn step(
40 &self,
41 core: &mut impl CoreInterface,
42 debug_info: &DebugInfo,
43 ) -> Result<(CoreStatus, u64), DebugError> {
44 let mut core_status = core.status()?;
45 let mut program_counter = match core_status {
46 CoreStatus::Halted(_) => core
47 .read_core_reg(core.program_counter().id())?
48 .try_into()?,
49 _ => {
50 return Err(DebugError::Other(
51 "Core must be halted before stepping.".to_string(),
52 ));
53 }
54 };
55 let origin_program_counter = program_counter;
56 let mut return_address = core.read_core_reg(core.return_address().id())?.try_into()?;
57
58 // Sometimes the target program_counter is at a location where the debug_info program row data does not contain valid statements for halt points.
59 // When DebugError::NoValidHaltLocation happens, we will step to the next instruction and try again(until we can reasonably expect to have passed out of an epilogue), before giving up.
60 let mut target_address: Option<u64> = None;
61 for _ in 0..10 {
62 let post_step_target = match self {
63 SteppingMode::StepInstruction => {
64 // First deal with the the fast/easy case.
65 program_counter = core.step()?.pc;
66 core_status = core.status()?;
67 return Ok((core_status, program_counter));
68 }
69 SteppingMode::BreakPoint => {
70 self.get_halt_location(core, debug_info, program_counter, None)
71 }
72 SteppingMode::IntoStatement
73 | SteppingMode::OverStatement
74 | SteppingMode::OutOfStatement => {
75 // The more complex cases, where specific handling is required.
76 self.get_halt_location(core, debug_info, program_counter, Some(return_address))
77 }
78 };
79 match post_step_target {
80 Ok(post_step_target) => {
81 target_address = Some(post_step_target.address);
82 // Re-read the program_counter, because it may have changed during the `get_halt_location` call.
83 program_counter = core
84 .read_core_reg(core.program_counter().id())?
85 .try_into()?;
86 break;
87 }
88 Err(error) => {
89 match error {
90 DebugError::WarnAndContinue { message } => {
91 // Step on target instruction, and then try again.
92 tracing::trace!(
93 "Incomplete stepping information @{program_counter:#010X}: {message}"
94 );
95 program_counter = core.step()?.pc;
96 return_address =
97 core.read_core_reg(core.return_address().id())?.try_into()?;
98 continue;
99 }
100 other_error => {
101 core_status = core.status()?;
102 program_counter = core
103 .read_core_reg(core.program_counter().id())?
104 .try_into()?;
105 tracing::error!("Error during step ({:?}): {}", self, other_error);
106 return Ok((core_status, program_counter));
107 }
108 }
109 }
110 }
111 }
112
113 (core_status, program_counter) = match target_address {
114 Some(target_address) => {
115 tracing::debug!(
116 "Preparing to step ({:20?}): \n\tfrom: {:?} @ {:#010X} \n\t to: {:?} @ {:#010X}",
117 self,
118 debug_info
119 .get_source_location(program_counter)
120 .map(|source_location| (
121 source_location.path,
122 source_location.line,
123 source_location.column
124 )),
125 origin_program_counter,
126 debug_info
127 .get_source_location(target_address)
128 .map(|source_location| (
129 source_location.path,
130 source_location.line,
131 source_location.column
132 )),
133 target_address,
134 );
135
136 run_to_address(program_counter, target_address, core)?
137 }
138 None => {
139 return Err(DebugError::WarnAndContinue {
140 message: "Unable to determine target address for this step request."
141 .to_string(),
142 });
143 }
144 };
145 Ok((core_status, program_counter))
146 }
147
148 /// To understand how this method works, use the following framework:
149 /// - Everything is calculated from a given machine instruction address, usually the current program counter.
150 /// - To calculate where the user might step to (step-over, step-into, step-out), we start from the given instruction
151 /// address/program counter, and work our way through all the rows in the sequence of instructions it is part of.
152 /// - A sequence of instructions represents a series of monotonically increasing target machine instructions,
153 /// and does not necessarily represent the whole of a function.
154 /// - Similarly, the instructions belonging to a sequence are not necessarily contiguous inside the sequence of instructions,
155 /// e.g. conditional branching inside the sequence.
156 /// - To determine valid halt points for breakpoints and stepping, we only use instructions that qualify as:
157 /// - The beginning of a statement that is neither inside the prologue, nor inside the epilogue.
158 /// - Based on this, we will attempt to return the "most appropriate" address for the [`SteppingMode`], given the available information in the instruction sequence.
159 ///
160 /// All data is calculated using the [`gimli::read::CompleteLineProgram`] as well as, function call data from the debug info frame section.
161 ///
162 /// NOTE about errors returned: Sometimes the target program_counter is at a location where the debug_info program row data does not contain valid statements
163 /// for halt points, and we will return a `DebugError::NoValidHaltLocation`. In this case, we recommend the consumer of this API step the core to the next instruction
164 /// and try again, with a reasonable retry limit. All other error kinds are should be treated as non recoverable errors.
165 pub(crate) fn get_halt_location(
166 &self,
167 core: &mut impl CoreInterface,
168 debug_info: &DebugInfo,
169 program_counter: u64,
170 return_address: Option<u64>,
171 ) -> Result<VerifiedBreakpoint, DebugError> {
172 let program_unit = debug_info.compile_unit_info(program_counter)?;
173 match self {
174 SteppingMode::BreakPoint => {
175 // Find the first_breakpoint_address
176 return VerifiedBreakpoint::for_address(debug_info, program_counter);
177 }
178 SteppingMode::OverStatement => {
179 // Find the "step over location"
180 // - The instructions in a sequence do not necessarily have contiguous addresses,
181 // and the next instruction address may be affected by conditonal branching at runtime.
182 // - Therefore, in order to find the correct "step over location", we iterate through the
183 // instructions to find the starting address of the next halt location, ie. the address
184 // is greater than the current program counter.
185 // -- If there is one, it means the step over target is in the current sequence,
186 // so we get the valid breakpoint location for this next location.
187 // -- If there is not one, the step over target is the same as the step out target.
188 return VerifiedBreakpoint::for_address(
189 debug_info,
190 program_counter.saturating_add(1),
191 )
192 .or_else(|_| {
193 // If we cannot find a valid breakpoint in the current sequence, we will step out of the current sequence.
194 SteppingMode::OutOfStatement.get_halt_location(
195 core,
196 debug_info,
197 program_counter,
198 return_address,
199 )
200 });
201 }
202 SteppingMode::IntoStatement => {
203 // This is a tricky case because the current RUST generated DWARF, does not store the DW_TAG_call_site information described in the DWARF 5 standard.
204 // - It is not a mandatory attribute, so not sure if we can ever expect it.
205 // To find if any functions are called from the current program counter:
206 // 1. Identify the next instruction location after the instruction corresponding to the current PC,
207 // 2. Single step the target core, until either of the following:
208 // (a) We hit a PC that is NOT in the range between the current PC and the next instruction location.
209 // This location, which could be any of the following:
210 // (a.i) A legitimate branch outside the current sequence (call to another instruction) such as
211 // an explicit call to a function, or something the compiler injected, like a `drop()`,
212 // (a.ii) An interrupt handler diverted the processing.
213 // (b) We hit a PC at the address of the identified next instruction location,
214 // which means there was nothing to step into, so the target is now halted (correctly) at the next statement.
215 let target_pc = match VerifiedBreakpoint::for_address(
216 debug_info,
217 program_counter.saturating_add(1),
218 ) {
219 Ok(identified_next_breakpoint) => identified_next_breakpoint.address,
220 Err(DebugError::WarnAndContinue { .. }) => {
221 // There are no next statements in this sequence, so we will use the return address as the target.
222 if let Some(return_address) = return_address {
223 return_address
224 } else {
225 return Err(DebugError::WarnAndContinue {
226 message: "Could not determine a 'step in' target. Please use 'step over'.".to_string(),
227 });
228 }
229 }
230 Err(other_error) => {
231 return Err(other_error);
232 }
233 };
234
235 let (core_status, new_pc) = step_to_address(program_counter..=target_pc, core)?;
236 if (program_counter..=target_pc).contains(&new_pc) {
237 // We have halted at an address after the current instruction (either in the same sequence,
238 // or at the return address of the current function),
239 // so we can conclude there were no branching calls in this instruction.
240 tracing::debug!(
241 "Stepping into next statement, but no branching calls found. Stepped to next available location."
242 );
243 } else if matches!(core_status, CoreStatus::Halted(HaltReason::Breakpoint(_))) {
244 // We have halted at a PC that is within the current statement, so there must be another breakpoint.
245 tracing::debug!("Stepping into next statement, but encountered a breakpoint.");
246 } else {
247 tracing::debug!("Stepping into next statement at address: {:#010x}.", new_pc);
248 }
249
250 return SteppingMode::BreakPoint.get_halt_location(core, debug_info, new_pc, None);
251 }
252 SteppingMode::OutOfStatement => {
253 if let Ok(function_dies) =
254 program_unit.get_function_dies(debug_info, program_counter)
255 {
256 // We want the first qualifying (PC is in range) function from the back of this list,
257 // to access the 'innermost' functions first.
258 if let Some(function) = function_dies.iter().next_back() {
259 tracing::trace!(
260 "Step Out target: Evaluating function {:?}, low_pc={:?}, high_pc={:?}",
261 function.function_name(debug_info),
262 function.low_pc(),
263 function.high_pc()
264 );
265
266 if function
267 .attribute(debug_info, gimli::DW_AT_noreturn)
268 .is_some()
269 {
270 return Err(DebugError::Other(format!(
271 "Function {:?} is marked as `noreturn`. Cannot step out of this function.",
272 function
273 .function_name(debug_info)
274 .as_deref()
275 .unwrap_or("<unknown>")
276 )));
277 } else if function.range_contains(program_counter) {
278 if function.is_inline() {
279 // Step_out_address for inlined functions, is the first available breakpoint address after the last statement in the inline function.
280 let (_, next_instruction_address) = run_to_address(
281 program_counter,
282 function.high_pc().unwrap(), //unwrap is OK because `range_contains` is true.
283 core,
284 )?;
285 return SteppingMode::BreakPoint.get_halt_location(
286 core,
287 debug_info,
288 next_instruction_address,
289 None,
290 );
291 } else if let Some(return_address) = return_address {
292 tracing::debug!(
293 "Step Out target: non-inline function, stepping over return address: {:#010x}",
294 return_address
295 );
296 // Step_out_address for non-inlined functions is the first available breakpoint address after the return address.
297 return SteppingMode::BreakPoint.get_halt_location(
298 core,
299 debug_info,
300 return_address,
301 None,
302 );
303 }
304 }
305 }
306 }
307 }
308 _ => {
309 // SteppingMode::StepInstruction is handled in the `step()` method.
310 }
311 }
312
313 Err(DebugError::WarnAndContinue {
314 message: "Could not determine valid halt locations for this request. Please consider using instruction level stepping.".to_string()
315 })
316 }
317}
318
319/// Run the target to the desired address. If available, we will use a breakpoint, otherwise we will use single step.
320/// Returns the program counter at the end of the step, when any of the following conditions are met:
321/// - We reach the `target_address_range.end()` (inclusive)
322/// - We reach some other legitimate halt point (e.g. the user tries to step past a series of statements, but there is another breakpoint active in that "gap")
323/// - We encounter an error (e.g. the core locks up, or the USB cable is unplugged, etc.)
324/// - It turns out this step will be long-running, and we do not have to wait any longer for the request to complete.
325fn run_to_address(
326 mut program_counter: u64,
327 target_address: u64,
328 core: &mut impl CoreInterface,
329) -> Result<(CoreStatus, u64), DebugError> {
330 if target_address == program_counter {
331 // No need to step further. e.g. For inline functions we have already stepped to the best available target address..
332 return Ok((
333 core.status()?,
334 core.read_core_reg(core.program_counter().id())?
335 .try_into()?,
336 ));
337 }
338
339 let breakpoints = core.hw_breakpoints()?;
340 let bp_to_use = breakpoints.iter().position(|bp| bp.is_none()).unwrap_or(0);
341
342 if core.set_hw_breakpoint(bp_to_use, target_address).is_ok() {
343 core.run()?;
344 // It is possible that we are stepping over long running instructions.
345 let status = core.wait_for_core_halted(Duration::from_millis(1000));
346
347 // Restore the original breakpoint.
348 if let Some(Some(bp)) = breakpoints.get(bp_to_use) {
349 core.set_hw_breakpoint(bp_to_use, *bp)?;
350 } else {
351 core.clear_hw_breakpoint(bp_to_use)?;
352 }
353
354 match status {
355 Ok(()) => {
356 // We have hit the target address, so all is good.
357 // NOTE: It is conceivable that the core has halted, but we have not yet stepped to the target address. (e.g. the user tries to step out of a function, but there is another breakpoint active before the end of the function.)
358 // This is a legitimate situation, so we clear the breakpoint at the target address, and pass control back to the user
359 Ok((
360 core.status()?,
361 core.read_core_reg(core.program_counter().id())?
362 .try_into()?,
363 ))
364 }
365 Err(error) => {
366 program_counter = core.halt(Duration::from_millis(500))?.pc;
367 if matches!(
368 error,
369 probe_rs::Error::Arm(ArmError::Timeout)
370 | probe_rs::Error::Riscv(RiscvError::Timeout)
371 | probe_rs::Error::Xtensa(XtensaError::Timeout)
372 ) {
373 // This is not a quick step and halt operation. Notify the user that we are not going to wait any longer, and then return the current program counter so that the debugger can show the user where the forced halt happened.
374 tracing::error!(
375 "The core did not halt after stepping to {:#010X}. Forced a halt at {:#010X}. Long running operations between debug steps are not currently supported.",
376 target_address,
377 program_counter
378 );
379 Ok((core.status()?, program_counter))
380 } else {
381 // Something else is wrong.
382 Err(DebugError::Other(format!(
383 "Unexpected error while waiting for the core to halt after stepping to {program_counter:#010X}. Forced a halt at {target_address:#010X}. {error:?}."
384 )))
385 }
386 }
387 }
388 } else {
389 // If we don't have breakpoints to use, we have to rely on single stepping.
390 // TODO: In theory, this could go on for a long time. Should we consider NOT allowing this kind of stepping if there are no breakpoints available?
391
392 Ok(step_to_address(target_address..=u64::MAX, core)?)
393 }
394}
395
396/// In some cases, we need to single-step the core, until ONE of the following conditions are met:
397/// - We reach the `target_address_range.end()`
398/// - We reach an address that is not in the sequential range of `target_address_range`,
399/// i.e. we stepped to some kind of branch instruction, or diversion to an interrupt handler.
400/// - We reach some other legitimate halt point (e.g. the user tries to step past a series of statements,
401/// but there is another breakpoint active in that "gap")
402/// - We encounter an error (e.g. the core locks up).
403fn step_to_address(
404 target_address_range: RangeInclusive<u64>,
405 core: &mut impl CoreInterface,
406) -> Result<(CoreStatus, u64), DebugError> {
407 while target_address_range.contains(&core.step()?.pc) {
408 // Single step the core until we get to the target_address;
409 match core.status()? {
410 CoreStatus::Halted(halt_reason) => match halt_reason {
411 HaltReason::Step | HaltReason::Request => continue,
412 HaltReason::Breakpoint(_) => {
413 tracing::debug!(
414 "Encountered a breakpoint before the target address ({:#010x}) was reached.",
415 target_address_range.end()
416 );
417 break;
418 }
419 // This is a recoverable error kind, and can be reported to the user higher up in the call stack.
420 other_halt_reason => {
421 return Err(DebugError::WarnAndContinue {
422 message: format!(
423 "Target halted unexpectedly before we reached the destination address of a step operation: {other_halt_reason:?}"
424 ),
425 });
426 }
427 },
428 // This is not a recoverable error, and will result in the debug session ending (we have no predicatable way of successfully continuing the session)
429 other_status => {
430 return Err(DebugError::Other(format!(
431 "Target failed to reach the destination address of a step operation: {other_status:?}"
432 )));
433 }
434 }
435 }
436 Ok((
437 core.status()?,
438 core.read_core_reg(core.program_counter().id())?
439 .try_into()?,
440 ))
441}