Debugging tool for Linux - gdb
- 1. Background
- 2. Get started
-
- The use of instructions is demonstrated with the following simple small code in C language:
- 1. list or l line number: display the file source code, and then go down from the last position, 10 lines at a time.
- 2. list or l function name: List the source code of a function.
- 3. r or run: run the program.
- 4. break or b line number: set a breakpoint on a certain line
- 5. n or next: single execution (similar to F10 in vs)
- 6. s or step: Enter function call (similar to F11 in vs)
- 7. break function name: Set a breakpoint at the beginning of a function
- 8. info break or info b: View breakpoint information.
- 9. finish: Execute until the current function returns, then wait for the command
- 10. p variable: print variable value.
- 11. continue or c: execute the program continuously rather than single-step from the current position
- 12. run or r: execute the program continuously from the beginning instead of single step
- 13. delete breakpoints: delete all breakpoints
- 14. delete breakpoints n: Delete the breakpoint with serial number n
- 15. disable breakpoints: disable breakpoints
- 16. enable breakpoints: enable breakpoints
- 17. Display variable name: Track and view a variable, and display its value every time you stop.
- 18. undisplay: Cancel tracking of previously set variables
- 19. until X (line number): jump to line X
- 20. breaktrace or bt: View function calls and parameters at all levels
- 21. q or quit: exit gdb
1. Background
- There are two ways to release a program, debug mode and release mode.
- Binary programs produced by Linux gcc/g++ are in release mode by default.
- To use gdb for debugging, you must add the -g option when generating a binary program from the source code and publish it in debug mode.
2. Get started
The use of instructions is demonstrated with the following simple small code in C language:
1 #include <stdio.h>
2
3 int SumAdd(int x)
4 {
5 int i=1;
6 int sum=0;
7 for(i=1;i<=x;i++)
8 {
9 sum+=i;
10 }
11 return sum;
12 }
13
14 int main()
15 {
16 printf("%d \n",SumAdd(100));
17 return 0;
18 }
1. list or l line number: display the file source code, and then go down from the last position, 10 lines at a time.
例:l 0
2. list or l function name: List the source code of a function.
例:l SumAdd
3. r or run: run the program.
Note: If there is no breakpoint, the program will run to the end
例:r
4. break or b line number: set a breakpoint on a certain line
例:b 3
5. n or next: single execution (similar to F10 in vs)
Note: Single execution needs to be carried out when debugging is started. You need to break the point first, and then execute r to the breakpoint position.
例:n
6. s or step: Enter function call (similar to F11 in vs)
例:s
7. break function name: Set a breakpoint at the beginning of a function
例:break SumAdd
8. info break or info b: View breakpoint information.
例:info b
9. finish: Execute until the current function returns, then wait for the command
例:finish
10. p variable: print variable value.
例:p sum
11. continue or c: execute the program continuously rather than single-step from the current position
例:c
12. run or r: execute the program continuously from the beginning instead of single step
r
13. delete breakpoints: delete all breakpoints
例;delete breakpoints
14. delete breakpoints n: Delete the breakpoint with serial number n
delete breakpoints 5
15. disable breakpoints: disable breakpoints
例:disable breakpoints
16. enable breakpoints: enable breakpoints
例:enable breakpoints
17. Display variable name: Track and view a variable, and display its value every time you stop.
例:display sum
18. undisplay: Cancel tracking of previously set variables
例:undisplay
19. until X (line number): jump to line X
例:until 11
20. breaktrace or bt: View function calls and parameters at all levels
例:bt
21. q or quit: exit gdb
例:q
(End of chapter)