Article Directory
Preface;
InVS
the environment, we can not only write code, compile, and run executable programs, but also debug the generated executable programs. In this chapter, we will learn how toLinux
debug in the environment.
1. Preface
To debug, you must first generate an executable program, so first prepare a simple C program:
touch Test.c
vim Test.c
//C代码:
#include<stdio.h>
int accumulate(int n)
{
printf("Start\n");
int sum = 0;
for (int i = 0; i <= n; i++)
{
sum += i;
}
printf("End\n");
return sum;
}
int main()
{
int n = 100;
int ret = accumulate(n);
printf("%d\n", ret);
return 0;
}
To review make/Makefile
the use of:
touch Makefile
vim Makefile
//Makefile的内容
Test:Test.c
gcc Test.c -o Test -std=c99
.PHONE:clean
clean:
rm -f Test
- The meaning of the option here
-std=c99
is:for
the operation of defining the variable i in the loop isc99
only supported, so it needs to be declared at compile time. Otherwise, the following error will appear:
2. Install the gdb debugger
In Linux
, we use gdb
to debug; switch roo
t identity or use sudo
the command to install:
yum install -y gdb
3. Enter debugging
After the executable file ( Test
) is generated, use the command gdb
[file name] to enter debugging.
gdb Test
- If you directly
Test
debug the file, there will be an error report without debugging information, as shown in the figure below:
- The reason is that there are two ways to publish a program:
release
anddebug
.gcc/g++
The executable program generated by default isrelease
version. And we all know that debugging is aimed atdebug
the version of the executable program, because onlydebug
the version contains debugging information.- When
gcc/g++
compiling,-g
you can select the generateddebug
versionTest
file by adding options (in order to distinguish, you candebug
add a suffix to the version file-debug
, andrelease
add a suffix to the version-release
).
Amended Makefile
file:
Test-debug:Test.c
gcc Test.c -o Test-debug -g -std=c99
.PHONE:clean
clean:
rm -f Test
Regenerate the file Test-debug
and enter debug:
make
gdb Test-debug
4. Debug related instructions
gdb[文件名]
: enter debuggingCtrl + d
orquit
: exit debugist+ [行号]
orl + [行号]
(short form): Displays the source code of an executable program. Start displaying from the line number, and display 10 lines at a time
(After using this command for the first time, you can directly press Enter to continue executing the previous command for convenience. Other commands have the same effect)
list + [函数名]
orl + [函数名]
: display the source code of a function
run
Orr
: run the program (run until the end of the program if no breakpoint is set)
break + [行号]
Orb + [行号]
: set a breakpoint on a certain lineinfo + b
View breakpoint information
break + [函数名]
: Set a breakpoint at the beginning of a functiondelete/d breakpoint n
: delete the breakpointn
numbered
delete/d breakpoint
: remove all breakpointsdisable breakpoint
: disable breakpoint
enable breakpoint
: enable breakpoint
next
orn
: process by process (vs
inF10
)step
ors
: statement by statement (vs
inF11
)
finish
: Execute until the current function returns, then stop and wait for the commandprint
Orp
: print the value of the expression, the value of the variable can be modified or the function can be called through the expressionp + 变量
: print variable valueset + 变量
: modify the value of the variablecontinue
orc
: run from the current position until the next breakpointdisplay + 变量名
: Trace to view a variable, displaying its value each time you stopundisplay
: untrack those variables that were previously setuntil + 行号
: jump to a line to executebreaktrace
Orbt
: view function calls and parameters at all levelsinfo + 变量 + locals
: View the value of the local variable of the current stack frame;
gdb
Commonly used commands are summarized here.
When learning gdb
, we should VS
compare it with debugging in the environment to understand better, for example:
r
equivalentvs
toF5
b
Equivalentvs
to setting a breakpoint inn
equivalentvs
toF10
s
F11
Equivalent to (at the function) in vsp/display
Equivalent tovs
surveillance in
gdb
The instructions in are generally abbreviated, for example:
list——l
run——r
break——b
delete——d
breakpoint——b
next——n
step——s
print——p
continue——c
breaktrace——bt
…
This is the end of this article, the code text is not easy, please support me a lot! !