17

I have a loadable kernel module and its init is as given below

static int __init id_init(void)
{
    struct identity *temp;

    /* some code which is not relevant to the question */

    temp = identity_find(3);
    pr_debug("id 3 = %s\n", temp->name);

    temp = identity_find(42);
    if (temp == NULL)
        pr_debug("id 42 not found\n");

    /* some code which is not relevant to the question */

    return 0;
}

Also I have enabled dynamic debugging enabled on the kernel version I am using - i.e CONFIG_DYNAMIC_DEBUG=y.

And in the Makefile of the module I have added a line CFLAGS_[id].o := -DDEBUG where id.c is the file name.

Now I checked in the /sys/kernel/debug/dynamic_debug/control after doing insmod of this module, in which I found the below lines

/home/pauldc/Programming/Kernel/id/id.c:69 [id]id_init =_ "id 42 not found\012"
/home/pauldc/Programming/Kernel/id/id.c:65 [id]id_init =_ "id 3 = %s\012"

Even after doing all this, to my disappointment I could not find the above two pr_debug statements in the output of dmesg. So what am I missing or doing wrong?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
PaulDaviesC
  • 1,161
  • 3
  • 16
  • 31
  • Check your system's log level. May be its not supposed to print debug logs. – Milind Dumbare Mar 09 '15 at 10:17
  • 1
    Have you not seem in dmesg or on screen or both? Try to add `dyndbg` parameter to the module when you load it. If you have compiled in use `.dyndbg`, where is a name of your module accordingly to Makefile. – 0andriy Mar 09 '15 at 11:00
  • @Miline Did you mean CONFIG_MESSAGE_LOGLEVEL_DEFAULT ? That is 4, as reported by the config file. But does that make any difference? – PaulDaviesC Mar 09 '15 at 11:42
  • Any specific reason why you can not use printk? – Milind Dumbare Mar 09 '15 at 11:43
  • @AndyShevchenko I did not get it. Can you please elaborate? – PaulDaviesC Mar 09 '15 at 11:44
  • @Miline I cannot use printk. This requirement comes from the assignment question. – PaulDaviesC Mar 09 '15 at 11:46
  • First of all, remove -DDEBUG from your Makefile. It's often wrong approach. Enable DYNAMIC_DEBUG (like you did already). Compile everything, then if you have module you load and it's called `id`, do `modprobe id dyndbg`. Also, if you would like to see on screen, set loglevel to 8. – 0andriy Mar 09 '15 at 19:16
  • Maybe you forgot the `echo 8 > /proc/sys/kernel/printk`: https://stackoverflow.com/a/49835405/895245 – Ciro Santilli OurBigBook.com Apr 14 '18 at 20:13

2 Answers2

18

Add following to Makefile, assuming filename.c is the module source file.

CFLAGS_filename.o := -DDEBUG

not

CFLAGS_[filename].o := -DDEBUG

Refer https://www.kernel.org/doc/local/pr_debug.txt

clearlight
  • 12,255
  • 11
  • 57
  • 75
Milind Dumbare
  • 3,104
  • 2
  • 19
  • 32
  • Or, to enable debug for the whole module, you can modify the Makefile in that directory and add flags to ccflags-y, if I understand section 4.2 of this document properly: https://www.kernel.org/doc/Documentation/kbuild/modules.txt – clearlight Aug 12 '19 at 01:53
8

CONFIG_DYNAMIC_DEBUG=y

https://www.kernel.org/doc/html/v4.11/admin-guide/dynamic-debug-howto.html

If you compile the kernel with this option, then you can do amazing things like:

echo 8 > /proc/sys/kernel/printk
echo 'file kernel/module.c +p' > /sys/kernel/debug/dynamic_debug/control

and this will selectively enable the pr_debug() you want.

We can then test this out with:

insmod mymodule.ko

which prints a lot of extra debug info as in:

[   84.875592] init_module: umod=0000000073518b66, len=185416, uargs=000000009c6e375a                    
[   84.876099] Core section allocation order:       
[   84.876257]  .text                               
[   84.876332]  .note.gnu.build-id                  
[   84.876418]  .rodata.str1.1                      
[   84.876492]  .orc_unwind_ip                      
[   84.876568]  .orc_unwind                         
[   84.876636]  __mcount_loc                        
[   84.876705]  .data                               
[   84.876760]  .gnu.linkonce.this_module           
[   84.876856]  .bss                                
[   84.876919] Init section allocation order:       
[   84.877041]  .symtab                             
[   84.877121]  .strtab                             
[   84.877235] final section addresses:             
[   84.877352]  0xffffffffc0006000 .note.gnu.build-id                                                    
[   84.877482]  0xffffffffc0005000 .text            
[   84.877580]  0xffffffffc0006024 .rodata.str1.1   
[   84.877695]  0xffffffffc0006040 .orc_unwind_ip   
[   84.877805]  0xffffffffc0006050 .orc_unwind      
[   84.877905]  0xffffffffc0006068 __mcount_loc     
[   84.878012]  0xffffffffc0007000 .data            
[   84.878107]  0xffffffffc0007000 .gnu.linkonce.this_module                                             
[   84.878238]  0xffffffffc0007340 .bss             
[   84.878331]  0xffffffffc000a000 .symtab          
[   84.878430]  0xffffffffc000a348 .strtab          
[   84.878657] Absolute symbol: 0x00000000          
[   84.878951] Absolute symbol: 0x00000000          
[   84.879713] hello init 

And in particular, it contains the module load address:

[   84.877482]  0xffffffffc0005000 .text            

which is useful to convert addresses to lines.

For modules, we can do:

echo 8 > /proc/sys/kernel/printk
echo 'module myprintk +p' > /sys/kernel/debug/dynamic_debug/control
insmod /myprintk.ko

which allows us to easily test pr_debug by adding that to our own module.

Tested on kernel 4.16 with this setup.

printk(KERN_DEBUG != pr_debug when CONFIG_DYNAMIC_DEBUG=y

This is very inconsistent, but printk(KERN_DEBUG does show up when loglevel=8 even if we don't enable /sys/kernel/debug/dynamic_debug/control, this can be seen from: https://stackoverflow.com/a/37283021/895245

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • Where do the default log levels come from without dynamic being enabled? And can you dig somewhere for the remainder of the log messages (the ones of lower priority than the default log levels) or are they just not emitted altogether? – sherrellbc Sep 29 '22 at 19:23
  • @sherrellbc I'm lazy to research now, but this should be easy to find out with some quick experiments/looking at the source, let me know if you do. – Ciro Santilli OurBigBook.com Oct 03 '22 at 12:56