开发者

How do I get the base type of a variable using GDB/MI

Using the GDB machine interface, is t开发者_如何学Chere a way to get the base type for a specific variable? For example, if I have a variable whose type is a uint32_t (from types.h) is there a way to get GDB to tell me that either that variable's basic type is an unsigned long int, or alternatively, that uint32_t is typedef'ed to an unsigned long int?


You can use "whatis" command

suppose you have

typedef unsigned char BYTE;
BYTE var;

(gdb)whatis var
type = BYTE
(gdb)whatis BYTE
BYTE = unsigned char


I know very little about gdb/mi; The following hacks use python to sidestep MI while being callable from the MI '-interpreter-exec' command. Probably not what you were imagining.

I didn't see anything obvious in the MI documentation -var-info-type doesn't seem to do what you want, and this is similar to bug 8143 (or i should say possible if bug 8143 were implemented):

http://sourceware.org/bugzilla/show_bug.cgi?id=8143

Part 1: implement a command that does what you want in python.

# TODO figure out how to do this without parsing the the normal gdb type = output

class basetype (gdb.Command):
    """prints the base type of expr"""

    def __init__ (self):
        super (basetype, self).__init__ ("basetype", gdb.COMMAND_OBSCURE);

    def call_recursively_until_arg_eq_ret(self, arg):
        x = arg.replace('type = ', "")
        x = gdb.execute("whatis " + x, to_string=True)
        if arg != x:
          x = self.call_recursively_until_arg_eq_ret(x).replace('type = ', "")
        return x

    def invoke (self, arg, from_tty):
        gdb.execute("ptype " + self.call_recursively_until_arg_eq_ret('type = ' + arg).replace('type = ', ""))


basetype ()

Part 2: execute it using the console interpreter

source ~/git/misc-gdb-stuff/misc_gdb/base_type.py
&"source ~/git/misc-gdb-stuff/misc_gdb/base_type.py\n"
^done
-interpreter-exec console "basetype y"
~"type = union foo_t {\n"
~"    int foo;\n"
~"    char *y;\n"
~"}\n"
^done
-interpreter-exec console "whatis y"
~"type = foo\n"
^done

Part 3:

Notice the limitations of part 2 all of your output is going to the stdout stream. If that is unacceptable you could hook open up a 2nd output channel from gdb for use with your interface and write to it with python. using something like twisted matrix, or a file.

here is an example using twisted matrix, you'd just need to switch it to direct the 'basetype' output where you want. https://gitorious.org/misc-gdb-stuff/misc-gdb-stuff/blobs/master/misc_gdb/twisted_gdb.py

otherwise you could parse the stdout stream i suppose, either way its hacks on hacks. hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜