簡體   English   中英

如何使用 GDB/MI 獲取變量的基本類型

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

使用 GDB 機器接口,有沒有辦法獲取特定變量的基類型? 例如,如果我有一個類型為 uint32_t(來自 types.h)的變量,是否有辦法讓 GDB 告訴我該變量的基本類型是無符號長整型,或者 uint32_t 是 typedef 的到無符號長整數?

您可以使用“whatis”命令

假設你有

typedef unsigned char BYTE;
BYTE var;

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

我對 gdb/mi 知之甚少; 以下黑客使用 python 來回避 MI,同時可以從 MI '-interpreter-exec' 命令調用。 可能不是你想象的那樣。

我在 MI 文檔中沒有看到任何明顯的東西 -var-info-type 似乎沒有做你想做的事,這類似於 bug 8143(或者如果實現了 bug 8143,我應該說可能):

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

第 1 部分:實現在 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 ()

第 2 部分:使用控制台解釋器執行它

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

第 3 部分

請注意第 2 部分的限制,所有 output 都將轉到標准輸出 stream。 如果這是不可接受的,您可以從 gdb 打開第二個 output 通道,用於您的接口並使用 python 寫入它。 使用扭曲矩陣或文件之類的東西。

這是一個使用扭曲矩陣的示例,您只需將其切換為將“基本類型” output 指向您想要的位置。 https://gitorious.org/misc-gdb-stuff/misc-gdb-stuff/blobs/master/misc_gdb/twisted_gdb.py

否則你可以解析標准輸出 stream 我想,無論哪種方式,它都是黑客攻擊。 希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM