trace.c 204 KB

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