gdb调试过程学习

说明:

  1. next(n)表示:命令是next,n是缩写;b表示break;i b表示info break;cond表示condition;r表示run;s表示step;c表示continue;p表示print;bt表示backtrack
  2. 命令与参数之间有一个空格

调试运行环境相关命令

1、set args 可指定运行时参数。(如:set args 10 20 30 40 50 ) 

2、show args 命令可以查看设置好的运行参数。 

3、run (r) 启动调试的程序 ,可以为程序指定运行参数或者使用输入输出重定向。默认情况下,使用上次设定的参数。想无参数调试,使用set args不带参数来设置。

4、cd 相当于shell的cd命令。 

5、pwd 显示当前的所在目录。

6、info terminal显示你程序用到的终端的模式。 使用重定向控制程序输出。如:run > outfile 

7、tty命令可以设置输入输出使用的终端设备。如:tty /dev/tty1

启动GDB 的方法指定程序程序

常见的用法  

1、 gdb 程序名

2、 gdb 程序名 core

3、 gdb 程序名 进程ID

            如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。

4、quit(q)    退出GDB的调试环境

5、help  [命令名称]    在gdb环境下可以使用help命令查看命令的详细解释,如果指定了“命令名称”参数,则显示该命令的详细说明;如果没有指定参数,则分类显示所有GDB命令,供用户进一步浏览和查询。例如help next。

 

 设置断点

命令含义
break LINENUM  再某行设置断点
break FUNCTION在函数处设置断点
break FILE:LINENUM  
break FILE:FUNCTION   
info break (i b)查看断点。i是info的缩写,用于显示各类信息
clear清除指定行号或者函数的断点,参数可以是一个行号、函数、*、地址。如果没有参数,则清除堆栈执行当前的所有的断点。
tbreak 临时断点 
[Enter]直接输入回车就会执行上次的命令了。 

  断点设置的完整命令是 break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]


  LOCATION可以是一个行号、函数、*、地址。

断点的持久性:

设置监视:

watch expr 当expr的值发生改变时,程序停住.

GDB中watch是一种特殊的断点。

watch的删除方法跟断点一样,使用断点查看得到watch的编号。

 单步调试源代码

命令含义
run(r) 运行程序
next(n)  相当于vs中的F10, 逐条过程,执行一行代码,单步越过(stepping over)函数,不进入函数,函数调用当作一条简单语句执行。用法是next [N],
step(s)  相当于vs中的F11,逐条语句,进入函数,(stepping into)函数,函数调用进入被调用函数体内。用法是step [N],
until(u) 在一个循环体内单步跟踪时,这个命令可以运行程序直到退出循环体。
continue(c)相当于vs中的F5,继续执行被调试程序直到遇到另一个断点或者程序结束
stepi或si, nexti或ni单步跟踪一条机器指令,一条程序代码有可能由数条机器指令完成,stepi和nexti可以单步执行机器指令。
info program 来查看程序的是否在运行,进程号,被暂停的原因。
[Enter]直接输入回车就会执行上次的命令了。 

查看运行时数据

  1. print(p)  VAR    打印变量、字符串、表达式等的值
  2. print EXP   打印表达式值print接受一个表达式,GDB会根据当前的程序运行的数据来计算这个表达式,表达式可以是当前程序运行中的const常量、变量、函数等内容。但是GDB不能使用程序中定义的宏。如果是一个结构体的指针,打印结构体的内容使用 print *结构体指针名称。
  3. info locals    当前栈中所有局部变量的值。
  4. ptype(pt) 打印定义类型的类型。用法ptype[/FLAGS] TYPE-NAME | EXPRESSION,参数可以是用typedef 定义的 类型名称、struct STRUCT-TAG"、 "class CLASS-NAME" 、 "union UNION-TAG" 、 "enum ENUM-TAG"。在当前的栈中去搜索这个名字。与”whatis“相反,"ptype" 总是显示任何类型。FLAGS参数如下:                                                                                                       /r    原始形式输出;不替换typedefs
      /m    不打印一个类中定义的方法
      /M    打印一个类中定义的方法
      /t     不打印类中定义的typedefs
      /T    打印类中定义的typedefs
  5. whatis   打印表达式的数据类型。只显示typedefs 的第一行。
  6. print array 查看数组

    print *array@len 查看动态内存

    print x=5 改变运行时的数据

    print &array 查看数组的地址

    6.打印内存的值

         可以使用x/nfu addr命令来
         意思是以f格式,打印从addr开始的n个长度单元为u的内存值         n:输出单元的个数
         f:输出的格式,x为16进制形式,o为八进制,u为十进制,t为二进制
         u:一个单元的长度,b是一个byte,h是两个byte,w是四个byte(一个word)
 

 显示源代码

命令含义
list(l)  直接使用list没有参数,在上次list查看之后或者附近的基础上显示10行源码。不是网上说的那种直接显示调试点的源代码。
list -再显示前面的10行源码,也是在在上次list查看之后或者附近的基础上显示的。
list LINENUM一个参数,LINENUM指定一个行号,显示行号前后共10行的源码
list start_LINENUM,end_LINENUM用逗号隔开的两个参数,显示指定区间(start_LINENUM-end_LINENUM)的源码
list FILE:LINENUM查看FILE文件中的LINENUM行附近的源码
list FUNCTION查看FUNCTION开始附近的10行源码
list FILE:FUNCTION 用于区别名称相似静态函数
list  *ADDRESS显示包含该地址的行

      List命令:显示指定的函数或者行。如果两个参数的有一个参数是空的,代表从另一个参数有效的10行。

core文件调试

在程序崩溃时,一般会生成一个文件叫core文件。core文件记录的是程序崩溃时的内存映像,并加入调试信息。core文件生成的过程叫做core dump,又叫核心转储,(dump意思为堆积)

设置生成core文件

ulimit -c 查看core-dump状态

ulimit -c 数字

ulimit -c unlimited

gdb利用core文件调试

gdb 文件名 core文件

Backtrace(bt) 查看堆栈

上下移动栈帧:

函数调用期间与调用关联的运行时信息存储在栈帧(stack frame)的内存区域中.

当前的栈帧编号为0,上一个依次加一

gdb> frame 1

特殊的backtrace(bt)显示所有栈帧 

多线程调试

程序运行过程中,需要ctrl + c先让程序停下来查看。不然直接蒙了,不知道怎么调试

ctrl + c

*号表示当前线程

命令含义
info threads显示当前可调试的所有线程
thread ID切换当前调试的线程为指定ID的线程
attach process-id在gdb状态下,开始调试一个正在运行的进程
thread apply all command所有线程执行command

图形化界面方式TUI(Terminal User Interface):

gdb ./xxx -tui

可以获得gui的体验感,可以在终端窗口上部显示一个源代码查看窗。

上下键变成代码的移动了,查看历史命令使用ctrl +P或ctrl +N

替代GUI:DDD、CGDB

使用脚本设置环境变量:

如果直接执行脚本,则只会在当前shell脚本里面生效。使用source ./**.sh 执行,则可在当前shell窗口生效,

gdb --help
This is the GNU debugger.  Usage:


    gdb [options] [executable-file [core-file or process-id]]
    gdb [options] --args executable-file [inferior-arguments ...]
    gdb [options] [--python|-P] script-file [script-arguments ...]


Options:


  --args             Arguments after executable-file are passed to inferior
  -b BAUDRATE        Set serial port baud rate used for remote debugging.
  --batch            Exit after processing options.
  --batch-silent     As for --batch, but suppress all gdb stdout output.
  --return-child-result
                     GDB exit code will be the child's exit code.
  --cd=DIR           Change current directory to DIR.
  --command=FILE, -x Execute GDB commands from FILE.
  --eval-command=COMMAND, -ex
                     Execute a single GDB command.
                     May be used multiple times and in conjunction
                     with --command.
  --init-command=FILE, -ix Like -x but execute it before loading inferior.
  --init-eval-command=COMMAND, -iex Like -ex but before loading inferior.
  --core=COREFILE    Analyze the core dump COREFILE.
  --pid=PID          Attach to running process PID.
  --dbx              DBX compatibility mode.
  --directory=DIR    Search for source files in DIR.
  --exec=EXECFILE    Use EXECFILE as the executable.
  --fullname         Output information used by emacs-GDB interface.
  --help             Print this message.
  --interpreter=INTERP
                     Select a specific interpreter / user interface
  -l TIMEOUT         Set timeout in seconds for remote debugging.
  --nw               Do not use a window interface.
  --nx               Do not read any .gdbinit files.
  --nh               Do not read .gdbinit file from home directory.
  --python, -P       Following argument is Python script file; remaining
                     arguments are passed to script.
  --quiet            Do not print version number on startup.
  --readnow          Fully read symbol files on first access.
  --readnever        Do not read symbol files.
  --se=FILE          Use FILE as symbol file and executable file.
  --symbols=SYMFILE  Read symbols from SYMFILE.
  --tty=TTY          Use TTY for input/output by the program being debugged.
  --tui              Use a terminal user interface.
  --version          Print version information and then exit.
  -w                 Use a window interface.
  --write            Set writing into executable and core files.
  --xdb              XDB compatibility mode.


At startup, GDB reads the following init files and executes their commands:
   * system-wide init file: /etc/gdbinit


For more information, type "help" from within GDB, or consult the
GDB manual (available as on-line info or a printed manual).
Report bugs to "<http://www.gnu.org/software/gdb/bugs/>".

 

 

[root@localhost test]# g++ --help

Usage: g++ [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
                           Display specific types of command line options
  (Use '-v --help' to display command line options of sub-processes)
  --version                Display compiler version information
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multiarch         Display the target's normalized GNU triplet, used as
                           a component in the library path
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -print-sysroot           Display the target libraries directory
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
  -Xlinker <arg>           Pass <arg> on to the linker
  -save-temps              Do not delete intermediate files
  -save-temps=<arg>        Do not delete intermediate files
  -no-canonical-prefixes   Do not canonicalize paths when building relative
                           prefixes to other gcc components
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link
  -S                       Compile only; do not assemble or link
  -c                       Compile and assemble, but do not link
  -o <file>                Place the output into <file>
  -pie                     Create a position independent executable
  -shared                  Create a shared library
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension


Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by g++.  In order to pass
 other options on to these processes the -W<letter> options must be used.


For bug reporting instructions, please see:

<http://bugzilla.redhat.com/bugzilla>.

 

 

 

 

参考:http://blog.csdn.net/wfdtxz/article/details/7368357

http://blog.chinaunix.net/uid-9525959-id-2001805.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道格拉斯范朋克

播种花生牛奶自留田

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值