3.3.3. Monitoring application’s library function calls with ltrace
To monitor an application’s calls to functions available in libraries (shared objects), use the ltrace tool.
Prerequisites
Procedure
- Identify the libraries and functions of interest, if possible.
Start
ltraceand attach it to the program.注意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 function programIf the program is already running, find its process id (pid):
$ ps -C programAttach
ltraceto the process:$ ltrace -f -l library -e function -ppid programUse 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
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.
-
If
-
Stop
ltraceby pressing Ctrl+C. Analyze the list of library calls done by the application.
- 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.
注意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