Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ltrace Command Guide for Tracing Library Calls

ltrace shows the library calls a program makes — the function-level conversation between a program and the C library it was built on.


What is ltrace, and Why Use It?

Where strace watches the kernel boundary, ltrace (library trace) watches the boundary between a program and the shared libraries it links against — libc, libssl, libpng, and the rest. It logs calls like malloc, strcpy, printf, and fopen as they happen. When you need to know which function a program is calling, in what order, and with what arguments, ltrace is the tool.

Why Learn ltrace?

  • Debug at the function level: see exactly which library function is failing or looping.
  • Understand compiled code: when you have no source, the call trace reveals the program’s behavior.
  • Spot misused APIs: an unexpected malloc/free pattern often points at a bug.
  • Complements strace: ltrace for library functions, strace for kernel calls — together they cover the whole stack.

ltrace is the microscope just below strace: same idea, one layer down, watching the library instead of the kernel.


1. Basic Usage: Trace a Command

# Trace a program's library calls
ltrace ls

# Trace and save to a file
ltrace -o trace.log ./myprogram

ltrace <command> runs the command and logs each library call it makes. Like strace, the raw output is verbose — filter it once you know the target.


2. Attach to a Running Process

# Trace an already-running process
ltrace -p 1234

# Follow child processes
ltrace -f -p 1234
  • -p pid attaches to a live process.
  • -f follows forks so children are traced too.

3. Filter the Calls

# Only calls matching a pattern
ltrace -e malloc ./myprogram

# Exclude noisy functions
ltrace -e '!fstat,stat' ./myprogram

# Show return values and timing
ltrace -r -T ./myprogram
  • -e <pattern> — filter by function name (glob-style).
  • -r — print the return value of each call.
  • -T — print the time each call took.

4. Summarize and Time

# Count calls per function
ltrace -c ./myprogram

# Timestamped trace
ltrace -tt -o trace.log ./myprogram
  • -c prints a summary: how many times each function ran and how long it took.
  • -tt adds microsecond timestamps for measuring gaps between calls.

5. ltrace vs. strace

LayerToolShows
Library functionsltracemalloc, printf, fopen
Kernel system callsstraceopen, read, write, connect
# A single fopen() in ltrace may trigger several syscalls in strace
ltrace -e fopen ./myprogram
strace -e trace=file ./myprogram
  • ltrace is higher-level and often easier to read; strace is closer to the hardware.

Common Pitfalls

  • Stripped binaries show little: if the program is compiled without symbols, ltrace has few names to print.
  • It only sees dynamically linked calls: statically linked programs are invisible to ltrace.
  • Same overhead as strace: tracing slows the program; use it to debug, not to monitor.
  • Names can be cryptic: without debugging symbols you may see raw addresses instead of function names.

PageWhy It Matters
straceTraces the kernel layer — the sibling tool.
psFind the PID to attach to.
lsofSee what files and sockets the process holds.

ltrace Cheatsheet

CommandWhat It Does
ltrace cmdTrace a command’s library calls.
ltrace -p pidAttach to a running process.
ltrace -f cmdFollow child processes.
ltrace -e malloc cmdFilter to specific functions.
ltrace -c cmdSummarize call counts and timing.
ltrace -r cmdShow return values.
ltrace -o log cmdWrite the trace to a file.