Search code examples
clinuxlinux-kernelkernel

Using DebugFS with Strings in the Linux Kernel


I am developing a Linux kernel module which uses DebugFS to read some values from files in userspace into kernel space.

So far, I have my DebugFS directory at /sys/kernel/debug/example. This directory contains a set of files bound to u32_t variables. I can read values from these files and assign them to u32_t variables in my kernel module.

I want to read some ASCII characters from a DebugFS file and assign it to a char[] of arbitrary length. However (as far as I have seen), the DebugFS documentation does not contain any functions specifically for this purpose.

Current code creates the file "example" at /sys/kernel/debug/ and binds hello to whatever unsigned 32-bit integer is in that file:


static u32 hello = 0;

int init_module(void)
{
    struct dentry *junk;

    dir = debugfs_create_dir("example", 0);
    if (!dir) {
        printk(KERN_ALERT "Failed to create /sys/kernel/debug/example\n");
        return -1;
    }

    junk = debugfs_create_u32("hello", 0666, dir, &hello);
    if (!junk) {
        printk(KERN_ALERT "Failed to create /sys/kernel/debug/example/hello\n");
        return -1;
    }

    return 0;
}

void cleanup_module(void)
{
    debugfs_remove_recursive(dir);
}

Solution

  • Solved. Refer to the link I put in the comments (https://janczer.github.io/create-simple-debugfs-object/), the content in the file "data_file" is assigned to char data[].