Linux GDB Debugging
GDB has not been used to debug the program feel that GDB is a very magical thing, if you use it to debug a guarantee you want to forget it is difficult, the following look at its true!
GDB Overview
GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but if you are in the UNIX platform to do software, you will find GDB this debugging tool than VC, BCB graphical debugger more powerful features. The so-called "inch, the ruler is short" is this truth.
In general, GDB is the main help you complete the following four aspects of the function:
1, start your program, you can follow your custom requirements to run the program at will.
2. Allow the program to be debugged to stop at the breakpoint where you have specified the adjustment. (Breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program.
From the above, GDB and the General debugging tool is not the same, basically is to complete these functions, but in the details, you will find GDB this debugging tool is powerful, you may be more accustomed to the graphical debugging tools, but sometimes, the command line debugging tools have a graphical tool can not be done. Let's see.
1234567891011121314 |
#include<iostream>
using namespace
std;
int
sum(
int
arg1,
int
arg2)
{
return
arg1+arg2;
}
int
main()
{
int
a=1;
int
b=2;
cout<<
"sum = "
<<sum(a,b)<<endl;
return
0;
}
|
First step: Compiling
[Email protected] cppproject]# g++-g sum.cpp-o sum
View the next file
Step two: Use GDB to set breakpoints debugging
[[email protected] cppproject]# gdb sum
1234567891011121314151617 |
(gdb) l
1 #include<iostream>
2
using
namespace
std;
3
int sum(
int
arg1,
int
arg2)
4 {
5
return
arg1+arg2;
6 }
7
int
main()
8 {
9
int
a=1;
10
int
b=2;
(gdb)
11 cout<<
"sum = "
<<sum(a,b)<<endl;
12
return
0;
13
14 }
(gdb)
|
Set breakpoints on line 11th
(GDB) Break 11
Breakpoint 1 at 0x8048742:file Sum.cpp, line 11.
Set breakpoints directly at the function name
(GDB) Break sum
Breakpoint 2 at 0x80486a7:file Sum.cpp, line 5.
To view the breakpoint information that has been set
(GDB) Info break
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x08048742 in main at sum.cpp:11
2 Breakpoint Keep y 0x080486a7 in sum (int, int.) at Sum.cpp:5
Run:
(GDB) Run
Starting program:/root/cppproject/sum
Breakpoint 1, Main () at Sum.cpp:11
cout<< "sum =" <<sum (A, b) <<endl;
View Code:
(GDB) L
(gdb) L1 #include <iostream>2 using namespace std;3 int sum (int arg1,int arg2) 4 {5 return Arg1+arg2;6 }7 int main () 8 {9 int a=1;10 int b=2; (gdb) cout<< "sum =" <<sum ( A, b) <<endl;12 return 0;1314
(GDB) n means the next step
Breakpoint 1, Main () at Sum.cpp:11
cout<< "sum =" <<sum (A, b) <<endl;
(GDB) n
sum = 3
return 0;
(GDB)
View the value of the variable under GDB: P variable name
(GDB) p a
$ = 1
(GDB) P b
$ = 2
View function memory Address:
(GDB) P sum
3=INT(INT,INT)0X80486A4<SUM(INt, Int ) > (gd b) Psu m ( A,b ) /span> 3=int (int,int) 0x80486a4<sum (int,int) > (GDB) psum (A, b) 4 = 3
Calling functions
(GDB) P sum (100,200)
$300
Breakpoint debugging is so much, interested can learn to set the observation point, capture Point debugging.
Linux GDB Debugging