Monday, 15 June 2009

The Debugfs Interface

Last week I was tweaking the rt73usb driver and doing some debugging using the debugfs kernel interface (Greg Kroah-Hartman wrote the debugfs interface back in 2004). Debugfs is a virtual filesystem devoted to debugging information, and makes it easier for kernel developers to tweak and twiddle drivers and hardware using a relatively simple API. Debugfs can be compiled in for debug purposes, and compiled out for final kernel delivery.

One generally mounts the debugfs as follows:

sudo mount -t debugfs debug /sys/kernel/debug

and then drivers populate this mount point according to their own whim.

To use debugfs in your drivers start with:

struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);

name being the name of the directory you want to create, and parent typically is NULL (causing the directory to be created in debugfs root).

There are some helper functions to allow one to read/write values as follows:
struct dentry *debugfs_create_u8(const char *name, mode_t mode, struct dentry *parent, u8 *value);
struct dentry *debugfs_create_u16(const char *name, mode_t mode, struct dentry *parent, u16 *value);
struct dentry *debugfs_create_u32(const char *name, mode_t mode, struct dentry *parent, u32 *value);
struct dentry *debugfs_create_bool(const char *name, mode_t mode, struct dentry *parent, u32 *value);

The above functions create debugfs files that can be read/written to modify the u8,u16,u32 and bool values.

Debugfs does not tidy up these files when a module is removed, so one has to do this for each file using:
void debugfs_remove(struct dentry *dentry);
It's quite a handy little interface, and can be replace printk()'s as a debug interface. As I said earlier in this article, I've used it now for tweaking and twiddling values in the rt73usb WiFi driver with success.


No comments:

Post a Comment