kgdbwait -- > This will make Kernel to wait on boot time and will expect someone to connect to it and give further commands

kgdbwait -- > This will make Kernel to wait on boot time and will expect someone to connect to it and give further commands

kgdboc --> This is a KGDB I/O driver and we are supplying two arguments. ttyS0 will tell that communication will happen on Serial Port 0 and 115200 is the baudrate.

kgdboc --> This is a KGDB I/O driver and we are supplying two arguments. ttyS0 will tell that communication will happen on Serial Port 0 and 115200 is the baudrate.

Line 51:

Line 51:

== There are several possible problems that you may face ==

== There are several possible problems that you may face ==

−

1. Your Kerenel is booted and SysRq+g is not working.

+

1. Your Kernel is booted and SysRq+g is not working.

[r00t@einfochips] echo 1 > /proc/sys/kernel/sysrq

[r00t@einfochips] echo 1 > /proc/sys/kernel/sysrq

This will enable sending SysRq commands.

This will enable sending SysRq commands.

Line 57:

Line 57:

2. You may find some time that while stopping execution through SysRq key on "TEST MACHINE", it stops but then it is not able to communicate over serial cable with "DEVELOPMENT MACHINE". The reason can be, your KGDB I/O driver is not passed arguments properly and you may need to reconfigure the driver by following way,

2. You may find some time that while stopping execution through SysRq key on "TEST MACHINE", it stops but then it is not able to communicate over serial cable with "DEVELOPMENT MACHINE". The reason can be, your KGDB I/O driver is not passed arguments properly and you may need to reconfigure the driver by following way,

Revision as of 09:20, 20 November 2012

It is fascinating to think that you have control over running Linux Kernel. You can stop, can single-step, can resume and even can put break-points on running Kernel. In fact, you can debug the kernel as easily as you debug any application.

Contents

How to setup kgdb

The steps mentioned here are with reference to 2.6.26 Kernel. The main reason is KGDB code is merged into Linux tree from 2.6.26-RC5 kernel.
(As a side note, for kernel < 2.6.26-RC5, you have to get kgdb patch from [ http://kgdb.linsyssoft.com/kernel.htm] and apply them to kernel)

Hardware Requirements

Two x86 machines are required for using KGDB. One of the machines runs a kernel to be debugged called "TEST MACHINE". The other machine runs gdb "DEVELOPMENT MACHINE".
A serial line is required between the development and the test machine. And so obviously, machines need one serial port each. Basically, you will be sending "Debugging Commands" from "DEVELOPMENT MACHINE" to "TEST MACHINE".

Preparing Kernel to be Debugged

1. Download the source of kernel (for e.g., 2.6.26.2)from kernel.org

2. Recompile the Kernel on "DEVELOPMENT MACHINE".
Go to Kernel Hacking and Enable the following options:

kgdbwait -- > This will make Kernel to wait on boot time and will expect someone to connect to it and give further commands
kgdboc --> This is a KGDB I/O driver and we are supplying two arguments. ttyS0 will tell that communication will happen on Serial Port 0 and 115200 is the baudrate.

6. Now boot the Kernel with those kernel parameters.

7. On dev machine, start GDB session.

[dev@einfochips]gdb vmlinux

The argument vmlinux file is the file that is created with Debug symbols.
It will be of much larger size and more than likely to be in the directory where you gave "make" command..

8. Assuming that on "DEVELOPMENT MACHINE" you have set serial interface baudrate as 115200. Connect to the "TEST MACHINE" with target command.

(gdb)target remote /dev/ttyS0

9. This will stop your Kernel booting on "TEST MACHINE" and will give control to your "DEVELOPMENT MACHINE". Now, you can do Single-stepping or put breakpoints and etc.

10. Once your Kernel is running on "TEST MACHINE" and you want control over your running kernel from "DEVELOPMENT MACHINE",
You have to send MANUALLY on TEST machine SysRq command. So, on "TEST MACHINE" press SysRq + g

There are several possible problems that you may face

1. Your Kernel is booted and SysRq+g is not working.

[r00t@einfochips] echo 1 > /proc/sys/kernel/sysrq

This will enable sending SysRq commands.

2. You may find some time that while stopping execution through SysRq key on "TEST MACHINE", it stops but then it is not able to communicate over serial cable with "DEVELOPMENT MACHINE". The reason can be, your KGDB I/O driver is not passed arguments properly and you may need to reconfigure the driver by following way,