trace.c 145 KB

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