====本文系本站原创,欢迎转载! 转载请注明出处:http://blog.csdn.net/yyplc====
Linux下调试方法多样,可以通过以下这些途径:#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*/在编写代码时可以根据实际情况选择输出级别,一般选KERN_DEBUG级别。
上面两种是等价的。
功能:Linux下一个混杂设备驱动的测试(linux-2.6.35.30)
#include <linux/module.h> #include <linux/kernel.h> #include <linux/errno.h> #include <linux/platform_device.h> #include <linux/irqflags.h> #include <linux/slab.h> #include <linux/init.h> #include <linux/idr.h> #include <linux/mutex.h> #include <linux/miscdevice.h> #include <linux/fs.h> #define MY_DEBUG #ifdef MY_DEBUG #define dbg(fmt,arg...) printk(KERN_DEBUG "my_debug:\npath:%s\nfunction:%s(),line:%d,date:%s,time:%s\n" fmt "\n", __FILE__, \ __FUNCTION__, __LINE__,__DATE__, \ __TIME__, ##arg) #else #define dbg(fmt,arg...) do {} while(0) //#define dbg(fmt,arg...) (void)(0) #endif static int my_open(struct inode *inode, struct file *file) { dbg("file open ok"); return 0; } static ssize_t my_read(struct file *file, char *buf, size_t size, loff_t *pos) { dbg("file read ok"); return size; } static ssize_t my_write(struct file *file, char *buf, size_t size, loff_t *pos) { dbg("file write ok"); return size; } static int my_release(struct inode *inode, struct file *file) { dbg("file release ok"); return 0; } static const struct file_operations my_fops = { .owner = THIS_MODULE, .open = my_open, .read = my_read, .write = my_write, .release = my_release, }; struct miscdevice my_misc_dev = { .minor = MISC_DYNAMIC_MINOR, .name = "my_dev", .fops = &my_fops, }; static int __init my_dev_init(void) { int res; res = misc_register(&my_misc_dev); if (res) { dbg("misc_register error: %d",res); return res; } dbg("mis_register ok"); return res; } static void __exit my_dev_exit(void) { misc_deregister(&my_misc_dev); dbg("mis deregister ok"); return; } module_init(my_dev_init); module_exit(my_dev_exit); MODULE_AUTHOR("itspy.wei<[email protected]>"); MODULE_DESCRIPTION("linux debug using printk"); MODULE_LICENSE("GPL");Ubuntu上测试结果:
[15449.892067] mis_register ok [16050.131267] my_debug: [16050.131269] path:/home/wsn/my_module/my_debug/my_debug.c [16050.131270] function:my_open(),line:28,date:Apr 15 2012,time:16:02:10 [16050.131272] file open ok [16050.131304] my_debug: [16050.131305] path:/home/wsn/my_module/my_debug/my_debug.c [16050.131306] function:my_write(),line:44,date:Apr 15 2012,time:16:02:10 [16050.131307] file write ok [16050.131336] my_debug: [16050.131337] path:/home/wsn/my_module/my_debug/my_debug.c [16050.131338] function:my_read(),line:37,date:Apr 15 2012,time:16:02:10 [16050.131339] file read ok [16050.131349] my_debug: [16050.131350] path:/home/wsn/my_module/my_debug/my_debug.c [16050.131351] function:my_release(),line:52,date:Apr 15 2012,time:16:02:10 [16050.131352] file release ok