trace.c 86 KB

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