关于GDB实现ARM汇编代码每秒自动单步执行的技术问询
Got it, let's sort this out for your booth demo—you need GDB to automatically step through ARM assembly code every second, no manual key presses required, until you hit Ctrl+C. Here are two reliable, easy-to-use approaches:
Method 1: GDB Built-In Script (Cleanest for Reuse)
You can leverage GDB's internal command loop to repeat single-step instructions with a 1-second delay. Here's how to set it up:
- Create a GDB script file (name it
auto-step.gdbfor example) with this content:
# Auto-step through ARM instructions every second until Ctrl+C while 1 # Step one individual assembly instruction (use "step" instead if you want to step over functions) stepi # Pause for 1 second using the system sleep command shell sleep 1 end
- Launch GDB and load your ARM executable as normal:
gdb your-arm-demo-program.elf
- Once inside GDB, start the auto-stepping by running:
source auto-step.gdb
GDB will immediately begin stepping through instructions every second. To stop the loop, just press Ctrl+C—this will break the cycle and return you to the GDB prompt.
Method 2: One-Liner Command (No Script File Needed)
If you don't want to create a separate script, you can run the loop directly in the GDB prompt:
while 1; stepi; shell sleep 1; end
This does exactly the same as the script—hit Ctrl+C whenever you need to stop the auto-stepping.
Quick Tips for Your Demo
- To start auto-stepping immediately when GDB launches, pass the script as an argument:
gdb -x auto-step.gdb your-arm-demo-program.elf - Make sure your ARM program is compiled with debug symbols (
-gflag) so GDB can properly resolve and step through assembly instructions. - Adjust the delay by changing
sleep 1to another value (e.g.,sleep 0.5for half-second steps, orsleep 2for slower pacing).
内容的提问来源于stack exchange,提问作者71GA

