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...

 

 

 

文章標籤
全站熱搜
創作者介紹
創作者 flykof 的頭像
flykof

十年磨一劍

flykof 發表在 痞客邦 留言(0) 人氣(1,923)