Memory dump formatted like xxd from gdb

后端 未结 5 948
轻奢々
轻奢々 2020-12-23 02:10

I\'m trying to inspect a buffer which contains a binary formatted message, but also contains string data. As an example, I\'m using this C code:

int main (vo         


        
5条回答
  •  無奈伤痛
    2020-12-23 03:07

    So, I ended up playing around with the python interface and came up with this:

    import gdb
    from curses.ascii import isgraph
    
    def groups_of(iterable, size, first=0):
        first = first if first != 0 else size
        chunk, iterable = iterable[:first], iterable[first:]
        while chunk:
            yield chunk
            chunk, iterable = iterable[:size], iterable[size:]
    
    class HexDump(gdb.Command):
        def __init__(self):
            super (HexDump, self).__init__ ('hex-dump', gdb.COMMAND_DATA)
    
        def invoke(self, arg, from_tty):
            argv = gdb.string_to_argv(arg)
            if len(argv) != 2:
                raise gdb.GdbError('hex-dump takes exactly 2 arguments.')
            addr = gdb.parse_and_eval(argv[0]).cast(
                gdb.lookup_type('void').pointer())
            try:
                bytes = int(gdb.parse_and_eval(argv[1]))
            except ValueError:
                raise gdb.GdbError('Byte count numst be an integer value.')
    
            inferior = gdb.selected_inferior()
    
            align = gdb.parameter('hex-dump-align')
            width = gdb.parameter('hex-dump-width')
            if width == 0:
                width = 16
    
            mem = inferior.read_memory(addr, bytes)
            pr_addr = int(str(addr), 16)
            pr_offset = width
    
            if align:
                pr_offset = width - (pr_addr % width)
                pr_addr -= pr_addr % width
    
            for group in groups_of(mem, width, pr_offset):
                print '0x%x: ' % (pr_addr,) + '   '*(width - pr_offset),
                print ' '.join(['%02X' % (ord(g),) for g in group]) + \
                    '   ' * (width - len(group) if pr_offset == width else 0) + ' ',
                print ' '*(width - pr_offset) +  ''.join(
                    [g if isgraph(g) or g == ' ' else '.' for g in group])
                pr_addr += width
                pr_offset = width
    
    class HexDumpAlign(gdb.Parameter):
        def __init__(self):
            super (HexDumpAlign, self).__init__('hex-dump-align',
                                                gdb.COMMAND_DATA,
                                                gdb.PARAM_BOOLEAN)
    
        set_doc = 'Determines if hex-dump always starts at an "aligned" address (see hex-dump-width'
        show_doc = 'Hex dump alignment is currently'
    
    class HexDumpWidth(gdb.Parameter):
        def __init__(self):
            super (HexDumpWidth, self).__init__('hex-dump-width',
                                                gdb.COMMAND_DATA,
                                                gdb.PARAM_INTEGER)
    
        set_doc = 'Set the number of bytes per line of hex-dump'
    
        show_doc = 'The number of bytes per line in hex-dump is'
    
    HexDump()
    HexDumpAlign()
    HexDumpWidth()
    

    I realize it might not be the most beautiful and elegant solution, but it gets the job done and works as a first draft. It could be included in ~/.gdbinit like:

    python
    sys.path.insert(0, '/path/to/module/dir')
    import hexdump
    end
    

    Then could be used with the program above like so:

    (gdb) hex-dump buf 100
    0x7fffffffdf00:  01 02 03 04 53 74 72 69 6E 67 20 44 61 74 61 AA  ....String Data.
    0x7fffffffdf10:  BB CC 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf20:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf30:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf40:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf50:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf60:  00 00 00 00                                      ....
    

    And a few other touches for good measure:

    (gdb) set hex-dump-align on
    Determines if hex-dump always starts at an "aligned" address (see hex-dump-width
    (gdb) hex-dump &buf[5] 95
    0x7fffffffdf00:                 74 72 69 6E 67 20 44 61 74 61 AA       tring Data.
    0x7fffffffdf10:  BB CC 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf20:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf30:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf40:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf50:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x7fffffffdf60:  00 00 00 00                                      ....
    
    (gdb) set hex-dump-width 8
    Set the number of bytes per line of hex-dump
    (gdb) hex-dump &buf[5] 95
    0x7fffffffdf00:                 74 72 69       tri
    0x7fffffffdf08:  6E 67 20 44 61 74 61 AA  ng Data.
    0x7fffffffdf10:  BB CC 00 00 00 00 00 00  ........
    0x7fffffffdf18:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf20:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf28:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf30:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf38:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf40:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf48:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf50:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf58:  00 00 00 00 00 00 00 00  ........
    0x7fffffffdf60:  00 00 00 00              ....
    

    No promises that there aren't bugs :). I might stick it up in github or something if people are interested.

    I've only tested it with GDB 7.4.

提交回复
热议问题