18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci===========================
48c2ecf20Sopenharmony_ciMessage logging with printk
58c2ecf20Sopenharmony_ci===========================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciprintk() is one of the most widely known functions in the Linux kernel. It's the
88c2ecf20Sopenharmony_cistandard tool we have for printing messages and usually the most basic way of
98c2ecf20Sopenharmony_citracing and debugging. If you're familiar with printf(3) you can tell printk()
108c2ecf20Sopenharmony_ciis based on it, although it has some functional differences:
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci  - printk() messages can specify a log level.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci  - the format string, while largely compatible with C99, doesn't follow the
158c2ecf20Sopenharmony_ci    exact same specification. It has some extensions and a few limitations
168c2ecf20Sopenharmony_ci    (no ``%n`` or floating point conversion specifiers). See :ref:`How to get
178c2ecf20Sopenharmony_ci    printk format specifiers right <printk-specifiers>`.
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciAll printk() messages are printed to the kernel log buffer, which is a ring
208c2ecf20Sopenharmony_cibuffer exported to userspace through /dev/kmsg. The usual way to read it is
218c2ecf20Sopenharmony_ciusing ``dmesg``.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciprintk() is typically used like this::
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci  printk(KERN_INFO "Message: %s\n", arg);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciwhere ``KERN_INFO`` is the log level (note that it's concatenated to the format
288c2ecf20Sopenharmony_cistring, the log level is not a separate argument). The available log levels are:
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
318c2ecf20Sopenharmony_ci| Name           | String |  Alias function                               |
328c2ecf20Sopenharmony_ci+================+========+===============================================+
338c2ecf20Sopenharmony_ci| KERN_EMERG     | "0"    | pr_emerg()                                    |
348c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
358c2ecf20Sopenharmony_ci| KERN_ALERT     | "1"    | pr_alert()                                    |
368c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
378c2ecf20Sopenharmony_ci| KERN_CRIT      | "2"    | pr_crit()                                     |
388c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
398c2ecf20Sopenharmony_ci| KERN_ERR       | "3"    | pr_err()                                      |
408c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
418c2ecf20Sopenharmony_ci| KERN_WARNING   | "4"    | pr_warn()                                     |
428c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
438c2ecf20Sopenharmony_ci| KERN_NOTICE    | "5"    | pr_notice()                                   |
448c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
458c2ecf20Sopenharmony_ci| KERN_INFO      | "6"    | pr_info()                                     |
468c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
478c2ecf20Sopenharmony_ci| KERN_DEBUG     | "7"    | pr_debug() and pr_devel() if DEBUG is defined |
488c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
498c2ecf20Sopenharmony_ci| KERN_DEFAULT   | ""     |                                               |
508c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
518c2ecf20Sopenharmony_ci| KERN_CONT      | "c"    | pr_cont()                                     |
528c2ecf20Sopenharmony_ci+----------------+--------+-----------------------------------------------+
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciThe log level specifies the importance of a message. The kernel decides whether
568c2ecf20Sopenharmony_cito show the message immediately (printing it to the current console) depending
578c2ecf20Sopenharmony_cion its log level and the current *console_loglevel* (a kernel variable). If the
588c2ecf20Sopenharmony_cimessage priority is higher (lower log level value) than the *console_loglevel*
598c2ecf20Sopenharmony_cithe message will be printed to the console.
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciIf the log level is omitted, the message is printed with ``KERN_DEFAULT``
628c2ecf20Sopenharmony_cilevel.
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciYou can check the current *console_loglevel* with::
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci  $ cat /proc/sys/kernel/printk
678c2ecf20Sopenharmony_ci  4        4        1        7
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciThe result shows the *current*, *default*, *minimum* and *boot-time-default* log
708c2ecf20Sopenharmony_cilevels.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciTo change the current console_loglevel simply write the desired level to
738c2ecf20Sopenharmony_ci``/proc/sys/kernel/printk``. For example, to print all messages to the console::
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci  # echo 8 > /proc/sys/kernel/printk
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciAnother way, using ``dmesg``::
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci  # dmesg -n 5
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cisets the console_loglevel to print KERN_WARNING (4) or more severe messages to
828c2ecf20Sopenharmony_ciconsole. See ``dmesg(1)`` for more information.
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciAs an alternative to printk() you can use the ``pr_*()`` aliases for
858c2ecf20Sopenharmony_cilogging. This family of macros embed the log level in the macro names. For
868c2ecf20Sopenharmony_ciexample::
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci  pr_info("Info message no. %d\n", msg_num);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciprints a ``KERN_INFO`` message.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciBesides being more concise than the equivalent printk() calls, they can use a
938c2ecf20Sopenharmony_cicommon definition for the format string through the pr_fmt() macro. For
948c2ecf20Sopenharmony_ciinstance, defining this at the top of a source file (before any ``#include``
958c2ecf20Sopenharmony_cidirective)::
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci  #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciwould prefix every pr_*() message in that file with the module and function name
1008c2ecf20Sopenharmony_cithat originated the message.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciFor debugging purposes there are also two conditionally-compiled macros:
1038c2ecf20Sopenharmony_cipr_debug() and pr_devel(), which are compiled-out unless ``DEBUG`` (or
1048c2ecf20Sopenharmony_cialso ``CONFIG_DYNAMIC_DEBUG`` in the case of pr_debug()) is defined.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciFunction reference
1088c2ecf20Sopenharmony_ci==================
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci.. kernel-doc:: kernel/printk/printk.c
1118c2ecf20Sopenharmony_ci   :functions: printk
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/printk.h
1148c2ecf20Sopenharmony_ci   :functions: pr_emerg pr_alert pr_crit pr_err pr_warn pr_notice pr_info
1158c2ecf20Sopenharmony_ci      pr_fmt pr_debug pr_devel pr_cont
116