1

In my code I mmap some memory and can read and write from it. For some reason when I run the code in gdb though, gbd can't access it. What's the problem?

The code in question

cout << "creating temp" << endl;
int *temp = mmap(... PROT_READ | PROT_WRITE ...);
*temp = 5;
cout << "temp = " << *temp << endl;

The output when ran

creating temp
temp = 5

My gdb prompt

(gdb) n  
77        *temp = 5;  
(gdb) p temp  
$1 = (int *)0x7fffec4000;   
(gdb) x temp  
0x7fffec4000: cannot access memory at 0x7fffec4000  
(gdb) n  
78        cout << "temp = " << *temp << endl;
(gdb) x temp  
0x7fffec4000: cannot access memory at 0x7fffec4000
(gdb) n
temp = 5
(gdb) x temp  
0x7fffec4000: cannot access memory at 0x7fffec4000
Robert
  • 203
  • 1
  • 9
  • Is that really a valid address? It reads like `deadbEEF` at a page-address. – too honest for this site Aug 10 '16 at 18:37
  • Any reason you changed the language? (and then remove the tag of the wrong language). – too honest for this site Aug 10 '16 at 18:42
  • I wrote an easily distinguishable address for the simplistic example. The actual address is a random address along the lines of 0x7fffec4000 – Robert Aug 10 '16 at 18:44
  • I'm writing in c++ but my problem comes code that's not specific to c++ – Robert Aug 10 '16 at 18:44
  • You should use the actual address. Such like the one used are missleading; good programmers trigger to patterns. – too honest for this site Aug 10 '16 at 19:27
  • 1
    What type of object is being mmapped? I've heard accounts of `ptrace` failing to access memory that has been mmapped to device special files. Also, can you add a tag that identifies your OS? – Mark Plotnick Aug 10 '16 at 20:24
  • @MarkPlotnick _"I've heard accounts"_ -> Actually, it's true. I've experienced it myself while developing on Linux for a device with memory-mapped registers and a badly coded kernel driver. The code [here](https://github.com/torvalds/linux/blob/d52bd54db8be8999df6df5a776f38c4f8b5e9cea/mm/memory.c#L3871) rejects reads or writes to `VM_IO/VM_PFNMAP` memory unless the driver defined a `vm_ops->access` getter function that encodes the knowledge about how to read that region of memory. I ended up hacking GDBserver into translating reads from that region into reads of its own device memory mapping. – Iwillnotexist Idonotexist Aug 11 '16 at 06:19
  • See http://stackoverflow.com/questions/3640095/gdb-cant-access-mmapd-kernel-allocated-memory – gilez Aug 11 '16 at 20:31

0 Answers0