$linuxjunkies
>

ltrace(1)

Trace library calls made by a program and display their arguments and return values.

UbuntuDebianFedoraArch

Synopsis

ltrace [OPTION]... [COMMAND [ARG]...]

Description

ltrace is a debugging tool that intercepts and logs library function calls executed by a running process. It shows what functions from shared libraries (libc, libpthread, etc.) a program invokes, along with their arguments and return values. This is useful for understanding a program's behavior and diagnosing library-related issues.

ltrace attaches to a process and uses breakpoints to monitor function calls. The output displays the function name, arguments, and return value in a human-readable format. It complements strace, which traces system calls instead of library calls.

Common options

FlagWhat it does
-cCount calls to each function and display a summary on exit
-e EXPRFilter traced functions using an expression (e.g., -e "malloc*" to trace malloc calls)
-fFollow child processes created by fork()
-iPrint the instruction pointer at the time of the library call
-o FILEWrite output to FILE instead of stderr
-p PIDAttach to an already running process by PID
-s NUMLimit string length to NUM characters in output (default 32)
-tPrint timestamps showing time spent in each function
-TShow total time spent in each function at exit
-u USERNAMERun the traced program as USERNAME

Examples

Trace all library calls made by myprogram and display them in real-time

ltrace ./myprogram

Run myprogram with ltrace and print a summary of call counts by function

ltrace -c ./myprogram

Trace only malloc and related memory allocation functions

ltrace -e "malloc*" ./myprogram

Attach ltrace to an already-running process with PID 1234

ltrace -p 1234

Trace myprogram and child processes, saving output to trace.log

ltrace -f -o trace.log ./myprogram

Show the first 20 function calls with timing information

ltrace -t ./myprogram 2>&1 | head -20

Trace all functions from the C library only

ltrace -e "*@libc*" ./myprogram

Increase string display limit to 256 characters for better visibility of string arguments

ltrace -s 256 ./myprogram

Related commands