Using the GDB command-line debugger to debug a C + + program "Go"

Source: Internet
Author: User
Tags arch linux

Transferred from: https://linux.cn/article-4302-1.html

Compiled from: http://xmodulo.com/gdb-command-line-debugger.html Adrien Brochard
Original: LCTT https://linux.cn/article-4302-1.html Translator: Spccman
This address: https://linux.cn/article-4302-1.html

2014-11-25 21:48 Comments: 31 Favorites: 19 share:

This article navigation
    • -Install gdb10%
    • -Sample code 16%
    • Use of-gdb 24%

What's the worst situation when writing a program without a debugger? On your knees at compile time don't make mistakes? Summon Demons to help you run the program with a blood sacrifice? or add a printf ("test") statement between each line of code to locate the error point? As you know, it is inconvenient to write a program without using a debugger. Fortunately, the Linux debugging is very convenient. Most of the Ides used by most people are integrated with the debugger, but Linux's most famous debugger is the command-line form of the C + + debugger gdb. However, consistent with other command-line tools, DGB requires some exercise to be fully mastered. Here, I'll tell you about GDB's basic situation and how to use it.

Installing GDB

GDB is available in most of the distribution repositories

Debian or Ubuntu

    1. $ sudo apt-get install gdb

Arch Linux

    1. $ sudo Pacman -S gdb

Fedora,centos or RHEL:

    1. $sudo Yum install gdb

If not found in the warehouse, you can download it from the official website.

Sample code

When learning gdb, it's best to have a code, try it out. The following code is a simple example of what I have written, which can well reflect GDB's characteristics. Copy it down and experiment with it-this is the best way to do it.

  1. #include <stdio. H>
  2. #include <stdlib. H>
  3. int main(int argc, char * *argv)
  4. {
  5. int i;
  6. int A=0, b=0, c=0;
  7. double d;
  8. for (i=0; i<; i+ +)
  9. {
  10. a+ +;
  11. if (i>)
  12. D = i / 2.0;
  13. b+ +;
  14. }
  15. return 0;
  16. }
Use of GDB

First and foremost, you need to use the compiler's "-g" option to compile the program so that the executable program can run through GDB. Start debugging with the following statement:

    1. $ gdb -tui [executable program name]

Use the "-tui" option to display the code in a nice interactive window (so called "Text user interface Tui"), where you can use the cursor to manipulate it, and enter commands in the following GDB shell.

Now we can set breakpoints anywhere in the program. You can set a breakpoint on a line of the current source file by using the following command.

    1. Break [line number]

or set breakpoints for a specific function:

    1. Break [function name]

You can even set conditional breakpoints

    1. Break [line number] if [condition]

For example, in our sample code, you can set the following:

    1. Break one-off if i >

This way, the program loops 97 times and then stays on the "a++" statement. This is very handy and avoids the need to manually cycle 97 times.

Last but not least, we can set a "observe breakpoint", and when the observed variable changes, the program is stopped.

    1. Watch [variable]

Here we can set the following:

    1. Watch D

When the value of D changes, the program stops running (for example, when i>97 is true).

When a breakpoint is set, use the "Run" command to start running the program or as follows:

    1. R [Input parameters of the program (if any)]

In gdb, most command words can be abbreviated to a single letter.

No accident, the program will stay in line 11. Here, we can do some interesting things. The following command:

    1. Bt

The backtracking function (BackTrace) lets us know how the program reached this statement.

    1. Info Locals

This statement shows all the local variables and their values (as you can see, I didn't set the initial value for D, so it's now a value of any value).

Of course:

    1. P [variable]

This command can display the value of a specific variable and further:

    1. PType [variable]

You can display the type of the variable. So here you can be sure that D is a double type.

Now that we have reached this point, I might as well do this:

    1. Set var [variable] = [new value]

This overrides the value of the variable. However, it is important to note that you cannot create a new variable or change the type of the variable. We can do this:

    1. Set var a = 0

Like other good debuggers, we can step through the steps:

    1. Step

Using the command as above, it is possible to enter into a function by running to the next statement. or use:

    1. Next

This can run the next statement directly without entering the inside of the child function.

After the test is finished, delete the breakpoint:

    1. Delete [line number]

To continue running the program from the current breakpoint:

    1. Continue

Exit GDB:

    1. Quit

In short, with GDB, compile without praying to God, run without blood sacrifice, no longer printf ("test"). Of course, what is said here is not complete, and GDB's function is much more than that. So I strongly advise you to learn it more deeply. What I am interested in now is the integration of GDB into VIM. At the same time, here is a memo that records all of GDB's command lines for review.

What do you think of gdb? Will you compare it to the graphical debugger, and what advantages does it have? What do you think about integrating GDB into vim? Write your thoughts in the comments.

Via:http://xmodulo.com/gdb-command-line-debugger.html

Adrien Brochard Translator: Spccman proofreading: Wxy

This article by LCTT original translation, Linux China honors launch

Using the GDB command-line debugger to debug a C + + program "Go"

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.