Talk gdb together (second time: gdb single-step debugging)

Source: Internet
Author: User


Hello, crossing, we said in the last time that we simply introduced GDB. In this one, we introduce GDB's debugging capabilities: one-step

Debugging. Gossip Hugh, words return to the positive. Let's talk gdb together!


Crossing, let's start by talking about single-step debugging. As we all know, the program is executed according to the instruction, step by step, just the program runs

Quickly, so you can't see every step you've been through. This is a little bit like our usual walk, step-by-step forward, on the road can

To see a lot of scenery, if the car, then the speed is too fast, you will not see clearly the scenery on the road. One-step debugging is to not let the program quickly

Implementation, popular point, said is not let the program "Ride" to go, let the program run step by step.


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

    • 1. Start Gdb:gdb FILE.O (indicates that the FILE.O program is debugged using GDB);
    • 2. Start debugging: Start. At this time, GDB will start from the main function in the FILE.O program;
    • 3. Single-Step debugging: Next (can be abbreviated as N). The program runs to the next statement;
    • 4. Debug repeatedly: Continue to enter N, the program will jump to the next line. So again and again, the program will run in step

Here's a tip to share with you: After executing the first n command, if you want to continue executing the n command, then just press ENTER to

You can continue to execute the N command without continuing to enter n because the return key in gdb means repeating the command before it, which is a bit like

A point command in Linux.


When you encounter a function in a program during one-step debugging, the function call is treated as a whole, and you may not understand it, okay, let's give a

Example: The following three statements are in the program:

a =5;b = 3;

C =min (A, b); Min is a function to find the minimum value

printf ("Min is:%d", c);

When the program runs to "a =5;b = 3;" When this is used, the program runs to "C =min (A, b) by using the single-step Debug command N." This line, if you do it again,

Single-step debugging n, then the program will run to "printf (" min is:%d ", c);" This line. As we all know, functions are composed of multiple statements, not

will run out one step at a time, but when you step into debugging the function as a whole, let it run out of one step. If we want to see each of the functions

The steps to run the statement, what should I do? Crossing, the mighty GdB won't let us down, we can use GDB's command: Step

(abbreviated as s), use this command to enter the function. You can then use the single-step debug command to see each statement in the function step-by-step

Running steps. If you want to jump out of a function, you can use the command: finish. This is where the program calls the function, which is our example.

In "printf (" min is:%d ", c);" This line.


Crossing, we can see the "Footprints" of the program running through single-step debugging. During the debugging process, if you see the program running steps with our

The procedure is not the same as that programmed, so the program does not follow the steps we have designed. There must be something wrong with that.

We need to find out where the problem is. Because the program is run step-by-step, it's easy to find out which step went wrong,

And find out the cause of the problem. This is the role of single-step debugging. Of course, the single-step debugging has other functions, it can and other debugging methods

Together with the use, as to what is the debugging method, we later in the chapter back will be introduced.


We usually still feel that the computer program is not fast enough, you do not let it "ride", let it walk, this does not make the computer more slowly? Crossing said.

It makes sense. However, we slow down the execution of the program by stepping through it to see how the program performs every step of the way, so

Helps us to find bugs in the program, which makes it easier for us to modify bugs in the program. Imagine the program "sitting in a car" running so fast that you haven't

Have a look at its appearance, it has run without a trace, not to mention the implementation of its error.


Crossing, here's what we're going to say about GDB today. Want to know how to funeral, and listen to tell!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk gdb together (second time: gdb single-step debugging)

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.