Skip to content

Tracing forked processes, eighteen years later

This URL held a developer's news page for a Linux debugger, and one entry on it was called "Tracing Forked Processes". That problem didn't go away. It moved.

The April 2008 capture reads like a changelog written on a Sunday. Visualising associative arrays in D, with a compiler bug fixed on the other side of the conversation. Debugging tips. And the fork entry, which ends with a crash the author finally pinned on libc after a user reported it. We aren't reproducing that text and have no connection to its author. The domain history page says who he was, and what became of his central argument is its own page.

What's worth keeping is the question underneath. A process forks. Something breaks in one of the two halves, or in the handoff between them. How do you see it?

The interactive answers, and they're good

Start with the boring news: this is solved. I pulled the flags below from the current GDB manual and the LLDB setting definition while writing this page, rather than from memory, because half of what gets repeated about fork debugging is a decade stale.

set follow-fork-mode parent | child

GDB

The default is parent, which is why a breakpoint in the child never fires and everybody decides the debugger is broken. Flip it to child. One line in a .gdbinit.

set detach-on-fork off

GDB

Keeps both halves under debugger control. From there, info inferiors lists them and inferior 2 switches. It works, it is fiddly, and I think most people bounce off it once and never come back.

catch fork, catch vfork, catch exec, catch syscall

GDB

Catchpoints stop on the event, not on a line of your code. That matters when the fork happens inside a library you did not write.

set follow-exec-mode new | same

GDB

Decides whether an exec gives you a new inferior or reuses the current one. The default is same. Worth knowing before you lose an afternoon hunting a process that never existed.

set non-stop on, set schedule-multiple

GDB

Non-stop lets you examine one stopped thread while the rest keep running, instead of freezing the world. schedule-multiple decides whether a continue resumes threads across every process or only this one.

settings set target.process.follow-fork-mode child

LLDB

Parent (the default) or child, and LLDB detaches the other. There is no equivalent of holding on to both. If your bug lives in the seam between the two, that gap is real and GDB is the tool.

Notice how short that list is. Six settings, all documented, none exotic. The tooling isn't the hard part. What eats your week is that the bug showed up once, on a build machine, at 3am, under a load you can't recreate at your desk.

When you only need to know what the child did

Sometimes you don't want a session at all. You want a transcript. Different tool.

strace -f

Follows children created by fork, vfork and clone. Output gets a [pid NNNN] prefix so you can tell who did what.

strace -ff -o trace

One file per process, named trace.PID. With four workers interleaving, that is the difference between a trace and a wall of text.

ltrace -f

Same idea one layer up, at library calls, attaching to each new child immediately. Less useful than it was, but quick when it works.

execsnoop (bcc) or execsnoop.bt (bpftrace)

The production answer. It watches exec across the whole machine without stopping anything, which is how you find the flood of short-lived processes top never shows you. Root only. And it hooks exec, so a worker that forks without exec-ing never appears.

The eBPF row is the new one, and it fixes a problem the other three share. In 2008, watching a live process tree meant attaching to it and slowing it down, which changed the timing, which hid the bug. Now you watch a production box run flat out for close to nothing, as long as the event rate stays modest. Measure that first.

rr, and where it stops

Record and replay is the one idea nobody in 2008 had shipped. rr records an execution and replays it deterministically under GDB, forwards and backwards, so the race that happened once can be examined a hundred times.

Then read the conditions. rr forces your threads onto a single core, which itself changes the timing you're chasing. Recording overhead runs 1.1x to 1.4x, though a syscall-heavy program can hit 4x. It needs hardware performance counters: Intel from Nehalem onward, certain AMD Zen and later parts, certain AArch64 designs such as Neoverse N1 and the Apple M-series. A VM guest has to virtualise those counters. The binary must not change between record and replay unless you use rr pack, and the temp directory can't be mounted noexec.

That's a shape, not a complaint. rr is a scalpel for a bug you can reproduce at least once, on hardware you control, in a program whose timing survives one core. Plenty of races don't. So what about those?

Move it left, into the gate

Here's the argument this blog exists to make. Every tool above is something you reach for after a failure. ThreadSanitizer is something you turn on before one.

Compile with -fsanitize=thread, which both Clang and GCC accept, add -g for file and line, and run the suite you already have. It's a runtime detector, so it only sees races on paths that execute, and it costs: Clang's documentation puts the slowdown at roughly 5x to 15x and memory overhead at 5x to 10x. Every object needs the flag or you get misses and false positives, statically linked libc or libstdc++ isn't supported, and GCC won't combine it with the address or leak sanitizers in one binary.

Fifteen times slower sounds fatal until you price the alternative. A four-minute suite takes forty under TSan. Run that on every pull request touching threading code and the machine finds the race while the author still remembers writing it. Is forty minutes expensive? Against an incident, a bisect across three months of commits, and an rr session on hardware that may not reproduce the thing?

So here's the part you can argue with. A test suite that retries flaky tests is buying silence, and the price is the best signal it produces. Intermittent failure in a threaded test isn't noise to be smoothed out. It's the race, telling you where it lives, before a customer does. Every retry-on-failure setting I've seen in a CI config was added by someone who knew that and was tired. Quarantine it, file it, fix it.

The bug that turned out not to be yours

Back to that 2008 entry. A user reports a crash, the author digs, and the fault turns out to sit in libc rather than his own code. It goes upstream. Short to describe, brutal to live through, and the one skill here that nothing has automated.

What does it take? Willingness to suspect the platform, which costs days if you're wrong and reads as arrogance if you say it too early. A cut-down a stranger can compile in one file. Then the report: version numbers, exact command, observed output, expected output, no theatre. Maintainers close vague bugs, and they're right to. Sanitizers point at an address. rr replays a failure. Neither writes the paragraph that convinces a libc maintainer to spend an evening on your problem.

If you followed an old link

You probably arrived from a mailing-list post or a links page written between 2006 and 2012, pointing at a numbered article on a debugger's site. That debugger stopped shipping. This site writes about code quality gates, in any language, and keeps the old URLs answering the question that was asked. The domain history explains how we got here, and debugging C++ on Linux covers what happened to the rest of that argument.