trace.c 119 KB

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