trace.c 254 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ring buffer based function tracer
  4. *
  5. * Copyright (C) 2007-2012 Steven Rostedt <[email protected]>
  6. * Copyright (C) 2008 Ingo Molnar <[email protected]>
  7. *
  8. * Originally taken from the RT patch by:
  9. * Arnaldo Carvalho de Melo <[email protected]>
  10. *
  11. * Based on code from the latency_tracer, that is:
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 Nadia Yvette Chambers
  14. */
  15. #include <linux/ring_buffer.h>
  16. #include <generated/utsrelease.h>
  17. #include <linux/stacktrace.h>
  18. #include <linux/writeback.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/security.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/notifier.h>
  23. #include <linux/irqflags.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/tracefs.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/linkage.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/ftrace.h>
  32. #include <linux/module.h>
  33. #include <linux/percpu.h>
  34. #include <linux/splice.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/string.h>
  37. #include <linux/mount.h>
  38. #include <linux/rwsem.h>
  39. #include <linux/slab.h>
  40. #include <linux/ctype.h>
  41. #include <linux/init.h>
  42. #include <linux/panic_notifier.h>
  43. #include <linux/poll.h>
  44. #include <linux/nmi.h>
  45. #include <linux/fs.h>
  46. #include <linux/trace.h>
  47. #include <linux/sched/clock.h>
  48. #include <linux/sched/rt.h>
  49. #include <linux/fsnotify.h>
  50. #include <linux/irq_work.h>
  51. #include <linux/workqueue.h>
  52. #include <trace/hooks/ftrace_dump.h>
  53. #include "trace.h"
  54. #include "trace_output.h"
  55. /*
  56. * On boot up, the ring buffer is set to the minimum size, so that
  57. * we do not waste memory on systems that are not using tracing.
  58. */
  59. bool ring_buffer_expanded;
  60. /*
  61. * We need to change this state when a selftest is running.
  62. * A selftest will lurk into the ring-buffer to count the
  63. * entries inserted during the selftest although some concurrent
  64. * insertions into the ring-buffer such as trace_printk could occurred
  65. * at the same time, giving false positive or negative results.
  66. */
  67. static bool __read_mostly tracing_selftest_running;
  68. /*
  69. * If boot-time tracing including tracers/events via kernel cmdline
  70. * is running, we do not want to run SELFTEST.
  71. */
  72. bool __read_mostly tracing_selftest_disabled;
  73. #ifdef CONFIG_FTRACE_STARTUP_TEST
  74. void __init disable_tracing_selftest(const char *reason)
  75. {
  76. if (!tracing_selftest_disabled) {
  77. tracing_selftest_disabled = true;
  78. pr_info("Ftrace startup test is disabled due to %s\n", reason);
  79. }
  80. }
  81. #endif
  82. /* Pipe tracepoints to printk */
  83. struct trace_iterator *tracepoint_print_iter;
  84. int tracepoint_printk;
  85. static bool tracepoint_printk_stop_on_boot __initdata;
  86. static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
  87. /* For tracers that don't implement custom flags */
  88. static struct tracer_opt dummy_tracer_opt[] = {
  89. { }
  90. };
  91. static int
  92. dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  93. {
  94. return 0;
  95. }
  96. /*
  97. * To prevent the comm cache from being overwritten when no
  98. * tracing is active, only save the comm when a trace event
  99. * occurred.
  100. */
  101. static DEFINE_PER_CPU(bool, trace_taskinfo_save);
  102. /*
  103. * Kill all tracing for good (never come back).
  104. * It is initialized to 1 but will turn to zero if the initialization
  105. * of the tracer is successful. But that is the only place that sets
  106. * this back to zero.
  107. */
  108. static int tracing_disabled = 1;
  109. cpumask_var_t __read_mostly tracing_buffer_mask;
  110. /*
  111. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  112. *
  113. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  114. * is set, then ftrace_dump is called. This will output the contents
  115. * of the ftrace buffers to the console. This is very useful for
  116. * capturing traces that lead to crashes and outputing it to a
  117. * serial console.
  118. *
  119. * It is default off, but you can enable it with either specifying
  120. * "ftrace_dump_on_oops" in the kernel command line, or setting
  121. * /proc/sys/kernel/ftrace_dump_on_oops
  122. * Set 1 if you want to dump buffers of all CPUs
  123. * Set 2 if you want to dump the buffer of the CPU that triggered oops
  124. */
  125. enum ftrace_dump_mode ftrace_dump_on_oops;
  126. /* When set, tracing will stop when a WARN*() is hit */
  127. int __disable_trace_on_warning;
  128. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  129. /* Map of enums to their values, for "eval_map" file */
  130. struct trace_eval_map_head {
  131. struct module *mod;
  132. unsigned long length;
  133. };
  134. union trace_eval_map_item;
  135. struct trace_eval_map_tail {
  136. /*
  137. * "end" is first and points to NULL as it must be different
  138. * than "mod" or "eval_string"
  139. */
  140. union trace_eval_map_item *next;
  141. const char *end; /* points to NULL */
  142. };
  143. static DEFINE_MUTEX(trace_eval_mutex);
  144. /*
  145. * The trace_eval_maps are saved in an array with two extra elements,
  146. * one at the beginning, and one at the end. The beginning item contains
  147. * the count of the saved maps (head.length), and the module they
  148. * belong to if not built in (head.mod). The ending item contains a
  149. * pointer to the next array of saved eval_map items.
  150. */
  151. union trace_eval_map_item {
  152. struct trace_eval_map map;
  153. struct trace_eval_map_head head;
  154. struct trace_eval_map_tail tail;
  155. };
  156. static union trace_eval_map_item *trace_eval_maps;
  157. #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
  158. int tracing_set_tracer(struct trace_array *tr, const char *buf);
  159. static void ftrace_trace_userstack(struct trace_array *tr,
  160. struct trace_buffer *buffer,
  161. unsigned int trace_ctx);
  162. #define MAX_TRACER_SIZE 100
  163. static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
  164. static char *default_bootup_tracer;
  165. static bool allocate_snapshot;
  166. static bool snapshot_at_boot;
  167. static int __init set_cmdline_ftrace(char *str)
  168. {
  169. strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
  170. default_bootup_tracer = bootup_tracer_buf;
  171. /* We are using ftrace early, expand it */
  172. ring_buffer_expanded = true;
  173. return 1;
  174. }
  175. __setup("ftrace=", set_cmdline_ftrace);
  176. static int __init set_ftrace_dump_on_oops(char *str)
  177. {
  178. if (*str++ != '=' || !*str || !strcmp("1", str)) {
  179. ftrace_dump_on_oops = DUMP_ALL;
  180. return 1;
  181. }
  182. if (!strcmp("orig_cpu", str) || !strcmp("2", str)) {
  183. ftrace_dump_on_oops = DUMP_ORIG;
  184. return 1;
  185. }
  186. return 0;
  187. }
  188. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  189. static int __init stop_trace_on_warning(char *str)
  190. {
  191. if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
  192. __disable_trace_on_warning = 1;
  193. return 1;
  194. }
  195. __setup("traceoff_on_warning", stop_trace_on_warning);
  196. static int __init boot_alloc_snapshot(char *str)
  197. {
  198. allocate_snapshot = true;
  199. /* We also need the main ring buffer expanded */
  200. ring_buffer_expanded = true;
  201. return 1;
  202. }
  203. __setup("alloc_snapshot", boot_alloc_snapshot);
  204. static int __init boot_snapshot(char *str)
  205. {
  206. snapshot_at_boot = true;
  207. boot_alloc_snapshot(str);
  208. return 1;
  209. }
  210. __setup("ftrace_boot_snapshot", boot_snapshot);
  211. static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
  212. static int __init set_trace_boot_options(char *str)
  213. {
  214. strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
  215. return 1;
  216. }
  217. __setup("trace_options=", set_trace_boot_options);
  218. static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
  219. static char *trace_boot_clock __initdata;
  220. static int __init set_trace_boot_clock(char *str)
  221. {
  222. strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
  223. trace_boot_clock = trace_boot_clock_buf;
  224. return 1;
  225. }
  226. __setup("trace_clock=", set_trace_boot_clock);
  227. static int __init set_tracepoint_printk(char *str)
  228. {
  229. /* Ignore the "tp_printk_stop_on_boot" param */
  230. if (*str == '_')
  231. return 0;
  232. if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
  233. tracepoint_printk = 1;
  234. return 1;
  235. }
  236. __setup("tp_printk", set_tracepoint_printk);
  237. static int __init set_tracepoint_printk_stop(char *str)
  238. {
  239. tracepoint_printk_stop_on_boot = true;
  240. return 1;
  241. }
  242. __setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop);
  243. unsigned long long ns2usecs(u64 nsec)
  244. {
  245. nsec += 500;
  246. do_div(nsec, 1000);
  247. return nsec;
  248. }
  249. static void
  250. trace_process_export(struct trace_export *export,
  251. struct ring_buffer_event *event, int flag)
  252. {
  253. struct trace_entry *entry;
  254. unsigned int size = 0;
  255. if (export->flags & flag) {
  256. entry = ring_buffer_event_data(event);
  257. size = ring_buffer_event_length(event);
  258. export->write(export, entry, size);
  259. }
  260. }
  261. static DEFINE_MUTEX(ftrace_export_lock);
  262. static struct trace_export __rcu *ftrace_exports_list __read_mostly;
  263. static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
  264. static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
  265. static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
  266. static inline void ftrace_exports_enable(struct trace_export *export)
  267. {
  268. if (export->flags & TRACE_EXPORT_FUNCTION)
  269. static_branch_inc(&trace_function_exports_enabled);
  270. if (export->flags & TRACE_EXPORT_EVENT)
  271. static_branch_inc(&trace_event_exports_enabled);
  272. if (export->flags & TRACE_EXPORT_MARKER)
  273. static_branch_inc(&trace_marker_exports_enabled);
  274. }
  275. static inline void ftrace_exports_disable(struct trace_export *export)
  276. {
  277. if (export->flags & TRACE_EXPORT_FUNCTION)
  278. static_branch_dec(&trace_function_exports_enabled);
  279. if (export->flags & TRACE_EXPORT_EVENT)
  280. static_branch_dec(&trace_event_exports_enabled);
  281. if (export->flags & TRACE_EXPORT_MARKER)
  282. static_branch_dec(&trace_marker_exports_enabled);
  283. }
  284. static void ftrace_exports(struct ring_buffer_event *event, int flag)
  285. {
  286. struct trace_export *export;
  287. preempt_disable_notrace();
  288. export = rcu_dereference_raw_check(ftrace_exports_list);
  289. while (export) {
  290. trace_process_export(export, event, flag);
  291. export = rcu_dereference_raw_check(export->next);
  292. }
  293. preempt_enable_notrace();
  294. }
  295. static inline void
  296. add_trace_export(struct trace_export **list, struct trace_export *export)
  297. {
  298. rcu_assign_pointer(export->next, *list);
  299. /*
  300. * We are entering export into the list but another
  301. * CPU might be walking that list. We need to make sure
  302. * the export->next pointer is valid before another CPU sees
  303. * the export pointer included into the list.
  304. */
  305. rcu_assign_pointer(*list, export);
  306. }
  307. static inline int
  308. rm_trace_export(struct trace_export **list, struct trace_export *export)
  309. {
  310. struct trace_export **p;
  311. for (p = list; *p != NULL; p = &(*p)->next)
  312. if (*p == export)
  313. break;
  314. if (*p != export)
  315. return -1;
  316. rcu_assign_pointer(*p, (*p)->next);
  317. return 0;
  318. }
  319. static inline void
  320. add_ftrace_export(struct trace_export **list, struct trace_export *export)
  321. {
  322. ftrace_exports_enable(export);
  323. add_trace_export(list, export);
  324. }
  325. static inline int
  326. rm_ftrace_export(struct trace_export **list, struct trace_export *export)
  327. {
  328. int ret;
  329. ret = rm_trace_export(list, export);
  330. ftrace_exports_disable(export);
  331. return ret;
  332. }
  333. int register_ftrace_export(struct trace_export *export)
  334. {
  335. if (WARN_ON_ONCE(!export->write))
  336. return -1;
  337. mutex_lock(&ftrace_export_lock);
  338. add_ftrace_export(&ftrace_exports_list, export);
  339. mutex_unlock(&ftrace_export_lock);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(register_ftrace_export);
  343. int unregister_ftrace_export(struct trace_export *export)
  344. {
  345. int ret;
  346. mutex_lock(&ftrace_export_lock);
  347. ret = rm_ftrace_export(&ftrace_exports_list, export);
  348. mutex_unlock(&ftrace_export_lock);
  349. return ret;
  350. }
  351. EXPORT_SYMBOL_GPL(unregister_ftrace_export);
  352. /* trace_flags holds trace_options default values */
  353. #define TRACE_DEFAULT_FLAGS \
  354. (FUNCTION_DEFAULT_FLAGS | \
  355. TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
  356. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
  357. TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
  358. TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \
  359. TRACE_ITER_HASH_PTR)
  360. /* trace_options that are only supported by global_trace */
  361. #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
  362. TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
  363. /* trace_flags that are default zero for instances */
  364. #define ZEROED_TRACE_FLAGS \
  365. (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
  366. /*
  367. * The global_trace is the descriptor that holds the top-level tracing
  368. * buffers for the live tracing.
  369. */
  370. static struct trace_array global_trace = {
  371. .trace_flags = TRACE_DEFAULT_FLAGS,
  372. };
  373. LIST_HEAD(ftrace_trace_arrays);
  374. int trace_array_get(struct trace_array *this_tr)
  375. {
  376. struct trace_array *tr;
  377. int ret = -ENODEV;
  378. mutex_lock(&trace_types_lock);
  379. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  380. if (tr == this_tr) {
  381. tr->ref++;
  382. ret = 0;
  383. break;
  384. }
  385. }
  386. mutex_unlock(&trace_types_lock);
  387. return ret;
  388. }
  389. static void __trace_array_put(struct trace_array *this_tr)
  390. {
  391. WARN_ON(!this_tr->ref);
  392. this_tr->ref--;
  393. }
  394. /**
  395. * trace_array_put - Decrement the reference counter for this trace array.
  396. * @this_tr : pointer to the trace array
  397. *
  398. * NOTE: Use this when we no longer need the trace array returned by
  399. * trace_array_get_by_name(). This ensures the trace array can be later
  400. * destroyed.
  401. *
  402. */
  403. void trace_array_put(struct trace_array *this_tr)
  404. {
  405. if (!this_tr)
  406. return;
  407. mutex_lock(&trace_types_lock);
  408. __trace_array_put(this_tr);
  409. mutex_unlock(&trace_types_lock);
  410. }
  411. EXPORT_SYMBOL_GPL(trace_array_put);
  412. int tracing_check_open_get_tr(struct trace_array *tr)
  413. {
  414. int ret;
  415. ret = security_locked_down(LOCKDOWN_TRACEFS);
  416. if (ret)
  417. return ret;
  418. if (tracing_disabled)
  419. return -ENODEV;
  420. if (tr && trace_array_get(tr) < 0)
  421. return -ENODEV;
  422. return 0;
  423. }
  424. int call_filter_check_discard(struct trace_event_call *call, void *rec,
  425. struct trace_buffer *buffer,
  426. struct ring_buffer_event *event)
  427. {
  428. if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
  429. !filter_match_preds(call->filter, rec)) {
  430. __trace_event_discard_commit(buffer, event);
  431. return 1;
  432. }
  433. return 0;
  434. }
  435. /**
  436. * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
  437. * @filtered_pids: The list of pids to check
  438. * @search_pid: The PID to find in @filtered_pids
  439. *
  440. * Returns true if @search_pid is found in @filtered_pids, and false otherwise.
  441. */
  442. bool
  443. trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
  444. {
  445. return trace_pid_list_is_set(filtered_pids, search_pid);
  446. }
  447. /**
  448. * trace_ignore_this_task - should a task be ignored for tracing
  449. * @filtered_pids: The list of pids to check
  450. * @filtered_no_pids: The list of pids not to be traced
  451. * @task: The task that should be ignored if not filtered
  452. *
  453. * Checks if @task should be traced or not from @filtered_pids.
  454. * Returns true if @task should *NOT* be traced.
  455. * Returns false if @task should be traced.
  456. */
  457. bool
  458. trace_ignore_this_task(struct trace_pid_list *filtered_pids,
  459. struct trace_pid_list *filtered_no_pids,
  460. struct task_struct *task)
  461. {
  462. /*
  463. * If filtered_no_pids is not empty, and the task's pid is listed
  464. * in filtered_no_pids, then return true.
  465. * Otherwise, if filtered_pids is empty, that means we can
  466. * trace all tasks. If it has content, then only trace pids
  467. * within filtered_pids.
  468. */
  469. return (filtered_pids &&
  470. !trace_find_filtered_pid(filtered_pids, task->pid)) ||
  471. (filtered_no_pids &&
  472. trace_find_filtered_pid(filtered_no_pids, task->pid));
  473. }
  474. /**
  475. * trace_filter_add_remove_task - Add or remove a task from a pid_list
  476. * @pid_list: The list to modify
  477. * @self: The current task for fork or NULL for exit
  478. * @task: The task to add or remove
  479. *
  480. * If adding a task, if @self is defined, the task is only added if @self
  481. * is also included in @pid_list. This happens on fork and tasks should
  482. * only be added when the parent is listed. If @self is NULL, then the
  483. * @task pid will be removed from the list, which would happen on exit
  484. * of a task.
  485. */
  486. void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
  487. struct task_struct *self,
  488. struct task_struct *task)
  489. {
  490. if (!pid_list)
  491. return;
  492. /* For forks, we only add if the forking task is listed */
  493. if (self) {
  494. if (!trace_find_filtered_pid(pid_list, self->pid))
  495. return;
  496. }
  497. /* "self" is set for forks, and NULL for exits */
  498. if (self)
  499. trace_pid_list_set(pid_list, task->pid);
  500. else
  501. trace_pid_list_clear(pid_list, task->pid);
  502. }
  503. /**
  504. * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
  505. * @pid_list: The pid list to show
  506. * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
  507. * @pos: The position of the file
  508. *
  509. * This is used by the seq_file "next" operation to iterate the pids
  510. * listed in a trace_pid_list structure.
  511. *
  512. * Returns the pid+1 as we want to display pid of zero, but NULL would
  513. * stop the iteration.
  514. */
  515. void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
  516. {
  517. long pid = (unsigned long)v;
  518. unsigned int next;
  519. (*pos)++;
  520. /* pid already is +1 of the actual previous bit */
  521. if (trace_pid_list_next(pid_list, pid, &next) < 0)
  522. return NULL;
  523. pid = next;
  524. /* Return pid + 1 to allow zero to be represented */
  525. return (void *)(pid + 1);
  526. }
  527. /**
  528. * trace_pid_start - Used for seq_file to start reading pid lists
  529. * @pid_list: The pid list to show
  530. * @pos: The position of the file
  531. *
  532. * This is used by seq_file "start" operation to start the iteration
  533. * of listing pids.
  534. *
  535. * Returns the pid+1 as we want to display pid of zero, but NULL would
  536. * stop the iteration.
  537. */
  538. void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
  539. {
  540. unsigned long pid;
  541. unsigned int first;
  542. loff_t l = 0;
  543. if (trace_pid_list_first(pid_list, &first) < 0)
  544. return NULL;
  545. pid = first;
  546. /* Return pid + 1 so that zero can be the exit value */
  547. for (pid++; pid && l < *pos;
  548. pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
  549. ;
  550. return (void *)pid;
  551. }
  552. /**
  553. * trace_pid_show - show the current pid in seq_file processing
  554. * @m: The seq_file structure to write into
  555. * @v: A void pointer of the pid (+1) value to display
  556. *
  557. * Can be directly used by seq_file operations to display the current
  558. * pid value.
  559. */
  560. int trace_pid_show(struct seq_file *m, void *v)
  561. {
  562. unsigned long pid = (unsigned long)v - 1;
  563. seq_printf(m, "%lu\n", pid);
  564. return 0;
  565. }
  566. /* 128 should be much more than enough */
  567. #define PID_BUF_SIZE 127
  568. int trace_pid_write(struct trace_pid_list *filtered_pids,
  569. struct trace_pid_list **new_pid_list,
  570. const char __user *ubuf, size_t cnt)
  571. {
  572. struct trace_pid_list *pid_list;
  573. struct trace_parser parser;
  574. unsigned long val;
  575. int nr_pids = 0;
  576. ssize_t read = 0;
  577. ssize_t ret;
  578. loff_t pos;
  579. pid_t pid;
  580. if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
  581. return -ENOMEM;
  582. /*
  583. * Always recreate a new array. The write is an all or nothing
  584. * operation. Always create a new array when adding new pids by
  585. * the user. If the operation fails, then the current list is
  586. * not modified.
  587. */
  588. pid_list = trace_pid_list_alloc();
  589. if (!pid_list) {
  590. trace_parser_put(&parser);
  591. return -ENOMEM;
  592. }
  593. if (filtered_pids) {
  594. /* copy the current bits to the new max */
  595. ret = trace_pid_list_first(filtered_pids, &pid);
  596. while (!ret) {
  597. trace_pid_list_set(pid_list, pid);
  598. ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
  599. nr_pids++;
  600. }
  601. }
  602. ret = 0;
  603. while (cnt > 0) {
  604. pos = 0;
  605. ret = trace_get_user(&parser, ubuf, cnt, &pos);
  606. if (ret < 0)
  607. break;
  608. read += ret;
  609. ubuf += ret;
  610. cnt -= ret;
  611. if (!trace_parser_loaded(&parser))
  612. break;
  613. ret = -EINVAL;
  614. if (kstrtoul(parser.buffer, 0, &val))
  615. break;
  616. pid = (pid_t)val;
  617. if (trace_pid_list_set(pid_list, pid) < 0) {
  618. ret = -1;
  619. break;
  620. }
  621. nr_pids++;
  622. trace_parser_clear(&parser);
  623. ret = 0;
  624. }
  625. trace_parser_put(&parser);
  626. if (ret < 0) {
  627. trace_pid_list_free(pid_list);
  628. return ret;
  629. }
  630. if (!nr_pids) {
  631. /* Cleared the list of pids */
  632. trace_pid_list_free(pid_list);
  633. pid_list = NULL;
  634. }
  635. *new_pid_list = pid_list;
  636. return read;
  637. }
  638. static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
  639. {
  640. u64 ts;
  641. /* Early boot up does not have a buffer yet */
  642. if (!buf->buffer)
  643. return trace_clock_local();
  644. ts = ring_buffer_time_stamp(buf->buffer);
  645. ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
  646. return ts;
  647. }
  648. u64 ftrace_now(int cpu)
  649. {
  650. return buffer_ftrace_now(&global_trace.array_buffer, cpu);
  651. }
  652. /**
  653. * tracing_is_enabled - Show if global_trace has been enabled
  654. *
  655. * Shows if the global trace has been enabled or not. It uses the
  656. * mirror flag "buffer_disabled" to be used in fast paths such as for
  657. * the irqsoff tracer. But it may be inaccurate due to races. If you
  658. * need to know the accurate state, use tracing_is_on() which is a little
  659. * slower, but accurate.
  660. */
  661. int tracing_is_enabled(void)
  662. {
  663. /*
  664. * For quick access (irqsoff uses this in fast path), just
  665. * return the mirror variable of the state of the ring buffer.
  666. * It's a little racy, but we don't really care.
  667. */
  668. smp_rmb();
  669. return !global_trace.buffer_disabled;
  670. }
  671. /*
  672. * trace_buf_size is the size in bytes that is allocated
  673. * for a buffer. Note, the number of bytes is always rounded
  674. * to page size.
  675. *
  676. * This number is purposely set to a low number of 16384.
  677. * If the dump on oops happens, it will be much appreciated
  678. * to not have to wait for all that output. Anyway this can be
  679. * boot time and run time configurable.
  680. */
  681. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  682. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  683. /* trace_types holds a link list of available tracers. */
  684. static struct tracer *trace_types __read_mostly;
  685. /*
  686. * trace_types_lock is used to protect the trace_types list.
  687. */
  688. DEFINE_MUTEX(trace_types_lock);
  689. /*
  690. * serialize the access of the ring buffer
  691. *
  692. * ring buffer serializes readers, but it is low level protection.
  693. * The validity of the events (which returns by ring_buffer_peek() ..etc)
  694. * are not protected by ring buffer.
  695. *
  696. * The content of events may become garbage if we allow other process consumes
  697. * these events concurrently:
  698. * A) the page of the consumed events may become a normal page
  699. * (not reader page) in ring buffer, and this page will be rewritten
  700. * by events producer.
  701. * B) The page of the consumed events may become a page for splice_read,
  702. * and this page will be returned to system.
  703. *
  704. * These primitives allow multi process access to different cpu ring buffer
  705. * concurrently.
  706. *
  707. * These primitives don't distinguish read-only and read-consume access.
  708. * Multi read-only access are also serialized.
  709. */
  710. #ifdef CONFIG_SMP
  711. static DECLARE_RWSEM(all_cpu_access_lock);
  712. static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
  713. static inline void trace_access_lock(int cpu)
  714. {
  715. if (cpu == RING_BUFFER_ALL_CPUS) {
  716. /* gain it for accessing the whole ring buffer. */
  717. down_write(&all_cpu_access_lock);
  718. } else {
  719. /* gain it for accessing a cpu ring buffer. */
  720. /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
  721. down_read(&all_cpu_access_lock);
  722. /* Secondly block other access to this @cpu ring buffer. */
  723. mutex_lock(&per_cpu(cpu_access_lock, cpu));
  724. }
  725. }
  726. static inline void trace_access_unlock(int cpu)
  727. {
  728. if (cpu == RING_BUFFER_ALL_CPUS) {
  729. up_write(&all_cpu_access_lock);
  730. } else {
  731. mutex_unlock(&per_cpu(cpu_access_lock, cpu));
  732. up_read(&all_cpu_access_lock);
  733. }
  734. }
  735. static inline void trace_access_lock_init(void)
  736. {
  737. int cpu;
  738. for_each_possible_cpu(cpu)
  739. mutex_init(&per_cpu(cpu_access_lock, cpu));
  740. }
  741. #else
  742. static DEFINE_MUTEX(access_lock);
  743. static inline void trace_access_lock(int cpu)
  744. {
  745. (void)cpu;
  746. mutex_lock(&access_lock);
  747. }
  748. static inline void trace_access_unlock(int cpu)
  749. {
  750. (void)cpu;
  751. mutex_unlock(&access_lock);
  752. }
  753. static inline void trace_access_lock_init(void)
  754. {
  755. }
  756. #endif
  757. #ifdef CONFIG_STACKTRACE
  758. static void __ftrace_trace_stack(struct trace_buffer *buffer,
  759. unsigned int trace_ctx,
  760. int skip, struct pt_regs *regs);
  761. static inline void ftrace_trace_stack(struct trace_array *tr,
  762. struct trace_buffer *buffer,
  763. unsigned int trace_ctx,
  764. int skip, struct pt_regs *regs);
  765. #else
  766. static inline void __ftrace_trace_stack(struct trace_buffer *buffer,
  767. unsigned int trace_ctx,
  768. int skip, struct pt_regs *regs)
  769. {
  770. }
  771. static inline void ftrace_trace_stack(struct trace_array *tr,
  772. struct trace_buffer *buffer,
  773. unsigned long trace_ctx,
  774. int skip, struct pt_regs *regs)
  775. {
  776. }
  777. #endif
  778. static __always_inline void
  779. trace_event_setup(struct ring_buffer_event *event,
  780. int type, unsigned int trace_ctx)
  781. {
  782. struct trace_entry *ent = ring_buffer_event_data(event);
  783. tracing_generic_entry_update(ent, type, trace_ctx);
  784. }
  785. static __always_inline struct ring_buffer_event *
  786. __trace_buffer_lock_reserve(struct trace_buffer *buffer,
  787. int type,
  788. unsigned long len,
  789. unsigned int trace_ctx)
  790. {
  791. struct ring_buffer_event *event;
  792. event = ring_buffer_lock_reserve(buffer, len);
  793. if (event != NULL)
  794. trace_event_setup(event, type, trace_ctx);
  795. return event;
  796. }
  797. void tracer_tracing_on(struct trace_array *tr)
  798. {
  799. if (tr->array_buffer.buffer)
  800. ring_buffer_record_on(tr->array_buffer.buffer);
  801. /*
  802. * This flag is looked at when buffers haven't been allocated
  803. * yet, or by some tracers (like irqsoff), that just want to
  804. * know if the ring buffer has been disabled, but it can handle
  805. * races of where it gets disabled but we still do a record.
  806. * As the check is in the fast path of the tracers, it is more
  807. * important to be fast than accurate.
  808. */
  809. tr->buffer_disabled = 0;
  810. /* Make the flag seen by readers */
  811. smp_wmb();
  812. }
  813. /**
  814. * tracing_on - enable tracing buffers
  815. *
  816. * This function enables tracing buffers that may have been
  817. * disabled with tracing_off.
  818. */
  819. void tracing_on(void)
  820. {
  821. tracer_tracing_on(&global_trace);
  822. }
  823. EXPORT_SYMBOL_GPL(tracing_on);
  824. static __always_inline void
  825. __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
  826. {
  827. __this_cpu_write(trace_taskinfo_save, true);
  828. /* If this is the temp buffer, we need to commit fully */
  829. if (this_cpu_read(trace_buffered_event) == event) {
  830. /* Length is in event->array[0] */
  831. ring_buffer_write(buffer, event->array[0], &event->array[1]);
  832. /* Release the temp buffer */
  833. this_cpu_dec(trace_buffered_event_cnt);
  834. /* ring_buffer_unlock_commit() enables preemption */
  835. preempt_enable_notrace();
  836. } else
  837. ring_buffer_unlock_commit(buffer, event);
  838. }
  839. int __trace_array_puts(struct trace_array *tr, unsigned long ip,
  840. const char *str, int size)
  841. {
  842. struct ring_buffer_event *event;
  843. struct trace_buffer *buffer;
  844. struct print_entry *entry;
  845. unsigned int trace_ctx;
  846. int alloc;
  847. if (!(tr->trace_flags & TRACE_ITER_PRINTK))
  848. return 0;
  849. if (unlikely(tracing_selftest_running || tracing_disabled))
  850. return 0;
  851. alloc = sizeof(*entry) + size + 2; /* possible \n added */
  852. trace_ctx = tracing_gen_ctx();
  853. buffer = tr->array_buffer.buffer;
  854. ring_buffer_nest_start(buffer);
  855. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
  856. trace_ctx);
  857. if (!event) {
  858. size = 0;
  859. goto out;
  860. }
  861. entry = ring_buffer_event_data(event);
  862. entry->ip = ip;
  863. memcpy(&entry->buf, str, size);
  864. /* Add a newline if necessary */
  865. if (entry->buf[size - 1] != '\n') {
  866. entry->buf[size] = '\n';
  867. entry->buf[size + 1] = '\0';
  868. } else
  869. entry->buf[size] = '\0';
  870. __buffer_unlock_commit(buffer, event);
  871. ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
  872. out:
  873. ring_buffer_nest_end(buffer);
  874. return size;
  875. }
  876. EXPORT_SYMBOL_GPL(__trace_array_puts);
  877. /**
  878. * __trace_puts - write a constant string into the trace buffer.
  879. * @ip: The address of the caller
  880. * @str: The constant string to write
  881. * @size: The size of the string.
  882. */
  883. int __trace_puts(unsigned long ip, const char *str, int size)
  884. {
  885. return __trace_array_puts(&global_trace, ip, str, size);
  886. }
  887. EXPORT_SYMBOL_GPL(__trace_puts);
  888. /**
  889. * __trace_bputs - write the pointer to a constant string into trace buffer
  890. * @ip: The address of the caller
  891. * @str: The constant string to write to the buffer to
  892. */
  893. int __trace_bputs(unsigned long ip, const char *str)
  894. {
  895. struct ring_buffer_event *event;
  896. struct trace_buffer *buffer;
  897. struct bputs_entry *entry;
  898. unsigned int trace_ctx;
  899. int size = sizeof(struct bputs_entry);
  900. int ret = 0;
  901. if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
  902. return 0;
  903. if (unlikely(tracing_selftest_running || tracing_disabled))
  904. return 0;
  905. trace_ctx = tracing_gen_ctx();
  906. buffer = global_trace.array_buffer.buffer;
  907. ring_buffer_nest_start(buffer);
  908. event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
  909. trace_ctx);
  910. if (!event)
  911. goto out;
  912. entry = ring_buffer_event_data(event);
  913. entry->ip = ip;
  914. entry->str = str;
  915. __buffer_unlock_commit(buffer, event);
  916. ftrace_trace_stack(&global_trace, buffer, trace_ctx, 4, NULL);
  917. ret = 1;
  918. out:
  919. ring_buffer_nest_end(buffer);
  920. return ret;
  921. }
  922. EXPORT_SYMBOL_GPL(__trace_bputs);
  923. #ifdef CONFIG_TRACER_SNAPSHOT
  924. static void tracing_snapshot_instance_cond(struct trace_array *tr,
  925. void *cond_data)
  926. {
  927. struct tracer *tracer = tr->current_trace;
  928. unsigned long flags;
  929. if (in_nmi()) {
  930. trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
  931. trace_array_puts(tr, "*** snapshot is being ignored ***\n");
  932. return;
  933. }
  934. if (!tr->allocated_snapshot) {
  935. trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
  936. trace_array_puts(tr, "*** stopping trace here! ***\n");
  937. tracer_tracing_off(tr);
  938. return;
  939. }
  940. /* Note, snapshot can not be used when the tracer uses it */
  941. if (tracer->use_max_tr) {
  942. trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
  943. trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
  944. return;
  945. }
  946. local_irq_save(flags);
  947. update_max_tr(tr, current, smp_processor_id(), cond_data);
  948. local_irq_restore(flags);
  949. }
  950. void tracing_snapshot_instance(struct trace_array *tr)
  951. {
  952. tracing_snapshot_instance_cond(tr, NULL);
  953. }
  954. /**
  955. * tracing_snapshot - take a snapshot of the current buffer.
  956. *
  957. * This causes a swap between the snapshot buffer and the current live
  958. * tracing buffer. You can use this to take snapshots of the live
  959. * trace when some condition is triggered, but continue to trace.
  960. *
  961. * Note, make sure to allocate the snapshot with either
  962. * a tracing_snapshot_alloc(), or by doing it manually
  963. * with: echo 1 > /sys/kernel/debug/tracing/snapshot
  964. *
  965. * If the snapshot buffer is not allocated, it will stop tracing.
  966. * Basically making a permanent snapshot.
  967. */
  968. void tracing_snapshot(void)
  969. {
  970. struct trace_array *tr = &global_trace;
  971. tracing_snapshot_instance(tr);
  972. }
  973. EXPORT_SYMBOL_GPL(tracing_snapshot);
  974. /**
  975. * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
  976. * @tr: The tracing instance to snapshot
  977. * @cond_data: The data to be tested conditionally, and possibly saved
  978. *
  979. * This is the same as tracing_snapshot() except that the snapshot is
  980. * conditional - the snapshot will only happen if the
  981. * cond_snapshot.update() implementation receiving the cond_data
  982. * returns true, which means that the trace array's cond_snapshot
  983. * update() operation used the cond_data to determine whether the
  984. * snapshot should be taken, and if it was, presumably saved it along
  985. * with the snapshot.
  986. */
  987. void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
  988. {
  989. tracing_snapshot_instance_cond(tr, cond_data);
  990. }
  991. EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
  992. /**
  993. * tracing_cond_snapshot_data - get the user data associated with a snapshot
  994. * @tr: The tracing instance
  995. *
  996. * When the user enables a conditional snapshot using
  997. * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
  998. * with the snapshot. This accessor is used to retrieve it.
  999. *
  1000. * Should not be called from cond_snapshot.update(), since it takes
  1001. * the tr->max_lock lock, which the code calling
  1002. * cond_snapshot.update() has already done.
  1003. *
  1004. * Returns the cond_data associated with the trace array's snapshot.
  1005. */
  1006. void *tracing_cond_snapshot_data(struct trace_array *tr)
  1007. {
  1008. void *cond_data = NULL;
  1009. local_irq_disable();
  1010. arch_spin_lock(&tr->max_lock);
  1011. if (tr->cond_snapshot)
  1012. cond_data = tr->cond_snapshot->cond_data;
  1013. arch_spin_unlock(&tr->max_lock);
  1014. local_irq_enable();
  1015. return cond_data;
  1016. }
  1017. EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
  1018. static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
  1019. struct array_buffer *size_buf, int cpu_id);
  1020. static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
  1021. int tracing_alloc_snapshot_instance(struct trace_array *tr)
  1022. {
  1023. int ret;
  1024. if (!tr->allocated_snapshot) {
  1025. /* allocate spare buffer */
  1026. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  1027. &tr->array_buffer, RING_BUFFER_ALL_CPUS);
  1028. if (ret < 0)
  1029. return ret;
  1030. tr->allocated_snapshot = true;
  1031. }
  1032. return 0;
  1033. }
  1034. static void free_snapshot(struct trace_array *tr)
  1035. {
  1036. /*
  1037. * We don't free the ring buffer. instead, resize it because
  1038. * The max_tr ring buffer has some state (e.g. ring->clock) and
  1039. * we want preserve it.
  1040. */
  1041. ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
  1042. set_buffer_entries(&tr->max_buffer, 1);
  1043. tracing_reset_online_cpus(&tr->max_buffer);
  1044. tr->allocated_snapshot = false;
  1045. }
  1046. /**
  1047. * tracing_alloc_snapshot - allocate snapshot buffer.
  1048. *
  1049. * This only allocates the snapshot buffer if it isn't already
  1050. * allocated - it doesn't also take a snapshot.
  1051. *
  1052. * This is meant to be used in cases where the snapshot buffer needs
  1053. * to be set up for events that can't sleep but need to be able to
  1054. * trigger a snapshot.
  1055. */
  1056. int tracing_alloc_snapshot(void)
  1057. {
  1058. struct trace_array *tr = &global_trace;
  1059. int ret;
  1060. ret = tracing_alloc_snapshot_instance(tr);
  1061. WARN_ON(ret < 0);
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
  1065. /**
  1066. * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
  1067. *
  1068. * This is similar to tracing_snapshot(), but it will allocate the
  1069. * snapshot buffer if it isn't already allocated. Use this only
  1070. * where it is safe to sleep, as the allocation may sleep.
  1071. *
  1072. * This causes a swap between the snapshot buffer and the current live
  1073. * tracing buffer. You can use this to take snapshots of the live
  1074. * trace when some condition is triggered, but continue to trace.
  1075. */
  1076. void tracing_snapshot_alloc(void)
  1077. {
  1078. int ret;
  1079. ret = tracing_alloc_snapshot();
  1080. if (ret < 0)
  1081. return;
  1082. tracing_snapshot();
  1083. }
  1084. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  1085. /**
  1086. * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
  1087. * @tr: The tracing instance
  1088. * @cond_data: User data to associate with the snapshot
  1089. * @update: Implementation of the cond_snapshot update function
  1090. *
  1091. * Check whether the conditional snapshot for the given instance has
  1092. * already been enabled, or if the current tracer is already using a
  1093. * snapshot; if so, return -EBUSY, else create a cond_snapshot and
  1094. * save the cond_data and update function inside.
  1095. *
  1096. * Returns 0 if successful, error otherwise.
  1097. */
  1098. int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
  1099. cond_update_fn_t update)
  1100. {
  1101. struct cond_snapshot *cond_snapshot;
  1102. int ret = 0;
  1103. cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
  1104. if (!cond_snapshot)
  1105. return -ENOMEM;
  1106. cond_snapshot->cond_data = cond_data;
  1107. cond_snapshot->update = update;
  1108. mutex_lock(&trace_types_lock);
  1109. ret = tracing_alloc_snapshot_instance(tr);
  1110. if (ret)
  1111. goto fail_unlock;
  1112. if (tr->current_trace->use_max_tr) {
  1113. ret = -EBUSY;
  1114. goto fail_unlock;
  1115. }
  1116. /*
  1117. * The cond_snapshot can only change to NULL without the
  1118. * trace_types_lock. We don't care if we race with it going
  1119. * to NULL, but we want to make sure that it's not set to
  1120. * something other than NULL when we get here, which we can
  1121. * do safely with only holding the trace_types_lock and not
  1122. * having to take the max_lock.
  1123. */
  1124. if (tr->cond_snapshot) {
  1125. ret = -EBUSY;
  1126. goto fail_unlock;
  1127. }
  1128. local_irq_disable();
  1129. arch_spin_lock(&tr->max_lock);
  1130. tr->cond_snapshot = cond_snapshot;
  1131. arch_spin_unlock(&tr->max_lock);
  1132. local_irq_enable();
  1133. mutex_unlock(&trace_types_lock);
  1134. return ret;
  1135. fail_unlock:
  1136. mutex_unlock(&trace_types_lock);
  1137. kfree(cond_snapshot);
  1138. return ret;
  1139. }
  1140. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
  1141. /**
  1142. * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
  1143. * @tr: The tracing instance
  1144. *
  1145. * Check whether the conditional snapshot for the given instance is
  1146. * enabled; if so, free the cond_snapshot associated with it,
  1147. * otherwise return -EINVAL.
  1148. *
  1149. * Returns 0 if successful, error otherwise.
  1150. */
  1151. int tracing_snapshot_cond_disable(struct trace_array *tr)
  1152. {
  1153. int ret = 0;
  1154. local_irq_disable();
  1155. arch_spin_lock(&tr->max_lock);
  1156. if (!tr->cond_snapshot)
  1157. ret = -EINVAL;
  1158. else {
  1159. kfree(tr->cond_snapshot);
  1160. tr->cond_snapshot = NULL;
  1161. }
  1162. arch_spin_unlock(&tr->max_lock);
  1163. local_irq_enable();
  1164. return ret;
  1165. }
  1166. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
  1167. #else
  1168. void tracing_snapshot(void)
  1169. {
  1170. WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
  1171. }
  1172. EXPORT_SYMBOL_GPL(tracing_snapshot);
  1173. void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
  1174. {
  1175. WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
  1176. }
  1177. EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
  1178. int tracing_alloc_snapshot(void)
  1179. {
  1180. WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
  1181. return -ENODEV;
  1182. }
  1183. EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
  1184. void tracing_snapshot_alloc(void)
  1185. {
  1186. /* Give warning */
  1187. tracing_snapshot();
  1188. }
  1189. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  1190. void *tracing_cond_snapshot_data(struct trace_array *tr)
  1191. {
  1192. return NULL;
  1193. }
  1194. EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
  1195. int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
  1196. {
  1197. return -ENODEV;
  1198. }
  1199. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
  1200. int tracing_snapshot_cond_disable(struct trace_array *tr)
  1201. {
  1202. return false;
  1203. }
  1204. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
  1205. #define free_snapshot(tr) do { } while (0)
  1206. #endif /* CONFIG_TRACER_SNAPSHOT */
  1207. void tracer_tracing_off(struct trace_array *tr)
  1208. {
  1209. if (tr->array_buffer.buffer)
  1210. ring_buffer_record_off(tr->array_buffer.buffer);
  1211. /*
  1212. * This flag is looked at when buffers haven't been allocated
  1213. * yet, or by some tracers (like irqsoff), that just want to
  1214. * know if the ring buffer has been disabled, but it can handle
  1215. * races of where it gets disabled but we still do a record.
  1216. * As the check is in the fast path of the tracers, it is more
  1217. * important to be fast than accurate.
  1218. */
  1219. tr->buffer_disabled = 1;
  1220. /* Make the flag seen by readers */
  1221. smp_wmb();
  1222. }
  1223. /**
  1224. * tracing_off - turn off tracing buffers
  1225. *
  1226. * This function stops the tracing buffers from recording data.
  1227. * It does not disable any overhead the tracers themselves may
  1228. * be causing. This function simply causes all recording to
  1229. * the ring buffers to fail.
  1230. */
  1231. void tracing_off(void)
  1232. {
  1233. tracer_tracing_off(&global_trace);
  1234. }
  1235. EXPORT_SYMBOL_GPL(tracing_off);
  1236. void disable_trace_on_warning(void)
  1237. {
  1238. if (__disable_trace_on_warning) {
  1239. trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
  1240. "Disabling tracing due to warning\n");
  1241. tracing_off();
  1242. }
  1243. }
  1244. /**
  1245. * tracer_tracing_is_on - show real state of ring buffer enabled
  1246. * @tr : the trace array to know if ring buffer is enabled
  1247. *
  1248. * Shows real state of the ring buffer if it is enabled or not.
  1249. */
  1250. bool tracer_tracing_is_on(struct trace_array *tr)
  1251. {
  1252. if (tr->array_buffer.buffer)
  1253. return ring_buffer_record_is_on(tr->array_buffer.buffer);
  1254. return !tr->buffer_disabled;
  1255. }
  1256. /**
  1257. * tracing_is_on - show state of ring buffers enabled
  1258. */
  1259. int tracing_is_on(void)
  1260. {
  1261. return tracer_tracing_is_on(&global_trace);
  1262. }
  1263. EXPORT_SYMBOL_GPL(tracing_is_on);
  1264. static int __init set_buf_size(char *str)
  1265. {
  1266. unsigned long buf_size;
  1267. if (!str)
  1268. return 0;
  1269. buf_size = memparse(str, &str);
  1270. /*
  1271. * nr_entries can not be zero and the startup
  1272. * tests require some buffer space. Therefore
  1273. * ensure we have at least 4096 bytes of buffer.
  1274. */
  1275. trace_buf_size = max(4096UL, buf_size);
  1276. return 1;
  1277. }
  1278. __setup("trace_buf_size=", set_buf_size);
  1279. static int __init set_tracing_thresh(char *str)
  1280. {
  1281. unsigned long threshold;
  1282. int ret;
  1283. if (!str)
  1284. return 0;
  1285. ret = kstrtoul(str, 0, &threshold);
  1286. if (ret < 0)
  1287. return 0;
  1288. tracing_thresh = threshold * 1000;
  1289. return 1;
  1290. }
  1291. __setup("tracing_thresh=", set_tracing_thresh);
  1292. unsigned long nsecs_to_usecs(unsigned long nsecs)
  1293. {
  1294. return nsecs / 1000;
  1295. }
  1296. /*
  1297. * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
  1298. * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
  1299. * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
  1300. * of strings in the order that the evals (enum) were defined.
  1301. */
  1302. #undef C
  1303. #define C(a, b) b
  1304. /* These must match the bit positions in trace_iterator_flags */
  1305. static const char *trace_options[] = {
  1306. TRACE_FLAGS
  1307. NULL
  1308. };
  1309. static struct {
  1310. u64 (*func)(void);
  1311. const char *name;
  1312. int in_ns; /* is this clock in nanoseconds? */
  1313. } trace_clocks[] = {
  1314. { trace_clock_local, "local", 1 },
  1315. { trace_clock_global, "global", 1 },
  1316. { trace_clock_counter, "counter", 0 },
  1317. { trace_clock_jiffies, "uptime", 0 },
  1318. { trace_clock, "perf", 1 },
  1319. { ktime_get_mono_fast_ns, "mono", 1 },
  1320. { ktime_get_raw_fast_ns, "mono_raw", 1 },
  1321. { ktime_get_boot_fast_ns, "boot", 1 },
  1322. { ktime_get_tai_fast_ns, "tai", 1 },
  1323. ARCH_TRACE_CLOCKS
  1324. };
  1325. bool trace_clock_in_ns(struct trace_array *tr)
  1326. {
  1327. if (trace_clocks[tr->clock_id].in_ns)
  1328. return true;
  1329. return false;
  1330. }
  1331. /*
  1332. * trace_parser_get_init - gets the buffer for trace parser
  1333. */
  1334. int trace_parser_get_init(struct trace_parser *parser, int size)
  1335. {
  1336. memset(parser, 0, sizeof(*parser));
  1337. parser->buffer = kmalloc(size, GFP_KERNEL);
  1338. if (!parser->buffer)
  1339. return 1;
  1340. parser->size = size;
  1341. return 0;
  1342. }
  1343. /*
  1344. * trace_parser_put - frees the buffer for trace parser
  1345. */
  1346. void trace_parser_put(struct trace_parser *parser)
  1347. {
  1348. kfree(parser->buffer);
  1349. parser->buffer = NULL;
  1350. }
  1351. /*
  1352. * trace_get_user - reads the user input string separated by space
  1353. * (matched by isspace(ch))
  1354. *
  1355. * For each string found the 'struct trace_parser' is updated,
  1356. * and the function returns.
  1357. *
  1358. * Returns number of bytes read.
  1359. *
  1360. * See kernel/trace/trace.h for 'struct trace_parser' details.
  1361. */
  1362. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  1363. size_t cnt, loff_t *ppos)
  1364. {
  1365. char ch;
  1366. size_t read = 0;
  1367. ssize_t ret;
  1368. if (!*ppos)
  1369. trace_parser_clear(parser);
  1370. ret = get_user(ch, ubuf++);
  1371. if (ret)
  1372. goto out;
  1373. read++;
  1374. cnt--;
  1375. /*
  1376. * The parser is not finished with the last write,
  1377. * continue reading the user input without skipping spaces.
  1378. */
  1379. if (!parser->cont) {
  1380. /* skip white space */
  1381. while (cnt && isspace(ch)) {
  1382. ret = get_user(ch, ubuf++);
  1383. if (ret)
  1384. goto out;
  1385. read++;
  1386. cnt--;
  1387. }
  1388. parser->idx = 0;
  1389. /* only spaces were written */
  1390. if (isspace(ch) || !ch) {
  1391. *ppos += read;
  1392. ret = read;
  1393. goto out;
  1394. }
  1395. }
  1396. /* read the non-space input */
  1397. while (cnt && !isspace(ch) && ch) {
  1398. if (parser->idx < parser->size - 1)
  1399. parser->buffer[parser->idx++] = ch;
  1400. else {
  1401. ret = -EINVAL;
  1402. goto out;
  1403. }
  1404. ret = get_user(ch, ubuf++);
  1405. if (ret)
  1406. goto out;
  1407. read++;
  1408. cnt--;
  1409. }
  1410. /* We either got finished input or we have to wait for another call. */
  1411. if (isspace(ch) || !ch) {
  1412. parser->buffer[parser->idx] = 0;
  1413. parser->cont = false;
  1414. } else if (parser->idx < parser->size - 1) {
  1415. parser->cont = true;
  1416. parser->buffer[parser->idx++] = ch;
  1417. /* Make sure the parsed string always terminates with '\0'. */
  1418. parser->buffer[parser->idx] = 0;
  1419. } else {
  1420. ret = -EINVAL;
  1421. goto out;
  1422. }
  1423. *ppos += read;
  1424. ret = read;
  1425. out:
  1426. return ret;
  1427. }
  1428. /* TODO add a seq_buf_to_buffer() */
  1429. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  1430. {
  1431. int len;
  1432. if (trace_seq_used(s) <= s->seq.readpos)
  1433. return -EBUSY;
  1434. len = trace_seq_used(s) - s->seq.readpos;
  1435. if (cnt > len)
  1436. cnt = len;
  1437. memcpy(buf, s->buffer + s->seq.readpos, cnt);
  1438. s->seq.readpos += cnt;
  1439. return cnt;
  1440. }
  1441. unsigned long __read_mostly tracing_thresh;
  1442. #ifdef CONFIG_TRACER_MAX_TRACE
  1443. static const struct file_operations tracing_max_lat_fops;
  1444. #ifdef LATENCY_FS_NOTIFY
  1445. static struct workqueue_struct *fsnotify_wq;
  1446. static void latency_fsnotify_workfn(struct work_struct *work)
  1447. {
  1448. struct trace_array *tr = container_of(work, struct trace_array,
  1449. fsnotify_work);
  1450. fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
  1451. }
  1452. static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
  1453. {
  1454. struct trace_array *tr = container_of(iwork, struct trace_array,
  1455. fsnotify_irqwork);
  1456. queue_work(fsnotify_wq, &tr->fsnotify_work);
  1457. }
  1458. static void trace_create_maxlat_file(struct trace_array *tr,
  1459. struct dentry *d_tracer)
  1460. {
  1461. INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
  1462. init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
  1463. tr->d_max_latency = trace_create_file("tracing_max_latency",
  1464. TRACE_MODE_WRITE,
  1465. d_tracer, tr,
  1466. &tracing_max_lat_fops);
  1467. }
  1468. __init static int latency_fsnotify_init(void)
  1469. {
  1470. fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
  1471. WQ_UNBOUND | WQ_HIGHPRI, 0);
  1472. if (!fsnotify_wq) {
  1473. pr_err("Unable to allocate tr_max_lat_wq\n");
  1474. return -ENOMEM;
  1475. }
  1476. return 0;
  1477. }
  1478. late_initcall_sync(latency_fsnotify_init);
  1479. void latency_fsnotify(struct trace_array *tr)
  1480. {
  1481. if (!fsnotify_wq)
  1482. return;
  1483. /*
  1484. * We cannot call queue_work(&tr->fsnotify_work) from here because it's
  1485. * possible that we are called from __schedule() or do_idle(), which
  1486. * could cause a deadlock.
  1487. */
  1488. irq_work_queue(&tr->fsnotify_irqwork);
  1489. }
  1490. #else /* !LATENCY_FS_NOTIFY */
  1491. #define trace_create_maxlat_file(tr, d_tracer) \
  1492. trace_create_file("tracing_max_latency", TRACE_MODE_WRITE, \
  1493. d_tracer, tr, &tracing_max_lat_fops)
  1494. #endif
  1495. /*
  1496. * Copy the new maximum trace into the separate maximum-trace
  1497. * structure. (this way the maximum trace is permanently saved,
  1498. * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
  1499. */
  1500. static void
  1501. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  1502. {
  1503. struct array_buffer *trace_buf = &tr->array_buffer;
  1504. struct array_buffer *max_buf = &tr->max_buffer;
  1505. struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
  1506. struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
  1507. max_buf->cpu = cpu;
  1508. max_buf->time_start = data->preempt_timestamp;
  1509. max_data->saved_latency = tr->max_latency;
  1510. max_data->critical_start = data->critical_start;
  1511. max_data->critical_end = data->critical_end;
  1512. strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  1513. max_data->pid = tsk->pid;
  1514. /*
  1515. * If tsk == current, then use current_uid(), as that does not use
  1516. * RCU. The irq tracer can be called out of RCU scope.
  1517. */
  1518. if (tsk == current)
  1519. max_data->uid = current_uid();
  1520. else
  1521. max_data->uid = task_uid(tsk);
  1522. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  1523. max_data->policy = tsk->policy;
  1524. max_data->rt_priority = tsk->rt_priority;
  1525. /* record this tasks comm */
  1526. tracing_record_cmdline(tsk);
  1527. latency_fsnotify(tr);
  1528. }
  1529. /**
  1530. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  1531. * @tr: tracer
  1532. * @tsk: the task with the latency
  1533. * @cpu: The cpu that initiated the trace.
  1534. * @cond_data: User data associated with a conditional snapshot
  1535. *
  1536. * Flip the buffers between the @tr and the max_tr and record information
  1537. * about which task was the cause of this latency.
  1538. */
  1539. void
  1540. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
  1541. void *cond_data)
  1542. {
  1543. if (tr->stop_count)
  1544. return;
  1545. WARN_ON_ONCE(!irqs_disabled());
  1546. if (!tr->allocated_snapshot) {
  1547. /* Only the nop tracer should hit this when disabling */
  1548. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  1549. return;
  1550. }
  1551. arch_spin_lock(&tr->max_lock);
  1552. /* Inherit the recordable setting from array_buffer */
  1553. if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
  1554. ring_buffer_record_on(tr->max_buffer.buffer);
  1555. else
  1556. ring_buffer_record_off(tr->max_buffer.buffer);
  1557. #ifdef CONFIG_TRACER_SNAPSHOT
  1558. if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) {
  1559. arch_spin_unlock(&tr->max_lock);
  1560. return;
  1561. }
  1562. #endif
  1563. swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
  1564. __update_max_tr(tr, tsk, cpu);
  1565. arch_spin_unlock(&tr->max_lock);
  1566. }
  1567. /**
  1568. * update_max_tr_single - only copy one trace over, and reset the rest
  1569. * @tr: tracer
  1570. * @tsk: task with the latency
  1571. * @cpu: the cpu of the buffer to copy.
  1572. *
  1573. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  1574. */
  1575. void
  1576. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  1577. {
  1578. int ret;
  1579. if (tr->stop_count)
  1580. return;
  1581. WARN_ON_ONCE(!irqs_disabled());
  1582. if (!tr->allocated_snapshot) {
  1583. /* Only the nop tracer should hit this when disabling */
  1584. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  1585. return;
  1586. }
  1587. arch_spin_lock(&tr->max_lock);
  1588. ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
  1589. if (ret == -EBUSY) {
  1590. /*
  1591. * We failed to swap the buffer due to a commit taking
  1592. * place on this CPU. We fail to record, but we reset
  1593. * the max trace buffer (no one writes directly to it)
  1594. * and flag that it failed.
  1595. * Another reason is resize is in progress.
  1596. */
  1597. trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
  1598. "Failed to swap buffers due to commit or resize in progress\n");
  1599. }
  1600. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  1601. __update_max_tr(tr, tsk, cpu);
  1602. arch_spin_unlock(&tr->max_lock);
  1603. }
  1604. #endif /* CONFIG_TRACER_MAX_TRACE */
  1605. static int wait_on_pipe(struct trace_iterator *iter, int full)
  1606. {
  1607. /* Iterators are static, they should be filled or empty */
  1608. if (trace_buffer_iter(iter, iter->cpu_file))
  1609. return 0;
  1610. return ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file,
  1611. full);
  1612. }
  1613. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1614. static bool selftests_can_run;
  1615. struct trace_selftests {
  1616. struct list_head list;
  1617. struct tracer *type;
  1618. };
  1619. static LIST_HEAD(postponed_selftests);
  1620. static int save_selftest(struct tracer *type)
  1621. {
  1622. struct trace_selftests *selftest;
  1623. selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
  1624. if (!selftest)
  1625. return -ENOMEM;
  1626. selftest->type = type;
  1627. list_add(&selftest->list, &postponed_selftests);
  1628. return 0;
  1629. }
  1630. static int run_tracer_selftest(struct tracer *type)
  1631. {
  1632. struct trace_array *tr = &global_trace;
  1633. struct tracer *saved_tracer = tr->current_trace;
  1634. int ret;
  1635. if (!type->selftest || tracing_selftest_disabled)
  1636. return 0;
  1637. /*
  1638. * If a tracer registers early in boot up (before scheduling is
  1639. * initialized and such), then do not run its selftests yet.
  1640. * Instead, run it a little later in the boot process.
  1641. */
  1642. if (!selftests_can_run)
  1643. return save_selftest(type);
  1644. if (!tracing_is_on()) {
  1645. pr_warn("Selftest for tracer %s skipped due to tracing disabled\n",
  1646. type->name);
  1647. return 0;
  1648. }
  1649. /*
  1650. * Run a selftest on this tracer.
  1651. * Here we reset the trace buffer, and set the current
  1652. * tracer to be this tracer. The tracer can then run some
  1653. * internal tracing to verify that everything is in order.
  1654. * If we fail, we do not register this tracer.
  1655. */
  1656. tracing_reset_online_cpus(&tr->array_buffer);
  1657. tr->current_trace = type;
  1658. #ifdef CONFIG_TRACER_MAX_TRACE
  1659. if (type->use_max_tr) {
  1660. /* If we expanded the buffers, make sure the max is expanded too */
  1661. if (ring_buffer_expanded)
  1662. ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
  1663. RING_BUFFER_ALL_CPUS);
  1664. tr->allocated_snapshot = true;
  1665. }
  1666. #endif
  1667. /* the test is responsible for initializing and enabling */
  1668. pr_info("Testing tracer %s: ", type->name);
  1669. ret = type->selftest(type, tr);
  1670. /* the test is responsible for resetting too */
  1671. tr->current_trace = saved_tracer;
  1672. if (ret) {
  1673. printk(KERN_CONT "FAILED!\n");
  1674. /* Add the warning after printing 'FAILED' */
  1675. WARN_ON(1);
  1676. return -1;
  1677. }
  1678. /* Only reset on passing, to avoid touching corrupted buffers */
  1679. tracing_reset_online_cpus(&tr->array_buffer);
  1680. #ifdef CONFIG_TRACER_MAX_TRACE
  1681. if (type->use_max_tr) {
  1682. tr->allocated_snapshot = false;
  1683. /* Shrink the max buffer again */
  1684. if (ring_buffer_expanded)
  1685. ring_buffer_resize(tr->max_buffer.buffer, 1,
  1686. RING_BUFFER_ALL_CPUS);
  1687. }
  1688. #endif
  1689. printk(KERN_CONT "PASSED\n");
  1690. return 0;
  1691. }
  1692. static __init int init_trace_selftests(void)
  1693. {
  1694. struct trace_selftests *p, *n;
  1695. struct tracer *t, **last;
  1696. int ret;
  1697. selftests_can_run = true;
  1698. mutex_lock(&trace_types_lock);
  1699. if (list_empty(&postponed_selftests))
  1700. goto out;
  1701. pr_info("Running postponed tracer tests:\n");
  1702. tracing_selftest_running = true;
  1703. list_for_each_entry_safe(p, n, &postponed_selftests, list) {
  1704. /* This loop can take minutes when sanitizers are enabled, so
  1705. * lets make sure we allow RCU processing.
  1706. */
  1707. cond_resched();
  1708. ret = run_tracer_selftest(p->type);
  1709. /* If the test fails, then warn and remove from available_tracers */
  1710. if (ret < 0) {
  1711. WARN(1, "tracer: %s failed selftest, disabling\n",
  1712. p->type->name);
  1713. last = &trace_types;
  1714. for (t = trace_types; t; t = t->next) {
  1715. if (t == p->type) {
  1716. *last = t->next;
  1717. break;
  1718. }
  1719. last = &t->next;
  1720. }
  1721. }
  1722. list_del(&p->list);
  1723. kfree(p);
  1724. }
  1725. tracing_selftest_running = false;
  1726. out:
  1727. mutex_unlock(&trace_types_lock);
  1728. return 0;
  1729. }
  1730. core_initcall(init_trace_selftests);
  1731. #else
  1732. static inline int run_tracer_selftest(struct tracer *type)
  1733. {
  1734. return 0;
  1735. }
  1736. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  1737. static void add_tracer_options(struct trace_array *tr, struct tracer *t);
  1738. static void __init apply_trace_boot_options(void);
  1739. /**
  1740. * register_tracer - register a tracer with the ftrace system.
  1741. * @type: the plugin for the tracer
  1742. *
  1743. * Register a new plugin tracer.
  1744. */
  1745. int __init register_tracer(struct tracer *type)
  1746. {
  1747. struct tracer *t;
  1748. int ret = 0;
  1749. if (!type->name) {
  1750. pr_info("Tracer must have a name\n");
  1751. return -1;
  1752. }
  1753. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  1754. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  1755. return -1;
  1756. }
  1757. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  1758. pr_warn("Can not register tracer %s due to lockdown\n",
  1759. type->name);
  1760. return -EPERM;
  1761. }
  1762. mutex_lock(&trace_types_lock);
  1763. tracing_selftest_running = true;
  1764. for (t = trace_types; t; t = t->next) {
  1765. if (strcmp(type->name, t->name) == 0) {
  1766. /* already found */
  1767. pr_info("Tracer %s already registered\n",
  1768. type->name);
  1769. ret = -1;
  1770. goto out;
  1771. }
  1772. }
  1773. if (!type->set_flag)
  1774. type->set_flag = &dummy_set_flag;
  1775. if (!type->flags) {
  1776. /*allocate a dummy tracer_flags*/
  1777. type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
  1778. if (!type->flags) {
  1779. ret = -ENOMEM;
  1780. goto out;
  1781. }
  1782. type->flags->val = 0;
  1783. type->flags->opts = dummy_tracer_opt;
  1784. } else
  1785. if (!type->flags->opts)
  1786. type->flags->opts = dummy_tracer_opt;
  1787. /* store the tracer for __set_tracer_option */
  1788. type->flags->trace = type;
  1789. ret = run_tracer_selftest(type);
  1790. if (ret < 0)
  1791. goto out;
  1792. type->next = trace_types;
  1793. trace_types = type;
  1794. add_tracer_options(&global_trace, type);
  1795. out:
  1796. tracing_selftest_running = false;
  1797. mutex_unlock(&trace_types_lock);
  1798. if (ret || !default_bootup_tracer)
  1799. goto out_unlock;
  1800. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  1801. goto out_unlock;
  1802. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  1803. /* Do we want this tracer to start on bootup? */
  1804. tracing_set_tracer(&global_trace, type->name);
  1805. default_bootup_tracer = NULL;
  1806. apply_trace_boot_options();
  1807. /* disable other selftests, since this will break it. */
  1808. disable_tracing_selftest("running a tracer");
  1809. out_unlock:
  1810. return ret;
  1811. }
  1812. static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
  1813. {
  1814. struct trace_buffer *buffer = buf->buffer;
  1815. if (!buffer)
  1816. return;
  1817. ring_buffer_record_disable(buffer);
  1818. /* Make sure all commits have finished */
  1819. synchronize_rcu();
  1820. ring_buffer_reset_cpu(buffer, cpu);
  1821. ring_buffer_record_enable(buffer);
  1822. }
  1823. void tracing_reset_online_cpus(struct array_buffer *buf)
  1824. {
  1825. struct trace_buffer *buffer = buf->buffer;
  1826. if (!buffer)
  1827. return;
  1828. ring_buffer_record_disable(buffer);
  1829. /* Make sure all commits have finished */
  1830. synchronize_rcu();
  1831. buf->time_start = buffer_ftrace_now(buf, buf->cpu);
  1832. ring_buffer_reset_online_cpus(buffer);
  1833. ring_buffer_record_enable(buffer);
  1834. }
  1835. /* Must have trace_types_lock held */
  1836. void tracing_reset_all_online_cpus_unlocked(void)
  1837. {
  1838. struct trace_array *tr;
  1839. lockdep_assert_held(&trace_types_lock);
  1840. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  1841. if (!tr->clear_trace)
  1842. continue;
  1843. tr->clear_trace = false;
  1844. tracing_reset_online_cpus(&tr->array_buffer);
  1845. #ifdef CONFIG_TRACER_MAX_TRACE
  1846. tracing_reset_online_cpus(&tr->max_buffer);
  1847. #endif
  1848. }
  1849. }
  1850. void tracing_reset_all_online_cpus(void)
  1851. {
  1852. mutex_lock(&trace_types_lock);
  1853. tracing_reset_all_online_cpus_unlocked();
  1854. mutex_unlock(&trace_types_lock);
  1855. }
  1856. /*
  1857. * The tgid_map array maps from pid to tgid; i.e. the value stored at index i
  1858. * is the tgid last observed corresponding to pid=i.
  1859. */
  1860. static int *tgid_map;
  1861. /* The maximum valid index into tgid_map. */
  1862. static size_t tgid_map_max;
  1863. #define SAVED_CMDLINES_DEFAULT 128
  1864. #define NO_CMDLINE_MAP UINT_MAX
  1865. /*
  1866. * Preemption must be disabled before acquiring trace_cmdline_lock.
  1867. * The various trace_arrays' max_lock must be acquired in a context
  1868. * where interrupt is disabled.
  1869. */
  1870. static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  1871. struct saved_cmdlines_buffer {
  1872. unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  1873. unsigned *map_cmdline_to_pid;
  1874. unsigned cmdline_num;
  1875. int cmdline_idx;
  1876. char *saved_cmdlines;
  1877. };
  1878. static struct saved_cmdlines_buffer *savedcmd;
  1879. static inline char *get_saved_cmdlines(int idx)
  1880. {
  1881. return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
  1882. }
  1883. static inline void set_cmdline(int idx, const char *cmdline)
  1884. {
  1885. strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
  1886. }
  1887. static int allocate_cmdlines_buffer(unsigned int val,
  1888. struct saved_cmdlines_buffer *s)
  1889. {
  1890. s->map_cmdline_to_pid = kmalloc_array(val,
  1891. sizeof(*s->map_cmdline_to_pid),
  1892. GFP_KERNEL);
  1893. if (!s->map_cmdline_to_pid)
  1894. return -ENOMEM;
  1895. s->saved_cmdlines = kmalloc_array(TASK_COMM_LEN, val, GFP_KERNEL);
  1896. if (!s->saved_cmdlines) {
  1897. kfree(s->map_cmdline_to_pid);
  1898. return -ENOMEM;
  1899. }
  1900. s->cmdline_idx = 0;
  1901. s->cmdline_num = val;
  1902. memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
  1903. sizeof(s->map_pid_to_cmdline));
  1904. memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
  1905. val * sizeof(*s->map_cmdline_to_pid));
  1906. return 0;
  1907. }
  1908. static int trace_create_savedcmd(void)
  1909. {
  1910. int ret;
  1911. savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
  1912. if (!savedcmd)
  1913. return -ENOMEM;
  1914. ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
  1915. if (ret < 0) {
  1916. kfree(savedcmd);
  1917. savedcmd = NULL;
  1918. return -ENOMEM;
  1919. }
  1920. return 0;
  1921. }
  1922. int is_tracing_stopped(void)
  1923. {
  1924. return global_trace.stop_count;
  1925. }
  1926. static void tracing_start_tr(struct trace_array *tr)
  1927. {
  1928. struct trace_buffer *buffer;
  1929. unsigned long flags;
  1930. if (tracing_disabled)
  1931. return;
  1932. raw_spin_lock_irqsave(&tr->start_lock, flags);
  1933. if (--tr->stop_count) {
  1934. if (WARN_ON_ONCE(tr->stop_count < 0)) {
  1935. /* Someone screwed up their debugging */
  1936. tr->stop_count = 0;
  1937. }
  1938. goto out;
  1939. }
  1940. /* Prevent the buffers from switching */
  1941. arch_spin_lock(&tr->max_lock);
  1942. buffer = tr->array_buffer.buffer;
  1943. if (buffer)
  1944. ring_buffer_record_enable(buffer);
  1945. #ifdef CONFIG_TRACER_MAX_TRACE
  1946. buffer = tr->max_buffer.buffer;
  1947. if (buffer)
  1948. ring_buffer_record_enable(buffer);
  1949. #endif
  1950. arch_spin_unlock(&tr->max_lock);
  1951. out:
  1952. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  1953. }
  1954. /**
  1955. * tracing_start - quick start of the tracer
  1956. *
  1957. * If tracing is enabled but was stopped by tracing_stop,
  1958. * this will start the tracer back up.
  1959. */
  1960. void tracing_start(void)
  1961. {
  1962. return tracing_start_tr(&global_trace);
  1963. }
  1964. static void tracing_stop_tr(struct trace_array *tr)
  1965. {
  1966. struct trace_buffer *buffer;
  1967. unsigned long flags;
  1968. raw_spin_lock_irqsave(&tr->start_lock, flags);
  1969. if (tr->stop_count++)
  1970. goto out;
  1971. /* Prevent the buffers from switching */
  1972. arch_spin_lock(&tr->max_lock);
  1973. buffer = tr->array_buffer.buffer;
  1974. if (buffer)
  1975. ring_buffer_record_disable(buffer);
  1976. #ifdef CONFIG_TRACER_MAX_TRACE
  1977. buffer = tr->max_buffer.buffer;
  1978. if (buffer)
  1979. ring_buffer_record_disable(buffer);
  1980. #endif
  1981. arch_spin_unlock(&tr->max_lock);
  1982. out:
  1983. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  1984. }
  1985. /**
  1986. * tracing_stop - quick stop of the tracer
  1987. *
  1988. * Light weight way to stop tracing. Use in conjunction with
  1989. * tracing_start.
  1990. */
  1991. void tracing_stop(void)
  1992. {
  1993. return tracing_stop_tr(&global_trace);
  1994. }
  1995. static int trace_save_cmdline(struct task_struct *tsk)
  1996. {
  1997. unsigned tpid, idx;
  1998. /* treat recording of idle task as a success */
  1999. if (!tsk->pid)
  2000. return 1;
  2001. tpid = tsk->pid & (PID_MAX_DEFAULT - 1);
  2002. /*
  2003. * It's not the end of the world if we don't get
  2004. * the lock, but we also don't want to spin
  2005. * nor do we want to disable interrupts,
  2006. * so if we miss here, then better luck next time.
  2007. *
  2008. * This is called within the scheduler and wake up, so interrupts
  2009. * had better been disabled and run queue lock been held.
  2010. */
  2011. lockdep_assert_preemption_disabled();
  2012. if (!arch_spin_trylock(&trace_cmdline_lock))
  2013. return 0;
  2014. idx = savedcmd->map_pid_to_cmdline[tpid];
  2015. if (idx == NO_CMDLINE_MAP) {
  2016. idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
  2017. savedcmd->map_pid_to_cmdline[tpid] = idx;
  2018. savedcmd->cmdline_idx = idx;
  2019. }
  2020. savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
  2021. set_cmdline(idx, tsk->comm);
  2022. arch_spin_unlock(&trace_cmdline_lock);
  2023. return 1;
  2024. }
  2025. static void __trace_find_cmdline(int pid, char comm[])
  2026. {
  2027. unsigned map;
  2028. int tpid;
  2029. if (!pid) {
  2030. strcpy(comm, "<idle>");
  2031. return;
  2032. }
  2033. if (WARN_ON_ONCE(pid < 0)) {
  2034. strcpy(comm, "<XXX>");
  2035. return;
  2036. }
  2037. tpid = pid & (PID_MAX_DEFAULT - 1);
  2038. map = savedcmd->map_pid_to_cmdline[tpid];
  2039. if (map != NO_CMDLINE_MAP) {
  2040. tpid = savedcmd->map_cmdline_to_pid[map];
  2041. if (tpid == pid) {
  2042. strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
  2043. return;
  2044. }
  2045. }
  2046. strcpy(comm, "<...>");
  2047. }
  2048. void trace_find_cmdline(int pid, char comm[])
  2049. {
  2050. preempt_disable();
  2051. arch_spin_lock(&trace_cmdline_lock);
  2052. __trace_find_cmdline(pid, comm);
  2053. arch_spin_unlock(&trace_cmdline_lock);
  2054. preempt_enable();
  2055. }
  2056. static int *trace_find_tgid_ptr(int pid)
  2057. {
  2058. /*
  2059. * Pairs with the smp_store_release in set_tracer_flag() to ensure that
  2060. * if we observe a non-NULL tgid_map then we also observe the correct
  2061. * tgid_map_max.
  2062. */
  2063. int *map = smp_load_acquire(&tgid_map);
  2064. if (unlikely(!map || pid > tgid_map_max))
  2065. return NULL;
  2066. return &map[pid];
  2067. }
  2068. int trace_find_tgid(int pid)
  2069. {
  2070. int *ptr = trace_find_tgid_ptr(pid);
  2071. return ptr ? *ptr : 0;
  2072. }
  2073. static int trace_save_tgid(struct task_struct *tsk)
  2074. {
  2075. int *ptr;
  2076. /* treat recording of idle task as a success */
  2077. if (!tsk->pid)
  2078. return 1;
  2079. ptr = trace_find_tgid_ptr(tsk->pid);
  2080. if (!ptr)
  2081. return 0;
  2082. *ptr = tsk->tgid;
  2083. return 1;
  2084. }
  2085. static bool tracing_record_taskinfo_skip(int flags)
  2086. {
  2087. if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
  2088. return true;
  2089. if (!__this_cpu_read(trace_taskinfo_save))
  2090. return true;
  2091. return false;
  2092. }
  2093. /**
  2094. * tracing_record_taskinfo - record the task info of a task
  2095. *
  2096. * @task: task to record
  2097. * @flags: TRACE_RECORD_CMDLINE for recording comm
  2098. * TRACE_RECORD_TGID for recording tgid
  2099. */
  2100. void tracing_record_taskinfo(struct task_struct *task, int flags)
  2101. {
  2102. bool done;
  2103. if (tracing_record_taskinfo_skip(flags))
  2104. return;
  2105. /*
  2106. * Record as much task information as possible. If some fail, continue
  2107. * to try to record the others.
  2108. */
  2109. done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
  2110. done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
  2111. /* If recording any information failed, retry again soon. */
  2112. if (!done)
  2113. return;
  2114. __this_cpu_write(trace_taskinfo_save, false);
  2115. }
  2116. /**
  2117. * tracing_record_taskinfo_sched_switch - record task info for sched_switch
  2118. *
  2119. * @prev: previous task during sched_switch
  2120. * @next: next task during sched_switch
  2121. * @flags: TRACE_RECORD_CMDLINE for recording comm
  2122. * TRACE_RECORD_TGID for recording tgid
  2123. */
  2124. void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
  2125. struct task_struct *next, int flags)
  2126. {
  2127. bool done;
  2128. if (tracing_record_taskinfo_skip(flags))
  2129. return;
  2130. /*
  2131. * Record as much task information as possible. If some fail, continue
  2132. * to try to record the others.
  2133. */
  2134. done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
  2135. done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
  2136. done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
  2137. done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
  2138. /* If recording any information failed, retry again soon. */
  2139. if (!done)
  2140. return;
  2141. __this_cpu_write(trace_taskinfo_save, false);
  2142. }
  2143. /* Helpers to record a specific task information */
  2144. void tracing_record_cmdline(struct task_struct *task)
  2145. {
  2146. tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
  2147. }
  2148. void tracing_record_tgid(struct task_struct *task)
  2149. {
  2150. tracing_record_taskinfo(task, TRACE_RECORD_TGID);
  2151. }
  2152. /*
  2153. * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
  2154. * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
  2155. * simplifies those functions and keeps them in sync.
  2156. */
  2157. enum print_line_t trace_handle_return(struct trace_seq *s)
  2158. {
  2159. return trace_seq_has_overflowed(s) ?
  2160. TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
  2161. }
  2162. EXPORT_SYMBOL_GPL(trace_handle_return);
  2163. static unsigned short migration_disable_value(void)
  2164. {
  2165. #if defined(CONFIG_SMP)
  2166. return current->migration_disabled;
  2167. #else
  2168. return 0;
  2169. #endif
  2170. }
  2171. unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status)
  2172. {
  2173. unsigned int trace_flags = irqs_status;
  2174. unsigned int pc;
  2175. pc = preempt_count();
  2176. if (pc & NMI_MASK)
  2177. trace_flags |= TRACE_FLAG_NMI;
  2178. if (pc & HARDIRQ_MASK)
  2179. trace_flags |= TRACE_FLAG_HARDIRQ;
  2180. if (in_serving_softirq())
  2181. trace_flags |= TRACE_FLAG_SOFTIRQ;
  2182. if (softirq_count() >> (SOFTIRQ_SHIFT + 1))
  2183. trace_flags |= TRACE_FLAG_BH_OFF;
  2184. if (tif_need_resched())
  2185. trace_flags |= TRACE_FLAG_NEED_RESCHED;
  2186. if (test_preempt_need_resched())
  2187. trace_flags |= TRACE_FLAG_PREEMPT_RESCHED;
  2188. return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) |
  2189. (min_t(unsigned int, migration_disable_value(), 0xf)) << 4;
  2190. }
  2191. struct ring_buffer_event *
  2192. trace_buffer_lock_reserve(struct trace_buffer *buffer,
  2193. int type,
  2194. unsigned long len,
  2195. unsigned int trace_ctx)
  2196. {
  2197. return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx);
  2198. }
  2199. DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
  2200. DEFINE_PER_CPU(int, trace_buffered_event_cnt);
  2201. static int trace_buffered_event_ref;
  2202. /**
  2203. * trace_buffered_event_enable - enable buffering events
  2204. *
  2205. * When events are being filtered, it is quicker to use a temporary
  2206. * buffer to write the event data into if there's a likely chance
  2207. * that it will not be committed. The discard of the ring buffer
  2208. * is not as fast as committing, and is much slower than copying
  2209. * a commit.
  2210. *
  2211. * When an event is to be filtered, allocate per cpu buffers to
  2212. * write the event data into, and if the event is filtered and discarded
  2213. * it is simply dropped, otherwise, the entire data is to be committed
  2214. * in one shot.
  2215. */
  2216. void trace_buffered_event_enable(void)
  2217. {
  2218. struct ring_buffer_event *event;
  2219. struct page *page;
  2220. int cpu;
  2221. WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
  2222. if (trace_buffered_event_ref++)
  2223. return;
  2224. for_each_tracing_cpu(cpu) {
  2225. page = alloc_pages_node(cpu_to_node(cpu),
  2226. GFP_KERNEL | __GFP_NORETRY, 0);
  2227. /* This is just an optimization and can handle failures */
  2228. if (!page) {
  2229. pr_err("Failed to allocate event buffer\n");
  2230. break;
  2231. }
  2232. event = page_address(page);
  2233. memset(event, 0, sizeof(*event));
  2234. per_cpu(trace_buffered_event, cpu) = event;
  2235. preempt_disable();
  2236. if (cpu == smp_processor_id() &&
  2237. __this_cpu_read(trace_buffered_event) !=
  2238. per_cpu(trace_buffered_event, cpu))
  2239. WARN_ON_ONCE(1);
  2240. preempt_enable();
  2241. }
  2242. }
  2243. static void enable_trace_buffered_event(void *data)
  2244. {
  2245. /* Probably not needed, but do it anyway */
  2246. smp_rmb();
  2247. this_cpu_dec(trace_buffered_event_cnt);
  2248. }
  2249. static void disable_trace_buffered_event(void *data)
  2250. {
  2251. this_cpu_inc(trace_buffered_event_cnt);
  2252. }
  2253. /**
  2254. * trace_buffered_event_disable - disable buffering events
  2255. *
  2256. * When a filter is removed, it is faster to not use the buffered
  2257. * events, and to commit directly into the ring buffer. Free up
  2258. * the temp buffers when there are no more users. This requires
  2259. * special synchronization with current events.
  2260. */
  2261. void trace_buffered_event_disable(void)
  2262. {
  2263. int cpu;
  2264. WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
  2265. if (WARN_ON_ONCE(!trace_buffered_event_ref))
  2266. return;
  2267. if (--trace_buffered_event_ref)
  2268. return;
  2269. /* For each CPU, set the buffer as used. */
  2270. on_each_cpu_mask(tracing_buffer_mask, disable_trace_buffered_event,
  2271. NULL, true);
  2272. /* Wait for all current users to finish */
  2273. synchronize_rcu();
  2274. for_each_tracing_cpu(cpu) {
  2275. free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
  2276. per_cpu(trace_buffered_event, cpu) = NULL;
  2277. }
  2278. /*
  2279. * Wait for all CPUs that potentially started checking if they can use
  2280. * their event buffer only after the previous synchronize_rcu() call and
  2281. * they still read a valid pointer from trace_buffered_event. It must be
  2282. * ensured they don't see cleared trace_buffered_event_cnt else they
  2283. * could wrongly decide to use the pointed-to buffer which is now freed.
  2284. */
  2285. synchronize_rcu();
  2286. /* For each CPU, relinquish the buffer */
  2287. on_each_cpu_mask(tracing_buffer_mask, enable_trace_buffered_event, NULL,
  2288. true);
  2289. }
  2290. static struct trace_buffer *temp_buffer;
  2291. struct ring_buffer_event *
  2292. trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
  2293. struct trace_event_file *trace_file,
  2294. int type, unsigned long len,
  2295. unsigned int trace_ctx)
  2296. {
  2297. struct ring_buffer_event *entry;
  2298. struct trace_array *tr = trace_file->tr;
  2299. int val;
  2300. *current_rb = tr->array_buffer.buffer;
  2301. if (!tr->no_filter_buffering_ref &&
  2302. (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED))) {
  2303. preempt_disable_notrace();
  2304. /*
  2305. * Filtering is on, so try to use the per cpu buffer first.
  2306. * This buffer will simulate a ring_buffer_event,
  2307. * where the type_len is zero and the array[0] will
  2308. * hold the full length.
  2309. * (see include/linux/ring-buffer.h for details on
  2310. * how the ring_buffer_event is structured).
  2311. *
  2312. * Using a temp buffer during filtering and copying it
  2313. * on a matched filter is quicker than writing directly
  2314. * into the ring buffer and then discarding it when
  2315. * it doesn't match. That is because the discard
  2316. * requires several atomic operations to get right.
  2317. * Copying on match and doing nothing on a failed match
  2318. * is still quicker than no copy on match, but having
  2319. * to discard out of the ring buffer on a failed match.
  2320. */
  2321. if ((entry = __this_cpu_read(trace_buffered_event))) {
  2322. int max_len = PAGE_SIZE - struct_size(entry, array, 1);
  2323. val = this_cpu_inc_return(trace_buffered_event_cnt);
  2324. /*
  2325. * Preemption is disabled, but interrupts and NMIs
  2326. * can still come in now. If that happens after
  2327. * the above increment, then it will have to go
  2328. * back to the old method of allocating the event
  2329. * on the ring buffer, and if the filter fails, it
  2330. * will have to call ring_buffer_discard_commit()
  2331. * to remove it.
  2332. *
  2333. * Need to also check the unlikely case that the
  2334. * length is bigger than the temp buffer size.
  2335. * If that happens, then the reserve is pretty much
  2336. * guaranteed to fail, as the ring buffer currently
  2337. * only allows events less than a page. But that may
  2338. * change in the future, so let the ring buffer reserve
  2339. * handle the failure in that case.
  2340. */
  2341. if (val == 1 && likely(len <= max_len)) {
  2342. trace_event_setup(entry, type, trace_ctx);
  2343. entry->array[0] = len;
  2344. /* Return with preemption disabled */
  2345. return entry;
  2346. }
  2347. this_cpu_dec(trace_buffered_event_cnt);
  2348. }
  2349. /* __trace_buffer_lock_reserve() disables preemption */
  2350. preempt_enable_notrace();
  2351. }
  2352. entry = __trace_buffer_lock_reserve(*current_rb, type, len,
  2353. trace_ctx);
  2354. /*
  2355. * If tracing is off, but we have triggers enabled
  2356. * we still need to look at the event data. Use the temp_buffer
  2357. * to store the trace event for the trigger to use. It's recursive
  2358. * safe and will not be recorded anywhere.
  2359. */
  2360. if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
  2361. *current_rb = temp_buffer;
  2362. entry = __trace_buffer_lock_reserve(*current_rb, type, len,
  2363. trace_ctx);
  2364. }
  2365. return entry;
  2366. }
  2367. EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
  2368. static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
  2369. static DEFINE_MUTEX(tracepoint_printk_mutex);
  2370. static void output_printk(struct trace_event_buffer *fbuffer)
  2371. {
  2372. struct trace_event_call *event_call;
  2373. struct trace_event_file *file;
  2374. struct trace_event *event;
  2375. unsigned long flags;
  2376. struct trace_iterator *iter = tracepoint_print_iter;
  2377. /* We should never get here if iter is NULL */
  2378. if (WARN_ON_ONCE(!iter))
  2379. return;
  2380. event_call = fbuffer->trace_file->event_call;
  2381. if (!event_call || !event_call->event.funcs ||
  2382. !event_call->event.funcs->trace)
  2383. return;
  2384. file = fbuffer->trace_file;
  2385. if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
  2386. (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
  2387. !filter_match_preds(file->filter, fbuffer->entry)))
  2388. return;
  2389. event = &fbuffer->trace_file->event_call->event;
  2390. raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
  2391. trace_seq_init(&iter->seq);
  2392. iter->ent = fbuffer->entry;
  2393. event_call->event.funcs->trace(iter, 0, event);
  2394. trace_seq_putc(&iter->seq, 0);
  2395. printk("%s", iter->seq.buffer);
  2396. raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
  2397. }
  2398. int tracepoint_printk_sysctl(struct ctl_table *table, int write,
  2399. void *buffer, size_t *lenp,
  2400. loff_t *ppos)
  2401. {
  2402. int save_tracepoint_printk;
  2403. int ret;
  2404. mutex_lock(&tracepoint_printk_mutex);
  2405. save_tracepoint_printk = tracepoint_printk;
  2406. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2407. /*
  2408. * This will force exiting early, as tracepoint_printk
  2409. * is always zero when tracepoint_printk_iter is not allocated
  2410. */
  2411. if (!tracepoint_print_iter)
  2412. tracepoint_printk = 0;
  2413. if (save_tracepoint_printk == tracepoint_printk)
  2414. goto out;
  2415. if (tracepoint_printk)
  2416. static_key_enable(&tracepoint_printk_key.key);
  2417. else
  2418. static_key_disable(&tracepoint_printk_key.key);
  2419. out:
  2420. mutex_unlock(&tracepoint_printk_mutex);
  2421. return ret;
  2422. }
  2423. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
  2424. {
  2425. enum event_trigger_type tt = ETT_NONE;
  2426. struct trace_event_file *file = fbuffer->trace_file;
  2427. if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event,
  2428. fbuffer->entry, &tt))
  2429. goto discard;
  2430. if (static_key_false(&tracepoint_printk_key.key))
  2431. output_printk(fbuffer);
  2432. if (static_branch_unlikely(&trace_event_exports_enabled))
  2433. ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
  2434. trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer,
  2435. fbuffer->event, fbuffer->trace_ctx, fbuffer->regs);
  2436. discard:
  2437. if (tt)
  2438. event_triggers_post_call(file, tt);
  2439. }
  2440. EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
  2441. /*
  2442. * Skip 3:
  2443. *
  2444. * trace_buffer_unlock_commit_regs()
  2445. * trace_event_buffer_commit()
  2446. * trace_event_raw_event_xxx()
  2447. */
  2448. # define STACK_SKIP 3
  2449. void trace_buffer_unlock_commit_regs(struct trace_array *tr,
  2450. struct trace_buffer *buffer,
  2451. struct ring_buffer_event *event,
  2452. unsigned int trace_ctx,
  2453. struct pt_regs *regs)
  2454. {
  2455. __buffer_unlock_commit(buffer, event);
  2456. /*
  2457. * If regs is not set, then skip the necessary functions.
  2458. * Note, we can still get here via blktrace, wakeup tracer
  2459. * and mmiotrace, but that's ok if they lose a function or
  2460. * two. They are not that meaningful.
  2461. */
  2462. ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs);
  2463. ftrace_trace_userstack(tr, buffer, trace_ctx);
  2464. }
  2465. /*
  2466. * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
  2467. */
  2468. void
  2469. trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
  2470. struct ring_buffer_event *event)
  2471. {
  2472. __buffer_unlock_commit(buffer, event);
  2473. }
  2474. void
  2475. trace_function(struct trace_array *tr, unsigned long ip, unsigned long
  2476. parent_ip, unsigned int trace_ctx)
  2477. {
  2478. struct trace_event_call *call = &event_function;
  2479. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2480. struct ring_buffer_event *event;
  2481. struct ftrace_entry *entry;
  2482. event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
  2483. trace_ctx);
  2484. if (!event)
  2485. return;
  2486. entry = ring_buffer_event_data(event);
  2487. entry->ip = ip;
  2488. entry->parent_ip = parent_ip;
  2489. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2490. if (static_branch_unlikely(&trace_function_exports_enabled))
  2491. ftrace_exports(event, TRACE_EXPORT_FUNCTION);
  2492. __buffer_unlock_commit(buffer, event);
  2493. }
  2494. }
  2495. #ifdef CONFIG_STACKTRACE
  2496. /* Allow 4 levels of nesting: normal, softirq, irq, NMI */
  2497. #define FTRACE_KSTACK_NESTING 4
  2498. #define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
  2499. struct ftrace_stack {
  2500. unsigned long calls[FTRACE_KSTACK_ENTRIES];
  2501. };
  2502. struct ftrace_stacks {
  2503. struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
  2504. };
  2505. static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
  2506. static DEFINE_PER_CPU(int, ftrace_stack_reserve);
  2507. static void __ftrace_trace_stack(struct trace_buffer *buffer,
  2508. unsigned int trace_ctx,
  2509. int skip, struct pt_regs *regs)
  2510. {
  2511. struct trace_event_call *call = &event_kernel_stack;
  2512. struct ring_buffer_event *event;
  2513. unsigned int size, nr_entries;
  2514. struct ftrace_stack *fstack;
  2515. struct stack_entry *entry;
  2516. int stackidx;
  2517. /*
  2518. * Add one, for this function and the call to save_stack_trace()
  2519. * If regs is set, then these functions will not be in the way.
  2520. */
  2521. #ifndef CONFIG_UNWINDER_ORC
  2522. if (!regs)
  2523. skip++;
  2524. #endif
  2525. preempt_disable_notrace();
  2526. stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
  2527. /* This should never happen. If it does, yell once and skip */
  2528. if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
  2529. goto out;
  2530. /*
  2531. * The above __this_cpu_inc_return() is 'atomic' cpu local. An
  2532. * interrupt will either see the value pre increment or post
  2533. * increment. If the interrupt happens pre increment it will have
  2534. * restored the counter when it returns. We just need a barrier to
  2535. * keep gcc from moving things around.
  2536. */
  2537. barrier();
  2538. fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
  2539. size = ARRAY_SIZE(fstack->calls);
  2540. if (regs) {
  2541. nr_entries = stack_trace_save_regs(regs, fstack->calls,
  2542. size, skip);
  2543. } else {
  2544. nr_entries = stack_trace_save(fstack->calls, size, skip);
  2545. }
  2546. size = nr_entries * sizeof(unsigned long);
  2547. event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
  2548. (sizeof(*entry) - sizeof(entry->caller)) + size,
  2549. trace_ctx);
  2550. if (!event)
  2551. goto out;
  2552. entry = ring_buffer_event_data(event);
  2553. memcpy(&entry->caller, fstack->calls, size);
  2554. entry->size = nr_entries;
  2555. if (!call_filter_check_discard(call, entry, buffer, event))
  2556. __buffer_unlock_commit(buffer, event);
  2557. out:
  2558. /* Again, don't let gcc optimize things here */
  2559. barrier();
  2560. __this_cpu_dec(ftrace_stack_reserve);
  2561. preempt_enable_notrace();
  2562. }
  2563. static inline void ftrace_trace_stack(struct trace_array *tr,
  2564. struct trace_buffer *buffer,
  2565. unsigned int trace_ctx,
  2566. int skip, struct pt_regs *regs)
  2567. {
  2568. if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
  2569. return;
  2570. __ftrace_trace_stack(buffer, trace_ctx, skip, regs);
  2571. }
  2572. void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
  2573. int skip)
  2574. {
  2575. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2576. if (rcu_is_watching()) {
  2577. __ftrace_trace_stack(buffer, trace_ctx, skip, NULL);
  2578. return;
  2579. }
  2580. /*
  2581. * When an NMI triggers, RCU is enabled via ct_nmi_enter(),
  2582. * but if the above rcu_is_watching() failed, then the NMI
  2583. * triggered someplace critical, and ct_irq_enter() should
  2584. * not be called from NMI.
  2585. */
  2586. if (unlikely(in_nmi()))
  2587. return;
  2588. ct_irq_enter_irqson();
  2589. __ftrace_trace_stack(buffer, trace_ctx, skip, NULL);
  2590. ct_irq_exit_irqson();
  2591. }
  2592. /**
  2593. * trace_dump_stack - record a stack back trace in the trace buffer
  2594. * @skip: Number of functions to skip (helper handlers)
  2595. */
  2596. void trace_dump_stack(int skip)
  2597. {
  2598. if (tracing_disabled || tracing_selftest_running)
  2599. return;
  2600. #ifndef CONFIG_UNWINDER_ORC
  2601. /* Skip 1 to skip this function. */
  2602. skip++;
  2603. #endif
  2604. __ftrace_trace_stack(global_trace.array_buffer.buffer,
  2605. tracing_gen_ctx(), skip, NULL);
  2606. }
  2607. EXPORT_SYMBOL_GPL(trace_dump_stack);
  2608. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  2609. static DEFINE_PER_CPU(int, user_stack_count);
  2610. static void
  2611. ftrace_trace_userstack(struct trace_array *tr,
  2612. struct trace_buffer *buffer, unsigned int trace_ctx)
  2613. {
  2614. struct trace_event_call *call = &event_user_stack;
  2615. struct ring_buffer_event *event;
  2616. struct userstack_entry *entry;
  2617. if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
  2618. return;
  2619. /*
  2620. * NMIs can not handle page faults, even with fix ups.
  2621. * The save user stack can (and often does) fault.
  2622. */
  2623. if (unlikely(in_nmi()))
  2624. return;
  2625. /*
  2626. * prevent recursion, since the user stack tracing may
  2627. * trigger other kernel events.
  2628. */
  2629. preempt_disable();
  2630. if (__this_cpu_read(user_stack_count))
  2631. goto out;
  2632. __this_cpu_inc(user_stack_count);
  2633. event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  2634. sizeof(*entry), trace_ctx);
  2635. if (!event)
  2636. goto out_drop_count;
  2637. entry = ring_buffer_event_data(event);
  2638. entry->tgid = current->tgid;
  2639. memset(&entry->caller, 0, sizeof(entry->caller));
  2640. stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
  2641. if (!call_filter_check_discard(call, entry, buffer, event))
  2642. __buffer_unlock_commit(buffer, event);
  2643. out_drop_count:
  2644. __this_cpu_dec(user_stack_count);
  2645. out:
  2646. preempt_enable();
  2647. }
  2648. #else /* CONFIG_USER_STACKTRACE_SUPPORT */
  2649. static void ftrace_trace_userstack(struct trace_array *tr,
  2650. struct trace_buffer *buffer,
  2651. unsigned int trace_ctx)
  2652. {
  2653. }
  2654. #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
  2655. #endif /* CONFIG_STACKTRACE */
  2656. static inline void
  2657. func_repeats_set_delta_ts(struct func_repeats_entry *entry,
  2658. unsigned long long delta)
  2659. {
  2660. entry->bottom_delta_ts = delta & U32_MAX;
  2661. entry->top_delta_ts = (delta >> 32);
  2662. }
  2663. void trace_last_func_repeats(struct trace_array *tr,
  2664. struct trace_func_repeats *last_info,
  2665. unsigned int trace_ctx)
  2666. {
  2667. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2668. struct func_repeats_entry *entry;
  2669. struct ring_buffer_event *event;
  2670. u64 delta;
  2671. event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS,
  2672. sizeof(*entry), trace_ctx);
  2673. if (!event)
  2674. return;
  2675. delta = ring_buffer_event_time_stamp(buffer, event) -
  2676. last_info->ts_last_call;
  2677. entry = ring_buffer_event_data(event);
  2678. entry->ip = last_info->ip;
  2679. entry->parent_ip = last_info->parent_ip;
  2680. entry->count = last_info->count;
  2681. func_repeats_set_delta_ts(entry, delta);
  2682. __buffer_unlock_commit(buffer, event);
  2683. }
  2684. /* created for use with alloc_percpu */
  2685. struct trace_buffer_struct {
  2686. int nesting;
  2687. char buffer[4][TRACE_BUF_SIZE];
  2688. };
  2689. static struct trace_buffer_struct __percpu *trace_percpu_buffer;
  2690. /*
  2691. * This allows for lockless recording. If we're nested too deeply, then
  2692. * this returns NULL.
  2693. */
  2694. static char *get_trace_buf(void)
  2695. {
  2696. struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
  2697. if (!trace_percpu_buffer || buffer->nesting >= 4)
  2698. return NULL;
  2699. buffer->nesting++;
  2700. /* Interrupts must see nesting incremented before we use the buffer */
  2701. barrier();
  2702. return &buffer->buffer[buffer->nesting - 1][0];
  2703. }
  2704. static void put_trace_buf(void)
  2705. {
  2706. /* Don't let the decrement of nesting leak before this */
  2707. barrier();
  2708. this_cpu_dec(trace_percpu_buffer->nesting);
  2709. }
  2710. static int alloc_percpu_trace_buffer(void)
  2711. {
  2712. struct trace_buffer_struct __percpu *buffers;
  2713. if (trace_percpu_buffer)
  2714. return 0;
  2715. buffers = alloc_percpu(struct trace_buffer_struct);
  2716. if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
  2717. return -ENOMEM;
  2718. trace_percpu_buffer = buffers;
  2719. return 0;
  2720. }
  2721. static int buffers_allocated;
  2722. void trace_printk_init_buffers(void)
  2723. {
  2724. if (buffers_allocated)
  2725. return;
  2726. if (alloc_percpu_trace_buffer())
  2727. return;
  2728. /* trace_printk() is for debug use only. Don't use it in production. */
  2729. pr_warn("\n");
  2730. pr_warn("**********************************************************\n");
  2731. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2732. pr_warn("** **\n");
  2733. pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
  2734. pr_warn("** **\n");
  2735. pr_warn("** This means that this is a DEBUG kernel and it is **\n");
  2736. pr_warn("** unsafe for production use. **\n");
  2737. pr_warn("** **\n");
  2738. pr_warn("** If you see this message and you are not debugging **\n");
  2739. pr_warn("** the kernel, report this immediately to your vendor! **\n");
  2740. pr_warn("** **\n");
  2741. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2742. pr_warn("**********************************************************\n");
  2743. /* Expand the buffers to set size */
  2744. tracing_update_buffers();
  2745. buffers_allocated = 1;
  2746. /*
  2747. * trace_printk_init_buffers() can be called by modules.
  2748. * If that happens, then we need to start cmdline recording
  2749. * directly here. If the global_trace.buffer is already
  2750. * allocated here, then this was called by module code.
  2751. */
  2752. if (global_trace.array_buffer.buffer)
  2753. tracing_start_cmdline_record();
  2754. }
  2755. EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
  2756. void trace_printk_start_comm(void)
  2757. {
  2758. /* Start tracing comms if trace printk is set */
  2759. if (!buffers_allocated)
  2760. return;
  2761. tracing_start_cmdline_record();
  2762. }
  2763. static void trace_printk_start_stop_comm(int enabled)
  2764. {
  2765. if (!buffers_allocated)
  2766. return;
  2767. if (enabled)
  2768. tracing_start_cmdline_record();
  2769. else
  2770. tracing_stop_cmdline_record();
  2771. }
  2772. /**
  2773. * trace_vbprintk - write binary msg to tracing buffer
  2774. * @ip: The address of the caller
  2775. * @fmt: The string format to write to the buffer
  2776. * @args: Arguments for @fmt
  2777. */
  2778. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  2779. {
  2780. struct trace_event_call *call = &event_bprint;
  2781. struct ring_buffer_event *event;
  2782. struct trace_buffer *buffer;
  2783. struct trace_array *tr = &global_trace;
  2784. struct bprint_entry *entry;
  2785. unsigned int trace_ctx;
  2786. char *tbuffer;
  2787. int len = 0, size;
  2788. if (unlikely(tracing_selftest_running || tracing_disabled))
  2789. return 0;
  2790. /* Don't pollute graph traces with trace_vprintk internals */
  2791. pause_graph_tracing();
  2792. trace_ctx = tracing_gen_ctx();
  2793. preempt_disable_notrace();
  2794. tbuffer = get_trace_buf();
  2795. if (!tbuffer) {
  2796. len = 0;
  2797. goto out_nobuffer;
  2798. }
  2799. len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
  2800. if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
  2801. goto out_put;
  2802. size = sizeof(*entry) + sizeof(u32) * len;
  2803. buffer = tr->array_buffer.buffer;
  2804. ring_buffer_nest_start(buffer);
  2805. event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  2806. trace_ctx);
  2807. if (!event)
  2808. goto out;
  2809. entry = ring_buffer_event_data(event);
  2810. entry->ip = ip;
  2811. entry->fmt = fmt;
  2812. memcpy(entry->buf, tbuffer, sizeof(u32) * len);
  2813. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2814. __buffer_unlock_commit(buffer, event);
  2815. ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL);
  2816. }
  2817. out:
  2818. ring_buffer_nest_end(buffer);
  2819. out_put:
  2820. put_trace_buf();
  2821. out_nobuffer:
  2822. preempt_enable_notrace();
  2823. unpause_graph_tracing();
  2824. return len;
  2825. }
  2826. EXPORT_SYMBOL_GPL(trace_vbprintk);
  2827. __printf(3, 0)
  2828. static int
  2829. __trace_array_vprintk(struct trace_buffer *buffer,
  2830. unsigned long ip, const char *fmt, va_list args)
  2831. {
  2832. struct trace_event_call *call = &event_print;
  2833. struct ring_buffer_event *event;
  2834. int len = 0, size;
  2835. struct print_entry *entry;
  2836. unsigned int trace_ctx;
  2837. char *tbuffer;
  2838. if (tracing_disabled || tracing_selftest_running)
  2839. return 0;
  2840. /* Don't pollute graph traces with trace_vprintk internals */
  2841. pause_graph_tracing();
  2842. trace_ctx = tracing_gen_ctx();
  2843. preempt_disable_notrace();
  2844. tbuffer = get_trace_buf();
  2845. if (!tbuffer) {
  2846. len = 0;
  2847. goto out_nobuffer;
  2848. }
  2849. len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
  2850. size = sizeof(*entry) + len + 1;
  2851. ring_buffer_nest_start(buffer);
  2852. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  2853. trace_ctx);
  2854. if (!event)
  2855. goto out;
  2856. entry = ring_buffer_event_data(event);
  2857. entry->ip = ip;
  2858. memcpy(&entry->buf, tbuffer, len + 1);
  2859. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2860. __buffer_unlock_commit(buffer, event);
  2861. ftrace_trace_stack(&global_trace, buffer, trace_ctx, 6, NULL);
  2862. }
  2863. out:
  2864. ring_buffer_nest_end(buffer);
  2865. put_trace_buf();
  2866. out_nobuffer:
  2867. preempt_enable_notrace();
  2868. unpause_graph_tracing();
  2869. return len;
  2870. }
  2871. __printf(3, 0)
  2872. int trace_array_vprintk(struct trace_array *tr,
  2873. unsigned long ip, const char *fmt, va_list args)
  2874. {
  2875. return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
  2876. }
  2877. /**
  2878. * trace_array_printk - Print a message to a specific instance
  2879. * @tr: The instance trace_array descriptor
  2880. * @ip: The instruction pointer that this is called from.
  2881. * @fmt: The format to print (printf format)
  2882. *
  2883. * If a subsystem sets up its own instance, they have the right to
  2884. * printk strings into their tracing instance buffer using this
  2885. * function. Note, this function will not write into the top level
  2886. * buffer (use trace_printk() for that), as writing into the top level
  2887. * buffer should only have events that can be individually disabled.
  2888. * trace_printk() is only used for debugging a kernel, and should not
  2889. * be ever incorporated in normal use.
  2890. *
  2891. * trace_array_printk() can be used, as it will not add noise to the
  2892. * top level tracing buffer.
  2893. *
  2894. * Note, trace_array_init_printk() must be called on @tr before this
  2895. * can be used.
  2896. */
  2897. __printf(3, 0)
  2898. int trace_array_printk(struct trace_array *tr,
  2899. unsigned long ip, const char *fmt, ...)
  2900. {
  2901. int ret;
  2902. va_list ap;
  2903. if (!tr)
  2904. return -ENOENT;
  2905. /* This is only allowed for created instances */
  2906. if (tr == &global_trace)
  2907. return 0;
  2908. if (!(tr->trace_flags & TRACE_ITER_PRINTK))
  2909. return 0;
  2910. va_start(ap, fmt);
  2911. ret = trace_array_vprintk(tr, ip, fmt, ap);
  2912. va_end(ap);
  2913. return ret;
  2914. }
  2915. EXPORT_SYMBOL_GPL(trace_array_printk);
  2916. /**
  2917. * trace_array_init_printk - Initialize buffers for trace_array_printk()
  2918. * @tr: The trace array to initialize the buffers for
  2919. *
  2920. * As trace_array_printk() only writes into instances, they are OK to
  2921. * have in the kernel (unlike trace_printk()). This needs to be called
  2922. * before trace_array_printk() can be used on a trace_array.
  2923. */
  2924. int trace_array_init_printk(struct trace_array *tr)
  2925. {
  2926. if (!tr)
  2927. return -ENOENT;
  2928. /* This is only allowed for created instances */
  2929. if (tr == &global_trace)
  2930. return -EINVAL;
  2931. return alloc_percpu_trace_buffer();
  2932. }
  2933. EXPORT_SYMBOL_GPL(trace_array_init_printk);
  2934. __printf(3, 4)
  2935. int trace_array_printk_buf(struct trace_buffer *buffer,
  2936. unsigned long ip, const char *fmt, ...)
  2937. {
  2938. int ret;
  2939. va_list ap;
  2940. if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
  2941. return 0;
  2942. va_start(ap, fmt);
  2943. ret = __trace_array_vprintk(buffer, ip, fmt, ap);
  2944. va_end(ap);
  2945. return ret;
  2946. }
  2947. __printf(2, 0)
  2948. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2949. {
  2950. return trace_array_vprintk(&global_trace, ip, fmt, args);
  2951. }
  2952. EXPORT_SYMBOL_GPL(trace_vprintk);
  2953. static void trace_iterator_increment(struct trace_iterator *iter)
  2954. {
  2955. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
  2956. iter->idx++;
  2957. if (buf_iter)
  2958. ring_buffer_iter_advance(buf_iter);
  2959. }
  2960. static struct trace_entry *
  2961. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  2962. unsigned long *lost_events)
  2963. {
  2964. struct ring_buffer_event *event;
  2965. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
  2966. if (buf_iter) {
  2967. event = ring_buffer_iter_peek(buf_iter, ts);
  2968. if (lost_events)
  2969. *lost_events = ring_buffer_iter_dropped(buf_iter) ?
  2970. (unsigned long)-1 : 0;
  2971. } else {
  2972. event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
  2973. lost_events);
  2974. }
  2975. if (event) {
  2976. iter->ent_size = ring_buffer_event_length(event);
  2977. return ring_buffer_event_data(event);
  2978. }
  2979. iter->ent_size = 0;
  2980. return NULL;
  2981. }
  2982. static struct trace_entry *
  2983. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  2984. unsigned long *missing_events, u64 *ent_ts)
  2985. {
  2986. struct trace_buffer *buffer = iter->array_buffer->buffer;
  2987. struct trace_entry *ent, *next = NULL;
  2988. unsigned long lost_events = 0, next_lost = 0;
  2989. int cpu_file = iter->cpu_file;
  2990. u64 next_ts = 0, ts;
  2991. int next_cpu = -1;
  2992. int next_size = 0;
  2993. int cpu;
  2994. /*
  2995. * If we are in a per_cpu trace file, don't bother by iterating over
  2996. * all cpu and peek directly.
  2997. */
  2998. if (cpu_file > RING_BUFFER_ALL_CPUS) {
  2999. if (ring_buffer_empty_cpu(buffer, cpu_file))
  3000. return NULL;
  3001. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  3002. if (ent_cpu)
  3003. *ent_cpu = cpu_file;
  3004. return ent;
  3005. }
  3006. for_each_tracing_cpu(cpu) {
  3007. if (ring_buffer_empty_cpu(buffer, cpu))
  3008. continue;
  3009. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  3010. /*
  3011. * Pick the entry with the smallest timestamp:
  3012. */
  3013. if (ent && (!next || ts < next_ts)) {
  3014. next = ent;
  3015. next_cpu = cpu;
  3016. next_ts = ts;
  3017. next_lost = lost_events;
  3018. next_size = iter->ent_size;
  3019. }
  3020. }
  3021. iter->ent_size = next_size;
  3022. if (ent_cpu)
  3023. *ent_cpu = next_cpu;
  3024. if (ent_ts)
  3025. *ent_ts = next_ts;
  3026. if (missing_events)
  3027. *missing_events = next_lost;
  3028. return next;
  3029. }
  3030. #define STATIC_FMT_BUF_SIZE 128
  3031. static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
  3032. static char *trace_iter_expand_format(struct trace_iterator *iter)
  3033. {
  3034. char *tmp;
  3035. /*
  3036. * iter->tr is NULL when used with tp_printk, which makes
  3037. * this get called where it is not safe to call krealloc().
  3038. */
  3039. if (!iter->tr || iter->fmt == static_fmt_buf)
  3040. return NULL;
  3041. tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
  3042. GFP_KERNEL);
  3043. if (tmp) {
  3044. iter->fmt_size += STATIC_FMT_BUF_SIZE;
  3045. iter->fmt = tmp;
  3046. }
  3047. return tmp;
  3048. }
  3049. /* Returns true if the string is safe to dereference from an event */
  3050. static bool trace_safe_str(struct trace_iterator *iter, const char *str,
  3051. bool star, int len)
  3052. {
  3053. unsigned long addr = (unsigned long)str;
  3054. struct trace_event *trace_event;
  3055. struct trace_event_call *event;
  3056. /* Ignore strings with no length */
  3057. if (star && !len)
  3058. return true;
  3059. /* OK if part of the event data */
  3060. if ((addr >= (unsigned long)iter->ent) &&
  3061. (addr < (unsigned long)iter->ent + iter->ent_size))
  3062. return true;
  3063. /* OK if part of the temp seq buffer */
  3064. if ((addr >= (unsigned long)iter->tmp_seq.buffer) &&
  3065. (addr < (unsigned long)iter->tmp_seq.buffer + PAGE_SIZE))
  3066. return true;
  3067. /* Core rodata can not be freed */
  3068. if (is_kernel_rodata(addr))
  3069. return true;
  3070. if (trace_is_tracepoint_string(str))
  3071. return true;
  3072. /*
  3073. * Now this could be a module event, referencing core module
  3074. * data, which is OK.
  3075. */
  3076. if (!iter->ent)
  3077. return false;
  3078. trace_event = ftrace_find_event(iter->ent->type);
  3079. if (!trace_event)
  3080. return false;
  3081. event = container_of(trace_event, struct trace_event_call, event);
  3082. if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module)
  3083. return false;
  3084. /* Would rather have rodata, but this will suffice */
  3085. if (within_module_core(addr, event->module))
  3086. return true;
  3087. return false;
  3088. }
  3089. static const char *show_buffer(struct trace_seq *s)
  3090. {
  3091. struct seq_buf *seq = &s->seq;
  3092. seq_buf_terminate(seq);
  3093. return seq->buffer;
  3094. }
  3095. static DEFINE_STATIC_KEY_FALSE(trace_no_verify);
  3096. static int test_can_verify_check(const char *fmt, ...)
  3097. {
  3098. char buf[16];
  3099. va_list ap;
  3100. int ret;
  3101. /*
  3102. * The verifier is dependent on vsnprintf() modifies the va_list
  3103. * passed to it, where it is sent as a reference. Some architectures
  3104. * (like x86_32) passes it by value, which means that vsnprintf()
  3105. * does not modify the va_list passed to it, and the verifier
  3106. * would then need to be able to understand all the values that
  3107. * vsnprintf can use. If it is passed by value, then the verifier
  3108. * is disabled.
  3109. */
  3110. va_start(ap, fmt);
  3111. vsnprintf(buf, 16, "%d", ap);
  3112. ret = va_arg(ap, int);
  3113. va_end(ap);
  3114. return ret;
  3115. }
  3116. static void test_can_verify(void)
  3117. {
  3118. if (!test_can_verify_check("%d %d", 0, 1)) {
  3119. pr_info("trace event string verifier disabled\n");
  3120. static_branch_inc(&trace_no_verify);
  3121. }
  3122. }
  3123. /**
  3124. * trace_check_vprintf - Check dereferenced strings while writing to the seq buffer
  3125. * @iter: The iterator that holds the seq buffer and the event being printed
  3126. * @fmt: The format used to print the event
  3127. * @ap: The va_list holding the data to print from @fmt.
  3128. *
  3129. * This writes the data into the @iter->seq buffer using the data from
  3130. * @fmt and @ap. If the format has a %s, then the source of the string
  3131. * is examined to make sure it is safe to print, otherwise it will
  3132. * warn and print "[UNSAFE MEMORY]" in place of the dereferenced string
  3133. * pointer.
  3134. */
  3135. void trace_check_vprintf(struct trace_iterator *iter, const char *fmt,
  3136. va_list ap)
  3137. {
  3138. const char *p = fmt;
  3139. const char *str;
  3140. int i, j;
  3141. if (WARN_ON_ONCE(!fmt))
  3142. return;
  3143. if (static_branch_unlikely(&trace_no_verify))
  3144. goto print;
  3145. /* Don't bother checking when doing a ftrace_dump() */
  3146. if (iter->fmt == static_fmt_buf)
  3147. goto print;
  3148. while (*p) {
  3149. bool star = false;
  3150. int len = 0;
  3151. j = 0;
  3152. /* We only care about %s and variants */
  3153. for (i = 0; p[i]; i++) {
  3154. if (i + 1 >= iter->fmt_size) {
  3155. /*
  3156. * If we can't expand the copy buffer,
  3157. * just print it.
  3158. */
  3159. if (!trace_iter_expand_format(iter))
  3160. goto print;
  3161. }
  3162. if (p[i] == '\\' && p[i+1]) {
  3163. i++;
  3164. continue;
  3165. }
  3166. if (p[i] == '%') {
  3167. /* Need to test cases like %08.*s */
  3168. for (j = 1; p[i+j]; j++) {
  3169. if (isdigit(p[i+j]) ||
  3170. p[i+j] == '.')
  3171. continue;
  3172. if (p[i+j] == '*') {
  3173. star = true;
  3174. continue;
  3175. }
  3176. break;
  3177. }
  3178. if (p[i+j] == 's')
  3179. break;
  3180. star = false;
  3181. }
  3182. j = 0;
  3183. }
  3184. /* If no %s found then just print normally */
  3185. if (!p[i])
  3186. break;
  3187. /* Copy up to the %s, and print that */
  3188. strncpy(iter->fmt, p, i);
  3189. iter->fmt[i] = '\0';
  3190. trace_seq_vprintf(&iter->seq, iter->fmt, ap);
  3191. /*
  3192. * If iter->seq is full, the above call no longer guarantees
  3193. * that ap is in sync with fmt processing, and further calls
  3194. * to va_arg() can return wrong positional arguments.
  3195. *
  3196. * Ensure that ap is no longer used in this case.
  3197. */
  3198. if (iter->seq.full) {
  3199. p = "";
  3200. break;
  3201. }
  3202. if (star)
  3203. len = va_arg(ap, int);
  3204. /* The ap now points to the string data of the %s */
  3205. str = va_arg(ap, const char *);
  3206. /*
  3207. * If you hit this warning, it is likely that the
  3208. * trace event in question used %s on a string that
  3209. * was saved at the time of the event, but may not be
  3210. * around when the trace is read. Use __string(),
  3211. * __assign_str() and __get_str() helpers in the TRACE_EVENT()
  3212. * instead. See samples/trace_events/trace-events-sample.h
  3213. * for reference.
  3214. */
  3215. if (WARN_ONCE(!trace_safe_str(iter, str, star, len),
  3216. "fmt: '%s' current_buffer: '%s'",
  3217. fmt, show_buffer(&iter->seq))) {
  3218. int ret;
  3219. /* Try to safely read the string */
  3220. if (star) {
  3221. if (len + 1 > iter->fmt_size)
  3222. len = iter->fmt_size - 1;
  3223. if (len < 0)
  3224. len = 0;
  3225. ret = copy_from_kernel_nofault(iter->fmt, str, len);
  3226. iter->fmt[len] = 0;
  3227. star = false;
  3228. } else {
  3229. ret = strncpy_from_kernel_nofault(iter->fmt, str,
  3230. iter->fmt_size);
  3231. }
  3232. if (ret < 0)
  3233. trace_seq_printf(&iter->seq, "(0x%px)", str);
  3234. else
  3235. trace_seq_printf(&iter->seq, "(0x%px:%s)",
  3236. str, iter->fmt);
  3237. str = "[UNSAFE-MEMORY]";
  3238. strcpy(iter->fmt, "%s");
  3239. } else {
  3240. strncpy(iter->fmt, p + i, j + 1);
  3241. iter->fmt[j+1] = '\0';
  3242. }
  3243. if (star)
  3244. trace_seq_printf(&iter->seq, iter->fmt, len, str);
  3245. else
  3246. trace_seq_printf(&iter->seq, iter->fmt, str);
  3247. p += i + j + 1;
  3248. }
  3249. print:
  3250. if (*p)
  3251. trace_seq_vprintf(&iter->seq, p, ap);
  3252. }
  3253. const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
  3254. {
  3255. const char *p, *new_fmt;
  3256. char *q;
  3257. if (WARN_ON_ONCE(!fmt))
  3258. return fmt;
  3259. if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR)
  3260. return fmt;
  3261. p = fmt;
  3262. new_fmt = q = iter->fmt;
  3263. while (*p) {
  3264. if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
  3265. if (!trace_iter_expand_format(iter))
  3266. return fmt;
  3267. q += iter->fmt - new_fmt;
  3268. new_fmt = iter->fmt;
  3269. }
  3270. *q++ = *p++;
  3271. /* Replace %p with %px */
  3272. if (p[-1] == '%') {
  3273. if (p[0] == '%') {
  3274. *q++ = *p++;
  3275. } else if (p[0] == 'p' && !isalnum(p[1])) {
  3276. *q++ = *p++;
  3277. *q++ = 'x';
  3278. }
  3279. }
  3280. }
  3281. *q = '\0';
  3282. return new_fmt;
  3283. }
  3284. #define STATIC_TEMP_BUF_SIZE 128
  3285. static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
  3286. /* Find the next real entry, without updating the iterator itself */
  3287. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  3288. int *ent_cpu, u64 *ent_ts)
  3289. {
  3290. /* __find_next_entry will reset ent_size */
  3291. int ent_size = iter->ent_size;
  3292. struct trace_entry *entry;
  3293. /*
  3294. * If called from ftrace_dump(), then the iter->temp buffer
  3295. * will be the static_temp_buf and not created from kmalloc.
  3296. * If the entry size is greater than the buffer, we can
  3297. * not save it. Just return NULL in that case. This is only
  3298. * used to add markers when two consecutive events' time
  3299. * stamps have a large delta. See trace_print_lat_context()
  3300. */
  3301. if (iter->temp == static_temp_buf &&
  3302. STATIC_TEMP_BUF_SIZE < ent_size)
  3303. return NULL;
  3304. /*
  3305. * The __find_next_entry() may call peek_next_entry(), which may
  3306. * call ring_buffer_peek() that may make the contents of iter->ent
  3307. * undefined. Need to copy iter->ent now.
  3308. */
  3309. if (iter->ent && iter->ent != iter->temp) {
  3310. if ((!iter->temp || iter->temp_size < iter->ent_size) &&
  3311. !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
  3312. void *temp;
  3313. temp = kmalloc(iter->ent_size, GFP_KERNEL);
  3314. if (!temp)
  3315. return NULL;
  3316. kfree(iter->temp);
  3317. iter->temp = temp;
  3318. iter->temp_size = iter->ent_size;
  3319. }
  3320. memcpy(iter->temp, iter->ent, iter->ent_size);
  3321. iter->ent = iter->temp;
  3322. }
  3323. entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  3324. /* Put back the original ent_size */
  3325. iter->ent_size = ent_size;
  3326. return entry;
  3327. }
  3328. /* Find the next real entry, and increment the iterator to the next entry */
  3329. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  3330. {
  3331. iter->ent = __find_next_entry(iter, &iter->cpu,
  3332. &iter->lost_events, &iter->ts);
  3333. if (iter->ent)
  3334. trace_iterator_increment(iter);
  3335. return iter->ent ? iter : NULL;
  3336. }
  3337. static void trace_consume(struct trace_iterator *iter)
  3338. {
  3339. ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
  3340. &iter->lost_events);
  3341. }
  3342. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  3343. {
  3344. struct trace_iterator *iter = m->private;
  3345. int i = (int)*pos;
  3346. void *ent;
  3347. WARN_ON_ONCE(iter->leftover);
  3348. (*pos)++;
  3349. /* can't go backwards */
  3350. if (iter->idx > i)
  3351. return NULL;
  3352. if (iter->idx < 0)
  3353. ent = trace_find_next_entry_inc(iter);
  3354. else
  3355. ent = iter;
  3356. while (ent && iter->idx < i)
  3357. ent = trace_find_next_entry_inc(iter);
  3358. iter->pos = *pos;
  3359. return ent;
  3360. }
  3361. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  3362. {
  3363. struct ring_buffer_iter *buf_iter;
  3364. unsigned long entries = 0;
  3365. u64 ts;
  3366. per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
  3367. buf_iter = trace_buffer_iter(iter, cpu);
  3368. if (!buf_iter)
  3369. return;
  3370. ring_buffer_iter_reset(buf_iter);
  3371. /*
  3372. * We could have the case with the max latency tracers
  3373. * that a reset never took place on a cpu. This is evident
  3374. * by the timestamp being before the start of the buffer.
  3375. */
  3376. while (ring_buffer_iter_peek(buf_iter, &ts)) {
  3377. if (ts >= iter->array_buffer->time_start)
  3378. break;
  3379. entries++;
  3380. ring_buffer_iter_advance(buf_iter);
  3381. }
  3382. per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
  3383. }
  3384. /*
  3385. * The current tracer is copied to avoid a global locking
  3386. * all around.
  3387. */
  3388. static void *s_start(struct seq_file *m, loff_t *pos)
  3389. {
  3390. struct trace_iterator *iter = m->private;
  3391. struct trace_array *tr = iter->tr;
  3392. int cpu_file = iter->cpu_file;
  3393. void *p = NULL;
  3394. loff_t l = 0;
  3395. int cpu;
  3396. /*
  3397. * copy the tracer to avoid using a global lock all around.
  3398. * iter->trace is a copy of current_trace, the pointer to the
  3399. * name may be used instead of a strcmp(), as iter->trace->name
  3400. * will point to the same string as current_trace->name.
  3401. */
  3402. mutex_lock(&trace_types_lock);
  3403. if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) {
  3404. /* Close iter->trace before switching to the new current tracer */
  3405. if (iter->trace->close)
  3406. iter->trace->close(iter);
  3407. *iter->trace = *tr->current_trace;
  3408. /* Reopen the new current tracer */
  3409. if (iter->trace->open)
  3410. iter->trace->open(iter);
  3411. }
  3412. mutex_unlock(&trace_types_lock);
  3413. #ifdef CONFIG_TRACER_MAX_TRACE
  3414. if (iter->snapshot && iter->trace->use_max_tr)
  3415. return ERR_PTR(-EBUSY);
  3416. #endif
  3417. if (*pos != iter->pos) {
  3418. iter->ent = NULL;
  3419. iter->cpu = 0;
  3420. iter->idx = -1;
  3421. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  3422. for_each_tracing_cpu(cpu)
  3423. tracing_iter_reset(iter, cpu);
  3424. } else
  3425. tracing_iter_reset(iter, cpu_file);
  3426. iter->leftover = 0;
  3427. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  3428. ;
  3429. } else {
  3430. /*
  3431. * If we overflowed the seq_file before, then we want
  3432. * to just reuse the trace_seq buffer again.
  3433. */
  3434. if (iter->leftover)
  3435. p = iter;
  3436. else {
  3437. l = *pos - 1;
  3438. p = s_next(m, p, &l);
  3439. }
  3440. }
  3441. trace_event_read_lock();
  3442. trace_access_lock(cpu_file);
  3443. return p;
  3444. }
  3445. static void s_stop(struct seq_file *m, void *p)
  3446. {
  3447. struct trace_iterator *iter = m->private;
  3448. #ifdef CONFIG_TRACER_MAX_TRACE
  3449. if (iter->snapshot && iter->trace->use_max_tr)
  3450. return;
  3451. #endif
  3452. trace_access_unlock(iter->cpu_file);
  3453. trace_event_read_unlock();
  3454. }
  3455. static void
  3456. get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
  3457. unsigned long *entries, int cpu)
  3458. {
  3459. unsigned long count;
  3460. count = ring_buffer_entries_cpu(buf->buffer, cpu);
  3461. /*
  3462. * If this buffer has skipped entries, then we hold all
  3463. * entries for the trace and we need to ignore the
  3464. * ones before the time stamp.
  3465. */
  3466. if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
  3467. count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
  3468. /* total is the same as the entries */
  3469. *total = count;
  3470. } else
  3471. *total = count +
  3472. ring_buffer_overrun_cpu(buf->buffer, cpu);
  3473. *entries = count;
  3474. }
  3475. static void
  3476. get_total_entries(struct array_buffer *buf,
  3477. unsigned long *total, unsigned long *entries)
  3478. {
  3479. unsigned long t, e;
  3480. int cpu;
  3481. *total = 0;
  3482. *entries = 0;
  3483. for_each_tracing_cpu(cpu) {
  3484. get_total_entries_cpu(buf, &t, &e, cpu);
  3485. *total += t;
  3486. *entries += e;
  3487. }
  3488. }
  3489. unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
  3490. {
  3491. unsigned long total, entries;
  3492. if (!tr)
  3493. tr = &global_trace;
  3494. get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
  3495. return entries;
  3496. }
  3497. unsigned long trace_total_entries(struct trace_array *tr)
  3498. {
  3499. unsigned long total, entries;
  3500. if (!tr)
  3501. tr = &global_trace;
  3502. get_total_entries(&tr->array_buffer, &total, &entries);
  3503. return entries;
  3504. }
  3505. static void print_lat_help_header(struct seq_file *m)
  3506. {
  3507. seq_puts(m, "# _------=> CPU# \n"
  3508. "# / _-----=> irqs-off/BH-disabled\n"
  3509. "# | / _----=> need-resched \n"
  3510. "# || / _---=> hardirq/softirq \n"
  3511. "# ||| / _--=> preempt-depth \n"
  3512. "# |||| / delay \n"
  3513. "# cmd pid ||||| time | caller \n"
  3514. "# \\ / ||||| \\ | / \n");
  3515. }
  3516. static void print_event_info(struct array_buffer *buf, struct seq_file *m)
  3517. {
  3518. unsigned long total;
  3519. unsigned long entries;
  3520. get_total_entries(buf, &total, &entries);
  3521. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  3522. entries, total, num_online_cpus());
  3523. seq_puts(m, "#\n");
  3524. }
  3525. static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
  3526. unsigned int flags)
  3527. {
  3528. bool tgid = flags & TRACE_ITER_RECORD_TGID;
  3529. print_event_info(buf, m);
  3530. seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
  3531. seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
  3532. }
  3533. static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
  3534. unsigned int flags)
  3535. {
  3536. bool tgid = flags & TRACE_ITER_RECORD_TGID;
  3537. static const char space[] = " ";
  3538. int prec = tgid ? 12 : 2;
  3539. print_event_info(buf, m);
  3540. seq_printf(m, "# %.*s _-----=> irqs-off/BH-disabled\n", prec, space);
  3541. seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
  3542. seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
  3543. seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
  3544. seq_printf(m, "# %.*s||| / delay\n", prec, space);
  3545. seq_printf(m, "# TASK-PID %.*s CPU# |||| TIMESTAMP FUNCTION\n", prec, " TGID ");
  3546. seq_printf(m, "# | | %.*s | |||| | |\n", prec, " | ");
  3547. }
  3548. void
  3549. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  3550. {
  3551. unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
  3552. struct array_buffer *buf = iter->array_buffer;
  3553. struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
  3554. struct tracer *type = iter->trace;
  3555. unsigned long entries;
  3556. unsigned long total;
  3557. const char *name = type->name;
  3558. get_total_entries(buf, &total, &entries);
  3559. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  3560. name, UTS_RELEASE);
  3561. seq_puts(m, "# -----------------------------------"
  3562. "---------------------------------\n");
  3563. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  3564. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  3565. nsecs_to_usecs(data->saved_latency),
  3566. entries,
  3567. total,
  3568. buf->cpu,
  3569. preempt_model_none() ? "server" :
  3570. preempt_model_voluntary() ? "desktop" :
  3571. preempt_model_full() ? "preempt" :
  3572. preempt_model_rt() ? "preempt_rt" :
  3573. "unknown",
  3574. /* These are reserved for later use */
  3575. 0, 0, 0, 0);
  3576. #ifdef CONFIG_SMP
  3577. seq_printf(m, " #P:%d)\n", num_online_cpus());
  3578. #else
  3579. seq_puts(m, ")\n");
  3580. #endif
  3581. seq_puts(m, "# -----------------\n");
  3582. seq_printf(m, "# | task: %.16s-%d "
  3583. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  3584. data->comm, data->pid,
  3585. from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
  3586. data->policy, data->rt_priority);
  3587. seq_puts(m, "# -----------------\n");
  3588. if (data->critical_start) {
  3589. seq_puts(m, "# => started at: ");
  3590. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  3591. trace_print_seq(m, &iter->seq);
  3592. seq_puts(m, "\n# => ended at: ");
  3593. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  3594. trace_print_seq(m, &iter->seq);
  3595. seq_puts(m, "\n#\n");
  3596. }
  3597. seq_puts(m, "#\n");
  3598. }
  3599. static void test_cpu_buff_start(struct trace_iterator *iter)
  3600. {
  3601. struct trace_seq *s = &iter->seq;
  3602. struct trace_array *tr = iter->tr;
  3603. if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
  3604. return;
  3605. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  3606. return;
  3607. if (cpumask_available(iter->started) &&
  3608. cpumask_test_cpu(iter->cpu, iter->started))
  3609. return;
  3610. if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
  3611. return;
  3612. if (cpumask_available(iter->started))
  3613. cpumask_set_cpu(iter->cpu, iter->started);
  3614. /* Don't print started cpu buffer for the first entry of the trace */
  3615. if (iter->idx > 1)
  3616. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  3617. iter->cpu);
  3618. }
  3619. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  3620. {
  3621. struct trace_array *tr = iter->tr;
  3622. struct trace_seq *s = &iter->seq;
  3623. unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
  3624. struct trace_entry *entry;
  3625. struct trace_event *event;
  3626. entry = iter->ent;
  3627. test_cpu_buff_start(iter);
  3628. event = ftrace_find_event(entry->type);
  3629. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3630. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  3631. trace_print_lat_context(iter);
  3632. else
  3633. trace_print_context(iter);
  3634. }
  3635. if (trace_seq_has_overflowed(s))
  3636. return TRACE_TYPE_PARTIAL_LINE;
  3637. if (event)
  3638. return event->funcs->trace(iter, sym_flags, event);
  3639. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  3640. return trace_handle_return(s);
  3641. }
  3642. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  3643. {
  3644. struct trace_array *tr = iter->tr;
  3645. struct trace_seq *s = &iter->seq;
  3646. struct trace_entry *entry;
  3647. struct trace_event *event;
  3648. entry = iter->ent;
  3649. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
  3650. trace_seq_printf(s, "%d %d %llu ",
  3651. entry->pid, iter->cpu, iter->ts);
  3652. if (trace_seq_has_overflowed(s))
  3653. return TRACE_TYPE_PARTIAL_LINE;
  3654. event = ftrace_find_event(entry->type);
  3655. if (event)
  3656. return event->funcs->raw(iter, 0, event);
  3657. trace_seq_printf(s, "%d ?\n", entry->type);
  3658. return trace_handle_return(s);
  3659. }
  3660. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  3661. {
  3662. struct trace_array *tr = iter->tr;
  3663. struct trace_seq *s = &iter->seq;
  3664. unsigned char newline = '\n';
  3665. struct trace_entry *entry;
  3666. struct trace_event *event;
  3667. entry = iter->ent;
  3668. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3669. SEQ_PUT_HEX_FIELD(s, entry->pid);
  3670. SEQ_PUT_HEX_FIELD(s, iter->cpu);
  3671. SEQ_PUT_HEX_FIELD(s, iter->ts);
  3672. if (trace_seq_has_overflowed(s))
  3673. return TRACE_TYPE_PARTIAL_LINE;
  3674. }
  3675. event = ftrace_find_event(entry->type);
  3676. if (event) {
  3677. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  3678. if (ret != TRACE_TYPE_HANDLED)
  3679. return ret;
  3680. }
  3681. SEQ_PUT_FIELD(s, newline);
  3682. return trace_handle_return(s);
  3683. }
  3684. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  3685. {
  3686. struct trace_array *tr = iter->tr;
  3687. struct trace_seq *s = &iter->seq;
  3688. struct trace_entry *entry;
  3689. struct trace_event *event;
  3690. entry = iter->ent;
  3691. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3692. SEQ_PUT_FIELD(s, entry->pid);
  3693. SEQ_PUT_FIELD(s, iter->cpu);
  3694. SEQ_PUT_FIELD(s, iter->ts);
  3695. if (trace_seq_has_overflowed(s))
  3696. return TRACE_TYPE_PARTIAL_LINE;
  3697. }
  3698. event = ftrace_find_event(entry->type);
  3699. return event ? event->funcs->binary(iter, 0, event) :
  3700. TRACE_TYPE_HANDLED;
  3701. }
  3702. int trace_empty(struct trace_iterator *iter)
  3703. {
  3704. struct ring_buffer_iter *buf_iter;
  3705. int cpu;
  3706. /* If we are looking at one CPU buffer, only check that one */
  3707. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3708. cpu = iter->cpu_file;
  3709. buf_iter = trace_buffer_iter(iter, cpu);
  3710. if (buf_iter) {
  3711. if (!ring_buffer_iter_empty(buf_iter))
  3712. return 0;
  3713. } else {
  3714. if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
  3715. return 0;
  3716. }
  3717. return 1;
  3718. }
  3719. for_each_tracing_cpu(cpu) {
  3720. buf_iter = trace_buffer_iter(iter, cpu);
  3721. if (buf_iter) {
  3722. if (!ring_buffer_iter_empty(buf_iter))
  3723. return 0;
  3724. } else {
  3725. if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
  3726. return 0;
  3727. }
  3728. }
  3729. return 1;
  3730. }
  3731. /* Called with trace_event_read_lock() held. */
  3732. enum print_line_t print_trace_line(struct trace_iterator *iter)
  3733. {
  3734. struct trace_array *tr = iter->tr;
  3735. unsigned long trace_flags = tr->trace_flags;
  3736. enum print_line_t ret;
  3737. if (iter->lost_events) {
  3738. if (iter->lost_events == (unsigned long)-1)
  3739. trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
  3740. iter->cpu);
  3741. else
  3742. trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  3743. iter->cpu, iter->lost_events);
  3744. if (trace_seq_has_overflowed(&iter->seq))
  3745. return TRACE_TYPE_PARTIAL_LINE;
  3746. }
  3747. if (iter->trace && iter->trace->print_line) {
  3748. ret = iter->trace->print_line(iter);
  3749. if (ret != TRACE_TYPE_UNHANDLED)
  3750. return ret;
  3751. }
  3752. if (iter->ent->type == TRACE_BPUTS &&
  3753. trace_flags & TRACE_ITER_PRINTK &&
  3754. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3755. return trace_print_bputs_msg_only(iter);
  3756. if (iter->ent->type == TRACE_BPRINT &&
  3757. trace_flags & TRACE_ITER_PRINTK &&
  3758. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3759. return trace_print_bprintk_msg_only(iter);
  3760. if (iter->ent->type == TRACE_PRINT &&
  3761. trace_flags & TRACE_ITER_PRINTK &&
  3762. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3763. return trace_print_printk_msg_only(iter);
  3764. if (trace_flags & TRACE_ITER_BIN)
  3765. return print_bin_fmt(iter);
  3766. if (trace_flags & TRACE_ITER_HEX)
  3767. return print_hex_fmt(iter);
  3768. if (trace_flags & TRACE_ITER_RAW)
  3769. return print_raw_fmt(iter);
  3770. return print_trace_fmt(iter);
  3771. }
  3772. void trace_latency_header(struct seq_file *m)
  3773. {
  3774. struct trace_iterator *iter = m->private;
  3775. struct trace_array *tr = iter->tr;
  3776. /* print nothing if the buffers are empty */
  3777. if (trace_empty(iter))
  3778. return;
  3779. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  3780. print_trace_header(m, iter);
  3781. if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
  3782. print_lat_help_header(m);
  3783. }
  3784. void trace_default_header(struct seq_file *m)
  3785. {
  3786. struct trace_iterator *iter = m->private;
  3787. struct trace_array *tr = iter->tr;
  3788. unsigned long trace_flags = tr->trace_flags;
  3789. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  3790. return;
  3791. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  3792. /* print nothing if the buffers are empty */
  3793. if (trace_empty(iter))
  3794. return;
  3795. print_trace_header(m, iter);
  3796. if (!(trace_flags & TRACE_ITER_VERBOSE))
  3797. print_lat_help_header(m);
  3798. } else {
  3799. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  3800. if (trace_flags & TRACE_ITER_IRQ_INFO)
  3801. print_func_help_header_irq(iter->array_buffer,
  3802. m, trace_flags);
  3803. else
  3804. print_func_help_header(iter->array_buffer, m,
  3805. trace_flags);
  3806. }
  3807. }
  3808. }
  3809. static void test_ftrace_alive(struct seq_file *m)
  3810. {
  3811. if (!ftrace_is_dead())
  3812. return;
  3813. seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
  3814. "# MAY BE MISSING FUNCTION EVENTS\n");
  3815. }
  3816. #ifdef CONFIG_TRACER_MAX_TRACE
  3817. static void show_snapshot_main_help(struct seq_file *m)
  3818. {
  3819. seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
  3820. "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
  3821. "# Takes a snapshot of the main buffer.\n"
  3822. "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
  3823. "# (Doesn't have to be '2' works with any number that\n"
  3824. "# is not a '0' or '1')\n");
  3825. }
  3826. static void show_snapshot_percpu_help(struct seq_file *m)
  3827. {
  3828. seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
  3829. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3830. seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
  3831. "# Takes a snapshot of the main buffer for this cpu.\n");
  3832. #else
  3833. seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
  3834. "# Must use main snapshot file to allocate.\n");
  3835. #endif
  3836. seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
  3837. "# (Doesn't have to be '2' works with any number that\n"
  3838. "# is not a '0' or '1')\n");
  3839. }
  3840. static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
  3841. {
  3842. if (iter->tr->allocated_snapshot)
  3843. seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
  3844. else
  3845. seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
  3846. seq_puts(m, "# Snapshot commands:\n");
  3847. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3848. show_snapshot_main_help(m);
  3849. else
  3850. show_snapshot_percpu_help(m);
  3851. }
  3852. #else
  3853. /* Should never be called */
  3854. static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
  3855. #endif
  3856. static int s_show(struct seq_file *m, void *v)
  3857. {
  3858. struct trace_iterator *iter = v;
  3859. int ret;
  3860. if (iter->ent == NULL) {
  3861. if (iter->tr) {
  3862. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  3863. seq_puts(m, "#\n");
  3864. test_ftrace_alive(m);
  3865. }
  3866. if (iter->snapshot && trace_empty(iter))
  3867. print_snapshot_help(m, iter);
  3868. else if (iter->trace && iter->trace->print_header)
  3869. iter->trace->print_header(m);
  3870. else
  3871. trace_default_header(m);
  3872. } else if (iter->leftover) {
  3873. /*
  3874. * If we filled the seq_file buffer earlier, we
  3875. * want to just show it now.
  3876. */
  3877. ret = trace_print_seq(m, &iter->seq);
  3878. /* ret should this time be zero, but you never know */
  3879. iter->leftover = ret;
  3880. } else {
  3881. print_trace_line(iter);
  3882. ret = trace_print_seq(m, &iter->seq);
  3883. /*
  3884. * If we overflow the seq_file buffer, then it will
  3885. * ask us for this data again at start up.
  3886. * Use that instead.
  3887. * ret is 0 if seq_file write succeeded.
  3888. * -1 otherwise.
  3889. */
  3890. iter->leftover = ret;
  3891. }
  3892. return 0;
  3893. }
  3894. /*
  3895. * Should be used after trace_array_get(), trace_types_lock
  3896. * ensures that i_cdev was already initialized.
  3897. */
  3898. static inline int tracing_get_cpu(struct inode *inode)
  3899. {
  3900. if (inode->i_cdev) /* See trace_create_cpu_file() */
  3901. return (long)inode->i_cdev - 1;
  3902. return RING_BUFFER_ALL_CPUS;
  3903. }
  3904. static const struct seq_operations tracer_seq_ops = {
  3905. .start = s_start,
  3906. .next = s_next,
  3907. .stop = s_stop,
  3908. .show = s_show,
  3909. };
  3910. static struct trace_iterator *
  3911. __tracing_open(struct inode *inode, struct file *file, bool snapshot)
  3912. {
  3913. struct trace_array *tr = inode->i_private;
  3914. struct trace_iterator *iter;
  3915. int cpu;
  3916. if (tracing_disabled)
  3917. return ERR_PTR(-ENODEV);
  3918. iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
  3919. if (!iter)
  3920. return ERR_PTR(-ENOMEM);
  3921. iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
  3922. GFP_KERNEL);
  3923. if (!iter->buffer_iter)
  3924. goto release;
  3925. /*
  3926. * trace_find_next_entry() may need to save off iter->ent.
  3927. * It will place it into the iter->temp buffer. As most
  3928. * events are less than 128, allocate a buffer of that size.
  3929. * If one is greater, then trace_find_next_entry() will
  3930. * allocate a new buffer to adjust for the bigger iter->ent.
  3931. * It's not critical if it fails to get allocated here.
  3932. */
  3933. iter->temp = kmalloc(128, GFP_KERNEL);
  3934. if (iter->temp)
  3935. iter->temp_size = 128;
  3936. /*
  3937. * trace_event_printf() may need to modify given format
  3938. * string to replace %p with %px so that it shows real address
  3939. * instead of hash value. However, that is only for the event
  3940. * tracing, other tracer may not need. Defer the allocation
  3941. * until it is needed.
  3942. */
  3943. iter->fmt = NULL;
  3944. iter->fmt_size = 0;
  3945. /*
  3946. * We make a copy of the current tracer to avoid concurrent
  3947. * changes on it while we are reading.
  3948. */
  3949. mutex_lock(&trace_types_lock);
  3950. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  3951. if (!iter->trace)
  3952. goto fail;
  3953. *iter->trace = *tr->current_trace;
  3954. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  3955. goto fail;
  3956. iter->tr = tr;
  3957. #ifdef CONFIG_TRACER_MAX_TRACE
  3958. /* Currently only the top directory has a snapshot */
  3959. if (tr->current_trace->print_max || snapshot)
  3960. iter->array_buffer = &tr->max_buffer;
  3961. else
  3962. #endif
  3963. iter->array_buffer = &tr->array_buffer;
  3964. iter->snapshot = snapshot;
  3965. iter->pos = -1;
  3966. iter->cpu_file = tracing_get_cpu(inode);
  3967. mutex_init(&iter->mutex);
  3968. /* Notify the tracer early; before we stop tracing. */
  3969. if (iter->trace->open)
  3970. iter->trace->open(iter);
  3971. /* Annotate start of buffers if we had overruns */
  3972. if (ring_buffer_overruns(iter->array_buffer->buffer))
  3973. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  3974. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  3975. if (trace_clocks[tr->clock_id].in_ns)
  3976. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  3977. /*
  3978. * If pause-on-trace is enabled, then stop the trace while
  3979. * dumping, unless this is the "snapshot" file
  3980. */
  3981. if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
  3982. tracing_stop_tr(tr);
  3983. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  3984. for_each_tracing_cpu(cpu) {
  3985. iter->buffer_iter[cpu] =
  3986. ring_buffer_read_prepare(iter->array_buffer->buffer,
  3987. cpu, GFP_KERNEL);
  3988. }
  3989. ring_buffer_read_prepare_sync();
  3990. for_each_tracing_cpu(cpu) {
  3991. ring_buffer_read_start(iter->buffer_iter[cpu]);
  3992. tracing_iter_reset(iter, cpu);
  3993. }
  3994. } else {
  3995. cpu = iter->cpu_file;
  3996. iter->buffer_iter[cpu] =
  3997. ring_buffer_read_prepare(iter->array_buffer->buffer,
  3998. cpu, GFP_KERNEL);
  3999. ring_buffer_read_prepare_sync();
  4000. ring_buffer_read_start(iter->buffer_iter[cpu]);
  4001. tracing_iter_reset(iter, cpu);
  4002. }
  4003. mutex_unlock(&trace_types_lock);
  4004. return iter;
  4005. fail:
  4006. mutex_unlock(&trace_types_lock);
  4007. kfree(iter->trace);
  4008. kfree(iter->temp);
  4009. kfree(iter->buffer_iter);
  4010. release:
  4011. seq_release_private(inode, file);
  4012. return ERR_PTR(-ENOMEM);
  4013. }
  4014. int tracing_open_generic(struct inode *inode, struct file *filp)
  4015. {
  4016. int ret;
  4017. ret = tracing_check_open_get_tr(NULL);
  4018. if (ret)
  4019. return ret;
  4020. filp->private_data = inode->i_private;
  4021. return 0;
  4022. }
  4023. bool tracing_is_disabled(void)
  4024. {
  4025. return (tracing_disabled) ? true: false;
  4026. }
  4027. /*
  4028. * Open and update trace_array ref count.
  4029. * Must have the current trace_array passed to it.
  4030. */
  4031. int tracing_open_generic_tr(struct inode *inode, struct file *filp)
  4032. {
  4033. struct trace_array *tr = inode->i_private;
  4034. int ret;
  4035. ret = tracing_check_open_get_tr(tr);
  4036. if (ret)
  4037. return ret;
  4038. filp->private_data = inode->i_private;
  4039. return 0;
  4040. }
  4041. /*
  4042. * The private pointer of the inode is the trace_event_file.
  4043. * Update the tr ref count associated to it.
  4044. */
  4045. int tracing_open_file_tr(struct inode *inode, struct file *filp)
  4046. {
  4047. struct trace_event_file *file = inode->i_private;
  4048. int ret;
  4049. ret = tracing_check_open_get_tr(file->tr);
  4050. if (ret)
  4051. return ret;
  4052. filp->private_data = inode->i_private;
  4053. return 0;
  4054. }
  4055. int tracing_release_file_tr(struct inode *inode, struct file *filp)
  4056. {
  4057. struct trace_event_file *file = inode->i_private;
  4058. trace_array_put(file->tr);
  4059. return 0;
  4060. }
  4061. static int tracing_mark_open(struct inode *inode, struct file *filp)
  4062. {
  4063. stream_open(inode, filp);
  4064. return tracing_open_generic_tr(inode, filp);
  4065. }
  4066. static int tracing_release(struct inode *inode, struct file *file)
  4067. {
  4068. struct trace_array *tr = inode->i_private;
  4069. struct seq_file *m = file->private_data;
  4070. struct trace_iterator *iter;
  4071. int cpu;
  4072. if (!(file->f_mode & FMODE_READ)) {
  4073. trace_array_put(tr);
  4074. return 0;
  4075. }
  4076. /* Writes do not use seq_file */
  4077. iter = m->private;
  4078. mutex_lock(&trace_types_lock);
  4079. for_each_tracing_cpu(cpu) {
  4080. if (iter->buffer_iter[cpu])
  4081. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  4082. }
  4083. if (iter->trace && iter->trace->close)
  4084. iter->trace->close(iter);
  4085. if (!iter->snapshot && tr->stop_count)
  4086. /* reenable tracing if it was previously enabled */
  4087. tracing_start_tr(tr);
  4088. __trace_array_put(tr);
  4089. mutex_unlock(&trace_types_lock);
  4090. mutex_destroy(&iter->mutex);
  4091. free_cpumask_var(iter->started);
  4092. kfree(iter->fmt);
  4093. kfree(iter->temp);
  4094. kfree(iter->trace);
  4095. kfree(iter->buffer_iter);
  4096. seq_release_private(inode, file);
  4097. return 0;
  4098. }
  4099. static int tracing_release_generic_tr(struct inode *inode, struct file *file)
  4100. {
  4101. struct trace_array *tr = inode->i_private;
  4102. trace_array_put(tr);
  4103. return 0;
  4104. }
  4105. static int tracing_single_release_tr(struct inode *inode, struct file *file)
  4106. {
  4107. struct trace_array *tr = inode->i_private;
  4108. trace_array_put(tr);
  4109. return single_release(inode, file);
  4110. }
  4111. static int tracing_open(struct inode *inode, struct file *file)
  4112. {
  4113. struct trace_array *tr = inode->i_private;
  4114. struct trace_iterator *iter;
  4115. int ret;
  4116. ret = tracing_check_open_get_tr(tr);
  4117. if (ret)
  4118. return ret;
  4119. /* If this file was open for write, then erase contents */
  4120. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  4121. int cpu = tracing_get_cpu(inode);
  4122. struct array_buffer *trace_buf = &tr->array_buffer;
  4123. #ifdef CONFIG_TRACER_MAX_TRACE
  4124. if (tr->current_trace->print_max)
  4125. trace_buf = &tr->max_buffer;
  4126. #endif
  4127. if (cpu == RING_BUFFER_ALL_CPUS)
  4128. tracing_reset_online_cpus(trace_buf);
  4129. else
  4130. tracing_reset_cpu(trace_buf, cpu);
  4131. }
  4132. if (file->f_mode & FMODE_READ) {
  4133. iter = __tracing_open(inode, file, false);
  4134. if (IS_ERR(iter))
  4135. ret = PTR_ERR(iter);
  4136. else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  4137. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  4138. }
  4139. if (ret < 0)
  4140. trace_array_put(tr);
  4141. return ret;
  4142. }
  4143. /*
  4144. * Some tracers are not suitable for instance buffers.
  4145. * A tracer is always available for the global array (toplevel)
  4146. * or if it explicitly states that it is.
  4147. */
  4148. static bool
  4149. trace_ok_for_array(struct tracer *t, struct trace_array *tr)
  4150. {
  4151. return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
  4152. }
  4153. /* Find the next tracer that this trace array may use */
  4154. static struct tracer *
  4155. get_tracer_for_array(struct trace_array *tr, struct tracer *t)
  4156. {
  4157. while (t && !trace_ok_for_array(t, tr))
  4158. t = t->next;
  4159. return t;
  4160. }
  4161. static void *
  4162. t_next(struct seq_file *m, void *v, loff_t *pos)
  4163. {
  4164. struct trace_array *tr = m->private;
  4165. struct tracer *t = v;
  4166. (*pos)++;
  4167. if (t)
  4168. t = get_tracer_for_array(tr, t->next);
  4169. return t;
  4170. }
  4171. static void *t_start(struct seq_file *m, loff_t *pos)
  4172. {
  4173. struct trace_array *tr = m->private;
  4174. struct tracer *t;
  4175. loff_t l = 0;
  4176. mutex_lock(&trace_types_lock);
  4177. t = get_tracer_for_array(tr, trace_types);
  4178. for (; t && l < *pos; t = t_next(m, t, &l))
  4179. ;
  4180. return t;
  4181. }
  4182. static void t_stop(struct seq_file *m, void *p)
  4183. {
  4184. mutex_unlock(&trace_types_lock);
  4185. }
  4186. static int t_show(struct seq_file *m, void *v)
  4187. {
  4188. struct tracer *t = v;
  4189. if (!t)
  4190. return 0;
  4191. seq_puts(m, t->name);
  4192. if (t->next)
  4193. seq_putc(m, ' ');
  4194. else
  4195. seq_putc(m, '\n');
  4196. return 0;
  4197. }
  4198. static const struct seq_operations show_traces_seq_ops = {
  4199. .start = t_start,
  4200. .next = t_next,
  4201. .stop = t_stop,
  4202. .show = t_show,
  4203. };
  4204. static int show_traces_open(struct inode *inode, struct file *file)
  4205. {
  4206. struct trace_array *tr = inode->i_private;
  4207. struct seq_file *m;
  4208. int ret;
  4209. ret = tracing_check_open_get_tr(tr);
  4210. if (ret)
  4211. return ret;
  4212. ret = seq_open(file, &show_traces_seq_ops);
  4213. if (ret) {
  4214. trace_array_put(tr);
  4215. return ret;
  4216. }
  4217. m = file->private_data;
  4218. m->private = tr;
  4219. return 0;
  4220. }
  4221. static int show_traces_release(struct inode *inode, struct file *file)
  4222. {
  4223. struct trace_array *tr = inode->i_private;
  4224. trace_array_put(tr);
  4225. return seq_release(inode, file);
  4226. }
  4227. static ssize_t
  4228. tracing_write_stub(struct file *filp, const char __user *ubuf,
  4229. size_t count, loff_t *ppos)
  4230. {
  4231. return count;
  4232. }
  4233. loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
  4234. {
  4235. int ret;
  4236. if (file->f_mode & FMODE_READ)
  4237. ret = seq_lseek(file, offset, whence);
  4238. else
  4239. file->f_pos = ret = 0;
  4240. return ret;
  4241. }
  4242. static const struct file_operations tracing_fops = {
  4243. .open = tracing_open,
  4244. .read = seq_read,
  4245. .read_iter = seq_read_iter,
  4246. .splice_read = generic_file_splice_read,
  4247. .write = tracing_write_stub,
  4248. .llseek = tracing_lseek,
  4249. .release = tracing_release,
  4250. };
  4251. static const struct file_operations show_traces_fops = {
  4252. .open = show_traces_open,
  4253. .read = seq_read,
  4254. .llseek = seq_lseek,
  4255. .release = show_traces_release,
  4256. };
  4257. static ssize_t
  4258. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  4259. size_t count, loff_t *ppos)
  4260. {
  4261. struct trace_array *tr = file_inode(filp)->i_private;
  4262. char *mask_str;
  4263. int len;
  4264. len = snprintf(NULL, 0, "%*pb\n",
  4265. cpumask_pr_args(tr->tracing_cpumask)) + 1;
  4266. mask_str = kmalloc(len, GFP_KERNEL);
  4267. if (!mask_str)
  4268. return -ENOMEM;
  4269. len = snprintf(mask_str, len, "%*pb\n",
  4270. cpumask_pr_args(tr->tracing_cpumask));
  4271. if (len >= count) {
  4272. count = -EINVAL;
  4273. goto out_err;
  4274. }
  4275. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
  4276. out_err:
  4277. kfree(mask_str);
  4278. return count;
  4279. }
  4280. int tracing_set_cpumask(struct trace_array *tr,
  4281. cpumask_var_t tracing_cpumask_new)
  4282. {
  4283. int cpu;
  4284. if (!tr)
  4285. return -EINVAL;
  4286. local_irq_disable();
  4287. arch_spin_lock(&tr->max_lock);
  4288. for_each_tracing_cpu(cpu) {
  4289. /*
  4290. * Increase/decrease the disabled counter if we are
  4291. * about to flip a bit in the cpumask:
  4292. */
  4293. if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  4294. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  4295. atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
  4296. ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
  4297. #ifdef CONFIG_TRACER_MAX_TRACE
  4298. ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
  4299. #endif
  4300. }
  4301. if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  4302. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  4303. atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
  4304. ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
  4305. #ifdef CONFIG_TRACER_MAX_TRACE
  4306. ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
  4307. #endif
  4308. }
  4309. }
  4310. arch_spin_unlock(&tr->max_lock);
  4311. local_irq_enable();
  4312. cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
  4313. return 0;
  4314. }
  4315. static ssize_t
  4316. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  4317. size_t count, loff_t *ppos)
  4318. {
  4319. struct trace_array *tr = file_inode(filp)->i_private;
  4320. cpumask_var_t tracing_cpumask_new;
  4321. int err;
  4322. if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  4323. return -ENOMEM;
  4324. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  4325. if (err)
  4326. goto err_free;
  4327. err = tracing_set_cpumask(tr, tracing_cpumask_new);
  4328. if (err)
  4329. goto err_free;
  4330. free_cpumask_var(tracing_cpumask_new);
  4331. return count;
  4332. err_free:
  4333. free_cpumask_var(tracing_cpumask_new);
  4334. return err;
  4335. }
  4336. static const struct file_operations tracing_cpumask_fops = {
  4337. .open = tracing_open_generic_tr,
  4338. .read = tracing_cpumask_read,
  4339. .write = tracing_cpumask_write,
  4340. .release = tracing_release_generic_tr,
  4341. .llseek = generic_file_llseek,
  4342. };
  4343. static int tracing_trace_options_show(struct seq_file *m, void *v)
  4344. {
  4345. struct tracer_opt *trace_opts;
  4346. struct trace_array *tr = m->private;
  4347. u32 tracer_flags;
  4348. int i;
  4349. mutex_lock(&trace_types_lock);
  4350. tracer_flags = tr->current_trace->flags->val;
  4351. trace_opts = tr->current_trace->flags->opts;
  4352. for (i = 0; trace_options[i]; i++) {
  4353. if (tr->trace_flags & (1 << i))
  4354. seq_printf(m, "%s\n", trace_options[i]);
  4355. else
  4356. seq_printf(m, "no%s\n", trace_options[i]);
  4357. }
  4358. for (i = 0; trace_opts[i].name; i++) {
  4359. if (tracer_flags & trace_opts[i].bit)
  4360. seq_printf(m, "%s\n", trace_opts[i].name);
  4361. else
  4362. seq_printf(m, "no%s\n", trace_opts[i].name);
  4363. }
  4364. mutex_unlock(&trace_types_lock);
  4365. return 0;
  4366. }
  4367. static int __set_tracer_option(struct trace_array *tr,
  4368. struct tracer_flags *tracer_flags,
  4369. struct tracer_opt *opts, int neg)
  4370. {
  4371. struct tracer *trace = tracer_flags->trace;
  4372. int ret;
  4373. ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
  4374. if (ret)
  4375. return ret;
  4376. if (neg)
  4377. tracer_flags->val &= ~opts->bit;
  4378. else
  4379. tracer_flags->val |= opts->bit;
  4380. return 0;
  4381. }
  4382. /* Try to assign a tracer specific option */
  4383. static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
  4384. {
  4385. struct tracer *trace = tr->current_trace;
  4386. struct tracer_flags *tracer_flags = trace->flags;
  4387. struct tracer_opt *opts = NULL;
  4388. int i;
  4389. for (i = 0; tracer_flags->opts[i].name; i++) {
  4390. opts = &tracer_flags->opts[i];
  4391. if (strcmp(cmp, opts->name) == 0)
  4392. return __set_tracer_option(tr, trace->flags, opts, neg);
  4393. }
  4394. return -EINVAL;
  4395. }
  4396. /* Some tracers require overwrite to stay enabled */
  4397. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  4398. {
  4399. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  4400. return -1;
  4401. return 0;
  4402. }
  4403. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  4404. {
  4405. int *map;
  4406. if ((mask == TRACE_ITER_RECORD_TGID) ||
  4407. (mask == TRACE_ITER_RECORD_CMD))
  4408. lockdep_assert_held(&event_mutex);
  4409. /* do nothing if flag is already set */
  4410. if (!!(tr->trace_flags & mask) == !!enabled)
  4411. return 0;
  4412. /* Give the tracer a chance to approve the change */
  4413. if (tr->current_trace->flag_changed)
  4414. if (tr->current_trace->flag_changed(tr, mask, !!enabled))
  4415. return -EINVAL;
  4416. if (enabled)
  4417. tr->trace_flags |= mask;
  4418. else
  4419. tr->trace_flags &= ~mask;
  4420. if (mask == TRACE_ITER_RECORD_CMD)
  4421. trace_event_enable_cmd_record(enabled);
  4422. if (mask == TRACE_ITER_RECORD_TGID) {
  4423. if (!tgid_map) {
  4424. tgid_map_max = pid_max;
  4425. map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
  4426. GFP_KERNEL);
  4427. /*
  4428. * Pairs with smp_load_acquire() in
  4429. * trace_find_tgid_ptr() to ensure that if it observes
  4430. * the tgid_map we just allocated then it also observes
  4431. * the corresponding tgid_map_max value.
  4432. */
  4433. smp_store_release(&tgid_map, map);
  4434. }
  4435. if (!tgid_map) {
  4436. tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
  4437. return -ENOMEM;
  4438. }
  4439. trace_event_enable_tgid_record(enabled);
  4440. }
  4441. if (mask == TRACE_ITER_EVENT_FORK)
  4442. trace_event_follow_fork(tr, enabled);
  4443. if (mask == TRACE_ITER_FUNC_FORK)
  4444. ftrace_pid_follow_fork(tr, enabled);
  4445. if (mask == TRACE_ITER_OVERWRITE) {
  4446. ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
  4447. #ifdef CONFIG_TRACER_MAX_TRACE
  4448. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  4449. #endif
  4450. }
  4451. if (mask == TRACE_ITER_PRINTK) {
  4452. trace_printk_start_stop_comm(enabled);
  4453. trace_printk_control(enabled);
  4454. }
  4455. return 0;
  4456. }
  4457. int trace_set_options(struct trace_array *tr, char *option)
  4458. {
  4459. char *cmp;
  4460. int neg = 0;
  4461. int ret;
  4462. size_t orig_len = strlen(option);
  4463. int len;
  4464. cmp = strstrip(option);
  4465. len = str_has_prefix(cmp, "no");
  4466. if (len)
  4467. neg = 1;
  4468. cmp += len;
  4469. mutex_lock(&event_mutex);
  4470. mutex_lock(&trace_types_lock);
  4471. ret = match_string(trace_options, -1, cmp);
  4472. /* If no option could be set, test the specific tracer options */
  4473. if (ret < 0)
  4474. ret = set_tracer_option(tr, cmp, neg);
  4475. else
  4476. ret = set_tracer_flag(tr, 1 << ret, !neg);
  4477. mutex_unlock(&trace_types_lock);
  4478. mutex_unlock(&event_mutex);
  4479. /*
  4480. * If the first trailing whitespace is replaced with '\0' by strstrip,
  4481. * turn it back into a space.
  4482. */
  4483. if (orig_len > strlen(option))
  4484. option[strlen(option)] = ' ';
  4485. return ret;
  4486. }
  4487. static void __init apply_trace_boot_options(void)
  4488. {
  4489. char *buf = trace_boot_options_buf;
  4490. char *option;
  4491. while (true) {
  4492. option = strsep(&buf, ",");
  4493. if (!option)
  4494. break;
  4495. if (*option)
  4496. trace_set_options(&global_trace, option);
  4497. /* Put back the comma to allow this to be called again */
  4498. if (buf)
  4499. *(buf - 1) = ',';
  4500. }
  4501. }
  4502. static ssize_t
  4503. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  4504. size_t cnt, loff_t *ppos)
  4505. {
  4506. struct seq_file *m = filp->private_data;
  4507. struct trace_array *tr = m->private;
  4508. char buf[64];
  4509. int ret;
  4510. if (cnt >= sizeof(buf))
  4511. return -EINVAL;
  4512. if (copy_from_user(buf, ubuf, cnt))
  4513. return -EFAULT;
  4514. buf[cnt] = 0;
  4515. ret = trace_set_options(tr, buf);
  4516. if (ret < 0)
  4517. return ret;
  4518. *ppos += cnt;
  4519. return cnt;
  4520. }
  4521. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  4522. {
  4523. struct trace_array *tr = inode->i_private;
  4524. int ret;
  4525. ret = tracing_check_open_get_tr(tr);
  4526. if (ret)
  4527. return ret;
  4528. ret = single_open(file, tracing_trace_options_show, inode->i_private);
  4529. if (ret < 0)
  4530. trace_array_put(tr);
  4531. return ret;
  4532. }
  4533. static const struct file_operations tracing_iter_fops = {
  4534. .open = tracing_trace_options_open,
  4535. .read = seq_read,
  4536. .llseek = seq_lseek,
  4537. .release = tracing_single_release_tr,
  4538. .write = tracing_trace_options_write,
  4539. };
  4540. static const char readme_msg[] =
  4541. "tracing mini-HOWTO:\n\n"
  4542. "# echo 0 > tracing_on : quick way to disable tracing\n"
  4543. "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
  4544. " Important files:\n"
  4545. " trace\t\t\t- The static contents of the buffer\n"
  4546. "\t\t\t To clear the buffer write into this file: echo > trace\n"
  4547. " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
  4548. " current_tracer\t- function and latency tracers\n"
  4549. " available_tracers\t- list of configured tracers for current_tracer\n"
  4550. " error_log\t- error log for failed commands (that support it)\n"
  4551. " buffer_size_kb\t- view and modify size of per cpu buffer\n"
  4552. " buffer_total_size_kb - view total size of all cpu buffers\n\n"
  4553. " trace_clock\t\t- change the clock used to order events\n"
  4554. " local: Per cpu clock but may not be synced across CPUs\n"
  4555. " global: Synced across CPUs but slows tracing down.\n"
  4556. " counter: Not a clock, but just an increment\n"
  4557. " uptime: Jiffy counter from time of boot\n"
  4558. " perf: Same clock that perf events use\n"
  4559. #ifdef CONFIG_X86_64
  4560. " x86-tsc: TSC cycle counter\n"
  4561. #endif
  4562. "\n timestamp_mode\t- view the mode used to timestamp events\n"
  4563. " delta: Delta difference against a buffer-wide timestamp\n"
  4564. " absolute: Absolute (standalone) timestamp\n"
  4565. "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
  4566. "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
  4567. " tracing_cpumask\t- Limit which CPUs to trace\n"
  4568. " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
  4569. "\t\t\t Remove sub-buffer with rmdir\n"
  4570. " trace_options\t\t- Set format or modify how tracing happens\n"
  4571. "\t\t\t Disable an option by prefixing 'no' to the\n"
  4572. "\t\t\t option name\n"
  4573. " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
  4574. #ifdef CONFIG_DYNAMIC_FTRACE
  4575. "\n available_filter_functions - list of functions that can be filtered on\n"
  4576. " set_ftrace_filter\t- echo function name in here to only trace these\n"
  4577. "\t\t\t functions\n"
  4578. "\t accepts: func_full_name or glob-matching-pattern\n"
  4579. "\t modules: Can select a group via module\n"
  4580. "\t Format: :mod:<module-name>\n"
  4581. "\t example: echo :mod:ext3 > set_ftrace_filter\n"
  4582. "\t triggers: a command to perform when function is hit\n"
  4583. "\t Format: <function>:<trigger>[:count]\n"
  4584. "\t trigger: traceon, traceoff\n"
  4585. "\t\t enable_event:<system>:<event>\n"
  4586. "\t\t disable_event:<system>:<event>\n"
  4587. #ifdef CONFIG_STACKTRACE
  4588. "\t\t stacktrace\n"
  4589. #endif
  4590. #ifdef CONFIG_TRACER_SNAPSHOT
  4591. "\t\t snapshot\n"
  4592. #endif
  4593. "\t\t dump\n"
  4594. "\t\t cpudump\n"
  4595. "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
  4596. "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
  4597. "\t The first one will disable tracing every time do_fault is hit\n"
  4598. "\t The second will disable tracing at most 3 times when do_trap is hit\n"
  4599. "\t The first time do trap is hit and it disables tracing, the\n"
  4600. "\t counter will decrement to 2. If tracing is already disabled,\n"
  4601. "\t the counter will not decrement. It only decrements when the\n"
  4602. "\t trigger did work\n"
  4603. "\t To remove trigger without count:\n"
  4604. "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
  4605. "\t To remove trigger with a count:\n"
  4606. "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
  4607. " set_ftrace_notrace\t- echo function name in here to never trace.\n"
  4608. "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
  4609. "\t modules: Can select a group via module command :mod:\n"
  4610. "\t Does not accept triggers\n"
  4611. #endif /* CONFIG_DYNAMIC_FTRACE */
  4612. #ifdef CONFIG_FUNCTION_TRACER
  4613. " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
  4614. "\t\t (function)\n"
  4615. " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
  4616. "\t\t (function)\n"
  4617. #endif
  4618. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  4619. " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
  4620. " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
  4621. " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
  4622. #endif
  4623. #ifdef CONFIG_TRACER_SNAPSHOT
  4624. "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
  4625. "\t\t\t snapshot buffer. Read the contents for more\n"
  4626. "\t\t\t information\n"
  4627. #endif
  4628. #ifdef CONFIG_STACK_TRACER
  4629. " stack_trace\t\t- Shows the max stack trace when active\n"
  4630. " stack_max_size\t- Shows current max stack size that was traced\n"
  4631. "\t\t\t Write into this file to reset the max size (trigger a\n"
  4632. "\t\t\t new trace)\n"
  4633. #ifdef CONFIG_DYNAMIC_FTRACE
  4634. " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
  4635. "\t\t\t traces\n"
  4636. #endif
  4637. #endif /* CONFIG_STACK_TRACER */
  4638. #ifdef CONFIG_DYNAMIC_EVENTS
  4639. " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
  4640. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4641. #endif
  4642. #ifdef CONFIG_KPROBE_EVENTS
  4643. " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
  4644. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4645. #endif
  4646. #ifdef CONFIG_UPROBE_EVENTS
  4647. " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
  4648. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4649. #endif
  4650. #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
  4651. "\t accepts: event-definitions (one definition per line)\n"
  4652. "\t Format: p[:[<group>/][<event>]] <place> [<args>]\n"
  4653. "\t r[maxactive][:[<group>/][<event>]] <place> [<args>]\n"
  4654. #ifdef CONFIG_HIST_TRIGGERS
  4655. "\t s:[synthetic/]<event> <field> [<field>]\n"
  4656. #endif
  4657. "\t e[:[<group>/][<event>]] <attached-group>.<attached-event> [<args>] [if <filter>]\n"
  4658. "\t -:[<group>/][<event>]\n"
  4659. #ifdef CONFIG_KPROBE_EVENTS
  4660. "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
  4661. "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
  4662. #endif
  4663. #ifdef CONFIG_UPROBE_EVENTS
  4664. " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
  4665. #endif
  4666. "\t args: <name>=fetcharg[:type]\n"
  4667. "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n"
  4668. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  4669. "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
  4670. #else
  4671. "\t $stack<index>, $stack, $retval, $comm,\n"
  4672. #endif
  4673. "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
  4674. "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
  4675. "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
  4676. "\t symstr, <type>\\[<array-size>\\]\n"
  4677. #ifdef CONFIG_HIST_TRIGGERS
  4678. "\t field: <stype> <name>;\n"
  4679. "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
  4680. "\t [unsigned] char/int/long\n"
  4681. #endif
  4682. "\t efield: For event probes ('e' types), the field is on of the fields\n"
  4683. "\t of the <attached-group>/<attached-event>.\n"
  4684. #endif
  4685. " events/\t\t- Directory containing all trace event subsystems:\n"
  4686. " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
  4687. " events/<system>/\t- Directory containing all trace events for <system>:\n"
  4688. " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
  4689. "\t\t\t events\n"
  4690. " filter\t\t- If set, only events passing filter are traced\n"
  4691. " events/<system>/<event>/\t- Directory containing control files for\n"
  4692. "\t\t\t <event>:\n"
  4693. " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
  4694. " filter\t\t- If set, only events passing filter are traced\n"
  4695. " trigger\t\t- If set, a command to perform when event is hit\n"
  4696. "\t Format: <trigger>[:count][if <filter>]\n"
  4697. "\t trigger: traceon, traceoff\n"
  4698. "\t enable_event:<system>:<event>\n"
  4699. "\t disable_event:<system>:<event>\n"
  4700. #ifdef CONFIG_HIST_TRIGGERS
  4701. "\t enable_hist:<system>:<event>\n"
  4702. "\t disable_hist:<system>:<event>\n"
  4703. #endif
  4704. #ifdef CONFIG_STACKTRACE
  4705. "\t\t stacktrace\n"
  4706. #endif
  4707. #ifdef CONFIG_TRACER_SNAPSHOT
  4708. "\t\t snapshot\n"
  4709. #endif
  4710. #ifdef CONFIG_HIST_TRIGGERS
  4711. "\t\t hist (see below)\n"
  4712. #endif
  4713. "\t example: echo traceoff > events/block/block_unplug/trigger\n"
  4714. "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
  4715. "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
  4716. "\t events/block/block_unplug/trigger\n"
  4717. "\t The first disables tracing every time block_unplug is hit.\n"
  4718. "\t The second disables tracing the first 3 times block_unplug is hit.\n"
  4719. "\t The third enables the kmalloc event the first 3 times block_unplug\n"
  4720. "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
  4721. "\t Like function triggers, the counter is only decremented if it\n"
  4722. "\t enabled or disabled tracing.\n"
  4723. "\t To remove a trigger without a count:\n"
  4724. "\t echo '!<trigger> > <system>/<event>/trigger\n"
  4725. "\t To remove a trigger with a count:\n"
  4726. "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
  4727. "\t Filters can be ignored when removing a trigger.\n"
  4728. #ifdef CONFIG_HIST_TRIGGERS
  4729. " hist trigger\t- If set, event hits are aggregated into a hash table\n"
  4730. "\t Format: hist:keys=<field1[,field2,...]>\n"
  4731. "\t [:<var1>=<field|var_ref|numeric_literal>[,<var2>=...]]\n"
  4732. "\t [:values=<field1[,field2,...]>]\n"
  4733. "\t [:sort=<field1[,field2,...]>]\n"
  4734. "\t [:size=#entries]\n"
  4735. "\t [:pause][:continue][:clear]\n"
  4736. "\t [:name=histname1]\n"
  4737. "\t [:<handler>.<action>]\n"
  4738. "\t [if <filter>]\n\n"
  4739. "\t Note, special fields can be used as well:\n"
  4740. "\t common_timestamp - to record current timestamp\n"
  4741. "\t common_cpu - to record the CPU the event happened on\n"
  4742. "\n"
  4743. "\t A hist trigger variable can be:\n"
  4744. "\t - a reference to a field e.g. x=current_timestamp,\n"
  4745. "\t - a reference to another variable e.g. y=$x,\n"
  4746. "\t - a numeric literal: e.g. ms_per_sec=1000,\n"
  4747. "\t - an arithmetic expression: e.g. time_secs=current_timestamp/1000\n"
  4748. "\n"
  4749. "\t hist trigger arithmetic expressions support addition(+), subtraction(-),\n"
  4750. "\t multiplication(*) and division(/) operators. An operand can be either a\n"
  4751. "\t variable reference, field or numeric literal.\n"
  4752. "\n"
  4753. "\t When a matching event is hit, an entry is added to a hash\n"
  4754. "\t table using the key(s) and value(s) named, and the value of a\n"
  4755. "\t sum called 'hitcount' is incremented. Keys and values\n"
  4756. "\t correspond to fields in the event's format description. Keys\n"
  4757. "\t can be any field, or the special string 'stacktrace'.\n"
  4758. "\t Compound keys consisting of up to two fields can be specified\n"
  4759. "\t by the 'keys' keyword. Values must correspond to numeric\n"
  4760. "\t fields. Sort keys consisting of up to two fields can be\n"
  4761. "\t specified using the 'sort' keyword. The sort direction can\n"
  4762. "\t be modified by appending '.descending' or '.ascending' to a\n"
  4763. "\t sort field. The 'size' parameter can be used to specify more\n"
  4764. "\t or fewer than the default 2048 entries for the hashtable size.\n"
  4765. "\t If a hist trigger is given a name using the 'name' parameter,\n"
  4766. "\t its histogram data will be shared with other triggers of the\n"
  4767. "\t same name, and trigger hits will update this common data.\n\n"
  4768. "\t Reading the 'hist' file for the event will dump the hash\n"
  4769. "\t table in its entirety to stdout. If there are multiple hist\n"
  4770. "\t triggers attached to an event, there will be a table for each\n"
  4771. "\t trigger in the output. The table displayed for a named\n"
  4772. "\t trigger will be the same as any other instance having the\n"
  4773. "\t same name. The default format used to display a given field\n"
  4774. "\t can be modified by appending any of the following modifiers\n"
  4775. "\t to the field name, as applicable:\n\n"
  4776. "\t .hex display a number as a hex value\n"
  4777. "\t .sym display an address as a symbol\n"
  4778. "\t .sym-offset display an address as a symbol and offset\n"
  4779. "\t .execname display a common_pid as a program name\n"
  4780. "\t .syscall display a syscall id as a syscall name\n"
  4781. "\t .log2 display log2 value rather than raw number\n"
  4782. "\t .buckets=size display values in groups of size rather than raw number\n"
  4783. "\t .usecs display a common_timestamp in microseconds\n"
  4784. "\t .percent display a number of percentage value\n"
  4785. "\t .graph display a bar-graph of a value\n\n"
  4786. "\t The 'pause' parameter can be used to pause an existing hist\n"
  4787. "\t trigger or to start a hist trigger but not log any events\n"
  4788. "\t until told to do so. 'continue' can be used to start or\n"
  4789. "\t restart a paused hist trigger.\n\n"
  4790. "\t The 'clear' parameter will clear the contents of a running\n"
  4791. "\t hist trigger and leave its current paused/active state\n"
  4792. "\t unchanged.\n\n"
  4793. "\t The enable_hist and disable_hist triggers can be used to\n"
  4794. "\t have one event conditionally start and stop another event's\n"
  4795. "\t already-attached hist trigger. The syntax is analogous to\n"
  4796. "\t the enable_event and disable_event triggers.\n\n"
  4797. "\t Hist trigger handlers and actions are executed whenever a\n"
  4798. "\t a histogram entry is added or updated. They take the form:\n\n"
  4799. "\t <handler>.<action>\n\n"
  4800. "\t The available handlers are:\n\n"
  4801. "\t onmatch(matching.event) - invoke on addition or update\n"
  4802. "\t onmax(var) - invoke if var exceeds current max\n"
  4803. "\t onchange(var) - invoke action if var changes\n\n"
  4804. "\t The available actions are:\n\n"
  4805. "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
  4806. "\t save(field,...) - save current event fields\n"
  4807. #ifdef CONFIG_TRACER_SNAPSHOT
  4808. "\t snapshot() - snapshot the trace buffer\n\n"
  4809. #endif
  4810. #ifdef CONFIG_SYNTH_EVENTS
  4811. " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
  4812. "\t Write into this file to define/undefine new synthetic events.\n"
  4813. "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
  4814. #endif
  4815. #endif
  4816. ;
  4817. static ssize_t
  4818. tracing_readme_read(struct file *filp, char __user *ubuf,
  4819. size_t cnt, loff_t *ppos)
  4820. {
  4821. return simple_read_from_buffer(ubuf, cnt, ppos,
  4822. readme_msg, strlen(readme_msg));
  4823. }
  4824. static const struct file_operations tracing_readme_fops = {
  4825. .open = tracing_open_generic,
  4826. .read = tracing_readme_read,
  4827. .llseek = generic_file_llseek,
  4828. };
  4829. static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
  4830. {
  4831. int pid = ++(*pos);
  4832. return trace_find_tgid_ptr(pid);
  4833. }
  4834. static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
  4835. {
  4836. int pid = *pos;
  4837. return trace_find_tgid_ptr(pid);
  4838. }
  4839. static void saved_tgids_stop(struct seq_file *m, void *v)
  4840. {
  4841. }
  4842. static int saved_tgids_show(struct seq_file *m, void *v)
  4843. {
  4844. int *entry = (int *)v;
  4845. int pid = entry - tgid_map;
  4846. int tgid = *entry;
  4847. if (tgid == 0)
  4848. return SEQ_SKIP;
  4849. seq_printf(m, "%d %d\n", pid, tgid);
  4850. return 0;
  4851. }
  4852. static const struct seq_operations tracing_saved_tgids_seq_ops = {
  4853. .start = saved_tgids_start,
  4854. .stop = saved_tgids_stop,
  4855. .next = saved_tgids_next,
  4856. .show = saved_tgids_show,
  4857. };
  4858. static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
  4859. {
  4860. int ret;
  4861. ret = tracing_check_open_get_tr(NULL);
  4862. if (ret)
  4863. return ret;
  4864. return seq_open(filp, &tracing_saved_tgids_seq_ops);
  4865. }
  4866. static const struct file_operations tracing_saved_tgids_fops = {
  4867. .open = tracing_saved_tgids_open,
  4868. .read = seq_read,
  4869. .llseek = seq_lseek,
  4870. .release = seq_release,
  4871. };
  4872. static void *saved_cmdlines_next(struct seq_file *m, void *v, loff_t *pos)
  4873. {
  4874. unsigned int *ptr = v;
  4875. if (*pos || m->count)
  4876. ptr++;
  4877. (*pos)++;
  4878. for (; ptr < &savedcmd->map_cmdline_to_pid[savedcmd->cmdline_num];
  4879. ptr++) {
  4880. if (*ptr == -1 || *ptr == NO_CMDLINE_MAP)
  4881. continue;
  4882. return ptr;
  4883. }
  4884. return NULL;
  4885. }
  4886. static void *saved_cmdlines_start(struct seq_file *m, loff_t *pos)
  4887. {
  4888. void *v;
  4889. loff_t l = 0;
  4890. preempt_disable();
  4891. arch_spin_lock(&trace_cmdline_lock);
  4892. v = &savedcmd->map_cmdline_to_pid[0];
  4893. while (l <= *pos) {
  4894. v = saved_cmdlines_next(m, v, &l);
  4895. if (!v)
  4896. return NULL;
  4897. }
  4898. return v;
  4899. }
  4900. static void saved_cmdlines_stop(struct seq_file *m, void *v)
  4901. {
  4902. arch_spin_unlock(&trace_cmdline_lock);
  4903. preempt_enable();
  4904. }
  4905. static int saved_cmdlines_show(struct seq_file *m, void *v)
  4906. {
  4907. char buf[TASK_COMM_LEN];
  4908. unsigned int *pid = v;
  4909. __trace_find_cmdline(*pid, buf);
  4910. seq_printf(m, "%d %s\n", *pid, buf);
  4911. return 0;
  4912. }
  4913. static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
  4914. .start = saved_cmdlines_start,
  4915. .next = saved_cmdlines_next,
  4916. .stop = saved_cmdlines_stop,
  4917. .show = saved_cmdlines_show,
  4918. };
  4919. static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
  4920. {
  4921. int ret;
  4922. ret = tracing_check_open_get_tr(NULL);
  4923. if (ret)
  4924. return ret;
  4925. return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
  4926. }
  4927. static const struct file_operations tracing_saved_cmdlines_fops = {
  4928. .open = tracing_saved_cmdlines_open,
  4929. .read = seq_read,
  4930. .llseek = seq_lseek,
  4931. .release = seq_release,
  4932. };
  4933. static ssize_t
  4934. tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
  4935. size_t cnt, loff_t *ppos)
  4936. {
  4937. char buf[64];
  4938. int r;
  4939. preempt_disable();
  4940. arch_spin_lock(&trace_cmdline_lock);
  4941. r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
  4942. arch_spin_unlock(&trace_cmdline_lock);
  4943. preempt_enable();
  4944. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4945. }
  4946. static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
  4947. {
  4948. kfree(s->saved_cmdlines);
  4949. kfree(s->map_cmdline_to_pid);
  4950. kfree(s);
  4951. }
  4952. static int tracing_resize_saved_cmdlines(unsigned int val)
  4953. {
  4954. struct saved_cmdlines_buffer *s, *savedcmd_temp;
  4955. s = kmalloc(sizeof(*s), GFP_KERNEL);
  4956. if (!s)
  4957. return -ENOMEM;
  4958. if (allocate_cmdlines_buffer(val, s) < 0) {
  4959. kfree(s);
  4960. return -ENOMEM;
  4961. }
  4962. preempt_disable();
  4963. arch_spin_lock(&trace_cmdline_lock);
  4964. savedcmd_temp = savedcmd;
  4965. savedcmd = s;
  4966. arch_spin_unlock(&trace_cmdline_lock);
  4967. preempt_enable();
  4968. free_saved_cmdlines_buffer(savedcmd_temp);
  4969. return 0;
  4970. }
  4971. static ssize_t
  4972. tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
  4973. size_t cnt, loff_t *ppos)
  4974. {
  4975. unsigned long val;
  4976. int ret;
  4977. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4978. if (ret)
  4979. return ret;
  4980. /* must have at least 1 entry or less than PID_MAX_DEFAULT */
  4981. if (!val || val > PID_MAX_DEFAULT)
  4982. return -EINVAL;
  4983. ret = tracing_resize_saved_cmdlines((unsigned int)val);
  4984. if (ret < 0)
  4985. return ret;
  4986. *ppos += cnt;
  4987. return cnt;
  4988. }
  4989. static const struct file_operations tracing_saved_cmdlines_size_fops = {
  4990. .open = tracing_open_generic,
  4991. .read = tracing_saved_cmdlines_size_read,
  4992. .write = tracing_saved_cmdlines_size_write,
  4993. };
  4994. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  4995. static union trace_eval_map_item *
  4996. update_eval_map(union trace_eval_map_item *ptr)
  4997. {
  4998. if (!ptr->map.eval_string) {
  4999. if (ptr->tail.next) {
  5000. ptr = ptr->tail.next;
  5001. /* Set ptr to the next real item (skip head) */
  5002. ptr++;
  5003. } else
  5004. return NULL;
  5005. }
  5006. return ptr;
  5007. }
  5008. static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
  5009. {
  5010. union trace_eval_map_item *ptr = v;
  5011. /*
  5012. * Paranoid! If ptr points to end, we don't want to increment past it.
  5013. * This really should never happen.
  5014. */
  5015. (*pos)++;
  5016. ptr = update_eval_map(ptr);
  5017. if (WARN_ON_ONCE(!ptr))
  5018. return NULL;
  5019. ptr++;
  5020. ptr = update_eval_map(ptr);
  5021. return ptr;
  5022. }
  5023. static void *eval_map_start(struct seq_file *m, loff_t *pos)
  5024. {
  5025. union trace_eval_map_item *v;
  5026. loff_t l = 0;
  5027. mutex_lock(&trace_eval_mutex);
  5028. v = trace_eval_maps;
  5029. if (v)
  5030. v++;
  5031. while (v && l < *pos) {
  5032. v = eval_map_next(m, v, &l);
  5033. }
  5034. return v;
  5035. }
  5036. static void eval_map_stop(struct seq_file *m, void *v)
  5037. {
  5038. mutex_unlock(&trace_eval_mutex);
  5039. }
  5040. static int eval_map_show(struct seq_file *m, void *v)
  5041. {
  5042. union trace_eval_map_item *ptr = v;
  5043. seq_printf(m, "%s %ld (%s)\n",
  5044. ptr->map.eval_string, ptr->map.eval_value,
  5045. ptr->map.system);
  5046. return 0;
  5047. }
  5048. static const struct seq_operations tracing_eval_map_seq_ops = {
  5049. .start = eval_map_start,
  5050. .next = eval_map_next,
  5051. .stop = eval_map_stop,
  5052. .show = eval_map_show,
  5053. };
  5054. static int tracing_eval_map_open(struct inode *inode, struct file *filp)
  5055. {
  5056. int ret;
  5057. ret = tracing_check_open_get_tr(NULL);
  5058. if (ret)
  5059. return ret;
  5060. return seq_open(filp, &tracing_eval_map_seq_ops);
  5061. }
  5062. static const struct file_operations tracing_eval_map_fops = {
  5063. .open = tracing_eval_map_open,
  5064. .read = seq_read,
  5065. .llseek = seq_lseek,
  5066. .release = seq_release,
  5067. };
  5068. static inline union trace_eval_map_item *
  5069. trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
  5070. {
  5071. /* Return tail of array given the head */
  5072. return ptr + ptr->head.length + 1;
  5073. }
  5074. static void
  5075. trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
  5076. int len)
  5077. {
  5078. struct trace_eval_map **stop;
  5079. struct trace_eval_map **map;
  5080. union trace_eval_map_item *map_array;
  5081. union trace_eval_map_item *ptr;
  5082. stop = start + len;
  5083. /*
  5084. * The trace_eval_maps contains the map plus a head and tail item,
  5085. * where the head holds the module and length of array, and the
  5086. * tail holds a pointer to the next list.
  5087. */
  5088. map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
  5089. if (!map_array) {
  5090. pr_warn("Unable to allocate trace eval mapping\n");
  5091. return;
  5092. }
  5093. mutex_lock(&trace_eval_mutex);
  5094. if (!trace_eval_maps)
  5095. trace_eval_maps = map_array;
  5096. else {
  5097. ptr = trace_eval_maps;
  5098. for (;;) {
  5099. ptr = trace_eval_jmp_to_tail(ptr);
  5100. if (!ptr->tail.next)
  5101. break;
  5102. ptr = ptr->tail.next;
  5103. }
  5104. ptr->tail.next = map_array;
  5105. }
  5106. map_array->head.mod = mod;
  5107. map_array->head.length = len;
  5108. map_array++;
  5109. for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
  5110. map_array->map = **map;
  5111. map_array++;
  5112. }
  5113. memset(map_array, 0, sizeof(*map_array));
  5114. mutex_unlock(&trace_eval_mutex);
  5115. }
  5116. static void trace_create_eval_file(struct dentry *d_tracer)
  5117. {
  5118. trace_create_file("eval_map", TRACE_MODE_READ, d_tracer,
  5119. NULL, &tracing_eval_map_fops);
  5120. }
  5121. #else /* CONFIG_TRACE_EVAL_MAP_FILE */
  5122. static inline void trace_create_eval_file(struct dentry *d_tracer) { }
  5123. static inline void trace_insert_eval_map_file(struct module *mod,
  5124. struct trace_eval_map **start, int len) { }
  5125. #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
  5126. static void trace_insert_eval_map(struct module *mod,
  5127. struct trace_eval_map **start, int len)
  5128. {
  5129. struct trace_eval_map **map;
  5130. if (len <= 0)
  5131. return;
  5132. map = start;
  5133. trace_event_eval_update(map, len);
  5134. trace_insert_eval_map_file(mod, start, len);
  5135. }
  5136. static ssize_t
  5137. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  5138. size_t cnt, loff_t *ppos)
  5139. {
  5140. struct trace_array *tr = filp->private_data;
  5141. char buf[MAX_TRACER_SIZE+2];
  5142. int r;
  5143. mutex_lock(&trace_types_lock);
  5144. r = sprintf(buf, "%s\n", tr->current_trace->name);
  5145. mutex_unlock(&trace_types_lock);
  5146. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5147. }
  5148. int tracer_init(struct tracer *t, struct trace_array *tr)
  5149. {
  5150. tracing_reset_online_cpus(&tr->array_buffer);
  5151. return t->init(tr);
  5152. }
  5153. static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
  5154. {
  5155. int cpu;
  5156. for_each_tracing_cpu(cpu)
  5157. per_cpu_ptr(buf->data, cpu)->entries = val;
  5158. }
  5159. static void update_buffer_entries(struct array_buffer *buf, int cpu)
  5160. {
  5161. if (cpu == RING_BUFFER_ALL_CPUS) {
  5162. set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0));
  5163. } else {
  5164. per_cpu_ptr(buf->data, cpu)->entries = ring_buffer_size(buf->buffer, cpu);
  5165. }
  5166. }
  5167. #ifdef CONFIG_TRACER_MAX_TRACE
  5168. /* resize @tr's buffer to the size of @size_tr's entries */
  5169. static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
  5170. struct array_buffer *size_buf, int cpu_id)
  5171. {
  5172. int cpu, ret = 0;
  5173. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  5174. for_each_tracing_cpu(cpu) {
  5175. ret = ring_buffer_resize(trace_buf->buffer,
  5176. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  5177. if (ret < 0)
  5178. break;
  5179. per_cpu_ptr(trace_buf->data, cpu)->entries =
  5180. per_cpu_ptr(size_buf->data, cpu)->entries;
  5181. }
  5182. } else {
  5183. ret = ring_buffer_resize(trace_buf->buffer,
  5184. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  5185. if (ret == 0)
  5186. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  5187. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  5188. }
  5189. return ret;
  5190. }
  5191. #endif /* CONFIG_TRACER_MAX_TRACE */
  5192. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  5193. unsigned long size, int cpu)
  5194. {
  5195. int ret;
  5196. /*
  5197. * If kernel or user changes the size of the ring buffer
  5198. * we use the size that was given, and we can forget about
  5199. * expanding it later.
  5200. */
  5201. ring_buffer_expanded = true;
  5202. /* May be called before buffers are initialized */
  5203. if (!tr->array_buffer.buffer)
  5204. return 0;
  5205. /* Do not allow tracing while resizng ring buffer */
  5206. tracing_stop_tr(tr);
  5207. ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
  5208. if (ret < 0)
  5209. goto out_start;
  5210. #ifdef CONFIG_TRACER_MAX_TRACE
  5211. if (!tr->current_trace->use_max_tr)
  5212. goto out;
  5213. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  5214. if (ret < 0) {
  5215. int r = resize_buffer_duplicate_size(&tr->array_buffer,
  5216. &tr->array_buffer, cpu);
  5217. if (r < 0) {
  5218. /*
  5219. * AARGH! We are left with different
  5220. * size max buffer!!!!
  5221. * The max buffer is our "snapshot" buffer.
  5222. * When a tracer needs a snapshot (one of the
  5223. * latency tracers), it swaps the max buffer
  5224. * with the saved snap shot. We succeeded to
  5225. * update the size of the main buffer, but failed to
  5226. * update the size of the max buffer. But when we tried
  5227. * to reset the main buffer to the original size, we
  5228. * failed there too. This is very unlikely to
  5229. * happen, but if it does, warn and kill all
  5230. * tracing.
  5231. */
  5232. WARN_ON(1);
  5233. tracing_disabled = 1;
  5234. }
  5235. goto out_start;
  5236. }
  5237. update_buffer_entries(&tr->max_buffer, cpu);
  5238. out:
  5239. #endif /* CONFIG_TRACER_MAX_TRACE */
  5240. update_buffer_entries(&tr->array_buffer, cpu);
  5241. out_start:
  5242. tracing_start_tr(tr);
  5243. return ret;
  5244. }
  5245. ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  5246. unsigned long size, int cpu_id)
  5247. {
  5248. int ret;
  5249. mutex_lock(&trace_types_lock);
  5250. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  5251. /* make sure, this cpu is enabled in the mask */
  5252. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
  5253. ret = -EINVAL;
  5254. goto out;
  5255. }
  5256. }
  5257. ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
  5258. if (ret < 0)
  5259. ret = -ENOMEM;
  5260. out:
  5261. mutex_unlock(&trace_types_lock);
  5262. return ret;
  5263. }
  5264. /**
  5265. * tracing_update_buffers - used by tracing facility to expand ring buffers
  5266. *
  5267. * To save on memory when the tracing is never used on a system with it
  5268. * configured in. The ring buffers are set to a minimum size. But once
  5269. * a user starts to use the tracing facility, then they need to grow
  5270. * to their default size.
  5271. *
  5272. * This function is to be called when a tracer is about to be used.
  5273. */
  5274. int tracing_update_buffers(void)
  5275. {
  5276. int ret = 0;
  5277. mutex_lock(&trace_types_lock);
  5278. if (!ring_buffer_expanded)
  5279. ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
  5280. RING_BUFFER_ALL_CPUS);
  5281. mutex_unlock(&trace_types_lock);
  5282. return ret;
  5283. }
  5284. struct trace_option_dentry;
  5285. static void
  5286. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  5287. /*
  5288. * Used to clear out the tracer before deletion of an instance.
  5289. * Must have trace_types_lock held.
  5290. */
  5291. static void tracing_set_nop(struct trace_array *tr)
  5292. {
  5293. if (tr->current_trace == &nop_trace)
  5294. return;
  5295. tr->current_trace->enabled--;
  5296. if (tr->current_trace->reset)
  5297. tr->current_trace->reset(tr);
  5298. tr->current_trace = &nop_trace;
  5299. }
  5300. static bool tracer_options_updated;
  5301. static void add_tracer_options(struct trace_array *tr, struct tracer *t)
  5302. {
  5303. /* Only enable if the directory has been created already. */
  5304. if (!tr->dir)
  5305. return;
  5306. /* Only create trace option files after update_tracer_options finish */
  5307. if (!tracer_options_updated)
  5308. return;
  5309. create_trace_option_files(tr, t);
  5310. }
  5311. int tracing_set_tracer(struct trace_array *tr, const char *buf)
  5312. {
  5313. struct tracer *t;
  5314. #ifdef CONFIG_TRACER_MAX_TRACE
  5315. bool had_max_tr;
  5316. #endif
  5317. int ret = 0;
  5318. mutex_lock(&trace_types_lock);
  5319. if (!ring_buffer_expanded) {
  5320. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  5321. RING_BUFFER_ALL_CPUS);
  5322. if (ret < 0)
  5323. goto out;
  5324. ret = 0;
  5325. }
  5326. for (t = trace_types; t; t = t->next) {
  5327. if (strcmp(t->name, buf) == 0)
  5328. break;
  5329. }
  5330. if (!t) {
  5331. ret = -EINVAL;
  5332. goto out;
  5333. }
  5334. if (t == tr->current_trace)
  5335. goto out;
  5336. #ifdef CONFIG_TRACER_SNAPSHOT
  5337. if (t->use_max_tr) {
  5338. local_irq_disable();
  5339. arch_spin_lock(&tr->max_lock);
  5340. if (tr->cond_snapshot)
  5341. ret = -EBUSY;
  5342. arch_spin_unlock(&tr->max_lock);
  5343. local_irq_enable();
  5344. if (ret)
  5345. goto out;
  5346. }
  5347. #endif
  5348. /* Some tracers won't work on kernel command line */
  5349. if (system_state < SYSTEM_RUNNING && t->noboot) {
  5350. pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
  5351. t->name);
  5352. goto out;
  5353. }
  5354. /* Some tracers are only allowed for the top level buffer */
  5355. if (!trace_ok_for_array(t, tr)) {
  5356. ret = -EINVAL;
  5357. goto out;
  5358. }
  5359. /* If trace pipe files are being read, we can't change the tracer */
  5360. if (tr->trace_ref) {
  5361. ret = -EBUSY;
  5362. goto out;
  5363. }
  5364. trace_branch_disable();
  5365. tr->current_trace->enabled--;
  5366. if (tr->current_trace->reset)
  5367. tr->current_trace->reset(tr);
  5368. #ifdef CONFIG_TRACER_MAX_TRACE
  5369. had_max_tr = tr->current_trace->use_max_tr;
  5370. /* Current trace needs to be nop_trace before synchronize_rcu */
  5371. tr->current_trace = &nop_trace;
  5372. if (had_max_tr && !t->use_max_tr) {
  5373. /*
  5374. * We need to make sure that the update_max_tr sees that
  5375. * current_trace changed to nop_trace to keep it from
  5376. * swapping the buffers after we resize it.
  5377. * The update_max_tr is called from interrupts disabled
  5378. * so a synchronized_sched() is sufficient.
  5379. */
  5380. synchronize_rcu();
  5381. free_snapshot(tr);
  5382. }
  5383. if (t->use_max_tr && !tr->allocated_snapshot) {
  5384. ret = tracing_alloc_snapshot_instance(tr);
  5385. if (ret < 0)
  5386. goto out;
  5387. }
  5388. #else
  5389. tr->current_trace = &nop_trace;
  5390. #endif
  5391. if (t->init) {
  5392. ret = tracer_init(t, tr);
  5393. if (ret)
  5394. goto out;
  5395. }
  5396. tr->current_trace = t;
  5397. tr->current_trace->enabled++;
  5398. trace_branch_enable(tr);
  5399. out:
  5400. mutex_unlock(&trace_types_lock);
  5401. return ret;
  5402. }
  5403. static ssize_t
  5404. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  5405. size_t cnt, loff_t *ppos)
  5406. {
  5407. struct trace_array *tr = filp->private_data;
  5408. char buf[MAX_TRACER_SIZE+1];
  5409. char *name;
  5410. size_t ret;
  5411. int err;
  5412. ret = cnt;
  5413. if (cnt > MAX_TRACER_SIZE)
  5414. cnt = MAX_TRACER_SIZE;
  5415. if (copy_from_user(buf, ubuf, cnt))
  5416. return -EFAULT;
  5417. buf[cnt] = 0;
  5418. name = strim(buf);
  5419. err = tracing_set_tracer(tr, name);
  5420. if (err)
  5421. return err;
  5422. *ppos += ret;
  5423. return ret;
  5424. }
  5425. static ssize_t
  5426. tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
  5427. size_t cnt, loff_t *ppos)
  5428. {
  5429. char buf[64];
  5430. int r;
  5431. r = snprintf(buf, sizeof(buf), "%ld\n",
  5432. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  5433. if (r > sizeof(buf))
  5434. r = sizeof(buf);
  5435. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5436. }
  5437. static ssize_t
  5438. tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
  5439. size_t cnt, loff_t *ppos)
  5440. {
  5441. unsigned long val;
  5442. int ret;
  5443. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  5444. if (ret)
  5445. return ret;
  5446. *ptr = val * 1000;
  5447. return cnt;
  5448. }
  5449. static ssize_t
  5450. tracing_thresh_read(struct file *filp, char __user *ubuf,
  5451. size_t cnt, loff_t *ppos)
  5452. {
  5453. return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
  5454. }
  5455. static ssize_t
  5456. tracing_thresh_write(struct file *filp, const char __user *ubuf,
  5457. size_t cnt, loff_t *ppos)
  5458. {
  5459. struct trace_array *tr = filp->private_data;
  5460. int ret;
  5461. mutex_lock(&trace_types_lock);
  5462. ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
  5463. if (ret < 0)
  5464. goto out;
  5465. if (tr->current_trace->update_thresh) {
  5466. ret = tr->current_trace->update_thresh(tr);
  5467. if (ret < 0)
  5468. goto out;
  5469. }
  5470. ret = cnt;
  5471. out:
  5472. mutex_unlock(&trace_types_lock);
  5473. return ret;
  5474. }
  5475. #ifdef CONFIG_TRACER_MAX_TRACE
  5476. static ssize_t
  5477. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  5478. size_t cnt, loff_t *ppos)
  5479. {
  5480. struct trace_array *tr = filp->private_data;
  5481. return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos);
  5482. }
  5483. static ssize_t
  5484. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  5485. size_t cnt, loff_t *ppos)
  5486. {
  5487. struct trace_array *tr = filp->private_data;
  5488. return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos);
  5489. }
  5490. #endif
  5491. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  5492. {
  5493. struct trace_array *tr = inode->i_private;
  5494. struct trace_iterator *iter;
  5495. int ret;
  5496. ret = tracing_check_open_get_tr(tr);
  5497. if (ret)
  5498. return ret;
  5499. mutex_lock(&trace_types_lock);
  5500. /* create a buffer to store the information to pass to userspace */
  5501. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  5502. if (!iter) {
  5503. ret = -ENOMEM;
  5504. __trace_array_put(tr);
  5505. goto out;
  5506. }
  5507. trace_seq_init(&iter->seq);
  5508. iter->trace = tr->current_trace;
  5509. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  5510. ret = -ENOMEM;
  5511. goto fail;
  5512. }
  5513. /* trace pipe does not show start of buffer */
  5514. cpumask_setall(iter->started);
  5515. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  5516. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  5517. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  5518. if (trace_clocks[tr->clock_id].in_ns)
  5519. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  5520. iter->tr = tr;
  5521. iter->array_buffer = &tr->array_buffer;
  5522. iter->cpu_file = tracing_get_cpu(inode);
  5523. mutex_init(&iter->mutex);
  5524. filp->private_data = iter;
  5525. if (iter->trace->pipe_open)
  5526. iter->trace->pipe_open(iter);
  5527. nonseekable_open(inode, filp);
  5528. tr->trace_ref++;
  5529. out:
  5530. mutex_unlock(&trace_types_lock);
  5531. return ret;
  5532. fail:
  5533. kfree(iter);
  5534. __trace_array_put(tr);
  5535. mutex_unlock(&trace_types_lock);
  5536. return ret;
  5537. }
  5538. static int tracing_release_pipe(struct inode *inode, struct file *file)
  5539. {
  5540. struct trace_iterator *iter = file->private_data;
  5541. struct trace_array *tr = inode->i_private;
  5542. mutex_lock(&trace_types_lock);
  5543. tr->trace_ref--;
  5544. if (iter->trace->pipe_close)
  5545. iter->trace->pipe_close(iter);
  5546. mutex_unlock(&trace_types_lock);
  5547. free_cpumask_var(iter->started);
  5548. kfree(iter->fmt);
  5549. kfree(iter->temp);
  5550. mutex_destroy(&iter->mutex);
  5551. kfree(iter);
  5552. trace_array_put(tr);
  5553. return 0;
  5554. }
  5555. static __poll_t
  5556. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  5557. {
  5558. struct trace_array *tr = iter->tr;
  5559. /* Iterators are static, they should be filled or empty */
  5560. if (trace_buffer_iter(iter, iter->cpu_file))
  5561. return EPOLLIN | EPOLLRDNORM;
  5562. if (tr->trace_flags & TRACE_ITER_BLOCK)
  5563. /*
  5564. * Always select as readable when in blocking mode
  5565. */
  5566. return EPOLLIN | EPOLLRDNORM;
  5567. else
  5568. return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
  5569. filp, poll_table, iter->tr->buffer_percent);
  5570. }
  5571. static __poll_t
  5572. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  5573. {
  5574. struct trace_iterator *iter = filp->private_data;
  5575. return trace_poll(iter, filp, poll_table);
  5576. }
  5577. /* Must be called with iter->mutex held. */
  5578. static int tracing_wait_pipe(struct file *filp)
  5579. {
  5580. struct trace_iterator *iter = filp->private_data;
  5581. int ret;
  5582. while (trace_empty(iter)) {
  5583. if ((filp->f_flags & O_NONBLOCK)) {
  5584. return -EAGAIN;
  5585. }
  5586. /*
  5587. * We block until we read something and tracing is disabled.
  5588. * We still block if tracing is disabled, but we have never
  5589. * read anything. This allows a user to cat this file, and
  5590. * then enable tracing. But after we have read something,
  5591. * we give an EOF when tracing is again disabled.
  5592. *
  5593. * iter->pos will be 0 if we haven't read anything.
  5594. */
  5595. if (!tracer_tracing_is_on(iter->tr) && iter->pos)
  5596. break;
  5597. mutex_unlock(&iter->mutex);
  5598. ret = wait_on_pipe(iter, 0);
  5599. mutex_lock(&iter->mutex);
  5600. if (ret)
  5601. return ret;
  5602. }
  5603. return 1;
  5604. }
  5605. /*
  5606. * Consumer reader.
  5607. */
  5608. static ssize_t
  5609. tracing_read_pipe(struct file *filp, char __user *ubuf,
  5610. size_t cnt, loff_t *ppos)
  5611. {
  5612. struct trace_iterator *iter = filp->private_data;
  5613. ssize_t sret;
  5614. /*
  5615. * Avoid more than one consumer on a single file descriptor
  5616. * This is just a matter of traces coherency, the ring buffer itself
  5617. * is protected.
  5618. */
  5619. mutex_lock(&iter->mutex);
  5620. /* return any leftover data */
  5621. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  5622. if (sret != -EBUSY)
  5623. goto out;
  5624. trace_seq_init(&iter->seq);
  5625. if (iter->trace->read) {
  5626. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  5627. if (sret)
  5628. goto out;
  5629. }
  5630. waitagain:
  5631. sret = tracing_wait_pipe(filp);
  5632. if (sret <= 0)
  5633. goto out;
  5634. /* stop when tracing is finished */
  5635. if (trace_empty(iter)) {
  5636. sret = 0;
  5637. goto out;
  5638. }
  5639. if (cnt >= PAGE_SIZE)
  5640. cnt = PAGE_SIZE - 1;
  5641. /* reset all but tr, trace, and overruns */
  5642. trace_iterator_reset(iter);
  5643. cpumask_clear(iter->started);
  5644. trace_seq_init(&iter->seq);
  5645. trace_event_read_lock();
  5646. trace_access_lock(iter->cpu_file);
  5647. while (trace_find_next_entry_inc(iter) != NULL) {
  5648. enum print_line_t ret;
  5649. int save_len = iter->seq.seq.len;
  5650. ret = print_trace_line(iter);
  5651. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  5652. /*
  5653. * If one print_trace_line() fills entire trace_seq in one shot,
  5654. * trace_seq_to_user() will returns -EBUSY because save_len == 0,
  5655. * In this case, we need to consume it, otherwise, loop will peek
  5656. * this event next time, resulting in an infinite loop.
  5657. */
  5658. if (save_len == 0) {
  5659. iter->seq.full = 0;
  5660. trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
  5661. trace_consume(iter);
  5662. break;
  5663. }
  5664. /* In other cases, don't print partial lines */
  5665. iter->seq.seq.len = save_len;
  5666. break;
  5667. }
  5668. if (ret != TRACE_TYPE_NO_CONSUME)
  5669. trace_consume(iter);
  5670. if (trace_seq_used(&iter->seq) >= cnt)
  5671. break;
  5672. /*
  5673. * Setting the full flag means we reached the trace_seq buffer
  5674. * size and we should leave by partial output condition above.
  5675. * One of the trace_seq_* functions is not used properly.
  5676. */
  5677. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  5678. iter->ent->type);
  5679. }
  5680. trace_access_unlock(iter->cpu_file);
  5681. trace_event_read_unlock();
  5682. /* Now copy what we have to the user */
  5683. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  5684. if (iter->seq.seq.readpos >= trace_seq_used(&iter->seq))
  5685. trace_seq_init(&iter->seq);
  5686. /*
  5687. * If there was nothing to send to user, in spite of consuming trace
  5688. * entries, go back to wait for more entries.
  5689. */
  5690. if (sret == -EBUSY)
  5691. goto waitagain;
  5692. out:
  5693. mutex_unlock(&iter->mutex);
  5694. return sret;
  5695. }
  5696. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  5697. unsigned int idx)
  5698. {
  5699. __free_page(spd->pages[idx]);
  5700. }
  5701. static size_t
  5702. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  5703. {
  5704. size_t count;
  5705. int save_len;
  5706. int ret;
  5707. /* Seq buffer is page-sized, exactly what we need. */
  5708. for (;;) {
  5709. save_len = iter->seq.seq.len;
  5710. ret = print_trace_line(iter);
  5711. if (trace_seq_has_overflowed(&iter->seq)) {
  5712. iter->seq.seq.len = save_len;
  5713. break;
  5714. }
  5715. /*
  5716. * This should not be hit, because it should only
  5717. * be set if the iter->seq overflowed. But check it
  5718. * anyway to be safe.
  5719. */
  5720. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  5721. iter->seq.seq.len = save_len;
  5722. break;
  5723. }
  5724. count = trace_seq_used(&iter->seq) - save_len;
  5725. if (rem < count) {
  5726. rem = 0;
  5727. iter->seq.seq.len = save_len;
  5728. break;
  5729. }
  5730. if (ret != TRACE_TYPE_NO_CONSUME)
  5731. trace_consume(iter);
  5732. rem -= count;
  5733. if (!trace_find_next_entry_inc(iter)) {
  5734. rem = 0;
  5735. iter->ent = NULL;
  5736. break;
  5737. }
  5738. }
  5739. return rem;
  5740. }
  5741. static ssize_t tracing_splice_read_pipe(struct file *filp,
  5742. loff_t *ppos,
  5743. struct pipe_inode_info *pipe,
  5744. size_t len,
  5745. unsigned int flags)
  5746. {
  5747. struct page *pages_def[PIPE_DEF_BUFFERS];
  5748. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  5749. struct trace_iterator *iter = filp->private_data;
  5750. struct splice_pipe_desc spd = {
  5751. .pages = pages_def,
  5752. .partial = partial_def,
  5753. .nr_pages = 0, /* This gets updated below. */
  5754. .nr_pages_max = PIPE_DEF_BUFFERS,
  5755. .ops = &default_pipe_buf_ops,
  5756. .spd_release = tracing_spd_release_pipe,
  5757. };
  5758. ssize_t ret;
  5759. size_t rem;
  5760. unsigned int i;
  5761. if (splice_grow_spd(pipe, &spd))
  5762. return -ENOMEM;
  5763. mutex_lock(&iter->mutex);
  5764. if (iter->trace->splice_read) {
  5765. ret = iter->trace->splice_read(iter, filp,
  5766. ppos, pipe, len, flags);
  5767. if (ret)
  5768. goto out_err;
  5769. }
  5770. ret = tracing_wait_pipe(filp);
  5771. if (ret <= 0)
  5772. goto out_err;
  5773. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  5774. ret = -EFAULT;
  5775. goto out_err;
  5776. }
  5777. trace_event_read_lock();
  5778. trace_access_lock(iter->cpu_file);
  5779. /* Fill as many pages as possible. */
  5780. for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
  5781. spd.pages[i] = alloc_page(GFP_KERNEL);
  5782. if (!spd.pages[i])
  5783. break;
  5784. rem = tracing_fill_pipe_page(rem, iter);
  5785. /* Copy the data into the page, so we can start over. */
  5786. ret = trace_seq_to_buffer(&iter->seq,
  5787. page_address(spd.pages[i]),
  5788. trace_seq_used(&iter->seq));
  5789. if (ret < 0) {
  5790. __free_page(spd.pages[i]);
  5791. break;
  5792. }
  5793. spd.partial[i].offset = 0;
  5794. spd.partial[i].len = trace_seq_used(&iter->seq);
  5795. trace_seq_init(&iter->seq);
  5796. }
  5797. trace_access_unlock(iter->cpu_file);
  5798. trace_event_read_unlock();
  5799. mutex_unlock(&iter->mutex);
  5800. spd.nr_pages = i;
  5801. if (i)
  5802. ret = splice_to_pipe(pipe, &spd);
  5803. else
  5804. ret = 0;
  5805. out:
  5806. splice_shrink_spd(&spd);
  5807. return ret;
  5808. out_err:
  5809. mutex_unlock(&iter->mutex);
  5810. goto out;
  5811. }
  5812. static ssize_t
  5813. tracing_entries_read(struct file *filp, char __user *ubuf,
  5814. size_t cnt, loff_t *ppos)
  5815. {
  5816. struct inode *inode = file_inode(filp);
  5817. struct trace_array *tr = inode->i_private;
  5818. int cpu = tracing_get_cpu(inode);
  5819. char buf[64];
  5820. int r = 0;
  5821. ssize_t ret;
  5822. mutex_lock(&trace_types_lock);
  5823. if (cpu == RING_BUFFER_ALL_CPUS) {
  5824. int cpu, buf_size_same;
  5825. unsigned long size;
  5826. size = 0;
  5827. buf_size_same = 1;
  5828. /* check if all cpu sizes are same */
  5829. for_each_tracing_cpu(cpu) {
  5830. /* fill in the size from first enabled cpu */
  5831. if (size == 0)
  5832. size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
  5833. if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
  5834. buf_size_same = 0;
  5835. break;
  5836. }
  5837. }
  5838. if (buf_size_same) {
  5839. if (!ring_buffer_expanded)
  5840. r = sprintf(buf, "%lu (expanded: %lu)\n",
  5841. size >> 10,
  5842. trace_buf_size >> 10);
  5843. else
  5844. r = sprintf(buf, "%lu\n", size >> 10);
  5845. } else
  5846. r = sprintf(buf, "X\n");
  5847. } else
  5848. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
  5849. mutex_unlock(&trace_types_lock);
  5850. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5851. return ret;
  5852. }
  5853. static ssize_t
  5854. tracing_entries_write(struct file *filp, const char __user *ubuf,
  5855. size_t cnt, loff_t *ppos)
  5856. {
  5857. struct inode *inode = file_inode(filp);
  5858. struct trace_array *tr = inode->i_private;
  5859. unsigned long val;
  5860. int ret;
  5861. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  5862. if (ret)
  5863. return ret;
  5864. /* must have at least 1 entry */
  5865. if (!val)
  5866. return -EINVAL;
  5867. /* value is in KB */
  5868. val <<= 10;
  5869. ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
  5870. if (ret < 0)
  5871. return ret;
  5872. *ppos += cnt;
  5873. return cnt;
  5874. }
  5875. static ssize_t
  5876. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  5877. size_t cnt, loff_t *ppos)
  5878. {
  5879. struct trace_array *tr = filp->private_data;
  5880. char buf[64];
  5881. int r, cpu;
  5882. unsigned long size = 0, expanded_size = 0;
  5883. mutex_lock(&trace_types_lock);
  5884. for_each_tracing_cpu(cpu) {
  5885. size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
  5886. if (!ring_buffer_expanded)
  5887. expanded_size += trace_buf_size >> 10;
  5888. }
  5889. if (ring_buffer_expanded)
  5890. r = sprintf(buf, "%lu\n", size);
  5891. else
  5892. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  5893. mutex_unlock(&trace_types_lock);
  5894. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5895. }
  5896. static ssize_t
  5897. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  5898. size_t cnt, loff_t *ppos)
  5899. {
  5900. /*
  5901. * There is no need to read what the user has written, this function
  5902. * is just to make sure that there is no error when "echo" is used
  5903. */
  5904. *ppos += cnt;
  5905. return cnt;
  5906. }
  5907. static int
  5908. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  5909. {
  5910. struct trace_array *tr = inode->i_private;
  5911. /* disable tracing ? */
  5912. if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
  5913. tracer_tracing_off(tr);
  5914. /* resize the ring buffer to 0 */
  5915. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  5916. trace_array_put(tr);
  5917. return 0;
  5918. }
  5919. static ssize_t
  5920. tracing_mark_write(struct file *filp, const char __user *ubuf,
  5921. size_t cnt, loff_t *fpos)
  5922. {
  5923. struct trace_array *tr = filp->private_data;
  5924. struct ring_buffer_event *event;
  5925. enum event_trigger_type tt = ETT_NONE;
  5926. struct trace_buffer *buffer;
  5927. struct print_entry *entry;
  5928. ssize_t written;
  5929. int size;
  5930. int len;
  5931. /* Used in tracing_mark_raw_write() as well */
  5932. #define FAULTED_STR "<faulted>"
  5933. #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
  5934. if (tracing_disabled)
  5935. return -EINVAL;
  5936. if (!(tr->trace_flags & TRACE_ITER_MARKERS))
  5937. return -EINVAL;
  5938. if (cnt > TRACE_BUF_SIZE)
  5939. cnt = TRACE_BUF_SIZE;
  5940. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  5941. size = sizeof(*entry) + cnt + 2; /* add '\0' and possible '\n' */
  5942. /* If less than "<faulted>", then make sure we can still add that */
  5943. if (cnt < FAULTED_SIZE)
  5944. size += FAULTED_SIZE - cnt;
  5945. buffer = tr->array_buffer.buffer;
  5946. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  5947. tracing_gen_ctx());
  5948. if (unlikely(!event))
  5949. /* Ring buffer disabled, return as if not open for write */
  5950. return -EBADF;
  5951. entry = ring_buffer_event_data(event);
  5952. entry->ip = _THIS_IP_;
  5953. len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
  5954. if (len) {
  5955. memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
  5956. cnt = FAULTED_SIZE;
  5957. written = -EFAULT;
  5958. } else
  5959. written = cnt;
  5960. if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
  5961. /* do not add \n before testing triggers, but add \0 */
  5962. entry->buf[cnt] = '\0';
  5963. tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event);
  5964. }
  5965. if (entry->buf[cnt - 1] != '\n') {
  5966. entry->buf[cnt] = '\n';
  5967. entry->buf[cnt + 1] = '\0';
  5968. } else
  5969. entry->buf[cnt] = '\0';
  5970. if (static_branch_unlikely(&trace_marker_exports_enabled))
  5971. ftrace_exports(event, TRACE_EXPORT_MARKER);
  5972. __buffer_unlock_commit(buffer, event);
  5973. if (tt)
  5974. event_triggers_post_call(tr->trace_marker_file, tt);
  5975. return written;
  5976. }
  5977. /* Limit it for now to 3K (including tag) */
  5978. #define RAW_DATA_MAX_SIZE (1024*3)
  5979. static ssize_t
  5980. tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
  5981. size_t cnt, loff_t *fpos)
  5982. {
  5983. struct trace_array *tr = filp->private_data;
  5984. struct ring_buffer_event *event;
  5985. struct trace_buffer *buffer;
  5986. struct raw_data_entry *entry;
  5987. ssize_t written;
  5988. int size;
  5989. int len;
  5990. #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
  5991. if (tracing_disabled)
  5992. return -EINVAL;
  5993. if (!(tr->trace_flags & TRACE_ITER_MARKERS))
  5994. return -EINVAL;
  5995. /* The marker must at least have a tag id */
  5996. if (cnt < sizeof(unsigned int) || cnt > RAW_DATA_MAX_SIZE)
  5997. return -EINVAL;
  5998. if (cnt > TRACE_BUF_SIZE)
  5999. cnt = TRACE_BUF_SIZE;
  6000. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  6001. size = sizeof(*entry) + cnt;
  6002. if (cnt < FAULT_SIZE_ID)
  6003. size += FAULT_SIZE_ID - cnt;
  6004. buffer = tr->array_buffer.buffer;
  6005. event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
  6006. tracing_gen_ctx());
  6007. if (!event)
  6008. /* Ring buffer disabled, return as if not open for write */
  6009. return -EBADF;
  6010. entry = ring_buffer_event_data(event);
  6011. len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
  6012. if (len) {
  6013. entry->id = -1;
  6014. memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
  6015. written = -EFAULT;
  6016. } else
  6017. written = cnt;
  6018. __buffer_unlock_commit(buffer, event);
  6019. return written;
  6020. }
  6021. static int tracing_clock_show(struct seq_file *m, void *v)
  6022. {
  6023. struct trace_array *tr = m->private;
  6024. int i;
  6025. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  6026. seq_printf(m,
  6027. "%s%s%s%s", i ? " " : "",
  6028. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  6029. i == tr->clock_id ? "]" : "");
  6030. seq_putc(m, '\n');
  6031. return 0;
  6032. }
  6033. int tracing_set_clock(struct trace_array *tr, const char *clockstr)
  6034. {
  6035. int i;
  6036. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  6037. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  6038. break;
  6039. }
  6040. if (i == ARRAY_SIZE(trace_clocks))
  6041. return -EINVAL;
  6042. mutex_lock(&trace_types_lock);
  6043. tr->clock_id = i;
  6044. ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
  6045. /*
  6046. * New clock may not be consistent with the previous clock.
  6047. * Reset the buffer so that it doesn't have incomparable timestamps.
  6048. */
  6049. tracing_reset_online_cpus(&tr->array_buffer);
  6050. #ifdef CONFIG_TRACER_MAX_TRACE
  6051. if (tr->max_buffer.buffer)
  6052. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  6053. tracing_reset_online_cpus(&tr->max_buffer);
  6054. #endif
  6055. mutex_unlock(&trace_types_lock);
  6056. return 0;
  6057. }
  6058. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  6059. size_t cnt, loff_t *fpos)
  6060. {
  6061. struct seq_file *m = filp->private_data;
  6062. struct trace_array *tr = m->private;
  6063. char buf[64];
  6064. const char *clockstr;
  6065. int ret;
  6066. if (cnt >= sizeof(buf))
  6067. return -EINVAL;
  6068. if (copy_from_user(buf, ubuf, cnt))
  6069. return -EFAULT;
  6070. buf[cnt] = 0;
  6071. clockstr = strstrip(buf);
  6072. ret = tracing_set_clock(tr, clockstr);
  6073. if (ret)
  6074. return ret;
  6075. *fpos += cnt;
  6076. return cnt;
  6077. }
  6078. static int tracing_clock_open(struct inode *inode, struct file *file)
  6079. {
  6080. struct trace_array *tr = inode->i_private;
  6081. int ret;
  6082. ret = tracing_check_open_get_tr(tr);
  6083. if (ret)
  6084. return ret;
  6085. ret = single_open(file, tracing_clock_show, inode->i_private);
  6086. if (ret < 0)
  6087. trace_array_put(tr);
  6088. return ret;
  6089. }
  6090. static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
  6091. {
  6092. struct trace_array *tr = m->private;
  6093. mutex_lock(&trace_types_lock);
  6094. if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
  6095. seq_puts(m, "delta [absolute]\n");
  6096. else
  6097. seq_puts(m, "[delta] absolute\n");
  6098. mutex_unlock(&trace_types_lock);
  6099. return 0;
  6100. }
  6101. static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
  6102. {
  6103. struct trace_array *tr = inode->i_private;
  6104. int ret;
  6105. ret = tracing_check_open_get_tr(tr);
  6106. if (ret)
  6107. return ret;
  6108. ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
  6109. if (ret < 0)
  6110. trace_array_put(tr);
  6111. return ret;
  6112. }
  6113. u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe)
  6114. {
  6115. if (rbe == this_cpu_read(trace_buffered_event))
  6116. return ring_buffer_time_stamp(buffer);
  6117. return ring_buffer_event_time_stamp(buffer, rbe);
  6118. }
  6119. /*
  6120. * Set or disable using the per CPU trace_buffer_event when possible.
  6121. */
  6122. int tracing_set_filter_buffering(struct trace_array *tr, bool set)
  6123. {
  6124. int ret = 0;
  6125. mutex_lock(&trace_types_lock);
  6126. if (set && tr->no_filter_buffering_ref++)
  6127. goto out;
  6128. if (!set) {
  6129. if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) {
  6130. ret = -EINVAL;
  6131. goto out;
  6132. }
  6133. --tr->no_filter_buffering_ref;
  6134. }
  6135. out:
  6136. mutex_unlock(&trace_types_lock);
  6137. return ret;
  6138. }
  6139. struct ftrace_buffer_info {
  6140. struct trace_iterator iter;
  6141. void *spare;
  6142. unsigned int spare_cpu;
  6143. unsigned int read;
  6144. };
  6145. #ifdef CONFIG_TRACER_SNAPSHOT
  6146. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  6147. {
  6148. struct trace_array *tr = inode->i_private;
  6149. struct trace_iterator *iter;
  6150. struct seq_file *m;
  6151. int ret;
  6152. ret = tracing_check_open_get_tr(tr);
  6153. if (ret)
  6154. return ret;
  6155. if (file->f_mode & FMODE_READ) {
  6156. iter = __tracing_open(inode, file, true);
  6157. if (IS_ERR(iter))
  6158. ret = PTR_ERR(iter);
  6159. } else {
  6160. /* Writes still need the seq_file to hold the private data */
  6161. ret = -ENOMEM;
  6162. m = kzalloc(sizeof(*m), GFP_KERNEL);
  6163. if (!m)
  6164. goto out;
  6165. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  6166. if (!iter) {
  6167. kfree(m);
  6168. goto out;
  6169. }
  6170. ret = 0;
  6171. iter->tr = tr;
  6172. iter->array_buffer = &tr->max_buffer;
  6173. iter->cpu_file = tracing_get_cpu(inode);
  6174. m->private = iter;
  6175. file->private_data = m;
  6176. }
  6177. out:
  6178. if (ret < 0)
  6179. trace_array_put(tr);
  6180. return ret;
  6181. }
  6182. static void tracing_swap_cpu_buffer(void *tr)
  6183. {
  6184. update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
  6185. }
  6186. static ssize_t
  6187. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  6188. loff_t *ppos)
  6189. {
  6190. struct seq_file *m = filp->private_data;
  6191. struct trace_iterator *iter = m->private;
  6192. struct trace_array *tr = iter->tr;
  6193. unsigned long val;
  6194. int ret;
  6195. ret = tracing_update_buffers();
  6196. if (ret < 0)
  6197. return ret;
  6198. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  6199. if (ret)
  6200. return ret;
  6201. mutex_lock(&trace_types_lock);
  6202. if (tr->current_trace->use_max_tr) {
  6203. ret = -EBUSY;
  6204. goto out;
  6205. }
  6206. local_irq_disable();
  6207. arch_spin_lock(&tr->max_lock);
  6208. if (tr->cond_snapshot)
  6209. ret = -EBUSY;
  6210. arch_spin_unlock(&tr->max_lock);
  6211. local_irq_enable();
  6212. if (ret)
  6213. goto out;
  6214. switch (val) {
  6215. case 0:
  6216. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  6217. ret = -EINVAL;
  6218. break;
  6219. }
  6220. if (tr->allocated_snapshot)
  6221. free_snapshot(tr);
  6222. break;
  6223. case 1:
  6224. /* Only allow per-cpu swap if the ring buffer supports it */
  6225. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  6226. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  6227. ret = -EINVAL;
  6228. break;
  6229. }
  6230. #endif
  6231. if (tr->allocated_snapshot)
  6232. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  6233. &tr->array_buffer, iter->cpu_file);
  6234. else
  6235. ret = tracing_alloc_snapshot_instance(tr);
  6236. if (ret < 0)
  6237. break;
  6238. /* Now, we're going to swap */
  6239. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  6240. local_irq_disable();
  6241. update_max_tr(tr, current, smp_processor_id(), NULL);
  6242. local_irq_enable();
  6243. } else {
  6244. smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
  6245. (void *)tr, 1);
  6246. }
  6247. break;
  6248. default:
  6249. if (tr->allocated_snapshot) {
  6250. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  6251. tracing_reset_online_cpus(&tr->max_buffer);
  6252. else
  6253. tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
  6254. }
  6255. break;
  6256. }
  6257. if (ret >= 0) {
  6258. *ppos += cnt;
  6259. ret = cnt;
  6260. }
  6261. out:
  6262. mutex_unlock(&trace_types_lock);
  6263. return ret;
  6264. }
  6265. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  6266. {
  6267. struct seq_file *m = file->private_data;
  6268. int ret;
  6269. ret = tracing_release(inode, file);
  6270. if (file->f_mode & FMODE_READ)
  6271. return ret;
  6272. /* If write only, the seq_file is just a stub */
  6273. if (m)
  6274. kfree(m->private);
  6275. kfree(m);
  6276. return 0;
  6277. }
  6278. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  6279. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  6280. size_t count, loff_t *ppos);
  6281. static int tracing_buffers_release(struct inode *inode, struct file *file);
  6282. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  6283. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  6284. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  6285. {
  6286. struct ftrace_buffer_info *info;
  6287. int ret;
  6288. /* The following checks for tracefs lockdown */
  6289. ret = tracing_buffers_open(inode, filp);
  6290. if (ret < 0)
  6291. return ret;
  6292. info = filp->private_data;
  6293. if (info->iter.trace->use_max_tr) {
  6294. tracing_buffers_release(inode, filp);
  6295. return -EBUSY;
  6296. }
  6297. info->iter.snapshot = true;
  6298. info->iter.array_buffer = &info->iter.tr->max_buffer;
  6299. return ret;
  6300. }
  6301. #endif /* CONFIG_TRACER_SNAPSHOT */
  6302. static const struct file_operations tracing_thresh_fops = {
  6303. .open = tracing_open_generic,
  6304. .read = tracing_thresh_read,
  6305. .write = tracing_thresh_write,
  6306. .llseek = generic_file_llseek,
  6307. };
  6308. #ifdef CONFIG_TRACER_MAX_TRACE
  6309. static const struct file_operations tracing_max_lat_fops = {
  6310. .open = tracing_open_generic_tr,
  6311. .read = tracing_max_lat_read,
  6312. .write = tracing_max_lat_write,
  6313. .llseek = generic_file_llseek,
  6314. .release = tracing_release_generic_tr,
  6315. };
  6316. #endif
  6317. static const struct file_operations set_tracer_fops = {
  6318. .open = tracing_open_generic_tr,
  6319. .read = tracing_set_trace_read,
  6320. .write = tracing_set_trace_write,
  6321. .llseek = generic_file_llseek,
  6322. .release = tracing_release_generic_tr,
  6323. };
  6324. static const struct file_operations tracing_pipe_fops = {
  6325. .open = tracing_open_pipe,
  6326. .poll = tracing_poll_pipe,
  6327. .read = tracing_read_pipe,
  6328. .splice_read = tracing_splice_read_pipe,
  6329. .release = tracing_release_pipe,
  6330. .llseek = no_llseek,
  6331. };
  6332. static const struct file_operations tracing_entries_fops = {
  6333. .open = tracing_open_generic_tr,
  6334. .read = tracing_entries_read,
  6335. .write = tracing_entries_write,
  6336. .llseek = generic_file_llseek,
  6337. .release = tracing_release_generic_tr,
  6338. };
  6339. static const struct file_operations tracing_total_entries_fops = {
  6340. .open = tracing_open_generic_tr,
  6341. .read = tracing_total_entries_read,
  6342. .llseek = generic_file_llseek,
  6343. .release = tracing_release_generic_tr,
  6344. };
  6345. static const struct file_operations tracing_free_buffer_fops = {
  6346. .open = tracing_open_generic_tr,
  6347. .write = tracing_free_buffer_write,
  6348. .release = tracing_free_buffer_release,
  6349. };
  6350. static const struct file_operations tracing_mark_fops = {
  6351. .open = tracing_mark_open,
  6352. .write = tracing_mark_write,
  6353. .release = tracing_release_generic_tr,
  6354. };
  6355. static const struct file_operations tracing_mark_raw_fops = {
  6356. .open = tracing_mark_open,
  6357. .write = tracing_mark_raw_write,
  6358. .release = tracing_release_generic_tr,
  6359. };
  6360. static const struct file_operations trace_clock_fops = {
  6361. .open = tracing_clock_open,
  6362. .read = seq_read,
  6363. .llseek = seq_lseek,
  6364. .release = tracing_single_release_tr,
  6365. .write = tracing_clock_write,
  6366. };
  6367. static const struct file_operations trace_time_stamp_mode_fops = {
  6368. .open = tracing_time_stamp_mode_open,
  6369. .read = seq_read,
  6370. .llseek = seq_lseek,
  6371. .release = tracing_single_release_tr,
  6372. };
  6373. #ifdef CONFIG_TRACER_SNAPSHOT
  6374. static const struct file_operations snapshot_fops = {
  6375. .open = tracing_snapshot_open,
  6376. .read = seq_read,
  6377. .write = tracing_snapshot_write,
  6378. .llseek = tracing_lseek,
  6379. .release = tracing_snapshot_release,
  6380. };
  6381. static const struct file_operations snapshot_raw_fops = {
  6382. .open = snapshot_raw_open,
  6383. .read = tracing_buffers_read,
  6384. .release = tracing_buffers_release,
  6385. .splice_read = tracing_buffers_splice_read,
  6386. .llseek = no_llseek,
  6387. };
  6388. #endif /* CONFIG_TRACER_SNAPSHOT */
  6389. /*
  6390. * trace_min_max_write - Write a u64 value to a trace_min_max_param struct
  6391. * @filp: The active open file structure
  6392. * @ubuf: The userspace provided buffer to read value into
  6393. * @cnt: The maximum number of bytes to read
  6394. * @ppos: The current "file" position
  6395. *
  6396. * This function implements the write interface for a struct trace_min_max_param.
  6397. * The filp->private_data must point to a trace_min_max_param structure that
  6398. * defines where to write the value, the min and the max acceptable values,
  6399. * and a lock to protect the write.
  6400. */
  6401. static ssize_t
  6402. trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
  6403. {
  6404. struct trace_min_max_param *param = filp->private_data;
  6405. u64 val;
  6406. int err;
  6407. if (!param)
  6408. return -EFAULT;
  6409. err = kstrtoull_from_user(ubuf, cnt, 10, &val);
  6410. if (err)
  6411. return err;
  6412. if (param->lock)
  6413. mutex_lock(param->lock);
  6414. if (param->min && val < *param->min)
  6415. err = -EINVAL;
  6416. if (param->max && val > *param->max)
  6417. err = -EINVAL;
  6418. if (!err)
  6419. *param->val = val;
  6420. if (param->lock)
  6421. mutex_unlock(param->lock);
  6422. if (err)
  6423. return err;
  6424. return cnt;
  6425. }
  6426. /*
  6427. * trace_min_max_read - Read a u64 value from a trace_min_max_param struct
  6428. * @filp: The active open file structure
  6429. * @ubuf: The userspace provided buffer to read value into
  6430. * @cnt: The maximum number of bytes to read
  6431. * @ppos: The current "file" position
  6432. *
  6433. * This function implements the read interface for a struct trace_min_max_param.
  6434. * The filp->private_data must point to a trace_min_max_param struct with valid
  6435. * data.
  6436. */
  6437. static ssize_t
  6438. trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  6439. {
  6440. struct trace_min_max_param *param = filp->private_data;
  6441. char buf[U64_STR_SIZE];
  6442. int len;
  6443. u64 val;
  6444. if (!param)
  6445. return -EFAULT;
  6446. val = *param->val;
  6447. if (cnt > sizeof(buf))
  6448. cnt = sizeof(buf);
  6449. len = snprintf(buf, sizeof(buf), "%llu\n", val);
  6450. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  6451. }
  6452. const struct file_operations trace_min_max_fops = {
  6453. .open = tracing_open_generic,
  6454. .read = trace_min_max_read,
  6455. .write = trace_min_max_write,
  6456. };
  6457. #define TRACING_LOG_ERRS_MAX 8
  6458. #define TRACING_LOG_LOC_MAX 128
  6459. #define CMD_PREFIX " Command: "
  6460. struct err_info {
  6461. const char **errs; /* ptr to loc-specific array of err strings */
  6462. u8 type; /* index into errs -> specific err string */
  6463. u16 pos; /* caret position */
  6464. u64 ts;
  6465. };
  6466. struct tracing_log_err {
  6467. struct list_head list;
  6468. struct err_info info;
  6469. char loc[TRACING_LOG_LOC_MAX]; /* err location */
  6470. char *cmd; /* what caused err */
  6471. };
  6472. static DEFINE_MUTEX(tracing_err_log_lock);
  6473. static struct tracing_log_err *alloc_tracing_log_err(int len)
  6474. {
  6475. struct tracing_log_err *err;
  6476. err = kzalloc(sizeof(*err), GFP_KERNEL);
  6477. if (!err)
  6478. return ERR_PTR(-ENOMEM);
  6479. err->cmd = kzalloc(len, GFP_KERNEL);
  6480. if (!err->cmd) {
  6481. kfree(err);
  6482. return ERR_PTR(-ENOMEM);
  6483. }
  6484. return err;
  6485. }
  6486. static void free_tracing_log_err(struct tracing_log_err *err)
  6487. {
  6488. kfree(err->cmd);
  6489. kfree(err);
  6490. }
  6491. static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr,
  6492. int len)
  6493. {
  6494. struct tracing_log_err *err;
  6495. char *cmd;
  6496. if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
  6497. err = alloc_tracing_log_err(len);
  6498. if (PTR_ERR(err) != -ENOMEM)
  6499. tr->n_err_log_entries++;
  6500. return err;
  6501. }
  6502. cmd = kzalloc(len, GFP_KERNEL);
  6503. if (!cmd)
  6504. return ERR_PTR(-ENOMEM);
  6505. err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
  6506. kfree(err->cmd);
  6507. err->cmd = cmd;
  6508. list_del(&err->list);
  6509. return err;
  6510. }
  6511. /**
  6512. * err_pos - find the position of a string within a command for error careting
  6513. * @cmd: The tracing command that caused the error
  6514. * @str: The string to position the caret at within @cmd
  6515. *
  6516. * Finds the position of the first occurrence of @str within @cmd. The
  6517. * return value can be passed to tracing_log_err() for caret placement
  6518. * within @cmd.
  6519. *
  6520. * Returns the index within @cmd of the first occurrence of @str or 0
  6521. * if @str was not found.
  6522. */
  6523. unsigned int err_pos(char *cmd, const char *str)
  6524. {
  6525. char *found;
  6526. if (WARN_ON(!strlen(cmd)))
  6527. return 0;
  6528. found = strstr(cmd, str);
  6529. if (found)
  6530. return found - cmd;
  6531. return 0;
  6532. }
  6533. /**
  6534. * tracing_log_err - write an error to the tracing error log
  6535. * @tr: The associated trace array for the error (NULL for top level array)
  6536. * @loc: A string describing where the error occurred
  6537. * @cmd: The tracing command that caused the error
  6538. * @errs: The array of loc-specific static error strings
  6539. * @type: The index into errs[], which produces the specific static err string
  6540. * @pos: The position the caret should be placed in the cmd
  6541. *
  6542. * Writes an error into tracing/error_log of the form:
  6543. *
  6544. * <loc>: error: <text>
  6545. * Command: <cmd>
  6546. * ^
  6547. *
  6548. * tracing/error_log is a small log file containing the last
  6549. * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
  6550. * unless there has been a tracing error, and the error log can be
  6551. * cleared and have its memory freed by writing the empty string in
  6552. * truncation mode to it i.e. echo > tracing/error_log.
  6553. *
  6554. * NOTE: the @errs array along with the @type param are used to
  6555. * produce a static error string - this string is not copied and saved
  6556. * when the error is logged - only a pointer to it is saved. See
  6557. * existing callers for examples of how static strings are typically
  6558. * defined for use with tracing_log_err().
  6559. */
  6560. void tracing_log_err(struct trace_array *tr,
  6561. const char *loc, const char *cmd,
  6562. const char **errs, u8 type, u16 pos)
  6563. {
  6564. struct tracing_log_err *err;
  6565. int len = 0;
  6566. if (!tr)
  6567. tr = &global_trace;
  6568. len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1;
  6569. mutex_lock(&tracing_err_log_lock);
  6570. err = get_tracing_log_err(tr, len);
  6571. if (PTR_ERR(err) == -ENOMEM) {
  6572. mutex_unlock(&tracing_err_log_lock);
  6573. return;
  6574. }
  6575. snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
  6576. snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd);
  6577. err->info.errs = errs;
  6578. err->info.type = type;
  6579. err->info.pos = pos;
  6580. err->info.ts = local_clock();
  6581. list_add_tail(&err->list, &tr->err_log);
  6582. mutex_unlock(&tracing_err_log_lock);
  6583. }
  6584. static void clear_tracing_err_log(struct trace_array *tr)
  6585. {
  6586. struct tracing_log_err *err, *next;
  6587. mutex_lock(&tracing_err_log_lock);
  6588. list_for_each_entry_safe(err, next, &tr->err_log, list) {
  6589. list_del(&err->list);
  6590. free_tracing_log_err(err);
  6591. }
  6592. tr->n_err_log_entries = 0;
  6593. mutex_unlock(&tracing_err_log_lock);
  6594. }
  6595. static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
  6596. {
  6597. struct trace_array *tr = m->private;
  6598. mutex_lock(&tracing_err_log_lock);
  6599. return seq_list_start(&tr->err_log, *pos);
  6600. }
  6601. static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
  6602. {
  6603. struct trace_array *tr = m->private;
  6604. return seq_list_next(v, &tr->err_log, pos);
  6605. }
  6606. static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
  6607. {
  6608. mutex_unlock(&tracing_err_log_lock);
  6609. }
  6610. static void tracing_err_log_show_pos(struct seq_file *m, u16 pos)
  6611. {
  6612. u16 i;
  6613. for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
  6614. seq_putc(m, ' ');
  6615. for (i = 0; i < pos; i++)
  6616. seq_putc(m, ' ');
  6617. seq_puts(m, "^\n");
  6618. }
  6619. static int tracing_err_log_seq_show(struct seq_file *m, void *v)
  6620. {
  6621. struct tracing_log_err *err = v;
  6622. if (err) {
  6623. const char *err_text = err->info.errs[err->info.type];
  6624. u64 sec = err->info.ts;
  6625. u32 nsec;
  6626. nsec = do_div(sec, NSEC_PER_SEC);
  6627. seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
  6628. err->loc, err_text);
  6629. seq_printf(m, "%s", err->cmd);
  6630. tracing_err_log_show_pos(m, err->info.pos);
  6631. }
  6632. return 0;
  6633. }
  6634. static const struct seq_operations tracing_err_log_seq_ops = {
  6635. .start = tracing_err_log_seq_start,
  6636. .next = tracing_err_log_seq_next,
  6637. .stop = tracing_err_log_seq_stop,
  6638. .show = tracing_err_log_seq_show
  6639. };
  6640. static int tracing_err_log_open(struct inode *inode, struct file *file)
  6641. {
  6642. struct trace_array *tr = inode->i_private;
  6643. int ret = 0;
  6644. ret = tracing_check_open_get_tr(tr);
  6645. if (ret)
  6646. return ret;
  6647. /* If this file was opened for write, then erase contents */
  6648. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
  6649. clear_tracing_err_log(tr);
  6650. if (file->f_mode & FMODE_READ) {
  6651. ret = seq_open(file, &tracing_err_log_seq_ops);
  6652. if (!ret) {
  6653. struct seq_file *m = file->private_data;
  6654. m->private = tr;
  6655. } else {
  6656. trace_array_put(tr);
  6657. }
  6658. }
  6659. return ret;
  6660. }
  6661. static ssize_t tracing_err_log_write(struct file *file,
  6662. const char __user *buffer,
  6663. size_t count, loff_t *ppos)
  6664. {
  6665. return count;
  6666. }
  6667. static int tracing_err_log_release(struct inode *inode, struct file *file)
  6668. {
  6669. struct trace_array *tr = inode->i_private;
  6670. trace_array_put(tr);
  6671. if (file->f_mode & FMODE_READ)
  6672. seq_release(inode, file);
  6673. return 0;
  6674. }
  6675. static const struct file_operations tracing_err_log_fops = {
  6676. .open = tracing_err_log_open,
  6677. .write = tracing_err_log_write,
  6678. .read = seq_read,
  6679. .llseek = tracing_lseek,
  6680. .release = tracing_err_log_release,
  6681. };
  6682. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  6683. {
  6684. struct trace_array *tr = inode->i_private;
  6685. struct ftrace_buffer_info *info;
  6686. int ret;
  6687. ret = tracing_check_open_get_tr(tr);
  6688. if (ret)
  6689. return ret;
  6690. info = kvzalloc(sizeof(*info), GFP_KERNEL);
  6691. if (!info) {
  6692. trace_array_put(tr);
  6693. return -ENOMEM;
  6694. }
  6695. mutex_lock(&trace_types_lock);
  6696. info->iter.tr = tr;
  6697. info->iter.cpu_file = tracing_get_cpu(inode);
  6698. info->iter.trace = tr->current_trace;
  6699. info->iter.array_buffer = &tr->array_buffer;
  6700. info->spare = NULL;
  6701. /* Force reading ring buffer for first read */
  6702. info->read = (unsigned int)-1;
  6703. filp->private_data = info;
  6704. tr->trace_ref++;
  6705. mutex_unlock(&trace_types_lock);
  6706. ret = nonseekable_open(inode, filp);
  6707. if (ret < 0)
  6708. trace_array_put(tr);
  6709. return ret;
  6710. }
  6711. static __poll_t
  6712. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  6713. {
  6714. struct ftrace_buffer_info *info = filp->private_data;
  6715. struct trace_iterator *iter = &info->iter;
  6716. return trace_poll(iter, filp, poll_table);
  6717. }
  6718. static ssize_t
  6719. tracing_buffers_read(struct file *filp, char __user *ubuf,
  6720. size_t count, loff_t *ppos)
  6721. {
  6722. struct ftrace_buffer_info *info = filp->private_data;
  6723. struct trace_iterator *iter = &info->iter;
  6724. ssize_t ret = 0;
  6725. ssize_t size;
  6726. if (!count)
  6727. return 0;
  6728. #ifdef CONFIG_TRACER_MAX_TRACE
  6729. if (iter->snapshot && iter->tr->current_trace->use_max_tr)
  6730. return -EBUSY;
  6731. #endif
  6732. if (!info->spare) {
  6733. info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
  6734. iter->cpu_file);
  6735. if (IS_ERR(info->spare)) {
  6736. ret = PTR_ERR(info->spare);
  6737. info->spare = NULL;
  6738. } else {
  6739. info->spare_cpu = iter->cpu_file;
  6740. }
  6741. }
  6742. if (!info->spare)
  6743. return ret;
  6744. /* Do we have previous read data to read? */
  6745. if (info->read < PAGE_SIZE)
  6746. goto read;
  6747. again:
  6748. trace_access_lock(iter->cpu_file);
  6749. ret = ring_buffer_read_page(iter->array_buffer->buffer,
  6750. &info->spare,
  6751. count,
  6752. iter->cpu_file, 0);
  6753. trace_access_unlock(iter->cpu_file);
  6754. if (ret < 0) {
  6755. if (trace_empty(iter)) {
  6756. if ((filp->f_flags & O_NONBLOCK))
  6757. return -EAGAIN;
  6758. ret = wait_on_pipe(iter, 0);
  6759. if (ret)
  6760. return ret;
  6761. goto again;
  6762. }
  6763. return 0;
  6764. }
  6765. info->read = 0;
  6766. read:
  6767. size = PAGE_SIZE - info->read;
  6768. if (size > count)
  6769. size = count;
  6770. ret = copy_to_user(ubuf, info->spare + info->read, size);
  6771. if (ret == size)
  6772. return -EFAULT;
  6773. size -= ret;
  6774. *ppos += size;
  6775. info->read += size;
  6776. return size;
  6777. }
  6778. static int tracing_buffers_release(struct inode *inode, struct file *file)
  6779. {
  6780. struct ftrace_buffer_info *info = file->private_data;
  6781. struct trace_iterator *iter = &info->iter;
  6782. mutex_lock(&trace_types_lock);
  6783. iter->tr->trace_ref--;
  6784. __trace_array_put(iter->tr);
  6785. iter->wait_index++;
  6786. /* Make sure the waiters see the new wait_index */
  6787. smp_wmb();
  6788. ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
  6789. if (info->spare)
  6790. ring_buffer_free_read_page(iter->array_buffer->buffer,
  6791. info->spare_cpu, info->spare);
  6792. kvfree(info);
  6793. mutex_unlock(&trace_types_lock);
  6794. return 0;
  6795. }
  6796. struct buffer_ref {
  6797. struct trace_buffer *buffer;
  6798. void *page;
  6799. int cpu;
  6800. refcount_t refcount;
  6801. };
  6802. static void buffer_ref_release(struct buffer_ref *ref)
  6803. {
  6804. if (!refcount_dec_and_test(&ref->refcount))
  6805. return;
  6806. ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
  6807. kfree(ref);
  6808. }
  6809. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  6810. struct pipe_buffer *buf)
  6811. {
  6812. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  6813. buffer_ref_release(ref);
  6814. buf->private = 0;
  6815. }
  6816. static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  6817. struct pipe_buffer *buf)
  6818. {
  6819. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  6820. if (refcount_read(&ref->refcount) > INT_MAX/2)
  6821. return false;
  6822. refcount_inc(&ref->refcount);
  6823. return true;
  6824. }
  6825. /* Pipe buffer operations for a buffer. */
  6826. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  6827. .release = buffer_pipe_buf_release,
  6828. .get = buffer_pipe_buf_get,
  6829. };
  6830. /*
  6831. * Callback from splice_to_pipe(), if we need to release some pages
  6832. * at the end of the spd in case we error'ed out in filling the pipe.
  6833. */
  6834. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  6835. {
  6836. struct buffer_ref *ref =
  6837. (struct buffer_ref *)spd->partial[i].private;
  6838. buffer_ref_release(ref);
  6839. spd->partial[i].private = 0;
  6840. }
  6841. static ssize_t
  6842. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  6843. struct pipe_inode_info *pipe, size_t len,
  6844. unsigned int flags)
  6845. {
  6846. struct ftrace_buffer_info *info = file->private_data;
  6847. struct trace_iterator *iter = &info->iter;
  6848. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  6849. struct page *pages_def[PIPE_DEF_BUFFERS];
  6850. struct splice_pipe_desc spd = {
  6851. .pages = pages_def,
  6852. .partial = partial_def,
  6853. .nr_pages_max = PIPE_DEF_BUFFERS,
  6854. .ops = &buffer_pipe_buf_ops,
  6855. .spd_release = buffer_spd_release,
  6856. };
  6857. struct buffer_ref *ref;
  6858. int entries, i;
  6859. ssize_t ret = 0;
  6860. #ifdef CONFIG_TRACER_MAX_TRACE
  6861. if (iter->snapshot && iter->tr->current_trace->use_max_tr)
  6862. return -EBUSY;
  6863. #endif
  6864. if (*ppos & (PAGE_SIZE - 1))
  6865. return -EINVAL;
  6866. if (len & (PAGE_SIZE - 1)) {
  6867. if (len < PAGE_SIZE)
  6868. return -EINVAL;
  6869. len &= PAGE_MASK;
  6870. }
  6871. if (splice_grow_spd(pipe, &spd))
  6872. return -ENOMEM;
  6873. again:
  6874. trace_access_lock(iter->cpu_file);
  6875. entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
  6876. for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= PAGE_SIZE) {
  6877. struct page *page;
  6878. int r;
  6879. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  6880. if (!ref) {
  6881. ret = -ENOMEM;
  6882. break;
  6883. }
  6884. refcount_set(&ref->refcount, 1);
  6885. ref->buffer = iter->array_buffer->buffer;
  6886. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  6887. if (IS_ERR(ref->page)) {
  6888. ret = PTR_ERR(ref->page);
  6889. ref->page = NULL;
  6890. kfree(ref);
  6891. break;
  6892. }
  6893. ref->cpu = iter->cpu_file;
  6894. r = ring_buffer_read_page(ref->buffer, &ref->page,
  6895. len, iter->cpu_file, 1);
  6896. if (r < 0) {
  6897. ring_buffer_free_read_page(ref->buffer, ref->cpu,
  6898. ref->page);
  6899. kfree(ref);
  6900. break;
  6901. }
  6902. page = virt_to_page(ref->page);
  6903. spd.pages[i] = page;
  6904. spd.partial[i].len = PAGE_SIZE;
  6905. spd.partial[i].offset = 0;
  6906. spd.partial[i].private = (unsigned long)ref;
  6907. spd.nr_pages++;
  6908. *ppos += PAGE_SIZE;
  6909. entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
  6910. }
  6911. trace_access_unlock(iter->cpu_file);
  6912. spd.nr_pages = i;
  6913. /* did we read anything? */
  6914. if (!spd.nr_pages) {
  6915. long wait_index;
  6916. if (ret)
  6917. goto out;
  6918. ret = -EAGAIN;
  6919. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
  6920. goto out;
  6921. wait_index = READ_ONCE(iter->wait_index);
  6922. ret = wait_on_pipe(iter, iter->tr->buffer_percent);
  6923. if (ret)
  6924. goto out;
  6925. /* No need to wait after waking up when tracing is off */
  6926. if (!tracer_tracing_is_on(iter->tr))
  6927. goto out;
  6928. /* Make sure we see the new wait_index */
  6929. smp_rmb();
  6930. if (wait_index != iter->wait_index)
  6931. goto out;
  6932. goto again;
  6933. }
  6934. ret = splice_to_pipe(pipe, &spd);
  6935. out:
  6936. splice_shrink_spd(&spd);
  6937. return ret;
  6938. }
  6939. /* An ioctl call with cmd 0 to the ring buffer file will wake up all waiters */
  6940. static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  6941. {
  6942. struct ftrace_buffer_info *info = file->private_data;
  6943. struct trace_iterator *iter = &info->iter;
  6944. if (cmd)
  6945. return -ENOIOCTLCMD;
  6946. mutex_lock(&trace_types_lock);
  6947. iter->wait_index++;
  6948. /* Make sure the waiters see the new wait_index */
  6949. smp_wmb();
  6950. ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
  6951. mutex_unlock(&trace_types_lock);
  6952. return 0;
  6953. }
  6954. static const struct file_operations tracing_buffers_fops = {
  6955. .open = tracing_buffers_open,
  6956. .read = tracing_buffers_read,
  6957. .poll = tracing_buffers_poll,
  6958. .release = tracing_buffers_release,
  6959. .splice_read = tracing_buffers_splice_read,
  6960. .unlocked_ioctl = tracing_buffers_ioctl,
  6961. .llseek = no_llseek,
  6962. };
  6963. static ssize_t
  6964. tracing_stats_read(struct file *filp, char __user *ubuf,
  6965. size_t count, loff_t *ppos)
  6966. {
  6967. struct inode *inode = file_inode(filp);
  6968. struct trace_array *tr = inode->i_private;
  6969. struct array_buffer *trace_buf = &tr->array_buffer;
  6970. int cpu = tracing_get_cpu(inode);
  6971. struct trace_seq *s;
  6972. unsigned long cnt;
  6973. unsigned long long t;
  6974. unsigned long usec_rem;
  6975. s = kmalloc(sizeof(*s), GFP_KERNEL);
  6976. if (!s)
  6977. return -ENOMEM;
  6978. trace_seq_init(s);
  6979. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  6980. trace_seq_printf(s, "entries: %ld\n", cnt);
  6981. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  6982. trace_seq_printf(s, "overrun: %ld\n", cnt);
  6983. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  6984. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  6985. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  6986. trace_seq_printf(s, "bytes: %ld\n", cnt);
  6987. if (trace_clocks[tr->clock_id].in_ns) {
  6988. /* local or global for trace_clock */
  6989. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  6990. usec_rem = do_div(t, USEC_PER_SEC);
  6991. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  6992. t, usec_rem);
  6993. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer));
  6994. usec_rem = do_div(t, USEC_PER_SEC);
  6995. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  6996. } else {
  6997. /* counter or tsc mode for trace_clock */
  6998. trace_seq_printf(s, "oldest event ts: %llu\n",
  6999. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  7000. trace_seq_printf(s, "now ts: %llu\n",
  7001. ring_buffer_time_stamp(trace_buf->buffer));
  7002. }
  7003. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  7004. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  7005. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  7006. trace_seq_printf(s, "read events: %ld\n", cnt);
  7007. count = simple_read_from_buffer(ubuf, count, ppos,
  7008. s->buffer, trace_seq_used(s));
  7009. kfree(s);
  7010. return count;
  7011. }
  7012. static const struct file_operations tracing_stats_fops = {
  7013. .open = tracing_open_generic_tr,
  7014. .read = tracing_stats_read,
  7015. .llseek = generic_file_llseek,
  7016. .release = tracing_release_generic_tr,
  7017. };
  7018. #ifdef CONFIG_DYNAMIC_FTRACE
  7019. static ssize_t
  7020. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  7021. size_t cnt, loff_t *ppos)
  7022. {
  7023. ssize_t ret;
  7024. char *buf;
  7025. int r;
  7026. /* 256 should be plenty to hold the amount needed */
  7027. buf = kmalloc(256, GFP_KERNEL);
  7028. if (!buf)
  7029. return -ENOMEM;
  7030. r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
  7031. ftrace_update_tot_cnt,
  7032. ftrace_number_of_pages,
  7033. ftrace_number_of_groups);
  7034. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7035. kfree(buf);
  7036. return ret;
  7037. }
  7038. static const struct file_operations tracing_dyn_info_fops = {
  7039. .open = tracing_open_generic,
  7040. .read = tracing_read_dyn_info,
  7041. .llseek = generic_file_llseek,
  7042. };
  7043. #endif /* CONFIG_DYNAMIC_FTRACE */
  7044. #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
  7045. static void
  7046. ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
  7047. struct trace_array *tr, struct ftrace_probe_ops *ops,
  7048. void *data)
  7049. {
  7050. tracing_snapshot_instance(tr);
  7051. }
  7052. static void
  7053. ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
  7054. struct trace_array *tr, struct ftrace_probe_ops *ops,
  7055. void *data)
  7056. {
  7057. struct ftrace_func_mapper *mapper = data;
  7058. long *count = NULL;
  7059. if (mapper)
  7060. count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
  7061. if (count) {
  7062. if (*count <= 0)
  7063. return;
  7064. (*count)--;
  7065. }
  7066. tracing_snapshot_instance(tr);
  7067. }
  7068. static int
  7069. ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
  7070. struct ftrace_probe_ops *ops, void *data)
  7071. {
  7072. struct ftrace_func_mapper *mapper = data;
  7073. long *count = NULL;
  7074. seq_printf(m, "%ps:", (void *)ip);
  7075. seq_puts(m, "snapshot");
  7076. if (mapper)
  7077. count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
  7078. if (count)
  7079. seq_printf(m, ":count=%ld\n", *count);
  7080. else
  7081. seq_puts(m, ":unlimited\n");
  7082. return 0;
  7083. }
  7084. static int
  7085. ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
  7086. unsigned long ip, void *init_data, void **data)
  7087. {
  7088. struct ftrace_func_mapper *mapper = *data;
  7089. if (!mapper) {
  7090. mapper = allocate_ftrace_func_mapper();
  7091. if (!mapper)
  7092. return -ENOMEM;
  7093. *data = mapper;
  7094. }
  7095. return ftrace_func_mapper_add_ip(mapper, ip, init_data);
  7096. }
  7097. static void
  7098. ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
  7099. unsigned long ip, void *data)
  7100. {
  7101. struct ftrace_func_mapper *mapper = data;
  7102. if (!ip) {
  7103. if (!mapper)
  7104. return;
  7105. free_ftrace_func_mapper(mapper, NULL);
  7106. return;
  7107. }
  7108. ftrace_func_mapper_remove_ip(mapper, ip);
  7109. }
  7110. static struct ftrace_probe_ops snapshot_probe_ops = {
  7111. .func = ftrace_snapshot,
  7112. .print = ftrace_snapshot_print,
  7113. };
  7114. static struct ftrace_probe_ops snapshot_count_probe_ops = {
  7115. .func = ftrace_count_snapshot,
  7116. .print = ftrace_snapshot_print,
  7117. .init = ftrace_snapshot_init,
  7118. .free = ftrace_snapshot_free,
  7119. };
  7120. static int
  7121. ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
  7122. char *glob, char *cmd, char *param, int enable)
  7123. {
  7124. struct ftrace_probe_ops *ops;
  7125. void *count = (void *)-1;
  7126. char *number;
  7127. int ret;
  7128. if (!tr)
  7129. return -ENODEV;
  7130. /* hash funcs only work with set_ftrace_filter */
  7131. if (!enable)
  7132. return -EINVAL;
  7133. ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
  7134. if (glob[0] == '!')
  7135. return unregister_ftrace_function_probe_func(glob+1, tr, ops);
  7136. if (!param)
  7137. goto out_reg;
  7138. number = strsep(&param, ":");
  7139. if (!strlen(number))
  7140. goto out_reg;
  7141. /*
  7142. * We use the callback data field (which is a pointer)
  7143. * as our counter.
  7144. */
  7145. ret = kstrtoul(number, 0, (unsigned long *)&count);
  7146. if (ret)
  7147. return ret;
  7148. out_reg:
  7149. ret = tracing_alloc_snapshot_instance(tr);
  7150. if (ret < 0)
  7151. goto out;
  7152. ret = register_ftrace_function_probe(glob, tr, ops, count);
  7153. out:
  7154. return ret < 0 ? ret : 0;
  7155. }
  7156. static struct ftrace_func_command ftrace_snapshot_cmd = {
  7157. .name = "snapshot",
  7158. .func = ftrace_trace_snapshot_callback,
  7159. };
  7160. static __init int register_snapshot_cmd(void)
  7161. {
  7162. return register_ftrace_command(&ftrace_snapshot_cmd);
  7163. }
  7164. #else
  7165. static inline __init int register_snapshot_cmd(void) { return 0; }
  7166. #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
  7167. static struct dentry *tracing_get_dentry(struct trace_array *tr)
  7168. {
  7169. if (WARN_ON(!tr->dir))
  7170. return ERR_PTR(-ENODEV);
  7171. /* Top directory uses NULL as the parent */
  7172. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  7173. return NULL;
  7174. /* All sub buffers have a descriptor */
  7175. return tr->dir;
  7176. }
  7177. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  7178. {
  7179. struct dentry *d_tracer;
  7180. if (tr->percpu_dir)
  7181. return tr->percpu_dir;
  7182. d_tracer = tracing_get_dentry(tr);
  7183. if (IS_ERR(d_tracer))
  7184. return NULL;
  7185. tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
  7186. MEM_FAIL(!tr->percpu_dir,
  7187. "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
  7188. return tr->percpu_dir;
  7189. }
  7190. static struct dentry *
  7191. trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
  7192. void *data, long cpu, const struct file_operations *fops)
  7193. {
  7194. struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
  7195. if (ret) /* See tracing_get_cpu() */
  7196. d_inode(ret)->i_cdev = (void *)(cpu + 1);
  7197. return ret;
  7198. }
  7199. static void
  7200. tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
  7201. {
  7202. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  7203. struct dentry *d_cpu;
  7204. char cpu_dir[30]; /* 30 characters should be more than enough */
  7205. if (!d_percpu)
  7206. return;
  7207. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  7208. d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
  7209. if (!d_cpu) {
  7210. pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
  7211. return;
  7212. }
  7213. /* per cpu trace_pipe */
  7214. trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu,
  7215. tr, cpu, &tracing_pipe_fops);
  7216. /* per cpu trace */
  7217. trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu,
  7218. tr, cpu, &tracing_fops);
  7219. trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu,
  7220. tr, cpu, &tracing_buffers_fops);
  7221. trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu,
  7222. tr, cpu, &tracing_stats_fops);
  7223. trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu,
  7224. tr, cpu, &tracing_entries_fops);
  7225. #ifdef CONFIG_TRACER_SNAPSHOT
  7226. trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
  7227. tr, cpu, &snapshot_fops);
  7228. trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
  7229. tr, cpu, &snapshot_raw_fops);
  7230. #endif
  7231. }
  7232. #ifdef CONFIG_FTRACE_SELFTEST
  7233. /* Let selftest have access to static functions in this file */
  7234. #include "trace_selftest.c"
  7235. #endif
  7236. static ssize_t
  7237. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  7238. loff_t *ppos)
  7239. {
  7240. struct trace_option_dentry *topt = filp->private_data;
  7241. char *buf;
  7242. if (topt->flags->val & topt->opt->bit)
  7243. buf = "1\n";
  7244. else
  7245. buf = "0\n";
  7246. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  7247. }
  7248. static ssize_t
  7249. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  7250. loff_t *ppos)
  7251. {
  7252. struct trace_option_dentry *topt = filp->private_data;
  7253. unsigned long val;
  7254. int ret;
  7255. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7256. if (ret)
  7257. return ret;
  7258. if (val != 0 && val != 1)
  7259. return -EINVAL;
  7260. if (!!(topt->flags->val & topt->opt->bit) != val) {
  7261. mutex_lock(&trace_types_lock);
  7262. ret = __set_tracer_option(topt->tr, topt->flags,
  7263. topt->opt, !val);
  7264. mutex_unlock(&trace_types_lock);
  7265. if (ret)
  7266. return ret;
  7267. }
  7268. *ppos += cnt;
  7269. return cnt;
  7270. }
  7271. static int tracing_open_options(struct inode *inode, struct file *filp)
  7272. {
  7273. struct trace_option_dentry *topt = inode->i_private;
  7274. int ret;
  7275. ret = tracing_check_open_get_tr(topt->tr);
  7276. if (ret)
  7277. return ret;
  7278. filp->private_data = inode->i_private;
  7279. return 0;
  7280. }
  7281. static int tracing_release_options(struct inode *inode, struct file *file)
  7282. {
  7283. struct trace_option_dentry *topt = file->private_data;
  7284. trace_array_put(topt->tr);
  7285. return 0;
  7286. }
  7287. static const struct file_operations trace_options_fops = {
  7288. .open = tracing_open_options,
  7289. .read = trace_options_read,
  7290. .write = trace_options_write,
  7291. .llseek = generic_file_llseek,
  7292. .release = tracing_release_options,
  7293. };
  7294. /*
  7295. * In order to pass in both the trace_array descriptor as well as the index
  7296. * to the flag that the trace option file represents, the trace_array
  7297. * has a character array of trace_flags_index[], which holds the index
  7298. * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
  7299. * The address of this character array is passed to the flag option file
  7300. * read/write callbacks.
  7301. *
  7302. * In order to extract both the index and the trace_array descriptor,
  7303. * get_tr_index() uses the following algorithm.
  7304. *
  7305. * idx = *ptr;
  7306. *
  7307. * As the pointer itself contains the address of the index (remember
  7308. * index[1] == 1).
  7309. *
  7310. * Then to get the trace_array descriptor, by subtracting that index
  7311. * from the ptr, we get to the start of the index itself.
  7312. *
  7313. * ptr - idx == &index[0]
  7314. *
  7315. * Then a simple container_of() from that pointer gets us to the
  7316. * trace_array descriptor.
  7317. */
  7318. static void get_tr_index(void *data, struct trace_array **ptr,
  7319. unsigned int *pindex)
  7320. {
  7321. *pindex = *(unsigned char *)data;
  7322. *ptr = container_of(data - *pindex, struct trace_array,
  7323. trace_flags_index);
  7324. }
  7325. static ssize_t
  7326. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  7327. loff_t *ppos)
  7328. {
  7329. void *tr_index = filp->private_data;
  7330. struct trace_array *tr;
  7331. unsigned int index;
  7332. char *buf;
  7333. get_tr_index(tr_index, &tr, &index);
  7334. if (tr->trace_flags & (1 << index))
  7335. buf = "1\n";
  7336. else
  7337. buf = "0\n";
  7338. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  7339. }
  7340. static ssize_t
  7341. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  7342. loff_t *ppos)
  7343. {
  7344. void *tr_index = filp->private_data;
  7345. struct trace_array *tr;
  7346. unsigned int index;
  7347. unsigned long val;
  7348. int ret;
  7349. get_tr_index(tr_index, &tr, &index);
  7350. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7351. if (ret)
  7352. return ret;
  7353. if (val != 0 && val != 1)
  7354. return -EINVAL;
  7355. mutex_lock(&event_mutex);
  7356. mutex_lock(&trace_types_lock);
  7357. ret = set_tracer_flag(tr, 1 << index, val);
  7358. mutex_unlock(&trace_types_lock);
  7359. mutex_unlock(&event_mutex);
  7360. if (ret < 0)
  7361. return ret;
  7362. *ppos += cnt;
  7363. return cnt;
  7364. }
  7365. static const struct file_operations trace_options_core_fops = {
  7366. .open = tracing_open_generic,
  7367. .read = trace_options_core_read,
  7368. .write = trace_options_core_write,
  7369. .llseek = generic_file_llseek,
  7370. };
  7371. struct dentry *trace_create_file(const char *name,
  7372. umode_t mode,
  7373. struct dentry *parent,
  7374. void *data,
  7375. const struct file_operations *fops)
  7376. {
  7377. struct dentry *ret;
  7378. ret = tracefs_create_file(name, mode, parent, data, fops);
  7379. if (!ret)
  7380. pr_warn("Could not create tracefs '%s' entry\n", name);
  7381. return ret;
  7382. }
  7383. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  7384. {
  7385. struct dentry *d_tracer;
  7386. if (tr->options)
  7387. return tr->options;
  7388. d_tracer = tracing_get_dentry(tr);
  7389. if (IS_ERR(d_tracer))
  7390. return NULL;
  7391. tr->options = tracefs_create_dir("options", d_tracer);
  7392. if (!tr->options) {
  7393. pr_warn("Could not create tracefs directory 'options'\n");
  7394. return NULL;
  7395. }
  7396. return tr->options;
  7397. }
  7398. static void
  7399. create_trace_option_file(struct trace_array *tr,
  7400. struct trace_option_dentry *topt,
  7401. struct tracer_flags *flags,
  7402. struct tracer_opt *opt)
  7403. {
  7404. struct dentry *t_options;
  7405. t_options = trace_options_init_dentry(tr);
  7406. if (!t_options)
  7407. return;
  7408. topt->flags = flags;
  7409. topt->opt = opt;
  7410. topt->tr = tr;
  7411. topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE,
  7412. t_options, topt, &trace_options_fops);
  7413. }
  7414. static void
  7415. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  7416. {
  7417. struct trace_option_dentry *topts;
  7418. struct trace_options *tr_topts;
  7419. struct tracer_flags *flags;
  7420. struct tracer_opt *opts;
  7421. int cnt;
  7422. int i;
  7423. if (!tracer)
  7424. return;
  7425. flags = tracer->flags;
  7426. if (!flags || !flags->opts)
  7427. return;
  7428. /*
  7429. * If this is an instance, only create flags for tracers
  7430. * the instance may have.
  7431. */
  7432. if (!trace_ok_for_array(tracer, tr))
  7433. return;
  7434. for (i = 0; i < tr->nr_topts; i++) {
  7435. /* Make sure there's no duplicate flags. */
  7436. if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
  7437. return;
  7438. }
  7439. opts = flags->opts;
  7440. for (cnt = 0; opts[cnt].name; cnt++)
  7441. ;
  7442. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  7443. if (!topts)
  7444. return;
  7445. tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
  7446. GFP_KERNEL);
  7447. if (!tr_topts) {
  7448. kfree(topts);
  7449. return;
  7450. }
  7451. tr->topts = tr_topts;
  7452. tr->topts[tr->nr_topts].tracer = tracer;
  7453. tr->topts[tr->nr_topts].topts = topts;
  7454. tr->nr_topts++;
  7455. for (cnt = 0; opts[cnt].name; cnt++) {
  7456. create_trace_option_file(tr, &topts[cnt], flags,
  7457. &opts[cnt]);
  7458. MEM_FAIL(topts[cnt].entry == NULL,
  7459. "Failed to create trace option: %s",
  7460. opts[cnt].name);
  7461. }
  7462. }
  7463. static struct dentry *
  7464. create_trace_option_core_file(struct trace_array *tr,
  7465. const char *option, long index)
  7466. {
  7467. struct dentry *t_options;
  7468. t_options = trace_options_init_dentry(tr);
  7469. if (!t_options)
  7470. return NULL;
  7471. return trace_create_file(option, TRACE_MODE_WRITE, t_options,
  7472. (void *)&tr->trace_flags_index[index],
  7473. &trace_options_core_fops);
  7474. }
  7475. static void create_trace_options_dir(struct trace_array *tr)
  7476. {
  7477. struct dentry *t_options;
  7478. bool top_level = tr == &global_trace;
  7479. int i;
  7480. t_options = trace_options_init_dentry(tr);
  7481. if (!t_options)
  7482. return;
  7483. for (i = 0; trace_options[i]; i++) {
  7484. if (top_level ||
  7485. !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
  7486. create_trace_option_core_file(tr, trace_options[i], i);
  7487. }
  7488. }
  7489. static ssize_t
  7490. rb_simple_read(struct file *filp, char __user *ubuf,
  7491. size_t cnt, loff_t *ppos)
  7492. {
  7493. struct trace_array *tr = filp->private_data;
  7494. char buf[64];
  7495. int r;
  7496. r = tracer_tracing_is_on(tr);
  7497. r = sprintf(buf, "%d\n", r);
  7498. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7499. }
  7500. static ssize_t
  7501. rb_simple_write(struct file *filp, const char __user *ubuf,
  7502. size_t cnt, loff_t *ppos)
  7503. {
  7504. struct trace_array *tr = filp->private_data;
  7505. struct trace_buffer *buffer = tr->array_buffer.buffer;
  7506. unsigned long val;
  7507. int ret;
  7508. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7509. if (ret)
  7510. return ret;
  7511. if (buffer) {
  7512. mutex_lock(&trace_types_lock);
  7513. if (!!val == tracer_tracing_is_on(tr)) {
  7514. val = 0; /* do nothing */
  7515. } else if (val) {
  7516. tracer_tracing_on(tr);
  7517. if (tr->current_trace->start)
  7518. tr->current_trace->start(tr);
  7519. } else {
  7520. tracer_tracing_off(tr);
  7521. if (tr->current_trace->stop)
  7522. tr->current_trace->stop(tr);
  7523. /* Wake up any waiters */
  7524. ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS);
  7525. }
  7526. mutex_unlock(&trace_types_lock);
  7527. }
  7528. (*ppos)++;
  7529. return cnt;
  7530. }
  7531. static const struct file_operations rb_simple_fops = {
  7532. .open = tracing_open_generic_tr,
  7533. .read = rb_simple_read,
  7534. .write = rb_simple_write,
  7535. .release = tracing_release_generic_tr,
  7536. .llseek = default_llseek,
  7537. };
  7538. static ssize_t
  7539. buffer_percent_read(struct file *filp, char __user *ubuf,
  7540. size_t cnt, loff_t *ppos)
  7541. {
  7542. struct trace_array *tr = filp->private_data;
  7543. char buf[64];
  7544. int r;
  7545. r = tr->buffer_percent;
  7546. r = sprintf(buf, "%d\n", r);
  7547. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7548. }
  7549. static ssize_t
  7550. buffer_percent_write(struct file *filp, const char __user *ubuf,
  7551. size_t cnt, loff_t *ppos)
  7552. {
  7553. struct trace_array *tr = filp->private_data;
  7554. unsigned long val;
  7555. int ret;
  7556. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7557. if (ret)
  7558. return ret;
  7559. if (val > 100)
  7560. return -EINVAL;
  7561. tr->buffer_percent = val;
  7562. (*ppos)++;
  7563. return cnt;
  7564. }
  7565. static const struct file_operations buffer_percent_fops = {
  7566. .open = tracing_open_generic_tr,
  7567. .read = buffer_percent_read,
  7568. .write = buffer_percent_write,
  7569. .release = tracing_release_generic_tr,
  7570. .llseek = default_llseek,
  7571. };
  7572. static struct dentry *trace_instance_dir;
  7573. static void
  7574. init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
  7575. static int
  7576. allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
  7577. {
  7578. enum ring_buffer_flags rb_flags;
  7579. rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  7580. buf->tr = tr;
  7581. buf->buffer = ring_buffer_alloc(size, rb_flags);
  7582. if (!buf->buffer)
  7583. return -ENOMEM;
  7584. buf->data = alloc_percpu(struct trace_array_cpu);
  7585. if (!buf->data) {
  7586. ring_buffer_free(buf->buffer);
  7587. buf->buffer = NULL;
  7588. return -ENOMEM;
  7589. }
  7590. /* Allocate the first page for all buffers */
  7591. set_buffer_entries(&tr->array_buffer,
  7592. ring_buffer_size(tr->array_buffer.buffer, 0));
  7593. return 0;
  7594. }
  7595. static void free_trace_buffer(struct array_buffer *buf)
  7596. {
  7597. if (buf->buffer) {
  7598. ring_buffer_free(buf->buffer);
  7599. buf->buffer = NULL;
  7600. free_percpu(buf->data);
  7601. buf->data = NULL;
  7602. }
  7603. }
  7604. static int allocate_trace_buffers(struct trace_array *tr, int size)
  7605. {
  7606. int ret;
  7607. ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
  7608. if (ret)
  7609. return ret;
  7610. #ifdef CONFIG_TRACER_MAX_TRACE
  7611. ret = allocate_trace_buffer(tr, &tr->max_buffer,
  7612. allocate_snapshot ? size : 1);
  7613. if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
  7614. free_trace_buffer(&tr->array_buffer);
  7615. return -ENOMEM;
  7616. }
  7617. tr->allocated_snapshot = allocate_snapshot;
  7618. /*
  7619. * Only the top level trace array gets its snapshot allocated
  7620. * from the kernel command line.
  7621. */
  7622. allocate_snapshot = false;
  7623. #endif
  7624. return 0;
  7625. }
  7626. static void free_trace_buffers(struct trace_array *tr)
  7627. {
  7628. if (!tr)
  7629. return;
  7630. free_trace_buffer(&tr->array_buffer);
  7631. #ifdef CONFIG_TRACER_MAX_TRACE
  7632. free_trace_buffer(&tr->max_buffer);
  7633. #endif
  7634. }
  7635. static void init_trace_flags_index(struct trace_array *tr)
  7636. {
  7637. int i;
  7638. /* Used by the trace options files */
  7639. for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
  7640. tr->trace_flags_index[i] = i;
  7641. }
  7642. static void __update_tracer_options(struct trace_array *tr)
  7643. {
  7644. struct tracer *t;
  7645. for (t = trace_types; t; t = t->next)
  7646. add_tracer_options(tr, t);
  7647. }
  7648. static void update_tracer_options(struct trace_array *tr)
  7649. {
  7650. mutex_lock(&trace_types_lock);
  7651. tracer_options_updated = true;
  7652. __update_tracer_options(tr);
  7653. mutex_unlock(&trace_types_lock);
  7654. }
  7655. /* Must have trace_types_lock held */
  7656. struct trace_array *trace_array_find(const char *instance)
  7657. {
  7658. struct trace_array *tr, *found = NULL;
  7659. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7660. if (tr->name && strcmp(tr->name, instance) == 0) {
  7661. found = tr;
  7662. break;
  7663. }
  7664. }
  7665. return found;
  7666. }
  7667. struct trace_array *trace_array_find_get(const char *instance)
  7668. {
  7669. struct trace_array *tr;
  7670. mutex_lock(&trace_types_lock);
  7671. tr = trace_array_find(instance);
  7672. if (tr)
  7673. tr->ref++;
  7674. mutex_unlock(&trace_types_lock);
  7675. return tr;
  7676. }
  7677. static int trace_array_create_dir(struct trace_array *tr)
  7678. {
  7679. int ret;
  7680. tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
  7681. if (!tr->dir)
  7682. return -EINVAL;
  7683. ret = event_trace_add_tracer(tr->dir, tr);
  7684. if (ret) {
  7685. tracefs_remove(tr->dir);
  7686. return ret;
  7687. }
  7688. init_tracer_tracefs(tr, tr->dir);
  7689. __update_tracer_options(tr);
  7690. return ret;
  7691. }
  7692. static struct trace_array *trace_array_create(const char *name)
  7693. {
  7694. struct trace_array *tr;
  7695. int ret;
  7696. ret = -ENOMEM;
  7697. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  7698. if (!tr)
  7699. return ERR_PTR(ret);
  7700. tr->name = kstrdup(name, GFP_KERNEL);
  7701. if (!tr->name)
  7702. goto out_free_tr;
  7703. if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
  7704. goto out_free_tr;
  7705. tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
  7706. cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
  7707. raw_spin_lock_init(&tr->start_lock);
  7708. tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  7709. tr->current_trace = &nop_trace;
  7710. INIT_LIST_HEAD(&tr->systems);
  7711. INIT_LIST_HEAD(&tr->events);
  7712. INIT_LIST_HEAD(&tr->hist_vars);
  7713. INIT_LIST_HEAD(&tr->err_log);
  7714. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  7715. goto out_free_tr;
  7716. if (ftrace_allocate_ftrace_ops(tr) < 0)
  7717. goto out_free_tr;
  7718. ftrace_init_trace_array(tr);
  7719. init_trace_flags_index(tr);
  7720. if (trace_instance_dir) {
  7721. ret = trace_array_create_dir(tr);
  7722. if (ret)
  7723. goto out_free_tr;
  7724. } else
  7725. __trace_early_add_events(tr);
  7726. list_add(&tr->list, &ftrace_trace_arrays);
  7727. tr->ref++;
  7728. return tr;
  7729. out_free_tr:
  7730. ftrace_free_ftrace_ops(tr);
  7731. free_trace_buffers(tr);
  7732. free_cpumask_var(tr->tracing_cpumask);
  7733. kfree(tr->name);
  7734. kfree(tr);
  7735. return ERR_PTR(ret);
  7736. }
  7737. static int instance_mkdir(const char *name)
  7738. {
  7739. struct trace_array *tr;
  7740. int ret;
  7741. mutex_lock(&event_mutex);
  7742. mutex_lock(&trace_types_lock);
  7743. ret = -EEXIST;
  7744. if (trace_array_find(name))
  7745. goto out_unlock;
  7746. tr = trace_array_create(name);
  7747. ret = PTR_ERR_OR_ZERO(tr);
  7748. out_unlock:
  7749. mutex_unlock(&trace_types_lock);
  7750. mutex_unlock(&event_mutex);
  7751. return ret;
  7752. }
  7753. /**
  7754. * trace_array_get_by_name - Create/Lookup a trace array, given its name.
  7755. * @name: The name of the trace array to be looked up/created.
  7756. *
  7757. * Returns pointer to trace array with given name.
  7758. * NULL, if it cannot be created.
  7759. *
  7760. * NOTE: This function increments the reference counter associated with the
  7761. * trace array returned. This makes sure it cannot be freed while in use.
  7762. * Use trace_array_put() once the trace array is no longer needed.
  7763. * If the trace_array is to be freed, trace_array_destroy() needs to
  7764. * be called after the trace_array_put(), or simply let user space delete
  7765. * it from the tracefs instances directory. But until the
  7766. * trace_array_put() is called, user space can not delete it.
  7767. *
  7768. */
  7769. struct trace_array *trace_array_get_by_name(const char *name)
  7770. {
  7771. struct trace_array *tr;
  7772. mutex_lock(&event_mutex);
  7773. mutex_lock(&trace_types_lock);
  7774. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7775. if (tr->name && strcmp(tr->name, name) == 0)
  7776. goto out_unlock;
  7777. }
  7778. tr = trace_array_create(name);
  7779. if (IS_ERR(tr))
  7780. tr = NULL;
  7781. out_unlock:
  7782. if (tr)
  7783. tr->ref++;
  7784. mutex_unlock(&trace_types_lock);
  7785. mutex_unlock(&event_mutex);
  7786. return tr;
  7787. }
  7788. EXPORT_SYMBOL_GPL(trace_array_get_by_name);
  7789. static int __remove_instance(struct trace_array *tr)
  7790. {
  7791. int i;
  7792. /* Reference counter for a newly created trace array = 1. */
  7793. if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
  7794. return -EBUSY;
  7795. list_del(&tr->list);
  7796. /* Disable all the flags that were enabled coming in */
  7797. for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
  7798. if ((1 << i) & ZEROED_TRACE_FLAGS)
  7799. set_tracer_flag(tr, 1 << i, 0);
  7800. }
  7801. tracing_set_nop(tr);
  7802. clear_ftrace_function_probes(tr);
  7803. event_trace_del_tracer(tr);
  7804. ftrace_clear_pids(tr);
  7805. ftrace_destroy_function_files(tr);
  7806. tracefs_remove(tr->dir);
  7807. free_percpu(tr->last_func_repeats);
  7808. free_trace_buffers(tr);
  7809. clear_tracing_err_log(tr);
  7810. for (i = 0; i < tr->nr_topts; i++) {
  7811. kfree(tr->topts[i].topts);
  7812. }
  7813. kfree(tr->topts);
  7814. free_cpumask_var(tr->tracing_cpumask);
  7815. kfree(tr->name);
  7816. kfree(tr);
  7817. return 0;
  7818. }
  7819. int trace_array_destroy(struct trace_array *this_tr)
  7820. {
  7821. struct trace_array *tr;
  7822. int ret;
  7823. if (!this_tr)
  7824. return -EINVAL;
  7825. mutex_lock(&event_mutex);
  7826. mutex_lock(&trace_types_lock);
  7827. ret = -ENODEV;
  7828. /* Making sure trace array exists before destroying it. */
  7829. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7830. if (tr == this_tr) {
  7831. ret = __remove_instance(tr);
  7832. break;
  7833. }
  7834. }
  7835. mutex_unlock(&trace_types_lock);
  7836. mutex_unlock(&event_mutex);
  7837. return ret;
  7838. }
  7839. EXPORT_SYMBOL_GPL(trace_array_destroy);
  7840. static int instance_rmdir(const char *name)
  7841. {
  7842. struct trace_array *tr;
  7843. int ret;
  7844. mutex_lock(&event_mutex);
  7845. mutex_lock(&trace_types_lock);
  7846. ret = -ENODEV;
  7847. tr = trace_array_find(name);
  7848. if (tr)
  7849. ret = __remove_instance(tr);
  7850. mutex_unlock(&trace_types_lock);
  7851. mutex_unlock(&event_mutex);
  7852. return ret;
  7853. }
  7854. static __init void create_trace_instances(struct dentry *d_tracer)
  7855. {
  7856. struct trace_array *tr;
  7857. trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
  7858. instance_mkdir,
  7859. instance_rmdir);
  7860. if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
  7861. return;
  7862. mutex_lock(&event_mutex);
  7863. mutex_lock(&trace_types_lock);
  7864. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7865. if (!tr->name)
  7866. continue;
  7867. if (MEM_FAIL(trace_array_create_dir(tr) < 0,
  7868. "Failed to create instance directory\n"))
  7869. break;
  7870. }
  7871. mutex_unlock(&trace_types_lock);
  7872. mutex_unlock(&event_mutex);
  7873. }
  7874. static void
  7875. init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
  7876. {
  7877. struct trace_event_file *file;
  7878. int cpu;
  7879. trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer,
  7880. tr, &show_traces_fops);
  7881. trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer,
  7882. tr, &set_tracer_fops);
  7883. trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer,
  7884. tr, &tracing_cpumask_fops);
  7885. trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer,
  7886. tr, &tracing_iter_fops);
  7887. trace_create_file("trace", TRACE_MODE_WRITE, d_tracer,
  7888. tr, &tracing_fops);
  7889. trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer,
  7890. tr, &tracing_pipe_fops);
  7891. trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer,
  7892. tr, &tracing_entries_fops);
  7893. trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer,
  7894. tr, &tracing_total_entries_fops);
  7895. trace_create_file("free_buffer", 0200, d_tracer,
  7896. tr, &tracing_free_buffer_fops);
  7897. trace_create_file("trace_marker", 0220, d_tracer,
  7898. tr, &tracing_mark_fops);
  7899. file = __find_event_file(tr, "ftrace", "print");
  7900. if (file && file->dir)
  7901. trace_create_file("trigger", TRACE_MODE_WRITE, file->dir,
  7902. file, &event_trigger_fops);
  7903. tr->trace_marker_file = file;
  7904. trace_create_file("trace_marker_raw", 0220, d_tracer,
  7905. tr, &tracing_mark_raw_fops);
  7906. trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr,
  7907. &trace_clock_fops);
  7908. trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer,
  7909. tr, &rb_simple_fops);
  7910. trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr,
  7911. &trace_time_stamp_mode_fops);
  7912. tr->buffer_percent = 50;
  7913. trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
  7914. tr, &buffer_percent_fops);
  7915. create_trace_options_dir(tr);
  7916. #ifdef CONFIG_TRACER_MAX_TRACE
  7917. trace_create_maxlat_file(tr, d_tracer);
  7918. #endif
  7919. if (ftrace_create_function_files(tr, d_tracer))
  7920. MEM_FAIL(1, "Could not allocate function filter files");
  7921. #ifdef CONFIG_TRACER_SNAPSHOT
  7922. trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
  7923. tr, &snapshot_fops);
  7924. #endif
  7925. trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,
  7926. tr, &tracing_err_log_fops);
  7927. for_each_tracing_cpu(cpu)
  7928. tracing_init_tracefs_percpu(tr, cpu);
  7929. ftrace_init_tracefs(tr, d_tracer);
  7930. }
  7931. static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
  7932. {
  7933. struct vfsmount *mnt;
  7934. struct file_system_type *type;
  7935. /*
  7936. * To maintain backward compatibility for tools that mount
  7937. * debugfs to get to the tracing facility, tracefs is automatically
  7938. * mounted to the debugfs/tracing directory.
  7939. */
  7940. type = get_fs_type("tracefs");
  7941. if (!type)
  7942. return NULL;
  7943. mnt = vfs_submount(mntpt, type, "tracefs", NULL);
  7944. put_filesystem(type);
  7945. if (IS_ERR(mnt))
  7946. return NULL;
  7947. mntget(mnt);
  7948. return mnt;
  7949. }
  7950. /**
  7951. * tracing_init_dentry - initialize top level trace array
  7952. *
  7953. * This is called when creating files or directories in the tracing
  7954. * directory. It is called via fs_initcall() by any of the boot up code
  7955. * and expects to return the dentry of the top level tracing directory.
  7956. */
  7957. int tracing_init_dentry(void)
  7958. {
  7959. struct trace_array *tr = &global_trace;
  7960. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  7961. pr_warn("Tracing disabled due to lockdown\n");
  7962. return -EPERM;
  7963. }
  7964. /* The top level trace array uses NULL as parent */
  7965. if (tr->dir)
  7966. return 0;
  7967. if (WARN_ON(!tracefs_initialized()))
  7968. return -ENODEV;
  7969. /*
  7970. * As there may still be users that expect the tracing
  7971. * files to exist in debugfs/tracing, we must automount
  7972. * the tracefs file system there, so older tools still
  7973. * work with the newer kernel.
  7974. */
  7975. tr->dir = debugfs_create_automount("tracing", NULL,
  7976. trace_automount, NULL);
  7977. return 0;
  7978. }
  7979. extern struct trace_eval_map *__start_ftrace_eval_maps[];
  7980. extern struct trace_eval_map *__stop_ftrace_eval_maps[];
  7981. static struct workqueue_struct *eval_map_wq __initdata;
  7982. static struct work_struct eval_map_work __initdata;
  7983. static struct work_struct tracerfs_init_work __initdata;
  7984. static void __init eval_map_work_func(struct work_struct *work)
  7985. {
  7986. int len;
  7987. len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
  7988. trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
  7989. }
  7990. static int __init trace_eval_init(void)
  7991. {
  7992. INIT_WORK(&eval_map_work, eval_map_work_func);
  7993. eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0);
  7994. if (!eval_map_wq) {
  7995. pr_err("Unable to allocate eval_map_wq\n");
  7996. /* Do work here */
  7997. eval_map_work_func(&eval_map_work);
  7998. return -ENOMEM;
  7999. }
  8000. queue_work(eval_map_wq, &eval_map_work);
  8001. return 0;
  8002. }
  8003. subsys_initcall(trace_eval_init);
  8004. static int __init trace_eval_sync(void)
  8005. {
  8006. /* Make sure the eval map updates are finished */
  8007. if (eval_map_wq)
  8008. destroy_workqueue(eval_map_wq);
  8009. return 0;
  8010. }
  8011. late_initcall_sync(trace_eval_sync);
  8012. #ifdef CONFIG_MODULES
  8013. static void trace_module_add_evals(struct module *mod)
  8014. {
  8015. if (!mod->num_trace_evals)
  8016. return;
  8017. /*
  8018. * Modules with bad taint do not have events created, do
  8019. * not bother with enums either.
  8020. */
  8021. if (trace_module_has_bad_taint(mod))
  8022. return;
  8023. trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
  8024. }
  8025. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  8026. static void trace_module_remove_evals(struct module *mod)
  8027. {
  8028. union trace_eval_map_item *map;
  8029. union trace_eval_map_item **last = &trace_eval_maps;
  8030. if (!mod->num_trace_evals)
  8031. return;
  8032. mutex_lock(&trace_eval_mutex);
  8033. map = trace_eval_maps;
  8034. while (map) {
  8035. if (map->head.mod == mod)
  8036. break;
  8037. map = trace_eval_jmp_to_tail(map);
  8038. last = &map->tail.next;
  8039. map = map->tail.next;
  8040. }
  8041. if (!map)
  8042. goto out;
  8043. *last = trace_eval_jmp_to_tail(map)->tail.next;
  8044. kfree(map);
  8045. out:
  8046. mutex_unlock(&trace_eval_mutex);
  8047. }
  8048. #else
  8049. static inline void trace_module_remove_evals(struct module *mod) { }
  8050. #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
  8051. static int trace_module_notify(struct notifier_block *self,
  8052. unsigned long val, void *data)
  8053. {
  8054. struct module *mod = data;
  8055. switch (val) {
  8056. case MODULE_STATE_COMING:
  8057. trace_module_add_evals(mod);
  8058. break;
  8059. case MODULE_STATE_GOING:
  8060. trace_module_remove_evals(mod);
  8061. break;
  8062. }
  8063. return NOTIFY_OK;
  8064. }
  8065. static struct notifier_block trace_module_nb = {
  8066. .notifier_call = trace_module_notify,
  8067. .priority = 0,
  8068. };
  8069. #endif /* CONFIG_MODULES */
  8070. static __init void tracer_init_tracefs_work_func(struct work_struct *work)
  8071. {
  8072. event_trace_init();
  8073. init_tracer_tracefs(&global_trace, NULL);
  8074. ftrace_init_tracefs_toplevel(&global_trace, NULL);
  8075. trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL,
  8076. &global_trace, &tracing_thresh_fops);
  8077. trace_create_file("README", TRACE_MODE_READ, NULL,
  8078. NULL, &tracing_readme_fops);
  8079. trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL,
  8080. NULL, &tracing_saved_cmdlines_fops);
  8081. trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL,
  8082. NULL, &tracing_saved_cmdlines_size_fops);
  8083. trace_create_file("saved_tgids", TRACE_MODE_READ, NULL,
  8084. NULL, &tracing_saved_tgids_fops);
  8085. trace_create_eval_file(NULL);
  8086. #ifdef CONFIG_MODULES
  8087. register_module_notifier(&trace_module_nb);
  8088. #endif
  8089. #ifdef CONFIG_DYNAMIC_FTRACE
  8090. trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL,
  8091. NULL, &tracing_dyn_info_fops);
  8092. #endif
  8093. create_trace_instances(NULL);
  8094. update_tracer_options(&global_trace);
  8095. }
  8096. static __init int tracer_init_tracefs(void)
  8097. {
  8098. int ret;
  8099. trace_access_lock_init();
  8100. ret = tracing_init_dentry();
  8101. if (ret)
  8102. return 0;
  8103. if (eval_map_wq) {
  8104. INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func);
  8105. queue_work(eval_map_wq, &tracerfs_init_work);
  8106. } else {
  8107. tracer_init_tracefs_work_func(NULL);
  8108. }
  8109. rv_init_interface();
  8110. return 0;
  8111. }
  8112. fs_initcall(tracer_init_tracefs);
  8113. static int trace_panic_handler(struct notifier_block *this,
  8114. unsigned long event, void *unused)
  8115. {
  8116. bool ftrace_check = false;
  8117. trace_android_vh_ftrace_oops_enter(&ftrace_check);
  8118. if (ftrace_check)
  8119. return NOTIFY_OK;
  8120. if (ftrace_dump_on_oops)
  8121. ftrace_dump(ftrace_dump_on_oops);
  8122. trace_android_vh_ftrace_oops_exit(&ftrace_check);
  8123. return NOTIFY_OK;
  8124. }
  8125. static struct notifier_block trace_panic_notifier = {
  8126. .notifier_call = trace_panic_handler,
  8127. .next = NULL,
  8128. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  8129. };
  8130. static int trace_die_handler(struct notifier_block *self,
  8131. unsigned long val,
  8132. void *data)
  8133. {
  8134. bool ftrace_check = false;
  8135. trace_android_vh_ftrace_oops_enter(&ftrace_check);
  8136. if (ftrace_check)
  8137. return NOTIFY_OK;
  8138. switch (val) {
  8139. case DIE_OOPS:
  8140. if (ftrace_dump_on_oops)
  8141. ftrace_dump(ftrace_dump_on_oops);
  8142. break;
  8143. default:
  8144. break;
  8145. }
  8146. trace_android_vh_ftrace_oops_exit(&ftrace_check);
  8147. return NOTIFY_OK;
  8148. }
  8149. static struct notifier_block trace_die_notifier = {
  8150. .notifier_call = trace_die_handler,
  8151. .priority = 200
  8152. };
  8153. /*
  8154. * printk is set to max of 1024, we really don't need it that big.
  8155. * Nothing should be printing 1000 characters anyway.
  8156. */
  8157. #define TRACE_MAX_PRINT 1000
  8158. /*
  8159. * Define here KERN_TRACE so that we have one place to modify
  8160. * it if we decide to change what log level the ftrace dump
  8161. * should be at.
  8162. */
  8163. #define KERN_TRACE KERN_EMERG
  8164. void
  8165. trace_printk_seq(struct trace_seq *s)
  8166. {
  8167. bool dump_printk = true;
  8168. /* Probably should print a warning here. */
  8169. if (s->seq.len >= TRACE_MAX_PRINT)
  8170. s->seq.len = TRACE_MAX_PRINT;
  8171. /*
  8172. * More paranoid code. Although the buffer size is set to
  8173. * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
  8174. * an extra layer of protection.
  8175. */
  8176. if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
  8177. s->seq.len = s->seq.size - 1;
  8178. /* should be zero ended, but we are paranoid. */
  8179. s->buffer[s->seq.len] = 0;
  8180. trace_android_vh_ftrace_dump_buffer(s, &dump_printk);
  8181. if (dump_printk)
  8182. printk(KERN_TRACE "%s", s->buffer);
  8183. trace_seq_init(s);
  8184. }
  8185. void trace_init_global_iter(struct trace_iterator *iter)
  8186. {
  8187. iter->tr = &global_trace;
  8188. iter->trace = iter->tr->current_trace;
  8189. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  8190. iter->array_buffer = &global_trace.array_buffer;
  8191. if (iter->trace && iter->trace->open)
  8192. iter->trace->open(iter);
  8193. /* Annotate start of buffers if we had overruns */
  8194. if (ring_buffer_overruns(iter->array_buffer->buffer))
  8195. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  8196. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  8197. if (trace_clocks[iter->tr->clock_id].in_ns)
  8198. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  8199. /* Can not use kmalloc for iter.temp and iter.fmt */
  8200. iter->temp = static_temp_buf;
  8201. iter->temp_size = STATIC_TEMP_BUF_SIZE;
  8202. iter->fmt = static_fmt_buf;
  8203. iter->fmt_size = STATIC_FMT_BUF_SIZE;
  8204. }
  8205. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  8206. {
  8207. /* use static because iter can be a bit big for the stack */
  8208. static struct trace_iterator iter;
  8209. static atomic_t dump_running;
  8210. struct trace_array *tr = &global_trace;
  8211. unsigned int old_userobj;
  8212. unsigned long flags;
  8213. int cnt = 0, cpu;
  8214. bool ftrace_check = false;
  8215. unsigned long size;
  8216. /* Only allow one dump user at a time. */
  8217. if (atomic_inc_return(&dump_running) != 1) {
  8218. atomic_dec(&dump_running);
  8219. return;
  8220. }
  8221. /*
  8222. * Always turn off tracing when we dump.
  8223. * We don't need to show trace output of what happens
  8224. * between multiple crashes.
  8225. *
  8226. * If the user does a sysrq-z, then they can re-enable
  8227. * tracing with echo 1 > tracing_on.
  8228. */
  8229. tracing_off();
  8230. local_irq_save(flags);
  8231. /* Simulate the iterator */
  8232. trace_init_global_iter(&iter);
  8233. for_each_tracing_cpu(cpu) {
  8234. atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
  8235. size = ring_buffer_size(iter.array_buffer->buffer, cpu);
  8236. trace_android_vh_ftrace_size_check(size, &ftrace_check);
  8237. }
  8238. old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
  8239. /* don't look at user memory in panic mode */
  8240. tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  8241. if (ftrace_check)
  8242. goto out_enable;
  8243. switch (oops_dump_mode) {
  8244. case DUMP_ALL:
  8245. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  8246. break;
  8247. case DUMP_ORIG:
  8248. iter.cpu_file = raw_smp_processor_id();
  8249. break;
  8250. case DUMP_NONE:
  8251. goto out_enable;
  8252. default:
  8253. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  8254. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  8255. }
  8256. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  8257. /* Did function tracer already get disabled? */
  8258. if (ftrace_is_dead()) {
  8259. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  8260. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  8261. }
  8262. /*
  8263. * We need to stop all tracing on all CPUS to read
  8264. * the next buffer. This is a bit expensive, but is
  8265. * not done often. We fill all what we can read,
  8266. * and then release the locks again.
  8267. */
  8268. while (!trace_empty(&iter)) {
  8269. ftrace_check = true;
  8270. if (!cnt)
  8271. printk(KERN_TRACE "---------------------------------\n");
  8272. cnt++;
  8273. trace_iterator_reset(&iter);
  8274. trace_android_vh_ftrace_format_check(&ftrace_check);
  8275. if (ftrace_check)
  8276. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  8277. if (trace_find_next_entry_inc(&iter) != NULL) {
  8278. int ret;
  8279. ret = print_trace_line(&iter);
  8280. if (ret != TRACE_TYPE_NO_CONSUME)
  8281. trace_consume(&iter);
  8282. }
  8283. touch_nmi_watchdog();
  8284. trace_printk_seq(&iter.seq);
  8285. }
  8286. if (!cnt)
  8287. printk(KERN_TRACE " (ftrace buffer empty)\n");
  8288. else
  8289. printk(KERN_TRACE "---------------------------------\n");
  8290. out_enable:
  8291. tr->trace_flags |= old_userobj;
  8292. for_each_tracing_cpu(cpu) {
  8293. atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
  8294. }
  8295. atomic_dec(&dump_running);
  8296. local_irq_restore(flags);
  8297. }
  8298. EXPORT_SYMBOL_GPL(ftrace_dump);
  8299. #define WRITE_BUFSIZE 4096
  8300. ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
  8301. size_t count, loff_t *ppos,
  8302. int (*createfn)(const char *))
  8303. {
  8304. char *kbuf, *buf, *tmp;
  8305. int ret = 0;
  8306. size_t done = 0;
  8307. size_t size;
  8308. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  8309. if (!kbuf)
  8310. return -ENOMEM;
  8311. while (done < count) {
  8312. size = count - done;
  8313. if (size >= WRITE_BUFSIZE)
  8314. size = WRITE_BUFSIZE - 1;
  8315. if (copy_from_user(kbuf, buffer + done, size)) {
  8316. ret = -EFAULT;
  8317. goto out;
  8318. }
  8319. kbuf[size] = '\0';
  8320. buf = kbuf;
  8321. do {
  8322. tmp = strchr(buf, '\n');
  8323. if (tmp) {
  8324. *tmp = '\0';
  8325. size = tmp - buf + 1;
  8326. } else {
  8327. size = strlen(buf);
  8328. if (done + size < count) {
  8329. if (buf != kbuf)
  8330. break;
  8331. /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
  8332. pr_warn("Line length is too long: Should be less than %d\n",
  8333. WRITE_BUFSIZE - 2);
  8334. ret = -EINVAL;
  8335. goto out;
  8336. }
  8337. }
  8338. done += size;
  8339. /* Remove comments */
  8340. tmp = strchr(buf, '#');
  8341. if (tmp)
  8342. *tmp = '\0';
  8343. ret = createfn(buf);
  8344. if (ret)
  8345. goto out;
  8346. buf += size;
  8347. } while (done < count);
  8348. }
  8349. ret = done;
  8350. out:
  8351. kfree(kbuf);
  8352. return ret;
  8353. }
  8354. __init static int tracer_alloc_buffers(void)
  8355. {
  8356. int ring_buf_size;
  8357. int ret = -ENOMEM;
  8358. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  8359. pr_warn("Tracing disabled due to lockdown\n");
  8360. return -EPERM;
  8361. }
  8362. /*
  8363. * Make sure we don't accidentally add more trace options
  8364. * than we have bits for.
  8365. */
  8366. BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
  8367. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  8368. goto out;
  8369. if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
  8370. goto out_free_buffer_mask;
  8371. /* Only allocate trace_printk buffers if a trace_printk exists */
  8372. if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
  8373. /* Must be called before global_trace.buffer is allocated */
  8374. trace_printk_init_buffers();
  8375. /* To save memory, keep the ring buffer size to its minimum */
  8376. if (ring_buffer_expanded)
  8377. ring_buf_size = trace_buf_size;
  8378. else
  8379. ring_buf_size = 1;
  8380. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  8381. cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
  8382. raw_spin_lock_init(&global_trace.start_lock);
  8383. /*
  8384. * The prepare callbacks allocates some memory for the ring buffer. We
  8385. * don't free the buffer if the CPU goes down. If we were to free
  8386. * the buffer, then the user would lose any trace that was in the
  8387. * buffer. The memory will be removed once the "instance" is removed.
  8388. */
  8389. ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
  8390. "trace/RB:prepare", trace_rb_cpu_prepare,
  8391. NULL);
  8392. if (ret < 0)
  8393. goto out_free_cpumask;
  8394. /* Used for event triggers */
  8395. ret = -ENOMEM;
  8396. temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
  8397. if (!temp_buffer)
  8398. goto out_rm_hp_state;
  8399. if (trace_create_savedcmd() < 0)
  8400. goto out_free_temp_buffer;
  8401. /* TODO: make the number of buffers hot pluggable with CPUS */
  8402. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  8403. MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
  8404. goto out_free_savedcmd;
  8405. }
  8406. if (global_trace.buffer_disabled)
  8407. tracing_off();
  8408. if (trace_boot_clock) {
  8409. ret = tracing_set_clock(&global_trace, trace_boot_clock);
  8410. if (ret < 0)
  8411. pr_warn("Trace clock %s not defined, going back to default\n",
  8412. trace_boot_clock);
  8413. }
  8414. /*
  8415. * register_tracer() might reference current_trace, so it
  8416. * needs to be set before we register anything. This is
  8417. * just a bootstrap of current_trace anyway.
  8418. */
  8419. global_trace.current_trace = &nop_trace;
  8420. global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  8421. ftrace_init_global_array_ops(&global_trace);
  8422. init_trace_flags_index(&global_trace);
  8423. register_tracer(&nop_trace);
  8424. /* Function tracing may start here (via kernel command line) */
  8425. init_function_trace();
  8426. /* All seems OK, enable tracing */
  8427. tracing_disabled = 0;
  8428. atomic_notifier_chain_register(&panic_notifier_list,
  8429. &trace_panic_notifier);
  8430. register_die_notifier(&trace_die_notifier);
  8431. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  8432. INIT_LIST_HEAD(&global_trace.systems);
  8433. INIT_LIST_HEAD(&global_trace.events);
  8434. INIT_LIST_HEAD(&global_trace.hist_vars);
  8435. INIT_LIST_HEAD(&global_trace.err_log);
  8436. list_add(&global_trace.list, &ftrace_trace_arrays);
  8437. apply_trace_boot_options();
  8438. register_snapshot_cmd();
  8439. test_can_verify();
  8440. return 0;
  8441. out_free_savedcmd:
  8442. free_saved_cmdlines_buffer(savedcmd);
  8443. out_free_temp_buffer:
  8444. ring_buffer_free(temp_buffer);
  8445. out_rm_hp_state:
  8446. cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
  8447. out_free_cpumask:
  8448. free_cpumask_var(global_trace.tracing_cpumask);
  8449. out_free_buffer_mask:
  8450. free_cpumask_var(tracing_buffer_mask);
  8451. out:
  8452. return ret;
  8453. }
  8454. void __init ftrace_boot_snapshot(void)
  8455. {
  8456. if (snapshot_at_boot) {
  8457. tracing_snapshot();
  8458. internal_trace_puts("** Boot snapshot taken **\n");
  8459. }
  8460. }
  8461. void __init early_trace_init(void)
  8462. {
  8463. if (tracepoint_printk) {
  8464. tracepoint_print_iter =
  8465. kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
  8466. if (MEM_FAIL(!tracepoint_print_iter,
  8467. "Failed to allocate trace iterator\n"))
  8468. tracepoint_printk = 0;
  8469. else
  8470. static_key_enable(&tracepoint_printk_key.key);
  8471. }
  8472. tracer_alloc_buffers();
  8473. init_events();
  8474. }
  8475. void __init trace_init(void)
  8476. {
  8477. trace_event_init();
  8478. }
  8479. __init static void clear_boot_tracer(void)
  8480. {
  8481. /*
  8482. * The default tracer at boot buffer is an init section.
  8483. * This function is called in lateinit. If we did not
  8484. * find the boot tracer, then clear it out, to prevent
  8485. * later registration from accessing the buffer that is
  8486. * about to be freed.
  8487. */
  8488. if (!default_bootup_tracer)
  8489. return;
  8490. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  8491. default_bootup_tracer);
  8492. default_bootup_tracer = NULL;
  8493. }
  8494. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  8495. __init static void tracing_set_default_clock(void)
  8496. {
  8497. /* sched_clock_stable() is determined in late_initcall */
  8498. if (!trace_boot_clock && !sched_clock_stable()) {
  8499. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  8500. pr_warn("Can not set tracing clock due to lockdown\n");
  8501. return;
  8502. }
  8503. printk(KERN_WARNING
  8504. "Unstable clock detected, switching default tracing clock to \"global\"\n"
  8505. "If you want to keep using the local clock, then add:\n"
  8506. " \"trace_clock=local\"\n"
  8507. "on the kernel command line\n");
  8508. tracing_set_clock(&global_trace, "global");
  8509. }
  8510. }
  8511. #else
  8512. static inline void tracing_set_default_clock(void) { }
  8513. #endif
  8514. __init static int late_trace_init(void)
  8515. {
  8516. if (tracepoint_printk && tracepoint_printk_stop_on_boot) {
  8517. static_key_disable(&tracepoint_printk_key.key);
  8518. tracepoint_printk = 0;
  8519. }
  8520. tracing_set_default_clock();
  8521. clear_boot_tracer();
  8522. return 0;
  8523. }
  8524. late_initcall_sync(late_trace_init);