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

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”: strace reveals the exact call the process is stuck on.
  • See why a program fails: a failed open() or connect() 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, and connect is logged.

strace is 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 pid attaches to a live process — no restart needed.
  • -f follows fork()/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
  • -c prints a summary table: call name, count, time, errors — the fastest way to spot what dominates.
  • -tt prints 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 -e or -c.
  • strace slows 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: strace sees system calls, not library internals — for those use ltrace.

PageWhy It Matters
ltraceTraces library calls — the layer above system calls.
psFind the PID to attach to.
lsofSee what files and sockets a process holds.
killStop a process that strace reveals is stuck.

strace Cheatsheet

CommandWhat It Does
strace cmdTrace a command’s system calls.
strace -p pidAttach to a running process.
strace -f cmdFollow child processes.
strace -e trace=file cmdFilter to file operations.
strace -c cmdSummarize call counts and timing.
strace -T cmdShow time spent per call.
strace -o log cmdWrite the trace to a file.