
Application of GDB Tool
GDB is a debugging tool for UNIX and UNIX-like systems..
Generally, GDB mainly helps us accomplish the following four functions:
1. Start programs, allowing developers to run programs as desired.
2. Allow the debugged program to stop at specified breakpoints.
(Breakpoints can be conditional expressions)
3. When the program is stopped, check what is happening in the program at that time.
4. Modify the program to correct the impact of a bug and test other bugs.
Specific Applications

List
(gdb) list line1,line2
View source code
list lineNum shows the source code before and after lineNum
list + lists the code lines after the current line
list – lists the code lines before the current line
list function
set listsize count
Set the number of code lines displayed
show listsize
Display the number of printed code lines
list first,last
Display the source code lines from first to last
The break command (abbreviated as b) can be used to set breakpoints in the debugged program, and it has the following four forms:
break line-number stops the program just before executing the given line.
break function-name stops the program just before entering the specified function.
break line-or-function if condition stops the program when it reaches the specified line or function if condition is true.
break routine-name sets a breakpoint at the entry of the specified routine.
If the program consists of many source files, you can set breakpoints in various source files instead of just the current one 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
1. Display the current gdb breakpoint information:
(gdb) info break
It will display all breakpoint information in the following format:
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 specific breakpoint:
(gdb) delete breakpoint 1
This command will delete breakpoint number 1; if no number is provided, it will delete all breakpoints.
(gdb) delete breakpoint
Disable a specific breakpoint:
(gdb) disable breakpoint 1
This command will disable breakpoint 1, and the (Enb) field will change to n.
Enable a specific breakpoint:
(gdb) enable breakpoint 1
This command will enable breakpoint 1, and the (Enb) field will change to y.
Clear all breakpoints on a specific line of code in the source file:
(gdb) clear number
Note: number is the line number of a specific line of code in the source file.

Some content of this article is selected from the internet.