Useful GDB Tricks

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

时间: 2024-07-31 14:30:44

Useful GDB Tricks的相关文章

How to Find Processlist Thread id in gdb !!!!!GDB 使用

https://mysqlentomologist.blogspot.jp/2017/07/ Saturday, July 29, 2017 How to Find Processlist Thread id in gdb I was involved in a discussion on some complex MySQL-related problem where we had to study backtraces of all threads in gdb (produced by t

Awesome C

A curated list of C good stuff. I give preference to free software for code, and sellers who aren't evil for physical resources. This is released under the GNU Free Documentation License - its text is provided in the LICENSE file. Compilers Clang - A

Linux下GDB调试与对拍(先挖个坑)

应为NOIP要复赛在NOI-Linux下编写程序,所以被迫选择Vim+Gdb(主要是Guide太丑了). 虽然GUIDE的调试功能已经对付大多数的调试,反正学一学GDB的使用也没什么坏处. 1 生成调试信息 要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中.使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点.如: gcc -g hello.c -o hello g++ -g hello.cpp -o hello 如果没有-g,你将看不见程序的函数名.变量名,

gdb调试

[前言]使用gdb调试前,在编译程序时,要加 -g 选项,否则你将看不见程序的函数名.变量名,所代替的全是运行时的内存地址. 1.开始调试 a.  gdb <program> program也就是你的执行文件,一般在当前目录下. b. gdb <program> core 用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件. 2.[列出源码],从第n行开始(编译时要加 -g 选项) l n 3.[设置断点]在第N行加断点 break

gdb调试命令

本篇摘自互联网,纯属自己学习笔记,然分享给看到我的博客的人们. 用GDB调试程序 GDB是一个强大的命令行调试工具.大家知道命令行的强大就是在于,其可以形成执行序列,形成脚本.UNIX下的软件全是命令行的,这给程序开发提代供了极大的便利,命令行软件的优势在于,它们可以非常容易的集成在一起,使用几个简单的已有工具的命令,就可以做出一个非常强大的功能. 于是UNIX下的软件比Windows下的软件更能有机地结合,各自发挥各自的长处,组合成更为强劲的功能.而Windows下的图形软件基本上是各自为营,

Go语言gdb调试踩坑

整个是一个docker环境 docker版本: 1.12.1,镜像是我自己做的基于ubuntu:14.04.05. 容器操作系统版本: Ubuntu 14.04.5 LTS go版本: 1.6.3 在gdb中执行run命令出错! 错误输出: warning:Error disabling address space randomization: Operation not permitted 环境:docker 解决办法: warning:Error disabling address spac

GDB踪函数的完整调用过程 及原理

http://www.lenky.info/archives/2013/02/2202 Breakpoint 1, 0x0000003c4e417410 in open64 () from /lib64/ld-linux-x86-64.so.2 (gdb) bt #0 0x0000003c4e417410 in open64 () from /lib64/ld-linux-x86-64.so.2 #1 0x0000003c4e40f789 in _dl_sysdep_read_whole_fil

Linux GDB程序调试工具使用简介

GDB概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试工具有比VC.BCB的图形化调试器更强大的功能.所谓"寸有所长,尺有所短"就是这个道理. 一般来说,GDB主要帮忙你完成下面四个方面的功能: 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序. 可让被调试的程序在你所指定的调置的断点处停住.(断点可以是条件表达式) 当程序被停住时,

makefile gdb ps

GDB 1.调试core ulimit -c ulimited 产生段错误,目录下有core文件, gdb a.out core 2.ps常用命令http://linux.cn/article-4743-1.html pstree,ps -aux,查看线程ps -eLf 调试线程,gdb中info thread thread 9