J. *_*ute 7 python debugging gdb
I want to create a python pretty printer within gdb. I have a hidden data-type xyzHandle in the program I'm trying to debug. I can pull out some information from that handle with a C function; which I can call within the gdb debugger. For example:
xyzHandle object1 = api_get_first_object();
(gdb) print (char *)xyz_get_string(1, object1)
Run Code Online (Sandbox Code Playgroud)
I know I can create a gdb command like 'pn' and use that; but I want to use the build-in python of (gdb) to create a pretty-printer that if it sees 'xyzHandle' it will call 'xyz_get_string(1, object1)' and just print the returned string. That way in (gdb) I can simply type 'print object1' .
我已经阅读了如何创建一个简单的 gdb-python Pretty_printer ,但我不知道如何调用我正在从 python 调试的程序中的函数。
这是我的 xyzHandle 对象的漂亮打印机示例。
# To load this into your gdb session
#
# (gdb) python execfile ("script-name")
#
import gdb.printing
class xyzHandlePrinter:
"""Print an xyzHandle object"""
def __init__(self, val):
self.val = val
def to_string(self):
return ( str( ???how-do-I-call xyz_get_str???) )
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("XYZ Library")
pp.add_printer('xyzHandle Printer', '^xyzHandle', xyzHandlePrinter)
return pp
gdb.printing.register_pretty_printer(
gdb.current_objfile(),
build_pretty_printer())
Run Code Online (Sandbox Code Playgroud)
创建一个漂亮的打印机,如果它看到“xyzHandle”,它将调用“xyz_get_string(1, object1)”并只打印返回的字符串
这通常是一个坏主意:你漂亮的打印机将在“实时”进程中工作,但在事后(core文件)调试中会出错。如果您xyz_get_string在纯 Python 中复制 的功能,通常情况会更好。
我不知道如何调用我正在从 python 调试的程序中的函数。
你可以用gdb.parse_and_eval它。