关闭

VirtualBox + kgdb analysis of Linux kernel (v3.4.0-rc3)

292人阅读 评论(0) 收藏 举报
分类:


https://tthtlc.wordpress.com/2012/06/16/virtualbox-kgdb-analysis-of-linux-kernel-v3-4-0-rc3/


Posted June 16, 2012 by Peter Teoh in kernel_general.


After compiling the kernel inside VirtualBox, copy out the vmlinux to the host, where “gdb” command is to be issued.

Add “kgdboc=ttyS0,115200 kgdbwait” at the end of the kernel line inside the /boot/grub/grub.cfg. Eg:

linux /boot/xxxxx kgdboc=ttyS0,115200 kgdbwait

Then issue at host level:

socat -d -d /tmp/serial1204 pty

after that “/dev/pts/6” is printed:

2012/06/17 08:04:20 socat[6051.18446744073196852992] N opening connection to AF=1 "/tmp/serial1204"
2012/06/17 08:04:20 socat[6051.18446744073196852992] N successfully connected from local address AF=1 "\x02\xE0"
2012/06/17 08:04:20 socat[6051.18446744073196852992] N successfully connected via s\xD1]\x7E
2012/06/17 08:04:20 socat[6051.18446744073196852992] N PTY is /dev/pts/6
2012/06/17 08:04:20 socat[6051.18446744073196852992] N starting data transfer loop with FDs [3,3] and [4,4]

Use that information below (at the host level):

gdb ./vmlinux (where vmlinux is the compiled vmlinux image from inside the VirtualBox).

(gdb) set remotebaud 115200
(gdb) target remote /dev/pts/6
Remote debugging using /dev/pts/6
kgdb_breakpoint () at kernel/debug/debug_core.c:987
987 kernel/debug/debug_core.c: No such file or directory.
in kernel/debug/debug_core.c

From above, “No such file” found is because we are not located at the root of the kernel source code. Restarting “gdb ./vmlinux” at the root of the kernel source code, and using the “list” command in gdb, we can list the source code.

(gdb) list
982 void kgdb_breakpoint(void)
983 {
984 atomic_inc(&kgdb_setting_breakpoint);
985 wmb(); /* Sync point before breakpoint */
986 arch_kgdb_breakpoint();
987 wmb(); /* Sync point after breakpoint */
988 atomic_dec(&kgdb_setting_breakpoint);
989 }
990 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
991

From above we can see that the very first breakpoint when kgdb stopped is at 986. So all codes that execute before this point cannot be traced by kgdb. As for how this kgdb_breakpoint() is reached, can be seen from the stacktrace:

(gdb) bt
#0 kgdb_breakpoint () at kernel/debug/debug_core.c:987
#1 0xffffffff810cf486 in kgdb_initial_breakpoint (
new_dbg_io_ops=0xffffffff81c7f9e0) at kernel/debug/debug_core.c:885
#2 kgdb_register_io_module (new_dbg_io_ops=0xffffffff81c7f9e0)
at kernel/debug/debug_core.c:927
#3 0xffffffff813e4d94 in configure_kgdboc ()
at drivers/tty/serial/kgdboc.c:197
#4 0xffffffff81d2709e in init_kgdboc () at drivers/tty/serial/kgdboc.c:219
#5 0xffffffff8100203f in do_one_initcall (fn=0xffffffff81d2708a )
at init/main.c:678
#6 0xffffffff81cf2d53 in do_initcall_level (unused=)
at init/main.c:753
#7 do_initcalls (unused=) at init/main.c:761
#8 do_basic_setup (unused=) at init/main.c:780
#9 kernel_init (unused=) at init/main.c:863
#10 0xffffffff816608e4 in ?? () at arch/x86/kernel/entry_64.S:1204
#11 0x0000000000000000 in ?? ()

Essentially the caller is kernel_init()—> do_basic_setup()—> do_initcalls()—>do_initcall_level()—>do_one_initcall()—>init_kgdboc()—>configure_kgdboc()—>kgdb_register_io_module()—>kgdb_initial_breakpoint()—>kgdb_breakpoint().

By setting breakpoints at “printk”, we can see all the stacktrace when the kernel is printing its output to the dmesg:

(gdb) break printk
Breakpoint 1 at 0xffffffff8164c7c6: file kernel/printk.c, line 753.
(gdb) cont
Continuing.
[New Thread 19]
[Switching to Thread 19]

From above, we can see there is a switching of execution from one thread to another.


Breakpoint 1, printk (
fmt=0xffffffff819ed780 "<6>Refined TSC clocksource calibration: %lu.%03lu MHz.\n") at kernel/printk.c:753
753 }
(gdb) bt
#0 printk (
fmt=0xffffffff819ed780 "<6>Refined TSC clocksource calibration: %lu.%03lu MHz.\n") at kernel/printk.c:753
#1 0xffffffff8101a4a1 in tsc_refine_calibration_work (
work=<value optimized out>) at arch/x86/kernel/tsc.c:915
#2 0xffffffff8106d26a in process_one_work (worker=<value optimized out>,
work=0xffffffff81c18e60) at kernel/workqueue.c:1866
#3 0xffffffff8106e2d5 in worker_thread (__worker=0xffff88003dbb4b80)
at kernel/workqueue.c:1977
#4 0xffffffff81072d63 in kthread (_create=0xffff88003da57d28)
at kernel/kthread.c:121
#5 0xffffffff816608e4 in ?? () at arch/x86/kernel/entry_64.S:1204
#6 0x0000000000000000 in ?? ()
(gdb) cont
Continuing.
[Switching to Thread 1]

Breakpoint 1, printk (fmt=0xffffffff81a37d88 "<6>brd: module loaded\n")
at kernel/printk.c:753
753 }
(gdb) bt
#0 printk (fmt=0xffffffff81a37d88 "<6>brd: module loaded\n")
at kernel/printk.c:753
#1 0xffffffff81d2818b in brd_init () at drivers/block/brd.c:624
#2 0xffffffff8100203f in do_one_initcall (fn=0xffffffff81d2803c <brd_init>)
at init/main.c:678
#3 0xffffffff81cf2d53 in do_initcall_level (unused=<value optimized out>)
at init/main.c:753
#4 do_initcalls (unused=<value optimized out>) at init/main.c:761
#5 do_basic_setup (unused=<value optimized out>) at init/main.c:780
#6 kernel_init (unused=<value optimized out>) at init/main.c:863
#7 0xffffffff816608e4 in ?? () at arch/x86/kernel/entry_64.S:1204
#8 0x0000000000000000 in ?? ()
(gdb) cont
Continuing.
[Switching to Thread 19]

Now boot up all the way by deleting all the breakpoints:

(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) cont
Continuing.


0
0
查看评论

通过KGDB进行双机内核调试

原理原理结构图原理过程Kgdb是双机在线调试,一端是Host端(linux),运行GDB,另一端是Target端,运行带Kgdb的linux内核。 两边通过串口(KGDBoc)或网络口(KGDBoE)相连接,kgdb实现了远程调试的功能,主要部件有:stubstub可卸任是一个运行在target端...
  • java211
  • java211
  • 2016-09-05 20:24
  • 872

使用virtualbox + kgdb调试kernel

主机环境: T400/Ubuntu9.10(2.6.31-21) 1、安装VirtualBox,        System->Administation->Synaptic Package Manager  ...
  • nancygreen
  • nancygreen
  • 2013-10-04 21:15
  • 2037

使用VirtualBox和KGDB调试内核

使用VirtualBox和KGDB调试内核
  • ztguang
  • ztguang
  • 2016-08-29 11:27
  • 379

用KGdb和VMware调试Linux内核,System Call

Linux的内核和System Call不好调试,参考这里: http://stackoverflow.com/questions/5999205/cannot-step-into-system-call-source-code 简单来说,如果想在本机调试system call,那么当你进入...
  • hengyunabc
  • hengyunabc
  • 2014-05-03 20:59
  • 3400

VirtualBox+linux虚拟机+KGDB

前言: Do you want to become a kernel hacker? Just modify and debug it。 Don't only read many books about linux kernel , or you will be lost in b...
  • xumin330774233
  • xumin330774233
  • 2015-02-04 16:14
  • 1172

linux内核调试gdb + KGDB

Linux内核开发者使用最普遍的调试方法是printk方法,但是这种方法每次添加一些调试信息后还要重新编译烧写,效率不高,Kgdb调试方法是一种源码级的Linux内核调试器。使用Kgdb调试内核时,需要结合gdb一起使用,使用他们可以对内核进行单步调试,设置断点,观察变量、寄存器的值等。使用kgdb...
  • luckywang1103
  • luckywang1103
  • 2017-03-24 09:01
  • 552

linux内核kgdb调试

从2.6.26开始,Linux 主干内核开始内置了代码级调试器 kgdb。通过 kgdb,可以在内核代码中设置断点,单步调试和观察变量。为了使用 kgdb,需要有两个系统。一个作为上位机,一个作为下位机(目标机)。两台机器通过串口线连接。需要调试的内核运行在下位机上。串口线用于kgdb连接远程目标板...
  • u013470224
  • u013470224
  • 2016-05-04 13:55
  • 1903

ARM-使用KGDB调试内核

今天看了《嵌入式linux应用开发 完全手册》中第18章,关于使用KGDB调试内核的问题,中间碰到了一些问题,最后查找资料,一一解决了把过程记录于此,以便以后参考>>>>>>>>>> 一、linux主机需要安装的软件 (1) 安装...
  • u012296253
  • u012296253
  • 2013-10-22 15:27
  • 1221

ubuntu kgdb kernel 调试环境搭建

<!-- @page {margin:2cm} p {margin-bottom:0.21cm} a:link {color:#0000ff} --> 调试环境(末尾还有ubuntu下vmware的环境搭建方法): 主机:WindowsXP Profe...
  • dndxhej
  • dndxhej
  • 2012-01-02 19:38
  • 5941

linux内核调试:kgdb,配置kgdb调试环境

一、kgdb介绍         linux下的模块开发,不可避免要用到kgdb来调试。         kgdb的调试环境需要一台开发机,一台目标机,其中代码运行在目标机上,开发机通过串口来调试目标机上的模块代码。...
  • kanguolaikanguolaik
  • kanguolaikanguolaik
  • 2014-09-17 17:14
  • 2122
    个人资料
    • 访问:1700877次
    • 积分:23041
    • 等级:
    • 排名:第363名
    • 原创:1829篇
    • 转载:1045篇
    • 译文:0篇
    • 评论:68条
    相册/老博客
    最新评论