stack_backtracing_inside_your_program

stack_backtracing_inside_your_program

ID:40102202

大小:299.08 KB

页数:15页

时间:2019-07-21

stack_backtracing_inside_your_program _第1页
stack_backtracing_inside_your_program _第2页
stack_backtracing_inside_your_program _第3页
stack_backtracing_inside_your_program _第4页
stack_backtracing_inside_your_program _第5页
资源描述:

《stack_backtracing_inside_your_program 》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库

1、StackBacktracingInsideYourProgramHowtouseabacktracetofollowtheexecutionpathandfindoutwhatwentwrongandwhere.如何使用backtrace函数追踪函数调用链,并且定位出错的地方以及出错原因。Ifyouusuallyworkwithnon-trivialCsources,youmayhavewonderedwhichexecutionpath(thatis,whichsequenceoffunctioncalls

2、)broughtyoutoacertainpointinyourprogram.Also,itwouldbeevenmoreusefulifyoucouldhavethatpieceofinformationwheneveryourbeautiful,bug-freeprogramsuddenlycrashes,andyouhavenodebuggerathand.Whatisneededisastackbacktraceand,thankstoalittleknownfeatureoftheGNUClibra

3、ry,obtainingitisafairlyeasytask.如果你经常面对一些大型的C程序的话,那么你可能想知道函数间的调用关系(函数调用链),从而帮助你定位程序特定的位置。当你的程序突然发生错误的时候,而你的环境又不允许使用调试器的话,你却能利用程序本身得到一些错误的信息的话,那就再好不过了。幸好glibc提供了backtrace函数,使得这一切变得很容易。StackFramesandBacktraces栈框架和栈回溯Beforedivingintothearticle,let'sbrieflygoove

4、rhowfunctioncallsandparameterspassworkinC.Inordertoprepareforthefunctioncall,parametersarepushedonthestackinreverseorder.Afterwards,thecaller'sreturnaddressalsoispushedonthestackandthefunctioniscalled.Finally,thecalledfunction'sentrycodecreatessomemorespaceo

5、nthestackforstorageofautomaticvariables.Thislayoutcommonlyiscalledastackframeforthatparticularinstanceofthefunctioncall.Whenmorefunctioncallsarenested,thewholeprocedureisrepeated,causingthestacktokeepgrowingdownwardsandbuildingachainofstackframes(seeFigure1)

6、.Thus,atanygivenpointinaprogramittheoreticallyispossibletobacktracethesequenceofstackframestotheoriginatingcallingpoint,uptothemain()function(tobeexact,uptothelibcfunction,whichcallsmain()whentheprocessstartsup).开始本文之前,让我们先简略的了解一下C语言的函数调用和参数传递。在函数调用之前,参数按照从右

7、向左的顺序入栈。接着,调用函数的返回地址也入栈,再接着才调用我们想调用的函数。最后,在被调用函数的栈内分配一些空间来存储变量(自动变量)。这种结构一般被称为栈框架。当有多个函数调用或者发生嵌套调用时,利用系统的栈空间来建立一个栈框架链(见图1)。因此,理论上在程序的任何一个位置,我们都可以回溯整个栈框架链,从而到达最初的main函数(实际上,main函数也是被libc库调用的)。图1:栈框架StackBacktracingfromwithinGDB利用GDB来栈回溯Gettingthestackbacktrac

8、ewithGDB(oranequivalentgraphicalfrontend)foraprogramthatcrashedwhilerunningisstraightforward:yousimplyissuethebtcommand,whichreturnsthelistoffunctionscalleduptothepointofthecrash.Asthisisastanda

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。