GDB编辑、搜索源码以及在线帮助

GDB编辑、搜索源码以及在线帮助

本节主要讲解的是在 GDB 内对源文件中的代码进行修改和查找,分别对应 GDB 中的 edit 命令和 search 命令,下面是对这两个命令的详细介绍。

GDB edit命令:编辑文件

在 GDB 中编辑源文件中使用 edit 命令,该命令的语法格式如下:

(gdb) edit [location]
(gdb) edit [filename] : [location]

location 表示程序中的位置。这个命令表示激活文件的指定位置,然后进行编辑。示例:

(gdb) edit 16            //表示激活文件中的第16 行的代码(光标定位到第 16 行代码的开头位置)
(gdb) edit func         //表示激活文件中的 func 处的代码(光标定位到 func 函数所在行的开头位置)
(gdb) edit test.c : 16 //表示激活 test.c 文件的第16 行。

GDB edit 命令编辑文件默认使用的是 ex 编译器,使用的时候可能会遇见下面的情况:

(gdb) edit
bash: /bin/ex: 没有那个文件或目录

遇到这种问题时,我们可以指定任意的编辑器(例如 Vim)去编辑文件。进入 GDB 调试器前,执行如下命令设置 EDITOR 环境变量:

export EDITOR=/usr/bin/vim

由此,当在 GDB 调试器中执行 edit 命令时,就会自动进入 Vim 编辑器,从而对当前调试的程序进行修改。
注意,上面修改编辑器的方法只是临时生效,当退出 shell 终端后配置就会被还原,下次使用的时候还要再次使用这条命令。想要永久的实现配置,需要去修改配置文件,具体做法是:修改当前 home 目录下的”~/.bashrc”文件,在文件的最后添加上述的命令就可以实现文件的永久配置(修改的是当前用户的配置文件,一般不建议修改系统的配置文件,出现问题不容易恢复)。配置完成重启 shell 终端,就可以完成配置。

GDB search命令:搜索文件

在调试文件时,某些时候可能会去找寻找某一行或者是某一部分的代码。可以使用 list 显示全部的源码,然后进行查看。当源文件的代码量较少时,我们可以使用这种方式搜索。如果源文件的代码量很大,使用这种方式寻找效率会很低。所以 GDB 中提供了相关的源代码搜索的的 search 命令。
search 命令的语法格式为:

search <regexp>
reverse-search <regexp>

第一项命令格式表示从当前行的开始向前搜索,后一项表示从当前行开始向后搜索。其中 regexp 就是正则表达式,正则表达式描述了一种字符串匹配的模式,可以用来检查一个串中是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串。很多的编程语言都支持使用正则表达式。
使用命令时可能会下面的情况:

(gdb) search func
Expression not found

表示搜索的范围没有出现要寻找的字符串或者定位到了代码行的末尾。

GDB help命令

为了降低用户使用 GDB 调试器的学习成本,GDB 提供了 help 命令,它可以帮用户打印出目标命令的功能和具体用法。首先,为了方便用户能够快速地从众多 GDB 命令中查找到目标命令,help 命令根据不同 GDB 命令的功能对它们做了分类:

(gdb) help
List of classes of commands:

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

可以看到,根据各个 GDB 命令的不同功能,help 命令将它们分成了 12 大类,每一类中都包含多个功能类似的 GDB 命令。
以 breakpoints 类为例,该类中包含了 GDB 所有的断点命令。借助 help 命令,我们可以查看某一类中具体包含的 GDB 命令:

(gdb) help breakpoints
Making program stop at certain points.

List of commands:

awatch -- Set a watchpoint for an expression
break -- Set breakpoint at specified location
break-range -- Set a breakpoint for an address range
catch -- Set catchpoints to catch events
catch assert -- Catch failed Ada assertions
catch catch -- Catch an exception
……

在此基础上,通过 help 命令,我们可以查看指定命令的功能和用法。例如:

(gdb) help break
Set breakpoint at specified line or function.
break [LOCATION] [thread THREADNUM] [if CONDITION]
LOCATION may be a line number, function name, or "*" and an address.
If a line number is specified, break at start of code for that line.
If a function is specified, break at start of code for that function.
If an address is specified, break at that exact address.
With no LOCATION, uses current execution address of the selected
stack frame.  This is useful for breaking on return to a stack frame.

THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

Multiple breakpoints at one place are permitted, and useful if their
conditions are different.

Do "help breakpoints" for info on other commands dealing with breakpoints.

可以看到,我们尝试借助 help 命令查看 break 命令,其打印信息中包括 break 命令的具体功能、完整语法格式以及各个参数的具体含义。由此,只要系统学习过目标命令的读者,借助 help 命令打印出的提示信息,一定可以回忆起来并切实用目标命令开始对程序进行调试。

其他:GDB 还支持命令的自动补全。所谓自动补全,即在 (gdb) 右侧输入 GDB 命令时,对于特别长的命令,我们只需要输入命令的前几个字符,然后按下 Tab 键,GDB 就会自动帮我们补全整个命令。

 

原文地址:https://www.cnblogs.com/jkin/p/13878003.html