Chapter 15. Creating uprobes with perf
Uprobes (user-space probes) provide dynamic instrumentation for user-space applications at runtime. They require no source code changes or recompilation.
There are two primary use cases where uprobes are useful:
- Debugging and performance analysis
-
Uprobesfunction similarly to watchpoints. You can insert them at specific locations in your application to count how often those code paths are run. Additionally, they can capture rich context such as call stacks or variable values. Use them to identify performance bottlenecks or track bugs. - Event-based data collection
-
Uprobesact as switching events for mechanisms such as circular buffers. They can control when data is recorded or flushed based on execution triggers in user space.
Uprobes integrate seamlessly with perf, which can both consume existing uprobes and create new ones. This flexibility enables powerful, non-intrusive observability and profiling of user-space behavior alongside kernel-space instrumentation (by using kprobes).
15.1. Creating uprobes at the function level with perf Copy linkLink copied to clipboard!
Use the perf tool to create dynamic tracepoints at arbitrary points in a process or application. Use these tracepoints with perf stat or perf record to analyze process and application behavior.
Prerequisites
-
You have the
perfuser space tool installed. For more information, see Installing perf.
Procedure
Create the uprobe in the process or application you are interested in monitoring at a location of interest within the process or application:
# perf probe -x /path/to/executable -a functionAdded new event: probe_executable:function (on function in /path/to/executable) You can now use it in all perf tools, such as: perf record -e probe_executable:function -aR sleep 1
15.2. Creating uprobes on lines within a function with perf Copy linkLink copied to clipboard!
You can use the tracepoints conjunction with other perf tools such as perf stat and perf record to better understand the process or application behavior.
Prerequisites
-
You have the
perfuser space tool installed. For more information, see Installing perf. You have received the debugging symbols for your executable:
# objdump -t ./your_executable | headNoteTo do this, install the
debuginfopackage of the executable. If the executable is a locally developed application, compile the application with debugging information by using the-goption in GCC.
Procedure
View the function lines where you can place a
uprobe:$ perf probe -x ./your_executable -L main<main@/home/user/my_executable:0> 0 int main(int argc, const char *argv) 1 { int err; const char *cmd; char sbuf[STRERR_BUFSIZE]; / libsubcmd init */ 7 exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT); 8 pager_init(PERF_PAGER_ENVIRONMENT);Create the
uprobefor the function line that you want to monitor:# perf probe -x ./my_executable main:8Added new event: probe_my_executable:main_L8 (on main:8 in /home/user/my_executable) you can now use it in all perf tools, such as: Perf record -e probe_my_executable:main_L8 -aR sleep 1
15.3. Perf script output of data recorded over uprobes Copy linkLink copied to clipboard!
A common method to analyze data collected with uprobes is to run the perf script command. It which reads the perf.data file and displays a detailed trace of the recorded workload.
- In the perf script example output
-
A
uprobeis added to the functionisprime()in a program calledmy_prog ais a function argument added to theuprobe. Alternatively,acould be an arbitrary variable visible in the code scope of where you add youruprobe:# perf scriptmy_prog 1367 [007] 10802159.906593: probe_my_prog:isprime: (400551) a=2 my_prog 1367 [007] 10802159.906623: probe_my_prog:isprime: (400551) a=3 my_prog 1367 [007] 10802159.906625: probe_my_prog:isprime: (400551) a=4 my_prog 1367 [007] 10802159.906627: probe_my_prog:isprime: (400551) a=5 my_prog 1367 [007] 10802159.906629: probe_my_prog:isprime: (400551) a=6 my_prog 1367 [007] 10802159.906631: probe_my_prog:isprime: (400551) a=7 my_prog 1367 [007] 10802159.906633: probe_my_prog:isprime: (400551) a=13 my_prog 1367 [007] 10802159.906635: probe_my_prog:isprime: (400551) a=17 my_prog 1367 [007] 10802159.906637: probe_my_prog:isprime: (400551) a=19
-
A