1. 程式人生 > >qemu&buildroot&gdb 建立x86除錯環境

qemu&buildroot&gdb 建立x86除錯環境

##qemu & buildroot & gdb 建立x86除錯環境

本文只針對模擬x86環境

本機環境:

作業系統:Debian GNU/Linux 8.6 (jessie)
CPU 架構: Intel(R) Core(TM) i5-4570 CPU @ 3.20GH

qemu

簡介

QEMU是一套由Fabrice Bellard所編寫的模擬處理器的自由軟體。其中的System mode模擬模式,亦即是系統模式。QEMU能模擬整個電腦系統,包括中央處理器及其他周邊裝置。它使得為系統原始碼進行測試及除錯工作變得容易。其亦能用來在一部主機上虛擬數部不同虛擬電腦。

安裝qemu

1 直接通過apt-get安裝

sudo apt-get install qemu

2 qemu原始碼安裝
step1 qemu原始碼下載:http://wiki.qemu.org/Download(建議不要下載最新版本,出現問題較難以解決)

step2 編譯&安裝
經典五步走:

tar -jx -f qemu-2.7.0.tar.bz2
cd qemu-2.7.0
./configure
make
sudo make install

buildroot

簡介

Buildroot is a simple, efficient and easy-to-use tool to generate embedded Linux systems through cross-compilation.

安裝buildroot

step2 配置buildroot,是最複雜的一步.

a)進入配置介面,如圖:

make menuconfig

這裡寫圖片描述
b) 配置目標結構:x86_64
在Target options–> Target Architecture目錄中, 選擇”x86_64”

c) 配置庫檔案:靜態庫和動態庫
在Build options–>Libraries目錄中,選擇”both static and shared”
(注:只選擇shared only,不是buildroot編譯的bzImage是不識別的.)
這裡寫圖片描述
d) 配置工具鏈:主要有C庫檔案,kernel標頭檔案和gcc版本.

  • 在Toolchain–>C library目錄中,配置C庫檔案,選擇”glibc”

  • 在Toolchain->kernel headers目錄中,配置kernel標頭檔案.我-要使用linux-4.4的核心版本,所以選擇”Linux 4.4.x kernel headers”

  • 在Toolchain–>Gcc Compiler Version目錄中,配置gcc版本.根據要編譯的核心對應的gcc的版本.這裡我選擇的是”gcc 4.9.x”

這裡寫圖片描述

e) 配置檔案系統:ext3或是ext4.buildroot預設是ext2
在Filesystem images->ext2/3/4 root filesystem 選擇使能這個選項,之後會出現ext2/3/4 variant目錄,選擇”ext4”.
這裡寫圖片描述
f)配置編譯的核心:如linux-4.4

  • 在Kernel目錄中,先選擇Linux Kernel,這樣可以自定義核心版本.之後會出現很多選項:
    這裡寫圖片描述
  • 在Kernel Version選項中選擇:
    若選擇”Custom Git repository”,那麼在URL of custom repository選項中寫入linux git原始碼本地的倉庫:”~/linux-stable”.(注:在linux source git repostiory,可以獲取到linux所有穩定版本的原始碼git倉庫).並且”Custom repository version”中寫入想要的版本號.如”v4.4”(注,在下載好的本地的git倉庫中,通過git tag檢視想要的版本號和名稱).OR若選擇”Custom version”,那麼”Kernel version”寫入下載好的核心(注,可在kernel.org中獲取)
  • 在”Kernel configuration”中, 選擇”Using a custom (def)config file”,這樣可以自定義核心的配置檔案.並需要在”Configuration file path”選項中寫入配置好linux核心原始碼的config檔案,如”~/config”

這裡寫圖片描述

g)儲存退出
h) 編譯

make -j4 (與邏輯cpu有關)

i) 編譯完成後多出現output目錄(現在在buildroot目錄中),其中output/images/root.ext2是buildroot生成的根檔案系統,這時這個根檔案映象,可以被其他bzImage掛載.output/images/bzImage是編譯好的linux映象. output/images/target是根檔案系統中的檔案.

j)rootfs.ext2只是最基本的根檔案系統,若需要新增更多的功能,則得另加選項.

qemu 建立x86模擬環境

利用buildroot中的bzImage linux映象

buildroot中,已經編譯特定版本的linux的bzImage映象.因此通過:(已經在buildroot根目錄下)

qemu-system-x86_64 -hda output/images/rootfs.ext2  -kernel output/images/bzImage  -m 1024M -append "root=/dev/sda"

可以建立x86模擬環境.

利用其他bzImage linux映象

上文已經提到在同時選擇靜態庫和共享庫後,生成的根檔案系統映象可以重複使用.假如在其他linux原始碼下編譯了bzImage映象.建立x86模擬環境:

qemu-system-x86_64 -hda ~/Workspace/kernel/linux-stable  -kernel output/images/bzImage  -m 1024M -append "root=/dev/sda"

這樣的好處是不需要每次編譯檔案系統,大大減少編譯時間。

qemu剛啟動時,qemu中只有root使用者,且無密碼.
這裡寫圖片描述

gdb & qemu

通過:

qemu-system-x86_64 -hda ~/Workspace/kernel/linux-stable  -kernel output/images/bzImage  -m 1024M -append "root=/dev/sda" -S 

啟動qemu,增加-S引數,這樣可以使系統控制權交給gdb。然後啟動gdb : gdb vmlinux 最後在gdb命令列中輸入:target remote localhost:1234 。這樣就連線到qemu上。gdb就可以除錯能核心了。

buildroot 與 busybox

Busybox: The Swiss Army Knife of Embedded Linux,and it combines tiny versions of many common UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc. The utilities in BusyBox generally have fewer options than their full-featured GNU cousins; however, the options that are included provide the expected functionality and behave very much like their GNU counterparts. BusyBox provides a fairly complete environment for any small or embedded system.

buildroot 相對於busybox,省去了手動新增配置檔案等過程,buildroot中不僅整合busybox,還有其他很多使用的工具.buildroot還是很容易的.