【问题标题】:How Can I Count malloc in linux kernel with kprobe如何使用 kprobe 计算 linux 内核中的 malloc
【发布时间】:2013-01-06 17:34:25
【问题描述】:

我想在fedora 中用Kprobe 计算malloc 系统调用。 我知道malloc 不是系统调用,是在用户空间中实现的,但如果可能的话,我想用 kprobe 来计算 malloc。

我必须给 Kprobe 的系统调用的名称是什么? 以 do_work 为例:

kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork");

【问题讨论】:

  • malloc不是系统调用。在 GNU Glibc 库中,它是使用 mmapsbrk 系统调用实现的。也许您想在您的用户应用程序上使用valgrind....内核本身正在使用kmalloc 和相关的内核函数分配动态内存(在内核内部,而不是为应用程序)。
  • 您也可以使用pmap,对于pid 1234的进程,查看/proc/1234/status/proc/1234/maps等...

标签: linux-kernel malloc system-calls fedora kprobe


【解决方案1】:

这对于 kprobes 是不可能的,因为正如你所说,malloc 不是系统调用。

但是,您可以使用 USDT 来跟踪用户空间进程。 The bcc tools 包含一个带有 uobjnew 的示例。它跟踪给定进程中的对象分配:

$ ./uobjnew -h
usage: uobjnew.py [-h] [-l {java,ruby,c}] [-C TOP_COUNT] [-S TOP_SIZE] [-v]
                  pid [interval]

Summarize object allocations in high-level languages.

positional arguments:
  pid                   process id to attach to
  interval              print every specified number of seconds

optional arguments:
  -h, --help            show this help message and exit
  -l {java,ruby,c}, --language {java,ruby,c}
                        language to trace
  -C TOP_COUNT, --top-count TOP_COUNT
                        number of most frequently allocated types to print
  -S TOP_SIZE, --top-size TOP_SIZE
                        number of largest types by allocated bytes to print
  -v, --verbose         verbose mode: print the BPF program (for debugging
                        purposes)

examples:
    ./uobjnew -l java 145         # summarize Java allocations in process 145
    ./uobjnew -l c 2020 1         # grab malloc() sizes and print every second
    ./uobjnew -l ruby 6712 -C 10  # top 10 Ruby types by number of allocations
    ./uobjnew -l ruby 6712 -S 10 # top 10 Ruby types by total size

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 2013-05-27
    • 2020-08-31
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多