『第 31 楼』:
gdb
使用 LLM 解释/回答一下
gdb 免费编辑 添加义项名
B 添加义项 ?
所属类别 : 软件
UNIX及UNIX-like下的调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具相比于VC、z的优点是具有修复网络断点以及恢复链接等功能。BCB的图形化调试器更强大的功能。所谓"寸有所长,尺有所短"就是这个道理。
词条 百科 精彩信息一览无遗
基本信息
外文名称
GDB
发布组织
GNU开源组织
类型
强大的UNIX下的程序调试工具
功能
动态的改变你程序的执行环境等
目录
1功能
2版本发布
3文件清单
4执行程序
5显示数据
6断点
7断点管理
8变量检查赋值
9单步执行
10函数调用
11机器语言工具
12信号
13GDB使用
折叠编辑本段功能
一般来说,GDB主要帮助你完成下面四个方面的功能:
1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、你可以改变你的程序,将一个BUG产生的影响修正从而测试其他BUG。
折叠编辑本段版本发布
2009年12月29日,程序调试工具 GDB 7.0.1 发布,新版本修正了7.0版本的一些严重的堆栈溢出bug,这些bug可能导致 GDB 调试进程中断,修正了在 FreeBSD 和 IRⅨ 系统下无法编译的问题,增加了对 Thumb2调试的支持,还有其他一些小bug的修复。
2010年03月19日,GDB 7.1 发布,
详细改进内容:多程序调试的支持;
位置独立的可执行文件(派)调试的支持;
新的目标(包括一个模拟器):Xilinx MicroBlaze和瑞萨RX;
Python支持增强;
c++支持扩展;
新tracepoint功能;
过程记录的改进;
远程协议扩展。
2010年09月06日 ,GDB 7.2 发布,
该版本改进记录:
⒈ 支持D语言
⒉ C++ 改进,支持参数依赖查找ADL,静态常量类成员和改进了用户自定义操作符的支持
⒊ Python 调试的改进,包括断点、符号、符号表、程序空间、线程等可通过命令行进行操作
⒋ Furthermore,enhancements were made for tracepoints and for GDBserver.在跟踪点和GDB程序上有了改善。
⒌ 支持 ARM Symbian 平台
⒍ 其他方面的改进和bug修复。
2011年08月26日,GDB 7.3a 发布,
变化:
1。GDB可以理解线程的名字。
2。这个命令"线程名称"(指定一个名称)和"线程找到"(匹配名称、目标ID,或者额外的信息)被添加。
3。Python脚本支持是大大增强。
4。在c++的支持,异常处理是提高,模板参数放在范围在一个实例化时调试。
5。线程调试的核心转储在GNU / Linux成为可能。
6。最初支持C语言版本的OpenCL。
7。许多其他改进。
折叠编辑本段文件清单
List
(gdb) list line1,line2
查看源代码
list lineNum 在lineNum的前后源代码显示出来
list + 列出当前行的后面代码行
list - 列出当前行的前面代码行
list function
set listsize count
设置显示代码的行数
show listsize
显示打印代码的行数
list first,last
显示从first到last的源代码行
折叠编辑本段执行程序
要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符(<;和>;)和shell通配符(*、?、)在内。如果你使用不带参数的run命令,gdb就再次使用你给予前一条run命令的参数,这是很有用的。利用set args 命令就可以修改发送给程序的参数,而使用show args 命令就可以查看其缺省参数的列表。
(gdb) file a.out //加载被调试的可执行程序文件。
(gdb)set args –b –x
(gdb) show args
(gdb)r //执行程序
backtrace命令为堆栈提供向后跟踪功能。
Backtrace 命令产生一张列表,包含着从最近的过程开始的所有有效过程和调用这些过程的参数。
折叠编辑本段显示数据
利用print 命令可以检查各个变量的值。
(gdb) print p (p为变量名)
print 是gdb的一个功能很强的命令,利用它可以显示被调试的语言中任何有效的表达式。表达式除了包含你程序中的变量外,还可以包含以下内容:
对程序中函数的调用
(gdb) print find_entry(1,0)
数据结构和其他复杂对象
(gdb) print *table_start
={e=reference='\000',location=0x0,next=0x0}
值的历史成分
(gdb)print (为历史记录变量,在以后可以直接引用的值)
人为数组
人为数组提供了一种去显示存储器块(数组节或动态分配的存储区)内容的方法。早期的调试程序没有很好的方法将任意的指针换成一个数组。就像对待参数一样,让我们查看内存中在变量h后面的10个整数,一个动态数组的语法如下所示:
base@length
因此,要想显示在h后面的10个元素,可以使用h@10:
(gdb)print h@10
=(-1,345,23,-234,0,0,0,98,345,10)
whatis命令可以显示某个变量的类型
(gdb) whatis p
type = int *
折叠编辑本段断点
break命令(可以简写为b)可以用来在调试的程序中设置断点,该命令有如下四种形式:
break line-number 使程序恰好在执行给定行之前停止。
break function-name 使程序恰好在进入指定的函数之前停止。
break line-or-function if condition 如果condition(条件)是真,程序到达指定行或函数时停止。
break routine-name 在指定例程的入口处设置断点
如果该程序是由很多原文件构成的,你可以在各个原文件中设置断点,而不是在当前的原文件中设置断点,其方法如下:
(gdb) break filename:line-number
(gdb) break filename:function-name
要想设置一个条件断点,可以利用break if命令,如下所示:
(gdb) break line-or-function if expr
例:
(gdb) break 46 if testsize==100
从断点继续运行:continue 命令
折叠编辑本段断点管理
折叠1.显示当前gdb的断点信息:
(gdb) info break
他会以如下的形式显示所有的断点信息:
Num Type Disp Enb Address What
1 breakpoint keep y 0x000028bc in init_random at qsort2.c:155
2 breakpoint keep y 0x0000291c in init_organ at qsort2.c:168
删除指定的某个断点:
(gdb) delete breakpoint 1
该命令将会删除编号为1的断点,如果不带编号参数,将删除所有的断点
(gdb) delete breakpoint
禁止使用某个断点
(gdb) disable breakpoint 1
该命令将禁止断点1,同时断点信息的 (Enb)域将变为 n
允许使用某个断点
(gdb) enable breakpoint 1
该命令将允许断点1,同时断点信息的 (Enb)域将变为 y
清除源文件中某一代码行上的所有断点
(gdb)clear number
注:number 为源文件的某个代码行的行号
折叠2.设置条件断点
例子:
gdb可以设置条件断点,也就是只有在条件满足时,断点才会被触发,命令是"break … if cond"。以上面程序为例:
可以看到设定断点只在i的值为101时触发,此时打印sum的值为5050。
折叠编辑本段变量检查赋值
whatis:识别数组或变量的类型
ptype:比whatis的功能更强,他可以提供一个结构的定义
set variable = value:将值赋予变量
print variable = value or p variable = value : 除了显示一个变量的值外,还可以用来赋值
折叠编辑本段单步执行
next 不进入的单步执行
step 进入的单步执行如果已经进入了某函数,而想退出该函数返回到它的调用函数中,可使用命令finish
折叠编辑本段函数调用
call name 调用和执行一个函数
(gdb) call gen_and_sork(1234,1,0)
(gdb) call printf("abcd")
=4
finish 结束执行当前函数,显示其返回值(如果有的话)
折叠编辑本段机器语言工具
有一组专用的gdb变量可以用来检查和修改计算机的通用寄存器,gdb提供了目 前每一台计算机中实际使用的4个寄存器的标准名字:
$pc :程序计数器
$fp :帧指针(当前堆栈帧)
$sp :栈指针
$ps :处理器状态
折叠编辑本段信号
gdb通常可以捕捉到发送给它的大多数信号,通过捕捉信号,它就可决定对于正在运行的进程要做些什么工作。例如,按CTRL-C将中断信号发送给gdb,通常就会终止gdb。但是你或许不想中断gdb,真正的目的是要中断gdb正在运行的程序,因此,gdb要抓住该信号并停止它正在运行的程序,这样就可以执行某些调试操作。
Handle命令可控制信号的处理,他有两个参数,一个是信号名,另一个是接受到信号时该作什么。几种可能的参数是:
nostop 接收到信号时,不要将它发送给程序,也不要停止程序。
stop 接受到信号时停止程序的执行,从而允许程序调试;显示一条表示已接受到信号的消息(禁止使用消息除外)
print 接受到信号时显示一条消息
noprint 接受到信号时不要显示消息(而且隐含着不停止程序运行)
pass 将信号发送给程序,从而允许你的程序去处理它、停止运行或采取别的动作。
nopass 停止程序运行,但不要将信号发送给程序。
例如,假定你截获SIGPIPE信号,以防止正在调试的程序接受到该信号,而且只要该信号一到达,就要求该程序停止,并通知你。要完成这一任务,可利用如下命令:
(gdb) handle SIGPIPE stop print
请注意,UNⅨ的信号名总是采用大写字母!你可以用信号编号替代信号名如果你的程序要执行任何信号处理操作,就需要能够测试其信号处理程序,为此,就需要一种能将信号发送给程序的简便方法,这就是signal命令的任务。该命令的参数是一个数字或者一个名字,如SIGINT。假定你的程序已将一个专用的SIGINT(键盘输入,或CTRL-C;信号2)信号处理程序设置成采取某个清理动作,要想测试该信号处理程序,你可以设置一个断点并使用如下命令:
(gdb) signal 2
continuing with signal SIGINT⑵
该程序继续执行,但是立即传输该信号,而且处理程序开始运行。
折叠编辑本段GDB使用
GDB是一个强大的命令行调试工具。大家知道命令行的强大就是在于,其可以形成
执行序列,形成脚本。UNⅨ下的软件全是命令行的,这给程序开发提代供了极大的
便利,命令行软件的优势在于,它们可以非常容易的集成在一起,使用几个简单的已
有工具的命令,就可以做出一个非常强大的功能。
于是UNⅨ下的软件比Windows下的软件更能有机地结合,各自发挥各自的长处,组合
成更为强劲的功能。而Windows下的图形软件基本上是各自为营,互相不能调用,很
不利于各种软件的相互集成。在这里并不是要和Windows做个什么比较,所谓"寸有
所长,尺有所短",图形化工具还有时不如命令行的地方。
Last edited by zzz19760225 on 2017-11-13 at 14:07 ]
gdb Free Edit Add Term
B Add Term?
Belongs to the category: Software
Debugging tool under UNIX and UNIX-like. Perhaps, everyone may prefer the graphical interface way, like the debugging of IDEs such as VC, BCB, etc., but if you are doing software under the UNIX platform, you will find that the advantage of the GDB debugging tool compared with VC, z is that it has functions such as repairing network breakpoints and restoring links. The graphical debugger of BCB has more powerful functions. The so-called "each has its own strengths and weaknesses" is the reason.
Entry Encyclopedia Wonderful Information at a Glance
Basic Information
Foreign Name
GDB
Publishing Organization
GNU Open Source Organization
Type
Powerful program debugging tool under UNIX
Function
Dynamically change the execution environment of your program, etc.
Table of Contents
1 Function
2 Version Release
3 File List
4 Executing Program
5 Displaying Data
6 Breakpoints
7 Breakpoint Management
8 Variable Inspection and Assignment
9 Step-by-Step Execution
10 Function Call
11 Machine Language Tool
12 Signals
13 GDB Usage
Folding Edit Section Function
Generally speaking, GDB mainly helps you complete the following four aspects of functions:
1. Start your program, and you can run the program as you want according to your custom requirements.
2. Let the program being debugged stop at the set breakpoint. (The breakpoint can be a conditional expression)
3. When the program is stopped, you can check what is happening in your program at this time.
4. You can change your program, correct the impact caused by a bug, and then test other bugs.
Folding Edit Section Version Release
On December 29, 2009, the program debugging tool GDB 7.0.1 was released. The new version corrected some serious stack overflow bugs of the 7.0 version, which may cause the GDB debugging process to be interrupted. It corrected the problems that could not be compiled under FreeBSD and IRⅨ systems, added support for Thumb2 debugging, and other small bug fixes.
On March 19, 2010, GDB 7.1 was released,
Detailed improvement content: support for multi-program debugging;
Support for debugging position-independent executables (Pie);
New targets (including an emulator): Xilinx MicroBlaze and Renesas RX;
Enhanced Python support;
Extended C++ support;
New tracepoint function;
Improvement of process recording;
Remote protocol extension.
On September 6, 2010, GDB 7.2 was released,
Improvement records of this version:
1. Support for D language
2. C++ improvement, support for argument-dependent lookup ADL, static constant class members and improved support for user-defined operators
3. Improvement of Python debugging, including breakpoints, symbols, symbol tables, program spaces, threads, etc., which can be operated through the command line
4. Furthermore, enhancements were made for tracepoints and for GDBserver. Improvements were made in tracepoints and GDBserver.
5. Support for ARM Symbian platform
6. Other improvements and bug fixes.
On August 26, 2011, GDB 7.3a was released,
Changes:
1. GDB can understand the name of the thread.
2. The commands "thread name" (specify a name) and "thread find " (match name, target ID, or additional information) are added.
3. Python script support is greatly enhanced.
4. In C++ support, exception handling is improved, and template parameters are placed in the scope when debugging an instance.
5. Core dump of thread debugging under GNU/Linux becomes possible.
6. Initial support for the C language version of OpenCL.
7. Many other improvements.
Folding Edit Section File List
List
(gdb) list line1,line2
View the source code
list lineNum Display the source code before and after lineNum
list + List the code lines after the current line
list - List the code lines before the current line
list function
set listsize count
Set the number of lines to display code
show listsize
Display the number of lines to print code
list first,last
Display the source code lines from first to last
Folding Edit Section Executing Program
To run the program to be debugged, you can use the run command. After it, you can follow any parameters sent to the program, including standard input and output specifiers (<; and>;) and shell wildcards (*,?, ) and so on. If you use the run command without parameters, gdb will use the parameters you gave to the previous run command again, which is very useful. You can use the set args command to modify the parameters sent to the program, and use the show args command to view the list of its default parameters.
(gdb) file a.out //Load the executable program file to be debugged.
(gdb) set args –b –x
(gdb) show args
(gdb) r //Execute the program
The backtrace command provides backtracking function for the stack.
The Backtrace command generates a list containing all valid processes starting from the most recent process and the parameters of calling these processes.
Folding Edit Section Displaying Data
You can use the print command to check the values of each variable.
(gdb) print p (p is the variable name)
print is a very powerful command of gdb. With it, you can display any valid expression in the debugged language. The expression can not only contain variables in your program but also include the following:
Call to a function in the program
(gdb) print find_entry(1,0)
Data structures and other complex objects
(gdb) print *table_start
={e=reference='\000',location=0x0,next=0x0}
Historical components of values
(gdb) print (For the historical record variable, the value that can be directly referenced later)
Artificial array
The artificial array provides a method to display the content of a memory block (array section or dynamically allocated storage area). Early debugging programs did not have a good way to convert any pointer into an array. Just like treating parameters, let's look at 10 integers after the variable h. The syntax of a dynamic array is as follows:
base@length
Therefore, to display 10 elements after h, you can use h@10:
(gdb) print h@10
=(-1,345,23,-234,0,0,0,98,345,10)
The whatis command can display the type of a certain variable
(gdb) whatis p
type = int *
Folding Edit Section Breakpoints
The break command (can be abbreviated as b) can be used to set breakpoints in the program being debugged. This command has the following four forms:
break line-number Make the program stop just before executing the given line.
break function-name Make the program stop just before entering the specified function.
break line-or-function if condition If condition (condition) is true, the program stops when reaching the specified line or function.
break routine-name Set a breakpoint at the entry of the specified routine
If the program is composed of many source files, you can set breakpoints in each source file instead of in the current source file. The method is as follows:
(gdb) break filename:line-number
(gdb) break filename:function-name
To set a conditional breakpoint, you can use the break if command, as follows:
(gdb) break line-or-function if expr
Example:
(gdb) break 46 if testsize==100
Continue running from the breakpoint: continue command
Folding Edit Section Breakpoint Management
Folding 1. Display the current breakpoint information of gdb:
(gdb) info break
It will display all breakpoint information in the following form:
Num Type Disp Enb Address What
1 breakpoint keep y 0x000028bc in init_random at qsort2.c:155
2 breakpoint keep y 0x0000291c in init_organ at qsort2.c:168
Delete a specified breakpoint:
(gdb) delete breakpoint 1
This command will delete the breakpoint with number 1. If there is no number parameter, all breakpoints will be deleted
(gdb) delete breakpoint
Disable a certain breakpoint
(gdb) disable breakpoint 1
This command will disable breakpoint 1, and the (Enb) field of the breakpoint information will become n
Enable a certain breakpoint
(gdb) enable breakpoint 1
This command will enable breakpoint 1, and the (Enb) field of the breakpoint information will become y
Clear all breakpoints on a certain code line in the source file
(gdb) clear number
Note: number is the line number of a certain code line in the source file
Folding 2. Set conditional breakpoints
Example:
gdb can set conditional breakpoints, that is, the breakpoint will only be triggered when the condition is met. The command is "break … if cond". Take the above program as an example:
It can be seen that the set breakpoint is only triggered when the value of i is 101. At this time, the value of sum is printed as 5050.
Folding Edit Section Variable Inspection and Assignment
whatis: Identify the type of array or variable
ptype: More powerful than whatis, it can provide the definition of a structure
set variable = value: Assign a value to a variable
print variable = value or p variable = value: In addition to displaying the value of a variable, it can also be used for assignment
Folding Edit Section Step-by-Step Execution
next Step-by-step execution without entering
step Step-by-step execution with entering. If you have entered a certain function and want to exit the function and return to its calling function, you can use the command finish
Folding Edit Section Function Call
call name Call and execute a function
(gdb) call gen_and_sork(1234,1,0)
(gdb) call printf("abcd")
=4
finish Finish executing the current function and display its return value (if any)
Folding Edit Section Machine Language Tool
There is a group of dedicated gdb variables that can be used to check and modify the general-purpose registers of the computer. gdb provides the standard names of the 4 registers actually used in each current computer:
$pc : Program counter
$fp : Frame pointer (current stack frame)
$sp : Stack pointer
$ps : Processor status
Folding Edit Section Signals
gdb can usually catch most of the signals sent to it. By catching the signals, it can decide what to do with the running process. For example, pressing CTRL-C sends an interrupt signal to gdb, which usually terminates gdb. But you may not want to interrupt gdb. The real purpose is to interrupt the program that gdb is running. Therefore, gdb will catch the signal and stop the program it is running, so that some debugging operations can be performed.
The Handle command can control the processing of signals. It has two parameters, one is the signal name, and the other is what to do when the signal is received. Several possible parameters are:
nostop When receiving the signal, do not send it to the program, and do not stop the program.
stop When receiving the signal, stop the execution of the program, so as to allow program debugging; display a message indicating that the signal has been received (except for the forbidden message)
print When receiving the signal, display a message
noprint When receiving the signal, do not display a message (and implicitly do not stop the program from running)
pass Send the signal to the program, so as to allow your program to handle it, stop running or take other actions.
nopass Stop the program from running, but do not send the signal to the program.
For example, suppose you intercept the SIGPIPE signal to prevent the program being debugged from receiving the signal, and whenever the signal arrives, require the program to stop and notify you. To complete this task, you can use the following command:
(gdb) handle SIGPIPE stop print
Please note that the signal names of UNⅨ are always in uppercase letters! You can use the signal number instead of the signal name. If your program is to perform any signal processing operations, you need to be able to test its signal processing program. For this, you need a convenient way to send signals to the program. This is the task of the signal command. The parameter of this command is a number or a name, such as SIGINT. Suppose your program has set a dedicated SIGINT (keyboard input, or CTRL-C; signal 2) signal processing program to take some cleaning actions. To test this signal processing program, you can set a breakpoint and use the following command:
(gdb) signal 2
continuing with signal SIGINT⑵
The program continues to execute, but immediately transmits the signal, and the processing program starts to run.
Folding Edit Section GDB Usage
GDB is a powerful command-line debugging tool. Everyone knows that the power of the command line lies in that it can form an execution sequence and form a script. The software under UNⅨ is all command-line, which provides great convenience for program development. The advantage of command-line software is that they can be integrated together very easily. Using a few simple commands of existing tools, a very powerful function can be made.
Therefore, the software under UNⅨ can be more organically combined than the software under Windows, each giving full play to its own strengths, and combining into a more powerful function. And the graphical software under Windows is basically self-contained and cannot call each other, which is not conducive to the mutual integration of various software. It is not to compare with Windows here. The so-called "each has its own strengths and weaknesses", and the graphical tool sometimes has places worse than the command line.
Last edited by zzz19760225 on 2017-11-13 at 14:07 ]
|