ltrace(1)
Trace library calls made by a program and display their arguments and return values.
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
| Flag | What it does |
|---|---|
-c | Count calls to each function and display a summary on exit |
-e EXPR | Filter traced functions using an expression (e.g., -e "malloc*" to trace malloc calls) |
-f | Follow child processes created by fork() |
-i | Print the instruction pointer at the time of the library call |
-o FILE | Write output to FILE instead of stderr |
-p PID | Attach to an already running process by PID |
-s NUM | Limit string length to NUM characters in output (default 32) |
-t | Print timestamps showing time spent in each function |
-T | Show total time spent in each function at exit |
-u USERNAME | Run the traced program as USERNAME |
Examples
Trace all library calls made by myprogram and display them in real-time
ltrace ./myprogramRun myprogram with ltrace and print a summary of call counts by function
ltrace -c ./myprogramTrace only malloc and related memory allocation functions
ltrace -e "malloc*" ./myprogramAttach ltrace to an already-running process with PID 1234
ltrace -p 1234Trace myprogram and child processes, saving output to trace.log
ltrace -f -o trace.log ./myprogramShow the first 20 function calls with timing information
ltrace -t ./myprogram 2>&1 | head -20Trace all functions from the C library only
ltrace -e "*@libc*" ./myprogramIncrease string display limit to 256 characters for better visibility of string arguments
ltrace -s 256 ./myprogram