strace Command Guide for Tracing System Calls
strace shows every system call a program makes — the raw conversation between a process and the Linux kernel.
What is strace, and Why Use It?
A program does not do much on its own; it asks the kernel to do things — open files, read from sockets, allocate memory, create threads. strace (system call trace) intercepts and logs every one of those requests and their results. When a program misbehaves — hangs, crashes, refuses to start — strace is how you see what it was actually trying to do instead of guessing.
Why Learn strace?
- Debug “it just hangs”:
stracereveals the exact call the process is stuck on. - See why a program fails: a failed
open()orconnect()tells you the real error, not a vague message. - Understand unknown binaries: when the source is gone, the trace is the documentation.
- Follow file and network activity: every
open,read,write, andconnectis logged.
straceis the kernel’s flight recorder: every question a process asked, and every answer it got, written down in order.
1. Basic Usage: Trace a Command
# Trace a program from start to finish
strace ls
# Trace and write the output to a file
strace -o trace.log ./myprogram
strace <command> runs the command under observation and prints each system call. The output is verbose — filter it (see below) once you know what you are looking for.
2. Attach to a Running Process
# Trace an already-running process by PID
strace -p 1234
# Follow child processes too
strace -f -p 1234
-p pidattaches to a live process — no restart needed.-ffollowsfork()/clone()so child processes are traced as well.
3. Filter the Noise
# Only file-related calls
strace -e trace=file ls /tmp
# Only network calls
strace -e trace=network curl example.com
# Exclude the noisy ones
strace -e trace=!fstat,stat ./myprogram
# Show timing for each call
strace -T ./myprogram
-e trace=<set>— restrict to a category (file,network,process,memory).-T— add the time each call took; invaluable for finding slowdowns.
4. Summarize and Count
# Count how many times each call was made
strace -c ./myprogram
# Trace with timestamps
strace -tt -o trace.log ./myprogram
-cprints a summary table: call name, count, time, errors — the fastest way to spot what dominates.-ttprints microsecond timestamps so you can measure gaps.
5. Common Debugging Patterns
# Why does this program fail to find its config?
strace -e trace=open,openat ./myprogram 2>&1 | grep -i config
# Where is the process stuck?
strace -p $(pgrep myprogram)
# What files does this installer touch?
strace -e trace=file -o install.log ./installer.sh
- Grep the trace for the resource you care about — the error lines usually name the exact path or socket.
Common Pitfalls
- Tracing everything is overwhelming: a busy program can emit thousands of calls per second; always filter with
-eor-c. straceslows the program: it adds real overhead; use it to debug, not to monitor in production.- Attaching to a stuck process is safe but noisy: the trace shows the hang, but killing it may still be the fix.
- Some calls are hidden:
stracesees system calls, not library internals — for those useltrace.
Related Pages
| Page | Why It Matters |
|---|---|
ltrace | Traces library calls — the layer above system calls. |
ps | Find the PID to attach to. |
lsof | See what files and sockets a process holds. |
kill | Stop a process that strace reveals is stuck. |
strace Cheatsheet
| Command | What It Does |
|---|---|
strace cmd | Trace a command’s system calls. |
strace -p pid | Attach to a running process. |
strace -f cmd | Follow child processes. |
strace -e trace=file cmd | Filter to file operations. |
strace -c cmd | Summarize call counts and timing. |
strace -T cmd | Show time spent per call. |
strace -o log cmd | Write the trace to a file. |