gdb - frame decorator example

The FrameDecorator example given in the documentation does not work?

If you naïvely try the implement your own MyFrameFilter following the example of the InlineFilter given in the gdb (holy?) documentation [1], you may face the following python errors.

Import issue

  class MyFrameDecorator(FrameDecorator):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

According to this answer [2], This means that you are trying to inherit from a module, not from a class.

Use from gdb.FrameDecorator import FrameDecorator or gdb.FrameDecorator.FrameDecorator.

Missing member issue

Python Exception <type 'exceptions.NameError'> global name 'fobj' is not defined

fobj was not saved in the class in the constructor.

Add self.fobj = fobj.

Complete example

# Based on:
# https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Frame-Filter.html#Writing-a-Frame-Filter

import gdb
from gdb.FrameDecorator import FrameDecorator
import itertools

class MyFrameDecorator(FrameDecorator):

    def __init__(self, fobj):
        super(self.__class__, self).__init__(fobj)
        self.fobj = fobj

    def function(self):
        frame = self.fobj.inferior_frame()
        name = str(frame.name())

        return name

class MyFrameFilter():

    def __init__(self):
        # Frame filter attribute creation.
        #
        # 'name' is the name of the filter that GDB will display.
        #
        # 'priority' is the priority of the filter relative to other
        # filters.
        #
        # 'enabled' is a boolean that indicates whether this filter is
        # enabled and should be executed.

        self.name = "my_filter"
        self.priority = 100
        self.enabled = True

        # Register this frame filter with the global frame_filters
        # dictionary.
        gdb.frame_filters[self.name] = self

    def filter(self, frame_iter):
        frame_iter = itertools.imap(MyFrameDecorator, frame_iter)

        # Just return the original iterator.
        return frame_iter
# gdbinit
source /mnt/data/gdb/MyFrameFilter.py
python MyFrameFilter()

Hint

Use info frame-filter to check whether the new python filter was successfully registered.

References

[1]gdb documentation, Writing a Frame Filter, https://sourceware.org/gdb/onlinedocs/gdb/Writing-a-Frame-Filter.html
[2]Andrea Corbellini, stackoverflow answer, http://stackoverflow.com/a/39247101