安装调试工具

编译

其实说安装有点不准确,因为之前已经把GDB已经悄悄的装进了环境变量里。其实就是在我们安装编译器的时候我们只需要打开lab0的文件夹,然后打开终端,输入make,就可以进行编译了

$ make
+ cc kern/init/entry.S
+ cc kern/init/init.c
+ cc kern/libs/stdio.c
+ cc kern/driver/console.c
+ cc libs/string.c
+ cc libs/printfmt.c
+ cc libs/readline.c
+ cc libs/sbi.c
+ ld bin/kernel
riscv64-unknown-elf-objcopy bin/kernel --strip-all -O binary bin/ucore.img

可以看到多了几个文件夹,我们之后再来介绍这些。

修改脚本

工具链(之前下载的编译器里面的工具)里提供了相应的gdb工具,要使用gdb,就需要对脚本进行修改。在源代码的文件夹中,找到lab0中的Makefile文件然后找到CC := $(GCCPREFIX)gcc在后面添加-g。

使用GDB进行调试

因为gdb和qemu是两个应用不能直接交流,比较常用的方法是以tcp进行通讯,也就是让qemu在localhost::1234端口上等待。

lab0文件夹下打开终端,运行

$ qemu-system-riscv64 -S -s -hda ./bin/ucore.img 
WARNING: Image format was not specified for './bin/ucore.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
VNC server running on 127.0.0.1:5900xxxxxxxxxx qemu-system-riscv64 -S -s -hda ./bin/ucore.img$ qemu-system-riscv64 -S -s -hda ./bin/ucore.img WARNING: Image format was not specified for './bin/ucore.img' and probing guessed raw.         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.         Specify the 'raw' format explicitly to remove the restrictions.VNC server running on 127.0.0.1:5900

然后在该文件夹下重新打开一个终端,运行

$ riscv64-unknown-elf-gdb ./bin/kernel
GNU gdb (SiFive GDB 8.3.0-2020.04.0) 8.3
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-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://github.com/sifive/freedom-tools/issues>.
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 ./bin/kernel...
(gdb)

接着连接qemu:

(gdb) target remote :1234
Remote debugging using :1234
0x0000000000001000 in ?? ()

连接成功输入si就可以进行运行下一条指令,

(gdb) si
0x0000000000001004 in ?? ()

这样就代表可以正常运行了噢,具体调试步骤与方法,看一下前面哈。

最后更新于