ftrace的使用.docx

上传人:b****5 文档编号:12700581 上传时间:2023-04-21 格式:DOCX 页数:11 大小:20.68KB
下载 相关 举报
ftrace的使用.docx_第1页
第1页 / 共11页
ftrace的使用.docx_第2页
第2页 / 共11页
ftrace的使用.docx_第3页
第3页 / 共11页
ftrace的使用.docx_第4页
第4页 / 共11页
ftrace的使用.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

ftrace的使用.docx

《ftrace的使用.docx》由会员分享,可在线阅读,更多相关《ftrace的使用.docx(11页珍藏版)》请在冰豆网上搜索。

ftrace的使用.docx

ftrace的使用

ftrace的使用

Thisarticleexplainshowtosetupftraceandbeabletounderstandhowtotracefunctions.Itshouldbeusefulforcurrentkerneldevelopersanddevicedriverdeveloperswhowanttodebugkernelissues,

andalsoforstudentswhoarekeentopursueaLinuxsystemsprogrammingcareer.

  本文探索如何建立ftrace并能理解如何跟踪函数。

ftrace对于内核开发者和设备驱动开发者在调试内核问题的时候应该很有用。

对于渴望探索Linux系统编程的爱好者来说,本文也值得学习一下。

ftrace(FunctionTracer)isthe“Swissarmyknife”ofkerneltracing.ItisatracingmechanismbuiltrightintotheLinuxkernel.Ithasthecapabilitytoseeexactlywhatishappeninginthekernel,anddebugit.ftraceismorethanamerefunctiontracer,

andhasawidevarietyoftracingabilitiestodebugandanalyseanumberofissueslikelatency,unexpectedcodepaths,performanceissues,etc.Itcanalsobeusedasagoodlearningtool.

ftracewasintroducedinkernel2.6.27byStevenRostedyandIngoMolnar.Itcomeswithitsownringbufferforstoringtracedata,andusesthe

GCCprofilingmechanism.

  ftrace(函数跟踪)是内核跟踪的“瑞士军刀”。

它是内建在Linux内核中的一种跟踪机制。

它能深入内核去发现里面究竟发生了什么,并调试它。

ftrace不只是一个函数跟踪工具,它的跟踪能力之强大,还能调试和分析诸如延迟、意外代码路径、性能问题等一大堆问题。

它也是一种很好的学习工具。

  ftrace是由StevenRostedy和IngoMolnar在内核2.6.27版本中引入的。

它有自己存储跟踪数据的环形缓冲区,并使用GCC配置机制。

Prerequisites

预备知识

Youneeda32-bitor64-bitLinuxmachinewithakerneldevelopmentenvironment,andasnewakernelaspossible(thenewerthekernel,themorethetracingoptionsyouget).IuseaFedoraCore13(x86_64)machineinmyenvironment,butanydistribution

wouldsuffice.

  你需要一台有内核开发环境的32位或者64位Linux机器,内核版本越新越好(内核越新,跟踪选项就越多)。

我是基于Fedora13的内核(X86_64)的机器,但其它版本也适用。

SettingupFtrace

设置ftrace

debugfsneedstobesetuptorunonthemachineyouwanttouseftraceon.Ifyouareunawareofhowtosetupdebugfs,dorefertomydebugfsarticlefromlastmonth.

debugfsshouldhavebeenmountedon/sys/kernel/debugfs,andiftracingisenabled,youshouldbeabletoseeadirectorycalledtracingunderdebugfs.Ifdebugfsisnotmounted,youcanissuethefollowingcommand:

  使用ftrace要求你的机器上配置有debugfs。

如果你不知道怎么配置debugfs,请参阅我上个月的文章。

  debugfs应该被挂载在/sys/kernel/debugfs,如果跟踪选项已启用,你应该能够在debugfs下面看到一个叫tracing的目录。

如果没有挂载debugfs,请按以下操作:

#mount-tdebugfsnodev/sys/kernel/debug

Ifyouareunabletoseethetracingsubdirectory,youwillneedtoenabletracinginthekernelconfiguration,andrecompileit.Lookforthefollowingoptionstobeenabledinthekernelconfigurationpath(refertoFigure1):

KernelHacking->Tracers

1.KernelFunctionTracer(FUNCTION_TRACER)

2.KernelFunctionGraphTracer(FUNCTION_GRAPH_TRACER)

3.Enable/disableftracedynamically(DYNAMIC_FTRACE)

4.Tracemaxstack(STACK_TRACER)

  如果你看不到tracing子目录的话,你应该在内核配置上启用相关选项,然后重编译内核。

请在你的内核配置中找到如图1所示的选项,启用它们:

KernelHacking->Tracers

1.KernelFunctionTracer(FUNCTION_TRACER)

2.KernelFunctionGraphTracer(FUNCTION_GRAPH_TRACER)

3.Enable/disableftracedynamically(DYNAMIC_FTRACE)

4.Tracemaxstack(STACK_TRACER)

 

Figure1:

Kernelconfigurationoptionsfortracing图1:

tracing的内核配置选项

Dependingonyourarchitecture,afewmoretracerscanbeenabledduringcompilation,asperrequirements.Thelistedtracersarefordebugging.Oncethekernelcompilationiscomplete,andyouhavebootedtothenewkernel,tracingcanbeinitiated.

  根据你的架构,在选择上面的选项时,一些其他的选项根据依赖关系可能也会自动被启用。

上面所列的选项主要是用于跟踪所用。

内核编译完成之后,你只需要重启机器,tracing功能就可以用了。

Tracing

跟踪

Filesinthetracingdirectory(/sys/kernel/debug/tracing)controlthetracingability(refertoFigure2foralistoffiles).Afewfilescouldbedifferent,dependinguponwhattracersyouselectedduringkernelconfiguration.Youcanobtaininformation

onthesefilesfromthe/Documentation/tracingdirectory.

  tracing目录(/sys/kernel/debug/tracing)中的文件(如图2所示)控制着跟踪的能力。

根据你在内核配置时的选项的不同,这里列的文件可能稍有差异。

你可以在内核源代码目录下/Documentation/trace[1]目录中找到这些文件的信息。

Figure2:

Tracingfiles图2:

用于跟踪的文件

Let’sexploreafewoftheimportantones:

●available_tracers:

Thisshowswhattracersarecompiledtotracethesystem.●current_tracer:

Displayswhattraceriscurrentlyenabled.Canbechangedbyechoinganewtracerintoit.●tracing_enabled:

Letsyouenableordisablethecurrenttracing.

●trace:

Actualtraceoutput.

●set_ftrace_pid:

SetsthePIDoftheprocessforwhichtraceneedstobeperformed.

Tofindouttheavailabletracers,justcattheavailable_tracersfile.Tracersinthespace-separatedoutputinclude:

nop(notatracer,thisissetbydefault);function(functiontracer);function_graph(functiongraphtracer),etc:

  让我们看看里面几个重要的文件:

●available_tracers:

这表示哪些被编译里系统的跟踪器。

●current_tracer:

这表示当前启用的哪个跟踪器。

可以通过echo向表输入一个新的跟踪器来改变相应值。

●tracing_enabled:

让你可以启用或者禁用当前跟踪功能

●trace:

实际地的跟踪输出。

●set_ftrace_pid:

设置跟踪所作用的进程的PID。

  要找到哪些跟踪器可用,你可以对available_tracers文件执行cat操作。

与输出空间分离的跟踪器有:

nop(它不是一个跟踪器,是默认设置的一个值)、函数(函数跟踪器)、函数图(函数图跟踪器),等等,如下所示:

#catavailable_tracers

blkfunction_graphmmiotracewakeup_rtwakeupirqsofffunctionsched_switchnop

Onceyouidentifythetracerthatyouwanttouse,enableit(ftracetakesonlyonetraceratatime):

  当你知道你需要使用哪个跟踪器后,启用它(ftrace每次只能打开一个跟踪器):

#catcurrent_tracer##toseewhattraceriscurrentlyinuse.

#echofunction>current_tracer##selectaparticulartracer.

#catcurrent_tracer##checkwhetherwegotwhatwewanted.

#catcurrent_tracer##查看当前在用哪个跟踪器。

#echofunction>current_tracer##选择一个特定的跟踪器。

#catcurrent_tracer##检查是否是你所设置的跟踪器。

Tostarttracing,usethefollowingcommands:

  使用下面的命令可以开始跟踪:

#echo1>tracing_enabled##initiatetracing

#cattrace>/tmp/trace.txt##savethecontentsofthetracetoatemporaryfile.

#echo0>tracing_enabled##disabletracing

#cat/tmp/trace.txt##toseetheoutputofthetracefile.

#echo1>tracing_enabled##初始化跟踪。

#cattrace>/tmp/trace.txt##将跟踪文件保存到一个临时文件。

#echo0>tracing_enabled##禁用跟踪功能

#cat/tmp/trace.txt##查看trace文件的输出。

Thetraceoutputisnowinthetrace.txtfile.AsampleoutputofafunctiontraceobtainedwiththeabovecommandsisshowninFigure3.

  现在trace文件的输入在trace.txt文件中。

通过上面操作所得到的函数跟踪的一个示例输出如图3所示。

Figure3:

Sampletraceoutput图3:

示例跟踪输出

本文英文源文

Let’sbeginwithtraceroptions.Theoutputofthetracingcanbecontrolledbyafilecalledtrace_options.Variousfieldscanbeenabledanddisabledbyupdatingoptionsinthefile/sys/kernel/debug/tracing/trace_options.Asampleoftrace_optionscan

beviewedinFigure1.

  让我们从tracer的选项开始。

tracing的输入可以由一个叫trace_options的文件控制。

可以通过更新/sys/kernel/debug/tracing/trace_options文件的选项来启用或者禁用各种域。

trace_options的一个示例如图1所示。

Figure1:

Traceoptions图1:

Trace选项

Todisableatracingoption,a“no”keywordneedstobeaddedtothestartoftheline.Forexample,echonotrace_printk>trace_options.(Remembernottohaveaspacebetweennoandtheoption.)Toenableatraceagain,youcoulduse,forinstance,echo

trace_printk>trace_options.

  要禁用一个跟踪选项,只需要在相应行首加一个“no”即可。

比如,echonotrace_printk>trace_options。

(no和选项之间没有空格。

)要再次启用一个跟踪选项,你可以这样:

echotrace_printk>trace_options。

ftraceforaspecificprocess

ftrace之特殊进程

ftraceallowsyoutoperformtracingevenforaspecificprocess.Inthe/sys/kernel/debug/tracingdirectory,thefileset_ftrace_pidneedstobeupdatedwiththePIDoftheprocessyouwanttobetraced.Thetraceprocess.shsamplescriptbelowshowshow

tocapturethePIDon-the-go,andenabletracing.

  ftrace允许你对一个特殊的进程进行跟踪。

在/sys/kernel/debug/tracing目录下,文件set_ftrace_pid的值要更新为你想跟踪的进程的PID。

以下traceprocess.sh示例脚本向你展示了如何抓取当前运行的进程的PID,并进行相应跟踪。

 

ViewCode

 

Youcanrefineitwithyourowninnovations.Runitwiththecommandwhoseprocessyouwanttotraceastheargument,asshowninFigure2,wherewetracedthelscommand.

  你也可以根据需要改进上面的脚本。

在命令行中运行上面的脚本,并以你想跟踪的进程名作为参数,如图2中跟踪ls命令所示。

Figure2:

Executingtraceprocess.shandviewingtraceoutput图2:

执行traceprocess.sh并查看跟踪输出

Oncetracingiscomplete,youneedtocleartheset_ftrace_pidfile,forwhichyoucanusethefollowingcommand:

  当跟踪完成后,你需要清除set_ftrace_pid文件,请用如下命令:

:

>set_ftrace_pid

Functiongraphtracer

函数图跟踪器

Thefunctiongraphtracertrackstheentryandexitofafunction,andisquiteusefultotrackitsexecutiontime.Functionswithadurationofover10microsecondsaremarkedwitha“+”,andthoseover100microsecondswith“!

”.Toenablethefunction

graphtracer,useechofunction_graph>current_tracer.ThesampleoutputisasshowninFigure3.

  函数图跟踪器对函数的进入与退出进行跟踪,这对于跟踪它的执行时间很有用。

函数执行时间超过10微秒的标记一个“+”号,超过1000微秒的标记为一个“!

”号。

通过echofunction_graph>current_tracer可以启用函数图跟踪器。

示例输入如图3所示。

Figure3:

Traceoutputoffunctiongraphtracer图3:

函数图跟踪器跟踪输出

Therearealotoftracers;theentirelistisinlinux/Documentation/trace/ftrace.txt.Thetracersareenabledordisabledbyechoingthetracernameintothecurrent_tracerfile.

  有很多跟踪器,所有的列表在linux/Documentation/trace/ftrace.txt文件中找得到。

通过将跟踪器的名字echo到current_tracer文件中可以启用或禁用相应跟踪器。

Dynamictracing

动态跟踪

Wecaneasilygetinundatedwiththeamountofdatathefunctiontracerthrowsatus.Thereisadynamicwaytofilterjustthefunctionsweneed,andeliminatethosethatwedon’tneed:

tospecifytheminthefileset_ftrace_filter.(Firstfindthefunction(s)

youwant,fromtheavailable_filter_functionsfile.)SeeFigure4foranexampleofdynamictracing.

  我们会很轻易地被淹没在函数跟踪器所抛给我们的大量数据中。

有一种动态的方法可以过滤出我们所需要的函数,排除那些我们不需要的:

在文件set_ftrace_filter中指明。

(首先从available_filter_functions文件中找到你需要的函数。

)图4就是一个动态跟踪的例子。

Figure4:

Listingfilterfunctions,usingwithwild-cards图4:

使用通配符列出过滤出的函数

Asyoucansee,youcanevenusewild-cardsforthefunctions’names.Iusedallthevmalloc_functions,andsetthemwith:

echovmalloc_*>set_ftrace_filter.

  如你所看到的,你甚至可以对函数的名字使用通配符。

我需要用所有的vmalloc_函数,通过echovmalloc_*>set_ftrace_filter进行设置。

Eventtracing

事件跟踪

Tracingcanalsobetriggeredwhenparticulareventshappenonthesystem.Ava

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1