trace.c 85 KB

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