如何使用kprobe在linux内核中计算malloc [英] How Can I Count malloc in linux kernel with kprobe

查看:139
本文介绍了如何使用kprobe在linux内核中计算malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to count the malloc system call with Kprobe in fedora. I know that malloc is not a system call and is implemented in user space, but I want to count malloc with kprobe if its possible.

我必须给Kprobe打电话的系统名称是什么? 例如do_work:

What is the name of system call that I must give to Kprobe? For example for do_work:

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

推荐答案

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

This is not possible with kprobes because, as you said, malloc is not a system call.

但是,您可以使用USDT跟踪用户空间进程. bcc工具包含一个带有

You can, however, use USDTs to trace userspace processes. The bcc tools contain an example with uobjnew. It traces object allocations in the given process:

$ ./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

这篇关于如何使用kprobe在linux内核中计算malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆