最近看Linux下netfilter中某個模塊,文件自帶有大量的 pr_debug 調試信息,就想輸出來看一下,第一反應是用函數指針將 pr_debug 指向 printk,但一想,函數指針要解指針,不得行。
查了下一些文章,發現打開這個動態Debug比較麻煩(沒有看懂),再一看 pr_debug 的實現,這個是宏定義耶,我自己重定義到 printk 估計可以。然後,拿出這個模塊文件,放到桌面或者那個文件夾中,在頭部引入頭文件之後的位置加了如下宏定義:
#ifdef pr_debug
#undef pr_debug
#endif // pr_debug
#ifndef pr_debug
#ifndef KERN_DEBUG
#define KERN_DEBUG ""
#endif // KERN_DEBUG
#ifndef pr_fmt
#define pr_fmt(fmt) fmt
#endif // pr_fmt
#define pr_debug(fmt, ...) \
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif // pr_debug
然後,使用瞭如下Makefile,與內核文件 進行了編譯
obj-m += module_file_name.o
# module_file_name-objs = other_dependence_file_1.o other_dependence_file_2.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.ko *.o *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions
這個module_file_name是我在看的那個模塊文件,make 之後,將得到一個 module_file_name.ko 的模塊文件,
然後 使用
insmod module_file_name.ko # 加載 模塊
rmmod module_file_name # 卸載 模塊
lsmod # 查看 所有加載的模塊
modprobe xxx # 加載 內核中已有的模塊
modinfo module_file_name.ko # 查看 模塊的信息(depends項是依賴模塊,用modprobe加載)
加載模塊後,可以在日誌 /var/log/kern.log 看輸出(因爲我看的是netfilter的,所以,我發送模塊相應的數據包就有輸出了)。
可以使用tail命令跟蹤日誌輸出。
tail -f /var/log/kern.log
此文件純屬記錄,希望對他人有幫助。