使用Bochs和GDB对内核进行源代码级调试 |
Written by YY |
Wednesday, 21 April 2010 12:49 |
编译 Bochs 要加入 --enable-gdb-stub 选项: $ ./configure --enable-gdb-stub $ make $ sudo make install
打开另一控制台,运行 gdb $ gdb
(gdb) file kernel.bin ← 注意这里的 kernel.bin 必须是加入了 -g 编译选项的带调试符号的内核 Reading symbols from /home/forrest/local/src/osfs/oranges/phases/chapter11/a/kernel.bin...done. (gdb) target remote localhost:1234 Remote debugging using localhost:1234 0x0000fff0 in ?? () (gdb) b start.c:26 Breakpoint 1 at 0x14a6: file kernel/start.c, line 26. (gdb) c Continuing. Breakpoint 1, cstart () at kernel/start.c:26 26 disp_str("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----\"cstart\" begins-----\n"); (gdb) n 29 memcpy( &gdt, /* New GDT */ (gdb)
由于每次都需要执行 file kernel.bin 和 target remote localhost:1234 两个命令,所以可以建立一个 .gdbinit 文件,比如: $ vi .gdbinit file kernel.bin target remote localhost:1234 set disassembly-flavor intel b start.c:26 b kernel/main.c:183这样下次直接执行 gdb 这个文件里的命令即可自动执行。 .gdbinit 可以做许多事,比如上例中就加了俩断点。 在 .gdbinit 中可添加自定义函数,比如: define lsproc set $count = 16 set $idx = 0 printf "The first %d TASKS/PROCS:\n",$count while($idx 这样在 gdb 中执行一个 lsproc,便可打印出所有进程的信息,巨方便:(gdb) lsproc The first 16 TASKS/PROCS: [ 0] TASK: TTY p_flags: 4h [ 1] TASK: SYS p_flags: 4h [ 2] TASK: HD p_flags: 4h [ 3] TASK: FS p_flags: 4h [ 4] TASK: MM p_flags: 4h [ 5] PROC: INIT p_flags: Ch [ 6] PROC: TestA p_flags: 0h [ 7] PROC: TestB p_flags: 0h [ 8] PROC: TestC p_flags: 0h [ 9] PROC: INIT_9 p_flags: 4h [10] PROC: INIT_10 p_flags: 4h |