Solved Debug FreeBSD klds

I'm trying to debug a function from a kld and in order to do that I use gdb, the steps that i follow in order to do that are:

1. load the debugging symbols from /boot/kernel/kernel;
2. load debugging symbols from <my_file>.ko;
3. set a breakpoint on a function that should execute when the module is inserted in the kernel;
My problem is that the breakpoint is never reached after kldload of the module.
 

Attachments

  • img1.jpg
    img1.jpg
    40.4 KB · Views: 267
Please don't post pictures of text output. They're impossible to quote or copy/paste from.
 
I never tried this myself – fortunately, the only time I did work on some kernel module code, I got along with the oldest debugging tool in history, namely printf(9) ;) Yes, I know this doesn't scale well with complex code… :-/
 
I never tried this myself – fortunately, the only time I did work on some kernel module code, I got along with the oldest debugging tool in history, namely printf(9) ;) Yes, I know this doesn't scale well with complex code… :-/
I also like printf(9) for debugging, but in my case I also try to better understand the code that I use and the feature of gdb to walk through code step by step would help a lot.
 
When you set the breakpoint, the code is not loaded so gdb doesn't even have the address, much less the code nor any mapping. How should that work?

You need to first load the module, THEN set the breakpoint.

And configure a dump device! Don't test on your development machine. I f.e. had a deploy make target, pushing stuff to the A4 next to my desk.
Get the FreeBSD Device Driver handbook.
 
When you set the breakpoint, the code is not loaded so gdb doesn't even have the address, much less the code nor any mapping. How should that work?

You need to first load the module, THEN set the breakpoint.

And configure a dump device! Don't test on your development machine. I f.e. had a deploy make target, pushing stuff to the A4 next to my desk.
Get the FreeBSD Device Driver handbook.
Thank you, the strange behaviour make sense now.
But now I have another question, can i set a breakpoint to kldload(2) in order to catch the loading of module in the kernel and if the answer is yes, how should I do that?
 
I found out that in order to set a breakpoint before a kld gets loaded in the kernel I should use module_register and for unload module_unload, more details can be found on sys/kern/kern_module.c ( from IRC group).

And for remote debugging there are two more steps, unspecified buy handbook, that should be taken into account for klds:
* first you need to set DEBUG_FLAGS=-g inside your make file for that module in order to compile the module with debug symbols (for extra details about compiling process of klds see /usr/src/sys/conf/kmod.mk - this is the place where I found out about DEBUG_FLAGS )
* use ]add-kld to load debugging symbols of kld into gdb (after you connect to remote host), more details can be found here.

Also make sure that you are using a remote debugging environment because as Greg Lehey mention in Debugging Kernel Problems

Debugging running systems

For some things, you don’t need to stop the kernel. If you’re only looking, for example, you can use a debugger on the same system to look at its own kernel. Inthis case you use the special file/dev/meminstead of dump file.You’re somewhat limited in what you can do: you can’t set breakpoints, you can’tstop execution, and things can change while you’re looking at them.Youcanchange data, but you need to be particularly careful, or not care too much whether you crashthe system.
for short: on the same system you can’t set breakpoints.

Bonus tip:
Also you should add COPTFLAGS=-O0 and CFLAGS=-O0 -pipe in /etc/make.conf in order to prevent "<optimized out>" when debugging the module (this will compile both kernel and modules s with -O0), "just don't forget to remove it after doing your debugging...", "that'll affect world and port builds too", basically anything you add to make.conf is added to any call of make. (thanks <RhodiumToad> and <dstolfa> from IRC)
 
Last edited:
Back
Top