trace.c 90 KB

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