.\" Automatically generated by Pandoc 2.17.1.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. .ie "\f[CB]x\f[]"x" \{\ . ftr V B . ftr VI BI . ftr VB B . ftr VBI BI .\} .el \{\ . ftr V CR . ftr VI CI . ftr VB CB . ftr VBI CBI .\} .TH "UFTRACE-RECORD" "1" "Sep, 2018" "Uftrace User Manuals" "" .hy .SH NAME .PP uftrace-record - Run a command and record its trace data .SH SYNOPSIS .PP uftrace record [\f[I]options\f[R]] COMMAND [\f[I]command-options\f[R]] .SH DESCRIPTION .PP This command runs COMMAND and gathers function trace data from it, and saves it into files under the uftrace data directory - without displaying anything. .PP This data can then be inspected later on, using \f[V]uftrace replay\f[R] or \f[V]uftrace report\f[R]. .SH RECORD OPTIONS .TP -A \f[I]SPEC\f[R], --argument=\f[I]SPEC\f[R] Record function arguments. This option can be used more than once. Implies --srcline. See \f[I]ARGUMENTS\f[R]. .TP -R \f[I]SPEC\f[R], --retval=\f[I]SPEC\f[R] Record function return values. This option can be used more than once. Implies --srcline. See \f[I]ARGUMENTS\f[R]. .TP -P \f[I]FUNC\f[R], --patch=\f[I]FUNC\f[R] Patch FUNC dynamically. This option can be used more than once. See \f[I]DYNAMIC TRACING\f[R]. .TP -U \f[I]FUNC\f[R], --unpatch=\f[I]FUNC\f[R] Do not apply dynamic patching for FUNC. This option can be used more than once. See \f[I]DYNAMIC TRACING\f[R]. .TP -E \f[I]EVENT\f[R], --event=\f[I]EVENT\f[R] Enable event tracing. The event should be available on the system. .TP -S \f[I]SCRIPT_PATH\f[R], --script=\f[I]SCRIPT_PATH\f[R] Run a given script to do additional work at the entry and exit of function during target program execution. The type of script is detected by the postfix such as `.py' for python. See \f[I]SCRIPT EXECUTION\f[R]. .TP -W, --watch=\f[I]POINT\f[R] Add watch point to display POINT if the value is changed. See \f[I]WATCH POINT\f[R]. .TP -a, --auto-args Automatically record arguments and return values of known functions. These are usually functions in standard (C language or system) libraries but if debug info is available it includes functions in the user program. Implies --srcline. .TP -l, --nest-libcall Trace function calls between libraries. By default, uftrace only record library call from the main executable. Implies \f[V]--force\f[R]. .TP -k, --kernel Trace kernel functions as well as user functions. Only kernel entry/exit functions will be traced by default. Use the \f[V]--kernel-depth\f[R] option to override this. .TP -K \f[I]DEPTH\f[R], --kernel-depth=\f[I]DEPTH\f[R] Set kernel max function depth separately. Implies \f[V]--kernel\f[R]. .TP --clock=\f[I]CLOCK\f[R] Set clock source for timestamp recording. \f[I]CLOCK\f[R] can be one of `mono', `mono_raw', or `boot'. Default is `mono'. .TP --host=\f[I]HOST\f[R] Send trace data to given host via the network, not writing to files. The \f[V]uftrace recv\f[R] command should be run on the destination host to receive the data. .TP --port=\f[I]PORT\f[R] When sending data to the network (with \f[V]--host\f[R]), use the given port instead of the default (8090). .TP --signal=\f[I]TRG\f[R] Set trigger on selected signals rather than functions. But there are restrictions so only a few of trigger actions are support for signals. The available actions are: trace_on, trace_off, finish. This option can be used more than once. See \f[I]TRIGGERS\f[R]. .TP --nop Do not record any functions. This is a no-op and only meaningful for performance comparisons. .TP --force Allow running uftrace even if some problems occur. When \f[V]uftrace record\f[R] finds no mcount symbol (which is generated by compiler) in the executable, it quits with an error message since uftrace can not trace the program. However, it is possible that the user is only interested in functions within a dynamically-linked library, in which case this option can be used to cause uftrace to run the program regardless. Also, the \f[V]-A\f[R]/\f[V]--argument\f[R] and \f[V]-R\f[R]/\f[V]--retval\f[R] options work only for binaries built with \f[V]-pg\f[R], so uftrace will normally exit when it tries to run binaries built without that option. This option ignores the warning and goes on tracing without the argument and/or return value. .TP --time Print running time of children in \f[V]time\f[R](1)-style. .TP -e, --estimate-return Record only ENTRY data for each function. This option is useful when the target program deals with stack in some way. Normally uftrace modifies task\[cq]s execution stack frame to hook return from the function. However sometimes it makes troubles and it\[cq]s hard to handle all the cases properly. This option tells uftrace not to hook return address in order to prevent those problems. The return time is estimated as a half of execution time of two consecutive functions. .SH COMMON OPTIONS .TP -F \f[I]FUNC\f[R], --filter=\f[I]FUNC\f[R] Set filter to trace selected functions and their children functions. This option can be used more than once. See \f[I]FILTERS\f[R]. .TP -N \f[I]FUNC\f[R], --notrace=\f[I]FUNC\f[R] Set filter not to trace selected functions and their children functions. This option can be used more than once. See \f[I]FILTERS\f[R]. .TP -C \f[I]FUNC\f[R], --caller-filter=\f[I]FUNC\f[R] Set filter to trace callers of selected functions only. This option can be used more than once. See \f[I]FILTERS\f[R]. .TP -T \f[I]TRG\f[R], --trigger=\f[I]TRG\f[R] Set trigger on selected functions. This option can be used more than once. See \f[I]TRIGGERS\f[R]. .TP -D \f[I]DEPTH\f[R], --depth=\f[I]DEPTH\f[R] Set global trace limit in nesting level. See \f[I]FILTERS\f[R]. .TP -t \f[I]TIME\f[R], --time-filter=\f[I]TIME\f[R] Do not show functions which run under the time threshold. If some functions explicitly have the `trace' trigger applied, those are always traced regardless of execution time. See \f[I]FILTERS\f[R]. .TP -Z \f[I]SIZE\f[R], --size-filter=\f[I]SIZE\f[R] Do not show functions smaller than SIZE bytes. See \f[I]FILTERS\f[R]. .TP -L \f[I]LOCATION\f[R], --loc-filter=\f[I]LOCATION\f[R] Set filter to trace selected source locations. This option can be used more than once. Implies \[en]srcline. See \f[I]FILTERS\f[R]. .TP --no-libcall Do not record library function invocations. Library calls are normally traced by hooking calls to the resolver function of dynamic linker in the PLT. One can disable it with this option. .TP --no-event Disable event recording which is used by default. Note that explicit event tracing by \f[V]--event\f[R] option is not affected by this. Implies \f[V]--no-sched\f[R]. .TP --no-sched Disable schedule event recording which is used by default. .TP --match=\f[I]TYPE\f[R] Use pattern match using TYPE. Possible types are \f[V]regex\f[R] and \f[V]glob\f[R]. Default is \f[V]regex\f[R]. .TP --disable Start uftrace with tracing disabled. This is only meaningful when used with a \f[V]trace_on\f[R] trigger. .TP --with-syms=\f[I]DIR\f[R] Read symbol data from the .sym files in \f[I]DIR\f[R] directory instead of the binary. This can be useful to deal with stripped binaries. The file name of the main binary should be the same when saved and used. .SH RECORD CONFIG OPTIONS .TP --libmcount-path=\f[I]PATH\f[R] Load libmcount libraries from this path. This is mostly for testing purposes. .TP -b \f[I]SIZE\f[R], --buffer=\f[I]SIZE\f[R] Size of internal buffer in which trace data will be saved. Default size is 128k. .TP --kernel-buffer=\f[I]SIZE\f[R] Set kernel tracing buffer size. The default value (in the kernel) is 1408k. .TP --no-pltbind Do not bind dynamic symbol address. This option uses the \f[V]LD_BIND_NOT\f[R] environment variable to trace library function calls which might be missing due to concurrent (first) accesses. It is not meaningful to use this option with the \f[V]--no-libcall\f[R] option. .TP --max-stack=\f[I]DEPTH\f[R] Set the max function stack depth for tracing. Default is 1024. .TP --num-thread=\f[I]NUM\f[R] Use NUM threads to record trace data. Default is 1/4 of online CPUs (but when full kernel tracing is enabled, it will use the full number of CPUs). .TP --libmcount-single Use single thread version of libmcount for faster recording. This is ignored if the target program links with the pthread library. .TP --rt-prio=\f[I]PRIO\f[R] Boost priority of recording threads to real-time (FIFO) with priority of \f[I]PRIO\f[R]. This is particularly useful for high-volume data such as full kernel tracing. .TP --keep-pid Retain same pid for traced program. For some daemon processes, it is important to have same pid when forked. Running under uftrace normally changes pid as it calls fork() again internally. .TP --no-randomize-addr Disable ASLR (Address Space Layout Randomization). It makes the target process fix its address space layout. .TP --srcline Enable recording source line in the debug info. .SH FILTERS .PP The uftrace tool supports filtering out uninteresting functions. Filtering is highly recommended since it helps users focus on the interesting functions and reduces the data size. When uftrace is called it receives two types of function filter; an opt-in filter with \f[V]-F\f[R]/\f[V]--filter\f[R] and an opt-out filter with \f[V]-N\f[R]/\f[V]--notrace\f[R]. These filters can be applied either at record time or replay time. .PP The first one is an opt-in filter. By default, it doesn\[cq]t trace anything. But when one of the specified functions is executed, tracing is started. When the function returns, tracing is stopped again. .PP For example, consider a simple program which calls \f[V]a()\f[R], \f[V]b()\f[R] and \f[V]c()\f[R] in turn. .IP .nf \f[C] $ cat abc.c void c(void) { /* do nothing */ } void b(void) { c(); } void a(void) { b(); } int main(void) { a(); return 0; } $ gcc -pg -o abc abc.c \f[R] .fi .PP Normally uftrace will trace all the functions from \f[V]main()\f[R] to \f[V]c()\f[R]. .IP .nf \f[C] $ uftrace record ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main() { [ 1234] | a() { [ 1234] | b() { 3.880 us [ 1234] | c(); 5.475 us [ 1234] | } /* b */ 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP But when the \f[V]-F b\f[R] filter option is used, it will not trace \f[V]main()\f[R] or \f[V]a()\f[R] but only \f[V]b()\f[R] and \f[V]c()\f[R]. .IP .nf \f[C] $ uftrace record -F b ./abc $ uftrace replay # DURATION TID FUNCTION [ 1234] | b() { 3.880 us [ 1234] | c(); 5.475 us [ 1234] | } /* b */ \f[R] .fi .PP The second type of filter is opt-out. By default, everything is traced, but when one of the specified functions is executed, tracing stops. When the excluded function returns, tracing is started again. .PP In the above example, you can omit the function \f[V]b()\f[R] and all calls it makes with the \f[V]-N\f[R] option. .IP .nf \f[C] $ uftrace record -N b ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main() { 6.448 us [ 1234] | a(); 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP If users only care about specific functions and want to know how they are called, one can use the caller filter. It makes the function as leaf and records the parent functions to the function. .IP .nf \f[C] $ uftrace record -C b ./abc $ uftrace replay # DURATION TID FUNCTION [ 1234] | main() { [ 1234] | a() { 5.475 us [ 1234] | b(); 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP In the above example, functions not in the calling path were not shown. Also the function `c' - which is a child of the function `b' - is also hidden. .PP In addition, you can limit the nesting level of functions with the \f[V]-D\f[R] option. .IP .nf \f[C] $ uftrace record -D 3 ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main() { [ 1234] | a() { 5.475 us [ 1234] | b(); 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP In the above example, uftrace only records functions up to a depth of 3, so leaf function \f[V]c()\f[R] was omitted. Note that the \f[V]-D\f[R] option works with \f[V]-F\f[R]. .PP Sometimes it\[cq]s useful to see long-running functions only. This is good because there are usually many tiny functions that are not interesting. The \f[V]-t\f[R]/\f[V]--time-filter\f[R] option implements the time-based filter that only records functions which run longer than the given threshold. In the above example, the user might want to see functions running more than 5 micro-seconds like below: .IP .nf \f[C] $ uftrace record -t 5us ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main() { [ 1234] | a() { 5.475 us [ 1234] | b(); 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP The \f[V]-t\f[R]/\f[V]--time-filter\f[R] option works for user-level functions only. It does not work for recording kernel functions, but they can be hidden in replay, report, dump and graph commands with \f[V]-t\f[R]/\f[V]--time-filter\f[R] option. .PP In addition, you can set filter to record selected source locations with \f[V]-L\f[R] option. .IP .nf \f[C] $ uftrace record -L s-libmain.c t-lib $ uftrace replay --srcline # DURATION TID FUNCTION [SOURCE] [ 5043] | main() { /* /home/uftrace/tests/s-libmain.c:16 */ 6.998 us [ 5043] | foo(); /* /home/uftrace/tests/s-libmain.c:11 */ 9.393 us [ 5043] | } /* main */ \f[R] .fi .PP You can set filter with the \f[V]\[at]hide\f[R] suffix not to record selected source locations. .IP .nf \f[C] $ uftrace record -L s-libmain.c\[at]hide t-lib $ uftrace replay --srcline # DURATION TID FUNCTION [SOURCE] [ 14688] | lib_a() { /* /home/uftrace/tests/s-lib.c:10 */ [ 14688] | lib_b() { /* /home/uftrace/tests/s-lib.c:15 */ 1.505 us [ 14688] | lib_c(); /* /home/uftrace/tests/s-lib.c:20 */ 2.816 us [ 14688] | } /* lib_b */ 3.181 us [ 14688] | } /* lib_a */ \f[R] .fi .PP The \f[V]-Z\f[R]/\f[V]--size-filter\f[R] option is to filter functions that has small sizes. It reads ELF symbols size and compare it with the given value. The PLT functions may have no symbol size in the ELF format, in that case the PLT entry size will be used as the size of the function. .IP .nf \f[C] $ uftrace record -Z 100 t-arg $ uftrace replay # DURATION TID FUNCTION [162500] | main() { 12.486 us [162500] | foo(); 0.505 us [162500] | many(); [162500] | pass() { 0.283 us [162500] | check(); 1.449 us [162500] | } /* pass */ 18.478 us [162500] | } /* main */ \f[R] .fi .PP You can also set triggers on filtered functions. See \f[I]TRIGGERS\f[R] section below for details. .PP When kernel function tracing is enabled, you can also set the filters on kernel functions by marking the symbol with the \f[V]\[at]kernel\f[R] modifier. The following example will show all user functions and the (kernel) page fault handler. .IP .nf \f[C] $ sudo uftrace -k -F \[aq].*page_fault\[at]kernel\[aq] ./abc # DURATION TID FUNCTION [14721] | main() { 7.713 us [14721] | __do_page_fault(); 6.600 us [14721] | __do_page_fault(); 6.544 us [14721] | __do_page_fault(); [14721] | a() { [14721] | b() { [14721] | c() { 0.860 us [14721] | getpid(); 2.346 us [14721] | } /* c */ 2.956 us [14721] | } /* b */ 3.340 us [14721] | } /* a */ 79.086 us [14721] | } /* main */ \f[R] .fi .SH TRIGGERS .PP The uftrace tool supports triggering actions on selected function calls (with or without filters) and/or signals. Currently supported triggers are listed below. The BNF for trigger specification is as follows: .IP .nf \f[C] := \[dq]\[at]\[dq] := | \[dq],\[dq] := \[dq]depth=\[dq] | \[dq]trace\[dq] | \[dq]trace_on\[dq] | \[dq]trace_off\[dq] | \[dq]time=\[dq] | \[dq]size=\[dq] | \[dq]read=\[dq] | \[dq]finish\[dq] | \[dq]filter\[dq] | \[dq]notrace\[dq] | \[dq]recover\[dq] \[dq]filter\[dq] | \[dq]notrace\[dq] | \[dq]recover\[dq] := \[dq]ns\[dq] | \[dq]nsec\[dq] | \[dq]us\[dq] | \[dq]usec\[dq] | \[dq]ms\[dq] | \[dq]msec\[dq] | \[dq]s\[dq] | \[dq]sec\[dq] | \[dq]m\[dq] | \[dq]min\[dq] := \[dq]proc/statm\[dq] | \[dq]page-fault\[dq] | \[dq]pmu-cycle\[dq] | \[dq]pmu-cache\[dq] | \[dq]pmu-branch\[dq] \f[R] .fi .PP The \f[V]depth\f[R] trigger is to change filter depth during execution of the function. It can be used to apply different filter depths for different functions. .PP The following example shows how triggers work. The global filter maximum depth is 5, but when function \f[V]b()\f[R] is called, it is changed to 1, so functions below \f[V]b()\f[R] will not be shown. .IP .nf \f[C] $ uftrace record -D 5 -T \[aq]b\[at]depth=1\[aq] ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main() { [ 1234] | a() { 5.475 us [ 1234] | b(); 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } /* main */ \f[R] .fi .PP The \f[V]backtrace\f[R] trigger is only meaningful in the replay command. .PP The \f[V]trace_on\f[R] and \f[V]trace_off\f[R] actions (the \f[V]_\f[R] can be omitted as \f[V]traceon\f[R] and \f[V]traceoff\f[R]) control whether uftrace records the specified functions or not. .PP The `recover' trigger is for some corner cases in which the process accesses the callstack directly. For now it\[cq]s not necessary to call it as uftrace does the job automatically. .PP The `time' trigger is to change time filter setting during execution of the function. It can be used to apply different time filter for different functions. .PP The \f[V]read\f[R] trigger is to read some information at runtime. The result will be recorded as (builtin) events at the beginning and the end of a given function. As of now, following events are supported: .IP \[bu] 2 \[lq]proc/statm\[rq]: process memory stat from /proc filesystem .IP \[bu] 2 \[lq]page-fault\[rq]: number of page faults using getrusage(2) .IP \[bu] 2 \[lq]pmu-cycle\[rq]: cpu cycles and instructions using Linux perf-event syscall .IP \[bu] 2 \[lq]pmu-cache\[rq]: (cpu) cache-references and misses using Linux perf-event syscall .IP \[bu] 2 \[lq]pmu-branch\[rq]: branch instructions and misses using Linux perf-event syscall .PP The results are printed as events (comments) like below. .IP .nf \f[C] $ uftrace record -T a\[at]read=proc/statm ./abc $ uftrace replay # DURATION TID FUNCTION [ 1234] | main() { [ 1234] | a() { [ 1234] | /* read:proc/statm (size=6808KB, rss=776KB, shared=712KB) */ [ 1234] | b() { [ 1234] | c() { 1.448 us [ 1234] | getpid(); 10.270 us [ 1234] | } /* c */ 11.250 us [ 1234] | } /* b */ [ 1234] | /* diff:proc/statm (size=+4KB, rss=+0KB, shared=+0KB) */ 18.380 us [ 1234] | } /* a */ 19.537 us [ 1234] | } /* main */ \f[R] .fi .PP The `finish' trigger is to end recording. The process still can run and this can be useful to trace unterminated processes like daemon. .PP The `filter' and `notrace' triggers have same effect as \f[V]-F\f[R]/\f[V]--filter\f[R] and \f[V]-N\f[R]/\f[V]--notrace\f[R] options respectively. .PP Triggers only work for user-level functions for now. .PP The trigger can be used for signals as well. This is done by signal trigger with --signal option. The syntax is similar to function trigger but only \[lq]trace_on\[rq], \[lq]trace_off\[rq] and \[lq]finish\[rq] trigger actions are supported. .IP .nf \f[C] $ uftrace record --signal \[aq]SIGUSR1\[at]finish\[aq] ./some-daemon \f[R] .fi .SH ARGUMENTS .PP The uftrace tool supports recording function arguments and/or return values using the -A/--argument and -R/--retval options respectively. The syntax is very similar to that of triggers: .IP .nf \f[C] := [ \[dq]\[at]\[dq] ] := | \[dq],\[dq] := ( | | ) := \[dq]arg\[dq] N [ \[dq]/\[dq] [ ] ] [ \[dq]%\[dq] ( | ) ] := \[dq]fparg\[dq] N [ \[dq]/\[dq] ( | \[dq]80\[dq] ) ] [ \[dq]%\[dq] ( | ) ] := \[dq]retval\[dq] [ \[dq]/\[dq] [ ] ] := \[dq]d\[dq] | \[dq]i\[dq] | \[dq]u\[dq] | \[dq]x\[dq] | \[dq]s\[dq] | \[dq]c\[dq] | \[dq]f\[dq] | \[dq]S\[dq] | \[dq]p\[dq] := \[dq]8\[dq] | \[dq]16\[dq] | \[dq]32\[dq] | \[dq]64\[dq] := # \[dq]rdi\[dq], \[dq]xmm0\[dq], \[dq]r0\[dq], ... := \[dq]stack\[dq] [ \[dq]+\[dq] ] \f[R] .fi .PP The -A/--argument option takes a symbol name pattern and its optional specs. The spec is started by argN where N is an index of the arguments. The index starts from 1 and corresponds to the argument passing order of the calling convention on the system. Note that the indexes of arguments are separately counted for integer (or pointer) and floating-point type, and they can interfere depending on the calling convention. The argN is for integer arguments and fpargN is for floating-point arguments. .PP Users can optionally specify a format and size for the arguments and/or return values. The \[lq]d\[rq] format or without format field, uftrace treats them as `long int' type for integers and `double' for floating-point numbers. The \[lq]i\[rq] format makes it signed integer type and \[lq]u\[rq] format is for unsigned type. Both are printed as decimal while \[lq]x\[rq] format makes it printed as hexadecimal. The \[lq]s\[rq] format is for null-terminated string type and \[lq]c\[rq] format is for character type. The \[lq]f\[rq] format is for floating-point type and is meaningful only for return value (generally). Note that fpargN doesn\[cq]t take the format field since it\[cq]s always floating-point. The \[lq]S\[rq] format is for std::string, but it only supports libstdc++ library as of yet. Finally, the \[lq]p\[rq] format is for function pointer. Once the target address is recorded, it will be displayed as function name. .PP Please beware when using string type arguments since it can crash the program if the (pointer) value is invalid. Actually uftrace tries to keep track of valid ranges of process address space but it might miss some corner cases. .PP It is also possible to specify a certain register name or stack offset for arguments (but not for return value). The following register names can be used for argument: .IP \[bu] 2 x86: rdi, rsi, rdx, rcx, r8, r9 (for integer), xmm[0-7] (for floating-point) .IP \[bu] 2 arm: r[0-3] (for integer), s[0-15] or d[0-7] (for floating-point) .PP Examples are below: .IP .nf \f[C] $ uftrace record -A main\[at]arg1/x -R main\[at]retval/i32 ./abc $ uftrace replay # DURATION TID FUNCTION 138.494 us [ 1234] | __cxa_atexit(); [ 1234] | main(0x1) { [ 1234] | a() { [ 1234] | b() { 3.880 us [ 1234] | c(); 5.475 us [ 1234] | } /* b */ 6.448 us [ 1234] | } /* a */ 8.631 us [ 1234] | } = 0; /* main */ $ uftrace record -A puts\[at]arg1/s -R puts\[at]retval ./hello Hello world $ uftrace replay # DURATION TID FUNCTION 1.457 us [21534] | __monstartup(); 0.997 us [21534] | __cxa_atexit(); [21534] | main() { 7.226 us [21534] | puts(\[dq]Hello world\[dq]) = 12; 8.708 us [21534] | } /* main */ \f[R] .fi .PP Note that these arguments and return value are recorded only if the executable was built with the \f[V]-pg\f[R] option. Executables built with \f[V]-finstrument-functions\f[R] will ignore it except for library calls. Recording of arguments and return values only works with user-level functions for now. .PP If the target program is built with debug info like DWARF, uftrace can identify number of arguments and their types automatically (when built with libdw). Also arguments and return value of some well-known library functions are provided even if the debug info is not available. In these cases user don\[cq]t need to specify spec of the arguments and return value manually - just a function name (or pattern) is enough. In fact, manual argspec will suppress the automatic argspec. .PP For example, the above example can be written like below: .IP .nf \f[C] $ uftrace record -A . -R main ./hello Hello world $ uftrace replay -F main # DURATION TID FUNCTION [ 18948] | main(1, 0x7ffeeb7590b8) { 7.183 us [ 18948] | puts(\[dq]Hello world\[dq]); 9.832 us [ 18948] | } = 0; /* main */ \f[R] .fi .PP Note that argument pattern (\[lq].\[rq]) matches to any character so it recorded all (supported) functions. It shows two arguments for \[lq]main\[rq] and a single string argument for \[lq]puts\[rq]. If you simply want to see all arguments and return values of every functions (if supported), use -a/--auto-args option. .SH DYNAMIC TRACING .SS FULL DYNAMIC TRACING .PP The uftrace tool supports dynamic function tracing which can be enabled at runtime (load-time, to be precise) on x86_64 and AArch64. Before recording functions, normally you need to build the target program with \f[V]-pg\f[R] (or \f[V]-finstrument-functions\f[R]), then it has some performance impact because all functions call \f[V]mcount()\f[R]. .PP With dynamic tracing, you can trace specific functions only given by the \f[V]-P\f[R]/\f[V]--patch\f[R] option and can also disable specific functions given by the \f[V]-U\f[R]/\f[V]--unpatch\f[R] option. With capstone disassembly engine you even don\[cq]t need to (re)compile the target with the option above. Now uftrace can analyze the instructions and (if possible) it can copy them to a different place and rewrite it to call \f[V]mcount()\f[R] function so that it can be traced by uftrace. After that the control is passed to the copied instructions and then returned back to the remaining instructions. .PP The following example shows an error message when normally running uftrace. Because the binary doesn\[cq]t call any instrumentation code (i.e.\ `mcount'). .IP .nf \f[C] $ gcc -o abc tests/s-abc.c $ uftrace abc uftrace: /home/namhyung/project/uftrace/cmd-record.c:1305:check_binary ERROR: Can\[aq]t find \[aq]mcount\[aq] symbol in the \[aq]abc\[aq]. It seems not to be compiled with -pg or -finstrument-functions flag which generates traceable code. Please check your binary file. \f[R] .fi .PP But when the \f[V]-P a\f[R] patch option is used, and then only it can dynamically trace \f[V]a()\f[R]. .IP .nf \f[C] $ uftrace record --no-libcall -P a abc $ uftrace replay # DURATION TID FUNCTION 0.923 us [19379] | a(); \f[R] .fi .PP In addition, you can enable all functions using `.' (for glob, \[cq]*\[cq]) that matches to any character in a regex pattern with \f[V]P\f[R] option. .IP .nf \f[C] $ uftrace record --no-libcall -P . abc $ uftrace replay # DURATION TID FUNCTION [19387] | main() { [19387] | a() { [19387] | b() { 0.940 us [19387] | c(); 2.030 us [19387] | } /* b */ 2.451 us [19387] | } /* a */ 3.289 us [19387] | } /* main */ \f[R] .fi .PP Note that \f[V]-U\f[R] option has the opposite effect of \f[V]-P\f[R] option so users can the both to fine-control. The option comes later will override the formers. For example if you want to trace all functions but `a' in the above: .IP .nf \f[C] $ uftrace record --no-libcall -P . -U a abc $ uftrace replay # DURATION TID FUNCTION [19390] | main() { [19390] | b() { 0.983 us [19390] | c(); 2.012 us [19390] | } /* b */ 3.373 us [19390] | } /* main */ \f[R] .fi .PP The order of the options is important, if you change it like \f[V]-U a -P .\f[R] then it will trace all the functions since \f[V]-P .\f[R] will be effective for all. .SS GCC FENTRY .PP If the capstone is not available, you need to add some more compiler (gcc) options when building the target program. The gcc 5.1 or more recent versions provide \f[V]-mfentry\f[R] and \f[V]-mnop-mcount\f[R] options which add instrumentation code (i.e.\ calling \f[V]mcount()\f[R] function) at the very beginning of a function and convert the instruction to a NOP. Then it has almost zero performance overhead when running in a normal condition. The uftrace can selectively convert it back to call \f[V]mcount()\f[R] using \f[V]-P\f[R] option. .IP .nf \f[C] $ gcc -pg -mfentry -mnop-mcount -o abc-fentry tests/s-abc.c $ uftrace record -P . --no-libcall abc-fentry $ uftrace replay # DURATION TID FUNCTION [ 18973] | main() { [ 18973] | a() { [ 18973] | b() { 0.852 us [ 18973] | c(); 2.378 us [ 18973] | } /* b */ 2.909 us [ 18973] | } /* a */ 3.756 us [ 18973] | } /* main */ \f[R] .fi .SS CLANG XRAY .PP Clang/LLVM 4.0 provides a dynamic instrumentation technique called X-ray (http://llvm.org/docs/XRay.html). It\[cq]s similar to a combination of \f[V]gcc -mfentry -mnop-mcount\f[R] and \f[V]-finstrument-functions\f[R]. The uftrace also supports dynamic tracing on the executables built with the \f[V]X-ray\f[R]. .PP For example, you can build the target program by clang with the below option and equally use \f[V]-P\f[R] option for dynamic tracing like below: .IP .nf \f[C] $ clang -fxray-instrument -fxray-instruction-threshold=1 -o abc-xray tests/s-abc.c $ uftrace record -P main abc-xray $ uftrace replay # DURATION TID FUNCTION [11093] | main() { 1.659 us [11093] | getpid(); 5.963 us [11093] | } /* main */ $ uftrace record -P . abc-xray $ uftrace replay # DURATION TID FUNCTION [11098] | main() { [11098] | a() { [11098] | b() { [11098] | c() { 0.753 us [11098] | getpid(); 1.430 us [11098] | } /* c */ 1.915 us [11098] | } /* b */ 2.405 us [11098] | } /* a */ 3.005 us [11098] | } /* main */ \f[R] .fi .SS PATCHABLE FUNCTION ENTRY .PP Recent compilers in both gcc and clang support another useful option \f[V]-fpatchable-function-entry=N[,M]\f[R] that generates M NOPs before the function entry and N-M NOPs after the function entry. We can simply use the case when M is 0 so \f[V]-fpatchable-function-entry=N\f[R] is enough. The number of NOPs required for dynamic tracing depends on the architecture but x86_64 requires 5 NOPs and AArch64 requires 2 NOPs to dynamically patch a call instruction for uftrace recording. .PP For example in x86_64, you can build the target program and trace as follows. .IP .nf \f[C] $ gcc -fpatchable-function-entry=5 -o abc-fpatchable tests/s-abc.c $ uftrace record -P . abc-fpatchable $ uftrace replay # DURATION TID FUNCTION [ 6818] | main() { [ 6818] | a() { [ 6818] | b() { [ 6818] | c() { 0.926 us [ 6818] | getpid(); 4.158 us [ 6818] | } /* c */ 4.590 us [ 6818] | } /* b */ 4.957 us [ 6818] | } /* a */ 5.593 us [ 6818] | } /* main */ \f[R] .fi .PP This feature can also be used by explicitly adding compiler attribute to some specific functions with \f[V]__attribute__ ((patchable_function_entry (N,M)))\f[R]. For example, the `tests/s-abc.c' program can be modified as follows. .IP .nf \f[C] static int c(void) { return 100000; } __attribute__((patchable_function_entry(5))) static int b(void) { return c() + 1; } static int a(void) { return b() - 1; } __attribute__((patchable_function_entry(5))) int main(void) { int ret = 0; ret += a(); return ret ? 0 : 1; } \f[R] .fi .PP The attribute is added to function `main' and `b' only and this program can normally be compiled without any additional compiler options, but the compiler detects the attributes and adds 5 NOPs at the entry of `main' and `b'. .IP .nf \f[C] $ gcc -o abc tests/s-patchable-abc.c $ uftrace record -P . abc $ uftrace replay # DURATION TID FUNCTION [ 20803] | main() { 0.342 us [ 20803] | b(); 1.608 us [ 20803] | } /* main */ \f[R] .fi .PP With this way, uftrace can selectively trace only the functions user wants by explicitly adding the attribute. This approach can collect trace records in a much less intrusive way compared to tracing the entire functions enabled by compiler flags. .PP \f[V]-fpatchable-function-entry=N[,M]\f[R] option and its attribute are supported since gcc-8.1 and clang-10. This dynamic tracing feature can be used in both x86_64 and AArch64 as of now. .SH SCRIPT EXECUTION .PP 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. .PP The user can write four 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. .IP .nf \f[C] $ cat scripts/simple.py def uftrace_begin(ctx): print(\[dq]program begins...\[dq]) def uftrace_entry(ctx): func = ctx[\[dq]name\[dq]] print(\[dq]entry : \[dq] + func + \[dq]()\[dq]) def uftrace_exit(ctx): func = ctx[\[dq]name\[dq]] print(\[dq]exit : \[dq] + func + \[dq]()\[dq]) def uftrace_end(): print(\[dq]program is finished\[dq]) \f[R] .fi .PP The above script can be executed in record time as follows: .IP .nf \f[C] $ uftrace record -S scripts/simple.py -F main tests/t-abc 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 \f[R] .fi .PP The `ctx' variable is a dictionary type that contains the below information. .IP .nf \f[C] /* 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 }; \f[R] .fi .PP Each field in `script_context' can be read inside the script. Please see \f[V]uftrace-script\f[R](1) for details about scripting. .SH WATCH POINT .PP The uftrace watch point is to display certain value only if it\[cq]s changed. It\[cq]s conceptually same as other debuggers but only works at function entry and exit so it might miss some updates. .PP As of now, following watch points are supported: .IP \[bu] 2 \[lq]cpu\[rq] : cpu number current task is running on .PP Like read triggers, the result is displayed as event (comment): .IP .nf \f[C] $ uftrace -W cpu tests/t-abc # DURATION TID FUNCTION [ 19060] | main() { [ 19060] | /* watch:cpu (cpu=8) */ [ 19060] | a() { [ 19060] | b() { [ 19060] | c() { 2.365 us [ 19060] | getpid(); 8.002 us [ 19060] | } /* c */ 8.690 us [ 19060] | } /* b */ 9.350 us [ 19060] | } /* a */ 12.479 us [ 19060] | } /* main */ \f[R] .fi .SH SEE ALSO .PP \f[V]uftrace\f[R](1), \f[V]uftrace-replay\f[R](1), \f[V]uftrace-report\f[R](1), \f[V]uftrace-recv\f[R](1), \f[V]uftrace-graph\f[R](1), \f[V]uftrace-script\f[R](1), \f[V]uftrace-tui\f[R](1) .SH AUTHORS Namhyung Kim .