Record application interactions
The executable code of applications interacts with the operating system and shared libraries. Recording an activity log of these interactions can provide enough insight into application behavior without debugging the actual application code, or help pinpoint conditions where a bug manifests.
Tools to record application interactions Copy linkLink copied!
To record application interactions, you can use several tools are available in RHEL. For system calls use strace, for library calls use ltrace, and for advanced probing SystemTap. Select the appropriate tool to log specific runtime behaviors and diagnose integration issues.
- strace
-
The
stracetool primarily enables logging of system calls (kernel functions) used by an application.- The
stracetool can provide a detailed output about calls, becausestraceinterprets parameters and results with knowledge of the underlying kernel code. Numbers are turned into the corresponding constant names, bitwise combined flags expanded to flag list, pointers to character arrays dereferenced to provide the actual string, and more. Support for more recent kernel features might be lacking. - To reduce the amount of captured data, filter the traced calls.
- The use of
stracedoes not require any particular configuration except for configuring the log filter. - Tracing the application code with
straceresults in significant slowdown of the application’s execution. As a result,straceis not suitable for many production deployments. As an alternative, consider usingltraceor SystemTap. - The version of
straceavailable in Red Hat Developer Toolset can also perform system call tampering. This capability is useful for debugging.
- The
- ltrace
-
The
ltracetool enables logging of an application’s user space calls into shared objects (dynamic libraries).- The
ltracetool enables tracing calls to any library. - To reduce the amount of captured data, filter the traced calls.
- The use of
ltracedoes not require any particular configuration except for configuring the log filter. - The
ltracetool is lightweight and fast, offering an alternative tostrace: it is possible to trace the corresponding interfaces in libraries such asglibcwithltraceinstead of tracing kernel functions withstrace. - Because
ltracedoes not handle a known set of calls such asstrace, it does not attempt to explain the values passed to library functions. Theltraceoutput contains only raw numbers and pointers. The interpretation ofltraceoutput requires consulting the actual interface declarations of the libraries present in the output.
Note In Red Hat Enterprise Linux 10, a known issue prevents
ltracefrom tracing system executable files. This limitation does not apply to executable files built by users. - The
- SystemTap
-
SystemTap is an instrumentation platform for probing running processes and kernel activity on the Linux system. SystemTap uses its own scripting language for programming custom event handlers.
- Compared to using
straceandltrace, scripting the logging means more work in the initial setup phase. However, the scripting capabilities extend SystemTap’s usefulness beyond just producing logs. - SystemTap works by creating and inserting a kernel module. The use of SystemTap is efficient and does not create a significant slowdown of the system or application execution on its own.
- SystemTap includes a set of usage examples.
- Compared to using
- GDB
-
The GNU Debugger (GDB) is primarily meant for debugging, not logging. However, some of its features make it useful even in the scenario where an application’s interaction is the primary activity of interest.
- With GDB, it is possible to conveniently combine the capture of an interaction event with immediate debugging of the subsequent execution path.
- GDB is best suited for analyzing response to infrequent or singular events, after the initial identification of problematic situation by other tools. Using GDB in any scenario with frequent events becomes inefficient or even impossible.
Monitor system calls of an application with strace Copy linkLink copied!
To monitor the system (kernel) calls performed by an application, use the strace tool.
Before you begin Copy linkLink copied!
- You must have
straceinstalled on the system. For details, see Installing tools for debugging applications.
Procedure Copy linkLink copied!
Monitor application’s library function calls with ltrace Copy linkLink copied!
To monitor an application’s calls to functions available in libraries (shared objects), use the ltrace tool.
Before you begin Copy linkLink copied!
- You must have
ltraceinstalled on the system. For details, see Installing tools for debugging applications. .Procedure- Identify the libraries and functions of interest, if possible.
- Start
ltraceand attach it to the program.Note In Red Hat Enterprise Linux 10, a known issue prevents
ltracefrom tracing system executable files. This limitation does not apply to executable files built by users.- If the program you want to monitor is not running, start
ltraceand specify program:$ ltrace -f -l library -e functionprogram - If the program is already running, find its process id (pid):
$ ps -C program - Attach
ltraceto the process:$ ltrace -f -l library -e function -ppidprogram - Use the
-e,-fand-loptions to filter the output:- Supply the function names to be displayed as function. The
-e functionoption can be used multiple times. If left out,ltracedisplays calls to all functions. - Instead of specifying functions, you can specify whole libraries with the
-l libraryoption. This option behaves similarly to the-e functionoption. - If you do not want to trace any forked processes or threads, omit the
-foption.
See the ltrace(1)_ manual page for more information.
- Supply the function names to be displayed as function. The
- If the program you want to monitor is not running, start
ltracedisplays the library calls made by the application.In most cases, an application makes a large number of calls and
ltraceoutput displays immediately, if no filter is set.ltraceexits when the program exits.To terminate the monitoring before the traced program exits, press .
- If
ltracestarted the program, the program terminates together withltrace. - If you attached
ltraceto an already running program, the program terminates together withltrace.- Stop
ltraceby pressing Ctrl+C. - Analyze the list of library calls done by the application.
- Stop
- If the application crashes, the important information is probably at the end of log.
- The output contains a large amount of unnecessary information. However, you can construct a more precise filter and repeat the procedure.
Note It is advantageous to both see the output and save it to a file. Use the
teecommand to achieve this:$ ltrace ... |& tee your_log_file.log
Monitor application’s system calls with SystemTap Copy linkLink copied!
To register custom event handlers for kernel events, use the SystemTap tool. SystemTap is more efficient than the strace tool, but requires more setup. Installing SystemTap also installs the strace.stp script, which provides strace-like functionality when using SystemTap on your system.
Before you begin Copy linkLink copied!
- SystemTap and the corresponding kernel packages must be installed on the system. For details, see Installing tools for measuring application performance.
.Procedure
- Find the process ID (pid) of the process you want to monitor:
$ ps -aux - Run SystemTap with the
strace.stpscript:# stap /usr/share/systemtap/examples/process/strace.stp -x pidThe value of pid is the process id.
The script is compiled to a kernel module, which is then loaded. This introduces a slight delay between entering the command and getting the output.
- When the process performs a system call, the call name and its parameters are printed on the command line.
- The script exits when the process terminates, or when you press
Ctrl+C.
- Find the process ID (pid) of the process you want to monitor:
Use GDB to intercept application system calls Copy linkLink copied!
To stop program execution when the program performs specific system calls, use GNU Debugger (GDB) catchpoints, then inspect the program state and system call parameters at those points.
Before you begin Copy linkLink copied!
- You must understand the usage of GDB breakpoints. For details, see Using GDB breakpoints to stop execution at defined code locations.
- GDB must be attached to the program. For details, see Attaching GDB to a process.
.Procedure
- Set the catchpoint:
(gdb) catch syscall syscall-nameThe command
catch syscallsets a special type of breakpoint that halts execution when the program performs a system call.The
syscall-nameoption specifies the name of the call. You can specify multiple catchpoints for various system calls. Leaving out thesyscall-nameoption causes GDB to stop on any system call. - Start execution of the program.
- If the program has not started execution, start it:
(gdb) r - If the program execution is halted, resume it:
(gdb) c
- If the program has not started execution, start it:
- GDB halts execution after the program performs any specified system call.
- Use further GDB commands to examine the program state and advance execution, according to the particular situation.
- To exit the GDB debugging session:
(gdb) q
- Set the catchpoint:
Use GDB to intercept handling of signals by applications Copy linkLink copied!
To stop the execution of a program under specific circumstances, you can use the GNU Debugger (GDB). To stop the execution when the program receives a signal from the operating system, use a GDB catchpoint.
Before you begin Copy linkLink copied!
- You must understand the usage of GDB breakpoints. For details, see Using GDB breakpoints to stop execution at defined code locations.
- GDB must be attached to the program. For details, see Attaching GDB to a process.
.Procedure
- Set the catchpoint:
(gdb) catch signal signal-typeThe command
catch signalsets a special type of a breakpoint that halts execution when a signal is received by the program. Thesignal-typeoption specifies the type of the signal. Use the special value'all'to catch all signals. - Let the program run.
- If the program has not started execution, start it:
(gdb) r - If the program execution is halted, resume it:
(gdb) c
- If the program has not started execution, start it:
- GDB halts execution after the program receives any specified signal.
- Continue by debugging the program code handling the signal, or resume execution with the knowledge of the signal being received.
- Later, to exit the GDB debugging session:
(gdb) q
- Set the catchpoint: