It has been a few days, and we’ve pushed a few changes. So, you should update your gdb and rebuild it before continuing with the examples.
In addition to ordinary commands, gdb has “set/show” commands, which are basically a way to manipulate various global variables that control aspects of gdb’s behavior. The Python API to gdb calls these “parameters”, and provides a way to create them — this is similar to gdb.Command
, which we’ve already learned about, but a bit simpler.
Here’s how our “backtrace
” reimplementation (which we’ll cover in detail later) defines a new parameter for controlling how the trace is printed:
import gdb class ReverseBacktraceParameter (gdb.Parameter): """The new backtrace command can show backtraces in 'reverse' order. This means that the innermost frame will be printed last. Note that reverse backtraces are more expensive to compute.""" set_doc = "Enable or disable reverse backtraces." show_doc = "Show whether backtraces will be printed in reverse order." def __init__(self): super (ReverseBacktraceParameter, self).__init__ (self, "reverse-backtrace", gdb.COMMAND_STACK, gdb.PARAM_BOOLEAN) # Default to compatibility with gdb. self.value = False
Parameters have a name, a command category, and a type. The available types are described in the manual. The current setting is available via the “value
” attribute.
I think parameters are a nice feature when you are polishing your gdb extensions for more general use. Otherwise… we’ll, they’re there.
In an earlier post, we saw the “require
” command for the first time. This is a new command, written in Python, that dynamically loads new Python commands and functions. I wrote this command to let people experiment a bit — and I didn’t want to automatically load all the extension commands. I’m not completely sure this will stick around (your opinion matters here…), but in the meantime, you can add your own commands to it quite easily.
The “require
” command works by simply importing a Python module of the correct name. And, its completion feature works by searching a certain directory in the gdb install tree for “.py
” files in the expected package. So, adding your own commands and functions is as simple as putting them into the correct directory.
For example, we can make the above command visible like so:
$ cp /tmp/example ~/archer/install/share/gdb/python/gdb/command/reverse-param.py
Now try the require
command — completion should see your new file. If you run the require
command, “show reverse-backtrace
” should start working.
Convenience functions can be required the same way — they live in the “gdb.function
” package.
Having Python extensions which are themselves extensible — like require
— is an emerging theme of python-gdb. This sort of hackery is much more natural in a dynamic scripting language. Next time we’ll dive into another case of this: the filtering backtrace
implementation.
Be the first to leave a comment. Don’t be shy.