從頭寫一個(gè)操作系統(tǒng) 12 (構(gòu)建工具->GDB調(diào)試QEMU)

Concepts you may want to Google beforehand: monolithic kernel, microkernel, debugger, gdb

請自行g(shù)oogle:monolithic kernel, microkernel, debugger, gdb

Goal: Pause and organize our code a little bit. Then learn how to debug the kernel with gdb

本節(jié)課目標(biāo):稍微暫停,梳理一下我們的代碼,然后學(xué)習(xí)如何使用gdb調(diào)試我們的kernel昨稼。

Maybe you didn't realize it, but you already have your own kernel running!
也許你沒有意識(shí)到你已經(jīng)擁有了一個(gè)可以跑起來的內(nèi)核。

However, it does very little, just print an 'X'. Now is the time to stop for a moment and organize the code into folders, create a scalable Makefile for future code, and think on a strategy.

雖然我們的內(nèi)核還很小坷备,只能打印'X'。但現(xiàn)在是時(shí)候停一下挨务,將這些代碼放入各自的文件夾中击你,寫一個(gè)Makefile。

Take a look at the new folder structure. Most of the files have been symlinked from previous lessons, so if we have to change them at some point, it will be a better idea to remove the symlink and create a new file.

看一看新的文件結(jié)構(gòu)吧谎柄,幾乎都是從之前的課程目錄中引用過的丁侄,所以如果要修改這些文件,最好是刪掉這些引用重新寫個(gè)新的朝巫。

Furthermore, since from now on we will use mostly C to code, we'll take advantage of qemu's ability to open a connection to gdb. First, let's install a cross-compiled gdb since OSX uses lldb which is not compatible with the ELF file format (neither is the gdb available on Homebrew's repos)

由于后面我們大部分時(shí)間都會(huì)用C來編碼鸿摇,我們將用gdb連接qemu提供的調(diào)試端口。
第一步劈猿,我們需要一個(gè)交叉編譯版本的gdb拙吉,蘋果系統(tǒng)使用的lldb無法識(shí)別ELF格式(并且潮孽,homebrew的倉庫里也查不到gdb)。

cd /tmp/src
curl -O http://ftp.rediris.es/mirror/GNU/gdb/gdb-7.8.tar.gz
tar xf gdb-7.8.tar.gz
mkdir gdb-build
cd gdb-build
export PREFIX="/usr/local/i386elfgcc"
export TARGET=i386-elf
../gdb-7.8/configure --target="TARGET" --prefix="PREFIX" --program-prefix=i386-elf-
make
make install

Check out the Makefile target make debug. This target uses builds kernel.elf, which is an object file (not binary) with all the symbols we generated on the kernel, thanks to the -g flag on gcc. Please examine it with xxd and you'll see some strings. Actually, the correct way to examine the strings in an object file is by strings kernel.elf

We can take advantage of this cool qemu feature. Type make debug and, on the gdb shell:

Set up a breakpoint in kernel.c:main(): b main
Run the OS: continue
Run two steps into the code: next then next. You will see that we are just about to set the 'X' on the screen, but it isn't there yet (check out the qemu screen)
Let's see what's in the video memory: print *video_memory. There is the 'L' from "Landed in 32-bit Protected Mode"
Hmmm, let's make sure that video_memory points to the correct address: print video_memory
next to put there our 'X'
Let's make sure: print *video_memory and look at the qemu screen. It's definitely there.
Now is a good time to read some tutorial on gdb and learn super useful things like info registers which will save us a lot of time in the future!

You may notice that, since this is a tutorial, we haven't yet discussed which kind of kernel we will write. It will probably be a monolithic one since they are easier to design and implement, and after all this is our first OS. Maybe in the future we'll add a lesson "15-b" with a microkernel design. Who knows.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末筷黔,一起剝皮案震驚了整個(gè)濱河市往史,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌佛舱,老刑警劉巖椎例,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異请祖,居然都是意外死亡订歪,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門肆捕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刷晋,“玉大人,你說我怎么就攤上這事慎陵⊙凼” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵荆姆,是天一觀的道長蒙幻。 經(jīng)常有香客問我,道長胆筒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任诈豌,我火速辦了婚禮仆救,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘矫渔。我一直安慰自己彤蔽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布庙洼。 她就那樣靜靜地躺著顿痪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪油够。 梳的紋絲不亂的頭發(fā)上蚁袭,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機(jī)與錄音石咬,去河邊找鬼揩悄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鬼悠,可吹牛的內(nèi)容都是我干的删性。 我是一名探鬼主播亏娜,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蹬挺!你這毒婦竟也來了维贺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤巴帮,失蹤者是張志新(化名)和其女友劉穎幸缕,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體晰韵,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡发乔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雪猪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片栏尚。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖只恨,靈堂內(nèi)的尸體忽然破棺而出译仗,到底是詐尸還是另有隱情,我是刑警寧澤官觅,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布纵菌,位于F島的核電站,受9級特大地震影響休涤,放射性物質(zhì)發(fā)生泄漏咱圆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一功氨、第九天 我趴在偏房一處隱蔽的房頂上張望序苏。 院中可真熱鬧,春花似錦捷凄、人聲如沸忱详。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匈睁。三九已至,卻和暖如春桶错,著一層夾襖步出監(jiān)牢的瞬間航唆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工牛曹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留佛点,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像超营,于是被迫代替她去往敵國和親鸳玩。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,079評論 3 20
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,511評論 0 23
  • 1演闭、為什么會(huì)有意志力不跟? 人類的進(jìn)化需要:為了適應(yīng)環(huán)境,需要與他人合作米碰、共享資源窝革,維持彼此關(guān)系。 因此吕座,做事不能隨心...
    An_An閱讀 207評論 0 3
  • 活了二十年 從來沒有人給過我一次意外感動(dòng)或驚喜 也沒有人在我生日的時(shí)候給過我特別的禮物 生病的時(shí)候得到的只是一些不...
    李文雅閱讀 313評論 2 1