Message logging with printk¶
printk() is one of the most widely known functions in the Linux kernel. It’s thestandard tool we have for printing messages and usually the most basic way oftracing and debugging. If you’re familiar with printf(3) you can tellprintk()is based on it, although it has some functional differences:
printk()messages can specify a log level.the format string, while largely compatible with C99, doesn’t follow theexact same specification. It has some extensions and a few limitations(no
%nor floating point conversion specifiers). SeeHow to getprintk format specifiers right.
Allprintk() messages are printed to the kernel log buffer, which is a ringbuffer exported to userspace through /dev/kmsg. The usual way to read it isusingdmesg.
printk() is typically used like this:
printk(KERN_INFO "Message: %s\n", arg);
whereKERN_INFO is the log level (note that it’s concatenated to the formatstring, the log level is not a separate argument). The available log levels are:
Name | String | Alias function |
|---|---|---|
KERN_EMERG | “0” | |
KERN_ALERT | “1” | |
KERN_CRIT | “2” | |
KERN_ERR | “3” | |
KERN_WARNING | “4” | |
KERN_NOTICE | “5” | |
KERN_INFO | “6” | |
KERN_DEBUG | “7” |
|
KERN_DEFAULT | “” | |
KERN_CONT | “c” |
The log level specifies the importance of a message. The kernel decides whetherto show the message immediately (printing it to the current console) dependingon its log level and the currentconsole_loglevel (a kernel variable). If themessage priority is higher (lower log level value) than theconsole_loglevelthe message will be printed to the console.
If the log level is omitted, the message is printed withKERN_DEFAULTlevel.
You can check the currentconsole_loglevel with:
$ cat /proc/sys/kernel/printk4 4 1 7
The result shows thecurrent,default,minimum andboot-time-default loglevels.
To change the current console_loglevel simply write the desired level to/proc/sys/kernel/printk. For example, to print all messages to the console:
# echo 8 > /proc/sys/kernel/printk
Another way, usingdmesg:
# dmesg -n 5
sets the console_loglevel to print KERN_WARNING (4) or more severe messages toconsole. Seedmesg(1) for more information.
As an alternative toprintk() you can use thepr_*() aliases forlogging. This family of macros embed the log level in the macro names. Forexample:
pr_info("Info message no. %d\n", msg_num);prints aKERN_INFO message.
Besides being more concise than the equivalentprintk() calls, they can use acommon definition for the format string through thepr_fmt() macro. Forinstance, defining this at the top of a source file (before any#includedirective):
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
would prefix every pr_*() message in that file with the module and function namethat originated the message.
For debugging purposes there are also two conditionally-compiled macros:pr_debug() andpr_devel(), which are compiled-out unlessDEBUG (oralsoCONFIG_DYNAMIC_DEBUG in the case ofpr_debug()) is defined.
Function reference¶
- pr_fmt¶
pr_fmt(fmt)
used by the pr_*() macros to generate the printk format string
Parameters
fmtformat string passed from a pr_*() macro
Description
This macro can be used to generate a unified format string for pr_*()macros. A common use is to prefix all pr_*() messages in a file with a commonstring. For example, defining this at the top of a source file:
#define pr_fmt(fmt) KBUILD_MODNAME “: “ fmt
would prefix all pr_info, pr_emerg... messages in the file with the modulename.
- printk¶
printk(fmt,...)
print a kernel message
Parameters
fmtformat string
...variable arguments
Description
This isprintk(). It can be called from any context. We want it to work.
If printk indexing is enabled,_printk() is called from printk_index_wrap.Otherwise, printk is simply #defined to _printk.
We try to grab the console_lock. If we succeed, it’s easy - we log theoutput and call the console drivers. If we fail to get the semaphore, weplace the output into the log buffer and return. The current holder ofthe console_sem will notice the new output inconsole_unlock(); and willsend it to the consoles before releasing the lock.
One effect of this deferred printing is that code which callsprintk() andthen changes console_loglevel may break. This is because console_loglevelis inspected when the actual printing occurs.
See also:printf(3)
See thevsnprintf() documentation for format string extensions over C99.
- pr_emerg¶
pr_emerg(fmt,...)
Print an emergency-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_EMERG loglevel. It usespr_fmt() togenerate the format string.
- pr_alert¶
pr_alert(fmt,...)
Print an alert-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_ALERT loglevel. It usespr_fmt() togenerate the format string.
- pr_crit¶
pr_crit(fmt,...)
Print a critical-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_CRIT loglevel. It usespr_fmt() togenerate the format string.
- pr_err¶
pr_err(fmt,...)
Print an error-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_ERR loglevel. It usespr_fmt() togenerate the format string.
- pr_warn¶
pr_warn(fmt,...)
Print a warning-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_WARNING loglevel. It usespr_fmt()to generate the format string.
- pr_notice¶
pr_notice(fmt,...)
Print a notice-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_NOTICE loglevel. It usespr_fmt() togenerate the format string.
- pr_info¶
pr_info(fmt,...)
Print an info-level message
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_INFO loglevel. It usespr_fmt() togenerate the format string.
- pr_cont¶
pr_cont(fmt,...)
Continues a previous log message in the same line.
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_CONT loglevel. It should only beused when continuing a log message with no newline (’n’) enclosed. Otherwiseit defaults back to KERN_DEFAULT loglevel.
- pr_devel¶
pr_devel(fmt,...)
Print a debug-level message conditionally
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands to a printk with KERN_DEBUG loglevel if DEBUG isdefined. Otherwise it does nothing.
It usespr_fmt() to generate the format string.
- pr_debug¶
pr_debug(fmt,...)
Print a debug-level message conditionally
Parameters
fmtformat string
...arguments for the format string
Description
This macro expands todynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG isset. Otherwise, if DEBUG is defined, it’s equivalent to a printk withKERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
It usespr_fmt() to generate the format string (dynamic_pr_debug() usespr_fmt() internally).