trace.c 56 KB

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