一、gdb介绍
GDB(GNU Debugger)是GCC的调试工具。其功能强大, 现描述如下:
GDB主要帮忙你完成下面四个方面的功能:
-
启动程序, 可以按照你的自定义的要求随心所欲的运行程序。
-
可让被调试的程序在你所指定的断点处停住。(断点可以是条件表达式)
-
当程序被停住时, 可以检查此时你的程序中所发生的事。
-
动态的改变你程序的执行环境。
二、生成调试信息
一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序, 首先在编译时, 我们必须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点。
如:
gcc -g hello.c -o hello
如果没有-g, 你将看不见程序的函数名、变量名, 所代替的全是运行时的内存地址。当你用-g把调试信息加入之后, 并成功编译目标代码以后, 让我们来看看如何用gdb来调试它。
三、启动gdb
-
启动gdb:
gdb program
program 也就是你的执行文件, 一般在当前目录下。 -
设置运行参数
set args
可指定运行时参数。(如:set args 10 20 30 40 50
)
show args
命令可以查看设置好的运行参数。
-
启动程序
run
:程序开始执行, 如果有断点, 停在第一个断点处
start
:程序向下执行一行。(在第一条语句处停止)
四、显示源代码
GDB
The source code of the debugged program can be printed out. Of course, -g
the parameters must be added when the program is compiled, and the source program information is compiled into the executable file. Otherwise, the source program will not be visible. When the program stops, GDB
it will report that the program stopped on the line of that file. You can use the list
command to print the source code of the program. By default, 10 lines are printed. list
The usage of the command is as follows:
-
list linenum
: Printlinenum
the context content of line th.
-
list function
: Displaysfunction
the source program of the function with the function name.
-
list
: Display the source program after the current line.
-
list -
: Display the source program at the beginning of the current file. -
list file:linenum
: Display the next line of thefile
filen
-
list file:function
: Display the source program of the functionfile
whose function name is in the filefunction
Generally, the upper 5 lines and the lower 5 lines of the current line are printed. If the display function is the upper 2 lines and the lower 8 lines, the default is 10 lines. Of course, you can also customize the display range. Use the following command to set the display source program once number of rows.
set listsize count
: Set the number of lines of source code to display at one time.show listsize
: View the currentlistsize
settings.
5. Set breakpoints
Simple breakpoint - current file
-
break sets a breakpoint, which can be abbreviated as b
-
b 10 set a breakpoint, at line 10 of the source program
-
b func sets a breakpoint, at the entry of the func function
Set breakpoints in multiple files - other files
-
Stop when entering the specified function:
-
b filename:linenum
--stop at the linenum line of the source file filename -
b filename:function
--Stop at the entry of the function function of the source file filename
query all breakpoints
info b == info break == i break == i b
Conditional breakpoints
- In general, to set a condition for a breakpoint, we use the if keyword followed by its breakpoint condition. Set a conditional breakpoint:
b test.c:8 if intValue == 5
maintenance breakpoints
1. delete [range...]
To delete the specified breakpoint, the shorthand command is d
.
If no breakpoint number is specified, all breakpoints are deleted. range
Indicates the range of breakpoint numbers (eg: 3-7).
-
To delete a breakpoint:
delete num
-
Remove multiple breakpoints:
delete num1 num2 ...
-
Remove multiple breakpoints in a row:
delete m-n
-
Remove all breakpoints:
delete
A better way than delete is disable
to stop the point, disable
the stop point, GDB
not delete, when you still need it enable
, just like the recycle bin.
2. disable [range...]
To invalidate the specified breakpoint, the shorthand command is dis
.
If nothing is specified, disable
all stops are indicated.
-
To disable/enable a breakpoint:
disable num
-
Invalidating multiple breakpoints works:
disable num1 num2 ...
-
Invalidating multiple consecutive breakpoints is valid:
disable m-n
-
Invalidate all breakpoints valid:
disable
3. enable [range...]
To make the invalid breakpoint take effect, the shorthand command is ena
.
If nothing is specified, all stops are enabled.
-
To disable/enable a breakpoint:
enable num
-
Invalidating multiple breakpoints works:
enable num1 num2 ...
-
Invalidating multiple consecutive breakpoints is valid:
enable m-n
-
Invalidate all breakpoints valid:
disable/enable
6. Debug code
-
run
run the program, which can be abbreviated asr
-
next
Single-step tracing, the function call is executed as a simple statement, which can be abbreviated asn
-
step
Single-step tracing, the function is called into the body of the called function, which can be abbreviated ass
-
finish
Exit the entered function. If you can't get out, check whether there is a breakpoint in the loop in the function body. If there is, delete it, or the setting is invalid. -
until
When single-step tracing in a loop body, this command can run the program until it exits the loop body, which can be abbreviated asu
,
if you can't get out, check if there is a breakpoint in the loop in the function body, if it is deleted, or the setting is invalid -
continue
Continue to run the program, which can be abbreviated asc
(if there is a breakpoint, jump to the next breakpoint)
Seven, view the value of the variable
1. View the value of the runtime variable
print
Print the value of a variable, string, expression, etc., abbreviated as p
p count -----
the printed count
value
2. Automatically display the value of the variable
You can set some variables to show automatically when the program stops, or when you step through the trace. The relevant GDB
command is display
.
-
display 变量名
-
info display
– View the automatically displayed information in the display setting.
-
undisplay num
(info display
the number displayed at the time)
-
delete display dnums…
– Delete the automatic display,dnums
which means the set automatic explicit number. If you want to delete several numbers at the same time, the numbers can be separated by spaces. If you want to delete numbers in a range, you can use a minus sign (eg: 2-5)
① Delete an automatic display: undisplay num or delete display num
② Delete multiple Items: delete display num1 num2
③ Delete a range: delete display mn -
disable display dnums…
① To disable one automatic display: disable display num
② To disable multiple automatic displays: delete display num1 num2
③ To disable a range of automatic displays: delete display mn -
enable display dnums…
① To enable one automatic display:enable display num
② To enable multiple automatic displays:enable display num1 num2
③ To enable a range of automatic displays:enable display m-n
④disable和enalbe
Do not delete the automatic display settings, but just disable and restore them.
3. View the value of the modified variable
-
ptype width
--View the type of variable width
type = double
-
p width
--print the value of the variable width
$4 = 13
You can use set var
commands to tell GDB
, width
not your GDB
parameters, but the variable names of the program, like:
set var width=47
// set the variable var value to 47
When you change the value of program variables, it is best to use set var
the format GDB
command.
Summarize
I look forward to your communication with me, leave a message or private message, learn together and make progress together!