Talk GDB together (second: single-step debugging of GDB), single-step gdb

Source: Internet
Author: User

Talk GDB together (second: single-step debugging of GDB), single-step gdb


Hello, everyone. We briefly introduced GDB in the previous article. In this example, we will introduce the debugging function of GDB: one step.

Debugging. When you leave the rest of your time, your words will go right. Let's talk GDB together!


Let's talk about what single-step debugging is. As we all know, the program is executed step by step according to instructions, but the program runs

So you can't see every step you 've taken. This is similar to walking normally. We can walk forward step by step on the road.

In order to see a lot of scenery, if the car, then the speed is too fast, you will not see the scenery on the road. One-step debugging means that the program will not be quickly executed.

Execution, in layman's terms, is to say that the program is not allowed to "Ride", let the program run step by step.


Let's take a look at how to use GDB to run the program step by step. The detailed steps are as follows:

  • 1. Start GDB: gdb file. o (it indicates using GDB to debug the file. o program );
  • 2. start debugging: start. At this time, GDB will start executing the main function in the file. o program;
  • 3. Single-step debugging: next (n ). Run the program to the next statement;
  • 4. Repeated debugging: If n is input, the program will jump to the next line. Once and again, the program runs step by step.

Here is a tip to share with you: After executing the first n command, if you want to continue to execute n command, you only need to press the Enter key

You can continue to run the n command without entering n, because the return key in GDB indicates repeated execution of the previous command, which is a bit similar

Linux.


When you encounter a function in a program during the single-step debugging process, the function call will be considered as a whole. You may not understand it. It doesn't matter. Let's give it an example.

Example: The program has the following three statements:

A = 5; B = 3;

C = min (a, B); // min is a function that calculates the minimum value.

Printf ("min is: % d", c );

When the program runs "a = 5; B = 3;", the program runs to "c = min (a, B)" using the single-step DEBUG command n ); "This line, if you execute again

If you debug n in one step, the program runs to "printf (" min is: % d ", c. As we all know, a function is composed of multiple statements. No

The function is run in one step, but the function is regarded as a whole during single-step debugging, so that it can be completed in one step. If you want to see

Statement execution steps, what should I do? The powerful GDB won't disappoint us. We can use the GDB command: step

(Abbreviated as s). Use this command to enter the function. Then you can use the single-step debugging command to view every statement in the function step by step.

. If you want to jump out of the function, run the command: finish. At this time, the program will call the function, that is, our example.

"Printf (" min is: % d ", c.


After one-step debugging, we can see the "footprints" of the program running ". During the debugging process, if you see the program running steps and

The steps designed for the program are different in advance, which means that the program did not follow the steps we designed. There must be something wrong with this,

We need to find out the problem. Because the program runs step by step, it is easy for us to find out which step has a problem.

Find out the cause of the problem. This is the role of single-step debugging. Of course, single-step debugging has other functions. It can be used with other debugging methods.

For debugging methods, we will introduce them in the chapter.


We usually think that the program on the computer is not fast enough. You still don't want it to "Ride" and let it walk. Isn't it even slower? What the viewer said

It makes sense. However, we use single-step debugging to slow down the program execution, so that we can see how the program is executed in each step.

It helps us to find errors in the program, so that we can easily modify errors in the program. Imagine that the program is running so fast in a car, you haven't

As you can see it clearly, it has run without a trace, not to mention the error in its execution.


Let's talk about GDB today. I want to know what to do later, and listen to the next decomposition!


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.