曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 9389|回复: 0
打印 上一主题 下一主题

QEMU 调试 Linux 内核

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2011-2-28 18:52:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
安装 QEMU 参考:http://www.groad.net/bbs/read.php?tid-3071.html

在运行 QEMU 开始要调试时出现 ”VNC server running on`127.0.0.1:5900'“ 的提示,解决办法参考:http://www.groad.net/bbs/read.php?tid-3161.html

先下载 Linux 内核,在 make menuconfig 时,将 kernel hacking 选项中,将绝大部分都勾选,然后 make bzImage 编译内核,不用 make modules_install 和 make install 这些步骤了,我们的目的只是获取 bzImage 和 vmlinux 两个文件。

编译好内核后,将这 bzImage 和 vmlinux 拷贝到随意一个工作目录。

接着从 http://wiki.qemu.org/Download 下载 linux-0.2.img.bz2 磁盘镜像文件,解压缩后得到的文件是 linux-0.2.img ,也将它拷贝到和 bzImage 及 vmlinux 一块。

运行下面用 qemu 命令进行调试:
$ qemu -S -kernel bzImage -hda linux-0.2.img -append "root=/dev/hda"
这时会弹出一个黑色的窗口:

在上面的命令中:


-S 参数表示在启动时CPU 停止(见上图的标题栏中的 stopped) 并等待 GDB 连接。

-kernel bzImage 表示指定 bzImage 作为内核镜像文件。
-hda linux-0.2.img 表示 linux-0.2.img 中的文件系统都放在 hda 中。
-append "root=/dev/hda" :-append 后接命令行参数,这里表示根文件系统设备使用 hda 。



使用 Ctrl + Alt + 2 组合键切换到 QEMU 终端,即上面的黑色窗口中,然后输入 gdbserver ,启动 gdbserver 服务:



这时启动另外一个终端窗口,输入 gdb vmlinux 命令进行调试:
beyes@debian:~/kernel$ gdb vmlinux
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb)



给内核函数下一个断点,比如是 start_kernel() ,然后运行,看是否能成功断下来:
beyes@debian:~/kernel$ gdb vmlinux
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) b start_kernel
Breakpoint 1 at 0xc13f05a6: file init/main.c, line 517.
(gdb) target remote localhost:1234               /*使用 gdbserver 进行调试命令*/
Remote debugging using localhost:1234           
[New Thread 1]
0x0000fff0 in ?? ()
(gdb) c
Continuing.


Breakpoint 1, start_kernel () at init/main.c:517
517        smp_setup_processor_id();
(gdb)
已经可以调试了,但这只是整个调试工作中的第一步。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-19 04:38 , Processed in 0.079155 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表