trace.c 59 KB

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