使用 Windows 主机在 VMWare 中调试 Linux 内核 [英] Debugging Linux Kernel in VMWare with Windows host

查看:36
本文介绍了使用 Windows 主机在 VMWare 中调试 Linux 内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个内核模块,所以我对此有点陌生......

我的模块偶尔会产生恐慌,我无法使用 printk 深入了解它.据我所知,我的调试选项是:

1.) 找到生成的 OOPS 消息并使用 ksymoops

2.) 尝试使用 kgdb 进行远程调试

我正在 Windows 主机上工作并在 VMWare 中运行 Ubuntu,所以这让事情变得有点复杂.我想先尝试 OOPS 消息,但我不知道如何捕获它.发生这种情况时是否需要运行串行控制台?如果是这样,我该如何使用 Windows 主机做到这一点?我需要两个虚拟机吗?

就像我说的,我对此(以及一般的 Linux)有点陌生,所以我真的可以使用一些指导.谢谢!

解决方案

不久前我成功地使用了alinrus"所指的技术.他提到的部分详细解释如下:http://stackframe.blogspot.com/2007/04/调试-linux-kernels-with.html

我实际上是直接从 Windows 主机使用的.所以在设置好VM后(主要是开启远程调试和下载内核(vmlinux文件,不是gdb无法解释的vmlinuz文件)),你需要做以下事情:>

  1. 在您的 Windows 机器上安装一个最新的 gdb(我在 Cygwin 中使用了那个).
  2. 使用 vmlinux 文件启动 gdb,然后执行target remote localhost:8832"以连接 VM(当它正在运行时).

这就是调试静态链接到内核的代码所需要的.您可以尝试静态链接您的模块,以上就足够了.为动态链接的模块设置调试需要一个额外的步骤来通知 gdb 也使用你的模块文件,以及如何解释文件的部分.

3a.在加载模块之后(并且在它崩溃之前 :) 之前,在 .ko 文件上运行以下脚本).

3b.将生成的add-symbol-file mymodule.ko 0xe8884000 ..."行粘贴到 gdb 中.gdb 将加载您的模块,只要它可以在您指定的当前目录或路径中找到它即可.

脚本来自 http://anomit.com/2009/11/04/kernel-module-debugging-a-simple-technique/

<前>#!/bin/sh## gdbline 模块图像## 输出一个适合粘贴到 gdb 中的 add-symbol-file 行进行检查# 一个加载的模块.#cd/sys/module/$1/sectionsPROG=${1}.koecho -n add-symbol-file ${PROG} `/bin/cat .text`#echo -n add-symbol-file $2 `/bin/cat .text` #取第二个参数为程序/目标文件的gdb名称对于 .[a-z]* * 中的部分;做if [ $section != ".text" ];然后回声\"echo -n " -s" $section `/bin/cat $section`菲完毕回声

您可能还想做更多的事情.要进行实际的源代码级调试,您需要收集所有内核和模块源代码,以便 gdb 可以找到它.一旦你走到这一步,你可以使用一些技巧来编译你的模块而无需优化.

您可能还想查看 Workstation 7.0 关于重放调试的技术说明,其中包含有关调试内核模块的信息.VMware 知道他们的东西.http://www.vmware.com/pdf/ws7_replay_linux_technote.pdf

I'm working on my first ever kernel module so I'm a bit new at this...

My module is occasionally producing a panic and I cannot get to the bottom of it using printk. As far as I can tell my options for debugging are:

1.) Find the generated OOPS message and use ksymoops

or

2.) Try at remote debug using kgdb

I'm working on a windows host and running Ubuntu in VMWare so that complicates things a bit. I'd like to try the OOPS message first but I don't know how to capture it. Do I need to be running a serial console when it happens? If so, how can I do that with Windows host? Do I need two VM's?

Like I said, I'm a bit new to this (and Linux in general) so I could really use some guidance. Thanks!

解决方案

A while ago I successfully used the technique "alinrus" refers to. The part he mentioned is explained in detail at: http://stackframe.blogspot.com/2007/04/debugging-linux-kernels-with.html

I actually used it directly from a Windows host. So after setting up the VM (mainly enabling remote debugging and downloading the kernel (the vmlinux file, not the vmlinuz file which cannot be interpreted by gdb)), you would need to do the following:

  1. Install a recent gdb on your Windows machine (I used the one in Cygwin).
  2. Start gdb with the vmlinux file, and then do "target remote localhost:8832" to connect the VM (when it's running).

That is what you need to debug code which is statically linked into the kernel. You could try statically linking your module and the above would be sufficient. Setting up debugging for a dynamically-linked module requires an additional step to inform to gdb to use your module file as well, and how to interpret the file's sections.

3a. Run the script below on your .ko file, after loading the module (and before crashing it :) ).

3b. Paste the resulting "add-symbol-file mymodule.ko 0xe8884000 ..." lines into gdb. gdb will then load your module, as long as it can find it in the current directory or path you specify.

Script is from http://anomit.com/2009/11/04/kernel-module-debugging-a-simple-technique/

#!/bin/sh
#
# gdbline module image
#
# Outputs an add-symbol-file line suitable for pasting into gdb to examine
# a loaded module.
#
cd /sys/module/$1/sections
PROG=${1}.ko
echo -n add-symbol-file ${PROG} `/bin/cat .text`
#echo -n add-symbol-file $2 `/bin/cat .text`  #Take second argument to be gdb name of program/object file

for section in .[a-z]* *; do
    if [ $section != ".text" ]; then
        echo  " \"
        echo -n "       -s" $section `/bin/cat $section`
    fi
done
echo

There are more things you may want to do. To do actual source-level debugging you'll want to suck down all the kernel and module source code so that gdb can find it. And there are some tricks you can use to compile your module without optimization once you get this far.

You may also want to look at Workstation 7.0's tech note on replay debugging, which contains information on debugging kernel modules. VMware knows their stuff. http://www.vmware.com/pdf/ws7_replay_linux_technote.pdf

这篇关于使用 Windows 主机在 VMWare 中调试 Linux 内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆