RISCV SOC开发环境 4——代码调试(openocd + gdb)

下载

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]
$

常用操作命令,

命令名称命令缩写命令说明
runr运行一个待调试的程序
continuec让暂停的程序继续运行
^c暂停程序
nextn运行到下一行
steps单步执行,遇到函数会进入
untilu运行到指定行停下来
finishfi结束当前调用函数,回到上一层调用函数处
return结束当前调用函数并返回指定值,到上一层函数调用处
jumpj将当前程序执行流跳转到指定行或地址
printp打印变量或寄存器值
backtracebt查看当前线程的调用堆栈
framef切换到当前调用线程的指定堆栈,up/down切换
thread切换到指定线程
breakb添加断点
tbreaktb添加临时断点
deleted删除断点
enable启用某个断点
disable禁用某个断点
watch监视某一个变量或内存地址的值是否发生变化
listl显示源码
where显示运行位置
infoi查看断点 / 线程等信息
ptypeptype查看变量类型
disassembledis查看汇编代码
set argsset args设置程序启动命令行参数
show argsshow args查看设置的命令行参数
quitq退出 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

引用

[1] https://openocd.org/

[2] https://gitee.com/mirrors/openocd

[3] https://github.com/riscv-software-src/riscv-isa-sim/tree/master

[4] http://c.biancheng.net/view/81

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值