trace.c 58 KB

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