trace.c 119 KB

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