Articles
Software
Gallery
Other works
Contacts

#Simple example of a gdb pretty printer.html



            1. Assume the following type in C
                {
                    typedef struct {
                        int i;
                    } hello_world_t;
                    // and an exemplary instance
                    hello_world_t my_hw;
                }
                >NOTE: compiling with debugging information is required (-ggdb under gcc)
            2. We create a pretty printer in python
                {@begin=python@
                    # myPrettyPrinter.py
                    class mybsPrinter:
                        def to_string:
                            return "Hello World"

                    def mylookup(val):
                        if str(val.type) == "hello_world_t":
                            return mybsPrinter(val)
                        return None

                    gdb.pretty_printers.append(mylookup)
                @end=python@}
            3. Source the pretty printer code from inside gdb like so:
                    $ python execfile('myPrettyPrinter.py')             # python 2
                    $ python exec(open(('myPrettyPrinter.py').read())   # python 3
                or copy paste the whole thing into your .gdbinit after the keyword "python"
                    python
                    <...>
            4. Invoke our pretty printer by printing an appropriate instance
                $ p my_hw