Author Topic: [solved] Making pretty printers for gdb  (Read 4359 times)

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
[solved] Making pretty printers for gdb
« 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
« Last Edit: October 29, 2017, 09:32:00 am by visir »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Making pretty printers for gdb
« Reply #1 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.
« Last Edit: October 18, 2017, 08:00:14 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: Making pretty printers for gdb
« Reply #2 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)]

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: Making pretty printers for gdb
« Reply #3 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.
« Last Edit: October 18, 2017, 08:43:02 pm by visir »

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: Making pretty printers for gdb
« Reply #4 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.
« Last Edit: October 18, 2017, 09:49:13 pm by visir »

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: Making pretty printers for gdb
« Reply #5 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?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: [solved, sort of]Making pretty printers for gdb
« Reply #6 on: October 19, 2017, 04:15:08 pm »
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.

Offline visir

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: [solved, sort of]Making pretty printers for gdb
« Reply #7 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)


« Last Edit: October 29, 2017, 11:54:01 am by visir »