trace.c 153 KB

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