免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: eaglec83
打印 上一主题 下一主题

[内核模块] 请问如何用虚拟机调试内核? [复制链接]

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-15 06:20:00数据库技术版块每日发帖之星
日期:2016-06-16 06:20:00数据库技术版块每日发帖之星
日期:2016-06-18 06:20:00程序设计版块每日发帖之星
日期:2016-06-27 06:20:00程序设计版块每日发帖之星
日期:2016-07-09 06:20:00IT运维版块每日发帖之星
日期:2016-07-15 06:20:00IT运维版块每日发帖之星
日期:2016-07-27 06:20:00程序设计版块每日发帖之星
日期:2016-08-18 06:20:00
11 [报告]
发表于 2014-10-10 19:42 |只看该作者
用vmware workstation + gdb就可以调试,就像调试用户态进程一样

论坛徽章:
0
12 [报告]
发表于 2014-10-10 19:59 |只看该作者
我想问的是怎么编出kernel-debuginfo?
现在我用的3.14的内核,所有的网上所说的debug 宏默认都是打开的,重新编译后
用systemtap还是不可用

我想知道的是网上的那些kernel debuginfo和我自己打开debug宏编译内核之后重启系统有什么不同?
回复 11# humjb_1983


   

论坛徽章:
0
13 [报告]
发表于 2014-10-11 14:55 |只看该作者
用qemu加gdb方式
qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -m 1024 -initrd /boot/initramfs-3.14.19.img -s
如果是这样启动, 终端界面在打印fixing recursive fault but reboot is needed就停止不动了
通过gdb vmlinux连接上去,看到的栈是
(gdb) bt
#0  default_idle () at arch/x86/kernel/process.c:314
#1  0xffffffff8100d1ee in arch_cpu_idle () at arch/x86/kernel/process.c:302
#2  0xffffffff810a81ca in cpu_idle_loop () at kernel/cpu/idle.c:98
#3  0xffffffff810a8393 in cpu_startup_entry (state=<value optimized out>) at kernel/cpu/idle.c:143
#4  0xffffffff8157c2c7 in rest_init () at init/main.c:397
#5  0xffffffff81b40204 in start_kernel () at init/main.c:656
#6  0xffffffff81b3f5ad in x86_64_start_reservations (real_mode_data=<value optimized out>) at arch/x86/kernel/head64.c:193
#7  0xffffffff81b3f6f0 in x86_64_start_kernel (real_mode_data=0x13de0 <Address 0x13de0 out of bounds>) at arch/x86/kernel/head64.c:182
#8  0x0000000000000000 in ?? ()

如果直接敲n,就又挂在那儿了

如果启动的时候加-S参数,设置断点start_kernel, 进入断点后,敲c或n,每次都只显示Cannot execute this command while the selected thread is running
但是这时明明只有一个线程在运行

论坛徽章:
0
14 [报告]
发表于 2014-10-11 16:39 |只看该作者
用网上说的两台虚拟机方式去调试内核,还是没成功
目标机上的内核编译就不说了, grub.conf里配置如下:
root (hd0,0)
        kernel /vmlinuz-3.12.30 ro root=/dev/mapper/vg_vm1-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg_vm1/lv_root SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_vm1/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet kgdboe=@192.168.122.164/,@192.168.122.43/
        initrd /initramfs-3.12.30.img

将编译好的vmlinux拷贝到开发机上一份, 然后
gdb vmlinux
target remote 192.168.122.164:6443
打印no route to host
在目标机上使用netstat -u, 看不到任何内容

如果用串口方式
title CentOS (3.12.30)
        root (hd0,0)
        kernel /vmlinuz-3.12.30 ro root=/dev/mapper/vg_vm1-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg_vm1/lv_root SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_vm1/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet kgdb=ttyS0,115200 kgdboc=ttyS0,115200
在开发机上
gdb vmlinux
set remotebaud 115200
target remote /dev/ttyS0
同样连接不上, 我在编译内核的时候选择的方式是串口方式

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
15 [报告]
发表于 2014-10-11 16:45 |只看该作者
eaglec83 发表于 2014-10-10 19:59
我想问的是怎么编出kernel-debuginfo?
现在我用的3.14的内核,所有的网上所说的debug 宏默认都是打开的, ...

如果使用make方式构建,make之后就有vmlinux文件,其中就包含了符号信息。
如果使用srpm来构建,rpmbuild之后就会有单独的debuginfo包。
内核的debug配置选项指的是内核中的一些调试开关,就是内核代码中的一些宏,用于控制内核代码的执行流程,比如在需要的地方打印一些需要的信息而已。
跟debuginfo包不是一个概念,debuginfo包是将vmlinux中的符号信息抽取出来,形成一个单独的rpm包,这样可以减小原有内核的尺寸,并且更安全,
类似systemtap这样的工具在打点时,须要内核的符号信息(没有的话,就没法在源代码级别进行打点了),所以须要有debuginfo包。

论坛徽章:
0
16 [报告]
发表于 2014-10-11 19:31 |只看该作者
今天刚换了一个内核版本, 就是编译的时候把KGDB_KDB打开了,其他参数和之前是一样的
切换版本后,直接跑[root@vm2 linux-3.12.30]# stap -ve 'probe kernel.function("do_fork") { print("hello world\n") exit() }'
Pass 1: parsed user script and 96 library script(s) using 198508virt/26456res/3072shr/24096data kb, in 170usr/10sys/187real ms.
Pass 2: analyzed script: 1 probe(s), 1 function(s), 0 embed(s), 0 global(s) using 344088virt/133168res/82268shr/51368data kb, in 490usr/40sys/531real ms.
Pass 3: translated to C into "/tmp/stapPb0mHZ/stap_a707a3ffeaedb1b291f86f0cee177397_971_src.c" using 344088virt/133300res/82400shr/51368data kb, in 0usr/0sys/4real ms.
Pass 4: compiled C into "stap_a707a3ffeaedb1b291f86f0cee177397_971.ko" in 6130usr/890sys/7125real ms.
Pass 5: starting run.
我应该没有编译debuginfo, 为什么也没报错呢?

另外,我查了一下rpmbuild, -ba参数是同时生成源码和2进制包, -bb参数是生成2进制包
参数--with <option> --without <option>表示要带哪些信息
那么我是不是只要在打开debug宏的kernel源码目录下
rpmbuild -bb --with debuginfo --target='uname -m' kernel-spec就可以了?
[root@vm2 linux-3.12.30]# rpmbuild -bb --with debuginfo --target='uname -m' kernel-spec
Building target platforms: uname -m
Building for target uname -m
error: File /home/ychen/bin/linux-3.12.30/kernel-spec does not appear to be a specfile.
但是总是报这个错误

回复 16# humjb_1983


   

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
17 [报告]
发表于 2014-10-11 20:31 |只看该作者
本帖最后由 Tinnal 于 2014-10-11 20:35 编辑

关于自己编译的内核没有debuginfo包的问题,systemtap的官方FAQ已经有说明:
https://sourceware.org/systemtap/wiki/SystemTapFAQ
2) What config options need to be enabled to be able to use systemtap on custom built kernels?

Look at SystemTapWithSelfBuiltKernel for the specific config options to be enabled for systemtap.

3) I have a kernel built with options listed in (2), but still can't get systemtap to work. What am I missing?

See Q2. Also, if you've used 'make install' to install your custom built kernel, it may not have copied the uncompressed 'vmlinux' to /boot/vmlinux-'uname-r'. You can place 'vmlinux' in any of these locations,

  /boot/vmlinux-`uname -r`
  /usr/lib/debug/lib/modules/`uname -r`/vmlinux
  /lib/modules/`uname -r`/vmlinux


https://sourceware.org/systemtap ... WithSelfBuiltKernel
Using systemtap with self-built kernels

If you are running a kernel that you built and installed "by hand", rather than by installing a package from a distribution, you can follow these steps to use systemtap. The following blurb may also be found at the bottom of the README file in the sources. See also cross compiling tips.

Build the kernel using your normal procedures. Enable CONFIG_DEBUG_INFO, CONFIG_KPROBES, CONFIG_RELAY, CONFIG_DEBUG_FS, CONFIG_MODULES, CONFIG_MODULE_UNLOAD, CONFIG_UPROBES and the minimal number of auxiliary modules you need (since CONFIG_DEBUG_INFO can multiply disk space usage).

% make modules_install install

Boot into the kernel.

If you want to build instrumentation for that installed kernel without rebooting, you can use the following (replacing RELEASE with version number of the installed kernel):

% stap -r RELEASE [...]

If you do not install the kernel, simply run

% stap -r /path/to/kernel/build/tree [...]

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
18 [报告]
发表于 2014-10-13 11:20 |只看该作者
回复 19# eaglec83

systemtap对内核兼容性做得不好的BUG。具体的详情请看:

https://www.sourceware.org/ml/systemtap/2013-q2/msg00166.html
https://www.sourceware.org/ml/systemtap/2014-q2/msg00023.html

你要不更改一下你的内核选项,规避这个BUG,要不就按上面的patch修复一下systemtap。


   

论坛徽章:
0
19 [报告]
发表于 2014-10-13 14:17 |只看该作者
systemtap在3.14上可以用了,多谢
我换了最新的systemtap 2.6版本,这个版本已经修正了runtime check的问题
回复 20# Tinnal


   

论坛徽章:
0
20 [报告]
发表于 2014-10-13 14:46 |只看该作者
刚刚又试了一把kgdboc的方式, 还是没能搞定
minicom: WARNING: configuration file not found, using defaults
Device /dev/modem access failed: No such file or directory
这个应该是没有这个设备吧? 于是我在virtual machine manager的界面上又加了一个serial的设备,类型选择为pseudo tty(pty)
创建重启后, lspci, 并没有看到该串口设备

minicom -s
选择serial port setting, 将/dev/modem改为/dev/ttyS0, 保存后退出再启动minicom
Welcome to minicom 2.3

OPTIONS: I18n                                                               
Compiled on Aug 19 2010, 05:50:19.                                          
Port /dev/ttyS0                                                              
                                                                             
               Press CTRL-A Z for help on special keys
就停在这个界面了,任何一个键都不起作用, 这个是不是还是串口设备没生效的问题?
请问:
1. 怎么在虚拟机上使能串口设备?
2. 为什么我用kgdboc和kgdboe方式都无法使开发机和目标机连通?
回复 15# eaglec83


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP