GDB调试指北-启动GDB并查看说明信息

文章目录

  • 前言
  • gdb 版本查看
  • gdb 启动
    • 直接启动
    • 去掉版本信息启动
  • gdb 信息查看
    • show copying
    • show warranty
    • show configuration
    • apropos
    • help
  • 总结

GDB调试指北-启动GDB并查看说明信息_第1张图片

前言

学习 gdb 使用是一个漫长的过程,先了解一下 gdb 的启动方式和基础信息的查看方法,能够帮助我们更全面的认知这个工具。gdb 是一个交互式命令行程序,在使用 gdb 调试的时候不断的在命令行内输入命令,然后 gdb 程序就会给出反馈信息,这在很大程序上可以帮助我们调试程序问题。

gdb 版本查看

gdb 的安装教程网络上有很多,这里就不提供安装步骤了,可以直接通过命令行,也可以从源码安装,找个教程一步步操作就行了,安装完之后使用 which 命令查看一下程序安装的位置:

albert@home-pc:~$ which gdb
/usr/bin/gdb

确认 gdb 已经安装后我们再看一下程序版本,我用的是 Ubuntu 16.04 版本中匹配的 gdb 程序,版本稍微有些低,据说 9.x 版本中对 Python 支持的非常好,调试的时候查看变量更加方便了,这些神奇的特性我们暂时还用不到,先简单了解下就好:

albert@home-pc:~$ gdb --version
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
albert@home-pc:~$

gdb 启动

直接启动

gdb 作为一个程序和其他的程序启动方式是一样的,直接敲入 gdb 命令回车就可以了:

albert@home-pc:~$ gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)

看着是不是有些眼熟,这就是刚刚查 gdb 版本时看到的那段文字,只不过这段文字结束时不是返回到命令行,而是显现出了 (gdb) 的字样,我们暂时把它叫做 gdb 命令行,这就是我们与 gdb 程序进行交互的主要途径了。

去掉版本信息启动

上面启动 gdb 时出现的这段文字很长,有时候反复调试程序时看到这段文字有点烦,想把它去掉怎么办?非常简单,在启动时加上 -q 参数就可以了。

albert@home-pc:~$ gdb -q
(gdb)

怎么样,这次上面那段文字不见了,直接就进入 gdb 命令行了吧。

gdb 信息查看

其实刚刚被我们嫌弃的那段文字,里面记录了不少信息,其中还展示了 show copyingshow warrantyshow configuration 等多个命令,我们可以简单尝试下这些命令有什么作用。

show copying

输入 show copying 命令展示的是一份比较长的版本许可证说明,我省略了中间的部分,如果想看的话可以自己输入命令试一下,GPL v3 的许可证看起来很熟悉吧。

albert@home-pc:~$ gdb -q
(gdb) show copying
                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

...
...

  Later license versions may give you additional or different
permissions.  However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.

show warranty

命令 show warranty 输出的内容相比之前的命令就短很多了,是一份免责声明,序号从15开始,接着版本许可证的序号往下写的。

(gdb) show warranty
  15. Disclaimer of Warranty.

  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

  16. Limitation of Liability.

  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

  17. Interpretation of Sections 15 and 16.

  If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.

show configuration

最后一个 show configuration 展示的 gdb 的一下配置信息,比如 --with-system-gdbinit=/etc/gdb/gdbinit 在进阶版的 gdb 调试技巧中应该经常用到,先有个印象就行。

(gdb) show configuration
This GDB was configured as follows:
   configure --host=x86_64-linux-gnu --target=x86_64-linux-gnu
             --with-auto-load-dir=$debugdir:$datadir/auto-load
             --with-auto-load-safe-path=$debugdir:$datadir/auto-load
             --with-expat
             --with-gdb-datadir=/usr/share/gdb (relocatable)
             --with-jit-reader-dir=/usr/lib/gdb (relocatable)
             --without-libunwind-ia64
             --with-lzma
             --with-python=/usr (relocatable)
             --without-guile
             --with-separate-debug-dir=/usr/lib/debug (relocatable)
             --with-system-gdbinit=/etc/gdb/gdbinit
             --with-babeltrace

("Relocatable" means the directory can be moved with the GDB installation
tree, and GDB will still find it.)
(gdb)

apropos

其实在 gdb 启动说明中还展示了 apropos 这个命令,可以用这个命令来显示与指定词语相关的命令,比如 apropos print 就是查询所有描述中带有 print 的命令,可以执行测试一下:

(gdb) apropos print
agent-printf -- Agent-printf "printf format string"
alias -- Define a new command that is an alias of an existing command
backtrace -- Print backtrace of all stack frames
bt -- Print backtrace of all stack frames
call -- Call a function in the program
commands -- Set commands to be executed when a breakpoint is hit
compile code -- Compile
compile print -- Evaluate EXPR by using the compiler and print result
disable pretty-printer -- GDB command to disable the specified pretty-printer
...
info type-printers -- GDB command to list all registered type-printers
info vector -- Print the status of the vector unit
inspect -- Print value of expression EXP
maintenance agent-printf -- Translate an expression into remote agent bytecode for evaluation and display the bytecodes
maintenance btrace packet-history -- Print the raw branch tracing data
---Type <return> to continue, or q <return> to quit---

help

查询具体的命令可以使用 help 子命令,比如查看 bt 这个查看调用栈帧的命令就可以使用 help bt,输入后回车可以得到这个命令的描述信息。

(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
Use of the 'no-filters' qualifier prohibits frame filters from executing
on this backtrace.

gdb工作作为一款调试利器,可以使用的命令是在是太多了,除了这些还有很多命令等着我们去发现,今天的内容仅仅作为入门必备先简单了解一下。

总结

  • gdb 是一个交互式的命令行调试工具,通过不断执行命令,展示调试信息帮助我们调试程序
  • 当启动 gdb 这个工具后,命令行会变成 (gdb)的形式,等着我们输入命令开始调试使用
  • gdb 作为一个强大的 GNU 工具,文档比较全,如果觉的文档枯燥,也可以跟着我的总结来熟悉一下基础用法。
  • 开源环境下软件的版权信息和免责声明写的都比较完整,其中有很多描述值得我们学习。

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

而世之奇伟、瑰怪,非常之观,常在于险远,而人之所罕至焉,故非有志者不能至也~

你可能感兴趣的:(GDB调试指北)