trace.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 Nadia Yvette Chambers
  13. */
  14. #include <linux/ring_buffer.h>
  15. #include <generated/utsrelease.h>
  16. #include <linux/stacktrace.h>
  17. #include <linux/writeback.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/notifier.h>
  21. #include <linux/irqflags.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/linkage.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kprobes.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu.h>
  31. #include <linux/splice.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/string.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/slab.h>
  36. #include <linux/ctype.h>
  37. #include <linux/init.h>
  38. #include <linux/poll.h>
  39. #include <linux/nmi.h>
  40. #include <linux/fs.h>
  41. #include <linux/sched/rt.h>
  42. #include "trace.h"
  43. #include "trace_output.h"
  44. /*
  45. * On boot up, the ring buffer is set to the minimum size, so that
  46. * we do not waste memory on systems that are not using tracing.
  47. */
  48. int ring_buffer_expanded;
  49. /*
  50. * We need to change this state when a selftest is running.
  51. * A selftest will lurk into the ring-buffer to count the
  52. * entries inserted during the selftest although some concurrent
  53. * insertions into the ring-buffer such as trace_printk could occurred
  54. * at the same time, giving false positive or negative results.
  55. */
  56. static bool __read_mostly tracing_selftest_running;
  57. /*
  58. * If a tracer is running, we do not want to run SELFTEST.
  59. */
  60. bool __read_mostly tracing_selftest_disabled;
  61. /* For tracers that don't implement custom flags */
  62. static struct tracer_opt dummy_tracer_opt[] = {
  63. { }
  64. };
  65. static struct tracer_flags dummy_tracer_flags = {
  66. .val = 0,
  67. .opts = dummy_tracer_opt
  68. };
  69. static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * To prevent the comm cache from being overwritten when no
  75. * tracing is active, only save the comm when a trace event
  76. * occurred.
  77. */
  78. static DEFINE_PER_CPU(bool, trace_cmdline_save);
  79. /*
  80. * Kill all tracing for good (never come back).
  81. * It is initialized to 1 but will turn to zero if the initialization
  82. * of the tracer is successful. But that is the only place that sets
  83. * this back to zero.
  84. */
  85. static int tracing_disabled = 1;
  86. DEFINE_PER_CPU(int, ftrace_cpu_disabled);
  87. cpumask_var_t __read_mostly tracing_buffer_mask;
  88. /*
  89. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  90. *
  91. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  92. * is set, then ftrace_dump is called. This will output the contents
  93. * of the ftrace buffers to the console. This is very useful for
  94. * capturing traces that lead to crashes and outputing it to a
  95. * serial console.
  96. *
  97. * It is default off, but you can enable it with either specifying
  98. * "ftrace_dump_on_oops" in the kernel command line, or setting
  99. * /proc/sys/kernel/ftrace_dump_on_oops
  100. * Set 1 if you want to dump buffers of all CPUs
  101. * Set 2 if you want to dump the buffer of the CPU that triggered oops
  102. */
  103. enum ftrace_dump_mode ftrace_dump_on_oops;
  104. static int tracing_set_tracer(const char *buf);
  105. #define MAX_TRACER_SIZE 100
  106. static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
  107. static char *default_bootup_tracer;
  108. static int __init set_cmdline_ftrace(char *str)
  109. {
  110. strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
  111. default_bootup_tracer = bootup_tracer_buf;
  112. /* We are using ftrace early, expand it */
  113. ring_buffer_expanded = 1;
  114. return 1;
  115. }
  116. __setup("ftrace=", set_cmdline_ftrace);
  117. static int __init set_ftrace_dump_on_oops(char *str)
  118. {
  119. if (*str++ != '=' || !*str) {
  120. ftrace_dump_on_oops = DUMP_ALL;
  121. return 1;
  122. }
  123. if (!strcmp("orig_cpu", str)) {
  124. ftrace_dump_on_oops = DUMP_ORIG;
  125. return 1;
  126. }
  127. return 0;
  128. }
  129. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  130. static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
  131. static char *trace_boot_options __initdata;
  132. static int __init set_trace_boot_options(char *str)
  133. {
  134. strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
  135. trace_boot_options = trace_boot_options_buf;
  136. return 0;
  137. }
  138. __setup("trace_options=", set_trace_boot_options);
  139. unsigned long long ns2usecs(cycle_t nsec)
  140. {
  141. nsec += 500;
  142. do_div(nsec, 1000);
  143. return nsec;
  144. }
  145. /*
  146. * The global_trace is the descriptor that holds the tracing
  147. * buffers for the live tracing. For each CPU, it contains
  148. * a link list of pages that will store trace entries. The
  149. * page descriptor of the pages in the memory is used to hold
  150. * the link list by linking the lru item in the page descriptor
  151. * to each of the pages in the buffer per CPU.
  152. *
  153. * For each active CPU there is a data field that holds the
  154. * pages for the buffer for that CPU. Each CPU has the same number
  155. * of pages allocated for its buffer.
  156. */
  157. static struct trace_array global_trace;
  158. LIST_HEAD(ftrace_trace_arrays);
  159. int filter_current_check_discard(struct ring_buffer *buffer,
  160. struct ftrace_event_call *call, void *rec,
  161. struct ring_buffer_event *event)
  162. {
  163. return filter_check_discard(call, rec, buffer, event);
  164. }
  165. EXPORT_SYMBOL_GPL(filter_current_check_discard);
  166. cycle_t ftrace_now(int cpu)
  167. {
  168. u64 ts;
  169. /* Early boot up does not have a buffer yet */
  170. if (!global_trace.trace_buffer.buffer)
  171. return trace_clock_local();
  172. ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
  173. ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
  174. return ts;
  175. }
  176. int tracing_is_enabled(void)
  177. {
  178. return tracing_is_on();
  179. }
  180. /*
  181. * trace_buf_size is the size in bytes that is allocated
  182. * for a buffer. Note, the number of bytes is always rounded
  183. * to page size.
  184. *
  185. * This number is purposely set to a low number of 16384.
  186. * If the dump on oops happens, it will be much appreciated
  187. * to not have to wait for all that output. Anyway this can be
  188. * boot time and run time configurable.
  189. */
  190. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  191. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  192. /* trace_types holds a link list of available tracers. */
  193. static struct tracer *trace_types __read_mostly;
  194. /*
  195. * trace_types_lock is used to protect the trace_types list.
  196. */
  197. static DEFINE_MUTEX(trace_types_lock);
  198. /*
  199. * serialize the access of the ring buffer
  200. *
  201. * ring buffer serializes readers, but it is low level protection.
  202. * The validity of the events (which returns by ring_buffer_peek() ..etc)
  203. * are not protected by ring buffer.
  204. *
  205. * The content of events may become garbage if we allow other process consumes
  206. * these events concurrently:
  207. * A) the page of the consumed events may become a normal page
  208. * (not reader page) in ring buffer, and this page will be rewrited
  209. * by events producer.
  210. * B) The page of the consumed events may become a page for splice_read,
  211. * and this page will be returned to system.
  212. *
  213. * These primitives allow multi process access to different cpu ring buffer
  214. * concurrently.
  215. *
  216. * These primitives don't distinguish read-only and read-consume access.
  217. * Multi read-only access are also serialized.
  218. */
  219. #ifdef CONFIG_SMP
  220. static DECLARE_RWSEM(all_cpu_access_lock);
  221. static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
  222. static inline void trace_access_lock(int cpu)
  223. {
  224. if (cpu == RING_BUFFER_ALL_CPUS) {
  225. /* gain it for accessing the whole ring buffer. */
  226. down_write(&all_cpu_access_lock);
  227. } else {
  228. /* gain it for accessing a cpu ring buffer. */
  229. /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
  230. down_read(&all_cpu_access_lock);
  231. /* Secondly block other access to this @cpu ring buffer. */
  232. mutex_lock(&per_cpu(cpu_access_lock, cpu));
  233. }
  234. }
  235. static inline void trace_access_unlock(int cpu)
  236. {
  237. if (cpu == RING_BUFFER_ALL_CPUS) {
  238. up_write(&all_cpu_access_lock);
  239. } else {
  240. mutex_unlock(&per_cpu(cpu_access_lock, cpu));
  241. up_read(&all_cpu_access_lock);
  242. }
  243. }
  244. static inline void trace_access_lock_init(void)
  245. {
  246. int cpu;
  247. for_each_possible_cpu(cpu)
  248. mutex_init(&per_cpu(cpu_access_lock, cpu));
  249. }
  250. #else
  251. static DEFINE_MUTEX(access_lock);
  252. static inline void trace_access_lock(int cpu)
  253. {
  254. (void)cpu;
  255. mutex_lock(&access_lock);
  256. }
  257. static inline void trace_access_unlock(int cpu)
  258. {
  259. (void)cpu;
  260. mutex_unlock(&access_lock);
  261. }
  262. static inline void trace_access_lock_init(void)
  263. {
  264. }
  265. #endif
  266. /* trace_flags holds trace_options default values */
  267. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
  268. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
  269. TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
  270. TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
  271. /**
  272. * tracing_on - enable tracing buffers
  273. *
  274. * This function enables tracing buffers that may have been
  275. * disabled with tracing_off.
  276. */
  277. void tracing_on(void)
  278. {
  279. if (global_trace.trace_buffer.buffer)
  280. ring_buffer_record_on(global_trace.trace_buffer.buffer);
  281. /*
  282. * This flag is only looked at when buffers haven't been
  283. * allocated yet. We don't really care about the race
  284. * between setting this flag and actually turning
  285. * on the buffer.
  286. */
  287. global_trace.buffer_disabled = 0;
  288. }
  289. EXPORT_SYMBOL_GPL(tracing_on);
  290. #ifdef CONFIG_TRACER_SNAPSHOT
  291. /**
  292. * trace_snapshot - take a snapshot of the current buffer.
  293. *
  294. * This causes a swap between the snapshot buffer and the current live
  295. * tracing buffer. You can use this to take snapshots of the live
  296. * trace when some condition is triggered, but continue to trace.
  297. *
  298. * Note, make sure to allocate the snapshot with either
  299. * a tracing_snapshot_alloc(), or by doing it manually
  300. * with: echo 1 > /sys/kernel/debug/tracing/snapshot
  301. *
  302. * If the snapshot buffer is not allocated, it will stop tracing.
  303. * Basically making a permanent snapshot.
  304. */
  305. void tracing_snapshot(void)
  306. {
  307. struct trace_array *tr = &global_trace;
  308. struct tracer *tracer = tr->current_trace;
  309. unsigned long flags;
  310. if (!tr->allocated_snapshot) {
  311. trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
  312. trace_printk("*** stopping trace here! ***\n");
  313. tracing_off();
  314. return;
  315. }
  316. /* Note, snapshot can not be used when the tracer uses it */
  317. if (tracer->use_max_tr) {
  318. trace_printk("*** LATENCY TRACER ACTIVE ***\n");
  319. trace_printk("*** Can not use snapshot (sorry) ***\n");
  320. return;
  321. }
  322. local_irq_save(flags);
  323. update_max_tr(tr, current, smp_processor_id());
  324. local_irq_restore(flags);
  325. }
  326. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  327. struct trace_buffer *size_buf, int cpu_id);
  328. /**
  329. * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
  330. *
  331. * This is similar to trace_snapshot(), but it will allocate the
  332. * snapshot buffer if it isn't already allocated. Use this only
  333. * where it is safe to sleep, as the allocation may sleep.
  334. *
  335. * This causes a swap between the snapshot buffer and the current live
  336. * tracing buffer. You can use this to take snapshots of the live
  337. * trace when some condition is triggered, but continue to trace.
  338. */
  339. void tracing_snapshot_alloc(void)
  340. {
  341. struct trace_array *tr = &global_trace;
  342. int ret;
  343. if (!tr->allocated_snapshot) {
  344. /* allocate spare buffer */
  345. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  346. &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
  347. if (WARN_ON(ret < 0))
  348. return;
  349. tr->allocated_snapshot = true;
  350. }
  351. tracing_snapshot();
  352. }
  353. #else
  354. void tracing_snapshot(void)
  355. {
  356. WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
  357. }
  358. void tracing_snapshot_alloc(void)
  359. {
  360. /* Give warning */
  361. tracing_snapshot();
  362. }
  363. #endif /* CONFIG_TRACER_SNAPSHOT */
  364. /**
  365. * tracing_off - turn off tracing buffers
  366. *
  367. * This function stops the tracing buffers from recording data.
  368. * It does not disable any overhead the tracers themselves may
  369. * be causing. This function simply causes all recording to
  370. * the ring buffers to fail.
  371. */
  372. void tracing_off(void)
  373. {
  374. if (global_trace.trace_buffer.buffer)
  375. ring_buffer_record_off(global_trace.trace_buffer.buffer);
  376. /*
  377. * This flag is only looked at when buffers haven't been
  378. * allocated yet. We don't really care about the race
  379. * between setting this flag and actually turning
  380. * on the buffer.
  381. */
  382. global_trace.buffer_disabled = 1;
  383. }
  384. EXPORT_SYMBOL_GPL(tracing_off);
  385. /**
  386. * tracing_is_on - show state of ring buffers enabled
  387. */
  388. int tracing_is_on(void)
  389. {
  390. if (global_trace.trace_buffer.buffer)
  391. return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
  392. return !global_trace.buffer_disabled;
  393. }
  394. EXPORT_SYMBOL_GPL(tracing_is_on);
  395. static int __init set_buf_size(char *str)
  396. {
  397. unsigned long buf_size;
  398. if (!str)
  399. return 0;
  400. buf_size = memparse(str, &str);
  401. /* nr_entries can not be zero */
  402. if (buf_size == 0)
  403. return 0;
  404. trace_buf_size = buf_size;
  405. return 1;
  406. }
  407. __setup("trace_buf_size=", set_buf_size);
  408. static int __init set_tracing_thresh(char *str)
  409. {
  410. unsigned long threshold;
  411. int ret;
  412. if (!str)
  413. return 0;
  414. ret = kstrtoul(str, 0, &threshold);
  415. if (ret < 0)
  416. return 0;
  417. tracing_thresh = threshold * 1000;
  418. return 1;
  419. }
  420. __setup("tracing_thresh=", set_tracing_thresh);
  421. unsigned long nsecs_to_usecs(unsigned long nsecs)
  422. {
  423. return nsecs / 1000;
  424. }
  425. /* These must match the bit postions in trace_iterator_flags */
  426. static const char *trace_options[] = {
  427. "print-parent",
  428. "sym-offset",
  429. "sym-addr",
  430. "verbose",
  431. "raw",
  432. "hex",
  433. "bin",
  434. "block",
  435. "stacktrace",
  436. "trace_printk",
  437. "ftrace_preempt",
  438. "branch",
  439. "annotate",
  440. "userstacktrace",
  441. "sym-userobj",
  442. "printk-msg-only",
  443. "context-info",
  444. "latency-format",
  445. "sleep-time",
  446. "graph-time",
  447. "record-cmd",
  448. "overwrite",
  449. "disable_on_free",
  450. "irq-info",
  451. "markers",
  452. NULL
  453. };
  454. static struct {
  455. u64 (*func)(void);
  456. const char *name;
  457. int in_ns; /* is this clock in nanoseconds? */
  458. } trace_clocks[] = {
  459. { trace_clock_local, "local", 1 },
  460. { trace_clock_global, "global", 1 },
  461. { trace_clock_counter, "counter", 0 },
  462. ARCH_TRACE_CLOCKS
  463. };
  464. int trace_clock_id;
  465. /*
  466. * trace_parser_get_init - gets the buffer for trace parser
  467. */
  468. int trace_parser_get_init(struct trace_parser *parser, int size)
  469. {
  470. memset(parser, 0, sizeof(*parser));
  471. parser->buffer = kmalloc(size, GFP_KERNEL);
  472. if (!parser->buffer)
  473. return 1;
  474. parser->size = size;
  475. return 0;
  476. }
  477. /*
  478. * trace_parser_put - frees the buffer for trace parser
  479. */
  480. void trace_parser_put(struct trace_parser *parser)
  481. {
  482. kfree(parser->buffer);
  483. }
  484. /*
  485. * trace_get_user - reads the user input string separated by space
  486. * (matched by isspace(ch))
  487. *
  488. * For each string found the 'struct trace_parser' is updated,
  489. * and the function returns.
  490. *
  491. * Returns number of bytes read.
  492. *
  493. * See kernel/trace/trace.h for 'struct trace_parser' details.
  494. */
  495. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  496. size_t cnt, loff_t *ppos)
  497. {
  498. char ch;
  499. size_t read = 0;
  500. ssize_t ret;
  501. if (!*ppos)
  502. trace_parser_clear(parser);
  503. ret = get_user(ch, ubuf++);
  504. if (ret)
  505. goto out;
  506. read++;
  507. cnt--;
  508. /*
  509. * The parser is not finished with the last write,
  510. * continue reading the user input without skipping spaces.
  511. */
  512. if (!parser->cont) {
  513. /* skip white space */
  514. while (cnt && isspace(ch)) {
  515. ret = get_user(ch, ubuf++);
  516. if (ret)
  517. goto out;
  518. read++;
  519. cnt--;
  520. }
  521. /* only spaces were written */
  522. if (isspace(ch)) {
  523. *ppos += read;
  524. ret = read;
  525. goto out;
  526. }
  527. parser->idx = 0;
  528. }
  529. /* read the non-space input */
  530. while (cnt && !isspace(ch)) {
  531. if (parser->idx < parser->size - 1)
  532. parser->buffer[parser->idx++] = ch;
  533. else {
  534. ret = -EINVAL;
  535. goto out;
  536. }
  537. ret = get_user(ch, ubuf++);
  538. if (ret)
  539. goto out;
  540. read++;
  541. cnt--;
  542. }
  543. /* We either got finished input or we have to wait for another call. */
  544. if (isspace(ch)) {
  545. parser->buffer[parser->idx] = 0;
  546. parser->cont = false;
  547. } else {
  548. parser->cont = true;
  549. parser->buffer[parser->idx++] = ch;
  550. }
  551. *ppos += read;
  552. ret = read;
  553. out:
  554. return ret;
  555. }
  556. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  557. {
  558. int len;
  559. int ret;
  560. if (!cnt)
  561. return 0;
  562. if (s->len <= s->readpos)
  563. return -EBUSY;
  564. len = s->len - s->readpos;
  565. if (cnt > len)
  566. cnt = len;
  567. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  568. if (ret == cnt)
  569. return -EFAULT;
  570. cnt -= ret;
  571. s->readpos += cnt;
  572. return cnt;
  573. }
  574. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  575. {
  576. int len;
  577. if (s->len <= s->readpos)
  578. return -EBUSY;
  579. len = s->len - s->readpos;
  580. if (cnt > len)
  581. cnt = len;
  582. memcpy(buf, s->buffer + s->readpos, cnt);
  583. s->readpos += cnt;
  584. return cnt;
  585. }
  586. /*
  587. * ftrace_max_lock is used to protect the swapping of buffers
  588. * when taking a max snapshot. The buffers themselves are
  589. * protected by per_cpu spinlocks. But the action of the swap
  590. * needs its own lock.
  591. *
  592. * This is defined as a arch_spinlock_t in order to help
  593. * with performance when lockdep debugging is enabled.
  594. *
  595. * It is also used in other places outside the update_max_tr
  596. * so it needs to be defined outside of the
  597. * CONFIG_TRACER_MAX_TRACE.
  598. */
  599. static arch_spinlock_t ftrace_max_lock =
  600. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  601. unsigned long __read_mostly tracing_thresh;
  602. #ifdef CONFIG_TRACER_MAX_TRACE
  603. unsigned long __read_mostly tracing_max_latency;
  604. /*
  605. * Copy the new maximum trace into the separate maximum-trace
  606. * structure. (this way the maximum trace is permanently saved,
  607. * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
  608. */
  609. static void
  610. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  611. {
  612. struct trace_buffer *trace_buf = &tr->trace_buffer;
  613. struct trace_buffer *max_buf = &tr->max_buffer;
  614. struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
  615. struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
  616. max_buf->cpu = cpu;
  617. max_buf->time_start = data->preempt_timestamp;
  618. max_data->saved_latency = tracing_max_latency;
  619. max_data->critical_start = data->critical_start;
  620. max_data->critical_end = data->critical_end;
  621. memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  622. max_data->pid = tsk->pid;
  623. max_data->uid = task_uid(tsk);
  624. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  625. max_data->policy = tsk->policy;
  626. max_data->rt_priority = tsk->rt_priority;
  627. /* record this tasks comm */
  628. tracing_record_cmdline(tsk);
  629. }
  630. /**
  631. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  632. * @tr: tracer
  633. * @tsk: the task with the latency
  634. * @cpu: The cpu that initiated the trace.
  635. *
  636. * Flip the buffers between the @tr and the max_tr and record information
  637. * about which task was the cause of this latency.
  638. */
  639. void
  640. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  641. {
  642. struct ring_buffer *buf;
  643. if (tr->stop_count)
  644. return;
  645. WARN_ON_ONCE(!irqs_disabled());
  646. if (!tr->allocated_snapshot) {
  647. /* Only the nop tracer should hit this when disabling */
  648. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  649. return;
  650. }
  651. arch_spin_lock(&ftrace_max_lock);
  652. buf = tr->trace_buffer.buffer;
  653. tr->trace_buffer.buffer = tr->max_buffer.buffer;
  654. tr->max_buffer.buffer = buf;
  655. __update_max_tr(tr, tsk, cpu);
  656. arch_spin_unlock(&ftrace_max_lock);
  657. }
  658. /**
  659. * update_max_tr_single - only copy one trace over, and reset the rest
  660. * @tr - tracer
  661. * @tsk - task with the latency
  662. * @cpu - the cpu of the buffer to copy.
  663. *
  664. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  665. */
  666. void
  667. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  668. {
  669. int ret;
  670. if (tr->stop_count)
  671. return;
  672. WARN_ON_ONCE(!irqs_disabled());
  673. if (WARN_ON_ONCE(!tr->allocated_snapshot))
  674. return;
  675. arch_spin_lock(&ftrace_max_lock);
  676. ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
  677. if (ret == -EBUSY) {
  678. /*
  679. * We failed to swap the buffer due to a commit taking
  680. * place on this CPU. We fail to record, but we reset
  681. * the max trace buffer (no one writes directly to it)
  682. * and flag that it failed.
  683. */
  684. trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
  685. "Failed to swap buffers due to commit in progress\n");
  686. }
  687. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  688. __update_max_tr(tr, tsk, cpu);
  689. arch_spin_unlock(&ftrace_max_lock);
  690. }
  691. #endif /* CONFIG_TRACER_MAX_TRACE */
  692. static void default_wait_pipe(struct trace_iterator *iter)
  693. {
  694. /* Iterators are static, they should be filled or empty */
  695. if (trace_buffer_iter(iter, iter->cpu_file))
  696. return;
  697. ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
  698. }
  699. #ifdef CONFIG_FTRACE_STARTUP_TEST
  700. static int run_tracer_selftest(struct tracer *type)
  701. {
  702. struct trace_array *tr = &global_trace;
  703. struct tracer *saved_tracer = tr->current_trace;
  704. int ret;
  705. if (!type->selftest || tracing_selftest_disabled)
  706. return 0;
  707. /*
  708. * Run a selftest on this tracer.
  709. * Here we reset the trace buffer, and set the current
  710. * tracer to be this tracer. The tracer can then run some
  711. * internal tracing to verify that everything is in order.
  712. * If we fail, we do not register this tracer.
  713. */
  714. tracing_reset_online_cpus(&tr->trace_buffer);
  715. tr->current_trace = type;
  716. #ifdef CONFIG_TRACER_MAX_TRACE
  717. if (type->use_max_tr) {
  718. /* If we expanded the buffers, make sure the max is expanded too */
  719. if (ring_buffer_expanded)
  720. ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
  721. RING_BUFFER_ALL_CPUS);
  722. tr->allocated_snapshot = true;
  723. }
  724. #endif
  725. /* the test is responsible for initializing and enabling */
  726. pr_info("Testing tracer %s: ", type->name);
  727. ret = type->selftest(type, tr);
  728. /* the test is responsible for resetting too */
  729. tr->current_trace = saved_tracer;
  730. if (ret) {
  731. printk(KERN_CONT "FAILED!\n");
  732. /* Add the warning after printing 'FAILED' */
  733. WARN_ON(1);
  734. return -1;
  735. }
  736. /* Only reset on passing, to avoid touching corrupted buffers */
  737. tracing_reset_online_cpus(&tr->trace_buffer);
  738. #ifdef CONFIG_TRACER_MAX_TRACE
  739. if (type->use_max_tr) {
  740. tr->allocated_snapshot = false;
  741. /* Shrink the max buffer again */
  742. if (ring_buffer_expanded)
  743. ring_buffer_resize(tr->max_buffer.buffer, 1,
  744. RING_BUFFER_ALL_CPUS);
  745. }
  746. #endif
  747. printk(KERN_CONT "PASSED\n");
  748. return 0;
  749. }
  750. #else
  751. static inline int run_tracer_selftest(struct tracer *type)
  752. {
  753. return 0;
  754. }
  755. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  756. /**
  757. * register_tracer - register a tracer with the ftrace system.
  758. * @type - the plugin for the tracer
  759. *
  760. * Register a new plugin tracer.
  761. */
  762. int register_tracer(struct tracer *type)
  763. {
  764. struct tracer *t;
  765. int ret = 0;
  766. if (!type->name) {
  767. pr_info("Tracer must have a name\n");
  768. return -1;
  769. }
  770. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  771. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  772. return -1;
  773. }
  774. mutex_lock(&trace_types_lock);
  775. tracing_selftest_running = true;
  776. for (t = trace_types; t; t = t->next) {
  777. if (strcmp(type->name, t->name) == 0) {
  778. /* already found */
  779. pr_info("Tracer %s already registered\n",
  780. type->name);
  781. ret = -1;
  782. goto out;
  783. }
  784. }
  785. if (!type->set_flag)
  786. type->set_flag = &dummy_set_flag;
  787. if (!type->flags)
  788. type->flags = &dummy_tracer_flags;
  789. else
  790. if (!type->flags->opts)
  791. type->flags->opts = dummy_tracer_opt;
  792. if (!type->wait_pipe)
  793. type->wait_pipe = default_wait_pipe;
  794. ret = run_tracer_selftest(type);
  795. if (ret < 0)
  796. goto out;
  797. type->next = trace_types;
  798. trace_types = type;
  799. out:
  800. tracing_selftest_running = false;
  801. mutex_unlock(&trace_types_lock);
  802. if (ret || !default_bootup_tracer)
  803. goto out_unlock;
  804. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  805. goto out_unlock;
  806. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  807. /* Do we want this tracer to start on bootup? */
  808. tracing_set_tracer(type->name);
  809. default_bootup_tracer = NULL;
  810. /* disable other selftests, since this will break it. */
  811. tracing_selftest_disabled = 1;
  812. #ifdef CONFIG_FTRACE_STARTUP_TEST
  813. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  814. type->name);
  815. #endif
  816. out_unlock:
  817. return ret;
  818. }
  819. void tracing_reset(struct trace_buffer *buf, int cpu)
  820. {
  821. struct ring_buffer *buffer = buf->buffer;
  822. if (!buffer)
  823. return;
  824. ring_buffer_record_disable(buffer);
  825. /* Make sure all commits have finished */
  826. synchronize_sched();
  827. ring_buffer_reset_cpu(buffer, cpu);
  828. ring_buffer_record_enable(buffer);
  829. }
  830. void tracing_reset_online_cpus(struct trace_buffer *buf)
  831. {
  832. struct ring_buffer *buffer = buf->buffer;
  833. int cpu;
  834. if (!buffer)
  835. return;
  836. ring_buffer_record_disable(buffer);
  837. /* Make sure all commits have finished */
  838. synchronize_sched();
  839. buf->time_start = ftrace_now(buf->cpu);
  840. for_each_online_cpu(cpu)
  841. ring_buffer_reset_cpu(buffer, cpu);
  842. ring_buffer_record_enable(buffer);
  843. }
  844. void tracing_reset_current(int cpu)
  845. {
  846. tracing_reset(&global_trace.trace_buffer, cpu);
  847. }
  848. void tracing_reset_all_online_cpus(void)
  849. {
  850. struct trace_array *tr;
  851. mutex_lock(&trace_types_lock);
  852. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  853. tracing_reset_online_cpus(&tr->trace_buffer);
  854. #ifdef CONFIG_TRACER_MAX_TRACE
  855. tracing_reset_online_cpus(&tr->max_buffer);
  856. #endif
  857. }
  858. mutex_unlock(&trace_types_lock);
  859. }
  860. #define SAVED_CMDLINES 128
  861. #define NO_CMDLINE_MAP UINT_MAX
  862. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  863. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  864. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  865. static int cmdline_idx;
  866. static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  867. /* temporary disable recording */
  868. static atomic_t trace_record_cmdline_disabled __read_mostly;
  869. static void trace_init_cmdlines(void)
  870. {
  871. memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
  872. memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
  873. cmdline_idx = 0;
  874. }
  875. int is_tracing_stopped(void)
  876. {
  877. return global_trace.stop_count;
  878. }
  879. /**
  880. * ftrace_off_permanent - disable all ftrace code permanently
  881. *
  882. * This should only be called when a serious anomally has
  883. * been detected. This will turn off the function tracing,
  884. * ring buffers, and other tracing utilites. It takes no
  885. * locks and can be called from any context.
  886. */
  887. void ftrace_off_permanent(void)
  888. {
  889. tracing_disabled = 1;
  890. ftrace_stop();
  891. tracing_off_permanent();
  892. }
  893. /**
  894. * tracing_start - quick start of the tracer
  895. *
  896. * If tracing is enabled but was stopped by tracing_stop,
  897. * this will start the tracer back up.
  898. */
  899. void tracing_start(void)
  900. {
  901. struct ring_buffer *buffer;
  902. unsigned long flags;
  903. if (tracing_disabled)
  904. return;
  905. raw_spin_lock_irqsave(&global_trace.start_lock, flags);
  906. if (--global_trace.stop_count) {
  907. if (global_trace.stop_count < 0) {
  908. /* Someone screwed up their debugging */
  909. WARN_ON_ONCE(1);
  910. global_trace.stop_count = 0;
  911. }
  912. goto out;
  913. }
  914. /* Prevent the buffers from switching */
  915. arch_spin_lock(&ftrace_max_lock);
  916. buffer = global_trace.trace_buffer.buffer;
  917. if (buffer)
  918. ring_buffer_record_enable(buffer);
  919. #ifdef CONFIG_TRACER_MAX_TRACE
  920. buffer = global_trace.max_buffer.buffer;
  921. if (buffer)
  922. ring_buffer_record_enable(buffer);
  923. #endif
  924. arch_spin_unlock(&ftrace_max_lock);
  925. ftrace_start();
  926. out:
  927. raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
  928. }
  929. static void tracing_start_tr(struct trace_array *tr)
  930. {
  931. struct ring_buffer *buffer;
  932. unsigned long flags;
  933. if (tracing_disabled)
  934. return;
  935. /* If global, we need to also start the max tracer */
  936. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  937. return tracing_start();
  938. raw_spin_lock_irqsave(&tr->start_lock, flags);
  939. if (--tr->stop_count) {
  940. if (tr->stop_count < 0) {
  941. /* Someone screwed up their debugging */
  942. WARN_ON_ONCE(1);
  943. tr->stop_count = 0;
  944. }
  945. goto out;
  946. }
  947. buffer = tr->trace_buffer.buffer;
  948. if (buffer)
  949. ring_buffer_record_enable(buffer);
  950. out:
  951. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  952. }
  953. /**
  954. * tracing_stop - quick stop of the tracer
  955. *
  956. * Light weight way to stop tracing. Use in conjunction with
  957. * tracing_start.
  958. */
  959. void tracing_stop(void)
  960. {
  961. struct ring_buffer *buffer;
  962. unsigned long flags;
  963. ftrace_stop();
  964. raw_spin_lock_irqsave(&global_trace.start_lock, flags);
  965. if (global_trace.stop_count++)
  966. goto out;
  967. /* Prevent the buffers from switching */
  968. arch_spin_lock(&ftrace_max_lock);
  969. buffer = global_trace.trace_buffer.buffer;
  970. if (buffer)
  971. ring_buffer_record_disable(buffer);
  972. #ifdef CONFIG_TRACER_MAX_TRACE
  973. buffer = global_trace.max_buffer.buffer;
  974. if (buffer)
  975. ring_buffer_record_disable(buffer);
  976. #endif
  977. arch_spin_unlock(&ftrace_max_lock);
  978. out:
  979. raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
  980. }
  981. static void tracing_stop_tr(struct trace_array *tr)
  982. {
  983. struct ring_buffer *buffer;
  984. unsigned long flags;
  985. /* If global, we need to also stop the max tracer */
  986. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  987. return tracing_stop();
  988. raw_spin_lock_irqsave(&tr->start_lock, flags);
  989. if (tr->stop_count++)
  990. goto out;
  991. buffer = tr->trace_buffer.buffer;
  992. if (buffer)
  993. ring_buffer_record_disable(buffer);
  994. out:
  995. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  996. }
  997. void trace_stop_cmdline_recording(void);
  998. static void trace_save_cmdline(struct task_struct *tsk)
  999. {
  1000. unsigned pid, idx;
  1001. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  1002. return;
  1003. /*
  1004. * It's not the end of the world if we don't get
  1005. * the lock, but we also don't want to spin
  1006. * nor do we want to disable interrupts,
  1007. * so if we miss here, then better luck next time.
  1008. */
  1009. if (!arch_spin_trylock(&trace_cmdline_lock))
  1010. return;
  1011. idx = map_pid_to_cmdline[tsk->pid];
  1012. if (idx == NO_CMDLINE_MAP) {
  1013. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  1014. /*
  1015. * Check whether the cmdline buffer at idx has a pid
  1016. * mapped. We are going to overwrite that entry so we
  1017. * need to clear the map_pid_to_cmdline. Otherwise we
  1018. * would read the new comm for the old pid.
  1019. */
  1020. pid = map_cmdline_to_pid[idx];
  1021. if (pid != NO_CMDLINE_MAP)
  1022. map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
  1023. map_cmdline_to_pid[idx] = tsk->pid;
  1024. map_pid_to_cmdline[tsk->pid] = idx;
  1025. cmdline_idx = idx;
  1026. }
  1027. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  1028. arch_spin_unlock(&trace_cmdline_lock);
  1029. }
  1030. void trace_find_cmdline(int pid, char comm[])
  1031. {
  1032. unsigned map;
  1033. if (!pid) {
  1034. strcpy(comm, "<idle>");
  1035. return;
  1036. }
  1037. if (WARN_ON_ONCE(pid < 0)) {
  1038. strcpy(comm, "<XXX>");
  1039. return;
  1040. }
  1041. if (pid > PID_MAX_DEFAULT) {
  1042. strcpy(comm, "<...>");
  1043. return;
  1044. }
  1045. preempt_disable();
  1046. arch_spin_lock(&trace_cmdline_lock);
  1047. map = map_pid_to_cmdline[pid];
  1048. if (map != NO_CMDLINE_MAP)
  1049. strcpy(comm, saved_cmdlines[map]);
  1050. else
  1051. strcpy(comm, "<...>");
  1052. arch_spin_unlock(&trace_cmdline_lock);
  1053. preempt_enable();
  1054. }
  1055. void tracing_record_cmdline(struct task_struct *tsk)
  1056. {
  1057. if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
  1058. return;
  1059. if (!__this_cpu_read(trace_cmdline_save))
  1060. return;
  1061. __this_cpu_write(trace_cmdline_save, false);
  1062. trace_save_cmdline(tsk);
  1063. }
  1064. void
  1065. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  1066. int pc)
  1067. {
  1068. struct task_struct *tsk = current;
  1069. entry->preempt_count = pc & 0xff;
  1070. entry->pid = (tsk) ? tsk->pid : 0;
  1071. entry->flags =
  1072. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  1073. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  1074. #else
  1075. TRACE_FLAG_IRQS_NOSUPPORT |
  1076. #endif
  1077. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  1078. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  1079. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  1080. }
  1081. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  1082. struct ring_buffer_event *
  1083. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  1084. int type,
  1085. unsigned long len,
  1086. unsigned long flags, int pc)
  1087. {
  1088. struct ring_buffer_event *event;
  1089. event = ring_buffer_lock_reserve(buffer, len);
  1090. if (event != NULL) {
  1091. struct trace_entry *ent = ring_buffer_event_data(event);
  1092. tracing_generic_entry_update(ent, flags, pc);
  1093. ent->type = type;
  1094. }
  1095. return event;
  1096. }
  1097. void
  1098. __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
  1099. {
  1100. __this_cpu_write(trace_cmdline_save, true);
  1101. ring_buffer_unlock_commit(buffer, event);
  1102. }
  1103. static inline void
  1104. __trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1105. struct ring_buffer_event *event,
  1106. unsigned long flags, int pc)
  1107. {
  1108. __buffer_unlock_commit(buffer, event);
  1109. ftrace_trace_stack(buffer, flags, 6, pc);
  1110. ftrace_trace_userstack(buffer, flags, pc);
  1111. }
  1112. void trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1113. struct ring_buffer_event *event,
  1114. unsigned long flags, int pc)
  1115. {
  1116. __trace_buffer_unlock_commit(buffer, event, flags, pc);
  1117. }
  1118. EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
  1119. struct ring_buffer_event *
  1120. trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
  1121. struct ftrace_event_file *ftrace_file,
  1122. int type, unsigned long len,
  1123. unsigned long flags, int pc)
  1124. {
  1125. *current_rb = ftrace_file->tr->trace_buffer.buffer;
  1126. return trace_buffer_lock_reserve(*current_rb,
  1127. type, len, flags, pc);
  1128. }
  1129. EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
  1130. struct ring_buffer_event *
  1131. trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
  1132. int type, unsigned long len,
  1133. unsigned long flags, int pc)
  1134. {
  1135. *current_rb = global_trace.trace_buffer.buffer;
  1136. return trace_buffer_lock_reserve(*current_rb,
  1137. type, len, flags, pc);
  1138. }
  1139. EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
  1140. void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
  1141. struct ring_buffer_event *event,
  1142. unsigned long flags, int pc)
  1143. {
  1144. __trace_buffer_unlock_commit(buffer, event, flags, pc);
  1145. }
  1146. EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
  1147. void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
  1148. struct ring_buffer_event *event,
  1149. unsigned long flags, int pc,
  1150. struct pt_regs *regs)
  1151. {
  1152. __buffer_unlock_commit(buffer, event);
  1153. ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
  1154. ftrace_trace_userstack(buffer, flags, pc);
  1155. }
  1156. EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
  1157. void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
  1158. struct ring_buffer_event *event)
  1159. {
  1160. ring_buffer_discard_commit(buffer, event);
  1161. }
  1162. EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
  1163. void
  1164. trace_function(struct trace_array *tr,
  1165. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1166. int pc)
  1167. {
  1168. struct ftrace_event_call *call = &event_function;
  1169. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  1170. struct ring_buffer_event *event;
  1171. struct ftrace_entry *entry;
  1172. /* If we are reading the ring buffer, don't trace */
  1173. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  1174. return;
  1175. event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
  1176. flags, pc);
  1177. if (!event)
  1178. return;
  1179. entry = ring_buffer_event_data(event);
  1180. entry->ip = ip;
  1181. entry->parent_ip = parent_ip;
  1182. if (!filter_check_discard(call, entry, buffer, event))
  1183. __buffer_unlock_commit(buffer, event);
  1184. }
  1185. void
  1186. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  1187. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1188. int pc)
  1189. {
  1190. if (likely(!atomic_read(&data->disabled)))
  1191. trace_function(tr, ip, parent_ip, flags, pc);
  1192. }
  1193. #ifdef CONFIG_STACKTRACE
  1194. #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
  1195. struct ftrace_stack {
  1196. unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
  1197. };
  1198. static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
  1199. static DEFINE_PER_CPU(int, ftrace_stack_reserve);
  1200. static void __ftrace_trace_stack(struct ring_buffer *buffer,
  1201. unsigned long flags,
  1202. int skip, int pc, struct pt_regs *regs)
  1203. {
  1204. struct ftrace_event_call *call = &event_kernel_stack;
  1205. struct ring_buffer_event *event;
  1206. struct stack_entry *entry;
  1207. struct stack_trace trace;
  1208. int use_stack;
  1209. int size = FTRACE_STACK_ENTRIES;
  1210. trace.nr_entries = 0;
  1211. trace.skip = skip;
  1212. /*
  1213. * Since events can happen in NMIs there's no safe way to
  1214. * use the per cpu ftrace_stacks. We reserve it and if an interrupt
  1215. * or NMI comes in, it will just have to use the default
  1216. * FTRACE_STACK_SIZE.
  1217. */
  1218. preempt_disable_notrace();
  1219. use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
  1220. /*
  1221. * We don't need any atomic variables, just a barrier.
  1222. * If an interrupt comes in, we don't care, because it would
  1223. * have exited and put the counter back to what we want.
  1224. * We just need a barrier to keep gcc from moving things
  1225. * around.
  1226. */
  1227. barrier();
  1228. if (use_stack == 1) {
  1229. trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
  1230. trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
  1231. if (regs)
  1232. save_stack_trace_regs(regs, &trace);
  1233. else
  1234. save_stack_trace(&trace);
  1235. if (trace.nr_entries > size)
  1236. size = trace.nr_entries;
  1237. } else
  1238. /* From now on, use_stack is a boolean */
  1239. use_stack = 0;
  1240. size *= sizeof(unsigned long);
  1241. event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
  1242. sizeof(*entry) + size, flags, pc);
  1243. if (!event)
  1244. goto out;
  1245. entry = ring_buffer_event_data(event);
  1246. memset(&entry->caller, 0, size);
  1247. if (use_stack)
  1248. memcpy(&entry->caller, trace.entries,
  1249. trace.nr_entries * sizeof(unsigned long));
  1250. else {
  1251. trace.max_entries = FTRACE_STACK_ENTRIES;
  1252. trace.entries = entry->caller;
  1253. if (regs)
  1254. save_stack_trace_regs(regs, &trace);
  1255. else
  1256. save_stack_trace(&trace);
  1257. }
  1258. entry->size = trace.nr_entries;
  1259. if (!filter_check_discard(call, entry, buffer, event))
  1260. __buffer_unlock_commit(buffer, event);
  1261. out:
  1262. /* Again, don't let gcc optimize things here */
  1263. barrier();
  1264. __this_cpu_dec(ftrace_stack_reserve);
  1265. preempt_enable_notrace();
  1266. }
  1267. void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
  1268. int skip, int pc, struct pt_regs *regs)
  1269. {
  1270. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1271. return;
  1272. __ftrace_trace_stack(buffer, flags, skip, pc, regs);
  1273. }
  1274. void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
  1275. int skip, int pc)
  1276. {
  1277. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1278. return;
  1279. __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
  1280. }
  1281. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  1282. int pc)
  1283. {
  1284. __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
  1285. }
  1286. /**
  1287. * trace_dump_stack - record a stack back trace in the trace buffer
  1288. */
  1289. void trace_dump_stack(void)
  1290. {
  1291. unsigned long flags;
  1292. if (tracing_disabled || tracing_selftest_running)
  1293. return;
  1294. local_save_flags(flags);
  1295. /* skipping 3 traces, seems to get us at the caller of this function */
  1296. __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
  1297. preempt_count(), NULL);
  1298. }
  1299. static DEFINE_PER_CPU(int, user_stack_count);
  1300. void
  1301. ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
  1302. {
  1303. struct ftrace_event_call *call = &event_user_stack;
  1304. struct ring_buffer_event *event;
  1305. struct userstack_entry *entry;
  1306. struct stack_trace trace;
  1307. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  1308. return;
  1309. /*
  1310. * NMIs can not handle page faults, even with fix ups.
  1311. * The save user stack can (and often does) fault.
  1312. */
  1313. if (unlikely(in_nmi()))
  1314. return;
  1315. /*
  1316. * prevent recursion, since the user stack tracing may
  1317. * trigger other kernel events.
  1318. */
  1319. preempt_disable();
  1320. if (__this_cpu_read(user_stack_count))
  1321. goto out;
  1322. __this_cpu_inc(user_stack_count);
  1323. event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  1324. sizeof(*entry), flags, pc);
  1325. if (!event)
  1326. goto out_drop_count;
  1327. entry = ring_buffer_event_data(event);
  1328. entry->tgid = current->tgid;
  1329. memset(&entry->caller, 0, sizeof(entry->caller));
  1330. trace.nr_entries = 0;
  1331. trace.max_entries = FTRACE_STACK_ENTRIES;
  1332. trace.skip = 0;
  1333. trace.entries = entry->caller;
  1334. save_stack_trace_user(&trace);
  1335. if (!filter_check_discard(call, entry, buffer, event))
  1336. __buffer_unlock_commit(buffer, event);
  1337. out_drop_count:
  1338. __this_cpu_dec(user_stack_count);
  1339. out:
  1340. preempt_enable();
  1341. }
  1342. #ifdef UNUSED
  1343. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  1344. {
  1345. ftrace_trace_userstack(tr, flags, preempt_count());
  1346. }
  1347. #endif /* UNUSED */
  1348. #endif /* CONFIG_STACKTRACE */
  1349. /* created for use with alloc_percpu */
  1350. struct trace_buffer_struct {
  1351. char buffer[TRACE_BUF_SIZE];
  1352. };
  1353. static struct trace_buffer_struct *trace_percpu_buffer;
  1354. static struct trace_buffer_struct *trace_percpu_sirq_buffer;
  1355. static struct trace_buffer_struct *trace_percpu_irq_buffer;
  1356. static struct trace_buffer_struct *trace_percpu_nmi_buffer;
  1357. /*
  1358. * The buffer used is dependent on the context. There is a per cpu
  1359. * buffer for normal context, softirq contex, hard irq context and
  1360. * for NMI context. Thise allows for lockless recording.
  1361. *
  1362. * Note, if the buffers failed to be allocated, then this returns NULL
  1363. */
  1364. static char *get_trace_buf(void)
  1365. {
  1366. struct trace_buffer_struct *percpu_buffer;
  1367. /*
  1368. * If we have allocated per cpu buffers, then we do not
  1369. * need to do any locking.
  1370. */
  1371. if (in_nmi())
  1372. percpu_buffer = trace_percpu_nmi_buffer;
  1373. else if (in_irq())
  1374. percpu_buffer = trace_percpu_irq_buffer;
  1375. else if (in_softirq())
  1376. percpu_buffer = trace_percpu_sirq_buffer;
  1377. else
  1378. percpu_buffer = trace_percpu_buffer;
  1379. if (!percpu_buffer)
  1380. return NULL;
  1381. return this_cpu_ptr(&percpu_buffer->buffer[0]);
  1382. }
  1383. static int alloc_percpu_trace_buffer(void)
  1384. {
  1385. struct trace_buffer_struct *buffers;
  1386. struct trace_buffer_struct *sirq_buffers;
  1387. struct trace_buffer_struct *irq_buffers;
  1388. struct trace_buffer_struct *nmi_buffers;
  1389. buffers = alloc_percpu(struct trace_buffer_struct);
  1390. if (!buffers)
  1391. goto err_warn;
  1392. sirq_buffers = alloc_percpu(struct trace_buffer_struct);
  1393. if (!sirq_buffers)
  1394. goto err_sirq;
  1395. irq_buffers = alloc_percpu(struct trace_buffer_struct);
  1396. if (!irq_buffers)
  1397. goto err_irq;
  1398. nmi_buffers = alloc_percpu(struct trace_buffer_struct);
  1399. if (!nmi_buffers)
  1400. goto err_nmi;
  1401. trace_percpu_buffer = buffers;
  1402. trace_percpu_sirq_buffer = sirq_buffers;
  1403. trace_percpu_irq_buffer = irq_buffers;
  1404. trace_percpu_nmi_buffer = nmi_buffers;
  1405. return 0;
  1406. err_nmi:
  1407. free_percpu(irq_buffers);
  1408. err_irq:
  1409. free_percpu(sirq_buffers);
  1410. err_sirq:
  1411. free_percpu(buffers);
  1412. err_warn:
  1413. WARN(1, "Could not allocate percpu trace_printk buffer");
  1414. return -ENOMEM;
  1415. }
  1416. static int buffers_allocated;
  1417. void trace_printk_init_buffers(void)
  1418. {
  1419. if (buffers_allocated)
  1420. return;
  1421. if (alloc_percpu_trace_buffer())
  1422. return;
  1423. pr_info("ftrace: Allocated trace_printk buffers\n");
  1424. /* Expand the buffers to set size */
  1425. tracing_update_buffers();
  1426. buffers_allocated = 1;
  1427. /*
  1428. * trace_printk_init_buffers() can be called by modules.
  1429. * If that happens, then we need to start cmdline recording
  1430. * directly here. If the global_trace.buffer is already
  1431. * allocated here, then this was called by module code.
  1432. */
  1433. if (global_trace.trace_buffer.buffer)
  1434. tracing_start_cmdline_record();
  1435. }
  1436. void trace_printk_start_comm(void)
  1437. {
  1438. /* Start tracing comms if trace printk is set */
  1439. if (!buffers_allocated)
  1440. return;
  1441. tracing_start_cmdline_record();
  1442. }
  1443. static void trace_printk_start_stop_comm(int enabled)
  1444. {
  1445. if (!buffers_allocated)
  1446. return;
  1447. if (enabled)
  1448. tracing_start_cmdline_record();
  1449. else
  1450. tracing_stop_cmdline_record();
  1451. }
  1452. /**
  1453. * trace_vbprintk - write binary msg to tracing buffer
  1454. *
  1455. */
  1456. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  1457. {
  1458. struct ftrace_event_call *call = &event_bprint;
  1459. struct ring_buffer_event *event;
  1460. struct ring_buffer *buffer;
  1461. struct trace_array *tr = &global_trace;
  1462. struct bprint_entry *entry;
  1463. unsigned long flags;
  1464. char *tbuffer;
  1465. int len = 0, size, pc;
  1466. if (unlikely(tracing_selftest_running || tracing_disabled))
  1467. return 0;
  1468. /* Don't pollute graph traces with trace_vprintk internals */
  1469. pause_graph_tracing();
  1470. pc = preempt_count();
  1471. preempt_disable_notrace();
  1472. tbuffer = get_trace_buf();
  1473. if (!tbuffer) {
  1474. len = 0;
  1475. goto out;
  1476. }
  1477. len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
  1478. if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
  1479. goto out;
  1480. local_save_flags(flags);
  1481. size = sizeof(*entry) + sizeof(u32) * len;
  1482. buffer = tr->trace_buffer.buffer;
  1483. event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  1484. flags, pc);
  1485. if (!event)
  1486. goto out;
  1487. entry = ring_buffer_event_data(event);
  1488. entry->ip = ip;
  1489. entry->fmt = fmt;
  1490. memcpy(entry->buf, tbuffer, sizeof(u32) * len);
  1491. if (!filter_check_discard(call, entry, buffer, event)) {
  1492. __buffer_unlock_commit(buffer, event);
  1493. ftrace_trace_stack(buffer, flags, 6, pc);
  1494. }
  1495. out:
  1496. preempt_enable_notrace();
  1497. unpause_graph_tracing();
  1498. return len;
  1499. }
  1500. EXPORT_SYMBOL_GPL(trace_vbprintk);
  1501. static int
  1502. __trace_array_vprintk(struct ring_buffer *buffer,
  1503. unsigned long ip, const char *fmt, va_list args)
  1504. {
  1505. struct ftrace_event_call *call = &event_print;
  1506. struct ring_buffer_event *event;
  1507. int len = 0, size, pc;
  1508. struct print_entry *entry;
  1509. unsigned long flags;
  1510. char *tbuffer;
  1511. if (tracing_disabled || tracing_selftest_running)
  1512. return 0;
  1513. /* Don't pollute graph traces with trace_vprintk internals */
  1514. pause_graph_tracing();
  1515. pc = preempt_count();
  1516. preempt_disable_notrace();
  1517. tbuffer = get_trace_buf();
  1518. if (!tbuffer) {
  1519. len = 0;
  1520. goto out;
  1521. }
  1522. len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
  1523. if (len > TRACE_BUF_SIZE)
  1524. goto out;
  1525. local_save_flags(flags);
  1526. size = sizeof(*entry) + len + 1;
  1527. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  1528. flags, pc);
  1529. if (!event)
  1530. goto out;
  1531. entry = ring_buffer_event_data(event);
  1532. entry->ip = ip;
  1533. memcpy(&entry->buf, tbuffer, len);
  1534. entry->buf[len] = '\0';
  1535. if (!filter_check_discard(call, entry, buffer, event)) {
  1536. __buffer_unlock_commit(buffer, event);
  1537. ftrace_trace_stack(buffer, flags, 6, pc);
  1538. }
  1539. out:
  1540. preempt_enable_notrace();
  1541. unpause_graph_tracing();
  1542. return len;
  1543. }
  1544. int trace_array_vprintk(struct trace_array *tr,
  1545. unsigned long ip, const char *fmt, va_list args)
  1546. {
  1547. return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
  1548. }
  1549. int trace_array_printk(struct trace_array *tr,
  1550. unsigned long ip, const char *fmt, ...)
  1551. {
  1552. int ret;
  1553. va_list ap;
  1554. if (!(trace_flags & TRACE_ITER_PRINTK))
  1555. return 0;
  1556. va_start(ap, fmt);
  1557. ret = trace_array_vprintk(tr, ip, fmt, ap);
  1558. va_end(ap);
  1559. return ret;
  1560. }
  1561. int trace_array_printk_buf(struct ring_buffer *buffer,
  1562. unsigned long ip, const char *fmt, ...)
  1563. {
  1564. int ret;
  1565. va_list ap;
  1566. if (!(trace_flags & TRACE_ITER_PRINTK))
  1567. return 0;
  1568. va_start(ap, fmt);
  1569. ret = __trace_array_vprintk(buffer, ip, fmt, ap);
  1570. va_end(ap);
  1571. return ret;
  1572. }
  1573. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  1574. {
  1575. return trace_array_vprintk(&global_trace, ip, fmt, args);
  1576. }
  1577. EXPORT_SYMBOL_GPL(trace_vprintk);
  1578. static void trace_iterator_increment(struct trace_iterator *iter)
  1579. {
  1580. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
  1581. iter->idx++;
  1582. if (buf_iter)
  1583. ring_buffer_read(buf_iter, NULL);
  1584. }
  1585. static struct trace_entry *
  1586. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  1587. unsigned long *lost_events)
  1588. {
  1589. struct ring_buffer_event *event;
  1590. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
  1591. if (buf_iter)
  1592. event = ring_buffer_iter_peek(buf_iter, ts);
  1593. else
  1594. event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
  1595. lost_events);
  1596. if (event) {
  1597. iter->ent_size = ring_buffer_event_length(event);
  1598. return ring_buffer_event_data(event);
  1599. }
  1600. iter->ent_size = 0;
  1601. return NULL;
  1602. }
  1603. static struct trace_entry *
  1604. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  1605. unsigned long *missing_events, u64 *ent_ts)
  1606. {
  1607. struct ring_buffer *buffer = iter->trace_buffer->buffer;
  1608. struct trace_entry *ent, *next = NULL;
  1609. unsigned long lost_events = 0, next_lost = 0;
  1610. int cpu_file = iter->cpu_file;
  1611. u64 next_ts = 0, ts;
  1612. int next_cpu = -1;
  1613. int next_size = 0;
  1614. int cpu;
  1615. /*
  1616. * If we are in a per_cpu trace file, don't bother by iterating over
  1617. * all cpu and peek directly.
  1618. */
  1619. if (cpu_file > RING_BUFFER_ALL_CPUS) {
  1620. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1621. return NULL;
  1622. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  1623. if (ent_cpu)
  1624. *ent_cpu = cpu_file;
  1625. return ent;
  1626. }
  1627. for_each_tracing_cpu(cpu) {
  1628. if (ring_buffer_empty_cpu(buffer, cpu))
  1629. continue;
  1630. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  1631. /*
  1632. * Pick the entry with the smallest timestamp:
  1633. */
  1634. if (ent && (!next || ts < next_ts)) {
  1635. next = ent;
  1636. next_cpu = cpu;
  1637. next_ts = ts;
  1638. next_lost = lost_events;
  1639. next_size = iter->ent_size;
  1640. }
  1641. }
  1642. iter->ent_size = next_size;
  1643. if (ent_cpu)
  1644. *ent_cpu = next_cpu;
  1645. if (ent_ts)
  1646. *ent_ts = next_ts;
  1647. if (missing_events)
  1648. *missing_events = next_lost;
  1649. return next;
  1650. }
  1651. /* Find the next real entry, without updating the iterator itself */
  1652. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1653. int *ent_cpu, u64 *ent_ts)
  1654. {
  1655. return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  1656. }
  1657. /* Find the next real entry, and increment the iterator to the next entry */
  1658. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  1659. {
  1660. iter->ent = __find_next_entry(iter, &iter->cpu,
  1661. &iter->lost_events, &iter->ts);
  1662. if (iter->ent)
  1663. trace_iterator_increment(iter);
  1664. return iter->ent ? iter : NULL;
  1665. }
  1666. static void trace_consume(struct trace_iterator *iter)
  1667. {
  1668. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
  1669. &iter->lost_events);
  1670. }
  1671. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1672. {
  1673. struct trace_iterator *iter = m->private;
  1674. int i = (int)*pos;
  1675. void *ent;
  1676. WARN_ON_ONCE(iter->leftover);
  1677. (*pos)++;
  1678. /* can't go backwards */
  1679. if (iter->idx > i)
  1680. return NULL;
  1681. if (iter->idx < 0)
  1682. ent = trace_find_next_entry_inc(iter);
  1683. else
  1684. ent = iter;
  1685. while (ent && iter->idx < i)
  1686. ent = trace_find_next_entry_inc(iter);
  1687. iter->pos = *pos;
  1688. return ent;
  1689. }
  1690. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  1691. {
  1692. struct ring_buffer_event *event;
  1693. struct ring_buffer_iter *buf_iter;
  1694. unsigned long entries = 0;
  1695. u64 ts;
  1696. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
  1697. buf_iter = trace_buffer_iter(iter, cpu);
  1698. if (!buf_iter)
  1699. return;
  1700. ring_buffer_iter_reset(buf_iter);
  1701. /*
  1702. * We could have the case with the max latency tracers
  1703. * that a reset never took place on a cpu. This is evident
  1704. * by the timestamp being before the start of the buffer.
  1705. */
  1706. while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
  1707. if (ts >= iter->trace_buffer->time_start)
  1708. break;
  1709. entries++;
  1710. ring_buffer_read(buf_iter, NULL);
  1711. }
  1712. per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
  1713. }
  1714. /*
  1715. * The current tracer is copied to avoid a global locking
  1716. * all around.
  1717. */
  1718. static void *s_start(struct seq_file *m, loff_t *pos)
  1719. {
  1720. struct trace_iterator *iter = m->private;
  1721. struct trace_array *tr = iter->tr;
  1722. int cpu_file = iter->cpu_file;
  1723. void *p = NULL;
  1724. loff_t l = 0;
  1725. int cpu;
  1726. /*
  1727. * copy the tracer to avoid using a global lock all around.
  1728. * iter->trace is a copy of current_trace, the pointer to the
  1729. * name may be used instead of a strcmp(), as iter->trace->name
  1730. * will point to the same string as current_trace->name.
  1731. */
  1732. mutex_lock(&trace_types_lock);
  1733. if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
  1734. *iter->trace = *tr->current_trace;
  1735. mutex_unlock(&trace_types_lock);
  1736. #ifdef CONFIG_TRACER_MAX_TRACE
  1737. if (iter->snapshot && iter->trace->use_max_tr)
  1738. return ERR_PTR(-EBUSY);
  1739. #endif
  1740. if (!iter->snapshot)
  1741. atomic_inc(&trace_record_cmdline_disabled);
  1742. if (*pos != iter->pos) {
  1743. iter->ent = NULL;
  1744. iter->cpu = 0;
  1745. iter->idx = -1;
  1746. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  1747. for_each_tracing_cpu(cpu)
  1748. tracing_iter_reset(iter, cpu);
  1749. } else
  1750. tracing_iter_reset(iter, cpu_file);
  1751. iter->leftover = 0;
  1752. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1753. ;
  1754. } else {
  1755. /*
  1756. * If we overflowed the seq_file before, then we want
  1757. * to just reuse the trace_seq buffer again.
  1758. */
  1759. if (iter->leftover)
  1760. p = iter;
  1761. else {
  1762. l = *pos - 1;
  1763. p = s_next(m, p, &l);
  1764. }
  1765. }
  1766. trace_event_read_lock();
  1767. trace_access_lock(cpu_file);
  1768. return p;
  1769. }
  1770. static void s_stop(struct seq_file *m, void *p)
  1771. {
  1772. struct trace_iterator *iter = m->private;
  1773. #ifdef CONFIG_TRACER_MAX_TRACE
  1774. if (iter->snapshot && iter->trace->use_max_tr)
  1775. return;
  1776. #endif
  1777. if (!iter->snapshot)
  1778. atomic_dec(&trace_record_cmdline_disabled);
  1779. trace_access_unlock(iter->cpu_file);
  1780. trace_event_read_unlock();
  1781. }
  1782. static void
  1783. get_total_entries(struct trace_buffer *buf,
  1784. unsigned long *total, unsigned long *entries)
  1785. {
  1786. unsigned long count;
  1787. int cpu;
  1788. *total = 0;
  1789. *entries = 0;
  1790. for_each_tracing_cpu(cpu) {
  1791. count = ring_buffer_entries_cpu(buf->buffer, cpu);
  1792. /*
  1793. * If this buffer has skipped entries, then we hold all
  1794. * entries for the trace and we need to ignore the
  1795. * ones before the time stamp.
  1796. */
  1797. if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
  1798. count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
  1799. /* total is the same as the entries */
  1800. *total += count;
  1801. } else
  1802. *total += count +
  1803. ring_buffer_overrun_cpu(buf->buffer, cpu);
  1804. *entries += count;
  1805. }
  1806. }
  1807. static void print_lat_help_header(struct seq_file *m)
  1808. {
  1809. seq_puts(m, "# _------=> CPU# \n");
  1810. seq_puts(m, "# / _-----=> irqs-off \n");
  1811. seq_puts(m, "# | / _----=> need-resched \n");
  1812. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1813. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1814. seq_puts(m, "# |||| / delay \n");
  1815. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1816. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1817. }
  1818. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  1819. {
  1820. unsigned long total;
  1821. unsigned long entries;
  1822. get_total_entries(buf, &total, &entries);
  1823. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  1824. entries, total, num_online_cpus());
  1825. seq_puts(m, "#\n");
  1826. }
  1827. static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
  1828. {
  1829. print_event_info(buf, m);
  1830. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1831. seq_puts(m, "# | | | | |\n");
  1832. }
  1833. static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
  1834. {
  1835. print_event_info(buf, m);
  1836. seq_puts(m, "# _-----=> irqs-off\n");
  1837. seq_puts(m, "# / _----=> need-resched\n");
  1838. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1839. seq_puts(m, "# || / _--=> preempt-depth\n");
  1840. seq_puts(m, "# ||| / delay\n");
  1841. seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
  1842. seq_puts(m, "# | | | |||| | |\n");
  1843. }
  1844. void
  1845. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1846. {
  1847. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1848. struct trace_buffer *buf = iter->trace_buffer;
  1849. struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
  1850. struct tracer *type = iter->trace;
  1851. unsigned long entries;
  1852. unsigned long total;
  1853. const char *name = "preemption";
  1854. name = type->name;
  1855. get_total_entries(buf, &total, &entries);
  1856. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1857. name, UTS_RELEASE);
  1858. seq_puts(m, "# -----------------------------------"
  1859. "---------------------------------\n");
  1860. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1861. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1862. nsecs_to_usecs(data->saved_latency),
  1863. entries,
  1864. total,
  1865. buf->cpu,
  1866. #if defined(CONFIG_PREEMPT_NONE)
  1867. "server",
  1868. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1869. "desktop",
  1870. #elif defined(CONFIG_PREEMPT)
  1871. "preempt",
  1872. #else
  1873. "unknown",
  1874. #endif
  1875. /* These are reserved for later use */
  1876. 0, 0, 0, 0);
  1877. #ifdef CONFIG_SMP
  1878. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1879. #else
  1880. seq_puts(m, ")\n");
  1881. #endif
  1882. seq_puts(m, "# -----------------\n");
  1883. seq_printf(m, "# | task: %.16s-%d "
  1884. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1885. data->comm, data->pid,
  1886. from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
  1887. data->policy, data->rt_priority);
  1888. seq_puts(m, "# -----------------\n");
  1889. if (data->critical_start) {
  1890. seq_puts(m, "# => started at: ");
  1891. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1892. trace_print_seq(m, &iter->seq);
  1893. seq_puts(m, "\n# => ended at: ");
  1894. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1895. trace_print_seq(m, &iter->seq);
  1896. seq_puts(m, "\n#\n");
  1897. }
  1898. seq_puts(m, "#\n");
  1899. }
  1900. static void test_cpu_buff_start(struct trace_iterator *iter)
  1901. {
  1902. struct trace_seq *s = &iter->seq;
  1903. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1904. return;
  1905. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1906. return;
  1907. if (cpumask_test_cpu(iter->cpu, iter->started))
  1908. return;
  1909. if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
  1910. return;
  1911. cpumask_set_cpu(iter->cpu, iter->started);
  1912. /* Don't print started cpu buffer for the first entry of the trace */
  1913. if (iter->idx > 1)
  1914. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  1915. iter->cpu);
  1916. }
  1917. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1918. {
  1919. struct trace_seq *s = &iter->seq;
  1920. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1921. struct trace_entry *entry;
  1922. struct trace_event *event;
  1923. entry = iter->ent;
  1924. test_cpu_buff_start(iter);
  1925. event = ftrace_find_event(entry->type);
  1926. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1927. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1928. if (!trace_print_lat_context(iter))
  1929. goto partial;
  1930. } else {
  1931. if (!trace_print_context(iter))
  1932. goto partial;
  1933. }
  1934. }
  1935. if (event)
  1936. return event->funcs->trace(iter, sym_flags, event);
  1937. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1938. goto partial;
  1939. return TRACE_TYPE_HANDLED;
  1940. partial:
  1941. return TRACE_TYPE_PARTIAL_LINE;
  1942. }
  1943. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1944. {
  1945. struct trace_seq *s = &iter->seq;
  1946. struct trace_entry *entry;
  1947. struct trace_event *event;
  1948. entry = iter->ent;
  1949. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1950. if (!trace_seq_printf(s, "%d %d %llu ",
  1951. entry->pid, iter->cpu, iter->ts))
  1952. goto partial;
  1953. }
  1954. event = ftrace_find_event(entry->type);
  1955. if (event)
  1956. return event->funcs->raw(iter, 0, event);
  1957. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1958. goto partial;
  1959. return TRACE_TYPE_HANDLED;
  1960. partial:
  1961. return TRACE_TYPE_PARTIAL_LINE;
  1962. }
  1963. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1964. {
  1965. struct trace_seq *s = &iter->seq;
  1966. unsigned char newline = '\n';
  1967. struct trace_entry *entry;
  1968. struct trace_event *event;
  1969. entry = iter->ent;
  1970. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1971. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1972. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1973. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1974. }
  1975. event = ftrace_find_event(entry->type);
  1976. if (event) {
  1977. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  1978. if (ret != TRACE_TYPE_HANDLED)
  1979. return ret;
  1980. }
  1981. SEQ_PUT_FIELD_RET(s, newline);
  1982. return TRACE_TYPE_HANDLED;
  1983. }
  1984. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1985. {
  1986. struct trace_seq *s = &iter->seq;
  1987. struct trace_entry *entry;
  1988. struct trace_event *event;
  1989. entry = iter->ent;
  1990. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1991. SEQ_PUT_FIELD_RET(s, entry->pid);
  1992. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1993. SEQ_PUT_FIELD_RET(s, iter->ts);
  1994. }
  1995. event = ftrace_find_event(entry->type);
  1996. return event ? event->funcs->binary(iter, 0, event) :
  1997. TRACE_TYPE_HANDLED;
  1998. }
  1999. int trace_empty(struct trace_iterator *iter)
  2000. {
  2001. struct ring_buffer_iter *buf_iter;
  2002. int cpu;
  2003. /* If we are looking at one CPU buffer, only check that one */
  2004. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  2005. cpu = iter->cpu_file;
  2006. buf_iter = trace_buffer_iter(iter, cpu);
  2007. if (buf_iter) {
  2008. if (!ring_buffer_iter_empty(buf_iter))
  2009. return 0;
  2010. } else {
  2011. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2012. return 0;
  2013. }
  2014. return 1;
  2015. }
  2016. for_each_tracing_cpu(cpu) {
  2017. buf_iter = trace_buffer_iter(iter, cpu);
  2018. if (buf_iter) {
  2019. if (!ring_buffer_iter_empty(buf_iter))
  2020. return 0;
  2021. } else {
  2022. if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
  2023. return 0;
  2024. }
  2025. }
  2026. return 1;
  2027. }
  2028. /* Called with trace_event_read_lock() held. */
  2029. enum print_line_t print_trace_line(struct trace_iterator *iter)
  2030. {
  2031. enum print_line_t ret;
  2032. if (iter->lost_events &&
  2033. !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  2034. iter->cpu, iter->lost_events))
  2035. return TRACE_TYPE_PARTIAL_LINE;
  2036. if (iter->trace && iter->trace->print_line) {
  2037. ret = iter->trace->print_line(iter);
  2038. if (ret != TRACE_TYPE_UNHANDLED)
  2039. return ret;
  2040. }
  2041. if (iter->ent->type == TRACE_BPRINT &&
  2042. trace_flags & TRACE_ITER_PRINTK &&
  2043. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2044. return trace_print_bprintk_msg_only(iter);
  2045. if (iter->ent->type == TRACE_PRINT &&
  2046. trace_flags & TRACE_ITER_PRINTK &&
  2047. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  2048. return trace_print_printk_msg_only(iter);
  2049. if (trace_flags & TRACE_ITER_BIN)
  2050. return print_bin_fmt(iter);
  2051. if (trace_flags & TRACE_ITER_HEX)
  2052. return print_hex_fmt(iter);
  2053. if (trace_flags & TRACE_ITER_RAW)
  2054. return print_raw_fmt(iter);
  2055. return print_trace_fmt(iter);
  2056. }
  2057. void trace_latency_header(struct seq_file *m)
  2058. {
  2059. struct trace_iterator *iter = m->private;
  2060. /* print nothing if the buffers are empty */
  2061. if (trace_empty(iter))
  2062. return;
  2063. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  2064. print_trace_header(m, iter);
  2065. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2066. print_lat_help_header(m);
  2067. }
  2068. void trace_default_header(struct seq_file *m)
  2069. {
  2070. struct trace_iterator *iter = m->private;
  2071. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  2072. return;
  2073. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  2074. /* print nothing if the buffers are empty */
  2075. if (trace_empty(iter))
  2076. return;
  2077. print_trace_header(m, iter);
  2078. if (!(trace_flags & TRACE_ITER_VERBOSE))
  2079. print_lat_help_header(m);
  2080. } else {
  2081. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  2082. if (trace_flags & TRACE_ITER_IRQ_INFO)
  2083. print_func_help_header_irq(iter->trace_buffer, m);
  2084. else
  2085. print_func_help_header(iter->trace_buffer, m);
  2086. }
  2087. }
  2088. }
  2089. static void test_ftrace_alive(struct seq_file *m)
  2090. {
  2091. if (!ftrace_is_dead())
  2092. return;
  2093. seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  2094. seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
  2095. }
  2096. #ifdef CONFIG_TRACER_MAX_TRACE
  2097. static void show_snapshot_main_help(struct seq_file *m)
  2098. {
  2099. seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
  2100. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2101. seq_printf(m, "# Takes a snapshot of the main buffer.\n");
  2102. seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
  2103. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2104. seq_printf(m, "# is not a '0' or '1')\n");
  2105. }
  2106. static void show_snapshot_percpu_help(struct seq_file *m)
  2107. {
  2108. seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
  2109. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  2110. seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
  2111. seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
  2112. #else
  2113. seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
  2114. seq_printf(m, "# Must use main snapshot file to allocate.\n");
  2115. #endif
  2116. seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
  2117. seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
  2118. seq_printf(m, "# is not a '0' or '1')\n");
  2119. }
  2120. static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
  2121. {
  2122. if (iter->tr->allocated_snapshot)
  2123. seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
  2124. else
  2125. seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
  2126. seq_printf(m, "# Snapshot commands:\n");
  2127. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  2128. show_snapshot_main_help(m);
  2129. else
  2130. show_snapshot_percpu_help(m);
  2131. }
  2132. #else
  2133. /* Should never be called */
  2134. static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
  2135. #endif
  2136. static int s_show(struct seq_file *m, void *v)
  2137. {
  2138. struct trace_iterator *iter = v;
  2139. int ret;
  2140. if (iter->ent == NULL) {
  2141. if (iter->tr) {
  2142. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  2143. seq_puts(m, "#\n");
  2144. test_ftrace_alive(m);
  2145. }
  2146. if (iter->snapshot && trace_empty(iter))
  2147. print_snapshot_help(m, iter);
  2148. else if (iter->trace && iter->trace->print_header)
  2149. iter->trace->print_header(m);
  2150. else
  2151. trace_default_header(m);
  2152. } else if (iter->leftover) {
  2153. /*
  2154. * If we filled the seq_file buffer earlier, we
  2155. * want to just show it now.
  2156. */
  2157. ret = trace_print_seq(m, &iter->seq);
  2158. /* ret should this time be zero, but you never know */
  2159. iter->leftover = ret;
  2160. } else {
  2161. print_trace_line(iter);
  2162. ret = trace_print_seq(m, &iter->seq);
  2163. /*
  2164. * If we overflow the seq_file buffer, then it will
  2165. * ask us for this data again at start up.
  2166. * Use that instead.
  2167. * ret is 0 if seq_file write succeeded.
  2168. * -1 otherwise.
  2169. */
  2170. iter->leftover = ret;
  2171. }
  2172. return 0;
  2173. }
  2174. static const struct seq_operations tracer_seq_ops = {
  2175. .start = s_start,
  2176. .next = s_next,
  2177. .stop = s_stop,
  2178. .show = s_show,
  2179. };
  2180. static struct trace_iterator *
  2181. __tracing_open(struct inode *inode, struct file *file, bool snapshot)
  2182. {
  2183. struct trace_cpu *tc = inode->i_private;
  2184. struct trace_array *tr = tc->tr;
  2185. struct trace_iterator *iter;
  2186. int cpu;
  2187. if (tracing_disabled)
  2188. return ERR_PTR(-ENODEV);
  2189. iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
  2190. if (!iter)
  2191. return ERR_PTR(-ENOMEM);
  2192. iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
  2193. GFP_KERNEL);
  2194. if (!iter->buffer_iter)
  2195. goto release;
  2196. /*
  2197. * We make a copy of the current tracer to avoid concurrent
  2198. * changes on it while we are reading.
  2199. */
  2200. mutex_lock(&trace_types_lock);
  2201. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  2202. if (!iter->trace)
  2203. goto fail;
  2204. *iter->trace = *tr->current_trace;
  2205. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  2206. goto fail;
  2207. iter->tr = tr;
  2208. #ifdef CONFIG_TRACER_MAX_TRACE
  2209. /* Currently only the top directory has a snapshot */
  2210. if (tr->current_trace->print_max || snapshot)
  2211. iter->trace_buffer = &tr->max_buffer;
  2212. else
  2213. #endif
  2214. iter->trace_buffer = &tr->trace_buffer;
  2215. iter->snapshot = snapshot;
  2216. iter->pos = -1;
  2217. mutex_init(&iter->mutex);
  2218. iter->cpu_file = tc->cpu;
  2219. /* Notify the tracer early; before we stop tracing. */
  2220. if (iter->trace && iter->trace->open)
  2221. iter->trace->open(iter);
  2222. /* Annotate start of buffers if we had overruns */
  2223. if (ring_buffer_overruns(iter->trace_buffer->buffer))
  2224. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  2225. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  2226. if (trace_clocks[trace_clock_id].in_ns)
  2227. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  2228. /* stop the trace while dumping if we are not opening "snapshot" */
  2229. if (!iter->snapshot)
  2230. tracing_stop_tr(tr);
  2231. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  2232. for_each_tracing_cpu(cpu) {
  2233. iter->buffer_iter[cpu] =
  2234. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2235. }
  2236. ring_buffer_read_prepare_sync();
  2237. for_each_tracing_cpu(cpu) {
  2238. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2239. tracing_iter_reset(iter, cpu);
  2240. }
  2241. } else {
  2242. cpu = iter->cpu_file;
  2243. iter->buffer_iter[cpu] =
  2244. ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
  2245. ring_buffer_read_prepare_sync();
  2246. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2247. tracing_iter_reset(iter, cpu);
  2248. }
  2249. tr->ref++;
  2250. mutex_unlock(&trace_types_lock);
  2251. return iter;
  2252. fail:
  2253. mutex_unlock(&trace_types_lock);
  2254. kfree(iter->trace);
  2255. kfree(iter->buffer_iter);
  2256. release:
  2257. seq_release_private(inode, file);
  2258. return ERR_PTR(-ENOMEM);
  2259. }
  2260. int tracing_open_generic(struct inode *inode, struct file *filp)
  2261. {
  2262. if (tracing_disabled)
  2263. return -ENODEV;
  2264. filp->private_data = inode->i_private;
  2265. return 0;
  2266. }
  2267. static int tracing_release(struct inode *inode, struct file *file)
  2268. {
  2269. struct seq_file *m = file->private_data;
  2270. struct trace_iterator *iter;
  2271. struct trace_array *tr;
  2272. int cpu;
  2273. if (!(file->f_mode & FMODE_READ))
  2274. return 0;
  2275. iter = m->private;
  2276. tr = iter->tr;
  2277. mutex_lock(&trace_types_lock);
  2278. WARN_ON(!tr->ref);
  2279. tr->ref--;
  2280. for_each_tracing_cpu(cpu) {
  2281. if (iter->buffer_iter[cpu])
  2282. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2283. }
  2284. if (iter->trace && iter->trace->close)
  2285. iter->trace->close(iter);
  2286. if (!iter->snapshot)
  2287. /* reenable tracing if it was previously enabled */
  2288. tracing_start_tr(tr);
  2289. mutex_unlock(&trace_types_lock);
  2290. mutex_destroy(&iter->mutex);
  2291. free_cpumask_var(iter->started);
  2292. kfree(iter->trace);
  2293. kfree(iter->buffer_iter);
  2294. seq_release_private(inode, file);
  2295. return 0;
  2296. }
  2297. static int tracing_open(struct inode *inode, struct file *file)
  2298. {
  2299. struct trace_iterator *iter;
  2300. int ret = 0;
  2301. /* If this file was open for write, then erase contents */
  2302. if ((file->f_mode & FMODE_WRITE) &&
  2303. (file->f_flags & O_TRUNC)) {
  2304. struct trace_cpu *tc = inode->i_private;
  2305. struct trace_array *tr = tc->tr;
  2306. if (tc->cpu == RING_BUFFER_ALL_CPUS)
  2307. tracing_reset_online_cpus(&tr->trace_buffer);
  2308. else
  2309. tracing_reset(&tr->trace_buffer, tc->cpu);
  2310. }
  2311. if (file->f_mode & FMODE_READ) {
  2312. iter = __tracing_open(inode, file, false);
  2313. if (IS_ERR(iter))
  2314. ret = PTR_ERR(iter);
  2315. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2316. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2317. }
  2318. return ret;
  2319. }
  2320. static void *
  2321. t_next(struct seq_file *m, void *v, loff_t *pos)
  2322. {
  2323. struct tracer *t = v;
  2324. (*pos)++;
  2325. if (t)
  2326. t = t->next;
  2327. return t;
  2328. }
  2329. static void *t_start(struct seq_file *m, loff_t *pos)
  2330. {
  2331. struct tracer *t;
  2332. loff_t l = 0;
  2333. mutex_lock(&trace_types_lock);
  2334. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  2335. ;
  2336. return t;
  2337. }
  2338. static void t_stop(struct seq_file *m, void *p)
  2339. {
  2340. mutex_unlock(&trace_types_lock);
  2341. }
  2342. static int t_show(struct seq_file *m, void *v)
  2343. {
  2344. struct tracer *t = v;
  2345. if (!t)
  2346. return 0;
  2347. seq_printf(m, "%s", t->name);
  2348. if (t->next)
  2349. seq_putc(m, ' ');
  2350. else
  2351. seq_putc(m, '\n');
  2352. return 0;
  2353. }
  2354. static const struct seq_operations show_traces_seq_ops = {
  2355. .start = t_start,
  2356. .next = t_next,
  2357. .stop = t_stop,
  2358. .show = t_show,
  2359. };
  2360. static int show_traces_open(struct inode *inode, struct file *file)
  2361. {
  2362. if (tracing_disabled)
  2363. return -ENODEV;
  2364. return seq_open(file, &show_traces_seq_ops);
  2365. }
  2366. static ssize_t
  2367. tracing_write_stub(struct file *filp, const char __user *ubuf,
  2368. size_t count, loff_t *ppos)
  2369. {
  2370. return count;
  2371. }
  2372. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  2373. {
  2374. if (file->f_mode & FMODE_READ)
  2375. return seq_lseek(file, offset, origin);
  2376. else
  2377. return 0;
  2378. }
  2379. static const struct file_operations tracing_fops = {
  2380. .open = tracing_open,
  2381. .read = seq_read,
  2382. .write = tracing_write_stub,
  2383. .llseek = tracing_seek,
  2384. .release = tracing_release,
  2385. };
  2386. static const struct file_operations show_traces_fops = {
  2387. .open = show_traces_open,
  2388. .read = seq_read,
  2389. .release = seq_release,
  2390. .llseek = seq_lseek,
  2391. };
  2392. /*
  2393. * Only trace on a CPU if the bitmask is set:
  2394. */
  2395. static cpumask_var_t tracing_cpumask;
  2396. /*
  2397. * The tracer itself will not take this lock, but still we want
  2398. * to provide a consistent cpumask to user-space:
  2399. */
  2400. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2401. /*
  2402. * Temporary storage for the character representation of the
  2403. * CPU bitmask (and one more byte for the newline):
  2404. */
  2405. static char mask_str[NR_CPUS + 1];
  2406. static ssize_t
  2407. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2408. size_t count, loff_t *ppos)
  2409. {
  2410. int len;
  2411. mutex_lock(&tracing_cpumask_update_lock);
  2412. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2413. if (count - len < 2) {
  2414. count = -EINVAL;
  2415. goto out_err;
  2416. }
  2417. len += sprintf(mask_str + len, "\n");
  2418. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2419. out_err:
  2420. mutex_unlock(&tracing_cpumask_update_lock);
  2421. return count;
  2422. }
  2423. static ssize_t
  2424. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2425. size_t count, loff_t *ppos)
  2426. {
  2427. struct trace_array *tr = filp->private_data;
  2428. cpumask_var_t tracing_cpumask_new;
  2429. int err, cpu;
  2430. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2431. return -ENOMEM;
  2432. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2433. if (err)
  2434. goto err_unlock;
  2435. mutex_lock(&tracing_cpumask_update_lock);
  2436. local_irq_disable();
  2437. arch_spin_lock(&ftrace_max_lock);
  2438. for_each_tracing_cpu(cpu) {
  2439. /*
  2440. * Increase/decrease the disabled counter if we are
  2441. * about to flip a bit in the cpumask:
  2442. */
  2443. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  2444. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2445. atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2446. ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
  2447. }
  2448. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2449. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2450. atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2451. ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
  2452. }
  2453. }
  2454. arch_spin_unlock(&ftrace_max_lock);
  2455. local_irq_enable();
  2456. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2457. mutex_unlock(&tracing_cpumask_update_lock);
  2458. free_cpumask_var(tracing_cpumask_new);
  2459. return count;
  2460. err_unlock:
  2461. free_cpumask_var(tracing_cpumask_new);
  2462. return err;
  2463. }
  2464. static const struct file_operations tracing_cpumask_fops = {
  2465. .open = tracing_open_generic,
  2466. .read = tracing_cpumask_read,
  2467. .write = tracing_cpumask_write,
  2468. .llseek = generic_file_llseek,
  2469. };
  2470. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2471. {
  2472. struct tracer_opt *trace_opts;
  2473. struct trace_array *tr = m->private;
  2474. u32 tracer_flags;
  2475. int i;
  2476. mutex_lock(&trace_types_lock);
  2477. tracer_flags = tr->current_trace->flags->val;
  2478. trace_opts = tr->current_trace->flags->opts;
  2479. for (i = 0; trace_options[i]; i++) {
  2480. if (trace_flags & (1 << i))
  2481. seq_printf(m, "%s\n", trace_options[i]);
  2482. else
  2483. seq_printf(m, "no%s\n", trace_options[i]);
  2484. }
  2485. for (i = 0; trace_opts[i].name; i++) {
  2486. if (tracer_flags & trace_opts[i].bit)
  2487. seq_printf(m, "%s\n", trace_opts[i].name);
  2488. else
  2489. seq_printf(m, "no%s\n", trace_opts[i].name);
  2490. }
  2491. mutex_unlock(&trace_types_lock);
  2492. return 0;
  2493. }
  2494. static int __set_tracer_option(struct tracer *trace,
  2495. struct tracer_flags *tracer_flags,
  2496. struct tracer_opt *opts, int neg)
  2497. {
  2498. int ret;
  2499. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2500. if (ret)
  2501. return ret;
  2502. if (neg)
  2503. tracer_flags->val &= ~opts->bit;
  2504. else
  2505. tracer_flags->val |= opts->bit;
  2506. return 0;
  2507. }
  2508. /* Try to assign a tracer specific option */
  2509. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2510. {
  2511. struct tracer_flags *tracer_flags = trace->flags;
  2512. struct tracer_opt *opts = NULL;
  2513. int i;
  2514. for (i = 0; tracer_flags->opts[i].name; i++) {
  2515. opts = &tracer_flags->opts[i];
  2516. if (strcmp(cmp, opts->name) == 0)
  2517. return __set_tracer_option(trace, trace->flags,
  2518. opts, neg);
  2519. }
  2520. return -EINVAL;
  2521. }
  2522. /* Some tracers require overwrite to stay enabled */
  2523. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  2524. {
  2525. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  2526. return -1;
  2527. return 0;
  2528. }
  2529. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  2530. {
  2531. /* do nothing if flag is already set */
  2532. if (!!(trace_flags & mask) == !!enabled)
  2533. return 0;
  2534. /* Give the tracer a chance to approve the change */
  2535. if (tr->current_trace->flag_changed)
  2536. if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
  2537. return -EINVAL;
  2538. if (enabled)
  2539. trace_flags |= mask;
  2540. else
  2541. trace_flags &= ~mask;
  2542. if (mask == TRACE_ITER_RECORD_CMD)
  2543. trace_event_enable_cmd_record(enabled);
  2544. if (mask == TRACE_ITER_OVERWRITE) {
  2545. ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
  2546. #ifdef CONFIG_TRACER_MAX_TRACE
  2547. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  2548. #endif
  2549. }
  2550. if (mask == TRACE_ITER_PRINTK)
  2551. trace_printk_start_stop_comm(enabled);
  2552. return 0;
  2553. }
  2554. static int trace_set_options(struct trace_array *tr, char *option)
  2555. {
  2556. char *cmp;
  2557. int neg = 0;
  2558. int ret = -ENODEV;
  2559. int i;
  2560. cmp = strstrip(option);
  2561. if (strncmp(cmp, "no", 2) == 0) {
  2562. neg = 1;
  2563. cmp += 2;
  2564. }
  2565. mutex_lock(&trace_types_lock);
  2566. for (i = 0; trace_options[i]; i++) {
  2567. if (strcmp(cmp, trace_options[i]) == 0) {
  2568. ret = set_tracer_flag(tr, 1 << i, !neg);
  2569. break;
  2570. }
  2571. }
  2572. /* If no option could be set, test the specific tracer options */
  2573. if (!trace_options[i])
  2574. ret = set_tracer_option(tr->current_trace, cmp, neg);
  2575. mutex_unlock(&trace_types_lock);
  2576. return ret;
  2577. }
  2578. static ssize_t
  2579. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2580. size_t cnt, loff_t *ppos)
  2581. {
  2582. struct seq_file *m = filp->private_data;
  2583. struct trace_array *tr = m->private;
  2584. char buf[64];
  2585. int ret;
  2586. if (cnt >= sizeof(buf))
  2587. return -EINVAL;
  2588. if (copy_from_user(&buf, ubuf, cnt))
  2589. return -EFAULT;
  2590. buf[cnt] = 0;
  2591. ret = trace_set_options(tr, buf);
  2592. if (ret < 0)
  2593. return ret;
  2594. *ppos += cnt;
  2595. return cnt;
  2596. }
  2597. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2598. {
  2599. if (tracing_disabled)
  2600. return -ENODEV;
  2601. return single_open(file, tracing_trace_options_show, inode->i_private);
  2602. }
  2603. static const struct file_operations tracing_iter_fops = {
  2604. .open = tracing_trace_options_open,
  2605. .read = seq_read,
  2606. .llseek = seq_lseek,
  2607. .release = single_release,
  2608. .write = tracing_trace_options_write,
  2609. };
  2610. static const char readme_msg[] =
  2611. "tracing mini-HOWTO:\n\n"
  2612. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2613. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2614. "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
  2615. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2616. "nop\n"
  2617. "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
  2618. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2619. "wakeup\n"
  2620. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2621. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2622. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2623. "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
  2624. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2625. "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
  2626. ;
  2627. static ssize_t
  2628. tracing_readme_read(struct file *filp, char __user *ubuf,
  2629. size_t cnt, loff_t *ppos)
  2630. {
  2631. return simple_read_from_buffer(ubuf, cnt, ppos,
  2632. readme_msg, strlen(readme_msg));
  2633. }
  2634. static const struct file_operations tracing_readme_fops = {
  2635. .open = tracing_open_generic,
  2636. .read = tracing_readme_read,
  2637. .llseek = generic_file_llseek,
  2638. };
  2639. static ssize_t
  2640. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2641. size_t cnt, loff_t *ppos)
  2642. {
  2643. char *buf_comm;
  2644. char *file_buf;
  2645. char *buf;
  2646. int len = 0;
  2647. int pid;
  2648. int i;
  2649. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2650. if (!file_buf)
  2651. return -ENOMEM;
  2652. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2653. if (!buf_comm) {
  2654. kfree(file_buf);
  2655. return -ENOMEM;
  2656. }
  2657. buf = file_buf;
  2658. for (i = 0; i < SAVED_CMDLINES; i++) {
  2659. int r;
  2660. pid = map_cmdline_to_pid[i];
  2661. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2662. continue;
  2663. trace_find_cmdline(pid, buf_comm);
  2664. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2665. buf += r;
  2666. len += r;
  2667. }
  2668. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2669. file_buf, len);
  2670. kfree(file_buf);
  2671. kfree(buf_comm);
  2672. return len;
  2673. }
  2674. static const struct file_operations tracing_saved_cmdlines_fops = {
  2675. .open = tracing_open_generic,
  2676. .read = tracing_saved_cmdlines_read,
  2677. .llseek = generic_file_llseek,
  2678. };
  2679. static ssize_t
  2680. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2681. size_t cnt, loff_t *ppos)
  2682. {
  2683. struct trace_array *tr = filp->private_data;
  2684. char buf[MAX_TRACER_SIZE+2];
  2685. int r;
  2686. mutex_lock(&trace_types_lock);
  2687. r = sprintf(buf, "%s\n", tr->current_trace->name);
  2688. mutex_unlock(&trace_types_lock);
  2689. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2690. }
  2691. int tracer_init(struct tracer *t, struct trace_array *tr)
  2692. {
  2693. tracing_reset_online_cpus(&tr->trace_buffer);
  2694. return t->init(tr);
  2695. }
  2696. static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
  2697. {
  2698. int cpu;
  2699. for_each_tracing_cpu(cpu)
  2700. per_cpu_ptr(buf->data, cpu)->entries = val;
  2701. }
  2702. #ifdef CONFIG_TRACER_MAX_TRACE
  2703. /* resize @tr's buffer to the size of @size_tr's entries */
  2704. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  2705. struct trace_buffer *size_buf, int cpu_id)
  2706. {
  2707. int cpu, ret = 0;
  2708. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  2709. for_each_tracing_cpu(cpu) {
  2710. ret = ring_buffer_resize(trace_buf->buffer,
  2711. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  2712. if (ret < 0)
  2713. break;
  2714. per_cpu_ptr(trace_buf->data, cpu)->entries =
  2715. per_cpu_ptr(size_buf->data, cpu)->entries;
  2716. }
  2717. } else {
  2718. ret = ring_buffer_resize(trace_buf->buffer,
  2719. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  2720. if (ret == 0)
  2721. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  2722. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  2723. }
  2724. return ret;
  2725. }
  2726. #endif /* CONFIG_TRACER_MAX_TRACE */
  2727. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  2728. unsigned long size, int cpu)
  2729. {
  2730. int ret;
  2731. /*
  2732. * If kernel or user changes the size of the ring buffer
  2733. * we use the size that was given, and we can forget about
  2734. * expanding it later.
  2735. */
  2736. ring_buffer_expanded = 1;
  2737. /* May be called before buffers are initialized */
  2738. if (!tr->trace_buffer.buffer)
  2739. return 0;
  2740. ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
  2741. if (ret < 0)
  2742. return ret;
  2743. #ifdef CONFIG_TRACER_MAX_TRACE
  2744. if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
  2745. !tr->current_trace->use_max_tr)
  2746. goto out;
  2747. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  2748. if (ret < 0) {
  2749. int r = resize_buffer_duplicate_size(&tr->trace_buffer,
  2750. &tr->trace_buffer, cpu);
  2751. if (r < 0) {
  2752. /*
  2753. * AARGH! We are left with different
  2754. * size max buffer!!!!
  2755. * The max buffer is our "snapshot" buffer.
  2756. * When a tracer needs a snapshot (one of the
  2757. * latency tracers), it swaps the max buffer
  2758. * with the saved snap shot. We succeeded to
  2759. * update the size of the main buffer, but failed to
  2760. * update the size of the max buffer. But when we tried
  2761. * to reset the main buffer to the original size, we
  2762. * failed there too. This is very unlikely to
  2763. * happen, but if it does, warn and kill all
  2764. * tracing.
  2765. */
  2766. WARN_ON(1);
  2767. tracing_disabled = 1;
  2768. }
  2769. return ret;
  2770. }
  2771. if (cpu == RING_BUFFER_ALL_CPUS)
  2772. set_buffer_entries(&tr->max_buffer, size);
  2773. else
  2774. per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
  2775. out:
  2776. #endif /* CONFIG_TRACER_MAX_TRACE */
  2777. if (cpu == RING_BUFFER_ALL_CPUS)
  2778. set_buffer_entries(&tr->trace_buffer, size);
  2779. else
  2780. per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
  2781. return ret;
  2782. }
  2783. static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  2784. unsigned long size, int cpu_id)
  2785. {
  2786. int ret = size;
  2787. mutex_lock(&trace_types_lock);
  2788. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  2789. /* make sure, this cpu is enabled in the mask */
  2790. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
  2791. ret = -EINVAL;
  2792. goto out;
  2793. }
  2794. }
  2795. ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
  2796. if (ret < 0)
  2797. ret = -ENOMEM;
  2798. out:
  2799. mutex_unlock(&trace_types_lock);
  2800. return ret;
  2801. }
  2802. /**
  2803. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2804. *
  2805. * To save on memory when the tracing is never used on a system with it
  2806. * configured in. The ring buffers are set to a minimum size. But once
  2807. * a user starts to use the tracing facility, then they need to grow
  2808. * to their default size.
  2809. *
  2810. * This function is to be called when a tracer is about to be used.
  2811. */
  2812. int tracing_update_buffers(void)
  2813. {
  2814. int ret = 0;
  2815. mutex_lock(&trace_types_lock);
  2816. if (!ring_buffer_expanded)
  2817. ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
  2818. RING_BUFFER_ALL_CPUS);
  2819. mutex_unlock(&trace_types_lock);
  2820. return ret;
  2821. }
  2822. struct trace_option_dentry;
  2823. static struct trace_option_dentry *
  2824. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  2825. static void
  2826. destroy_trace_option_files(struct trace_option_dentry *topts);
  2827. static int tracing_set_tracer(const char *buf)
  2828. {
  2829. static struct trace_option_dentry *topts;
  2830. struct trace_array *tr = &global_trace;
  2831. struct tracer *t;
  2832. #ifdef CONFIG_TRACER_MAX_TRACE
  2833. bool had_max_tr;
  2834. #endif
  2835. int ret = 0;
  2836. mutex_lock(&trace_types_lock);
  2837. if (!ring_buffer_expanded) {
  2838. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  2839. RING_BUFFER_ALL_CPUS);
  2840. if (ret < 0)
  2841. goto out;
  2842. ret = 0;
  2843. }
  2844. for (t = trace_types; t; t = t->next) {
  2845. if (strcmp(t->name, buf) == 0)
  2846. break;
  2847. }
  2848. if (!t) {
  2849. ret = -EINVAL;
  2850. goto out;
  2851. }
  2852. if (t == tr->current_trace)
  2853. goto out;
  2854. trace_branch_disable();
  2855. tr->current_trace->enabled = false;
  2856. if (tr->current_trace->reset)
  2857. tr->current_trace->reset(tr);
  2858. /* Current trace needs to be nop_trace before synchronize_sched */
  2859. tr->current_trace = &nop_trace;
  2860. #ifdef CONFIG_TRACER_MAX_TRACE
  2861. had_max_tr = tr->allocated_snapshot;
  2862. if (had_max_tr && !t->use_max_tr) {
  2863. /*
  2864. * We need to make sure that the update_max_tr sees that
  2865. * current_trace changed to nop_trace to keep it from
  2866. * swapping the buffers after we resize it.
  2867. * The update_max_tr is called from interrupts disabled
  2868. * so a synchronized_sched() is sufficient.
  2869. */
  2870. synchronize_sched();
  2871. /*
  2872. * We don't free the ring buffer. instead, resize it because
  2873. * The max_tr ring buffer has some state (e.g. ring->clock) and
  2874. * we want preserve it.
  2875. */
  2876. ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
  2877. set_buffer_entries(&tr->max_buffer, 1);
  2878. tracing_reset_online_cpus(&tr->max_buffer);
  2879. tr->allocated_snapshot = false;
  2880. }
  2881. #endif
  2882. destroy_trace_option_files(topts);
  2883. topts = create_trace_option_files(tr, t);
  2884. #ifdef CONFIG_TRACER_MAX_TRACE
  2885. if (t->use_max_tr && !had_max_tr) {
  2886. /* we need to make per cpu buffer sizes equivalent */
  2887. ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->trace_buffer,
  2888. RING_BUFFER_ALL_CPUS);
  2889. if (ret < 0)
  2890. goto out;
  2891. tr->allocated_snapshot = true;
  2892. }
  2893. #endif
  2894. if (t->init) {
  2895. ret = tracer_init(t, tr);
  2896. if (ret)
  2897. goto out;
  2898. }
  2899. tr->current_trace = t;
  2900. tr->current_trace->enabled = true;
  2901. trace_branch_enable(tr);
  2902. out:
  2903. mutex_unlock(&trace_types_lock);
  2904. return ret;
  2905. }
  2906. static ssize_t
  2907. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2908. size_t cnt, loff_t *ppos)
  2909. {
  2910. char buf[MAX_TRACER_SIZE+1];
  2911. int i;
  2912. size_t ret;
  2913. int err;
  2914. ret = cnt;
  2915. if (cnt > MAX_TRACER_SIZE)
  2916. cnt = MAX_TRACER_SIZE;
  2917. if (copy_from_user(&buf, ubuf, cnt))
  2918. return -EFAULT;
  2919. buf[cnt] = 0;
  2920. /* strip ending whitespace. */
  2921. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2922. buf[i] = 0;
  2923. err = tracing_set_tracer(buf);
  2924. if (err)
  2925. return err;
  2926. *ppos += ret;
  2927. return ret;
  2928. }
  2929. static ssize_t
  2930. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2931. size_t cnt, loff_t *ppos)
  2932. {
  2933. unsigned long *ptr = filp->private_data;
  2934. char buf[64];
  2935. int r;
  2936. r = snprintf(buf, sizeof(buf), "%ld\n",
  2937. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2938. if (r > sizeof(buf))
  2939. r = sizeof(buf);
  2940. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2941. }
  2942. static ssize_t
  2943. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2944. size_t cnt, loff_t *ppos)
  2945. {
  2946. unsigned long *ptr = filp->private_data;
  2947. unsigned long val;
  2948. int ret;
  2949. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  2950. if (ret)
  2951. return ret;
  2952. *ptr = val * 1000;
  2953. return cnt;
  2954. }
  2955. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2956. {
  2957. struct trace_cpu *tc = inode->i_private;
  2958. struct trace_array *tr = tc->tr;
  2959. struct trace_iterator *iter;
  2960. int ret = 0;
  2961. if (tracing_disabled)
  2962. return -ENODEV;
  2963. mutex_lock(&trace_types_lock);
  2964. /* create a buffer to store the information to pass to userspace */
  2965. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2966. if (!iter) {
  2967. ret = -ENOMEM;
  2968. goto out;
  2969. }
  2970. /*
  2971. * We make a copy of the current tracer to avoid concurrent
  2972. * changes on it while we are reading.
  2973. */
  2974. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2975. if (!iter->trace) {
  2976. ret = -ENOMEM;
  2977. goto fail;
  2978. }
  2979. *iter->trace = *tr->current_trace;
  2980. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2981. ret = -ENOMEM;
  2982. goto fail;
  2983. }
  2984. /* trace pipe does not show start of buffer */
  2985. cpumask_setall(iter->started);
  2986. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2987. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2988. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  2989. if (trace_clocks[trace_clock_id].in_ns)
  2990. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  2991. iter->cpu_file = tc->cpu;
  2992. iter->tr = tc->tr;
  2993. iter->trace_buffer = &tc->tr->trace_buffer;
  2994. mutex_init(&iter->mutex);
  2995. filp->private_data = iter;
  2996. if (iter->trace->pipe_open)
  2997. iter->trace->pipe_open(iter);
  2998. nonseekable_open(inode, filp);
  2999. out:
  3000. mutex_unlock(&trace_types_lock);
  3001. return ret;
  3002. fail:
  3003. kfree(iter->trace);
  3004. kfree(iter);
  3005. mutex_unlock(&trace_types_lock);
  3006. return ret;
  3007. }
  3008. static int tracing_release_pipe(struct inode *inode, struct file *file)
  3009. {
  3010. struct trace_iterator *iter = file->private_data;
  3011. mutex_lock(&trace_types_lock);
  3012. if (iter->trace->pipe_close)
  3013. iter->trace->pipe_close(iter);
  3014. mutex_unlock(&trace_types_lock);
  3015. free_cpumask_var(iter->started);
  3016. mutex_destroy(&iter->mutex);
  3017. kfree(iter->trace);
  3018. kfree(iter);
  3019. return 0;
  3020. }
  3021. static unsigned int
  3022. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  3023. {
  3024. /* Iterators are static, they should be filled or empty */
  3025. if (trace_buffer_iter(iter, iter->cpu_file))
  3026. return POLLIN | POLLRDNORM;
  3027. if (trace_flags & TRACE_ITER_BLOCK)
  3028. /*
  3029. * Always select as readable when in blocking mode
  3030. */
  3031. return POLLIN | POLLRDNORM;
  3032. else
  3033. return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
  3034. filp, poll_table);
  3035. }
  3036. static unsigned int
  3037. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  3038. {
  3039. struct trace_iterator *iter = filp->private_data;
  3040. return trace_poll(iter, filp, poll_table);
  3041. }
  3042. /*
  3043. * This is a make-shift waitqueue.
  3044. * A tracer might use this callback on some rare cases:
  3045. *
  3046. * 1) the current tracer might hold the runqueue lock when it wakes up
  3047. * a reader, hence a deadlock (sched, function, and function graph tracers)
  3048. * 2) the function tracers, trace all functions, we don't want
  3049. * the overhead of calling wake_up and friends
  3050. * (and tracing them too)
  3051. *
  3052. * Anyway, this is really very primitive wakeup.
  3053. */
  3054. void poll_wait_pipe(struct trace_iterator *iter)
  3055. {
  3056. set_current_state(TASK_INTERRUPTIBLE);
  3057. /* sleep for 100 msecs, and try again. */
  3058. schedule_timeout(HZ / 10);
  3059. }
  3060. /* Must be called with trace_types_lock mutex held. */
  3061. static int tracing_wait_pipe(struct file *filp)
  3062. {
  3063. struct trace_iterator *iter = filp->private_data;
  3064. while (trace_empty(iter)) {
  3065. if ((filp->f_flags & O_NONBLOCK)) {
  3066. return -EAGAIN;
  3067. }
  3068. mutex_unlock(&iter->mutex);
  3069. iter->trace->wait_pipe(iter);
  3070. mutex_lock(&iter->mutex);
  3071. if (signal_pending(current))
  3072. return -EINTR;
  3073. /*
  3074. * We block until we read something and tracing is disabled.
  3075. * We still block if tracing is disabled, but we have never
  3076. * read anything. This allows a user to cat this file, and
  3077. * then enable tracing. But after we have read something,
  3078. * we give an EOF when tracing is again disabled.
  3079. *
  3080. * iter->pos will be 0 if we haven't read anything.
  3081. */
  3082. if (!tracing_is_enabled() && iter->pos)
  3083. break;
  3084. }
  3085. return 1;
  3086. }
  3087. /*
  3088. * Consumer reader.
  3089. */
  3090. static ssize_t
  3091. tracing_read_pipe(struct file *filp, char __user *ubuf,
  3092. size_t cnt, loff_t *ppos)
  3093. {
  3094. struct trace_iterator *iter = filp->private_data;
  3095. struct trace_array *tr = iter->tr;
  3096. ssize_t sret;
  3097. /* return any leftover data */
  3098. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3099. if (sret != -EBUSY)
  3100. return sret;
  3101. trace_seq_init(&iter->seq);
  3102. /* copy the tracer to avoid using a global lock all around */
  3103. mutex_lock(&trace_types_lock);
  3104. if (unlikely(iter->trace->name != tr->current_trace->name))
  3105. *iter->trace = *tr->current_trace;
  3106. mutex_unlock(&trace_types_lock);
  3107. /*
  3108. * Avoid more than one consumer on a single file descriptor
  3109. * This is just a matter of traces coherency, the ring buffer itself
  3110. * is protected.
  3111. */
  3112. mutex_lock(&iter->mutex);
  3113. if (iter->trace->read) {
  3114. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  3115. if (sret)
  3116. goto out;
  3117. }
  3118. waitagain:
  3119. sret = tracing_wait_pipe(filp);
  3120. if (sret <= 0)
  3121. goto out;
  3122. /* stop when tracing is finished */
  3123. if (trace_empty(iter)) {
  3124. sret = 0;
  3125. goto out;
  3126. }
  3127. if (cnt >= PAGE_SIZE)
  3128. cnt = PAGE_SIZE - 1;
  3129. /* reset all but tr, trace, and overruns */
  3130. memset(&iter->seq, 0,
  3131. sizeof(struct trace_iterator) -
  3132. offsetof(struct trace_iterator, seq));
  3133. iter->pos = -1;
  3134. trace_event_read_lock();
  3135. trace_access_lock(iter->cpu_file);
  3136. while (trace_find_next_entry_inc(iter) != NULL) {
  3137. enum print_line_t ret;
  3138. int len = iter->seq.len;
  3139. ret = print_trace_line(iter);
  3140. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3141. /* don't print partial lines */
  3142. iter->seq.len = len;
  3143. break;
  3144. }
  3145. if (ret != TRACE_TYPE_NO_CONSUME)
  3146. trace_consume(iter);
  3147. if (iter->seq.len >= cnt)
  3148. break;
  3149. /*
  3150. * Setting the full flag means we reached the trace_seq buffer
  3151. * size and we should leave by partial output condition above.
  3152. * One of the trace_seq_* functions is not used properly.
  3153. */
  3154. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  3155. iter->ent->type);
  3156. }
  3157. trace_access_unlock(iter->cpu_file);
  3158. trace_event_read_unlock();
  3159. /* Now copy what we have to the user */
  3160. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3161. if (iter->seq.readpos >= iter->seq.len)
  3162. trace_seq_init(&iter->seq);
  3163. /*
  3164. * If there was nothing to send to user, in spite of consuming trace
  3165. * entries, go back to wait for more entries.
  3166. */
  3167. if (sret == -EBUSY)
  3168. goto waitagain;
  3169. out:
  3170. mutex_unlock(&iter->mutex);
  3171. return sret;
  3172. }
  3173. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  3174. struct pipe_buffer *buf)
  3175. {
  3176. __free_page(buf->page);
  3177. }
  3178. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  3179. unsigned int idx)
  3180. {
  3181. __free_page(spd->pages[idx]);
  3182. }
  3183. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  3184. .can_merge = 0,
  3185. .map = generic_pipe_buf_map,
  3186. .unmap = generic_pipe_buf_unmap,
  3187. .confirm = generic_pipe_buf_confirm,
  3188. .release = tracing_pipe_buf_release,
  3189. .steal = generic_pipe_buf_steal,
  3190. .get = generic_pipe_buf_get,
  3191. };
  3192. static size_t
  3193. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  3194. {
  3195. size_t count;
  3196. int ret;
  3197. /* Seq buffer is page-sized, exactly what we need. */
  3198. for (;;) {
  3199. count = iter->seq.len;
  3200. ret = print_trace_line(iter);
  3201. count = iter->seq.len - count;
  3202. if (rem < count) {
  3203. rem = 0;
  3204. iter->seq.len -= count;
  3205. break;
  3206. }
  3207. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3208. iter->seq.len -= count;
  3209. break;
  3210. }
  3211. if (ret != TRACE_TYPE_NO_CONSUME)
  3212. trace_consume(iter);
  3213. rem -= count;
  3214. if (!trace_find_next_entry_inc(iter)) {
  3215. rem = 0;
  3216. iter->ent = NULL;
  3217. break;
  3218. }
  3219. }
  3220. return rem;
  3221. }
  3222. static ssize_t tracing_splice_read_pipe(struct file *filp,
  3223. loff_t *ppos,
  3224. struct pipe_inode_info *pipe,
  3225. size_t len,
  3226. unsigned int flags)
  3227. {
  3228. struct page *pages_def[PIPE_DEF_BUFFERS];
  3229. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3230. struct trace_iterator *iter = filp->private_data;
  3231. struct splice_pipe_desc spd = {
  3232. .pages = pages_def,
  3233. .partial = partial_def,
  3234. .nr_pages = 0, /* This gets updated below. */
  3235. .nr_pages_max = PIPE_DEF_BUFFERS,
  3236. .flags = flags,
  3237. .ops = &tracing_pipe_buf_ops,
  3238. .spd_release = tracing_spd_release_pipe,
  3239. };
  3240. struct trace_array *tr = iter->tr;
  3241. ssize_t ret;
  3242. size_t rem;
  3243. unsigned int i;
  3244. if (splice_grow_spd(pipe, &spd))
  3245. return -ENOMEM;
  3246. /* copy the tracer to avoid using a global lock all around */
  3247. mutex_lock(&trace_types_lock);
  3248. if (unlikely(iter->trace->name != tr->current_trace->name))
  3249. *iter->trace = *tr->current_trace;
  3250. mutex_unlock(&trace_types_lock);
  3251. mutex_lock(&iter->mutex);
  3252. if (iter->trace->splice_read) {
  3253. ret = iter->trace->splice_read(iter, filp,
  3254. ppos, pipe, len, flags);
  3255. if (ret)
  3256. goto out_err;
  3257. }
  3258. ret = tracing_wait_pipe(filp);
  3259. if (ret <= 0)
  3260. goto out_err;
  3261. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  3262. ret = -EFAULT;
  3263. goto out_err;
  3264. }
  3265. trace_event_read_lock();
  3266. trace_access_lock(iter->cpu_file);
  3267. /* Fill as many pages as possible. */
  3268. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  3269. spd.pages[i] = alloc_page(GFP_KERNEL);
  3270. if (!spd.pages[i])
  3271. break;
  3272. rem = tracing_fill_pipe_page(rem, iter);
  3273. /* Copy the data into the page, so we can start over. */
  3274. ret = trace_seq_to_buffer(&iter->seq,
  3275. page_address(spd.pages[i]),
  3276. iter->seq.len);
  3277. if (ret < 0) {
  3278. __free_page(spd.pages[i]);
  3279. break;
  3280. }
  3281. spd.partial[i].offset = 0;
  3282. spd.partial[i].len = iter->seq.len;
  3283. trace_seq_init(&iter->seq);
  3284. }
  3285. trace_access_unlock(iter->cpu_file);
  3286. trace_event_read_unlock();
  3287. mutex_unlock(&iter->mutex);
  3288. spd.nr_pages = i;
  3289. ret = splice_to_pipe(pipe, &spd);
  3290. out:
  3291. splice_shrink_spd(&spd);
  3292. return ret;
  3293. out_err:
  3294. mutex_unlock(&iter->mutex);
  3295. goto out;
  3296. }
  3297. static ssize_t
  3298. tracing_entries_read(struct file *filp, char __user *ubuf,
  3299. size_t cnt, loff_t *ppos)
  3300. {
  3301. struct trace_cpu *tc = filp->private_data;
  3302. struct trace_array *tr = tc->tr;
  3303. char buf[64];
  3304. int r = 0;
  3305. ssize_t ret;
  3306. mutex_lock(&trace_types_lock);
  3307. if (tc->cpu == RING_BUFFER_ALL_CPUS) {
  3308. int cpu, buf_size_same;
  3309. unsigned long size;
  3310. size = 0;
  3311. buf_size_same = 1;
  3312. /* check if all cpu sizes are same */
  3313. for_each_tracing_cpu(cpu) {
  3314. /* fill in the size from first enabled cpu */
  3315. if (size == 0)
  3316. size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
  3317. if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
  3318. buf_size_same = 0;
  3319. break;
  3320. }
  3321. }
  3322. if (buf_size_same) {
  3323. if (!ring_buffer_expanded)
  3324. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3325. size >> 10,
  3326. trace_buf_size >> 10);
  3327. else
  3328. r = sprintf(buf, "%lu\n", size >> 10);
  3329. } else
  3330. r = sprintf(buf, "X\n");
  3331. } else
  3332. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
  3333. mutex_unlock(&trace_types_lock);
  3334. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3335. return ret;
  3336. }
  3337. static ssize_t
  3338. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3339. size_t cnt, loff_t *ppos)
  3340. {
  3341. struct trace_cpu *tc = filp->private_data;
  3342. unsigned long val;
  3343. int ret;
  3344. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3345. if (ret)
  3346. return ret;
  3347. /* must have at least 1 entry */
  3348. if (!val)
  3349. return -EINVAL;
  3350. /* value is in KB */
  3351. val <<= 10;
  3352. ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
  3353. if (ret < 0)
  3354. return ret;
  3355. *ppos += cnt;
  3356. return cnt;
  3357. }
  3358. static ssize_t
  3359. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3360. size_t cnt, loff_t *ppos)
  3361. {
  3362. struct trace_array *tr = filp->private_data;
  3363. char buf[64];
  3364. int r, cpu;
  3365. unsigned long size = 0, expanded_size = 0;
  3366. mutex_lock(&trace_types_lock);
  3367. for_each_tracing_cpu(cpu) {
  3368. size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
  3369. if (!ring_buffer_expanded)
  3370. expanded_size += trace_buf_size >> 10;
  3371. }
  3372. if (ring_buffer_expanded)
  3373. r = sprintf(buf, "%lu\n", size);
  3374. else
  3375. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3376. mutex_unlock(&trace_types_lock);
  3377. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3378. }
  3379. static ssize_t
  3380. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3381. size_t cnt, loff_t *ppos)
  3382. {
  3383. /*
  3384. * There is no need to read what the user has written, this function
  3385. * is just to make sure that there is no error when "echo" is used
  3386. */
  3387. *ppos += cnt;
  3388. return cnt;
  3389. }
  3390. static int
  3391. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3392. {
  3393. struct trace_array *tr = inode->i_private;
  3394. /* disable tracing ? */
  3395. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3396. tracing_off();
  3397. /* resize the ring buffer to 0 */
  3398. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  3399. return 0;
  3400. }
  3401. static ssize_t
  3402. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3403. size_t cnt, loff_t *fpos)
  3404. {
  3405. unsigned long addr = (unsigned long)ubuf;
  3406. struct ring_buffer_event *event;
  3407. struct ring_buffer *buffer;
  3408. struct print_entry *entry;
  3409. unsigned long irq_flags;
  3410. struct page *pages[2];
  3411. void *map_page[2];
  3412. int nr_pages = 1;
  3413. ssize_t written;
  3414. int offset;
  3415. int size;
  3416. int len;
  3417. int ret;
  3418. int i;
  3419. if (tracing_disabled)
  3420. return -EINVAL;
  3421. if (!(trace_flags & TRACE_ITER_MARKERS))
  3422. return -EINVAL;
  3423. if (cnt > TRACE_BUF_SIZE)
  3424. cnt = TRACE_BUF_SIZE;
  3425. /*
  3426. * Userspace is injecting traces into the kernel trace buffer.
  3427. * We want to be as non intrusive as possible.
  3428. * To do so, we do not want to allocate any special buffers
  3429. * or take any locks, but instead write the userspace data
  3430. * straight into the ring buffer.
  3431. *
  3432. * First we need to pin the userspace buffer into memory,
  3433. * which, most likely it is, because it just referenced it.
  3434. * But there's no guarantee that it is. By using get_user_pages_fast()
  3435. * and kmap_atomic/kunmap_atomic() we can get access to the
  3436. * pages directly. We then write the data directly into the
  3437. * ring buffer.
  3438. */
  3439. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3440. /* check if we cross pages */
  3441. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3442. nr_pages = 2;
  3443. offset = addr & (PAGE_SIZE - 1);
  3444. addr &= PAGE_MASK;
  3445. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3446. if (ret < nr_pages) {
  3447. while (--ret >= 0)
  3448. put_page(pages[ret]);
  3449. written = -EFAULT;
  3450. goto out;
  3451. }
  3452. for (i = 0; i < nr_pages; i++)
  3453. map_page[i] = kmap_atomic(pages[i]);
  3454. local_save_flags(irq_flags);
  3455. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3456. buffer = global_trace.trace_buffer.buffer;
  3457. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3458. irq_flags, preempt_count());
  3459. if (!event) {
  3460. /* Ring buffer disabled, return as if not open for write */
  3461. written = -EBADF;
  3462. goto out_unlock;
  3463. }
  3464. entry = ring_buffer_event_data(event);
  3465. entry->ip = _THIS_IP_;
  3466. if (nr_pages == 2) {
  3467. len = PAGE_SIZE - offset;
  3468. memcpy(&entry->buf, map_page[0] + offset, len);
  3469. memcpy(&entry->buf[len], map_page[1], cnt - len);
  3470. } else
  3471. memcpy(&entry->buf, map_page[0] + offset, cnt);
  3472. if (entry->buf[cnt - 1] != '\n') {
  3473. entry->buf[cnt] = '\n';
  3474. entry->buf[cnt + 1] = '\0';
  3475. } else
  3476. entry->buf[cnt] = '\0';
  3477. __buffer_unlock_commit(buffer, event);
  3478. written = cnt;
  3479. *fpos += written;
  3480. out_unlock:
  3481. for (i = 0; i < nr_pages; i++){
  3482. kunmap_atomic(map_page[i]);
  3483. put_page(pages[i]);
  3484. }
  3485. out:
  3486. return written;
  3487. }
  3488. static int tracing_clock_show(struct seq_file *m, void *v)
  3489. {
  3490. struct trace_array *tr = m->private;
  3491. int i;
  3492. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3493. seq_printf(m,
  3494. "%s%s%s%s", i ? " " : "",
  3495. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  3496. i == tr->clock_id ? "]" : "");
  3497. seq_putc(m, '\n');
  3498. return 0;
  3499. }
  3500. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3501. size_t cnt, loff_t *fpos)
  3502. {
  3503. struct seq_file *m = filp->private_data;
  3504. struct trace_array *tr = m->private;
  3505. char buf[64];
  3506. const char *clockstr;
  3507. int i;
  3508. if (cnt >= sizeof(buf))
  3509. return -EINVAL;
  3510. if (copy_from_user(&buf, ubuf, cnt))
  3511. return -EFAULT;
  3512. buf[cnt] = 0;
  3513. clockstr = strstrip(buf);
  3514. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3515. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3516. break;
  3517. }
  3518. if (i == ARRAY_SIZE(trace_clocks))
  3519. return -EINVAL;
  3520. mutex_lock(&trace_types_lock);
  3521. tr->clock_id = i;
  3522. ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
  3523. /*
  3524. * New clock may not be consistent with the previous clock.
  3525. * Reset the buffer so that it doesn't have incomparable timestamps.
  3526. */
  3527. tracing_reset_online_cpus(&global_trace.trace_buffer);
  3528. #ifdef CONFIG_TRACER_MAX_TRACE
  3529. if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
  3530. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  3531. tracing_reset_online_cpus(&global_trace.max_buffer);
  3532. #endif
  3533. mutex_unlock(&trace_types_lock);
  3534. *fpos += cnt;
  3535. return cnt;
  3536. }
  3537. static int tracing_clock_open(struct inode *inode, struct file *file)
  3538. {
  3539. if (tracing_disabled)
  3540. return -ENODEV;
  3541. return single_open(file, tracing_clock_show, inode->i_private);
  3542. }
  3543. struct ftrace_buffer_info {
  3544. struct trace_iterator iter;
  3545. void *spare;
  3546. unsigned int read;
  3547. };
  3548. #ifdef CONFIG_TRACER_SNAPSHOT
  3549. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  3550. {
  3551. struct trace_cpu *tc = inode->i_private;
  3552. struct trace_iterator *iter;
  3553. struct seq_file *m;
  3554. int ret = 0;
  3555. if (file->f_mode & FMODE_READ) {
  3556. iter = __tracing_open(inode, file, true);
  3557. if (IS_ERR(iter))
  3558. ret = PTR_ERR(iter);
  3559. } else {
  3560. /* Writes still need the seq_file to hold the private data */
  3561. m = kzalloc(sizeof(*m), GFP_KERNEL);
  3562. if (!m)
  3563. return -ENOMEM;
  3564. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3565. if (!iter) {
  3566. kfree(m);
  3567. return -ENOMEM;
  3568. }
  3569. iter->tr = tc->tr;
  3570. iter->trace_buffer = &tc->tr->max_buffer;
  3571. iter->cpu_file = tc->cpu;
  3572. m->private = iter;
  3573. file->private_data = m;
  3574. }
  3575. return ret;
  3576. }
  3577. static ssize_t
  3578. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3579. loff_t *ppos)
  3580. {
  3581. struct seq_file *m = filp->private_data;
  3582. struct trace_iterator *iter = m->private;
  3583. struct trace_array *tr = iter->tr;
  3584. unsigned long val;
  3585. int ret;
  3586. ret = tracing_update_buffers();
  3587. if (ret < 0)
  3588. return ret;
  3589. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3590. if (ret)
  3591. return ret;
  3592. mutex_lock(&trace_types_lock);
  3593. if (tr->current_trace->use_max_tr) {
  3594. ret = -EBUSY;
  3595. goto out;
  3596. }
  3597. switch (val) {
  3598. case 0:
  3599. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3600. ret = -EINVAL;
  3601. break;
  3602. }
  3603. if (tr->allocated_snapshot) {
  3604. /* free spare buffer */
  3605. ring_buffer_resize(tr->max_buffer.buffer, 1,
  3606. RING_BUFFER_ALL_CPUS);
  3607. set_buffer_entries(&tr->max_buffer, 1);
  3608. tracing_reset_online_cpus(&tr->max_buffer);
  3609. tr->allocated_snapshot = false;
  3610. }
  3611. break;
  3612. case 1:
  3613. /* Only allow per-cpu swap if the ring buffer supports it */
  3614. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  3615. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3616. ret = -EINVAL;
  3617. break;
  3618. }
  3619. #endif
  3620. if (!tr->allocated_snapshot) {
  3621. /* allocate spare buffer */
  3622. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  3623. &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
  3624. if (ret < 0)
  3625. break;
  3626. tr->allocated_snapshot = true;
  3627. }
  3628. local_irq_disable();
  3629. /* Now, we're going to swap */
  3630. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3631. update_max_tr(tr, current, smp_processor_id());
  3632. else
  3633. update_max_tr_single(tr, current, iter->cpu_file);
  3634. local_irq_enable();
  3635. break;
  3636. default:
  3637. if (tr->allocated_snapshot) {
  3638. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3639. tracing_reset_online_cpus(&tr->max_buffer);
  3640. else
  3641. tracing_reset(&tr->max_buffer, iter->cpu_file);
  3642. }
  3643. break;
  3644. }
  3645. if (ret >= 0) {
  3646. *ppos += cnt;
  3647. ret = cnt;
  3648. }
  3649. out:
  3650. mutex_unlock(&trace_types_lock);
  3651. return ret;
  3652. }
  3653. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  3654. {
  3655. struct seq_file *m = file->private_data;
  3656. if (file->f_mode & FMODE_READ)
  3657. return tracing_release(inode, file);
  3658. /* If write only, the seq_file is just a stub */
  3659. if (m)
  3660. kfree(m->private);
  3661. kfree(m);
  3662. return 0;
  3663. }
  3664. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  3665. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  3666. size_t count, loff_t *ppos);
  3667. static int tracing_buffers_release(struct inode *inode, struct file *file);
  3668. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3669. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  3670. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  3671. {
  3672. struct ftrace_buffer_info *info;
  3673. int ret;
  3674. ret = tracing_buffers_open(inode, filp);
  3675. if (ret < 0)
  3676. return ret;
  3677. info = filp->private_data;
  3678. if (info->iter.trace->use_max_tr) {
  3679. tracing_buffers_release(inode, filp);
  3680. return -EBUSY;
  3681. }
  3682. info->iter.snapshot = true;
  3683. info->iter.trace_buffer = &info->iter.tr->max_buffer;
  3684. return ret;
  3685. }
  3686. #endif /* CONFIG_TRACER_SNAPSHOT */
  3687. static const struct file_operations tracing_max_lat_fops = {
  3688. .open = tracing_open_generic,
  3689. .read = tracing_max_lat_read,
  3690. .write = tracing_max_lat_write,
  3691. .llseek = generic_file_llseek,
  3692. };
  3693. static const struct file_operations set_tracer_fops = {
  3694. .open = tracing_open_generic,
  3695. .read = tracing_set_trace_read,
  3696. .write = tracing_set_trace_write,
  3697. .llseek = generic_file_llseek,
  3698. };
  3699. static const struct file_operations tracing_pipe_fops = {
  3700. .open = tracing_open_pipe,
  3701. .poll = tracing_poll_pipe,
  3702. .read = tracing_read_pipe,
  3703. .splice_read = tracing_splice_read_pipe,
  3704. .release = tracing_release_pipe,
  3705. .llseek = no_llseek,
  3706. };
  3707. static const struct file_operations tracing_entries_fops = {
  3708. .open = tracing_open_generic,
  3709. .read = tracing_entries_read,
  3710. .write = tracing_entries_write,
  3711. .llseek = generic_file_llseek,
  3712. };
  3713. static const struct file_operations tracing_total_entries_fops = {
  3714. .open = tracing_open_generic,
  3715. .read = tracing_total_entries_read,
  3716. .llseek = generic_file_llseek,
  3717. };
  3718. static const struct file_operations tracing_free_buffer_fops = {
  3719. .write = tracing_free_buffer_write,
  3720. .release = tracing_free_buffer_release,
  3721. };
  3722. static const struct file_operations tracing_mark_fops = {
  3723. .open = tracing_open_generic,
  3724. .write = tracing_mark_write,
  3725. .llseek = generic_file_llseek,
  3726. };
  3727. static const struct file_operations trace_clock_fops = {
  3728. .open = tracing_clock_open,
  3729. .read = seq_read,
  3730. .llseek = seq_lseek,
  3731. .release = single_release,
  3732. .write = tracing_clock_write,
  3733. };
  3734. #ifdef CONFIG_TRACER_SNAPSHOT
  3735. static const struct file_operations snapshot_fops = {
  3736. .open = tracing_snapshot_open,
  3737. .read = seq_read,
  3738. .write = tracing_snapshot_write,
  3739. .llseek = tracing_seek,
  3740. .release = tracing_snapshot_release,
  3741. };
  3742. static const struct file_operations snapshot_raw_fops = {
  3743. .open = snapshot_raw_open,
  3744. .read = tracing_buffers_read,
  3745. .release = tracing_buffers_release,
  3746. .splice_read = tracing_buffers_splice_read,
  3747. .llseek = no_llseek,
  3748. };
  3749. #endif /* CONFIG_TRACER_SNAPSHOT */
  3750. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  3751. {
  3752. struct trace_cpu *tc = inode->i_private;
  3753. struct trace_array *tr = tc->tr;
  3754. struct ftrace_buffer_info *info;
  3755. if (tracing_disabled)
  3756. return -ENODEV;
  3757. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3758. if (!info)
  3759. return -ENOMEM;
  3760. mutex_lock(&trace_types_lock);
  3761. tr->ref++;
  3762. info->iter.tr = tr;
  3763. info->iter.cpu_file = tc->cpu;
  3764. info->iter.trace = tr->current_trace;
  3765. info->iter.trace_buffer = &tr->trace_buffer;
  3766. info->spare = NULL;
  3767. /* Force reading ring buffer for first read */
  3768. info->read = (unsigned int)-1;
  3769. filp->private_data = info;
  3770. mutex_unlock(&trace_types_lock);
  3771. return nonseekable_open(inode, filp);
  3772. }
  3773. static unsigned int
  3774. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  3775. {
  3776. struct ftrace_buffer_info *info = filp->private_data;
  3777. struct trace_iterator *iter = &info->iter;
  3778. return trace_poll(iter, filp, poll_table);
  3779. }
  3780. static ssize_t
  3781. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3782. size_t count, loff_t *ppos)
  3783. {
  3784. struct ftrace_buffer_info *info = filp->private_data;
  3785. struct trace_iterator *iter = &info->iter;
  3786. ssize_t ret;
  3787. ssize_t size;
  3788. if (!count)
  3789. return 0;
  3790. mutex_lock(&trace_types_lock);
  3791. #ifdef CONFIG_TRACER_MAX_TRACE
  3792. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  3793. size = -EBUSY;
  3794. goto out_unlock;
  3795. }
  3796. #endif
  3797. if (!info->spare)
  3798. info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
  3799. iter->cpu_file);
  3800. size = -ENOMEM;
  3801. if (!info->spare)
  3802. goto out_unlock;
  3803. /* Do we have previous read data to read? */
  3804. if (info->read < PAGE_SIZE)
  3805. goto read;
  3806. again:
  3807. trace_access_lock(iter->cpu_file);
  3808. ret = ring_buffer_read_page(iter->trace_buffer->buffer,
  3809. &info->spare,
  3810. count,
  3811. iter->cpu_file, 0);
  3812. trace_access_unlock(iter->cpu_file);
  3813. if (ret < 0) {
  3814. if (trace_empty(iter)) {
  3815. if ((filp->f_flags & O_NONBLOCK)) {
  3816. size = -EAGAIN;
  3817. goto out_unlock;
  3818. }
  3819. mutex_unlock(&trace_types_lock);
  3820. iter->trace->wait_pipe(iter);
  3821. mutex_lock(&trace_types_lock);
  3822. if (signal_pending(current)) {
  3823. size = -EINTR;
  3824. goto out_unlock;
  3825. }
  3826. goto again;
  3827. }
  3828. size = 0;
  3829. goto out_unlock;
  3830. }
  3831. info->read = 0;
  3832. read:
  3833. size = PAGE_SIZE - info->read;
  3834. if (size > count)
  3835. size = count;
  3836. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3837. if (ret == size) {
  3838. size = -EFAULT;
  3839. goto out_unlock;
  3840. }
  3841. size -= ret;
  3842. *ppos += size;
  3843. info->read += size;
  3844. out_unlock:
  3845. mutex_unlock(&trace_types_lock);
  3846. return size;
  3847. }
  3848. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3849. {
  3850. struct ftrace_buffer_info *info = file->private_data;
  3851. struct trace_iterator *iter = &info->iter;
  3852. mutex_lock(&trace_types_lock);
  3853. WARN_ON(!iter->tr->ref);
  3854. iter->tr->ref--;
  3855. if (info->spare)
  3856. ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
  3857. kfree(info);
  3858. mutex_unlock(&trace_types_lock);
  3859. return 0;
  3860. }
  3861. struct buffer_ref {
  3862. struct ring_buffer *buffer;
  3863. void *page;
  3864. int ref;
  3865. };
  3866. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3867. struct pipe_buffer *buf)
  3868. {
  3869. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3870. if (--ref->ref)
  3871. return;
  3872. ring_buffer_free_read_page(ref->buffer, ref->page);
  3873. kfree(ref);
  3874. buf->private = 0;
  3875. }
  3876. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3877. struct pipe_buffer *buf)
  3878. {
  3879. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3880. ref->ref++;
  3881. }
  3882. /* Pipe buffer operations for a buffer. */
  3883. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3884. .can_merge = 0,
  3885. .map = generic_pipe_buf_map,
  3886. .unmap = generic_pipe_buf_unmap,
  3887. .confirm = generic_pipe_buf_confirm,
  3888. .release = buffer_pipe_buf_release,
  3889. .steal = generic_pipe_buf_steal,
  3890. .get = buffer_pipe_buf_get,
  3891. };
  3892. /*
  3893. * Callback from splice_to_pipe(), if we need to release some pages
  3894. * at the end of the spd in case we error'ed out in filling the pipe.
  3895. */
  3896. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3897. {
  3898. struct buffer_ref *ref =
  3899. (struct buffer_ref *)spd->partial[i].private;
  3900. if (--ref->ref)
  3901. return;
  3902. ring_buffer_free_read_page(ref->buffer, ref->page);
  3903. kfree(ref);
  3904. spd->partial[i].private = 0;
  3905. }
  3906. static ssize_t
  3907. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3908. struct pipe_inode_info *pipe, size_t len,
  3909. unsigned int flags)
  3910. {
  3911. struct ftrace_buffer_info *info = file->private_data;
  3912. struct trace_iterator *iter = &info->iter;
  3913. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3914. struct page *pages_def[PIPE_DEF_BUFFERS];
  3915. struct splice_pipe_desc spd = {
  3916. .pages = pages_def,
  3917. .partial = partial_def,
  3918. .nr_pages_max = PIPE_DEF_BUFFERS,
  3919. .flags = flags,
  3920. .ops = &buffer_pipe_buf_ops,
  3921. .spd_release = buffer_spd_release,
  3922. };
  3923. struct buffer_ref *ref;
  3924. int entries, size, i;
  3925. ssize_t ret;
  3926. mutex_lock(&trace_types_lock);
  3927. #ifdef CONFIG_TRACER_MAX_TRACE
  3928. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  3929. ret = -EBUSY;
  3930. goto out;
  3931. }
  3932. #endif
  3933. if (splice_grow_spd(pipe, &spd)) {
  3934. ret = -ENOMEM;
  3935. goto out;
  3936. }
  3937. if (*ppos & (PAGE_SIZE - 1)) {
  3938. ret = -EINVAL;
  3939. goto out;
  3940. }
  3941. if (len & (PAGE_SIZE - 1)) {
  3942. if (len < PAGE_SIZE) {
  3943. ret = -EINVAL;
  3944. goto out;
  3945. }
  3946. len &= PAGE_MASK;
  3947. }
  3948. again:
  3949. trace_access_lock(iter->cpu_file);
  3950. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  3951. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  3952. struct page *page;
  3953. int r;
  3954. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  3955. if (!ref)
  3956. break;
  3957. ref->ref = 1;
  3958. ref->buffer = iter->trace_buffer->buffer;
  3959. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  3960. if (!ref->page) {
  3961. kfree(ref);
  3962. break;
  3963. }
  3964. r = ring_buffer_read_page(ref->buffer, &ref->page,
  3965. len, iter->cpu_file, 1);
  3966. if (r < 0) {
  3967. ring_buffer_free_read_page(ref->buffer, ref->page);
  3968. kfree(ref);
  3969. break;
  3970. }
  3971. /*
  3972. * zero out any left over data, this is going to
  3973. * user land.
  3974. */
  3975. size = ring_buffer_page_len(ref->page);
  3976. if (size < PAGE_SIZE)
  3977. memset(ref->page + size, 0, PAGE_SIZE - size);
  3978. page = virt_to_page(ref->page);
  3979. spd.pages[i] = page;
  3980. spd.partial[i].len = PAGE_SIZE;
  3981. spd.partial[i].offset = 0;
  3982. spd.partial[i].private = (unsigned long)ref;
  3983. spd.nr_pages++;
  3984. *ppos += PAGE_SIZE;
  3985. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  3986. }
  3987. trace_access_unlock(iter->cpu_file);
  3988. spd.nr_pages = i;
  3989. /* did we read anything? */
  3990. if (!spd.nr_pages) {
  3991. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
  3992. ret = -EAGAIN;
  3993. goto out;
  3994. }
  3995. mutex_unlock(&trace_types_lock);
  3996. iter->trace->wait_pipe(iter);
  3997. mutex_lock(&trace_types_lock);
  3998. if (signal_pending(current)) {
  3999. ret = -EINTR;
  4000. goto out;
  4001. }
  4002. goto again;
  4003. }
  4004. ret = splice_to_pipe(pipe, &spd);
  4005. splice_shrink_spd(&spd);
  4006. out:
  4007. mutex_unlock(&trace_types_lock);
  4008. return ret;
  4009. }
  4010. static const struct file_operations tracing_buffers_fops = {
  4011. .open = tracing_buffers_open,
  4012. .read = tracing_buffers_read,
  4013. .poll = tracing_buffers_poll,
  4014. .release = tracing_buffers_release,
  4015. .splice_read = tracing_buffers_splice_read,
  4016. .llseek = no_llseek,
  4017. };
  4018. static ssize_t
  4019. tracing_stats_read(struct file *filp, char __user *ubuf,
  4020. size_t count, loff_t *ppos)
  4021. {
  4022. struct trace_cpu *tc = filp->private_data;
  4023. struct trace_array *tr = tc->tr;
  4024. struct trace_buffer *trace_buf = &tr->trace_buffer;
  4025. struct trace_seq *s;
  4026. unsigned long cnt;
  4027. unsigned long long t;
  4028. unsigned long usec_rem;
  4029. int cpu = tc->cpu;
  4030. s = kmalloc(sizeof(*s), GFP_KERNEL);
  4031. if (!s)
  4032. return -ENOMEM;
  4033. trace_seq_init(s);
  4034. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  4035. trace_seq_printf(s, "entries: %ld\n", cnt);
  4036. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  4037. trace_seq_printf(s, "overrun: %ld\n", cnt);
  4038. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  4039. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  4040. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  4041. trace_seq_printf(s, "bytes: %ld\n", cnt);
  4042. if (trace_clocks[trace_clock_id].in_ns) {
  4043. /* local or global for trace_clock */
  4044. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4045. usec_rem = do_div(t, USEC_PER_SEC);
  4046. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  4047. t, usec_rem);
  4048. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4049. usec_rem = do_div(t, USEC_PER_SEC);
  4050. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  4051. } else {
  4052. /* counter or tsc mode for trace_clock */
  4053. trace_seq_printf(s, "oldest event ts: %llu\n",
  4054. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4055. trace_seq_printf(s, "now ts: %llu\n",
  4056. ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4057. }
  4058. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  4059. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  4060. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  4061. trace_seq_printf(s, "read events: %ld\n", cnt);
  4062. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  4063. kfree(s);
  4064. return count;
  4065. }
  4066. static const struct file_operations tracing_stats_fops = {
  4067. .open = tracing_open_generic,
  4068. .read = tracing_stats_read,
  4069. .llseek = generic_file_llseek,
  4070. };
  4071. #ifdef CONFIG_DYNAMIC_FTRACE
  4072. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  4073. {
  4074. return 0;
  4075. }
  4076. static ssize_t
  4077. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  4078. size_t cnt, loff_t *ppos)
  4079. {
  4080. static char ftrace_dyn_info_buffer[1024];
  4081. static DEFINE_MUTEX(dyn_info_mutex);
  4082. unsigned long *p = filp->private_data;
  4083. char *buf = ftrace_dyn_info_buffer;
  4084. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  4085. int r;
  4086. mutex_lock(&dyn_info_mutex);
  4087. r = sprintf(buf, "%ld ", *p);
  4088. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  4089. buf[r++] = '\n';
  4090. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4091. mutex_unlock(&dyn_info_mutex);
  4092. return r;
  4093. }
  4094. static const struct file_operations tracing_dyn_info_fops = {
  4095. .open = tracing_open_generic,
  4096. .read = tracing_read_dyn_info,
  4097. .llseek = generic_file_llseek,
  4098. };
  4099. #endif
  4100. struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
  4101. {
  4102. static int once;
  4103. if (tr->dir)
  4104. return tr->dir;
  4105. if (!debugfs_initialized())
  4106. return NULL;
  4107. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  4108. tr->dir = debugfs_create_dir("tracing", NULL);
  4109. if (!tr->dir && !once) {
  4110. once = 1;
  4111. pr_warning("Could not create debugfs directory 'tracing'\n");
  4112. return NULL;
  4113. }
  4114. return tr->dir;
  4115. }
  4116. struct dentry *tracing_init_dentry(void)
  4117. {
  4118. return tracing_init_dentry_tr(&global_trace);
  4119. }
  4120. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  4121. {
  4122. struct dentry *d_tracer;
  4123. if (tr->percpu_dir)
  4124. return tr->percpu_dir;
  4125. d_tracer = tracing_init_dentry_tr(tr);
  4126. if (!d_tracer)
  4127. return NULL;
  4128. tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
  4129. WARN_ONCE(!tr->percpu_dir,
  4130. "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
  4131. return tr->percpu_dir;
  4132. }
  4133. static void
  4134. tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
  4135. {
  4136. struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  4137. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  4138. struct dentry *d_cpu;
  4139. char cpu_dir[30]; /* 30 characters should be more than enough */
  4140. if (!d_percpu)
  4141. return;
  4142. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  4143. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  4144. if (!d_cpu) {
  4145. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  4146. return;
  4147. }
  4148. /* per cpu trace_pipe */
  4149. trace_create_file("trace_pipe", 0444, d_cpu,
  4150. (void *)&data->trace_cpu, &tracing_pipe_fops);
  4151. /* per cpu trace */
  4152. trace_create_file("trace", 0644, d_cpu,
  4153. (void *)&data->trace_cpu, &tracing_fops);
  4154. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  4155. (void *)&data->trace_cpu, &tracing_buffers_fops);
  4156. trace_create_file("stats", 0444, d_cpu,
  4157. (void *)&data->trace_cpu, &tracing_stats_fops);
  4158. trace_create_file("buffer_size_kb", 0444, d_cpu,
  4159. (void *)&data->trace_cpu, &tracing_entries_fops);
  4160. #ifdef CONFIG_TRACER_SNAPSHOT
  4161. trace_create_file("snapshot", 0644, d_cpu,
  4162. (void *)&data->trace_cpu, &snapshot_fops);
  4163. trace_create_file("snapshot_raw", 0444, d_cpu,
  4164. (void *)&data->trace_cpu, &snapshot_raw_fops);
  4165. #endif
  4166. }
  4167. #ifdef CONFIG_FTRACE_SELFTEST
  4168. /* Let selftest have access to static functions in this file */
  4169. #include "trace_selftest.c"
  4170. #endif
  4171. struct trace_option_dentry {
  4172. struct tracer_opt *opt;
  4173. struct tracer_flags *flags;
  4174. struct trace_array *tr;
  4175. struct dentry *entry;
  4176. };
  4177. static ssize_t
  4178. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  4179. loff_t *ppos)
  4180. {
  4181. struct trace_option_dentry *topt = filp->private_data;
  4182. char *buf;
  4183. if (topt->flags->val & topt->opt->bit)
  4184. buf = "1\n";
  4185. else
  4186. buf = "0\n";
  4187. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4188. }
  4189. static ssize_t
  4190. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4191. loff_t *ppos)
  4192. {
  4193. struct trace_option_dentry *topt = filp->private_data;
  4194. unsigned long val;
  4195. int ret;
  4196. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4197. if (ret)
  4198. return ret;
  4199. if (val != 0 && val != 1)
  4200. return -EINVAL;
  4201. if (!!(topt->flags->val & topt->opt->bit) != val) {
  4202. mutex_lock(&trace_types_lock);
  4203. ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
  4204. topt->opt, !val);
  4205. mutex_unlock(&trace_types_lock);
  4206. if (ret)
  4207. return ret;
  4208. }
  4209. *ppos += cnt;
  4210. return cnt;
  4211. }
  4212. static const struct file_operations trace_options_fops = {
  4213. .open = tracing_open_generic,
  4214. .read = trace_options_read,
  4215. .write = trace_options_write,
  4216. .llseek = generic_file_llseek,
  4217. };
  4218. static ssize_t
  4219. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  4220. loff_t *ppos)
  4221. {
  4222. long index = (long)filp->private_data;
  4223. char *buf;
  4224. if (trace_flags & (1 << index))
  4225. buf = "1\n";
  4226. else
  4227. buf = "0\n";
  4228. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4229. }
  4230. static ssize_t
  4231. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4232. loff_t *ppos)
  4233. {
  4234. struct trace_array *tr = &global_trace;
  4235. long index = (long)filp->private_data;
  4236. unsigned long val;
  4237. int ret;
  4238. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4239. if (ret)
  4240. return ret;
  4241. if (val != 0 && val != 1)
  4242. return -EINVAL;
  4243. mutex_lock(&trace_types_lock);
  4244. ret = set_tracer_flag(tr, 1 << index, val);
  4245. mutex_unlock(&trace_types_lock);
  4246. if (ret < 0)
  4247. return ret;
  4248. *ppos += cnt;
  4249. return cnt;
  4250. }
  4251. static const struct file_operations trace_options_core_fops = {
  4252. .open = tracing_open_generic,
  4253. .read = trace_options_core_read,
  4254. .write = trace_options_core_write,
  4255. .llseek = generic_file_llseek,
  4256. };
  4257. struct dentry *trace_create_file(const char *name,
  4258. umode_t mode,
  4259. struct dentry *parent,
  4260. void *data,
  4261. const struct file_operations *fops)
  4262. {
  4263. struct dentry *ret;
  4264. ret = debugfs_create_file(name, mode, parent, data, fops);
  4265. if (!ret)
  4266. pr_warning("Could not create debugfs '%s' entry\n", name);
  4267. return ret;
  4268. }
  4269. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  4270. {
  4271. struct dentry *d_tracer;
  4272. if (tr->options)
  4273. return tr->options;
  4274. d_tracer = tracing_init_dentry_tr(tr);
  4275. if (!d_tracer)
  4276. return NULL;
  4277. tr->options = debugfs_create_dir("options", d_tracer);
  4278. if (!tr->options) {
  4279. pr_warning("Could not create debugfs directory 'options'\n");
  4280. return NULL;
  4281. }
  4282. return tr->options;
  4283. }
  4284. static void
  4285. create_trace_option_file(struct trace_array *tr,
  4286. struct trace_option_dentry *topt,
  4287. struct tracer_flags *flags,
  4288. struct tracer_opt *opt)
  4289. {
  4290. struct dentry *t_options;
  4291. t_options = trace_options_init_dentry(tr);
  4292. if (!t_options)
  4293. return;
  4294. topt->flags = flags;
  4295. topt->opt = opt;
  4296. topt->tr = tr;
  4297. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  4298. &trace_options_fops);
  4299. }
  4300. static struct trace_option_dentry *
  4301. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  4302. {
  4303. struct trace_option_dentry *topts;
  4304. struct tracer_flags *flags;
  4305. struct tracer_opt *opts;
  4306. int cnt;
  4307. if (!tracer)
  4308. return NULL;
  4309. flags = tracer->flags;
  4310. if (!flags || !flags->opts)
  4311. return NULL;
  4312. opts = flags->opts;
  4313. for (cnt = 0; opts[cnt].name; cnt++)
  4314. ;
  4315. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  4316. if (!topts)
  4317. return NULL;
  4318. for (cnt = 0; opts[cnt].name; cnt++)
  4319. create_trace_option_file(tr, &topts[cnt], flags,
  4320. &opts[cnt]);
  4321. return topts;
  4322. }
  4323. static void
  4324. destroy_trace_option_files(struct trace_option_dentry *topts)
  4325. {
  4326. int cnt;
  4327. if (!topts)
  4328. return;
  4329. for (cnt = 0; topts[cnt].opt; cnt++) {
  4330. if (topts[cnt].entry)
  4331. debugfs_remove(topts[cnt].entry);
  4332. }
  4333. kfree(topts);
  4334. }
  4335. static struct dentry *
  4336. create_trace_option_core_file(struct trace_array *tr,
  4337. const char *option, long index)
  4338. {
  4339. struct dentry *t_options;
  4340. t_options = trace_options_init_dentry(tr);
  4341. if (!t_options)
  4342. return NULL;
  4343. return trace_create_file(option, 0644, t_options, (void *)index,
  4344. &trace_options_core_fops);
  4345. }
  4346. static __init void create_trace_options_dir(struct trace_array *tr)
  4347. {
  4348. struct dentry *t_options;
  4349. int i;
  4350. t_options = trace_options_init_dentry(tr);
  4351. if (!t_options)
  4352. return;
  4353. for (i = 0; trace_options[i]; i++)
  4354. create_trace_option_core_file(tr, trace_options[i], i);
  4355. }
  4356. static ssize_t
  4357. rb_simple_read(struct file *filp, char __user *ubuf,
  4358. size_t cnt, loff_t *ppos)
  4359. {
  4360. struct trace_array *tr = filp->private_data;
  4361. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4362. char buf[64];
  4363. int r;
  4364. if (buffer)
  4365. r = ring_buffer_record_is_on(buffer);
  4366. else
  4367. r = 0;
  4368. r = sprintf(buf, "%d\n", r);
  4369. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4370. }
  4371. static ssize_t
  4372. rb_simple_write(struct file *filp, const char __user *ubuf,
  4373. size_t cnt, loff_t *ppos)
  4374. {
  4375. struct trace_array *tr = filp->private_data;
  4376. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4377. unsigned long val;
  4378. int ret;
  4379. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4380. if (ret)
  4381. return ret;
  4382. if (buffer) {
  4383. mutex_lock(&trace_types_lock);
  4384. if (val) {
  4385. ring_buffer_record_on(buffer);
  4386. if (tr->current_trace->start)
  4387. tr->current_trace->start(tr);
  4388. } else {
  4389. ring_buffer_record_off(buffer);
  4390. if (tr->current_trace->stop)
  4391. tr->current_trace->stop(tr);
  4392. }
  4393. mutex_unlock(&trace_types_lock);
  4394. }
  4395. (*ppos)++;
  4396. return cnt;
  4397. }
  4398. static const struct file_operations rb_simple_fops = {
  4399. .open = tracing_open_generic,
  4400. .read = rb_simple_read,
  4401. .write = rb_simple_write,
  4402. .llseek = default_llseek,
  4403. };
  4404. struct dentry *trace_instance_dir;
  4405. static void
  4406. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
  4407. static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
  4408. {
  4409. int cpu;
  4410. for_each_tracing_cpu(cpu) {
  4411. memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
  4412. per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
  4413. per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
  4414. }
  4415. }
  4416. static int allocate_trace_buffers(struct trace_array *tr, int size)
  4417. {
  4418. enum ring_buffer_flags rb_flags;
  4419. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4420. tr->trace_buffer.buffer = ring_buffer_alloc(size, rb_flags);
  4421. if (!tr->trace_buffer.buffer)
  4422. goto out_free;
  4423. tr->trace_buffer.data = alloc_percpu(struct trace_array_cpu);
  4424. if (!tr->trace_buffer.data)
  4425. goto out_free;
  4426. init_trace_buffers(tr, &tr->trace_buffer);
  4427. /* Allocate the first page for all buffers */
  4428. set_buffer_entries(&tr->trace_buffer,
  4429. ring_buffer_size(tr->trace_buffer.buffer, 0));
  4430. #ifdef CONFIG_TRACER_MAX_TRACE
  4431. tr->max_buffer.buffer = ring_buffer_alloc(1, rb_flags);
  4432. if (!tr->max_buffer.buffer)
  4433. goto out_free;
  4434. tr->max_buffer.data = alloc_percpu(struct trace_array_cpu);
  4435. if (!tr->max_buffer.data)
  4436. goto out_free;
  4437. init_trace_buffers(tr, &tr->max_buffer);
  4438. set_buffer_entries(&tr->max_buffer, 1);
  4439. #endif
  4440. return 0;
  4441. out_free:
  4442. if (tr->trace_buffer.buffer)
  4443. ring_buffer_free(tr->trace_buffer.buffer);
  4444. free_percpu(tr->trace_buffer.data);
  4445. #ifdef CONFIG_TRACER_MAX_TRACE
  4446. if (tr->max_buffer.buffer)
  4447. ring_buffer_free(tr->max_buffer.buffer);
  4448. free_percpu(tr->max_buffer.data);
  4449. #endif
  4450. return -ENOMEM;
  4451. }
  4452. static int new_instance_create(const char *name)
  4453. {
  4454. struct trace_array *tr;
  4455. int ret;
  4456. mutex_lock(&trace_types_lock);
  4457. ret = -EEXIST;
  4458. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4459. if (tr->name && strcmp(tr->name, name) == 0)
  4460. goto out_unlock;
  4461. }
  4462. ret = -ENOMEM;
  4463. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  4464. if (!tr)
  4465. goto out_unlock;
  4466. tr->name = kstrdup(name, GFP_KERNEL);
  4467. if (!tr->name)
  4468. goto out_free_tr;
  4469. raw_spin_lock_init(&tr->start_lock);
  4470. tr->current_trace = &nop_trace;
  4471. INIT_LIST_HEAD(&tr->systems);
  4472. INIT_LIST_HEAD(&tr->events);
  4473. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  4474. goto out_free_tr;
  4475. /* Holder for file callbacks */
  4476. tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  4477. tr->trace_cpu.tr = tr;
  4478. tr->dir = debugfs_create_dir(name, trace_instance_dir);
  4479. if (!tr->dir)
  4480. goto out_free_tr;
  4481. ret = event_trace_add_tracer(tr->dir, tr);
  4482. if (ret)
  4483. goto out_free_tr;
  4484. init_tracer_debugfs(tr, tr->dir);
  4485. list_add(&tr->list, &ftrace_trace_arrays);
  4486. mutex_unlock(&trace_types_lock);
  4487. return 0;
  4488. out_free_tr:
  4489. if (tr->trace_buffer.buffer)
  4490. ring_buffer_free(tr->trace_buffer.buffer);
  4491. kfree(tr->name);
  4492. kfree(tr);
  4493. out_unlock:
  4494. mutex_unlock(&trace_types_lock);
  4495. return ret;
  4496. }
  4497. static int instance_delete(const char *name)
  4498. {
  4499. struct trace_array *tr;
  4500. int found = 0;
  4501. int ret;
  4502. mutex_lock(&trace_types_lock);
  4503. ret = -ENODEV;
  4504. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4505. if (tr->name && strcmp(tr->name, name) == 0) {
  4506. found = 1;
  4507. break;
  4508. }
  4509. }
  4510. if (!found)
  4511. goto out_unlock;
  4512. ret = -EBUSY;
  4513. if (tr->ref)
  4514. goto out_unlock;
  4515. list_del(&tr->list);
  4516. event_trace_del_tracer(tr);
  4517. debugfs_remove_recursive(tr->dir);
  4518. free_percpu(tr->trace_buffer.data);
  4519. ring_buffer_free(tr->trace_buffer.buffer);
  4520. kfree(tr->name);
  4521. kfree(tr);
  4522. ret = 0;
  4523. out_unlock:
  4524. mutex_unlock(&trace_types_lock);
  4525. return ret;
  4526. }
  4527. static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
  4528. {
  4529. struct dentry *parent;
  4530. int ret;
  4531. /* Paranoid: Make sure the parent is the "instances" directory */
  4532. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4533. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4534. return -ENOENT;
  4535. /*
  4536. * The inode mutex is locked, but debugfs_create_dir() will also
  4537. * take the mutex. As the instances directory can not be destroyed
  4538. * or changed in any other way, it is safe to unlock it, and
  4539. * let the dentry try. If two users try to make the same dir at
  4540. * the same time, then the new_instance_create() will determine the
  4541. * winner.
  4542. */
  4543. mutex_unlock(&inode->i_mutex);
  4544. ret = new_instance_create(dentry->d_iname);
  4545. mutex_lock(&inode->i_mutex);
  4546. return ret;
  4547. }
  4548. static int instance_rmdir(struct inode *inode, struct dentry *dentry)
  4549. {
  4550. struct dentry *parent;
  4551. int ret;
  4552. /* Paranoid: Make sure the parent is the "instances" directory */
  4553. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4554. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4555. return -ENOENT;
  4556. /* The caller did a dget() on dentry */
  4557. mutex_unlock(&dentry->d_inode->i_mutex);
  4558. /*
  4559. * The inode mutex is locked, but debugfs_create_dir() will also
  4560. * take the mutex. As the instances directory can not be destroyed
  4561. * or changed in any other way, it is safe to unlock it, and
  4562. * let the dentry try. If two users try to make the same dir at
  4563. * the same time, then the instance_delete() will determine the
  4564. * winner.
  4565. */
  4566. mutex_unlock(&inode->i_mutex);
  4567. ret = instance_delete(dentry->d_iname);
  4568. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  4569. mutex_lock(&dentry->d_inode->i_mutex);
  4570. return ret;
  4571. }
  4572. static const struct inode_operations instance_dir_inode_operations = {
  4573. .lookup = simple_lookup,
  4574. .mkdir = instance_mkdir,
  4575. .rmdir = instance_rmdir,
  4576. };
  4577. static __init void create_trace_instances(struct dentry *d_tracer)
  4578. {
  4579. trace_instance_dir = debugfs_create_dir("instances", d_tracer);
  4580. if (WARN_ON(!trace_instance_dir))
  4581. return;
  4582. /* Hijack the dir inode operations, to allow mkdir */
  4583. trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
  4584. }
  4585. static void
  4586. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
  4587. {
  4588. int cpu;
  4589. trace_create_file("trace_options", 0644, d_tracer,
  4590. tr, &tracing_iter_fops);
  4591. trace_create_file("trace", 0644, d_tracer,
  4592. (void *)&tr->trace_cpu, &tracing_fops);
  4593. trace_create_file("trace_pipe", 0444, d_tracer,
  4594. (void *)&tr->trace_cpu, &tracing_pipe_fops);
  4595. trace_create_file("buffer_size_kb", 0644, d_tracer,
  4596. (void *)&tr->trace_cpu, &tracing_entries_fops);
  4597. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  4598. tr, &tracing_total_entries_fops);
  4599. trace_create_file("free_buffer", 0644, d_tracer,
  4600. tr, &tracing_free_buffer_fops);
  4601. trace_create_file("trace_marker", 0220, d_tracer,
  4602. tr, &tracing_mark_fops);
  4603. trace_create_file("trace_clock", 0644, d_tracer, tr,
  4604. &trace_clock_fops);
  4605. trace_create_file("tracing_on", 0644, d_tracer,
  4606. tr, &rb_simple_fops);
  4607. #ifdef CONFIG_TRACER_SNAPSHOT
  4608. trace_create_file("snapshot", 0644, d_tracer,
  4609. (void *)&tr->trace_cpu, &snapshot_fops);
  4610. #endif
  4611. for_each_tracing_cpu(cpu)
  4612. tracing_init_debugfs_percpu(tr, cpu);
  4613. }
  4614. static __init int tracer_init_debugfs(void)
  4615. {
  4616. struct dentry *d_tracer;
  4617. trace_access_lock_init();
  4618. d_tracer = tracing_init_dentry();
  4619. init_tracer_debugfs(&global_trace, d_tracer);
  4620. trace_create_file("tracing_cpumask", 0644, d_tracer,
  4621. &global_trace, &tracing_cpumask_fops);
  4622. trace_create_file("available_tracers", 0444, d_tracer,
  4623. &global_trace, &show_traces_fops);
  4624. trace_create_file("current_tracer", 0644, d_tracer,
  4625. &global_trace, &set_tracer_fops);
  4626. #ifdef CONFIG_TRACER_MAX_TRACE
  4627. trace_create_file("tracing_max_latency", 0644, d_tracer,
  4628. &tracing_max_latency, &tracing_max_lat_fops);
  4629. #endif
  4630. trace_create_file("tracing_thresh", 0644, d_tracer,
  4631. &tracing_thresh, &tracing_max_lat_fops);
  4632. trace_create_file("README", 0444, d_tracer,
  4633. NULL, &tracing_readme_fops);
  4634. trace_create_file("saved_cmdlines", 0444, d_tracer,
  4635. NULL, &tracing_saved_cmdlines_fops);
  4636. #ifdef CONFIG_DYNAMIC_FTRACE
  4637. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  4638. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  4639. #endif
  4640. create_trace_instances(d_tracer);
  4641. create_trace_options_dir(&global_trace);
  4642. return 0;
  4643. }
  4644. static int trace_panic_handler(struct notifier_block *this,
  4645. unsigned long event, void *unused)
  4646. {
  4647. if (ftrace_dump_on_oops)
  4648. ftrace_dump(ftrace_dump_on_oops);
  4649. return NOTIFY_OK;
  4650. }
  4651. static struct notifier_block trace_panic_notifier = {
  4652. .notifier_call = trace_panic_handler,
  4653. .next = NULL,
  4654. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  4655. };
  4656. static int trace_die_handler(struct notifier_block *self,
  4657. unsigned long val,
  4658. void *data)
  4659. {
  4660. switch (val) {
  4661. case DIE_OOPS:
  4662. if (ftrace_dump_on_oops)
  4663. ftrace_dump(ftrace_dump_on_oops);
  4664. break;
  4665. default:
  4666. break;
  4667. }
  4668. return NOTIFY_OK;
  4669. }
  4670. static struct notifier_block trace_die_notifier = {
  4671. .notifier_call = trace_die_handler,
  4672. .priority = 200
  4673. };
  4674. /*
  4675. * printk is set to max of 1024, we really don't need it that big.
  4676. * Nothing should be printing 1000 characters anyway.
  4677. */
  4678. #define TRACE_MAX_PRINT 1000
  4679. /*
  4680. * Define here KERN_TRACE so that we have one place to modify
  4681. * it if we decide to change what log level the ftrace dump
  4682. * should be at.
  4683. */
  4684. #define KERN_TRACE KERN_EMERG
  4685. void
  4686. trace_printk_seq(struct trace_seq *s)
  4687. {
  4688. /* Probably should print a warning here. */
  4689. if (s->len >= 1000)
  4690. s->len = 1000;
  4691. /* should be zero ended, but we are paranoid. */
  4692. s->buffer[s->len] = 0;
  4693. printk(KERN_TRACE "%s", s->buffer);
  4694. trace_seq_init(s);
  4695. }
  4696. void trace_init_global_iter(struct trace_iterator *iter)
  4697. {
  4698. iter->tr = &global_trace;
  4699. iter->trace = iter->tr->current_trace;
  4700. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  4701. iter->trace_buffer = &global_trace.trace_buffer;
  4702. }
  4703. static void
  4704. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
  4705. {
  4706. static arch_spinlock_t ftrace_dump_lock =
  4707. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  4708. /* use static because iter can be a bit big for the stack */
  4709. static struct trace_iterator iter;
  4710. unsigned int old_userobj;
  4711. static int dump_ran;
  4712. unsigned long flags;
  4713. int cnt = 0, cpu;
  4714. /* only one dump */
  4715. local_irq_save(flags);
  4716. arch_spin_lock(&ftrace_dump_lock);
  4717. if (dump_ran)
  4718. goto out;
  4719. dump_ran = 1;
  4720. tracing_off();
  4721. /* Did function tracer already get disabled? */
  4722. if (ftrace_is_dead()) {
  4723. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  4724. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  4725. }
  4726. if (disable_tracing)
  4727. ftrace_kill();
  4728. /* Simulate the iterator */
  4729. trace_init_global_iter(&iter);
  4730. for_each_tracing_cpu(cpu) {
  4731. atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
  4732. }
  4733. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  4734. /* don't look at user memory in panic mode */
  4735. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  4736. switch (oops_dump_mode) {
  4737. case DUMP_ALL:
  4738. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4739. break;
  4740. case DUMP_ORIG:
  4741. iter.cpu_file = raw_smp_processor_id();
  4742. break;
  4743. case DUMP_NONE:
  4744. goto out_enable;
  4745. default:
  4746. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  4747. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  4748. }
  4749. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  4750. /*
  4751. * We need to stop all tracing on all CPUS to read the
  4752. * the next buffer. This is a bit expensive, but is
  4753. * not done often. We fill all what we can read,
  4754. * and then release the locks again.
  4755. */
  4756. while (!trace_empty(&iter)) {
  4757. if (!cnt)
  4758. printk(KERN_TRACE "---------------------------------\n");
  4759. cnt++;
  4760. /* reset all but tr, trace, and overruns */
  4761. memset(&iter.seq, 0,
  4762. sizeof(struct trace_iterator) -
  4763. offsetof(struct trace_iterator, seq));
  4764. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  4765. iter.pos = -1;
  4766. if (trace_find_next_entry_inc(&iter) != NULL) {
  4767. int ret;
  4768. ret = print_trace_line(&iter);
  4769. if (ret != TRACE_TYPE_NO_CONSUME)
  4770. trace_consume(&iter);
  4771. }
  4772. touch_nmi_watchdog();
  4773. trace_printk_seq(&iter.seq);
  4774. }
  4775. if (!cnt)
  4776. printk(KERN_TRACE " (ftrace buffer empty)\n");
  4777. else
  4778. printk(KERN_TRACE "---------------------------------\n");
  4779. out_enable:
  4780. /* Re-enable tracing if requested */
  4781. if (!disable_tracing) {
  4782. trace_flags |= old_userobj;
  4783. for_each_tracing_cpu(cpu) {
  4784. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  4785. }
  4786. tracing_on();
  4787. }
  4788. out:
  4789. arch_spin_unlock(&ftrace_dump_lock);
  4790. local_irq_restore(flags);
  4791. }
  4792. /* By default: disable tracing after the dump */
  4793. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  4794. {
  4795. __ftrace_dump(true, oops_dump_mode);
  4796. }
  4797. EXPORT_SYMBOL_GPL(ftrace_dump);
  4798. __init static int tracer_alloc_buffers(void)
  4799. {
  4800. int ring_buf_size;
  4801. int ret = -ENOMEM;
  4802. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  4803. goto out;
  4804. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  4805. goto out_free_buffer_mask;
  4806. /* Only allocate trace_printk buffers if a trace_printk exists */
  4807. if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
  4808. /* Must be called before global_trace.buffer is allocated */
  4809. trace_printk_init_buffers();
  4810. /* To save memory, keep the ring buffer size to its minimum */
  4811. if (ring_buffer_expanded)
  4812. ring_buf_size = trace_buf_size;
  4813. else
  4814. ring_buf_size = 1;
  4815. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  4816. cpumask_copy(tracing_cpumask, cpu_all_mask);
  4817. raw_spin_lock_init(&global_trace.start_lock);
  4818. /* TODO: make the number of buffers hot pluggable with CPUS */
  4819. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  4820. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  4821. WARN_ON(1);
  4822. goto out_free_cpumask;
  4823. }
  4824. if (global_trace.buffer_disabled)
  4825. tracing_off();
  4826. trace_init_cmdlines();
  4827. register_tracer(&nop_trace);
  4828. global_trace.current_trace = &nop_trace;
  4829. /* All seems OK, enable tracing */
  4830. tracing_disabled = 0;
  4831. atomic_notifier_chain_register(&panic_notifier_list,
  4832. &trace_panic_notifier);
  4833. register_die_notifier(&trace_die_notifier);
  4834. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  4835. /* Holder for file callbacks */
  4836. global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
  4837. global_trace.trace_cpu.tr = &global_trace;
  4838. INIT_LIST_HEAD(&global_trace.systems);
  4839. INIT_LIST_HEAD(&global_trace.events);
  4840. list_add(&global_trace.list, &ftrace_trace_arrays);
  4841. while (trace_boot_options) {
  4842. char *option;
  4843. option = strsep(&trace_boot_options, ",");
  4844. trace_set_options(&global_trace, option);
  4845. }
  4846. return 0;
  4847. out_free_cpumask:
  4848. free_percpu(global_trace.trace_buffer.data);
  4849. #ifdef CONFIG_TRACER_MAX_TRACE
  4850. free_percpu(global_trace.max_buffer.data);
  4851. #endif
  4852. free_cpumask_var(tracing_cpumask);
  4853. out_free_buffer_mask:
  4854. free_cpumask_var(tracing_buffer_mask);
  4855. out:
  4856. return ret;
  4857. }
  4858. __init static int clear_boot_tracer(void)
  4859. {
  4860. /*
  4861. * The default tracer at boot buffer is an init section.
  4862. * This function is called in lateinit. If we did not
  4863. * find the boot tracer, then clear it out, to prevent
  4864. * later registration from accessing the buffer that is
  4865. * about to be freed.
  4866. */
  4867. if (!default_bootup_tracer)
  4868. return 0;
  4869. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  4870. default_bootup_tracer);
  4871. default_bootup_tracer = NULL;
  4872. return 0;
  4873. }
  4874. early_initcall(tracer_alloc_buffers);
  4875. fs_initcall(tracer_init_debugfs);
  4876. late_initcall(clear_boot_tracer);