trace.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008
  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 trace_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. "trace_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. /**
  984. * trace_vprintk - write binary msg to tracing buffer
  985. *
  986. */
  987. int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
  988. {
  989. static DEFINE_SPINLOCK(trace_buf_lock);
  990. static u32 trace_buf[TRACE_BUF_SIZE];
  991. struct ring_buffer_event *event;
  992. struct trace_array *tr = &global_trace;
  993. struct trace_array_cpu *data;
  994. struct print_entry *entry;
  995. unsigned long flags;
  996. int resched;
  997. int cpu, len = 0, size, pc;
  998. if (unlikely(tracing_selftest_running || tracing_disabled))
  999. return 0;
  1000. /* Don't pollute graph traces with trace_vprintk internals */
  1001. pause_graph_tracing();
  1002. pc = preempt_count();
  1003. resched = ftrace_preempt_disable();
  1004. cpu = raw_smp_processor_id();
  1005. data = tr->data[cpu];
  1006. if (unlikely(atomic_read(&data->disabled)))
  1007. goto out;
  1008. spin_lock_irqsave(&trace_buf_lock, flags);
  1009. len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1010. if (len > TRACE_BUF_SIZE || len < 0)
  1011. goto out_unlock;
  1012. size = sizeof(*entry) + sizeof(u32) * len;
  1013. event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
  1014. if (!event)
  1015. goto out_unlock;
  1016. entry = ring_buffer_event_data(event);
  1017. entry->ip = ip;
  1018. entry->depth = depth;
  1019. entry->fmt = fmt;
  1020. memcpy(entry->buf, trace_buf, sizeof(u32) * len);
  1021. ring_buffer_unlock_commit(tr->buffer, event);
  1022. out_unlock:
  1023. spin_unlock_irqrestore(&trace_buf_lock, flags);
  1024. out:
  1025. ftrace_preempt_enable(resched);
  1026. unpause_graph_tracing();
  1027. return len;
  1028. }
  1029. EXPORT_SYMBOL_GPL(trace_vprintk);
  1030. enum trace_file_type {
  1031. TRACE_FILE_LAT_FMT = 1,
  1032. TRACE_FILE_ANNOTATE = 2,
  1033. };
  1034. static void trace_iterator_increment(struct trace_iterator *iter)
  1035. {
  1036. /* Don't allow ftrace to trace into the ring buffers */
  1037. ftrace_disable_cpu();
  1038. iter->idx++;
  1039. if (iter->buffer_iter[iter->cpu])
  1040. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1041. ftrace_enable_cpu();
  1042. }
  1043. static struct trace_entry *
  1044. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  1045. {
  1046. struct ring_buffer_event *event;
  1047. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  1048. /* Don't allow ftrace to trace into the ring buffers */
  1049. ftrace_disable_cpu();
  1050. if (buf_iter)
  1051. event = ring_buffer_iter_peek(buf_iter, ts);
  1052. else
  1053. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  1054. ftrace_enable_cpu();
  1055. return event ? ring_buffer_event_data(event) : NULL;
  1056. }
  1057. static struct trace_entry *
  1058. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  1059. {
  1060. struct ring_buffer *buffer = iter->tr->buffer;
  1061. struct trace_entry *ent, *next = NULL;
  1062. int cpu_file = iter->cpu_file;
  1063. u64 next_ts = 0, ts;
  1064. int next_cpu = -1;
  1065. int cpu;
  1066. /*
  1067. * If we are in a per_cpu trace file, don't bother by iterating over
  1068. * all cpu and peek directly.
  1069. */
  1070. if (cpu_file > TRACE_PIPE_ALL_CPU) {
  1071. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1072. return NULL;
  1073. ent = peek_next_entry(iter, cpu_file, ent_ts);
  1074. if (ent_cpu)
  1075. *ent_cpu = cpu_file;
  1076. return ent;
  1077. }
  1078. for_each_tracing_cpu(cpu) {
  1079. if (ring_buffer_empty_cpu(buffer, cpu))
  1080. continue;
  1081. ent = peek_next_entry(iter, cpu, &ts);
  1082. /*
  1083. * Pick the entry with the smallest timestamp:
  1084. */
  1085. if (ent && (!next || ts < next_ts)) {
  1086. next = ent;
  1087. next_cpu = cpu;
  1088. next_ts = ts;
  1089. }
  1090. }
  1091. if (ent_cpu)
  1092. *ent_cpu = next_cpu;
  1093. if (ent_ts)
  1094. *ent_ts = next_ts;
  1095. return next;
  1096. }
  1097. /* Find the next real entry, without updating the iterator itself */
  1098. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1099. int *ent_cpu, u64 *ent_ts)
  1100. {
  1101. return __find_next_entry(iter, ent_cpu, ent_ts);
  1102. }
  1103. /* Find the next real entry, and increment the iterator to the next entry */
  1104. static void *find_next_entry_inc(struct trace_iterator *iter)
  1105. {
  1106. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  1107. if (iter->ent)
  1108. trace_iterator_increment(iter);
  1109. return iter->ent ? iter : NULL;
  1110. }
  1111. static void trace_consume(struct trace_iterator *iter)
  1112. {
  1113. /* Don't allow ftrace to trace into the ring buffers */
  1114. ftrace_disable_cpu();
  1115. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  1116. ftrace_enable_cpu();
  1117. }
  1118. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1119. {
  1120. struct trace_iterator *iter = m->private;
  1121. int i = (int)*pos;
  1122. void *ent;
  1123. (*pos)++;
  1124. /* can't go backwards */
  1125. if (iter->idx > i)
  1126. return NULL;
  1127. if (iter->idx < 0)
  1128. ent = find_next_entry_inc(iter);
  1129. else
  1130. ent = iter;
  1131. while (ent && iter->idx < i)
  1132. ent = find_next_entry_inc(iter);
  1133. iter->pos = *pos;
  1134. return ent;
  1135. }
  1136. /*
  1137. * No necessary locking here. The worst thing which can
  1138. * happen is loosing events consumed at the same time
  1139. * by a trace_pipe reader.
  1140. * Other than that, we don't risk to crash the ring buffer
  1141. * because it serializes the readers.
  1142. *
  1143. * The current tracer is copied to avoid a global locking
  1144. * all around.
  1145. */
  1146. static void *s_start(struct seq_file *m, loff_t *pos)
  1147. {
  1148. struct trace_iterator *iter = m->private;
  1149. static struct tracer *old_tracer;
  1150. int cpu_file = iter->cpu_file;
  1151. void *p = NULL;
  1152. loff_t l = 0;
  1153. int cpu;
  1154. /* copy the tracer to avoid using a global lock all around */
  1155. mutex_lock(&trace_types_lock);
  1156. if (unlikely(old_tracer != current_trace && current_trace)) {
  1157. old_tracer = current_trace;
  1158. *iter->trace = *current_trace;
  1159. }
  1160. mutex_unlock(&trace_types_lock);
  1161. atomic_inc(&trace_record_cmdline_disabled);
  1162. if (*pos != iter->pos) {
  1163. iter->ent = NULL;
  1164. iter->cpu = 0;
  1165. iter->idx = -1;
  1166. ftrace_disable_cpu();
  1167. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  1168. for_each_tracing_cpu(cpu)
  1169. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  1170. } else
  1171. ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
  1172. ftrace_enable_cpu();
  1173. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1174. ;
  1175. } else {
  1176. l = *pos - 1;
  1177. p = s_next(m, p, &l);
  1178. }
  1179. return p;
  1180. }
  1181. static void s_stop(struct seq_file *m, void *p)
  1182. {
  1183. atomic_dec(&trace_record_cmdline_disabled);
  1184. }
  1185. static void print_lat_help_header(struct seq_file *m)
  1186. {
  1187. seq_puts(m, "# _------=> CPU# \n");
  1188. seq_puts(m, "# / _-----=> irqs-off \n");
  1189. seq_puts(m, "# | / _----=> need-resched \n");
  1190. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1191. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1192. seq_puts(m, "# |||| / \n");
  1193. seq_puts(m, "# ||||| delay \n");
  1194. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1195. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1196. }
  1197. static void print_func_help_header(struct seq_file *m)
  1198. {
  1199. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1200. seq_puts(m, "# | | | | |\n");
  1201. }
  1202. static void
  1203. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1204. {
  1205. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1206. struct trace_array *tr = iter->tr;
  1207. struct trace_array_cpu *data = tr->data[tr->cpu];
  1208. struct tracer *type = current_trace;
  1209. unsigned long total;
  1210. unsigned long entries;
  1211. const char *name = "preemption";
  1212. if (type)
  1213. name = type->name;
  1214. entries = ring_buffer_entries(iter->tr->buffer);
  1215. total = entries +
  1216. ring_buffer_overruns(iter->tr->buffer);
  1217. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1218. name, UTS_RELEASE);
  1219. seq_puts(m, "# -----------------------------------"
  1220. "---------------------------------\n");
  1221. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1222. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1223. nsecs_to_usecs(data->saved_latency),
  1224. entries,
  1225. total,
  1226. tr->cpu,
  1227. #if defined(CONFIG_PREEMPT_NONE)
  1228. "server",
  1229. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1230. "desktop",
  1231. #elif defined(CONFIG_PREEMPT)
  1232. "preempt",
  1233. #else
  1234. "unknown",
  1235. #endif
  1236. /* These are reserved for later use */
  1237. 0, 0, 0, 0);
  1238. #ifdef CONFIG_SMP
  1239. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1240. #else
  1241. seq_puts(m, ")\n");
  1242. #endif
  1243. seq_puts(m, "# -----------------\n");
  1244. seq_printf(m, "# | task: %.16s-%d "
  1245. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1246. data->comm, data->pid, data->uid, data->nice,
  1247. data->policy, data->rt_priority);
  1248. seq_puts(m, "# -----------------\n");
  1249. if (data->critical_start) {
  1250. seq_puts(m, "# => started at: ");
  1251. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1252. trace_print_seq(m, &iter->seq);
  1253. seq_puts(m, "\n# => ended at: ");
  1254. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1255. trace_print_seq(m, &iter->seq);
  1256. seq_puts(m, "#\n");
  1257. }
  1258. seq_puts(m, "#\n");
  1259. }
  1260. static void test_cpu_buff_start(struct trace_iterator *iter)
  1261. {
  1262. struct trace_seq *s = &iter->seq;
  1263. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1264. return;
  1265. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1266. return;
  1267. if (cpumask_test_cpu(iter->cpu, iter->started))
  1268. return;
  1269. cpumask_set_cpu(iter->cpu, iter->started);
  1270. trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
  1271. }
  1272. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1273. {
  1274. struct trace_seq *s = &iter->seq;
  1275. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1276. struct trace_entry *entry;
  1277. struct trace_event *event;
  1278. entry = iter->ent;
  1279. test_cpu_buff_start(iter);
  1280. event = ftrace_find_event(entry->type);
  1281. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1282. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1283. if (!trace_print_lat_context(iter))
  1284. goto partial;
  1285. } else {
  1286. if (!trace_print_context(iter))
  1287. goto partial;
  1288. }
  1289. }
  1290. if (event)
  1291. return event->trace(iter, sym_flags);
  1292. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1293. goto partial;
  1294. return TRACE_TYPE_HANDLED;
  1295. partial:
  1296. return TRACE_TYPE_PARTIAL_LINE;
  1297. }
  1298. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1299. {
  1300. struct trace_seq *s = &iter->seq;
  1301. struct trace_entry *entry;
  1302. struct trace_event *event;
  1303. entry = iter->ent;
  1304. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1305. if (!trace_seq_printf(s, "%d %d %llu ",
  1306. entry->pid, iter->cpu, iter->ts))
  1307. goto partial;
  1308. }
  1309. event = ftrace_find_event(entry->type);
  1310. if (event)
  1311. return event->raw(iter, 0);
  1312. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1313. goto partial;
  1314. return TRACE_TYPE_HANDLED;
  1315. partial:
  1316. return TRACE_TYPE_PARTIAL_LINE;
  1317. }
  1318. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1319. {
  1320. struct trace_seq *s = &iter->seq;
  1321. unsigned char newline = '\n';
  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_HEX_FIELD_RET(s, entry->pid);
  1327. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1328. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1329. }
  1330. event = ftrace_find_event(entry->type);
  1331. if (event) {
  1332. enum print_line_t ret = event->hex(iter, 0);
  1333. if (ret != TRACE_TYPE_HANDLED)
  1334. return ret;
  1335. }
  1336. SEQ_PUT_FIELD_RET(s, newline);
  1337. return TRACE_TYPE_HANDLED;
  1338. }
  1339. static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
  1340. {
  1341. struct trace_seq *s = &iter->seq;
  1342. struct trace_entry *entry = iter->ent;
  1343. struct print_entry *field;
  1344. int ret;
  1345. trace_assign_type(field, entry);
  1346. ret = trace_seq_bprintf(s, field->fmt, field->buf);
  1347. if (!ret)
  1348. return TRACE_TYPE_PARTIAL_LINE;
  1349. return TRACE_TYPE_HANDLED;
  1350. }
  1351. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1352. {
  1353. struct trace_seq *s = &iter->seq;
  1354. struct trace_entry *entry;
  1355. struct trace_event *event;
  1356. entry = iter->ent;
  1357. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1358. SEQ_PUT_FIELD_RET(s, entry->pid);
  1359. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1360. SEQ_PUT_FIELD_RET(s, iter->ts);
  1361. }
  1362. event = ftrace_find_event(entry->type);
  1363. return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
  1364. }
  1365. static int trace_empty(struct trace_iterator *iter)
  1366. {
  1367. int cpu;
  1368. for_each_tracing_cpu(cpu) {
  1369. if (iter->buffer_iter[cpu]) {
  1370. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1371. return 0;
  1372. } else {
  1373. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1374. return 0;
  1375. }
  1376. }
  1377. return 1;
  1378. }
  1379. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1380. {
  1381. enum print_line_t ret;
  1382. if (iter->trace && iter->trace->print_line) {
  1383. ret = iter->trace->print_line(iter);
  1384. if (ret != TRACE_TYPE_UNHANDLED)
  1385. return ret;
  1386. }
  1387. if (iter->ent->type == TRACE_PRINT &&
  1388. trace_flags & TRACE_ITER_PRINTK &&
  1389. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1390. return print_printk_msg_only(iter);
  1391. if (trace_flags & TRACE_ITER_BIN)
  1392. return print_bin_fmt(iter);
  1393. if (trace_flags & TRACE_ITER_HEX)
  1394. return print_hex_fmt(iter);
  1395. if (trace_flags & TRACE_ITER_RAW)
  1396. return print_raw_fmt(iter);
  1397. return print_trace_fmt(iter);
  1398. }
  1399. static int s_show(struct seq_file *m, void *v)
  1400. {
  1401. struct trace_iterator *iter = v;
  1402. if (iter->ent == NULL) {
  1403. if (iter->tr) {
  1404. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1405. seq_puts(m, "#\n");
  1406. }
  1407. if (iter->trace && iter->trace->print_header)
  1408. iter->trace->print_header(m);
  1409. else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1410. /* print nothing if the buffers are empty */
  1411. if (trace_empty(iter))
  1412. return 0;
  1413. print_trace_header(m, iter);
  1414. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1415. print_lat_help_header(m);
  1416. } else {
  1417. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1418. print_func_help_header(m);
  1419. }
  1420. } else {
  1421. print_trace_line(iter);
  1422. trace_print_seq(m, &iter->seq);
  1423. }
  1424. return 0;
  1425. }
  1426. static struct seq_operations tracer_seq_ops = {
  1427. .start = s_start,
  1428. .next = s_next,
  1429. .stop = s_stop,
  1430. .show = s_show,
  1431. };
  1432. static struct trace_iterator *
  1433. __tracing_open(struct inode *inode, struct file *file)
  1434. {
  1435. long cpu_file = (long) inode->i_private;
  1436. void *fail_ret = ERR_PTR(-ENOMEM);
  1437. struct trace_iterator *iter;
  1438. struct seq_file *m;
  1439. int cpu, ret;
  1440. if (tracing_disabled)
  1441. return ERR_PTR(-ENODEV);
  1442. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1443. if (!iter)
  1444. return ERR_PTR(-ENOMEM);
  1445. /*
  1446. * We make a copy of the current tracer to avoid concurrent
  1447. * changes on it while we are reading.
  1448. */
  1449. mutex_lock(&trace_types_lock);
  1450. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1451. if (!iter->trace)
  1452. goto fail;
  1453. if (current_trace)
  1454. *iter->trace = *current_trace;
  1455. if (current_trace && current_trace->print_max)
  1456. iter->tr = &max_tr;
  1457. else
  1458. iter->tr = &global_trace;
  1459. iter->pos = -1;
  1460. mutex_init(&iter->mutex);
  1461. iter->cpu_file = cpu_file;
  1462. /* Notify the tracer early; before we stop tracing. */
  1463. if (iter->trace && iter->trace->open)
  1464. iter->trace->open(iter);
  1465. /* Annotate start of buffers if we had overruns */
  1466. if (ring_buffer_overruns(iter->tr->buffer))
  1467. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1468. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  1469. for_each_tracing_cpu(cpu) {
  1470. iter->buffer_iter[cpu] =
  1471. ring_buffer_read_start(iter->tr->buffer, cpu);
  1472. if (!iter->buffer_iter[cpu])
  1473. goto fail_buffer;
  1474. }
  1475. } else {
  1476. cpu = iter->cpu_file;
  1477. iter->buffer_iter[cpu] =
  1478. ring_buffer_read_start(iter->tr->buffer, cpu);
  1479. if (!iter->buffer_iter[cpu])
  1480. goto fail;
  1481. }
  1482. /* TODO stop tracer */
  1483. ret = seq_open(file, &tracer_seq_ops);
  1484. if (ret < 0) {
  1485. fail_ret = ERR_PTR(ret);
  1486. goto fail_buffer;
  1487. }
  1488. m = file->private_data;
  1489. m->private = iter;
  1490. /* stop the trace while dumping */
  1491. tracing_stop();
  1492. mutex_unlock(&trace_types_lock);
  1493. return iter;
  1494. fail_buffer:
  1495. for_each_tracing_cpu(cpu) {
  1496. if (iter->buffer_iter[cpu])
  1497. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1498. }
  1499. fail:
  1500. mutex_unlock(&trace_types_lock);
  1501. kfree(iter->trace);
  1502. kfree(iter);
  1503. return fail_ret;
  1504. }
  1505. int tracing_open_generic(struct inode *inode, struct file *filp)
  1506. {
  1507. if (tracing_disabled)
  1508. return -ENODEV;
  1509. filp->private_data = inode->i_private;
  1510. return 0;
  1511. }
  1512. static int tracing_release(struct inode *inode, struct file *file)
  1513. {
  1514. struct seq_file *m = (struct seq_file *)file->private_data;
  1515. struct trace_iterator *iter = m->private;
  1516. int cpu;
  1517. mutex_lock(&trace_types_lock);
  1518. for_each_tracing_cpu(cpu) {
  1519. if (iter->buffer_iter[cpu])
  1520. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1521. }
  1522. if (iter->trace && iter->trace->close)
  1523. iter->trace->close(iter);
  1524. /* reenable tracing if it was previously enabled */
  1525. tracing_start();
  1526. mutex_unlock(&trace_types_lock);
  1527. seq_release(inode, file);
  1528. mutex_destroy(&iter->mutex);
  1529. kfree(iter->trace);
  1530. kfree(iter);
  1531. return 0;
  1532. }
  1533. static int tracing_open(struct inode *inode, struct file *file)
  1534. {
  1535. struct trace_iterator *iter;
  1536. int ret = 0;
  1537. iter = __tracing_open(inode, file);
  1538. if (IS_ERR(iter))
  1539. ret = PTR_ERR(iter);
  1540. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  1541. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1542. return ret;
  1543. }
  1544. static void *
  1545. t_next(struct seq_file *m, void *v, loff_t *pos)
  1546. {
  1547. struct tracer *t = m->private;
  1548. (*pos)++;
  1549. if (t)
  1550. t = t->next;
  1551. m->private = t;
  1552. return t;
  1553. }
  1554. static void *t_start(struct seq_file *m, loff_t *pos)
  1555. {
  1556. struct tracer *t = m->private;
  1557. loff_t l = 0;
  1558. mutex_lock(&trace_types_lock);
  1559. for (; t && l < *pos; t = t_next(m, t, &l))
  1560. ;
  1561. return t;
  1562. }
  1563. static void t_stop(struct seq_file *m, void *p)
  1564. {
  1565. mutex_unlock(&trace_types_lock);
  1566. }
  1567. static int t_show(struct seq_file *m, void *v)
  1568. {
  1569. struct tracer *t = v;
  1570. if (!t)
  1571. return 0;
  1572. seq_printf(m, "%s", t->name);
  1573. if (t->next)
  1574. seq_putc(m, ' ');
  1575. else
  1576. seq_putc(m, '\n');
  1577. return 0;
  1578. }
  1579. static struct seq_operations show_traces_seq_ops = {
  1580. .start = t_start,
  1581. .next = t_next,
  1582. .stop = t_stop,
  1583. .show = t_show,
  1584. };
  1585. static int show_traces_open(struct inode *inode, struct file *file)
  1586. {
  1587. int ret;
  1588. if (tracing_disabled)
  1589. return -ENODEV;
  1590. ret = seq_open(file, &show_traces_seq_ops);
  1591. if (!ret) {
  1592. struct seq_file *m = file->private_data;
  1593. m->private = trace_types;
  1594. }
  1595. return ret;
  1596. }
  1597. static const struct file_operations tracing_fops = {
  1598. .open = tracing_open,
  1599. .read = seq_read,
  1600. .llseek = seq_lseek,
  1601. .release = tracing_release,
  1602. };
  1603. static const struct file_operations show_traces_fops = {
  1604. .open = show_traces_open,
  1605. .read = seq_read,
  1606. .release = seq_release,
  1607. };
  1608. /*
  1609. * Only trace on a CPU if the bitmask is set:
  1610. */
  1611. static cpumask_var_t tracing_cpumask;
  1612. /*
  1613. * The tracer itself will not take this lock, but still we want
  1614. * to provide a consistent cpumask to user-space:
  1615. */
  1616. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1617. /*
  1618. * Temporary storage for the character representation of the
  1619. * CPU bitmask (and one more byte for the newline):
  1620. */
  1621. static char mask_str[NR_CPUS + 1];
  1622. static ssize_t
  1623. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1624. size_t count, loff_t *ppos)
  1625. {
  1626. int len;
  1627. mutex_lock(&tracing_cpumask_update_lock);
  1628. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1629. if (count - len < 2) {
  1630. count = -EINVAL;
  1631. goto out_err;
  1632. }
  1633. len += sprintf(mask_str + len, "\n");
  1634. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1635. out_err:
  1636. mutex_unlock(&tracing_cpumask_update_lock);
  1637. return count;
  1638. }
  1639. static ssize_t
  1640. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1641. size_t count, loff_t *ppos)
  1642. {
  1643. int err, cpu;
  1644. cpumask_var_t tracing_cpumask_new;
  1645. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  1646. return -ENOMEM;
  1647. mutex_lock(&tracing_cpumask_update_lock);
  1648. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1649. if (err)
  1650. goto err_unlock;
  1651. local_irq_disable();
  1652. __raw_spin_lock(&ftrace_max_lock);
  1653. for_each_tracing_cpu(cpu) {
  1654. /*
  1655. * Increase/decrease the disabled counter if we are
  1656. * about to flip a bit in the cpumask:
  1657. */
  1658. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  1659. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1660. atomic_inc(&global_trace.data[cpu]->disabled);
  1661. }
  1662. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  1663. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1664. atomic_dec(&global_trace.data[cpu]->disabled);
  1665. }
  1666. }
  1667. __raw_spin_unlock(&ftrace_max_lock);
  1668. local_irq_enable();
  1669. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  1670. mutex_unlock(&tracing_cpumask_update_lock);
  1671. free_cpumask_var(tracing_cpumask_new);
  1672. return count;
  1673. err_unlock:
  1674. mutex_unlock(&tracing_cpumask_update_lock);
  1675. free_cpumask_var(tracing_cpumask);
  1676. return err;
  1677. }
  1678. static const struct file_operations tracing_cpumask_fops = {
  1679. .open = tracing_open_generic,
  1680. .read = tracing_cpumask_read,
  1681. .write = tracing_cpumask_write,
  1682. };
  1683. static ssize_t
  1684. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  1685. size_t cnt, loff_t *ppos)
  1686. {
  1687. struct tracer_opt *trace_opts;
  1688. u32 tracer_flags;
  1689. int len = 0;
  1690. char *buf;
  1691. int r = 0;
  1692. int i;
  1693. /* calculate max size */
  1694. for (i = 0; trace_options[i]; i++) {
  1695. len += strlen(trace_options[i]);
  1696. len += 3; /* "no" and newline */
  1697. }
  1698. mutex_lock(&trace_types_lock);
  1699. tracer_flags = current_trace->flags->val;
  1700. trace_opts = current_trace->flags->opts;
  1701. /*
  1702. * Increase the size with names of options specific
  1703. * of the current tracer.
  1704. */
  1705. for (i = 0; trace_opts[i].name; i++) {
  1706. len += strlen(trace_opts[i].name);
  1707. len += 3; /* "no" and newline */
  1708. }
  1709. /* +2 for \n and \0 */
  1710. buf = kmalloc(len + 2, GFP_KERNEL);
  1711. if (!buf) {
  1712. mutex_unlock(&trace_types_lock);
  1713. return -ENOMEM;
  1714. }
  1715. for (i = 0; trace_options[i]; i++) {
  1716. if (trace_flags & (1 << i))
  1717. r += sprintf(buf + r, "%s\n", trace_options[i]);
  1718. else
  1719. r += sprintf(buf + r, "no%s\n", trace_options[i]);
  1720. }
  1721. for (i = 0; trace_opts[i].name; i++) {
  1722. if (tracer_flags & trace_opts[i].bit)
  1723. r += sprintf(buf + r, "%s\n",
  1724. trace_opts[i].name);
  1725. else
  1726. r += sprintf(buf + r, "no%s\n",
  1727. trace_opts[i].name);
  1728. }
  1729. mutex_unlock(&trace_types_lock);
  1730. WARN_ON(r >= len + 2);
  1731. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1732. kfree(buf);
  1733. return r;
  1734. }
  1735. /* Try to assign a tracer specific option */
  1736. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  1737. {
  1738. struct tracer_flags *trace_flags = trace->flags;
  1739. struct tracer_opt *opts = NULL;
  1740. int ret = 0, i = 0;
  1741. int len;
  1742. for (i = 0; trace_flags->opts[i].name; i++) {
  1743. opts = &trace_flags->opts[i];
  1744. len = strlen(opts->name);
  1745. if (strncmp(cmp, opts->name, len) == 0) {
  1746. ret = trace->set_flag(trace_flags->val,
  1747. opts->bit, !neg);
  1748. break;
  1749. }
  1750. }
  1751. /* Not found */
  1752. if (!trace_flags->opts[i].name)
  1753. return -EINVAL;
  1754. /* Refused to handle */
  1755. if (ret)
  1756. return ret;
  1757. if (neg)
  1758. trace_flags->val &= ~opts->bit;
  1759. else
  1760. trace_flags->val |= opts->bit;
  1761. return 0;
  1762. }
  1763. static ssize_t
  1764. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  1765. size_t cnt, loff_t *ppos)
  1766. {
  1767. char buf[64];
  1768. char *cmp = buf;
  1769. int neg = 0;
  1770. int ret;
  1771. int i;
  1772. if (cnt >= sizeof(buf))
  1773. return -EINVAL;
  1774. if (copy_from_user(&buf, ubuf, cnt))
  1775. return -EFAULT;
  1776. buf[cnt] = 0;
  1777. if (strncmp(buf, "no", 2) == 0) {
  1778. neg = 1;
  1779. cmp += 2;
  1780. }
  1781. for (i = 0; trace_options[i]; i++) {
  1782. int len = strlen(trace_options[i]);
  1783. if (strncmp(cmp, trace_options[i], len) == 0) {
  1784. if (neg)
  1785. trace_flags &= ~(1 << i);
  1786. else
  1787. trace_flags |= (1 << i);
  1788. break;
  1789. }
  1790. }
  1791. /* If no option could be set, test the specific tracer options */
  1792. if (!trace_options[i]) {
  1793. mutex_lock(&trace_types_lock);
  1794. ret = set_tracer_option(current_trace, cmp, neg);
  1795. mutex_unlock(&trace_types_lock);
  1796. if (ret)
  1797. return ret;
  1798. }
  1799. filp->f_pos += cnt;
  1800. return cnt;
  1801. }
  1802. static const struct file_operations tracing_iter_fops = {
  1803. .open = tracing_open_generic,
  1804. .read = tracing_trace_options_read,
  1805. .write = tracing_trace_options_write,
  1806. };
  1807. static const char readme_msg[] =
  1808. "tracing mini-HOWTO:\n\n"
  1809. "# mkdir /debug\n"
  1810. "# mount -t debugfs nodev /debug\n\n"
  1811. "# cat /debug/tracing/available_tracers\n"
  1812. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1813. "# cat /debug/tracing/current_tracer\n"
  1814. "none\n"
  1815. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1816. "# cat /debug/tracing/current_tracer\n"
  1817. "sched_switch\n"
  1818. "# cat /debug/tracing/trace_options\n"
  1819. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1820. "# echo print-parent > /debug/tracing/trace_options\n"
  1821. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1822. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1823. "echo 0 > /debug/tracing/tracing_enabled\n"
  1824. ;
  1825. static ssize_t
  1826. tracing_readme_read(struct file *filp, char __user *ubuf,
  1827. size_t cnt, loff_t *ppos)
  1828. {
  1829. return simple_read_from_buffer(ubuf, cnt, ppos,
  1830. readme_msg, strlen(readme_msg));
  1831. }
  1832. static const struct file_operations tracing_readme_fops = {
  1833. .open = tracing_open_generic,
  1834. .read = tracing_readme_read,
  1835. };
  1836. static ssize_t
  1837. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1838. size_t cnt, loff_t *ppos)
  1839. {
  1840. char buf[64];
  1841. int r;
  1842. r = sprintf(buf, "%u\n", tracer_enabled);
  1843. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1844. }
  1845. static ssize_t
  1846. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1847. size_t cnt, loff_t *ppos)
  1848. {
  1849. struct trace_array *tr = filp->private_data;
  1850. char buf[64];
  1851. unsigned long val;
  1852. int ret;
  1853. if (cnt >= sizeof(buf))
  1854. return -EINVAL;
  1855. if (copy_from_user(&buf, ubuf, cnt))
  1856. return -EFAULT;
  1857. buf[cnt] = 0;
  1858. ret = strict_strtoul(buf, 10, &val);
  1859. if (ret < 0)
  1860. return ret;
  1861. val = !!val;
  1862. mutex_lock(&trace_types_lock);
  1863. if (tracer_enabled ^ val) {
  1864. if (val) {
  1865. tracer_enabled = 1;
  1866. if (current_trace->start)
  1867. current_trace->start(tr);
  1868. tracing_start();
  1869. } else {
  1870. tracer_enabled = 0;
  1871. tracing_stop();
  1872. if (current_trace->stop)
  1873. current_trace->stop(tr);
  1874. }
  1875. }
  1876. mutex_unlock(&trace_types_lock);
  1877. filp->f_pos += cnt;
  1878. return cnt;
  1879. }
  1880. static ssize_t
  1881. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1882. size_t cnt, loff_t *ppos)
  1883. {
  1884. char buf[max_tracer_type_len+2];
  1885. int r;
  1886. mutex_lock(&trace_types_lock);
  1887. if (current_trace)
  1888. r = sprintf(buf, "%s\n", current_trace->name);
  1889. else
  1890. r = sprintf(buf, "\n");
  1891. mutex_unlock(&trace_types_lock);
  1892. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1893. }
  1894. int tracer_init(struct tracer *t, struct trace_array *tr)
  1895. {
  1896. tracing_reset_online_cpus(tr);
  1897. return t->init(tr);
  1898. }
  1899. struct trace_option_dentry;
  1900. static struct trace_option_dentry *
  1901. create_trace_option_files(struct tracer *tracer);
  1902. static void
  1903. destroy_trace_option_files(struct trace_option_dentry *topts);
  1904. static int tracing_set_tracer(const char *buf)
  1905. {
  1906. static struct trace_option_dentry *topts;
  1907. struct trace_array *tr = &global_trace;
  1908. struct tracer *t;
  1909. int ret = 0;
  1910. mutex_lock(&trace_types_lock);
  1911. for (t = trace_types; t; t = t->next) {
  1912. if (strcmp(t->name, buf) == 0)
  1913. break;
  1914. }
  1915. if (!t) {
  1916. ret = -EINVAL;
  1917. goto out;
  1918. }
  1919. if (t == current_trace)
  1920. goto out;
  1921. trace_branch_disable();
  1922. if (current_trace && current_trace->reset)
  1923. current_trace->reset(tr);
  1924. destroy_trace_option_files(topts);
  1925. current_trace = t;
  1926. topts = create_trace_option_files(current_trace);
  1927. if (t->init) {
  1928. ret = tracer_init(t, tr);
  1929. if (ret)
  1930. goto out;
  1931. }
  1932. trace_branch_enable(tr);
  1933. out:
  1934. mutex_unlock(&trace_types_lock);
  1935. return ret;
  1936. }
  1937. static ssize_t
  1938. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1939. size_t cnt, loff_t *ppos)
  1940. {
  1941. char buf[max_tracer_type_len+1];
  1942. int i;
  1943. size_t ret;
  1944. int err;
  1945. ret = cnt;
  1946. if (cnt > max_tracer_type_len)
  1947. cnt = max_tracer_type_len;
  1948. if (copy_from_user(&buf, ubuf, cnt))
  1949. return -EFAULT;
  1950. buf[cnt] = 0;
  1951. /* strip ending whitespace. */
  1952. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1953. buf[i] = 0;
  1954. err = tracing_set_tracer(buf);
  1955. if (err)
  1956. return err;
  1957. filp->f_pos += ret;
  1958. return ret;
  1959. }
  1960. static ssize_t
  1961. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1962. size_t cnt, loff_t *ppos)
  1963. {
  1964. unsigned long *ptr = filp->private_data;
  1965. char buf[64];
  1966. int r;
  1967. r = snprintf(buf, sizeof(buf), "%ld\n",
  1968. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1969. if (r > sizeof(buf))
  1970. r = sizeof(buf);
  1971. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1972. }
  1973. static ssize_t
  1974. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1975. size_t cnt, loff_t *ppos)
  1976. {
  1977. unsigned long *ptr = filp->private_data;
  1978. char buf[64];
  1979. unsigned long val;
  1980. int ret;
  1981. if (cnt >= sizeof(buf))
  1982. return -EINVAL;
  1983. if (copy_from_user(&buf, ubuf, cnt))
  1984. return -EFAULT;
  1985. buf[cnt] = 0;
  1986. ret = strict_strtoul(buf, 10, &val);
  1987. if (ret < 0)
  1988. return ret;
  1989. *ptr = val * 1000;
  1990. return cnt;
  1991. }
  1992. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1993. {
  1994. long cpu_file = (long) inode->i_private;
  1995. struct trace_iterator *iter;
  1996. int ret = 0;
  1997. if (tracing_disabled)
  1998. return -ENODEV;
  1999. mutex_lock(&trace_types_lock);
  2000. /* We only allow one reader per cpu */
  2001. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  2002. if (!cpumask_empty(tracing_reader_cpumask)) {
  2003. ret = -EBUSY;
  2004. goto out;
  2005. }
  2006. cpumask_setall(tracing_reader_cpumask);
  2007. } else {
  2008. if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
  2009. cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
  2010. else {
  2011. ret = -EBUSY;
  2012. goto out;
  2013. }
  2014. }
  2015. /* create a buffer to store the information to pass to userspace */
  2016. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2017. if (!iter) {
  2018. ret = -ENOMEM;
  2019. goto out;
  2020. }
  2021. /*
  2022. * We make a copy of the current tracer to avoid concurrent
  2023. * changes on it while we are reading.
  2024. */
  2025. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2026. if (!iter->trace) {
  2027. ret = -ENOMEM;
  2028. goto fail;
  2029. }
  2030. if (current_trace)
  2031. *iter->trace = *current_trace;
  2032. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2033. ret = -ENOMEM;
  2034. goto fail;
  2035. }
  2036. /* trace pipe does not show start of buffer */
  2037. cpumask_setall(iter->started);
  2038. iter->cpu_file = cpu_file;
  2039. iter->tr = &global_trace;
  2040. mutex_init(&iter->mutex);
  2041. filp->private_data = iter;
  2042. if (iter->trace->pipe_open)
  2043. iter->trace->pipe_open(iter);
  2044. out:
  2045. mutex_unlock(&trace_types_lock);
  2046. return ret;
  2047. fail:
  2048. kfree(iter->trace);
  2049. kfree(iter);
  2050. mutex_unlock(&trace_types_lock);
  2051. return ret;
  2052. }
  2053. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2054. {
  2055. struct trace_iterator *iter = file->private_data;
  2056. mutex_lock(&trace_types_lock);
  2057. if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
  2058. cpumask_clear(tracing_reader_cpumask);
  2059. else
  2060. cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
  2061. mutex_unlock(&trace_types_lock);
  2062. free_cpumask_var(iter->started);
  2063. mutex_destroy(&iter->mutex);
  2064. kfree(iter->trace);
  2065. kfree(iter);
  2066. return 0;
  2067. }
  2068. static unsigned int
  2069. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2070. {
  2071. struct trace_iterator *iter = filp->private_data;
  2072. if (trace_flags & TRACE_ITER_BLOCK) {
  2073. /*
  2074. * Always select as readable when in blocking mode
  2075. */
  2076. return POLLIN | POLLRDNORM;
  2077. } else {
  2078. if (!trace_empty(iter))
  2079. return POLLIN | POLLRDNORM;
  2080. poll_wait(filp, &trace_wait, poll_table);
  2081. if (!trace_empty(iter))
  2082. return POLLIN | POLLRDNORM;
  2083. return 0;
  2084. }
  2085. }
  2086. void default_wait_pipe(struct trace_iterator *iter)
  2087. {
  2088. DEFINE_WAIT(wait);
  2089. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2090. if (trace_empty(iter))
  2091. schedule();
  2092. finish_wait(&trace_wait, &wait);
  2093. }
  2094. /*
  2095. * This is a make-shift waitqueue.
  2096. * A tracer might use this callback on some rare cases:
  2097. *
  2098. * 1) the current tracer might hold the runqueue lock when it wakes up
  2099. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2100. * 2) the function tracers, trace all functions, we don't want
  2101. * the overhead of calling wake_up and friends
  2102. * (and tracing them too)
  2103. *
  2104. * Anyway, this is really very primitive wakeup.
  2105. */
  2106. void poll_wait_pipe(struct trace_iterator *iter)
  2107. {
  2108. set_current_state(TASK_INTERRUPTIBLE);
  2109. /* sleep for 100 msecs, and try again. */
  2110. schedule_timeout(HZ / 10);
  2111. }
  2112. /* Must be called with trace_types_lock mutex held. */
  2113. static int tracing_wait_pipe(struct file *filp)
  2114. {
  2115. struct trace_iterator *iter = filp->private_data;
  2116. while (trace_empty(iter)) {
  2117. if ((filp->f_flags & O_NONBLOCK)) {
  2118. return -EAGAIN;
  2119. }
  2120. mutex_unlock(&iter->mutex);
  2121. iter->trace->wait_pipe(iter);
  2122. mutex_lock(&iter->mutex);
  2123. if (signal_pending(current))
  2124. return -EINTR;
  2125. /*
  2126. * We block until we read something and tracing is disabled.
  2127. * We still block if tracing is disabled, but we have never
  2128. * read anything. This allows a user to cat this file, and
  2129. * then enable tracing. But after we have read something,
  2130. * we give an EOF when tracing is again disabled.
  2131. *
  2132. * iter->pos will be 0 if we haven't read anything.
  2133. */
  2134. if (!tracer_enabled && iter->pos)
  2135. break;
  2136. }
  2137. return 1;
  2138. }
  2139. /*
  2140. * Consumer reader.
  2141. */
  2142. static ssize_t
  2143. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2144. size_t cnt, loff_t *ppos)
  2145. {
  2146. struct trace_iterator *iter = filp->private_data;
  2147. static struct tracer *old_tracer;
  2148. ssize_t sret;
  2149. /* return any leftover data */
  2150. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2151. if (sret != -EBUSY)
  2152. return sret;
  2153. trace_seq_init(&iter->seq);
  2154. /* copy the tracer to avoid using a global lock all around */
  2155. mutex_lock(&trace_types_lock);
  2156. if (unlikely(old_tracer != current_trace && current_trace)) {
  2157. old_tracer = current_trace;
  2158. *iter->trace = *current_trace;
  2159. }
  2160. mutex_unlock(&trace_types_lock);
  2161. /*
  2162. * Avoid more than one consumer on a single file descriptor
  2163. * This is just a matter of traces coherency, the ring buffer itself
  2164. * is protected.
  2165. */
  2166. mutex_lock(&iter->mutex);
  2167. if (iter->trace->read) {
  2168. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2169. if (sret)
  2170. goto out;
  2171. }
  2172. waitagain:
  2173. sret = tracing_wait_pipe(filp);
  2174. if (sret <= 0)
  2175. goto out;
  2176. /* stop when tracing is finished */
  2177. if (trace_empty(iter)) {
  2178. sret = 0;
  2179. goto out;
  2180. }
  2181. if (cnt >= PAGE_SIZE)
  2182. cnt = PAGE_SIZE - 1;
  2183. /* reset all but tr, trace, and overruns */
  2184. memset(&iter->seq, 0,
  2185. sizeof(struct trace_iterator) -
  2186. offsetof(struct trace_iterator, seq));
  2187. iter->pos = -1;
  2188. while (find_next_entry_inc(iter) != NULL) {
  2189. enum print_line_t ret;
  2190. int len = iter->seq.len;
  2191. ret = print_trace_line(iter);
  2192. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2193. /* don't print partial lines */
  2194. iter->seq.len = len;
  2195. break;
  2196. }
  2197. if (ret != TRACE_TYPE_NO_CONSUME)
  2198. trace_consume(iter);
  2199. if (iter->seq.len >= cnt)
  2200. break;
  2201. }
  2202. /* Now copy what we have to the user */
  2203. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2204. if (iter->seq.readpos >= iter->seq.len)
  2205. trace_seq_init(&iter->seq);
  2206. /*
  2207. * If there was nothing to send to user, inspite of consuming trace
  2208. * entries, go back to wait for more entries.
  2209. */
  2210. if (sret == -EBUSY)
  2211. goto waitagain;
  2212. out:
  2213. mutex_unlock(&iter->mutex);
  2214. return sret;
  2215. }
  2216. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2217. struct pipe_buffer *buf)
  2218. {
  2219. __free_page(buf->page);
  2220. }
  2221. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2222. unsigned int idx)
  2223. {
  2224. __free_page(spd->pages[idx]);
  2225. }
  2226. static struct pipe_buf_operations tracing_pipe_buf_ops = {
  2227. .can_merge = 0,
  2228. .map = generic_pipe_buf_map,
  2229. .unmap = generic_pipe_buf_unmap,
  2230. .confirm = generic_pipe_buf_confirm,
  2231. .release = tracing_pipe_buf_release,
  2232. .steal = generic_pipe_buf_steal,
  2233. .get = generic_pipe_buf_get,
  2234. };
  2235. static size_t
  2236. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2237. {
  2238. size_t count;
  2239. int ret;
  2240. /* Seq buffer is page-sized, exactly what we need. */
  2241. for (;;) {
  2242. count = iter->seq.len;
  2243. ret = print_trace_line(iter);
  2244. count = iter->seq.len - count;
  2245. if (rem < count) {
  2246. rem = 0;
  2247. iter->seq.len -= count;
  2248. break;
  2249. }
  2250. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2251. iter->seq.len -= count;
  2252. break;
  2253. }
  2254. trace_consume(iter);
  2255. rem -= count;
  2256. if (!find_next_entry_inc(iter)) {
  2257. rem = 0;
  2258. iter->ent = NULL;
  2259. break;
  2260. }
  2261. }
  2262. return rem;
  2263. }
  2264. static ssize_t tracing_splice_read_pipe(struct file *filp,
  2265. loff_t *ppos,
  2266. struct pipe_inode_info *pipe,
  2267. size_t len,
  2268. unsigned int flags)
  2269. {
  2270. struct page *pages[PIPE_BUFFERS];
  2271. struct partial_page partial[PIPE_BUFFERS];
  2272. struct trace_iterator *iter = filp->private_data;
  2273. struct splice_pipe_desc spd = {
  2274. .pages = pages,
  2275. .partial = partial,
  2276. .nr_pages = 0, /* This gets updated below. */
  2277. .flags = flags,
  2278. .ops = &tracing_pipe_buf_ops,
  2279. .spd_release = tracing_spd_release_pipe,
  2280. };
  2281. static struct tracer *old_tracer;
  2282. ssize_t ret;
  2283. size_t rem;
  2284. unsigned int i;
  2285. /* copy the tracer to avoid using a global lock all around */
  2286. mutex_lock(&trace_types_lock);
  2287. if (unlikely(old_tracer != current_trace && current_trace)) {
  2288. old_tracer = current_trace;
  2289. *iter->trace = *current_trace;
  2290. }
  2291. mutex_unlock(&trace_types_lock);
  2292. mutex_lock(&iter->mutex);
  2293. if (iter->trace->splice_read) {
  2294. ret = iter->trace->splice_read(iter, filp,
  2295. ppos, pipe, len, flags);
  2296. if (ret)
  2297. goto out_err;
  2298. }
  2299. ret = tracing_wait_pipe(filp);
  2300. if (ret <= 0)
  2301. goto out_err;
  2302. if (!iter->ent && !find_next_entry_inc(iter)) {
  2303. ret = -EFAULT;
  2304. goto out_err;
  2305. }
  2306. /* Fill as many pages as possible. */
  2307. for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
  2308. pages[i] = alloc_page(GFP_KERNEL);
  2309. if (!pages[i])
  2310. break;
  2311. rem = tracing_fill_pipe_page(rem, iter);
  2312. /* Copy the data into the page, so we can start over. */
  2313. ret = trace_seq_to_buffer(&iter->seq,
  2314. page_address(pages[i]),
  2315. iter->seq.len);
  2316. if (ret < 0) {
  2317. __free_page(pages[i]);
  2318. break;
  2319. }
  2320. partial[i].offset = 0;
  2321. partial[i].len = iter->seq.len;
  2322. trace_seq_init(&iter->seq);
  2323. }
  2324. mutex_unlock(&iter->mutex);
  2325. spd.nr_pages = i;
  2326. return splice_to_pipe(pipe, &spd);
  2327. out_err:
  2328. mutex_unlock(&iter->mutex);
  2329. return ret;
  2330. }
  2331. static ssize_t
  2332. tracing_entries_read(struct file *filp, char __user *ubuf,
  2333. size_t cnt, loff_t *ppos)
  2334. {
  2335. struct trace_array *tr = filp->private_data;
  2336. char buf[64];
  2337. int r;
  2338. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2339. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2340. }
  2341. static ssize_t
  2342. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2343. size_t cnt, loff_t *ppos)
  2344. {
  2345. unsigned long val;
  2346. char buf[64];
  2347. int ret, cpu;
  2348. if (cnt >= sizeof(buf))
  2349. return -EINVAL;
  2350. if (copy_from_user(&buf, ubuf, cnt))
  2351. return -EFAULT;
  2352. buf[cnt] = 0;
  2353. ret = strict_strtoul(buf, 10, &val);
  2354. if (ret < 0)
  2355. return ret;
  2356. /* must have at least 1 entry */
  2357. if (!val)
  2358. return -EINVAL;
  2359. mutex_lock(&trace_types_lock);
  2360. tracing_stop();
  2361. /* disable all cpu buffers */
  2362. for_each_tracing_cpu(cpu) {
  2363. if (global_trace.data[cpu])
  2364. atomic_inc(&global_trace.data[cpu]->disabled);
  2365. if (max_tr.data[cpu])
  2366. atomic_inc(&max_tr.data[cpu]->disabled);
  2367. }
  2368. /* value is in KB */
  2369. val <<= 10;
  2370. if (val != global_trace.entries) {
  2371. ret = ring_buffer_resize(global_trace.buffer, val);
  2372. if (ret < 0) {
  2373. cnt = ret;
  2374. goto out;
  2375. }
  2376. ret = ring_buffer_resize(max_tr.buffer, val);
  2377. if (ret < 0) {
  2378. int r;
  2379. cnt = ret;
  2380. r = ring_buffer_resize(global_trace.buffer,
  2381. global_trace.entries);
  2382. if (r < 0) {
  2383. /* AARGH! We are left with different
  2384. * size max buffer!!!! */
  2385. WARN_ON(1);
  2386. tracing_disabled = 1;
  2387. }
  2388. goto out;
  2389. }
  2390. global_trace.entries = val;
  2391. }
  2392. filp->f_pos += cnt;
  2393. /* If check pages failed, return ENOMEM */
  2394. if (tracing_disabled)
  2395. cnt = -ENOMEM;
  2396. out:
  2397. for_each_tracing_cpu(cpu) {
  2398. if (global_trace.data[cpu])
  2399. atomic_dec(&global_trace.data[cpu]->disabled);
  2400. if (max_tr.data[cpu])
  2401. atomic_dec(&max_tr.data[cpu]->disabled);
  2402. }
  2403. tracing_start();
  2404. max_tr.entries = global_trace.entries;
  2405. mutex_unlock(&trace_types_lock);
  2406. return cnt;
  2407. }
  2408. static int mark_printk(const char *fmt, ...)
  2409. {
  2410. int ret;
  2411. va_list args;
  2412. va_start(args, fmt);
  2413. ret = trace_vprintk(0, -1, fmt, args);
  2414. va_end(args);
  2415. return ret;
  2416. }
  2417. static ssize_t
  2418. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2419. size_t cnt, loff_t *fpos)
  2420. {
  2421. char *buf;
  2422. char *end;
  2423. if (tracing_disabled)
  2424. return -EINVAL;
  2425. if (cnt > TRACE_BUF_SIZE)
  2426. cnt = TRACE_BUF_SIZE;
  2427. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2428. if (buf == NULL)
  2429. return -ENOMEM;
  2430. if (copy_from_user(buf, ubuf, cnt)) {
  2431. kfree(buf);
  2432. return -EFAULT;
  2433. }
  2434. /* Cut from the first nil or newline. */
  2435. buf[cnt] = '\0';
  2436. end = strchr(buf, '\n');
  2437. if (end)
  2438. *end = '\0';
  2439. cnt = mark_printk("%s\n", buf);
  2440. kfree(buf);
  2441. *fpos += cnt;
  2442. return cnt;
  2443. }
  2444. static const struct file_operations tracing_max_lat_fops = {
  2445. .open = tracing_open_generic,
  2446. .read = tracing_max_lat_read,
  2447. .write = tracing_max_lat_write,
  2448. };
  2449. static const struct file_operations tracing_ctrl_fops = {
  2450. .open = tracing_open_generic,
  2451. .read = tracing_ctrl_read,
  2452. .write = tracing_ctrl_write,
  2453. };
  2454. static const struct file_operations set_tracer_fops = {
  2455. .open = tracing_open_generic,
  2456. .read = tracing_set_trace_read,
  2457. .write = tracing_set_trace_write,
  2458. };
  2459. static const struct file_operations tracing_pipe_fops = {
  2460. .open = tracing_open_pipe,
  2461. .poll = tracing_poll_pipe,
  2462. .read = tracing_read_pipe,
  2463. .splice_read = tracing_splice_read_pipe,
  2464. .release = tracing_release_pipe,
  2465. };
  2466. static const struct file_operations tracing_entries_fops = {
  2467. .open = tracing_open_generic,
  2468. .read = tracing_entries_read,
  2469. .write = tracing_entries_write,
  2470. };
  2471. static const struct file_operations tracing_mark_fops = {
  2472. .open = tracing_open_generic,
  2473. .write = tracing_mark_write,
  2474. };
  2475. struct ftrace_buffer_info {
  2476. struct trace_array *tr;
  2477. void *spare;
  2478. int cpu;
  2479. unsigned int read;
  2480. };
  2481. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  2482. {
  2483. int cpu = (int)(long)inode->i_private;
  2484. struct ftrace_buffer_info *info;
  2485. if (tracing_disabled)
  2486. return -ENODEV;
  2487. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2488. if (!info)
  2489. return -ENOMEM;
  2490. info->tr = &global_trace;
  2491. info->cpu = cpu;
  2492. info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
  2493. /* Force reading ring buffer for first read */
  2494. info->read = (unsigned int)-1;
  2495. if (!info->spare)
  2496. goto out;
  2497. filp->private_data = info;
  2498. return 0;
  2499. out:
  2500. kfree(info);
  2501. return -ENOMEM;
  2502. }
  2503. static ssize_t
  2504. tracing_buffers_read(struct file *filp, char __user *ubuf,
  2505. size_t count, loff_t *ppos)
  2506. {
  2507. struct ftrace_buffer_info *info = filp->private_data;
  2508. unsigned int pos;
  2509. ssize_t ret;
  2510. size_t size;
  2511. if (!count)
  2512. return 0;
  2513. /* Do we have previous read data to read? */
  2514. if (info->read < PAGE_SIZE)
  2515. goto read;
  2516. info->read = 0;
  2517. ret = ring_buffer_read_page(info->tr->buffer,
  2518. &info->spare,
  2519. count,
  2520. info->cpu, 0);
  2521. if (ret < 0)
  2522. return 0;
  2523. pos = ring_buffer_page_len(info->spare);
  2524. if (pos < PAGE_SIZE)
  2525. memset(info->spare + pos, 0, PAGE_SIZE - pos);
  2526. read:
  2527. size = PAGE_SIZE - info->read;
  2528. if (size > count)
  2529. size = count;
  2530. ret = copy_to_user(ubuf, info->spare + info->read, size);
  2531. if (ret == size)
  2532. return -EFAULT;
  2533. size -= ret;
  2534. *ppos += size;
  2535. info->read += size;
  2536. return size;
  2537. }
  2538. static int tracing_buffers_release(struct inode *inode, struct file *file)
  2539. {
  2540. struct ftrace_buffer_info *info = file->private_data;
  2541. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  2542. kfree(info);
  2543. return 0;
  2544. }
  2545. struct buffer_ref {
  2546. struct ring_buffer *buffer;
  2547. void *page;
  2548. int ref;
  2549. };
  2550. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  2551. struct pipe_buffer *buf)
  2552. {
  2553. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2554. if (--ref->ref)
  2555. return;
  2556. ring_buffer_free_read_page(ref->buffer, ref->page);
  2557. kfree(ref);
  2558. buf->private = 0;
  2559. }
  2560. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  2561. struct pipe_buffer *buf)
  2562. {
  2563. return 1;
  2564. }
  2565. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  2566. struct pipe_buffer *buf)
  2567. {
  2568. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2569. ref->ref++;
  2570. }
  2571. /* Pipe buffer operations for a buffer. */
  2572. static struct pipe_buf_operations buffer_pipe_buf_ops = {
  2573. .can_merge = 0,
  2574. .map = generic_pipe_buf_map,
  2575. .unmap = generic_pipe_buf_unmap,
  2576. .confirm = generic_pipe_buf_confirm,
  2577. .release = buffer_pipe_buf_release,
  2578. .steal = buffer_pipe_buf_steal,
  2579. .get = buffer_pipe_buf_get,
  2580. };
  2581. /*
  2582. * Callback from splice_to_pipe(), if we need to release some pages
  2583. * at the end of the spd in case we error'ed out in filling the pipe.
  2584. */
  2585. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  2586. {
  2587. struct buffer_ref *ref =
  2588. (struct buffer_ref *)spd->partial[i].private;
  2589. if (--ref->ref)
  2590. return;
  2591. ring_buffer_free_read_page(ref->buffer, ref->page);
  2592. kfree(ref);
  2593. spd->partial[i].private = 0;
  2594. }
  2595. static ssize_t
  2596. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  2597. struct pipe_inode_info *pipe, size_t len,
  2598. unsigned int flags)
  2599. {
  2600. struct ftrace_buffer_info *info = file->private_data;
  2601. struct partial_page partial[PIPE_BUFFERS];
  2602. struct page *pages[PIPE_BUFFERS];
  2603. struct splice_pipe_desc spd = {
  2604. .pages = pages,
  2605. .partial = partial,
  2606. .flags = flags,
  2607. .ops = &buffer_pipe_buf_ops,
  2608. .spd_release = buffer_spd_release,
  2609. };
  2610. struct buffer_ref *ref;
  2611. int size, i;
  2612. size_t ret;
  2613. /*
  2614. * We can't seek on a buffer input
  2615. */
  2616. if (unlikely(*ppos))
  2617. return -ESPIPE;
  2618. for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
  2619. struct page *page;
  2620. int r;
  2621. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  2622. if (!ref)
  2623. break;
  2624. ref->buffer = info->tr->buffer;
  2625. ref->page = ring_buffer_alloc_read_page(ref->buffer);
  2626. if (!ref->page) {
  2627. kfree(ref);
  2628. break;
  2629. }
  2630. r = ring_buffer_read_page(ref->buffer, &ref->page,
  2631. len, info->cpu, 0);
  2632. if (r < 0) {
  2633. ring_buffer_free_read_page(ref->buffer,
  2634. ref->page);
  2635. kfree(ref);
  2636. break;
  2637. }
  2638. /*
  2639. * zero out any left over data, this is going to
  2640. * user land.
  2641. */
  2642. size = ring_buffer_page_len(ref->page);
  2643. if (size < PAGE_SIZE)
  2644. memset(ref->page + size, 0, PAGE_SIZE - size);
  2645. page = virt_to_page(ref->page);
  2646. spd.pages[i] = page;
  2647. spd.partial[i].len = PAGE_SIZE;
  2648. spd.partial[i].offset = 0;
  2649. spd.partial[i].private = (unsigned long)ref;
  2650. spd.nr_pages++;
  2651. }
  2652. spd.nr_pages = i;
  2653. /* did we read anything? */
  2654. if (!spd.nr_pages) {
  2655. if (flags & SPLICE_F_NONBLOCK)
  2656. ret = -EAGAIN;
  2657. else
  2658. ret = 0;
  2659. /* TODO: block */
  2660. return ret;
  2661. }
  2662. ret = splice_to_pipe(pipe, &spd);
  2663. return ret;
  2664. }
  2665. static const struct file_operations tracing_buffers_fops = {
  2666. .open = tracing_buffers_open,
  2667. .read = tracing_buffers_read,
  2668. .release = tracing_buffers_release,
  2669. .splice_read = tracing_buffers_splice_read,
  2670. .llseek = no_llseek,
  2671. };
  2672. #ifdef CONFIG_DYNAMIC_FTRACE
  2673. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2674. {
  2675. return 0;
  2676. }
  2677. static ssize_t
  2678. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2679. size_t cnt, loff_t *ppos)
  2680. {
  2681. static char ftrace_dyn_info_buffer[1024];
  2682. static DEFINE_MUTEX(dyn_info_mutex);
  2683. unsigned long *p = filp->private_data;
  2684. char *buf = ftrace_dyn_info_buffer;
  2685. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2686. int r;
  2687. mutex_lock(&dyn_info_mutex);
  2688. r = sprintf(buf, "%ld ", *p);
  2689. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2690. buf[r++] = '\n';
  2691. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2692. mutex_unlock(&dyn_info_mutex);
  2693. return r;
  2694. }
  2695. static const struct file_operations tracing_dyn_info_fops = {
  2696. .open = tracing_open_generic,
  2697. .read = tracing_read_dyn_info,
  2698. };
  2699. #endif
  2700. static struct dentry *d_tracer;
  2701. struct dentry *tracing_init_dentry(void)
  2702. {
  2703. static int once;
  2704. if (d_tracer)
  2705. return d_tracer;
  2706. d_tracer = debugfs_create_dir("tracing", NULL);
  2707. if (!d_tracer && !once) {
  2708. once = 1;
  2709. pr_warning("Could not create debugfs directory 'tracing'\n");
  2710. return NULL;
  2711. }
  2712. return d_tracer;
  2713. }
  2714. static struct dentry *d_percpu;
  2715. struct dentry *tracing_dentry_percpu(void)
  2716. {
  2717. static int once;
  2718. struct dentry *d_tracer;
  2719. if (d_percpu)
  2720. return d_percpu;
  2721. d_tracer = tracing_init_dentry();
  2722. if (!d_tracer)
  2723. return NULL;
  2724. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  2725. if (!d_percpu && !once) {
  2726. once = 1;
  2727. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  2728. return NULL;
  2729. }
  2730. return d_percpu;
  2731. }
  2732. static void tracing_init_debugfs_percpu(long cpu)
  2733. {
  2734. struct dentry *d_percpu = tracing_dentry_percpu();
  2735. struct dentry *entry, *d_cpu;
  2736. /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
  2737. char cpu_dir[7];
  2738. if (cpu > 999 || cpu < 0)
  2739. return;
  2740. sprintf(cpu_dir, "cpu%ld", cpu);
  2741. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  2742. if (!d_cpu) {
  2743. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  2744. return;
  2745. }
  2746. /* per cpu trace_pipe */
  2747. entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
  2748. (void *) cpu, &tracing_pipe_fops);
  2749. if (!entry)
  2750. pr_warning("Could not create debugfs 'trace_pipe' entry\n");
  2751. /* per cpu trace */
  2752. entry = debugfs_create_file("trace", 0444, d_cpu,
  2753. (void *) cpu, &tracing_fops);
  2754. if (!entry)
  2755. pr_warning("Could not create debugfs 'trace' entry\n");
  2756. }
  2757. #ifdef CONFIG_FTRACE_SELFTEST
  2758. /* Let selftest have access to static functions in this file */
  2759. #include "trace_selftest.c"
  2760. #endif
  2761. struct trace_option_dentry {
  2762. struct tracer_opt *opt;
  2763. struct tracer_flags *flags;
  2764. struct dentry *entry;
  2765. };
  2766. static ssize_t
  2767. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  2768. loff_t *ppos)
  2769. {
  2770. struct trace_option_dentry *topt = filp->private_data;
  2771. char *buf;
  2772. if (topt->flags->val & topt->opt->bit)
  2773. buf = "1\n";
  2774. else
  2775. buf = "0\n";
  2776. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  2777. }
  2778. static ssize_t
  2779. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  2780. loff_t *ppos)
  2781. {
  2782. struct trace_option_dentry *topt = filp->private_data;
  2783. unsigned long val;
  2784. char buf[64];
  2785. int ret;
  2786. if (cnt >= sizeof(buf))
  2787. return -EINVAL;
  2788. if (copy_from_user(&buf, ubuf, cnt))
  2789. return -EFAULT;
  2790. buf[cnt] = 0;
  2791. ret = strict_strtoul(buf, 10, &val);
  2792. if (ret < 0)
  2793. return ret;
  2794. ret = 0;
  2795. switch (val) {
  2796. case 0:
  2797. /* do nothing if already cleared */
  2798. if (!(topt->flags->val & topt->opt->bit))
  2799. break;
  2800. mutex_lock(&trace_types_lock);
  2801. if (current_trace->set_flag)
  2802. ret = current_trace->set_flag(topt->flags->val,
  2803. topt->opt->bit, 0);
  2804. mutex_unlock(&trace_types_lock);
  2805. if (ret)
  2806. return ret;
  2807. topt->flags->val &= ~topt->opt->bit;
  2808. break;
  2809. case 1:
  2810. /* do nothing if already set */
  2811. if (topt->flags->val & topt->opt->bit)
  2812. break;
  2813. mutex_lock(&trace_types_lock);
  2814. if (current_trace->set_flag)
  2815. ret = current_trace->set_flag(topt->flags->val,
  2816. topt->opt->bit, 1);
  2817. mutex_unlock(&trace_types_lock);
  2818. if (ret)
  2819. return ret;
  2820. topt->flags->val |= topt->opt->bit;
  2821. break;
  2822. default:
  2823. return -EINVAL;
  2824. }
  2825. *ppos += cnt;
  2826. return cnt;
  2827. }
  2828. static const struct file_operations trace_options_fops = {
  2829. .open = tracing_open_generic,
  2830. .read = trace_options_read,
  2831. .write = trace_options_write,
  2832. };
  2833. static ssize_t
  2834. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  2835. loff_t *ppos)
  2836. {
  2837. long index = (long)filp->private_data;
  2838. char *buf;
  2839. if (trace_flags & (1 << index))
  2840. buf = "1\n";
  2841. else
  2842. buf = "0\n";
  2843. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  2844. }
  2845. static ssize_t
  2846. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  2847. loff_t *ppos)
  2848. {
  2849. long index = (long)filp->private_data;
  2850. char buf[64];
  2851. unsigned long val;
  2852. int ret;
  2853. if (cnt >= sizeof(buf))
  2854. return -EINVAL;
  2855. if (copy_from_user(&buf, ubuf, cnt))
  2856. return -EFAULT;
  2857. buf[cnt] = 0;
  2858. ret = strict_strtoul(buf, 10, &val);
  2859. if (ret < 0)
  2860. return ret;
  2861. switch (val) {
  2862. case 0:
  2863. trace_flags &= ~(1 << index);
  2864. break;
  2865. case 1:
  2866. trace_flags |= 1 << index;
  2867. break;
  2868. default:
  2869. return -EINVAL;
  2870. }
  2871. *ppos += cnt;
  2872. return cnt;
  2873. }
  2874. static const struct file_operations trace_options_core_fops = {
  2875. .open = tracing_open_generic,
  2876. .read = trace_options_core_read,
  2877. .write = trace_options_core_write,
  2878. };
  2879. static struct dentry *trace_options_init_dentry(void)
  2880. {
  2881. struct dentry *d_tracer;
  2882. static struct dentry *t_options;
  2883. if (t_options)
  2884. return t_options;
  2885. d_tracer = tracing_init_dentry();
  2886. if (!d_tracer)
  2887. return NULL;
  2888. t_options = debugfs_create_dir("options", d_tracer);
  2889. if (!t_options) {
  2890. pr_warning("Could not create debugfs directory 'options'\n");
  2891. return NULL;
  2892. }
  2893. return t_options;
  2894. }
  2895. static void
  2896. create_trace_option_file(struct trace_option_dentry *topt,
  2897. struct tracer_flags *flags,
  2898. struct tracer_opt *opt)
  2899. {
  2900. struct dentry *t_options;
  2901. struct dentry *entry;
  2902. t_options = trace_options_init_dentry();
  2903. if (!t_options)
  2904. return;
  2905. topt->flags = flags;
  2906. topt->opt = opt;
  2907. entry = debugfs_create_file(opt->name, 0644, t_options, topt,
  2908. &trace_options_fops);
  2909. topt->entry = entry;
  2910. }
  2911. static struct trace_option_dentry *
  2912. create_trace_option_files(struct tracer *tracer)
  2913. {
  2914. struct trace_option_dentry *topts;
  2915. struct tracer_flags *flags;
  2916. struct tracer_opt *opts;
  2917. int cnt;
  2918. if (!tracer)
  2919. return NULL;
  2920. flags = tracer->flags;
  2921. if (!flags || !flags->opts)
  2922. return NULL;
  2923. opts = flags->opts;
  2924. for (cnt = 0; opts[cnt].name; cnt++)
  2925. ;
  2926. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  2927. if (!topts)
  2928. return NULL;
  2929. for (cnt = 0; opts[cnt].name; cnt++)
  2930. create_trace_option_file(&topts[cnt], flags,
  2931. &opts[cnt]);
  2932. return topts;
  2933. }
  2934. static void
  2935. destroy_trace_option_files(struct trace_option_dentry *topts)
  2936. {
  2937. int cnt;
  2938. if (!topts)
  2939. return;
  2940. for (cnt = 0; topts[cnt].opt; cnt++) {
  2941. if (topts[cnt].entry)
  2942. debugfs_remove(topts[cnt].entry);
  2943. }
  2944. kfree(topts);
  2945. }
  2946. static struct dentry *
  2947. create_trace_option_core_file(const char *option, long index)
  2948. {
  2949. struct dentry *t_options;
  2950. struct dentry *entry;
  2951. t_options = trace_options_init_dentry();
  2952. if (!t_options)
  2953. return NULL;
  2954. entry = debugfs_create_file(option, 0644, t_options, (void *)index,
  2955. &trace_options_core_fops);
  2956. return entry;
  2957. }
  2958. static __init void create_trace_options_dir(void)
  2959. {
  2960. struct dentry *t_options;
  2961. struct dentry *entry;
  2962. int i;
  2963. t_options = trace_options_init_dentry();
  2964. if (!t_options)
  2965. return;
  2966. for (i = 0; trace_options[i]; i++) {
  2967. entry = create_trace_option_core_file(trace_options[i], i);
  2968. if (!entry)
  2969. pr_warning("Could not create debugfs %s entry\n",
  2970. trace_options[i]);
  2971. }
  2972. }
  2973. static __init int tracer_init_debugfs(void)
  2974. {
  2975. struct dentry *d_tracer;
  2976. struct dentry *buffers;
  2977. struct dentry *entry;
  2978. int cpu;
  2979. d_tracer = tracing_init_dentry();
  2980. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2981. &global_trace, &tracing_ctrl_fops);
  2982. if (!entry)
  2983. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2984. entry = debugfs_create_file("trace_options", 0644, d_tracer,
  2985. NULL, &tracing_iter_fops);
  2986. if (!entry)
  2987. pr_warning("Could not create debugfs 'trace_options' entry\n");
  2988. create_trace_options_dir();
  2989. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2990. NULL, &tracing_cpumask_fops);
  2991. if (!entry)
  2992. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2993. entry = debugfs_create_file("trace", 0444, d_tracer,
  2994. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  2995. if (!entry)
  2996. pr_warning("Could not create debugfs 'trace' entry\n");
  2997. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2998. &global_trace, &show_traces_fops);
  2999. if (!entry)
  3000. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  3001. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  3002. &global_trace, &set_tracer_fops);
  3003. if (!entry)
  3004. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  3005. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  3006. &tracing_max_latency,
  3007. &tracing_max_lat_fops);
  3008. if (!entry)
  3009. pr_warning("Could not create debugfs "
  3010. "'tracing_max_latency' entry\n");
  3011. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  3012. &tracing_thresh, &tracing_max_lat_fops);
  3013. if (!entry)
  3014. pr_warning("Could not create debugfs "
  3015. "'tracing_thresh' entry\n");
  3016. entry = debugfs_create_file("README", 0644, d_tracer,
  3017. NULL, &tracing_readme_fops);
  3018. if (!entry)
  3019. pr_warning("Could not create debugfs 'README' entry\n");
  3020. entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
  3021. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3022. if (!entry)
  3023. pr_warning("Could not create debugfs "
  3024. "'trace_pipe' entry\n");
  3025. entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
  3026. &global_trace, &tracing_entries_fops);
  3027. if (!entry)
  3028. pr_warning("Could not create debugfs "
  3029. "'buffer_size_kb' entry\n");
  3030. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  3031. NULL, &tracing_mark_fops);
  3032. if (!entry)
  3033. pr_warning("Could not create debugfs "
  3034. "'trace_marker' entry\n");
  3035. buffers = debugfs_create_dir("binary_buffers", d_tracer);
  3036. if (!buffers)
  3037. pr_warning("Could not create buffers directory\n");
  3038. else {
  3039. int cpu;
  3040. char buf[64];
  3041. for_each_tracing_cpu(cpu) {
  3042. sprintf(buf, "%d", cpu);
  3043. entry = debugfs_create_file(buf, 0444, buffers,
  3044. (void *)(long)cpu,
  3045. &tracing_buffers_fops);
  3046. if (!entry)
  3047. pr_warning("Could not create debugfs buffers "
  3048. "'%s' entry\n", buf);
  3049. }
  3050. }
  3051. #ifdef CONFIG_DYNAMIC_FTRACE
  3052. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3053. &ftrace_update_tot_cnt,
  3054. &tracing_dyn_info_fops);
  3055. if (!entry)
  3056. pr_warning("Could not create debugfs "
  3057. "'dyn_ftrace_total_info' entry\n");
  3058. #endif
  3059. #ifdef CONFIG_SYSPROF_TRACER
  3060. init_tracer_sysprof_debugfs(d_tracer);
  3061. #endif
  3062. for_each_tracing_cpu(cpu)
  3063. tracing_init_debugfs_percpu(cpu);
  3064. return 0;
  3065. }
  3066. static int trace_panic_handler(struct notifier_block *this,
  3067. unsigned long event, void *unused)
  3068. {
  3069. if (ftrace_dump_on_oops)
  3070. ftrace_dump();
  3071. return NOTIFY_OK;
  3072. }
  3073. static struct notifier_block trace_panic_notifier = {
  3074. .notifier_call = trace_panic_handler,
  3075. .next = NULL,
  3076. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3077. };
  3078. static int trace_die_handler(struct notifier_block *self,
  3079. unsigned long val,
  3080. void *data)
  3081. {
  3082. switch (val) {
  3083. case DIE_OOPS:
  3084. if (ftrace_dump_on_oops)
  3085. ftrace_dump();
  3086. break;
  3087. default:
  3088. break;
  3089. }
  3090. return NOTIFY_OK;
  3091. }
  3092. static struct notifier_block trace_die_notifier = {
  3093. .notifier_call = trace_die_handler,
  3094. .priority = 200
  3095. };
  3096. /*
  3097. * printk is set to max of 1024, we really don't need it that big.
  3098. * Nothing should be printing 1000 characters anyway.
  3099. */
  3100. #define TRACE_MAX_PRINT 1000
  3101. /*
  3102. * Define here KERN_TRACE so that we have one place to modify
  3103. * it if we decide to change what log level the ftrace dump
  3104. * should be at.
  3105. */
  3106. #define KERN_TRACE KERN_EMERG
  3107. static void
  3108. trace_printk_seq(struct trace_seq *s)
  3109. {
  3110. /* Probably should print a warning here. */
  3111. if (s->len >= 1000)
  3112. s->len = 1000;
  3113. /* should be zero ended, but we are paranoid. */
  3114. s->buffer[s->len] = 0;
  3115. printk(KERN_TRACE "%s", s->buffer);
  3116. trace_seq_init(s);
  3117. }
  3118. void ftrace_dump(void)
  3119. {
  3120. static DEFINE_SPINLOCK(ftrace_dump_lock);
  3121. /* use static because iter can be a bit big for the stack */
  3122. static struct trace_iterator iter;
  3123. static int dump_ran;
  3124. unsigned long flags;
  3125. int cnt = 0, cpu;
  3126. /* only one dump */
  3127. spin_lock_irqsave(&ftrace_dump_lock, flags);
  3128. if (dump_ran)
  3129. goto out;
  3130. dump_ran = 1;
  3131. /* No turning back! */
  3132. tracing_off();
  3133. ftrace_kill();
  3134. for_each_tracing_cpu(cpu) {
  3135. atomic_inc(&global_trace.data[cpu]->disabled);
  3136. }
  3137. /* don't look at user memory in panic mode */
  3138. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3139. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3140. /* Simulate the iterator */
  3141. iter.tr = &global_trace;
  3142. iter.trace = current_trace;
  3143. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3144. /*
  3145. * We need to stop all tracing on all CPUS to read the
  3146. * the next buffer. This is a bit expensive, but is
  3147. * not done often. We fill all what we can read,
  3148. * and then release the locks again.
  3149. */
  3150. while (!trace_empty(&iter)) {
  3151. if (!cnt)
  3152. printk(KERN_TRACE "---------------------------------\n");
  3153. cnt++;
  3154. /* reset all but tr, trace, and overruns */
  3155. memset(&iter.seq, 0,
  3156. sizeof(struct trace_iterator) -
  3157. offsetof(struct trace_iterator, seq));
  3158. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3159. iter.pos = -1;
  3160. if (find_next_entry_inc(&iter) != NULL) {
  3161. print_trace_line(&iter);
  3162. trace_consume(&iter);
  3163. }
  3164. trace_printk_seq(&iter.seq);
  3165. }
  3166. if (!cnt)
  3167. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3168. else
  3169. printk(KERN_TRACE "---------------------------------\n");
  3170. out:
  3171. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  3172. }
  3173. __init static int tracer_alloc_buffers(void)
  3174. {
  3175. struct trace_array_cpu *data;
  3176. int i;
  3177. int ret = -ENOMEM;
  3178. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  3179. goto out;
  3180. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  3181. goto out_free_buffer_mask;
  3182. if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
  3183. goto out_free_tracing_cpumask;
  3184. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  3185. cpumask_copy(tracing_cpumask, cpu_all_mask);
  3186. cpumask_clear(tracing_reader_cpumask);
  3187. /* TODO: make the number of buffers hot pluggable with CPUS */
  3188. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  3189. TRACE_BUFFER_FLAGS);
  3190. if (!global_trace.buffer) {
  3191. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3192. WARN_ON(1);
  3193. goto out_free_cpumask;
  3194. }
  3195. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3196. #ifdef CONFIG_TRACER_MAX_TRACE
  3197. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  3198. TRACE_BUFFER_FLAGS);
  3199. if (!max_tr.buffer) {
  3200. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3201. WARN_ON(1);
  3202. ring_buffer_free(global_trace.buffer);
  3203. goto out_free_cpumask;
  3204. }
  3205. max_tr.entries = ring_buffer_size(max_tr.buffer);
  3206. WARN_ON(max_tr.entries != global_trace.entries);
  3207. #endif
  3208. /* Allocate the first page for all buffers */
  3209. for_each_tracing_cpu(i) {
  3210. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3211. max_tr.data[i] = &per_cpu(max_data, i);
  3212. }
  3213. trace_init_cmdlines();
  3214. register_tracer(&nop_trace);
  3215. current_trace = &nop_trace;
  3216. #ifdef CONFIG_BOOT_TRACER
  3217. register_tracer(&boot_tracer);
  3218. #endif
  3219. /* All seems OK, enable tracing */
  3220. tracing_disabled = 0;
  3221. atomic_notifier_chain_register(&panic_notifier_list,
  3222. &trace_panic_notifier);
  3223. register_die_notifier(&trace_die_notifier);
  3224. ret = 0;
  3225. out_free_cpumask:
  3226. free_cpumask_var(tracing_reader_cpumask);
  3227. out_free_tracing_cpumask:
  3228. free_cpumask_var(tracing_cpumask);
  3229. out_free_buffer_mask:
  3230. free_cpumask_var(tracing_buffer_mask);
  3231. out:
  3232. return ret;
  3233. }
  3234. __init static int clear_boot_tracer(void)
  3235. {
  3236. /*
  3237. * The default tracer at boot buffer is an init section.
  3238. * This function is called in lateinit. If we did not
  3239. * find the boot tracer, then clear it out, to prevent
  3240. * later registration from accessing the buffer that is
  3241. * about to be freed.
  3242. */
  3243. if (!default_bootup_tracer)
  3244. return 0;
  3245. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  3246. default_bootup_tracer);
  3247. default_bootup_tracer = NULL;
  3248. return 0;
  3249. }
  3250. early_initcall(tracer_alloc_buffers);
  3251. fs_initcall(tracer_init_debugfs);
  3252. late_initcall(clear_boot_tracer);