gdb 调试命令的使用及总结

GDB: The GNU Project Debugger:http://www.gnu.org/software/gdb/documentation/

参考:http://www.jianshu.com/p/30ffc01380a0

参考:https://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/

参考:http://wiki.ubuntu.org.cn/%E7%94%A8GDB%E8%B0%83%E8%AF%95%E7%A8%8B%E5%BA%8F

参考:http://blog.csdn.net/21cnbao/article/details/7385161

GDB 常用调试命令以及多进程多线程调试:https://blog.csdn.net/freeelinux/article/details/53700266

Linux 下 GDB 用法基础教程详解:http://blog.csdn.net/cc198877/article/details/7767312

100 个 gdb 技巧:https://github.com/hellogcc/100-gdb-tips/blob/master/src/index.md

Hyperpwn:基于 gdb 的调试利器,让你的调试过程更轻松https://bbs.pediy.com/thread-257344.htm

编译时必须添加  -g 才能利用 GDB进行调试,如:gcc -g test.c -o test  这样编译生成的可执行文件才可以利用gdb进行源码调试。

-g 是在可执行文件中加入源代码的信息,比如可执行文件中第几条机器指令对应源代码的第几行,但并不是把整个源文件嵌入到可执行文件中,
所以调试时必须保证 gdb 能找到源文件。如果把当前的 gdb.c 改名为 g.c 或者将 gdb.c 移动到其他地方,则 gdb 无法进行调试。

  • -g
    Produce debugging information in the operating system’s native format.GDB can work with this debugging information.
    产生debug信息是操作系统默认格式。GDB可以使用这种格式。

  • -ggdb
    Produce debugging information for use by GDB.
    产生debug信息给GDB使用的。

  • -g3
    Request debugging information and also use level to specify how much information.The default level is 2.Level 0 produces no debug information at all.Thus -g0 negates -g.
    Level 3 includes extra information,such as all the macro definitions present in the program.Some debuggers support macro expansion when you use -g3.
    Level 1 produces minimal information,enough for making backtraces in parts of the program that you don’t plan to debug.This includes descriptions of functions and external variables,and line number tables,but no information about local variables.
    -g3 的 3表示级别,默认级别是2,级别0没有debug信息,级别3可以调试宏。

  • -gstabs          此选项以stabs格式声称调试信息,但是不包括gdb调试信息。

  • -gstabs+        此选项以stabs格式声称调试信息,并且包含仅供gdb使用的额外调试信息。

GCC 参数详解:https://www.runoob.com/w3cnote/gcc-parameter-detail.html

1. linux 下 gdb 的安装方法

gdb 是 Linux 环境下的代码调试工具,其安装步骤如下:

step1:首先检查系统中有没有安装过,有的话用一下命令卸载 gdb旧版本
    step2:在网址:http://ftp.gnu.org/gnu/gdb下载 gdb 源码包。
           或者直接在linux系统中用wget命令下载:
           wget http://ftp.gnu.org/gnu/gdb/gdb-8.0.1.tar.gz
           会下载到当前目录下。将源码包放在 home 目录的 Download 目录中。
    step3:打开Download目录,用tar -zxvf 命令解压缩你下载的源码包
    step4:.用以下命令生成makefile文件 
           1. ./configure
           2.make(这个需要的时间比较长,耐心等待哟~)
           3.sudo make install
           4.查看安装是否成功:gdb -v

查看 core 文件

产生core文件后,就可以利用命令gdb进行查找。
切换到core文件所在的目录,输入命令:gdb ./test core-test-26795-1519971969
参数一是应用程序的名称,参数二是core文件,展示错误内容,如下图所示:

(gdb)后输入where,就会看到程序崩溃时堆栈信息
(当前函数之前的所有已调用函数的列表(包括当前函数),gdb只显示最近几个)

gdb 插件 Peda、Pwndbg、Gef 的安装

http://www.peckerwood.top/post/peda_vs_pwndbg_gdb/

GDB的三个插件(gef gdbinit peda)超简单安装:https://blog.csdn.net/aoxixi/article/details/90142736

gdb/pwndbg 常用命令简单整理:https://www.cnblogs.com/zhwer/p/12494317.html

kali 下 gdb 安装 peda|pwndbg|gef 走过的坑:https://zhuanlan.zhihu.com/p/129837931

在调试时有时候需要不同功能,在gdb下需要安装两个工具 pwndbg 和 peda,可惜这两个不兼容

pwndbg 在调试堆的数据结构时候很方便。peda 在查找字符串等功能时方便

安装 pwndbg:

git clone https://github.com/pwndbg/pwndbg
cd pwndbg
./setup.sh

安装 peda:

git clone https://github.com/longld/peda.git ~/peda

安装 gef:

# via the install script
$ wget -q -O- https://github.com/hugsy/gef/raw/master/scripts/gef.sh | sh
# manually
$ wget -O ~/.gdbinit-gef.py -q https://github.com/hugsy/gef/raw/master/gef.py
$ echo source ~/.gdbinit-gef.py >> ~/.gdbinit

切换 gdb 的调试工具 pwndbg 或 peda:

vim ~/.gdbinit
source ~/peda/peda.py

把第二行添加到 gdbinit 的配置文件中去,把 pwndbg 的注释掉,即可切换为 peda

选中 gef 的话,即添加一行:source ~/.gdbinit-gef.pysource ~/.gdbinit-gef.py

2. gdb 的 图形化GUI

gdb 的 图形化

关键字:gdb前端 vim

优秀的 gdb 图形化前端调试器:https://blog.csdn.net/weixin_34242331/article/details/85905052

vi/vim使用进阶: 在VIM中使用GDB调试 – 使用vimgdb:https://www.cnblogs.com/snowbook/p/5920637.html

gdb前端: VIM+Pyclewn 调试C/C++:https://www.cnblogs.com/wucg/p/4095574.html

gdb 文本界面

gdb Text User Interface(TUI) GDB 文本用户界面

(1) 打开TUI模式
    方法一: 使用‘gdbtui’ or ‘gdb-tui’开始一个调试
            $ gdbtui -q sample
            (gdb) ....
    方法二: 使用切换键 `ctrl+x ctrl+a` or `ctrl+x A` 或者 gdb模式下输入命令:(gdb)layout
(2) TUI模式下有4个窗口,
    command 命令窗口. 可以键入调试命令
    source 源代码窗口. 显示当前行,断点等信息
    assembly 汇编代码窗口
    register 寄存器窗口
    除command 窗口外,其他三个窗口不可同时显示.其可用 layout 命令来进行选择
    自己需要的窗口. 可参见 `help layout` .
(3) 设置TUI
    set tui border-kind kind
    Select the border appearance for the source, assembly and register windows.
    The possible values are the following:
    space: Use a space character to draw the border.
    ascii: Use ascii characters ‘+’, ‘-’ and ‘|’ to draw the border.
    acs Use the Alternate Character Set to draw the border. The 
    border is
    drawn: using character line graphics if the terminal supports them.
(4) 更详尽的说明
    http://sourceware.org/gdb/current/onlinedocs/gdb_23.html#SEC235.

另:
   ctrl+x再ctrl+a: 在classic和layout两种方式之间切换gdb的显示方式。
   (layout方式的gdb很有用,但是layout方式的gdb有bug,你会用到这种快速切换的)

gdb 帮助

gdb 提供一个类似 Shell 的命令行环境,下面的 (gdb) 就是提示符,在这个提示符下输入help可以查看命令的类别。
使用 help 类别(比如 help data)可以进一步查看 data 类别下的命令帮助。

(gdb) help
下面列出了命令分类(对每个命令都可以使用:help 命令 获取帮助。例如:help info):

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

(gdb) help info

使用 gdb 进行调试

使用gdb进行调试

    1. 启动
        gdb 应用程序名
        gdb 应用程序名 core文件名
        gdb 应用程序名 pid             
        gdb 应用程序名 --args 应用程序的运行参数    //这个是在进入gdb 之前设置参数,也可以在进入gdb后设置参数。
        
    帮助:
        help    显示帮助
        info    显示程序状态
        set     修改
        show    显示gdb状态
    	
    运行及运行环境设置:
        set args                #   设置运行参数
        show args               #   显示运行参数
        set env 变量名 = 值     #   设置环境变量
        unset env [变量名]      #   取消环境变量
        show env [变量名]       #   显示环境变量
        path 目录名             #   把目录添加到查找路径中
        show paths              #   显示当前查找路径
        cd 目录                 #   切换工作目录
        pwd                     #   显示当前工作目录
        tty /dev/pts/1              #   修改程序的输入输出到指定的tty
        set inferior-tty /dev/pts/1 #   修改程序的输出到指定的tty
        show inferior-tty
        show tty
        run 参数         #   运行
        start 参数       #   开始运行,但是会在main函数停止
        attach pid
        detach
        
        kill             #   退出
        Ctrl-C           #   中断(SIGINT)
    
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值