All articles

Trace a Failing Linux Program with strace

7 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Trace a Failing Linux Program with strace

An application can report “cannot open file” after trying several paths, loading configuration, and consulting environment variables. strace records the system calls at the user-kernel boundary, including each pathname, return value, and errno 1.

Use it after the application’s own logs have narrowed the problem but have not identified the failing resource.

Capture a trace without flooding the terminal

Write the trace to a file and preserve the program’s exit status:

strace -f -o trace.log -- command arg1 arg2
status=$?
printf 'command status=%s\n' "$status"

-f follows children created by the command. The separator -- prevents an argument beginning with - from becoming a strace option. By default, strace writes its records to standard error; -o keeps them separate from application output 1.

Caution: A trace can contain pathnames, command arguments, socket addresses, and fragments of data passed to system calls. Store it with restrictive permissions and redact it before sharing. Tracing also changes timing, so do not treat a race that disappears under strace as fixed.

Filter by the resource that failed

Start with a broad class instead of naming a handful of syscalls:

strace -f -e trace=%file -o trace.log -- command arg1
strace -f -e trace=%network -o trace.log -- command arg1
strace -f -e trace=%process,%signal -o trace.log -- command arg1

The %file, %network, %process, and %signal groups follow changes in the syscall interface better than a copied list of syscall names. The older trace=file spelling is deprecated in current strace documentation 1.

Search failures first:

grep -E '= -1 (EACCES|ENOENT|ECONNREFUSED|ETIMEDOUT|ENOSPC)\b' trace.log
Errno First question
ENOENT Which exact pathname was missing?
EACCES Which process credentials and directory permissions applied?
ECONNREFUSED Which address and port rejected the connection?
ETIMEDOUT Which operation waited, and for how long?
ENOSPC Did the target filesystem run out of blocks or inodes?

The last failure is not automatically the root cause. Programs often probe optional files and continue. Match the errno with nearby calls and the application’s final message.

A bounded missing-file lab

This lab asks cat to open one absent path and filters the trace to file calls:

lab=$(mktemp -d)
missing="$lab/not-here"
status=0

strace --quiet=attach,personality -e trace=%file \
  -o "$lab/trace.log" -- cat "$missing" 2>/dev/null || status=$?

printf 'status=%s\n' "$status"
grep -F "$missing" "$lab/trace.log"

The relevant line has this shape:

openat(AT_FDCWD, "/tmp/tmp.example/not-here", O_RDONLY) = -1 ENOENT (No such file or directory)

The exact syscall may vary with architecture and libc. The pathname, -1 return, and ENOENT are the useful evidence.

Attach only with permission

To observe an existing process:

strace -f -p 12345 -o trace.log

Replace the PID with a current value. Attaching uses the kernel’s ptrace access checks, and security policy may reject it even when both processes have the same user 2.

Caution: Attaching pauses tracees briefly while strace intercepts calls. Avoid latency-sensitive production processes unless the owner accepts that effect. Never add sudo merely to bypass an unexplained denial.

For a command that may spawn descendants, current strace also provides --kill-on-exit. Review its compatibility notes before using it; an interrupted tracer can otherwise leave descendants running.

Record the command line, filter, exit status, first relevant errno, and several surrounding calls. That is usually enough to move the investigation back to the application, permissions, network, or filesystem with a specific failure.

Sources and further reading
  1. strace(1) manual
  2. ptrace(2) manual