C write/read detection on memory block

1.5k views Asked by At

i like to ask if someone have any idea how to detect a write on alloc memory address.

At first i used mprotect along with sigaction to force a segmentation fault when was made a write/read operation.

Two negative factor with this approach among several:

  • is the difficult to pass through a segmentation fault
  • the memory address pass in mprotect must be aligned to a page boundary i.e its not possible to handle this memory address with a simple malloc.

To clarify the problematic:

I construct a app in C for cluster environment. In some point I allocate memory that i call buffer in a local host and assign some data. This buffer will be sent to a remote node and have the same procedure. At same point this buffer will be write/read in remote node but i don't know when(it will be used DMA to write/read buffer), the local host must be notified about buffer modification. Like i said above i already used some mechanisms but none of them its capable to handle it with some ability. For now i just want some idea.

Every different idea here its welcome.

Thanks

1

There are 1 answers

2
Bart On

You could use hardware breakpoints. The downsides are that this is hardware specific and only a limited number of breakpoints can be set. Also most of the times such facilities are not task specific, so if you run multiple instances of the program they'll share the number of available 'slots'.

The x86 architecture has debug registers which can be used to set hardware memory breakpoints (see: http://en.wikipedia.org/wiki/X86_debug_register).

If you want to test this you could use GDB to set hardware breakpoints. You can use the 'watch' command of GDB to place a hardware memory breakpoint on a variable.

Note that using debug registers and mprotect() are just methods to get the job done you're asking for, I don't think they are sound engineering practices for doing memory management (what you probably try to do here). Maybe you can explain a bit more about what you trying to do at a higher level: http://catb.org/esr/faqs/smart-questions.html#goal