The solution of the GDB function stack is out of the way __ function

Source: Internet
Author: User

Program core off, to debug, but the function stack is out of order, very disgusting ... After Google/wiki some, find two solutions.

1. Manual Restore BackTrace

Manual restore is actually looking at the stack of data, their own restore function stack, it sounds very complex in fact also relatively simple. There is no better example on hand, so everyone goes to see

Http://devpit.org/wiki/x86ManualBacktrace The example above. The example is very good, it is x86 below, AMD64 is similar below.

AMD64 below, nothing more than register into RBP, word length increased by one times. Of course, this side chose the manual search function return address, and then info symbol print function name, in fact, can also be formatted to directly print the function name:

Content within the Gdb>x/128ag RBP

So the manual restore approach becomes simple:

Gdb>info reg RBP *x86 replaced by info reg EBP

Gdb>x/128ag RBP content *x86 replaced with X/128aw EBP content

This allows you to see the stack of functions. If you want to parse the parameter is what, it is also possible, just more trouble, coolie work .... To resolve the parameters, you need to know the layout of the stack, you can refer to this article:

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

This approach is relatively simple, easy to practice, but there is a prerequisite, if the content of the stack is washed clean, you can not even see the hair (the fact is). So you need to start stack protection ... At least you can find the top of the stack function ...

GCC has parameters:-fstack-protector and-fstack-protector-all, strongly recommended to open ....

2. Manual Record BackTrace

The stack protection is turned on so that at least one function stack can be seen .... If you want to know more information, sorry, no ... Then look at the company's internal wiki, plus Google, learned that many people through the trace method to debug.:-D

To put it simply, in the GCC2 era, two interface functions were provided:

void __cyg_profile_func_enter (void *this_fn, void *call_site)
void __cyg_profile_func_exit (void *this_fn, void *call_site)

Easy for big guys to do profile, and then a lot of people use these two functions to debug the code.:-D

function is very simple, the first is the function into the stack, the second is the function out of the stack. So you just have to maintain a stack yourself, and then when he goes into the stack, you also go into the stack (just record the function address), and you also stack it out of the stack. When the program hangs up, you look at your own maintenance stack, so you can get a second hand function stack ( First-hand may be destroyed). Then in the Go info symbol or x/num AG format printing can also.

It should be noted that the compiler needs to add parameter-finstrumnet-function, and here the function declaration needs to add __attribute__ ((no_instrument_function)) macros, otherwise he will be infinite recursive call down,:-)

If it is a single thread, you can do a stack on the line, if more than one thread, a stack ~ ~ ~

Reference:

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.