這是不可能的使用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
'malloc'是*不*系統調用。在GNU Glibc庫中,它使用'mmap'和'sbrk'系統調用來實現。也許你想在你的用戶應用程序中使用'valgrind' ....內核本身使用'kmalloc'和相關的內核函數來分配動態內存(在內核中,而不是在應用程序中)。 –
你也可以使用'pmap',對於pid 1234的進程,查看'/ proc/1234/status'和'/ proc/1234/maps'等等...... –