trace.c 117 KB

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