用于 C/C++/Rust/Python 的函数图形跟踪器
uftrace is a function call graph tracer for C, C++, Rust and Python programs.
It hooks into the entry and exit of each function, recording timestamps as well as the function's arguments and return values. uftrace is capable of tracing both user and kernel functions, as well as library functions and system events providing an integrated execution flow in a single timeline.
Initially, uftrace only supported function tracing with compiler support. However, it now allows users to trace function calls without recompilation by analyzing instructions in each function prologue and dynamically and selectively patching those instructions.
Users can also write and run scripts for each function entry and exit using python/luajit APIs to create custom tools for their specific purposes.
uftrace offers various filters to reduce the amount of trace data and provides visualization using Chrome trace viewer and flame graph or call-graph diagrams for graphviz and mermaid, allowing for a big picture view of the execution flow.
It was heavily inspired by the ftrace framework of the Linux kernel and the name uftrace stems from the combination of user and ftrace.
It can record data from:
-P., or else selective NOP patching using code compiled with -pg,
-finstrument-functions or -fpatchable-function-entry=N.With the recorded data, uftrace can:
It supports many commands and filters such as filtering by function call duration for analysis of program execution and performance.
uftrace traces each function in the executable and shows time durations.
Usually, for this to be possible, the program needs to be compiled with
-pg or -fpatchable-function-entry=5 (=2 is enough on aarch64).
With full dynamic tracing (-P.|--patch=.), uftrace works on all executables (as
long they are not stripped, or symbol information is available from a separate file).
uftrace hooks into the PLT in the given executable file to trace library calls and with
(-l|--nest-libcall), it also hooks into the procedure linkage tables (PLTs) of shared
libraries. The depth can be limited using -D<num>, where 1 is flat call tracing.
Using (-a|--auto-args), uftrace automatically records arguments and
return values of known functions. Without extra debug information, this
includes the API functions of standard (C language or system) libraries.
This can be combined with -P. or -l:
For example, -la traces nested library calls, even in stripped executables.
In addition, -a implies --srcline, so it records the source line location info, and
this info can be shown by uftrace replay --srcline and in uftrace tui. Users can directly
open the editor at the source location as shown in https://uftrace.github.io/slide/#120.
If debug information for the program (gcc -g) is available, --auto-args
works even on functions inside the user-compiled programs.
In case argument information is not available, argument specifications like
(-A udev_new@arg1/s) can be passed on the command line or an options file.
Example:
…
Furthermore, it can show detailed execution flow at function level, and report which functions had the longest execution time. It also shows information about the execution environment.
You can set up filters to exclude or include specific functions when tracing. In addition, function arguments and return values can be saved and shown later.
It supports multi-process and/or multi-threaded applications. With root
privileges and if the kernel was built with CONFIG_FUNCTION_GRAPH_TRACER=y,
kernel functions can be traced as well.
On Linux distros, misc/install-deps.sh can be used to install required software(s) for building uftrace. Those are for optional and advanced features, but are highly recommended.
$ sudo misc/install-deps.sh
Once you installed required software(s) on your system, it can be built and installed like following:
$ ./configure
$ make
$ sudo make install
For details about installation and dependencies, please refer to INSTALL.md
These are the commands supported by uftrace:
record : runs a program and saves the trace datareplay : shows program execution in the trace datareport : shows performance statistics in the trace datalive : does record and replay in a row (default)info : shows system and program info in the trace datadump : shows low-level trace datarecv : saves the trace data from networkgraph : shows function call graph in the trace datascript : runs a script for recorded trace datatui : show text user interface for graph and reportYou can use -h or --help option to see available commands and options.
$ uftrace
uftrace -- function (graph) tracer for userspace
usage: uftrace [COMMAND] [OPTION...] [<program>]
COMMAND:
record Run a program and saves the trace data
replay Show program execution in the trace data
report Show performance statistics in the trace data
live Do record and replay in a row (default)
info Show system and program info in the trace data
dump Show low-level trace data
recv Save the trace data from network
graph Show function call graph in the trace data
script Run a script for recorded trace data
tui Show text user interface for graph and report
Try `uftrace --help' or `man uftrace [COMMAND]' for more information.
If omitted, it defaults to the live command which is almost same as running
record and replay subcommand in a row (but does not record the trace info
to files).
For recording, the executable needs to be compiled with the -pg
(or -finstrument-functions) option which generates profiling code
(calling mcount or __cyg_profile_func_enter/exit) for each function.
Note that, there's an experimental support for dynamic tracing on x86_64 and AArch64(ARM64) which doesn't require such (re-)compilations. Also, recent compilers have some options to help uftrace to reduce tracing overhead with similar way (although it still needs recompilation of your program). Please see dynamic tracing section for more details.
$ uftrace tests/t-abc
# DURATION TID FUNCTION
16.134 us [ 1892] | __monstartup();
223.736 us [ 1892] | __cxa_atexit();
[ 1892] | main() {
[ 1892] | a() {
[ 1892] | b() {
[ 1892] | c() {
2.579 us [ 1892] | getpid();
3.739 us [ 1892] | } /* c */
4.376 us [ 1892] | } /* b */
4.962 us [ 1892] | } /* a */
5.769 us [ 1892] | } /* main */
For more analysis, you'd be better recording it first so that it can run analysis commands like replay, report, graph, dump and/or info multiple times.
$ uftrace record tests/t-abc
It'll create uftrace.data directory that contains trace data files.
Other analysis commands expect the directory exists in the current directory,
but one can use another using -d option.
The replay command shows execution information like above. As you can see,
t-abc is a very simple program merely calls a, b and c functions.
In the c function it called getpid() which is a library function implemented
in the C library (glibc) on normal systems - the same goes to __cxa_atexit().
Users can use various filter options to limit functions it records/prints.
The depth filter (-D option) is to omit functions under the given call depth.
The time filter (-t option) is to omit functions running less than the given
time. And the function filters (-F and -N options) are to show/hide functions
under the given function.
The -k option enables to trace kernel functions as well (needs root access).
With the classic hello world program, the output would look like below (Note,
I changed it to use fprintf() with stderr rather than the plain printf() to make
it invoke system call directly):
$ sudo uftrace -k tests/t-hello
Hello world
# DURATION TID FUNCTION
1.365 us [21901] | __monstartup();
0.951 us [21901] | __cxa_atexit();
[21901] | main() {
[21901] | fprintf() {
3.569 us [21901] | __do_page_fault();
10.127 us [21901] | sys_write();
20.103 us [21901] | } /* fprintf */
21.286 us [21901] | } /* main */
You can see the page fault handler and the system call handler for write() were called inside the fprintf() call.
Also, it can record and show function arguments and return value with -A and
-R options respectively. The following example records first argument and
return value of 'fib' (Fibonacci number) function.
$ uftrace record -A fib@arg1 -R fib@retval tests/t-fibonacci 5
$ uftrace replay
# DURATION TID FUNCTION
2.853 us [22080] | __monstartup();
2.194 us [22080] | __cxa_atexit();
[22080] | main() {
2.706 us [22080] | atoi();
[22080] | fib(5) {
[22080] | fib(4) {
[22080] | fib(3) {
7.473 us [22080] | fib(2) = 1;
0.419 us [22080] | fib(1) = 1;
11.452 us [22080] | } = 2; /* fib */
0.460 us [22080] | fib(2) = 1;
13.823 us [22080] | } = 3; /* fib */
[22080] | fib(3) {
0.424 us [22080] | fib(2) = 1;
0.437 us [22080] | fib(1) = 1;
2.860 us [22080] | } = 2; /* fib */
19.600 us [22080] | } = 5; /* fib */
25.024 us [22080] | } /* main */
The report command lets you know which function spends the longest time
including its children (total time).
$ uftrace report
Total time Self time Calls Function
========== ========== ========== ====================================
25.024 us 2.718 us 1 main
19.600 us 19.600 us 9 fib
2.853 us 2.853
暂无开放 Issues,或尚未同步最近议题。