HOME

TheInfoList



OR:

printk is a
printf printf is a C standard library function that formats text and writes it to standard output. The function accepts a format c-string argument and a variable number of value arguments that the function serializes per the format string. Mism ...
-like function of the
Linux kernel interface The Linux kernel provides multiple interfaces to user-space and kernel-mode code. The interfaces can be classified as either application programming interface (API) or application binary interface (ABI), and they can be classified as either ke ...
for formatting and writing kernel log entries. Since the
C standard library The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
(which contains the ubiquitous printf-like functions) is not available in
kernel mode In computer science, hierarchical protection domains, often called protection rings, are mechanisms to protect data and functionality from faults (by improving fault tolerance) and malicious behavior (by providing computer security). Computer ...
, provides for general-purpose output in the kernel. Due to limitations of the kernel design, the function is often used to aid
debugging In engineering, debugging is the process of finding the Root cause analysis, root cause, workarounds, and possible fixes for bug (engineering), bugs. For software, debugging tactics can involve interactive debugging, control flow analysis, Logf ...
kernel mode
software Software consists of computer programs that instruct the Execution (computing), execution of a computer. Software also includes design documents and specifications. The history of software is closely tied to the development of digital comput ...
. printk can be called from anywhere in the kernel except during early stages of the boot process; before the system console is initialized. 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.


Use

has the same
syntax In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
as , but somewhat different
semantics Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
. Like , accepts a format c-string
argument An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
and a list of value arguments. Both format text based on the input parameters and with significantly similar behavior, but there are also significant differences. The
function prototype In computer programming, a function prototype is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body. While a function definition ...
(which matches that of ) is: The features different from printf are described below.


Log level

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 string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
and via one of the macros designed for this purpose. For example, a message could be logged at the informational level as: printk(KERN_INFO "Message: %s", arg) The text specifying the log level consists of the
ASCII ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
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. The following table lists each log level with its canonical meaning. When no log level is specified, the entry is logged as the default level which is typically , but can be set; such as via the boot argument. Log levels are defined in header file . Which log levels are printed is configured using the
sysctl sysctl is a software mechanism in some Unix-like operating systems that reads and modifies the attributes of the system kernel such as its version number, maximum limits, and security settings. It is available both as a system call for compile ...
file .


Pointer formats

The %p format specifier which is supported by printf, is extended with additional formatting modes. For example, requesting to print a using %pISpc formats an IPv4/v6 address and port in a human-friendly format such as or .


No floating point support

While printf supports formatting
floating point In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a signed sequence of a fixed number of digits in some base) multiplied by an integer power of that base. Numbers of this form ...
numbers, printk does not, since the Linux kernel does not support floating point numbers.


Implementation

The function tries to lock the
semaphore Semaphore (; ) is the use of an apparatus to create a visual signal transmitted over distance. A semaphore can be performed with devices including: fire, lights, flags, sunlight, and moving arms. Semaphores can be used for telegraphy when arra ...
controlling access to the Linux system console. If it succeeds, the output is logged and the console drivers are called. 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. 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.


References

{{Reflist


External links


printk format reference
Linux kernel Operating system APIs Articles with example C code