使用 Gdb Python API 创建新参数

Create new parameters with Gdb Python API

我不知道如何使用 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'

如何添加此文档,或者如何停止打印此 "not documented" 消息?

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

$ 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
...

这是 setshow 产生的输出:

(gdb) set test .profile
You have set test to .profile
(gdb) show test
The value of test is .profile