trace.c 153 KB

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