Skip to content

Lesson 7 of 13 · 14m

strace Is a Scalpel, Not a Profiler

`strace -c` exposes the syscall truth — stat storms, tiny writes, fsync-per-record — at a price that demands short windows.

strace works by ptrace: the kernel stops the traced process at every syscall entry and exit, wakes strace, which formats the call, prints it, and lets the process resume. That is two context switches per syscall plus formatting work. On a process doing 100,000 syscalls a second, attaching strace does not just slow it down — it changes the thing being measured by an order of magnitude. Rule one: strace tells you which syscalls happen. It is the wrong tool for how long the process takes.

What it's for

strace -c aggregates: syscall name, count, error count, total seconds, microseconds per call. Five seconds of -c answers questions that take an hour of guessing — which files the process stats every request, whether it reads 4 KB at a time, whether it calls fsync per record:

strace -c -p 31337          # Ctrl-C prints the table
strace -f -tt -T -e trace=%file -o opens.txt -p 31337

-tt timestamps each call, -T appends its duration, -f follows children and threads, -e trace= narrows to a class (%file, %network, or a list like read,write), and -k prints a kernel stack per call — the "who is calling fsync" answer in one shot.

What the output actually looks like

A healthy strace -c table has a short tail: futex, epoll_wait, read, write dominating count, a few slow fsync or poll calls dominating time. The pathological version shows stat or openat at five digits a second — the tell for per-request file lookups nobody cached.

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 45.12    0.014502          12      1203           write
 22.87    0.007351        7351         1           fsync

One fsync at 7 ms is already the largest single line; issued per request, it is the request's floor. -yy resolves fd numbers to paths and sockets, -e signal=all catches the SIGPIPE/SIGSEGV a crash investigation needs, and -o keeps the log out of the terminal's own latency.

When it lies to you

Ptrace does not just slow the target — it serializes it. Threads that would run in parallel queue behind the tracer, so a strace'd process looks more contended than it is; contention findings from strace output need confirming with offcputime or perf lock, never trusted straight. The -T durations skew the same way: every call pays the stop-and-resume, so syscall-dense workloads overstate their own per-call cost.

The canonical findings are all the same shape: N small write() calls that should be one buffered write; stat() on the same path thousands of times a second because a lookup isn't cached; fsync after every log line turning 5 ms of disk latency into 5 ms per log line; open() on config files in the request path because nobody moved the read outside the handler. The syscall layer is where "the code is fine" goes to die.

The cost budget

-c still interposes on every syscall — it prints a table instead of a line, nothing more. For production syscall counting without the ptrace tax, syscount-bpfcc counts in-kernel via eBPF; perf trace is the middle ground. Keep interactive strace windows short, and never leave it attached to a hot production process to "watch for a while."

One absence that confuses people: clock_gettime and gettimeofday normally never appear in strace output at all, because the vDSO serves them in userspace with no kernel transition. If you see them as real syscalls, something — an old glibc, a seccomp filter, a clocksource the kernel marked unstable — knocked the process off the fast path.

The practice

  • Start with strace -c for the shape; switch to filtered -tt -T only for the calls that mattered.
  • Use -f on anything that forks workers — otherwise you are tracing the supervisor.
  • For always-on syscall telemetry in prod, use syscount/eBPF, not strace.
  • Missing vDSO calls are normal; seeing them is a finding, not an error.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.