trace.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314
  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 <linux/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/ctype.h>
  35. #include <linux/init.h>
  36. #include <linux/poll.h>
  37. #include <linux/gfp.h>
  38. #include <linux/fs.h>
  39. #include "trace.h"
  40. #include "trace_output.h"
  41. #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
  42. unsigned long __read_mostly tracing_max_latency;
  43. unsigned long __read_mostly tracing_thresh;
  44. /*
  45. * On boot up, the ring buffer is set to the minimum size, so that
  46. * we do not waste memory on systems that are not using tracing.
  47. */
  48. static int ring_buffer_expanded;
  49. /*
  50. * We need to change this state when a selftest is running.
  51. * A selftest will lurk into the ring-buffer to count the
  52. * entries inserted during the selftest although some concurrent
  53. * insertions into the ring-buffer such as trace_printk could occurred
  54. * at the same time, giving false positive or negative results.
  55. */
  56. static bool __read_mostly tracing_selftest_running;
  57. /*
  58. * If a tracer is running, we do not want to run SELFTEST.
  59. */
  60. static bool __read_mostly tracing_selftest_disabled;
  61. /* For tracers that don't implement custom flags */
  62. static struct tracer_opt dummy_tracer_opt[] = {
  63. { }
  64. };
  65. static struct tracer_flags dummy_tracer_flags = {
  66. .val = 0,
  67. .opts = dummy_tracer_opt
  68. };
  69. static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * Kill all tracing for good (never come back).
  75. * It is initialized to 1 but will turn to zero if the initialization
  76. * of the tracer is successful. But that is the only place that sets
  77. * this back to zero.
  78. */
  79. static int tracing_disabled = 1;
  80. static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
  81. static inline void ftrace_disable_cpu(void)
  82. {
  83. preempt_disable();
  84. local_inc(&__get_cpu_var(ftrace_cpu_disabled));
  85. }
  86. static inline void ftrace_enable_cpu(void)
  87. {
  88. local_dec(&__get_cpu_var(ftrace_cpu_disabled));
  89. preempt_enable();
  90. }
  91. static cpumask_var_t __read_mostly tracing_buffer_mask;
  92. /* Define which cpu buffers are currently read in trace_pipe */
  93. static cpumask_var_t tracing_reader_cpumask;
  94. #define for_each_tracing_cpu(cpu) \
  95. for_each_cpu(cpu, tracing_buffer_mask)
  96. /*
  97. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  98. *
  99. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  100. * is set, then ftrace_dump is called. This will output the contents
  101. * of the ftrace buffers to the console. This is very useful for
  102. * capturing traces that lead to crashes and outputing it to a
  103. * serial console.
  104. *
  105. * It is default off, but you can enable it with either specifying
  106. * "ftrace_dump_on_oops" in the kernel command line, or setting
  107. * /proc/sys/kernel/ftrace_dump_on_oops to true.
  108. */
  109. int ftrace_dump_on_oops;
  110. static int tracing_set_tracer(const char *buf);
  111. #define BOOTUP_TRACER_SIZE 100
  112. static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
  113. static char *default_bootup_tracer;
  114. static int __init set_ftrace(char *str)
  115. {
  116. strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
  117. default_bootup_tracer = bootup_tracer_buf;
  118. /* We are using ftrace early, expand it */
  119. ring_buffer_expanded = 1;
  120. return 1;
  121. }
  122. __setup("ftrace=", set_ftrace);
  123. static int __init set_ftrace_dump_on_oops(char *str)
  124. {
  125. ftrace_dump_on_oops = 1;
  126. return 1;
  127. }
  128. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  129. unsigned long long ns2usecs(cycle_t nsec)
  130. {
  131. nsec += 500;
  132. do_div(nsec, 1000);
  133. return nsec;
  134. }
  135. /*
  136. * The global_trace is the descriptor that holds the tracing
  137. * buffers for the live tracing. For each CPU, it contains
  138. * a link list of pages that will store trace entries. The
  139. * page descriptor of the pages in the memory is used to hold
  140. * the link list by linking the lru item in the page descriptor
  141. * to each of the pages in the buffer per CPU.
  142. *
  143. * For each active CPU there is a data field that holds the
  144. * pages for the buffer for that CPU. Each CPU has the same number
  145. * of pages allocated for its buffer.
  146. */
  147. static struct trace_array global_trace;
  148. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  149. int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
  150. struct ring_buffer_event *event)
  151. {
  152. return filter_check_discard(call, rec, global_trace.buffer, event);
  153. }
  154. EXPORT_SYMBOL_GPL(filter_current_check_discard);
  155. cycle_t ftrace_now(int cpu)
  156. {
  157. u64 ts;
  158. /* Early boot up does not have a buffer yet */
  159. if (!global_trace.buffer)
  160. return trace_clock_local();
  161. ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
  162. ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
  163. return ts;
  164. }
  165. /*
  166. * The max_tr is used to snapshot the global_trace when a maximum
  167. * latency is reached. Some tracers will use this to store a maximum
  168. * trace while it continues examining live traces.
  169. *
  170. * The buffers for the max_tr are set up the same as the global_trace.
  171. * When a snapshot is taken, the link list of the max_tr is swapped
  172. * with the link list of the global_trace and the buffers are reset for
  173. * the global_trace so the tracing can continue.
  174. */
  175. static struct trace_array max_tr;
  176. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  177. /* tracer_enabled is used to toggle activation of a tracer */
  178. static int tracer_enabled = 1;
  179. /**
  180. * tracing_is_enabled - return tracer_enabled status
  181. *
  182. * This function is used by other tracers to know the status
  183. * of the tracer_enabled flag. Tracers may use this function
  184. * to know if it should enable their features when starting
  185. * up. See irqsoff tracer for an example (start_irqsoff_tracer).
  186. */
  187. int tracing_is_enabled(void)
  188. {
  189. return tracer_enabled;
  190. }
  191. /*
  192. * trace_buf_size is the size in bytes that is allocated
  193. * for a buffer. Note, the number of bytes is always rounded
  194. * to page size.
  195. *
  196. * This number is purposely set to a low number of 16384.
  197. * If the dump on oops happens, it will be much appreciated
  198. * to not have to wait for all that output. Anyway this can be
  199. * boot time and run time configurable.
  200. */
  201. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  202. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  203. /* trace_types holds a link list of available tracers. */
  204. static struct tracer *trace_types __read_mostly;
  205. /* current_trace points to the tracer that is currently active */
  206. static struct tracer *current_trace __read_mostly;
  207. /*
  208. * max_tracer_type_len is used to simplify the allocating of
  209. * buffers to read userspace tracer names. We keep track of
  210. * the longest tracer name registered.
  211. */
  212. static int max_tracer_type_len;
  213. /*
  214. * trace_types_lock is used to protect the trace_types list.
  215. * This lock is also used to keep user access serialized.
  216. * Accesses from userspace will grab this lock while userspace
  217. * activities happen inside the kernel.
  218. */
  219. static DEFINE_MUTEX(trace_types_lock);
  220. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  221. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  222. /* trace_flags holds trace_options default values */
  223. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
  224. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
  225. TRACE_ITER_GRAPH_TIME;
  226. /**
  227. * trace_wake_up - wake up tasks waiting for trace input
  228. *
  229. * Simply wakes up any task that is blocked on the trace_wait
  230. * queue. These is used with trace_poll for tasks polling the trace.
  231. */
  232. void trace_wake_up(void)
  233. {
  234. /*
  235. * The runqueue_is_locked() can fail, but this is the best we
  236. * have for now:
  237. */
  238. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  239. wake_up(&trace_wait);
  240. }
  241. static int __init set_buf_size(char *str)
  242. {
  243. unsigned long buf_size;
  244. int ret;
  245. if (!str)
  246. return 0;
  247. ret = strict_strtoul(str, 0, &buf_size);
  248. /* nr_entries can not be zero */
  249. if (ret < 0 || buf_size == 0)
  250. return 0;
  251. trace_buf_size = buf_size;
  252. return 1;
  253. }
  254. __setup("trace_buf_size=", set_buf_size);
  255. unsigned long nsecs_to_usecs(unsigned long nsecs)
  256. {
  257. return nsecs / 1000;
  258. }
  259. /* These must match the bit postions in trace_iterator_flags */
  260. static const char *trace_options[] = {
  261. "print-parent",
  262. "sym-offset",
  263. "sym-addr",
  264. "verbose",
  265. "raw",
  266. "hex",
  267. "bin",
  268. "block",
  269. "stacktrace",
  270. "sched-tree",
  271. "trace_printk",
  272. "ftrace_preempt",
  273. "branch",
  274. "annotate",
  275. "userstacktrace",
  276. "sym-userobj",
  277. "printk-msg-only",
  278. "context-info",
  279. "latency-format",
  280. "global-clock",
  281. "sleep-time",
  282. "graph-time",
  283. NULL
  284. };
  285. /*
  286. * ftrace_max_lock is used to protect the swapping of buffers
  287. * when taking a max snapshot. The buffers themselves are
  288. * protected by per_cpu spinlocks. But the action of the swap
  289. * needs its own lock.
  290. *
  291. * This is defined as a raw_spinlock_t in order to help
  292. * with performance when lockdep debugging is enabled.
  293. */
  294. static raw_spinlock_t ftrace_max_lock =
  295. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  296. /*
  297. * Copy the new maximum trace into the separate maximum-trace
  298. * structure. (this way the maximum trace is permanently saved,
  299. * for later retrieval via /debugfs/tracing/latency_trace)
  300. */
  301. static void
  302. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  303. {
  304. struct trace_array_cpu *data = tr->data[cpu];
  305. max_tr.cpu = cpu;
  306. max_tr.time_start = data->preempt_timestamp;
  307. data = max_tr.data[cpu];
  308. data->saved_latency = tracing_max_latency;
  309. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  310. data->pid = tsk->pid;
  311. data->uid = task_uid(tsk);
  312. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  313. data->policy = tsk->policy;
  314. data->rt_priority = tsk->rt_priority;
  315. /* record this tasks comm */
  316. tracing_record_cmdline(tsk);
  317. }
  318. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  319. {
  320. int len;
  321. int ret;
  322. if (!cnt)
  323. return 0;
  324. if (s->len <= s->readpos)
  325. return -EBUSY;
  326. len = s->len - s->readpos;
  327. if (cnt > len)
  328. cnt = len;
  329. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  330. if (ret == cnt)
  331. return -EFAULT;
  332. cnt -= ret;
  333. s->readpos += cnt;
  334. return cnt;
  335. }
  336. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  337. {
  338. int len;
  339. void *ret;
  340. if (s->len <= s->readpos)
  341. return -EBUSY;
  342. len = s->len - s->readpos;
  343. if (cnt > len)
  344. cnt = len;
  345. ret = memcpy(buf, s->buffer + s->readpos, cnt);
  346. if (!ret)
  347. return -EFAULT;
  348. s->readpos += cnt;
  349. return cnt;
  350. }
  351. /**
  352. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  353. * @tr: tracer
  354. * @tsk: the task with the latency
  355. * @cpu: The cpu that initiated the trace.
  356. *
  357. * Flip the buffers between the @tr and the max_tr and record information
  358. * about which task was the cause of this latency.
  359. */
  360. void
  361. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  362. {
  363. struct ring_buffer *buf = tr->buffer;
  364. WARN_ON_ONCE(!irqs_disabled());
  365. __raw_spin_lock(&ftrace_max_lock);
  366. tr->buffer = max_tr.buffer;
  367. max_tr.buffer = buf;
  368. ftrace_disable_cpu();
  369. ring_buffer_reset(tr->buffer);
  370. ftrace_enable_cpu();
  371. __update_max_tr(tr, tsk, cpu);
  372. __raw_spin_unlock(&ftrace_max_lock);
  373. }
  374. /**
  375. * update_max_tr_single - only copy one trace over, and reset the rest
  376. * @tr - tracer
  377. * @tsk - task with the latency
  378. * @cpu - the cpu of the buffer to copy.
  379. *
  380. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  381. */
  382. void
  383. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  384. {
  385. int ret;
  386. WARN_ON_ONCE(!irqs_disabled());
  387. __raw_spin_lock(&ftrace_max_lock);
  388. ftrace_disable_cpu();
  389. ring_buffer_reset(max_tr.buffer);
  390. ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
  391. ftrace_enable_cpu();
  392. WARN_ON_ONCE(ret && ret != -EAGAIN);
  393. __update_max_tr(tr, tsk, cpu);
  394. __raw_spin_unlock(&ftrace_max_lock);
  395. }
  396. /**
  397. * register_tracer - register a tracer with the ftrace system.
  398. * @type - the plugin for the tracer
  399. *
  400. * Register a new plugin tracer.
  401. */
  402. int register_tracer(struct tracer *type)
  403. __releases(kernel_lock)
  404. __acquires(kernel_lock)
  405. {
  406. struct tracer *t;
  407. int len;
  408. int ret = 0;
  409. if (!type->name) {
  410. pr_info("Tracer must have a name\n");
  411. return -1;
  412. }
  413. /*
  414. * When this gets called we hold the BKL which means that
  415. * preemption is disabled. Various trace selftests however
  416. * need to disable and enable preemption for successful tests.
  417. * So we drop the BKL here and grab it after the tests again.
  418. */
  419. unlock_kernel();
  420. mutex_lock(&trace_types_lock);
  421. tracing_selftest_running = true;
  422. for (t = trace_types; t; t = t->next) {
  423. if (strcmp(type->name, t->name) == 0) {
  424. /* already found */
  425. pr_info("Trace %s already registered\n",
  426. type->name);
  427. ret = -1;
  428. goto out;
  429. }
  430. }
  431. if (!type->set_flag)
  432. type->set_flag = &dummy_set_flag;
  433. if (!type->flags)
  434. type->flags = &dummy_tracer_flags;
  435. else
  436. if (!type->flags->opts)
  437. type->flags->opts = dummy_tracer_opt;
  438. if (!type->wait_pipe)
  439. type->wait_pipe = default_wait_pipe;
  440. #ifdef CONFIG_FTRACE_STARTUP_TEST
  441. if (type->selftest && !tracing_selftest_disabled) {
  442. struct tracer *saved_tracer = current_trace;
  443. struct trace_array *tr = &global_trace;
  444. int i;
  445. /*
  446. * Run a selftest on this tracer.
  447. * Here we reset the trace buffer, and set the current
  448. * tracer to be this tracer. The tracer can then run some
  449. * internal tracing to verify that everything is in order.
  450. * If we fail, we do not register this tracer.
  451. */
  452. for_each_tracing_cpu(i)
  453. tracing_reset(tr, i);
  454. current_trace = type;
  455. /* the test is responsible for initializing and enabling */
  456. pr_info("Testing tracer %s: ", type->name);
  457. ret = type->selftest(type, tr);
  458. /* the test is responsible for resetting too */
  459. current_trace = saved_tracer;
  460. if (ret) {
  461. printk(KERN_CONT "FAILED!\n");
  462. goto out;
  463. }
  464. /* Only reset on passing, to avoid touching corrupted buffers */
  465. for_each_tracing_cpu(i)
  466. tracing_reset(tr, i);
  467. printk(KERN_CONT "PASSED\n");
  468. }
  469. #endif
  470. type->next = trace_types;
  471. trace_types = type;
  472. len = strlen(type->name);
  473. if (len > max_tracer_type_len)
  474. max_tracer_type_len = len;
  475. out:
  476. tracing_selftest_running = false;
  477. mutex_unlock(&trace_types_lock);
  478. if (ret || !default_bootup_tracer)
  479. goto out_unlock;
  480. if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
  481. goto out_unlock;
  482. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  483. /* Do we want this tracer to start on bootup? */
  484. tracing_set_tracer(type->name);
  485. default_bootup_tracer = NULL;
  486. /* disable other selftests, since this will break it. */
  487. tracing_selftest_disabled = 1;
  488. #ifdef CONFIG_FTRACE_STARTUP_TEST
  489. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  490. type->name);
  491. #endif
  492. out_unlock:
  493. lock_kernel();
  494. return ret;
  495. }
  496. void unregister_tracer(struct tracer *type)
  497. {
  498. struct tracer **t;
  499. int len;
  500. mutex_lock(&trace_types_lock);
  501. for (t = &trace_types; *t; t = &(*t)->next) {
  502. if (*t == type)
  503. goto found;
  504. }
  505. pr_info("Trace %s not registered\n", type->name);
  506. goto out;
  507. found:
  508. *t = (*t)->next;
  509. if (type == current_trace && tracer_enabled) {
  510. tracer_enabled = 0;
  511. tracing_stop();
  512. if (current_trace->stop)
  513. current_trace->stop(&global_trace);
  514. current_trace = &nop_trace;
  515. }
  516. if (strlen(type->name) != max_tracer_type_len)
  517. goto out;
  518. max_tracer_type_len = 0;
  519. for (t = &trace_types; *t; t = &(*t)->next) {
  520. len = strlen((*t)->name);
  521. if (len > max_tracer_type_len)
  522. max_tracer_type_len = len;
  523. }
  524. out:
  525. mutex_unlock(&trace_types_lock);
  526. }
  527. void tracing_reset(struct trace_array *tr, int cpu)
  528. {
  529. ftrace_disable_cpu();
  530. ring_buffer_reset_cpu(tr->buffer, cpu);
  531. ftrace_enable_cpu();
  532. }
  533. void tracing_reset_online_cpus(struct trace_array *tr)
  534. {
  535. int cpu;
  536. tr->time_start = ftrace_now(tr->cpu);
  537. for_each_online_cpu(cpu)
  538. tracing_reset(tr, cpu);
  539. }
  540. #define SAVED_CMDLINES 128
  541. #define NO_CMDLINE_MAP UINT_MAX
  542. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  543. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  544. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  545. static int cmdline_idx;
  546. static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
  547. /* temporary disable recording */
  548. static atomic_t trace_record_cmdline_disabled __read_mostly;
  549. static void trace_init_cmdlines(void)
  550. {
  551. memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
  552. memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
  553. cmdline_idx = 0;
  554. }
  555. static int trace_stop_count;
  556. static DEFINE_SPINLOCK(tracing_start_lock);
  557. /**
  558. * ftrace_off_permanent - disable all ftrace code permanently
  559. *
  560. * This should only be called when a serious anomally has
  561. * been detected. This will turn off the function tracing,
  562. * ring buffers, and other tracing utilites. It takes no
  563. * locks and can be called from any context.
  564. */
  565. void ftrace_off_permanent(void)
  566. {
  567. tracing_disabled = 1;
  568. ftrace_stop();
  569. tracing_off_permanent();
  570. }
  571. /**
  572. * tracing_start - quick start of the tracer
  573. *
  574. * If tracing is enabled but was stopped by tracing_stop,
  575. * this will start the tracer back up.
  576. */
  577. void tracing_start(void)
  578. {
  579. struct ring_buffer *buffer;
  580. unsigned long flags;
  581. if (tracing_disabled)
  582. return;
  583. spin_lock_irqsave(&tracing_start_lock, flags);
  584. if (--trace_stop_count) {
  585. if (trace_stop_count < 0) {
  586. /* Someone screwed up their debugging */
  587. WARN_ON_ONCE(1);
  588. trace_stop_count = 0;
  589. }
  590. goto out;
  591. }
  592. buffer = global_trace.buffer;
  593. if (buffer)
  594. ring_buffer_record_enable(buffer);
  595. buffer = max_tr.buffer;
  596. if (buffer)
  597. ring_buffer_record_enable(buffer);
  598. ftrace_start();
  599. out:
  600. spin_unlock_irqrestore(&tracing_start_lock, flags);
  601. }
  602. /**
  603. * tracing_stop - quick stop of the tracer
  604. *
  605. * Light weight way to stop tracing. Use in conjunction with
  606. * tracing_start.
  607. */
  608. void tracing_stop(void)
  609. {
  610. struct ring_buffer *buffer;
  611. unsigned long flags;
  612. ftrace_stop();
  613. spin_lock_irqsave(&tracing_start_lock, flags);
  614. if (trace_stop_count++)
  615. goto out;
  616. buffer = global_trace.buffer;
  617. if (buffer)
  618. ring_buffer_record_disable(buffer);
  619. buffer = max_tr.buffer;
  620. if (buffer)
  621. ring_buffer_record_disable(buffer);
  622. out:
  623. spin_unlock_irqrestore(&tracing_start_lock, flags);
  624. }
  625. void trace_stop_cmdline_recording(void);
  626. static void trace_save_cmdline(struct task_struct *tsk)
  627. {
  628. unsigned pid, idx;
  629. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  630. return;
  631. /*
  632. * It's not the end of the world if we don't get
  633. * the lock, but we also don't want to spin
  634. * nor do we want to disable interrupts,
  635. * so if we miss here, then better luck next time.
  636. */
  637. if (!__raw_spin_trylock(&trace_cmdline_lock))
  638. return;
  639. idx = map_pid_to_cmdline[tsk->pid];
  640. if (idx == NO_CMDLINE_MAP) {
  641. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  642. /*
  643. * Check whether the cmdline buffer at idx has a pid
  644. * mapped. We are going to overwrite that entry so we
  645. * need to clear the map_pid_to_cmdline. Otherwise we
  646. * would read the new comm for the old pid.
  647. */
  648. pid = map_cmdline_to_pid[idx];
  649. if (pid != NO_CMDLINE_MAP)
  650. map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
  651. map_cmdline_to_pid[idx] = tsk->pid;
  652. map_pid_to_cmdline[tsk->pid] = idx;
  653. cmdline_idx = idx;
  654. }
  655. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  656. __raw_spin_unlock(&trace_cmdline_lock);
  657. }
  658. void trace_find_cmdline(int pid, char comm[])
  659. {
  660. unsigned map;
  661. if (!pid) {
  662. strcpy(comm, "<idle>");
  663. return;
  664. }
  665. if (pid > PID_MAX_DEFAULT) {
  666. strcpy(comm, "<...>");
  667. return;
  668. }
  669. __raw_spin_lock(&trace_cmdline_lock);
  670. map = map_pid_to_cmdline[pid];
  671. if (map != NO_CMDLINE_MAP)
  672. strcpy(comm, saved_cmdlines[map]);
  673. else
  674. strcpy(comm, "<...>");
  675. __raw_spin_unlock(&trace_cmdline_lock);
  676. }
  677. void tracing_record_cmdline(struct task_struct *tsk)
  678. {
  679. if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
  680. !tracing_is_on())
  681. return;
  682. trace_save_cmdline(tsk);
  683. }
  684. void
  685. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  686. int pc)
  687. {
  688. struct task_struct *tsk = current;
  689. entry->preempt_count = pc & 0xff;
  690. entry->pid = (tsk) ? tsk->pid : 0;
  691. entry->tgid = (tsk) ? tsk->tgid : 0;
  692. entry->flags =
  693. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  694. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  695. #else
  696. TRACE_FLAG_IRQS_NOSUPPORT |
  697. #endif
  698. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  699. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  700. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  701. }
  702. struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
  703. int type,
  704. unsigned long len,
  705. unsigned long flags, int pc)
  706. {
  707. struct ring_buffer_event *event;
  708. event = ring_buffer_lock_reserve(tr->buffer, len);
  709. if (event != NULL) {
  710. struct trace_entry *ent = ring_buffer_event_data(event);
  711. tracing_generic_entry_update(ent, flags, pc);
  712. ent->type = type;
  713. }
  714. return event;
  715. }
  716. static void ftrace_trace_stack(struct trace_array *tr,
  717. unsigned long flags, int skip, int pc);
  718. static void ftrace_trace_userstack(struct trace_array *tr,
  719. unsigned long flags, int pc);
  720. static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
  721. struct ring_buffer_event *event,
  722. unsigned long flags, int pc,
  723. int wake)
  724. {
  725. ring_buffer_unlock_commit(tr->buffer, event);
  726. ftrace_trace_stack(tr, flags, 6, pc);
  727. ftrace_trace_userstack(tr, flags, pc);
  728. if (wake)
  729. trace_wake_up();
  730. }
  731. void trace_buffer_unlock_commit(struct trace_array *tr,
  732. struct ring_buffer_event *event,
  733. unsigned long flags, int pc)
  734. {
  735. __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
  736. }
  737. struct ring_buffer_event *
  738. trace_current_buffer_lock_reserve(int type, unsigned long len,
  739. unsigned long flags, int pc)
  740. {
  741. return trace_buffer_lock_reserve(&global_trace,
  742. type, len, flags, pc);
  743. }
  744. EXPORT_SYMBOL(trace_current_buffer_lock_reserve);
  745. void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
  746. unsigned long flags, int pc)
  747. {
  748. __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
  749. }
  750. EXPORT_SYMBOL(trace_current_buffer_unlock_commit);
  751. void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
  752. unsigned long flags, int pc)
  753. {
  754. __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
  755. }
  756. EXPORT_SYMBOL(trace_nowake_buffer_unlock_commit);
  757. void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
  758. {
  759. ring_buffer_discard_commit(global_trace.buffer, event);
  760. }
  761. EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
  762. void
  763. trace_function(struct trace_array *tr,
  764. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  765. int pc)
  766. {
  767. struct ftrace_event_call *call = &event_function;
  768. struct ring_buffer_event *event;
  769. struct ftrace_entry *entry;
  770. /* If we are reading the ring buffer, don't trace */
  771. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  772. return;
  773. event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
  774. flags, pc);
  775. if (!event)
  776. return;
  777. entry = ring_buffer_event_data(event);
  778. entry->ip = ip;
  779. entry->parent_ip = parent_ip;
  780. if (!filter_check_discard(call, entry, tr->buffer, event))
  781. ring_buffer_unlock_commit(tr->buffer, event);
  782. }
  783. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  784. static int __trace_graph_entry(struct trace_array *tr,
  785. struct ftrace_graph_ent *trace,
  786. unsigned long flags,
  787. int pc)
  788. {
  789. struct ftrace_event_call *call = &event_funcgraph_entry;
  790. struct ring_buffer_event *event;
  791. struct ftrace_graph_ent_entry *entry;
  792. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  793. return 0;
  794. event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
  795. sizeof(*entry), flags, pc);
  796. if (!event)
  797. return 0;
  798. entry = ring_buffer_event_data(event);
  799. entry->graph_ent = *trace;
  800. if (!filter_current_check_discard(call, entry, event))
  801. ring_buffer_unlock_commit(global_trace.buffer, event);
  802. return 1;
  803. }
  804. static void __trace_graph_return(struct trace_array *tr,
  805. struct ftrace_graph_ret *trace,
  806. unsigned long flags,
  807. int pc)
  808. {
  809. struct ftrace_event_call *call = &event_funcgraph_exit;
  810. struct ring_buffer_event *event;
  811. struct ftrace_graph_ret_entry *entry;
  812. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  813. return;
  814. event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
  815. sizeof(*entry), flags, pc);
  816. if (!event)
  817. return;
  818. entry = ring_buffer_event_data(event);
  819. entry->ret = *trace;
  820. if (!filter_current_check_discard(call, entry, event))
  821. ring_buffer_unlock_commit(global_trace.buffer, event);
  822. }
  823. #endif
  824. void
  825. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  826. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  827. int pc)
  828. {
  829. if (likely(!atomic_read(&data->disabled)))
  830. trace_function(tr, ip, parent_ip, flags, pc);
  831. }
  832. static void __ftrace_trace_stack(struct trace_array *tr,
  833. unsigned long flags,
  834. int skip, int pc)
  835. {
  836. #ifdef CONFIG_STACKTRACE
  837. struct ftrace_event_call *call = &event_kernel_stack;
  838. struct ring_buffer_event *event;
  839. struct stack_entry *entry;
  840. struct stack_trace trace;
  841. event = trace_buffer_lock_reserve(tr, TRACE_STACK,
  842. sizeof(*entry), flags, pc);
  843. if (!event)
  844. return;
  845. entry = ring_buffer_event_data(event);
  846. memset(&entry->caller, 0, sizeof(entry->caller));
  847. trace.nr_entries = 0;
  848. trace.max_entries = FTRACE_STACK_ENTRIES;
  849. trace.skip = skip;
  850. trace.entries = entry->caller;
  851. save_stack_trace(&trace);
  852. if (!filter_check_discard(call, entry, tr->buffer, event))
  853. ring_buffer_unlock_commit(tr->buffer, event);
  854. #endif
  855. }
  856. static void ftrace_trace_stack(struct trace_array *tr,
  857. unsigned long flags,
  858. int skip, int pc)
  859. {
  860. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  861. return;
  862. __ftrace_trace_stack(tr, flags, skip, pc);
  863. }
  864. void __trace_stack(struct trace_array *tr,
  865. unsigned long flags,
  866. int skip, int pc)
  867. {
  868. __ftrace_trace_stack(tr, flags, skip, pc);
  869. }
  870. static void ftrace_trace_userstack(struct trace_array *tr,
  871. unsigned long flags, int pc)
  872. {
  873. #ifdef CONFIG_STACKTRACE
  874. struct ftrace_event_call *call = &event_user_stack;
  875. struct ring_buffer_event *event;
  876. struct userstack_entry *entry;
  877. struct stack_trace trace;
  878. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  879. return;
  880. event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
  881. sizeof(*entry), flags, pc);
  882. if (!event)
  883. return;
  884. entry = ring_buffer_event_data(event);
  885. memset(&entry->caller, 0, sizeof(entry->caller));
  886. trace.nr_entries = 0;
  887. trace.max_entries = FTRACE_STACK_ENTRIES;
  888. trace.skip = 0;
  889. trace.entries = entry->caller;
  890. save_stack_trace_user(&trace);
  891. if (!filter_check_discard(call, entry, tr->buffer, event))
  892. ring_buffer_unlock_commit(tr->buffer, event);
  893. #endif
  894. }
  895. #ifdef UNUSED
  896. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  897. {
  898. ftrace_trace_userstack(tr, flags, preempt_count());
  899. }
  900. #endif /* UNUSED */
  901. static void
  902. ftrace_trace_special(void *__tr,
  903. unsigned long arg1, unsigned long arg2, unsigned long arg3,
  904. int pc)
  905. {
  906. struct ring_buffer_event *event;
  907. struct trace_array *tr = __tr;
  908. struct special_entry *entry;
  909. event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
  910. sizeof(*entry), 0, pc);
  911. if (!event)
  912. return;
  913. entry = ring_buffer_event_data(event);
  914. entry->arg1 = arg1;
  915. entry->arg2 = arg2;
  916. entry->arg3 = arg3;
  917. trace_buffer_unlock_commit(tr, event, 0, pc);
  918. }
  919. void
  920. __trace_special(void *__tr, void *__data,
  921. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  922. {
  923. ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
  924. }
  925. void
  926. tracing_sched_switch_trace(struct trace_array *tr,
  927. struct task_struct *prev,
  928. struct task_struct *next,
  929. unsigned long flags, int pc)
  930. {
  931. struct ftrace_event_call *call = &event_context_switch;
  932. struct ring_buffer_event *event;
  933. struct ctx_switch_entry *entry;
  934. event = trace_buffer_lock_reserve(tr, TRACE_CTX,
  935. sizeof(*entry), flags, pc);
  936. if (!event)
  937. return;
  938. entry = ring_buffer_event_data(event);
  939. entry->prev_pid = prev->pid;
  940. entry->prev_prio = prev->prio;
  941. entry->prev_state = prev->state;
  942. entry->next_pid = next->pid;
  943. entry->next_prio = next->prio;
  944. entry->next_state = next->state;
  945. entry->next_cpu = task_cpu(next);
  946. if (!filter_check_discard(call, entry, tr->buffer, event))
  947. trace_buffer_unlock_commit(tr, event, flags, pc);
  948. }
  949. void
  950. tracing_sched_wakeup_trace(struct trace_array *tr,
  951. struct task_struct *wakee,
  952. struct task_struct *curr,
  953. unsigned long flags, int pc)
  954. {
  955. struct ftrace_event_call *call = &event_wakeup;
  956. struct ring_buffer_event *event;
  957. struct ctx_switch_entry *entry;
  958. event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
  959. sizeof(*entry), flags, pc);
  960. if (!event)
  961. return;
  962. entry = ring_buffer_event_data(event);
  963. entry->prev_pid = curr->pid;
  964. entry->prev_prio = curr->prio;
  965. entry->prev_state = curr->state;
  966. entry->next_pid = wakee->pid;
  967. entry->next_prio = wakee->prio;
  968. entry->next_state = wakee->state;
  969. entry->next_cpu = task_cpu(wakee);
  970. if (!filter_check_discard(call, entry, tr->buffer, event))
  971. ring_buffer_unlock_commit(tr->buffer, event);
  972. ftrace_trace_stack(tr, flags, 6, pc);
  973. ftrace_trace_userstack(tr, flags, pc);
  974. }
  975. void
  976. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  977. {
  978. struct trace_array *tr = &global_trace;
  979. struct trace_array_cpu *data;
  980. unsigned long flags;
  981. int cpu;
  982. int pc;
  983. if (tracing_disabled)
  984. return;
  985. pc = preempt_count();
  986. local_irq_save(flags);
  987. cpu = raw_smp_processor_id();
  988. data = tr->data[cpu];
  989. if (likely(atomic_inc_return(&data->disabled) == 1))
  990. ftrace_trace_special(tr, arg1, arg2, arg3, pc);
  991. atomic_dec(&data->disabled);
  992. local_irq_restore(flags);
  993. }
  994. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  995. int trace_graph_entry(struct ftrace_graph_ent *trace)
  996. {
  997. struct trace_array *tr = &global_trace;
  998. struct trace_array_cpu *data;
  999. unsigned long flags;
  1000. long disabled;
  1001. int ret;
  1002. int cpu;
  1003. int pc;
  1004. if (!ftrace_trace_task(current))
  1005. return 0;
  1006. if (!ftrace_graph_addr(trace->func))
  1007. return 0;
  1008. local_irq_save(flags);
  1009. cpu = raw_smp_processor_id();
  1010. data = tr->data[cpu];
  1011. disabled = atomic_inc_return(&data->disabled);
  1012. if (likely(disabled == 1)) {
  1013. pc = preempt_count();
  1014. ret = __trace_graph_entry(tr, trace, flags, pc);
  1015. } else {
  1016. ret = 0;
  1017. }
  1018. /* Only do the atomic if it is not already set */
  1019. if (!test_tsk_trace_graph(current))
  1020. set_tsk_trace_graph(current);
  1021. atomic_dec(&data->disabled);
  1022. local_irq_restore(flags);
  1023. return ret;
  1024. }
  1025. void trace_graph_return(struct ftrace_graph_ret *trace)
  1026. {
  1027. struct trace_array *tr = &global_trace;
  1028. struct trace_array_cpu *data;
  1029. unsigned long flags;
  1030. long disabled;
  1031. int cpu;
  1032. int pc;
  1033. local_irq_save(flags);
  1034. cpu = raw_smp_processor_id();
  1035. data = tr->data[cpu];
  1036. disabled = atomic_inc_return(&data->disabled);
  1037. if (likely(disabled == 1)) {
  1038. pc = preempt_count();
  1039. __trace_graph_return(tr, trace, flags, pc);
  1040. }
  1041. if (!trace->depth)
  1042. clear_tsk_trace_graph(current);
  1043. atomic_dec(&data->disabled);
  1044. local_irq_restore(flags);
  1045. }
  1046. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1047. /**
  1048. * trace_vbprintk - write binary msg to tracing buffer
  1049. *
  1050. */
  1051. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  1052. {
  1053. static raw_spinlock_t trace_buf_lock =
  1054. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  1055. static u32 trace_buf[TRACE_BUF_SIZE];
  1056. struct ftrace_event_call *call = &event_bprint;
  1057. struct ring_buffer_event *event;
  1058. struct trace_array *tr = &global_trace;
  1059. struct trace_array_cpu *data;
  1060. struct bprint_entry *entry;
  1061. unsigned long flags;
  1062. int disable;
  1063. int resched;
  1064. int cpu, len = 0, size, pc;
  1065. if (unlikely(tracing_selftest_running || tracing_disabled))
  1066. return 0;
  1067. /* Don't pollute graph traces with trace_vprintk internals */
  1068. pause_graph_tracing();
  1069. pc = preempt_count();
  1070. resched = ftrace_preempt_disable();
  1071. cpu = raw_smp_processor_id();
  1072. data = tr->data[cpu];
  1073. disable = atomic_inc_return(&data->disabled);
  1074. if (unlikely(disable != 1))
  1075. goto out;
  1076. /* Lockdep uses trace_printk for lock tracing */
  1077. local_irq_save(flags);
  1078. __raw_spin_lock(&trace_buf_lock);
  1079. len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1080. if (len > TRACE_BUF_SIZE || len < 0)
  1081. goto out_unlock;
  1082. size = sizeof(*entry) + sizeof(u32) * len;
  1083. event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
  1084. if (!event)
  1085. goto out_unlock;
  1086. entry = ring_buffer_event_data(event);
  1087. entry->ip = ip;
  1088. entry->fmt = fmt;
  1089. memcpy(entry->buf, trace_buf, sizeof(u32) * len);
  1090. if (!filter_check_discard(call, entry, tr->buffer, event))
  1091. ring_buffer_unlock_commit(tr->buffer, event);
  1092. out_unlock:
  1093. __raw_spin_unlock(&trace_buf_lock);
  1094. local_irq_restore(flags);
  1095. out:
  1096. atomic_dec_return(&data->disabled);
  1097. ftrace_preempt_enable(resched);
  1098. unpause_graph_tracing();
  1099. return len;
  1100. }
  1101. EXPORT_SYMBOL_GPL(trace_vbprintk);
  1102. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  1103. {
  1104. static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
  1105. static char trace_buf[TRACE_BUF_SIZE];
  1106. struct ftrace_event_call *call = &event_print;
  1107. struct ring_buffer_event *event;
  1108. struct trace_array *tr = &global_trace;
  1109. struct trace_array_cpu *data;
  1110. int cpu, len = 0, size, pc;
  1111. struct print_entry *entry;
  1112. unsigned long irq_flags;
  1113. int disable;
  1114. if (tracing_disabled || tracing_selftest_running)
  1115. return 0;
  1116. pc = preempt_count();
  1117. preempt_disable_notrace();
  1118. cpu = raw_smp_processor_id();
  1119. data = tr->data[cpu];
  1120. disable = atomic_inc_return(&data->disabled);
  1121. if (unlikely(disable != 1))
  1122. goto out;
  1123. pause_graph_tracing();
  1124. raw_local_irq_save(irq_flags);
  1125. __raw_spin_lock(&trace_buf_lock);
  1126. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1127. len = min(len, TRACE_BUF_SIZE-1);
  1128. trace_buf[len] = 0;
  1129. size = sizeof(*entry) + len + 1;
  1130. event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
  1131. if (!event)
  1132. goto out_unlock;
  1133. entry = ring_buffer_event_data(event);
  1134. entry->ip = ip;
  1135. memcpy(&entry->buf, trace_buf, len);
  1136. entry->buf[len] = 0;
  1137. if (!filter_check_discard(call, entry, tr->buffer, event))
  1138. ring_buffer_unlock_commit(tr->buffer, event);
  1139. out_unlock:
  1140. __raw_spin_unlock(&trace_buf_lock);
  1141. raw_local_irq_restore(irq_flags);
  1142. unpause_graph_tracing();
  1143. out:
  1144. atomic_dec_return(&data->disabled);
  1145. preempt_enable_notrace();
  1146. return len;
  1147. }
  1148. EXPORT_SYMBOL_GPL(trace_vprintk);
  1149. enum trace_file_type {
  1150. TRACE_FILE_LAT_FMT = 1,
  1151. TRACE_FILE_ANNOTATE = 2,
  1152. };
  1153. static void trace_iterator_increment(struct trace_iterator *iter)
  1154. {
  1155. /* Don't allow ftrace to trace into the ring buffers */
  1156. ftrace_disable_cpu();
  1157. iter->idx++;
  1158. if (iter->buffer_iter[iter->cpu])
  1159. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1160. ftrace_enable_cpu();
  1161. }
  1162. static struct trace_entry *
  1163. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  1164. {
  1165. struct ring_buffer_event *event;
  1166. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  1167. /* Don't allow ftrace to trace into the ring buffers */
  1168. ftrace_disable_cpu();
  1169. if (buf_iter)
  1170. event = ring_buffer_iter_peek(buf_iter, ts);
  1171. else
  1172. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  1173. ftrace_enable_cpu();
  1174. return event ? ring_buffer_event_data(event) : NULL;
  1175. }
  1176. static struct trace_entry *
  1177. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  1178. {
  1179. struct ring_buffer *buffer = iter->tr->buffer;
  1180. struct trace_entry *ent, *next = NULL;
  1181. int cpu_file = iter->cpu_file;
  1182. u64 next_ts = 0, ts;
  1183. int next_cpu = -1;
  1184. int cpu;
  1185. /*
  1186. * If we are in a per_cpu trace file, don't bother by iterating over
  1187. * all cpu and peek directly.
  1188. */
  1189. if (cpu_file > TRACE_PIPE_ALL_CPU) {
  1190. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1191. return NULL;
  1192. ent = peek_next_entry(iter, cpu_file, ent_ts);
  1193. if (ent_cpu)
  1194. *ent_cpu = cpu_file;
  1195. return ent;
  1196. }
  1197. for_each_tracing_cpu(cpu) {
  1198. if (ring_buffer_empty_cpu(buffer, cpu))
  1199. continue;
  1200. ent = peek_next_entry(iter, cpu, &ts);
  1201. /*
  1202. * Pick the entry with the smallest timestamp:
  1203. */
  1204. if (ent && (!next || ts < next_ts)) {
  1205. next = ent;
  1206. next_cpu = cpu;
  1207. next_ts = ts;
  1208. }
  1209. }
  1210. if (ent_cpu)
  1211. *ent_cpu = next_cpu;
  1212. if (ent_ts)
  1213. *ent_ts = next_ts;
  1214. return next;
  1215. }
  1216. /* Find the next real entry, without updating the iterator itself */
  1217. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1218. int *ent_cpu, u64 *ent_ts)
  1219. {
  1220. return __find_next_entry(iter, ent_cpu, ent_ts);
  1221. }
  1222. /* Find the next real entry, and increment the iterator to the next entry */
  1223. static void *find_next_entry_inc(struct trace_iterator *iter)
  1224. {
  1225. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  1226. if (iter->ent)
  1227. trace_iterator_increment(iter);
  1228. return iter->ent ? iter : NULL;
  1229. }
  1230. static void trace_consume(struct trace_iterator *iter)
  1231. {
  1232. /* Don't allow ftrace to trace into the ring buffers */
  1233. ftrace_disable_cpu();
  1234. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  1235. ftrace_enable_cpu();
  1236. }
  1237. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1238. {
  1239. struct trace_iterator *iter = m->private;
  1240. int i = (int)*pos;
  1241. void *ent;
  1242. (*pos)++;
  1243. /* can't go backwards */
  1244. if (iter->idx > i)
  1245. return NULL;
  1246. if (iter->idx < 0)
  1247. ent = find_next_entry_inc(iter);
  1248. else
  1249. ent = iter;
  1250. while (ent && iter->idx < i)
  1251. ent = find_next_entry_inc(iter);
  1252. iter->pos = *pos;
  1253. return ent;
  1254. }
  1255. /*
  1256. * No necessary locking here. The worst thing which can
  1257. * happen is loosing events consumed at the same time
  1258. * by a trace_pipe reader.
  1259. * Other than that, we don't risk to crash the ring buffer
  1260. * because it serializes the readers.
  1261. *
  1262. * The current tracer is copied to avoid a global locking
  1263. * all around.
  1264. */
  1265. static void *s_start(struct seq_file *m, loff_t *pos)
  1266. {
  1267. struct trace_iterator *iter = m->private;
  1268. static struct tracer *old_tracer;
  1269. int cpu_file = iter->cpu_file;
  1270. void *p = NULL;
  1271. loff_t l = 0;
  1272. int cpu;
  1273. /* copy the tracer to avoid using a global lock all around */
  1274. mutex_lock(&trace_types_lock);
  1275. if (unlikely(old_tracer != current_trace && current_trace)) {
  1276. old_tracer = current_trace;
  1277. *iter->trace = *current_trace;
  1278. }
  1279. mutex_unlock(&trace_types_lock);
  1280. atomic_inc(&trace_record_cmdline_disabled);
  1281. if (*pos != iter->pos) {
  1282. iter->ent = NULL;
  1283. iter->cpu = 0;
  1284. iter->idx = -1;
  1285. ftrace_disable_cpu();
  1286. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  1287. for_each_tracing_cpu(cpu)
  1288. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  1289. } else
  1290. ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
  1291. ftrace_enable_cpu();
  1292. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1293. ;
  1294. } else {
  1295. l = *pos - 1;
  1296. p = s_next(m, p, &l);
  1297. }
  1298. return p;
  1299. }
  1300. static void s_stop(struct seq_file *m, void *p)
  1301. {
  1302. atomic_dec(&trace_record_cmdline_disabled);
  1303. }
  1304. static void print_lat_help_header(struct seq_file *m)
  1305. {
  1306. seq_puts(m, "# _------=> CPU# \n");
  1307. seq_puts(m, "# / _-----=> irqs-off \n");
  1308. seq_puts(m, "# | / _----=> need-resched \n");
  1309. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1310. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1311. seq_puts(m, "# |||| / \n");
  1312. seq_puts(m, "# ||||| delay \n");
  1313. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1314. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1315. }
  1316. static void print_func_help_header(struct seq_file *m)
  1317. {
  1318. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1319. seq_puts(m, "# | | | | |\n");
  1320. }
  1321. static void
  1322. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1323. {
  1324. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1325. struct trace_array *tr = iter->tr;
  1326. struct trace_array_cpu *data = tr->data[tr->cpu];
  1327. struct tracer *type = current_trace;
  1328. unsigned long total;
  1329. unsigned long entries;
  1330. const char *name = "preemption";
  1331. if (type)
  1332. name = type->name;
  1333. entries = ring_buffer_entries(iter->tr->buffer);
  1334. total = entries +
  1335. ring_buffer_overruns(iter->tr->buffer);
  1336. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1337. name, UTS_RELEASE);
  1338. seq_puts(m, "# -----------------------------------"
  1339. "---------------------------------\n");
  1340. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1341. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1342. nsecs_to_usecs(data->saved_latency),
  1343. entries,
  1344. total,
  1345. tr->cpu,
  1346. #if defined(CONFIG_PREEMPT_NONE)
  1347. "server",
  1348. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1349. "desktop",
  1350. #elif defined(CONFIG_PREEMPT)
  1351. "preempt",
  1352. #else
  1353. "unknown",
  1354. #endif
  1355. /* These are reserved for later use */
  1356. 0, 0, 0, 0);
  1357. #ifdef CONFIG_SMP
  1358. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1359. #else
  1360. seq_puts(m, ")\n");
  1361. #endif
  1362. seq_puts(m, "# -----------------\n");
  1363. seq_printf(m, "# | task: %.16s-%d "
  1364. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1365. data->comm, data->pid, data->uid, data->nice,
  1366. data->policy, data->rt_priority);
  1367. seq_puts(m, "# -----------------\n");
  1368. if (data->critical_start) {
  1369. seq_puts(m, "# => started at: ");
  1370. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1371. trace_print_seq(m, &iter->seq);
  1372. seq_puts(m, "\n# => ended at: ");
  1373. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1374. trace_print_seq(m, &iter->seq);
  1375. seq_puts(m, "#\n");
  1376. }
  1377. seq_puts(m, "#\n");
  1378. }
  1379. static void test_cpu_buff_start(struct trace_iterator *iter)
  1380. {
  1381. struct trace_seq *s = &iter->seq;
  1382. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1383. return;
  1384. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1385. return;
  1386. if (cpumask_test_cpu(iter->cpu, iter->started))
  1387. return;
  1388. cpumask_set_cpu(iter->cpu, iter->started);
  1389. /* Don't print started cpu buffer for the first entry of the trace */
  1390. if (iter->idx > 1)
  1391. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  1392. iter->cpu);
  1393. }
  1394. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1395. {
  1396. struct trace_seq *s = &iter->seq;
  1397. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1398. struct trace_entry *entry;
  1399. struct trace_event *event;
  1400. entry = iter->ent;
  1401. test_cpu_buff_start(iter);
  1402. event = ftrace_find_event(entry->type);
  1403. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1404. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1405. if (!trace_print_lat_context(iter))
  1406. goto partial;
  1407. } else {
  1408. if (!trace_print_context(iter))
  1409. goto partial;
  1410. }
  1411. }
  1412. if (event)
  1413. return event->trace(iter, sym_flags);
  1414. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1415. goto partial;
  1416. return TRACE_TYPE_HANDLED;
  1417. partial:
  1418. return TRACE_TYPE_PARTIAL_LINE;
  1419. }
  1420. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1421. {
  1422. struct trace_seq *s = &iter->seq;
  1423. struct trace_entry *entry;
  1424. struct trace_event *event;
  1425. entry = iter->ent;
  1426. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1427. if (!trace_seq_printf(s, "%d %d %llu ",
  1428. entry->pid, iter->cpu, iter->ts))
  1429. goto partial;
  1430. }
  1431. event = ftrace_find_event(entry->type);
  1432. if (event)
  1433. return event->raw(iter, 0);
  1434. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1435. goto partial;
  1436. return TRACE_TYPE_HANDLED;
  1437. partial:
  1438. return TRACE_TYPE_PARTIAL_LINE;
  1439. }
  1440. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1441. {
  1442. struct trace_seq *s = &iter->seq;
  1443. unsigned char newline = '\n';
  1444. struct trace_entry *entry;
  1445. struct trace_event *event;
  1446. entry = iter->ent;
  1447. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1448. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1449. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1450. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1451. }
  1452. event = ftrace_find_event(entry->type);
  1453. if (event) {
  1454. enum print_line_t ret = event->hex(iter, 0);
  1455. if (ret != TRACE_TYPE_HANDLED)
  1456. return ret;
  1457. }
  1458. SEQ_PUT_FIELD_RET(s, newline);
  1459. return TRACE_TYPE_HANDLED;
  1460. }
  1461. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1462. {
  1463. struct trace_seq *s = &iter->seq;
  1464. struct trace_entry *entry;
  1465. struct trace_event *event;
  1466. entry = iter->ent;
  1467. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1468. SEQ_PUT_FIELD_RET(s, entry->pid);
  1469. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1470. SEQ_PUT_FIELD_RET(s, iter->ts);
  1471. }
  1472. event = ftrace_find_event(entry->type);
  1473. return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
  1474. }
  1475. static int trace_empty(struct trace_iterator *iter)
  1476. {
  1477. int cpu;
  1478. /* If we are looking at one CPU buffer, only check that one */
  1479. if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
  1480. cpu = iter->cpu_file;
  1481. if (iter->buffer_iter[cpu]) {
  1482. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1483. return 0;
  1484. } else {
  1485. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1486. return 0;
  1487. }
  1488. return 1;
  1489. }
  1490. for_each_tracing_cpu(cpu) {
  1491. if (iter->buffer_iter[cpu]) {
  1492. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1493. return 0;
  1494. } else {
  1495. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1496. return 0;
  1497. }
  1498. }
  1499. return 1;
  1500. }
  1501. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1502. {
  1503. enum print_line_t ret;
  1504. if (iter->trace && iter->trace->print_line) {
  1505. ret = iter->trace->print_line(iter);
  1506. if (ret != TRACE_TYPE_UNHANDLED)
  1507. return ret;
  1508. }
  1509. if (iter->ent->type == TRACE_BPRINT &&
  1510. trace_flags & TRACE_ITER_PRINTK &&
  1511. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1512. return trace_print_bprintk_msg_only(iter);
  1513. if (iter->ent->type == TRACE_PRINT &&
  1514. trace_flags & TRACE_ITER_PRINTK &&
  1515. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1516. return trace_print_printk_msg_only(iter);
  1517. if (trace_flags & TRACE_ITER_BIN)
  1518. return print_bin_fmt(iter);
  1519. if (trace_flags & TRACE_ITER_HEX)
  1520. return print_hex_fmt(iter);
  1521. if (trace_flags & TRACE_ITER_RAW)
  1522. return print_raw_fmt(iter);
  1523. return print_trace_fmt(iter);
  1524. }
  1525. static int s_show(struct seq_file *m, void *v)
  1526. {
  1527. struct trace_iterator *iter = v;
  1528. if (iter->ent == NULL) {
  1529. if (iter->tr) {
  1530. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1531. seq_puts(m, "#\n");
  1532. }
  1533. if (iter->trace && iter->trace->print_header)
  1534. iter->trace->print_header(m);
  1535. else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1536. /* print nothing if the buffers are empty */
  1537. if (trace_empty(iter))
  1538. return 0;
  1539. print_trace_header(m, iter);
  1540. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1541. print_lat_help_header(m);
  1542. } else {
  1543. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1544. print_func_help_header(m);
  1545. }
  1546. } else {
  1547. print_trace_line(iter);
  1548. trace_print_seq(m, &iter->seq);
  1549. }
  1550. return 0;
  1551. }
  1552. static struct seq_operations tracer_seq_ops = {
  1553. .start = s_start,
  1554. .next = s_next,
  1555. .stop = s_stop,
  1556. .show = s_show,
  1557. };
  1558. static struct trace_iterator *
  1559. __tracing_open(struct inode *inode, struct file *file)
  1560. {
  1561. long cpu_file = (long) inode->i_private;
  1562. void *fail_ret = ERR_PTR(-ENOMEM);
  1563. struct trace_iterator *iter;
  1564. struct seq_file *m;
  1565. int cpu, ret;
  1566. if (tracing_disabled)
  1567. return ERR_PTR(-ENODEV);
  1568. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1569. if (!iter)
  1570. return ERR_PTR(-ENOMEM);
  1571. /*
  1572. * We make a copy of the current tracer to avoid concurrent
  1573. * changes on it while we are reading.
  1574. */
  1575. mutex_lock(&trace_types_lock);
  1576. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1577. if (!iter->trace)
  1578. goto fail;
  1579. if (current_trace)
  1580. *iter->trace = *current_trace;
  1581. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
  1582. goto fail;
  1583. cpumask_clear(iter->started);
  1584. if (current_trace && current_trace->print_max)
  1585. iter->tr = &max_tr;
  1586. else
  1587. iter->tr = &global_trace;
  1588. iter->pos = -1;
  1589. mutex_init(&iter->mutex);
  1590. iter->cpu_file = cpu_file;
  1591. /* Notify the tracer early; before we stop tracing. */
  1592. if (iter->trace && iter->trace->open)
  1593. iter->trace->open(iter);
  1594. /* Annotate start of buffers if we had overruns */
  1595. if (ring_buffer_overruns(iter->tr->buffer))
  1596. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1597. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  1598. for_each_tracing_cpu(cpu) {
  1599. iter->buffer_iter[cpu] =
  1600. ring_buffer_read_start(iter->tr->buffer, cpu);
  1601. }
  1602. } else {
  1603. cpu = iter->cpu_file;
  1604. iter->buffer_iter[cpu] =
  1605. ring_buffer_read_start(iter->tr->buffer, cpu);
  1606. }
  1607. /* TODO stop tracer */
  1608. ret = seq_open(file, &tracer_seq_ops);
  1609. if (ret < 0) {
  1610. fail_ret = ERR_PTR(ret);
  1611. goto fail_buffer;
  1612. }
  1613. m = file->private_data;
  1614. m->private = iter;
  1615. /* stop the trace while dumping */
  1616. tracing_stop();
  1617. mutex_unlock(&trace_types_lock);
  1618. return iter;
  1619. fail_buffer:
  1620. for_each_tracing_cpu(cpu) {
  1621. if (iter->buffer_iter[cpu])
  1622. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1623. }
  1624. free_cpumask_var(iter->started);
  1625. fail:
  1626. mutex_unlock(&trace_types_lock);
  1627. kfree(iter->trace);
  1628. kfree(iter);
  1629. return fail_ret;
  1630. }
  1631. int tracing_open_generic(struct inode *inode, struct file *filp)
  1632. {
  1633. if (tracing_disabled)
  1634. return -ENODEV;
  1635. filp->private_data = inode->i_private;
  1636. return 0;
  1637. }
  1638. static int tracing_release(struct inode *inode, struct file *file)
  1639. {
  1640. struct seq_file *m = (struct seq_file *)file->private_data;
  1641. struct trace_iterator *iter;
  1642. int cpu;
  1643. if (!(file->f_mode & FMODE_READ))
  1644. return 0;
  1645. iter = m->private;
  1646. mutex_lock(&trace_types_lock);
  1647. for_each_tracing_cpu(cpu) {
  1648. if (iter->buffer_iter[cpu])
  1649. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1650. }
  1651. if (iter->trace && iter->trace->close)
  1652. iter->trace->close(iter);
  1653. /* reenable tracing if it was previously enabled */
  1654. tracing_start();
  1655. mutex_unlock(&trace_types_lock);
  1656. seq_release(inode, file);
  1657. mutex_destroy(&iter->mutex);
  1658. free_cpumask_var(iter->started);
  1659. kfree(iter->trace);
  1660. kfree(iter);
  1661. return 0;
  1662. }
  1663. static int tracing_open(struct inode *inode, struct file *file)
  1664. {
  1665. struct trace_iterator *iter;
  1666. int ret = 0;
  1667. /* If this file was open for write, then erase contents */
  1668. if ((file->f_mode & FMODE_WRITE) &&
  1669. !(file->f_flags & O_APPEND)) {
  1670. long cpu = (long) inode->i_private;
  1671. if (cpu == TRACE_PIPE_ALL_CPU)
  1672. tracing_reset_online_cpus(&global_trace);
  1673. else
  1674. tracing_reset(&global_trace, cpu);
  1675. }
  1676. if (file->f_mode & FMODE_READ) {
  1677. iter = __tracing_open(inode, file);
  1678. if (IS_ERR(iter))
  1679. ret = PTR_ERR(iter);
  1680. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  1681. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1682. }
  1683. return ret;
  1684. }
  1685. static void *
  1686. t_next(struct seq_file *m, void *v, loff_t *pos)
  1687. {
  1688. struct tracer *t = m->private;
  1689. (*pos)++;
  1690. if (t)
  1691. t = t->next;
  1692. m->private = t;
  1693. return t;
  1694. }
  1695. static void *t_start(struct seq_file *m, loff_t *pos)
  1696. {
  1697. struct tracer *t = m->private;
  1698. loff_t l = 0;
  1699. mutex_lock(&trace_types_lock);
  1700. for (; t && l < *pos; t = t_next(m, t, &l))
  1701. ;
  1702. return t;
  1703. }
  1704. static void t_stop(struct seq_file *m, void *p)
  1705. {
  1706. mutex_unlock(&trace_types_lock);
  1707. }
  1708. static int t_show(struct seq_file *m, void *v)
  1709. {
  1710. struct tracer *t = v;
  1711. if (!t)
  1712. return 0;
  1713. seq_printf(m, "%s", t->name);
  1714. if (t->next)
  1715. seq_putc(m, ' ');
  1716. else
  1717. seq_putc(m, '\n');
  1718. return 0;
  1719. }
  1720. static struct seq_operations show_traces_seq_ops = {
  1721. .start = t_start,
  1722. .next = t_next,
  1723. .stop = t_stop,
  1724. .show = t_show,
  1725. };
  1726. static int show_traces_open(struct inode *inode, struct file *file)
  1727. {
  1728. int ret;
  1729. if (tracing_disabled)
  1730. return -ENODEV;
  1731. ret = seq_open(file, &show_traces_seq_ops);
  1732. if (!ret) {
  1733. struct seq_file *m = file->private_data;
  1734. m->private = trace_types;
  1735. }
  1736. return ret;
  1737. }
  1738. static ssize_t
  1739. tracing_write_stub(struct file *filp, const char __user *ubuf,
  1740. size_t count, loff_t *ppos)
  1741. {
  1742. return count;
  1743. }
  1744. static const struct file_operations tracing_fops = {
  1745. .open = tracing_open,
  1746. .read = seq_read,
  1747. .write = tracing_write_stub,
  1748. .llseek = seq_lseek,
  1749. .release = tracing_release,
  1750. };
  1751. static const struct file_operations show_traces_fops = {
  1752. .open = show_traces_open,
  1753. .read = seq_read,
  1754. .release = seq_release,
  1755. };
  1756. /*
  1757. * Only trace on a CPU if the bitmask is set:
  1758. */
  1759. static cpumask_var_t tracing_cpumask;
  1760. /*
  1761. * The tracer itself will not take this lock, but still we want
  1762. * to provide a consistent cpumask to user-space:
  1763. */
  1764. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1765. /*
  1766. * Temporary storage for the character representation of the
  1767. * CPU bitmask (and one more byte for the newline):
  1768. */
  1769. static char mask_str[NR_CPUS + 1];
  1770. static ssize_t
  1771. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1772. size_t count, loff_t *ppos)
  1773. {
  1774. int len;
  1775. mutex_lock(&tracing_cpumask_update_lock);
  1776. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1777. if (count - len < 2) {
  1778. count = -EINVAL;
  1779. goto out_err;
  1780. }
  1781. len += sprintf(mask_str + len, "\n");
  1782. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1783. out_err:
  1784. mutex_unlock(&tracing_cpumask_update_lock);
  1785. return count;
  1786. }
  1787. static ssize_t
  1788. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1789. size_t count, loff_t *ppos)
  1790. {
  1791. int err, cpu;
  1792. cpumask_var_t tracing_cpumask_new;
  1793. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  1794. return -ENOMEM;
  1795. mutex_lock(&tracing_cpumask_update_lock);
  1796. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1797. if (err)
  1798. goto err_unlock;
  1799. local_irq_disable();
  1800. __raw_spin_lock(&ftrace_max_lock);
  1801. for_each_tracing_cpu(cpu) {
  1802. /*
  1803. * Increase/decrease the disabled counter if we are
  1804. * about to flip a bit in the cpumask:
  1805. */
  1806. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  1807. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1808. atomic_inc(&global_trace.data[cpu]->disabled);
  1809. }
  1810. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  1811. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1812. atomic_dec(&global_trace.data[cpu]->disabled);
  1813. }
  1814. }
  1815. __raw_spin_unlock(&ftrace_max_lock);
  1816. local_irq_enable();
  1817. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  1818. mutex_unlock(&tracing_cpumask_update_lock);
  1819. free_cpumask_var(tracing_cpumask_new);
  1820. return count;
  1821. err_unlock:
  1822. mutex_unlock(&tracing_cpumask_update_lock);
  1823. free_cpumask_var(tracing_cpumask);
  1824. return err;
  1825. }
  1826. static const struct file_operations tracing_cpumask_fops = {
  1827. .open = tracing_open_generic,
  1828. .read = tracing_cpumask_read,
  1829. .write = tracing_cpumask_write,
  1830. };
  1831. static ssize_t
  1832. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  1833. size_t cnt, loff_t *ppos)
  1834. {
  1835. struct tracer_opt *trace_opts;
  1836. u32 tracer_flags;
  1837. int len = 0;
  1838. char *buf;
  1839. int r = 0;
  1840. int i;
  1841. /* calculate max size */
  1842. for (i = 0; trace_options[i]; i++) {
  1843. len += strlen(trace_options[i]);
  1844. len += 3; /* "no" and newline */
  1845. }
  1846. mutex_lock(&trace_types_lock);
  1847. tracer_flags = current_trace->flags->val;
  1848. trace_opts = current_trace->flags->opts;
  1849. /*
  1850. * Increase the size with names of options specific
  1851. * of the current tracer.
  1852. */
  1853. for (i = 0; trace_opts[i].name; i++) {
  1854. len += strlen(trace_opts[i].name);
  1855. len += 3; /* "no" and newline */
  1856. }
  1857. /* +2 for \n and \0 */
  1858. buf = kmalloc(len + 2, GFP_KERNEL);
  1859. if (!buf) {
  1860. mutex_unlock(&trace_types_lock);
  1861. return -ENOMEM;
  1862. }
  1863. for (i = 0; trace_options[i]; i++) {
  1864. if (trace_flags & (1 << i))
  1865. r += sprintf(buf + r, "%s\n", trace_options[i]);
  1866. else
  1867. r += sprintf(buf + r, "no%s\n", trace_options[i]);
  1868. }
  1869. for (i = 0; trace_opts[i].name; i++) {
  1870. if (tracer_flags & trace_opts[i].bit)
  1871. r += sprintf(buf + r, "%s\n",
  1872. trace_opts[i].name);
  1873. else
  1874. r += sprintf(buf + r, "no%s\n",
  1875. trace_opts[i].name);
  1876. }
  1877. mutex_unlock(&trace_types_lock);
  1878. WARN_ON(r >= len + 2);
  1879. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1880. kfree(buf);
  1881. return r;
  1882. }
  1883. /* Try to assign a tracer specific option */
  1884. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  1885. {
  1886. struct tracer_flags *trace_flags = trace->flags;
  1887. struct tracer_opt *opts = NULL;
  1888. int ret = 0, i = 0;
  1889. int len;
  1890. for (i = 0; trace_flags->opts[i].name; i++) {
  1891. opts = &trace_flags->opts[i];
  1892. len = strlen(opts->name);
  1893. if (strncmp(cmp, opts->name, len) == 0) {
  1894. ret = trace->set_flag(trace_flags->val,
  1895. opts->bit, !neg);
  1896. break;
  1897. }
  1898. }
  1899. /* Not found */
  1900. if (!trace_flags->opts[i].name)
  1901. return -EINVAL;
  1902. /* Refused to handle */
  1903. if (ret)
  1904. return ret;
  1905. if (neg)
  1906. trace_flags->val &= ~opts->bit;
  1907. else
  1908. trace_flags->val |= opts->bit;
  1909. return 0;
  1910. }
  1911. static void set_tracer_flags(unsigned int mask, int enabled)
  1912. {
  1913. /* do nothing if flag is already set */
  1914. if (!!(trace_flags & mask) == !!enabled)
  1915. return;
  1916. if (enabled)
  1917. trace_flags |= mask;
  1918. else
  1919. trace_flags &= ~mask;
  1920. if (mask == TRACE_ITER_GLOBAL_CLK) {
  1921. u64 (*func)(void);
  1922. if (enabled)
  1923. func = trace_clock_global;
  1924. else
  1925. func = trace_clock_local;
  1926. mutex_lock(&trace_types_lock);
  1927. ring_buffer_set_clock(global_trace.buffer, func);
  1928. if (max_tr.buffer)
  1929. ring_buffer_set_clock(max_tr.buffer, func);
  1930. mutex_unlock(&trace_types_lock);
  1931. }
  1932. }
  1933. static ssize_t
  1934. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  1935. size_t cnt, loff_t *ppos)
  1936. {
  1937. char buf[64];
  1938. char *cmp = buf;
  1939. int neg = 0;
  1940. int ret;
  1941. int i;
  1942. if (cnt >= sizeof(buf))
  1943. return -EINVAL;
  1944. if (copy_from_user(&buf, ubuf, cnt))
  1945. return -EFAULT;
  1946. buf[cnt] = 0;
  1947. if (strncmp(buf, "no", 2) == 0) {
  1948. neg = 1;
  1949. cmp += 2;
  1950. }
  1951. for (i = 0; trace_options[i]; i++) {
  1952. int len = strlen(trace_options[i]);
  1953. if (strncmp(cmp, trace_options[i], len) == 0) {
  1954. set_tracer_flags(1 << i, !neg);
  1955. break;
  1956. }
  1957. }
  1958. /* If no option could be set, test the specific tracer options */
  1959. if (!trace_options[i]) {
  1960. mutex_lock(&trace_types_lock);
  1961. ret = set_tracer_option(current_trace, cmp, neg);
  1962. mutex_unlock(&trace_types_lock);
  1963. if (ret)
  1964. return ret;
  1965. }
  1966. filp->f_pos += cnt;
  1967. return cnt;
  1968. }
  1969. static const struct file_operations tracing_iter_fops = {
  1970. .open = tracing_open_generic,
  1971. .read = tracing_trace_options_read,
  1972. .write = tracing_trace_options_write,
  1973. };
  1974. static const char readme_msg[] =
  1975. "tracing mini-HOWTO:\n\n"
  1976. "# mkdir /debug\n"
  1977. "# mount -t debugfs nodev /debug\n\n"
  1978. "# cat /debug/tracing/available_tracers\n"
  1979. "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
  1980. "# cat /debug/tracing/current_tracer\n"
  1981. "nop\n"
  1982. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1983. "# cat /debug/tracing/current_tracer\n"
  1984. "sched_switch\n"
  1985. "# cat /debug/tracing/trace_options\n"
  1986. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1987. "# echo print-parent > /debug/tracing/trace_options\n"
  1988. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1989. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1990. "echo 0 > /debug/tracing/tracing_enabled\n"
  1991. ;
  1992. static ssize_t
  1993. tracing_readme_read(struct file *filp, char __user *ubuf,
  1994. size_t cnt, loff_t *ppos)
  1995. {
  1996. return simple_read_from_buffer(ubuf, cnt, ppos,
  1997. readme_msg, strlen(readme_msg));
  1998. }
  1999. static const struct file_operations tracing_readme_fops = {
  2000. .open = tracing_open_generic,
  2001. .read = tracing_readme_read,
  2002. };
  2003. static ssize_t
  2004. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2005. size_t cnt, loff_t *ppos)
  2006. {
  2007. char *buf_comm;
  2008. char *file_buf;
  2009. char *buf;
  2010. int len = 0;
  2011. int pid;
  2012. int i;
  2013. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2014. if (!file_buf)
  2015. return -ENOMEM;
  2016. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2017. if (!buf_comm) {
  2018. kfree(file_buf);
  2019. return -ENOMEM;
  2020. }
  2021. buf = file_buf;
  2022. for (i = 0; i < SAVED_CMDLINES; i++) {
  2023. int r;
  2024. pid = map_cmdline_to_pid[i];
  2025. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2026. continue;
  2027. trace_find_cmdline(pid, buf_comm);
  2028. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2029. buf += r;
  2030. len += r;
  2031. }
  2032. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2033. file_buf, len);
  2034. kfree(file_buf);
  2035. kfree(buf_comm);
  2036. return len;
  2037. }
  2038. static const struct file_operations tracing_saved_cmdlines_fops = {
  2039. .open = tracing_open_generic,
  2040. .read = tracing_saved_cmdlines_read,
  2041. };
  2042. static ssize_t
  2043. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2044. size_t cnt, loff_t *ppos)
  2045. {
  2046. char buf[64];
  2047. int r;
  2048. r = sprintf(buf, "%u\n", tracer_enabled);
  2049. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2050. }
  2051. static ssize_t
  2052. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2053. size_t cnt, loff_t *ppos)
  2054. {
  2055. struct trace_array *tr = filp->private_data;
  2056. char buf[64];
  2057. unsigned long val;
  2058. int ret;
  2059. if (cnt >= sizeof(buf))
  2060. return -EINVAL;
  2061. if (copy_from_user(&buf, ubuf, cnt))
  2062. return -EFAULT;
  2063. buf[cnt] = 0;
  2064. ret = strict_strtoul(buf, 10, &val);
  2065. if (ret < 0)
  2066. return ret;
  2067. val = !!val;
  2068. mutex_lock(&trace_types_lock);
  2069. if (tracer_enabled ^ val) {
  2070. if (val) {
  2071. tracer_enabled = 1;
  2072. if (current_trace->start)
  2073. current_trace->start(tr);
  2074. tracing_start();
  2075. } else {
  2076. tracer_enabled = 0;
  2077. tracing_stop();
  2078. if (current_trace->stop)
  2079. current_trace->stop(tr);
  2080. }
  2081. }
  2082. mutex_unlock(&trace_types_lock);
  2083. filp->f_pos += cnt;
  2084. return cnt;
  2085. }
  2086. static ssize_t
  2087. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2088. size_t cnt, loff_t *ppos)
  2089. {
  2090. char buf[max_tracer_type_len+2];
  2091. int r;
  2092. mutex_lock(&trace_types_lock);
  2093. if (current_trace)
  2094. r = sprintf(buf, "%s\n", current_trace->name);
  2095. else
  2096. r = sprintf(buf, "\n");
  2097. mutex_unlock(&trace_types_lock);
  2098. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2099. }
  2100. int tracer_init(struct tracer *t, struct trace_array *tr)
  2101. {
  2102. tracing_reset_online_cpus(tr);
  2103. return t->init(tr);
  2104. }
  2105. static int tracing_resize_ring_buffer(unsigned long size)
  2106. {
  2107. int ret;
  2108. /*
  2109. * If kernel or user changes the size of the ring buffer
  2110. * we use the size that was given, and we can forget about
  2111. * expanding it later.
  2112. */
  2113. ring_buffer_expanded = 1;
  2114. ret = ring_buffer_resize(global_trace.buffer, size);
  2115. if (ret < 0)
  2116. return ret;
  2117. ret = ring_buffer_resize(max_tr.buffer, size);
  2118. if (ret < 0) {
  2119. int r;
  2120. r = ring_buffer_resize(global_trace.buffer,
  2121. global_trace.entries);
  2122. if (r < 0) {
  2123. /*
  2124. * AARGH! We are left with different
  2125. * size max buffer!!!!
  2126. * The max buffer is our "snapshot" buffer.
  2127. * When a tracer needs a snapshot (one of the
  2128. * latency tracers), it swaps the max buffer
  2129. * with the saved snap shot. We succeeded to
  2130. * update the size of the main buffer, but failed to
  2131. * update the size of the max buffer. But when we tried
  2132. * to reset the main buffer to the original size, we
  2133. * failed there too. This is very unlikely to
  2134. * happen, but if it does, warn and kill all
  2135. * tracing.
  2136. */
  2137. WARN_ON(1);
  2138. tracing_disabled = 1;
  2139. }
  2140. return ret;
  2141. }
  2142. global_trace.entries = size;
  2143. return ret;
  2144. }
  2145. /**
  2146. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2147. *
  2148. * To save on memory when the tracing is never used on a system with it
  2149. * configured in. The ring buffers are set to a minimum size. But once
  2150. * a user starts to use the tracing facility, then they need to grow
  2151. * to their default size.
  2152. *
  2153. * This function is to be called when a tracer is about to be used.
  2154. */
  2155. int tracing_update_buffers(void)
  2156. {
  2157. int ret = 0;
  2158. mutex_lock(&trace_types_lock);
  2159. if (!ring_buffer_expanded)
  2160. ret = tracing_resize_ring_buffer(trace_buf_size);
  2161. mutex_unlock(&trace_types_lock);
  2162. return ret;
  2163. }
  2164. struct trace_option_dentry;
  2165. static struct trace_option_dentry *
  2166. create_trace_option_files(struct tracer *tracer);
  2167. static void
  2168. destroy_trace_option_files(struct trace_option_dentry *topts);
  2169. static int tracing_set_tracer(const char *buf)
  2170. {
  2171. static struct trace_option_dentry *topts;
  2172. struct trace_array *tr = &global_trace;
  2173. struct tracer *t;
  2174. int ret = 0;
  2175. mutex_lock(&trace_types_lock);
  2176. if (!ring_buffer_expanded) {
  2177. ret = tracing_resize_ring_buffer(trace_buf_size);
  2178. if (ret < 0)
  2179. goto out;
  2180. ret = 0;
  2181. }
  2182. for (t = trace_types; t; t = t->next) {
  2183. if (strcmp(t->name, buf) == 0)
  2184. break;
  2185. }
  2186. if (!t) {
  2187. ret = -EINVAL;
  2188. goto out;
  2189. }
  2190. if (t == current_trace)
  2191. goto out;
  2192. trace_branch_disable();
  2193. if (current_trace && current_trace->reset)
  2194. current_trace->reset(tr);
  2195. destroy_trace_option_files(topts);
  2196. current_trace = t;
  2197. topts = create_trace_option_files(current_trace);
  2198. if (t->init) {
  2199. ret = tracer_init(t, tr);
  2200. if (ret)
  2201. goto out;
  2202. }
  2203. trace_branch_enable(tr);
  2204. out:
  2205. mutex_unlock(&trace_types_lock);
  2206. return ret;
  2207. }
  2208. static ssize_t
  2209. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2210. size_t cnt, loff_t *ppos)
  2211. {
  2212. char buf[max_tracer_type_len+1];
  2213. int i;
  2214. size_t ret;
  2215. int err;
  2216. ret = cnt;
  2217. if (cnt > max_tracer_type_len)
  2218. cnt = max_tracer_type_len;
  2219. if (copy_from_user(&buf, ubuf, cnt))
  2220. return -EFAULT;
  2221. buf[cnt] = 0;
  2222. /* strip ending whitespace. */
  2223. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2224. buf[i] = 0;
  2225. err = tracing_set_tracer(buf);
  2226. if (err)
  2227. return err;
  2228. filp->f_pos += ret;
  2229. return ret;
  2230. }
  2231. static ssize_t
  2232. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2233. size_t cnt, loff_t *ppos)
  2234. {
  2235. unsigned long *ptr = filp->private_data;
  2236. char buf[64];
  2237. int r;
  2238. r = snprintf(buf, sizeof(buf), "%ld\n",
  2239. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2240. if (r > sizeof(buf))
  2241. r = sizeof(buf);
  2242. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2243. }
  2244. static ssize_t
  2245. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2246. size_t cnt, loff_t *ppos)
  2247. {
  2248. unsigned long *ptr = filp->private_data;
  2249. char buf[64];
  2250. unsigned long val;
  2251. int ret;
  2252. if (cnt >= sizeof(buf))
  2253. return -EINVAL;
  2254. if (copy_from_user(&buf, ubuf, cnt))
  2255. return -EFAULT;
  2256. buf[cnt] = 0;
  2257. ret = strict_strtoul(buf, 10, &val);
  2258. if (ret < 0)
  2259. return ret;
  2260. *ptr = val * 1000;
  2261. return cnt;
  2262. }
  2263. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2264. {
  2265. long cpu_file = (long) inode->i_private;
  2266. struct trace_iterator *iter;
  2267. int ret = 0;
  2268. if (tracing_disabled)
  2269. return -ENODEV;
  2270. mutex_lock(&trace_types_lock);
  2271. /* We only allow one reader per cpu */
  2272. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  2273. if (!cpumask_empty(tracing_reader_cpumask)) {
  2274. ret = -EBUSY;
  2275. goto out;
  2276. }
  2277. cpumask_setall(tracing_reader_cpumask);
  2278. } else {
  2279. if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
  2280. cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
  2281. else {
  2282. ret = -EBUSY;
  2283. goto out;
  2284. }
  2285. }
  2286. /* create a buffer to store the information to pass to userspace */
  2287. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2288. if (!iter) {
  2289. ret = -ENOMEM;
  2290. goto out;
  2291. }
  2292. /*
  2293. * We make a copy of the current tracer to avoid concurrent
  2294. * changes on it while we are reading.
  2295. */
  2296. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2297. if (!iter->trace) {
  2298. ret = -ENOMEM;
  2299. goto fail;
  2300. }
  2301. if (current_trace)
  2302. *iter->trace = *current_trace;
  2303. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2304. ret = -ENOMEM;
  2305. goto fail;
  2306. }
  2307. /* trace pipe does not show start of buffer */
  2308. cpumask_setall(iter->started);
  2309. iter->cpu_file = cpu_file;
  2310. iter->tr = &global_trace;
  2311. mutex_init(&iter->mutex);
  2312. filp->private_data = iter;
  2313. if (iter->trace->pipe_open)
  2314. iter->trace->pipe_open(iter);
  2315. out:
  2316. mutex_unlock(&trace_types_lock);
  2317. return ret;
  2318. fail:
  2319. kfree(iter->trace);
  2320. kfree(iter);
  2321. mutex_unlock(&trace_types_lock);
  2322. return ret;
  2323. }
  2324. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2325. {
  2326. struct trace_iterator *iter = file->private_data;
  2327. mutex_lock(&trace_types_lock);
  2328. if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
  2329. cpumask_clear(tracing_reader_cpumask);
  2330. else
  2331. cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
  2332. mutex_unlock(&trace_types_lock);
  2333. free_cpumask_var(iter->started);
  2334. mutex_destroy(&iter->mutex);
  2335. kfree(iter->trace);
  2336. kfree(iter);
  2337. return 0;
  2338. }
  2339. static unsigned int
  2340. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2341. {
  2342. struct trace_iterator *iter = filp->private_data;
  2343. if (trace_flags & TRACE_ITER_BLOCK) {
  2344. /*
  2345. * Always select as readable when in blocking mode
  2346. */
  2347. return POLLIN | POLLRDNORM;
  2348. } else {
  2349. if (!trace_empty(iter))
  2350. return POLLIN | POLLRDNORM;
  2351. poll_wait(filp, &trace_wait, poll_table);
  2352. if (!trace_empty(iter))
  2353. return POLLIN | POLLRDNORM;
  2354. return 0;
  2355. }
  2356. }
  2357. void default_wait_pipe(struct trace_iterator *iter)
  2358. {
  2359. DEFINE_WAIT(wait);
  2360. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2361. if (trace_empty(iter))
  2362. schedule();
  2363. finish_wait(&trace_wait, &wait);
  2364. }
  2365. /*
  2366. * This is a make-shift waitqueue.
  2367. * A tracer might use this callback on some rare cases:
  2368. *
  2369. * 1) the current tracer might hold the runqueue lock when it wakes up
  2370. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2371. * 2) the function tracers, trace all functions, we don't want
  2372. * the overhead of calling wake_up and friends
  2373. * (and tracing them too)
  2374. *
  2375. * Anyway, this is really very primitive wakeup.
  2376. */
  2377. void poll_wait_pipe(struct trace_iterator *iter)
  2378. {
  2379. set_current_state(TASK_INTERRUPTIBLE);
  2380. /* sleep for 100 msecs, and try again. */
  2381. schedule_timeout(HZ / 10);
  2382. }
  2383. /* Must be called with trace_types_lock mutex held. */
  2384. static int tracing_wait_pipe(struct file *filp)
  2385. {
  2386. struct trace_iterator *iter = filp->private_data;
  2387. while (trace_empty(iter)) {
  2388. if ((filp->f_flags & O_NONBLOCK)) {
  2389. return -EAGAIN;
  2390. }
  2391. mutex_unlock(&iter->mutex);
  2392. iter->trace->wait_pipe(iter);
  2393. mutex_lock(&iter->mutex);
  2394. if (signal_pending(current))
  2395. return -EINTR;
  2396. /*
  2397. * We block until we read something and tracing is disabled.
  2398. * We still block if tracing is disabled, but we have never
  2399. * read anything. This allows a user to cat this file, and
  2400. * then enable tracing. But after we have read something,
  2401. * we give an EOF when tracing is again disabled.
  2402. *
  2403. * iter->pos will be 0 if we haven't read anything.
  2404. */
  2405. if (!tracer_enabled && iter->pos)
  2406. break;
  2407. }
  2408. return 1;
  2409. }
  2410. /*
  2411. * Consumer reader.
  2412. */
  2413. static ssize_t
  2414. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2415. size_t cnt, loff_t *ppos)
  2416. {
  2417. struct trace_iterator *iter = filp->private_data;
  2418. static struct tracer *old_tracer;
  2419. ssize_t sret;
  2420. /* return any leftover data */
  2421. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2422. if (sret != -EBUSY)
  2423. return sret;
  2424. trace_seq_init(&iter->seq);
  2425. /* copy the tracer to avoid using a global lock all around */
  2426. mutex_lock(&trace_types_lock);
  2427. if (unlikely(old_tracer != current_trace && current_trace)) {
  2428. old_tracer = current_trace;
  2429. *iter->trace = *current_trace;
  2430. }
  2431. mutex_unlock(&trace_types_lock);
  2432. /*
  2433. * Avoid more than one consumer on a single file descriptor
  2434. * This is just a matter of traces coherency, the ring buffer itself
  2435. * is protected.
  2436. */
  2437. mutex_lock(&iter->mutex);
  2438. if (iter->trace->read) {
  2439. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2440. if (sret)
  2441. goto out;
  2442. }
  2443. waitagain:
  2444. sret = tracing_wait_pipe(filp);
  2445. if (sret <= 0)
  2446. goto out;
  2447. /* stop when tracing is finished */
  2448. if (trace_empty(iter)) {
  2449. sret = 0;
  2450. goto out;
  2451. }
  2452. if (cnt >= PAGE_SIZE)
  2453. cnt = PAGE_SIZE - 1;
  2454. /* reset all but tr, trace, and overruns */
  2455. memset(&iter->seq, 0,
  2456. sizeof(struct trace_iterator) -
  2457. offsetof(struct trace_iterator, seq));
  2458. iter->pos = -1;
  2459. while (find_next_entry_inc(iter) != NULL) {
  2460. enum print_line_t ret;
  2461. int len = iter->seq.len;
  2462. ret = print_trace_line(iter);
  2463. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2464. /* don't print partial lines */
  2465. iter->seq.len = len;
  2466. break;
  2467. }
  2468. if (ret != TRACE_TYPE_NO_CONSUME)
  2469. trace_consume(iter);
  2470. if (iter->seq.len >= cnt)
  2471. break;
  2472. }
  2473. /* Now copy what we have to the user */
  2474. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2475. if (iter->seq.readpos >= iter->seq.len)
  2476. trace_seq_init(&iter->seq);
  2477. /*
  2478. * If there was nothing to send to user, inspite of consuming trace
  2479. * entries, go back to wait for more entries.
  2480. */
  2481. if (sret == -EBUSY)
  2482. goto waitagain;
  2483. out:
  2484. mutex_unlock(&iter->mutex);
  2485. return sret;
  2486. }
  2487. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2488. struct pipe_buffer *buf)
  2489. {
  2490. __free_page(buf->page);
  2491. }
  2492. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2493. unsigned int idx)
  2494. {
  2495. __free_page(spd->pages[idx]);
  2496. }
  2497. static struct pipe_buf_operations tracing_pipe_buf_ops = {
  2498. .can_merge = 0,
  2499. .map = generic_pipe_buf_map,
  2500. .unmap = generic_pipe_buf_unmap,
  2501. .confirm = generic_pipe_buf_confirm,
  2502. .release = tracing_pipe_buf_release,
  2503. .steal = generic_pipe_buf_steal,
  2504. .get = generic_pipe_buf_get,
  2505. };
  2506. static size_t
  2507. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2508. {
  2509. size_t count;
  2510. int ret;
  2511. /* Seq buffer is page-sized, exactly what we need. */
  2512. for (;;) {
  2513. count = iter->seq.len;
  2514. ret = print_trace_line(iter);
  2515. count = iter->seq.len - count;
  2516. if (rem < count) {
  2517. rem = 0;
  2518. iter->seq.len -= count;
  2519. break;
  2520. }
  2521. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2522. iter->seq.len -= count;
  2523. break;
  2524. }
  2525. trace_consume(iter);
  2526. rem -= count;
  2527. if (!find_next_entry_inc(iter)) {
  2528. rem = 0;
  2529. iter->ent = NULL;
  2530. break;
  2531. }
  2532. }
  2533. return rem;
  2534. }
  2535. static ssize_t tracing_splice_read_pipe(struct file *filp,
  2536. loff_t *ppos,
  2537. struct pipe_inode_info *pipe,
  2538. size_t len,
  2539. unsigned int flags)
  2540. {
  2541. struct page *pages[PIPE_BUFFERS];
  2542. struct partial_page partial[PIPE_BUFFERS];
  2543. struct trace_iterator *iter = filp->private_data;
  2544. struct splice_pipe_desc spd = {
  2545. .pages = pages,
  2546. .partial = partial,
  2547. .nr_pages = 0, /* This gets updated below. */
  2548. .flags = flags,
  2549. .ops = &tracing_pipe_buf_ops,
  2550. .spd_release = tracing_spd_release_pipe,
  2551. };
  2552. static struct tracer *old_tracer;
  2553. ssize_t ret;
  2554. size_t rem;
  2555. unsigned int i;
  2556. /* copy the tracer to avoid using a global lock all around */
  2557. mutex_lock(&trace_types_lock);
  2558. if (unlikely(old_tracer != current_trace && current_trace)) {
  2559. old_tracer = current_trace;
  2560. *iter->trace = *current_trace;
  2561. }
  2562. mutex_unlock(&trace_types_lock);
  2563. mutex_lock(&iter->mutex);
  2564. if (iter->trace->splice_read) {
  2565. ret = iter->trace->splice_read(iter, filp,
  2566. ppos, pipe, len, flags);
  2567. if (ret)
  2568. goto out_err;
  2569. }
  2570. ret = tracing_wait_pipe(filp);
  2571. if (ret <= 0)
  2572. goto out_err;
  2573. if (!iter->ent && !find_next_entry_inc(iter)) {
  2574. ret = -EFAULT;
  2575. goto out_err;
  2576. }
  2577. /* Fill as many pages as possible. */
  2578. for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
  2579. pages[i] = alloc_page(GFP_KERNEL);
  2580. if (!pages[i])
  2581. break;
  2582. rem = tracing_fill_pipe_page(rem, iter);
  2583. /* Copy the data into the page, so we can start over. */
  2584. ret = trace_seq_to_buffer(&iter->seq,
  2585. page_address(pages[i]),
  2586. iter->seq.len);
  2587. if (ret < 0) {
  2588. __free_page(pages[i]);
  2589. break;
  2590. }
  2591. partial[i].offset = 0;
  2592. partial[i].len = iter->seq.len;
  2593. trace_seq_init(&iter->seq);
  2594. }
  2595. mutex_unlock(&iter->mutex);
  2596. spd.nr_pages = i;
  2597. return splice_to_pipe(pipe, &spd);
  2598. out_err:
  2599. mutex_unlock(&iter->mutex);
  2600. return ret;
  2601. }
  2602. static ssize_t
  2603. tracing_entries_read(struct file *filp, char __user *ubuf,
  2604. size_t cnt, loff_t *ppos)
  2605. {
  2606. struct trace_array *tr = filp->private_data;
  2607. char buf[96];
  2608. int r;
  2609. mutex_lock(&trace_types_lock);
  2610. if (!ring_buffer_expanded)
  2611. r = sprintf(buf, "%lu (expanded: %lu)\n",
  2612. tr->entries >> 10,
  2613. trace_buf_size >> 10);
  2614. else
  2615. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2616. mutex_unlock(&trace_types_lock);
  2617. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2618. }
  2619. static ssize_t
  2620. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2621. size_t cnt, loff_t *ppos)
  2622. {
  2623. unsigned long val;
  2624. char buf[64];
  2625. int ret, cpu;
  2626. if (cnt >= sizeof(buf))
  2627. return -EINVAL;
  2628. if (copy_from_user(&buf, ubuf, cnt))
  2629. return -EFAULT;
  2630. buf[cnt] = 0;
  2631. ret = strict_strtoul(buf, 10, &val);
  2632. if (ret < 0)
  2633. return ret;
  2634. /* must have at least 1 entry */
  2635. if (!val)
  2636. return -EINVAL;
  2637. mutex_lock(&trace_types_lock);
  2638. tracing_stop();
  2639. /* disable all cpu buffers */
  2640. for_each_tracing_cpu(cpu) {
  2641. if (global_trace.data[cpu])
  2642. atomic_inc(&global_trace.data[cpu]->disabled);
  2643. if (max_tr.data[cpu])
  2644. atomic_inc(&max_tr.data[cpu]->disabled);
  2645. }
  2646. /* value is in KB */
  2647. val <<= 10;
  2648. if (val != global_trace.entries) {
  2649. ret = tracing_resize_ring_buffer(val);
  2650. if (ret < 0) {
  2651. cnt = ret;
  2652. goto out;
  2653. }
  2654. }
  2655. filp->f_pos += cnt;
  2656. /* If check pages failed, return ENOMEM */
  2657. if (tracing_disabled)
  2658. cnt = -ENOMEM;
  2659. out:
  2660. for_each_tracing_cpu(cpu) {
  2661. if (global_trace.data[cpu])
  2662. atomic_dec(&global_trace.data[cpu]->disabled);
  2663. if (max_tr.data[cpu])
  2664. atomic_dec(&max_tr.data[cpu]->disabled);
  2665. }
  2666. tracing_start();
  2667. max_tr.entries = global_trace.entries;
  2668. mutex_unlock(&trace_types_lock);
  2669. return cnt;
  2670. }
  2671. static int mark_printk(const char *fmt, ...)
  2672. {
  2673. int ret;
  2674. va_list args;
  2675. va_start(args, fmt);
  2676. ret = trace_vprintk(0, fmt, args);
  2677. va_end(args);
  2678. return ret;
  2679. }
  2680. static ssize_t
  2681. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2682. size_t cnt, loff_t *fpos)
  2683. {
  2684. char *buf;
  2685. char *end;
  2686. if (tracing_disabled)
  2687. return -EINVAL;
  2688. if (cnt > TRACE_BUF_SIZE)
  2689. cnt = TRACE_BUF_SIZE;
  2690. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2691. if (buf == NULL)
  2692. return -ENOMEM;
  2693. if (copy_from_user(buf, ubuf, cnt)) {
  2694. kfree(buf);
  2695. return -EFAULT;
  2696. }
  2697. /* Cut from the first nil or newline. */
  2698. buf[cnt] = '\0';
  2699. end = strchr(buf, '\n');
  2700. if (end)
  2701. *end = '\0';
  2702. cnt = mark_printk("%s\n", buf);
  2703. kfree(buf);
  2704. *fpos += cnt;
  2705. return cnt;
  2706. }
  2707. static const struct file_operations tracing_max_lat_fops = {
  2708. .open = tracing_open_generic,
  2709. .read = tracing_max_lat_read,
  2710. .write = tracing_max_lat_write,
  2711. };
  2712. static const struct file_operations tracing_ctrl_fops = {
  2713. .open = tracing_open_generic,
  2714. .read = tracing_ctrl_read,
  2715. .write = tracing_ctrl_write,
  2716. };
  2717. static const struct file_operations set_tracer_fops = {
  2718. .open = tracing_open_generic,
  2719. .read = tracing_set_trace_read,
  2720. .write = tracing_set_trace_write,
  2721. };
  2722. static const struct file_operations tracing_pipe_fops = {
  2723. .open = tracing_open_pipe,
  2724. .poll = tracing_poll_pipe,
  2725. .read = tracing_read_pipe,
  2726. .splice_read = tracing_splice_read_pipe,
  2727. .release = tracing_release_pipe,
  2728. };
  2729. static const struct file_operations tracing_entries_fops = {
  2730. .open = tracing_open_generic,
  2731. .read = tracing_entries_read,
  2732. .write = tracing_entries_write,
  2733. };
  2734. static const struct file_operations tracing_mark_fops = {
  2735. .open = tracing_open_generic,
  2736. .write = tracing_mark_write,
  2737. };
  2738. struct ftrace_buffer_info {
  2739. struct trace_array *tr;
  2740. void *spare;
  2741. int cpu;
  2742. unsigned int read;
  2743. };
  2744. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  2745. {
  2746. int cpu = (int)(long)inode->i_private;
  2747. struct ftrace_buffer_info *info;
  2748. if (tracing_disabled)
  2749. return -ENODEV;
  2750. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2751. if (!info)
  2752. return -ENOMEM;
  2753. info->tr = &global_trace;
  2754. info->cpu = cpu;
  2755. info->spare = NULL;
  2756. /* Force reading ring buffer for first read */
  2757. info->read = (unsigned int)-1;
  2758. filp->private_data = info;
  2759. return nonseekable_open(inode, filp);
  2760. }
  2761. static ssize_t
  2762. tracing_buffers_read(struct file *filp, char __user *ubuf,
  2763. size_t count, loff_t *ppos)
  2764. {
  2765. struct ftrace_buffer_info *info = filp->private_data;
  2766. unsigned int pos;
  2767. ssize_t ret;
  2768. size_t size;
  2769. if (!count)
  2770. return 0;
  2771. if (!info->spare)
  2772. info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
  2773. if (!info->spare)
  2774. return -ENOMEM;
  2775. /* Do we have previous read data to read? */
  2776. if (info->read < PAGE_SIZE)
  2777. goto read;
  2778. info->read = 0;
  2779. ret = ring_buffer_read_page(info->tr->buffer,
  2780. &info->spare,
  2781. count,
  2782. info->cpu, 0);
  2783. if (ret < 0)
  2784. return 0;
  2785. pos = ring_buffer_page_len(info->spare);
  2786. if (pos < PAGE_SIZE)
  2787. memset(info->spare + pos, 0, PAGE_SIZE - pos);
  2788. read:
  2789. size = PAGE_SIZE - info->read;
  2790. if (size > count)
  2791. size = count;
  2792. ret = copy_to_user(ubuf, info->spare + info->read, size);
  2793. if (ret == size)
  2794. return -EFAULT;
  2795. size -= ret;
  2796. *ppos += size;
  2797. info->read += size;
  2798. return size;
  2799. }
  2800. static int tracing_buffers_release(struct inode *inode, struct file *file)
  2801. {
  2802. struct ftrace_buffer_info *info = file->private_data;
  2803. if (info->spare)
  2804. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  2805. kfree(info);
  2806. return 0;
  2807. }
  2808. struct buffer_ref {
  2809. struct ring_buffer *buffer;
  2810. void *page;
  2811. int ref;
  2812. };
  2813. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  2814. struct pipe_buffer *buf)
  2815. {
  2816. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2817. if (--ref->ref)
  2818. return;
  2819. ring_buffer_free_read_page(ref->buffer, ref->page);
  2820. kfree(ref);
  2821. buf->private = 0;
  2822. }
  2823. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  2824. struct pipe_buffer *buf)
  2825. {
  2826. return 1;
  2827. }
  2828. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  2829. struct pipe_buffer *buf)
  2830. {
  2831. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2832. ref->ref++;
  2833. }
  2834. /* Pipe buffer operations for a buffer. */
  2835. static struct pipe_buf_operations buffer_pipe_buf_ops = {
  2836. .can_merge = 0,
  2837. .map = generic_pipe_buf_map,
  2838. .unmap = generic_pipe_buf_unmap,
  2839. .confirm = generic_pipe_buf_confirm,
  2840. .release = buffer_pipe_buf_release,
  2841. .steal = buffer_pipe_buf_steal,
  2842. .get = buffer_pipe_buf_get,
  2843. };
  2844. /*
  2845. * Callback from splice_to_pipe(), if we need to release some pages
  2846. * at the end of the spd in case we error'ed out in filling the pipe.
  2847. */
  2848. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  2849. {
  2850. struct buffer_ref *ref =
  2851. (struct buffer_ref *)spd->partial[i].private;
  2852. if (--ref->ref)
  2853. return;
  2854. ring_buffer_free_read_page(ref->buffer, ref->page);
  2855. kfree(ref);
  2856. spd->partial[i].private = 0;
  2857. }
  2858. static ssize_t
  2859. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  2860. struct pipe_inode_info *pipe, size_t len,
  2861. unsigned int flags)
  2862. {
  2863. struct ftrace_buffer_info *info = file->private_data;
  2864. struct partial_page partial[PIPE_BUFFERS];
  2865. struct page *pages[PIPE_BUFFERS];
  2866. struct splice_pipe_desc spd = {
  2867. .pages = pages,
  2868. .partial = partial,
  2869. .flags = flags,
  2870. .ops = &buffer_pipe_buf_ops,
  2871. .spd_release = buffer_spd_release,
  2872. };
  2873. struct buffer_ref *ref;
  2874. int size, i;
  2875. size_t ret;
  2876. if (*ppos & (PAGE_SIZE - 1)) {
  2877. WARN_ONCE(1, "Ftrace: previous read must page-align\n");
  2878. return -EINVAL;
  2879. }
  2880. if (len & (PAGE_SIZE - 1)) {
  2881. WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
  2882. if (len < PAGE_SIZE)
  2883. return -EINVAL;
  2884. len &= PAGE_MASK;
  2885. }
  2886. for (i = 0; i < PIPE_BUFFERS && len; i++, len -= PAGE_SIZE) {
  2887. struct page *page;
  2888. int r;
  2889. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  2890. if (!ref)
  2891. break;
  2892. ref->buffer = info->tr->buffer;
  2893. ref->page = ring_buffer_alloc_read_page(ref->buffer);
  2894. if (!ref->page) {
  2895. kfree(ref);
  2896. break;
  2897. }
  2898. r = ring_buffer_read_page(ref->buffer, &ref->page,
  2899. len, info->cpu, 0);
  2900. if (r < 0) {
  2901. ring_buffer_free_read_page(ref->buffer,
  2902. ref->page);
  2903. kfree(ref);
  2904. break;
  2905. }
  2906. /*
  2907. * zero out any left over data, this is going to
  2908. * user land.
  2909. */
  2910. size = ring_buffer_page_len(ref->page);
  2911. if (size < PAGE_SIZE)
  2912. memset(ref->page + size, 0, PAGE_SIZE - size);
  2913. page = virt_to_page(ref->page);
  2914. spd.pages[i] = page;
  2915. spd.partial[i].len = PAGE_SIZE;
  2916. spd.partial[i].offset = 0;
  2917. spd.partial[i].private = (unsigned long)ref;
  2918. spd.nr_pages++;
  2919. *ppos += PAGE_SIZE;
  2920. }
  2921. spd.nr_pages = i;
  2922. /* did we read anything? */
  2923. if (!spd.nr_pages) {
  2924. if (flags & SPLICE_F_NONBLOCK)
  2925. ret = -EAGAIN;
  2926. else
  2927. ret = 0;
  2928. /* TODO: block */
  2929. return ret;
  2930. }
  2931. ret = splice_to_pipe(pipe, &spd);
  2932. return ret;
  2933. }
  2934. static const struct file_operations tracing_buffers_fops = {
  2935. .open = tracing_buffers_open,
  2936. .read = tracing_buffers_read,
  2937. .release = tracing_buffers_release,
  2938. .splice_read = tracing_buffers_splice_read,
  2939. .llseek = no_llseek,
  2940. };
  2941. #ifdef CONFIG_DYNAMIC_FTRACE
  2942. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2943. {
  2944. return 0;
  2945. }
  2946. static ssize_t
  2947. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2948. size_t cnt, loff_t *ppos)
  2949. {
  2950. static char ftrace_dyn_info_buffer[1024];
  2951. static DEFINE_MUTEX(dyn_info_mutex);
  2952. unsigned long *p = filp->private_data;
  2953. char *buf = ftrace_dyn_info_buffer;
  2954. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2955. int r;
  2956. mutex_lock(&dyn_info_mutex);
  2957. r = sprintf(buf, "%ld ", *p);
  2958. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2959. buf[r++] = '\n';
  2960. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2961. mutex_unlock(&dyn_info_mutex);
  2962. return r;
  2963. }
  2964. static const struct file_operations tracing_dyn_info_fops = {
  2965. .open = tracing_open_generic,
  2966. .read = tracing_read_dyn_info,
  2967. };
  2968. #endif
  2969. static struct dentry *d_tracer;
  2970. struct dentry *tracing_init_dentry(void)
  2971. {
  2972. static int once;
  2973. if (d_tracer)
  2974. return d_tracer;
  2975. if (!debugfs_initialized())
  2976. return NULL;
  2977. d_tracer = debugfs_create_dir("tracing", NULL);
  2978. if (!d_tracer && !once) {
  2979. once = 1;
  2980. pr_warning("Could not create debugfs directory 'tracing'\n");
  2981. return NULL;
  2982. }
  2983. return d_tracer;
  2984. }
  2985. static struct dentry *d_percpu;
  2986. struct dentry *tracing_dentry_percpu(void)
  2987. {
  2988. static int once;
  2989. struct dentry *d_tracer;
  2990. if (d_percpu)
  2991. return d_percpu;
  2992. d_tracer = tracing_init_dentry();
  2993. if (!d_tracer)
  2994. return NULL;
  2995. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  2996. if (!d_percpu && !once) {
  2997. once = 1;
  2998. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  2999. return NULL;
  3000. }
  3001. return d_percpu;
  3002. }
  3003. static void tracing_init_debugfs_percpu(long cpu)
  3004. {
  3005. struct dentry *d_percpu = tracing_dentry_percpu();
  3006. struct dentry *d_cpu;
  3007. /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
  3008. char cpu_dir[7];
  3009. if (cpu > 999 || cpu < 0)
  3010. return;
  3011. sprintf(cpu_dir, "cpu%ld", cpu);
  3012. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  3013. if (!d_cpu) {
  3014. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  3015. return;
  3016. }
  3017. /* per cpu trace_pipe */
  3018. trace_create_file("trace_pipe", 0444, d_cpu,
  3019. (void *) cpu, &tracing_pipe_fops);
  3020. /* per cpu trace */
  3021. trace_create_file("trace", 0644, d_cpu,
  3022. (void *) cpu, &tracing_fops);
  3023. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  3024. (void *) cpu, &tracing_buffers_fops);
  3025. }
  3026. #ifdef CONFIG_FTRACE_SELFTEST
  3027. /* Let selftest have access to static functions in this file */
  3028. #include "trace_selftest.c"
  3029. #endif
  3030. struct trace_option_dentry {
  3031. struct tracer_opt *opt;
  3032. struct tracer_flags *flags;
  3033. struct dentry *entry;
  3034. };
  3035. static ssize_t
  3036. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  3037. loff_t *ppos)
  3038. {
  3039. struct trace_option_dentry *topt = filp->private_data;
  3040. char *buf;
  3041. if (topt->flags->val & topt->opt->bit)
  3042. buf = "1\n";
  3043. else
  3044. buf = "0\n";
  3045. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3046. }
  3047. static ssize_t
  3048. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3049. loff_t *ppos)
  3050. {
  3051. struct trace_option_dentry *topt = filp->private_data;
  3052. unsigned long val;
  3053. char buf[64];
  3054. int ret;
  3055. if (cnt >= sizeof(buf))
  3056. return -EINVAL;
  3057. if (copy_from_user(&buf, ubuf, cnt))
  3058. return -EFAULT;
  3059. buf[cnt] = 0;
  3060. ret = strict_strtoul(buf, 10, &val);
  3061. if (ret < 0)
  3062. return ret;
  3063. ret = 0;
  3064. switch (val) {
  3065. case 0:
  3066. /* do nothing if already cleared */
  3067. if (!(topt->flags->val & topt->opt->bit))
  3068. break;
  3069. mutex_lock(&trace_types_lock);
  3070. if (current_trace->set_flag)
  3071. ret = current_trace->set_flag(topt->flags->val,
  3072. topt->opt->bit, 0);
  3073. mutex_unlock(&trace_types_lock);
  3074. if (ret)
  3075. return ret;
  3076. topt->flags->val &= ~topt->opt->bit;
  3077. break;
  3078. case 1:
  3079. /* do nothing if already set */
  3080. if (topt->flags->val & topt->opt->bit)
  3081. break;
  3082. mutex_lock(&trace_types_lock);
  3083. if (current_trace->set_flag)
  3084. ret = current_trace->set_flag(topt->flags->val,
  3085. topt->opt->bit, 1);
  3086. mutex_unlock(&trace_types_lock);
  3087. if (ret)
  3088. return ret;
  3089. topt->flags->val |= topt->opt->bit;
  3090. break;
  3091. default:
  3092. return -EINVAL;
  3093. }
  3094. *ppos += cnt;
  3095. return cnt;
  3096. }
  3097. static const struct file_operations trace_options_fops = {
  3098. .open = tracing_open_generic,
  3099. .read = trace_options_read,
  3100. .write = trace_options_write,
  3101. };
  3102. static ssize_t
  3103. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  3104. loff_t *ppos)
  3105. {
  3106. long index = (long)filp->private_data;
  3107. char *buf;
  3108. if (trace_flags & (1 << index))
  3109. buf = "1\n";
  3110. else
  3111. buf = "0\n";
  3112. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3113. }
  3114. static ssize_t
  3115. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3116. loff_t *ppos)
  3117. {
  3118. long index = (long)filp->private_data;
  3119. char buf[64];
  3120. unsigned long val;
  3121. int ret;
  3122. if (cnt >= sizeof(buf))
  3123. return -EINVAL;
  3124. if (copy_from_user(&buf, ubuf, cnt))
  3125. return -EFAULT;
  3126. buf[cnt] = 0;
  3127. ret = strict_strtoul(buf, 10, &val);
  3128. if (ret < 0)
  3129. return ret;
  3130. switch (val) {
  3131. case 0:
  3132. trace_flags &= ~(1 << index);
  3133. break;
  3134. case 1:
  3135. trace_flags |= 1 << index;
  3136. break;
  3137. default:
  3138. return -EINVAL;
  3139. }
  3140. *ppos += cnt;
  3141. return cnt;
  3142. }
  3143. static const struct file_operations trace_options_core_fops = {
  3144. .open = tracing_open_generic,
  3145. .read = trace_options_core_read,
  3146. .write = trace_options_core_write,
  3147. };
  3148. struct dentry *trace_create_file(const char *name,
  3149. mode_t mode,
  3150. struct dentry *parent,
  3151. void *data,
  3152. const struct file_operations *fops)
  3153. {
  3154. struct dentry *ret;
  3155. ret = debugfs_create_file(name, mode, parent, data, fops);
  3156. if (!ret)
  3157. pr_warning("Could not create debugfs '%s' entry\n", name);
  3158. return ret;
  3159. }
  3160. static struct dentry *trace_options_init_dentry(void)
  3161. {
  3162. struct dentry *d_tracer;
  3163. static struct dentry *t_options;
  3164. if (t_options)
  3165. return t_options;
  3166. d_tracer = tracing_init_dentry();
  3167. if (!d_tracer)
  3168. return NULL;
  3169. t_options = debugfs_create_dir("options", d_tracer);
  3170. if (!t_options) {
  3171. pr_warning("Could not create debugfs directory 'options'\n");
  3172. return NULL;
  3173. }
  3174. return t_options;
  3175. }
  3176. static void
  3177. create_trace_option_file(struct trace_option_dentry *topt,
  3178. struct tracer_flags *flags,
  3179. struct tracer_opt *opt)
  3180. {
  3181. struct dentry *t_options;
  3182. t_options = trace_options_init_dentry();
  3183. if (!t_options)
  3184. return;
  3185. topt->flags = flags;
  3186. topt->opt = opt;
  3187. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  3188. &trace_options_fops);
  3189. }
  3190. static struct trace_option_dentry *
  3191. create_trace_option_files(struct tracer *tracer)
  3192. {
  3193. struct trace_option_dentry *topts;
  3194. struct tracer_flags *flags;
  3195. struct tracer_opt *opts;
  3196. int cnt;
  3197. if (!tracer)
  3198. return NULL;
  3199. flags = tracer->flags;
  3200. if (!flags || !flags->opts)
  3201. return NULL;
  3202. opts = flags->opts;
  3203. for (cnt = 0; opts[cnt].name; cnt++)
  3204. ;
  3205. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  3206. if (!topts)
  3207. return NULL;
  3208. for (cnt = 0; opts[cnt].name; cnt++)
  3209. create_trace_option_file(&topts[cnt], flags,
  3210. &opts[cnt]);
  3211. return topts;
  3212. }
  3213. static void
  3214. destroy_trace_option_files(struct trace_option_dentry *topts)
  3215. {
  3216. int cnt;
  3217. if (!topts)
  3218. return;
  3219. for (cnt = 0; topts[cnt].opt; cnt++) {
  3220. if (topts[cnt].entry)
  3221. debugfs_remove(topts[cnt].entry);
  3222. }
  3223. kfree(topts);
  3224. }
  3225. static struct dentry *
  3226. create_trace_option_core_file(const char *option, long index)
  3227. {
  3228. struct dentry *t_options;
  3229. t_options = trace_options_init_dentry();
  3230. if (!t_options)
  3231. return NULL;
  3232. return trace_create_file(option, 0644, t_options, (void *)index,
  3233. &trace_options_core_fops);
  3234. }
  3235. static __init void create_trace_options_dir(void)
  3236. {
  3237. struct dentry *t_options;
  3238. int i;
  3239. t_options = trace_options_init_dentry();
  3240. if (!t_options)
  3241. return;
  3242. for (i = 0; trace_options[i]; i++)
  3243. create_trace_option_core_file(trace_options[i], i);
  3244. }
  3245. static __init int tracer_init_debugfs(void)
  3246. {
  3247. struct dentry *d_tracer;
  3248. int cpu;
  3249. d_tracer = tracing_init_dentry();
  3250. trace_create_file("tracing_enabled", 0644, d_tracer,
  3251. &global_trace, &tracing_ctrl_fops);
  3252. trace_create_file("trace_options", 0644, d_tracer,
  3253. NULL, &tracing_iter_fops);
  3254. trace_create_file("tracing_cpumask", 0644, d_tracer,
  3255. NULL, &tracing_cpumask_fops);
  3256. trace_create_file("trace", 0644, d_tracer,
  3257. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  3258. trace_create_file("available_tracers", 0444, d_tracer,
  3259. &global_trace, &show_traces_fops);
  3260. trace_create_file("current_tracer", 0644, d_tracer,
  3261. &global_trace, &set_tracer_fops);
  3262. trace_create_file("tracing_max_latency", 0644, d_tracer,
  3263. &tracing_max_latency, &tracing_max_lat_fops);
  3264. trace_create_file("tracing_thresh", 0644, d_tracer,
  3265. &tracing_thresh, &tracing_max_lat_fops);
  3266. trace_create_file("README", 0444, d_tracer,
  3267. NULL, &tracing_readme_fops);
  3268. trace_create_file("trace_pipe", 0444, d_tracer,
  3269. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3270. trace_create_file("buffer_size_kb", 0644, d_tracer,
  3271. &global_trace, &tracing_entries_fops);
  3272. trace_create_file("trace_marker", 0220, d_tracer,
  3273. NULL, &tracing_mark_fops);
  3274. trace_create_file("saved_cmdlines", 0444, d_tracer,
  3275. NULL, &tracing_saved_cmdlines_fops);
  3276. #ifdef CONFIG_DYNAMIC_FTRACE
  3277. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3278. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  3279. #endif
  3280. #ifdef CONFIG_SYSPROF_TRACER
  3281. init_tracer_sysprof_debugfs(d_tracer);
  3282. #endif
  3283. create_trace_options_dir();
  3284. for_each_tracing_cpu(cpu)
  3285. tracing_init_debugfs_percpu(cpu);
  3286. return 0;
  3287. }
  3288. static int trace_panic_handler(struct notifier_block *this,
  3289. unsigned long event, void *unused)
  3290. {
  3291. if (ftrace_dump_on_oops)
  3292. ftrace_dump();
  3293. return NOTIFY_OK;
  3294. }
  3295. static struct notifier_block trace_panic_notifier = {
  3296. .notifier_call = trace_panic_handler,
  3297. .next = NULL,
  3298. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3299. };
  3300. static int trace_die_handler(struct notifier_block *self,
  3301. unsigned long val,
  3302. void *data)
  3303. {
  3304. switch (val) {
  3305. case DIE_OOPS:
  3306. if (ftrace_dump_on_oops)
  3307. ftrace_dump();
  3308. break;
  3309. default:
  3310. break;
  3311. }
  3312. return NOTIFY_OK;
  3313. }
  3314. static struct notifier_block trace_die_notifier = {
  3315. .notifier_call = trace_die_handler,
  3316. .priority = 200
  3317. };
  3318. /*
  3319. * printk is set to max of 1024, we really don't need it that big.
  3320. * Nothing should be printing 1000 characters anyway.
  3321. */
  3322. #define TRACE_MAX_PRINT 1000
  3323. /*
  3324. * Define here KERN_TRACE so that we have one place to modify
  3325. * it if we decide to change what log level the ftrace dump
  3326. * should be at.
  3327. */
  3328. #define KERN_TRACE KERN_EMERG
  3329. static void
  3330. trace_printk_seq(struct trace_seq *s)
  3331. {
  3332. /* Probably should print a warning here. */
  3333. if (s->len >= 1000)
  3334. s->len = 1000;
  3335. /* should be zero ended, but we are paranoid. */
  3336. s->buffer[s->len] = 0;
  3337. printk(KERN_TRACE "%s", s->buffer);
  3338. trace_seq_init(s);
  3339. }
  3340. static void __ftrace_dump(bool disable_tracing)
  3341. {
  3342. static raw_spinlock_t ftrace_dump_lock =
  3343. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  3344. /* use static because iter can be a bit big for the stack */
  3345. static struct trace_iterator iter;
  3346. unsigned int old_userobj;
  3347. static int dump_ran;
  3348. unsigned long flags;
  3349. int cnt = 0, cpu;
  3350. /* only one dump */
  3351. local_irq_save(flags);
  3352. __raw_spin_lock(&ftrace_dump_lock);
  3353. if (dump_ran)
  3354. goto out;
  3355. dump_ran = 1;
  3356. tracing_off();
  3357. if (disable_tracing)
  3358. ftrace_kill();
  3359. for_each_tracing_cpu(cpu) {
  3360. atomic_inc(&global_trace.data[cpu]->disabled);
  3361. }
  3362. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  3363. /* don't look at user memory in panic mode */
  3364. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3365. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3366. /* Simulate the iterator */
  3367. iter.tr = &global_trace;
  3368. iter.trace = current_trace;
  3369. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3370. /*
  3371. * We need to stop all tracing on all CPUS to read the
  3372. * the next buffer. This is a bit expensive, but is
  3373. * not done often. We fill all what we can read,
  3374. * and then release the locks again.
  3375. */
  3376. while (!trace_empty(&iter)) {
  3377. if (!cnt)
  3378. printk(KERN_TRACE "---------------------------------\n");
  3379. cnt++;
  3380. /* reset all but tr, trace, and overruns */
  3381. memset(&iter.seq, 0,
  3382. sizeof(struct trace_iterator) -
  3383. offsetof(struct trace_iterator, seq));
  3384. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3385. iter.pos = -1;
  3386. if (find_next_entry_inc(&iter) != NULL) {
  3387. print_trace_line(&iter);
  3388. trace_consume(&iter);
  3389. }
  3390. trace_printk_seq(&iter.seq);
  3391. }
  3392. if (!cnt)
  3393. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3394. else
  3395. printk(KERN_TRACE "---------------------------------\n");
  3396. /* Re-enable tracing if requested */
  3397. if (!disable_tracing) {
  3398. trace_flags |= old_userobj;
  3399. for_each_tracing_cpu(cpu) {
  3400. atomic_dec(&global_trace.data[cpu]->disabled);
  3401. }
  3402. tracing_on();
  3403. }
  3404. out:
  3405. __raw_spin_unlock(&ftrace_dump_lock);
  3406. local_irq_restore(flags);
  3407. }
  3408. /* By default: disable tracing after the dump */
  3409. void ftrace_dump(void)
  3410. {
  3411. __ftrace_dump(true);
  3412. }
  3413. __init static int tracer_alloc_buffers(void)
  3414. {
  3415. struct trace_array_cpu *data;
  3416. int ring_buf_size;
  3417. int i;
  3418. int ret = -ENOMEM;
  3419. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  3420. goto out;
  3421. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  3422. goto out_free_buffer_mask;
  3423. if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
  3424. goto out_free_tracing_cpumask;
  3425. /* To save memory, keep the ring buffer size to its minimum */
  3426. if (ring_buffer_expanded)
  3427. ring_buf_size = trace_buf_size;
  3428. else
  3429. ring_buf_size = 1;
  3430. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  3431. cpumask_copy(tracing_cpumask, cpu_all_mask);
  3432. cpumask_clear(tracing_reader_cpumask);
  3433. /* TODO: make the number of buffers hot pluggable with CPUS */
  3434. global_trace.buffer = ring_buffer_alloc(ring_buf_size,
  3435. TRACE_BUFFER_FLAGS);
  3436. if (!global_trace.buffer) {
  3437. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3438. WARN_ON(1);
  3439. goto out_free_cpumask;
  3440. }
  3441. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3442. #ifdef CONFIG_TRACER_MAX_TRACE
  3443. max_tr.buffer = ring_buffer_alloc(ring_buf_size,
  3444. TRACE_BUFFER_FLAGS);
  3445. if (!max_tr.buffer) {
  3446. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3447. WARN_ON(1);
  3448. ring_buffer_free(global_trace.buffer);
  3449. goto out_free_cpumask;
  3450. }
  3451. max_tr.entries = ring_buffer_size(max_tr.buffer);
  3452. WARN_ON(max_tr.entries != global_trace.entries);
  3453. #endif
  3454. /* Allocate the first page for all buffers */
  3455. for_each_tracing_cpu(i) {
  3456. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3457. max_tr.data[i] = &per_cpu(max_data, i);
  3458. }
  3459. trace_init_cmdlines();
  3460. register_tracer(&nop_trace);
  3461. current_trace = &nop_trace;
  3462. #ifdef CONFIG_BOOT_TRACER
  3463. register_tracer(&boot_tracer);
  3464. #endif
  3465. /* All seems OK, enable tracing */
  3466. tracing_disabled = 0;
  3467. atomic_notifier_chain_register(&panic_notifier_list,
  3468. &trace_panic_notifier);
  3469. register_die_notifier(&trace_die_notifier);
  3470. return 0;
  3471. out_free_cpumask:
  3472. free_cpumask_var(tracing_reader_cpumask);
  3473. out_free_tracing_cpumask:
  3474. free_cpumask_var(tracing_cpumask);
  3475. out_free_buffer_mask:
  3476. free_cpumask_var(tracing_buffer_mask);
  3477. out:
  3478. return ret;
  3479. }
  3480. __init static int clear_boot_tracer(void)
  3481. {
  3482. /*
  3483. * The default tracer at boot buffer is an init section.
  3484. * This function is called in lateinit. If we did not
  3485. * find the boot tracer, then clear it out, to prevent
  3486. * later registration from accessing the buffer that is
  3487. * about to be freed.
  3488. */
  3489. if (!default_bootup_tracer)
  3490. return 0;
  3491. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  3492. default_bootup_tracer);
  3493. default_bootup_tracer = NULL;
  3494. return 0;
  3495. }
  3496. early_initcall(tracer_alloc_buffers);
  3497. fs_initcall(tracer_init_debugfs);
  3498. late_initcall(clear_boot_tracer);