Linux Kernel print function printk usage instructions

Source: Internet
Author: User
Printk Function

We used the printk function in earlier chapters with the simplifying assumption that it works like printf. Now it's time to introduce some of the differences.

In the previous chapter, we simply use printk as a printf function. It's time to introduce some of its differences.

One of the differences is that printk lets you classify messages according to their severity by associating different loglevels, or priorities, with the messages. you usually indicate the loglevel with a macro. for example, kern_info, which we saw prepended to some of the earlier print statements, is one of the possible loglevels of the message. the loglevel macro expands to a string, which is concatenated to the message text at compile time; that's why there is no comma between the priority and the format string in the following examples. here are two examples of printk commands, a debug message and a critical message:

One difference is that printk allows you to strictly classify messages based on their record level or priority. Generally, you need a macro to specify the record level. For example, kern_info, we have seen this macro in earlier examples, which is a type of message record level. Record level macros are used to expand to a string, which is connected to the corresponding message text during compilation; this explains why there is no comma between the priority and the formatted string in the following example. The following are two examples of printk functions: one is the debugging message and the other is the critical message:

Printk (kern_debug "here I am: % s: % I/N", _ file _, _ line __);

 

Printk (kern_crit "I'm trashed; giving up on % P/N", PTR );

 

There are eight possible loglevel strings, defined in the header

 

; We list them in order of decreasing severity:

In the header file <Linux/kernel. h>, eight available record levels are defined. We will list them in descending order of their severity:

Kern_emerg

Used for emergency messages, usually those that precede a crash.

Messages used for emergencies are usually reported before a system crash.

Kern_alert

A situation requiring immediate action.

Use this message if you need immediate operation.

Kern_crit

Critical conditions, often related to serious hardware or software failures.

This message is used when a critical hardware error occurs.

Kern_err

Used to report error conditions; device drivers often use kern_err to report hardware difficulties.

Used to report error conditions. device drivers often use kern_err to report hardware difficulties.

Kern_warning

Warnings about problematic situations that do not, in themselves, create serious problems with the system.

It is a warning about the problem situation, which generally does not cause serious problems of the system.

Kern_notice

Situations that are normal, but still worthy of note. A number of security-related conditions are reported at this level.

This level is common, but it is worth noting. Many security-related situations are reported at this level.

Kern_info

Informational messages. Many drivers print information about the hardware they find at startup time at this level.

Information Message. Many drivers use it to output hardware information at startup.

Kern_debug

Used for debugging messages.

Used to output debugging information

Each string (in the macro expansion) represents an integer in angle brackets. integers range from 0 to 7, with smaller values representing higher priorities.

Each string (expanded by a macro) represents an integer in angle brackets. The value ranges from 0 to 7. The smaller the value, the higher the priority.

A printk statement with no specified priority defaults to default_message_loglevel, specified in kernel/printk. C as an integer. in the 2.6.10 kernel, default_message_loglevel is kern_warning, but that has been known to change in the past.

The default priority of a printk is default_message_loglevel, which is an integer specified in the kernel/printk. c file. In kernel 2.6.10, default_message_loglevel is equivalent to kern_warning, But it is said that in earlier versions this is two different priorities.

Based on the loglevel, the kernel may print the message to the current console, be it a text-mode terminal, a serial port, or a parallel printer. if the priority is less than the integer variable console_loglevel, the message is delivered to the console one line at a time (nothing is sent unless a trailing newline is provided ). if both klogd and syslogd are running on the system, kernel messages are appended to/var/log/messages (or otherwise treated depending on your syslogd configuration), independent of console_loglevel. if klogd is not running, the message won't reach user space unless you read/proc/kmsg (which is often most easily done with the dmesg command ). when using klogd, you shoshould remember that it doesn't save consecutive identical lines; it only saves the first such line and, at a later time, the number of repetitions it already ed.

Based on these record levels, the kernel can output messages to the current console, or a text-mode terminal, a serial port, or a parallel printer. If the priority is less than the integer variable lele_loglevel, only one row of messages will be sent to the console at a time (unless a line break is encountered, nothing will be sent ). If the system runs the klogd and syslogd processes, then the kernel message will be completely added to the/var/log/messages file (or sent based on the configuration of your syslogd process) and the console_loglevel will be ignored. If klogd is not running, the message will not reach the user space unless you read the/proc/kmsg file (in fact, this work has been completed by the earlier dmesg command ). When using klogd, you should remember that it does not retain duplicate message lines; For the duplicated messages it receives, it only retains the first one.

The variable console_loglevel is initialized to default_console_loglevel and can be modified through the sys_syslog system call. one way to change it is by specifying the-C switch when invoking klogd, as specified in the klogd manpage. note that to change the current value, you must first kill klogd and then restart it with the-C option. alternatively, you can write a program to change the console loglevel. you'll find a version of such a program in MISC-progs/setlevel. c In the source files provided on O 'Reilly's FTP site. the new level is specified as an integer value between 1 and 8, inclusive. if it is set to 1, only messages of level 0 (kern_emerg) reach the console; if it is set to 8, all messages, including debugging ones, are displayed.

The lele_loglevel variable initialized as default_console_loglevel can be modified by calling sys_syslog. One way to change its content is to use the-C option when calling klogd. For details, refer to the man help of klogd. Note that to change the current value, you must first end the klogd process and restart it with the-C option. Another way is to write an application to change the record level of the console. You can find the/MISC-progs/setlevel. c file in the source code file provided by O 'Reilly's FTP site. There is such a program. The new record level is an integer ranging from 1 to 8. If it is set to 1, only messages with a priority of 0 (kern_emerg) can arrive at the console. If the level is set to 8, all messages including debugging information will be displayed.

It is also possible to read and modify the console loglevel using the text file/proc/sys/kernel/printk. the file hosts four integer values: the current loglevel, the default level for messages that lack an explicit loglevel, the minimum allowed loglevel, And the boot-time default loglevel. writing a single value to this file changes the current loglevel to that value; thus, for example, you can cause all kernel messages to appear at the console by simply entering:

You can also use the text file/proc/sys/kernel/printk to obtain and change the record level of the console. This file stores four integer values: current record level, default record level, minimum record level, and default record level at startup time. You can write a single value to the file to change the current record level. For example, if you want to display all kernel messages on the console, you can run the following command:

# Echo 8>/proc/sys/kernel/printk

It shoshould now be apparent why the hello. c sample had the kern_alert; markers; they are there to make sure that the messages appear on the console.

Now you should understand why there is a kern_alert; identifier in the hello. c sample code. In this way, the message can be smoothly output to the console.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.