GDB Common Commands

Source: Internet
Author: User

In debugging the program, GDB is a tool, the proper use of GDB can solve the program's many bugs.

GDB does not check for syntax errors, which is GCC or g++, and GDB is doing debugging.

Description

  1. gdb Program Name [Corefile] The kind of thing that represents the use of commands, [] The middle of the content is optional, that is, you can add, or do not add.
  2. If you need to repeat a command, you do not need to type the command every time,gdb remembers the last command executed, simply press enter Key to repeat the final command.
      1. GDB command

    This command is primarily used to start debugging.

    GDB program name [Corefile]

    The corefile is optional, but it enhances GDB's ability to debug. Linux does not generate Corefile by default, so it needs to be added in the. bashrc file

    Ulimit-c Unlimited

    After you finish modifying the . BASHRC file, remember : BASHRC Let the changes take effect.

    The following is a code with no syntax errors, but there are logic errors:

    An error is immediately indicated when the operation is run:

    Segmentation fault (core dumped)

    We list the files in the current directory and find a core.* file, which is the core file that the system generates for us.

    We can now start gdb for debugging.

    GDB 1 core.1997

    Where 1 is the code-generated program,core.1997 is the core file generated by the system after the error.

    If you don't like a whole bunch of software information, you can turn off the software information by using the-Q parameter

    Gdb-q 1 core.1997

    #0 0x080483c4 in Test () at Test.c:5

    5 *p = 2;

    You can see that GDB is telling you through the core that there is a problem with the program's statement

      1. Run command

    This command makes the program run and needs attention: The GDB command does not run the program, just enters the GDB state.

      1. Continue command

    In contrast to run is the Continue command, remember that run is to start execution, continue is to continue execution, the two are different, the program at the breakpoint after listening to, if you enter run, the program will restart, and input continue, the program will continue to execute from the breakpoint.

      1. where command

    The where command can display the execution function that caused the segment error.

    #0 0x080483c4 in Test () at Test.c:5

    #1 0x080483e6 in Main () at Test.c:10

      1. List command

    It is helpful to know that the context of a function error line is useful to the debugger.

    list [M,n],m,n is to display the starting and ending lines that contain the first occurrence of the error. A list with no parameters displays 10 lines of code nearby.

      1. Break command

    The break command is used primarily to set breakpoints. The specific usage is as follows:

    Break LineNum set a breakpoint on the LineNum line of the file;

    Break funcname sets a breakpoint on the FuncName function, and each time the function is called it triggers a breakpoint;

    Break filename: LineNum sets a breakpoint on the linenum line of the filename file;

    Break filename: funcname Sets a breakpoint on the funcname function in the filename file.

    For the previous piece of code, we set a breakpoint on the test function and set a breakpoint on line 10th:

    Info Break To view information about an existing breakpoint.

    Delete + breakpoint ordinal to remove a breakpoint.

      1. Single-Step debug commands

    Step command: Step as the name implies. When a function is encountered, step enters the function and executes one statement at a time, which is equivalent to step into.

    Next command: When a function is encountered, next executes the entire function, which is equivalent to step over.

      1. Print command

    One of the most useful features of GDB is that it can display the values of any expression, variable in the program being debugged.

    The print variable, an expression.

    print ' file ':: variable, expression, ' is required to let gdb know that it refers to a file name.

    Print FuncName:: variable, expression

    We first set a breakpoint on the test function, then step into it and then output the value of I:

    As we can see, the Print command is really powerful and makes it easy to output the value of a variable.

      1. Whatis command

    The Whatis command tells you the type of the variable and ptype tells you the definition of the structure.

      1. return command

    return [value]

    Stops executing the current function, returning value to the caller, equivalent to step return.

    Executing this command causes the current function to exit immediately and return.

      1. Set command

    The command can change the value of a variable.

    Set Variable varname = value

    VarName is the variable name, and value is the new value of the variable.

    Of course, GDB has a lot of complex commands, but they use the probability is very low, skillfully mastered the above command, generally dealing with most of the debugging is not a problem.

GDB Common Commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.