Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


10.2.3 The Debug Filesystem


One particular filesystem using functions from libfs is the debug filesystem debugfs. It presents kernel
developers with a possibility of providing information to userland. The information is not supposed to
be compiled into production kernels. Quite in contrast, it is only an aid for developing new features.
Support for debugfs is only activated if the kernel is compiled with theDEBUG_FSconfiguration option.
Code that registers files in debugfs thus needs to be embraced by C pre-processor conditionals checking
forCONFIG_DEBUG_FS.

Example


Recall the kprobes example discussed earlier in the chapter as an example for the sequential file mecha-
nism. The resulting file is exported via debugfs in only a couple of lines — as simple as can be!

kernel/kprobes.c
#ifdef CONFIG_DEBUG_FS
...
static int __kprobes debugfs_kprobe_init(void)
{
struct dentry *dir, *file;
unsigned int value = 1;

dir = debugfs_create_dir("kprobes", NULL);
...
file = debugfs_create_file("list", 0444, dir, NULL,
&debugfs_kprobes_operations);
...
return 0;
}
...
#endif /* CONFIG_DEBUG_FS */

debugfs_create_diris used to create a new directory, anddebugfs_create_fileestablishes a new file
in this directory.debugfs_kprobes_operationswas discussed above as an example for the sequential
file mechanism.

Programming Interface


Since the debugfs code is very clean, simple, and well documented, it is not necessary to add remarks
about the implementation. It suffices to discuss the programming interface. However, have a look at the
source code, which is a very nice application of the libfs routines.

Three functions are available to create new filesystem objects:

<debugfs.h>
struct dentry *debugfs_create_file(const char *name, mode_t mode,
struct dentry *parent, void *data,
const struct file_operations *fops);

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