trace.c 124 KB

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