1. 程式人生 > 其它 >ubuntu14使用qemu除錯linux核心【轉】

ubuntu14使用qemu除錯linux核心【轉】

轉自:https://www.cnblogs.com/pingandezhufu/p/4392297.html

ubuntu14使用qemu除錯linux核心

    # 下載核心原始碼編譯核心
    cd ~/LinuxKernel/
    wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.xz
    xz -d linux-3.18.6.tar.xz
    tar -xvf linux-3.18.6.tar
    cd linux-3.18.6
    make i386_defconfig
    make # 一般要編譯很長時間,少則20分鐘多則數小時
     
    # 製作根檔案系統
    cd ~/LinuxKernel/
    mkdir rootfs
    git clone https://github.com/mengning/menu.git   # 如果被牆,可以使用附件menu.zip 
    cd menu
    gcc -o init linktable.c menu.c test.c -m32 -static –lpthread
    cd ../rootfs
    cp ../menu/init ./
    find . | cpio -o -Hnewc |gzip -9 > ../rootfs.img
     
    # 啟動MenuOS系統
    cd ~/LinuxKernel/
    qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img

在執行qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img時報錯,沒有qemu這個命令。

正確做法:qemu-system-i386 -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img

原因分析:

我以為沒有安裝qemu,於是開始安裝,按照http://jingyan.baidu.com/album/b907e627cfffc946e7891cd5.html的網址

sudo apt-get install qemu-kvm libvirt-bin virt-manager bridge-utils

lsmod | grep kvm

virsh-cqemu:///systemlist

但是後來發現這個網址:http://blog.csdn.net/xsckernel/article/details/8159548#comments

得知kvm是系統自帶的,於是解除安裝了原來安裝的

sudo apt-get remove qemu-kvm

sudo apt-get remove libvirt-bin

sudo apt-get remove virt-manager

sudo apt-get remove bridge-utils

但是不知道為什麼 還存在kvm例如:lsmod | grep kvm

通過網址:

http://zhangjun2915.blog.163.com/blog/static/3808623620103585320514/

得知/usr/bin/qemu-system-x86_64就是qemu-kvm

於是我就在/esr/bin/目錄下看到:

從而確定了qemu-system-x86_64是模擬64位系統,qemu-system-i386是模擬32位系統。

於是最後一行命令改為:

qemu-system-i386 -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img

可以執行效果是:

  • 重新配置編譯Linux使之攜帶除錯資訊

在原來配置的基礎上,在shell終端輸入:make menuconfig

會報錯,錯誤資訊如下:

In file included from scripts/kconfig/lxdialog/checklist.c:24:

scripts/kconfig/lxdialog/dialog.h:31:20: error: curses.h: 沒有那個檔案或目錄

In file included from scripts/kconfig/lxdialog/checklist.c:24:

參考網址:http://blog.chinaunix.net/uid-25896350-id-367612.html

解決辦法:sudo apt-get install libncurses5-dev

然後重新輸入:make menuconfig,效果如下:按空格選擇kernelhacking—>

按空格選擇 Compile -time checks and Complier options -->

按空格選擇[*]compilethekernelwithdebuginfo,然後儲存退出。

make重新編譯(時間較長)

使用gdb跟蹤除錯核心

   cd LinuxKernel
qemu-system-i386 -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -s -S # 關於-s和-S選項的說明: # -S freeze CPU at startup (use ’c’ to start execution) # -s shorthand for -gdb tcp::1234 若不想使用1234埠,則可以使用-gdb tcp:xxxx來取代-s選項

另開一個shell視窗

    gdb
    (gdb)file linux-3.18.6/vmlinux # 在gdb介面中targe remote之前載入符號表
    (gdb)target remote:1234 # 建立gdb和gdbserver之間的連線,按c 讓qemu上的Linux繼續執行
    (gdb)break start_kernel # 斷點的設定可以在target remote之前,也可以在之後

實驗效果:

【作者】張昺華 【出處】http://www.cnblogs.com/sky-heaven/ 【部落格園】 http://www.cnblogs.com/sky-heaven/ 【知乎】 http://www.zhihu.com/people/zhang-bing-hua 【我的作品---旋轉倒立擺】 http://v.youku.com/v_show/id_XODM5NDAzNjQw.html?spm=a2hzp.8253869.0.0&from=y1.7-2 【我的作品---自平衡自動循跡車】 http://v.youku.com/v_show/id_XODM5MzYyNTIw.html?spm=a2hzp.8253869.0.0&from=y1.7-2 【大餅教你學系列】https://edu.csdn.net/course/detail/10393 【新浪微博】 張昺華--sky 【twitter】 @sky2030_ 【微信公眾號】 張昺華 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利.