Linux general technology-Linux programming and kernel information. For more information, see the following. I recently gave a Linux video card driver debug. The driver is complex and involves Xserver. You need to jump to different places to view the code from time to time. The kernel driver, Xserver driver, and Xorg server have more than 0.2 million lines of code. This kind of stuff needs to start debugging in a short time. Obviously, it is impossible to read the code, clarify the logic in detail, and then start to do so.
??
Because I don't know the details, I can only guess the problem in most cases, and then I will try again.
??
I don't need kgdb. I only like to output debugging information through printk, and then analyze the process by reading logs. Although printk is simple, it has several very useful functions:
??
1. Set the message priority. Because each piece of printk information has a level, you can set the target output file of the message with the corresponding priority in/etc/syslog. conf. For example, to output a message at the KERN_ERR level to/var/log/kerr. log, then in/etc/syslog. add "kern. err/var/log/keer. log. If "kern. */var/log/kern. log ", all printk outputs, including KERN_ERR, are output to kern. log, so that you can easily view different types of debugging information as needed.
??
This is where printk is more powerful than printf.
??
2. Add the row number and FUNCTION name in the output information of printk, and use the _ LINE _ and _ FUNCTION _ macros. The former uses % d for output, the latter is output in % s. Like this, there is also _ FILE __, but I don't use it very much. This is actually not a special function of printk. It seems that it is a built-in macro in C language or a gcc extension. In general user space programs, printf can also be output.
??
Sometimes there are a lot of logs. And because I don't know where the problem is, I need to output a lot of log information to spread the internet. At this time, you can find the problem by comparing the logs for normal operation and the logs for errors. The difference between the two will be easier to find the problem. Generally, you can use diff to compare logs, ediff in Emacs, vimdiff, or beyondcompare in windows. In order to make the difference, you often need to make some changes to the Code, such as changing the branch direction in some conditional tests to cause bugs (or temporarily eliminate bugs ).
??
If a bug is not found in the previous version of the code, the code of the old version and the new version with the bug may be different. We also use tools such as diff, ediff, and vimdiff to compare the code. I recently used git for version control. This tool is developed by Linus and is quite good. I personally think it is a little better than svn. Git can directly compare the differences between any two versions. You can use git diff v1.. v2. You can also find Bugs by using the binary method. For details, refer to the git bisect command manual.
??
I only want to think about this for the time being. You are welcome to share your experiences.
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