trace.c 143 KB

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