zoukankan      html  css  js  c++  java
  • gdb使用整理

    编译

    -g

    -ggdb更多调试信息

    不要用-O,加-O后编译器会做优化,导致程序和二进制代码之间关系变得复杂,给调试带来不必要的麻烦

    启动

    gdb executable core 分析core文件

    gdb executable pid

    gdb --pid=PID attach到一个运行中的进程

    -d dir 到指定目录找源代码

    -q 不打印乱七八糟信息

    STL支持

    通过.gdbinit 进行支持,见http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt

    help pvectorhelp pmap 查看使用方式

    启动后的使用

    l test.cpp:3,5显示test.cpp 35行代码

    list –显示前面的代码 list+显示后面的代码 list +offset

    file FILE 装入想要调试的executable

    n(ext line)

    s(tep into) step进去后,如果想出来,可以用down回到上一层的位置然后用n继续

    whatis variable 查看变量的类型,ptype可以看到更加完整的定义

    info 命令查看各种信息,help info可以看到很多。常用的info thread, info breakpoints,再如info sharedlibrary, info stack

    updown命令,在函数调用栈中遍历,frame可以查看当前的栈(上下文),也可以用 f NUM 在各个栈之间切换,NUM的取值范围来自info stack

    attach PID,这个效果和启动gdb --pid PID效果一样,用来调试一个正在运行的进程。detach 断开调试

    kill 终止正在

    show 命令,使用可以用help show看,比如show args

    until 跳出当前的循环或者调用栈,跳出调用栈的时候类似down

    info locals 当前栈内的局部变量

    stepi单条机器指令

    nexti单条机器指令

    break

    break [LOCATION] [thread THREADNUM] [if CONDITION]

    break FILE:LINE

    break 38 if count==3 count

    disable NUM 取消第几个断点

    info breakpoints 的输出含义

    `Number'

    number of the breakpoint

    `Type'

    type of the breakpoint: `breakpoint' or `watchpoint'

    `Disposition'

    should the breakpoint be deleted or disabled when it is hit: `keep' or `nokeep'

    `Enabled'

    is the breakpoint enabled or no: `y' or `n'

    `Address'

    memory location at which the breakpoint is set

    `What'

    logical location of the breakpoint, expressed by function name, file name, line number

    `times'

    number of times the breakpoint has been hit

    watch

    A watchpoint stops execution of your program whenever the value of an expression changes.

    类似的还有rwatchawatch

    可以通过info watchpoints查看

    print

    print variable@NUM 查看variable,以及后面NUM个数值

    可以打印寄存器的取值有哪些寄存器可以使用info all-registers

    比如print $rax

    查找

    在代码中查找

    search

    reverse-search

    clear

    Clear breakpoint at specified line or function.

    Argument may be line number, function name, or "*" and an address.

    If line number is specified, all breakpoints in that line are cleared.

    If function is specified, breakpoints at beginning of function are cleared.

    If an address is specified, breakpoints at that address are cleared.

    With no argument, clears all breakpoints in the line that the selected frame

    is executing in.

    特殊

    某些时候,用 -g 编译过后的执行程序中只是包括了源文件的名字,没有路径名。 GDB 提供了可以让你指定源文件的路径的命令,以便 GDB 进行搜索。

    directory <dirname ... >

    dir <dirname ... >

    加一个源文件路径到当前路径的前面。如果你要指定多个路径, UNIX 下你可以使用 :

    show directories

    print的特定输出格式

    一般来说, GDB 会根据变量的类型输出变量的值。但你也可以自定义 GDB 的输出的格式。例如,你想输出一个整

    数的十六进制,或是二进制来查看这个整型变量的中的位的情况。要做到这样,你可以使用 GDB 的数据显示格式:

    x 按十六进制格式显示变量。

    d 按十进制格式显示变量。

    u 按十六进制格式显示无符号整型。

    o 按八进制格式显示变量。

    t 按二进制格式显示变量。

    a 按十六进制格式显示变量。

    c 按字符格式显示变量。

    f 按浮点数格式显示变量。

    (gdb) p i

    $21 = 101

    (gdb) p/a i

    $22 = 0x65

    (gdb) p/c i

    $23 = 101 'e'

    (gdb) p/f i

    $24 = 1.41531145e-43

    (gdb) p/x i

    $25 = 0x65

    (gdb) p/t i

    $26 = 1100101

    自动显示

    你可以设置一些自动显示的变量,当程序停住时,或是在你单步跟踪时,这些变量会自动显示。相关的 GDB 命令是

    display

    display <expr>

    display/<fmt> <expr>

    display/<fmt> <addr>

    expr 是一个表达式, fmt 表示显示的格式, addr 表示内存地址,当你用 display 设定好了一个或多个表达式后,只要你的程序被停下来, GDB 会自动显示你所设置的这些表达式的值。格式 i s 同样被 display 支持,一个非常有用的命令是:

    display/i $pc

    $pc GDB 的环境变量,表示着指令的地址, /i 则表示输出格式为机器指令码,也就是汇编。于是当程序停下后,就会出现源代码和机器指令码相对应的情形,这是一个很有意思的功能。

    下面是一些和 display 相关的 GDB 命令:

    undisplay <dnums...>

    delete display <dnums...>

    删除自动显示, dnums 意为所设置好了的自动显式的编号。如果要同时删除几个,编号可以用空格分隔,如果要删除一个范围内的编号,可以用减号表示(如: 2-5

    disable display <dnums...>

    enable display <dnums...>

    disable enalbe 不删除自动显示的设置,而只是让其失效和恢复。

    info display

    查看 display 设置的自动显示的信息。 GDB 会打出一张表格,向你报告当然调试中设置了多少个自动显示设置,其中包括,设置的编号,表达式,是否 enable

    所有功能都可以通过下面的方式获得

    (gdb) help

    List of classes of commands: gdb的命令可以分为一下几类

    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.

    每一个类的所有命令只要”help 类名就可以看到。比如:

    (gdb) help running

    Running the program.

    List of commands:

    advance -- Continue the program up to the given location (same form as args for break command)

    attach -- Attach to a process or file outside of GDB

    continue -- Continue program being debugged

    detach -- Detach a process or file previously attached

    detach checkpoint -- Detach from a fork/checkpoint (experimental)

    disconnect -- Disconnect from a target

    finish -- Execute until selected stack frame returns

    handle -- Specify how to handle a signal

    interrupt -- Interrupt the execution of the debugged program

    jump -- Continue program being debugged at specified line or address

    kill -- Kill execution of program being debugged

    next -- Step program

    nexti -- Step one instruction

    run -- Start debugged program

    signal -- Continue program giving it signal specified by the argument

    start -- Run the debugged program until the beginning of the main procedure

    step -- Step program until it reaches a different source line

    stepi -- Step one instruction exactly

    target -- Connect to a target machine or process

    target async -- Use a remote computer via a serial line

    target child -- Unix child process (started by the "run" command)

    target core -- Use a core file as a target

    target exec -- Use an executable file as a target

    target extended-async -- Use a remote computer via a serial line

    target extended-remote -- Use a remote computer via a serial line

    target multi-thread -- Threads and pthreads support

    target remote -- Use a remote computer via a serial line

    thread -- Use this command to switch between threads

    thread apply -- Apply a command to a list of threads

    thread apply all -- Apply a command to all threads

    until -- Execute until the program reaches a source line greater than the current

    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.

    backtrace 显示程序中的当前位置和表示如何到达当前位置的栈跟踪(同义词:where

    breakpoint 在程序中设置一个断点

    cd 改变当前工作目录

    clear 删除刚才停止处的断点

    commands 命中断点时,列出将要执行的命令

    continue 从断点开始继续执行

    delete 删除一个断点或监测点;也可与其他命令一起使用

    display 程序停止时显示变量和表达时

    down 下移栈帧,使得另一个函数成为当前函数

    frame 选择下一条continue命令的帧

    info 显示与该程序有关的各种信息

    jump 在源程序中的另一点开始运行

    kill 异常终止在gdb 控制下运行的程序

    list 列出相应于正在执行的程序的原文件内容

    next 执行下一个源程序行,从而执行其整体中的一个函数

    print 显示变量或表达式的值

    pwd 显示当前工作目录

    pype 显示一个数据结构(如一个结构或C++类)的内容

    quit 退出gdb

    reverse-search 在源文件中反向搜索正规表达式

    run 执行该程序

    search 在源文件中搜索正规表达式

    set variable 给变量赋值

    signal 将一个信号发送到正在运行的进程

    step 执行下一个源程序行,必要时进入下一个函数

    undisplay display命令的反命令,不要显示表达式

    until 结束当前循环

    up 上移栈帧,使另一函数成为当前函数

    watch 在程序中设置一个监测点(即数据断点)

    whatis 显示变量或函数类型

    ****************************************************

  • 相关阅读:
    《剑指offer》-判断平衡二叉树
    《剑指offer》-前n项和不准用通解和各种判断
    《剑指offer》-统计整数二进制表示中1的个数
    《剑指offer》-双栈实现队列
    《剑指offer》-数组乘积,不使用除法
    《剑指offer》-青蛙跳台阶II
    gradle入门(1-8)gradle 的依赖查看、依赖排除和指定版本(需要验证!)
    groovy入门(2-1)Groovy的Maven插件安装:Plugin execution not covered by lifecycle configuration
    zuul入门(4)zuul的注解@EnableZuulServer和@EnableZuulProxy
    zuul入门(2)zuul的过滤器分类和加载
  • 原文地址:https://www.cnblogs.com/welkinwalker/p/2254497.html
Copyright © 2011-2022 走看看