zoukankan      html  css  js  c++  java
  • 【持续更新】GDB使用笔记

    多文件程序的调试,例子:

    文件结构:

    /demo

      Makefile

      /src

        demo.cpp

        util.cpp

      /include

        util.h

    截图:

    ----------------------------------------------------------------------

    -----------------------------------------------------------

    注意detach和kill的区别,仔细看文档的说明:

    detach

    When you have fiished debugging the attached process, you can use the detach
    command to release it from gdb control. Detaching the process continues its
    execution. After the detach command, that process and gdb become completely 

    independent once more, and you are ready to attach another process

    or start one with run. detach does not repeat if you press RET again after
    executing the command.

    kill

    Kill the child process in which your program is running under gdb.
    This command is useful if you wish to debug a core dump instead of a running process.
    gdb ignores any core dump fie while your program is running.


    On some operating systems, a program cannot be executed outside gdb while you have
    breakpoints set on it inside gdb. You can use the kill command in this situation to permit
    running your program outside the debugger.


    The kill command is also useful if you wish to recompile and relink your program,
    since on many systems it is impossible to modify an executable fie while it is running in a
    process. In this case, when you next type run, gdb notices that the fie has changed, and
    reads the symbol table again (while trying to preserve your current breakpoint settings).

    GDB文档下载地址:https://www.gnu.org/software/gdb/documentation/

    -----------------------------------------------

    如何给被调试的程序设置命令行参数:

    gdb demo_exe

    set args <arg1> <arg2> <arg3> ...

    show args // 查看已设置的命令行参数

    或者

    gdb demo_exe

    run <arg1> <arg2> <arg3> ...

    参考资料:http://www.cnblogs.com/rosesmall/archive/2012/04/10/2440514.html

    -----------------------------------------------------------

    专题-GDB设置断点,变量观察:http://wangjinxin7.blog.163.com/blog/static/1752150162012175255611/

    专题-GDB打印变量、指针、结构:http://blog.chinaunix.net/uid-26822401-id-3155297.html

    ---------------------------------------------------------

    这里着重讲一下GDB的TUI模式,尽管没有GUI方便,不过还是比命令行要直观一些的,相比命令行界面最大的好处就是你能直观的看到你当前程序跑到哪个位置了,也不用在GDB和EDITOR之间来回切换,并且你可以在一个窗口中同时查看源代码、汇编代码、寄存器、命令行4个区域(如果你都打开的话)。

    其他方面,TUI模式与命令行模式基本没有区别

    TUI - Text User Interface,说白了,就是在command-line的窗口上画一个类似GUI的界面,效果图如下

     

    这里主要说一下最基本的也是最常用的使用方法(相关的命令),完整的文档可以去http://www.gnu.org/software/gdb/documentation/下载一个PDF来看,目前TUI的内容在Chapter 25

    1.打开/关闭TUI模式

    gdb -tui <executable-file-name>

    其中<executable-file-name>可选

    开关次模式的快捷键是CTRL+X A,意思就是你按着CTRL不放的同时,依次按X,A

    2.如果你不需要查看寄存器和汇编指令的话,只打开源代码窗口(src)和命令窗口(cmd)即可

    layout src

    3.另外,如果你需要在源代码窗口按KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_PGUP,KEY_PGDN,KEY_HOME,KEY_END进行代码查看的话,使用如下命令可以切换到带源代码窗口

    focus src

    类似的如果你需要在命令窗口使用KEY_UP,KEY_DOWN查看命令的话,使用如下命令切换到命令窗口

    focus cmd

    ------------------------------------------------------------

    命令的话,这里大多数只写缩写的格式。可以打开GDB后输入help查看帮助,或者直接去看documentation或网上的教程

    n

    单步(step over, next)

    break <function-name>

    给某个函数打断点

    break <line-number>

    在某一行打断点

    l <line-number>

    <line-number>可以不要,如果没有<line-number>,则从第一行开始列出源码(list)

    ENTER

    执行上一次命令

    info break

    查看断点信息

    r

    运行程序(run)

    p <variable-name>

    打印变量的值(print)

    bt

    查看调用栈(backtrace)

    finish

    退出函数

    c

    继续运行直到遇到下一个断点(continue)

    q

    退出GDB(quit)

    info locals

    查看局部变量

    info watch

    查看监视

    break <line-number/function-name> if <condition>

    满足<condition>的情况下在特定的行或函数中断,condition是程序中合法的boolean表达式

    condition <break-point number> <condition>

    给已存在的断点设置中断条件

    delete/disable/enable <break-point number/watch number>

    删除/启用/禁用 断点/监视

    watch <condition>

    添加监视,满足<condition>的情况下中断,condition是程序中合法的boolean表达式

    frame <#frame-number>

    切换frame(其实一个frame就是调用栈的一层),可以结合info locals查看调用栈各层的local变量,利用bt命令可以查看当前调用栈的所有frame(#0为栈顶frame)

    set <variable-name>=<variable-value>

    给变量设置新值,比如你有一个char buf[255]你可以在GDB调试的时候输入set *buf='M'从而把buf[0]修改为‘M’,你可以通过p buf来查看

    ----------------------------------------------------------------------

    我的GDB学习路线:

    // 介绍了最最最基础的命令

    http://blog.csdn.net/haoel/article/details/2879

    // 下面这一篇非常重要,必看

    http://www.ibm.com/developerworks/cn/linux/sdk/gdb/index.html

    上面那篇讲到core dump file的时候,如果发现无法产生core dump file,则参考这篇文章:http://www.cnblogs.com/qrlozte/p/4435065.html

    另外,上面那篇讲到连接进程的内容的时候,一般要用sudo命令提供root密码才行,比如

    sudo gdb demo_exe attach 28688

    或者,拆开为3条命令

    sudo gdb

    // 输入root密码

    file demo_exe

    attach 28688 // ./demo_exe & 可以查看到process id

    http://www.programlife.net/gdb-manual.html

      @

  • 相关阅读:
    MySQL ==> Maxwell ==> Kafka ==> Spark
    HBase 基本操作
    Spark Streaming 整合Kafka的 Offset 管理 【数据零丢失之 checkpoint 方式管理Offset】
    数据零丢失 + 仅一次消费数据【终极方案】
    Spark大数据相关经典面试题总结 【一直更新...】
    Spark Streaming 整合Kafka的 Offset 管理 【数据零丢失之 MySQL管理Offset】
    InfuxDB 时序数据库入门+influxdb-java
    Connection to node 0 could not be established. Broker may not be available.
    天池新人实战赛之[离线赛]-初体验-Spark处理
    Java进阶【有空常翻出来看看】
  • 原文地址:https://www.cnblogs.com/qrlozte/p/4431887.html
Copyright © 2011-2022 走看看