trace.c 122 KB

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