ltrace

On this page 7

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.

ComponentRoleEffect on output
PLT entryStub through which the program reaches a library functionDefines which calls are visible
ptrace interfaceKernel mechanism to stop, inspect and resume the processEnables tracing without source code
BreakpointTrap instruction placed on the PLT stubTriggers a stop on every call
Prototype fileDescribes argument types for known functionsTurns raw values into readable strings
Return hookCatches the moment the function returnsShows the result after the arrow sign
Components in an ltrace session

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.

OptionPurposeTypical use
-p PIDAttach to a running processInspect a live service without restarting it
-fTrace forked child processesPrograms that spawn workers or helpers
-e PATTERNSelect or exclude functionsShow only malloc and free, for example
-cCount calls and time per functionQuick profile of library usage
-SAlso display system callsCombined view of library and kernel activity
-TShow time spent inside each callLocate slow library functions
-l LIBRARYTrace only calls into a given libraryFocus on libssl or libcrypto
-s SIZESet maximum printed string lengthSee full paths or long buffers
Frequently used ltrace options

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.

ToolWhat it observesMechanismOverhead
ltraceShared library callsptrace and PLT breakpointsHigh
straceSystem calls and signalsptrace syscall stopsHigh, lower with seccomp filtering
perfCPU samples, hardware events, tracepointsKernel perf events subsystemLow for sampling
bpftraceKernel and user functions via probeseBPF programs with uprobesLow to moderate
Tracing tools in the monitoring topic

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.

SituationSuggested commandWhat it reveals
Program ignores an environment variableltrace -e getenv ./appWhich variable names are actually queried
Suspected memory leakltrace -e malloc+free -c ./appImbalance between allocations and releases
License or password check failsltrace -e strcmp+strncmp ./appValues being compared at runtime
Slow startupltrace -c -T ./appLibrary functions consuming the most time
Locale or encoding errorsltrace -e setlocale+iconv_open ./appRequested locales and character sets
When to reach for ltrace

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.