trace.c 154 KB

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