Join talkGDB (fourth time: GDB calls stack debugging)

Source: Internet
Author: User

Join talkGDB (fourth time: GDB calls stack debugging)

Hello, everyone, the last time we talked about the GDB breakpoint debugging function and how to use GDB for breakpoint debugging.

In this session, we will continue to introduce the debugging function of GDB: Call Stack debugging. Of course, we will also introduce how to use GDB to call the stack.

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

Let's talk about calling stacks first. As we all know, various functions are often used in programs, and some are provided by languages.

Library functions, such as printf (), and some functions defined by ourselves. Various functions call each other. Sometimes, when there are too many functions, it is difficult for us

Function stacks are used to display the call relationships between functions. This may be a bit abstract, no

It is easy to understand. Here is an example. For example, the program has the following code.

Void funA ()

{

Printf ("A function is called \ n ");

}

Void funB ()

{

Printf ("B function is called \ n ");

FunA ();

}

Void funC ()

{

Printf ("C function is called \ n ");

FunB ();

}

Void funD ()

{

Printf ("D function is called \ n ");

FunC ();

}


Int main ()

{


Printf ("show the call stack of functions \ n ");

FunD ();

Return 0;

}

The functions of these functions are relatively simple. There is only one output statement, indicating that the function is called. As you can see, the program defines four

Functions: funA, funB, funC, and funD. (No full name later, ABCD for short) call between functions

System: D-> C-> B->. When you compile and run the function, you can get the following results:

Show the call stack of functions

D function is called

C function is called

B function is called

A function is called

The call relationships between functions can also be seen from the running results of the program. If we compare D to the bottom of the stack, each call to a function is

During the stack import operation, Calling C in the dcall means to push C into the stack, and so on until the last function. This function call relationship conforms to the stack's "advanced"

After "features, we call it as a call stack. In addition, we can also see the call of the ABCD function from the running results of the program.

Links are arranged from the top of the stack to the bottom of the stack.

In actual programs, the functions of functions are not so simple, and the call relationships of functions are not so simple. If you want to learn more

Call relationship between numbers. What should I do? Don't worry. GDB provides the function to display the function call stack. Same as a single-step call, call stack call

There are also dedicated commands: backtrace (abbreviated as bt ). With this command, we can print out the function call stack through GDB.

For convenience, we also use the Code mentioned above.

1. Compile the program firstAnd add the debugging information: gcc-g file. c-o file

2. Start GDB for debugging.: Gdb file

3. Create a breakpoint at function D.: B funD. The running result is as follows: Breakpoint 3, funA () at file. c: 5

4. Run the programWhen a breakpoint stops running: run

5. view the function call stack: Bt. the following result is displayed:

(Gdb) bt // view the function call stack

#0 funA () at file. c: 5

#1 0x08048448 in funB () at file. c: 10

#2 0x08048461 in funC () at file. c: 15

#3 0x0804847a in funD () at file. c: 20

#4 0x08048496 in main () at file. c: 27

Through the running result, we can see that function A is at the bottom of the stack, and the MAIN function is at the top of the stack. In addition, there is a number at the beginning of each line. Of course

The number starts from 0, a bit similar to the position of elements in the array.

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

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.