使用Qt Creator调试STM32遇故障,寻求调试器配置方法
Hey, let’s work through this debugging headache together—this is a pretty common snag when setting up bare-metal STM32 projects in Qt Creator with ARM GCC. Let’s break down the fixes and configurations to get your breakpoints, source-level stepping, and debugger features working properly:
1. First: Make Sure Your Build Generates Debug Information (No Optimizations!)
The #1 reason breakpoints fail or jump to disassembly is missing debug symbols or compiler optimizations that scramble your source code. For your QBS project:
- Open your
.qbsproject file and add these lines to your product configuration:cpp.debugInformation: true cpp.optimization: "none" // Critical—optimizations will break source-level debugging cpp.stripSymbols: false - Switch your Qt Creator build profile to Debug (not Release) before compiling. Optimized builds will rearrange code, making breakpoints map to the wrong lines or disappear entirely.
2. Configure the Correct ARM GDB Debugger
Qt Creator needs the ARM-specific GDB binary, not your system’s default GDB:
- Go to
Tools > Options > Devices > Debuggers - Click
Addand browse to your ARM GCC toolchain’sarm-none-eabi-gdb(Windows:.exe, Linux/macOS: no extension) - Name it something like "ARM STM32 GDB" and save it
- Now, go to your project’s
Run Settings > Debuggertab, and select this newly added ARM GDB from the dropdown.
3. Set Up the Debug Server (OpenOCD or ST-Link GDB Server)
You need a debug server to bridge GDB with your ST-Link. Here’s how to configure it in Qt Creator:
Option A: Using OpenOCD (Most Flexible)
- In your debug configuration’s
Debuggertab, scroll to theStartupsection - Check
Start remote debug server - For the
Command, enter:
Note: If you’re using an older ST-Link v2, useopenocd -f interface/stlink.cfg -f target/stm32f3x.cfginterface/stlink-v2.cfginstead. Adjust the target file if needed for your exact chip variant. - Set the
Remote portto2331(OpenOCD’s default)
Option B: Using ST-Link GDB Server
- If you prefer ST’s official tool, set the command to your
st-link_gdbserver.exepath (Windows) or the equivalent for Linux/macOS, with arguments like:st-link_gdbserver.exe -p 2331 -d -m -s - Ensure the remote port matches what you set in GDB.
4. Fix Source Code Path Mismatches
If Qt Creator can’t map compiled code to your local source files, it’ll jump to disassembly instead:
- In your debug configuration’s
Debuggertab, find theSource Path Mappingsection - Add a mapping if your build output’s source paths don’t match your local project structure. For example:
- Compiled path:
/build-dir/src/main.c - Local path:
/home/you/project/src/main.c
- Compiled path:
- This tells GDB exactly where to find your source files when debugging.
5. Enable Registers & Watchpoints
Once debugging is working, you can access these features easily:
- Registers View: When paused in debugging, open the
Debugpanel (left sidebar, or pressShift+F4). Look for theRegisterstab—you can expand core registers, peripheral registers, and custom groups to monitor values in real time. - Watch Variables: In the same
Debugpanel, go to theWatchtab. Click the+button and type in a variable name or expression (e.g.,my_counter). Just make sure the variable is in scope and wasn’t optimized out (remember step 1!). - If watch variables show
<optimized out>, double-check that you disabled optimizations in your QBS config.
6. Quick Hardware/Connection Checks
If everything above is set and you still have issues:
- Unplug and re-plug your ST-Link/development board, ensure power is stable
- Re-flash your firmware to make sure the latest debug build is on the chip
- Test your debug server outside Qt Creator: run the OpenOCD/ST-Link command in a terminal to confirm it connects to your STM32 without errors.
内容的提问来源于stack exchange,提问作者Leonid

