trace.c 149 KB

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