Weekly Edition Return to the Kernel pageSponsored link Serve your customers, not your servers, with VERIO Linux VPS. Full-access test-drive here. |
Debugfs
Kernel hackers often need to be able to export debugging information to
user space. This information is not needed for the regular operation of
the system, but it can be highly useful for a developer who is trying to
figure out why things are behaving strangely. Sometimes putting in a few
printk() calls is sufficient, but, often, that is not the best way
to go. The debugging information may only be useful occasionally, but the
printed output clogs up the logs all the time. Using printk()
also does not help if the developer wishes to be able to change values from
user space.
A common way of making debugging information available only when needed (and possibly for write access) is to create one or more files in a virtual filesystem. There are a few ways in which that can be done:
As a way of making life easier for developers, Greg Kroah-Hartman has created debugfs, a virtual filesystem devoted to debugging information. Debugfs is intended to be a relatively easy and lightweight subsystem which gracefully disappears when configured out of the kernel. A developer wishing to use debugfs starts by creating a directory within the filesystem:
struct dentry *debugfs_create_dir(const char *name,
struct dentry *parent);
The parent argument will usually be NULL, causing the directory to be created in the debugfs root. If debugfs is not configured into the system, the return value is -ENODEV; a NULL return, instead, indicates some other sort of error. The general-purpose function for creating a file in debugfs is:
struct dentry *debugfs_create_file(const char *name, mode_t mode,
struct dentry *parent, void *data,
struct file_operations *fops);
The structure pointed to by fops should, of course, contain pointers to the functions which implement the actual operations on the file. In many cases, most of those functions can be the helpers provided by seq_file, making the task of exporting a file easy. Some additional helpers have been provided to make exporting a single value as easy as possible:
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);
Debugfs does not automatically clean up files when a module shuts down, so, for every file or directory created with the above functions, there must be a call to:
void debugfs_remove(struct dentry *dentry);
The debugfs interface is quite new, and it may well see changes before finding its way into the mainline kernel. In particular, Greg has considered adding a kobject parameter to the creation calls; the kobject would then provide the name for the resulting files. (Log in to post comments)
Debugfs Posted Dec 16, 2004 2:04 UTC (Thu) by iabervon (subscriber, #722) [Link] A bit later in that thread, he revised that idea to having another set of functions to use a kobject, to keep the code that doesn't have a kobject to pass simple.
Debugfs Posted Dec 16, 2004 9:38 UTC (Thu) by raible (subscriber, #3899) [Link] Not to nitpick (ok, to nitpick), but the final declaration is incorrect.debugfs_create_bool should _not_ take "u32 *value" as an argument; presumable what was meant was "bool *", no?
Debugfs Posted Dec 16, 2004 12:33 UTC (Thu) by pjdc (guest, #6906) [Link] C does not have a 'bool' type.
bool Posted Dec 17, 2004 10:37 UTC (Fri) by dododge (subscriber, #2870) [Link] C does not have a 'bool' type.Actually the latest version (C99) does have such a type, but you have to include <stdbool.h> to make it visible by that name. "bool" is implemented using a new integer type _Bool. _Bool requires special behavior when values are converted to it, so it can't be implemented as a simple typedef of some other integer type.
Debugfs Posted Dec 16, 2004 12:42 UTC (Thu) by ekj (subscriber, #1524) [Link] One thing I never 100% got the point of is the change from procfs tosysfs. I mean, other than the fact that normally "procfs" is mounted at /proc and "sysfs" is mounted at /sys, what is the fundamental difference ? Yes I'm aware that different information is available, in different formats, in the two hierarchies. What I do *not* understand is what fundamental reason prevented the files now populating sysfs from instead being created in proc.
Sysfs vs Procfs Posted Dec 16, 2004 13:15 UTC (Thu) by alex (subscriber, #1355) [Link] IIRC it was because /proc was meant to be to store process information but then turned into a maze of twisty directories with all sorts of random data dumped into it - including device information.
Sysfs on the other hand is designed to be a representation of the kernel device model. For that reason stuff isn't just dumped in, the position of nodes with sysfs actually represents the hierachy of devices in relation to their subsytems.
Sysfs vs Procfs Posted Dec 17, 2004 10:21 UTC (Fri) by xav (subscriber, #18536) [Link] Yes, the big difference is that sysfs can be parsed automatically, whereas procfs needs ad-hoc tools for each of its components.
Why multiple filesystems? Posted Dec 17, 2004 22:18 UTC (Fri) by giraffedata (subscriber, #1954) [Link] What is even less clear to me than why procfs isn't good enough for what sysfs does is why procfs isn't good enough for what debugfs does.The /proc name space did get messed up when people decided to put stuff other than process information (pid entries) in it. It would make more sense to call it something else and have a 'proc' directory in it for the pid entries. But other than that, I can't see why someone would object to having a debug interface in /proc but not object to it being in /debug. As a kernel developer, I don't see anything that would make me want to learn a whole extra interface rather than put my debug interface in /proc.
|
Copyright © 2004, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds
Powered by Rackspace Managed Hosting.