Basic strace Usage
sudo apt install -y strace # Ubuntu / Debian
# Trace a program as it runs
strace ls /tmp
# Trace a running process (PID)
sudo strace -p 1234
# Also trace child processes
sudo strace -f -p 1234
# Summary of system call statistics (-c)
strace -c ls /tmp
# Trace only specific system calls
strace -e trace=open,read,write ls /tmp
# Print timestamps
strace -t ls /tmp # Absolute time
strace -r ls /tmp # Relative time (interval between calls)Tracing File Access
This is the most common use case: seeing which files a process opens and reads.
# Trace files being opened
strace -e trace=openat,open,read,write,close ls /tmp 2>&1 | grep open
# Practical pattern: find config file locations
strace -e trace=openat nginx 2>&1 | grep ".conf"
# See which files are being read
strace -e trace=openat python3 myapp.py 2>&1 | grep -v "No such file"
# Write output to a separate file
strace -o trace.log -p 1234Tracing Network Sockets
# Network-related calls only
strace -e trace=network curl https://example.com
# Diagnose why connect failed
strace -e trace=connect,socket myapp 2>&1 | grep -E "connect|ECONNREFUSED|ETIMEDOUT"
# Check received data size
strace -e trace=read,write -s 1024 curl https://example.com 2>&1 | grep "read("Finding Bottlenecks with Statistics
# Call count and time statistics per system call
strace -c -p 1234
# Example output
# % time seconds usecs/call calls errors syscall
# ------- ----------- ---------- -------- -------- -------
# 45.23 0.002345 12 195 read
# 32.11 0.001667 8 208 write
# 12.05 0.000625 6 104 42 openat ← lots of errors
# Find slow system calls
strace -T -p 1234 2>&1 | awk -F'<' '{if($2+0 > 0.01) print}' | head -20Practical Diagnostic Patterns
# When a program exits unexpectedly — check signals
strace -e trace=signal -p 1234
# Find the cause of Permission denied
strace -e trace=openat,access myapp 2>&1 | grep "EACCES|EPERM"
# Suspected deadlock — check blocked system calls
sudo strace -p 1234 # If it's stuck, this shows which call is blocking
# Trace which programs are executed via execve
strace -e trace=execve -f bash myscript.sh 2>&1 | grep execveltrace — Tracing Library Functions
sudo apt install -y ltrace
# Trace library calls
ltrace ls /tmp
# Specific functions only
ltrace -e malloc,free myapp
# Summary statistics
ltrace -c myapp
# Combined with strace (system calls + library calls)
ltrace -S myapp 2>&1 | head -50When to Choose strace vs ltrace vs perf
| Tool | Use case |
|---|---|
| strace | File, network, and process issues at the kernel interface |
| ltrace | Shared library function call patterns (e.g. malloc leaks) |
| perf | CPU bottlenecks, hotspot finding, performance counters |
FAQ
Q. When should I use strace vs ltrace?
A. Use strace to trace system calls (kernel boundary: open, read, connect, etc.) and diagnose file, network, and permission issues. Use ltrace to trace library calls (malloc, printf, etc.) and see which function a process is stuck in. Can't open a file? Use strace. Need to follow a specific library's behavior? Use ltrace. If you need both, start with strace -f so you follow child processes as well.
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Questions & answers
Ask a question about this guide. We'll review and reply.