trace.c 71 KB

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