trace.c 52 KB

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