trace.c 73 KB

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