下载
gdb随工具链一起已经有了,只需要下载openocd。从国内镜像克隆,
git clone https://gitee.com/mirrors/openocd.git
安装
安装的时候要使能用到的适配器驱动,比如 --enable-ftdi 等。
cd openocd
./bootstrap # (when building from the git repository)
./configure --prefix=$RISCV --enable-remote-bitbang --enable-jtag_vpi --disable-werror
make
sudo make install
实例——GDB调试
1)c 源代码
$ cat rot13.c
char text[] = "Vafgehpgvba frgf jnag gb or serr!";
// Don't use the stack, because sp isn't set up.
volatile int wait = 1;
int main()
{
while (wait)
;
// Doesn't actually go on the stack, because there are lots of GPRs.
int i = 0;
while (text[i]) {
char lower = text[i] | 32;
if (lower >= 'a' && lower <= 'm')
text[i] += 13;
else if (lower > 'm' && lower <= 'z')
text[i] -= 13;
i++;
}
done:
while (!wait)
;
}
2)链接脚本
$ cat spike.lds
OUTPUT_ARCH( "riscv" )
SECTIONS
{
. = 0x10010000;
.text : { *(.text) }
.data : { *(.data) }
}
3)编译
$ riscv64-unknown-elf-gcc -g -Og -o rot13-64.o -c rot13.c
$ riscv64-unknown-elf-gcc -g -Og -T spike.lds -nostartfiles -o rot13-64 rot13-64.o
注意:
- gcc 默认编译64位应用,如果是32位编译,则是“riscv64-unknown-elf-gcc -march=rv32gc -misa=ilp32”
- 选项“-T spike.lds”是指定链接脚本
4)spike 仿真应用
$ spike --rbb-port=9824 -m0x10000000:0x20000 rot13-64
Listening for remote bitbang connection on port 9824.
注意:
- spike 默认是64位仿真,如果是32位仿真,则是“spike --isa=rv32gc”
- 适配器 Remote-bitbang 的端口号是 9824
5)openocd 连接 remote-bitbang
目标 spike 的配置文件,
$ cat spike.cfg
interface remote_bitbang
remote_bitbang_host localhost
remote_bitbang_port 9824
set _CHIPNAME riscv
jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x10e31913
set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME riscv -chain-position $_TARGETNAME
gdb_report_data_abort enable
init
halt
连接spike。
$ openocd -f spike.cfg
Open On-Chip Debugger 0.10.0-dev-00002-gc3b344d (2017-06-08-12:14)
...
riscv.cpu: target state: halted
6)gdb 连接 openocd
连接端口号是 3333。
$ riscv64-unknown-elf-gdb rot13-64
GNU gdb (GDB) 12.1
Copyright (C) 2022 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=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
https://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"...
Reading symbols from rot13-64...
(gdb) target remote localhost:3333
Remote debugging using localhost:3333
0x10010004 in main () at rot13.c:9
9 while (wait)
(gdb) p wait
$1 = 1
(gdb) set wait=0
(gdb) n
14 while (text[i]) {
(gdb) n
15 char lower = text[i] | 32;
(gdb) n
16 if (lower >= 'a' && lower <= 'm')
(gdb) b done
Breakpoint 1 at 0x10010060: file rot13.c, line 23.
(gdb) c
Continuing.
Breakpoint 1, main () at rot13.c:24
24 while (!wait)
(gdb) p i
$4 = 33
(gdb) set wait=1
(gdb) n
0x00000000 in ?? ()
(gdb) q
A debugging session is active.
Inferior 1 [Remote target] will be detached.
Quit anyway? (y or n) y
Detaching from program: /home/asic/spike-ex/rot13-64, Remote target
Ending remote debugging.
[Inferior 1 (Remote target) detached]
$
常用操作命令,
命令名称 | 命令缩写 | 命令说明 |
run | r | 运行一个待调试的程序 |
continue | c | 让暂停的程序继续运行 |
^c | 暂停程序 | |
next | n | 运行到下一行 |
step | s | 单步执行,遇到函数会进入 |
until | u | 运行到指定行停下来 |
finish | fi | 结束当前调用函数,回到上一层调用函数处 |
return | 结束当前调用函数并返回指定值,到上一层函数调用处 | |
jump | j | 将当前程序执行流跳转到指定行或地址 |
p | 打印变量或寄存器值 | |
backtrace | bt | 查看当前线程的调用堆栈 |
frame | f | 切换到当前调用线程的指定堆栈,up/down切换 |
thread | 切换到指定线程 | |
break | b | 添加断点 |
tbreak | tb | 添加临时断点 |
delete | d | 删除断点 |
enable | 启用某个断点 | |
disable | 禁用某个断点 | |
watch | 监视某一个变量或内存地址的值是否发生变化 | |
list | l | 显示源码 |
where | 显示运行位置 | |
info | i | 查看断点 / 线程等信息 |
ptype | ptype | 查看变量类型 |
disassemble | dis | 查看汇编代码 |
set args | set args | 设置程序启动命令行参数 |
show args | show args | 查看设置的命令行参数 |
quit | q | 退出 gdb |
gdb有3种断点,分别为普通断点、观察断点和捕捉断点,
break line_num
watch var
catch event
6)telnet 连接 openocd
连接端口号是 4444。
$ telnet localhost 4444
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
>
常用操作命令,
命令 | 说明 |
---|---|
init / reset | 初始化 / 复位 |
halt / resume | 停止 / 恢复 |
reg | 查看寄存器 |
program | 烧录 flash |
flash read_bank | 读取 flash |
引用
[2] https://gitee.com/mirrors/openocd
[3] https://github.com/riscv-software-src/riscv-isa-sim/tree/master