2013-01-23 29 views
1

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

我必须给Kprobe系统调用的名称是什么? 例如,对于do_work:

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

'malloc'是*不*系统调用。在GNU Glibc库中,它使用'mmap'和'sbrk'系统调用来实现。也许你想在你的用户应用程序中使用'valgrind' ....内核本身使用'kmalloc'和相关的内核函数来分配动态内存(在内核中,而不是在应用程序中)。 –

+0

你也可以使用'pmap',对于pid 1234的进程,查看'/ proc/1234/status'和'/ proc/1234/maps'等等...... –

回答

0

这是不可能的使用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