trace.c 152 KB

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