4

I want to see if it is possible to get the type of a variable in gdb as a string. For example, if

int i = 1;
MyStruct *ms = NULL;

then I want to get something like

(gdb) <the-command-I-am-looking-for> i $local_var_i
(gdb) p $local_var_i
      $local_var_i = "int"
(gdb) <the-command-I-am-looking-for> ms $local_var_ms
(gdb) p $local_var_ms
      $local_var_ms = "MyStruct *"

I may have allocated the local variables before the above code segment, and the command may be a custom command.

Is such a thing possible? How could I achieve this?

Edit for clarification: I have a group of functions in my program that change name according to the type they serve (I know it's not remotely the best way to do this, but I cannot change that). I want to write a gdb function which I can feed with just the variable and the rest will be done automatically, without my intevention. Preferably, I would also like to avoid a wall of if/else if.

Gandhal
  • 43
  • 5
  • 2
    Does this answer your question? [gdb: show typeinfo of some data](https://stackoverflow.com/questions/9568201/gdb-show-typeinfo-of-some-data) – Ctx Dec 18 '19 at 14:39
  • No, it doesn't. I have already seen this answer. I want to have the type of the variable as a string for automatic usage, I don't want to copy it by hand. – Gandhal Dec 18 '19 at 14:41
  • 1
    What is the *actual* problem you need to solve? Why do you need the type as a string in a debugger variable? It's okay if it's just for curiosity (but then please state it in the question), otherwise it's better to ask about the problem you have directly instead of asking about a possible solution. Please read about [the XY problem](https://en.wikipedia.org/wiki/XY_problem), and think about how it relates to your question. – Some programmer dude Dec 18 '19 at 14:41
  • I have a group of functions in my program that change name according to the type they serve (I know it's not remotely the best way to do this, but I cannot change that). I want to write a gdb function which I can feed with just the variable and the rest will be done automatically, without my intevention. Preferably, I would also like to avoid a wall of if/else if – Gandhal Dec 18 '19 at 14:45
  • 1
    Just to be clear, given a pointer p, of type `MyCarType`, you want a way in gdb to infer the name `polymethod_MyCarType(MyCarType* ptr)` ? – Gem Taylor Dec 18 '19 at 14:58
  • Yes, that is exactly what I want. – Gandhal Dec 18 '19 at 14:59
  • Still not really sure what you are getting at. `ptype myvariable` will give you a type name for the variable type. But things like `std::string` vs `std::basic_string<...>` and so forth can make any such names in C++ sometimes confusing. – Fire Lancer Dec 18 '19 at 15:00
  • (well, maybe also `polymethod_MyCarType_withextension(MyCarType* ptr)`...) :) – Gandhal Dec 18 '19 at 15:00
  • @FireLancer Yes, what you say is true, but in my current problem I am only dealing with C structs. Maybe I should remove the C++ tag. – Gandhal Dec 18 '19 at 15:02

1 Answers1

1

If your gdb supports the python scripting extensions, you can try it like this:

define type2var
  eval "python gdb.execute(\"set $%s=\\\"\"+(gdb.execute(\"ptype %s\", False, True)).strip()+\"\\\"\")",$arg1,$arg0
end

Now you can call it like this:

>>> type2var "variable" "typevar"
>>> print $typevar
$1 = "type = char [32]"

You can of course format it further as needed using python string functions.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • It may need some tweaking, but I think I can work with this. I'll get back to you later. Thanks! – Gandhal Dec 18 '19 at 15:40
  • Thankfully, my gdb does support the `python` scripting extensions. In the end, this is what provides what I need: `eval "python gdb.execute(\"set $_type_=\\\"\"+(gdb.execute(\"whatis $arg0\", False, True)).strip()+\"\\\"\")"` And I call it like this: `type2var myvar` `print $_type_` `$1 = "type = char [32]"` As you can see, I am ok with pre-naming the local variable `$_type_`. I have not researched what the `True` and `False` arguments define, but since it works, I suppose it's not that important. Thanks a lot for the swift reply! – Gandhal Dec 18 '19 at 16:07