trace.c 154 KB

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