trace.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  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. }
  536. void
  537. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  538. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  539. {
  540. if (likely(!atomic_read(&data->disabled)))
  541. trace_function(tr, data, ip, parent_ip, flags);
  542. }
  543. void
  544. __trace_special(void *__tr, void *__data,
  545. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  546. {
  547. struct trace_array_cpu *data = __data;
  548. struct trace_array *tr = __tr;
  549. struct trace_entry *entry;
  550. unsigned long irq_flags;
  551. spin_lock_irqsave(&data->lock, irq_flags);
  552. entry = tracing_get_trace_entry(tr, data);
  553. tracing_generic_entry_update(entry, 0);
  554. entry->type = TRACE_SPECIAL;
  555. entry->special.arg1 = arg1;
  556. entry->special.arg2 = arg2;
  557. entry->special.arg3 = arg3;
  558. spin_unlock_irqrestore(&data->lock, irq_flags);
  559. trace_wake_up();
  560. }
  561. void __trace_stack(struct trace_array *tr,
  562. struct trace_array_cpu *data,
  563. unsigned long flags,
  564. int skip)
  565. {
  566. struct trace_entry *entry;
  567. struct stack_trace trace;
  568. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  569. return;
  570. entry = tracing_get_trace_entry(tr, data);
  571. tracing_generic_entry_update(entry, flags);
  572. entry->type = TRACE_STACK;
  573. memset(&entry->stack, 0, sizeof(entry->stack));
  574. trace.nr_entries = 0;
  575. trace.max_entries = FTRACE_STACK_ENTRIES;
  576. trace.skip = skip;
  577. trace.entries = entry->stack.caller;
  578. save_stack_trace(&trace);
  579. }
  580. void
  581. tracing_sched_switch_trace(struct trace_array *tr,
  582. struct trace_array_cpu *data,
  583. struct task_struct *prev,
  584. struct task_struct *next,
  585. unsigned long flags)
  586. {
  587. struct trace_entry *entry;
  588. unsigned long irq_flags;
  589. spin_lock_irqsave(&data->lock, irq_flags);
  590. entry = tracing_get_trace_entry(tr, data);
  591. tracing_generic_entry_update(entry, flags);
  592. entry->type = TRACE_CTX;
  593. entry->ctx.prev_pid = prev->pid;
  594. entry->ctx.prev_prio = prev->prio;
  595. entry->ctx.prev_state = prev->state;
  596. entry->ctx.next_pid = next->pid;
  597. entry->ctx.next_prio = next->prio;
  598. entry->ctx.next_state = next->state;
  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. entry->ctx.next_state = wakee->state;
  621. __trace_stack(tr, data, flags, 5);
  622. spin_unlock_irqrestore(&data->lock, irq_flags);
  623. trace_wake_up();
  624. }
  625. #ifdef CONFIG_FTRACE
  626. static void
  627. function_trace_call(unsigned long ip, unsigned long parent_ip)
  628. {
  629. struct trace_array *tr = &global_trace;
  630. struct trace_array_cpu *data;
  631. unsigned long flags;
  632. long disabled;
  633. int cpu;
  634. if (unlikely(!tracer_enabled))
  635. return;
  636. local_irq_save(flags);
  637. cpu = raw_smp_processor_id();
  638. data = tr->data[cpu];
  639. disabled = atomic_inc_return(&data->disabled);
  640. if (likely(disabled == 1))
  641. trace_function(tr, data, ip, parent_ip, flags);
  642. atomic_dec(&data->disabled);
  643. local_irq_restore(flags);
  644. }
  645. static struct ftrace_ops trace_ops __read_mostly =
  646. {
  647. .func = function_trace_call,
  648. };
  649. void tracing_start_function_trace(void)
  650. {
  651. register_ftrace_function(&trace_ops);
  652. }
  653. void tracing_stop_function_trace(void)
  654. {
  655. unregister_ftrace_function(&trace_ops);
  656. }
  657. #endif
  658. enum trace_file_type {
  659. TRACE_FILE_LAT_FMT = 1,
  660. };
  661. static struct trace_entry *
  662. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  663. struct trace_iterator *iter, int cpu)
  664. {
  665. struct page *page;
  666. struct trace_entry *array;
  667. if (iter->next_idx[cpu] >= tr->entries ||
  668. iter->next_idx[cpu] >= data->trace_idx ||
  669. (data->trace_head == data->trace_tail &&
  670. data->trace_head_idx == data->trace_tail_idx))
  671. return NULL;
  672. if (!iter->next_page[cpu]) {
  673. /* Initialize the iterator for this cpu trace buffer */
  674. WARN_ON(!data->trace_tail);
  675. page = virt_to_page(data->trace_tail);
  676. iter->next_page[cpu] = &page->lru;
  677. iter->next_page_idx[cpu] = data->trace_tail_idx;
  678. }
  679. page = list_entry(iter->next_page[cpu], struct page, lru);
  680. BUG_ON(&data->trace_pages == &page->lru);
  681. array = page_address(page);
  682. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  683. return &array[iter->next_page_idx[cpu]];
  684. }
  685. static struct trace_entry *
  686. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  687. {
  688. struct trace_array *tr = iter->tr;
  689. struct trace_entry *ent, *next = NULL;
  690. int next_cpu = -1;
  691. int cpu;
  692. for_each_possible_cpu(cpu) {
  693. if (!head_page(tr->data[cpu]))
  694. continue;
  695. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  696. /*
  697. * Pick the entry with the smallest timestamp:
  698. */
  699. if (ent && (!next || ent->t < next->t)) {
  700. next = ent;
  701. next_cpu = cpu;
  702. }
  703. }
  704. if (ent_cpu)
  705. *ent_cpu = next_cpu;
  706. return next;
  707. }
  708. static void trace_iterator_increment(struct trace_iterator *iter)
  709. {
  710. iter->idx++;
  711. iter->next_idx[iter->cpu]++;
  712. iter->next_page_idx[iter->cpu]++;
  713. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  714. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  715. iter->next_page_idx[iter->cpu] = 0;
  716. iter->next_page[iter->cpu] =
  717. trace_next_list(data, iter->next_page[iter->cpu]);
  718. }
  719. }
  720. static void trace_consume(struct trace_iterator *iter)
  721. {
  722. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  723. data->trace_tail_idx++;
  724. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  725. data->trace_tail = trace_next_page(data, data->trace_tail);
  726. data->trace_tail_idx = 0;
  727. }
  728. /* Check if we empty it, then reset the index */
  729. if (data->trace_head == data->trace_tail &&
  730. data->trace_head_idx == data->trace_tail_idx)
  731. data->trace_idx = 0;
  732. }
  733. static void *find_next_entry_inc(struct trace_iterator *iter)
  734. {
  735. struct trace_entry *next;
  736. int next_cpu = -1;
  737. next = find_next_entry(iter, &next_cpu);
  738. iter->prev_ent = iter->ent;
  739. iter->prev_cpu = iter->cpu;
  740. iter->ent = next;
  741. iter->cpu = next_cpu;
  742. if (next)
  743. trace_iterator_increment(iter);
  744. return next ? iter : NULL;
  745. }
  746. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  747. {
  748. struct trace_iterator *iter = m->private;
  749. void *last_ent = iter->ent;
  750. int i = (int)*pos;
  751. void *ent;
  752. (*pos)++;
  753. /* can't go backwards */
  754. if (iter->idx > i)
  755. return NULL;
  756. if (iter->idx < 0)
  757. ent = find_next_entry_inc(iter);
  758. else
  759. ent = iter;
  760. while (ent && iter->idx < i)
  761. ent = find_next_entry_inc(iter);
  762. iter->pos = *pos;
  763. if (last_ent && !ent)
  764. seq_puts(m, "\n\nvim:ft=help\n");
  765. return ent;
  766. }
  767. static void *s_start(struct seq_file *m, loff_t *pos)
  768. {
  769. struct trace_iterator *iter = m->private;
  770. void *p = NULL;
  771. loff_t l = 0;
  772. int i;
  773. mutex_lock(&trace_types_lock);
  774. if (!current_trace || current_trace != iter->trace)
  775. return NULL;
  776. atomic_inc(&trace_record_cmdline_disabled);
  777. /* let the tracer grab locks here if needed */
  778. if (current_trace->start)
  779. current_trace->start(iter);
  780. if (*pos != iter->pos) {
  781. iter->ent = NULL;
  782. iter->cpu = 0;
  783. iter->idx = -1;
  784. iter->prev_ent = NULL;
  785. iter->prev_cpu = -1;
  786. for_each_possible_cpu(i) {
  787. iter->next_idx[i] = 0;
  788. iter->next_page[i] = NULL;
  789. }
  790. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  791. ;
  792. } else {
  793. l = *pos - 1;
  794. p = s_next(m, p, &l);
  795. }
  796. return p;
  797. }
  798. static void s_stop(struct seq_file *m, void *p)
  799. {
  800. struct trace_iterator *iter = m->private;
  801. atomic_dec(&trace_record_cmdline_disabled);
  802. /* let the tracer release locks here if needed */
  803. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  804. iter->trace->stop(iter);
  805. mutex_unlock(&trace_types_lock);
  806. }
  807. static int
  808. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  809. {
  810. #ifdef CONFIG_KALLSYMS
  811. char str[KSYM_SYMBOL_LEN];
  812. kallsyms_lookup(address, NULL, NULL, NULL, str);
  813. return trace_seq_printf(s, fmt, str);
  814. #endif
  815. return 1;
  816. }
  817. static int
  818. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  819. unsigned long address)
  820. {
  821. #ifdef CONFIG_KALLSYMS
  822. char str[KSYM_SYMBOL_LEN];
  823. sprint_symbol(str, address);
  824. return trace_seq_printf(s, fmt, str);
  825. #endif
  826. return 1;
  827. }
  828. #ifndef CONFIG_64BIT
  829. # define IP_FMT "%08lx"
  830. #else
  831. # define IP_FMT "%016lx"
  832. #endif
  833. static int
  834. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  835. {
  836. int ret;
  837. if (!ip)
  838. return trace_seq_printf(s, "0");
  839. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  840. ret = seq_print_sym_offset(s, "%s", ip);
  841. else
  842. ret = seq_print_sym_short(s, "%s", ip);
  843. if (!ret)
  844. return 0;
  845. if (sym_flags & TRACE_ITER_SYM_ADDR)
  846. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  847. return ret;
  848. }
  849. static void print_lat_help_header(struct seq_file *m)
  850. {
  851. seq_puts(m, "# _------=> CPU# \n");
  852. seq_puts(m, "# / _-----=> irqs-off \n");
  853. seq_puts(m, "# | / _----=> need-resched \n");
  854. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  855. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  856. seq_puts(m, "# |||| / \n");
  857. seq_puts(m, "# ||||| delay \n");
  858. seq_puts(m, "# cmd pid ||||| time | caller \n");
  859. seq_puts(m, "# \\ / ||||| \\ | / \n");
  860. }
  861. static void print_func_help_header(struct seq_file *m)
  862. {
  863. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  864. seq_puts(m, "# | | | | |\n");
  865. }
  866. static void
  867. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  868. {
  869. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  870. struct trace_array *tr = iter->tr;
  871. struct trace_array_cpu *data = tr->data[tr->cpu];
  872. struct tracer *type = current_trace;
  873. unsigned long total = 0;
  874. unsigned long entries = 0;
  875. int cpu;
  876. const char *name = "preemption";
  877. if (type)
  878. name = type->name;
  879. for_each_possible_cpu(cpu) {
  880. if (head_page(tr->data[cpu])) {
  881. total += tr->data[cpu]->trace_idx;
  882. if (tr->data[cpu]->trace_idx > tr->entries)
  883. entries += tr->entries;
  884. else
  885. entries += tr->data[cpu]->trace_idx;
  886. }
  887. }
  888. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  889. name, UTS_RELEASE);
  890. seq_puts(m, "-----------------------------------"
  891. "---------------------------------\n");
  892. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  893. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  894. nsecs_to_usecs(data->saved_latency),
  895. entries,
  896. total,
  897. tr->cpu,
  898. #if defined(CONFIG_PREEMPT_NONE)
  899. "server",
  900. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  901. "desktop",
  902. #elif defined(CONFIG_PREEMPT_DESKTOP)
  903. "preempt",
  904. #else
  905. "unknown",
  906. #endif
  907. /* These are reserved for later use */
  908. 0, 0, 0, 0);
  909. #ifdef CONFIG_SMP
  910. seq_printf(m, " #P:%d)\n", num_online_cpus());
  911. #else
  912. seq_puts(m, ")\n");
  913. #endif
  914. seq_puts(m, " -----------------\n");
  915. seq_printf(m, " | task: %.16s-%d "
  916. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  917. data->comm, data->pid, data->uid, data->nice,
  918. data->policy, data->rt_priority);
  919. seq_puts(m, " -----------------\n");
  920. if (data->critical_start) {
  921. seq_puts(m, " => started at: ");
  922. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  923. trace_print_seq(m, &iter->seq);
  924. seq_puts(m, "\n => ended at: ");
  925. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  926. trace_print_seq(m, &iter->seq);
  927. seq_puts(m, "\n");
  928. }
  929. seq_puts(m, "\n");
  930. }
  931. static void
  932. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  933. {
  934. int hardirq, softirq;
  935. char *comm;
  936. comm = trace_find_cmdline(entry->pid);
  937. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  938. trace_seq_printf(s, "%d", cpu);
  939. trace_seq_printf(s, "%c%c",
  940. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  941. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  942. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  943. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  944. if (hardirq && softirq)
  945. trace_seq_putc(s, 'H');
  946. else {
  947. if (hardirq)
  948. trace_seq_putc(s, 'h');
  949. else {
  950. if (softirq)
  951. trace_seq_putc(s, 's');
  952. else
  953. trace_seq_putc(s, '.');
  954. }
  955. }
  956. if (entry->preempt_count)
  957. trace_seq_printf(s, "%x", entry->preempt_count);
  958. else
  959. trace_seq_puts(s, ".");
  960. }
  961. unsigned long preempt_mark_thresh = 100;
  962. static void
  963. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  964. unsigned long rel_usecs)
  965. {
  966. trace_seq_printf(s, " %4lldus", abs_usecs);
  967. if (rel_usecs > preempt_mark_thresh)
  968. trace_seq_puts(s, "!: ");
  969. else if (rel_usecs > 1)
  970. trace_seq_puts(s, "+: ");
  971. else
  972. trace_seq_puts(s, " : ");
  973. }
  974. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  975. static int
  976. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  977. {
  978. struct trace_seq *s = &iter->seq;
  979. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  980. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  981. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  982. struct trace_entry *entry = iter->ent;
  983. unsigned long abs_usecs;
  984. unsigned long rel_usecs;
  985. char *comm;
  986. int S, T;
  987. int i;
  988. if (!next_entry)
  989. next_entry = entry;
  990. rel_usecs = ns2usecs(next_entry->t - entry->t);
  991. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  992. if (verbose) {
  993. comm = trace_find_cmdline(entry->pid);
  994. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  995. " %ld.%03ldms (+%ld.%03ldms): ",
  996. comm,
  997. entry->pid, cpu, entry->flags,
  998. entry->preempt_count, trace_idx,
  999. ns2usecs(entry->t),
  1000. abs_usecs/1000,
  1001. abs_usecs % 1000, rel_usecs/1000,
  1002. rel_usecs % 1000);
  1003. } else {
  1004. lat_print_generic(s, entry, cpu);
  1005. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1006. }
  1007. switch (entry->type) {
  1008. case TRACE_FN:
  1009. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1010. trace_seq_puts(s, " (");
  1011. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1012. trace_seq_puts(s, ")\n");
  1013. break;
  1014. case TRACE_CTX:
  1015. case TRACE_WAKE:
  1016. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1017. state_to_char[entry->ctx.prev_state] : 'X';
  1018. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1019. state_to_char[entry->ctx.next_state] : 'X';
  1020. comm = trace_find_cmdline(entry->ctx.next_pid);
  1021. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1022. entry->ctx.prev_pid,
  1023. entry->ctx.prev_prio,
  1024. S, entry->type == TRACE_CTX ? "==>" : " +",
  1025. entry->ctx.next_pid,
  1026. entry->ctx.next_prio,
  1027. T, comm);
  1028. break;
  1029. case TRACE_SPECIAL:
  1030. trace_seq_printf(s, "# %ld %ld %ld\n",
  1031. entry->special.arg1,
  1032. entry->special.arg2,
  1033. entry->special.arg3);
  1034. break;
  1035. case TRACE_STACK:
  1036. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1037. if (i)
  1038. trace_seq_puts(s, " <= ");
  1039. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1040. }
  1041. trace_seq_puts(s, "\n");
  1042. break;
  1043. default:
  1044. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1045. }
  1046. return 1;
  1047. }
  1048. static int print_trace_fmt(struct trace_iterator *iter)
  1049. {
  1050. struct trace_seq *s = &iter->seq;
  1051. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1052. struct trace_entry *entry;
  1053. unsigned long usec_rem;
  1054. unsigned long long t;
  1055. unsigned long secs;
  1056. char *comm;
  1057. int ret;
  1058. int S, T;
  1059. int i;
  1060. entry = iter->ent;
  1061. comm = trace_find_cmdline(iter->ent->pid);
  1062. t = ns2usecs(entry->t);
  1063. usec_rem = do_div(t, 1000000ULL);
  1064. secs = (unsigned long)t;
  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. switch (entry->type) {
  1075. case TRACE_FN:
  1076. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1077. if (!ret)
  1078. return 0;
  1079. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1080. entry->fn.parent_ip) {
  1081. ret = trace_seq_printf(s, " <-");
  1082. if (!ret)
  1083. return 0;
  1084. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1085. sym_flags);
  1086. if (!ret)
  1087. return 0;
  1088. }
  1089. ret = trace_seq_printf(s, "\n");
  1090. if (!ret)
  1091. return 0;
  1092. break;
  1093. case TRACE_CTX:
  1094. case TRACE_WAKE:
  1095. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1096. state_to_char[entry->ctx.prev_state] : 'X';
  1097. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1098. state_to_char[entry->ctx.next_state] : 'X';
  1099. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1100. entry->ctx.prev_pid,
  1101. entry->ctx.prev_prio,
  1102. S,
  1103. entry->type == TRACE_CTX ? "==>" : " +",
  1104. entry->ctx.next_pid,
  1105. entry->ctx.next_prio,
  1106. T);
  1107. if (!ret)
  1108. return 0;
  1109. break;
  1110. case TRACE_SPECIAL:
  1111. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1112. entry->special.arg1,
  1113. entry->special.arg2,
  1114. entry->special.arg3);
  1115. if (!ret)
  1116. return 0;
  1117. break;
  1118. case TRACE_STACK:
  1119. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1120. if (i) {
  1121. ret = trace_seq_puts(s, " <= ");
  1122. if (!ret)
  1123. return 0;
  1124. }
  1125. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1126. sym_flags);
  1127. if (!ret)
  1128. return 0;
  1129. }
  1130. ret = trace_seq_puts(s, "\n");
  1131. if (!ret)
  1132. return 0;
  1133. break;
  1134. }
  1135. return 1;
  1136. }
  1137. static int print_raw_fmt(struct trace_iterator *iter)
  1138. {
  1139. struct trace_seq *s = &iter->seq;
  1140. struct trace_entry *entry;
  1141. int ret;
  1142. int S, T;
  1143. entry = iter->ent;
  1144. ret = trace_seq_printf(s, "%d %d %llu ",
  1145. entry->pid, iter->cpu, entry->t);
  1146. if (!ret)
  1147. return 0;
  1148. switch (entry->type) {
  1149. case TRACE_FN:
  1150. ret = trace_seq_printf(s, "%x %x\n",
  1151. entry->fn.ip, entry->fn.parent_ip);
  1152. if (!ret)
  1153. return 0;
  1154. break;
  1155. case TRACE_CTX:
  1156. case TRACE_WAKE:
  1157. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1158. state_to_char[entry->ctx.prev_state] : 'X';
  1159. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1160. state_to_char[entry->ctx.next_state] : 'X';
  1161. if (entry->type == TRACE_WAKE)
  1162. S = '+';
  1163. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1164. entry->ctx.prev_pid,
  1165. entry->ctx.prev_prio,
  1166. S,
  1167. entry->ctx.next_pid,
  1168. entry->ctx.next_prio,
  1169. T);
  1170. if (!ret)
  1171. return 0;
  1172. break;
  1173. case TRACE_SPECIAL:
  1174. case TRACE_STACK:
  1175. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1176. entry->special.arg1,
  1177. entry->special.arg2,
  1178. entry->special.arg3);
  1179. if (!ret)
  1180. return 0;
  1181. break;
  1182. }
  1183. return 1;
  1184. }
  1185. #define SEQ_PUT_FIELD_RET(s, x) \
  1186. do { \
  1187. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1188. return 0; \
  1189. } while (0)
  1190. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1191. do { \
  1192. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1193. return 0; \
  1194. } while (0)
  1195. static int print_hex_fmt(struct trace_iterator *iter)
  1196. {
  1197. struct trace_seq *s = &iter->seq;
  1198. unsigned char newline = '\n';
  1199. struct trace_entry *entry;
  1200. int S, T;
  1201. entry = iter->ent;
  1202. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1203. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1204. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1205. switch (entry->type) {
  1206. case TRACE_FN:
  1207. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1208. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1209. break;
  1210. case TRACE_CTX:
  1211. case TRACE_WAKE:
  1212. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1213. state_to_char[entry->ctx.prev_state] : 'X';
  1214. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1215. state_to_char[entry->ctx.next_state] : 'X';
  1216. if (entry->type == TRACE_WAKE)
  1217. S = '+';
  1218. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1219. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1220. SEQ_PUT_HEX_FIELD_RET(s, S);
  1221. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1222. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1223. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1224. SEQ_PUT_HEX_FIELD_RET(s, T);
  1225. break;
  1226. case TRACE_SPECIAL:
  1227. case TRACE_STACK:
  1228. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1229. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1230. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1231. break;
  1232. }
  1233. SEQ_PUT_FIELD_RET(s, newline);
  1234. return 1;
  1235. }
  1236. static int print_bin_fmt(struct trace_iterator *iter)
  1237. {
  1238. struct trace_seq *s = &iter->seq;
  1239. struct trace_entry *entry;
  1240. entry = iter->ent;
  1241. SEQ_PUT_FIELD_RET(s, entry->pid);
  1242. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1243. SEQ_PUT_FIELD_RET(s, entry->t);
  1244. switch (entry->type) {
  1245. case TRACE_FN:
  1246. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1247. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1248. break;
  1249. case TRACE_CTX:
  1250. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1251. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1252. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1253. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1254. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1255. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1256. break;
  1257. case TRACE_SPECIAL:
  1258. case TRACE_STACK:
  1259. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1260. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1261. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1262. break;
  1263. }
  1264. return 1;
  1265. }
  1266. static int trace_empty(struct trace_iterator *iter)
  1267. {
  1268. struct trace_array_cpu *data;
  1269. int cpu;
  1270. for_each_possible_cpu(cpu) {
  1271. data = iter->tr->data[cpu];
  1272. if (head_page(data) && data->trace_idx &&
  1273. (data->trace_tail != data->trace_head ||
  1274. data->trace_tail_idx != data->trace_head_idx))
  1275. return 0;
  1276. }
  1277. return 1;
  1278. }
  1279. static int print_trace_line(struct trace_iterator *iter)
  1280. {
  1281. if (trace_flags & TRACE_ITER_BIN)
  1282. return print_bin_fmt(iter);
  1283. if (trace_flags & TRACE_ITER_HEX)
  1284. return print_hex_fmt(iter);
  1285. if (trace_flags & TRACE_ITER_RAW)
  1286. return print_raw_fmt(iter);
  1287. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1288. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1289. return print_trace_fmt(iter);
  1290. }
  1291. static int s_show(struct seq_file *m, void *v)
  1292. {
  1293. struct trace_iterator *iter = v;
  1294. if (iter->ent == NULL) {
  1295. if (iter->tr) {
  1296. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1297. seq_puts(m, "#\n");
  1298. }
  1299. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1300. /* print nothing if the buffers are empty */
  1301. if (trace_empty(iter))
  1302. return 0;
  1303. print_trace_header(m, iter);
  1304. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1305. print_lat_help_header(m);
  1306. } else {
  1307. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1308. print_func_help_header(m);
  1309. }
  1310. } else {
  1311. print_trace_line(iter);
  1312. trace_print_seq(m, &iter->seq);
  1313. }
  1314. return 0;
  1315. }
  1316. static struct seq_operations tracer_seq_ops = {
  1317. .start = s_start,
  1318. .next = s_next,
  1319. .stop = s_stop,
  1320. .show = s_show,
  1321. };
  1322. static struct trace_iterator *
  1323. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1324. {
  1325. struct trace_iterator *iter;
  1326. if (tracing_disabled) {
  1327. *ret = -ENODEV;
  1328. return NULL;
  1329. }
  1330. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1331. if (!iter) {
  1332. *ret = -ENOMEM;
  1333. goto out;
  1334. }
  1335. mutex_lock(&trace_types_lock);
  1336. if (current_trace && current_trace->print_max)
  1337. iter->tr = &max_tr;
  1338. else
  1339. iter->tr = inode->i_private;
  1340. iter->trace = current_trace;
  1341. iter->pos = -1;
  1342. /* TODO stop tracer */
  1343. *ret = seq_open(file, &tracer_seq_ops);
  1344. if (!*ret) {
  1345. struct seq_file *m = file->private_data;
  1346. m->private = iter;
  1347. /* stop the trace while dumping */
  1348. if (iter->tr->ctrl)
  1349. tracer_enabled = 0;
  1350. if (iter->trace && iter->trace->open)
  1351. iter->trace->open(iter);
  1352. } else {
  1353. kfree(iter);
  1354. iter = NULL;
  1355. }
  1356. mutex_unlock(&trace_types_lock);
  1357. out:
  1358. return iter;
  1359. }
  1360. int tracing_open_generic(struct inode *inode, struct file *filp)
  1361. {
  1362. if (tracing_disabled)
  1363. return -ENODEV;
  1364. filp->private_data = inode->i_private;
  1365. return 0;
  1366. }
  1367. int tracing_release(struct inode *inode, struct file *file)
  1368. {
  1369. struct seq_file *m = (struct seq_file *)file->private_data;
  1370. struct trace_iterator *iter = m->private;
  1371. mutex_lock(&trace_types_lock);
  1372. if (iter->trace && iter->trace->close)
  1373. iter->trace->close(iter);
  1374. /* reenable tracing if it was previously enabled */
  1375. if (iter->tr->ctrl)
  1376. tracer_enabled = 1;
  1377. mutex_unlock(&trace_types_lock);
  1378. seq_release(inode, file);
  1379. kfree(iter);
  1380. return 0;
  1381. }
  1382. static int tracing_open(struct inode *inode, struct file *file)
  1383. {
  1384. int ret;
  1385. __tracing_open(inode, file, &ret);
  1386. return ret;
  1387. }
  1388. static int tracing_lt_open(struct inode *inode, struct file *file)
  1389. {
  1390. struct trace_iterator *iter;
  1391. int ret;
  1392. iter = __tracing_open(inode, file, &ret);
  1393. if (!ret)
  1394. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1395. return ret;
  1396. }
  1397. static void *
  1398. t_next(struct seq_file *m, void *v, loff_t *pos)
  1399. {
  1400. struct tracer *t = m->private;
  1401. (*pos)++;
  1402. if (t)
  1403. t = t->next;
  1404. m->private = t;
  1405. return t;
  1406. }
  1407. static void *t_start(struct seq_file *m, loff_t *pos)
  1408. {
  1409. struct tracer *t = m->private;
  1410. loff_t l = 0;
  1411. mutex_lock(&trace_types_lock);
  1412. for (; t && l < *pos; t = t_next(m, t, &l))
  1413. ;
  1414. return t;
  1415. }
  1416. static void t_stop(struct seq_file *m, void *p)
  1417. {
  1418. mutex_unlock(&trace_types_lock);
  1419. }
  1420. static int t_show(struct seq_file *m, void *v)
  1421. {
  1422. struct tracer *t = v;
  1423. if (!t)
  1424. return 0;
  1425. seq_printf(m, "%s", t->name);
  1426. if (t->next)
  1427. seq_putc(m, ' ');
  1428. else
  1429. seq_putc(m, '\n');
  1430. return 0;
  1431. }
  1432. static struct seq_operations show_traces_seq_ops = {
  1433. .start = t_start,
  1434. .next = t_next,
  1435. .stop = t_stop,
  1436. .show = t_show,
  1437. };
  1438. static int show_traces_open(struct inode *inode, struct file *file)
  1439. {
  1440. int ret;
  1441. if (tracing_disabled)
  1442. return -ENODEV;
  1443. ret = seq_open(file, &show_traces_seq_ops);
  1444. if (!ret) {
  1445. struct seq_file *m = file->private_data;
  1446. m->private = trace_types;
  1447. }
  1448. return ret;
  1449. }
  1450. static struct file_operations tracing_fops = {
  1451. .open = tracing_open,
  1452. .read = seq_read,
  1453. .llseek = seq_lseek,
  1454. .release = tracing_release,
  1455. };
  1456. static struct file_operations tracing_lt_fops = {
  1457. .open = tracing_lt_open,
  1458. .read = seq_read,
  1459. .llseek = seq_lseek,
  1460. .release = tracing_release,
  1461. };
  1462. static struct file_operations show_traces_fops = {
  1463. .open = show_traces_open,
  1464. .read = seq_read,
  1465. .release = seq_release,
  1466. };
  1467. /*
  1468. * Only trace on a CPU if the bitmask is set:
  1469. */
  1470. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1471. /*
  1472. * When tracing/tracing_cpu_mask is modified then this holds
  1473. * the new bitmask we are about to install:
  1474. */
  1475. static cpumask_t tracing_cpumask_new;
  1476. /*
  1477. * The tracer itself will not take this lock, but still we want
  1478. * to provide a consistent cpumask to user-space:
  1479. */
  1480. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1481. /*
  1482. * Temporary storage for the character representation of the
  1483. * CPU bitmask (and one more byte for the newline):
  1484. */
  1485. static char mask_str[NR_CPUS + 1];
  1486. static ssize_t
  1487. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1488. size_t count, loff_t *ppos)
  1489. {
  1490. int len;
  1491. mutex_lock(&tracing_cpumask_update_lock);
  1492. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1493. if (count - len < 2) {
  1494. count = -EINVAL;
  1495. goto out_err;
  1496. }
  1497. len += sprintf(mask_str + len, "\n");
  1498. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1499. out_err:
  1500. mutex_unlock(&tracing_cpumask_update_lock);
  1501. return count;
  1502. }
  1503. static ssize_t
  1504. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1505. size_t count, loff_t *ppos)
  1506. {
  1507. int err, cpu;
  1508. mutex_lock(&tracing_cpumask_update_lock);
  1509. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1510. if (err)
  1511. goto err_unlock;
  1512. spin_lock_irq(&ftrace_max_lock);
  1513. for_each_possible_cpu(cpu) {
  1514. /*
  1515. * Increase/decrease the disabled counter if we are
  1516. * about to flip a bit in the cpumask:
  1517. */
  1518. if (cpu_isset(cpu, tracing_cpumask) &&
  1519. !cpu_isset(cpu, tracing_cpumask_new)) {
  1520. atomic_inc(&global_trace.data[cpu]->disabled);
  1521. }
  1522. if (!cpu_isset(cpu, tracing_cpumask) &&
  1523. cpu_isset(cpu, tracing_cpumask_new)) {
  1524. atomic_dec(&global_trace.data[cpu]->disabled);
  1525. }
  1526. }
  1527. spin_unlock_irq(&ftrace_max_lock);
  1528. tracing_cpumask = tracing_cpumask_new;
  1529. mutex_unlock(&tracing_cpumask_update_lock);
  1530. return count;
  1531. err_unlock:
  1532. mutex_unlock(&tracing_cpumask_update_lock);
  1533. return err;
  1534. }
  1535. static struct file_operations tracing_cpumask_fops = {
  1536. .open = tracing_open_generic,
  1537. .read = tracing_cpumask_read,
  1538. .write = tracing_cpumask_write,
  1539. };
  1540. static ssize_t
  1541. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1542. size_t cnt, loff_t *ppos)
  1543. {
  1544. char *buf;
  1545. int r = 0;
  1546. int len = 0;
  1547. int i;
  1548. /* calulate max size */
  1549. for (i = 0; trace_options[i]; i++) {
  1550. len += strlen(trace_options[i]);
  1551. len += 3; /* "no" and space */
  1552. }
  1553. /* +2 for \n and \0 */
  1554. buf = kmalloc(len + 2, GFP_KERNEL);
  1555. if (!buf)
  1556. return -ENOMEM;
  1557. for (i = 0; trace_options[i]; i++) {
  1558. if (trace_flags & (1 << i))
  1559. r += sprintf(buf + r, "%s ", trace_options[i]);
  1560. else
  1561. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1562. }
  1563. r += sprintf(buf + r, "\n");
  1564. WARN_ON(r >= len + 2);
  1565. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1566. kfree(buf);
  1567. return r;
  1568. }
  1569. static ssize_t
  1570. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1571. size_t cnt, loff_t *ppos)
  1572. {
  1573. char buf[64];
  1574. char *cmp = buf;
  1575. int neg = 0;
  1576. int i;
  1577. if (cnt > 63)
  1578. cnt = 63;
  1579. if (copy_from_user(&buf, ubuf, cnt))
  1580. return -EFAULT;
  1581. buf[cnt] = 0;
  1582. if (strncmp(buf, "no", 2) == 0) {
  1583. neg = 1;
  1584. cmp += 2;
  1585. }
  1586. for (i = 0; trace_options[i]; i++) {
  1587. int len = strlen(trace_options[i]);
  1588. if (strncmp(cmp, trace_options[i], len) == 0) {
  1589. if (neg)
  1590. trace_flags &= ~(1 << i);
  1591. else
  1592. trace_flags |= (1 << i);
  1593. break;
  1594. }
  1595. }
  1596. /*
  1597. * If no option could be set, return an error:
  1598. */
  1599. if (!trace_options[i])
  1600. return -EINVAL;
  1601. filp->f_pos += cnt;
  1602. return cnt;
  1603. }
  1604. static struct file_operations tracing_iter_fops = {
  1605. .open = tracing_open_generic,
  1606. .read = tracing_iter_ctrl_read,
  1607. .write = tracing_iter_ctrl_write,
  1608. };
  1609. static const char readme_msg[] =
  1610. "tracing mini-HOWTO:\n\n"
  1611. "# mkdir /debug\n"
  1612. "# mount -t debugfs nodev /debug\n\n"
  1613. "# cat /debug/tracing/available_tracers\n"
  1614. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1615. "# cat /debug/tracing/current_tracer\n"
  1616. "none\n"
  1617. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1618. "# cat /debug/tracing/current_tracer\n"
  1619. "sched_switch\n"
  1620. "# cat /debug/tracing/iter_ctrl\n"
  1621. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1622. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1623. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1624. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1625. "echo 0 > /debug/tracing/tracing_enabled\n"
  1626. ;
  1627. static ssize_t
  1628. tracing_readme_read(struct file *filp, char __user *ubuf,
  1629. size_t cnt, loff_t *ppos)
  1630. {
  1631. return simple_read_from_buffer(ubuf, cnt, ppos,
  1632. readme_msg, strlen(readme_msg));
  1633. }
  1634. static struct file_operations tracing_readme_fops = {
  1635. .open = tracing_open_generic,
  1636. .read = tracing_readme_read,
  1637. };
  1638. static ssize_t
  1639. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1640. size_t cnt, loff_t *ppos)
  1641. {
  1642. struct trace_array *tr = filp->private_data;
  1643. char buf[64];
  1644. int r;
  1645. r = sprintf(buf, "%ld\n", tr->ctrl);
  1646. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1647. }
  1648. static ssize_t
  1649. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1650. size_t cnt, loff_t *ppos)
  1651. {
  1652. struct trace_array *tr = filp->private_data;
  1653. long val;
  1654. char buf[64];
  1655. if (cnt > 63)
  1656. cnt = 63;
  1657. if (copy_from_user(&buf, ubuf, cnt))
  1658. return -EFAULT;
  1659. buf[cnt] = 0;
  1660. val = simple_strtoul(buf, NULL, 10);
  1661. val = !!val;
  1662. mutex_lock(&trace_types_lock);
  1663. if (tr->ctrl ^ val) {
  1664. if (val)
  1665. tracer_enabled = 1;
  1666. else
  1667. tracer_enabled = 0;
  1668. tr->ctrl = val;
  1669. if (current_trace && current_trace->ctrl_update)
  1670. current_trace->ctrl_update(tr);
  1671. }
  1672. mutex_unlock(&trace_types_lock);
  1673. filp->f_pos += cnt;
  1674. return cnt;
  1675. }
  1676. static ssize_t
  1677. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1678. size_t cnt, loff_t *ppos)
  1679. {
  1680. char buf[max_tracer_type_len+2];
  1681. int r;
  1682. mutex_lock(&trace_types_lock);
  1683. if (current_trace)
  1684. r = sprintf(buf, "%s\n", current_trace->name);
  1685. else
  1686. r = sprintf(buf, "\n");
  1687. mutex_unlock(&trace_types_lock);
  1688. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1689. }
  1690. static ssize_t
  1691. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1692. size_t cnt, loff_t *ppos)
  1693. {
  1694. struct trace_array *tr = &global_trace;
  1695. struct tracer *t;
  1696. char buf[max_tracer_type_len+1];
  1697. int i;
  1698. if (cnt > max_tracer_type_len)
  1699. cnt = max_tracer_type_len;
  1700. if (copy_from_user(&buf, ubuf, cnt))
  1701. return -EFAULT;
  1702. buf[cnt] = 0;
  1703. /* strip ending whitespace. */
  1704. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1705. buf[i] = 0;
  1706. mutex_lock(&trace_types_lock);
  1707. for (t = trace_types; t; t = t->next) {
  1708. if (strcmp(t->name, buf) == 0)
  1709. break;
  1710. }
  1711. if (!t || t == current_trace)
  1712. goto out;
  1713. if (current_trace && current_trace->reset)
  1714. current_trace->reset(tr);
  1715. current_trace = t;
  1716. if (t->init)
  1717. t->init(tr);
  1718. out:
  1719. mutex_unlock(&trace_types_lock);
  1720. filp->f_pos += cnt;
  1721. return cnt;
  1722. }
  1723. static ssize_t
  1724. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1725. size_t cnt, loff_t *ppos)
  1726. {
  1727. unsigned long *ptr = filp->private_data;
  1728. char buf[64];
  1729. int r;
  1730. r = snprintf(buf, 64, "%ld\n",
  1731. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1732. if (r > 64)
  1733. r = 64;
  1734. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1735. }
  1736. static ssize_t
  1737. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1738. size_t cnt, loff_t *ppos)
  1739. {
  1740. long *ptr = filp->private_data;
  1741. long val;
  1742. char buf[64];
  1743. if (cnt > 63)
  1744. cnt = 63;
  1745. if (copy_from_user(&buf, ubuf, cnt))
  1746. return -EFAULT;
  1747. buf[cnt] = 0;
  1748. val = simple_strtoul(buf, NULL, 10);
  1749. *ptr = val * 1000;
  1750. return cnt;
  1751. }
  1752. static atomic_t tracing_reader;
  1753. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1754. {
  1755. struct trace_iterator *iter;
  1756. if (tracing_disabled)
  1757. return -ENODEV;
  1758. /* We only allow for reader of the pipe */
  1759. if (atomic_inc_return(&tracing_reader) != 1) {
  1760. atomic_dec(&tracing_reader);
  1761. return -EBUSY;
  1762. }
  1763. /* create a buffer to store the information to pass to userspace */
  1764. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1765. if (!iter)
  1766. return -ENOMEM;
  1767. iter->tr = &global_trace;
  1768. filp->private_data = iter;
  1769. return 0;
  1770. }
  1771. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1772. {
  1773. struct trace_iterator *iter = file->private_data;
  1774. kfree(iter);
  1775. atomic_dec(&tracing_reader);
  1776. return 0;
  1777. }
  1778. static unsigned int
  1779. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1780. {
  1781. struct trace_iterator *iter = filp->private_data;
  1782. if (trace_flags & TRACE_ITER_BLOCK) {
  1783. /*
  1784. * Always select as readable when in blocking mode
  1785. */
  1786. return POLLIN | POLLRDNORM;
  1787. }
  1788. else {
  1789. if (!trace_empty(iter))
  1790. return POLLIN | POLLRDNORM;
  1791. poll_wait(filp, &trace_wait, poll_table);
  1792. if (!trace_empty(iter))
  1793. return POLLIN | POLLRDNORM;
  1794. return 0;
  1795. }
  1796. }
  1797. /*
  1798. * Consumer reader.
  1799. */
  1800. static ssize_t
  1801. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1802. size_t cnt, loff_t *ppos)
  1803. {
  1804. struct trace_iterator *iter = filp->private_data;
  1805. struct trace_array_cpu *data;
  1806. static cpumask_t mask;
  1807. static int start;
  1808. unsigned long flags;
  1809. #ifdef CONFIG_FTRACE
  1810. int ftrace_save;
  1811. #endif
  1812. int read = 0;
  1813. int cpu;
  1814. int len;
  1815. int ret;
  1816. /* return any leftover data */
  1817. if (iter->seq.len > start) {
  1818. len = iter->seq.len - start;
  1819. if (cnt > len)
  1820. cnt = len;
  1821. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1822. if (ret)
  1823. cnt = -EFAULT;
  1824. start += len;
  1825. return cnt;
  1826. }
  1827. trace_seq_reset(&iter->seq);
  1828. start = 0;
  1829. while (trace_empty(iter)) {
  1830. if (!(trace_flags & TRACE_ITER_BLOCK))
  1831. return -EWOULDBLOCK;
  1832. /*
  1833. * This is a make-shift waitqueue. The reason we don't use
  1834. * an actual wait queue is because:
  1835. * 1) we only ever have one waiter
  1836. * 2) the tracing, traces all functions, we don't want
  1837. * the overhead of calling wake_up and friends
  1838. * (and tracing them too)
  1839. * Anyway, this is really very primitive wakeup.
  1840. */
  1841. set_current_state(TASK_INTERRUPTIBLE);
  1842. iter->tr->waiter = current;
  1843. /* sleep for one second, and try again. */
  1844. schedule_timeout(HZ);
  1845. iter->tr->waiter = NULL;
  1846. if (signal_pending(current))
  1847. return -EINTR;
  1848. /*
  1849. * We block until we read something and tracing is disabled.
  1850. * We still block if tracing is disabled, but we have never
  1851. * read anything. This allows a user to cat this file, and
  1852. * then enable tracing. But after we have read something,
  1853. * we give an EOF when tracing is again disabled.
  1854. *
  1855. * iter->pos will be 0 if we haven't read anything.
  1856. */
  1857. if (!tracer_enabled && iter->pos)
  1858. break;
  1859. continue;
  1860. }
  1861. /* stop when tracing is finished */
  1862. if (trace_empty(iter))
  1863. return 0;
  1864. if (cnt >= PAGE_SIZE)
  1865. cnt = PAGE_SIZE - 1;
  1866. memset(iter, 0, sizeof(*iter));
  1867. iter->tr = &global_trace;
  1868. iter->pos = -1;
  1869. /*
  1870. * We need to stop all tracing on all CPUS to read the
  1871. * the next buffer. This is a bit expensive, but is
  1872. * not done often. We fill all what we can read,
  1873. * and then release the locks again.
  1874. */
  1875. cpus_clear(mask);
  1876. local_irq_save(flags);
  1877. #ifdef CONFIG_FTRACE
  1878. ftrace_save = ftrace_enabled;
  1879. ftrace_enabled = 0;
  1880. #endif
  1881. smp_wmb();
  1882. for_each_possible_cpu(cpu) {
  1883. data = iter->tr->data[cpu];
  1884. if (!head_page(data) || !data->trace_idx)
  1885. continue;
  1886. atomic_inc(&data->disabled);
  1887. cpu_set(cpu, mask);
  1888. }
  1889. for_each_cpu_mask(cpu, mask) {
  1890. data = iter->tr->data[cpu];
  1891. spin_lock(&data->lock);
  1892. }
  1893. while (find_next_entry_inc(iter) != NULL) {
  1894. int len = iter->seq.len;
  1895. ret = print_trace_line(iter);
  1896. if (!ret) {
  1897. /* don't print partial lines */
  1898. iter->seq.len = len;
  1899. break;
  1900. }
  1901. trace_consume(iter);
  1902. if (iter->seq.len >= cnt)
  1903. break;
  1904. }
  1905. for_each_cpu_mask(cpu, mask) {
  1906. data = iter->tr->data[cpu];
  1907. spin_unlock(&data->lock);
  1908. }
  1909. for_each_cpu_mask(cpu, mask) {
  1910. data = iter->tr->data[cpu];
  1911. atomic_dec(&data->disabled);
  1912. }
  1913. #ifdef CONFIG_FTRACE
  1914. ftrace_enabled = ftrace_save;
  1915. #endif
  1916. local_irq_restore(flags);
  1917. /* Now copy what we have to the user */
  1918. read = iter->seq.len;
  1919. if (read > cnt)
  1920. read = cnt;
  1921. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1922. if (read < iter->seq.len)
  1923. start = read;
  1924. else
  1925. trace_seq_reset(&iter->seq);
  1926. if (ret)
  1927. read = -EFAULT;
  1928. return read;
  1929. }
  1930. static struct file_operations tracing_max_lat_fops = {
  1931. .open = tracing_open_generic,
  1932. .read = tracing_max_lat_read,
  1933. .write = tracing_max_lat_write,
  1934. };
  1935. static struct file_operations tracing_ctrl_fops = {
  1936. .open = tracing_open_generic,
  1937. .read = tracing_ctrl_read,
  1938. .write = tracing_ctrl_write,
  1939. };
  1940. static struct file_operations set_tracer_fops = {
  1941. .open = tracing_open_generic,
  1942. .read = tracing_set_trace_read,
  1943. .write = tracing_set_trace_write,
  1944. };
  1945. static struct file_operations tracing_pipe_fops = {
  1946. .open = tracing_open_pipe,
  1947. .poll = tracing_poll_pipe,
  1948. .read = tracing_read_pipe,
  1949. .release = tracing_release_pipe,
  1950. };
  1951. #ifdef CONFIG_DYNAMIC_FTRACE
  1952. static ssize_t
  1953. tracing_read_long(struct file *filp, char __user *ubuf,
  1954. size_t cnt, loff_t *ppos)
  1955. {
  1956. unsigned long *p = filp->private_data;
  1957. char buf[64];
  1958. int r;
  1959. r = sprintf(buf, "%ld\n", *p);
  1960. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1961. }
  1962. static struct file_operations tracing_read_long_fops = {
  1963. .open = tracing_open_generic,
  1964. .read = tracing_read_long,
  1965. };
  1966. #endif
  1967. static struct dentry *d_tracer;
  1968. struct dentry *tracing_init_dentry(void)
  1969. {
  1970. static int once;
  1971. if (d_tracer)
  1972. return d_tracer;
  1973. d_tracer = debugfs_create_dir("tracing", NULL);
  1974. if (!d_tracer && !once) {
  1975. once = 1;
  1976. pr_warning("Could not create debugfs directory 'tracing'\n");
  1977. return NULL;
  1978. }
  1979. return d_tracer;
  1980. }
  1981. #ifdef CONFIG_FTRACE_SELFTEST
  1982. /* Let selftest have access to static functions in this file */
  1983. #include "trace_selftest.c"
  1984. #endif
  1985. static __init void tracer_init_debugfs(void)
  1986. {
  1987. struct dentry *d_tracer;
  1988. struct dentry *entry;
  1989. d_tracer = tracing_init_dentry();
  1990. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1991. &global_trace, &tracing_ctrl_fops);
  1992. if (!entry)
  1993. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1994. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1995. NULL, &tracing_iter_fops);
  1996. if (!entry)
  1997. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1998. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  1999. NULL, &tracing_cpumask_fops);
  2000. if (!entry)
  2001. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2002. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2003. &global_trace, &tracing_lt_fops);
  2004. if (!entry)
  2005. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2006. entry = debugfs_create_file("trace", 0444, d_tracer,
  2007. &global_trace, &tracing_fops);
  2008. if (!entry)
  2009. pr_warning("Could not create debugfs 'trace' entry\n");
  2010. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2011. &global_trace, &show_traces_fops);
  2012. if (!entry)
  2013. pr_warning("Could not create debugfs 'trace' entry\n");
  2014. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2015. &global_trace, &set_tracer_fops);
  2016. if (!entry)
  2017. pr_warning("Could not create debugfs 'trace' entry\n");
  2018. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2019. &tracing_max_latency,
  2020. &tracing_max_lat_fops);
  2021. if (!entry)
  2022. pr_warning("Could not create debugfs "
  2023. "'tracing_max_latency' entry\n");
  2024. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2025. &tracing_thresh, &tracing_max_lat_fops);
  2026. if (!entry)
  2027. pr_warning("Could not create debugfs "
  2028. "'tracing_threash' entry\n");
  2029. entry = debugfs_create_file("README", 0644, d_tracer,
  2030. NULL, &tracing_readme_fops);
  2031. if (!entry)
  2032. pr_warning("Could not create debugfs 'README' entry\n");
  2033. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2034. NULL, &tracing_pipe_fops);
  2035. if (!entry)
  2036. pr_warning("Could not create debugfs "
  2037. "'tracing_threash' entry\n");
  2038. #ifdef CONFIG_DYNAMIC_FTRACE
  2039. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2040. &ftrace_update_tot_cnt,
  2041. &tracing_read_long_fops);
  2042. if (!entry)
  2043. pr_warning("Could not create debugfs "
  2044. "'dyn_ftrace_total_info' entry\n");
  2045. #endif
  2046. }
  2047. /* dummy trace to disable tracing */
  2048. static struct tracer no_tracer __read_mostly =
  2049. {
  2050. .name = "none",
  2051. };
  2052. static int trace_alloc_page(void)
  2053. {
  2054. struct trace_array_cpu *data;
  2055. struct page *page, *tmp;
  2056. LIST_HEAD(pages);
  2057. void *array;
  2058. int i;
  2059. /* first allocate a page for each CPU */
  2060. for_each_possible_cpu(i) {
  2061. array = (void *)__get_free_page(GFP_KERNEL);
  2062. if (array == NULL) {
  2063. printk(KERN_ERR "tracer: failed to allocate page"
  2064. "for trace buffer!\n");
  2065. goto free_pages;
  2066. }
  2067. page = virt_to_page(array);
  2068. list_add(&page->lru, &pages);
  2069. /* Only allocate if we are actually using the max trace */
  2070. #ifdef CONFIG_TRACER_MAX_TRACE
  2071. array = (void *)__get_free_page(GFP_KERNEL);
  2072. if (array == NULL) {
  2073. printk(KERN_ERR "tracer: failed to allocate page"
  2074. "for trace buffer!\n");
  2075. goto free_pages;
  2076. }
  2077. page = virt_to_page(array);
  2078. list_add(&page->lru, &pages);
  2079. #endif
  2080. }
  2081. /* Now that we successfully allocate a page per CPU, add them */
  2082. for_each_possible_cpu(i) {
  2083. data = global_trace.data[i];
  2084. spin_lock_init(&data->lock);
  2085. lockdep_set_class(&data->lock, &data->lock_key);
  2086. page = list_entry(pages.next, struct page, lru);
  2087. list_del_init(&page->lru);
  2088. list_add_tail(&page->lru, &data->trace_pages);
  2089. ClearPageLRU(page);
  2090. #ifdef CONFIG_TRACER_MAX_TRACE
  2091. data = max_tr.data[i];
  2092. spin_lock_init(&data->lock);
  2093. lockdep_set_class(&data->lock, &data->lock_key);
  2094. page = list_entry(pages.next, struct page, lru);
  2095. list_del_init(&page->lru);
  2096. list_add_tail(&page->lru, &data->trace_pages);
  2097. SetPageLRU(page);
  2098. #endif
  2099. }
  2100. global_trace.entries += ENTRIES_PER_PAGE;
  2101. return 0;
  2102. free_pages:
  2103. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2104. list_del_init(&page->lru);
  2105. __free_page(page);
  2106. }
  2107. return -ENOMEM;
  2108. }
  2109. __init static int tracer_alloc_buffers(void)
  2110. {
  2111. struct trace_array_cpu *data;
  2112. void *array;
  2113. struct page *page;
  2114. int pages = 0;
  2115. int ret = -ENOMEM;
  2116. int i;
  2117. global_trace.ctrl = tracer_enabled;
  2118. /* Allocate the first page for all buffers */
  2119. for_each_possible_cpu(i) {
  2120. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2121. max_tr.data[i] = &per_cpu(max_data, i);
  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. /* set the array to the list */
  2129. INIT_LIST_HEAD(&data->trace_pages);
  2130. page = virt_to_page(array);
  2131. list_add(&page->lru, &data->trace_pages);
  2132. /* use the LRU flag to differentiate the two buffers */
  2133. ClearPageLRU(page);
  2134. /* Only allocate if we are actually using the max trace */
  2135. #ifdef CONFIG_TRACER_MAX_TRACE
  2136. array = (void *)__get_free_page(GFP_KERNEL);
  2137. if (array == NULL) {
  2138. printk(KERN_ERR "tracer: failed to allocate page"
  2139. "for trace buffer!\n");
  2140. goto free_buffers;
  2141. }
  2142. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2143. page = virt_to_page(array);
  2144. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2145. SetPageLRU(page);
  2146. #endif
  2147. }
  2148. /*
  2149. * Since we allocate by orders of pages, we may be able to
  2150. * round up a bit.
  2151. */
  2152. global_trace.entries = ENTRIES_PER_PAGE;
  2153. pages++;
  2154. while (global_trace.entries < trace_nr_entries) {
  2155. if (trace_alloc_page())
  2156. break;
  2157. pages++;
  2158. }
  2159. max_tr.entries = global_trace.entries;
  2160. pr_info("tracer: %d pages allocated for %ld",
  2161. pages, trace_nr_entries);
  2162. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2163. pr_info(" actual entries %ld\n", global_trace.entries);
  2164. tracer_init_debugfs();
  2165. trace_init_cmdlines();
  2166. register_tracer(&no_tracer);
  2167. current_trace = &no_tracer;
  2168. /* All seems OK, enable tracing */
  2169. tracing_disabled = 0;
  2170. return 0;
  2171. free_buffers:
  2172. for (i-- ; i >= 0; i--) {
  2173. struct page *page, *tmp;
  2174. struct trace_array_cpu *data = global_trace.data[i];
  2175. if (data) {
  2176. list_for_each_entry_safe(page, tmp,
  2177. &data->trace_pages, lru) {
  2178. list_del_init(&page->lru);
  2179. __free_page(page);
  2180. }
  2181. }
  2182. #ifdef CONFIG_TRACER_MAX_TRACE
  2183. data = max_tr.data[i];
  2184. if (data) {
  2185. list_for_each_entry_safe(page, tmp,
  2186. &data->trace_pages, lru) {
  2187. list_del_init(&page->lru);
  2188. __free_page(page);
  2189. }
  2190. }
  2191. #endif
  2192. }
  2193. return ret;
  2194. }
  2195. fs_initcall(tracer_alloc_buffers);