Chinaunix首页 | 论坛 | 博客
  • 博客访问: 281474
  • 博文数量: 59
  • 博客积分: 1346
  • 博客等级: 中尉
  • 技术积分: 461
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-06 17:17
文章分类

全部博文(59)

文章存档

2012年(9)

2011年(50)

分类: LINUX

2011-01-19 15:15:44

一、内核中的调试支持
在前面已经建议过:学习编写驱动程序要构建安装自己的内核(标准主线内核)。最重要的原因之一是:内核开发者已经建立了多项用于调试的功能。但是由于这些功能会造成额外的输出,并导致能下降,因此发行版厂商通常会禁止发行版内核中的调试功能。
为了实现内核调试,我在内核配置上增加了几项:
  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
书上介绍的还有其他配置,有的我不需要,或是s3c2440不支持,菜单里看不见。
二、通过打印调试
(1)printk
 首先,printk有8个loglevel,定义在中:

#define    KERN_EMERG    "<0>"    /* system is unusable           */
#define    KERN_ALERT    "<1>"   /* action must be taken immediately*/
#define    KERN_CRIT    "<2>"    /* critical conditions    */
#define    KERN_ERR    "<3>"    /* error conditions            */
#define    KERN_WARNING    "<4>"    /* warning conditions   */
#define    KERN_NOTICE    "<5>"    /* normal but significant condition */
#define    KERN_INFO    "<6>"    /* informational            */
#define    KERN_DEBUG    "<7>"    /* debug-level messages   */

 
/proc/sys/kernel/printk里的内容含义。kernel/printk.c
控制台日志级别:优先级高于该值的消息将被打印至控制台
缺省的消息日志级别:将用该优先级来打印没有优先级的消息
最低的控制台日志级别:控制台日志级别可被设置的最小值(最高优先级)
缺省的控制台日志级别:控制台日志级别的缺省值
修改:
echo 8 < /proc/sys/kernel/printk
 
4.2.4. 打开和关闭消息

在驱动开发的早期, printk 非常有助于调试和测试新代码. 当你正式发行驱动时, 换句话说, 你应当去掉, 或者至少关闭, 这些打印语句. 不幸的是, 你很可能会发现, 就在你认为你不再需要这些消息并去掉它们时, 你要在驱动中实现一个新特性(或者有人发现了一个 bug), 你想要至少再打开一个消息. 有几个方法来解决这 2 个问题, 全局性地打开或关闭你地调试消息和打开或关闭单个消息.

这里我们展示一种编码 printk 调用的方法, 你可以单独或全局地打开或关闭它们; 这个技术依靠定义一个宏, 在你想使用它时就转变成一个 printk (或者 printf)调用。

首先,在头文件中(例如 scrull.h)中添加如下程序

/* 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)

说明:当在Makefile中DEBFLAGS = -O -g -DSCULL_DEBUG 中加入了-D,则会打印出信息,如果去掉了D,则不会打印信息。其实-D就是调试的关键

 

模块程序链接:模块程序
模块测试程序链接模块测试程序

阅读(1269) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~