gdb常用命令

1. GDB Tutorial

    (1). 编译时加 -g 选项生成gdb所需的调试信息:

        gcc [other flags] -g <source files> -o <output file>

    (2). 显示代码信息

$gdb program
(gdb) list                        #向后显示源码信息
(gdb) list -                      #向前显示源码信息
(gdb) set listsize                #设置一次显示源代码的行数
(gdb) show listsize               #显示一次显示源代码的行数

    (3). gdb 常用命令

$gdb program  
(gdb) dir code_directory        #open a directory to load code
(gdb) b file1.c:6 if i>=5       #set a conditional breakpoint
(gdb) dis 1                     #disable a specified breakpoint
(gdb) en  1                     #enable a specified breakpoint
(gdb) del 1                     #delete a specified breakpoint
(gdb) n                         #next
(gdb) s                         #step
(gdb) bt                        #backtrace - produces a stack trace
(gdb) watch var                 #watch variable
(gdb) info b                    #shows information about all declared breakpoints
(gdb) print s.var               #see a particular field “var” of a object “s”
(gdb) printf “run1,run2=[%d,%d)
", rule->numRun1, rule->numRun2
(gdb) focus src                 #focus set to SRC window.
(gdb) focus cmd                 #focus set to CMD window.
(gdb) define pc                 #define a gdb command.
> p var1
> p var2
> end
(gdb) p array[10]@100           #print 100 element starting from index 10.
(gdb) dir /path/of/your/code
(gdb) set var=n    #set the value of var
(gdb) dump mem data.txt str_array[0] str_array[1000]
(gdb) attach pid   #attach to a program after starting gdb
(gdb) …...

    (4). ~/.gdbinit: gdb启动时会自动加载的配置脚本文件. 示例:

set print pretty on
set print elements 200
set breakpoint pending on
winheight CMD +20
focus cmd

define pxy
    printf "index=%d xy=%f %f 
", index, x, y
    p $arg0 * x
    p $arg1 * y
end

b test.cc:125

(5)堆栈的调试 

$gdb program
(gdb) bt -3                     #打印栈底下3层的栈信息
(gdb) f 0                       #查看栈顶信息
(gdb) down n                    #向栈顶移动n层
(gdb) up n                      #向栈低移动n层
(gdb) info f                    #查看当前栈层信息
(gdb) info args                 #查看当前栈层参数信息
(gdb) info locals               #查看当前栈层局部变量信息

2 打印容器:.gdbinit中添加

python
import sys 
sys.path.insert(0, '/usr/share/gcc-4.8.5/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
原文地址:https://www.cnblogs.com/yyqng/p/14696329.html