Skip to content

Develop custom modules

Andrea Cardaci edited this page May 16, 2021 · 2 revisions

A custom module is a Python class that inherit the Dashboard.Module class, use the following GDB command to access the documentation:

python help(Dashboard.Module)

A number of auxiliary common functions are defined in the global scope, they can be found in the provided .gdbinit and concern topics like divider formatting, conversion callbacks, etc. See the bundled default modules for some usage examples.

Default modules should provide a good example, but here is a simple module which may be used as a template for new custom modules. This module allows the programmer to note down some snippets of text during the debugging session.

from datetime import datetime

class Notes(Dashboard.Module):
    '''Simple user-defined notes.'''

    def __init__(self):
        self.notes = []

    def label(self):
        return 'Notes'

    def lines(self, term_width, term_height, style_changed):
        out = []
        for index, (ts, note) in enumerate(self.notes):
            # format the index
            index = ansi(str(index), R.style_selected_1)
            line = '[{}]'.format(index)
            # optionally add the timestamp
            if self.timestamp:
                ts = ts.strftime('%Y-%m-%d %H:%M:%S')
                line += ' {} '.format(ansi(ts, R.style_high))
            # finally add the note
            line += ' {}'.format(note)
            out.append(line)
        return out

    def add(self, arg):
        if arg:
            self.notes.append((datetime.now(), arg))
        else:
            raise Exception('Cannot add an empty note')

    def drop(self, arg):
        if arg:
            index = int(arg)
            del self.notes[index]
        else:
            raise Exception('Specify the note index')

    def clear(self, arg):
        self.notes = []

    def commands(self):
        return {
            'add': {
                'action': self.add,
                'doc': 'Add a note.'
            },
            'drop': {
                'action': self.drop,
                'doc': 'Remove a note by index.'
            },
            'clear': {
                'action': self.clear,
                'doc': 'Remove all the notes.'
            }
        }

    def attributes(self):
        return {
            'timestamp': {
                'doc': 'Show the timestamp along with the note.',
                'default': True,
                'type': bool
            }
        }

To use the above just save it as a configuration file. The following commands will be available (together with the corresponding help):

dashboard notes
dashboard notes add
dashboard notes drop
dashboard notes clear
dashboard notes -style timestamp