trace.c 153 KB

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