systemtap 打印进程的所有内核函数

systemtap 打印进程的所有内核函数

a.out当进程进入内核模式时,如何打印与进程关联的所有内核函数。 IE;我想过滤掉特定 pid/execname 的内核函数。以下是我创建的一个脚本,用于跟踪与进程在内核模式下执行时stap相关的内核函数。这对我来说永远不会成功,我想这是由于试图跟踪的mm符号量所致。stap

root@test:~/systemtap# cat test9.stp
#! /usr/bin/env stap

global traces

probe kernel.function("*@mm/*.c") {
  traces[pid(), pexecname(), backtrace()] ++
}
probe end {
  foreach ([pid, name, stack] in traces-) {
    printf ("traces[%d,%s,\n", pid, name)
    print_stack (stack)
    printf ("] = %d\n", traces[pid, name, stack]);
  }
}
root@test:~/systemtap# stap test9.stp
WARNING: probe kernel.function("is_errata93@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:417") (address 0xffffffffb329ebd4) registration error [man warning::pass5] (rc -22)
WARNING: probe kernel.function("do_sigbus@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:934") (address 0xffffffffb329ef28) registration error [man warning::pass5] (rc -22)
WARNING: probe kernel.function("spurious_kernel_fault@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:1007") (address 0xffffffffb329ef40) registration error [man warning::pass5] (rc -22)
WARNING: probe kernel.function("exc_page_fault@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:1497") (address 0xffffffffb3f9e780) registration error [man warning::pass5] (rc -22)
WARNING: probe kernel.function("do_user_addr_fault@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:1220") (address 0xffffffffb329e8d0) registration error [man warning::pass5] (rc -22)
WARNING: probe kernel.function("do_kern_addr_fault@/build/linux-pm2SeW/linux-5.15.0/arch/x86/mm/fault.c:1147") (address 0xffffffffb329f170) registration error [man warning::pass5] (rc -22)
WARNING: Missing unwind data for a module, rerun with 'stap -d kernel'
WARNING: too many pending (warning) messages
ERROR: too many pending (error) messages
WARNING: Number of errors: 1, skipped probes: 1891693
WARNING: /usr/bin/staprun exited with status: 1
Pass 5: run failed.  [man error::pass5]
Number of similar warning messages suppressed: 38.
Rerun with -v to see them.

答案1

systemtap有以下标志:

   -x PID Sets target() to PID. This allows scripts to  be  written  that  filter  on  a  specific
          process. Scripts run independent of the PID's lifespan.

如果您使用标志运行脚本-x <pid>,则target()在脚本内部调用将返回您提供的 pid。

stap test9.stp -x <pid>

然后您可以在脚本中按以下方式使用它:

probe kernel.function("*@mm/*.c") {
  if (pid() == target()) {
    traces[pid(), pexecname(), backtrace()] ++
  }
}

相关内容