Tuesday, February 22, 2022

[SOLVED] How to print function name with function pointer in Linux kernel?

Issue

I'm reading the Linux kernel code. Some functions are called by function pointer. I want to know the call sequence of all these functions, so I've tried to print the function. But I haven't figured out how to make it.

Here is my code:

for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++) {
    //printk("xhl -- %pF \n", fn);
    do_one_initcall(*fn);
}

Solution

Kernel's printk() supports special %p format specifiers:

Symbols/Function Pointers

    %pF     versatile_init+0x0/0x110
    %pf     versatile_init
    %pS     versatile_init+0x0/0x110
    %pSR    versatile_init+0x9/0x110
            (with __builtin_extract_return_addr() translation)
    %ps     versatile_init
    %pB     prev_fn_of_versatile_init+0x88/0x88

See https://www.kernel.org/doc/Documentation/printk-formats.txt for full list.

For your example, setting the initcall_debug=1 kernel cmdline option might be a better way, than adding printk() manually.



Answered By - ensc
Answer Checked By - Terry (WPSolving Volunteer)