zoukankan      html  css  js  c++  java
  • gdb高级功能与配置

    原文地址:https://www.cnblogs.com/anhongyu/p/12709760.html

    1. 查看函数对应的汇编代码

    gdb -batch -ex "disas /m <function-name>" "./<binary-name>"

    作用:查看<binary-name>程序里<function-name>函数对应的汇编代码,"/m"修饰符同时显示源代码。

    当然,你也可以选择用objdump;相比之下,我更喜欢gdb,因为我通常只是对某一个函数的汇编代码感兴趣。

    2. 关闭new thread / thread exited消息

    调试多线程程序时,经常会被new thread / thread exited的消息刷屏,分散调试者的注意力。

    可以通过修改gdb配置文件(一般是"~/.gdbinit"文件),关闭此信息。

    set print thread-events off

    3. 屏蔽gdb signal信息

    gdb调试有时会遇到“Program received signal SIGSEGV, Segmentation fault.”的中断,执行continue之后,很快又会断住。

    解决方法是忽略相应信号,在gdb中输入如下指令

    # 查看SIGSEGV信号状态
    handle SIGSEGV
    # 关闭print
    handle SIGSEGV noprint

    参考:https://blog.csdn.net/ma100/article/details/62424408

    4. 断点命令列表

    当gdb遇到断点时,几乎总是要查看某个变量。如果反复遇到同一个断点,将反复查看相同的变量。

    如果能让gdb在每次到达某个断点时,自动执行一组命令,岂不完美。断点命令列表就是用来做这件事的。

    复制代码
    (gdb) break main.cc:13
    # 此时,你会获得一个断点号<breakpoint-number>
    
    (gdb) commands <breakpoint-number>
    >silent
    >print <var-name>
    >continue
    >end
    (gdb)
    复制代码

    需要额外解释的一点,silent命令经常是命令列表的第一条命令,以使gdb更安静地触发断点。

    5. 调度器锁

    按说在gdb里是可以通过 print 或 call 来达到调用函数的目的的,但在真实生产环境中(多线程程序的调试),往往会遇到下面的错误:

    "The program stopped in another thread while making a function call from GDB."

    出现这个错误的原因是,你在断点处调用函数的同时,其他线程也跟着运行起来了,并且有一个线程遇到了这个断点。解决的方法很简单,gdb里有一个叫做scheduler-locking的东西,在 print 或 call 之前置为“on”,打印完之后再恢复即可。

    我在.gdbinit里增加了一个扩展的打印宏,帮助我完成这个事情。

    复制代码
    define pp
      if $argc != 1
        help pp
      else
        set scheduler-locking on
        print $arg0
        set scheduler-locking step
      end 
    end
    复制代码

    补充一点,gdb默认的scheduler-locking模式是step,所以要记得恢复默认值。

    参考:https://blog.0x972.info/?d=2017/02/07/17/22/34-gdb-scheduler-locking-function-calls-and-multi-threading

    关于scheduler-locking,gdb官方文档是这么介绍的

    set scheduler-locking mode
    Set the scheduler locking mode. It applies to normal execution, record mode,
    and replay mode. If it is off, then there is no locking and any thread may
    run at any time. If on, then only the current thread may run when the inferior
    is resumed. The step mode optimizes for single-stepping; it prevents other
    threads from preempting the current thread while you are stepping, so that the
    focus of debugging does not change unexpectedly.

    show scheduler-locking
    Display the current scheduler locking mode.

    6. 我的简易版gdb配置样例

    复制代码
    set print thread-events off 
    
    define pac 
      if $argc != 1
        help pac 
      else
        print $arg0
        continue
      end 
    end
    
    document pac 
    Print And Continue
    usage: pac <var>
    end
    
    define pp
      if $argc != 1
        help pp
      else
        set scheduler-locking on
        print $arg0
        set scheduler-locking step
      end 
    end
    
    document pp
    Print in scheduler-locking mode
    usage: pp <var>
    end
    复制代码

     7. 临时向文件输出信息

    这种情况往往是因为输出信息较多,刷屏了,或者怎样。

    比如,要用到info functions输出所有函数,结果往往一屏显示不了,可以将其输出到文件。

    (gdb) set logging file <file name>
    (gdb) set logging on
    (gdb) info functions
    (gdb) set logging off

     8. 设置动态链接库搜索路径

    (gdb) set solib-search-path <path>
    (gdb) show solib-search-path
  • 相关阅读:
    asp.net url传参中使用javascript过滤中文乱码
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)【转】
    element.firstChild
    NHibernate使用Criteria分页显示并返回记录总数 【转】
    动态生成select选项全接触Javascript技术【转】
    struts中ApplicationResources.properties支持中文
    博客园博客美化方法大全[附源码]
    fileinput模块
    python学习
    遗忘的果实
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/14075746.html
Copyright © 2011-2022 走看看