我正在开发一个 Linux 内核模块,它使用 DebugFS 将用户空间文件中的一些值读取到内核空间。
到目前为止,我的 DebugFS 目录位于
/sys/kernel/debug/example
。该目录包含一组绑定到 u32_t 变量的文件。我可以从这些文件中读取值并将它们分配给我的内核模块中的 u32_t 变量。
我想从 DebugFS 文件中读取一些 ASCII 字符并将其分配给任意长度的 char[] 。然而(据我所知),DebugFS 文档不包含任何专门用于此目的的函数。
当前代码在
/sys/kernel/debug/
创建文件“example”,并将 hello
绑定到该文件中的任何无符号 32 位整数:
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);
}
解决了。参考我在评论中放置的链接(https://janczer.github.io/create-simple-debugfs-object/),文件“data_file”中的内容被分配给char data[]。