Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereference arbitrary memory location in C

I'm trying to debug a program I've written. According to the debugger a particular void * holds the value 0x804b008. I'd like to be able to dereference this value (cast it to an int * and get it's value).

I'm getting a Segmentation Error with this code. (The program with the void * is still running in the background btw - it's 'paused')

#include <stdio.h>

int main() {
    int* pVal = (int *)0x804b008;
    printf("%d", *pVal);
}

I can see why being able to deference any point in memory could be dangerous so maybe that's why this isn't working.

Thank you!

like image 592
Tyler Avatar asked Feb 16 '10 05:02

Tyler


1 Answers

Your program (running in the debugger) and this one won't be running in the same virtual memory space; accessing that pointer (even if it were a valid one) won't give you any information.

Every program running on your machine has its own logical address space. Your operating system, programming language runtime, and other factors can affect the actual literal values you see used as pointers for any given program. But one program definitely can't see into another program's memory space, barring of course software debuggers, which do some special tricks to support this behaviour.

In any case, your debugger should let you see whatever memory you want while your program is paused - assuming you have a valid address. In gdb x/x 0x804b008 would get you what you want to see.

For more information:

  1. Wikipedia article on Virtual Memory.
  2. gdb documentation
like image 102
Carl Norum Avatar answered Nov 07 '22 02:11

Carl Norum