trace.c 52 KB

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