trace.c 83 KB

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