Linux Kernel (14)-Two points and PRINTK

Source: Internet
Author: User

Life is a coffee table, which is filled with cups. The kernel is also a big coffee table, but the cup above it is a bug. Determining when a bug is being introduced is a critical step, and it is natural to use a binary lookup method, whether intentional or unintentional, in the process of locating a bug.

The basic principle of binary search method

We will not and should not be unfamiliar with the binary search method. As an efficient search algorithm, it appeared in our data structure classroom, in the interview again and again, it will be frequently used in our code. In the various algorithms we are exposed to, it can be said to be the most popular, the most full of life wisdom, many people do not know the concept of binary search method, but can be skilled in life to apply.

For example, a worker to repair a 10km long telephone line, first he needs to locate the fault, if a small section along the line to find, it is obviously very difficult, every search point to climb a pole, 10km long distance will have about 200 poles. Assuming that the ends of the wire are A, B, then he will be naturally first from the middle of the C start to check, with the phone to the end of the test, found that the AC section is normal, and therefore concluded that the fault in BC, and then to the midpoint of the BC section D, if the BD segment is found to be normal, the fault in the , you can reduce the length of the unknown origin line by half, so after 7 times to find, you can reduce the scope of the failure to 50~100m around, that is, near one or two poles. This saves a lot of effort and time.

This is a binary search method in the life of a typical application, in fact, to find the kernel of the bug and to find the phone line of the fault compared to the essence is the same, and not advanced to where to go, are first to locate the fault location, and then to solve it.

For example, when you use a version of the kernel, you find a kernel bug, you need to know it is in the application of which patch was introduced, if one to restore those patches, each restore a patch will be tested once the kernel, it will inevitably waste too much time, and the use of binary search method, Start by identifying a kernel version that definitely does not appear, and then test the version in the middle of the two versions so that you can easily pinpoint which version of the bug was started by repeating the filter.

PRINTK ()

PRINTK () should be the closest partner for every driver developer, and we often combine it with binary lookup to find the place where the problem is occurring in the code.

Typically, for the two PRINTK () statements in the code, if one is executed normally and the other is not executed, the problem occurs between these two printk () and the next step is to use the binary lookup method to locate the problematic code within this range.

1. PRINTK () and printf ()

The user space has printf (), kernel space has PRINTK (), they are like the fate of good and evil twins, even if the appearance of how close to the function, can not coexist in the code.

The easiest mistake for us to make is to misuse printf () where PRINTK () is needed, and to misuse PRINTK () where printf () is needed, usually not because you don't know the difference, but only by habit. Folk have this saying: when you write a user-space application, subconsciously write is PRINTK (), then it means that you are a standard kernel developers.

2. Message level for PRINTK ()

One important difference between PRINTK () and printf () is that the former can specify the print level of the message, and the kernel determines whether to print the message to the terminal based on the specified level. As shown in the table below, there are 8 levels of PRINTK ().

Level

Describe

Kern_emerg

In case of emergency, the system may crash.

Kern_alert

Must respond immediately

Kern_crit

Critical condition

Kern_err

Error message

Kern_warning

Warning message

Kern_notice

General information that may require attention

Kern_info

Informational Information

Kern_debug

Debugging information

If you do not specify a level for the message, PRINTK () uses the default default_message_loglevel (usually kern_warning).

3. Log level of the console (Console_loglevel)

When the message level specified by PRINTK is less than the specified console log level, the contents of the message are displayed on the console. The log level of the console is defined in the Include/linux/kernel.h file, which defaults to Default_console_loglevel (the value equals 7), which means that by default, PRINTK () is higher than the kern_debug level The message content can be displayed on the console.

We can execute the following command so that any level of PRINTK () messages are printed on the terminal

$ echo 8 >/PROC/SYS/KERNEL/PRINTK

4. Variants of PRINTK ()

The kernel provides two variants of PRINTK () Pr_debug and pr_info in the include/linux/kernel.h file, defined as:

235 #define PR_DEBUG (Fmt,arg ...)/

236 PRINTK (Kern_debug fmt,# #arg)

244 #define PR_INFO (Fmt,arg ...)/

245 PRINTK (Kern_info fmt,# #arg)

5. PRINTK () is not a panacea

PRINTK () Although very useful, but it is not omnipotent, in the system startup, the terminal has not been initialized before, it can not be used, but if not in the debugging system startup process, this is not considered a problem.

In fact, the kernel provides a variant of the PRINTK () EARLY_PRINTK (), which is designed to print messages on the terminal at the beginning of the system, and it differs from the PRINTK () only by the difference in name and the ability to work earlier.

Linux Kernel (14)-Two points and PRINTK

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.