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/freepattern often points at a bug. - Complements
strace:ltracefor library functions,stracefor kernel calls — together they cover the whole stack.
ltraceis the microscope just belowstrace: 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 pidattaches to a live process.-ffollows 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
-cprints a summary: how many times each function ran and how long it took.-ttadds microsecond timestamps for measuring gaps between calls.
5. ltrace vs. strace
| Layer | Tool | Shows |
|---|---|---|
| Library functions | ltrace | malloc, printf, fopen |
| Kernel system calls | strace | open, read, write, connect |
# A single fopen() in ltrace may trigger several syscalls in strace
ltrace -e fopen ./myprogram
strace -e trace=file ./myprogram
ltraceis higher-level and often easier to read;straceis closer to the hardware.
Common Pitfalls
- Stripped binaries show little: if the program is compiled without symbols,
ltracehas 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.
Related Pages
| Page | Why It Matters |
|---|---|
strace | Traces the kernel layer — the sibling tool. |
ps | Find the PID to attach to. |
lsof | See what files and sockets the process holds. |
ltrace Cheatsheet
| Command | What It Does |
|---|---|
ltrace cmd | Trace a command’s library calls. |
ltrace -p pid | Attach to a running process. |
ltrace -f cmd | Follow child processes. |
ltrace -e malloc cmd | Filter to specific functions. |
ltrace -c cmd | Summarize call counts and timing. |
ltrace -r cmd | Show return values. |
ltrace -o log cmd | Write the trace to a file. |