gdbgdb-python

Create new parameters with Gdb Python API


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?


Solution

  • 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