I am trying to detect every access programs written in C/C++ is making while running, in particular to dynamically allocated memory. In addition, I would like to have some information about where the access was made. The function name that made the access is what I am looking for.
For example, for the following code:
void MemoryAccess(int* my_vector) {
int x = my_vector[0]; // Read access from 0xFFFF0000.
my_vector[0] = x + 1; // Write access to 0XFFFF0000.
}
int main(void) {
// Assume that the address returned by malloc is 0xFFFF0000.
int* vec = malloc(VEC_SIZE * sizeof(int));
if (!vec) {
return MEMORY_ALLOCATION_FAIL;
}
MemoryAccess(vec);
int second_elem = vec[1]; // Read access from 0xFFFF0004.
v[1] = 10; // Write access to 0xFFFF0004.
return 0;
}
I would like to record a log file, containing the following: address that have been accessed, the function name that made the access. For the above example, my log should look something like:
{Read} {MemoryAccess} {0xFFFF0000}
{Write} {MemoryAccess} {0xFFFF0000}
{Read} {main} {0xFFFF0004}
{Write} {main} {0xFFFF0004}
I have been able to do something like this on Windows using Exception Filter and modify the page protection of the monitored memory block. I couldn't get the function name that made the access, only Instruction Pointer value when the exception was raised and I couldn't figure how to use that to get the function name.
Note: My goal is to monitor memory access on a Linux based system (Debian/Ubuntu mostly), not Windows.