How to use breakpoints, watch variables, and single-stepping?

 Debugging embedded systems or software often involves breakpoints, watch variables, and single-stepping to analyze code execution. Here’s a step-by-step guide for popular IDEs (Keil, IAR, STM32CubeIDE, Arduino, VS Code, etc.).




1. Setting Breakpoints

Breakpoints pause code execution at a specific line, allowing inspection of variables and registers.

Steps:

  1. Open your project in the IDE (e.g., STM32CubeIDE, Keil, Arduino).

  2. Navigate to the desired line in your code.

  3. Set a breakpoint by:

    • Clicking the left gutter (most IDEs).

    • Pressing F9 (VS Code, Keil).

    • Using Ctrl+Shift+B (some IDEs).

  4. Run in debug mode (F5 or click the debug button).

  5. Execution stops at the breakpoint.

Types of Breakpoints:

  • Hardware Breakpoints (limited, used in MCUs).

  • Software Breakpoints (more flexible, modifies code temporarily).

  • Conditional Breakpoints (pause only if a condition is met).


2. Watching Variables

Watch variables let you monitor real-time values of variables, registers, or expressions.

Steps:

  1. Start debugging (with breakpoints).

  2. Open the "Watch" window (usually View > Watch or Alt+3 in Keil).

  3. Add variables to watch:

    • Manually type the variable name.

    • Right-click a variable → "Add to Watch".

  4. Values update as you step through code.

Useful Tips:

  • Change display format (hex, decimal, binary).

  • Monitor peripheral registers (e.g., GPIOA->ODR in STM32).

  • Use expressions (e.g., (variable1 + variable2) / 2).


3. Single-Stepping (Step Into/Over/Out)

Single-stepping executes code line-by-line to trace logic flow.

Common Stepping Commands:

CommandShortcut (Most IDEs)Behavior
Step IntoF11Enters function calls.
Step OverF10Skips function details (executes but doesn’t dive in).
Step OutShift+F11Exits current function.
Run to CursorCtrl+F10Runs until the cursor’s line.

Example Workflow:

  1. Set a breakpoint where you suspect an issue.

  2. Start debugging (F5).

  3. Use F10/F11 to step through code.

  4. Check variables in the Watch window.

  5. Identify incorrect values or unexpected jumps.


4. Debugging in Different IDEs

STM32CubeIDE (ARM Cortex-M)

  • Breakpoints: Click left margin or Ctrl+Shift+B.

  • Watch Window: Window > Show View > Expressions.

  • Stepping: F5 (Run), F6 (Step Over), F7 (Step Into).

Keil µVision

  • Breakpoints: Click margin or F9.

  • Watch Window: View > Watch Windows.

  • Peripheral Debugging: View > System Viewer (for GPIO, UART, etc.).

Arduino (VS Code + PlatformIO)

  • Breakpoints: Click left margin.

  • Watch Variables: View > Debug Console.

  • Stepping: F10 (Step Over), F11 (Step Into).

IAR Embedded Workbench

  • Breakpoints: Click margin or F9.

  • Live Watch: View > Watch.

  • Disassembly View: Useful for low-level debugging.


5. Advanced Debugging Tips

✔ Use Conditional Breakpoints (pause only if x > 100).
✔ Monitor Memory (View > Memory in Keil/STM32CubeIDE).
✔ Check Call Stack to trace function hierarchy.
✔ Reset & Restart Debugging if the system locks up.


Summary Table

Debugging ActionHow To Use It
Set BreakpointClick left margin or press F9.
Watch VariableRight-click → "Add to Watch".
Step Over (F10)Execute current line, skip function details.
Step Into (F11)Dive into function calls.
Step Out (Shift+F11)Exit current function.

Troubleshooting Common Issues

❌ Breakpoints not hitting?
→ Check if optimization disabled (-O0 in compiler flags).
→ Verify debugger connection (JTAG/SWD).

❌ Variables show <optimized out>?
→ Disable compiler optimizations or declare variables as volatile.

❌ Debugger disconnects?
→ Ensure stable power supply and proper wiring.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How to add a GPS sensor to ESP32 for Wokwi?