參考資訊:
https://www.brendangregg.com/perf.html
https://docs.kernel.org/trace/uprobetracer.html
https://bristot.me/using-perf-probe-to-measure-execution-time-of-user-space-code-on-linux/
main.c
#include <stdio.h> void test(void) { printf("hello\n"); } int main(int argc, char *argv[]) { test(); return 0; }
編譯
$ gcc main.c -o main -ggdb -O0
Probe方式1
$ sudo perf probe --line test -x ./main <test@main.c:0> 0 void test(void) { 2 printf("hello\n"); 3 } $ sudo perf probe -x ./main 'myprobe=test:0' $ sudo perf stat -e probe_main:* ./main Performance counter stats for './main': 1 probe_main:myprobe $ sudo perf probe --del probe_main:* Removed event: probe_main:myprobe
Probe方式2
$ nm main | grep test 0000000000001139 T test $ sudo perf probe -x ./main -a myprobe=0x1139 $ sudo perf stat -e probe_main:* ./main Performance counter stats for './main': 1 probe_main:myprobe $ sudo perf probe --del probe_main:* Removed event: probe_main:myprobe
Probe方式3
$ nm main | grep test 0000000000001139 T test $ su # echo 'p:myprobe ./main:0x1139' > /sys/kernel/tracing/uprobe_events # echo 1 > /sys/kernel/tracing/events/uprobes/myprobe/enable # ./main hello # tail /sys/kernel/tracing/trace # # _-----=> irqs-off/BH-disabled # / _----=> need-resched # | / _---=> hardirq/softirq # || / _--=> preempt-depth # ||| / _-=> migrate-disable # |||| / delay # TASK-PID CPU# ||||| TIMESTAMP FUNCTION # | | | ||||| | | main-41148 [000] DNZff 10856.113989: myprobe: (0x55fc66688139) # cat /sys/kernel/tracing/uprobe_events p:uprobes/myprobe ./main:0x0000000000001139 # echo 0 > /sys/kernel/tracing/events/uprobes/myprobe/enable # echo "" > /sys/kernel/tracing/uprobe_events