簡體   English   中英

GDB-Python腳本:迭代C / C ++結構域的任何樣本

[英]GDB-Python scripting: any samples iterating through C/C++ struct fields

新的GDB-Python腳本API看起來非常強大,應該非常有用。 但是,編寫一個有用的腳本來迭代C或C ++結構中的字段並非易事。 有沒有人知道一些確切的樣品呢?

提前致謝。


更新最終樣本:替換早期樣本中的_print_fields()

    if l.type.code == gdb.TYPE_CODE_STRUCT:
        print "Found a struct  %s " % n
        #self._print_fields(n, t)
        self._print_deep_items(n, t, l)
    else:
        print "Found no struct"

def _print_deep_items (self, n_, type_, instance_):
    for fld in type_.fields():
        fn = fld.name
        ft = fld.type
        fv = instance_[fn]
        if fv.type.code == gdb.TYPE_CODE_STRUCT:
            print "  Found a sub struct  %s " % fn
            self._print_deep_items(fn, ft, fv)
        else:
            print "    Field %s " % fn, " type %s " % ft.tag, " value %s " % fv

並輸出:

  variable s1   type S1
Found a struct  s1
    Field v1   type None   value 0
    Field v2   type None   value 0
  Found a sub struct  v3
    Field w3   type None   value 0

使用第一個示例進行更新:獲得以下示例代碼。 這不是最佳的,因為它在組成字符串字段名稱后在每個字段上進行查找。 abarnert正在展示一種有前途和優雅的方法,工作代碼在上面的最終更新部分進行了更新。

import gdb
class PrintGList(gdb.Command):
    """print fields of a struct: wzd struct_object

Iterate through the fields of a struct, and display
a human-readable form of the objects."""
    def __init__(self):
        gdb.Command.__init__(self, "wzd", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)

    def invoke(self, arg, from_tty):

        arg_list = gdb.string_to_argv(arg)
        if len(arg_list) < 1:
            print "usage: wzd struct"
            return

        n = arg_list[0]
        l = gdb.parse_and_eval(arg_list[0])
        m = l.type.tag

        print "  variable %s " % n, " type %s " % m
        try:
            t = gdb.lookup_type(m)
        except RuntimeError, e:
            print "type %s not found" % t
            return

        if l.type.code == gdb.TYPE_CODE_STRUCT:
            print "Found a struct  %s " % n
            self._print_fields(n, t)
        else:
            print "Found no struct"

    def _print_fields(self, n, typeobject):
        print typeobject
        flds = typeobject.fields()
        for x in flds:
            sn = n + "." + x.name
            print "  field %s" % sn, " code %s " % x.type.code, " type %s " % x.type.tag
            if x.type.code == gdb.TYPE_CODE_STRUCT:
                print "Found sub level struct  %s " % sn
                sl = gdb.parse_and_eval(sn)
                sm = sl.type.tag
                st = gdb.lookup_type( sm )
                self._print_fields(sn, x.type)

    def _deep_items (self, type_):
        for k, v in type_.iteritems():
            if k:
                print " k v %s " % k , " %s " % v
            else:
                print "   v    ",      " %s " % v

PrintGList()

要測試的源文件:

struct S2 {        int w3;    };
struct S1 {        int v1, v2;      struct S2 v3; } s1;
int main(int argc, char *argv[]) {   return 0; }

示例輸出:

  variable s1   type S1
Found a struct  s1
S1
  field s1.v1  typecode 8   type None
  field s1.v2  typecode 8   type None
  field s1.v3  typecode 3   type S2
Found sub level struct  s1.v3
S2
  field s1.v3.w3  typecode 8   type None

GDB會話獲取:source /home/me/testpath/wzdfile.py文件a.out b main r wzd s1 quit

根據文檔 ,迭代C結構的字段應該是非常簡單的:

如果類型是結構或類類型或枚舉類型,則可以使用Python字典語法訪問該類型的字段。 例如,如果some_type是包含結構類型的gdb.Type實例,則可以使用以下命令訪問其foo字段:

 bar = some_type['foo'] 

bar將是一個gdb.Field對象; 有關gdb.Field類的說明,請參閱下面的Type.fields方法的說明。

您也可以使用Type.fields顯式獲取struct的字段,但是(從7.4開始)您也可以使用普通的dict方法,以獲取名稱/ Field對的列表:

for name, field in foo.type.iteritems():

或者,僅為名稱:

for name, field in foo.type.iterkeys():

等等。

這似乎沒有在該頁面上直接記錄,但gdb.types在它表示deep_items時非常強烈地暗示它:

返回類似於標准gdb.Type.iteritems方法的Python迭代器。

例如,給定此C類型:

struct S {
    int x;
    int y;
};

你可以這樣做:

(gdb) python struct_S = my_s.type # or gdb.lookup_type("struct S"), etc.
(gdb) python print struct_S.keys()
{['a', 'b']}
(gdb) python print my_s['a']
0

快速瀏覽一下types.py源代碼 ,看看如何實現gdb.types.deep_item(type_) ,這似乎就是它的全部內容。


在gdb 7.4之前,您無法直接將類型視為dict 也就是說, for name in instance_.type:instance_.type.iteritems()等中沒有for name in instance_.type: 。您必須顯式調用fields 無論如何,將它們放在一起,這是一個用gdb 7.2迭代結構的所有字段的簡單示例:

for field in inst.fields:
    fname = field.name
    ftype = field.type
    fval = inst[fname]

除非你的struct有一個匿名struct ,否則這將不起作用。 為此,你需要deep_items (如果在7.2中不存在,你需要查看代碼並找出如何自己實現它)。

所以,在7.2中並不是那么簡單,但非常簡單。 而且,如果你想要瑣碎,只需升級到7.4。

暫無
暫無

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

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