trace.c 82 KB

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