How to use pr_cont to continue a previous log message in the same line?

Viewed 65

[Question] Recently I did some work on the Linux kernel, and want to use 'print' to continue a previous log message in the same line. So I tried to use pr_cont in <linux/kernel.h> to replace printk in the function.

The code is here:

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/printk.h>

typedef struct {
    TEST_PRINTF    printf;
    TEST_UDELAY    udelay;
    TEST_MALLOC    malloc;
    TEST_FREE      free;
} TEST_INIT_PARAM_T;

static int __init test_init(void) {
    TEST_INIT_PARAM_T my_init_param = { 0 };

    /* The problem is here */
    my_init_param.printf = pr_cont;
    my_init_param.udelya = my_udelay;
    my_init_param.malloc = my_malloc;
    my_init_param.free = my_free;

    my_init(&my_init_param);

    ....
}

and GCC shows an error: 'pr_cont' undeclared (first use in this function). However, if use

my_init_param.printf = printk;

and this will be ok.

I have also tried using

void test_printk(some_variables) {
    printk(KERN_CONT some_variables)
}

And make my_init_param.printf = test_printk. But I don't know what the 'some_variable' should be?

Also, I really want to know why the equation my_init_param.printf = pr_cont is wrong?

[Attempt] I have realized that the true reason why the equation my_init_param.printf = pr_cont;doesn't hold. Just like I can't define a = int b. So I add typedef as below

typedef int
(*TEST_PRINTK)(
    const char *fmt,
    ...);

TEST_PRINTK test_pr_cont;

#define test_pr_cont(fmt, ...) \
    printk(KERN_CONT fmt, ##__VA_ARGS__)

Then the equationmy_init_param.printf = test_pr_cont;can pass the GCC.

However, other functions called this 'print' cannot print anything.

1 Answers

why the [expression] my_init_param.printf = pr_cont is wrong?

Because pr_cont is a macro, it's not a function. Sole identifier pr_cont does not exist, only the call pr_cont( the call ) will be expanded to the call to printk by preprocessor. pr_cont is a function-like macro.

How to use pr_cont to continue a previous log message in the same line?

Write pr_cont with your message after another pr_* or printk above without a newline on the end. There are endless examples in kernel, browse them. https://elixir.bootlin.com/linux/latest/source/arch/csky/kernel/traps.c#L124

Related