Debugging · observability
strace command cheat sheet and output guide
strace prints every syscall a process makes, with its arguments, return value, and (optionally) timing. It is the fastest way to answer "what is this program actually doing" — and almost always the first tool to reach for when a binary misbehaves and you don't have its source.
strace cheat sheet
| Command | What it does |
|---|---|
strace ./app arg1 | Start app and print every syscall it makes to stderr |
strace -f ./app | Follow fork() / clone(): trace child processes and threads too. With -p, attaches to every thread of the process |
strace -p 1234 | Attach to a running process (several: -p "$(pidof nginx)"). Ctrl-C detaches and leaves it running |
strace -o trace.txt ./app | Write the trace to a file instead of stderr, so it does not mix with the program's own output |
strace -ff -o trace ./app | One file per process: trace.<pid>. Merge them later with strace-log-merge trace |
strace -e trace=openat,connect ./app | Only the listed syscalls |
strace -e trace=%file ./app | Every syscall that takes a path: openat, statx, newfstatat, access, readlink, unlinkat... |
strace -e trace=%network ./app | Socket calls: socket, connect, sendto, recvfrom, accept4... Also %process, %signal, %memory, %desc, %ipc, %creds |
strace -e 'trace=!futex,epoll_wait' -p 1234 | Everything except the listed calls (quote the ! for your shell) |
strace -e trace=/^open ./app | Syscalls whose name matches a regex |
strace -Z ./app | Only failed calls (--failed-only, same as -e status=failed). -z shows only successful ones |
strace -P /etc/hosts ./app | Only calls that touch this path |
strace -c ./app | No trace, just a summary table: calls, errors and time per syscall. -C prints both |
strace -c -w ./app | Summary using wall-clock time instead of CPU time spent in the kernel |
strace -T ./app | Append the time spent in each call, e.g. = 0 <0.200216> |
strace -tt ./app | Wall-clock timestamp with microseconds on each line. -t seconds, -ttt epoch, -r delta since previous call |
strace -s 4096 ./app | Print up to 4096 bytes of each string buffer (default 32). File names are always printed in full |
strace -y ./app | Show the path behind each fd: read(3</etc/hosts>, ...) |
strace -yy ./app | Also decode sockets: 4<TCP:[127.0.0.1:8080->127.0.0.1:39820]> |
strace -k -e trace=openat ./app | Print the user-space stack trace for each call (needs strace built with libdw or libunwind) |
strace -v ./app | Do not abbreviate structures, environment arrays and the like |
strace -f --seccomp-bpf -e trace=execve ./build.sh | Let the kernel stop the tracee only on traced syscalls: far less overhead for narrow filters. Requires -f, not usable with -p |
strace -e inject=openat:error=EACCES -P /etc/hostname cat /etc/hostname | Fault injection: make a syscall fail on purpose to test error handling. The line is tagged (INJECTED) |
How it works
strace is a thin wrapper around the kernel's ptrace(2) facility. It attaches to the target process, asks the kernel to stop it before and after every syscall (PTRACE_SYSCALL), reads the registers to decode the call, formats them human-readably, and lets the target resume. Modern kernels expose PTRACE_GET_SYSCALL_INFO so strace can do this without needing per-architecture register knowledge.
Because it rides on ptrace, strace inherits its security model: Yama ptrace_scope and CAP_SYS_PTRACE apply. See ptrace(2) for the full mechanism.
Attach strace to a running process
strace -f -p "$(pidof nginx)" -tt -T -s 256 -o /tmp/nginx.trace-p accepts several PIDs (comma or space separated), and with -f strace attaches to every thread of each process, not just the main thread. Press Ctrl-C to detach: the process keeps running.
If attaching fails with strace: attach: ptrace(PTRACE_SEIZE, 1234): Operation not permitted, check these in order:
- Yama `ptrace_scope`.
cat /proc/sys/kernel/yama/ptrace_scope.0lets you attach to any process running as your UID.1(the default on Ubuntu and several other distributions) only lets you attach to your own descendants, which is whystrace ./appworks butstrace -pdoes not.2requiresCAP_SYS_PTRACEfor any tracing.3disables ptrace until reboot. Runningsudo strace -p PIDworks for0to2. To relax it temporarily:sudo sysctl kernel.yama.ptrace_scope=0. - Different user or a setuid binary. Without
CAP_SYS_PTRACEyou can only trace processes with your UID that are "dumpable". Daemons that changed UID, or that calledprctl(PR_SET_DUMPABLE, 0)(ssh-agent does), need root. - Someone else is already tracing it. A process has only one tracer.
grep TracerPid /proc/1234/statusshows the PID of the current one (0means none). Debuggers, other strace sessions and some anti-debugging code take that slot. - Containers. The process may be in a container without
CAP_SYS_PTRACEor with a seccomp profile that blocksptrace, see below.
Attaching is not entirely invisible: the strace man page warns that a process attached with -p may get a spurious EINTR from a non-restartable syscall that was in progress. On a production process, prefer narrow filters (-e trace=...) and short sessions.
Recipes
Find the file the program can't open
strace -f -e trace=openat,access -e status=failed ./app 2>&1 | tailSee which syscalls dominate run time
strace -c -f ./appAttach to nginx and watch network syscalls
strace -f -p $(pidof nginx) -e trace=network -s 512 -tDump what the program writes to stderr
strace -e write=2 -s 8192 ./app | grep -A2 'write(2,'How to read strace output
Every line is one syscall: name, decoded arguments, =, return value. These are real lines, from top to bottom: a failed file open, a value-result argument, a blocked call cut by a signal, and the end of a process.
openat(AT_FDCWD, "/nonexistent", O_RDONLY) = -1 ENOENT (No such file or directory)
accept4(3, {sa_family=AF_INET, sin_port=htons(39820), sin_addr=inet_addr("127.0.0.1")}, [128 => 16], SOCK_CLOEXEC) = 4
read(3, "# Locale name al"..., 4096) = 2996
connect(4, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=200000001}, 0x7ffd1119bef0) = 0 <0.200216>
[pid 2713997] wait4(-1, <unfinished ...>
[pid 2713999] exit_group(0) = ?
[pid 2713999] +++ exited with 0 +++
<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 2713999
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2713999, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++| You see | It means |
|---|---|
= 3 | Success. For openat, socket, accept4 the number is the new file descriptor; for read / write it is the byte count (= 0 from read is end of file) |
= -1 ENOENT (No such file or directory) | Failure. The kernel returned an error and libc set errno to ENOENT. Each syscall page lists the errno values it can return and why |
= -1 EAGAIN (Resource temporarily unavailable) | Usually not a bug: a non-blocking fd had nothing ready. Expect it next to epoll_wait / poll |
= -1 EINPROGRESS | Non-blocking connect started; the result arrives later via poll and getsockopt(SO_ERROR) |
= ? ERESTARTSYS (To be restarted if SA_RESTART is set) | A blocking call was interrupted by a signal. Kernel-internal, never visible to the program: it is restarted or turned into EINTR |
= ? | The call never returned: exit_group, or an execve in another thread |
"# Locale name al"... | A string buffer cut at the -s limit (32 bytes by default) |
[128 => 16] | A value-result argument: 128 went in, 16 came back |
0x7ffd1119bef0 | A pointer strace did not (or could not) dereference, often because the call failed before filling it |
<0.200216> | Time spent inside the call in seconds, printed with -T |
[pid 2713997] | Which process or thread made the call, shown with -f once there is more than one |
<unfinished ...> then <... wait4 resumed> | Another thread made a call while this one was still blocked. strace splits the line in two and stitches it back on return |
--- SIGCHLD {...} --- | A signal was delivered to the tracee, with its decoded siginfo |
+++ exited with 0 +++ / +++ killed by SIGTERM +++ | The process ended, with its exit status or the fatal signal |
Two habits make long traces readable. First grep for = -1 (or rerun with -Z) and read upward from the first unexpected failure: the last few openat/statx calls before a program prints an error usually name the file it was looking for. Second, ignore the noise at the top: everything from execve to the first call that mentions your program's own files is the dynamic loader and libc start-up (openat of ld.so.cache and libc.so.6, mmap, mprotect, arch_prctl), and ENOENT on locale files there is normal.
Explain your own strace output
Paste a trace and get each line annotated with what the syscall does, what the return value means and, for failures, what that errno usually points to for that specific call. Every syscall name links to its reference page.
strace in Docker and Kubernetes
Two things decide whether strace works in a container: the seccomp profile must allow the ptrace syscall, and the tracer needs `CAP_SYS_PTRACE` for anything Yama or the UID rules would otherwise refuse.
Docker's default seccomp profile allows ptrace on kernels 4.8 and newer, and unconditionally when the container has CAP_SYS_PTRACE. Docker does not grant that capability by default, so:
# Trace a command inside a fresh container (strace must be in the image)
docker run --rm -it --cap-add=SYS_PTRACE myimage strace -f ./app
# Attach from a debug container that joins the target's PID namespace
docker run --rm -it --pid=container:web --cap-add=SYS_PTRACE alpine \
sh -c 'apk add --no-cache strace && strace -f -p 1'
# Or trace from the host, as root, using the container's host PID
sudo strace -f -p "$(docker inspect -f '{{.State.Pid}}' web)"On a host where ptrace_scope is 1 or higher, strace -p 1 from docker exec into a container started without SYS_PTRACE fails with ptrace(PTRACE_SEIZE, 1): Operation not permitted, because the target is not a descendant of the exec'd shell. ptrace_scope is a host-wide setting, not per container.
In Kubernetes, use an ephemeral debug container that targets the application container. The general debugging profile adds SYS_PTRACE to it:
kubectl debug -it pod/myapp --image=alpine --target=app --profile=general -- \
sh -c 'apk add --no-cache strace && strace -f -p 1'With --target, the debug container shares the process namespace of the app container, so its main process is usually PID 1. Namespaces that enforce the baseline or restricted Pod Security Standard reject added capabilities other than the allowed list, which excludes SYS_PTRACE, so the ephemeral container is refused there. In that case trace from the node (kubectl debug node/NAME --profile=sysadmin, then strace -p with the host PID) or ask for a namespace exemption.
Seccomp also explains odd results inside a traced container: a syscall the profile denies shows up as failing with EPERM (Docker's default action), and a filter that kills instead ends the trace with the process killed by SIGSYS.
strace vs ltrace vs perf trace vs bpftrace
strace stops the tracee twice per syscall through ptrace. That makes it simple and precise, and slow: syscall-heavy programs can run many times slower under it, so keep production sessions short and filtered, or use a tool that does not stop the process.
| Tool | Traces | Mechanism | Overhead | Reach for it when |
|---|---|---|---|---|
strace | Syscalls and signals of chosen processes, fully decoded | ptrace stops | High | You need exact arguments and errno for one program, now |
ltrace | Library calls (malloc, strcmp, getaddrinfo...); -S adds syscalls | Breakpoints on PLT entries | High | The interesting behavior is in libc or another shared library, not the kernel |
perf trace | Syscalls system-wide or per process (-p PID), with -s summaries and --failure | Kernel tracepoints | Low | strace-like output on a busy production host |
bpftrace | Any tracepoint or kprobe, aggregated in the kernel | eBPF | Low | Counting or filtering across the whole system |
Equivalent one-liners:
strace -f -e trace=openat ./app
perf trace -e openat -- ./app
bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args.filename)); }'
strace -c -f -p 1234
perf trace -s -p 1234
bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'For continuous, fleet-wide syscall monitoring rather than one-off debugging, eBPF-based agents such as Falco, Tracee or sysdig are the usual answer.
Popular syscalls people trace
Click through for the reference page on each — argument format, common errno values, kernel-history notes.
- strace -e open
- strace -e openat
- strace -e read
- strace -e write
- strace -e close
- strace -e execve
- strace -e clone
- strace -e fork
- strace -e exit_group
- strace -e stat
- strace -e fstat
- strace -e lstat
- strace -e access
- strace -e connect
- strace -e accept
- strace -e bind
- strace -e sendto
- strace -e recvfrom
- strace -e mmap
- strace -e munmap
- strace -e brk
- strace -e ioctl
- strace -e fcntl
- strace -e ptrace
- strace -e futex
- strace -e epoll_wait
- strace -e select
- strace -e poll