Follow Child Processes and Threads With strace -f
Trace a whole process tree with strace -f on Linux: read the pid prefix, split output per process with -ff -o, and follow threads that do the real work.

Run strace on a shell script, a build tool or a web server and the trace is almost empty, and the part you wanted is missing. The reason is that strace traces only the process it started. Every child created with fork or clone, every program the shell launches with execve, and every thread of a multi-threaded server runs untraced unless you ask for it. The -f option fixes that: it follows each new process or thread as soon as it appears. This post explains what -f does in strace 6.8 on Linux, how to read the output it produces, how to write one file per process with -ff, and the cases where following everything is too much. It assumes you can already run strace on a single program, as in the introduction to strace.
What -f changes
A new process starts with a clone, fork or vfork system call in its parent. By default strace does not set up tracing on the child, so the child's calls are invisible. With -f, strace asks the kernel to extend the trace to every process created by a traced process, using the ptrace event options that report each new child. The manual page calls this --follow-forks, and it says the option also covers threads, because Linux creates a thread with the same clone call that creates a process, differing only in the flags.
The practical result is that the single tracee becomes a tree. Each member is a separate thread of control with its own system call stream, and strace has to interleave them in one output.
Read the pid prefix
Once there is more than one tracee, every line carries the process ID of the one that made the call. A pipeline through a shell shows it clearly:
strace -f -e trace=execve,clone sh -c 'echo hi | tr a-z A-Z'execve("/usr/bin/sh", ["sh", "-c", "echo hi | tr a-z A-Z"], 0x7ffe3a1d4c58 /* 38 vars */) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f2b9c1d8a10) = 3102
strace: Process 3102 attached
[pid 3101] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f2b9c1d8a10) = 3103
strace: Process 3103 attached
[pid 3103] execve("/usr/bin/tr", ["tr", "a-z", "A-Z"], 0x7ffe3a1d4c58 /* 38 vars */) = 0
HI
[pid 3103] +++ exited with 0 +++
[pid 3102] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3103, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++Process IDs and addresses differ on every run, and the exact interleaving can vary, because the processes really do run at the same time. Three things to notice. First, the shell is process 3101, and the first lines have no prefix because only one process was traced at that point. Second, each clone returns the new child's ID in the parent, and strace announces Process 3102 attached on standard error as it takes over the child. Third, tr runs execve in process 3103, so the program that does the real work appears under that ID, and the word HI in the middle of the trace is the pipeline's own output.
The shell in this example is dash, which implements echo as a builtin, so the left side of the pipe never runs execve. That is the kind of fact a trace shows for free: which parts of a command line are separate programs and which are not.
One file per process with -ff
A busy process tree produces an interleaved stream that is hard to read, because lines from different processes alternate. Doubling the option splits them:
strace -ff -o build.trace -e trace=execve makeWith -o and -ff, strace writes each process to its own file named after the base plus a dot and the process ID:
build.trace.4120
build.trace.4121
build.trace.4125Each file reads like an ordinary single-process trace, with no prefix on any line. This is the right choice when you want to ask what one particular child did. Find the child's ID from the clone or execve lines in the parent's file, then open its file. -ff requires -o, since there would be no place to send separate streams otherwise.
For a build, the execve-only filter doubles as a list of every command the build ran, with its full argument vector, which is often the quickest way to see the real compiler flags a build system passes:
grep -h 'execve("/usr/bin/gcc' build.trace.*Threads
Threads need the same option. A server written with a thread pool does its work in worker threads, and without -f the trace shows only the main thread sitting in futex or epoll_wait. With -f, each thread appears under its own thread ID, which Linux numbers like process IDs:
strace -f -e trace=clone3,accept4,read -p 2310 -o /tmp/server.traceA new thread appears as a clone3 call (or clone on older glibc) with CLONE_THREAD among its flags, followed by the same Process N attached message. Reading the flags is how you tell a thread from a process: a thread shares the address space and signal handlers, so its flags include CLONE_VM, CLONE_FILES, CLONE_SIGHAND and CLONE_THREAD, while a plain fork has only SIGCHLD and the tid-bookkeeping flags seen above.
Attaching with -p and -f together traces all existing threads of the process, not just the one you named, and any created later.
Cost and noise
Following everything has a price. Each additional tracee is another thread of control that strace must stop at every system call and interleave in its output, so the slowdown grows with the number of busy processes. A parallel build with a dozen compiler processes, or a server with fifty threads, can become hard to use. Three habits keep it manageable:
Filter early.
-e trace=%processshows only fork, exec and exit, which is enough to draw the tree. Add more calls only for the process you care about.Use
-ff -oand read one file, not the interleaved stream.Add
-ttso lines from different processes can be ordered by time when you do need to merge them.
For a container or a service that restarts its workers, remember that a worker which starts after you attach is only traced if -f was given when you attached.
When -f is the wrong choice
If you only care about one process in a large tree, start strace on that process by ID and leave -f off, because the extra output is pure cost. If the child is launched by a wrapper script and you want its own trace, a better approach is to run the script under strace -f -e trace=execve once to learn what it runs, then trace that command directly. And if the problem is a hang rather than a failure, an interleaved trace of many processes is exactly what makes a hang hard to read, so find the stuck process first with ps or pstree and attach to it alone.


