Chapter 15. Creating uprobes with perf
Uprobes
(user-space probes) are dynamic instrumentation mechanisms that monitor specific points in user-space applications at runtime, without requiring changes to the source code or recompilation.
There are two primary use cases where uprobes
are useful:
- Debugging and performance analysis
-
Uprobes
function similarly to watchpoints. You can insert them at specific locations in your application to count how often those code paths are executed. Additionally, they can capture rich context such as call stacks or variable values, making them useful for identifying performance bottlenecks or tracking down bugs. - Event-based data collection
-
Uprobes
act as switching events for mechanisms such as circular buffers, helping 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 allows for powerful, non-intrusive observability and profiling of user-space behavior alongside kernel-space instrumentation (via kprobes
).
15.1. Creating uprobes at the function level with perf Copy linkLink copied to clipboard!
You can use the perf
tool to create dynamic tracepoints at arbitrary points in a process or application. These tracepoints can then be used in conjunction with other perf
tools such as perf stat
and perf record
to better understand the process or applications behavior.
Prerequisites
-
You have the
perf
user 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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 applications behavior.
Prerequisites
-
You have the
perf
user space tool installed. For more information, see Installing perf. You have received the debugging symbols for your executable:
objdump -t ./your_executable | head
# objdump -t ./your_executable | head
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteTo do this, the
debuginfo
package of the executable must be installed or, if the executable is a locally developed application, the application must be compiled with debugging information, the-g
option in GCC.
Procedure
View the function lines where you can place a
uprobe
:perf probe -x ./your_executable -L main
$ perf probe -x ./your_executable -L main
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Output of this command looks similar to:
<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);
<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);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
uprobe
for the desired function line:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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, which reads the perf.data
file and displays a detailed trace of the recorded workload.
- In the perf script example output
-
A
uprobe
is added to the functionisprime()
in a program calledmy_prog
a
is a function argument added to theuprobe
. Alternatively,a
could be an arbitrary variable visible in the code scope of where you add youruprobe
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
A