Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-05-2011, 05:02 AM   #1
LQ Newbie
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
kgdb module debugging question


I am trying to debug my loadable kernel module via KGDB remote debugging. I have tried to explain the steps that I am following in detail:

[dev@dev] represents development machine
[test@test] represents test machine

1. Configure the kerenl:
[dev@dev] cd ~/linux-
[dev@dev/linux-] make menuconfig

Make sure the following two options are enabled

Kernel Hacking -> Magic SysRq Key
-> KGDB: Kernel Debugger

Give 'Y' to both option

In Linux Version, these options are enabled by default.

2. Built he kernel and my driver

Note: mydriver source resides at ~/mysource/

a. mydriver.ko file at ~/output/.
b. bzImage at ~/linux-
c. at ~/linux-
d. vmlinux at ~/linux-

3. Copy and bzImage to some external media storage.

4. Copy the following kernel images to target machine (/boot) with root permissions

[root$test] cd /boot
[root$test/boot] cp /media/<device>/bzImage vmlinuz-
[root$test/boot] cp /media/<device>/

5. Copy mydriver.ko file to test machine

[root$test] cd ~/mydriver/
[root$test/mydriver] cp /media/<device>/mydriver.ko mydriver.ko

6. Add a grub entry
[root$test] cd /boot/grub
[root$test] vi grub.cfg

Add below lines:

menuentry 'Ubuntu, with Linux' --class ubuntu --class gnu-linux --class gnu --class os {
set gfxpayload=$linux_gfx_mode
insmod part_msdos
insmod ext2
set root='(/dev/sda,msdos1)'
search --no-floppy --fs-uuid --set=root bfc015dd-36ce-469a-b11f-4e7939ba4b90
linux /boot/vmlinuz- root=/dev/sda1 ro crashkernel=384M-2G:64M,2G-:128M quiet splash vt.handoff=7 kgbwait kgdboc=ttyS0,115200

7. Boot the test machine with those kernel parameters.

8. This will stop your Kernel booting on Test machine and wait for the gdb connection from devlopment machine.

9. On development machine, start GDB session and run following commands

[dev@dev] cd ~/linux-
[dev@dev] gdb vmlinux

The argument vmlinux file is the file that is created with Debug symbols and it is present in the directory where you gave "make" command..

(gdb)set remotebaud 115200
(gdb)target remote /dev/ttyS0
Remote debugging using /dev/ttyS0
kgdb_breakpoint () at kernel/debug/debug_core.c:960
960 wmb(); /*Sync point after breakpoint */

10. This will give control to your development machine.

11. Once development machine has control and to do the normal boot give the below command


This will continue Kernel booting and it will not return GDB prompt back unless and until there is a kernel crash.

12. Before loading your module on test machine, make sure the serial interface baud rate is set to 115200
[test@root] stty -F /dev/ttyS0 115200

13. Load the module on test machine,

[test@root] cd ~/mydriver/
[test@root] insmod mydriver.ko

14. Send SysRq + g command.

[test@root] echo 1 > /proc/sys/kernel/sysrq
[test@root] echo g > /proc/sysrq-trigger

This will stop the kernel execution and give control to the gdb

(gdb) target remote /dev/ttyS0
Remote debugging using /dev/ttyS0

kgdb_breakpoint () at kernel/debug/debug_core.c:960
960 wmb(); /* Sync point after breakpoint */


15. Since we had started your GDB session with vmlinux file and now we are trying to debug a module that we have written, GDB session wont know any of the symbols that we are using in our Module. To solve this problem, we have to make GDB aware of those symbols using add-symbol-file.

Syntax: (gdb)add-symbol-file <path to .ko file> <.Text Section address>

<Test Machine>

Run the below command to know the address of .text section

[test@root]cat /sys/modules/mydriver/sections/.text

This will give the second argument that you need to give to add-symbol-file commands.

<Development Machine>

(gdb)add-symbol-file ~/output/mydriver.ko 0xfa425000
add symbol table from file "/home/mymachine/output/mydriver.ko" at
.text_addr = 0xfa425000
(y or n)y
Reading symbols from /home/mymachine/output/mydriver.ko

It will load the symbol information.

16. Now we are able to set breakpoint to your module functions.

(gdb)br mydriver_function

Breakpoint 1 at 0xfa4581ae: file ~/mysource/mydriver_source.c, line 1168

16. Continue


Cannot insert breakpoint 1.
Error accessing memory address 0xfa4581ae: Unknown error 4294967295

Currently getting above error on continuing. On exploring, came accross below link

As per the above link, CONFIG_DEBUG_RODATA should be turned off when using kgdb. I tried with turning CONFIG_DEBUG_RODATA off, but still facing the above issue.
Please correct me where I went wrong.

Thanks and Regards,

Last edited by meenajain; 10-05-2011 at 05:05 AM.
Old 10-11-2011, 06:05 AM   #2
LQ Newbie
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
The mistake I was making is that I wase setting the breakpoint using break (br) instead I have to use hbreak(hb) hardware assisted breakpoint.
1 members found this post helpful.
Old 08-17-2012, 11:11 AM   #3
LQ Newbie
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
I got the same problem with error, i.e. "Error accessing memory address 0xf895d000: Unknown error -1."
Using hbreak as you said solved my problem!
Old 09-03-2012, 10:10 AM   #4
LQ Newbie
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
CONFIG_DEBUG_SET_MODULE_RONX need turn off also to debug kernel module
Old 04-11-2018, 01:51 AM   #5
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
kgdb module debugging question vkmgeek Programming 5 09-30-2011 01:43 AM
Problem Debugging Kernel Module with KGDB raycope14 Linux - Software 2 09-08-2009 12:46 PM
kgdb module debugging vkmgeek Programming 1 07-23-2008 06:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration