0

I have the following code:

struct A
{
  int fieldA;
  int fieldB;
};

A *a = (A *) ptr;

cout << a->fieldA; // Works fine
cout << a->fieldB; // Works fine

ptr is a char * object that points to somewhere in memory. The object that is stored in memory at address ptr is an A object. The structure is written by another process in memory that is shared between the processes. In the process where I have the above code, the shared memory is read only.

The program being executed in on a remote target. I run the program on the target using gdb-server, and I connect to the server from my development machine using gdb.

The print statements correctly prints the expected value. However, when I print the fields of the structure from gdb by doing p a->fieldA, I get "Cannot access memory at address ...". This doesn't make sense because I would expect that since my program can access the contents of the structure, so should gdb.

Why is this happening?

user2233706
  • 6,148
  • 5
  • 44
  • 86
  • _"I would expect that since my program can access the contents of the structure, so should gdb"_ That doesn't necessarily hold. For one thing, the value of `ptr` and the value of `a` can trivially change over time. – Lightness Races in Orbit Apr 16 '15 at 22:03
  • 1
    Let me guess, `ptr` is not just an ordinary pointer allocated with `malloc` or `new`, but for example some "device memory" allocated by a driver function, or something like that? – Mats Petersson Apr 16 '15 at 22:05
  • @Mats To be specific, ```ptr``` is pointing to memory that is being written by another processor. The code where I am reading the contents at ```ptr``` is running on another processor. – user2233706 Apr 16 '15 at 22:09
  • @Lightning The address that ```ptr``` is pointing to is not changing. I observe that if I step through the print statements line-by-line, before ```cout```, gdb gives the error and after ```cout``` gdb gives the error. Yet from the program, the correct value is printed. – user2233706 Apr 16 '15 at 22:12
  • 1
    Right, so the memory is NOT "regular" memory, like I said. At least in Linux, "device memory" (memory allocated by device drivers, and similar) is not considered as readable by debugger [don't ask me exactly why, but I've run into this a few times when debugging in OpenCL, where the memory is a mapped OpenCL buffer, perfectly accessible by the processor code, but not by the debugger] – Mats Petersson Apr 16 '15 at 22:24
  • It's not really device memory. It is a region in DDR that is shared with the two processors. – user2233706 Apr 16 '15 at 22:45
  • One tip to help with gdb is to turn off optimizations (-O0), which sometimes makes memory more accessible – WakkaDojo Apr 16 '15 at 23:48

1 Answers1

2

I didn't fully explain the entire background. The memory region I am trying to access is a mmap()'d region, and gdb cannot view such regions. The following solution solution solves the problem: write a function that prints from the region, and call that function from gdb.

Community
  • 1
  • 1
user2233706
  • 6,148
  • 5
  • 44
  • 86