1

I have files a.c and b.c which have different types with the same name:

a.c

struct map {
    int a;
}map_t;

//map_t * is use in file

b.c

struct map {
    int a;
    int b;
}map_t;

//map_t * is use in file

What I need ? Here I know heap address that I want to typecast to particular map_t in a.c or b.c file. And then want to see values a or a,b (depend on typecast).

e.g.

Address = 0x1000

I want to see this address in typedef format of map_t define in a.c file. I tried the following line in GDB but it fails to locate symbol a.c::map_t.

(GDB) p *(('a.c::map_t'*)0x1000)
No symbol "a.c::map_t" in current context.

How can I typecast address by map_t mention in a.c file ? I'd also like to know how to do same in Python within GDB.

Note: I know which memory address of which type (I mean map_t from a.c or b.c). By raw memory I can work but want to see in proper GDB format.

Rohit
  • 98
  • 4
  • Welcome to Stackoverflow. Are you asking about C or C++? Please be sure to read on [asking good questions](http://stackoverflow.com/help/how-to-ask) – user3078414 May 16 '16 at 11:51
  • 1
    Thinking twice I'd say this is not possible, as you cannot have two types of the same name both being globally accessible at the same time by definition. – alk May 16 '16 at 12:26
  • If being inside a function defined in `a.c` you get `map_t` defined in `a.c` when casting to `map_t` and if in `b.c` you get the other one. – alk May 16 '16 at 12:28
  • I do agree, but in gdb how to view heap allocated memory with typecast. – Rohit May 16 '16 at 12:44
  • Just do `p *((T*) ptr)` with `ptr` being any valid address/pointer and `T` a known, complete type. Or just use the `x ptr` command to dump memory content as address/pointer `ptr`. – alk May 16 '16 at 12:57
  • To cast to types not known because being defined local to any other module you want to define a function to the module defining the type in question taking just a `void *`, put a break point into this function then use `call functionname(ptr)` to call the function, at the breakpoint it stops the type in question is known so you can do as proposed in my last comment. – alk May 16 '16 at 13:04
  • 1
    Related: [Specifying correct type by name in gdb when multiple types of same name exist](http://stackoverflow.com/questions/32900427/specifying-correct-type-by-name-in-gdb-when-multiple-types-of-same-name-exist) – Mark Plotnick May 16 '16 at 15:53
  • The questions linked links to another question which got this answer using a neat trick: http://stackoverflow.com/a/7273785/694576 – alk May 16 '16 at 16:04
  • Thanks alk, It solved my problem – Rohit May 17 '16 at 04:56

0 Answers0