19

I am investigating a crash, based on the available core dump. The application crashing is a C++ program, built with gcc and running on RH5. The backtrace seems valid till the #1 frame. There trying to print an object I get <invalid address>, <error reading variable> Since I have the address of the object from the #2 frame is it a valid presumption that I can somehow 'dump' the memory in which the object is allocated and still collect some info. Furthermore, instead of trying to guess how the object is aligned, can I force gdb to print the address as if it is an object, even though it detects some error. My idea is that maybe the object has already been deleted, but just maybe the memory is still there and I can print some member variable.

Please comment on is that possible, and if so, how it should be done in gdb terms. 10x.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
Yordan Pavlov
  • 1,303
  • 2
  • 13
  • 26
  • 4
    Not sure if it will work, but you could try `print *((Obj*)address)`. It works sometimes for me. – JaredC Mar 21 '11 at 18:26

1 Answers1

23

Well, if you have an address you can always do:

print *(class MyClass*)pointer_var

hoc_age
  • 1,945
  • 2
  • 18
  • 28
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • Well I tried printing it that way `(gdb) p (class MyClass *)m_pointer` But I get `No struct type named MyClass` It seems like gdb do not know of my class. – Yordan Pavlov Mar 22 '11 at 14:02
  • @Yordan You of course need to cast to the structure you want to print. – Šimon Tóth Mar 22 '11 at 14:04
  • @Let_Me_Be What do you mean? Is not what the `(class MyClass *)` part does. – Yordan Pavlov Mar 22 '11 at 14:09
  • @Yordan Yes, but you don't want to print `MyClass`, you don't even have `MyClass`. `MyClass` is just an example as in: if you want to print `struct SomeCrazyStruct` put `(struct SomeCrazyStruct*)` in there. – Šimon Tóth Mar 22 '11 at 14:12
  • 1
    @Let_Me_Be Now I got what you are thinking. Well I am using the correct name of my class of course. :) I am just sticking with your MyClass example. Still gdb can not find my class, most probably because it is from a shared library. Any idea on that one? – Yordan Pavlov Mar 22 '11 at 14:33
  • @Yordan You can try -s filename when starting GDB. – Šimon Tóth Mar 22 '11 at 14:47
  • 1
    7 years late but - try without `class` - e.g. `p *(MyClass *) ` – galois Mar 08 '18 at 17:55