Search code examples
cgdbshared-memorytracewatchpoint

Watchpoints in shared memory?


I'm debugging an issue in a patch to PostgreSQL where a word in shared memory seems to get overwritten unintentionally.

Valgrind isn't any help as it can't keep track of interactions in shared memory between multiple processes.

The address that gets overwritten is fairly stable but not totally fixed, though it's always identified by a pointer in a global struct initialized early in startup by each process.

I'm trying to find a way to get a stack trace whenever any process writes to the address of interest, but it's proving rather harder than I would've expected.

gdb watchpoints aren't any help, as gdb can't follow fork() and establish the same watch on child processes. Manually doing it with multiple gdb processes is very cumbersome due to the number of child processes PostgreSQL uses and the timing issues involved in setting it up by hand.

perf userspace probes looked promising, but seem to attach only to functions, there's no apparent way to trap a write to a memory address.

So is there any way to grab stack traces for each writer to a given shared memory address across multiple processes?


Solution

  • gdb can't follow fork() and establish the same watch on child processes

    A sufficiently recent GDB can do that. Documentation here and here.