trace.c 51 KB

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