trace.c 51 KB

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