gdb learning 1

1 GDB

GDB is a UNIX program debugging tool released by the GNU open source organization. It is specially used to debug C and C++ programs, and they are all in command line mode.

2 Preparation

Usually when we develop in IDE, we must select debug mode when running, otherwise the breakpoint will be invalid. Therefore, some compilation parameters are required when compiling with gcc.

2.1 Compile

When using GCC to compile, you must add the -g option to compile the source code, so that you can see the source code when debugging, otherwise it is very inconvenient to debug and many commands are not available. E.g:

gcc helloword.c -o helloword -g

2.2 Run and debug

gdb helloword //也可以直接运行gdb,再使用命令file加载程序。

3 Common operation commands

  • Display code
    l command (abbreviation of list, direct list can also be used) displays the next 10 lines of code, add a line number parameter to display the 10 lines of code starting with the line number, such as:
l 3
  • Add two parameters to display the code between the two line numbers, such as:
l 20, 30
  • Set a breakpoint
    b (abbreviation for break, break directly can also be), you can specify the line number, you can also specify the function, such as b 11, or break main.
  • View and delete breakpoint
    info break (ib can also be used) to view the currently set breakpoint
    clear linenumber can delete the breakpoint of the specified line
    Delete breaknumber can delete the breakpoint of the specified breakpoint number
  • Run the program
    r (short for run, you can also run directly) to run the program. If your program needs to read parameters, you can add parameters later
  • Single-line execution of
    n (abbreviation for next, you can also directly next), execution of a line of code, is the meaning of step over. If there is a breakpoint, the program execution stops at the breakpoint line, and then enter n, you can step over.
  • Tracking into (step into)
    s (abbreviation of step, you can also directly step) command is equivalent to step into, if you stop at the function line, then you can enter s to enter the function.
  • Print variable
    p variable name, you can print the value of the variable, such as print a. You can also print expressions, and expressions can modify the value of variables.
  • Continue to run
    c (abbreviation for continue), which is equivalent to the triangular run button on the IDE, and run directly to the next breakpoint or end.
  • View the stack
    bt (abbreviation of backtrace, you can also directly backbrace), you can view the stack situation, especially useful when entering a function, you can see the value of the function parameter changes.
  • Exit function
    finish Exit function debugging, it is convenient.
  • Thread debugging
    Info threads can display the current thread. The thread with an asterisk is the currently debugged thread, that is, the active thread. The output results of other threads cannot be seen and must be switched.
    The thread number can be switched to the thread of the specified number for debugging.
  • Quit
    q(quit) Quit

Guess you like

Origin blog.csdn.net/qq_36783816/article/details/112836338