trace.c 120 KB

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