Common gdb commands and gdb commands

Source: Internet
Author: User

Common gdb commands and gdb commands

When debugging a program, gdb is a powerful tool. The proper use of gdb can solve many bugs in the program.

Gdb does not check syntax errors. It is a gcc or g ++ task. gdb does debugging.

Note:

(1) The gdb program name, such as [corefile], indicates the usage of commands. The content in [] is optional, that is, you can add or not.

(2) If you need to execute a command repeatedly, you do not need to type the command every time. gdb remembers the last command to be executed, simply press enter to repeat the last command.

1. gdb command

This command is mainly used to start debugging.

Gdb program name [corefile]

Corefile is optional, but can enhance the debugging capability of gdb. Linux does not generate corefile by default, so you need to add

Ulimit-c unlimited

After modifying the. bashrc file, remember to make the modification take effect.

 


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


An error will be prompted immediately after the Migration:

Segmentation fault (coredumped)

We listed the files in the current directory and found an additional file such as core. *, which is the core file generated by the system.

Now we can start gdb for debugging.

Gdb 1 core.1997

1 is the Code Generation Program, and core.1997 is the core file generated by the system after an error.

If you do not like a lot of software information, you can disable the software information through the-q parameter.

Gdb-q 1 core.1997

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

5 * p = 2;

We can see that gdb uses core to tell you which statement of the program has a problem.

2. run Command

Note that the gdb command does not run the program, but enters the gdb state.

3. continue command

The "run" command is opposite to the "run" command. Remember, "run" is the start command and "continue" is the execution. The two are different. After the program listens to the breakpoint, if you enter "run, the program restarts, and the program continues to run from the breakpoint after entering the continue.

4. where command

Where command to display the execution function that causes a segment error.


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

#1 0x080483e6 in main () at test. c: 10

5. list Command

Knowing the context of the function error line is helpful for debugging programs.

List [m, n], M, n is to display the start and end rows that contain the first error location. A list without parameters will display 10 lines of code nearby.



6. break command

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

Break linenumSet a breakpoint in the linenum line of the file;

Break funcnameSets a breakpoint for the funcname function. Each call to this function triggers a breakpoint;

Break filename: linenumSet a breakpoint in the linenum line of the filename file;

Break filename: funcnameSet a breakpoint for the funcname function in the filename file.


For the above Code, we set a breakpoint for the test function and set a breakpoint in line 10th:


Info break allows you to view information about existing breakpoints.

Delete + breakpoint sequence number can delete breakpoints.

7. Single-step debugging command

Step command: As the name implies, step is a step-by-step execution. When a function is encountered, step enters the function and executes a statement every time, which is equivalent to step.

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

8. print command

One of the most useful functions of gdb is that it can display any expressions and variable values in the program to be debugged.

Print variable, 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 for the test function, then perform one step, and then output the I value:


We can see that the print command is indeed powerful, and the variable value is conveniently output.

9. whatis command

The whatis Command tells you the type of the variable, and ptype tells you the structure definition.


10. return command

Return [value]

Stop executing the current function and return the value to the caller, which is equivalent to stepreturn.

Executing this command will immediately exit the current function and return it.


11. set command

This 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 complicated commands, but they have a very low chance of use and are proficient in the above commands. Generally, most of the debugging tasks are not problematic.

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.