$linuxjunkies
>

strace(1)

Trace system calls and signals made by a process to diagnose problems and understand program behavior.

UbuntuDebianFedoraArch

Synopsis

strace [OPTION]... COMMAND [ARG]...

Description

strace is a diagnostic tool that intercepts and logs all system calls and signals received by a process. It shows you exactly which kernel functions a program calls, their arguments, and return values. This is invaluable for debugging why a program fails, hangs, or behaves unexpectedly.

By default, strace writes output to stderr. You can redirect it to a file or filter by specific system calls. It works on compiled binaries without needing source code or recompilation.

Common options

FlagWhat it does
-e EXPRfilter which syscalls/signals to trace; e.g. -e open,read or -e !mmap
-o FILEwrite output to FILE instead of stderr
-p PIDattach to and trace an existing process by PID
-ffollow child processes created by fork/clone
-ccount syscalls and print summary statistics; don't print individual calls
-s SIZEprint up to SIZE characters for string arguments (default 32)
-vprint all arguments fully; don't abbreviate long structures or arrays
-xprint all non-ASCII bytes in hex
-tprefix output with wall-clock time
-Tshow time spent in each syscall

Examples

trace all syscalls made by ls; watch it open files, stat paths, read directories

strace ls /tmp

trace only file operations; filter to see just open, read, and write syscalls

strace -e openat,read,write cat file.txt

attach to running process 1234 and save trace to trace.log without disrupting terminal

strace -p 1234 -o trace.log

follow forks and trace all execve calls; see child processes and their exec chains

strace -f -e execve bash -c 'echo hello'

summarize syscall counts and time; useful for quick performance profiling

strace -c curl https://example.com

trace only file-related syscalls and view first 20 lines

strace -e trace=file ls -la 2>&1 | head -20

trace signals; see when and how the process receives SIGTERM

strace -e signal=SIGTERM sleep 10 &

Related commands