为GDB编写一个智能调试信息浏览器,类似VS的watch多级结构查看功能

使用CODEBLOCKS中,发现WATCH窗口的结构体数据都是指针形式,而且下下级信息难以查看,严重影响调试信息的阅读,发现https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Pretty_002dPrinter.html这里有说明,简单翻译了一下,方便阅读.语句不通顺处,请见谅,有更好的翻译请告知,谢谢.
23.2.2.7 Writing a Pretty-Printer
23.2.2.7 编写一个智能展示器

A pretty-printer consists of two parts: a lookup function to detect if the type is supported, and the printer itself.

一个智能展示器由两部分组成:一个查找功能用于侦测支持的类型,和展示器自身.

Here is an example showing how a std::string printer might be written. See Pretty Printing API, for details on the API this class must provide.
有一个怎样写显示std::string展示器的例子.关于这个类必须提供的API等详情,请看 Pretty Printing API.
     class StdStringPrinter(object):
         "Print a std::string"
     
         def __init__(self, val):
             self.val = val
     
         def to_string(self):
             return self.val['_M_dataplus']['_M_p']
     
         def display_hint(self):
             return 'string'

And here is an example showing how a lookup function for the printer example above might be written.

这有个例子说明展示器的查找功能应该怎么写:

     def str_lookup_function(val):
         lookup_tag = val.type.tag
         if lookup_tag == None:
             return None
         regex = re.compile("^std::basic_string<char,.*>$")
         if regex.match(lookup_tag):
             return StdStringPrinter(val)
         return None

The example lookup function extracts the value's type, and attempts to match it to a type that it can pretty-print. If it is a type the printer can pretty-print, it will return a printer object. If not, it returns None.

这个例子中,查找功能展开类型的数值,并且试图匹配可以智能展示的类型.如果这个类型可以智能展示,将会返回一个对象的指针.如果不能,就返回none.

We recommend that you put your core pretty-printers into a Python package. If your pretty-printers are for use with a library, we further recommend embedding a version number into the package name. This practice will enable gdb to load multiple versions of your pretty-printers at the same time, because they will have different names.

我们建议你的核心智能展示器放到一个python包中.如果你的智能展示器用于一个lib,我们进一步建议嵌入一个版本号到包名中.实践证明将允许GDB同时加载你的智能展示器的多个版本,因此他们将有不同名称.

You should write auto-loaded code (see Python Auto-loading) such that it can be evaluated multiple times without changing its meaning. An ideal auto-load file will consist solely of imports of your printer modules, followed by a call to a register pretty-printers with the current objfile.

这意味着,你将写自动装载代码(看Python Auto-loading)   能模拟多次而不需更改.有个主意:自动装载文件最好仅仅包括重要的展示模块,随着一个当前OBJ文件的注册智能展示器的调用.

Taken as a whole, this approach will scale nicely to multiple inferiors, each potentially using a different library version. Embedding a version number in the Python package name will ensure that gdb is able to load both sets of printers simultaneously. Then, because the search for pretty-printers is done by objfile, and because your auto-loaded code took care to register your library's printers with a specific objfile, gdb will find the correct printers for the specific version of the library used by each inferior.

作为一个整体,这个方法将很好的衡量多级,每个潜在的使用不同的lib库版本.在PYTHON包名中嵌入一个版本号将确保GDB能够同时装载展示器集合.然后,因为已经从OBJ文件完成查找到智能展示器,并且因为你的自动装载代码用指定的OBJ文件注册了你的LIB库展示器,GDB将为每个下级LIB库找到正确版本的展示器

To continue the std::string example (see Pretty Printing API), this code might appear in gdb.libstdcxx.v6:

继续std::string 例子 (看 Pretty Printing API),这个在代码gdb.libstdcxx.v6将表现为:

     def register_printers(objfile):
         objfile.pretty_printers.append(str_lookup_function)

And then the corresponding contents of the auto-load file would be:

然后相应的自动装载文件内容为:

     import gdb.libstdcxx.v6
     gdb.libstdcxx.v6.register_printers(gdb.current_objfile())

The previous example illustrates a basic pretty-printer. There are a few things that can be improved on. The printer doesn't have a name, making it hard to identify in a list of installed printers. The lookup function has a name, but lookup functions can have arbitrary, even identical, names.

上个例子阐述了一个基本的智能展示器.还有一些改进事项.展示器没有一个名称,在安装的展示器清单中,就难以标识.查找功能有一个名称,但查找功能可以是任意的,即使是相同的名字。

Second, the printer only handles one type, whereas a library typically has several types. One could install a lookup function for each desired type in the library, but one could also have a single lookup function recognize several types. The latter is the conventional way this is handled. If a pretty-printer can handle multiple data types, then its subprinters are the printers for the individual data types.

再则,展示器仅仅处理一种类型.而LIB库通常用好几种类型.一个就能为每个所需的类型安装查找功能.但也可能一个就能识别多种类型.后者是常规的处理方式.如果一个智能展示器能处理多种数据类型,由子展示器处理单独的数据类型.

The gdb.printing module provides a formal way of solving these problems (see gdb.printing). Here is another example that handles multiple types.

gdb.printing模块提供一个解决问题的模式(看 gdb.printing).这有处理多种类型的例子:

These are the types we are going to pretty-print:

这些是我们要智能展示的类型:

     struct foo { int a, b; };
     struct bar { struct foo x, y; };

Here are the printers:

这是展示器:

     class fooPrinter:
         """Print a foo object."""
     
         def __init__(self, val):
             self.val = val
     
         def to_string(self):
             return ("a=<" + str(self.val["a"]) +
                     "> b=<" + str(self.val["b"]) + ">")
     
     class barPrinter:
         """Print a bar object."""
     
         def __init__(self, val):
             self.val = val
     
         def to_string(self):
             return ("x=<" + str(self.val["x"]) +
                     "> y=<" + str(self.val["y"]) + ">")

This example doesn't need a lookup function, that is handled by the gdb.printing module. Instead a function is provided to build up the object that handles the lookup.

这些例子不需要一个查找功能,而是由gdb.printing模块处理.提供构建一个处理查找对象的替代功能.

     import gdb.printing
     
     def build_pretty_printer():
         pp = gdb.printing.RegexpCollectionPrettyPrinter(
             "my_library")
         pp.add_printer('foo', '^foo$', fooPrinter)
         pp.add_printer('bar', '^bar$', barPrinter)
         return pp

And here is the autoload support:

自动装载支持:

     import gdb.printing
     import my_library
     gdb.printing.register_pretty_printer(
         gdb.current_objfile(),
         my_library.build_pretty_printer())

Finally, when this printer is loaded into gdb, here is the corresponding output of ‘info pretty-printer’:

最后,当这个展示器装载到GDB时,‘info pretty-printer’相应的输出信息

     (gdb) info pretty-printer
     my_library.so:
       my_library
         foo
         bar
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值