printk is a printf-like function of the Linux kernel interface for formatting and writing kernel log entries.[1] Since the C standard library (which contains the ubiquitous printf-like functions) is not available in kernel mode, printk provides for general-purpose output in the kernel.[2] Due to limitations of the kernel design, the function is often used to aid debugging kernel mode software.[1]

printk can be called from anywhere in the kernel except during early stages of the boot process, before the system console is initialized.[3] The alternative function early_printk is implemented on some architectures and is used identically to printk but during the early stages of the boot process.[3]

Use

edit

printk has the same syntax as printf, but somewhat different semantics. Like printf, printk accepts a format C-string argument and a list of value arguments.[1] Both format text based on the input parameters and with significantly similar behavior, but there are also significant differences.[1] The printk function prototype (which matches that of printf) is:

int printk(const char* format, ...);

The features different from printf are described below.

Log level

edit

printk allows a caller to specify a log level – the type and importance of the message being sent. The level is specified by prepending text that identifies a log level. Typically the text is prepended via C's string literal concatenation and via one of the macros designed for this purpose. For example, a message could be logged at the informational level as:[1]

printk(KERN_INFO "Message: %s", arg)

The text specifying the log level consists of the ASCII SOH character followed by a digit that identifies the log level or the letter 'c' to indicate the message is a continuation of the previous message.[1][4] The following table lists each log level with its canonical meaning.[3]

0 KERN_EMERG An emergency condition; the system is probably dead
1 KERN_ALERT A problem that requires immediate attention
2 KERN_CRIT A critical condition
3 KERN_ERR An error
4 KERN_WARNING A warning
5 KERN_NOTICE A normal, but perhaps noteworthy, condition
6 KERN_INFO An informational message
7 KERN_DEBUG A debug message

When no log level is specified, the entry is logged as the default level which is typically KERN_WARNING,[1] but can be set, such as via the loglevel= boot argument.[5]

Log levels are defined in header file <linux/kern_levels.h>.[4] Which log levels are printed is configured using the sysctl file /proc/sys/kernel/printk.[1]

Pointer formats

edit

The %p format specifier which is supported by printf, is extended with additional formatting modes. For example, requesting to print a struct sockaddr * using %pISpc formats an IPv4/v6 address and port in a human-friendly format such as 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345.[6]

No floating point support

edit

While printf supports formatting floating point numbers, printk does not,[6] since the Linux kernel does not allow the use floating-point numbers in kernel code. (The use of floating-point numbers requires saving and restoring additional registers and status flags compared to integer-only code.)[7]

Implementation

edit

The function tries to lock the semaphore controlling access to the Linux system console.[1][8] If it succeeds, the output is logged and the console drivers are called.[1] If it is not possible to acquire the semaphore the output is placed into the log buffer, and the current holder of the console semaphore will notice the new output when they release the console semaphore and will send the buffered output to the console before releasing the semaphore.[1]

One effect of this deferred printing is that code which calls printk and then changes the log levels to be printed may break. This is because the log level to be printed is inspected when the actual printing occurs.[1]

References

edit
  1. ^ a b c d e f g h i j k l "Message logging with printk — The Linux Kernel documentation". www.kernel.org. Retrieved September 9, 2020.
  2. ^ ISO/IEC 9899:2018. International Standards Organization. 2018.
  3. ^ a b c "printk()". archive.is. August 30, 2007. Archived from the original on February 1, 2012. Retrieved September 9, 2020.
  4. ^ a b "kern_levels.h". GitHub. Retrieved September 27, 2020.
  5. ^ "The kernel's command-line parameters". kernel.org. Retrieved September 27, 2023.
  6. ^ a b "How to get printk format specifiers right — The Linux Kernel documentation". www.kernel.org. Retrieved September 9, 2020.
  7. ^ "Re: Linux kernel and floating point". www.redhat.com. Archived from the original on July 21, 2019. Retrieved September 9, 2020.
  8. ^ "Driver Basics — The Linux Kernel documentation". www.kernel.org. Retrieved September 9, 2020.
edit

📚 Artikel Terkait di Wikipedia

Printf

the same problems that register_printf_function() has. The Linux kernel printk function supports a number of ways to display kernel structures using the

PREEMPT RT

mainline Linux kernel after a protracted development hurdle involving the printk kernel logging facility. PREEMPT-RT is actively used at the moment by distributors

Linux kernel

and halts the computer. The kernel provides for debugging by printing via printk(), which stores messages in a circular buffer (overwriting older entries

Kernel panic

implementation of version 0.01 simply being: volatile void panic(const char * s) { printk("Kernel panic: %s\n\r",s); for(;;); } Since then, however, the panic routine

Symbol table

AT91F_US3_CfgPIO_useB 2000005c t AT91F_PIO_CfgPeriph 200000b0 T main 20000120 T AT91F_DBGU_Printk 20000190 t AT91F_US_TxReady 200001c0 t AT91F_US_PutChar 200001f8 T AT91F_SpuriousHandler

Giant lock

June 28, 2017 Thomas Gleixner (September 11, 2022). "[patch RFC 19/29] printk: Add basic infrastructure for non-BKL consoles". linux-kernel (Mailing list)