1. 内核调试的困难:
内核不和特定的进程关联,所以无法很容易的用调试器来调试,而且很难跟踪。
2. 打印调试
2.1 printk
printk的形式如下:
printk(KERN_DEBUG "Here I am: %s:%i\n", __FILE__, __LINE__);
形式和printf相当类似,其中KERN_DEBUG代表的是当前打印的优先级。
打印的优先级包括如下几种:
#define KERN_EMERG KERN_SOH "0" /* system is unusable */
#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
#define KERN_CRIT KERN_SOH "2" /* critical conditions */
#define KERN_ERR KERN_SOH "3" /* error conditions */
#define KERN_WARNING KERN_SOH "4" /* warning conditions */
#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */
#define KERN_INFO KERN_SOH "6" /* informational */
#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
定义优先级的目的是为了尽量减少不必要的输出信息,所以在编写的代码时候要注意优先级的使用。
对于printk的每个优先级,还有相对的宏可以直接使用:
#define pr_emerg(fmt, ...) \
printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert(fmt, ...) \
printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit(fmt, ...) \
printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
#