|
5 | 5 | :hidden: |
6 | 6 | :maxdepth: 4 |
7 | 7 |
|
| 8 | +调试工具的使用 |
| 9 | +------------------------ |
| 10 | + |
| 11 | +下载或编译GDB |
| 12 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 13 | +可以在 :doc:`chapter0/5setup-devel-env` 中下载编译好的二进制(版本为8.3.0,由于包括整个哦那工具链,解压后大小约为1G),也可以编译最新版本(仅gdb,大小约为300M) |
| 14 | + |
| 15 | +.. code-block:: console |
| 16 | +
|
| 17 | + wget https://github.com/riscv/riscv-binutils-gdb/archive/refs/heads/riscv-binutils-2.36.1.zip |
| 18 | + unzip riscv-binutils-2.36.1.zip |
| 19 | + mkdir build |
| 20 | + cd build |
| 21 | + ../riscv-binutils-2.36.1/configure --target=riscv64-unknown-elf |
| 22 | + make |
| 23 | +
|
| 24 | +如果是编译好的二进制,gdb在 ``./bin/riscv64-unknown-elf-gdb`` 中。如果是自己编译的最新版本,gdb在 ``build/bin/gdb`` 中。你可以移动到一个你喜欢的位置。 |
| 25 | + |
| 26 | +首先修改 ``Makefile`` ,下以 ``ch1`` 分支的为例: |
| 27 | + |
| 28 | +1. 第三行 ``release`` 改为 ``debug`` |
| 29 | + |
| 30 | +2. 第46行去掉 ``--release`` |
| 31 | + |
| 32 | +3. 第66行的qemu的选项中增加 ``-s -S`` |
| 33 | + |
| 34 | +这时,运行 ``make run`` 应该会停在系统开始前,等待 ``gdb`` 客户端连接。 |
| 35 | + |
| 36 | +在命令行中直接使用gdb |
| 37 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 38 | + |
| 39 | +.. code-block:: console |
| 40 | + # 启动gdb,传入二进制文件作为参数。 |
| 41 | + # 记得修改路径 |
| 42 | + ./bin/riscv64-unknown-elf-gdb /Volumes/Code/rCore-Tutorial-v3/os/target/riscv64gc-unknown-none-elf/debug/os |
| 43 | + # 导入源码路径 |
| 44 | + (gdb) directory /Volumes/Code/rCore-Tutorial-v3/os/ |
| 45 | + Source directories searched: /Volumes/Code/rCore-Tutorial-v3/os:$cdir:$cwd |
| 46 | + # 连接到qemu中的gdb-server |
| 47 | + (gdb) target remote localhost:1234 |
| 48 | + Remote debugging using localhost:1234 |
| 49 | + 0x0000000000001000 in ?? () |
| 50 | + # 现在可以开始调试了,下面给出一些示例指令: |
| 51 | + (gdb) b rust_main |
| 52 | + Breakpoint 1 at 0x802005aa: file /Volumes/Code/rCore-Tutorial-v3/os/src/main.rs, line 36. |
| 53 | + (gdb) continue |
| 54 | + Continuing. |
| 55 | +
|
| 56 | + Breakpoint 1, os::rust_main () at /Volumes/Code/rCore-Tutorial-v3/os/src/main.rs:36 |
| 57 | + 36 clear_bss(); |
| 58 | + (gdb) l |
| 59 | + 31 fn sbss(); |
| 60 | + 32 fn ebss(); |
| 61 | + 33 fn boot_stack(); |
| 62 | + 34 fn boot_stack_top(); |
| 63 | + 35 } |
| 64 | + 36 clear_bss(); |
| 65 | +
|
| 66 | +在IDE中直接使用gdb |
| 67 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 68 | + |
| 69 | +下面以[CLion](https://www.jetbrains.com/clion/)中[Rust插件](https://plugins.jetbrains.com/plugin/8182-rust)为例。其他IDE的配置大同小异。 |
| 70 | + |
| 71 | +注意:上面提供的GDB二进制版本过低,需要使用自己编译的最新版本的GDB。 |
| 72 | + |
| 73 | +1. 在 CLion 中打开项目(os文件夹),选择 ``cargo project`` 。 |
| 74 | + |
| 75 | +2. 在项目中新建一个 ``sh`` 文件,输入以下内容并给予可执行权限: |
| 76 | + |
| 77 | +.. code-block:: console |
| 78 | + #!/usr/bin/env bash |
| 79 | + killall qemu-system-riscv64 # 由于无法在debug结束时关闭虚拟机,我们在debug开始时关闭上一次开启的虚拟机。 |
| 80 | + nohup bash -c "make run > run.log 2>&1" & # 后台启动qemu |
| 81 | + echo "Done!" |
| 82 | +
|
| 83 | +3. 在右上角点击 ``Edit Configurations`` ,新增一个 ``GDB Remote Debug`` ,并如图配置: |
| 84 | + |
| 85 | +.. image:: clion_config.jpg |
8 | 86 |
|
| 87 | +第 1 个红框中选择你的自己编译的gdb路径 |
| 88 | +第 3, 4 个红框中根据你的代码路径做适当修改 |
| 89 | +第 5 个红框中,点击下面加号,选择`External Tools`,并选择上面新建的`sh`脚本。 |
9 | 90 |
|
10 | 91 | 分析可执行文件 |
11 | 92 | ------------------------ |
|
0 commit comments