Skip to content

Instantly share code, notes, and snippets.

@wvdschel
Created July 3, 2012 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvdschel/3038553 to your computer and use it in GitHub Desktop.
Save wvdschel/3038553 to your computer and use it in GitHub Desktop.
Dump memory from gdb
unsigned int ReadWord(unsigned int *address)
{
return *address;
}
int DumpMemory(char *fileName, void* address, size_t size)
{
int file;
int ret = 0;
size_t written = 0;
do {
file = open(fileName, O_RDWR | O_CREAT | O_TRUNC);
} while(file == -1 && errno == EINTR);
if(file == -1) {
TraceErr(m, "Couldn't open %s to dump memory: %s", fileName, strerror(errno));
ret = -1;
}
while(written < size)
{
int w = write(file, address+written, size-written);
if(w < 0 && errno != EINTR)
{
TraceErr(m, "Write to %s failed: %s", fileName, strerror(errno));
ret = -1;
break;
} else {
written += w;
}
}
if(ret == 0 && close(file) != 0 && errno != EINTR)
{
TraceErr(m, "Closing %s failed (%s), file may be wrong or incomplete.", fileName, strerror(errno));
ret = -1;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment