使用Gdb Python API创建新参数

时间:2017-05-15 15:09:05

标签: gdb gdb-python

我无法弄清楚如何使用Gdb中的Python API完全定义新参数。我提供的脚本包含以下内容:

python
param = gdb.Parameter("test", gdb.COMMAND_NONE, gdb.PARAM_OPTIONAL_FILENAME)
param.set_doc = "This is the documentation" --> throws exception
end

我使用以下命令在Gdb中更改并显示其值:

(gdb) set test "hello world"
This command is not documented.
(gdb) show test
This command is not documented. "hello world"

Gdb文档提到了Parameter.set_doc,但是当我尝试分配给它时,我得到了例外:

AttributeError: 'gdb.Parameter' object has no attribute 'set_doc'

如何添加此文档,或者如何阻止此文档"未记录"来自印刷的消息?

1 个答案:

答案 0 :(得分:1)

虽然可以通过直接实例化gdb.Parameter并稍后添加属性来在gdb中创建新参数 - 也许有人可以回答 - 通常的方法是定义一个新类,{{1的子类在该类中定义必要的属性,例如gdb.Parameter,并实例化该类。这是你的例子,重做:

set_doc

以下显示各种doc字符串的显示位置和方式:

$ cat test.py
class TestParameter(gdb.Parameter):
    """Manage the test parameter.

    Usage: set test filename
           show test
    """
    set_doc = "This is the single-line documentation for set test"
    show_doc = "This is the single-line documentation for show test"
    def __init__(self):
        super(TestParameter, self).__init__("test", gdb.COMMAND_NONE,
                                             gdb.PARAM_OPTIONAL_FILENAME)
        self.value=""
    def get_set_string(self):
        return "You have set test to " + self.value
    def get_show_string(self, _):
        return "The value of test is " + self.value

TestParameter()

$ gdb -q
(gdb) source test.py

以下是(gdb) help set test This is the single-line documentation for set test Manage the test parameter. Usage: set test filename show test (gdb) help show test This is the single-line documentation for show test Manage the test parameter. Usage: set test filename show test (gdb) help set ... List of set subcommands: ... set test -- This is the single-line documentation for set test ... set生成的输出:

show