trace.c 59 KB

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