Code::Blocks Forums

User forums => Help => Topic started by: visir on October 18, 2017, 06:39:56 pm

Title: [solved] Making pretty printers for gdb
Post by: visir on October 18, 2017, 06:39:56 pm
I need to write a simple pretty printer for this:

Code
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

Code
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
Title: Re: Making pretty printers for gdb
Post by: stahta01 on October 18, 2017, 07:48:26 pm
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.

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

Tim S.
Title: Re: Making pretty printers for gdb
Post by: visir on October 18, 2017, 08:14:25 pm
Practically same

2.7.14 (default, Sep 18 2017, 09:17:44)  [GCC 7.2.0 64 bit (AMD64)]
Title: Re: Making pretty printers for gdb
Post by: visir on October 18, 2017, 08:38:22 pm
info auto-load python-scripts

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

Not loaded, but it sees it...

show auto-load python-scripts

Code
Auto-loading of Python scripts is on.
Title: Re: Making pretty printers for gdb
Post by: visir on October 18, 2017, 09:38:08 pm
I think I got it

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

Code
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)

Code
python execfile("zig-gdb.py")

Code
info pretty-printer

It reacted to str(val.type) == 'Buf', but the self.val['list']['items'].string() still doesn't work.
Title: Re: Making pretty printers for gdb
Post by: visir on October 19, 2017, 08:37:13 am
I have no idea what I'm doing

pig-gdb.py:

Code

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)

Code
python execfile("pig-gdb.py")

Spam this until it works:

p.py:

Code
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

Code
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?
Title: Re: [solved, sort of]Making pretty printers for gdb
Post by: ollydbg on October 19, 2017, 04:15:08 pm
Some hint: issue when I try to auto load GDB python pretty printer (http://forums.codeblocks.org/index.php/topic,19477.0.html)
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.
Title: Re: [solved, sort of]Making pretty printers for gdb
Post by: visir on October 29, 2017, 09:27:31 am
ollydbg, thanks, this worked.

Final Stable version

Code
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)