Ray's Knowledge Base
Unlock

dftracer did not trace fprintf, write on a fileno fd or close on a dup'd fd

PitfallVerified 12 Oct 2026Holds project: dftracer
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

Compared with Darshan or strace, a traced run had no fprintf event, no write on fileno(fp) and only one close for fd plus dup(fd). A later write on a reused fd number was logged under the hash of a closed file.

Cause#

  • brahma binds a function only when the dftracer subclass overrides it (GOTCHA_BINDING_MACRO_IMPL in brahma include/brahma/interceptor.h). A base-class method with no override is never hooked.
  • With _FORTIFY_SOURCE and optimization, fprintf becomes __fprintf_chk. brahma needs that name too.
  • fopen and fclose open and close the fd inside libc, so the POSIX fd table never sees it. An old entry stays after fclose.
  • dup, dup2 and fcntl(F_DUPFD) did not copy the file hash to the new fd.
  • brahma had creat64 but not creat or dup3.

Fix#

  • brahma: add creat, dup3, __fprintf_chk and __vfprintf_chk. Declare __vfprintf_chk before the __fprintf_chk varargs macro, because that macro calls it.
  • dftracer: override fprintf and the _chk names. Call the real function through STDIOBypass. DFTracerLogReentryGuard stops the logger loop.
  • In STDIODFTracer::trace and remove_trace, also set the POSIX entry for fileno(fp). Untrack before the real fclose, close and freopen.
  • Copy the hash to the new fd in dup, dup2, dup3 and fcntl(F_DUPFD).

Evidence#

A C program that does fopen, fileno, write, fprintf, fclose, then dup, creat and fdopen, ran with LD_PRELOAD on manylinux_2_28 arm64. Before the fix, the trace had STDIO:fopen STDIO:fileno STDIO:fclose and no write or fprintf. The new test_fd_tracking test (plain and fortify builds) passes with exact counts write=8 close=6 fprintf=1.

Also found by searching

  • dftracer trace has no fprintf event but Darshan counts STDIO_WRITES
  • write(fileno(fp)) is missing from the dftracer trace
  • second close of a dup'd fd is not traced
  • write logged under the wrong fhash after fclose and open
  • how do I make brahma bind a new libc function