当前位置: 首页 > news >正文

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

文章目录

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

start_gdb

前言

学习 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 工具,文档比较全,如果觉的文档枯燥,也可以跟着我的总结来熟悉一下基础用法。
  • 开源环境下软件的版权信息和免责声明写的都比较完整,其中有很多描述值得我们学习。

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

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

相关文章:

  • Redis源码-BFS方式浏览main函数
  • GDB调试指北-启动调试或者附加到进程
  • Python中时间戳、时间字符串、时间结构对象之间的相互转化
  • git log根据特定条件查询日志并统计修改的代码行数
  • C++中优先队列priority_queue的基础用法
  • C++求解组合数的具体实现
  • 东拉西扯01世界的沧海桑田
  • Go语言在解决实际问题时的优点与不便
  • 使用Spreadsheet Compare工具对比Excel文件差异
  • linux环境下使用sort命令完成常见排序操作
  • 关于数据一致性的思考
  • linux环境下sed命令的基础用法
  • 学习cmake从成功编译一个小程序开始
  • linux环境下使用netstat命令查看网络信息
  • 简单聊聊01世界中编码和解码这对磨人的小妖儿
  • [译] 怎样写一个基础的编译器
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • CentOS7 安装JDK
  • C语言笔记(第一章:C语言编程)
  • gf框架之分页模块(五) - 自定义分页
  • Gradle 5.0 正式版发布
  • jdbc就是这么简单
  • JS学习笔记——闭包
  • Odoo domain写法及运用
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Phpstorm怎样批量删除空行?
  • storm drpc实例
  • Vue 2.3、2.4 知识点小结
  • webpack入门学习手记(二)
  • windows下如何用phpstorm同步测试服务器
  • 半理解系列--Promise的进化史
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 观察者模式实现非直接耦合
  • 聊一聊前端的监控
  • 盘点那些不知名却常用的 Git 操作
  • 如何进阶一名有竞争力的程序员?
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 学习JavaScript数据结构与算法 — 树
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 国内开源镜像站点
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (4) PIVOT 和 UPIVOT 的使用
  • (C语言)球球大作战
  • (Git) gitignore基础使用
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (ros//EnvironmentVariables)ros环境变量
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (黑马C++)L06 重载与继承
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐