Hi
Here are the steps for configuring Kernel and consequently doing Kernel and Module debugging...
Everything below is related to or with reference to 2.6.26 Kernel.... The main reason is KGDB code is merged into Linux tree from 2.6.26-RC5 kernel
1. Obviously start with downloading the source of 2.6.26.2 kernel, the latest stable version.... from kernel.org
2. Recompile the Kernel -> Go to Kernel Hacking -> Enable two options one is related to SysRq Key, and the other one is KGDB ... Give "Y" to both of them...
3. Build kernel make -j 12 && make modules && make modules_install
4. Transfer the vmlinux and system.map and initrd.img files on Test machine.
5. Now, edit the GRUB entry for that kernel on Test machine... Add options or give Kernel Parameters like kgddbwait and kgdboc=ttyS0,115200
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 aarguments.... ttyS0 will tell that communication will happen on Serial Port 0 and the baudrate will be 115200
6. Now boot the Kernel with those kernel parameters...
7. On dev machine, start GDB session ... [dev@root]gdb vmlinux
The argument vmlinux file is the file that is created with Debug symbols... It will be of much larger size... and probably in the present directory where you gave "make" command..
8. We assume that on Dev machine you have set serial interface baudrate as 115200.. Then 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 Dev machine..Now, you can do next and step and put breakpoints and etc...
BUT The real problem starts now, that I faced and solved
Once your Dev machine has control and you want to do normal boot.... You can do two things....
1. (gdb)Continue --> This will continue Kernel booting and it will not return GDB prompt back because it is quite understandable that your kernel will be running until it is not really powered off..
2. (gdb)detach --> This will continue kernel booting and gdb prompt will be returned and now GDB is detached from the test machine...
Now, the question is HOW to get to the KERNEL debugging again ? How Dev machine GDB can get control over Test machine again ?
So, the answer is Sending TRAP signal on GDB session doesnt help... You have to send MANUALLY on TEST machine SysRq command... So, on TEST machine press SysRq + g (i.e., press "ALT" key then Press "PrintScreen" Key and then Press "g" key)
This will stop the Kernel booting and give control to GDB... If you had "detach" on GDB session previously.... you will have to do again (gdb)target remote /dev/ttyS0
There are other possibilities where you face problems
1. Your Kerenel is booted and SysRq+g is not working...
Please do [r00t@root] echo 1 > /proc/sys/kernel/sysrq
This will enable sending SysRq commands.....
2. You may face problem that you want to do Kernel Module Debugging and how to stop Kernel...
There are two ways.... 1. You call kgdb_breakpoints() function in your init_module function and it is declared in kgdb.h file....This will send a TRAP signal and control will be passed to GDB session.........
Another way is load your module and then send SysRq + g command...
However, one fundamental thing you need to take care here ....
You had started your GDB session with vmlinux file and now you are trying to debug a module that you have written... Quite obviously your GDB session wont know any of the symbols that you are using in your Module...
To solve that problem.... you have to make GDB aware of those symbols....
(gdb)add-symbol-file <.ko file> <.TEXT section address>
You load module on Test machine and then do [root@test]cat /sys/modules/<module name>/sections/.text
This will give you the second argument that you need to give to add-symbol-file commands.....
It is assumed that every time you load the module, it will be loaded at the same location .......
There is one more problem that I faced
When I was doing Module debugging and I was calling the kgdb_breakpoints, the machine was hanging and I was not able to connect to Dev machine.... The logical reason was, it was hanging due to breakpoint but then it was not able to basically communicate.... and the reason was KGDB I/O driver was not configured.......... In such case you may need to reconfigure the KGDB I/O driver....
You can do the same on Test machine in a following way
[root@test]echo "ttyS0,115200" > /sys/modules/<module name>/parameters
I think after all these i was able to do everything quite perfectly