trace.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  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. struct ring_buffer_event *event;
  827. struct userstack_entry *entry;
  828. struct stack_trace trace;
  829. unsigned long irq_flags;
  830. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  831. return;
  832. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  833. &irq_flags);
  834. if (!event)
  835. return;
  836. entry = ring_buffer_event_data(event);
  837. tracing_generic_entry_update(&entry->ent, flags, pc);
  838. entry->ent.type = TRACE_USER_STACK;
  839. memset(&entry->caller, 0, sizeof(entry->caller));
  840. trace.nr_entries = 0;
  841. trace.max_entries = FTRACE_STACK_ENTRIES;
  842. trace.skip = 0;
  843. trace.entries = entry->caller;
  844. save_stack_trace_user(&trace);
  845. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  846. }
  847. void __trace_userstack(struct trace_array *tr,
  848. struct trace_array_cpu *data,
  849. unsigned long flags)
  850. {
  851. ftrace_trace_userstack(tr, data, flags, preempt_count());
  852. }
  853. static void
  854. ftrace_trace_special(void *__tr, void *__data,
  855. unsigned long arg1, unsigned long arg2, unsigned long arg3,
  856. int pc)
  857. {
  858. struct ring_buffer_event *event;
  859. struct trace_array_cpu *data = __data;
  860. struct trace_array *tr = __tr;
  861. struct special_entry *entry;
  862. unsigned long irq_flags;
  863. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  864. &irq_flags);
  865. if (!event)
  866. return;
  867. entry = ring_buffer_event_data(event);
  868. tracing_generic_entry_update(&entry->ent, 0, pc);
  869. entry->ent.type = TRACE_SPECIAL;
  870. entry->arg1 = arg1;
  871. entry->arg2 = arg2;
  872. entry->arg3 = arg3;
  873. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  874. ftrace_trace_stack(tr, data, irq_flags, 4, pc);
  875. ftrace_trace_userstack(tr, data, irq_flags, pc);
  876. trace_wake_up();
  877. }
  878. void
  879. __trace_special(void *__tr, void *__data,
  880. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  881. {
  882. ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
  883. }
  884. void
  885. tracing_sched_switch_trace(struct trace_array *tr,
  886. struct trace_array_cpu *data,
  887. struct task_struct *prev,
  888. struct task_struct *next,
  889. unsigned long flags, int pc)
  890. {
  891. struct ring_buffer_event *event;
  892. struct ctx_switch_entry *entry;
  893. unsigned long irq_flags;
  894. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  895. &irq_flags);
  896. if (!event)
  897. return;
  898. entry = ring_buffer_event_data(event);
  899. tracing_generic_entry_update(&entry->ent, flags, pc);
  900. entry->ent.type = TRACE_CTX;
  901. entry->prev_pid = prev->pid;
  902. entry->prev_prio = prev->prio;
  903. entry->prev_state = prev->state;
  904. entry->next_pid = next->pid;
  905. entry->next_prio = next->prio;
  906. entry->next_state = next->state;
  907. entry->next_cpu = task_cpu(next);
  908. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  909. ftrace_trace_stack(tr, data, flags, 5, pc);
  910. ftrace_trace_userstack(tr, data, flags, pc);
  911. }
  912. void
  913. tracing_sched_wakeup_trace(struct trace_array *tr,
  914. struct trace_array_cpu *data,
  915. struct task_struct *wakee,
  916. struct task_struct *curr,
  917. unsigned long flags, int pc)
  918. {
  919. struct ring_buffer_event *event;
  920. struct ctx_switch_entry *entry;
  921. unsigned long irq_flags;
  922. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  923. &irq_flags);
  924. if (!event)
  925. return;
  926. entry = ring_buffer_event_data(event);
  927. tracing_generic_entry_update(&entry->ent, flags, pc);
  928. entry->ent.type = TRACE_WAKE;
  929. entry->prev_pid = curr->pid;
  930. entry->prev_prio = curr->prio;
  931. entry->prev_state = curr->state;
  932. entry->next_pid = wakee->pid;
  933. entry->next_prio = wakee->prio;
  934. entry->next_state = wakee->state;
  935. entry->next_cpu = task_cpu(wakee);
  936. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  937. ftrace_trace_stack(tr, data, flags, 6, pc);
  938. ftrace_trace_userstack(tr, data, flags, pc);
  939. trace_wake_up();
  940. }
  941. void
  942. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  943. {
  944. struct trace_array *tr = &global_trace;
  945. struct trace_array_cpu *data;
  946. unsigned long flags;
  947. int cpu;
  948. int pc;
  949. if (tracing_disabled)
  950. return;
  951. pc = preempt_count();
  952. local_irq_save(flags);
  953. cpu = raw_smp_processor_id();
  954. data = tr->data[cpu];
  955. if (likely(atomic_inc_return(&data->disabled) == 1))
  956. ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
  957. atomic_dec(&data->disabled);
  958. local_irq_restore(flags);
  959. }
  960. #ifdef CONFIG_FUNCTION_TRACER
  961. static void
  962. function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
  963. {
  964. struct trace_array *tr = &global_trace;
  965. struct trace_array_cpu *data;
  966. unsigned long flags;
  967. long disabled;
  968. int cpu, resched;
  969. int pc;
  970. if (unlikely(!ftrace_function_enabled))
  971. return;
  972. pc = preempt_count();
  973. resched = ftrace_preempt_disable();
  974. local_save_flags(flags);
  975. cpu = raw_smp_processor_id();
  976. data = tr->data[cpu];
  977. disabled = atomic_inc_return(&data->disabled);
  978. if (likely(disabled == 1))
  979. trace_function(tr, data, ip, parent_ip, flags, pc);
  980. atomic_dec(&data->disabled);
  981. ftrace_preempt_enable(resched);
  982. }
  983. static void
  984. function_trace_call(unsigned long ip, unsigned long parent_ip)
  985. {
  986. struct trace_array *tr = &global_trace;
  987. struct trace_array_cpu *data;
  988. unsigned long flags;
  989. long disabled;
  990. int cpu;
  991. int pc;
  992. if (unlikely(!ftrace_function_enabled))
  993. return;
  994. /*
  995. * Need to use raw, since this must be called before the
  996. * recursive protection is performed.
  997. */
  998. local_irq_save(flags);
  999. cpu = raw_smp_processor_id();
  1000. data = tr->data[cpu];
  1001. disabled = atomic_inc_return(&data->disabled);
  1002. if (likely(disabled == 1)) {
  1003. pc = preempt_count();
  1004. trace_function(tr, data, ip, parent_ip, flags, pc);
  1005. }
  1006. atomic_dec(&data->disabled);
  1007. local_irq_restore(flags);
  1008. }
  1009. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1010. void trace_graph_entry(struct ftrace_graph_ent *trace)
  1011. {
  1012. struct trace_array *tr = &global_trace;
  1013. struct trace_array_cpu *data;
  1014. unsigned long flags;
  1015. long disabled;
  1016. int cpu;
  1017. int pc;
  1018. raw_local_irq_save(flags);
  1019. cpu = raw_smp_processor_id();
  1020. data = tr->data[cpu];
  1021. disabled = atomic_inc_return(&data->disabled);
  1022. if (likely(disabled == 1)) {
  1023. pc = preempt_count();
  1024. __trace_graph_entry(tr, data, trace, flags, pc);
  1025. }
  1026. atomic_dec(&data->disabled);
  1027. raw_local_irq_restore(flags);
  1028. }
  1029. void trace_graph_return(struct ftrace_graph_ret *trace)
  1030. {
  1031. struct trace_array *tr = &global_trace;
  1032. struct trace_array_cpu *data;
  1033. unsigned long flags;
  1034. long disabled;
  1035. int cpu;
  1036. int pc;
  1037. raw_local_irq_save(flags);
  1038. cpu = raw_smp_processor_id();
  1039. data = tr->data[cpu];
  1040. disabled = atomic_inc_return(&data->disabled);
  1041. if (likely(disabled == 1)) {
  1042. pc = preempt_count();
  1043. __trace_graph_return(tr, data, trace, flags, pc);
  1044. }
  1045. atomic_dec(&data->disabled);
  1046. raw_local_irq_restore(flags);
  1047. }
  1048. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1049. static struct ftrace_ops trace_ops __read_mostly =
  1050. {
  1051. .func = function_trace_call,
  1052. };
  1053. void tracing_start_function_trace(void)
  1054. {
  1055. ftrace_function_enabled = 0;
  1056. if (trace_flags & TRACE_ITER_PREEMPTONLY)
  1057. trace_ops.func = function_trace_call_preempt_only;
  1058. else
  1059. trace_ops.func = function_trace_call;
  1060. register_ftrace_function(&trace_ops);
  1061. ftrace_function_enabled = 1;
  1062. }
  1063. void tracing_stop_function_trace(void)
  1064. {
  1065. ftrace_function_enabled = 0;
  1066. unregister_ftrace_function(&trace_ops);
  1067. }
  1068. #endif
  1069. enum trace_file_type {
  1070. TRACE_FILE_LAT_FMT = 1,
  1071. TRACE_FILE_ANNOTATE = 2,
  1072. };
  1073. static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
  1074. {
  1075. /* Don't allow ftrace to trace into the ring buffers */
  1076. ftrace_disable_cpu();
  1077. iter->idx++;
  1078. if (iter->buffer_iter[iter->cpu])
  1079. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1080. ftrace_enable_cpu();
  1081. }
  1082. static struct trace_entry *
  1083. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  1084. {
  1085. struct ring_buffer_event *event;
  1086. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  1087. /* Don't allow ftrace to trace into the ring buffers */
  1088. ftrace_disable_cpu();
  1089. if (buf_iter)
  1090. event = ring_buffer_iter_peek(buf_iter, ts);
  1091. else
  1092. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  1093. ftrace_enable_cpu();
  1094. return event ? ring_buffer_event_data(event) : NULL;
  1095. }
  1096. static struct trace_entry *
  1097. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  1098. {
  1099. struct ring_buffer *buffer = iter->tr->buffer;
  1100. struct trace_entry *ent, *next = NULL;
  1101. u64 next_ts = 0, ts;
  1102. int next_cpu = -1;
  1103. int cpu;
  1104. for_each_tracing_cpu(cpu) {
  1105. if (ring_buffer_empty_cpu(buffer, cpu))
  1106. continue;
  1107. ent = peek_next_entry(iter, cpu, &ts);
  1108. /*
  1109. * Pick the entry with the smallest timestamp:
  1110. */
  1111. if (ent && (!next || ts < next_ts)) {
  1112. next = ent;
  1113. next_cpu = cpu;
  1114. next_ts = ts;
  1115. }
  1116. }
  1117. if (ent_cpu)
  1118. *ent_cpu = next_cpu;
  1119. if (ent_ts)
  1120. *ent_ts = next_ts;
  1121. return next;
  1122. }
  1123. /* Find the next real entry, without updating the iterator itself */
  1124. static struct trace_entry *
  1125. find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  1126. {
  1127. return __find_next_entry(iter, ent_cpu, ent_ts);
  1128. }
  1129. /* Find the next real entry, and increment the iterator to the next entry */
  1130. static void *find_next_entry_inc(struct trace_iterator *iter)
  1131. {
  1132. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  1133. if (iter->ent)
  1134. trace_iterator_increment(iter, iter->cpu);
  1135. return iter->ent ? iter : NULL;
  1136. }
  1137. static void trace_consume(struct trace_iterator *iter)
  1138. {
  1139. /* Don't allow ftrace to trace into the ring buffers */
  1140. ftrace_disable_cpu();
  1141. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  1142. ftrace_enable_cpu();
  1143. }
  1144. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1145. {
  1146. struct trace_iterator *iter = m->private;
  1147. int i = (int)*pos;
  1148. void *ent;
  1149. (*pos)++;
  1150. /* can't go backwards */
  1151. if (iter->idx > i)
  1152. return NULL;
  1153. if (iter->idx < 0)
  1154. ent = find_next_entry_inc(iter);
  1155. else
  1156. ent = iter;
  1157. while (ent && iter->idx < i)
  1158. ent = find_next_entry_inc(iter);
  1159. iter->pos = *pos;
  1160. return ent;
  1161. }
  1162. static void *s_start(struct seq_file *m, loff_t *pos)
  1163. {
  1164. struct trace_iterator *iter = m->private;
  1165. void *p = NULL;
  1166. loff_t l = 0;
  1167. int cpu;
  1168. mutex_lock(&trace_types_lock);
  1169. if (!current_trace || current_trace != iter->trace) {
  1170. mutex_unlock(&trace_types_lock);
  1171. return NULL;
  1172. }
  1173. atomic_inc(&trace_record_cmdline_disabled);
  1174. if (*pos != iter->pos) {
  1175. iter->ent = NULL;
  1176. iter->cpu = 0;
  1177. iter->idx = -1;
  1178. ftrace_disable_cpu();
  1179. for_each_tracing_cpu(cpu) {
  1180. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  1181. }
  1182. ftrace_enable_cpu();
  1183. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1184. ;
  1185. } else {
  1186. l = *pos - 1;
  1187. p = s_next(m, p, &l);
  1188. }
  1189. return p;
  1190. }
  1191. static void s_stop(struct seq_file *m, void *p)
  1192. {
  1193. atomic_dec(&trace_record_cmdline_disabled);
  1194. mutex_unlock(&trace_types_lock);
  1195. }
  1196. #ifdef CONFIG_KRETPROBES
  1197. static inline const char *kretprobed(const char *name)
  1198. {
  1199. static const char tramp_name[] = "kretprobe_trampoline";
  1200. int size = sizeof(tramp_name);
  1201. if (strncmp(tramp_name, name, size) == 0)
  1202. return "[unknown/kretprobe'd]";
  1203. return name;
  1204. }
  1205. #else
  1206. static inline const char *kretprobed(const char *name)
  1207. {
  1208. return name;
  1209. }
  1210. #endif /* CONFIG_KRETPROBES */
  1211. static int
  1212. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  1213. {
  1214. #ifdef CONFIG_KALLSYMS
  1215. char str[KSYM_SYMBOL_LEN];
  1216. const char *name;
  1217. kallsyms_lookup(address, NULL, NULL, NULL, str);
  1218. name = kretprobed(str);
  1219. return trace_seq_printf(s, fmt, name);
  1220. #endif
  1221. return 1;
  1222. }
  1223. static int
  1224. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  1225. unsigned long address)
  1226. {
  1227. #ifdef CONFIG_KALLSYMS
  1228. char str[KSYM_SYMBOL_LEN];
  1229. const char *name;
  1230. sprint_symbol(str, address);
  1231. name = kretprobed(str);
  1232. return trace_seq_printf(s, fmt, name);
  1233. #endif
  1234. return 1;
  1235. }
  1236. #ifndef CONFIG_64BIT
  1237. # define IP_FMT "%08lx"
  1238. #else
  1239. # define IP_FMT "%016lx"
  1240. #endif
  1241. int
  1242. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  1243. {
  1244. int ret;
  1245. if (!ip)
  1246. return trace_seq_printf(s, "0");
  1247. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  1248. ret = seq_print_sym_offset(s, "%s", ip);
  1249. else
  1250. ret = seq_print_sym_short(s, "%s", ip);
  1251. if (!ret)
  1252. return 0;
  1253. if (sym_flags & TRACE_ITER_SYM_ADDR)
  1254. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1255. return ret;
  1256. }
  1257. static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
  1258. unsigned long ip, unsigned long sym_flags)
  1259. {
  1260. struct file *file = NULL;
  1261. unsigned long vmstart = 0;
  1262. int ret = 1;
  1263. if (mm) {
  1264. const struct vm_area_struct *vma;
  1265. down_read(&mm->mmap_sem);
  1266. vma = find_vma(mm, ip);
  1267. if (vma) {
  1268. file = vma->vm_file;
  1269. vmstart = vma->vm_start;
  1270. }
  1271. if (file) {
  1272. ret = trace_seq_path(s, &file->f_path);
  1273. if (ret)
  1274. ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
  1275. }
  1276. up_read(&mm->mmap_sem);
  1277. }
  1278. if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
  1279. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1280. return ret;
  1281. }
  1282. static int
  1283. seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
  1284. unsigned long sym_flags)
  1285. {
  1286. struct mm_struct *mm = NULL;
  1287. int ret = 1;
  1288. unsigned int i;
  1289. if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
  1290. struct task_struct *task;
  1291. /*
  1292. * we do the lookup on the thread group leader,
  1293. * since individual threads might have already quit!
  1294. */
  1295. rcu_read_lock();
  1296. task = find_task_by_vpid(entry->ent.tgid);
  1297. if (task)
  1298. mm = get_task_mm(task);
  1299. rcu_read_unlock();
  1300. }
  1301. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1302. unsigned long ip = entry->caller[i];
  1303. if (ip == ULONG_MAX || !ret)
  1304. break;
  1305. if (i && ret)
  1306. ret = trace_seq_puts(s, " <- ");
  1307. if (!ip) {
  1308. if (ret)
  1309. ret = trace_seq_puts(s, "??");
  1310. continue;
  1311. }
  1312. if (!ret)
  1313. break;
  1314. if (ret)
  1315. ret = seq_print_user_ip(s, mm, ip, sym_flags);
  1316. }
  1317. if (mm)
  1318. mmput(mm);
  1319. return ret;
  1320. }
  1321. static void print_lat_help_header(struct seq_file *m)
  1322. {
  1323. seq_puts(m, "# _------=> CPU# \n");
  1324. seq_puts(m, "# / _-----=> irqs-off \n");
  1325. seq_puts(m, "# | / _----=> need-resched \n");
  1326. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1327. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1328. seq_puts(m, "# |||| / \n");
  1329. seq_puts(m, "# ||||| delay \n");
  1330. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1331. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1332. }
  1333. static void print_func_help_header(struct seq_file *m)
  1334. {
  1335. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1336. seq_puts(m, "# | | | | |\n");
  1337. }
  1338. static void
  1339. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1340. {
  1341. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1342. struct trace_array *tr = iter->tr;
  1343. struct trace_array_cpu *data = tr->data[tr->cpu];
  1344. struct tracer *type = current_trace;
  1345. unsigned long total;
  1346. unsigned long entries;
  1347. const char *name = "preemption";
  1348. if (type)
  1349. name = type->name;
  1350. entries = ring_buffer_entries(iter->tr->buffer);
  1351. total = entries +
  1352. ring_buffer_overruns(iter->tr->buffer);
  1353. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1354. name, UTS_RELEASE);
  1355. seq_puts(m, "-----------------------------------"
  1356. "---------------------------------\n");
  1357. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1358. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1359. nsecs_to_usecs(data->saved_latency),
  1360. entries,
  1361. total,
  1362. tr->cpu,
  1363. #if defined(CONFIG_PREEMPT_NONE)
  1364. "server",
  1365. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1366. "desktop",
  1367. #elif defined(CONFIG_PREEMPT)
  1368. "preempt",
  1369. #else
  1370. "unknown",
  1371. #endif
  1372. /* These are reserved for later use */
  1373. 0, 0, 0, 0);
  1374. #ifdef CONFIG_SMP
  1375. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1376. #else
  1377. seq_puts(m, ")\n");
  1378. #endif
  1379. seq_puts(m, " -----------------\n");
  1380. seq_printf(m, " | task: %.16s-%d "
  1381. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1382. data->comm, data->pid, data->uid, data->nice,
  1383. data->policy, data->rt_priority);
  1384. seq_puts(m, " -----------------\n");
  1385. if (data->critical_start) {
  1386. seq_puts(m, " => started at: ");
  1387. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1388. trace_print_seq(m, &iter->seq);
  1389. seq_puts(m, "\n => ended at: ");
  1390. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1391. trace_print_seq(m, &iter->seq);
  1392. seq_puts(m, "\n");
  1393. }
  1394. seq_puts(m, "\n");
  1395. }
  1396. static void
  1397. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1398. {
  1399. int hardirq, softirq;
  1400. char *comm;
  1401. comm = trace_find_cmdline(entry->pid);
  1402. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1403. trace_seq_printf(s, "%3d", cpu);
  1404. trace_seq_printf(s, "%c%c",
  1405. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  1406. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
  1407. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1408. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1409. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1410. if (hardirq && softirq) {
  1411. trace_seq_putc(s, 'H');
  1412. } else {
  1413. if (hardirq) {
  1414. trace_seq_putc(s, 'h');
  1415. } else {
  1416. if (softirq)
  1417. trace_seq_putc(s, 's');
  1418. else
  1419. trace_seq_putc(s, '.');
  1420. }
  1421. }
  1422. if (entry->preempt_count)
  1423. trace_seq_printf(s, "%x", entry->preempt_count);
  1424. else
  1425. trace_seq_puts(s, ".");
  1426. }
  1427. unsigned long preempt_mark_thresh = 100;
  1428. static void
  1429. lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
  1430. unsigned long rel_usecs)
  1431. {
  1432. trace_seq_printf(s, " %4lldus", abs_usecs);
  1433. if (rel_usecs > preempt_mark_thresh)
  1434. trace_seq_puts(s, "!: ");
  1435. else if (rel_usecs > 1)
  1436. trace_seq_puts(s, "+: ");
  1437. else
  1438. trace_seq_puts(s, " : ");
  1439. }
  1440. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1441. /*
  1442. * The message is supposed to contain an ending newline.
  1443. * If the printing stops prematurely, try to add a newline of our own.
  1444. */
  1445. void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1446. {
  1447. struct trace_entry *ent;
  1448. struct trace_field_cont *cont;
  1449. bool ok = true;
  1450. ent = peek_next_entry(iter, iter->cpu, NULL);
  1451. if (!ent || ent->type != TRACE_CONT) {
  1452. trace_seq_putc(s, '\n');
  1453. return;
  1454. }
  1455. do {
  1456. cont = (struct trace_field_cont *)ent;
  1457. if (ok)
  1458. ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
  1459. ftrace_disable_cpu();
  1460. if (iter->buffer_iter[iter->cpu])
  1461. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1462. else
  1463. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  1464. ftrace_enable_cpu();
  1465. ent = peek_next_entry(iter, iter->cpu, NULL);
  1466. } while (ent && ent->type == TRACE_CONT);
  1467. if (!ok)
  1468. trace_seq_putc(s, '\n');
  1469. }
  1470. static void test_cpu_buff_start(struct trace_iterator *iter)
  1471. {
  1472. struct trace_seq *s = &iter->seq;
  1473. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1474. return;
  1475. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1476. return;
  1477. if (cpu_isset(iter->cpu, iter->started))
  1478. return;
  1479. cpu_set(iter->cpu, iter->started);
  1480. trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
  1481. }
  1482. static enum print_line_t
  1483. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1484. {
  1485. struct trace_seq *s = &iter->seq;
  1486. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1487. struct trace_entry *next_entry;
  1488. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1489. struct trace_entry *entry = iter->ent;
  1490. unsigned long abs_usecs;
  1491. unsigned long rel_usecs;
  1492. u64 next_ts;
  1493. char *comm;
  1494. int S, T;
  1495. int i;
  1496. unsigned state;
  1497. if (entry->type == TRACE_CONT)
  1498. return TRACE_TYPE_HANDLED;
  1499. test_cpu_buff_start(iter);
  1500. next_entry = find_next_entry(iter, NULL, &next_ts);
  1501. if (!next_entry)
  1502. next_ts = iter->ts;
  1503. rel_usecs = ns2usecs(next_ts - iter->ts);
  1504. abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
  1505. if (verbose) {
  1506. comm = trace_find_cmdline(entry->pid);
  1507. trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
  1508. " %ld.%03ldms (+%ld.%03ldms): ",
  1509. comm,
  1510. entry->pid, cpu, entry->flags,
  1511. entry->preempt_count, trace_idx,
  1512. ns2usecs(iter->ts),
  1513. abs_usecs/1000,
  1514. abs_usecs % 1000, rel_usecs/1000,
  1515. rel_usecs % 1000);
  1516. } else {
  1517. lat_print_generic(s, entry, cpu);
  1518. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1519. }
  1520. switch (entry->type) {
  1521. case TRACE_FN: {
  1522. struct ftrace_entry *field;
  1523. trace_assign_type(field, entry);
  1524. seq_print_ip_sym(s, field->ip, sym_flags);
  1525. trace_seq_puts(s, " (");
  1526. seq_print_ip_sym(s, field->parent_ip, sym_flags);
  1527. trace_seq_puts(s, ")\n");
  1528. break;
  1529. }
  1530. case TRACE_CTX:
  1531. case TRACE_WAKE: {
  1532. struct ctx_switch_entry *field;
  1533. trace_assign_type(field, entry);
  1534. T = field->next_state < sizeof(state_to_char) ?
  1535. state_to_char[field->next_state] : 'X';
  1536. state = field->prev_state ?
  1537. __ffs(field->prev_state) + 1 : 0;
  1538. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1539. comm = trace_find_cmdline(field->next_pid);
  1540. trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  1541. field->prev_pid,
  1542. field->prev_prio,
  1543. S, entry->type == TRACE_CTX ? "==>" : " +",
  1544. field->next_cpu,
  1545. field->next_pid,
  1546. field->next_prio,
  1547. T, comm);
  1548. break;
  1549. }
  1550. case TRACE_SPECIAL: {
  1551. struct special_entry *field;
  1552. trace_assign_type(field, entry);
  1553. trace_seq_printf(s, "# %ld %ld %ld\n",
  1554. field->arg1,
  1555. field->arg2,
  1556. field->arg3);
  1557. break;
  1558. }
  1559. case TRACE_STACK: {
  1560. struct stack_entry *field;
  1561. trace_assign_type(field, entry);
  1562. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1563. if (i)
  1564. trace_seq_puts(s, " <= ");
  1565. seq_print_ip_sym(s, field->caller[i], sym_flags);
  1566. }
  1567. trace_seq_puts(s, "\n");
  1568. break;
  1569. }
  1570. case TRACE_PRINT: {
  1571. struct print_entry *field;
  1572. trace_assign_type(field, entry);
  1573. seq_print_ip_sym(s, field->ip, sym_flags);
  1574. trace_seq_printf(s, ": %s", field->buf);
  1575. if (entry->flags & TRACE_FLAG_CONT)
  1576. trace_seq_print_cont(s, iter);
  1577. break;
  1578. }
  1579. case TRACE_BRANCH: {
  1580. struct trace_branch *field;
  1581. trace_assign_type(field, entry);
  1582. trace_seq_printf(s, "[%s] %s:%s:%d\n",
  1583. field->correct ? " ok " : " MISS ",
  1584. field->func,
  1585. field->file,
  1586. field->line);
  1587. break;
  1588. }
  1589. case TRACE_USER_STACK: {
  1590. struct userstack_entry *field;
  1591. trace_assign_type(field, entry);
  1592. seq_print_userip_objs(field, s, sym_flags);
  1593. trace_seq_putc(s, '\n');
  1594. break;
  1595. }
  1596. default:
  1597. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1598. }
  1599. return TRACE_TYPE_HANDLED;
  1600. }
  1601. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1602. {
  1603. struct trace_seq *s = &iter->seq;
  1604. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1605. struct trace_entry *entry;
  1606. unsigned long usec_rem;
  1607. unsigned long long t;
  1608. unsigned long secs;
  1609. char *comm;
  1610. int ret;
  1611. int S, T;
  1612. int i;
  1613. entry = iter->ent;
  1614. if (entry->type == TRACE_CONT)
  1615. return TRACE_TYPE_HANDLED;
  1616. test_cpu_buff_start(iter);
  1617. comm = trace_find_cmdline(iter->ent->pid);
  1618. t = ns2usecs(iter->ts);
  1619. usec_rem = do_div(t, 1000000ULL);
  1620. secs = (unsigned long)t;
  1621. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1622. if (!ret)
  1623. return TRACE_TYPE_PARTIAL_LINE;
  1624. ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
  1625. if (!ret)
  1626. return TRACE_TYPE_PARTIAL_LINE;
  1627. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1628. if (!ret)
  1629. return TRACE_TYPE_PARTIAL_LINE;
  1630. switch (entry->type) {
  1631. case TRACE_FN: {
  1632. struct ftrace_entry *field;
  1633. trace_assign_type(field, entry);
  1634. ret = seq_print_ip_sym(s, field->ip, sym_flags);
  1635. if (!ret)
  1636. return TRACE_TYPE_PARTIAL_LINE;
  1637. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1638. field->parent_ip) {
  1639. ret = trace_seq_printf(s, " <-");
  1640. if (!ret)
  1641. return TRACE_TYPE_PARTIAL_LINE;
  1642. ret = seq_print_ip_sym(s,
  1643. field->parent_ip,
  1644. sym_flags);
  1645. if (!ret)
  1646. return TRACE_TYPE_PARTIAL_LINE;
  1647. }
  1648. ret = trace_seq_printf(s, "\n");
  1649. if (!ret)
  1650. return TRACE_TYPE_PARTIAL_LINE;
  1651. break;
  1652. }
  1653. case TRACE_CTX:
  1654. case TRACE_WAKE: {
  1655. struct ctx_switch_entry *field;
  1656. trace_assign_type(field, entry);
  1657. S = field->prev_state < sizeof(state_to_char) ?
  1658. state_to_char[field->prev_state] : 'X';
  1659. T = field->next_state < sizeof(state_to_char) ?
  1660. state_to_char[field->next_state] : 'X';
  1661. ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
  1662. field->prev_pid,
  1663. field->prev_prio,
  1664. S,
  1665. entry->type == TRACE_CTX ? "==>" : " +",
  1666. field->next_cpu,
  1667. field->next_pid,
  1668. field->next_prio,
  1669. T);
  1670. if (!ret)
  1671. return TRACE_TYPE_PARTIAL_LINE;
  1672. break;
  1673. }
  1674. case TRACE_SPECIAL: {
  1675. struct special_entry *field;
  1676. trace_assign_type(field, entry);
  1677. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1678. field->arg1,
  1679. field->arg2,
  1680. field->arg3);
  1681. if (!ret)
  1682. return TRACE_TYPE_PARTIAL_LINE;
  1683. break;
  1684. }
  1685. case TRACE_STACK: {
  1686. struct stack_entry *field;
  1687. trace_assign_type(field, entry);
  1688. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1689. if (i) {
  1690. ret = trace_seq_puts(s, " <= ");
  1691. if (!ret)
  1692. return TRACE_TYPE_PARTIAL_LINE;
  1693. }
  1694. ret = seq_print_ip_sym(s, field->caller[i],
  1695. sym_flags);
  1696. if (!ret)
  1697. return TRACE_TYPE_PARTIAL_LINE;
  1698. }
  1699. ret = trace_seq_puts(s, "\n");
  1700. if (!ret)
  1701. return TRACE_TYPE_PARTIAL_LINE;
  1702. break;
  1703. }
  1704. case TRACE_PRINT: {
  1705. struct print_entry *field;
  1706. trace_assign_type(field, entry);
  1707. seq_print_ip_sym(s, field->ip, sym_flags);
  1708. trace_seq_printf(s, ": %s", field->buf);
  1709. if (entry->flags & TRACE_FLAG_CONT)
  1710. trace_seq_print_cont(s, iter);
  1711. break;
  1712. }
  1713. case TRACE_GRAPH_RET: {
  1714. return print_graph_function(iter);
  1715. }
  1716. case TRACE_GRAPH_ENT: {
  1717. return print_graph_function(iter);
  1718. }
  1719. case TRACE_BRANCH: {
  1720. struct trace_branch *field;
  1721. trace_assign_type(field, entry);
  1722. trace_seq_printf(s, "[%s] %s:%s:%d\n",
  1723. field->correct ? " ok " : " MISS ",
  1724. field->func,
  1725. field->file,
  1726. field->line);
  1727. break;
  1728. }
  1729. case TRACE_USER_STACK: {
  1730. struct userstack_entry *field;
  1731. trace_assign_type(field, entry);
  1732. ret = seq_print_userip_objs(field, s, sym_flags);
  1733. if (!ret)
  1734. return TRACE_TYPE_PARTIAL_LINE;
  1735. ret = trace_seq_putc(s, '\n');
  1736. if (!ret)
  1737. return TRACE_TYPE_PARTIAL_LINE;
  1738. break;
  1739. }
  1740. }
  1741. return TRACE_TYPE_HANDLED;
  1742. }
  1743. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1744. {
  1745. struct trace_seq *s = &iter->seq;
  1746. struct trace_entry *entry;
  1747. int ret;
  1748. int S, T;
  1749. entry = iter->ent;
  1750. if (entry->type == TRACE_CONT)
  1751. return TRACE_TYPE_HANDLED;
  1752. ret = trace_seq_printf(s, "%d %d %llu ",
  1753. entry->pid, iter->cpu, iter->ts);
  1754. if (!ret)
  1755. return TRACE_TYPE_PARTIAL_LINE;
  1756. switch (entry->type) {
  1757. case TRACE_FN: {
  1758. struct ftrace_entry *field;
  1759. trace_assign_type(field, entry);
  1760. ret = trace_seq_printf(s, "%x %x\n",
  1761. field->ip,
  1762. field->parent_ip);
  1763. if (!ret)
  1764. return TRACE_TYPE_PARTIAL_LINE;
  1765. break;
  1766. }
  1767. case TRACE_CTX:
  1768. case TRACE_WAKE: {
  1769. struct ctx_switch_entry *field;
  1770. trace_assign_type(field, entry);
  1771. S = field->prev_state < sizeof(state_to_char) ?
  1772. state_to_char[field->prev_state] : 'X';
  1773. T = field->next_state < sizeof(state_to_char) ?
  1774. state_to_char[field->next_state] : 'X';
  1775. if (entry->type == TRACE_WAKE)
  1776. S = '+';
  1777. ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
  1778. field->prev_pid,
  1779. field->prev_prio,
  1780. S,
  1781. field->next_cpu,
  1782. field->next_pid,
  1783. field->next_prio,
  1784. T);
  1785. if (!ret)
  1786. return TRACE_TYPE_PARTIAL_LINE;
  1787. break;
  1788. }
  1789. case TRACE_SPECIAL:
  1790. case TRACE_USER_STACK:
  1791. case TRACE_STACK: {
  1792. struct special_entry *field;
  1793. trace_assign_type(field, entry);
  1794. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1795. field->arg1,
  1796. field->arg2,
  1797. field->arg3);
  1798. if (!ret)
  1799. return TRACE_TYPE_PARTIAL_LINE;
  1800. break;
  1801. }
  1802. case TRACE_PRINT: {
  1803. struct print_entry *field;
  1804. trace_assign_type(field, entry);
  1805. trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
  1806. if (entry->flags & TRACE_FLAG_CONT)
  1807. trace_seq_print_cont(s, iter);
  1808. break;
  1809. }
  1810. }
  1811. return TRACE_TYPE_HANDLED;
  1812. }
  1813. #define SEQ_PUT_FIELD_RET(s, x) \
  1814. do { \
  1815. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1816. return 0; \
  1817. } while (0)
  1818. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1819. do { \
  1820. BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
  1821. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1822. return 0; \
  1823. } while (0)
  1824. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1825. {
  1826. struct trace_seq *s = &iter->seq;
  1827. unsigned char newline = '\n';
  1828. struct trace_entry *entry;
  1829. int S, T;
  1830. entry = iter->ent;
  1831. if (entry->type == TRACE_CONT)
  1832. return TRACE_TYPE_HANDLED;
  1833. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1834. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1835. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1836. switch (entry->type) {
  1837. case TRACE_FN: {
  1838. struct ftrace_entry *field;
  1839. trace_assign_type(field, entry);
  1840. SEQ_PUT_HEX_FIELD_RET(s, field->ip);
  1841. SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
  1842. break;
  1843. }
  1844. case TRACE_CTX:
  1845. case TRACE_WAKE: {
  1846. struct ctx_switch_entry *field;
  1847. trace_assign_type(field, entry);
  1848. S = field->prev_state < sizeof(state_to_char) ?
  1849. state_to_char[field->prev_state] : 'X';
  1850. T = field->next_state < sizeof(state_to_char) ?
  1851. state_to_char[field->next_state] : 'X';
  1852. if (entry->type == TRACE_WAKE)
  1853. S = '+';
  1854. SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
  1855. SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
  1856. SEQ_PUT_HEX_FIELD_RET(s, S);
  1857. SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
  1858. SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
  1859. SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
  1860. SEQ_PUT_HEX_FIELD_RET(s, T);
  1861. break;
  1862. }
  1863. case TRACE_SPECIAL:
  1864. case TRACE_USER_STACK:
  1865. case TRACE_STACK: {
  1866. struct special_entry *field;
  1867. trace_assign_type(field, entry);
  1868. SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
  1869. SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
  1870. SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
  1871. break;
  1872. }
  1873. }
  1874. SEQ_PUT_FIELD_RET(s, newline);
  1875. return TRACE_TYPE_HANDLED;
  1876. }
  1877. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1878. {
  1879. struct trace_seq *s = &iter->seq;
  1880. struct trace_entry *entry;
  1881. entry = iter->ent;
  1882. if (entry->type == TRACE_CONT)
  1883. return TRACE_TYPE_HANDLED;
  1884. SEQ_PUT_FIELD_RET(s, entry->pid);
  1885. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1886. SEQ_PUT_FIELD_RET(s, iter->ts);
  1887. switch (entry->type) {
  1888. case TRACE_FN: {
  1889. struct ftrace_entry *field;
  1890. trace_assign_type(field, entry);
  1891. SEQ_PUT_FIELD_RET(s, field->ip);
  1892. SEQ_PUT_FIELD_RET(s, field->parent_ip);
  1893. break;
  1894. }
  1895. case TRACE_CTX: {
  1896. struct ctx_switch_entry *field;
  1897. trace_assign_type(field, entry);
  1898. SEQ_PUT_FIELD_RET(s, field->prev_pid);
  1899. SEQ_PUT_FIELD_RET(s, field->prev_prio);
  1900. SEQ_PUT_FIELD_RET(s, field->prev_state);
  1901. SEQ_PUT_FIELD_RET(s, field->next_pid);
  1902. SEQ_PUT_FIELD_RET(s, field->next_prio);
  1903. SEQ_PUT_FIELD_RET(s, field->next_state);
  1904. break;
  1905. }
  1906. case TRACE_SPECIAL:
  1907. case TRACE_USER_STACK:
  1908. case TRACE_STACK: {
  1909. struct special_entry *field;
  1910. trace_assign_type(field, entry);
  1911. SEQ_PUT_FIELD_RET(s, field->arg1);
  1912. SEQ_PUT_FIELD_RET(s, field->arg2);
  1913. SEQ_PUT_FIELD_RET(s, field->arg3);
  1914. break;
  1915. }
  1916. }
  1917. return 1;
  1918. }
  1919. static int trace_empty(struct trace_iterator *iter)
  1920. {
  1921. int cpu;
  1922. for_each_tracing_cpu(cpu) {
  1923. if (iter->buffer_iter[cpu]) {
  1924. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1925. return 0;
  1926. } else {
  1927. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1928. return 0;
  1929. }
  1930. }
  1931. return 1;
  1932. }
  1933. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1934. {
  1935. enum print_line_t ret;
  1936. if (iter->trace && iter->trace->print_line) {
  1937. ret = iter->trace->print_line(iter);
  1938. if (ret != TRACE_TYPE_UNHANDLED)
  1939. return ret;
  1940. }
  1941. if (trace_flags & TRACE_ITER_BIN)
  1942. return print_bin_fmt(iter);
  1943. if (trace_flags & TRACE_ITER_HEX)
  1944. return print_hex_fmt(iter);
  1945. if (trace_flags & TRACE_ITER_RAW)
  1946. return print_raw_fmt(iter);
  1947. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1948. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1949. return print_trace_fmt(iter);
  1950. }
  1951. static int s_show(struct seq_file *m, void *v)
  1952. {
  1953. struct trace_iterator *iter = v;
  1954. if (iter->ent == NULL) {
  1955. if (iter->tr) {
  1956. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1957. seq_puts(m, "#\n");
  1958. }
  1959. if (iter->trace && iter->trace->print_header)
  1960. iter->trace->print_header(m);
  1961. else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1962. /* print nothing if the buffers are empty */
  1963. if (trace_empty(iter))
  1964. return 0;
  1965. print_trace_header(m, iter);
  1966. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1967. print_lat_help_header(m);
  1968. } else {
  1969. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1970. print_func_help_header(m);
  1971. }
  1972. } else {
  1973. print_trace_line(iter);
  1974. trace_print_seq(m, &iter->seq);
  1975. }
  1976. return 0;
  1977. }
  1978. static struct seq_operations tracer_seq_ops = {
  1979. .start = s_start,
  1980. .next = s_next,
  1981. .stop = s_stop,
  1982. .show = s_show,
  1983. };
  1984. static struct trace_iterator *
  1985. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1986. {
  1987. struct trace_iterator *iter;
  1988. struct seq_file *m;
  1989. int cpu;
  1990. if (tracing_disabled) {
  1991. *ret = -ENODEV;
  1992. return NULL;
  1993. }
  1994. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1995. if (!iter) {
  1996. *ret = -ENOMEM;
  1997. goto out;
  1998. }
  1999. mutex_lock(&trace_types_lock);
  2000. if (current_trace && current_trace->print_max)
  2001. iter->tr = &max_tr;
  2002. else
  2003. iter->tr = inode->i_private;
  2004. iter->trace = current_trace;
  2005. iter->pos = -1;
  2006. /* Notify the tracer early; before we stop tracing. */
  2007. if (iter->trace && iter->trace->open)
  2008. iter->trace->open(iter);
  2009. /* Annotate start of buffers if we had overruns */
  2010. if (ring_buffer_overruns(iter->tr->buffer))
  2011. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  2012. for_each_tracing_cpu(cpu) {
  2013. iter->buffer_iter[cpu] =
  2014. ring_buffer_read_start(iter->tr->buffer, cpu);
  2015. if (!iter->buffer_iter[cpu])
  2016. goto fail_buffer;
  2017. }
  2018. /* TODO stop tracer */
  2019. *ret = seq_open(file, &tracer_seq_ops);
  2020. if (*ret)
  2021. goto fail_buffer;
  2022. m = file->private_data;
  2023. m->private = iter;
  2024. /* stop the trace while dumping */
  2025. tracing_stop();
  2026. mutex_unlock(&trace_types_lock);
  2027. out:
  2028. return iter;
  2029. fail_buffer:
  2030. for_each_tracing_cpu(cpu) {
  2031. if (iter->buffer_iter[cpu])
  2032. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2033. }
  2034. mutex_unlock(&trace_types_lock);
  2035. kfree(iter);
  2036. return ERR_PTR(-ENOMEM);
  2037. }
  2038. int tracing_open_generic(struct inode *inode, struct file *filp)
  2039. {
  2040. if (tracing_disabled)
  2041. return -ENODEV;
  2042. filp->private_data = inode->i_private;
  2043. return 0;
  2044. }
  2045. int tracing_release(struct inode *inode, struct file *file)
  2046. {
  2047. struct seq_file *m = (struct seq_file *)file->private_data;
  2048. struct trace_iterator *iter = m->private;
  2049. int cpu;
  2050. mutex_lock(&trace_types_lock);
  2051. for_each_tracing_cpu(cpu) {
  2052. if (iter->buffer_iter[cpu])
  2053. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2054. }
  2055. if (iter->trace && iter->trace->close)
  2056. iter->trace->close(iter);
  2057. /* reenable tracing if it was previously enabled */
  2058. tracing_start();
  2059. mutex_unlock(&trace_types_lock);
  2060. seq_release(inode, file);
  2061. kfree(iter);
  2062. return 0;
  2063. }
  2064. static int tracing_open(struct inode *inode, struct file *file)
  2065. {
  2066. int ret;
  2067. __tracing_open(inode, file, &ret);
  2068. return ret;
  2069. }
  2070. static int tracing_lt_open(struct inode *inode, struct file *file)
  2071. {
  2072. struct trace_iterator *iter;
  2073. int ret;
  2074. iter = __tracing_open(inode, file, &ret);
  2075. if (!ret)
  2076. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2077. return ret;
  2078. }
  2079. static void *
  2080. t_next(struct seq_file *m, void *v, loff_t *pos)
  2081. {
  2082. struct tracer *t = m->private;
  2083. (*pos)++;
  2084. if (t)
  2085. t = t->next;
  2086. m->private = t;
  2087. return t;
  2088. }
  2089. static void *t_start(struct seq_file *m, loff_t *pos)
  2090. {
  2091. struct tracer *t = m->private;
  2092. loff_t l = 0;
  2093. mutex_lock(&trace_types_lock);
  2094. for (; t && l < *pos; t = t_next(m, t, &l))
  2095. ;
  2096. return t;
  2097. }
  2098. static void t_stop(struct seq_file *m, void *p)
  2099. {
  2100. mutex_unlock(&trace_types_lock);
  2101. }
  2102. static int t_show(struct seq_file *m, void *v)
  2103. {
  2104. struct tracer *t = v;
  2105. if (!t)
  2106. return 0;
  2107. seq_printf(m, "%s", t->name);
  2108. if (t->next)
  2109. seq_putc(m, ' ');
  2110. else
  2111. seq_putc(m, '\n');
  2112. return 0;
  2113. }
  2114. static struct seq_operations show_traces_seq_ops = {
  2115. .start = t_start,
  2116. .next = t_next,
  2117. .stop = t_stop,
  2118. .show = t_show,
  2119. };
  2120. static int show_traces_open(struct inode *inode, struct file *file)
  2121. {
  2122. int ret;
  2123. if (tracing_disabled)
  2124. return -ENODEV;
  2125. ret = seq_open(file, &show_traces_seq_ops);
  2126. if (!ret) {
  2127. struct seq_file *m = file->private_data;
  2128. m->private = trace_types;
  2129. }
  2130. return ret;
  2131. }
  2132. static struct file_operations tracing_fops = {
  2133. .open = tracing_open,
  2134. .read = seq_read,
  2135. .llseek = seq_lseek,
  2136. .release = tracing_release,
  2137. };
  2138. static struct file_operations tracing_lt_fops = {
  2139. .open = tracing_lt_open,
  2140. .read = seq_read,
  2141. .llseek = seq_lseek,
  2142. .release = tracing_release,
  2143. };
  2144. static struct file_operations show_traces_fops = {
  2145. .open = show_traces_open,
  2146. .read = seq_read,
  2147. .release = seq_release,
  2148. };
  2149. /*
  2150. * Only trace on a CPU if the bitmask is set:
  2151. */
  2152. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  2153. /*
  2154. * When tracing/tracing_cpu_mask is modified then this holds
  2155. * the new bitmask we are about to install:
  2156. */
  2157. static cpumask_t tracing_cpumask_new;
  2158. /*
  2159. * The tracer itself will not take this lock, but still we want
  2160. * to provide a consistent cpumask to user-space:
  2161. */
  2162. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2163. /*
  2164. * Temporary storage for the character representation of the
  2165. * CPU bitmask (and one more byte for the newline):
  2166. */
  2167. static char mask_str[NR_CPUS + 1];
  2168. static ssize_t
  2169. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2170. size_t count, loff_t *ppos)
  2171. {
  2172. int len;
  2173. mutex_lock(&tracing_cpumask_update_lock);
  2174. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2175. if (count - len < 2) {
  2176. count = -EINVAL;
  2177. goto out_err;
  2178. }
  2179. len += sprintf(mask_str + len, "\n");
  2180. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2181. out_err:
  2182. mutex_unlock(&tracing_cpumask_update_lock);
  2183. return count;
  2184. }
  2185. static ssize_t
  2186. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2187. size_t count, loff_t *ppos)
  2188. {
  2189. int err, cpu;
  2190. mutex_lock(&tracing_cpumask_update_lock);
  2191. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2192. if (err)
  2193. goto err_unlock;
  2194. raw_local_irq_disable();
  2195. __raw_spin_lock(&ftrace_max_lock);
  2196. for_each_tracing_cpu(cpu) {
  2197. /*
  2198. * Increase/decrease the disabled counter if we are
  2199. * about to flip a bit in the cpumask:
  2200. */
  2201. if (cpu_isset(cpu, tracing_cpumask) &&
  2202. !cpu_isset(cpu, tracing_cpumask_new)) {
  2203. atomic_inc(&global_trace.data[cpu]->disabled);
  2204. }
  2205. if (!cpu_isset(cpu, tracing_cpumask) &&
  2206. cpu_isset(cpu, tracing_cpumask_new)) {
  2207. atomic_dec(&global_trace.data[cpu]->disabled);
  2208. }
  2209. }
  2210. __raw_spin_unlock(&ftrace_max_lock);
  2211. raw_local_irq_enable();
  2212. tracing_cpumask = tracing_cpumask_new;
  2213. mutex_unlock(&tracing_cpumask_update_lock);
  2214. return count;
  2215. err_unlock:
  2216. mutex_unlock(&tracing_cpumask_update_lock);
  2217. return err;
  2218. }
  2219. static struct file_operations tracing_cpumask_fops = {
  2220. .open = tracing_open_generic,
  2221. .read = tracing_cpumask_read,
  2222. .write = tracing_cpumask_write,
  2223. };
  2224. static ssize_t
  2225. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  2226. size_t cnt, loff_t *ppos)
  2227. {
  2228. int i;
  2229. char *buf;
  2230. int r = 0;
  2231. int len = 0;
  2232. u32 tracer_flags = current_trace->flags->val;
  2233. struct tracer_opt *trace_opts = current_trace->flags->opts;
  2234. /* calulate max size */
  2235. for (i = 0; trace_options[i]; i++) {
  2236. len += strlen(trace_options[i]);
  2237. len += 3; /* "no" and space */
  2238. }
  2239. /*
  2240. * Increase the size with names of options specific
  2241. * of the current tracer.
  2242. */
  2243. for (i = 0; trace_opts[i].name; i++) {
  2244. len += strlen(trace_opts[i].name);
  2245. len += 3; /* "no" and space */
  2246. }
  2247. /* +2 for \n and \0 */
  2248. buf = kmalloc(len + 2, GFP_KERNEL);
  2249. if (!buf)
  2250. return -ENOMEM;
  2251. for (i = 0; trace_options[i]; i++) {
  2252. if (trace_flags & (1 << i))
  2253. r += sprintf(buf + r, "%s ", trace_options[i]);
  2254. else
  2255. r += sprintf(buf + r, "no%s ", trace_options[i]);
  2256. }
  2257. for (i = 0; trace_opts[i].name; i++) {
  2258. if (tracer_flags & trace_opts[i].bit)
  2259. r += sprintf(buf + r, "%s ",
  2260. trace_opts[i].name);
  2261. else
  2262. r += sprintf(buf + r, "no%s ",
  2263. trace_opts[i].name);
  2264. }
  2265. r += sprintf(buf + r, "\n");
  2266. WARN_ON(r >= len + 2);
  2267. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2268. kfree(buf);
  2269. return r;
  2270. }
  2271. /* Try to assign a tracer specific option */
  2272. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2273. {
  2274. struct tracer_flags *trace_flags = trace->flags;
  2275. struct tracer_opt *opts = NULL;
  2276. int ret = 0, i = 0;
  2277. int len;
  2278. for (i = 0; trace_flags->opts[i].name; i++) {
  2279. opts = &trace_flags->opts[i];
  2280. len = strlen(opts->name);
  2281. if (strncmp(cmp, opts->name, len) == 0) {
  2282. ret = trace->set_flag(trace_flags->val,
  2283. opts->bit, !neg);
  2284. break;
  2285. }
  2286. }
  2287. /* Not found */
  2288. if (!trace_flags->opts[i].name)
  2289. return -EINVAL;
  2290. /* Refused to handle */
  2291. if (ret)
  2292. return ret;
  2293. if (neg)
  2294. trace_flags->val &= ~opts->bit;
  2295. else
  2296. trace_flags->val |= opts->bit;
  2297. return 0;
  2298. }
  2299. static ssize_t
  2300. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2301. size_t cnt, loff_t *ppos)
  2302. {
  2303. char buf[64];
  2304. char *cmp = buf;
  2305. int neg = 0;
  2306. int ret;
  2307. int i;
  2308. if (cnt >= sizeof(buf))
  2309. return -EINVAL;
  2310. if (copy_from_user(&buf, ubuf, cnt))
  2311. return -EFAULT;
  2312. buf[cnt] = 0;
  2313. if (strncmp(buf, "no", 2) == 0) {
  2314. neg = 1;
  2315. cmp += 2;
  2316. }
  2317. for (i = 0; trace_options[i]; i++) {
  2318. int len = strlen(trace_options[i]);
  2319. if (strncmp(cmp, trace_options[i], len) == 0) {
  2320. if (neg)
  2321. trace_flags &= ~(1 << i);
  2322. else
  2323. trace_flags |= (1 << i);
  2324. break;
  2325. }
  2326. }
  2327. /* If no option could be set, test the specific tracer options */
  2328. if (!trace_options[i]) {
  2329. ret = set_tracer_option(current_trace, cmp, neg);
  2330. if (ret)
  2331. return ret;
  2332. }
  2333. filp->f_pos += cnt;
  2334. return cnt;
  2335. }
  2336. static struct file_operations tracing_iter_fops = {
  2337. .open = tracing_open_generic,
  2338. .read = tracing_trace_options_read,
  2339. .write = tracing_trace_options_write,
  2340. };
  2341. static const char readme_msg[] =
  2342. "tracing mini-HOWTO:\n\n"
  2343. "# mkdir /debug\n"
  2344. "# mount -t debugfs nodev /debug\n\n"
  2345. "# cat /debug/tracing/available_tracers\n"
  2346. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  2347. "# cat /debug/tracing/current_tracer\n"
  2348. "none\n"
  2349. "# echo sched_switch > /debug/tracing/current_tracer\n"
  2350. "# cat /debug/tracing/current_tracer\n"
  2351. "sched_switch\n"
  2352. "# cat /debug/tracing/trace_options\n"
  2353. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2354. "# echo print-parent > /debug/tracing/trace_options\n"
  2355. "# echo 1 > /debug/tracing/tracing_enabled\n"
  2356. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  2357. "echo 0 > /debug/tracing/tracing_enabled\n"
  2358. ;
  2359. static ssize_t
  2360. tracing_readme_read(struct file *filp, char __user *ubuf,
  2361. size_t cnt, loff_t *ppos)
  2362. {
  2363. return simple_read_from_buffer(ubuf, cnt, ppos,
  2364. readme_msg, strlen(readme_msg));
  2365. }
  2366. static struct file_operations tracing_readme_fops = {
  2367. .open = tracing_open_generic,
  2368. .read = tracing_readme_read,
  2369. };
  2370. static ssize_t
  2371. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2372. size_t cnt, loff_t *ppos)
  2373. {
  2374. char buf[64];
  2375. int r;
  2376. r = sprintf(buf, "%u\n", tracer_enabled);
  2377. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2378. }
  2379. static ssize_t
  2380. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2381. size_t cnt, loff_t *ppos)
  2382. {
  2383. struct trace_array *tr = filp->private_data;
  2384. char buf[64];
  2385. long val;
  2386. int ret;
  2387. if (cnt >= sizeof(buf))
  2388. return -EINVAL;
  2389. if (copy_from_user(&buf, ubuf, cnt))
  2390. return -EFAULT;
  2391. buf[cnt] = 0;
  2392. ret = strict_strtoul(buf, 10, &val);
  2393. if (ret < 0)
  2394. return ret;
  2395. val = !!val;
  2396. mutex_lock(&trace_types_lock);
  2397. if (tracer_enabled ^ val) {
  2398. if (val) {
  2399. tracer_enabled = 1;
  2400. if (current_trace->start)
  2401. current_trace->start(tr);
  2402. tracing_start();
  2403. } else {
  2404. tracer_enabled = 0;
  2405. tracing_stop();
  2406. if (current_trace->stop)
  2407. current_trace->stop(tr);
  2408. }
  2409. }
  2410. mutex_unlock(&trace_types_lock);
  2411. filp->f_pos += cnt;
  2412. return cnt;
  2413. }
  2414. static ssize_t
  2415. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2416. size_t cnt, loff_t *ppos)
  2417. {
  2418. char buf[max_tracer_type_len+2];
  2419. int r;
  2420. mutex_lock(&trace_types_lock);
  2421. if (current_trace)
  2422. r = sprintf(buf, "%s\n", current_trace->name);
  2423. else
  2424. r = sprintf(buf, "\n");
  2425. mutex_unlock(&trace_types_lock);
  2426. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2427. }
  2428. static int tracing_set_tracer(char *buf)
  2429. {
  2430. struct trace_array *tr = &global_trace;
  2431. struct tracer *t;
  2432. int ret = 0;
  2433. mutex_lock(&trace_types_lock);
  2434. for (t = trace_types; t; t = t->next) {
  2435. if (strcmp(t->name, buf) == 0)
  2436. break;
  2437. }
  2438. if (!t) {
  2439. ret = -EINVAL;
  2440. goto out;
  2441. }
  2442. if (t == current_trace)
  2443. goto out;
  2444. trace_branch_disable();
  2445. if (current_trace && current_trace->reset)
  2446. current_trace->reset(tr);
  2447. current_trace = t;
  2448. if (t->init) {
  2449. ret = t->init(tr);
  2450. if (ret)
  2451. goto out;
  2452. }
  2453. trace_branch_enable(tr);
  2454. out:
  2455. mutex_unlock(&trace_types_lock);
  2456. return ret;
  2457. }
  2458. static ssize_t
  2459. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2460. size_t cnt, loff_t *ppos)
  2461. {
  2462. char buf[max_tracer_type_len+1];
  2463. int i;
  2464. size_t ret;
  2465. int err;
  2466. ret = cnt;
  2467. if (cnt > max_tracer_type_len)
  2468. cnt = max_tracer_type_len;
  2469. if (copy_from_user(&buf, ubuf, cnt))
  2470. return -EFAULT;
  2471. buf[cnt] = 0;
  2472. /* strip ending whitespace. */
  2473. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2474. buf[i] = 0;
  2475. err = tracing_set_tracer(buf);
  2476. if (err)
  2477. return err;
  2478. filp->f_pos += ret;
  2479. return ret;
  2480. }
  2481. static ssize_t
  2482. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2483. size_t cnt, loff_t *ppos)
  2484. {
  2485. unsigned long *ptr = filp->private_data;
  2486. char buf[64];
  2487. int r;
  2488. r = snprintf(buf, sizeof(buf), "%ld\n",
  2489. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2490. if (r > sizeof(buf))
  2491. r = sizeof(buf);
  2492. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2493. }
  2494. static ssize_t
  2495. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2496. size_t cnt, loff_t *ppos)
  2497. {
  2498. long *ptr = filp->private_data;
  2499. char buf[64];
  2500. long val;
  2501. int ret;
  2502. if (cnt >= sizeof(buf))
  2503. return -EINVAL;
  2504. if (copy_from_user(&buf, ubuf, cnt))
  2505. return -EFAULT;
  2506. buf[cnt] = 0;
  2507. ret = strict_strtoul(buf, 10, &val);
  2508. if (ret < 0)
  2509. return ret;
  2510. *ptr = val * 1000;
  2511. return cnt;
  2512. }
  2513. static atomic_t tracing_reader;
  2514. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2515. {
  2516. struct trace_iterator *iter;
  2517. if (tracing_disabled)
  2518. return -ENODEV;
  2519. /* We only allow for reader of the pipe */
  2520. if (atomic_inc_return(&tracing_reader) != 1) {
  2521. atomic_dec(&tracing_reader);
  2522. return -EBUSY;
  2523. }
  2524. /* create a buffer to store the information to pass to userspace */
  2525. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2526. if (!iter)
  2527. return -ENOMEM;
  2528. mutex_lock(&trace_types_lock);
  2529. /* trace pipe does not show start of buffer */
  2530. cpus_setall(iter->started);
  2531. iter->tr = &global_trace;
  2532. iter->trace = current_trace;
  2533. filp->private_data = iter;
  2534. if (iter->trace->pipe_open)
  2535. iter->trace->pipe_open(iter);
  2536. mutex_unlock(&trace_types_lock);
  2537. return 0;
  2538. }
  2539. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2540. {
  2541. struct trace_iterator *iter = file->private_data;
  2542. kfree(iter);
  2543. atomic_dec(&tracing_reader);
  2544. return 0;
  2545. }
  2546. static unsigned int
  2547. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2548. {
  2549. struct trace_iterator *iter = filp->private_data;
  2550. if (trace_flags & TRACE_ITER_BLOCK) {
  2551. /*
  2552. * Always select as readable when in blocking mode
  2553. */
  2554. return POLLIN | POLLRDNORM;
  2555. } else {
  2556. if (!trace_empty(iter))
  2557. return POLLIN | POLLRDNORM;
  2558. poll_wait(filp, &trace_wait, poll_table);
  2559. if (!trace_empty(iter))
  2560. return POLLIN | POLLRDNORM;
  2561. return 0;
  2562. }
  2563. }
  2564. /*
  2565. * Consumer reader.
  2566. */
  2567. static ssize_t
  2568. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2569. size_t cnt, loff_t *ppos)
  2570. {
  2571. struct trace_iterator *iter = filp->private_data;
  2572. ssize_t sret;
  2573. /* return any leftover data */
  2574. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2575. if (sret != -EBUSY)
  2576. return sret;
  2577. trace_seq_reset(&iter->seq);
  2578. mutex_lock(&trace_types_lock);
  2579. if (iter->trace->read) {
  2580. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2581. if (sret)
  2582. goto out;
  2583. }
  2584. waitagain:
  2585. sret = 0;
  2586. while (trace_empty(iter)) {
  2587. if ((filp->f_flags & O_NONBLOCK)) {
  2588. sret = -EAGAIN;
  2589. goto out;
  2590. }
  2591. /*
  2592. * This is a make-shift waitqueue. The reason we don't use
  2593. * an actual wait queue is because:
  2594. * 1) we only ever have one waiter
  2595. * 2) the tracing, traces all functions, we don't want
  2596. * the overhead of calling wake_up and friends
  2597. * (and tracing them too)
  2598. * Anyway, this is really very primitive wakeup.
  2599. */
  2600. set_current_state(TASK_INTERRUPTIBLE);
  2601. iter->tr->waiter = current;
  2602. mutex_unlock(&trace_types_lock);
  2603. /* sleep for 100 msecs, and try again. */
  2604. schedule_timeout(HZ/10);
  2605. mutex_lock(&trace_types_lock);
  2606. iter->tr->waiter = NULL;
  2607. if (signal_pending(current)) {
  2608. sret = -EINTR;
  2609. goto out;
  2610. }
  2611. if (iter->trace != current_trace)
  2612. goto out;
  2613. /*
  2614. * We block until we read something and tracing is disabled.
  2615. * We still block if tracing is disabled, but we have never
  2616. * read anything. This allows a user to cat this file, and
  2617. * then enable tracing. But after we have read something,
  2618. * we give an EOF when tracing is again disabled.
  2619. *
  2620. * iter->pos will be 0 if we haven't read anything.
  2621. */
  2622. if (!tracer_enabled && iter->pos)
  2623. break;
  2624. continue;
  2625. }
  2626. /* stop when tracing is finished */
  2627. if (trace_empty(iter))
  2628. goto out;
  2629. if (cnt >= PAGE_SIZE)
  2630. cnt = PAGE_SIZE - 1;
  2631. /* reset all but tr, trace, and overruns */
  2632. memset(&iter->seq, 0,
  2633. sizeof(struct trace_iterator) -
  2634. offsetof(struct trace_iterator, seq));
  2635. iter->pos = -1;
  2636. while (find_next_entry_inc(iter) != NULL) {
  2637. enum print_line_t ret;
  2638. int len = iter->seq.len;
  2639. ret = print_trace_line(iter);
  2640. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2641. /* don't print partial lines */
  2642. iter->seq.len = len;
  2643. break;
  2644. }
  2645. trace_consume(iter);
  2646. if (iter->seq.len >= cnt)
  2647. break;
  2648. }
  2649. /* Now copy what we have to the user */
  2650. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2651. if (iter->seq.readpos >= iter->seq.len)
  2652. trace_seq_reset(&iter->seq);
  2653. /*
  2654. * If there was nothing to send to user, inspite of consuming trace
  2655. * entries, go back to wait for more entries.
  2656. */
  2657. if (sret == -EBUSY)
  2658. goto waitagain;
  2659. out:
  2660. mutex_unlock(&trace_types_lock);
  2661. return sret;
  2662. }
  2663. static ssize_t
  2664. tracing_entries_read(struct file *filp, char __user *ubuf,
  2665. size_t cnt, loff_t *ppos)
  2666. {
  2667. struct trace_array *tr = filp->private_data;
  2668. char buf[64];
  2669. int r;
  2670. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2671. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2672. }
  2673. static ssize_t
  2674. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2675. size_t cnt, loff_t *ppos)
  2676. {
  2677. unsigned long val;
  2678. char buf[64];
  2679. int ret, cpu;
  2680. if (cnt >= sizeof(buf))
  2681. return -EINVAL;
  2682. if (copy_from_user(&buf, ubuf, cnt))
  2683. return -EFAULT;
  2684. buf[cnt] = 0;
  2685. ret = strict_strtoul(buf, 10, &val);
  2686. if (ret < 0)
  2687. return ret;
  2688. /* must have at least 1 entry */
  2689. if (!val)
  2690. return -EINVAL;
  2691. mutex_lock(&trace_types_lock);
  2692. tracing_stop();
  2693. /* disable all cpu buffers */
  2694. for_each_tracing_cpu(cpu) {
  2695. if (global_trace.data[cpu])
  2696. atomic_inc(&global_trace.data[cpu]->disabled);
  2697. if (max_tr.data[cpu])
  2698. atomic_inc(&max_tr.data[cpu]->disabled);
  2699. }
  2700. /* value is in KB */
  2701. val <<= 10;
  2702. if (val != global_trace.entries) {
  2703. ret = ring_buffer_resize(global_trace.buffer, val);
  2704. if (ret < 0) {
  2705. cnt = ret;
  2706. goto out;
  2707. }
  2708. ret = ring_buffer_resize(max_tr.buffer, val);
  2709. if (ret < 0) {
  2710. int r;
  2711. cnt = ret;
  2712. r = ring_buffer_resize(global_trace.buffer,
  2713. global_trace.entries);
  2714. if (r < 0) {
  2715. /* AARGH! We are left with different
  2716. * size max buffer!!!! */
  2717. WARN_ON(1);
  2718. tracing_disabled = 1;
  2719. }
  2720. goto out;
  2721. }
  2722. global_trace.entries = val;
  2723. }
  2724. filp->f_pos += cnt;
  2725. /* If check pages failed, return ENOMEM */
  2726. if (tracing_disabled)
  2727. cnt = -ENOMEM;
  2728. out:
  2729. for_each_tracing_cpu(cpu) {
  2730. if (global_trace.data[cpu])
  2731. atomic_dec(&global_trace.data[cpu]->disabled);
  2732. if (max_tr.data[cpu])
  2733. atomic_dec(&max_tr.data[cpu]->disabled);
  2734. }
  2735. tracing_start();
  2736. max_tr.entries = global_trace.entries;
  2737. mutex_unlock(&trace_types_lock);
  2738. return cnt;
  2739. }
  2740. static int mark_printk(const char *fmt, ...)
  2741. {
  2742. int ret;
  2743. va_list args;
  2744. va_start(args, fmt);
  2745. ret = trace_vprintk(0, fmt, args);
  2746. va_end(args);
  2747. return ret;
  2748. }
  2749. static ssize_t
  2750. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2751. size_t cnt, loff_t *fpos)
  2752. {
  2753. char *buf;
  2754. char *end;
  2755. if (tracing_disabled)
  2756. return -EINVAL;
  2757. if (cnt > TRACE_BUF_SIZE)
  2758. cnt = TRACE_BUF_SIZE;
  2759. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2760. if (buf == NULL)
  2761. return -ENOMEM;
  2762. if (copy_from_user(buf, ubuf, cnt)) {
  2763. kfree(buf);
  2764. return -EFAULT;
  2765. }
  2766. /* Cut from the first nil or newline. */
  2767. buf[cnt] = '\0';
  2768. end = strchr(buf, '\n');
  2769. if (end)
  2770. *end = '\0';
  2771. cnt = mark_printk("%s\n", buf);
  2772. kfree(buf);
  2773. *fpos += cnt;
  2774. return cnt;
  2775. }
  2776. static struct file_operations tracing_max_lat_fops = {
  2777. .open = tracing_open_generic,
  2778. .read = tracing_max_lat_read,
  2779. .write = tracing_max_lat_write,
  2780. };
  2781. static struct file_operations tracing_ctrl_fops = {
  2782. .open = tracing_open_generic,
  2783. .read = tracing_ctrl_read,
  2784. .write = tracing_ctrl_write,
  2785. };
  2786. static struct file_operations set_tracer_fops = {
  2787. .open = tracing_open_generic,
  2788. .read = tracing_set_trace_read,
  2789. .write = tracing_set_trace_write,
  2790. };
  2791. static struct file_operations tracing_pipe_fops = {
  2792. .open = tracing_open_pipe,
  2793. .poll = tracing_poll_pipe,
  2794. .read = tracing_read_pipe,
  2795. .release = tracing_release_pipe,
  2796. };
  2797. static struct file_operations tracing_entries_fops = {
  2798. .open = tracing_open_generic,
  2799. .read = tracing_entries_read,
  2800. .write = tracing_entries_write,
  2801. };
  2802. static struct file_operations tracing_mark_fops = {
  2803. .open = tracing_open_generic,
  2804. .write = tracing_mark_write,
  2805. };
  2806. #ifdef CONFIG_DYNAMIC_FTRACE
  2807. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2808. {
  2809. return 0;
  2810. }
  2811. static ssize_t
  2812. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2813. size_t cnt, loff_t *ppos)
  2814. {
  2815. static char ftrace_dyn_info_buffer[1024];
  2816. static DEFINE_MUTEX(dyn_info_mutex);
  2817. unsigned long *p = filp->private_data;
  2818. char *buf = ftrace_dyn_info_buffer;
  2819. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2820. int r;
  2821. mutex_lock(&dyn_info_mutex);
  2822. r = sprintf(buf, "%ld ", *p);
  2823. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2824. buf[r++] = '\n';
  2825. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2826. mutex_unlock(&dyn_info_mutex);
  2827. return r;
  2828. }
  2829. static struct file_operations tracing_dyn_info_fops = {
  2830. .open = tracing_open_generic,
  2831. .read = tracing_read_dyn_info,
  2832. };
  2833. #endif
  2834. static struct dentry *d_tracer;
  2835. struct dentry *tracing_init_dentry(void)
  2836. {
  2837. static int once;
  2838. if (d_tracer)
  2839. return d_tracer;
  2840. d_tracer = debugfs_create_dir("tracing", NULL);
  2841. if (!d_tracer && !once) {
  2842. once = 1;
  2843. pr_warning("Could not create debugfs directory 'tracing'\n");
  2844. return NULL;
  2845. }
  2846. return d_tracer;
  2847. }
  2848. #ifdef CONFIG_FTRACE_SELFTEST
  2849. /* Let selftest have access to static functions in this file */
  2850. #include "trace_selftest.c"
  2851. #endif
  2852. static __init int tracer_init_debugfs(void)
  2853. {
  2854. struct dentry *d_tracer;
  2855. struct dentry *entry;
  2856. d_tracer = tracing_init_dentry();
  2857. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2858. &global_trace, &tracing_ctrl_fops);
  2859. if (!entry)
  2860. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2861. entry = debugfs_create_file("trace_options", 0644, d_tracer,
  2862. NULL, &tracing_iter_fops);
  2863. if (!entry)
  2864. pr_warning("Could not create debugfs 'trace_options' entry\n");
  2865. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2866. NULL, &tracing_cpumask_fops);
  2867. if (!entry)
  2868. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2869. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2870. &global_trace, &tracing_lt_fops);
  2871. if (!entry)
  2872. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2873. entry = debugfs_create_file("trace", 0444, d_tracer,
  2874. &global_trace, &tracing_fops);
  2875. if (!entry)
  2876. pr_warning("Could not create debugfs 'trace' entry\n");
  2877. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2878. &global_trace, &show_traces_fops);
  2879. if (!entry)
  2880. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2881. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2882. &global_trace, &set_tracer_fops);
  2883. if (!entry)
  2884. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2885. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2886. &tracing_max_latency,
  2887. &tracing_max_lat_fops);
  2888. if (!entry)
  2889. pr_warning("Could not create debugfs "
  2890. "'tracing_max_latency' entry\n");
  2891. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2892. &tracing_thresh, &tracing_max_lat_fops);
  2893. if (!entry)
  2894. pr_warning("Could not create debugfs "
  2895. "'tracing_thresh' entry\n");
  2896. entry = debugfs_create_file("README", 0644, d_tracer,
  2897. NULL, &tracing_readme_fops);
  2898. if (!entry)
  2899. pr_warning("Could not create debugfs 'README' entry\n");
  2900. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2901. NULL, &tracing_pipe_fops);
  2902. if (!entry)
  2903. pr_warning("Could not create debugfs "
  2904. "'trace_pipe' entry\n");
  2905. entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
  2906. &global_trace, &tracing_entries_fops);
  2907. if (!entry)
  2908. pr_warning("Could not create debugfs "
  2909. "'buffer_size_kb' entry\n");
  2910. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  2911. NULL, &tracing_mark_fops);
  2912. if (!entry)
  2913. pr_warning("Could not create debugfs "
  2914. "'trace_marker' entry\n");
  2915. #ifdef CONFIG_DYNAMIC_FTRACE
  2916. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2917. &ftrace_update_tot_cnt,
  2918. &tracing_dyn_info_fops);
  2919. if (!entry)
  2920. pr_warning("Could not create debugfs "
  2921. "'dyn_ftrace_total_info' entry\n");
  2922. #endif
  2923. #ifdef CONFIG_SYSPROF_TRACER
  2924. init_tracer_sysprof_debugfs(d_tracer);
  2925. #endif
  2926. return 0;
  2927. }
  2928. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2929. {
  2930. static DEFINE_SPINLOCK(trace_buf_lock);
  2931. static char trace_buf[TRACE_BUF_SIZE];
  2932. struct ring_buffer_event *event;
  2933. struct trace_array *tr = &global_trace;
  2934. struct trace_array_cpu *data;
  2935. struct print_entry *entry;
  2936. unsigned long flags, irq_flags;
  2937. int cpu, len = 0, size, pc;
  2938. if (tracing_disabled)
  2939. return 0;
  2940. pc = preempt_count();
  2941. preempt_disable_notrace();
  2942. cpu = raw_smp_processor_id();
  2943. data = tr->data[cpu];
  2944. if (unlikely(atomic_read(&data->disabled)))
  2945. goto out;
  2946. spin_lock_irqsave(&trace_buf_lock, flags);
  2947. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  2948. len = min(len, TRACE_BUF_SIZE-1);
  2949. trace_buf[len] = 0;
  2950. size = sizeof(*entry) + len + 1;
  2951. event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
  2952. if (!event)
  2953. goto out_unlock;
  2954. entry = ring_buffer_event_data(event);
  2955. tracing_generic_entry_update(&entry->ent, flags, pc);
  2956. entry->ent.type = TRACE_PRINT;
  2957. entry->ip = ip;
  2958. memcpy(&entry->buf, trace_buf, len);
  2959. entry->buf[len] = 0;
  2960. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  2961. out_unlock:
  2962. spin_unlock_irqrestore(&trace_buf_lock, flags);
  2963. out:
  2964. preempt_enable_notrace();
  2965. return len;
  2966. }
  2967. EXPORT_SYMBOL_GPL(trace_vprintk);
  2968. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2969. {
  2970. int ret;
  2971. va_list ap;
  2972. if (!(trace_flags & TRACE_ITER_PRINTK))
  2973. return 0;
  2974. va_start(ap, fmt);
  2975. ret = trace_vprintk(ip, fmt, ap);
  2976. va_end(ap);
  2977. return ret;
  2978. }
  2979. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2980. static int trace_panic_handler(struct notifier_block *this,
  2981. unsigned long event, void *unused)
  2982. {
  2983. if (ftrace_dump_on_oops)
  2984. ftrace_dump();
  2985. return NOTIFY_OK;
  2986. }
  2987. static struct notifier_block trace_panic_notifier = {
  2988. .notifier_call = trace_panic_handler,
  2989. .next = NULL,
  2990. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2991. };
  2992. static int trace_die_handler(struct notifier_block *self,
  2993. unsigned long val,
  2994. void *data)
  2995. {
  2996. switch (val) {
  2997. case DIE_OOPS:
  2998. if (ftrace_dump_on_oops)
  2999. ftrace_dump();
  3000. break;
  3001. default:
  3002. break;
  3003. }
  3004. return NOTIFY_OK;
  3005. }
  3006. static struct notifier_block trace_die_notifier = {
  3007. .notifier_call = trace_die_handler,
  3008. .priority = 200
  3009. };
  3010. /*
  3011. * printk is set to max of 1024, we really don't need it that big.
  3012. * Nothing should be printing 1000 characters anyway.
  3013. */
  3014. #define TRACE_MAX_PRINT 1000
  3015. /*
  3016. * Define here KERN_TRACE so that we have one place to modify
  3017. * it if we decide to change what log level the ftrace dump
  3018. * should be at.
  3019. */
  3020. #define KERN_TRACE KERN_INFO
  3021. static void
  3022. trace_printk_seq(struct trace_seq *s)
  3023. {
  3024. /* Probably should print a warning here. */
  3025. if (s->len >= 1000)
  3026. s->len = 1000;
  3027. /* should be zero ended, but we are paranoid. */
  3028. s->buffer[s->len] = 0;
  3029. printk(KERN_TRACE "%s", s->buffer);
  3030. trace_seq_reset(s);
  3031. }
  3032. void ftrace_dump(void)
  3033. {
  3034. static DEFINE_SPINLOCK(ftrace_dump_lock);
  3035. /* use static because iter can be a bit big for the stack */
  3036. static struct trace_iterator iter;
  3037. static cpumask_t mask;
  3038. static int dump_ran;
  3039. unsigned long flags;
  3040. int cnt = 0, cpu;
  3041. /* only one dump */
  3042. spin_lock_irqsave(&ftrace_dump_lock, flags);
  3043. if (dump_ran)
  3044. goto out;
  3045. dump_ran = 1;
  3046. /* No turning back! */
  3047. ftrace_kill();
  3048. for_each_tracing_cpu(cpu) {
  3049. atomic_inc(&global_trace.data[cpu]->disabled);
  3050. }
  3051. /* don't look at user memory in panic mode */
  3052. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3053. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3054. iter.tr = &global_trace;
  3055. iter.trace = current_trace;
  3056. /*
  3057. * We need to stop all tracing on all CPUS to read the
  3058. * the next buffer. This is a bit expensive, but is
  3059. * not done often. We fill all what we can read,
  3060. * and then release the locks again.
  3061. */
  3062. cpus_clear(mask);
  3063. while (!trace_empty(&iter)) {
  3064. if (!cnt)
  3065. printk(KERN_TRACE "---------------------------------\n");
  3066. cnt++;
  3067. /* reset all but tr, trace, and overruns */
  3068. memset(&iter.seq, 0,
  3069. sizeof(struct trace_iterator) -
  3070. offsetof(struct trace_iterator, seq));
  3071. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3072. iter.pos = -1;
  3073. if (find_next_entry_inc(&iter) != NULL) {
  3074. print_trace_line(&iter);
  3075. trace_consume(&iter);
  3076. }
  3077. trace_printk_seq(&iter.seq);
  3078. }
  3079. if (!cnt)
  3080. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3081. else
  3082. printk(KERN_TRACE "---------------------------------\n");
  3083. out:
  3084. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  3085. }
  3086. __init static int tracer_alloc_buffers(void)
  3087. {
  3088. struct trace_array_cpu *data;
  3089. int i;
  3090. /* TODO: make the number of buffers hot pluggable with CPUS */
  3091. tracing_buffer_mask = cpu_possible_map;
  3092. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  3093. TRACE_BUFFER_FLAGS);
  3094. if (!global_trace.buffer) {
  3095. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3096. WARN_ON(1);
  3097. return 0;
  3098. }
  3099. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3100. #ifdef CONFIG_TRACER_MAX_TRACE
  3101. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  3102. TRACE_BUFFER_FLAGS);
  3103. if (!max_tr.buffer) {
  3104. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3105. WARN_ON(1);
  3106. ring_buffer_free(global_trace.buffer);
  3107. return 0;
  3108. }
  3109. max_tr.entries = ring_buffer_size(max_tr.buffer);
  3110. WARN_ON(max_tr.entries != global_trace.entries);
  3111. #endif
  3112. /* Allocate the first page for all buffers */
  3113. for_each_tracing_cpu(i) {
  3114. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3115. max_tr.data[i] = &per_cpu(max_data, i);
  3116. }
  3117. trace_init_cmdlines();
  3118. register_tracer(&nop_trace);
  3119. #ifdef CONFIG_BOOT_TRACER
  3120. register_tracer(&boot_tracer);
  3121. current_trace = &boot_tracer;
  3122. current_trace->init(&global_trace);
  3123. #else
  3124. current_trace = &nop_trace;
  3125. #endif
  3126. /* All seems OK, enable tracing */
  3127. tracing_disabled = 0;
  3128. atomic_notifier_chain_register(&panic_notifier_list,
  3129. &trace_panic_notifier);
  3130. register_die_notifier(&trace_die_notifier);
  3131. return 0;
  3132. }
  3133. early_initcall(tracer_alloc_buffers);
  3134. fs_initcall(tracer_init_debugfs);