Tham khảo tài liệu 'linux device drivers-chapter 4 : debugging techniques', công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 4 Debugging Techniques One of the most compelling problems for anyone writing kernel code is how to approach debugging. Kernel code cannot be easily executed under a debugger nor can it be easily traced because it is a set of functionalities not related to a specific process. Kernel code errors can also be exceedingly hard to reproduce and can bring down the entire system with them thus destroying much of the evidence that could be used to track them down. This chapter introduces techniques you can use to monitor kernel code and trace errors under such trying circumstances. Debugging by Printing The most common debugging technique is monitoring which in applications programming is done by calling printf at suitable points. When you are debugging kernel code you can accomplish the same goal with printk. printk 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. 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 printkcommands a debug message and a 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 linux KERN_EMERG Used for emergency messages usually those that precede a crash. KERN_ALERT A situation requiring immediate action. KERN_CRIT Critical conditions often related to serious hardware or software .