Talk to GdB (fifth back: GDB View information)

Source: Internet
Author: User


Hello, crossing, we are talking about GDB's call stack debugging function, and how to use GDB to view the call

Stack. In this one, we continue to describe GDB's debugging capabilities: viewing information. Of course, we'll also show you how to use GDB to view programs

Run-time information. Gossip Hugh, words return to the positive. Let's talk gdb together!


Crossing, we need to look at the relevant information in the program when debugging, such as the value of the variable. GDB provides the ability to view information, which

The main features of the view are: viewing source code and variable values, tracking variables. Let's talk about these viewing features in detail below.


View Source : List or list n.list default list of the first 10 of the source code, it will start from the first line to display the program source code, each time

Displays 10 rows, and if you execute the list command again, it will continue to display the source code after the last row count. List n indicates that 10 lines of source code are displayed,

Where the nth row is in the middle of the 10 lines of code.


View variable values:info (abbreviated to i). Example: I locals to see the values of all variables when the program is run. Info shows all the changes in the program

Value, if you want to see only the value of a variable, use the command: print (abbreviated p) to print variable values, example: P ival

Represents the value of the view variable ival. This function is often used in conjunction with breakpoints, if you want to see the value of a variable during the program run, you can first let the process

And then use this function to view the value of the variable.


Tracking variables:Display. Example: Display index indicates that the trace displays the value of the variable index. The functionality provided by GDB can be seen as a view of the

variable function supplement. Because when you use P and I to display the value of a variable, it is displayed only once, and display can always show the value of the variable. This feature can be

To display the index value in a loop in a looping statement, the loop is automatically displayed once every time it is executed, and no manual view of the index value is required.

This is useful for tracking array out-of-bounds. If you do not want to track variable variables, use undisply to suppress the trace display. Example Undisply Index

Indicates that the value of the index of the displayed variable is no longer tracked.


Ancient Poetry Cloud: the paper came to the end of shallow, I know this matter to preach. Let's give an example to practice:

     1    #include <stdio.h>     2         3    void Exchange (int a, int b)     4    {     5        int s = 0;     6         7        s = A;     8        a = B;     9        B = S;    Ten} One and one    int main (    )    : {+        int a,b,i;        a = 3;        B = 5;    +        i = 0;        printf ("before change a =%d,b =%d  \ n", b);        Exchange (A, b)    ;        printf ("after change  a =%d,b =%d  \ n", b);        (i++ < 3)            printf ("I =%d \ n", i);        0 return;    +    }


1. Write the program . Open Vim, enter the above program, and save to the M.c file

2. Compile the program . Input in Terminal: Gcc-g m.c-o S

3. Run the program . Enter:./s in the terminal to get the following running results:

Before change a = 3,b = 5

After change a = 3,b = 5

I =1

I =2

I =3

As a result, we can see that the value of I prints no problem, but the Exchange function does not run correctly, before and after a and B exchange

The value is exactly the same. There seems to be a logic problem with the program, and we use GDB for debugging.


4. Debug the program . Enter in the terminal: GDB S.

(GDB) b Exchange //Where to set location breakpoints in function Exchange

Breakpoint 1 at 0x8048423:file M.C, line 5.

(GDB) Run //Start debugging, Encounter breakpoint will stop

Starting program:xxx/test/s

Before change a = 3,b = 5


Breakpoint 1, Exchange (A=3, b=5) at M.c:5 //stop at Breakpoint

5 int s = 0;

(GDB) n //Single Step debugging

7 s = A;

(GDB) //input Enter to continue single-step debugging

8 A = b;

(GDB) //input Enter to continue single-step debugging

9 B = s;

(GDB) //input Enter to continue single-step debugging

10}

(GDB) p a //view the value of variable a

$ = 5

(GDB) P B //View the value of variable B

$ = 3


By debugging the results, you can see where the breakpoint stops, A=3, b=5. At the end of the function when we view their values by P

Exchange has occurred. But after the function ends, their values are not exchanged. Can you tell me what the reason is? This is a typical C language.

Value call, learned C language, certainly know the reason, I will not say more.


Next, let's take a look at the function of the path variable.

(gdb) Display I //tracking variable i

(GDB) n //single-step debugging, omitting the results of the previous single step debugging

I =1 //The value of the program running into the display variable

(i++ < 3)

1:I = 1 //trace Displays the value of the variable

(GDB) //input Enter to continue single-step debugging

printf ("I =%d \ n", i);

1:I = 2

(GDB) //input Enter to continue single-step debugging

I =2

(i++ < 3)

1:I = 2

(GDB) //input Enter to continue single-step debugging

printf ("I =%d \ n", i);

1:I = 3

(GDB) //input Enter to continue single-step debugging

I =3

(i++ < 3)

1:I = 3

(GDB) //input Enter to continue single-step debugging

return 0;


As you can see from the results of the debugs, the value of I is displayed each time you run single-step debugging, and the values displayed by the path and the program run-time print out

to the same value. This is the function of tracking variables.


We illustrate how to use the View information functionality provided by GDB using the example. By viewing the program's runtime information, you can easily locate the program

, we hope that we can use this function flexibly and improve the efficiency of the debugging program.


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 to GdB (fifth back: GDB View information)

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.