Chapter 10. ltrace
ltrace is a diagnostic and debugging tool for the command line that can be used to display calls that are made to shared libraries. It uses the dynamic library hooking mechanism, which prevents it from tracing calls to statically linked libraries. ltrace also displays return values of the library calls. The output is printed to standard error output or to a selected file.
Red Hat Developer Toolset is distributed with ltrace 0.7.91. While the base version ltrace remains the same as in the previous release of Red Hat Developer Toolset, various enhancements and bug fixes have ported.
10.1. Installing ltrace
In Red Hat Enterprise Linux, the ltrace
utility is provided by the devtoolset-9-ltrace package and is automatically installed with devtoolset-9-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”.
10.2. Using ltrace
To run the ltrace
utility on a program you want to analyze:
$ scl enable devtoolset-9 'ltrace program argument...'
Replace program with the name of the program you want to analyze, and argument with any command line options and arguments you want to supply to this program. Alternatively, you can run the utility on an already running process by using the -p
command line option followed by the process ID:
$ scl enable devtoolset-9 'ltrace -p process_id'
Note that you can execute any command using the scl
utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset ltrace
as default:
$ scl enable devtoolset-9 'bash'
To verify the version of ltrace
you are using at any point:
$ which ltrace
Red Hat Developer Toolset’s ltrace
executable path will begin with /opt
. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset ltrace
:
$ ltrace -V
10.2.1. Redirecting Output to a File
By default, ltrace
prints the name of each system call, its arguments and the return value to standard error output. To redirect this output to a file, use the -o
command line option followed by the file name:
$ scl enable devtoolset-9 'ltrace -o file_name program argument...'
Replace file_name with the name of the file.
Example 10.1. Redirecting Output to a File
Consider a slightly modified version of the fibonacci
file from Example 8.1, “Compiling a C Program With Debugging Information”. This executable file displays the Fibonacci sequence and optionally allows you to specify how many members of this sequence to list. Run the ltrace
utility on this file and redirect the trace output to fibonacci.log
:
$ scl enable devtoolset-9 'ltrace -o fibonacci.log ./fibonacci 20'
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
This creates a new plain-text file called fibonacci.log
in the current working directory.
10.2.2. Tracing Selected Library Calls
To trace only a selected set of library calls, run the ltrace
utility with the -e
command line option:
$ scl enable devtoolset-9 'ltrace -e expression program argument...'
Replace expression with a chain of rules to specify the library calls to trace. The rules can consist of patterns that identify symbol names (such as malloc
or free
) and patterns that identify library SONAMEs (such as libc.so
). For example, to trace call to the malloc
and free
function but to omit those that are done by the libc
library:
$ scl enable devtoolset-9 'ltrace -e malloc+free-@libc.so* program'
Example 10.2. Tracing Selected Library Calls
Consider the ls
command. Run the ltrace
utility on this program and trace only the opendir
, readdir
, and closedir
function calls:
$ scl enable devtoolset-9 'ltrace -e opendir+readdir+closedir ls'
ls->opendir(".") = { 3 }
ls->readdir({ 3 }) = { 61533, "." }
ls->readdir({ 3 }) = { 131, ".." }
ls->readdir({ 3 }) = { 67185100, "BUILDROOT" }
ls->readdir({ 3 }) = { 202390772, "SOURCES" }
ls->readdir({ 3 }) = { 60249, "SPECS" }
ls->readdir({ 3 }) = { 67130110, "BUILD" }
ls->readdir({ 3 }) = { 136599168, "RPMS" }
ls->readdir({ 3 }) = { 202383274, "SRPMS" }
ls->readdir({ 3 }) = nil
ls->closedir({ 3 }) = 0
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
+++ exited (status 0) +++
For a detailed description of available filter expressions, see the ltrace(1) manual page.
10.2.3. Displaying Time Stamps
To prefix each line of the trace with the exact time of the day in hours, minutes, and seconds, run the ltrace
utility with the -t
command line option:
$ scl enable devtoolset-9 'ltrace -t program argument...'
To also display milliseconds, supply the -t
option twice:
$ scl enable devtoolset-9 'ltrace -tt program argument...'
To prefix each line of the trace with the time required to execute the respective system call, use the -r
command line option:
$ scl enable devtoolset-9 'ltrace -r program argument...'
Example 10.3. Displaying Time Stamps
Consider the pwd
command. Run the ltrace
utility on this program and include time stamps in the output:
$ scl enable devtoolset-9 'ltrace -tt pwd'
13:27:19.631371 __libc_start_main([ "pwd" ] <unfinished ...>
13:27:19.632240 getenv("POSIXLY_CORRECT") = nil
13:27:19.632520 strrchr("pwd", '/') = nil
13:27:19.632786 setlocale(LC_ALL, "") = "en_US.UTF-8"
13:27:19.633220 bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
13:27:19.633471 textdomain("coreutils") = "coreutils"
(...)
13:27:19.637110 exited (status 0)
10.2.4. Displaying a Summary
To display a summary of how much time was required to execute each system call and how many times were these system calls executed, run the ltrace
utility with the -c
command line option:
$ scl enable devtoolset-9 'ltrace -c program argument...'
Example 10.4. Displaying a Summary
Consider the lsblk
command. Run the ltrace
utility on this program and display a trace summary:
$ scl enable devtoolset-9 'ltrace -c lsblk > /dev/null'
% time seconds usecs/call calls function
------ ----------- ----------- --------- --------------------
53.60 0.261644 261644 1 __libc_start_main
4.48 0.021848 58 374 mbrtowc
4.41 0.021524 57 374 wcwidth
4.39 0.021409 57 374 __ctype_get_mb_cur_max
4.38 0.021359 57 374 iswprint
4.06 0.019838 74 266 readdir64
3.21 0.015652 69 224 strlen
...
------ ----------- ----------- --------- --------------------
100.00 0.488135 3482 total
10.3. Additional Resources
For more information about ltrace and its features, see the resources listed below.
Installed Documentation
ltrace(1) — The manual page for the
ltrace
utility provides detailed information about its usage. To display the manual page for the version included in Red Hat Developer Toolset:$
scl enable devtoolset-9 'man ltrace'
Online Documentation
- ltrace for RHEL 6 and 7 — This article on the Red Hat Developer Blog offers additional in-depth information (including practical examples) on how to use ltrace for application debugging.
See Also
- Chapter 1, Red Hat Developer Toolset — An overview of Red Hat Developer Toolset and more information on how to install it on your system.
- Chapter 9, strace — Instructions on tracing program system calls using the strace tool.
- Chapter 8, GNU Debugger (GDB) — Instructions on debugging programs written in C, C++, and Fortran.
- Chapter 11, memstomp — Instructions on using the memstomp utility to identify calls to library functions with overlapping memory regions that are not allowed by various standards.