更好的阅读体验
安装XV6
apt-get update
sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu
The latest xv6 source and text are available via
git clone git://github.com/mit-pdos/xv6-riscv.git
and
git clone git://github.com/mit-pdos/xv6-riscv-book.git
cd xv6-riscv
make qemu
安装成功
指针预备知识
#include <stdio.h>
#include <stdlib.h>
void f(void) {
int a[4];
int *b = malloc(16);
int *c;
int i;
printf("1: a = %p, b = %p, c = %p\n", a, b, c);
c = a;
for (i = 0; i < 4; i++)
a[i] = 100 + i;
c[0] = 200;
printf("2: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n",
a[0], a[1], a[2], a[3]);
c[1] = 300;
*(c + 2) = 301;
3[c] = 302;
printf("3: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n",
a[0], a[1], a[2], a[3]);
c = c + 1;
*c = 400;
printf("4: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n",
a[0], a[1], a[2], a[3]);
c = (int *) ((char *) c + 1);
*c = 500;
printf("5: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n",
a[0], a[1], a[2], a[3]);
b = (int *) a + 1;
c = (int *) ((char *) a + 1);
printf("6: a = %p, b = %p, c = %p\n", a, b, c);
}
int main(int ac, char **av) {
f();
return 0;
}
在QEMU中使用GDB
-
打开两个终端窗口
-
在第一个窗口中输入
make qemu-gdb
*** Now run 'gdb' in another window. qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -S -gdb tcp::25500
-
在第二个窗口中,修改自己
home
目录下的.gdbinit
文件,允许gdb
在xv6-riscv
这个目录启动的时候,加载该文件夹下的.gdbinit
文件。echo "add-auto-load-safe-path YOUR_PATH/xv6-riscv/.gdbinit " >> ~/.gdbinit
-
输入
gdb-multiarch
$ echo "add-auto-load-safe-path /home/ubuntu/xv6-riscv/.gdbinit " >> ~/.gdbinit $ gdb-multiarch GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.ht ml> 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 "x86_64-linux-gnu". 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". The target architecture is assumed to be riscv:rv64 warning: No executable has been specified and target does not support determining executable automatically. Try using the "file" command. 0x0000000000001000 in ?? () (gdb)
- 给代码打断点 简单来说,就是以下几步:
file user/_ls
b main
c
- 之后就是正常的
GDB
使用流程了
FINALLY:
第二个窗口:
(gdb) file user/_ls
Reading symbols from user/_ls...
(gdb) b main
Breakpoint 1 at 0x27a: file user/ls.c, line 75.
(gdb) c
Continuing.
[Switching to Thread 1.3]
Thread 3 hit Breakpoint 1, main (argc=0, argv=0x6c <fmtname+108>)
at user/ls.c:75
75 {
(gdb)
第一个窗口
(......)
xv6 kernel is booting
hart 1 starting
hart 2 starting
init: starting sh
$ ls
(等待 gdb 发送 continue 信号)
- 之后我们就可以正常地通过
GDB
调试程序啦~