如何访问Linux内核数据结构?

时间:2015-09-16 13:05:58

标签: linux-kernel kernel kernel-module

我想打印每个进程的信息以及该进程在运行时正在做什么。即,该过程连续读/写哪个文件。

为此,我正在编写一个内核模块。

任何人都知道如何在内核模块中访问此信息或如何访问内核模块中的进程表数据结构?

任务的伪代码将是这样的:

1. get each process from /proc.
2. Access the data structure of that process i.e. process table and all
3. print what that process is doing i.e. which file it is accessing (i.e. reading or writing) at rutime.

2 个答案:

答案 0 :(得分:0)

请查看this示例。

它专门展示了如何创建一个打印进程的打开文件的内核模块(并依赖于我在我提到的当前宏中获得的 task_struct 结构)评论)。这可以被操作到更复杂的东西,可以通过进程 task_struct struct访问。

答案 1 :(得分:0)

for_each_process

中声明了一个名为/include/linux/sched.h的宏

http://lxr.free-electrons.com/source/include/linux/sched.h#L2621

通过使用此宏,可以遍历所有进程的task_struct
http://lxr.free-electrons.com/source/include/linux/sched.h#L1343