GDB is a powerful Linux program debugging tool released by the GNU Open source organization. In general, GDB mainly completes the following four parts of the function.
1) Start your program, you can follow your custom requirements to run the program.
2) allows the debugged program to stop at the breakpoint you specified for debugging (the breakpoint can make the conditional expression).
3) When the program has been stopped. You can check what happens in your program at this point, including viewing the values of the variables specified in the program in the current state.
4) Dynamically change the execution environment of your program.
Steps:
1, first use the VI Editor to edit the file test.c, for the GDB debugger debugging.
2. Use the command gcc-g test.c-o test, compile the test.c, and generate the file test containing the standard debug information.
3, using the command GDB test, start GdB debugging, in the GDB Start screen display GDB version, free software and other information, press ENTER to confirm after entering the "GDB" as a prompt command interface.
4, using the L (list) command, view the source file, you can see each line of code in the line number, so that we easily set breakpoints. Note: Using the l command, the default starts with the code, displays 10 rows at a time, uses the L command again, displays the next 10 lines, displays the contents of the specified line, and can use the L (list) [Start line],[end Line] command. For example L 3, 9, view content from 3 to 9 lines.
5, use the B (break) command to view the source file, visible only after the command "B" plus the corresponding line number, you can set the breakpoint on the line, after the program is running, a breakpoint will be stopped in the line before the breakpoint.
6. Use command info B to view breakpoint settings.
7, run the code with command R, the default is to start from the beginning of the program, when executed to the breakpoint, the program stopped, in addition, you can also pass this command to the program parameters, the format of Run [parameter 1] [parameter 2] ....
8, use the command P (print), the format is p [variable name], see the value of the variable.
9, using the command S (stop), for single-step debugging, when the function is encountered, the execution of S will jump into the function, and did not enter the S command, the program will be a step down.
10, using command N (next), for single-step debugging, when the function is encountered, the execution of n does not enter the function, but also as a step to execute the function.
11, use the command finish, pick out a function (this function is finished running).
12, using C (continue), the recovery program runs, we set two breakpoints in the program code, when the program stops at the first breakpoint, we can use the C command to keep the program running until the 2nd breakpoint.
13. Use the command Q (quit) to exit GDB debugging.
Summarize:
L (list) [line number]: Displays 10 lines of content in the code [line number].
L (list) [filename] [line number]/[Start line number],[end line number], specify the name of the list
P (print): In addition to the variable name directly, it can also be called with a function. Print Add (3,4)
Whatis: View variable type, formatted as Whatis [variable name]
Break (b): b [Line number]: Set a breakpoint on the specified line
break [function name]: When entering a specified function, the program terminates
Break [line number]/[function name] if [condition]: will not stop until the condition is established.
Break [Routine entry]: When a program contains more than one code file, it stops when it enters the specified file.
Tbreak: Set temporary breakpoint, after arrival is automatically deleted, use the same break.
Delete (d): Deletes the breakpoint, in the format: D (delete) [Breakpoint label], and removes all breakpoints without marking.
Disable/enable: In addition to the ability or enable breakpoint, the format is disable/enable [breakpoint designator], if not with the breakpoint label, then all do the corresponding action.
Condition: Format: Condition [breakpoint number]< conditional expression, the condition used to modify the corresponding breakpoint.
Ignore: Format: Ignore [breakpoint number] <num> ignore breakpoint number num times when executing
Set: Sets the value of the variable in the format: Set [variable name]=[value]
Help (h): see How to use the specified command: Help (h) [command name].
1, GDB program debugging