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