【问题标题】:How to access Linux kernel data structures?如何访问 Linux 内核数据结构?
【发布时间】:2015-09-16 13:05:57
【问题描述】:

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

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

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

任务的伪代码如下:

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.

【问题讨论】:

  • 你不能使用current宏吗?
  • 请看看我刚才添加的伪代码有问题。我猜“当前”将提供对当前流程数据结构的访问权限。
  • 请看下面答案中的示例是否满足用例,我认为可以。
  • 是的,但是如果我想访问其他进程数据结构,它会起作用吗?虽然我会尝试那个例子。
  • 当前宏查看当前进程的内核栈地址,然后切断最后 13 位以获得 PCB(进程的 task_struct)地址,因为它们都“活”在同一个 8K 中.其他进程的 task_structs 是可访问的,例如按 PID(请参阅 this question and answer)。

标签: linux-kernel kernel kernel-module


【解决方案1】:

请查看this 示例。

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多