trace.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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/debugfs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/linkage.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/module.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ctype.h>
  26. #include <linux/init.h>
  27. #include <linux/poll.h>
  28. #include <linux/gfp.h>
  29. #include <linux/fs.h>
  30. #include "trace.h"
  31. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  32. unsigned long __read_mostly tracing_thresh;
  33. static int tracing_disabled = 1;
  34. static long
  35. ns2usecs(cycle_t nsec)
  36. {
  37. nsec += 500;
  38. do_div(nsec, 1000);
  39. return nsec;
  40. }
  41. cycle_t ftrace_now(int cpu)
  42. {
  43. return cpu_clock(cpu);
  44. }
  45. static struct trace_array global_trace;
  46. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  47. static struct trace_array max_tr;
  48. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  49. static int tracer_enabled = 1;
  50. static unsigned long trace_nr_entries = 65536UL;
  51. static struct tracer *trace_types __read_mostly;
  52. static struct tracer *current_trace __read_mostly;
  53. static int max_tracer_type_len;
  54. static DEFINE_MUTEX(trace_types_lock);
  55. static DECLARE_WAIT_QUEUE_HEAD (trace_wait);
  56. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  57. static int __init set_nr_entries(char *str)
  58. {
  59. if (!str)
  60. return 0;
  61. trace_nr_entries = simple_strtoul(str, &str, 0);
  62. return 1;
  63. }
  64. __setup("trace_entries=", set_nr_entries);
  65. unsigned long nsecs_to_usecs(unsigned long nsecs)
  66. {
  67. return nsecs / 1000;
  68. }
  69. enum trace_type {
  70. __TRACE_FIRST_TYPE = 0,
  71. TRACE_FN,
  72. TRACE_CTX,
  73. TRACE_WAKE,
  74. TRACE_SPECIAL,
  75. __TRACE_LAST_TYPE
  76. };
  77. enum trace_flag_type {
  78. TRACE_FLAG_IRQS_OFF = 0x01,
  79. TRACE_FLAG_NEED_RESCHED = 0x02,
  80. TRACE_FLAG_HARDIRQ = 0x04,
  81. TRACE_FLAG_SOFTIRQ = 0x08,
  82. };
  83. enum trace_iterator_flags {
  84. TRACE_ITER_PRINT_PARENT = 0x01,
  85. TRACE_ITER_SYM_OFFSET = 0x02,
  86. TRACE_ITER_SYM_ADDR = 0x04,
  87. TRACE_ITER_VERBOSE = 0x08,
  88. TRACE_ITER_RAW = 0x10,
  89. TRACE_ITER_HEX = 0x20,
  90. TRACE_ITER_BIN = 0x40,
  91. TRACE_ITER_BLOCK = 0x80,
  92. };
  93. #define TRACE_ITER_SYM_MASK \
  94. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  95. /* These must match the bit postions above */
  96. static const char *trace_options[] = {
  97. "print-parent",
  98. "sym-offset",
  99. "sym-addr",
  100. "verbose",
  101. "raw",
  102. "hex",
  103. "bin",
  104. "block",
  105. NULL
  106. };
  107. static unsigned trace_flags;
  108. static DEFINE_SPINLOCK(ftrace_max_lock);
  109. /*
  110. * Copy the new maximum trace into the separate maximum-trace
  111. * structure. (this way the maximum trace is permanently saved,
  112. * for later retrieval via /debugfs/tracing/latency_trace)
  113. */
  114. static void
  115. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  116. {
  117. struct trace_array_cpu *data = tr->data[cpu];
  118. max_tr.cpu = cpu;
  119. max_tr.time_start = data->preempt_timestamp;
  120. data = max_tr.data[cpu];
  121. data->saved_latency = tracing_max_latency;
  122. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  123. data->pid = tsk->pid;
  124. data->uid = tsk->uid;
  125. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  126. data->policy = tsk->policy;
  127. data->rt_priority = tsk->rt_priority;
  128. /* record this tasks comm */
  129. tracing_record_cmdline(current);
  130. }
  131. void check_pages(struct trace_array_cpu *data)
  132. {
  133. struct page *page, *tmp;
  134. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  135. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  136. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  137. BUG_ON(page->lru.next->prev != &page->lru);
  138. BUG_ON(page->lru.prev->next != &page->lru);
  139. }
  140. }
  141. void *head_page(struct trace_array_cpu *data)
  142. {
  143. struct page *page;
  144. check_pages(data);
  145. if (list_empty(&data->trace_pages))
  146. return NULL;
  147. page = list_entry(data->trace_pages.next, struct page, lru);
  148. BUG_ON(&page->lru == &data->trace_pages);
  149. return page_address(page);
  150. }
  151. static int
  152. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  153. {
  154. int len = (PAGE_SIZE - 1) - s->len;
  155. va_list ap;
  156. int ret;
  157. if (!len)
  158. return 0;
  159. va_start(ap, fmt);
  160. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  161. va_end(ap);
  162. /* If we can't write it all, don't bother writing anything */
  163. if (ret > len)
  164. return 0;
  165. s->len += ret;
  166. return len;
  167. }
  168. static int
  169. trace_seq_puts(struct trace_seq *s, const char *str)
  170. {
  171. int len = strlen(str);
  172. if (len > ((PAGE_SIZE - 1) - s->len))
  173. return 0;
  174. memcpy(s->buffer + s->len, str, len);
  175. s->len += len;
  176. return len;
  177. }
  178. static int
  179. trace_seq_putc(struct trace_seq *s, unsigned char c)
  180. {
  181. if (s->len >= (PAGE_SIZE - 1))
  182. return 0;
  183. s->buffer[s->len++] = c;
  184. return 1;
  185. }
  186. static int
  187. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  188. {
  189. if (len > ((PAGE_SIZE - 1) - s->len))
  190. return 0;
  191. memcpy(s->buffer + s->len, mem, len);
  192. s->len += len;
  193. return len;
  194. }
  195. #define HEX_CHARS 17
  196. static int
  197. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  198. {
  199. unsigned char hex[HEX_CHARS];
  200. unsigned char *data;
  201. unsigned char byte;
  202. int i, j;
  203. BUG_ON(len >= HEX_CHARS);
  204. data = mem;
  205. #ifdef __BIG_ENDIAN
  206. for (i = 0, j = 0; i < len; i++) {
  207. #else
  208. for (i = len-1, j = 0; i >= 0; i--) {
  209. #endif
  210. byte = data[i];
  211. hex[j] = byte & 0x0f;
  212. if (hex[j] >= 10)
  213. hex[j] += 'a' - 10;
  214. else
  215. hex[j] += '0';
  216. j++;
  217. hex[j] = byte >> 4;
  218. if (hex[j] >= 10)
  219. hex[j] += 'a' - 10;
  220. else
  221. hex[j] += '0';
  222. j++;
  223. }
  224. hex[j] = ' ';
  225. j++;
  226. return trace_seq_putmem(s, hex, j);
  227. }
  228. static void
  229. trace_seq_reset(struct trace_seq *s)
  230. {
  231. s->len = 0;
  232. }
  233. static void
  234. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  235. {
  236. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  237. s->buffer[len] = 0;
  238. seq_puts(m, s->buffer);
  239. trace_seq_reset(s);
  240. }
  241. static void
  242. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  243. {
  244. struct list_head flip_pages;
  245. INIT_LIST_HEAD(&flip_pages);
  246. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  247. sizeof(struct trace_array_cpu) -
  248. offsetof(struct trace_array_cpu, trace_head_idx));
  249. check_pages(tr1);
  250. check_pages(tr2);
  251. list_splice_init(&tr1->trace_pages, &flip_pages);
  252. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  253. list_splice_init(&flip_pages, &tr2->trace_pages);
  254. BUG_ON(!list_empty(&flip_pages));
  255. check_pages(tr1);
  256. check_pages(tr2);
  257. }
  258. void
  259. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  260. {
  261. struct trace_array_cpu *data;
  262. int i;
  263. WARN_ON_ONCE(!irqs_disabled());
  264. spin_lock(&ftrace_max_lock);
  265. /* clear out all the previous traces */
  266. for_each_possible_cpu(i) {
  267. data = tr->data[i];
  268. flip_trace(max_tr.data[i], data);
  269. tracing_reset(data);
  270. }
  271. __update_max_tr(tr, tsk, cpu);
  272. spin_unlock(&ftrace_max_lock);
  273. }
  274. /**
  275. * update_max_tr_single - only copy one trace over, and reset the rest
  276. * @tr - tracer
  277. * @tsk - task with the latency
  278. * @cpu - the cpu of the buffer to copy.
  279. */
  280. void
  281. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  282. {
  283. struct trace_array_cpu *data = tr->data[cpu];
  284. int i;
  285. WARN_ON_ONCE(!irqs_disabled());
  286. spin_lock(&ftrace_max_lock);
  287. for_each_possible_cpu(i)
  288. tracing_reset(max_tr.data[i]);
  289. flip_trace(max_tr.data[cpu], data);
  290. tracing_reset(data);
  291. __update_max_tr(tr, tsk, cpu);
  292. spin_unlock(&ftrace_max_lock);
  293. }
  294. int register_tracer(struct tracer *type)
  295. {
  296. struct tracer *t;
  297. int len;
  298. int ret = 0;
  299. if (!type->name) {
  300. pr_info("Tracer must have a name\n");
  301. return -1;
  302. }
  303. mutex_lock(&trace_types_lock);
  304. for (t = trace_types; t; t = t->next) {
  305. if (strcmp(type->name, t->name) == 0) {
  306. /* already found */
  307. pr_info("Trace %s already registered\n",
  308. type->name);
  309. ret = -1;
  310. goto out;
  311. }
  312. }
  313. #ifdef CONFIG_FTRACE_STARTUP_TEST
  314. if (type->selftest) {
  315. struct tracer *saved_tracer = current_trace;
  316. struct trace_array_cpu *data;
  317. struct trace_array *tr = &global_trace;
  318. int saved_ctrl = tr->ctrl;
  319. int i;
  320. /*
  321. * Run a selftest on this tracer.
  322. * Here we reset the trace buffer, and set the current
  323. * tracer to be this tracer. The tracer can then run some
  324. * internal tracing to verify that everything is in order.
  325. * If we fail, we do not register this tracer.
  326. */
  327. for_each_possible_cpu(i) {
  328. data = tr->data[i];
  329. if (!head_page(data))
  330. continue;
  331. tracing_reset(data);
  332. }
  333. current_trace = type;
  334. tr->ctrl = 0;
  335. /* the test is responsible for initializing and enabling */
  336. pr_info("Testing tracer %s: ", type->name);
  337. ret = type->selftest(type, tr);
  338. /* the test is responsible for resetting too */
  339. current_trace = saved_tracer;
  340. tr->ctrl = saved_ctrl;
  341. if (ret) {
  342. printk(KERN_CONT "FAILED!\n");
  343. goto out;
  344. }
  345. /* Only reset on passing, to avoid touching corrupted buffers */
  346. for_each_possible_cpu(i) {
  347. data = tr->data[i];
  348. if (!head_page(data))
  349. continue;
  350. tracing_reset(data);
  351. }
  352. printk(KERN_CONT "PASSED\n");
  353. }
  354. #endif
  355. type->next = trace_types;
  356. trace_types = type;
  357. len = strlen(type->name);
  358. if (len > max_tracer_type_len)
  359. max_tracer_type_len = len;
  360. out:
  361. mutex_unlock(&trace_types_lock);
  362. return ret;
  363. }
  364. void unregister_tracer(struct tracer *type)
  365. {
  366. struct tracer **t;
  367. int len;
  368. mutex_lock(&trace_types_lock);
  369. for (t = &trace_types; *t; t = &(*t)->next) {
  370. if (*t == type)
  371. goto found;
  372. }
  373. pr_info("Trace %s not registered\n", type->name);
  374. goto out;
  375. found:
  376. *t = (*t)->next;
  377. if (strlen(type->name) != max_tracer_type_len)
  378. goto out;
  379. max_tracer_type_len = 0;
  380. for (t = &trace_types; *t; t = &(*t)->next) {
  381. len = strlen((*t)->name);
  382. if (len > max_tracer_type_len)
  383. max_tracer_type_len = len;
  384. }
  385. out:
  386. mutex_unlock(&trace_types_lock);
  387. }
  388. void tracing_reset(struct trace_array_cpu *data)
  389. {
  390. data->trace_idx = 0;
  391. data->trace_head = data->trace_tail = head_page(data);
  392. data->trace_head_idx = 0;
  393. data->trace_tail_idx = 0;
  394. }
  395. #define SAVED_CMDLINES 128
  396. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  397. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  398. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  399. static int cmdline_idx;
  400. static DEFINE_SPINLOCK(trace_cmdline_lock);
  401. atomic_t trace_record_cmdline_disabled;
  402. static void trace_init_cmdlines(void)
  403. {
  404. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  405. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  406. cmdline_idx = 0;
  407. }
  408. void trace_stop_cmdline_recording(void);
  409. static void trace_save_cmdline(struct task_struct *tsk)
  410. {
  411. unsigned map;
  412. unsigned idx;
  413. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  414. return;
  415. /*
  416. * It's not the end of the world if we don't get
  417. * the lock, but we also don't want to spin
  418. * nor do we want to disable interrupts,
  419. * so if we miss here, then better luck next time.
  420. */
  421. if (!spin_trylock(&trace_cmdline_lock))
  422. return;
  423. idx = map_pid_to_cmdline[tsk->pid];
  424. if (idx >= SAVED_CMDLINES) {
  425. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  426. map = map_cmdline_to_pid[idx];
  427. if (map <= PID_MAX_DEFAULT)
  428. map_pid_to_cmdline[map] = (unsigned)-1;
  429. map_pid_to_cmdline[tsk->pid] = idx;
  430. cmdline_idx = idx;
  431. }
  432. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  433. spin_unlock(&trace_cmdline_lock);
  434. }
  435. static char *trace_find_cmdline(int pid)
  436. {
  437. char *cmdline = "<...>";
  438. unsigned map;
  439. if (!pid)
  440. return "<idle>";
  441. if (pid > PID_MAX_DEFAULT)
  442. goto out;
  443. map = map_pid_to_cmdline[pid];
  444. if (map >= SAVED_CMDLINES)
  445. goto out;
  446. cmdline = saved_cmdlines[map];
  447. out:
  448. return cmdline;
  449. }
  450. void tracing_record_cmdline(struct task_struct *tsk)
  451. {
  452. if (atomic_read(&trace_record_cmdline_disabled))
  453. return;
  454. trace_save_cmdline(tsk);
  455. }
  456. static inline struct list_head *
  457. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  458. {
  459. /*
  460. * Roundrobin - but skip the head (which is not a real page):
  461. */
  462. next = next->next;
  463. if (unlikely(next == &data->trace_pages))
  464. next = next->next;
  465. BUG_ON(next == &data->trace_pages);
  466. return next;
  467. }
  468. static inline void *
  469. trace_next_page(struct trace_array_cpu *data, void *addr)
  470. {
  471. struct list_head *next;
  472. struct page *page;
  473. page = virt_to_page(addr);
  474. next = trace_next_list(data, &page->lru);
  475. page = list_entry(next, struct page, lru);
  476. return page_address(page);
  477. }
  478. static inline struct trace_entry *
  479. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  480. {
  481. unsigned long idx, idx_next;
  482. struct trace_entry *entry;
  483. data->trace_idx++;
  484. idx = data->trace_head_idx;
  485. idx_next = idx + 1;
  486. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  487. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  488. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  489. data->trace_head = trace_next_page(data, data->trace_head);
  490. idx_next = 0;
  491. }
  492. if (data->trace_head == data->trace_tail &&
  493. idx_next == data->trace_tail_idx) {
  494. /* overrun */
  495. data->trace_tail_idx++;
  496. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  497. data->trace_tail =
  498. trace_next_page(data, data->trace_tail);
  499. data->trace_tail_idx = 0;
  500. }
  501. }
  502. data->trace_head_idx = idx_next;
  503. return entry;
  504. }
  505. static inline void
  506. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  507. {
  508. struct task_struct *tsk = current;
  509. unsigned long pc;
  510. pc = preempt_count();
  511. entry->preempt_count = pc & 0xff;
  512. entry->pid = tsk->pid;
  513. entry->t = ftrace_now(raw_smp_processor_id());
  514. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  515. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  516. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  517. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  518. }
  519. void
  520. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  521. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  522. {
  523. struct trace_entry *entry;
  524. unsigned long irq_flags;
  525. spin_lock_irqsave(&data->lock, irq_flags);
  526. entry = tracing_get_trace_entry(tr, data);
  527. tracing_generic_entry_update(entry, flags);
  528. entry->type = TRACE_FN;
  529. entry->fn.ip = ip;
  530. entry->fn.parent_ip = parent_ip;
  531. spin_unlock_irqrestore(&data->lock, irq_flags);
  532. if (!(trace_flags & TRACE_ITER_BLOCK))
  533. wake_up (&trace_wait);
  534. }
  535. void
  536. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  537. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  538. {
  539. if (likely(!atomic_read(&data->disabled)))
  540. trace_function(tr, data, ip, parent_ip, flags);
  541. }
  542. void
  543. trace_special(struct trace_array *tr, struct trace_array_cpu *data,
  544. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  545. {
  546. struct trace_entry *entry;
  547. unsigned long irq_flags;
  548. spin_lock_irqsave(&data->lock, irq_flags);
  549. entry = tracing_get_trace_entry(tr, data);
  550. tracing_generic_entry_update(entry, 0);
  551. entry->type = TRACE_SPECIAL;
  552. entry->special.arg1 = arg1;
  553. entry->special.arg2 = arg2;
  554. entry->special.arg3 = arg3;
  555. spin_unlock_irqrestore(&data->lock, irq_flags);
  556. if (!(trace_flags & TRACE_ITER_BLOCK))
  557. wake_up (&trace_wait);
  558. }
  559. void
  560. tracing_sched_switch_trace(struct trace_array *tr,
  561. struct trace_array_cpu *data,
  562. struct task_struct *prev, struct task_struct *next,
  563. unsigned long flags)
  564. {
  565. struct trace_entry *entry;
  566. unsigned long irq_flags;
  567. spin_lock_irqsave(&data->lock, irq_flags);
  568. entry = tracing_get_trace_entry(tr, data);
  569. tracing_generic_entry_update(entry, flags);
  570. entry->type = TRACE_CTX;
  571. entry->ctx.prev_pid = prev->pid;
  572. entry->ctx.prev_prio = prev->prio;
  573. entry->ctx.prev_state = prev->state;
  574. entry->ctx.next_pid = next->pid;
  575. entry->ctx.next_prio = next->prio;
  576. spin_unlock_irqrestore(&data->lock, irq_flags);
  577. if (!(trace_flags & TRACE_ITER_BLOCK))
  578. wake_up (&trace_wait);
  579. }
  580. void
  581. tracing_sched_wakeup_trace(struct trace_array *tr,
  582. struct trace_array_cpu *data,
  583. struct task_struct *wakee, struct task_struct *curr,
  584. unsigned long flags)
  585. {
  586. struct trace_entry *entry;
  587. unsigned long irq_flags;
  588. spin_lock_irqsave(&data->lock, irq_flags);
  589. entry = tracing_get_trace_entry(tr, data);
  590. tracing_generic_entry_update(entry, flags);
  591. entry->type = TRACE_WAKE;
  592. entry->ctx.prev_pid = curr->pid;
  593. entry->ctx.prev_prio = curr->prio;
  594. entry->ctx.prev_state = curr->state;
  595. entry->ctx.next_pid = wakee->pid;
  596. entry->ctx.next_prio = wakee->prio;
  597. spin_unlock_irqrestore(&data->lock, irq_flags);
  598. if (!(trace_flags & TRACE_ITER_BLOCK))
  599. wake_up(&trace_wait);
  600. }
  601. #ifdef CONFIG_FTRACE
  602. static void
  603. function_trace_call(unsigned long ip, unsigned long parent_ip)
  604. {
  605. struct trace_array *tr = &global_trace;
  606. struct trace_array_cpu *data;
  607. unsigned long flags;
  608. long disabled;
  609. int cpu;
  610. if (unlikely(!tracer_enabled))
  611. return;
  612. local_irq_save(flags);
  613. cpu = raw_smp_processor_id();
  614. data = tr->data[cpu];
  615. disabled = atomic_inc_return(&data->disabled);
  616. if (likely(disabled == 1))
  617. trace_function(tr, data, ip, parent_ip, flags);
  618. atomic_dec(&data->disabled);
  619. local_irq_restore(flags);
  620. }
  621. static struct ftrace_ops trace_ops __read_mostly =
  622. {
  623. .func = function_trace_call,
  624. };
  625. void tracing_start_function_trace(void)
  626. {
  627. register_ftrace_function(&trace_ops);
  628. }
  629. void tracing_stop_function_trace(void)
  630. {
  631. unregister_ftrace_function(&trace_ops);
  632. }
  633. #endif
  634. enum trace_file_type {
  635. TRACE_FILE_LAT_FMT = 1,
  636. };
  637. static struct trace_entry *
  638. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  639. struct trace_iterator *iter, int cpu)
  640. {
  641. struct page *page;
  642. struct trace_entry *array;
  643. if (iter->next_idx[cpu] >= tr->entries ||
  644. iter->next_idx[cpu] >= data->trace_idx ||
  645. (data->trace_head == data->trace_tail &&
  646. data->trace_head_idx == data->trace_tail_idx))
  647. return NULL;
  648. if (!iter->next_page[cpu]) {
  649. /* Initialize the iterator for this cpu trace buffer */
  650. WARN_ON(!data->trace_tail);
  651. page = virt_to_page(data->trace_tail);
  652. iter->next_page[cpu] = &page->lru;
  653. iter->next_page_idx[cpu] = data->trace_tail_idx;
  654. }
  655. page = list_entry(iter->next_page[cpu], struct page, lru);
  656. BUG_ON(&data->trace_pages == &page->lru);
  657. array = page_address(page);
  658. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  659. return &array[iter->next_page_idx[cpu]];
  660. }
  661. static struct trace_entry *
  662. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  663. {
  664. struct trace_array *tr = iter->tr;
  665. struct trace_entry *ent, *next = NULL;
  666. int next_cpu = -1;
  667. int cpu;
  668. for_each_possible_cpu(cpu) {
  669. if (!head_page(tr->data[cpu]))
  670. continue;
  671. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  672. /*
  673. * Pick the entry with the smallest timestamp:
  674. */
  675. if (ent && (!next || ent->t < next->t)) {
  676. next = ent;
  677. next_cpu = cpu;
  678. }
  679. }
  680. if (ent_cpu)
  681. *ent_cpu = next_cpu;
  682. return next;
  683. }
  684. static void trace_iterator_increment(struct trace_iterator *iter)
  685. {
  686. iter->idx++;
  687. iter->next_idx[iter->cpu]++;
  688. iter->next_page_idx[iter->cpu]++;
  689. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  690. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  691. iter->next_page_idx[iter->cpu] = 0;
  692. iter->next_page[iter->cpu] =
  693. trace_next_list(data, iter->next_page[iter->cpu]);
  694. }
  695. }
  696. static void trace_consume(struct trace_iterator *iter)
  697. {
  698. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  699. data->trace_tail_idx++;
  700. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  701. data->trace_tail = trace_next_page(data, data->trace_tail);
  702. data->trace_tail_idx = 0;
  703. }
  704. /* Check if we empty it, then reset the index */
  705. if (data->trace_head == data->trace_tail &&
  706. data->trace_head_idx == data->trace_tail_idx)
  707. data->trace_idx = 0;
  708. }
  709. static void *find_next_entry_inc(struct trace_iterator *iter)
  710. {
  711. struct trace_entry *next;
  712. int next_cpu = -1;
  713. next = find_next_entry(iter, &next_cpu);
  714. iter->prev_ent = iter->ent;
  715. iter->prev_cpu = iter->cpu;
  716. iter->ent = next;
  717. iter->cpu = next_cpu;
  718. if (next)
  719. trace_iterator_increment(iter);
  720. return next ? iter : NULL;
  721. }
  722. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  723. {
  724. struct trace_iterator *iter = m->private;
  725. void *last_ent = iter->ent;
  726. int i = (int)*pos;
  727. void *ent;
  728. (*pos)++;
  729. /* can't go backwards */
  730. if (iter->idx > i)
  731. return NULL;
  732. if (iter->idx < 0)
  733. ent = find_next_entry_inc(iter);
  734. else
  735. ent = iter;
  736. while (ent && iter->idx < i)
  737. ent = find_next_entry_inc(iter);
  738. iter->pos = *pos;
  739. if (last_ent && !ent)
  740. seq_puts(m, "\n\nvim:ft=help\n");
  741. return ent;
  742. }
  743. static void *s_start(struct seq_file *m, loff_t *pos)
  744. {
  745. struct trace_iterator *iter = m->private;
  746. void *p = NULL;
  747. loff_t l = 0;
  748. int i;
  749. mutex_lock(&trace_types_lock);
  750. if (!current_trace || current_trace != iter->trace)
  751. return NULL;
  752. atomic_inc(&trace_record_cmdline_disabled);
  753. /* let the tracer grab locks here if needed */
  754. if (current_trace->start)
  755. current_trace->start(iter);
  756. if (*pos != iter->pos) {
  757. iter->ent = NULL;
  758. iter->cpu = 0;
  759. iter->idx = -1;
  760. iter->prev_ent = NULL;
  761. iter->prev_cpu = -1;
  762. for_each_possible_cpu(i) {
  763. iter->next_idx[i] = 0;
  764. iter->next_page[i] = NULL;
  765. }
  766. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  767. ;
  768. } else {
  769. l = *pos - 1;
  770. p = s_next(m, p, &l);
  771. }
  772. return p;
  773. }
  774. static void s_stop(struct seq_file *m, void *p)
  775. {
  776. struct trace_iterator *iter = m->private;
  777. atomic_dec(&trace_record_cmdline_disabled);
  778. /* let the tracer release locks here if needed */
  779. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  780. iter->trace->stop(iter);
  781. mutex_unlock(&trace_types_lock);
  782. }
  783. static int
  784. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  785. {
  786. #ifdef CONFIG_KALLSYMS
  787. char str[KSYM_SYMBOL_LEN];
  788. kallsyms_lookup(address, NULL, NULL, NULL, str);
  789. return trace_seq_printf(s, fmt, str);
  790. #endif
  791. return 1;
  792. }
  793. static int
  794. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  795. unsigned long address)
  796. {
  797. #ifdef CONFIG_KALLSYMS
  798. char str[KSYM_SYMBOL_LEN];
  799. sprint_symbol(str, address);
  800. return trace_seq_printf(s, fmt, str);
  801. #endif
  802. return 1;
  803. }
  804. #ifndef CONFIG_64BIT
  805. # define IP_FMT "%08lx"
  806. #else
  807. # define IP_FMT "%016lx"
  808. #endif
  809. static int
  810. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  811. {
  812. int ret;
  813. if (!ip)
  814. return trace_seq_printf(s, "0");
  815. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  816. ret = seq_print_sym_offset(s, "%s", ip);
  817. else
  818. ret = seq_print_sym_short(s, "%s", ip);
  819. if (!ret)
  820. return 0;
  821. if (sym_flags & TRACE_ITER_SYM_ADDR)
  822. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  823. return ret;
  824. }
  825. static void print_lat_help_header(struct seq_file *m)
  826. {
  827. seq_puts(m, "# _------=> CPU# \n");
  828. seq_puts(m, "# / _-----=> irqs-off \n");
  829. seq_puts(m, "# | / _----=> need-resched \n");
  830. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  831. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  832. seq_puts(m, "# |||| / \n");
  833. seq_puts(m, "# ||||| delay \n");
  834. seq_puts(m, "# cmd pid ||||| time | caller \n");
  835. seq_puts(m, "# \\ / ||||| \\ | / \n");
  836. }
  837. static void print_func_help_header(struct seq_file *m)
  838. {
  839. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  840. seq_puts(m, "# | | | | |\n");
  841. }
  842. static void
  843. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  844. {
  845. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  846. struct trace_array *tr = iter->tr;
  847. struct trace_array_cpu *data = tr->data[tr->cpu];
  848. struct tracer *type = current_trace;
  849. unsigned long total = 0;
  850. unsigned long entries = 0;
  851. int cpu;
  852. const char *name = "preemption";
  853. if (type)
  854. name = type->name;
  855. for_each_possible_cpu(cpu) {
  856. if (head_page(tr->data[cpu])) {
  857. total += tr->data[cpu]->trace_idx;
  858. if (tr->data[cpu]->trace_idx > tr->entries)
  859. entries += tr->entries;
  860. else
  861. entries += tr->data[cpu]->trace_idx;
  862. }
  863. }
  864. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  865. name, UTS_RELEASE);
  866. seq_puts(m, "-----------------------------------"
  867. "---------------------------------\n");
  868. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  869. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  870. nsecs_to_usecs(data->saved_latency),
  871. entries,
  872. total,
  873. tr->cpu,
  874. #if defined(CONFIG_PREEMPT_NONE)
  875. "server",
  876. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  877. "desktop",
  878. #elif defined(CONFIG_PREEMPT_DESKTOP)
  879. "preempt",
  880. #else
  881. "unknown",
  882. #endif
  883. /* These are reserved for later use */
  884. 0, 0, 0, 0);
  885. #ifdef CONFIG_SMP
  886. seq_printf(m, " #P:%d)\n", num_online_cpus());
  887. #else
  888. seq_puts(m, ")\n");
  889. #endif
  890. seq_puts(m, " -----------------\n");
  891. seq_printf(m, " | task: %.16s-%d "
  892. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  893. data->comm, data->pid, data->uid, data->nice,
  894. data->policy, data->rt_priority);
  895. seq_puts(m, " -----------------\n");
  896. if (data->critical_start) {
  897. seq_puts(m, " => started at: ");
  898. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  899. trace_print_seq(m, &iter->seq);
  900. seq_puts(m, "\n => ended at: ");
  901. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  902. trace_print_seq(m, &iter->seq);
  903. seq_puts(m, "\n");
  904. }
  905. seq_puts(m, "\n");
  906. }
  907. static void
  908. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  909. {
  910. int hardirq, softirq;
  911. char *comm;
  912. comm = trace_find_cmdline(entry->pid);
  913. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  914. trace_seq_printf(s, "%d", cpu);
  915. trace_seq_printf(s, "%c%c",
  916. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  917. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  918. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  919. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  920. if (hardirq && softirq)
  921. trace_seq_putc(s, 'H');
  922. else {
  923. if (hardirq)
  924. trace_seq_putc(s, 'h');
  925. else {
  926. if (softirq)
  927. trace_seq_putc(s, 's');
  928. else
  929. trace_seq_putc(s, '.');
  930. }
  931. }
  932. if (entry->preempt_count)
  933. trace_seq_printf(s, "%x", entry->preempt_count);
  934. else
  935. trace_seq_puts(s, ".");
  936. }
  937. unsigned long preempt_mark_thresh = 100;
  938. static void
  939. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  940. unsigned long rel_usecs)
  941. {
  942. trace_seq_printf(s, " %4lldus", abs_usecs);
  943. if (rel_usecs > preempt_mark_thresh)
  944. trace_seq_puts(s, "!: ");
  945. else if (rel_usecs > 1)
  946. trace_seq_puts(s, "+: ");
  947. else
  948. trace_seq_puts(s, " : ");
  949. }
  950. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  951. static int
  952. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  953. {
  954. struct trace_seq *s = &iter->seq;
  955. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  956. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  957. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  958. struct trace_entry *entry = iter->ent;
  959. unsigned long abs_usecs;
  960. unsigned long rel_usecs;
  961. char *comm;
  962. int S;
  963. if (!next_entry)
  964. next_entry = entry;
  965. rel_usecs = ns2usecs(next_entry->t - entry->t);
  966. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  967. if (verbose) {
  968. comm = trace_find_cmdline(entry->pid);
  969. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  970. " %ld.%03ldms (+%ld.%03ldms): ",
  971. comm,
  972. entry->pid, cpu, entry->flags,
  973. entry->preempt_count, trace_idx,
  974. ns2usecs(entry->t),
  975. abs_usecs/1000,
  976. abs_usecs % 1000, rel_usecs/1000,
  977. rel_usecs % 1000);
  978. } else {
  979. lat_print_generic(s, entry, cpu);
  980. lat_print_timestamp(s, abs_usecs, rel_usecs);
  981. }
  982. switch (entry->type) {
  983. case TRACE_FN:
  984. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  985. trace_seq_puts(s, " (");
  986. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  987. trace_seq_puts(s, ")\n");
  988. break;
  989. case TRACE_CTX:
  990. case TRACE_WAKE:
  991. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  992. state_to_char[entry->ctx.prev_state] : 'X';
  993. comm = trace_find_cmdline(entry->ctx.next_pid);
  994. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
  995. entry->ctx.prev_pid,
  996. entry->ctx.prev_prio,
  997. S, entry->type == TRACE_CTX ? "==>" : " +",
  998. entry->ctx.next_pid,
  999. entry->ctx.next_prio,
  1000. comm);
  1001. break;
  1002. case TRACE_SPECIAL:
  1003. trace_seq_printf(s, " %lx %lx %lx\n",
  1004. entry->special.arg1,
  1005. entry->special.arg2,
  1006. entry->special.arg3);
  1007. break;
  1008. default:
  1009. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1010. }
  1011. return 1;
  1012. }
  1013. static int print_trace_fmt(struct trace_iterator *iter)
  1014. {
  1015. struct trace_seq *s = &iter->seq;
  1016. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1017. struct trace_entry *entry;
  1018. unsigned long usec_rem;
  1019. unsigned long long t;
  1020. unsigned long secs;
  1021. char *comm;
  1022. int S;
  1023. int ret;
  1024. entry = iter->ent;
  1025. comm = trace_find_cmdline(iter->ent->pid);
  1026. t = ns2usecs(entry->t);
  1027. usec_rem = do_div(t, 1000000ULL);
  1028. secs = (unsigned long)t;
  1029. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1030. if (!ret)
  1031. return 0;
  1032. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1033. if (!ret)
  1034. return 0;
  1035. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1036. if (!ret)
  1037. return 0;
  1038. switch (entry->type) {
  1039. case TRACE_FN:
  1040. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1041. if (!ret)
  1042. return 0;
  1043. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1044. entry->fn.parent_ip) {
  1045. ret = trace_seq_printf(s, " <-");
  1046. if (!ret)
  1047. return 0;
  1048. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1049. sym_flags);
  1050. if (!ret)
  1051. return 0;
  1052. }
  1053. ret = trace_seq_printf(s, "\n");
  1054. if (!ret)
  1055. return 0;
  1056. break;
  1057. case TRACE_CTX:
  1058. case TRACE_WAKE:
  1059. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1060. state_to_char[entry->ctx.prev_state] : 'X';
  1061. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
  1062. entry->ctx.prev_pid,
  1063. entry->ctx.prev_prio,
  1064. S,
  1065. entry->type == TRACE_CTX ? "==>" : " +",
  1066. entry->ctx.next_pid,
  1067. entry->ctx.next_prio);
  1068. if (!ret)
  1069. return 0;
  1070. break;
  1071. case TRACE_SPECIAL:
  1072. ret = trace_seq_printf(s, " %lx %lx %lx\n",
  1073. entry->special.arg1,
  1074. entry->special.arg2,
  1075. entry->special.arg3);
  1076. if (!ret)
  1077. return 0;
  1078. break;
  1079. }
  1080. return 1;
  1081. }
  1082. static int print_raw_fmt(struct trace_iterator *iter)
  1083. {
  1084. struct trace_seq *s = &iter->seq;
  1085. struct trace_entry *entry;
  1086. int ret;
  1087. int S;
  1088. entry = iter->ent;
  1089. ret = trace_seq_printf(s, "%d %d %llu ",
  1090. entry->pid, iter->cpu, entry->t);
  1091. if (!ret)
  1092. return 0;
  1093. switch (entry->type) {
  1094. case TRACE_FN:
  1095. ret = trace_seq_printf(s, "%x %x\n",
  1096. entry->fn.ip, entry->fn.parent_ip);
  1097. if (!ret)
  1098. return 0;
  1099. break;
  1100. case TRACE_CTX:
  1101. case TRACE_WAKE:
  1102. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1103. state_to_char[entry->ctx.prev_state] : 'X';
  1104. if (entry->type == TRACE_WAKE)
  1105. S = '+';
  1106. ret = trace_seq_printf(s, "%d %d %c %d %d\n",
  1107. entry->ctx.prev_pid,
  1108. entry->ctx.prev_prio,
  1109. S,
  1110. entry->ctx.next_pid,
  1111. entry->ctx.next_prio);
  1112. if (!ret)
  1113. return 0;
  1114. break;
  1115. case TRACE_SPECIAL:
  1116. ret = trace_seq_printf(s, " %lx %lx %lx\n",
  1117. entry->special.arg1,
  1118. entry->special.arg2,
  1119. entry->special.arg3);
  1120. if (!ret)
  1121. return 0;
  1122. break;
  1123. }
  1124. return 1;
  1125. }
  1126. #define SEQ_PUT_FIELD_RET(s, x) \
  1127. do { \
  1128. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1129. return 0; \
  1130. } while (0)
  1131. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1132. do { \
  1133. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1134. return 0; \
  1135. } while (0)
  1136. static int print_hex_fmt(struct trace_iterator *iter)
  1137. {
  1138. struct trace_seq *s = &iter->seq;
  1139. unsigned char newline = '\n';
  1140. struct trace_entry *entry;
  1141. int S;
  1142. entry = iter->ent;
  1143. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1144. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1145. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1146. switch (entry->type) {
  1147. case TRACE_FN:
  1148. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1149. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1150. break;
  1151. case TRACE_CTX:
  1152. case TRACE_WAKE:
  1153. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1154. state_to_char[entry->ctx.prev_state] : 'X';
  1155. if (entry->type == TRACE_WAKE)
  1156. S = '+';
  1157. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1158. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1159. SEQ_PUT_HEX_FIELD_RET(s, S);
  1160. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1161. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1162. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1163. break;
  1164. case TRACE_SPECIAL:
  1165. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1166. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1167. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1168. break;
  1169. }
  1170. SEQ_PUT_FIELD_RET(s, newline);
  1171. return 1;
  1172. }
  1173. static int print_bin_fmt(struct trace_iterator *iter)
  1174. {
  1175. struct trace_seq *s = &iter->seq;
  1176. struct trace_entry *entry;
  1177. entry = iter->ent;
  1178. SEQ_PUT_FIELD_RET(s, entry->pid);
  1179. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1180. SEQ_PUT_FIELD_RET(s, entry->t);
  1181. switch (entry->type) {
  1182. case TRACE_FN:
  1183. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1184. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1185. break;
  1186. case TRACE_CTX:
  1187. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1188. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1189. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1190. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1191. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1192. break;
  1193. case TRACE_SPECIAL:
  1194. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1195. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1196. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1197. break;
  1198. }
  1199. return 1;
  1200. }
  1201. static int trace_empty(struct trace_iterator *iter)
  1202. {
  1203. struct trace_array_cpu *data;
  1204. int cpu;
  1205. for_each_possible_cpu(cpu) {
  1206. data = iter->tr->data[cpu];
  1207. if (head_page(data) && data->trace_idx &&
  1208. (data->trace_tail != data->trace_head ||
  1209. data->trace_tail_idx != data->trace_head_idx))
  1210. return 0;
  1211. }
  1212. return 1;
  1213. }
  1214. static int print_trace_line(struct trace_iterator *iter)
  1215. {
  1216. if (trace_flags & TRACE_ITER_BIN)
  1217. return print_bin_fmt(iter);
  1218. if (trace_flags & TRACE_ITER_HEX)
  1219. return print_hex_fmt(iter);
  1220. if (trace_flags & TRACE_ITER_RAW)
  1221. return print_raw_fmt(iter);
  1222. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1223. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1224. return print_trace_fmt(iter);
  1225. }
  1226. static int s_show(struct seq_file *m, void *v)
  1227. {
  1228. struct trace_iterator *iter = v;
  1229. if (iter->ent == NULL) {
  1230. if (iter->tr) {
  1231. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1232. seq_puts(m, "#\n");
  1233. }
  1234. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1235. /* print nothing if the buffers are empty */
  1236. if (trace_empty(iter))
  1237. return 0;
  1238. print_trace_header(m, iter);
  1239. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1240. print_lat_help_header(m);
  1241. } else {
  1242. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1243. print_func_help_header(m);
  1244. }
  1245. } else {
  1246. print_trace_line(iter);
  1247. trace_print_seq(m, &iter->seq);
  1248. }
  1249. return 0;
  1250. }
  1251. static struct seq_operations tracer_seq_ops = {
  1252. .start = s_start,
  1253. .next = s_next,
  1254. .stop = s_stop,
  1255. .show = s_show,
  1256. };
  1257. static struct trace_iterator *
  1258. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1259. {
  1260. struct trace_iterator *iter;
  1261. if (tracing_disabled) {
  1262. *ret = -ENODEV;
  1263. return NULL;
  1264. }
  1265. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1266. if (!iter) {
  1267. *ret = -ENOMEM;
  1268. goto out;
  1269. }
  1270. mutex_lock(&trace_types_lock);
  1271. if (current_trace && current_trace->print_max)
  1272. iter->tr = &max_tr;
  1273. else
  1274. iter->tr = inode->i_private;
  1275. iter->trace = current_trace;
  1276. iter->pos = -1;
  1277. /* TODO stop tracer */
  1278. *ret = seq_open(file, &tracer_seq_ops);
  1279. if (!*ret) {
  1280. struct seq_file *m = file->private_data;
  1281. m->private = iter;
  1282. /* stop the trace while dumping */
  1283. if (iter->tr->ctrl)
  1284. tracer_enabled = 0;
  1285. if (iter->trace && iter->trace->open)
  1286. iter->trace->open(iter);
  1287. } else {
  1288. kfree(iter);
  1289. iter = NULL;
  1290. }
  1291. mutex_unlock(&trace_types_lock);
  1292. out:
  1293. return iter;
  1294. }
  1295. int tracing_open_generic(struct inode *inode, struct file *filp)
  1296. {
  1297. if (tracing_disabled)
  1298. return -ENODEV;
  1299. filp->private_data = inode->i_private;
  1300. return 0;
  1301. }
  1302. int tracing_release(struct inode *inode, struct file *file)
  1303. {
  1304. struct seq_file *m = (struct seq_file *)file->private_data;
  1305. struct trace_iterator *iter = m->private;
  1306. mutex_lock(&trace_types_lock);
  1307. if (iter->trace && iter->trace->close)
  1308. iter->trace->close(iter);
  1309. /* reenable tracing if it was previously enabled */
  1310. if (iter->tr->ctrl)
  1311. tracer_enabled = 1;
  1312. mutex_unlock(&trace_types_lock);
  1313. seq_release(inode, file);
  1314. kfree(iter);
  1315. return 0;
  1316. }
  1317. static int tracing_open(struct inode *inode, struct file *file)
  1318. {
  1319. int ret;
  1320. __tracing_open(inode, file, &ret);
  1321. return ret;
  1322. }
  1323. static int tracing_lt_open(struct inode *inode, struct file *file)
  1324. {
  1325. struct trace_iterator *iter;
  1326. int ret;
  1327. iter = __tracing_open(inode, file, &ret);
  1328. if (!ret)
  1329. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1330. return ret;
  1331. }
  1332. static void *
  1333. t_next(struct seq_file *m, void *v, loff_t *pos)
  1334. {
  1335. struct tracer *t = m->private;
  1336. (*pos)++;
  1337. if (t)
  1338. t = t->next;
  1339. m->private = t;
  1340. return t;
  1341. }
  1342. static void *t_start(struct seq_file *m, loff_t *pos)
  1343. {
  1344. struct tracer *t = m->private;
  1345. loff_t l = 0;
  1346. mutex_lock(&trace_types_lock);
  1347. for (; t && l < *pos; t = t_next(m, t, &l))
  1348. ;
  1349. return t;
  1350. }
  1351. static void t_stop(struct seq_file *m, void *p)
  1352. {
  1353. mutex_unlock(&trace_types_lock);
  1354. }
  1355. static int t_show(struct seq_file *m, void *v)
  1356. {
  1357. struct tracer *t = v;
  1358. if (!t)
  1359. return 0;
  1360. seq_printf(m, "%s", t->name);
  1361. if (t->next)
  1362. seq_putc(m, ' ');
  1363. else
  1364. seq_putc(m, '\n');
  1365. return 0;
  1366. }
  1367. static struct seq_operations show_traces_seq_ops = {
  1368. .start = t_start,
  1369. .next = t_next,
  1370. .stop = t_stop,
  1371. .show = t_show,
  1372. };
  1373. static int show_traces_open(struct inode *inode, struct file *file)
  1374. {
  1375. int ret;
  1376. if (tracing_disabled)
  1377. return -ENODEV;
  1378. ret = seq_open(file, &show_traces_seq_ops);
  1379. if (!ret) {
  1380. struct seq_file *m = file->private_data;
  1381. m->private = trace_types;
  1382. }
  1383. return ret;
  1384. }
  1385. static struct file_operations tracing_fops = {
  1386. .open = tracing_open,
  1387. .read = seq_read,
  1388. .llseek = seq_lseek,
  1389. .release = tracing_release,
  1390. };
  1391. static struct file_operations tracing_lt_fops = {
  1392. .open = tracing_lt_open,
  1393. .read = seq_read,
  1394. .llseek = seq_lseek,
  1395. .release = tracing_release,
  1396. };
  1397. static struct file_operations show_traces_fops = {
  1398. .open = show_traces_open,
  1399. .read = seq_read,
  1400. .release = seq_release,
  1401. };
  1402. static ssize_t
  1403. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1404. size_t cnt, loff_t *ppos)
  1405. {
  1406. char *buf;
  1407. int r = 0;
  1408. int len = 0;
  1409. int i;
  1410. /* calulate max size */
  1411. for (i = 0; trace_options[i]; i++) {
  1412. len += strlen(trace_options[i]);
  1413. len += 3; /* "no" and space */
  1414. }
  1415. /* +2 for \n and \0 */
  1416. buf = kmalloc(len + 2, GFP_KERNEL);
  1417. if (!buf)
  1418. return -ENOMEM;
  1419. for (i = 0; trace_options[i]; i++) {
  1420. if (trace_flags & (1 << i))
  1421. r += sprintf(buf + r, "%s ", trace_options[i]);
  1422. else
  1423. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1424. }
  1425. r += sprintf(buf + r, "\n");
  1426. WARN_ON(r >= len + 2);
  1427. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1428. buf, r);
  1429. kfree(buf);
  1430. return r;
  1431. }
  1432. static ssize_t
  1433. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1434. size_t cnt, loff_t *ppos)
  1435. {
  1436. char buf[64];
  1437. char *cmp = buf;
  1438. int neg = 0;
  1439. int i;
  1440. if (cnt > 63)
  1441. cnt = 63;
  1442. if (copy_from_user(&buf, ubuf, cnt))
  1443. return -EFAULT;
  1444. buf[cnt] = 0;
  1445. if (strncmp(buf, "no", 2) == 0) {
  1446. neg = 1;
  1447. cmp += 2;
  1448. }
  1449. for (i = 0; trace_options[i]; i++) {
  1450. int len = strlen(trace_options[i]);
  1451. if (strncmp(cmp, trace_options[i], len) == 0) {
  1452. if (neg)
  1453. trace_flags &= ~(1 << i);
  1454. else
  1455. trace_flags |= (1 << i);
  1456. break;
  1457. }
  1458. }
  1459. filp->f_pos += cnt;
  1460. return cnt;
  1461. }
  1462. static struct file_operations tracing_iter_fops = {
  1463. .open = tracing_open_generic,
  1464. .read = tracing_iter_ctrl_read,
  1465. .write = tracing_iter_ctrl_write,
  1466. };
  1467. static const char readme_msg[] =
  1468. "tracing mini-HOWTO:\n\n"
  1469. "# mkdir /debug\n"
  1470. "# mount -t debugfs nodev /debug\n\n"
  1471. "# cat /debug/tracing/available_tracers\n"
  1472. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1473. "# cat /debug/tracing/current_tracer\n"
  1474. "none\n"
  1475. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1476. "# cat /debug/tracing/current_tracer\n"
  1477. "sched_switch\n"
  1478. "# cat /debug/tracing/iter_ctrl\n"
  1479. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1480. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1481. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1482. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1483. "echo 0 > /debug/tracing/tracing_enabled\n"
  1484. ;
  1485. static ssize_t
  1486. tracing_readme_read(struct file *filp, char __user *ubuf,
  1487. size_t cnt, loff_t *ppos)
  1488. {
  1489. return simple_read_from_buffer(ubuf, cnt, ppos,
  1490. readme_msg, strlen(readme_msg));
  1491. }
  1492. static struct file_operations tracing_readme_fops = {
  1493. .open = tracing_open_generic,
  1494. .read = tracing_readme_read,
  1495. };
  1496. static ssize_t
  1497. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1498. size_t cnt, loff_t *ppos)
  1499. {
  1500. struct trace_array *tr = filp->private_data;
  1501. char buf[64];
  1502. int r;
  1503. r = sprintf(buf, "%ld\n", tr->ctrl);
  1504. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1505. }
  1506. static ssize_t
  1507. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1508. size_t cnt, loff_t *ppos)
  1509. {
  1510. struct trace_array *tr = filp->private_data;
  1511. long val;
  1512. char buf[64];
  1513. if (cnt > 63)
  1514. cnt = 63;
  1515. if (copy_from_user(&buf, ubuf, cnt))
  1516. return -EFAULT;
  1517. buf[cnt] = 0;
  1518. val = simple_strtoul(buf, NULL, 10);
  1519. val = !!val;
  1520. mutex_lock(&trace_types_lock);
  1521. if (tr->ctrl ^ val) {
  1522. if (val)
  1523. tracer_enabled = 1;
  1524. else
  1525. tracer_enabled = 0;
  1526. tr->ctrl = val;
  1527. if (current_trace && current_trace->ctrl_update)
  1528. current_trace->ctrl_update(tr);
  1529. }
  1530. mutex_unlock(&trace_types_lock);
  1531. filp->f_pos += cnt;
  1532. return cnt;
  1533. }
  1534. static ssize_t
  1535. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1536. size_t cnt, loff_t *ppos)
  1537. {
  1538. char buf[max_tracer_type_len+2];
  1539. int r;
  1540. mutex_lock(&trace_types_lock);
  1541. if (current_trace)
  1542. r = sprintf(buf, "%s\n", current_trace->name);
  1543. else
  1544. r = sprintf(buf, "\n");
  1545. mutex_unlock(&trace_types_lock);
  1546. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1547. }
  1548. static ssize_t
  1549. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1550. size_t cnt, loff_t *ppos)
  1551. {
  1552. struct trace_array *tr = &global_trace;
  1553. struct tracer *t;
  1554. char buf[max_tracer_type_len+1];
  1555. int i;
  1556. if (cnt > max_tracer_type_len)
  1557. cnt = max_tracer_type_len;
  1558. if (copy_from_user(&buf, ubuf, cnt))
  1559. return -EFAULT;
  1560. buf[cnt] = 0;
  1561. /* strip ending whitespace. */
  1562. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1563. buf[i] = 0;
  1564. mutex_lock(&trace_types_lock);
  1565. for (t = trace_types; t; t = t->next) {
  1566. if (strcmp(t->name, buf) == 0)
  1567. break;
  1568. }
  1569. if (!t || t == current_trace)
  1570. goto out;
  1571. if (current_trace && current_trace->reset)
  1572. current_trace->reset(tr);
  1573. current_trace = t;
  1574. if (t->init)
  1575. t->init(tr);
  1576. out:
  1577. mutex_unlock(&trace_types_lock);
  1578. filp->f_pos += cnt;
  1579. return cnt;
  1580. }
  1581. static ssize_t
  1582. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1583. size_t cnt, loff_t *ppos)
  1584. {
  1585. unsigned long *ptr = filp->private_data;
  1586. char buf[64];
  1587. int r;
  1588. r = snprintf(buf, 64, "%ld\n",
  1589. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1590. if (r > 64)
  1591. r = 64;
  1592. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1593. }
  1594. static ssize_t
  1595. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1596. size_t cnt, loff_t *ppos)
  1597. {
  1598. long *ptr = filp->private_data;
  1599. long val;
  1600. char buf[64];
  1601. if (cnt > 63)
  1602. cnt = 63;
  1603. if (copy_from_user(&buf, ubuf, cnt))
  1604. return -EFAULT;
  1605. buf[cnt] = 0;
  1606. val = simple_strtoul(buf, NULL, 10);
  1607. *ptr = val * 1000;
  1608. return cnt;
  1609. }
  1610. static atomic_t tracing_reader;
  1611. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1612. {
  1613. struct trace_iterator *iter;
  1614. if (tracing_disabled)
  1615. return -ENODEV;
  1616. /* We only allow for reader of the pipe */
  1617. if (atomic_inc_return(&tracing_reader) != 1) {
  1618. atomic_dec(&tracing_reader);
  1619. return -EBUSY;
  1620. }
  1621. /* create a buffer to store the information to pass to userspace */
  1622. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1623. if (!iter)
  1624. return -ENOMEM;
  1625. iter->tr = &global_trace;
  1626. filp->private_data = iter;
  1627. return 0;
  1628. }
  1629. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1630. {
  1631. struct trace_iterator *iter = file->private_data;
  1632. kfree(iter);
  1633. atomic_dec(&tracing_reader);
  1634. return 0;
  1635. }
  1636. static unsigned int
  1637. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1638. {
  1639. struct trace_iterator *iter = filp->private_data;
  1640. if (trace_flags & TRACE_ITER_BLOCK) {
  1641. /*
  1642. * Always select as readable when in blocking mode
  1643. */
  1644. return POLLIN | POLLRDNORM;
  1645. }
  1646. else {
  1647. if (!trace_empty(iter))
  1648. return POLLIN | POLLRDNORM;
  1649. poll_wait(filp, &trace_wait, poll_table);
  1650. if (!trace_empty(iter))
  1651. return POLLIN | POLLRDNORM;
  1652. return 0;
  1653. }
  1654. }
  1655. /*
  1656. * Consumer reader.
  1657. */
  1658. static ssize_t
  1659. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1660. size_t cnt, loff_t *ppos)
  1661. {
  1662. struct trace_iterator *iter = filp->private_data;
  1663. struct trace_array_cpu *data;
  1664. static cpumask_t mask;
  1665. static int start;
  1666. unsigned long flags;
  1667. #ifdef CONFIG_FTRACE
  1668. int ftrace_save;
  1669. #endif
  1670. int read = 0;
  1671. int cpu;
  1672. int len;
  1673. int ret;
  1674. /* return any leftover data */
  1675. if (iter->seq.len > start) {
  1676. len = iter->seq.len - start;
  1677. if (cnt > len)
  1678. cnt = len;
  1679. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1680. if (ret)
  1681. cnt = -EFAULT;
  1682. start += len;
  1683. return cnt;
  1684. }
  1685. trace_seq_reset(&iter->seq);
  1686. start = 0;
  1687. while (trace_empty(iter)) {
  1688. if (!(trace_flags & TRACE_ITER_BLOCK))
  1689. return -EWOULDBLOCK;
  1690. /*
  1691. * This is a make-shift waitqueue. The reason we don't use
  1692. * an actual wait queue is because:
  1693. * 1) we only ever have one waiter
  1694. * 2) the tracing, traces all functions, we don't want
  1695. * the overhead of calling wake_up and friends
  1696. * (and tracing them too)
  1697. * Anyway, this is really very primitive wakeup.
  1698. */
  1699. set_current_state(TASK_INTERRUPTIBLE);
  1700. iter->tr->waiter = current;
  1701. /* sleep for one second, and try again. */
  1702. schedule_timeout(HZ);
  1703. iter->tr->waiter = NULL;
  1704. if (signal_pending(current))
  1705. return -EINTR;
  1706. /*
  1707. * We block until we read something and tracing is disabled.
  1708. * We still block if tracing is disabled, but we have never
  1709. * read anything. This allows a user to cat this file, and
  1710. * then enable tracing. But after we have read something,
  1711. * we give an EOF when tracing is again disabled.
  1712. *
  1713. * iter->pos will be 0 if we haven't read anything.
  1714. */
  1715. if (!tracer_enabled && iter->pos)
  1716. break;
  1717. continue;
  1718. }
  1719. /* stop when tracing is finished */
  1720. if (trace_empty(iter))
  1721. return 0;
  1722. if (cnt >= PAGE_SIZE)
  1723. cnt = PAGE_SIZE - 1;
  1724. memset(iter, 0, sizeof(*iter));
  1725. iter->tr = &global_trace;
  1726. iter->pos = -1;
  1727. /*
  1728. * We need to stop all tracing on all CPUS to read the
  1729. * the next buffer. This is a bit expensive, but is
  1730. * not done often. We fill all what we can read,
  1731. * and then release the locks again.
  1732. */
  1733. cpus_clear(mask);
  1734. local_irq_save(flags);
  1735. #ifdef CONFIG_FTRACE
  1736. ftrace_save = ftrace_enabled;
  1737. ftrace_enabled = 0;
  1738. #endif
  1739. smp_wmb();
  1740. for_each_possible_cpu(cpu) {
  1741. data = iter->tr->data[cpu];
  1742. if (!head_page(data) || !data->trace_idx)
  1743. continue;
  1744. atomic_inc(&data->disabled);
  1745. cpu_set(cpu, mask);
  1746. }
  1747. for_each_cpu_mask(cpu, mask) {
  1748. data = iter->tr->data[cpu];
  1749. spin_lock(&data->lock);
  1750. }
  1751. while (find_next_entry_inc(iter) != NULL) {
  1752. int len = iter->seq.len;
  1753. ret = print_trace_line(iter);
  1754. if (!ret) {
  1755. /* don't print partial lines */
  1756. iter->seq.len = len;
  1757. break;
  1758. }
  1759. trace_consume(iter);
  1760. if (iter->seq.len >= cnt)
  1761. break;
  1762. }
  1763. for_each_cpu_mask(cpu, mask) {
  1764. data = iter->tr->data[cpu];
  1765. spin_unlock(&data->lock);
  1766. }
  1767. for_each_cpu_mask(cpu, mask) {
  1768. data = iter->tr->data[cpu];
  1769. atomic_dec(&data->disabled);
  1770. }
  1771. #ifdef CONFIG_FTRACE
  1772. ftrace_enabled = ftrace_save;
  1773. #endif
  1774. local_irq_restore(flags);
  1775. /* Now copy what we have to the user */
  1776. read = iter->seq.len;
  1777. if (read > cnt)
  1778. read = cnt;
  1779. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1780. if (read < iter->seq.len)
  1781. start = read;
  1782. else
  1783. trace_seq_reset(&iter->seq);
  1784. if (ret)
  1785. read = -EFAULT;
  1786. return read;
  1787. }
  1788. static struct file_operations tracing_max_lat_fops = {
  1789. .open = tracing_open_generic,
  1790. .read = tracing_max_lat_read,
  1791. .write = tracing_max_lat_write,
  1792. };
  1793. static struct file_operations tracing_ctrl_fops = {
  1794. .open = tracing_open_generic,
  1795. .read = tracing_ctrl_read,
  1796. .write = tracing_ctrl_write,
  1797. };
  1798. static struct file_operations set_tracer_fops = {
  1799. .open = tracing_open_generic,
  1800. .read = tracing_set_trace_read,
  1801. .write = tracing_set_trace_write,
  1802. };
  1803. static struct file_operations tracing_pipe_fops = {
  1804. .open = tracing_open_pipe,
  1805. .poll = tracing_poll_pipe,
  1806. .read = tracing_read_pipe,
  1807. .release = tracing_release_pipe,
  1808. };
  1809. #ifdef CONFIG_DYNAMIC_FTRACE
  1810. static ssize_t
  1811. tracing_read_long(struct file *filp, char __user *ubuf,
  1812. size_t cnt, loff_t *ppos)
  1813. {
  1814. unsigned long *p = filp->private_data;
  1815. char buf[64];
  1816. int r;
  1817. r = sprintf(buf, "%ld\n", *p);
  1818. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1819. }
  1820. static struct file_operations tracing_read_long_fops = {
  1821. .open = tracing_open_generic,
  1822. .read = tracing_read_long,
  1823. };
  1824. #endif
  1825. static struct dentry *d_tracer;
  1826. struct dentry *tracing_init_dentry(void)
  1827. {
  1828. static int once;
  1829. if (d_tracer)
  1830. return d_tracer;
  1831. d_tracer = debugfs_create_dir("tracing", NULL);
  1832. if (!d_tracer && !once) {
  1833. once = 1;
  1834. pr_warning("Could not create debugfs directory 'tracing'\n");
  1835. return NULL;
  1836. }
  1837. return d_tracer;
  1838. }
  1839. #ifdef CONFIG_FTRACE_SELFTEST
  1840. /* Let selftest have access to static functions in this file */
  1841. #include "trace_selftest.c"
  1842. #endif
  1843. static __init void tracer_init_debugfs(void)
  1844. {
  1845. struct dentry *d_tracer;
  1846. struct dentry *entry;
  1847. d_tracer = tracing_init_dentry();
  1848. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1849. &global_trace, &tracing_ctrl_fops);
  1850. if (!entry)
  1851. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1852. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1853. NULL, &tracing_iter_fops);
  1854. if (!entry)
  1855. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1856. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1857. &global_trace, &tracing_lt_fops);
  1858. if (!entry)
  1859. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1860. entry = debugfs_create_file("trace", 0444, d_tracer,
  1861. &global_trace, &tracing_fops);
  1862. if (!entry)
  1863. pr_warning("Could not create debugfs 'trace' entry\n");
  1864. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1865. &global_trace, &show_traces_fops);
  1866. if (!entry)
  1867. pr_warning("Could not create debugfs 'trace' entry\n");
  1868. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1869. &global_trace, &set_tracer_fops);
  1870. if (!entry)
  1871. pr_warning("Could not create debugfs 'trace' entry\n");
  1872. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1873. &tracing_max_latency,
  1874. &tracing_max_lat_fops);
  1875. if (!entry)
  1876. pr_warning("Could not create debugfs "
  1877. "'tracing_max_latency' entry\n");
  1878. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1879. &tracing_thresh, &tracing_max_lat_fops);
  1880. if (!entry)
  1881. pr_warning("Could not create debugfs "
  1882. "'tracing_threash' entry\n");
  1883. entry = debugfs_create_file("README", 0644, d_tracer,
  1884. NULL, &tracing_readme_fops);
  1885. if (!entry)
  1886. pr_warning("Could not create debugfs 'README' entry\n");
  1887. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  1888. NULL, &tracing_pipe_fops);
  1889. if (!entry)
  1890. pr_warning("Could not create debugfs "
  1891. "'tracing_threash' entry\n");
  1892. #ifdef CONFIG_DYNAMIC_FTRACE
  1893. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1894. &ftrace_update_tot_cnt,
  1895. &tracing_read_long_fops);
  1896. if (!entry)
  1897. pr_warning("Could not create debugfs "
  1898. "'dyn_ftrace_total_info' entry\n");
  1899. #endif
  1900. }
  1901. /* dummy trace to disable tracing */
  1902. static struct tracer no_tracer __read_mostly =
  1903. {
  1904. .name = "none",
  1905. };
  1906. static int trace_alloc_page(void)
  1907. {
  1908. struct trace_array_cpu *data;
  1909. struct page *page, *tmp;
  1910. LIST_HEAD(pages);
  1911. void *array;
  1912. int i;
  1913. /* first allocate a page for each CPU */
  1914. for_each_possible_cpu(i) {
  1915. array = (void *)__get_free_page(GFP_KERNEL);
  1916. if (array == NULL) {
  1917. printk(KERN_ERR "tracer: failed to allocate page"
  1918. "for trace buffer!\n");
  1919. goto free_pages;
  1920. }
  1921. page = virt_to_page(array);
  1922. list_add(&page->lru, &pages);
  1923. /* Only allocate if we are actually using the max trace */
  1924. #ifdef CONFIG_TRACER_MAX_TRACE
  1925. array = (void *)__get_free_page(GFP_KERNEL);
  1926. if (array == NULL) {
  1927. printk(KERN_ERR "tracer: failed to allocate page"
  1928. "for trace buffer!\n");
  1929. goto free_pages;
  1930. }
  1931. page = virt_to_page(array);
  1932. list_add(&page->lru, &pages);
  1933. #endif
  1934. }
  1935. /* Now that we successfully allocate a page per CPU, add them */
  1936. for_each_possible_cpu(i) {
  1937. data = global_trace.data[i];
  1938. spin_lock_init(&data->lock);
  1939. lockdep_set_class(&data->lock, &data->lock_key);
  1940. page = list_entry(pages.next, struct page, lru);
  1941. list_del_init(&page->lru);
  1942. list_add_tail(&page->lru, &data->trace_pages);
  1943. ClearPageLRU(page);
  1944. #ifdef CONFIG_TRACER_MAX_TRACE
  1945. data = max_tr.data[i];
  1946. spin_lock_init(&data->lock);
  1947. lockdep_set_class(&data->lock, &data->lock_key);
  1948. page = list_entry(pages.next, struct page, lru);
  1949. list_del_init(&page->lru);
  1950. list_add_tail(&page->lru, &data->trace_pages);
  1951. SetPageLRU(page);
  1952. #endif
  1953. }
  1954. global_trace.entries += ENTRIES_PER_PAGE;
  1955. return 0;
  1956. free_pages:
  1957. list_for_each_entry_safe(page, tmp, &pages, lru) {
  1958. list_del_init(&page->lru);
  1959. __free_page(page);
  1960. }
  1961. return -ENOMEM;
  1962. }
  1963. __init static int tracer_alloc_buffers(void)
  1964. {
  1965. struct trace_array_cpu *data;
  1966. void *array;
  1967. struct page *page;
  1968. int pages = 0;
  1969. int ret = -ENOMEM;
  1970. int i;
  1971. global_trace.ctrl = tracer_enabled;
  1972. /* Allocate the first page for all buffers */
  1973. for_each_possible_cpu(i) {
  1974. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  1975. max_tr.data[i] = &per_cpu(max_data, i);
  1976. array = (void *)__get_free_page(GFP_KERNEL);
  1977. if (array == NULL) {
  1978. printk(KERN_ERR "tracer: failed to allocate page"
  1979. "for trace buffer!\n");
  1980. goto free_buffers;
  1981. }
  1982. /* set the array to the list */
  1983. INIT_LIST_HEAD(&data->trace_pages);
  1984. page = virt_to_page(array);
  1985. list_add(&page->lru, &data->trace_pages);
  1986. /* use the LRU flag to differentiate the two buffers */
  1987. ClearPageLRU(page);
  1988. /* Only allocate if we are actually using the max trace */
  1989. #ifdef CONFIG_TRACER_MAX_TRACE
  1990. array = (void *)__get_free_page(GFP_KERNEL);
  1991. if (array == NULL) {
  1992. printk(KERN_ERR "tracer: failed to allocate page"
  1993. "for trace buffer!\n");
  1994. goto free_buffers;
  1995. }
  1996. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  1997. page = virt_to_page(array);
  1998. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  1999. SetPageLRU(page);
  2000. #endif
  2001. }
  2002. /*
  2003. * Since we allocate by orders of pages, we may be able to
  2004. * round up a bit.
  2005. */
  2006. global_trace.entries = ENTRIES_PER_PAGE;
  2007. pages++;
  2008. while (global_trace.entries < trace_nr_entries) {
  2009. if (trace_alloc_page())
  2010. break;
  2011. pages++;
  2012. }
  2013. max_tr.entries = global_trace.entries;
  2014. pr_info("tracer: %d pages allocated for %ld",
  2015. pages, trace_nr_entries);
  2016. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2017. pr_info(" actual entries %ld\n", global_trace.entries);
  2018. tracer_init_debugfs();
  2019. trace_init_cmdlines();
  2020. register_tracer(&no_tracer);
  2021. current_trace = &no_tracer;
  2022. /* All seems OK, enable tracing */
  2023. tracing_disabled = 0;
  2024. return 0;
  2025. free_buffers:
  2026. for (i-- ; i >= 0; i--) {
  2027. struct page *page, *tmp;
  2028. struct trace_array_cpu *data = global_trace.data[i];
  2029. if (data) {
  2030. list_for_each_entry_safe(page, tmp,
  2031. &data->trace_pages, lru) {
  2032. list_del_init(&page->lru);
  2033. __free_page(page);
  2034. }
  2035. }
  2036. #ifdef CONFIG_TRACER_MAX_TRACE
  2037. data = max_tr.data[i];
  2038. if (data) {
  2039. list_for_each_entry_safe(page, tmp,
  2040. &data->trace_pages, lru) {
  2041. list_del_init(&page->lru);
  2042. __free_page(page);
  2043. }
  2044. }
  2045. #endif
  2046. }
  2047. return ret;
  2048. }
  2049. fs_initcall(tracer_alloc_buffers);