Skip to content
Visual explainer 2h to learn 5 steps

Tracing programs with strace & ltrace

Diagnose a misbehaving program by reading its system call trace.

By Invicly Tech, , in Linux System Programming. About 9 minutes to read.

Topic 7 of 42

In short

strace uses the ptrace facility to show every system call a program makes, with arguments and return values. Because all interaction with files, the network and other processes goes through syscalls, strace answers questions like which config file was opened or why a connect failed, even without source code. ltrace does the same for shared-library calls.

Explained simply

strace lets you watch a program talk to the kernel. Every file it opens, every byte it reads, every network connection it tries shows up as a line of text. You do not need the program's source code.

This is powerful because programs keep secrets. An app says "config error" but does not say which file. strace shows it trying /etc/app.conf, getting "No such file or directory", and giving up. Mystery solved in seconds.

ltrace is a cousin that shows calls into shared libraries, like malloc or strcmp, instead of system calls. Together they show what a program does, from the outside.

Both tools slow the program down a lot, so use them for diagnosis, not for measuring speed, and be careful on busy production servers.

Press Play on a diagram to walk through it one step at a time.

FlowDebugging with strace
  1. 1
    Reproduce

    Run the failing command under strace.

  2. 2
    Filter

    -e trace=openat,connect narrows the noise.

  3. 3
    Follow

    -f follows children; -p attaches to a running PID.

  4. 4
    Find the failure

    Look for = -1 ENOENT, EACCES or ECONNREFUSED.

  5. 5
    Fix

    Create the missing file, fix the permission or the path.

SequenceHow strace sees each call
Program
Kernel
strace
  1. 1openat("/etc/app.conf")
    Program to Kernel: openat("/etc/app.conf")
  2. 2ptrace stop: syscall entry
    Kernel to strace: ptrace stop: syscall entry
  3. 3Record name and args, continue
    strace to Kernel: Record name and args, continue
  4. 4ptrace stop: syscall exit, -1 ENOENT
    Kernel to strace: ptrace stop: syscall exit, -1 ENOENT
  5. 5Print the line, continue
    strace to Kernel: Print the line, continue
  6. 6Return -1, errno = ENOENT
    Kernel to Program: Return -1, errno = ENOENT

In depth

Reading a line of strace output

A typical line looks like openat(AT_FDCWD, "/etc/app.conf", O_RDONLY) = -1 ENOENT (No such file or directory). On the left is the system call name. In the brackets are its arguments, decoded into readable names where strace can. After the equals sign is the return value. On failure you get -1, the errno name and its message.

Strings are shortened to 32 characters by default; add -s 200 to see more. Structs are shown with their fields in braces. A call that is still waiting is marked unfinished ... and later shown again marked ... resumed, which is common with -f and threads.

  • Name(arguments) = result
  • -1 ENOENT etc. marks failure
  • -s 200 shows longer strings
  • -y prints the path behind each fd number

Cutting through the noise

A simple program makes dozens of startup calls just loading libraries. Filters help. -e trace=openat,read limits output to named calls. Classes make this easier: trace=%file covers every call that takes a path, %network covers socket calls, %process covers fork, exec and exit. Newer strace versions also support -e status=failed to show only calls that failed, which is often exactly what you want.

To see timing, -t adds the wall-clock time to each line, -T shows how long each call took, and -r shows time since the previous line. A slow request that spends 2 seconds in one connect call becomes obvious.

  • -e trace=%file, %network, %process
  • -e status=failed (or -Z) for failures only
  • -T shows time spent in each call
  • -o file writes the trace to a file
MapSyscall classes for -e trace=
-e trace=
  • %file
    • Every call that takes a path
    • openat, stat, unlink
  • %network
    • socket, connect, sendto
  • %process
    • fork and clone
    • execve
    • exit
  • Named calls
    • openat,read

Following children and attaching

Many programs fork helpers: shells, build tools, web servers. Without -f, strace only follows the first process and you may miss the one that fails. With -f, each line is prefixed by the PID that made the call, and -ff -o out writes one file per process.

To trace something already running, use strace -p PID. You need to own the process or be root, and on some distros ptrace is restricted by the Yama security module. Press Ctrl-C to detach; the program carries on. Remember that strace stops the target at every call, which can make a busy server many times slower.

CompareTracing programs that fork
No -f
  • The default
  • Only the first process
  • Child failures are missed
-f
  • Follows every child
  • Each line prefixed by its PID
  • One combined output
-ff -o out
  • One file per process
  • Named out.PID
  • Easy to read per process

Summary mode and ltrace

strace -c skips the line-by-line output and prints a table when the program ends: how many times each syscall ran, how many failed, and how much time they took. It is the quickest way to spot a program making 50,000 tiny writes or retrying a failing call in a loop.

ltrace works at the library level. ltrace ./app shows calls like malloc(64) = 0x55d0... and strcmp("admin", "guest") = -1. It is handy for understanding closed-source tools, but it only works for dynamically linked programs and is less maintained than strace.

  • strace -c: per-syscall counts and time
  • ltrace -c: the same for library calls
  • ltrace needs a dynamically linked binary

Learn it in 5 short steps

About 25m each. Track them with a timer in your plan.

  1. 1
    Trace a command with strace~25m
  2. 2
    Filter syscalls with -e trace~25m
  3. 3
    Follow child processes with -f~25m
  4. 4
    Summarise syscall counts with -c~25m
  5. 5
    Trace library calls with ltrace~25m

Key points

  • Each line shows call(arguments) = return value, plus the errno name on failure.
  • -f is essential for shells and servers that fork.
  • -c prints a summary table of call counts and time spent.
  • strace slows the target heavily, so be careful on busy production processes.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
strace -e trace=openat,read cat /etc/hostname
strace -f -o trace.log ./server
strace -c ls > /dev/null
sudo strace -p 1234 -e trace=network
grep -E "= -1 E" trace.log | head

Trace a command, a forking server, a summary and a live process.

Finding a missing config file

A tool exits with "failed to load configuration" and nothing else. We trace only file-related calls that failed. The output shows the program looked in three places, in order, and none existed. Creating the file in any of those paths fixes it. We never needed the source code.

bash
strace -f -e trace=%file -e status=failed ./mytool 2>&1 | grep -v "\.so"
# openat(AT_FDCWD, "./mytool.toml", O_RDONLY) = -1 ENOENT (No such file or directory)
# openat(AT_FDCWD, "/home/sam/.config/mytool/config.toml", O_RDONLY) = -1 ENOENT
# openat(AT_FDCWD, "/etc/mytool/config.toml", O_RDONLY) = -1 ENOENT
mkdir -p ~/.config/mytool && cp example.toml ~/.config/mytool/config.toml

Spotting why a program is slow

A log converter takes ages on a small file. strace -c shows it made about 2 million write calls, one per character, and that write dominates the time. The fix is buffering: write to a FILE stream with stdio, or collect output in a large buffer and write it in chunks. After the fix, the same run makes a few hundred writes.

bash
strace -c -o summary.txt ./convert input.log > out.txt
head -6 summary.txt
# % time     seconds  usecs/call     calls    errors syscall
#  98.71    3.120417           1   2000004           write
#   0.80    0.025315           6      4096           read
# after switching to buffered output:
#  65.10    0.004021          16       245           write

Quick reference

  • strace ./app runs and traces a command
  • strace -f follows forks; -ff -o out splits per process
  • strace -p PID attaches to a running process; Ctrl-C detaches
  • -e trace=%file / %network / %process filters by class
  • -e status=failed (or -Z) shows only failing calls
  • -T time per call, -t timestamps, -s 200 longer strings, -y show fd paths
  • strace -c gives a summary table at exit
  • ltrace ./app traces shared library calls

Common mistakes

  • Forgetting -f and missing the child process that actually fails.
  • Leaving strace attached to a production process and slowing it down.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A shell script fails, but strace ./script.sh shows nothing wrong. What is the most likely fix?

Question 2

2Which option shows how long each individual system call took?

Question 3

3What mechanism does strace use to observe another process?

Question 4

4Why should you be careful running strace on a busy production server?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Use strace to find which files the ls command opens when you run ls /tmp. List the non-library ones.

  2. Exercise 2medium

    Find out what address curl connects to for example.com and how long the connect takes, using only strace.

  3. Exercise 3hard

    Compare cat with a 1-byte-buffer copy program using strace -c on the same 1 MB file. Explain the difference in calls and time.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Does strace work on statically linked programs?
Yes. strace watches system calls at the kernel boundary, so it does not care how the program was linked. ltrace, which hooks library calls, does need dynamic linking.
Why does strace say "Operation not permitted" when attaching?
You may not own the process, or the kernel setting kernel.yama.ptrace_scope blocks attaching to non-child processes. Run it with sudo or start the program under strace instead.
Is there something lighter than strace for production?
Yes. eBPF-based tools such as bpftrace, perf trace and the bcc tools (opensnoop, execsnoop) observe syscalls with far lower overhead.

Go deeper

ShareXLinkedInWhatsApp