LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: Disabled
kgdb module debugging question


Hi,

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:

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

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

Make sure the following two options are enabled

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

Give 'Y' to both option

Note:
In Linux Version 2.6.38.8, 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-2.6.38.8/arch/i386/boot
c. System.map at ~/linux-2.6.38.8
d. vmlinux at ~/linux-2.6.38.8

3. Copy System.map 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-2.6.38.8-kgdb
[root$test/boot] cp /media/<device>/System.map System.map-2.6.38.8-kgdb

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 2.6.38.8-kgdb' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
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-2.6.38.8-kgdb 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-2.6.38.8
[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

(gdb)continue

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 */

(gdb)

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
0xfa4a7000

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
..done

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

(gdb)c

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

Note:
Currently getting above error on continuing. On exploring, came accross below link
http://www.mail-archive.com/kgdb-bug.../msg03464.html

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,
Meena

Last edited by meenajain; 10-05-2011 at 04:05 AM.
 
Old 10-11-2011, 05:05 AM   #2
meenajain
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, 10:11 AM   #3
ducalpha
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!
Thanks.
 
Old 09-03-2012, 09:10 AM   #4
gaoping561
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, 12:51 AM   #5
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Try to disable CONFIG_DEBUG_RODATA and CONFIG_RANDOMIZE_BASE
 
  


Reply


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 12:43 AM
Problem Debugging Kernel Module with KGDB raycope14 Linux - Software 2 09-08-2009 11:46 AM
kgdb module debugging vkmgeek Programming 1 07-23-2008 05:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 AM.

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