(1) compile and generate the execution File
# Gcc-g tst. C-o TST
Note that the-G option cannot be absent; otherwise, the gdb l command cannot list the source file content.
(2) Start GDB
GDB./TST
If you want to debug the program code simultaneously, executeGDB-tui./test(This debugging is recommended)
(3) Common gdb commands
(GDB) Help <------------- help
(GDB) help running <-------- display commands in the running category
(GDB) L <-------------------- l command is equivalent to list, the source code is obtained from the first line.
(GDB) Press ENTER <-------------------- press enter to repeat the previous command.
Breakpoint:
(GDB) Break 137 <-------------------- sets a breakpoint. In the current file (usually in the file where the main function is located), the source program is located on line 137th.
Breakpoint 1 at 0x400de6: file readxml. C, line 137.
(GDB) Break func <-------------------- sets the breakpoint, at the entrance of the func () function of the source program in the current file (usually in the file where the main function is located.
Breakpoint 2 at 0x8048456: file TST. C, line 5.
(GDB)Break ana_balance.c: 12<------------------ Set a breakpoint in line 12 of the file ana_balance.c.
(GDB)Break procxmldata. C: Analyze<------------------ Set a breakpoint at the Analyze function in the procxmldata. C: file.
Note:
These code files may be located in a subdirectory in the current directory. GDB will find them automatically. In the gdb status, use the tab key to directly complete them without entering the absolute path.
(GDB) info break 3 or info watchpoints 3 <-------------------- view the information of breakpoint 3
(GDB) info break <-------------------- view all breakpoint information.
Num type disp ENB address what
1 breakpoint keep Y <pending> cluster. c
2 breakpoint keep y 0x0000000000407ce8 in load_balance at./analyze/ana_balance.c: 12
3 breakpoint keep Y <pending> ana_balance.c: load_balacne
4 breakpoint keep Y 0x0000000000404196 in analyze at./main/procxmldata. C: 1154
Note: The pending state indicates that the current breakpoint is suspended. For example, this is true if the file ana_balance.c does not contain the load_balacne function.
Breakpoint information:
* Breakpoint numbers * ---- breakpoint number
* Type * ---- breakpoint type (breakpoint or observation point)
* Disposition * --- display the breakpoint status.
* Enabled or disabled * --- enable or not enable. 'Y' indicates enable, and 'n' indicates not enable.
* Address * ---- address of the breakpoint in your program (memory address)
* What * --- address, the line number of the breakpoint in your program.
(GDB) delete 1 <-------------------- delete a breakpoint whose breakpoint number is 1. After deleting it, the number of other breakpoints remains unchanged.
(GDB) info break
Num type disp ENB address what
2 breakpoint keep y 0x0000000000407ce8 in load_balance at./analyze/ana_balance.c: 12
3 breakpoint keep Y <pending> ana_balance.c: load_balacne
4 breakpoint keep Y 0x0000000000404196 in analyze at./main/procxmldata. C: 1154
Run:
(GDB) r <--------------------- run the program. The Run Command is short for R commands in the running type. It can also be written as run or running.
Run <arg1> <arg2><---------------------------If the program has command line parameters, run
(GDB) n <--------------------- single-step
(GDB) until num <--------------------- execute to a row larger than the current row number
(GDB)P tmpstr<--------------------- Print the variable value
(GDB) C <----------------- The Program continues to run until the next breakpoint
(GDB) finish execution completed
(GDB) Q: Exit GDB
========================================================== ======================================
(GDB) Start $1 $2 ----- step-by-step execution from the beginning of the program; if a statement with a variable declaration and no initial value is assigned, start skips. If an initial value is assigned During Variable declaration, start does not skip the variable definition statement.
(GDB) Run (r) $1 $2 ----- run the program from the beginning rather than in a single step
(GDB) Next (n) ----- single-step execution of the program. When a function is called, it is also executed as one line.
(GDB) Step (s) ----- one-step execution of the program, when a function enters the Function
(GDB) backtrace (BT) ----- function call stack frame (the called function is displayed in the stack only when the step command is executed)
#0 getregionnumfromfile (xmlfilepath = 0x7fffffffe4f0 "Data/ST_orgzkl_16nodes_1198shot_2011-3-29_zkl/MPI. Tree") at./main/procxmldata. C: 1143
#1 0x00000000004046a4 in readtreexmldata (treefilepath = 0x7fffffffe4f0 "Data/ST_orgzkl_16nodes_1198shot_2011-3-29_zkl/MPI. Tree") at./main/procxmldata. C: 1016
#2 0x0000000000401138 in main (argc = 8, argv = 0x7fffffffe6e8) at./main/procxmldata. C: 154
(GDB) frame (abbreviated as f) ----- view the current stack frame
#0 getregionnumfromfile (xmlfilepath = 0x7fffffffe4f0 "Data/ST_orgzkl_16nodes_1198shot_2011-3-29_zkl/MPI. Tree") at./main/procxmldata. C: 1143
1143 int K = 0;
(GDB) Info (I) locals ----- view the local variables in the current stack frame
(GDB) frame 2 ----- select the stack frame numbered 2
#2 0x0000000000401138 in main (argc = 8, argv = 0x7fffffffe6e8) at./main/procxmldata. C: 154
154 node = readtreexmldata (treefilepath );
(GDB) finish ----- keep the program running until the current function is returned. For example, after entering a function and running several lines, if you want to run the current function, return to the function call.
(GDB) List (l) ----- List the source code, and then the last position to the following, each column 10 rows
(GDB) List row number ----- List source code starting from the nth line, for example, l 0
(GDB) list function name ----- List the source code of a function
(GDB) continue (c) ----- continuous running instead of single-step running, the program will automatically stop when it reaches the breakpoint; it is often used when there is a breakpoint in a loop
(GDB) display region_num, procsnum ----- trace variables. The value of region_num and procsnum is displayed in each row of the program. Each time the display is used, it will be designed for the current count.
2: region_num, procsnum = 0
1: region_num = 0
(GDB) undisplay 2 ----- cancel the display of the variable corresponding to number 2
(GDB) Break (B) line number or function name ----- set a breakpoint at the beginning of a line or function
(GDB) I B
(GDB) info break
(GDB) info breakpoint
(GDB) info breakpoints ----- view all breakpoint Information
Num type disp ENB address what
4 breakpoint keep y 0x0000000000400ec1 in main at./main/procxmldata. C: 9
5 breakpoint keep y 0x0000000000400ec1 in main at./main/procxmldata. C: 19
(GDB) Delete (D, del) breakpoint (break, B) ----- delete all breakpoints
(GDB) Delete (D, del) breakpoint (break, B) $ num ----- delete a breakpoint with a code of $ num
(GDB) Disable breakpoints ----- disable a breakpoint: Sometimes you do not want to use a breakpoint to disable it, so you can enable it directly when you want to use it.
(GDB) Enable breakpoints ----- enable breakpoint
A breakpoint is interrupted when the program runs on a certain code line, and the observation point is that the program is interrupted when it accesses a certain storage unit. If we do not know where a certain storage unit is modified, this is especially useful for observation points.
(GDB) Watch variable ----- set the observation point
(GDB) info watchpoints (or watch, watchpoint) ----- view which observations are currently set
(GDB) x ------ print a piece of content in the memory from a certain position, all of which are regarded as bytes, without distinguishing which bytes belong to which variables
========================================================== ======================================
References:
Debug program http://dsec.pku.edu.cn with GDB /~ Yuhj/wiki/gdb.html