[Linux] The use of debugger-gdb under Linux

We usually debug C/C++ code mostly in VS under Windows platform. In LInux, we usually use gdbit to debug code. Although we rarely debug code on LInux, gdb is rarely used in actual use. But we must understand it in order to face various environments.

The purpose of this article: to be able to carry out basic debugging of single-process programs, that is, to use the basic debugging functions of VS 2019 and VS 2022, all of which are implemented using gdb on Linux.

This article will use the vim editor , make/Makefile , and gcc/g++ in the Linux environment . If you are not familiar with these three tools, you can click the link below to learn together with this article.
vim editor
make/Makefile
gcc/g++

1.debug and release

Before learning to use gdb debugging under Linux, we need to master the following three foreshadowing knowledge about debug and release modes

  1. There are two ways to release the program, debug mode and release mode

    Debug mode : This mode is a mode for programmers to write programs and debug codes. The executable program generated in this mode contains debugging information that is larger than the executable program generated in release mode .

    Release mode : There is no debugging information, the program is optimized, the size of the program is reduced, and it is more suitable for users to use. The executable program generated under this mode is what the company will eventually push to the market. The testers in the company will test whether it is perfect, and then push it to the market after passing the test.

    • Note: You can debug in debug mode, but not in release mode.

    The picture below is the selection diagram of the two modes under VS 2019. You can choose which mode you need.
    insert image description here
    Then how to convert the two modes under Linux?

  2. Under Linux, the binary executable program produced by gcc/g++ is in release mode by default

    • That is, under Linux, the executable program generated by using conventional instructions is in release mode , and cannot be debugged
    //常规生成
    gcc -o 可执行文件名 源代码文件
    g++ -o 可执行文件名 源代码文件
    
  3. -gTo use gdb debugging, you must add options when using gcc/g++ to generate binary programs

    gcc -o 可执行文件名 源代码文件 -g
    g++ -o 可执行文件名 源代码文件 -g
    

Next, we edit the test code under the following test.c file to test.
insert image description here
Note: the 7th line of code defines variables in the for loop i. This writing method is supported in c99, and the gcc/g++ editor I use does not support it. This way of writing needs to be added after executing the command -std=c99,

The Makefile is as follows:
insert image description here
If you do not know how to use the Makefile, you can directly use the following two instructions to test

gcc -o MyTest-debug test.c -g -std=c99     //生成debug模式的可执行文件
gcc -o MyTest-release test.c -std=c99     //生成release模式的可执行文件

First , test whether debugging is possible in release mode.
insert image description here
As shown in the above figure, the red box indicates that no debugging symbols have been found and debugging cannot be performed.

Second , test whether debugging is possible in debug mode

insert image description here
No error message is displayed, and it can be debugged normally

Finally: Observe the volume of the two executable files, it is obvious that debug is larger than release
insert image description here

expand

We can use the readlefcommand to view the information under the two executable files, as follows:

insert image description here
We can use this command to view the debug information under the two files, as follows (the grep command is a search string command):
insert image description here
The debug information is found in the MyTest-debug file, but there is nothing in MyTest-release.

2. How to use gdb to debug

The core job of the debugger: locating the problem

Next, we briefly introduce how to use gdb to debug under Linux. This paragraph is explained step by step, and the summary of commands under gdb is placed in the next paragraph.
If you don’t have properties for gdb, just follow the steps

  1. enter debug

    Use the gdb filecommand to enter the debug mode, as shown below
    insert image description here

  2. show code to debug

    • l/list 行号: Display the source code of the file, starting from the input line number, and list ten lines downward. Generally, when we start debugging, we start from the l 1beginning to view, and then enter the lor Enterkey to get the code from the last search position.
      insert image description here
      Note: After entering the gdb command, the Enterkey will be recorded. That is, after inputting the command, inputting the Enter key will repeat the above action. The following directives also follow this rule.
    • After entering the gdb mode, use it alone lto view, the displayed code is random
      insert image description here
  3. set breakpoint

    • break/b 行号: Set a breakpoint on a certain line

      As shown below, set a breakpoint on line 19

    • break/b 函数名: Set a breakpoint at the beginning of a function

      As shown in the figure below, set a breakpoint at the addTestTop function

      insert image description here

      5 is the line number of the first line of code inside the addTestTop function.

    • break/b 文件名:函数名: When there are multiple files, you can use this method to set breakpoints

    • info break/b: View breakpoint information

      insert image description here

      When the program is running, it will stop at the breakpoint, and when you view the breakpoint for the next time, it will display the number of times it stopped at the breakpoint.

      insert image description here

    Note: The serial number of the gdb breakpoint is increasing, and it will continue to increase after each use, until it is turned off and turned on, and starts from 1 again

  4. run the program

    • r/run: run the program

      • If there is a breakpoint, run to the breakpoint and stop
        insert image description here
      • If there is no breakpoint, run directlyinsert image description here
    • Note: A run ends after all the codes in all main functions run. After stopping at a breakpoint, performing other viewing instructions will not affect this debugging.

  5. There are breakpoints, after running, execute statement by statement

    • s/step: When a function is encountered, enter the function

      insert image description here

    Note: After entering other functions, a call chain will be generated, that is, one function calls another function, and then calls other functions.

    Can be viewed by btcommand

    insert image description here

  6. There are breakpoints, after running, run process by process

    • n/nextThat is, when a function is encountered, the next instruction is directly executed without entering the function

    insert image description here

    • Remember the Enterbutton execute the command executed last time? If there are too many contents to be debugged, just press once sor nthen Enterpress
  7. delete breakpoint

    • delete /d breakpoints: delete all breakpoints

      insert image description here

    • delete/dn: delete the breakpoint with sequence number n

      insert image description here

  8. Turn breakpoints on and off

    Used when we don't want to use a breakpoint and don't want to delete the breakpoint

    • disable breakpoint breakpoint number: turn off the breakpoint

      insert image description here

    • enable breakpoint breakpoint number: enable breakpoint

      insert image description here

  9. view variables

    • p 变量名: This instruction is input when it needs to be checked, and it will not affect debugging. As follows, after I enter the addTestTop function in debugging, I execute a few statements and check the result of the sum variable. At this time, the sum is 6.

      insert image description here

    • The efficiency of viewing in this way is too low. When we use debugging in VS, we execute the corresponding variable once and change accordingly. You can view it directly. This method needs to be called by the user itself, which is inconvenient. We can use the following method

  10. Track View Variables

    • display 变量名: After execution, the variable will be displayed after each statement is executed

      insert image description here

    • Can display custom types such as built-in types and structures, stl

  11. untrace on breakpoint

    • undisplay 序号: Use when you don't want to track a variable

      insert image description here

  12. Jump to the specified line

    • until 行号: Jump to line X, there is a loop in our code above, we can use this instruction to jump out of the loop
      insert image description here
  13. run the whole function

    • finash: Execute after entering a function, only execute this function, and stop after execution

    insert image description here

  14. From one breakpoint to the next

    • continue/c: run directly to the next breakpoint

      insert image description here

    • After combining util、finish、continue, the basic debugging function can be realized

  15. exit gdb

    • q/quit: If you are debugging, you will be prompted whether you need to exit

    insert image description here

3. Instruction set

gdb binFileExit: ctrl + dor quit/qDebug command:

  • list/l line number: display the source code of binFile, and then go to the following position from the last position, and list 10 lines each time.
  • list/l function name: List the source code of a function.
  • r or run: Run the program.
  • n or next: single execution.
  • s or step: enter the function call
  • break(b) line number: set a breakpoint on a certain line
  • break function name: set a breakpoint at the beginning of a function
  • info break : View breakpoint information.
  • finish: Execute until the current function returns, then stand down and wait for the command
  • print§: print the value of the expression, the value of the variable can be modified or the function can be called through the expression
  • p variable: Print variable value.
  • set var: modify the value of a variable
  • continue (or c): continuous rather than single-step execution of the program from the current position
  • run (or r): Execute the program continuously from the beginning instead of single-step
  • delete breakpoints: delete all breakpoints
  • delete breakpoints n: delete the breakpoint with sequence number n
  • disable breakpoints: disable breakpoints
  • enable breakpoints: enable breakpoints
  • info (or i) breakpoints: See which breakpoints are currently set
  • display variable name: track and view a variable, and display its value every time you stop
  • undisplay: undisplay those variables that were previously set
  • until X line number: jump to X line
  • breaktrace (or bt): View function calls and parameters at all levels
  • info(i) locals: View the value of local variables in the current stack frame
  • quit: quit gdb

Guess you like

Origin blog.csdn.net/m0_52094687/article/details/128748413