trace.c 118 KB

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