2013-05-10 22 views
0

如何爲c代碼做?..?可能嗎..?我讀過這篇文章。我也希望做類似的事情,但我無法使用給定的更新腳本在鏈接 GDB-Python scripting: any samples iterating through C/C++ struct fieldsgdb python:任何人都可以解釋我如何使用這篇文章寫的腳本?

我也跟着下面的步驟進行測試: 我的源代碼的名字是:test.c的和pretty.py

gcc -g test.c

gdb test

(gdb) source pretty.py

(gdb) run

(gdb) print <stcruct object>

如何使用這個腳本?

+0

大家好,..我等待着你的迴應....... – 2013-05-13 05:08:06

回答

2

該腳本實現了一個新的GDB命令,它採用一個C結構作爲參數。 您可以後class PrintGList

"""print fields of a struct: wzd struct_object 
Iterate through the fields of a struct, and display 
a human-readable form of the objects.""" 

從Python的文檔字符串告訴您想要的腳本來實現自定義數據類型的GDB漂亮打印機和改變,當你使用GDB的打印命令得到什麼打印,但是這不是劇本如何迷上。

類名稱PrintGList建議代碼來自在glib庫中打印鏈接列表的腳本。複製並粘貼編碼罷工再次;)我已經修復了一些小錯誤,並清理了下面的代碼(wzd.py):

import gdb 

def _type_is_container(t): 
    return t.code == gdb.TYPE_CODE_STRUCT 

class WZD(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]) 
     (t, m) = (l.type, l.type.tag) 

     print " variable %s " % n, " type %s " % t 

     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 
      if _type_is_container(x.type): 
       tag_msg = ', tag: %r' % (x.type.tag,) 
      else: 
       tag_msg = '' 
      print ' field %r type %s (code: %s%s)' % (sn, x.type, x.type.code, tag_msg) 
      if _type_is_container(x.type): 
       print "Found sub level struct %s " % sn 
       sl = gdb.parse_and_eval(sn) 
       sm = sl.type.tag 
       st = sl.type 
       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 

WZD() 

測試程序(結構-read.c):

#include <assert.h> 
#include <stdio.h> 

/* https://github.com/scottt/debugbreak */ 
#include <debugbreak/debugbreak.h> 

struct T { 
    int x, y; 
}; 

struct S { 
    struct T t; 
    char b; 
}; 

int main() 
{ 
    int r; 
    struct S s; 
    r = scanf("%d%d%c", &s.t.x, &s.t.y, &s.b); 
    assert(r == 3); 
    debug_break(); 

    return 0; 
} 

樣品GDB會話:

$ echo 1 2 x > in 
$ gdb -q -x wzd.py struct-read 
<...> 

(gdb) run < in 
<...> 
Program received signal SIGTRAP, Trace/breakpoint trap. 
main() at struct-read.c:25 
25 } 

(gdb) wzd s 
    variable s type struct S 
Found a struct s 
struct S 
    field 's.t' type struct T (code: 3, tag: 'T') 
Found sub level struct s.t 
struct T 
    field 's.t.x' type int (code: 8) 
    field 's.t.y' type int (code: 8) 
    field 's.b' type char (code: 8) 
+0

非常感謝你斯科特:-) – 2013-05-14 09:20:19

+0

@BaijnathJaiswal,不客氣。我想強調的是,上面的Python代碼實際上寫得不是很好,也不是很乾淨。如果您想要使用Python和GDB處理特定數據結構,請考慮在SO上發佈另一個問題,如果遇到問題。 – scottt 2013-05-14 09:27:38

+0

當然..!並再次感謝您的回答對我非常有幫助。:-) – 2013-05-14 09:38:14

相關問題