trace.c 90 KB

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