trace.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475
  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. * The tracer itself will not take this lock, but still we want
  2621. * to provide a consistent cpumask to user-space:
  2622. */
  2623. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2624. /*
  2625. * Temporary storage for the character representation of the
  2626. * CPU bitmask (and one more byte for the newline):
  2627. */
  2628. static char mask_str[NR_CPUS + 1];
  2629. static ssize_t
  2630. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2631. size_t count, loff_t *ppos)
  2632. {
  2633. struct trace_array *tr = file_inode(filp)->i_private;
  2634. int len;
  2635. mutex_lock(&tracing_cpumask_update_lock);
  2636. len = cpumask_scnprintf(mask_str, count, tr->tracing_cpumask);
  2637. if (count - len < 2) {
  2638. count = -EINVAL;
  2639. goto out_err;
  2640. }
  2641. len += sprintf(mask_str + len, "\n");
  2642. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2643. out_err:
  2644. mutex_unlock(&tracing_cpumask_update_lock);
  2645. return count;
  2646. }
  2647. static ssize_t
  2648. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2649. size_t count, loff_t *ppos)
  2650. {
  2651. struct trace_array *tr = file_inode(filp)->i_private;
  2652. cpumask_var_t tracing_cpumask_new;
  2653. int err, cpu;
  2654. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2655. return -ENOMEM;
  2656. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2657. if (err)
  2658. goto err_unlock;
  2659. mutex_lock(&tracing_cpumask_update_lock);
  2660. local_irq_disable();
  2661. arch_spin_lock(&ftrace_max_lock);
  2662. for_each_tracing_cpu(cpu) {
  2663. /*
  2664. * Increase/decrease the disabled counter if we are
  2665. * about to flip a bit in the cpumask:
  2666. */
  2667. if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  2668. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2669. atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2670. ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
  2671. }
  2672. if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  2673. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2674. atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
  2675. ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
  2676. }
  2677. }
  2678. arch_spin_unlock(&ftrace_max_lock);
  2679. local_irq_enable();
  2680. cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
  2681. mutex_unlock(&tracing_cpumask_update_lock);
  2682. free_cpumask_var(tracing_cpumask_new);
  2683. return count;
  2684. err_unlock:
  2685. free_cpumask_var(tracing_cpumask_new);
  2686. return err;
  2687. }
  2688. static const struct file_operations tracing_cpumask_fops = {
  2689. .open = tracing_open_generic_tr,
  2690. .read = tracing_cpumask_read,
  2691. .write = tracing_cpumask_write,
  2692. .release = tracing_release_generic_tr,
  2693. .llseek = generic_file_llseek,
  2694. };
  2695. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2696. {
  2697. struct tracer_opt *trace_opts;
  2698. struct trace_array *tr = m->private;
  2699. u32 tracer_flags;
  2700. int i;
  2701. mutex_lock(&trace_types_lock);
  2702. tracer_flags = tr->current_trace->flags->val;
  2703. trace_opts = tr->current_trace->flags->opts;
  2704. for (i = 0; trace_options[i]; i++) {
  2705. if (trace_flags & (1 << i))
  2706. seq_printf(m, "%s\n", trace_options[i]);
  2707. else
  2708. seq_printf(m, "no%s\n", trace_options[i]);
  2709. }
  2710. for (i = 0; trace_opts[i].name; i++) {
  2711. if (tracer_flags & trace_opts[i].bit)
  2712. seq_printf(m, "%s\n", trace_opts[i].name);
  2713. else
  2714. seq_printf(m, "no%s\n", trace_opts[i].name);
  2715. }
  2716. mutex_unlock(&trace_types_lock);
  2717. return 0;
  2718. }
  2719. static int __set_tracer_option(struct tracer *trace,
  2720. struct tracer_flags *tracer_flags,
  2721. struct tracer_opt *opts, int neg)
  2722. {
  2723. int ret;
  2724. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2725. if (ret)
  2726. return ret;
  2727. if (neg)
  2728. tracer_flags->val &= ~opts->bit;
  2729. else
  2730. tracer_flags->val |= opts->bit;
  2731. return 0;
  2732. }
  2733. /* Try to assign a tracer specific option */
  2734. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2735. {
  2736. struct tracer_flags *tracer_flags = trace->flags;
  2737. struct tracer_opt *opts = NULL;
  2738. int i;
  2739. for (i = 0; tracer_flags->opts[i].name; i++) {
  2740. opts = &tracer_flags->opts[i];
  2741. if (strcmp(cmp, opts->name) == 0)
  2742. return __set_tracer_option(trace, trace->flags,
  2743. opts, neg);
  2744. }
  2745. return -EINVAL;
  2746. }
  2747. /* Some tracers require overwrite to stay enabled */
  2748. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  2749. {
  2750. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  2751. return -1;
  2752. return 0;
  2753. }
  2754. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  2755. {
  2756. /* do nothing if flag is already set */
  2757. if (!!(trace_flags & mask) == !!enabled)
  2758. return 0;
  2759. /* Give the tracer a chance to approve the change */
  2760. if (tr->current_trace->flag_changed)
  2761. if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
  2762. return -EINVAL;
  2763. if (enabled)
  2764. trace_flags |= mask;
  2765. else
  2766. trace_flags &= ~mask;
  2767. if (mask == TRACE_ITER_RECORD_CMD)
  2768. trace_event_enable_cmd_record(enabled);
  2769. if (mask == TRACE_ITER_OVERWRITE) {
  2770. ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
  2771. #ifdef CONFIG_TRACER_MAX_TRACE
  2772. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  2773. #endif
  2774. }
  2775. if (mask == TRACE_ITER_PRINTK)
  2776. trace_printk_start_stop_comm(enabled);
  2777. return 0;
  2778. }
  2779. static int trace_set_options(struct trace_array *tr, char *option)
  2780. {
  2781. char *cmp;
  2782. int neg = 0;
  2783. int ret = -ENODEV;
  2784. int i;
  2785. cmp = strstrip(option);
  2786. if (strncmp(cmp, "no", 2) == 0) {
  2787. neg = 1;
  2788. cmp += 2;
  2789. }
  2790. mutex_lock(&trace_types_lock);
  2791. for (i = 0; trace_options[i]; i++) {
  2792. if (strcmp(cmp, trace_options[i]) == 0) {
  2793. ret = set_tracer_flag(tr, 1 << i, !neg);
  2794. break;
  2795. }
  2796. }
  2797. /* If no option could be set, test the specific tracer options */
  2798. if (!trace_options[i])
  2799. ret = set_tracer_option(tr->current_trace, cmp, neg);
  2800. mutex_unlock(&trace_types_lock);
  2801. return ret;
  2802. }
  2803. static ssize_t
  2804. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2805. size_t cnt, loff_t *ppos)
  2806. {
  2807. struct seq_file *m = filp->private_data;
  2808. struct trace_array *tr = m->private;
  2809. char buf[64];
  2810. int ret;
  2811. if (cnt >= sizeof(buf))
  2812. return -EINVAL;
  2813. if (copy_from_user(&buf, ubuf, cnt))
  2814. return -EFAULT;
  2815. buf[cnt] = 0;
  2816. ret = trace_set_options(tr, buf);
  2817. if (ret < 0)
  2818. return ret;
  2819. *ppos += cnt;
  2820. return cnt;
  2821. }
  2822. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2823. {
  2824. struct trace_array *tr = inode->i_private;
  2825. int ret;
  2826. if (tracing_disabled)
  2827. return -ENODEV;
  2828. if (trace_array_get(tr) < 0)
  2829. return -ENODEV;
  2830. ret = single_open(file, tracing_trace_options_show, inode->i_private);
  2831. if (ret < 0)
  2832. trace_array_put(tr);
  2833. return ret;
  2834. }
  2835. static const struct file_operations tracing_iter_fops = {
  2836. .open = tracing_trace_options_open,
  2837. .read = seq_read,
  2838. .llseek = seq_lseek,
  2839. .release = tracing_single_release_tr,
  2840. .write = tracing_trace_options_write,
  2841. };
  2842. static const char readme_msg[] =
  2843. "tracing mini-HOWTO:\n\n"
  2844. "# echo 0 > tracing_on : quick way to disable tracing\n"
  2845. "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
  2846. " Important files:\n"
  2847. " trace\t\t\t- The static contents of the buffer\n"
  2848. "\t\t\t To clear the buffer write into this file: echo > trace\n"
  2849. " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
  2850. " current_tracer\t- function and latency tracers\n"
  2851. " available_tracers\t- list of configured tracers for current_tracer\n"
  2852. " buffer_size_kb\t- view and modify size of per cpu buffer\n"
  2853. " buffer_total_size_kb - view total size of all cpu buffers\n\n"
  2854. " trace_clock\t\t-change the clock used to order events\n"
  2855. " local: Per cpu clock but may not be synced across CPUs\n"
  2856. " global: Synced across CPUs but slows tracing down.\n"
  2857. " counter: Not a clock, but just an increment\n"
  2858. " uptime: Jiffy counter from time of boot\n"
  2859. " perf: Same clock that perf events use\n"
  2860. #ifdef CONFIG_X86_64
  2861. " x86-tsc: TSC cycle counter\n"
  2862. #endif
  2863. "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
  2864. " tracing_cpumask\t- Limit which CPUs to trace\n"
  2865. " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
  2866. "\t\t\t Remove sub-buffer with rmdir\n"
  2867. " trace_options\t\t- Set format or modify how tracing happens\n"
  2868. "\t\t\t Disable an option by adding a suffix 'no' to the option name\n"
  2869. #ifdef CONFIG_DYNAMIC_FTRACE
  2870. "\n available_filter_functions - list of functions that can be filtered on\n"
  2871. " set_ftrace_filter\t- echo function name in here to only trace these functions\n"
  2872. " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
  2873. " modules: Can select a group via module\n"
  2874. " Format: :mod:<module-name>\n"
  2875. " example: echo :mod:ext3 > set_ftrace_filter\n"
  2876. " triggers: a command to perform when function is hit\n"
  2877. " Format: <function>:<trigger>[:count]\n"
  2878. " trigger: traceon, traceoff\n"
  2879. " enable_event:<system>:<event>\n"
  2880. " disable_event:<system>:<event>\n"
  2881. #ifdef CONFIG_STACKTRACE
  2882. " stacktrace\n"
  2883. #endif
  2884. #ifdef CONFIG_TRACER_SNAPSHOT
  2885. " snapshot\n"
  2886. #endif
  2887. " example: echo do_fault:traceoff > set_ftrace_filter\n"
  2888. " echo do_trap:traceoff:3 > set_ftrace_filter\n"
  2889. " The first one will disable tracing every time do_fault is hit\n"
  2890. " The second will disable tracing at most 3 times when do_trap is hit\n"
  2891. " The first time do trap is hit and it disables tracing, the counter\n"
  2892. " will decrement to 2. If tracing is already disabled, the counter\n"
  2893. " will not decrement. It only decrements when the trigger did work\n"
  2894. " To remove trigger without count:\n"
  2895. " echo '!<function>:<trigger> > set_ftrace_filter\n"
  2896. " To remove trigger with a count:\n"
  2897. " echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
  2898. " set_ftrace_notrace\t- echo function name in here to never trace.\n"
  2899. " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
  2900. " modules: Can select a group via module command :mod:\n"
  2901. " Does not accept triggers\n"
  2902. #endif /* CONFIG_DYNAMIC_FTRACE */
  2903. #ifdef CONFIG_FUNCTION_TRACER
  2904. " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
  2905. #endif
  2906. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2907. " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
  2908. " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
  2909. #endif
  2910. #ifdef CONFIG_TRACER_SNAPSHOT
  2911. "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
  2912. "\t\t\t Read the contents for more information\n"
  2913. #endif
  2914. #ifdef CONFIG_STACK_TRACER
  2915. " stack_trace\t\t- Shows the max stack trace when active\n"
  2916. " stack_max_size\t- Shows current max stack size that was traced\n"
  2917. "\t\t\t Write into this file to reset the max size (trigger a new trace)\n"
  2918. #ifdef CONFIG_DYNAMIC_FTRACE
  2919. " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
  2920. #endif
  2921. #endif /* CONFIG_STACK_TRACER */
  2922. ;
  2923. static ssize_t
  2924. tracing_readme_read(struct file *filp, char __user *ubuf,
  2925. size_t cnt, loff_t *ppos)
  2926. {
  2927. return simple_read_from_buffer(ubuf, cnt, ppos,
  2928. readme_msg, strlen(readme_msg));
  2929. }
  2930. static const struct file_operations tracing_readme_fops = {
  2931. .open = tracing_open_generic,
  2932. .read = tracing_readme_read,
  2933. .llseek = generic_file_llseek,
  2934. };
  2935. static ssize_t
  2936. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2937. size_t cnt, loff_t *ppos)
  2938. {
  2939. char *buf_comm;
  2940. char *file_buf;
  2941. char *buf;
  2942. int len = 0;
  2943. int pid;
  2944. int i;
  2945. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2946. if (!file_buf)
  2947. return -ENOMEM;
  2948. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2949. if (!buf_comm) {
  2950. kfree(file_buf);
  2951. return -ENOMEM;
  2952. }
  2953. buf = file_buf;
  2954. for (i = 0; i < SAVED_CMDLINES; i++) {
  2955. int r;
  2956. pid = map_cmdline_to_pid[i];
  2957. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2958. continue;
  2959. trace_find_cmdline(pid, buf_comm);
  2960. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2961. buf += r;
  2962. len += r;
  2963. }
  2964. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2965. file_buf, len);
  2966. kfree(file_buf);
  2967. kfree(buf_comm);
  2968. return len;
  2969. }
  2970. static const struct file_operations tracing_saved_cmdlines_fops = {
  2971. .open = tracing_open_generic,
  2972. .read = tracing_saved_cmdlines_read,
  2973. .llseek = generic_file_llseek,
  2974. };
  2975. static ssize_t
  2976. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2977. size_t cnt, loff_t *ppos)
  2978. {
  2979. struct trace_array *tr = filp->private_data;
  2980. char buf[MAX_TRACER_SIZE+2];
  2981. int r;
  2982. mutex_lock(&trace_types_lock);
  2983. r = sprintf(buf, "%s\n", tr->current_trace->name);
  2984. mutex_unlock(&trace_types_lock);
  2985. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2986. }
  2987. int tracer_init(struct tracer *t, struct trace_array *tr)
  2988. {
  2989. tracing_reset_online_cpus(&tr->trace_buffer);
  2990. return t->init(tr);
  2991. }
  2992. static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
  2993. {
  2994. int cpu;
  2995. for_each_tracing_cpu(cpu)
  2996. per_cpu_ptr(buf->data, cpu)->entries = val;
  2997. }
  2998. #ifdef CONFIG_TRACER_MAX_TRACE
  2999. /* resize @tr's buffer to the size of @size_tr's entries */
  3000. static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
  3001. struct trace_buffer *size_buf, int cpu_id)
  3002. {
  3003. int cpu, ret = 0;
  3004. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  3005. for_each_tracing_cpu(cpu) {
  3006. ret = ring_buffer_resize(trace_buf->buffer,
  3007. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  3008. if (ret < 0)
  3009. break;
  3010. per_cpu_ptr(trace_buf->data, cpu)->entries =
  3011. per_cpu_ptr(size_buf->data, cpu)->entries;
  3012. }
  3013. } else {
  3014. ret = ring_buffer_resize(trace_buf->buffer,
  3015. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  3016. if (ret == 0)
  3017. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  3018. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  3019. }
  3020. return ret;
  3021. }
  3022. #endif /* CONFIG_TRACER_MAX_TRACE */
  3023. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  3024. unsigned long size, int cpu)
  3025. {
  3026. int ret;
  3027. /*
  3028. * If kernel or user changes the size of the ring buffer
  3029. * we use the size that was given, and we can forget about
  3030. * expanding it later.
  3031. */
  3032. ring_buffer_expanded = true;
  3033. /* May be called before buffers are initialized */
  3034. if (!tr->trace_buffer.buffer)
  3035. return 0;
  3036. ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
  3037. if (ret < 0)
  3038. return ret;
  3039. #ifdef CONFIG_TRACER_MAX_TRACE
  3040. if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
  3041. !tr->current_trace->use_max_tr)
  3042. goto out;
  3043. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  3044. if (ret < 0) {
  3045. int r = resize_buffer_duplicate_size(&tr->trace_buffer,
  3046. &tr->trace_buffer, cpu);
  3047. if (r < 0) {
  3048. /*
  3049. * AARGH! We are left with different
  3050. * size max buffer!!!!
  3051. * The max buffer is our "snapshot" buffer.
  3052. * When a tracer needs a snapshot (one of the
  3053. * latency tracers), it swaps the max buffer
  3054. * with the saved snap shot. We succeeded to
  3055. * update the size of the main buffer, but failed to
  3056. * update the size of the max buffer. But when we tried
  3057. * to reset the main buffer to the original size, we
  3058. * failed there too. This is very unlikely to
  3059. * happen, but if it does, warn and kill all
  3060. * tracing.
  3061. */
  3062. WARN_ON(1);
  3063. tracing_disabled = 1;
  3064. }
  3065. return ret;
  3066. }
  3067. if (cpu == RING_BUFFER_ALL_CPUS)
  3068. set_buffer_entries(&tr->max_buffer, size);
  3069. else
  3070. per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
  3071. out:
  3072. #endif /* CONFIG_TRACER_MAX_TRACE */
  3073. if (cpu == RING_BUFFER_ALL_CPUS)
  3074. set_buffer_entries(&tr->trace_buffer, size);
  3075. else
  3076. per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
  3077. return ret;
  3078. }
  3079. static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  3080. unsigned long size, int cpu_id)
  3081. {
  3082. int ret = size;
  3083. mutex_lock(&trace_types_lock);
  3084. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  3085. /* make sure, this cpu is enabled in the mask */
  3086. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
  3087. ret = -EINVAL;
  3088. goto out;
  3089. }
  3090. }
  3091. ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
  3092. if (ret < 0)
  3093. ret = -ENOMEM;
  3094. out:
  3095. mutex_unlock(&trace_types_lock);
  3096. return ret;
  3097. }
  3098. /**
  3099. * tracing_update_buffers - used by tracing facility to expand ring buffers
  3100. *
  3101. * To save on memory when the tracing is never used on a system with it
  3102. * configured in. The ring buffers are set to a minimum size. But once
  3103. * a user starts to use the tracing facility, then they need to grow
  3104. * to their default size.
  3105. *
  3106. * This function is to be called when a tracer is about to be used.
  3107. */
  3108. int tracing_update_buffers(void)
  3109. {
  3110. int ret = 0;
  3111. mutex_lock(&trace_types_lock);
  3112. if (!ring_buffer_expanded)
  3113. ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
  3114. RING_BUFFER_ALL_CPUS);
  3115. mutex_unlock(&trace_types_lock);
  3116. return ret;
  3117. }
  3118. struct trace_option_dentry;
  3119. static struct trace_option_dentry *
  3120. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  3121. static void
  3122. destroy_trace_option_files(struct trace_option_dentry *topts);
  3123. static int tracing_set_tracer(const char *buf)
  3124. {
  3125. static struct trace_option_dentry *topts;
  3126. struct trace_array *tr = &global_trace;
  3127. struct tracer *t;
  3128. #ifdef CONFIG_TRACER_MAX_TRACE
  3129. bool had_max_tr;
  3130. #endif
  3131. int ret = 0;
  3132. mutex_lock(&trace_types_lock);
  3133. if (!ring_buffer_expanded) {
  3134. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  3135. RING_BUFFER_ALL_CPUS);
  3136. if (ret < 0)
  3137. goto out;
  3138. ret = 0;
  3139. }
  3140. for (t = trace_types; t; t = t->next) {
  3141. if (strcmp(t->name, buf) == 0)
  3142. break;
  3143. }
  3144. if (!t) {
  3145. ret = -EINVAL;
  3146. goto out;
  3147. }
  3148. if (t == tr->current_trace)
  3149. goto out;
  3150. trace_branch_disable();
  3151. tr->current_trace->enabled = false;
  3152. if (tr->current_trace->reset)
  3153. tr->current_trace->reset(tr);
  3154. /* Current trace needs to be nop_trace before synchronize_sched */
  3155. tr->current_trace = &nop_trace;
  3156. #ifdef CONFIG_TRACER_MAX_TRACE
  3157. had_max_tr = tr->allocated_snapshot;
  3158. if (had_max_tr && !t->use_max_tr) {
  3159. /*
  3160. * We need to make sure that the update_max_tr sees that
  3161. * current_trace changed to nop_trace to keep it from
  3162. * swapping the buffers after we resize it.
  3163. * The update_max_tr is called from interrupts disabled
  3164. * so a synchronized_sched() is sufficient.
  3165. */
  3166. synchronize_sched();
  3167. free_snapshot(tr);
  3168. }
  3169. #endif
  3170. destroy_trace_option_files(topts);
  3171. topts = create_trace_option_files(tr, t);
  3172. #ifdef CONFIG_TRACER_MAX_TRACE
  3173. if (t->use_max_tr && !had_max_tr) {
  3174. ret = alloc_snapshot(tr);
  3175. if (ret < 0)
  3176. goto out;
  3177. }
  3178. #endif
  3179. if (t->init) {
  3180. ret = tracer_init(t, tr);
  3181. if (ret)
  3182. goto out;
  3183. }
  3184. tr->current_trace = t;
  3185. tr->current_trace->enabled = true;
  3186. trace_branch_enable(tr);
  3187. out:
  3188. mutex_unlock(&trace_types_lock);
  3189. return ret;
  3190. }
  3191. static ssize_t
  3192. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  3193. size_t cnt, loff_t *ppos)
  3194. {
  3195. char buf[MAX_TRACER_SIZE+1];
  3196. int i;
  3197. size_t ret;
  3198. int err;
  3199. ret = cnt;
  3200. if (cnt > MAX_TRACER_SIZE)
  3201. cnt = MAX_TRACER_SIZE;
  3202. if (copy_from_user(&buf, ubuf, cnt))
  3203. return -EFAULT;
  3204. buf[cnt] = 0;
  3205. /* strip ending whitespace. */
  3206. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  3207. buf[i] = 0;
  3208. err = tracing_set_tracer(buf);
  3209. if (err)
  3210. return err;
  3211. *ppos += ret;
  3212. return ret;
  3213. }
  3214. static ssize_t
  3215. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  3216. size_t cnt, loff_t *ppos)
  3217. {
  3218. unsigned long *ptr = filp->private_data;
  3219. char buf[64];
  3220. int r;
  3221. r = snprintf(buf, sizeof(buf), "%ld\n",
  3222. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  3223. if (r > sizeof(buf))
  3224. r = sizeof(buf);
  3225. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3226. }
  3227. static ssize_t
  3228. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  3229. size_t cnt, loff_t *ppos)
  3230. {
  3231. unsigned long *ptr = filp->private_data;
  3232. unsigned long val;
  3233. int ret;
  3234. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3235. if (ret)
  3236. return ret;
  3237. *ptr = val * 1000;
  3238. return cnt;
  3239. }
  3240. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  3241. {
  3242. struct trace_array *tr = inode->i_private;
  3243. struct trace_iterator *iter;
  3244. int ret = 0;
  3245. if (tracing_disabled)
  3246. return -ENODEV;
  3247. if (trace_array_get(tr) < 0)
  3248. return -ENODEV;
  3249. mutex_lock(&trace_types_lock);
  3250. /* create a buffer to store the information to pass to userspace */
  3251. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3252. if (!iter) {
  3253. ret = -ENOMEM;
  3254. __trace_array_put(tr);
  3255. goto out;
  3256. }
  3257. /*
  3258. * We make a copy of the current tracer to avoid concurrent
  3259. * changes on it while we are reading.
  3260. */
  3261. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  3262. if (!iter->trace) {
  3263. ret = -ENOMEM;
  3264. goto fail;
  3265. }
  3266. *iter->trace = *tr->current_trace;
  3267. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  3268. ret = -ENOMEM;
  3269. goto fail;
  3270. }
  3271. /* trace pipe does not show start of buffer */
  3272. cpumask_setall(iter->started);
  3273. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  3274. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  3275. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  3276. if (trace_clocks[tr->clock_id].in_ns)
  3277. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  3278. iter->tr = tr;
  3279. iter->trace_buffer = &tr->trace_buffer;
  3280. iter->cpu_file = tracing_get_cpu(inode);
  3281. mutex_init(&iter->mutex);
  3282. filp->private_data = iter;
  3283. if (iter->trace->pipe_open)
  3284. iter->trace->pipe_open(iter);
  3285. nonseekable_open(inode, filp);
  3286. out:
  3287. mutex_unlock(&trace_types_lock);
  3288. return ret;
  3289. fail:
  3290. kfree(iter->trace);
  3291. kfree(iter);
  3292. __trace_array_put(tr);
  3293. mutex_unlock(&trace_types_lock);
  3294. return ret;
  3295. }
  3296. static int tracing_release_pipe(struct inode *inode, struct file *file)
  3297. {
  3298. struct trace_iterator *iter = file->private_data;
  3299. struct trace_array *tr = inode->i_private;
  3300. mutex_lock(&trace_types_lock);
  3301. if (iter->trace->pipe_close)
  3302. iter->trace->pipe_close(iter);
  3303. mutex_unlock(&trace_types_lock);
  3304. free_cpumask_var(iter->started);
  3305. mutex_destroy(&iter->mutex);
  3306. kfree(iter->trace);
  3307. kfree(iter);
  3308. trace_array_put(tr);
  3309. return 0;
  3310. }
  3311. static unsigned int
  3312. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  3313. {
  3314. /* Iterators are static, they should be filled or empty */
  3315. if (trace_buffer_iter(iter, iter->cpu_file))
  3316. return POLLIN | POLLRDNORM;
  3317. if (trace_flags & TRACE_ITER_BLOCK)
  3318. /*
  3319. * Always select as readable when in blocking mode
  3320. */
  3321. return POLLIN | POLLRDNORM;
  3322. else
  3323. return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
  3324. filp, poll_table);
  3325. }
  3326. static unsigned int
  3327. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  3328. {
  3329. struct trace_iterator *iter = filp->private_data;
  3330. return trace_poll(iter, filp, poll_table);
  3331. }
  3332. /*
  3333. * This is a make-shift waitqueue.
  3334. * A tracer might use this callback on some rare cases:
  3335. *
  3336. * 1) the current tracer might hold the runqueue lock when it wakes up
  3337. * a reader, hence a deadlock (sched, function, and function graph tracers)
  3338. * 2) the function tracers, trace all functions, we don't want
  3339. * the overhead of calling wake_up and friends
  3340. * (and tracing them too)
  3341. *
  3342. * Anyway, this is really very primitive wakeup.
  3343. */
  3344. void poll_wait_pipe(struct trace_iterator *iter)
  3345. {
  3346. set_current_state(TASK_INTERRUPTIBLE);
  3347. /* sleep for 100 msecs, and try again. */
  3348. schedule_timeout(HZ / 10);
  3349. }
  3350. /* Must be called with trace_types_lock mutex held. */
  3351. static int tracing_wait_pipe(struct file *filp)
  3352. {
  3353. struct trace_iterator *iter = filp->private_data;
  3354. while (trace_empty(iter)) {
  3355. if ((filp->f_flags & O_NONBLOCK)) {
  3356. return -EAGAIN;
  3357. }
  3358. mutex_unlock(&iter->mutex);
  3359. iter->trace->wait_pipe(iter);
  3360. mutex_lock(&iter->mutex);
  3361. if (signal_pending(current))
  3362. return -EINTR;
  3363. /*
  3364. * We block until we read something and tracing is disabled.
  3365. * We still block if tracing is disabled, but we have never
  3366. * read anything. This allows a user to cat this file, and
  3367. * then enable tracing. But after we have read something,
  3368. * we give an EOF when tracing is again disabled.
  3369. *
  3370. * iter->pos will be 0 if we haven't read anything.
  3371. */
  3372. if (!tracing_is_on() && iter->pos)
  3373. break;
  3374. }
  3375. return 1;
  3376. }
  3377. /*
  3378. * Consumer reader.
  3379. */
  3380. static ssize_t
  3381. tracing_read_pipe(struct file *filp, char __user *ubuf,
  3382. size_t cnt, loff_t *ppos)
  3383. {
  3384. struct trace_iterator *iter = filp->private_data;
  3385. struct trace_array *tr = iter->tr;
  3386. ssize_t sret;
  3387. /* return any leftover data */
  3388. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3389. if (sret != -EBUSY)
  3390. return sret;
  3391. trace_seq_init(&iter->seq);
  3392. /* copy the tracer to avoid using a global lock all around */
  3393. mutex_lock(&trace_types_lock);
  3394. if (unlikely(iter->trace->name != tr->current_trace->name))
  3395. *iter->trace = *tr->current_trace;
  3396. mutex_unlock(&trace_types_lock);
  3397. /*
  3398. * Avoid more than one consumer on a single file descriptor
  3399. * This is just a matter of traces coherency, the ring buffer itself
  3400. * is protected.
  3401. */
  3402. mutex_lock(&iter->mutex);
  3403. if (iter->trace->read) {
  3404. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  3405. if (sret)
  3406. goto out;
  3407. }
  3408. waitagain:
  3409. sret = tracing_wait_pipe(filp);
  3410. if (sret <= 0)
  3411. goto out;
  3412. /* stop when tracing is finished */
  3413. if (trace_empty(iter)) {
  3414. sret = 0;
  3415. goto out;
  3416. }
  3417. if (cnt >= PAGE_SIZE)
  3418. cnt = PAGE_SIZE - 1;
  3419. /* reset all but tr, trace, and overruns */
  3420. memset(&iter->seq, 0,
  3421. sizeof(struct trace_iterator) -
  3422. offsetof(struct trace_iterator, seq));
  3423. cpumask_clear(iter->started);
  3424. iter->pos = -1;
  3425. trace_event_read_lock();
  3426. trace_access_lock(iter->cpu_file);
  3427. while (trace_find_next_entry_inc(iter) != NULL) {
  3428. enum print_line_t ret;
  3429. int len = iter->seq.len;
  3430. ret = print_trace_line(iter);
  3431. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3432. /* don't print partial lines */
  3433. iter->seq.len = len;
  3434. break;
  3435. }
  3436. if (ret != TRACE_TYPE_NO_CONSUME)
  3437. trace_consume(iter);
  3438. if (iter->seq.len >= cnt)
  3439. break;
  3440. /*
  3441. * Setting the full flag means we reached the trace_seq buffer
  3442. * size and we should leave by partial output condition above.
  3443. * One of the trace_seq_* functions is not used properly.
  3444. */
  3445. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  3446. iter->ent->type);
  3447. }
  3448. trace_access_unlock(iter->cpu_file);
  3449. trace_event_read_unlock();
  3450. /* Now copy what we have to the user */
  3451. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  3452. if (iter->seq.readpos >= iter->seq.len)
  3453. trace_seq_init(&iter->seq);
  3454. /*
  3455. * If there was nothing to send to user, in spite of consuming trace
  3456. * entries, go back to wait for more entries.
  3457. */
  3458. if (sret == -EBUSY)
  3459. goto waitagain;
  3460. out:
  3461. mutex_unlock(&iter->mutex);
  3462. return sret;
  3463. }
  3464. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  3465. struct pipe_buffer *buf)
  3466. {
  3467. __free_page(buf->page);
  3468. }
  3469. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  3470. unsigned int idx)
  3471. {
  3472. __free_page(spd->pages[idx]);
  3473. }
  3474. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  3475. .can_merge = 0,
  3476. .map = generic_pipe_buf_map,
  3477. .unmap = generic_pipe_buf_unmap,
  3478. .confirm = generic_pipe_buf_confirm,
  3479. .release = tracing_pipe_buf_release,
  3480. .steal = generic_pipe_buf_steal,
  3481. .get = generic_pipe_buf_get,
  3482. };
  3483. static size_t
  3484. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  3485. {
  3486. size_t count;
  3487. int ret;
  3488. /* Seq buffer is page-sized, exactly what we need. */
  3489. for (;;) {
  3490. count = iter->seq.len;
  3491. ret = print_trace_line(iter);
  3492. count = iter->seq.len - count;
  3493. if (rem < count) {
  3494. rem = 0;
  3495. iter->seq.len -= count;
  3496. break;
  3497. }
  3498. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3499. iter->seq.len -= count;
  3500. break;
  3501. }
  3502. if (ret != TRACE_TYPE_NO_CONSUME)
  3503. trace_consume(iter);
  3504. rem -= count;
  3505. if (!trace_find_next_entry_inc(iter)) {
  3506. rem = 0;
  3507. iter->ent = NULL;
  3508. break;
  3509. }
  3510. }
  3511. return rem;
  3512. }
  3513. static ssize_t tracing_splice_read_pipe(struct file *filp,
  3514. loff_t *ppos,
  3515. struct pipe_inode_info *pipe,
  3516. size_t len,
  3517. unsigned int flags)
  3518. {
  3519. struct page *pages_def[PIPE_DEF_BUFFERS];
  3520. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3521. struct trace_iterator *iter = filp->private_data;
  3522. struct splice_pipe_desc spd = {
  3523. .pages = pages_def,
  3524. .partial = partial_def,
  3525. .nr_pages = 0, /* This gets updated below. */
  3526. .nr_pages_max = PIPE_DEF_BUFFERS,
  3527. .flags = flags,
  3528. .ops = &tracing_pipe_buf_ops,
  3529. .spd_release = tracing_spd_release_pipe,
  3530. };
  3531. struct trace_array *tr = iter->tr;
  3532. ssize_t ret;
  3533. size_t rem;
  3534. unsigned int i;
  3535. if (splice_grow_spd(pipe, &spd))
  3536. return -ENOMEM;
  3537. /* copy the tracer to avoid using a global lock all around */
  3538. mutex_lock(&trace_types_lock);
  3539. if (unlikely(iter->trace->name != tr->current_trace->name))
  3540. *iter->trace = *tr->current_trace;
  3541. mutex_unlock(&trace_types_lock);
  3542. mutex_lock(&iter->mutex);
  3543. if (iter->trace->splice_read) {
  3544. ret = iter->trace->splice_read(iter, filp,
  3545. ppos, pipe, len, flags);
  3546. if (ret)
  3547. goto out_err;
  3548. }
  3549. ret = tracing_wait_pipe(filp);
  3550. if (ret <= 0)
  3551. goto out_err;
  3552. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  3553. ret = -EFAULT;
  3554. goto out_err;
  3555. }
  3556. trace_event_read_lock();
  3557. trace_access_lock(iter->cpu_file);
  3558. /* Fill as many pages as possible. */
  3559. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  3560. spd.pages[i] = alloc_page(GFP_KERNEL);
  3561. if (!spd.pages[i])
  3562. break;
  3563. rem = tracing_fill_pipe_page(rem, iter);
  3564. /* Copy the data into the page, so we can start over. */
  3565. ret = trace_seq_to_buffer(&iter->seq,
  3566. page_address(spd.pages[i]),
  3567. iter->seq.len);
  3568. if (ret < 0) {
  3569. __free_page(spd.pages[i]);
  3570. break;
  3571. }
  3572. spd.partial[i].offset = 0;
  3573. spd.partial[i].len = iter->seq.len;
  3574. trace_seq_init(&iter->seq);
  3575. }
  3576. trace_access_unlock(iter->cpu_file);
  3577. trace_event_read_unlock();
  3578. mutex_unlock(&iter->mutex);
  3579. spd.nr_pages = i;
  3580. ret = splice_to_pipe(pipe, &spd);
  3581. out:
  3582. splice_shrink_spd(&spd);
  3583. return ret;
  3584. out_err:
  3585. mutex_unlock(&iter->mutex);
  3586. goto out;
  3587. }
  3588. static ssize_t
  3589. tracing_entries_read(struct file *filp, char __user *ubuf,
  3590. size_t cnt, loff_t *ppos)
  3591. {
  3592. struct inode *inode = file_inode(filp);
  3593. struct trace_array *tr = inode->i_private;
  3594. int cpu = tracing_get_cpu(inode);
  3595. char buf[64];
  3596. int r = 0;
  3597. ssize_t ret;
  3598. mutex_lock(&trace_types_lock);
  3599. if (cpu == RING_BUFFER_ALL_CPUS) {
  3600. int cpu, buf_size_same;
  3601. unsigned long size;
  3602. size = 0;
  3603. buf_size_same = 1;
  3604. /* check if all cpu sizes are same */
  3605. for_each_tracing_cpu(cpu) {
  3606. /* fill in the size from first enabled cpu */
  3607. if (size == 0)
  3608. size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
  3609. if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
  3610. buf_size_same = 0;
  3611. break;
  3612. }
  3613. }
  3614. if (buf_size_same) {
  3615. if (!ring_buffer_expanded)
  3616. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3617. size >> 10,
  3618. trace_buf_size >> 10);
  3619. else
  3620. r = sprintf(buf, "%lu\n", size >> 10);
  3621. } else
  3622. r = sprintf(buf, "X\n");
  3623. } else
  3624. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
  3625. mutex_unlock(&trace_types_lock);
  3626. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3627. return ret;
  3628. }
  3629. static ssize_t
  3630. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3631. size_t cnt, loff_t *ppos)
  3632. {
  3633. struct inode *inode = file_inode(filp);
  3634. struct trace_array *tr = inode->i_private;
  3635. unsigned long val;
  3636. int ret;
  3637. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3638. if (ret)
  3639. return ret;
  3640. /* must have at least 1 entry */
  3641. if (!val)
  3642. return -EINVAL;
  3643. /* value is in KB */
  3644. val <<= 10;
  3645. ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
  3646. if (ret < 0)
  3647. return ret;
  3648. *ppos += cnt;
  3649. return cnt;
  3650. }
  3651. static ssize_t
  3652. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3653. size_t cnt, loff_t *ppos)
  3654. {
  3655. struct trace_array *tr = filp->private_data;
  3656. char buf[64];
  3657. int r, cpu;
  3658. unsigned long size = 0, expanded_size = 0;
  3659. mutex_lock(&trace_types_lock);
  3660. for_each_tracing_cpu(cpu) {
  3661. size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
  3662. if (!ring_buffer_expanded)
  3663. expanded_size += trace_buf_size >> 10;
  3664. }
  3665. if (ring_buffer_expanded)
  3666. r = sprintf(buf, "%lu\n", size);
  3667. else
  3668. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3669. mutex_unlock(&trace_types_lock);
  3670. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3671. }
  3672. static ssize_t
  3673. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3674. size_t cnt, loff_t *ppos)
  3675. {
  3676. /*
  3677. * There is no need to read what the user has written, this function
  3678. * is just to make sure that there is no error when "echo" is used
  3679. */
  3680. *ppos += cnt;
  3681. return cnt;
  3682. }
  3683. static int
  3684. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3685. {
  3686. struct trace_array *tr = inode->i_private;
  3687. /* disable tracing ? */
  3688. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3689. tracer_tracing_off(tr);
  3690. /* resize the ring buffer to 0 */
  3691. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  3692. trace_array_put(tr);
  3693. return 0;
  3694. }
  3695. static ssize_t
  3696. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3697. size_t cnt, loff_t *fpos)
  3698. {
  3699. unsigned long addr = (unsigned long)ubuf;
  3700. struct trace_array *tr = filp->private_data;
  3701. struct ring_buffer_event *event;
  3702. struct ring_buffer *buffer;
  3703. struct print_entry *entry;
  3704. unsigned long irq_flags;
  3705. struct page *pages[2];
  3706. void *map_page[2];
  3707. int nr_pages = 1;
  3708. ssize_t written;
  3709. int offset;
  3710. int size;
  3711. int len;
  3712. int ret;
  3713. int i;
  3714. if (tracing_disabled)
  3715. return -EINVAL;
  3716. if (!(trace_flags & TRACE_ITER_MARKERS))
  3717. return -EINVAL;
  3718. if (cnt > TRACE_BUF_SIZE)
  3719. cnt = TRACE_BUF_SIZE;
  3720. /*
  3721. * Userspace is injecting traces into the kernel trace buffer.
  3722. * We want to be as non intrusive as possible.
  3723. * To do so, we do not want to allocate any special buffers
  3724. * or take any locks, but instead write the userspace data
  3725. * straight into the ring buffer.
  3726. *
  3727. * First we need to pin the userspace buffer into memory,
  3728. * which, most likely it is, because it just referenced it.
  3729. * But there's no guarantee that it is. By using get_user_pages_fast()
  3730. * and kmap_atomic/kunmap_atomic() we can get access to the
  3731. * pages directly. We then write the data directly into the
  3732. * ring buffer.
  3733. */
  3734. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3735. /* check if we cross pages */
  3736. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3737. nr_pages = 2;
  3738. offset = addr & (PAGE_SIZE - 1);
  3739. addr &= PAGE_MASK;
  3740. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3741. if (ret < nr_pages) {
  3742. while (--ret >= 0)
  3743. put_page(pages[ret]);
  3744. written = -EFAULT;
  3745. goto out;
  3746. }
  3747. for (i = 0; i < nr_pages; i++)
  3748. map_page[i] = kmap_atomic(pages[i]);
  3749. local_save_flags(irq_flags);
  3750. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3751. buffer = tr->trace_buffer.buffer;
  3752. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3753. irq_flags, preempt_count());
  3754. if (!event) {
  3755. /* Ring buffer disabled, return as if not open for write */
  3756. written = -EBADF;
  3757. goto out_unlock;
  3758. }
  3759. entry = ring_buffer_event_data(event);
  3760. entry->ip = _THIS_IP_;
  3761. if (nr_pages == 2) {
  3762. len = PAGE_SIZE - offset;
  3763. memcpy(&entry->buf, map_page[0] + offset, len);
  3764. memcpy(&entry->buf[len], map_page[1], cnt - len);
  3765. } else
  3766. memcpy(&entry->buf, map_page[0] + offset, cnt);
  3767. if (entry->buf[cnt - 1] != '\n') {
  3768. entry->buf[cnt] = '\n';
  3769. entry->buf[cnt + 1] = '\0';
  3770. } else
  3771. entry->buf[cnt] = '\0';
  3772. __buffer_unlock_commit(buffer, event);
  3773. written = cnt;
  3774. *fpos += written;
  3775. out_unlock:
  3776. for (i = 0; i < nr_pages; i++){
  3777. kunmap_atomic(map_page[i]);
  3778. put_page(pages[i]);
  3779. }
  3780. out:
  3781. return written;
  3782. }
  3783. static int tracing_clock_show(struct seq_file *m, void *v)
  3784. {
  3785. struct trace_array *tr = m->private;
  3786. int i;
  3787. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3788. seq_printf(m,
  3789. "%s%s%s%s", i ? " " : "",
  3790. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  3791. i == tr->clock_id ? "]" : "");
  3792. seq_putc(m, '\n');
  3793. return 0;
  3794. }
  3795. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3796. size_t cnt, loff_t *fpos)
  3797. {
  3798. struct seq_file *m = filp->private_data;
  3799. struct trace_array *tr = m->private;
  3800. char buf[64];
  3801. const char *clockstr;
  3802. int i;
  3803. if (cnt >= sizeof(buf))
  3804. return -EINVAL;
  3805. if (copy_from_user(&buf, ubuf, cnt))
  3806. return -EFAULT;
  3807. buf[cnt] = 0;
  3808. clockstr = strstrip(buf);
  3809. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3810. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3811. break;
  3812. }
  3813. if (i == ARRAY_SIZE(trace_clocks))
  3814. return -EINVAL;
  3815. mutex_lock(&trace_types_lock);
  3816. tr->clock_id = i;
  3817. ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
  3818. /*
  3819. * New clock may not be consistent with the previous clock.
  3820. * Reset the buffer so that it doesn't have incomparable timestamps.
  3821. */
  3822. tracing_reset_online_cpus(&tr->trace_buffer);
  3823. #ifdef CONFIG_TRACER_MAX_TRACE
  3824. if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
  3825. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  3826. tracing_reset_online_cpus(&tr->max_buffer);
  3827. #endif
  3828. mutex_unlock(&trace_types_lock);
  3829. *fpos += cnt;
  3830. return cnt;
  3831. }
  3832. static int tracing_clock_open(struct inode *inode, struct file *file)
  3833. {
  3834. struct trace_array *tr = inode->i_private;
  3835. int ret;
  3836. if (tracing_disabled)
  3837. return -ENODEV;
  3838. if (trace_array_get(tr))
  3839. return -ENODEV;
  3840. ret = single_open(file, tracing_clock_show, inode->i_private);
  3841. if (ret < 0)
  3842. trace_array_put(tr);
  3843. return ret;
  3844. }
  3845. struct ftrace_buffer_info {
  3846. struct trace_iterator iter;
  3847. void *spare;
  3848. unsigned int read;
  3849. };
  3850. #ifdef CONFIG_TRACER_SNAPSHOT
  3851. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  3852. {
  3853. struct trace_array *tr = inode->i_private;
  3854. struct trace_iterator *iter;
  3855. struct seq_file *m;
  3856. int ret = 0;
  3857. if (trace_array_get(tr) < 0)
  3858. return -ENODEV;
  3859. if (file->f_mode & FMODE_READ) {
  3860. iter = __tracing_open(inode, file, true);
  3861. if (IS_ERR(iter))
  3862. ret = PTR_ERR(iter);
  3863. } else {
  3864. /* Writes still need the seq_file to hold the private data */
  3865. ret = -ENOMEM;
  3866. m = kzalloc(sizeof(*m), GFP_KERNEL);
  3867. if (!m)
  3868. goto out;
  3869. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  3870. if (!iter) {
  3871. kfree(m);
  3872. goto out;
  3873. }
  3874. ret = 0;
  3875. iter->tr = tr;
  3876. iter->trace_buffer = &tr->max_buffer;
  3877. iter->cpu_file = tracing_get_cpu(inode);
  3878. m->private = iter;
  3879. file->private_data = m;
  3880. }
  3881. out:
  3882. if (ret < 0)
  3883. trace_array_put(tr);
  3884. return ret;
  3885. }
  3886. static ssize_t
  3887. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3888. loff_t *ppos)
  3889. {
  3890. struct seq_file *m = filp->private_data;
  3891. struct trace_iterator *iter = m->private;
  3892. struct trace_array *tr = iter->tr;
  3893. unsigned long val;
  3894. int ret;
  3895. ret = tracing_update_buffers();
  3896. if (ret < 0)
  3897. return ret;
  3898. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3899. if (ret)
  3900. return ret;
  3901. mutex_lock(&trace_types_lock);
  3902. if (tr->current_trace->use_max_tr) {
  3903. ret = -EBUSY;
  3904. goto out;
  3905. }
  3906. switch (val) {
  3907. case 0:
  3908. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3909. ret = -EINVAL;
  3910. break;
  3911. }
  3912. if (tr->allocated_snapshot)
  3913. free_snapshot(tr);
  3914. break;
  3915. case 1:
  3916. /* Only allow per-cpu swap if the ring buffer supports it */
  3917. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  3918. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3919. ret = -EINVAL;
  3920. break;
  3921. }
  3922. #endif
  3923. if (!tr->allocated_snapshot) {
  3924. ret = alloc_snapshot(tr);
  3925. if (ret < 0)
  3926. break;
  3927. }
  3928. local_irq_disable();
  3929. /* Now, we're going to swap */
  3930. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3931. update_max_tr(tr, current, smp_processor_id());
  3932. else
  3933. update_max_tr_single(tr, current, iter->cpu_file);
  3934. local_irq_enable();
  3935. break;
  3936. default:
  3937. if (tr->allocated_snapshot) {
  3938. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3939. tracing_reset_online_cpus(&tr->max_buffer);
  3940. else
  3941. tracing_reset(&tr->max_buffer, iter->cpu_file);
  3942. }
  3943. break;
  3944. }
  3945. if (ret >= 0) {
  3946. *ppos += cnt;
  3947. ret = cnt;
  3948. }
  3949. out:
  3950. mutex_unlock(&trace_types_lock);
  3951. return ret;
  3952. }
  3953. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  3954. {
  3955. struct seq_file *m = file->private_data;
  3956. int ret;
  3957. ret = tracing_release(inode, file);
  3958. if (file->f_mode & FMODE_READ)
  3959. return ret;
  3960. /* If write only, the seq_file is just a stub */
  3961. if (m)
  3962. kfree(m->private);
  3963. kfree(m);
  3964. return 0;
  3965. }
  3966. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  3967. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  3968. size_t count, loff_t *ppos);
  3969. static int tracing_buffers_release(struct inode *inode, struct file *file);
  3970. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3971. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  3972. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  3973. {
  3974. struct ftrace_buffer_info *info;
  3975. int ret;
  3976. ret = tracing_buffers_open(inode, filp);
  3977. if (ret < 0)
  3978. return ret;
  3979. info = filp->private_data;
  3980. if (info->iter.trace->use_max_tr) {
  3981. tracing_buffers_release(inode, filp);
  3982. return -EBUSY;
  3983. }
  3984. info->iter.snapshot = true;
  3985. info->iter.trace_buffer = &info->iter.tr->max_buffer;
  3986. return ret;
  3987. }
  3988. #endif /* CONFIG_TRACER_SNAPSHOT */
  3989. static const struct file_operations tracing_max_lat_fops = {
  3990. .open = tracing_open_generic,
  3991. .read = tracing_max_lat_read,
  3992. .write = tracing_max_lat_write,
  3993. .llseek = generic_file_llseek,
  3994. };
  3995. static const struct file_operations set_tracer_fops = {
  3996. .open = tracing_open_generic,
  3997. .read = tracing_set_trace_read,
  3998. .write = tracing_set_trace_write,
  3999. .llseek = generic_file_llseek,
  4000. };
  4001. static const struct file_operations tracing_pipe_fops = {
  4002. .open = tracing_open_pipe,
  4003. .poll = tracing_poll_pipe,
  4004. .read = tracing_read_pipe,
  4005. .splice_read = tracing_splice_read_pipe,
  4006. .release = tracing_release_pipe,
  4007. .llseek = no_llseek,
  4008. };
  4009. static const struct file_operations tracing_entries_fops = {
  4010. .open = tracing_open_generic_tr,
  4011. .read = tracing_entries_read,
  4012. .write = tracing_entries_write,
  4013. .llseek = generic_file_llseek,
  4014. .release = tracing_release_generic_tr,
  4015. };
  4016. static const struct file_operations tracing_total_entries_fops = {
  4017. .open = tracing_open_generic_tr,
  4018. .read = tracing_total_entries_read,
  4019. .llseek = generic_file_llseek,
  4020. .release = tracing_release_generic_tr,
  4021. };
  4022. static const struct file_operations tracing_free_buffer_fops = {
  4023. .open = tracing_open_generic_tr,
  4024. .write = tracing_free_buffer_write,
  4025. .release = tracing_free_buffer_release,
  4026. };
  4027. static const struct file_operations tracing_mark_fops = {
  4028. .open = tracing_open_generic_tr,
  4029. .write = tracing_mark_write,
  4030. .llseek = generic_file_llseek,
  4031. .release = tracing_release_generic_tr,
  4032. };
  4033. static const struct file_operations trace_clock_fops = {
  4034. .open = tracing_clock_open,
  4035. .read = seq_read,
  4036. .llseek = seq_lseek,
  4037. .release = tracing_single_release_tr,
  4038. .write = tracing_clock_write,
  4039. };
  4040. #ifdef CONFIG_TRACER_SNAPSHOT
  4041. static const struct file_operations snapshot_fops = {
  4042. .open = tracing_snapshot_open,
  4043. .read = seq_read,
  4044. .write = tracing_snapshot_write,
  4045. .llseek = tracing_seek,
  4046. .release = tracing_snapshot_release,
  4047. };
  4048. static const struct file_operations snapshot_raw_fops = {
  4049. .open = snapshot_raw_open,
  4050. .read = tracing_buffers_read,
  4051. .release = tracing_buffers_release,
  4052. .splice_read = tracing_buffers_splice_read,
  4053. .llseek = no_llseek,
  4054. };
  4055. #endif /* CONFIG_TRACER_SNAPSHOT */
  4056. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  4057. {
  4058. struct trace_array *tr = inode->i_private;
  4059. struct ftrace_buffer_info *info;
  4060. int ret;
  4061. if (tracing_disabled)
  4062. return -ENODEV;
  4063. if (trace_array_get(tr) < 0)
  4064. return -ENODEV;
  4065. info = kzalloc(sizeof(*info), GFP_KERNEL);
  4066. if (!info) {
  4067. trace_array_put(tr);
  4068. return -ENOMEM;
  4069. }
  4070. mutex_lock(&trace_types_lock);
  4071. info->iter.tr = tr;
  4072. info->iter.cpu_file = tracing_get_cpu(inode);
  4073. info->iter.trace = tr->current_trace;
  4074. info->iter.trace_buffer = &tr->trace_buffer;
  4075. info->spare = NULL;
  4076. /* Force reading ring buffer for first read */
  4077. info->read = (unsigned int)-1;
  4078. filp->private_data = info;
  4079. mutex_unlock(&trace_types_lock);
  4080. ret = nonseekable_open(inode, filp);
  4081. if (ret < 0)
  4082. trace_array_put(tr);
  4083. return ret;
  4084. }
  4085. static unsigned int
  4086. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  4087. {
  4088. struct ftrace_buffer_info *info = filp->private_data;
  4089. struct trace_iterator *iter = &info->iter;
  4090. return trace_poll(iter, filp, poll_table);
  4091. }
  4092. static ssize_t
  4093. tracing_buffers_read(struct file *filp, char __user *ubuf,
  4094. size_t count, loff_t *ppos)
  4095. {
  4096. struct ftrace_buffer_info *info = filp->private_data;
  4097. struct trace_iterator *iter = &info->iter;
  4098. ssize_t ret;
  4099. ssize_t size;
  4100. if (!count)
  4101. return 0;
  4102. mutex_lock(&trace_types_lock);
  4103. #ifdef CONFIG_TRACER_MAX_TRACE
  4104. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  4105. size = -EBUSY;
  4106. goto out_unlock;
  4107. }
  4108. #endif
  4109. if (!info->spare)
  4110. info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
  4111. iter->cpu_file);
  4112. size = -ENOMEM;
  4113. if (!info->spare)
  4114. goto out_unlock;
  4115. /* Do we have previous read data to read? */
  4116. if (info->read < PAGE_SIZE)
  4117. goto read;
  4118. again:
  4119. trace_access_lock(iter->cpu_file);
  4120. ret = ring_buffer_read_page(iter->trace_buffer->buffer,
  4121. &info->spare,
  4122. count,
  4123. iter->cpu_file, 0);
  4124. trace_access_unlock(iter->cpu_file);
  4125. if (ret < 0) {
  4126. if (trace_empty(iter)) {
  4127. if ((filp->f_flags & O_NONBLOCK)) {
  4128. size = -EAGAIN;
  4129. goto out_unlock;
  4130. }
  4131. mutex_unlock(&trace_types_lock);
  4132. iter->trace->wait_pipe(iter);
  4133. mutex_lock(&trace_types_lock);
  4134. if (signal_pending(current)) {
  4135. size = -EINTR;
  4136. goto out_unlock;
  4137. }
  4138. goto again;
  4139. }
  4140. size = 0;
  4141. goto out_unlock;
  4142. }
  4143. info->read = 0;
  4144. read:
  4145. size = PAGE_SIZE - info->read;
  4146. if (size > count)
  4147. size = count;
  4148. ret = copy_to_user(ubuf, info->spare + info->read, size);
  4149. if (ret == size) {
  4150. size = -EFAULT;
  4151. goto out_unlock;
  4152. }
  4153. size -= ret;
  4154. *ppos += size;
  4155. info->read += size;
  4156. out_unlock:
  4157. mutex_unlock(&trace_types_lock);
  4158. return size;
  4159. }
  4160. static int tracing_buffers_release(struct inode *inode, struct file *file)
  4161. {
  4162. struct ftrace_buffer_info *info = file->private_data;
  4163. struct trace_iterator *iter = &info->iter;
  4164. mutex_lock(&trace_types_lock);
  4165. __trace_array_put(iter->tr);
  4166. if (info->spare)
  4167. ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
  4168. kfree(info);
  4169. mutex_unlock(&trace_types_lock);
  4170. return 0;
  4171. }
  4172. struct buffer_ref {
  4173. struct ring_buffer *buffer;
  4174. void *page;
  4175. int ref;
  4176. };
  4177. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  4178. struct pipe_buffer *buf)
  4179. {
  4180. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  4181. if (--ref->ref)
  4182. return;
  4183. ring_buffer_free_read_page(ref->buffer, ref->page);
  4184. kfree(ref);
  4185. buf->private = 0;
  4186. }
  4187. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  4188. struct pipe_buffer *buf)
  4189. {
  4190. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  4191. ref->ref++;
  4192. }
  4193. /* Pipe buffer operations for a buffer. */
  4194. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  4195. .can_merge = 0,
  4196. .map = generic_pipe_buf_map,
  4197. .unmap = generic_pipe_buf_unmap,
  4198. .confirm = generic_pipe_buf_confirm,
  4199. .release = buffer_pipe_buf_release,
  4200. .steal = generic_pipe_buf_steal,
  4201. .get = buffer_pipe_buf_get,
  4202. };
  4203. /*
  4204. * Callback from splice_to_pipe(), if we need to release some pages
  4205. * at the end of the spd in case we error'ed out in filling the pipe.
  4206. */
  4207. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  4208. {
  4209. struct buffer_ref *ref =
  4210. (struct buffer_ref *)spd->partial[i].private;
  4211. if (--ref->ref)
  4212. return;
  4213. ring_buffer_free_read_page(ref->buffer, ref->page);
  4214. kfree(ref);
  4215. spd->partial[i].private = 0;
  4216. }
  4217. static ssize_t
  4218. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  4219. struct pipe_inode_info *pipe, size_t len,
  4220. unsigned int flags)
  4221. {
  4222. struct ftrace_buffer_info *info = file->private_data;
  4223. struct trace_iterator *iter = &info->iter;
  4224. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  4225. struct page *pages_def[PIPE_DEF_BUFFERS];
  4226. struct splice_pipe_desc spd = {
  4227. .pages = pages_def,
  4228. .partial = partial_def,
  4229. .nr_pages_max = PIPE_DEF_BUFFERS,
  4230. .flags = flags,
  4231. .ops = &buffer_pipe_buf_ops,
  4232. .spd_release = buffer_spd_release,
  4233. };
  4234. struct buffer_ref *ref;
  4235. int entries, size, i;
  4236. ssize_t ret;
  4237. mutex_lock(&trace_types_lock);
  4238. #ifdef CONFIG_TRACER_MAX_TRACE
  4239. if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
  4240. ret = -EBUSY;
  4241. goto out;
  4242. }
  4243. #endif
  4244. if (splice_grow_spd(pipe, &spd)) {
  4245. ret = -ENOMEM;
  4246. goto out;
  4247. }
  4248. if (*ppos & (PAGE_SIZE - 1)) {
  4249. ret = -EINVAL;
  4250. goto out;
  4251. }
  4252. if (len & (PAGE_SIZE - 1)) {
  4253. if (len < PAGE_SIZE) {
  4254. ret = -EINVAL;
  4255. goto out;
  4256. }
  4257. len &= PAGE_MASK;
  4258. }
  4259. again:
  4260. trace_access_lock(iter->cpu_file);
  4261. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4262. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  4263. struct page *page;
  4264. int r;
  4265. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  4266. if (!ref)
  4267. break;
  4268. ref->ref = 1;
  4269. ref->buffer = iter->trace_buffer->buffer;
  4270. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  4271. if (!ref->page) {
  4272. kfree(ref);
  4273. break;
  4274. }
  4275. r = ring_buffer_read_page(ref->buffer, &ref->page,
  4276. len, iter->cpu_file, 1);
  4277. if (r < 0) {
  4278. ring_buffer_free_read_page(ref->buffer, ref->page);
  4279. kfree(ref);
  4280. break;
  4281. }
  4282. /*
  4283. * zero out any left over data, this is going to
  4284. * user land.
  4285. */
  4286. size = ring_buffer_page_len(ref->page);
  4287. if (size < PAGE_SIZE)
  4288. memset(ref->page + size, 0, PAGE_SIZE - size);
  4289. page = virt_to_page(ref->page);
  4290. spd.pages[i] = page;
  4291. spd.partial[i].len = PAGE_SIZE;
  4292. spd.partial[i].offset = 0;
  4293. spd.partial[i].private = (unsigned long)ref;
  4294. spd.nr_pages++;
  4295. *ppos += PAGE_SIZE;
  4296. entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
  4297. }
  4298. trace_access_unlock(iter->cpu_file);
  4299. spd.nr_pages = i;
  4300. /* did we read anything? */
  4301. if (!spd.nr_pages) {
  4302. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
  4303. ret = -EAGAIN;
  4304. goto out;
  4305. }
  4306. mutex_unlock(&trace_types_lock);
  4307. iter->trace->wait_pipe(iter);
  4308. mutex_lock(&trace_types_lock);
  4309. if (signal_pending(current)) {
  4310. ret = -EINTR;
  4311. goto out;
  4312. }
  4313. goto again;
  4314. }
  4315. ret = splice_to_pipe(pipe, &spd);
  4316. splice_shrink_spd(&spd);
  4317. out:
  4318. mutex_unlock(&trace_types_lock);
  4319. return ret;
  4320. }
  4321. static const struct file_operations tracing_buffers_fops = {
  4322. .open = tracing_buffers_open,
  4323. .read = tracing_buffers_read,
  4324. .poll = tracing_buffers_poll,
  4325. .release = tracing_buffers_release,
  4326. .splice_read = tracing_buffers_splice_read,
  4327. .llseek = no_llseek,
  4328. };
  4329. static ssize_t
  4330. tracing_stats_read(struct file *filp, char __user *ubuf,
  4331. size_t count, loff_t *ppos)
  4332. {
  4333. struct inode *inode = file_inode(filp);
  4334. struct trace_array *tr = inode->i_private;
  4335. struct trace_buffer *trace_buf = &tr->trace_buffer;
  4336. int cpu = tracing_get_cpu(inode);
  4337. struct trace_seq *s;
  4338. unsigned long cnt;
  4339. unsigned long long t;
  4340. unsigned long usec_rem;
  4341. s = kmalloc(sizeof(*s), GFP_KERNEL);
  4342. if (!s)
  4343. return -ENOMEM;
  4344. trace_seq_init(s);
  4345. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  4346. trace_seq_printf(s, "entries: %ld\n", cnt);
  4347. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  4348. trace_seq_printf(s, "overrun: %ld\n", cnt);
  4349. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  4350. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  4351. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  4352. trace_seq_printf(s, "bytes: %ld\n", cnt);
  4353. if (trace_clocks[tr->clock_id].in_ns) {
  4354. /* local or global for trace_clock */
  4355. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4356. usec_rem = do_div(t, USEC_PER_SEC);
  4357. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  4358. t, usec_rem);
  4359. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4360. usec_rem = do_div(t, USEC_PER_SEC);
  4361. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  4362. } else {
  4363. /* counter or tsc mode for trace_clock */
  4364. trace_seq_printf(s, "oldest event ts: %llu\n",
  4365. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  4366. trace_seq_printf(s, "now ts: %llu\n",
  4367. ring_buffer_time_stamp(trace_buf->buffer, cpu));
  4368. }
  4369. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  4370. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  4371. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  4372. trace_seq_printf(s, "read events: %ld\n", cnt);
  4373. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  4374. kfree(s);
  4375. return count;
  4376. }
  4377. static const struct file_operations tracing_stats_fops = {
  4378. .open = tracing_open_generic_tr,
  4379. .read = tracing_stats_read,
  4380. .llseek = generic_file_llseek,
  4381. .release = tracing_release_generic_tr,
  4382. };
  4383. #ifdef CONFIG_DYNAMIC_FTRACE
  4384. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  4385. {
  4386. return 0;
  4387. }
  4388. static ssize_t
  4389. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  4390. size_t cnt, loff_t *ppos)
  4391. {
  4392. static char ftrace_dyn_info_buffer[1024];
  4393. static DEFINE_MUTEX(dyn_info_mutex);
  4394. unsigned long *p = filp->private_data;
  4395. char *buf = ftrace_dyn_info_buffer;
  4396. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  4397. int r;
  4398. mutex_lock(&dyn_info_mutex);
  4399. r = sprintf(buf, "%ld ", *p);
  4400. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  4401. buf[r++] = '\n';
  4402. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4403. mutex_unlock(&dyn_info_mutex);
  4404. return r;
  4405. }
  4406. static const struct file_operations tracing_dyn_info_fops = {
  4407. .open = tracing_open_generic,
  4408. .read = tracing_read_dyn_info,
  4409. .llseek = generic_file_llseek,
  4410. };
  4411. #endif /* CONFIG_DYNAMIC_FTRACE */
  4412. #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
  4413. static void
  4414. ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
  4415. {
  4416. tracing_snapshot();
  4417. }
  4418. static void
  4419. ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
  4420. {
  4421. unsigned long *count = (long *)data;
  4422. if (!*count)
  4423. return;
  4424. if (*count != -1)
  4425. (*count)--;
  4426. tracing_snapshot();
  4427. }
  4428. static int
  4429. ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
  4430. struct ftrace_probe_ops *ops, void *data)
  4431. {
  4432. long count = (long)data;
  4433. seq_printf(m, "%ps:", (void *)ip);
  4434. seq_printf(m, "snapshot");
  4435. if (count == -1)
  4436. seq_printf(m, ":unlimited\n");
  4437. else
  4438. seq_printf(m, ":count=%ld\n", count);
  4439. return 0;
  4440. }
  4441. static struct ftrace_probe_ops snapshot_probe_ops = {
  4442. .func = ftrace_snapshot,
  4443. .print = ftrace_snapshot_print,
  4444. };
  4445. static struct ftrace_probe_ops snapshot_count_probe_ops = {
  4446. .func = ftrace_count_snapshot,
  4447. .print = ftrace_snapshot_print,
  4448. };
  4449. static int
  4450. ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
  4451. char *glob, char *cmd, char *param, int enable)
  4452. {
  4453. struct ftrace_probe_ops *ops;
  4454. void *count = (void *)-1;
  4455. char *number;
  4456. int ret;
  4457. /* hash funcs only work with set_ftrace_filter */
  4458. if (!enable)
  4459. return -EINVAL;
  4460. ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
  4461. if (glob[0] == '!') {
  4462. unregister_ftrace_function_probe_func(glob+1, ops);
  4463. return 0;
  4464. }
  4465. if (!param)
  4466. goto out_reg;
  4467. number = strsep(&param, ":");
  4468. if (!strlen(number))
  4469. goto out_reg;
  4470. /*
  4471. * We use the callback data field (which is a pointer)
  4472. * as our counter.
  4473. */
  4474. ret = kstrtoul(number, 0, (unsigned long *)&count);
  4475. if (ret)
  4476. return ret;
  4477. out_reg:
  4478. ret = register_ftrace_function_probe(glob, ops, count);
  4479. if (ret >= 0)
  4480. alloc_snapshot(&global_trace);
  4481. return ret < 0 ? ret : 0;
  4482. }
  4483. static struct ftrace_func_command ftrace_snapshot_cmd = {
  4484. .name = "snapshot",
  4485. .func = ftrace_trace_snapshot_callback,
  4486. };
  4487. static int register_snapshot_cmd(void)
  4488. {
  4489. return register_ftrace_command(&ftrace_snapshot_cmd);
  4490. }
  4491. #else
  4492. static inline int register_snapshot_cmd(void) { return 0; }
  4493. #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
  4494. struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
  4495. {
  4496. if (tr->dir)
  4497. return tr->dir;
  4498. if (!debugfs_initialized())
  4499. return NULL;
  4500. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  4501. tr->dir = debugfs_create_dir("tracing", NULL);
  4502. if (!tr->dir)
  4503. pr_warn_once("Could not create debugfs directory 'tracing'\n");
  4504. return tr->dir;
  4505. }
  4506. struct dentry *tracing_init_dentry(void)
  4507. {
  4508. return tracing_init_dentry_tr(&global_trace);
  4509. }
  4510. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  4511. {
  4512. struct dentry *d_tracer;
  4513. if (tr->percpu_dir)
  4514. return tr->percpu_dir;
  4515. d_tracer = tracing_init_dentry_tr(tr);
  4516. if (!d_tracer)
  4517. return NULL;
  4518. tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
  4519. WARN_ONCE(!tr->percpu_dir,
  4520. "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
  4521. return tr->percpu_dir;
  4522. }
  4523. static struct dentry *
  4524. trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
  4525. void *data, long cpu, const struct file_operations *fops)
  4526. {
  4527. struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
  4528. if (ret) /* See tracing_get_cpu() */
  4529. ret->d_inode->i_cdev = (void *)(cpu + 1);
  4530. return ret;
  4531. }
  4532. static void
  4533. tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
  4534. {
  4535. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  4536. struct dentry *d_cpu;
  4537. char cpu_dir[30]; /* 30 characters should be more than enough */
  4538. if (!d_percpu)
  4539. return;
  4540. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  4541. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  4542. if (!d_cpu) {
  4543. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  4544. return;
  4545. }
  4546. /* per cpu trace_pipe */
  4547. trace_create_cpu_file("trace_pipe", 0444, d_cpu,
  4548. tr, cpu, &tracing_pipe_fops);
  4549. /* per cpu trace */
  4550. trace_create_cpu_file("trace", 0644, d_cpu,
  4551. tr, cpu, &tracing_fops);
  4552. trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
  4553. tr, cpu, &tracing_buffers_fops);
  4554. trace_create_cpu_file("stats", 0444, d_cpu,
  4555. tr, cpu, &tracing_stats_fops);
  4556. trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
  4557. tr, cpu, &tracing_entries_fops);
  4558. #ifdef CONFIG_TRACER_SNAPSHOT
  4559. trace_create_cpu_file("snapshot", 0644, d_cpu,
  4560. tr, cpu, &snapshot_fops);
  4561. trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
  4562. tr, cpu, &snapshot_raw_fops);
  4563. #endif
  4564. }
  4565. #ifdef CONFIG_FTRACE_SELFTEST
  4566. /* Let selftest have access to static functions in this file */
  4567. #include "trace_selftest.c"
  4568. #endif
  4569. struct trace_option_dentry {
  4570. struct tracer_opt *opt;
  4571. struct tracer_flags *flags;
  4572. struct trace_array *tr;
  4573. struct dentry *entry;
  4574. };
  4575. static ssize_t
  4576. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  4577. loff_t *ppos)
  4578. {
  4579. struct trace_option_dentry *topt = filp->private_data;
  4580. char *buf;
  4581. if (topt->flags->val & topt->opt->bit)
  4582. buf = "1\n";
  4583. else
  4584. buf = "0\n";
  4585. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4586. }
  4587. static ssize_t
  4588. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4589. loff_t *ppos)
  4590. {
  4591. struct trace_option_dentry *topt = filp->private_data;
  4592. unsigned long val;
  4593. int ret;
  4594. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4595. if (ret)
  4596. return ret;
  4597. if (val != 0 && val != 1)
  4598. return -EINVAL;
  4599. if (!!(topt->flags->val & topt->opt->bit) != val) {
  4600. mutex_lock(&trace_types_lock);
  4601. ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
  4602. topt->opt, !val);
  4603. mutex_unlock(&trace_types_lock);
  4604. if (ret)
  4605. return ret;
  4606. }
  4607. *ppos += cnt;
  4608. return cnt;
  4609. }
  4610. static const struct file_operations trace_options_fops = {
  4611. .open = tracing_open_generic,
  4612. .read = trace_options_read,
  4613. .write = trace_options_write,
  4614. .llseek = generic_file_llseek,
  4615. };
  4616. static ssize_t
  4617. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  4618. loff_t *ppos)
  4619. {
  4620. long index = (long)filp->private_data;
  4621. char *buf;
  4622. if (trace_flags & (1 << index))
  4623. buf = "1\n";
  4624. else
  4625. buf = "0\n";
  4626. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  4627. }
  4628. static ssize_t
  4629. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  4630. loff_t *ppos)
  4631. {
  4632. struct trace_array *tr = &global_trace;
  4633. long index = (long)filp->private_data;
  4634. unsigned long val;
  4635. int ret;
  4636. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4637. if (ret)
  4638. return ret;
  4639. if (val != 0 && val != 1)
  4640. return -EINVAL;
  4641. mutex_lock(&trace_types_lock);
  4642. ret = set_tracer_flag(tr, 1 << index, val);
  4643. mutex_unlock(&trace_types_lock);
  4644. if (ret < 0)
  4645. return ret;
  4646. *ppos += cnt;
  4647. return cnt;
  4648. }
  4649. static const struct file_operations trace_options_core_fops = {
  4650. .open = tracing_open_generic,
  4651. .read = trace_options_core_read,
  4652. .write = trace_options_core_write,
  4653. .llseek = generic_file_llseek,
  4654. };
  4655. struct dentry *trace_create_file(const char *name,
  4656. umode_t mode,
  4657. struct dentry *parent,
  4658. void *data,
  4659. const struct file_operations *fops)
  4660. {
  4661. struct dentry *ret;
  4662. ret = debugfs_create_file(name, mode, parent, data, fops);
  4663. if (!ret)
  4664. pr_warning("Could not create debugfs '%s' entry\n", name);
  4665. return ret;
  4666. }
  4667. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  4668. {
  4669. struct dentry *d_tracer;
  4670. if (tr->options)
  4671. return tr->options;
  4672. d_tracer = tracing_init_dentry_tr(tr);
  4673. if (!d_tracer)
  4674. return NULL;
  4675. tr->options = debugfs_create_dir("options", d_tracer);
  4676. if (!tr->options) {
  4677. pr_warning("Could not create debugfs directory 'options'\n");
  4678. return NULL;
  4679. }
  4680. return tr->options;
  4681. }
  4682. static void
  4683. create_trace_option_file(struct trace_array *tr,
  4684. struct trace_option_dentry *topt,
  4685. struct tracer_flags *flags,
  4686. struct tracer_opt *opt)
  4687. {
  4688. struct dentry *t_options;
  4689. t_options = trace_options_init_dentry(tr);
  4690. if (!t_options)
  4691. return;
  4692. topt->flags = flags;
  4693. topt->opt = opt;
  4694. topt->tr = tr;
  4695. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  4696. &trace_options_fops);
  4697. }
  4698. static struct trace_option_dentry *
  4699. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  4700. {
  4701. struct trace_option_dentry *topts;
  4702. struct tracer_flags *flags;
  4703. struct tracer_opt *opts;
  4704. int cnt;
  4705. if (!tracer)
  4706. return NULL;
  4707. flags = tracer->flags;
  4708. if (!flags || !flags->opts)
  4709. return NULL;
  4710. opts = flags->opts;
  4711. for (cnt = 0; opts[cnt].name; cnt++)
  4712. ;
  4713. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  4714. if (!topts)
  4715. return NULL;
  4716. for (cnt = 0; opts[cnt].name; cnt++)
  4717. create_trace_option_file(tr, &topts[cnt], flags,
  4718. &opts[cnt]);
  4719. return topts;
  4720. }
  4721. static void
  4722. destroy_trace_option_files(struct trace_option_dentry *topts)
  4723. {
  4724. int cnt;
  4725. if (!topts)
  4726. return;
  4727. for (cnt = 0; topts[cnt].opt; cnt++) {
  4728. if (topts[cnt].entry)
  4729. debugfs_remove(topts[cnt].entry);
  4730. }
  4731. kfree(topts);
  4732. }
  4733. static struct dentry *
  4734. create_trace_option_core_file(struct trace_array *tr,
  4735. const char *option, long index)
  4736. {
  4737. struct dentry *t_options;
  4738. t_options = trace_options_init_dentry(tr);
  4739. if (!t_options)
  4740. return NULL;
  4741. return trace_create_file(option, 0644, t_options, (void *)index,
  4742. &trace_options_core_fops);
  4743. }
  4744. static __init void create_trace_options_dir(struct trace_array *tr)
  4745. {
  4746. struct dentry *t_options;
  4747. int i;
  4748. t_options = trace_options_init_dentry(tr);
  4749. if (!t_options)
  4750. return;
  4751. for (i = 0; trace_options[i]; i++)
  4752. create_trace_option_core_file(tr, trace_options[i], i);
  4753. }
  4754. static ssize_t
  4755. rb_simple_read(struct file *filp, char __user *ubuf,
  4756. size_t cnt, loff_t *ppos)
  4757. {
  4758. struct trace_array *tr = filp->private_data;
  4759. char buf[64];
  4760. int r;
  4761. r = tracer_tracing_is_on(tr);
  4762. r = sprintf(buf, "%d\n", r);
  4763. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4764. }
  4765. static ssize_t
  4766. rb_simple_write(struct file *filp, const char __user *ubuf,
  4767. size_t cnt, loff_t *ppos)
  4768. {
  4769. struct trace_array *tr = filp->private_data;
  4770. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  4771. unsigned long val;
  4772. int ret;
  4773. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  4774. if (ret)
  4775. return ret;
  4776. if (buffer) {
  4777. mutex_lock(&trace_types_lock);
  4778. if (val) {
  4779. tracer_tracing_on(tr);
  4780. if (tr->current_trace->start)
  4781. tr->current_trace->start(tr);
  4782. } else {
  4783. tracer_tracing_off(tr);
  4784. if (tr->current_trace->stop)
  4785. tr->current_trace->stop(tr);
  4786. }
  4787. mutex_unlock(&trace_types_lock);
  4788. }
  4789. (*ppos)++;
  4790. return cnt;
  4791. }
  4792. static const struct file_operations rb_simple_fops = {
  4793. .open = tracing_open_generic_tr,
  4794. .read = rb_simple_read,
  4795. .write = rb_simple_write,
  4796. .release = tracing_release_generic_tr,
  4797. .llseek = default_llseek,
  4798. };
  4799. struct dentry *trace_instance_dir;
  4800. static void
  4801. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
  4802. static int
  4803. allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
  4804. {
  4805. enum ring_buffer_flags rb_flags;
  4806. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4807. buf->buffer = ring_buffer_alloc(size, rb_flags);
  4808. if (!buf->buffer)
  4809. return -ENOMEM;
  4810. buf->data = alloc_percpu(struct trace_array_cpu);
  4811. if (!buf->data) {
  4812. ring_buffer_free(buf->buffer);
  4813. return -ENOMEM;
  4814. }
  4815. /* Allocate the first page for all buffers */
  4816. set_buffer_entries(&tr->trace_buffer,
  4817. ring_buffer_size(tr->trace_buffer.buffer, 0));
  4818. return 0;
  4819. }
  4820. static int allocate_trace_buffers(struct trace_array *tr, int size)
  4821. {
  4822. int ret;
  4823. ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
  4824. if (ret)
  4825. return ret;
  4826. #ifdef CONFIG_TRACER_MAX_TRACE
  4827. ret = allocate_trace_buffer(tr, &tr->max_buffer,
  4828. allocate_snapshot ? size : 1);
  4829. if (WARN_ON(ret)) {
  4830. ring_buffer_free(tr->trace_buffer.buffer);
  4831. free_percpu(tr->trace_buffer.data);
  4832. return -ENOMEM;
  4833. }
  4834. tr->allocated_snapshot = allocate_snapshot;
  4835. /*
  4836. * Only the top level trace array gets its snapshot allocated
  4837. * from the kernel command line.
  4838. */
  4839. allocate_snapshot = false;
  4840. #endif
  4841. return 0;
  4842. }
  4843. static int new_instance_create(const char *name)
  4844. {
  4845. struct trace_array *tr;
  4846. int ret;
  4847. mutex_lock(&trace_types_lock);
  4848. ret = -EEXIST;
  4849. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4850. if (tr->name && strcmp(tr->name, name) == 0)
  4851. goto out_unlock;
  4852. }
  4853. ret = -ENOMEM;
  4854. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  4855. if (!tr)
  4856. goto out_unlock;
  4857. tr->name = kstrdup(name, GFP_KERNEL);
  4858. if (!tr->name)
  4859. goto out_free_tr;
  4860. if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
  4861. goto out_free_tr;
  4862. cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
  4863. raw_spin_lock_init(&tr->start_lock);
  4864. tr->current_trace = &nop_trace;
  4865. INIT_LIST_HEAD(&tr->systems);
  4866. INIT_LIST_HEAD(&tr->events);
  4867. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  4868. goto out_free_tr;
  4869. tr->dir = debugfs_create_dir(name, trace_instance_dir);
  4870. if (!tr->dir)
  4871. goto out_free_tr;
  4872. ret = event_trace_add_tracer(tr->dir, tr);
  4873. if (ret) {
  4874. debugfs_remove_recursive(tr->dir);
  4875. goto out_free_tr;
  4876. }
  4877. init_tracer_debugfs(tr, tr->dir);
  4878. list_add(&tr->list, &ftrace_trace_arrays);
  4879. mutex_unlock(&trace_types_lock);
  4880. return 0;
  4881. out_free_tr:
  4882. if (tr->trace_buffer.buffer)
  4883. ring_buffer_free(tr->trace_buffer.buffer);
  4884. free_cpumask_var(tr->tracing_cpumask);
  4885. kfree(tr->name);
  4886. kfree(tr);
  4887. out_unlock:
  4888. mutex_unlock(&trace_types_lock);
  4889. return ret;
  4890. }
  4891. static int instance_delete(const char *name)
  4892. {
  4893. struct trace_array *tr;
  4894. int found = 0;
  4895. int ret;
  4896. mutex_lock(&trace_types_lock);
  4897. ret = -ENODEV;
  4898. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  4899. if (tr->name && strcmp(tr->name, name) == 0) {
  4900. found = 1;
  4901. break;
  4902. }
  4903. }
  4904. if (!found)
  4905. goto out_unlock;
  4906. ret = -EBUSY;
  4907. if (tr->ref)
  4908. goto out_unlock;
  4909. list_del(&tr->list);
  4910. event_trace_del_tracer(tr);
  4911. debugfs_remove_recursive(tr->dir);
  4912. free_percpu(tr->trace_buffer.data);
  4913. ring_buffer_free(tr->trace_buffer.buffer);
  4914. kfree(tr->name);
  4915. kfree(tr);
  4916. ret = 0;
  4917. out_unlock:
  4918. mutex_unlock(&trace_types_lock);
  4919. return ret;
  4920. }
  4921. static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
  4922. {
  4923. struct dentry *parent;
  4924. int ret;
  4925. /* Paranoid: Make sure the parent is the "instances" directory */
  4926. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4927. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4928. return -ENOENT;
  4929. /*
  4930. * The inode mutex is locked, but debugfs_create_dir() will also
  4931. * take the mutex. As the instances directory can not be destroyed
  4932. * or changed in any other way, it is safe to unlock it, and
  4933. * let the dentry try. If two users try to make the same dir at
  4934. * the same time, then the new_instance_create() will determine the
  4935. * winner.
  4936. */
  4937. mutex_unlock(&inode->i_mutex);
  4938. ret = new_instance_create(dentry->d_iname);
  4939. mutex_lock(&inode->i_mutex);
  4940. return ret;
  4941. }
  4942. static int instance_rmdir(struct inode *inode, struct dentry *dentry)
  4943. {
  4944. struct dentry *parent;
  4945. int ret;
  4946. /* Paranoid: Make sure the parent is the "instances" directory */
  4947. parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
  4948. if (WARN_ON_ONCE(parent != trace_instance_dir))
  4949. return -ENOENT;
  4950. /* The caller did a dget() on dentry */
  4951. mutex_unlock(&dentry->d_inode->i_mutex);
  4952. /*
  4953. * The inode mutex is locked, but debugfs_create_dir() will also
  4954. * take the mutex. As the instances directory can not be destroyed
  4955. * or changed in any other way, it is safe to unlock it, and
  4956. * let the dentry try. If two users try to make the same dir at
  4957. * the same time, then the instance_delete() will determine the
  4958. * winner.
  4959. */
  4960. mutex_unlock(&inode->i_mutex);
  4961. ret = instance_delete(dentry->d_iname);
  4962. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  4963. mutex_lock(&dentry->d_inode->i_mutex);
  4964. return ret;
  4965. }
  4966. static const struct inode_operations instance_dir_inode_operations = {
  4967. .lookup = simple_lookup,
  4968. .mkdir = instance_mkdir,
  4969. .rmdir = instance_rmdir,
  4970. };
  4971. static __init void create_trace_instances(struct dentry *d_tracer)
  4972. {
  4973. trace_instance_dir = debugfs_create_dir("instances", d_tracer);
  4974. if (WARN_ON(!trace_instance_dir))
  4975. return;
  4976. /* Hijack the dir inode operations, to allow mkdir */
  4977. trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
  4978. }
  4979. static void
  4980. init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
  4981. {
  4982. int cpu;
  4983. trace_create_file("tracing_cpumask", 0644, d_tracer,
  4984. tr, &tracing_cpumask_fops);
  4985. trace_create_file("trace_options", 0644, d_tracer,
  4986. tr, &tracing_iter_fops);
  4987. trace_create_file("trace", 0644, d_tracer,
  4988. tr, &tracing_fops);
  4989. trace_create_file("trace_pipe", 0444, d_tracer,
  4990. tr, &tracing_pipe_fops);
  4991. trace_create_file("buffer_size_kb", 0644, d_tracer,
  4992. tr, &tracing_entries_fops);
  4993. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  4994. tr, &tracing_total_entries_fops);
  4995. trace_create_file("free_buffer", 0200, d_tracer,
  4996. tr, &tracing_free_buffer_fops);
  4997. trace_create_file("trace_marker", 0220, d_tracer,
  4998. tr, &tracing_mark_fops);
  4999. trace_create_file("trace_clock", 0644, d_tracer, tr,
  5000. &trace_clock_fops);
  5001. trace_create_file("tracing_on", 0644, d_tracer,
  5002. tr, &rb_simple_fops);
  5003. #ifdef CONFIG_TRACER_SNAPSHOT
  5004. trace_create_file("snapshot", 0644, d_tracer,
  5005. tr, &snapshot_fops);
  5006. #endif
  5007. for_each_tracing_cpu(cpu)
  5008. tracing_init_debugfs_percpu(tr, cpu);
  5009. }
  5010. static __init int tracer_init_debugfs(void)
  5011. {
  5012. struct dentry *d_tracer;
  5013. trace_access_lock_init();
  5014. d_tracer = tracing_init_dentry();
  5015. if (!d_tracer)
  5016. return 0;
  5017. init_tracer_debugfs(&global_trace, d_tracer);
  5018. trace_create_file("available_tracers", 0444, d_tracer,
  5019. &global_trace, &show_traces_fops);
  5020. trace_create_file("current_tracer", 0644, d_tracer,
  5021. &global_trace, &set_tracer_fops);
  5022. #ifdef CONFIG_TRACER_MAX_TRACE
  5023. trace_create_file("tracing_max_latency", 0644, d_tracer,
  5024. &tracing_max_latency, &tracing_max_lat_fops);
  5025. #endif
  5026. trace_create_file("tracing_thresh", 0644, d_tracer,
  5027. &tracing_thresh, &tracing_max_lat_fops);
  5028. trace_create_file("README", 0444, d_tracer,
  5029. NULL, &tracing_readme_fops);
  5030. trace_create_file("saved_cmdlines", 0444, d_tracer,
  5031. NULL, &tracing_saved_cmdlines_fops);
  5032. #ifdef CONFIG_DYNAMIC_FTRACE
  5033. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  5034. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  5035. #endif
  5036. create_trace_instances(d_tracer);
  5037. create_trace_options_dir(&global_trace);
  5038. return 0;
  5039. }
  5040. static int trace_panic_handler(struct notifier_block *this,
  5041. unsigned long event, void *unused)
  5042. {
  5043. if (ftrace_dump_on_oops)
  5044. ftrace_dump(ftrace_dump_on_oops);
  5045. return NOTIFY_OK;
  5046. }
  5047. static struct notifier_block trace_panic_notifier = {
  5048. .notifier_call = trace_panic_handler,
  5049. .next = NULL,
  5050. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  5051. };
  5052. static int trace_die_handler(struct notifier_block *self,
  5053. unsigned long val,
  5054. void *data)
  5055. {
  5056. switch (val) {
  5057. case DIE_OOPS:
  5058. if (ftrace_dump_on_oops)
  5059. ftrace_dump(ftrace_dump_on_oops);
  5060. break;
  5061. default:
  5062. break;
  5063. }
  5064. return NOTIFY_OK;
  5065. }
  5066. static struct notifier_block trace_die_notifier = {
  5067. .notifier_call = trace_die_handler,
  5068. .priority = 200
  5069. };
  5070. /*
  5071. * printk is set to max of 1024, we really don't need it that big.
  5072. * Nothing should be printing 1000 characters anyway.
  5073. */
  5074. #define TRACE_MAX_PRINT 1000
  5075. /*
  5076. * Define here KERN_TRACE so that we have one place to modify
  5077. * it if we decide to change what log level the ftrace dump
  5078. * should be at.
  5079. */
  5080. #define KERN_TRACE KERN_EMERG
  5081. void
  5082. trace_printk_seq(struct trace_seq *s)
  5083. {
  5084. /* Probably should print a warning here. */
  5085. if (s->len >= TRACE_MAX_PRINT)
  5086. s->len = TRACE_MAX_PRINT;
  5087. /* should be zero ended, but we are paranoid. */
  5088. s->buffer[s->len] = 0;
  5089. printk(KERN_TRACE "%s", s->buffer);
  5090. trace_seq_init(s);
  5091. }
  5092. void trace_init_global_iter(struct trace_iterator *iter)
  5093. {
  5094. iter->tr = &global_trace;
  5095. iter->trace = iter->tr->current_trace;
  5096. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  5097. iter->trace_buffer = &global_trace.trace_buffer;
  5098. }
  5099. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  5100. {
  5101. /* use static because iter can be a bit big for the stack */
  5102. static struct trace_iterator iter;
  5103. static atomic_t dump_running;
  5104. unsigned int old_userobj;
  5105. unsigned long flags;
  5106. int cnt = 0, cpu;
  5107. /* Only allow one dump user at a time. */
  5108. if (atomic_inc_return(&dump_running) != 1) {
  5109. atomic_dec(&dump_running);
  5110. return;
  5111. }
  5112. /*
  5113. * Always turn off tracing when we dump.
  5114. * We don't need to show trace output of what happens
  5115. * between multiple crashes.
  5116. *
  5117. * If the user does a sysrq-z, then they can re-enable
  5118. * tracing with echo 1 > tracing_on.
  5119. */
  5120. tracing_off();
  5121. local_irq_save(flags);
  5122. /* Simulate the iterator */
  5123. trace_init_global_iter(&iter);
  5124. for_each_tracing_cpu(cpu) {
  5125. atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
  5126. }
  5127. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  5128. /* don't look at user memory in panic mode */
  5129. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  5130. switch (oops_dump_mode) {
  5131. case DUMP_ALL:
  5132. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  5133. break;
  5134. case DUMP_ORIG:
  5135. iter.cpu_file = raw_smp_processor_id();
  5136. break;
  5137. case DUMP_NONE:
  5138. goto out_enable;
  5139. default:
  5140. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  5141. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  5142. }
  5143. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  5144. /* Did function tracer already get disabled? */
  5145. if (ftrace_is_dead()) {
  5146. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  5147. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  5148. }
  5149. /*
  5150. * We need to stop all tracing on all CPUS to read the
  5151. * the next buffer. This is a bit expensive, but is
  5152. * not done often. We fill all what we can read,
  5153. * and then release the locks again.
  5154. */
  5155. while (!trace_empty(&iter)) {
  5156. if (!cnt)
  5157. printk(KERN_TRACE "---------------------------------\n");
  5158. cnt++;
  5159. /* reset all but tr, trace, and overruns */
  5160. memset(&iter.seq, 0,
  5161. sizeof(struct trace_iterator) -
  5162. offsetof(struct trace_iterator, seq));
  5163. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  5164. iter.pos = -1;
  5165. if (trace_find_next_entry_inc(&iter) != NULL) {
  5166. int ret;
  5167. ret = print_trace_line(&iter);
  5168. if (ret != TRACE_TYPE_NO_CONSUME)
  5169. trace_consume(&iter);
  5170. }
  5171. touch_nmi_watchdog();
  5172. trace_printk_seq(&iter.seq);
  5173. }
  5174. if (!cnt)
  5175. printk(KERN_TRACE " (ftrace buffer empty)\n");
  5176. else
  5177. printk(KERN_TRACE "---------------------------------\n");
  5178. out_enable:
  5179. trace_flags |= old_userobj;
  5180. for_each_tracing_cpu(cpu) {
  5181. atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
  5182. }
  5183. atomic_dec(&dump_running);
  5184. local_irq_restore(flags);
  5185. }
  5186. EXPORT_SYMBOL_GPL(ftrace_dump);
  5187. __init static int tracer_alloc_buffers(void)
  5188. {
  5189. int ring_buf_size;
  5190. int ret = -ENOMEM;
  5191. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  5192. goto out;
  5193. if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
  5194. goto out_free_buffer_mask;
  5195. /* Only allocate trace_printk buffers if a trace_printk exists */
  5196. if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
  5197. /* Must be called before global_trace.buffer is allocated */
  5198. trace_printk_init_buffers();
  5199. /* To save memory, keep the ring buffer size to its minimum */
  5200. if (ring_buffer_expanded)
  5201. ring_buf_size = trace_buf_size;
  5202. else
  5203. ring_buf_size = 1;
  5204. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  5205. cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
  5206. raw_spin_lock_init(&global_trace.start_lock);
  5207. /* TODO: make the number of buffers hot pluggable with CPUS */
  5208. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  5209. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  5210. WARN_ON(1);
  5211. goto out_free_cpumask;
  5212. }
  5213. if (global_trace.buffer_disabled)
  5214. tracing_off();
  5215. trace_init_cmdlines();
  5216. /*
  5217. * register_tracer() might reference current_trace, so it
  5218. * needs to be set before we register anything. This is
  5219. * just a bootstrap of current_trace anyway.
  5220. */
  5221. global_trace.current_trace = &nop_trace;
  5222. register_tracer(&nop_trace);
  5223. /* All seems OK, enable tracing */
  5224. tracing_disabled = 0;
  5225. atomic_notifier_chain_register(&panic_notifier_list,
  5226. &trace_panic_notifier);
  5227. register_die_notifier(&trace_die_notifier);
  5228. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  5229. INIT_LIST_HEAD(&global_trace.systems);
  5230. INIT_LIST_HEAD(&global_trace.events);
  5231. list_add(&global_trace.list, &ftrace_trace_arrays);
  5232. while (trace_boot_options) {
  5233. char *option;
  5234. option = strsep(&trace_boot_options, ",");
  5235. trace_set_options(&global_trace, option);
  5236. }
  5237. register_snapshot_cmd();
  5238. return 0;
  5239. out_free_cpumask:
  5240. free_percpu(global_trace.trace_buffer.data);
  5241. #ifdef CONFIG_TRACER_MAX_TRACE
  5242. free_percpu(global_trace.max_buffer.data);
  5243. #endif
  5244. free_cpumask_var(global_trace.tracing_cpumask);
  5245. out_free_buffer_mask:
  5246. free_cpumask_var(tracing_buffer_mask);
  5247. out:
  5248. return ret;
  5249. }
  5250. __init static int clear_boot_tracer(void)
  5251. {
  5252. /*
  5253. * The default tracer at boot buffer is an init section.
  5254. * This function is called in lateinit. If we did not
  5255. * find the boot tracer, then clear it out, to prevent
  5256. * later registration from accessing the buffer that is
  5257. * about to be freed.
  5258. */
  5259. if (!default_bootup_tracer)
  5260. return 0;
  5261. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  5262. default_bootup_tracer);
  5263. default_bootup_tracer = NULL;
  5264. return 0;
  5265. }
  5266. early_initcall(tracer_alloc_buffers);
  5267. fs_initcall(tracer_init_debugfs);
  5268. late_initcall(clear_boot_tracer);