Linux Command Reference ยท Processes and Signals
Linux strace command
Reference for the Linux strace command: trace system calls and signals of a program, filter with -e, follow children, attach to a PID, summarize with -c (read-only).
strace runs a program and prints every system call it makes: opening files, reading, writing, connecting, forking, waiting. A system call is the request a program makes to the kernel, and everything a program does with the outside world goes through them. Reading a trace answers "which file is it really opening?", "why does it hang?" and "which config file does it look for?" without the source code.
strace is not installed on our live runner (it needs permission to trace, which the sandbox denies), so the examples on this page are read-only and show typical output.
strace [OPTION]... COMMAND [ARG]...
strace [OPTION]... -p PID
Try it
Read-only, on a real machine:
strace -e trace=openat cat /etc/hostname # openat(AT_FDCWD, "/etc/hostname", O_RDONLY) = 3 # (other openat calls for libraries precede this line) strace -c ls > /dev/null # % time seconds usecs/call calls errors syscall # ------ ----------- ----------- --------- --------- ---------------- # ... strace -f -o trace.log sh -c 'sleep 1; echo hi' # -f follows children, -o writes the trace to a file
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
strace [-fFtTvxy] [-c | -C] [-e EXPR] [-o FILE] [-p PID] [-s SIZE] COMMAND
| Option | Description |
|---|---|
-f |
Follow child processes and threads. |
-p PID |
Attach to a running process (may need root or ptrace_scope 0). |
-e trace=SET |
Show only some calls: names (openat,read) or groups (file, network, process, memory, signal). |
-e status=failed |
Only calls that returned an error. |
-o FILE |
Write the trace to a file, not stderr. |
-s N |
Show strings up to N characters (default 32). |
-c |
Count calls and time per system call; print a summary at the end. |
-t, -tt, -T |
Prefix times (-tt with microseconds); -T appends the time spent in each call. |
-y |
Print the file path next to each descriptor number. |
-x |
Print non-ASCII strings in hex. |
Line format: name(arguments) = result. A failed call shows = -1 ENOENT (No such file or directory). A signal appears as --- SIGCHLD {...} ---.
Returns: the traced command's exit status, or 1 if strace itself failed.
Usage notes
- Reading the output. Start from the bottom (the last calls before a hang or crash) and search for
ENOENTandEACCESto find missing or forbidden files. - A hang with the last line
read(0,means it waits for input;futex(...)means it waits on a lock;poll/epoll_waitmeans it waits for events. - Overhead. Tracing slows a program a lot, sometimes 10 to 100 times. Do not leave it attached in production longer than needed; use
-eto cut noise. - Permissions. Tracing your own processes usually works; attaching to others needs root, and containers often block
ptraceunlessSYS_PTRACEis granted. - Traces are huge. Use
-o fileand thengrep; combine-fwith-oand-ffto get one file per process. - Library calls are shown by
ltrace, notstrace. - Related tools:
perf traceis lighter;bpftraceandlsofgive other views.