Pengdonglin
Email: [Email protected]
When reading kernel code, always see a lot of drivers are in the first line to define the PR_FMT, idle to Nothing, analyzed, found, indeed very convenient. Share the following records.
We know that the dev_dbg can be used to output log in the driver, and there will be some additional information in the output log, such as the name of the device to which it belongs.
And PR_FMT can achieve this goal, first look at a usage (drivers/i2c/i2c-core.c):
#define pr_fmt (FMT) "I2c-core:" FMT<dt-bindings/i2c/i2c.h><asm/uaccess.h> <linux/acpi.h> ...
However, this file does not see pr_fmt being used. Then, the guess should be which macro is used, so I search for pr_fmt found under include:
Include/linux/printk.h:271: PRINTK (Kern_emerg pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:273: PRINTK (Kern_alert pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:275: PRINTK (Kern_crit pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:277: PRINTK (Kern_err pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:279: PRINTK (kern_warning pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:282: PRINTK (Kern_notice pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:284: PRINTK (Kern_info pr_fmt (FMT), # #__VA_ARGS__) include/linux/printk.h:296: PRINTK (Kern_debug pr_fmt (FMT), # #__VA_ARGS__)
Then look at the printk.h:
1 ... ...2 #ifndef pr_fmt3 #definePR_FMT (FMT) FMT4 #endif5 6 /*7 * These can used to print at the various log levels.8 * All of these would print unconditionally, although note that Pr_debug ()9 * and other debug macros be compiled out unless either debug is definedTen * or Config_dynamic_debug is set. One */ A #definePr_emerg (FMT, ...) - PRINTK (Kern_emerg pr_fmt (FMT), # #__VA_ARGS__) - #definePr_alert (FMT, ...) the PRINTK (Kern_alert pr_fmt (FMT), # #__VA_ARGS__) - #definePr_crit (FMT, ...) - PRINTK (Kern_crit pr_fmt (FMT), # #__VA_ARGS__) - #definePr_err (FMT, ...) + PRINTK (Kern_err pr_fmt (FMT), # #__VA_ARGS__) - #definePr_warning (FMT, ...) + PRINTK (kern_warning pr_fmt (FMT), # #__VA_ARGS__) A #definePr_warn pr_warning at #definePr_notice (FMT, ...) - PRINTK (Kern_notice pr_fmt (FMT), # #__VA_ARGS__) - #definePr_info (FMT, ...) - PRINTK (Kern_info pr_fmt (FMT), # #__VA_ARGS__) - - #ifDefined (config_dynamic_debug) in#include <linux/dynamic_debug.h> - to /*Dynamic_pr_debug () uses PR_FMT () internally so we don ' t need it 's here*/ + #definePr_debug (FMT, ...) - Dynamic_pr_debug (FMT, # #__VA_ARGS__) the #elifDefined (DEBUG) * #definePr_debug (FMT, ...) $ PRINTK (Kern_debug pr_fmt (FMT), # #__VA_ARGS__)Panax Notoginseng #else - #definePr_debug (FMT, ...) the NO_PRINTK (Kern_debug pr_fmt (FMT), # #__VA_ARGS__) + #endif A... ...
As you can see, if PR_FMT (FMT) is not defined in the driver, then PR_FMT (FMT) is the FMT.
With PR_FMT, there are a few more commonly used functions: Pr_err, Pr_info, Pr_debug will also use PR_FMT if no config_dynamic_debug is defined.
From the above code has seen the role of PR_FMT, is the user to output the log before adding additional fixed information. The following combination of GPIO_DEMO.C driver to look at several uses of pr_fmt:
1 #define pr_fmt (FMT) "Gpio_demo:" FMT2#define pr_fmt (FMT) kbuild_modname ":" FMT3 c5> #define pr_fmt (FMT) kbuild_modname ":%s:%d:" FMT, __func__, __line__
in the first line of GPIO_DEMO.C, define one of the above, and then, in the driver, call pr_info, such as:
1 Static intGpio_demo_probe (structPlatform_device *Pdev) {2 structDevice *dev = &pdev->Dev;3 intRET =0;4 inti =0;5 intGpio =-1;6gpio_demo_data_t *data =NULL;7 structResource *res =NULL;8 u32 config, pud, DRV;9 TenPr_info ("%s enter.\n", __func__); One... ...
The following is the output of the fmt "Gpio_demo:" #define pr_fmt (FMT) :
[1022.623230] Gpio_demo:gpio_demo_probe Enter.
The following is #define pr_fmt (FMT) kbuild_modname ":" the output of the FMT (kbuild_modname is the name of the module, Can read this driver file for makefile file obtained):
[1088.639631] Gpio_demo:gpio_demo_probe Enter.
The following is the output of #define pr_fmt (FMT) kbuild_modname ":%s:%d:" FMT, __func__, __line__ :
[1135.108534] Gpio_demo:gpio_demo_probe:87:gpio_demo_probe Enter.
This is quite handy for output log.
Finish.
The use of Linux driver development--pr_fmt