GDB调试命令整理(以RISC-V为例)
1. 进入GDB环境
$ riscv32-elf-gdb GNU gdb (2022-02-07_riscv32-elf-0278d8cc40b) 8.2.50.20190522-git Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv32-elf". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word". [info] Loading .Andesgdbinit. [info] .Andesgdbinit loaded. (gdb)
2.连接目标板
target remote:1132 Remote debugging using :1132 warning: No executable has been specified and target does not support determining executable automatically. Try using the "file" command. 0x00000000 in ?? () tap0_target_0(gdb)
3.导入elf文件
tap0_target_0(gdb) file IMG_C0.elf A program is being debugged already. Are you sure you want to change the file? (y or n) y Load new symbol table from "IMG_C0.elf"? (y or n) y Reading symbols from IMG_C0.elf...
4.load文件到内存
tap0_target_0(gdb) load Loading section .vec, size 0x8a lma 0x0 Loading section .text, size 0x12bc lma 0x100 Loading section .data, size 0x10 lma 0x13bc Start address 0xc, load size 4950 Transfer rate: 105 KB/sec, 1650 bytes/write
5.添加普通断点
tap0_target_0(gdb) b trap_handler Note: breakpoints 1, 2, 3 and 4 also set at pc 0x606. Breakpoint 5 at 0x606: file trap.c, line 83.
地址断点: b *address
6.查看与删除断点
tap0_target_0(gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 4 times 2 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 3 times 3 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 2 times 4 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 1 time 5 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 1 time 6 breakpoint keep y 0x00000606 trap.c:83 tap0_target_0(gdb) delete 1 tap0_target_0(gdb) info b Num Type Disp Enb Address What 2 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 3 times 3 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 2 times 4 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 1 time 5 breakpoint keep y 0x00000606 in trap_handler at trap.c:83 breakpoint already hit 1 time 6 breakpoint keep y 0x00000606 trap.c:83
7.运行
tap0_target_0(gdb) c
Continuing.
8.查看调用栈
tap0_target_0(gdb) bt #0 0x000008aa in delay_us (nus=nus@entry=1) at drv_timer.c:101 #1 0x00000690 in mext_interrupt () at irq.c:100 #2 0x00000618 in trap_handler (mcause=<optimized out>, context=0x10007e 0) at trap.c:87 #3 0x00000138 in trap_entry () at start.S:170
9.查看内存
tap0_target_0(gdb) x 0x01000000 0x1000000 <uCoreId>: 0x00000000
10.写入内存
tap0_target_0(gdb) set {int}0x01000000=0x88 tap0_target_0(gdb) x 0x01000000 0x1000000 <uCoreId>: 0x00000088
11.查看寄存器
tap0_target_0(gdb) info reg ra 0x690 0x690 <mext_interrupt+14> sp 0x10007c0 0x10007c0 gp 0x1000800 0x1000800 tp 0x0 0x0 <reset_vector> t0 0x18000 98304 t1 0x186a0 100000 t2 0x186a0 100000 fp 0x10007e0 0x10007e0 s1 0x1000000 16777216 a0 0x3e8 1000 a1 0x64 100 a2 0x1 1 a3 0xe6000000 -436207616 a4 0x6c962fc 113861372 a5 0x1 1 a6 0x6c96360 113861472 a7 0x0 0 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x1 1 t4 0xe6000000 -436207616 t5 0x1 1 t6 0x6c962fc 113861372 pc 0x8aa 0x8aa <delay_us+60>
12.写入寄存器
tap0_target_0(gdb) print $gp=0x01000900 $1 = (void *) 0x1000900 tap0_target_0(gdb) info reg ra 0x690 0x690 <mext_interrupt+14> sp 0x10007c0 0x10007c0 gp 0x1000900 0x1000900 tp 0x0 0x0 <reset_vector> t0 0x18000 98304 t1 0x186a0 100000 t2 0x186a0 100000 fp 0x10007e0 0x10007e0 s1 0x1000000 16777216 a0 0x3e8 1000 a1 0x64 100 a2 0x1 1 a3 0xe6000000 -436207616 a4 0x6c962fc 113861372 a5 0x1 1 a6 0x6c96360 113861472 a7 0x0 0 s2 0x0 0 s3 0x0 0 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0x0 0 s8 0x0 0 s9 0x0 0 s10 0x0 0 s11 0x0 0 t3 0x1 1 t4 0xe6000000 -436207616 t5 0x1 1 t6 0x6c962fc 113861372 pc 0x8aa 0x8aa <delay_us+60>
13.导出内存镜像
tap0_target_0(gdb) dump binary memory /home/x/work/N45/a.bin 0x40000000 0x401ea000 tap0_target_0(gdb)
14.导入内存镜像
tap0_target_0(gdb) restore /home/x/work/N45/a.bin binary 0x40000000 Restoring binary file /home/x/work/N45/a.bin into memory (0x40000000 to 0x401ea000)
15.添加内存断点
tap0_target_0(gdb) watch *(int*)0xc Hardware watchpoint 7: *(int*)0xc
15.删除内存断点
tap0_target_0(gdb) info watch Num Type Disp Enb Address What 7 hw watchpoint keep y *(int*)0xc tap0_target_0(gdb) delete 7 tap0_target_0(gdb) info watch No watchpoints. tap0_target_0(gdb)