[GDB] solution to function stack disorder

Source: Internet
Author: User

ProgramCore drop, go to debug, but the function stack is messy, so it's disgusting... after some Google/wiki times, find two solutions.

1. Manually restore backtrace

Manual restoration is actually to look at the data in the stack. It sounds complicated and simple to restore the function stack by yourself. There are no good examples at hand, so let's take a look.

The example above http://devpit.org/wiki/x86ManualBacktrace. That example is very good, is under x86, amd64 below is also similar.

Under amd64, the register is changed to RBP, And the font length is doubled. of course, you have selected to manually find the return address of the function, and then print the function name in info symbol. In fact, you can also print the function name directly using GDB format:

Content in GDB> X/128ag RBP

Therefore, the manual restoration method becomes very simple:

GDB> info Reg RBP* Replace x86 with info Reg EBP

Content in GDB> X/128ag RBP* Replace x86 with x/128aw EBP.

In this way, we can see the function stack. if you want to parse what the parameters are, it's okay. It's just a bit of a hassle .... to parse parameters, you must know the STACK layout. For details, refer to this article.Article:

Http://blog.csdn.net/liigo/archive/2006/12/23/1456938.aspx

This method is relatively simple and easy to practice, but there is a premise that if the stack content is washed away, you will not even be able to see it (this is the case ). so you need to start stack protection... at least you can find the function at the top of the stack...

GCC has parameters:-fstack-protector and-fstack-protector-all. We strongly recommend that you enable this function ....

2. manually record backtrace

With stack protection enabled, at least one function stack will be seen .... if you want more information, sorry, no... later, let's look at the company's internal wiki, and Google. I learned that many people use the TRACE method to debug. :-d

In the gcc2 era, two interface functions are provided:

Void _ cyg_profile_func_enter (void * this_fn, void * call_site)
Void _ cyg_profile_func_exit (void * this_fn, void * call_site)

This makes it easy for the big guy to do profile, and then many people use these two functions for debugging.Code.:-D

Function functions are very simple. The first is function stack, and the second is function stack. therefore, you only need to maintain a stack by yourself, and then you also go to the stack (only record the function address) when it comes to the stack. When you exit the stack, you also go out of the stack. when the program crashes, you can see the stack you maintain, so that you can get the second-hand function stack (first-hand may be damaged ). you can also format and print it in info symbol or X/num AG.

Note that the finstrumnet-function parameter must be added for compilation, and the _ attribute _ (no_instrument_function) macro must be added for function declaration, otherwise, he will call the API infinitely recursively ,:-)

For a single thread, just create a stack. for multiple threads, one thread and one stack ~~~

Refer:

Http://devpit.org/wiki/x86ManualBacktrace

Http://blogold.chinaunix.net/u3/111887/showart_2182373.html

Http://blog.csdn.net/liigo/archive/2006/12/23/1456938.aspx

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.