Linux Device Drivers

Debug 訊息開關

 

在研發驅動程式的初期,printk()是輔助測試、偵錯新程式碼的好工具。到了正式釋出驅動程式的階段,就該拿掉這些無謂的printk()敘述或至少讓它們失效。

以下的範例提供一個簡便的方式,讓你可以一口氣拿掉所有偵測用途的printk()

 另外,也可以多增加一個不偵錯的空巨集,當有想註銷掉的錯誤訊息,可直接替換它的名稱。

Makefile

CFILES=hello.c

APP=fly_debug

KERNELDIR ?= $(shell pwd)/../../..

PWD := $(shell pwd)

 

obj-$(CONFIG_EMIL_LDD) := $(APP).o

$(APP)-objs := $(CFILES:.c=.o)

 

# If you wnat disable all debug massage, nullify the following command

DEBUG = y

 

ifeq ($(DEBUG),y)

CFLAGS += -O -g -DFLYKOF_DEBUG

else

CFLAGS +=

endif

 

default:

           $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

 

clean:

           $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

 

hello.c

#include <linux/init.h>

#include <linux/module.h>

 

#include "debug.h"

 

MODULE_LICENSE("Dual BSD/GPL");

static int __init_hello(void)

{

       printk(KERN_INFO "Hello, world\n");

                     FLY_DEBUG("DEBUG call...\n");

       return 0;

}

 

static void __exit_hello(void)

{

       printk(KERN_INFO "Goodbye, cruel world\n");

                     FLY_DEBUG("Goodbye...\n");

}

 

module_init(__init_hello);

module_exit(__exit_hello);

 

debug.h

#ifndef _DEBUG_H_

#define _DEBUG_H_

 

//#define RL_DEBUG

#if defined(FLYKOF_DEBUG_HI)

/* note: prints function name for you */

           #define FLY_DEBUG(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ## args)

#elif defined(FLYKOF_DEBUG)

           #define FLY_DEBUG(fmt, args...) printk("Flykof Debug : " fmt, ## args)

#else

           #define FLY_DEBUG(fmt, args...) /* Don't detect error (Empty Macro) */

#endif

 

#undef FLY_DEBUGG

#define FLY_DEBUGG(fmt, args...) /* Disable single error massage (Empty Macro) */

 

#endif /* _DEBUG_H_ */

 

Test…

# insmod fly_debug.ko

Hello, world

Flykof Debug : DEBUG call...

# rmmod fly_debug.ko

Goodbye, cruel world

Flykof Debug : Goodbye...

 

 http://flykof.pixnet.net/blog/post/27909634-linux-device-drivers-%E2%94%80--debug-%E8%A8%8A%E6%81%AF%E9%96%8B%E9%97%9C

arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()