【问题标题】:Linux kernel function call flowLinux内核函数调用流程
【发布时间】:2020-03-16 17:10:21
【问题描述】:

有没有办法知道在某个事件期间调用了哪些内核函数。

例如,如果我按下键盘上的任何键,我想知道所有内核函数和设备驱动程序函数被调用 - 在字符出现在屏幕上之前(对应于我在键盘上键入的键的字符) .

我想将完整的流程转储到某个地方并稍后检查。我说的是内核空间函数,而不是用户空间函数。

【问题讨论】:

  • 对于 x86 机器,关键字是 Intel Processor Tracing,对于 ARM -- Coresight。对于一般的 Linux 内核 -- perf.

标签: c linux-kernel linux-device-driver stack-trace perf


【解决方案1】:

我想知道所有内核函数和设备驱动程序函数都被调用了

显然,跟踪的完整性取决于它的开始位置。这取决于你。

您可以通过方便的trace-cmd 追踪您想要的内容。例如。您可以获得函数图。首先,您需要根据自己的需要确定一些切入点。
如果是关于键盘按键的,你需要找到它的驱动和一些与之相关的功能。

示例:
经典 AT 和 PS/2 键盘驱动程序atkbd 具有中断功能atkbd_interrupt。我们来看看有没有这样的trace点:

trace-cmd list -f | grep atkbd_int

然后开始录制:

trace-cmd record -p function_graph -g atkbd_interrupt &

按一些键并停止录制:fg 然后Ctrl+C。现在可以获取函数图了:

trace-cmd report | vim -

应该是这样的:

CPU 1 is empty
CPU 2 is empty
CPU 3 is empty
cpus=4
          <idle>-0     [000] 1095787.266859: funcgraph_entry:                   |  atkbd_interrupt() {
          <idle>-0     [000] 1095787.266863: funcgraph_entry:                   |    input_event() {
          <idle>-0     [000] 1095787.266864: funcgraph_entry:        0.215 us   |      _raw_spin_lock_irqsave();
          <idle>-0     [000] 1095787.266866: funcgraph_entry:        0.386 us   |      input_handle_event();
          <idle>-0     [000] 1095787.266867: funcgraph_entry:        0.163 us   |      _raw_spin_unlock_irqrestore();
          <idle>-0     [000] 1095787.266868: funcgraph_exit:         3.882 us   |    }
          <idle>-0     [000] 1095787.266869: funcgraph_entry:                   |    input_event() {
          <idle>-0     [000] 1095787.266869: funcgraph_entry:        0.123 us   |      _raw_spin_lock_irqsave();
          <idle>-0     [000] 1095787.266870: funcgraph_entry:                   |      input_handle_event() {
          <idle>-0     [000] 1095787.266871: funcgraph_entry:                   |        add_input_randomness() {
          <idle>-0     [000] 1095787.266871: funcgraph_entry:                   |          add_timer_randomness() {
          <idle>-0     [000] 1095787.266872: funcgraph_entry:                   |            mix_pool_bytes() {
          <idle>-0     [000] 1095787.266872: funcgraph_entry:        0.327 us   |              _raw_spin_lock_irqsave();
          <idle>-0     [000] 1095787.266873: funcgraph_entry:        0.877 us   |              _mix_pool_bytes();
          <idle>-0     [000] 1095787.266875: funcgraph_entry:        0.163 us   |              _raw_spin_unlock_irqrestore();
          <idle>-0     [000] 1095787.266876: funcgraph_exit:         3.628 us   |            }
          <idle>-0     [000] 1095787.266876: funcgraph_entry:                   |            credit_entropy_bits() {
          <idle>-0     [000] 1095787.266877: funcgraph_entry:                   |              __wake_up() {
          <idle>-0     [000] 1095787.266877: funcgraph_entry:        0.229 us   |                _raw_spin_lock_irqsave();
          <idle>-0     [000] 1095787.266878: funcgraph_entry:        0.120 us   |                __wake_up_common();
          <idle>-0     [000] 1095787.266879: funcgraph_entry:        0.135 us   |                _raw_spin_unlock_irqrestore();
          <idle>-0     [000] 1095787.266880: funcgraph_exit:         2.719 us   |              }
          <idle>-0     [000] 1095787.266880: funcgraph_entry:        0.108 us   |              kill_fasync();
          <idle>-0     [000] 1095787.266881: funcgraph_exit:         4.833 us   |            }
          <idle>-0     [000] 1095787.266882: funcgraph_exit:       + 10.249 us  |          }
          <idle>-0     [000] 1095787.266882: funcgraph_exit:       + 11.186 us  |        }
          <idle>-0     [000] 1095787.266883: funcgraph_entry:        0.237 us   |        atkbd_event();
          <idle>-0     [000] 1095787.266884: funcgraph_exit:       + 13.347 us  |      }
          <idle>-0     [000] 1095787.266884: funcgraph_entry:        0.138 us   |      _raw_spin_unlock_irqrestore();
          ........                                                         

例如,这只是一小段痕迹。我是在我的 qemu VM 中完成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2011-05-07
    • 2021-03-14
    • 2012-07-21
    • 2014-01-07
    • 2012-11-16
    • 2012-02-28
    • 1970-01-01
    相关资源
    最近更新 更多