trace.c 53 KB

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