码迷,mamicode.com
首页 > 数据库 > 详细

Useful GDB Tricks

时间:2015-11-21 00:35:36      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

http://stackoverflow.com/questions/9233095/memory-dump-formatted-like-xxd-from-gdb

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.‘)

        print(‘dumping %d bytes from 0x%x‘ % (bytes, addr))
        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()

 http://haifux.org/lectures/210/gdb_-_customize_it.html

 

Useful GDB Tricks

标签:

原文地址:http://www.cnblogs.com/scoder/p/4982733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!