|home| |posts| |projects| |cv| |bookmarks| |github|

Function tracing with Linux perf

Define your probe(s):

sudo perf probe -x ./your_exe "your_function"

One usefull thing is to a probe also at the return of the function:

sudo perf probe -x ./your_exe "your_function%return"

Run your exe and record traces:

sudo perf record -e "probe_your_exe:*" ./your_exe

Print the results:

sudo perf script

Your can add extra options, for example print only time and event name and print also nanseconds --ns and print time relative --reltime to trace start not as timestamp:

sudo perf script -F time,event --ns --reltime

When done, you can delete your probes:

sudo perf probe --del "probe_your_exe:*"

More info about the probe syntax can be found in man perf-probe.

Process traces with python script:

Generate starter script:

sudo perf script -g python

And edit it at will.

For example, to print the event name and the timestamp:

def trace_unhandled(event_name, context, event_fields_dict, perf_sample_dict):
    print("%u.%u %s" % (event_fields_dict["common_s"], event_fields_dict["common_ns"], event_name))

And run it with:

sudo perf script -s perf-script.py

See more info man perf-script-python.