Better learning experience with video:
https://www.bilibili.com/video/BV12z4y177VQ/?vd_source=701807c4f8684b13e922d0a8b116af31
Now let’s build a bochs environment that supports gdb debugging. Because the later code is all written in C, gdb debugging is essential if you want to clarify the code logic.
Environment vmware + deepin-desktop-community-20.8-amd64
Install other required things first:
sudo apt install build-essential
sudo apt-get install libghc-x11-dev
sudo apt-get install xorg-dev
Download Bochshttps://udomain.dl.sourceforge.net/project/bochs/bochs/2.6.8/bochs-2.6.8.tar.gz
After downloading, move it to the desired location in the virtual machine, and then unzip it, command:tar -zxvf bochs-2.6.8.tar.gz
Create an empty directory for bochs to be installed
mkdir bochs-gdb
Enter the decompressed bochs-2.6.8 foldercd bochs-2.6.8
Configure the config file of bochs (the following –prefix is the directory where you want to install bochs), compile and install
./configure --prefix=/home/rlk/Desktop/bochs-gdb --enable-gdb-stub --enable-disasm --enable-iodebug --enable-x86-debugger --with-x --with-x11 LDFLAGS='-pthread'
make
make install
Enter the directory where bochs is installed cd ..
cd bochs-gdb
and create bochsrc.disk
the command: touch bochsrc.disk
, and write down the configuration information (modify romimage:
, romimage:
, and keyboard:
the first part of the following path information to be your own bochs installation path)
megs : 32
romimage: file=/home/rlk/Desktop/bochs-gdb/share/bochs/BIOS-bochs-latest
vgaromimage: file=/home/rlk/Desktop/bochs-gdb/share/bochs/VGABIOS-lgpl-latest
boot: disk
log: bochs.out
mouse:enabled=0
keyboard:keymap=/home/rlk/Desktop/bochs-gdb/share/bochs/keymaps/x11-pc-us.map
ata0:enabled=1,ioaddr1=0x1f0,ioaddr2=0x3f0,irq=14
ata0-master: type=disk, path="hd60M.img", mode=flat,cylinders=121,heads=16,spt=63
gdbstub:enabled=1,port=1234,text_base=0,data_base=0,bss_base=0
Create a boot disk
bin/bximage
Then enter the following in the input box in sequence, enter one, and press Enter once.
1
hd
flat
60
hd60M.img
Then in the Makefile
ASFLAGS
Add the -g parameter to CFLAGS
both, so that the .o file generated contains debugging information, such as symbols. The final kernel.bin generated by ld also contains debugging information. and then use
objcopy --only-keep-debug kernel.bin kernel.sym
Before loading the kernel image into Bochs, export the symbol table format that GDB can understand. Or add rules to the Makefile like I did, so that kernel.sym
the file is automatically generated when making all
Modify ( Makefiel )
HD60M_PATH=/home/rlk/Desktop/bochs-gdb/hd60M.img #hd60M的路径改成支持GDB调试的bochs环境内的磁盘
ASFLAGS= -f elf -g
CFLAGS= -Wall $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes -m32 -fno-stack-protector -g
.PHONY:mk_dir hd clean build all boot gdb_symbol #定义了7个伪目标
#生成可以被GDB理解的符号表,用于GDB调试
gdb_symbol:
objcopy --only-keep-debug $(BUILD_DIR)/kernel.bin $(BUILD_DIR)/kernel.sym
all:mk_dir boot build hd gdb_symbol
#make all 就是依次执行mk_dir build hd gdb_symbol
Compile and write our kernel.bin file to hd60M.img
When you start the bochs virtual machine bin/bochs -f bochsrc.disk
, you will be prompted to wait for the gdb connection. Now we open a new terminal and enter gdb to start gdb. If there is no gdb, you need to install it. Command:sudo apt install gdb
Enter the command in the gdb window: target remote localhost:1234
to connect to bochs for debugging
Then enter the command: symbol-file /home/rlk/Desktop/the_truth_of_operationg_system/chapter_12/c/build/kernel.sym
to load the symbol table of kernel.bin
Now you can debug C language with gdb. You need to learn how to debug with gdb by yourself.