trace.c 174 KB

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