trace.c 128 KB

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