Search code examples
c++cgdbgdbserver

Print in program works but gdb says "Cannot access memory at address ..."


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?


Solution

  • 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.