Linux 0.11 实验环境搭建与调试
緣起
之前我寫過一篇博文:Linux 0.11 實驗環境搭建
本以為有了這個環境(gcc-3.4 & gdb-6.8),就可以調試無憂了。誰知遇到了以下問題:
(1)用 gdb 調試 main 函數的時候,無法輸出變量的值。總是提示:
No symbol “XXX” in current context.
(2)雖然在編譯時為 gcc 加上了-gdwarf-2 -g3這兩個選項,但仍然無法查看宏的定義。
如下圖所示:
對于(1),因為有-g選項,所以 gcc 編譯后確實生成了相關符號,但為什么 gdb 總說找不到符號呢?因為 gdb 版本過低,對于較高版本的 gcc 產生的符號表,它根本不認識,所以說找不到符號。解決辦法就是升級 gdb.
對于(2),我本以為原因同(1)。把 gdb 升級到 8.1 還是不行,看來另有原因。
我寫了一個小程序,gcc-5.4.0 配合 gdb-8.1,調試時可以打印宏的值,info macro xxx等命令沒有問題。所以,我打算用高版本的 gcc 編譯 Linux-0.11.
問題來了,Linux-0.11 已經是很古老的代碼了,用高版本的 gcc 根本編譯不過,怎么辦呢?好在有很多前輩勇于探索,修改了源碼和 Makefile,使得 Linux-0.11 可以被高版本的 gcc 編譯通過。
環境搭建
他山之石,可以攻玉。搜了一波后,發現了一個叫“泰曉科技”的網站,其官網是http://tinylab.org/,官方代碼倉庫是 https://github.com/tinyclub
代碼倉庫里面有個項目——Linux 0.11 Lab
下載源碼
得到linux-0.11-lab-master.zip文件,解壓后進入目錄linux-0.11-lab-master,如下圖:
用make help命令可以查看幫助信息。
—– Linux 0.11 Lab (http://tinylab.org/linux-0.11-lab) —–
:: Compile ::
make –generate a kernel floppy Image with a fs on hda1
make clean – clean the object files
make distclean – only keep the source code files
:: Test ::
make start – start the kernel in vm (qemu/bochs)
make start-fd – start the kernel with fs in floppy
make start-hd – start the kernel with fs in hard disk
make start-hd G=0 – start with curses based terminal, instead of SDL
:: Debug ::
make debug – debug the kernel in qemu/bochs & gdb at port 1234
make debug-fd – debug the kernel with fs in floppy
make debug-hd – debug the kernel with fs in hard disk
make debug DST=boot/bootsect.sym – debug bootsect
make debug DST=boot/setup.sym – debug setup
make boot BOCHS=bochs-debugger VM=bochs – debug with bochs internal debugger
make switch – switch the emulator: qemu and bochs
make boot VM=qemu|bochs – switch the emulator: qemu and bochs
:: Read ::
make cscope – genereate the cscope index databases
make tags – generate the tag file
make cg – generate callgraph of the default main entry
make cg f=func d=dir|file b=browser – generate callgraph of func in file/directory
:: More ::
>>> README.md <<<
~ Enjoy It ~
—–Linux 0.11 Lab (http://tinylab.org/linux-0.11-lab)—–
—> Linux Kernel Lab (http://tinylab.org/linux-lab) <—
安裝一些軟件
這里以 Linux Ubuntu 操作系統為例。作者推薦用 docker.
The docker install method is recommended for all systems, including Linux, Windows and Mac OS.
不過我沒有用 docker,還是用比較原始的辦法。
Here is the deprecated method:
The Linux distributions: debian and ubuntu (>= 14.04) are recommended
Install basic tools
$ sudo apt-get install vim cscope exuberant-ctags build-essential qemu lxterminalOptional
$ sudo apt-get install bochs vgabios bochsbios bochs-doc bochs-x >libltdl7 bochs-sdl bochs-term $ sudo apt-get install graphviz cflow編譯
在linux-0.11-lab-master目錄下,運行make命令.
運行
$ make start采用 qemu 或者 bochs 模擬器運行 Linux-0.11,如果想切換模擬器,可以用
$ make switch調試
$ make debug運行后出現如下界面:
再開一個終端,還是在linux-0.11-lab-master目錄下,運行命令
$ gdb --quiet src/kernel.sym這時候,就可以用 gdb 的各種命令進行調試了。
如何查看宏
正如 GDB 官網上說:
We pass the -gdwarf-2 and-g3 flags to ensure the compiler includes information about preprocessor macros in the debugging information.
所以,先要給 gcc 添加 -gdwarf-2 和-g3 選項。對于本實驗環境,就是修改src 目錄下的 Makefile.head 文件。為第 29 行的CFLAGS加上 -gdwarf-2 -g3 。
如圖:
之后重新編譯,再次調試。
在調試的時候,可以用p(print)命令查看宏的值,還可以用
info macro 宏名
例如:
【參考資料】
https://sourceware.org/gdb/onlinedocs/gdb/Macros.html
https://github.com/tinyclub/linux-0.11-lab/blob/master/README.md
總結
以上是生活随笔為你收集整理的Linux 0.11 实验环境搭建与调试的全部內容,希望文章能夠幫你解決所遇到的問題。