Simple debugging in kernel programming

Posted by on mai 17, 2007
Fără categorie

When programming Linux kernel modules, you have limited debugging options. The main way is to use the printk function (the kernel equivalent of printf). If you want to give as much information as possible, you could use some of the macros that the language offers you, such as __FILE__ or __line__. Here is a small snippet you could use in your modules:

#define DEBUG 1
#if DEBUG
#define Dprintk(format, ...) \
printk (KERN_ALERT "[%s]:FUNC:%s:line:%d: " format, __FILE__, \
__func__, __LINE__, __VA_ARGS__)
#else
#define Dprintk(format, ...) do {}while(0)
#endif

You can use the same code in userspace programs by replacing printk with printf. And just in case you’re wondering what’s with the empty do-while, you might want to take a look at this older article.

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.