0

I can't figure out how to fully define a new parameter using the Python API in Gdb. A script that I source contains the following:

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

I change and show its value in Gdb using:

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

The Gdb documentation mentions Parameter.set_doc, but when I try to assign to it I get the exception:

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

How can I add this documentation, or how can I stop this "not documented" message from printing?

gospes
  • 3,819
  • 1
  • 28
  • 31

1 Answers1

1

Although it may be possible to create a new parameter in gdb by instantiating gdb.Parameter directly and adding attributes later - maybe someone can answer that - the usual way is to define a new class, a subclass of gdb.Parameter, defining the necessary attributes such as set_doc in that class, and instantiating that class. Here's your example, reworked:

$ 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

The following shows where and how the various doc strings are displayed:

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

Here's the output produced by set and show:

(gdb) set test .profile
You have set test to .profile
(gdb) show test
The value of test is .profile
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • Great example +1! thx. Is it also possible to silence the `set` command? I'm using the `set` command in a gdb function which now prints a doc string I don't want to see. – gospes May 15 '17 at 18:20
  • I haven't found a way to stop `set` of a user-defined parameter from producing output. If you make `get_set_string` return `""`, gdb will output an empty line. If you don't define `get_set_string`, gdb will output the value of `set_doc`. If you don't define `get_set_string` and don't define `set_doc`, gdb will output `"This command is not documented."` – Mark Plotnick May 15 '17 at 18:34