9.2. Using ltrace

To run the ltrace utility on a program you want to analyze, type the following at a shell prompt:
scl enable devtoolset-4 '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-4 '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-4 'bash'

Note

To verify the version of ltrace you are using at any point, type the following at a shell prompt:
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

9.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-4 'ltrace -o file_name program [argument...]'
Replace file_name with the name of the file.

Example 9.1. Redirecting Output to a File

Consider a slightly modified version of the fibonacci file from Example 7.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. To run the ltrace utility on this file and redirect the trace output to fibonacci.log, type:
~]$ scl enable devtoolset-4 '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.

9.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-4 '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, use:
scl enable devtoolset-4 'ltrace -e malloc+free-@libc.so* program'

Example 9.2. Tracing Selected Library Calls

Consider the ls command. To run the ltrace utility on this program and trace only the opendir, readdir, and closedir function calls, type:
~]$ scl enable devtoolset-4 '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.

9.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-4 'ltrace -t program [argument...]'
To also display milliseconds, supply the -t option twice:
scl enable devtoolset-4 '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-4 'ltrace -r program [argument...]'

Example 9.3. Displaying Time Stamps

Consider the pwd command. To run the ltrace utility on this program and include time stamps in the output, type:
~]$ scl enable devtoolset-4 '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) +++

9.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-4 'ltrace -c program [argument...]'

Example 9.4. Displaying a Summary

Consider the lsblk command. To run the ltrace utility on this program and display a trace summary, type:
~]$ scl enable devtoolset-4 '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