为了实现内核调试,我在内核配置上增加了几项:
Kernel hacking --->
[*] Magic SysRq key
[*] Kernel debugging
[*] Debug slab memory allocations
[*] Spinlock and rw-lock debugging: basic checks
[*] Spinlock debugging: sleep-inside-spinlock checking
[*] Compile the kernel with debug info
[*] Magic SysRq key
Device Drivers --->
Generic Driver Options --->
[*] Driver Core verbose debug messages
General setup --->
[*] Configure standard kernel features (for small systems) --->
[*] Load all symbols for debugging/ksymoops
书上介绍的还有其他配置。
#define KERN_EMERG "<0>" /* system is unusable */ |
- /* printk's without a loglevel use this.. */
- #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */ |
- /* Macros to help debugging */
- #undef PDEBUG /* undef it, just in case */
- #ifdef SCULL_DEBUG
- # ifdef __KERNEL__
- /* This one if debugging is on, and kernel space */
- # define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ## args)
- # else /* This one for user space */
- # define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
- # endif
- #else
- # define PDEBUG(fmt, args...) /* not debugging: nothing */
- #endif
- #undef PDEBUGG
- #define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
Makefile中要添加的语句:
- # Comment/uncomment the following line to disable/enable debugging
- DEBUG = y
- # Add your debugging flag (or not) to CFLAGS
- ifeq ($(DEBUG),y)
- DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
- else
- DEBFLAGS = -O2
- endif
- CFLAGS += $(DEBFLAGS)
这些宏是很有用的,仅有的缺点是每次开启和关闭消息显示时,都要重新编译模块。
③为了避免printk重复输出过快而阻塞系统,内核使用以下函数跳过部分输出
- int printk_ratelimit(void);
- if (printk_ratelimit( ))
- printk(KERN_NOTICE "The printer is still on fire\n");
可以通过修改/proc/sys/kernel/printk_ratelimit(重开信息前应等待的秒数)和/proc/sys/kernel/printk_ratelimit_burst(在速度限制前可接受的信息数)来定制printk_ratelimit的行为。 Linux还提供了打印设备编号的宏(在 中定义):
- int print_dev_t(char *buffer, dev_t dev);
- char *format_dev_t(char *buffer, dev_t dev);
两个函数的唯一区别是:print_dev_t返回打印字符数,format_dev_t返回缓冲区指针。注意缓冲区char *buffer的大小应至少有20B。