LTTng | Function tracing in user space

Install the following packages to trace functions in user space

  • liblttng-ust
  • lttng-tools
  • liburcu
  • babeltrace
For Ubuntu:

sudo apt install liblttng-ust-dev lttng-tools liburcu-dev babeltrace

How it works

  • LTTng function trace used profiling hooks(__cyg_profile_func_enter(), __cyg_profile_func_exit()) supported by compiler.
  • The sources of the application needs to be compiled with the -finstrument-functions compiler option.
  • By preloading LTTng shared libraries (using LD_PRELOAD), these profiling hooks get defined and start emitting LTTng events
  • Function tracing uses one of the provided library, each one providing a different trade-off between performance and robustness
    • liblttng-ust-cyg-profile-fast.so: This is a lightweight variant that should only be used where it can be guaranteed that the complete event stream is recorded without any missing events.
    • liblttng-ust-cyg-profile.so: This is robust variant which also works for use cases where events might get discarded, or not recorded from application startup.

Sample demonstration

1. Create a source file demo.cpp

#include <cstdio>

void foo(int x)
{
    printf("in func[%p]\n", func);
}
int main()
{
    foo(5);
    printf("in main[%p]\n", main);
    return 0;
}

2. Compile with g++ or clang++

g++ demo.cpp -o demo -finstrument-functions

3. Start tracing

lttng create demo -o ./out
lttng enable-event -u -a -c chan1
lttng start
LD_PRELOAD=liblttng-ust-cyg-profile-fast.so ./demo
lttng destroy

4. Trace output

Session demo created.
Traces will be written in /data/out
All UST events are enabled in channel chan1
Tracing started for session demo
in func[0x557bd9b361a9]
in main[0x557bd9b36225]
Session demo destroyed

4. Run babeltrace

babeltrace ./out
…
14:25:28.228561232 compname lttng_ust_statedump:build_id: { cpu_id = 0 }, { baddr = 0x7F50A1769000, _build_id_length = 20, build_id = [ [0] = 0xEB, [1] = 0x3, [2] = 0x87, [3] = 0x9F, [4] = 0xEA, [5] = 0x7D, [6] = 0x52, [7] = 0xA0, [8] = 0x3C, [9] = 0xC5, [10] = 0xD6, [11] = 0xBB, [12] = 0x46, [13] = 0xB5, [14] = 0x74, [15] = 0xED, [16] = 0xF1, [17] = 0x56, [18] = 0x18, [19] = 0xC4 ] }
14:25:28.228561341 compname lttng_ust_statedump:debug_link: { cpu_id = 0 }, { baddr = 0x7F50A1769000, crc = 2862702264, filename = "ld-2.30.so" }
14:25:28.228561481 compname lttng_ust_statedump:bin_info: { cpu_id = 0 }, { baddr = 0x7FFE4D983000, memsz = 0, path = "[linux-vdso.so.1]", is_pic = 0, has_build_id = 0, has_debug_link = 0 }
14:25:28.228563193 compname lttng_ust_statedump:end: { cpu_id = 0 }, { }
14:25:28.229006299 compname lttng_ust_cyg_profile_fast:func_entry: { cpu_id = 4 }, { addr = 0x557BD9B36225 }
14:25:28.229010698 compname lttng_ust_cyg_profile_fast:func_entry: { cpu_id = 4 }, { addr = 0x557BD9B361A9 }
14:25:28.229021102 compname lttng_ust_cyg_profile_fast:func_exit: { cpu_id = 4 }, { }
14:25:28.229022254 compname lttng_ust_cyg_profile_fast:func_exit: { cpu_id = 4 }, { }