News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

[solved] Making pretty printers for gdb

Started by visir, October 18, 2017, 06:39:56 PM

Previous topic - Next topic

visir

I need to write a simple pretty printer for this:

Buf *in_file_buf

struct Buf {
    PigList<char> list;
}

template<typename T>
struct PigList {
    T *items;
    size_t length;
    size_t capacity;
}


I created a <executable-name>-gdb.py with this content

import gdb

class BufPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val['list']['items'].string()

gdb.pretty_printers['^Buf *$'] = BufPrinter


Nothing changed.

Extra links:

https://sourceware.org/gdb/onlinedocs/gdb/Python-Auto_002dloading.html

http://wiki.codeblocks.org/index.php/Pretty_Printers

http://tromey.com/blog/?p=524

stahta01

#1
What version of Python is supported by the gdb you are using?

Note: I have no idea what to do after you find out the version.


(gdb) python print sys.version
2.7.14 (default, Sep 18 2017, 09:26:14)  [GCC 7.2.0 32 bit]


Tim S.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

visir

Practically same

2.7.14 (default, Sep 18 2017, 09:17:44)  [GCC 7.2.0 64 bit (AMD64)]

visir

#3
info auto-load python-scripts

Loaded  Script                                                                 
No      C:\Users\MyUser\Downloads\pig-gdb.py   


Not loaded, but it sees it...

show auto-load python-scripts

Auto-loading of Python scripts is on.

visir

#4
I think I got it

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Developer_Guide/debuggingprettyprinters.html

import gdb

class BufPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return self.val['list']['items'].string()


def lookup_type (val):
    print val.type
    if str(val.type) == 'Buf':
        print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
        return BufPrinter(val)
    else:
        print '>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<'
    return None

gdb.pretty_printers.append(lookup_type)


python execfile("zig-gdb.py")

info pretty-printer

It reacted to str(val.type) == 'Buf', but the self.val['list']['items'].string() still doesn't work.

visir

I have no idea what I'm doing

pig-gdb.py:



import gdb



def real_to_string(self):
    # print self.val
    return self.val['list']['items'].string()


def real_lookup_type(val):
    # print val.type
    # print 'fsjodifsiohfiosehfoshifhioehsfihseiofhs'
    if str(val.type) == 'Buf':
        print '!!!!!!!!!!!!!!!!!!!!!!!'
        return BufPrinter(val)
    else:
        print '======================='
    return None





class BufPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return real_to_string(self)

def lookup_type (val):
    return real_lookup_type(val)

gdb.pretty_printers.append(lookup_type)


python execfile("pig-gdb.py")

Spam this until it works:

p.py:

def real_to_string(self):
    # print self.val
    return '"' + self.val['list']['items'].string() + '"'


def real_lookup_type(val):
    # print val.type
    # print 'fsjodifsiohfiosehfoshifhioehsfihseiofhs'
    if str(val.type) == 'Buf':
        # print '!!!!!!!!!!!!!!!!!!!!!!!'
        return BufPrinter(val)
    # else:
    #     print '=======================>'
    #     print val.type
    #     print '<======================='
    return None


python execfile("p.py")

This worked, but for some reason it doesn't autoload. Well, I guess copypasting an extra line at every debug session isn't that bad. Any ideas?

ollydbg

Some hint: issue when I try to auto load GDB python pretty printer
If I remember correctly, if you have a a.dll, and a-gdb.py in the same folder, if your gdb load the a.dll, it should automatically load the py file.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

visir

#7
ollydbg, thanks, this worked.

Final Stable version

import gdb


def real_to_string(self):
    # print self.val
    return '"' + self.val['list']['items'].string() + '"'


def real_lookup_type(val):
    # print val.type
    # print 'fsjodifsiohfiosehfoshifhioehsfihseiofhs'
    if str(val.type) == 'Buf':
        # print '!!!!!!!!!!!!!!!!!!!!!!!'
        return BufPrinter(val)
    # else:
    #     print '=======================>'
    #     print val.type
    #     print '<======================='
    return None





class BufPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return real_to_string(self)

def lookup_type (val):
    return real_lookup_type(val)

gdb.pretty_printers.append(lookup_type)