trace.c 52 KB

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