UFTRACE-SCRIPT(1) | UFTRACE-SCRIPT(1) |
uftrace-script - Run a script for recorded function trace
uftrace script (-S|–script) [options] uftrace script (-S|–script)
[options] –record COMMAND
This command runs a script for trace data recorded using the uftrace-record(1) command.
The uftrace tool supports script execution for each function entry and exit. The supported script types are Python 2.7, Python 3 and Lua 5.1 as of now.
The user can write five functions. `uftrace_entry' and `uftrace_exit' are executed whenever each function is executed at the entry and exit. However `uftrace_begin' and `uftrace_end' are only executed once when the target program begins and ends. `uftrace_event' is called when it sees an event.
$ cat scripts/simple.py def uftrace_begin(ctx): print("program begins...") def uftrace_entry(ctx): func = ctx["name"] print("entry : " + func + "()") def uftrace_exit(ctx): func = ctx["name"] print("exit : " + func + "()") def uftrace_event(ctx): name = ctx["name"] print("event : " + name) def uftrace_end(): print("program is finished")
The `ctx' variable is a dictionary type that contains the below information.
/* context information passed to uftrace_entry(ctx) and uftrace_exit(ctx) */ script_context = { int tid; int depth; long timestamp; long duration; # exit only long address; string name; list args; # entry only (if available) value retval; # exit only (if available) }; /* context information passed to uftrace_begin(ctx) */ script_context = { bool record; # True if it runs at record time, otherwise False string version; # uftrace version info list cmds; # execution commands };
The above script can be executed while reading the recorded data. The usage is as follows:
$ uftrace record -F main tests/t-abc $ uftrace script -S scripts/simple.py program begins... entry : main() entry : a() entry : b() entry : c() entry : getpid() exit : getpid() exit : c() exit : b() exit : a() exit : main() program is finished
The below is another example that shows the different output compared to previous one for the same recorded data. The output looks similar to uftrace replay this time.
$ uftrace script -S scripts/replay.py # DURATION TID FUNCTION [25794] | main() { [25794] | a() { [25794] | b() { [25794] | c() { [25794] | getpid() { 11.037 us [25794] | } /* getpid */ 44.752 us [25794] | } /* c */ 70.924 us [25794] | } /* b */ 98.191 us [25794] | } /* a */ 124.329 us [25794] | } /* main */
The script above can be modified to do more output customization.
A script can have an optional “UFTRACE_FUNCS” list which can have name (or pattern depending on the –match option) of functions to run the script. If it exists, only matched functions will run the script. For example, if you add following lines to the script, it will run only for functions with a single letter name.
$ echo 'UFTRACE_FUNCS = [ "^.$" ]' >> replay.py $ uftrace script -S replay.py # DURATION TID FUNCTION [25794] | a() { [25794] | b() { [25794] | c() { 44.752 us [25794] | } /* c */ 70.924 us [25794] | } /* b */ 98.191 us [25794] | } /* a */
Also a script can have options for record if it requires some form of data (i.e. function argument or return value). A comment line started with “uftrace-option:” will provide (a part of) such options when recording.
$ cat arg.py # # uftrace-option: -A a@arg1 -R b@retval # def uftrace_entry(ctx): if "args" in ctx: print(ctx["name"] + " has args") def uftrace_exit(ctx): if "retval" in ctx: print(ctx["name"] + " has retval") $ uftrace record -S arg.py abc a has args b has retval $ uftrace script -S arg.py a has args b has retval
Also a script can handle event records and optional event data in “args”. The argument is a string containing KEY=VALUE pairs. Currently it only works with uftrace script and not for record.
$ cat event.py def uftrace_entry(ctx): pass def uftrace_exit(ctx): pass def uftrace_event(ctx): if "args" in ctx: print(ctx["name"] + " ::: " + ctx["args"]) else: print(ctx["name"]) $ uftrace record -T a@read=proc/statm abc $ uftrace script -S event.py read:proc/statm ::: vmsize=31060KB vmrss=15412KB shared=11064KB diff:proc/statm ::: vmsize=+0KB vmrss=+0KB shared=+0KB
uftrace(1), uftrace-record(1), uftrace-replay(1), uftrace-live(1)
Honggyu Kim <honggyu.kp@gmail.com>, Namhyung Kim <namhyung@gmail.com>.
Sep, 2018 | Uftrace User Manuals |