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:
Open your project in the IDE (e.g., STM32CubeIDE, Keil, Arduino).
Navigate to the desired line in your code.
Set a breakpoint by:
Clicking the left gutter (most IDEs).
Pressing
F9
(VS Code, Keil).Using
Ctrl+Shift+B
(some IDEs).
Run in debug mode (
F5
or click the debug button).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:
Start debugging (with breakpoints).
Open the "Watch" window (usually
View > Watch
orAlt+3
in Keil).Add variables to watch:
Manually type the variable name.
Right-click a variable → "Add to Watch".
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:
Command | Shortcut (Most IDEs) | Behavior |
---|---|---|
Step Into | F11 | Enters function calls. |
Step Over | F10 | Skips function details (executes but doesn’t dive in). |
Step Out | Shift+F11 | Exits current function. |
Run to Cursor | Ctrl+F10 | Runs until the cursor’s line. |
Example Workflow:
Set a breakpoint where you suspect an issue.
Start debugging (
F5
).Use
F10
/F11
to step through code.Check variables in the Watch window.
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 Action | How To Use It |
---|---|
Set Breakpoint | Click left margin or press F9 . |
Watch Variable | Right-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.
评论
发表评论