trace.c 170 KB

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