2013-01-23 31 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