




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
SystemTap
Linux下的萬能觀測工具褚霸核心系統(tǒng)數(shù)據(jù)庫組chuba@2010/11/18Agenda介紹SystemTap
安裝和系統(tǒng)要求
實(shí)踐例子
參考和雜項(xiàng)
結(jié)論SystemTap是什么?Accordingto/systemtap/
SystemTapprovidesfreesoftware(GPL)infrastructuretosimplifythegatheringofinformationabouttherunningLinuxsystem.Thisassistsdiagnosisofaperformanceorfunctionalproblem.SystemTapeliminatestheneedforthedevelopertogothroughthetediousanddisruptiveinstrument,recompile,install,andrebootsequencethatmaybeotherwiserequiredtocollectdata.
觀察活體系統(tǒng)最佳工具,前提是你懂得如何觀察!SystemTap是如何工作的1.writeorchooseascriptdescribingwhatyouwanttoobserve
2.staptranslatesitintoakernelmodule
3.staploadsthemoduleandcommunicateswithit
4.justwaitforyourdata
五步走#
stap-uvtest.stp
Pass1:parseduserscriptand74libraryscript(s)using86868virt/20488res/1792shrkb,in190usr/20sys/209realms.
Pass2:analyzedscript:1probe(s),0function(s),0embed(s),0global(s)using87264virt/21148res/1976shrkb,in10usr/0sys/7realms.
Pass3:translatedtoCinto"/tmp/stapz2iv97/stap_aef621603e006af62084b361e0a0c981_553.c"using87264virt/21332res/2144shrkb,in0usr/0sys/0realms.
Pass4:compiledCinto"stap_aef621603e006af62084b361e0a0c981_553.ko"in1230usr/160sys/1384realms.
Pass5:startingrun.
Pass5:runcompletedin10usr/20sys/12331realms.SystemTap探測點(diǎn)例子SystemTapisallaboutexecutingcertainactionswhenhittingcertainprobepoints.
syscall.read
whenenteringread()systemcallsyscall.close.return
whenreturningfromtheclose()systemcallmodule("floppy").function("*")
whenenteringanyfunctionfromthe"floppy"modulekernel.function("*@net/socket.c").return
whenreturningfromanyfunctioninlenet/socket.ckernel.statement("*@kernel/sched.c:2917")
whenhittingline2917oflekernel/sched.c更多探測點(diǎn)例子timer.ms(200)
every200milliseconds
process("/bin/ls").function("*")
whenenteringanyfunctionin/bin/ls(notitslibrariesorsyscalls)
process("/lib/libc.so.6").function("*malloc*")
whenenteringanyglibcfunctionwhichhas"malloc"initsname
kernel.function("*exit*").return
whenreturning
fromanykernelfunctionwhichhas"exit"initsname
RTFMformore(manstapprobes).SystemTap編程語言mostlyC-stylesyntaxwithafeelingofawk
builtinassociativearrays
builtinaggregatesofstatisticaldata
veryeasytocollectdataanddostatisticsonit(average,min,
max,count,...)
manyhelperfunctions(builtinandintapsets)
RTFM:SystemTapLanguageReferenceshippedwithSystemTap
(langref.pdf)Performancesandsafetylanguage-levelsafetyfeaturesnopointersnounboundedloopstypeinferenceyoucanalsowriteprobehandlersinC(with-g)butdon'tcomplainifyoubreakstuff
runtimesafetyfeaturesstapenforcesmaximumruntimeforeachprobehandlervariousconcurrencyconstraintsareenforcedoverloadprocessing(don'tallowstaptotakeupalltheCPUtime)manythingscanbeoverridenmanuallyifyoureallywantseeSAFETYANDSECURITYsectionofstap(1)
Theoverheaddependsalotofwhatyouaretryingtodobutingeneralstapwilltrytostopyoufromdoingsomethingstupid(butthenyoucanstillforceittodoit).
Somehelperfunctionsyou'llseealotpid()whichprocessisthis?
uid()whichuserisrunningthis?
execname()whatisthenameofthisprocess?
tid()whichthreadisthis?
gettimeofday_s()epochtimeinseconds
probefunc()whatfunctionarewein?
print_backtrace()figureouthowweendeduphere
Therearemanymanymore.RTFM(manstapfuncs)andexplore
/usr/share/systemtap/tapset/.Somecoolstapoptions-xtraceonlyspeciedPID(onlyforuserlandprobing)
-crungivencommandandonlytraceitanditschildren
(willstilltraceallthreadsforkernelprobes)
-Llistprobepointsmatchinggivenpatternalongwith
availablevariables
-dloadgivenmoduledebuginfotohelpwithsymbolresolutioninbacktraces
-gembedCcodeinstapscript
unsafe,dangerousandfunAgenda介紹SystemTap
安裝和系統(tǒng)要求
實(shí)踐例子
參考
結(jié)論RequirementsSystemTap探測用戶空間程序需要utrace的支持,但是這個特性還沒有被Linux上游吸收。Redhat的發(fā)行版本目前支持這個特性。
源碼級別跟蹤需要安裝符號信息包層面需要安裝package-debuginfoonRPMdistros用戶自己的程序需要gcc
-g-gdwarf-2-g3編譯
stap腳本是編譯成內(nèi)核模塊運(yùn)行的,需要root權(quán)限
安裝SystemTapRHEL5U4需要安裝內(nèi)核符號信息:rpm-ikernel-debuginfo-common-2.6.18-164.el5.x86_64.rpm
rpm-ikernel-debuginfo-2.6.18-164.el5.x86_64.rpm
由于5U4帶的SystemTap是0.97版本,需要升級到1.3:./configureprefix=/usr&&make&&makeinstall
如何驗(yàn)證是否成功:#staptopsys.stp
SYSCALL
COUNT
read
48
fcntl
42
...
fstat
1
--------------------------------------------------------------Agenda介紹SystemTap
安裝和系統(tǒng)要求
實(shí)踐例子
參考和雜項(xiàng)
結(jié)論Example:誰在執(zhí)行我們的程序Listing:exec.stp
probesyscall.exec*{
printf("exec%s%s\n",execname(),argstr)
}
$stap-L'syscall.exec*'
syscall.execvename:stringfilename:stringargs:stringargstr:string$filename:char*$argv:char**$envp:char**$regs:structpt_regs*
#stapexec.stp
execsshd/usr/sbin/sshd"-R"
execsshd/bin/bash
例子:誰殺了我的程序Listing:sigkill.stpprobesignal.send{
if(sig_name=="SIGKILL")
printf("%swassentto%s(pid:%d)by%suid:%d\n",sig_name,pid_name,sig_pid,execname(),uid())
}#kill-9`pgreptop`
#
stapsigkill.stp
SIGKILLwassenttotop(pid:19281)bybashuid:50920Exampletac.c:工具函數(shù)#include<stdio.h>
#include<stddef.h>
#include<string.h>char*haha="wahaha\n";char*read_line(FILE*fp,char*buf,size_tlen){
returnfgets(buf,len,fp);}char*reverse_line(char*line,size_tl){
char*s=line,*e=s+l-sizeof("\n"),t;
while(s<e){
t=*s,*s=*e,*e=t;s++,e--;}
returnline;}voidwrite_line(char*line){fputs(line,stdout);}
Exampletac.ccontinued:主程序intmain(intargc,char*argv[]){
charbuf[4096],*line;
FILE*fp=stdin;
if(argc!=1){fp=fopen(argv[1],"r");}
if(fp==NULL){fprintf(stdout,"usage:%sfilename\n",argv[0]);return-1;}
while((line=read_line(fp,buf,sizeof(buf)))){
line=reverse_line(line,strlen(line));
write_line(line);
}
if(argc!=1)fclose(fp);
return0;
}編譯tac
#必須要帶調(diào)試信息#gcc-g-gdwarf-2-g3tac.c
#確認(rèn)符號信息的存在
#stap-L'process("a.out").function("*")'
process("/tmp/a.out").function("main@/tmp/tac.c:25")$argc:int$argv:char**$buf:char[]$line:char*$fp:FILE*
process("/tmp/a.out").function("read_line@/tmp/tac.c:7")$fp:FILE*$buf:char*$len:size_t
process("/tmp/a.out").function("reverse_line@/tmp/tac.c:11")$line:char*$l:size_t$s:char*$e:char*$t:char
process("/tmp/a.out").function("write_line@/tmp/tac.c:21")$line:char*Example1:讀出程序的參數(shù)functionget_argv_1:long(argv:long)%{/*pure*/
THIS->__retvalue=(long)((char**)THIS->argv)[1];
%}
probeprocess("a.out").function("main"){
filename="stdin";
if($argc>1){
filename=user_string(get_argv_1($argv));
}
println(filename);
}Example1continued:#
echo"hi"|./a.out
#
./a.outtac.c
#
stap-gu./ex1.stp
:)
stdin
tac.cExample2:callgraphforanythingfunctiontrace(entry_p,extra){
%($#>1%?if(tid()intrace)%)
printf("%s%s%s%s\n",
thread_indent(entry_p),
(entry_p>0?"->":"<-"),
probefunc(),
extra)
}
probe$1.call
{trace(1,$$parms)}probe$1.return{trace(-1,$$return)}Example2continued:
#
echo"hi"|./a.out
#
sudostap./ex2.stp'process("a.out").function("*")'
:)
0a.out(18123):->mainargc=0x1argv=0x7fff351ee0c8
30a.out(18123):->readlinefp=0x3f7bb516a0buf=0x7fff351ecfd0len=0x1000
590a.out(18123):<-readlinereturn=0x7fff351ecfd0
611a.out(18123):->reverse_lineline=0x7fff351ecfd0l=0x3
625a.out(18123):<-reverse_linereturn=0x7fff351ecfd0
642a.out(18123):->write_lineline=0x7fff351ecfd0
731a.out(18123):<-write_line
748a.out(18123):->readlinefp=0x3f7bb516a0buf=0x7fff351ecfd0len=0x1000
762a.out(18123):<-readlinereturn=0x0
770a.out(18123):<-mainreturn=0x0Example3:
獲取行長度globalline_len
probeprocess("a.out").statement("reverse_line@tac.c+1"){
line_len<<<($e-$s+2);
}
probeend{
if(@count(line_len)>0)print(@hist_linear(line_len,8,128,8));
}Example3continued:#ls-al|./a.out#
./ex3.stp
:)value|--------------------------------------------------count
<8|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
64
8|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
69
16|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
68
24|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
68
32|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
68
40|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
68
48|@@@@@@@@@@@@@@@@@@@@@@@@@
50
56|
0
64|
0Example4:
行反轉(zhuǎn)平均時間globalt,call_time
probeprocess("a.out").function("reverse_line"){
t=gettimeofday_ns()
}
probeprocess("a.out").function("reverse_line").return{
call_time<<<(gettimeofday_ns()-t)
}
probeend{
if(@count(call_time)>0)printf("avgreverse_lineexecutetime:%dns\n",@avg(call_time))
}Example4continued:#ls-al|./a.out#
./ex4.stp
:)
avgreverse_lineexecutetime:6651nsExample5:列出調(diào)用棧probeprocess(@1).function(@2){
print_ubacktrace();
exit();
}Example5continued:#ls-al|./a.out#stap./ex5.stp'./a.out''*_line'
:)
0x40066d:reverse_line+0xc/0x61[a.out]
0x40078f:main+0xaf/0x100[a.out]
0x3bd441d994[libc-2.5.so+0x1d994/0x357000]Example6:修改程序的行為globallinefunctionalert_line(line:long)%{/*pure*/
strcpy((char*)THIS->line,"abcdefg\n");
%}
probeprocess("a.out")
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)代辦公軟件的安全防護(hù)策略研究
- 2025至2030年中國聚四氟乙烯棒管數(shù)據(jù)監(jiān)測研究報告
- 二零二五年度跨境貸款借款合同授信額度管理協(xié)議
- 二零二五年度房屋租賃合同物業(yè)服務(wù)補(bǔ)充條款
- 2025年度智能家居租賃服務(wù)協(xié)議延期補(bǔ)充協(xié)議
- 2025年度能源消耗評估與節(jié)能合作協(xié)議
- 二零二五年度農(nóng)業(yè)種植與科研機(jī)構(gòu)合作合同
- 2025年度生態(tài)園林建設(shè)植樹承包協(xié)議書
- 二零二五年度北京市工商局股權(quán)質(zhì)押登記與變更風(fēng)險評估與法律審查服務(wù)合同
- 疫情背景下全球經(jīng)濟(jì)環(huán)境對企管體系挑戰(zhàn)的解讀與未來規(guī)劃探討報告
- 兒童氣管插管醫(yī)學(xué)課件
- 內(nèi)燃機(jī)車無火回送操作方法
- 第十四屆全國交通運(yùn)輸行業(yè)職業(yè)技能競賽(公路收費(fèi)及監(jiān)控員)賽項(xiàng)題庫-上(單選題匯總-共3部分-1)
- NBT 10664-2021 核電廠工程巖土試驗(yàn)規(guī)程
- 2019年遠(yuǎn)程開具電子處方管理制度及操作規(guī)程版
- 奧太焊機(jī)維修教材MZ系列
- 哈利波特和死亡圣器PPT培訓(xùn)課件
- 財務(wù)會計實(shí)務(wù)教學(xué)課件匯總?cè)纂娮咏贪?完整版)
- JJG 818-2018磁性、電渦流式覆層厚度測量儀-(高清現(xiàn)行)
- 房室結(jié)雙徑路傳導(dǎo)的基本電生理特征與常見心電圖表現(xiàn)分析課件
- 《傳統(tǒng)功法-八段錦》教學(xué)大綱
評論
0/150
提交評論