Linux printk 的内核模块错误:格式参数太多

Linux kernel module error for printk: too many arguments for format

我在 /home/coder/workspace/test_module 目录中创建了 hello.cMakefile。我正在使用 arch linux。我已经安装了 linux-headers.

hello.c

#include <linux/init.h>
#include <linux/module.h>


static int hello_init(void){
    printk(KERN_ALERT, "hello, this is hello module");

    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT, "exiting the hello module");
}

module_init(hello_init);
module_exit(hello_exit);

生成文件

obj-m=hello.o

KDIR = /usr/lib/modules/$(shell uname -r)/build

all :
        make -C $(KDIR) M=$(PWD) modules


clean :
        rm -rf *.o *.ko *.mod.* *.symvers *.order

错误

[coder@coder test_module]$ make
make -C /usr/lib/modules/4.10.13-1-ARCH/build M=/home/coder/workspace/test_module modules
make[1]: Entering directory '/usr/lib/modules/4.10.13-1-ARCH/build'
  CC [M]  /home/coder/workspace/test_module/hello.o
In file included from ./include/linux/printk.h:6:0,
                 from ./include/linux/kernel.h:13,
                 from ./include/linux/list.h:8,
                 from ./include/linux/module.h:9,
                 from /home/coder/workspace/test_module/hello.c:2:
/home/coder/workspace/test_module/hello.c: In function 'hello_init':
./include/linux/kern_levels.h:4:18: warning: too many arguments for format [-Wformat-extra-args]
 #define KERN_SOH "[=13=]1"  /* ASCII Start Of Header */
                  ^
./include/linux/kern_levels.h:8:20: note: in expansion of macro 'KERN_SOH'
 #define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
                    ^~~~~~~~
/home/coder/workspace/test_module/hello.c:6:9: note: in expansion of macro 'KERN_ALERT'
  printk(KERN_ALERT, "hello, this is hello module");
         ^~~~~~~~~~
/home/coder/workspace/test_module/hello.c: In function 'hello_exit':
./include/linux/kern_levels.h:4:18: warning: too many arguments for format [-Wformat-extra-args]
 #define KERN_SOH "[=13=]1"  /* ASCII Start Of Header */
                  ^
./include/linux/kern_levels.h:8:20: note: in expansion of macro 'KERN_SOH'
 #define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
                    ^~~~~~~~
/home/coder/workspace/test_module/hello.c:12:9: note: in expansion of macro 'KERN_ALERT'
  printk(KERN_ALERT, "exiting the hello module");
         ^~~~~~~~~~
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/coder/workspace/test_module/hello.mod.o
  LD [M]  /home/coder/workspace/test_module/hello.ko
make[1]: Leaving directory '/usr/lib/modules/4.10.13-1-ARCH/build'

更多详情

$ uname -r
4.10.13-1-ARCH

$ ls -l /usr/lib/modules/$(uname -r)/build
drwxr-xr-x   4 root root     4096 Apr 27 15:47 Documentation
-rw-r--r--   1 root root      252 Apr 27 15:47 Kconfig
-rw-r--r--   1 root root    59062 Apr 27 15:47 Makefile
-rw-r--r--   1 root root  1321690 Apr 27 15:47 Module.symvers
drwxr-xr-x   4 root root     4096 Apr 27 15:47 arch
drwxr-xr-x   3 root root     4096 Apr 27 15:47 block
drwxr-xr-x   2 root root     4096 Apr 27 15:47 certs
drwxr-xr-x   4 root root     4096 Apr 27 15:47 crypto
drwxr-xr-x 123 root root     4096 Apr 27 15:47 drivers
drwxr-xr-x  64 root root     4096 Apr 27 15:47 fs
drwxr-xr-x  21 root root     4096 Apr 27 15:47 include
drwxr-xr-x   2 root root     4096 Apr 27 15:47 init
drwxr-xr-x   8 root root     4096 Apr 27 15:47 kernel
drwxr-xr-x   4 root root     4096 Apr 27 15:47 lib
drwxr-xr-x   2 root root     4096 Apr 27 15:47 mm
drwxr-xr-x  58 root root     4096 Apr 27 15:47 net
drwxr-xr-x   2 root root     4096 Apr 27 15:47 samples
drwxr-xr-x  14 root root     4096 Apr 27 15:47 scripts
drwxr-xr-x  10 root root     4096 Apr 27 15:47 security
drwxr-xr-x  21 root root     4096 Apr 27 15:47 sound
drwxr-xr-x   3 root root     4096 Apr 27 15:47 tools
drwxr-xr-x   2 root root     4096 Apr 27 15:47 usr
drwxr-xr-x   4 root root     4096 Apr 27 15:47 virt
-rw-r--r--   1 root root 20557744 Apr 27 15:46 vmlinux

我做错了什么?

Loglevel 不是 printk() 的单独参数,它是消息的前缀。你的情况:

printk(KERN_ALERT "hello, this is hello module");

更多信息:https://en.wikipedia.org/wiki/Printk