Skip to content

Instantly share code, notes, and snippets.

@Preetam
Created November 21, 2013 05:30
Show Gist options
  • Save Preetam/7576522 to your computer and use it in GitHub Desktop.
Save Preetam/7576522 to your computer and use it in GitHub Desktop.
/*
∂ [src]: cc memmaps.c && ./a.out
Address of myVar: 0x7fff808cb864
Address of pointer: 0x7fff808cb878
Looks like 0x7fff808cb3b0->0x7fff808cb864 is a pointer to 0x7fff808cb864!
Looks like 0x7fff808cb770->0x7fff808cb864 is a pointer to 0x7fff808cb864!
Looks like 0x7fff808cb778->0x7fff808cb864 is a pointer to 0x7fff808cb864!
Looks like 0x7fff808cb828->0x7fff808cb864 is a pointer to 0x7fff808cb864!
Looks like 0x7fff808cb878->0x7fff808cb864 is a pointer to 0x7fff808cb864!
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void*
look_for_pointer(void* start, void* end, void* address) {
void* i;
for(i = start; i != end; i+=8) {
if( *(void**)i == address ) {
printf("Looks like %p->%p is a pointer to %p!\n", i, *(void**)i, address);
}
}
return NULL;
}
int
main(void) {
FILE* fp;
char* line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/proc/self/maps", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
int myVar = 100;
int* pointer = &myVar;
printf("Address of myVar: %p\n", &myVar);
printf("Address of pointer: %p\n", &pointer);
while ((read = getline(&line, &len, fp)) != -1) {
long start, stop;
char read, write;
sscanf(line, "%16lx-%16lx %c%c", &start, &stop, &read, &write);
if (read == 'r' && write == 'w') {
look_for_pointer((void*)start, (void*)stop, &myVar);
}
}
if (line)
free(line);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment