trace.c 62 KB

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