trace.c 51 KB

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