Debugging tool for Linux - gdb (super detailed)

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.
    Insert image description here

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

Insert image description here


2. list or l function name: List the source code of a function.

例:l SumAdd

Insert image description here


3. r or run: run the program.

Note: If there is no breakpoint, the program will run to the end

例:r

Insert image description here


4. break or b line number: set a breakpoint on a certain line

例:b 3

Insert image description here


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

Insert image description here


6. s or step: Enter function call (similar to F11 in vs)

例:s

Insert image description here


7. break function name: Set a breakpoint at the beginning of a function

例:break SumAdd

Insert image description here


8. info break or info b: View breakpoint information.

例:info b

Insert image description here


9. finish: Execute until the current function returns, then wait for the command

例:finish

Insert image description here


10. p variable: print variable value.

例:p sum

Insert image description here


11. continue or c: execute the program continuously rather than single-step from the current position

例:c

Insert image description here


12. run or r: execute the program continuously from the beginning instead of single step

r

Insert image description here

13. delete breakpoints: delete all breakpoints

例;delete breakpoints

Insert image description here


14. delete breakpoints n: Delete the breakpoint with serial number n

delete breakpoints 5

Insert image description here


15. disable breakpoints: disable breakpoints

例:disable breakpoints

Insert image description here


16. enable breakpoints: enable breakpoints

例:enable breakpoints

Insert image description here


17. Display variable name: Track and view a variable, and display its value every time you stop.

例:display sum

Insert image description here


18. undisplay: Cancel tracking of previously set variables

例:undisplay

Insert image description here


19. until X (line number): jump to line X

例:until 11

Insert image description here


20. breaktrace or bt: View function calls and parameters at all levels

例:bt

Insert image description here


21. q or quit: exit gdb

例:q

Insert image description here

(End of chapter)

Guess you like

Origin blog.csdn.net/originalHSL/article/details/132948667