trace.c 153 KB

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