ltrace is a command-line tracer for Linux that intercepts calls from a program into shared libraries, such as malloc, strlen or fopen in the C library, and prints each call with its arguments and return value. In Linux monitoring and troubleshooting, it complements strace: where strace shows the boundary between a process and the kernel, ltrace shows the boundary between a program and its dynamically linked libraries, which is where much application logic actually happens.
Administrators and developers use ltrace to answer questions that logs rarely cover: which configuration file a binary really opens, why a string comparison fails, how often a program allocates memory, or which library function is consuming time. It works on unmodified binaries without source code, which makes it valuable for closed or legacy software.
How ltrace intercepts library calls
ltrace works by exploiting the dynamic linking mechanism of ELF executables. A dynamically linked program does not call a library function directly; it jumps through a stub in the Procedure Linkage Table (PLT), which the dynamic loader resolves to the real address in the shared object. ltrace places software breakpoints on these PLT entries, so every call through them stops the process briefly.
The tracing itself relies on the ptrace system call, the same kernel interface used by debuggers such as GDB and by strace. When a breakpoint fires, ltrace reads the registers and stack of the traced process, decodes the arguments according to a prototype description, lets the call proceed, and then captures the return value. Prototypes come from configuration files, traditionally /etc/ltrace.conf and files under /usr/share/ltrace, and newer versions can also read DWARF debug information when it is present.
The process runs as normal code between breakpoints, but every traced call costs at least two context switches into the tracer. This design explains both the strength of ltrace, which needs no recompilation, and its main weakness, which is significant runtime overhead.
The following table summarizes the components involved in a single traced call.
| Component | Role | Effect on output |
|---|---|---|
| PLT entry | Stub through which the program reaches a library function | Defines which calls are visible |
| ptrace interface | Kernel mechanism to stop, inspect and resume the process | Enables tracing without source code |
| Breakpoint | Trap instruction placed on the PLT stub | Triggers a stop on every call |
| Prototype file | Describes argument types for known functions | Turns raw values into readable strings |
| Return hook | Catches the moment the function returns | Shows the result after the arrow sign |
Key ltrace options and output
A typical invocation is ltrace -f -tt -o trace.log ./program, which follows child processes, adds microsecond timestamps and writes to a file instead of the terminal. Each output line shows the function name, the decoded arguments and the return value after an equals sign, for example a call to getenv with the string “HOME” returning a path.
The options below cover most practical work. Filtering is essential, because an unfiltered trace of a busy program can produce millions of lines within seconds.
| Option | Purpose | Typical use |
|---|---|---|
-p PID | Attach to a running process | Inspect a live service without restarting it |
-f | Trace forked child processes | Programs that spawn workers or helpers |
-e PATTERN | Select or exclude functions | Show only malloc and free, for example |
-c | Count calls and time per function | Quick profile of library usage |
-S | Also display system calls | Combined view of library and kernel activity |
-T | Show time spent inside each call | Locate slow library functions |
-l LIBRARY | Trace only calls into a given library | Focus on libssl or libcrypto |
-s SIZE | Set maximum printed string length | See full paths or long buffers |
The summary mode enabled by -c is often the best starting point. It prints a table of call counts, total time and average time per function, which reveals patterns such as excessive allocation churn or thousands of redundant string conversions before any line-by-line reading begins.
ltrace compared with strace, perf and bpftrace
ltrace occupies a specific layer in the Linux observability stack. Choosing the right tool depends on which boundary needs to be observed and how much overhead the system can tolerate.
| Tool | What it observes | Mechanism | Overhead |
|---|---|---|---|
| ltrace | Shared library calls | ptrace and PLT breakpoints | High |
| strace | System calls and signals | ptrace syscall stops | High, lower with seccomp filtering |
| perf | CPU samples, hardware events, tracepoints | Kernel perf events subsystem | Low for sampling |
| bpftrace | Kernel and user functions via probes | eBPF programs with uprobes | Low to moderate |
The key difference from bpftrace with uprobes is that ltrace needs no kernel features beyond ptrace and requires no root privileges for a user’s own processes, whereas eBPF tools usually need elevated capabilities but impose far less overhead on production systems. For a quick look at an unfamiliar binary on a test machine, ltrace remains the faster route; for sustained measurement on a busy server, eBPF-based tracing is the safer choice.
Practical uses in diagnosis
ltrace is most useful when the question concerns application behavior visible at the library level. The decision table below maps common situations to an approach.
| Situation | Suggested command | What it reveals |
|---|---|---|
| Program ignores an environment variable | ltrace -e getenv ./app | Which variable names are actually queried |
| Suspected memory leak | ltrace -e malloc+free -c ./app | Imbalance between allocations and releases |
| License or password check fails | ltrace -e strcmp+strncmp ./app | Values being compared at runtime |
| Slow startup | ltrace -c -T ./app | Library functions consuming the most time |
| Locale or encoding errors | ltrace -e setlocale+iconv_open ./app | Requested locales and character sets |
Because ltrace prints decoded string arguments, it can expose sensitive data such as passwords or tokens passed to comparison or cryptographic functions. Trace files should therefore be treated as confidential diagnostic material and deleted after use.
Limitations and common pitfalls
ltrace has several structural blind spots that experienced users keep in mind. Misreading an empty or incomplete trace as proof that a function is never called is the most frequent mistake.
- Statically linked binaries have no PLT calls into shared objects, so ltrace shows little or nothing; Go programs and many container images built with musl static linking fall into this category.
- Calls within the same library, for example one libc function calling another internally, bypass the PLT and remain invisible.
- Binaries linked with immediate binding and full RELRO, or compiled with
-fno-plt, may reduce or alter what ltrace can intercept, depending on the ltrace version and architecture. - Timing-sensitive bugs can disappear under tracing because the overhead changes thread scheduling, a classic observer effect.
- Only one ptrace tracer may attach at a time, so ltrace cannot run alongside GDB or strace on the same process.
- Security settings such as the Yama ptrace_scope sysctl may block attaching to running processes without root privileges.
Development of ltrace has also been slower than that of strace for years, and support varies across CPU architectures. On modern distributions it still works well for x86-64 user space, but for long-term instrumentation many teams now prefer uprobes through eBPF.
A plain-language summary
Most programs do not do everything themselves. They borrow ready-made functions from shared libraries, such as a function that measures the length of text or one that opens a file. ltrace is a tool that listens in on these requests and writes down each one: what the program asked for, what details it passed along and what answer came back.
Imagine a cook who keeps calling a pantry assistant for ingredients. ltrace is like someone standing at the pantry door with a notebook. It does not need the recipe, yet it can tell you exactly what the cook asked for and when, which often explains why the dish turned out wrong. The catch is that the note-taking slows the kitchen down.
An example from practice
A systems administrator at a mid-sized hosting company receives a ticket: a closed-source reporting utility, delivered only as a binary, prints “configuration not found” on a new Linux server even though the configuration file is clearly present in /etc/reportd/. The vendor no longer offers support, and the strace output only shows a failed open attempt on a path built from several pieces.
The administrator runs ltrace -e getenv+snprintf+fopen ./reportd. Within a few lines the trace shows that the utility calls getenv with “REPORTD_HOME”, receives a null result and then uses snprintf to build the fallback path /usr/local/etc/reportd/report.conf. The real cause is a missing environment variable, not a missing file. After the variable is added to the systemd unit file, the utility starts normally, and the entire diagnosis takes under ten minutes without source code.