[Linux] Linux debugger -gdb use

Table of contents

        1. Background

        2. Start using


1. Background

  • There are two ways to release the program, debug mode and release mode;
  • The binary program from Linux gcc/g++ is in release mode by default;
  • To use gdb debugging, it is necessary to generate a binary file in debug mode, and add the -g option when generating a binary program from the source code, so that a debug version can be generated.

    After we write the code on vim, some problems may occur, causing the compilation to fail. At this time, we can use gdb to debug the code and solve the problem.

    The demonstration is as follows:

 

As can be seen from the figure above, debugging has been entered. 

Note: If it is under the release version, gdb debugging cannot be performed. 

2. Start using

  • l Line number: display the test source code, and then go down to the last position, and list 10 lines each time.

  • r : Run the program.

 

Because we didn't set a breakpoint before, we can run to the end directly.

  • n: Single execution (equivalent to our F10 under VS debugging).
  • s: Enter the function call (equivalent to our F11 under VS debugging).

Note: You need to combine breakpoints to execute the above commands. 

  •  b line number: set a breakpoint at a certain line.

   Assuming a breakpoint is set at line 13, the demonstration is as follows:

 

   Then run the program and stop at the breakpoint:

   At this point, after executing the s command, you will enter the function, and the demonstration is as follows:

  •  c: Jump to the next breakpoint

Assume that when a breakpoint is set on line 15, the c command is executed, and the demonstration is as follows:

 

  • info b: view the currently set breakpoint information
  • disable breakpoint number: turn off the breakpoint
  • enable breakpoint number: open breakpoint

 

    Indicates that breakpoints have been set on lines 13 and 15. Among them, Enb: indicates whether the breakpoint is enabled, y is the enabled state, and n is the disabled state (can be ignored).

  • d : delete all breakpoints;
  • dn: delete the breakpoint with sequence number n.

The demonstration is as follows:

 

  •  p variable: print variable value;
  • set var: modify variable value

The demonstration is as follows: 

Note: The values ​​of variables a and b can only be displayed when the program is running, and the values ​​of variables can also be set.

  •  until X line number: transfer to X line

The demonstration is as follows:

 

Note: Jump to the next position only when the program is running. 

  • q: Quit gdb debugging. 

 


If there are deficiencies in this article, you are welcome to comment below, and I will correct it as soon as possible.

  Old irons, remember to like and pay attention!!! 

 

Guess you like

Origin blog.csdn.net/m0_63198468/article/details/130655606