trace.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369
  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/kprobes.h>
  31. #include <linux/writeback.h>
  32. #include <linux/stacktrace.h>
  33. #include "trace.h"
  34. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  35. unsigned long __read_mostly tracing_thresh;
  36. static unsigned long __read_mostly tracing_nr_buffers;
  37. static cpumask_t __read_mostly tracing_buffer_mask;
  38. #define for_each_tracing_cpu(cpu) \
  39. for_each_cpu_mask(cpu, tracing_buffer_mask)
  40. static int trace_alloc_page(void);
  41. static int trace_free_page(void);
  42. static int tracing_disabled = 1;
  43. static unsigned long tracing_pages_allocated;
  44. long
  45. ns2usecs(cycle_t nsec)
  46. {
  47. nsec += 500;
  48. do_div(nsec, 1000);
  49. return nsec;
  50. }
  51. cycle_t ftrace_now(int cpu)
  52. {
  53. return cpu_clock(cpu);
  54. }
  55. /*
  56. * The global_trace is the descriptor that holds the tracing
  57. * buffers for the live tracing. For each CPU, it contains
  58. * a link list of pages that will store trace entries. The
  59. * page descriptor of the pages in the memory is used to hold
  60. * the link list by linking the lru item in the page descriptor
  61. * to each of the pages in the buffer per CPU.
  62. *
  63. * For each active CPU there is a data field that holds the
  64. * pages for the buffer for that CPU. Each CPU has the same number
  65. * of pages allocated for its buffer.
  66. */
  67. static struct trace_array global_trace;
  68. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  69. /*
  70. * The max_tr is used to snapshot the global_trace when a maximum
  71. * latency is reached. Some tracers will use this to store a maximum
  72. * trace while it continues examining live traces.
  73. *
  74. * The buffers for the max_tr are set up the same as the global_trace.
  75. * When a snapshot is taken, the link list of the max_tr is swapped
  76. * with the link list of the global_trace and the buffers are reset for
  77. * the global_trace so the tracing can continue.
  78. */
  79. static struct trace_array max_tr;
  80. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  81. /* tracer_enabled is used to toggle activation of a tracer */
  82. static int tracer_enabled = 1;
  83. /* function tracing enabled */
  84. int ftrace_function_enabled;
  85. /*
  86. * trace_nr_entries is the number of entries that is allocated
  87. * for a buffer. Note, the number of entries is always rounded
  88. * to ENTRIES_PER_PAGE.
  89. */
  90. static unsigned long trace_nr_entries = 65536UL;
  91. /* trace_types holds a link list of available tracers. */
  92. static struct tracer *trace_types __read_mostly;
  93. /* current_trace points to the tracer that is currently active */
  94. static struct tracer *current_trace __read_mostly;
  95. /*
  96. * max_tracer_type_len is used to simplify the allocating of
  97. * buffers to read userspace tracer names. We keep track of
  98. * the longest tracer name registered.
  99. */
  100. static int max_tracer_type_len;
  101. /*
  102. * trace_types_lock is used to protect the trace_types list.
  103. * This lock is also used to keep user access serialized.
  104. * Accesses from userspace will grab this lock while userspace
  105. * activities happen inside the kernel.
  106. */
  107. static DEFINE_MUTEX(trace_types_lock);
  108. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  109. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  110. /* trace_flags holds iter_ctrl options */
  111. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  112. static notrace void no_trace_init(struct trace_array *tr)
  113. {
  114. int cpu;
  115. ftrace_function_enabled = 0;
  116. if(tr->ctrl)
  117. for_each_online_cpu(cpu)
  118. tracing_reset(tr->data[cpu]);
  119. tracer_enabled = 0;
  120. }
  121. /* dummy trace to disable tracing */
  122. static struct tracer no_tracer __read_mostly = {
  123. .name = "none",
  124. .init = no_trace_init
  125. };
  126. /**
  127. * trace_wake_up - wake up tasks waiting for trace input
  128. *
  129. * Simply wakes up any task that is blocked on the trace_wait
  130. * queue. These is used with trace_poll for tasks polling the trace.
  131. */
  132. void trace_wake_up(void)
  133. {
  134. /*
  135. * The runqueue_is_locked() can fail, but this is the best we
  136. * have for now:
  137. */
  138. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  139. wake_up(&trace_wait);
  140. }
  141. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  142. static int __init set_nr_entries(char *str)
  143. {
  144. unsigned long nr_entries;
  145. int ret;
  146. if (!str)
  147. return 0;
  148. ret = strict_strtoul(str, 0, &nr_entries);
  149. /* nr_entries can not be zero */
  150. if (ret < 0 || nr_entries == 0)
  151. return 0;
  152. trace_nr_entries = nr_entries;
  153. return 1;
  154. }
  155. __setup("trace_entries=", set_nr_entries);
  156. unsigned long nsecs_to_usecs(unsigned long nsecs)
  157. {
  158. return nsecs / 1000;
  159. }
  160. /*
  161. * trace_flag_type is an enumeration that holds different
  162. * states when a trace occurs. These are:
  163. * IRQS_OFF - interrupts were disabled
  164. * NEED_RESCED - reschedule is requested
  165. * HARDIRQ - inside an interrupt handler
  166. * SOFTIRQ - inside a softirq handler
  167. * CONT - multiple entries hold the trace item
  168. */
  169. enum trace_flag_type {
  170. TRACE_FLAG_IRQS_OFF = 0x01,
  171. TRACE_FLAG_NEED_RESCHED = 0x02,
  172. TRACE_FLAG_HARDIRQ = 0x04,
  173. TRACE_FLAG_SOFTIRQ = 0x08,
  174. TRACE_FLAG_CONT = 0x10,
  175. };
  176. /*
  177. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  178. * control the output of kernel symbols.
  179. */
  180. #define TRACE_ITER_SYM_MASK \
  181. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  182. /* These must match the bit postions in trace_iterator_flags */
  183. static const char *trace_options[] = {
  184. "print-parent",
  185. "sym-offset",
  186. "sym-addr",
  187. "verbose",
  188. "raw",
  189. "hex",
  190. "bin",
  191. "block",
  192. "stacktrace",
  193. "sched-tree",
  194. NULL
  195. };
  196. /*
  197. * ftrace_max_lock is used to protect the swapping of buffers
  198. * when taking a max snapshot. The buffers themselves are
  199. * protected by per_cpu spinlocks. But the action of the swap
  200. * needs its own lock.
  201. *
  202. * This is defined as a raw_spinlock_t in order to help
  203. * with performance when lockdep debugging is enabled.
  204. */
  205. static raw_spinlock_t ftrace_max_lock =
  206. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  207. /*
  208. * Copy the new maximum trace into the separate maximum-trace
  209. * structure. (this way the maximum trace is permanently saved,
  210. * for later retrieval via /debugfs/tracing/latency_trace)
  211. */
  212. static void
  213. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  214. {
  215. struct trace_array_cpu *data = tr->data[cpu];
  216. max_tr.cpu = cpu;
  217. max_tr.time_start = data->preempt_timestamp;
  218. data = max_tr.data[cpu];
  219. data->saved_latency = tracing_max_latency;
  220. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  221. data->pid = tsk->pid;
  222. data->uid = tsk->uid;
  223. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  224. data->policy = tsk->policy;
  225. data->rt_priority = tsk->rt_priority;
  226. /* record this tasks comm */
  227. tracing_record_cmdline(current);
  228. }
  229. #define CHECK_COND(cond) \
  230. if (unlikely(cond)) { \
  231. tracing_disabled = 1; \
  232. WARN_ON(1); \
  233. return -1; \
  234. }
  235. /**
  236. * check_pages - integrity check of trace buffers
  237. *
  238. * As a safty measure we check to make sure the data pages have not
  239. * been corrupted.
  240. */
  241. int check_pages(struct trace_array_cpu *data)
  242. {
  243. struct page *page, *tmp;
  244. CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
  245. CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
  246. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  247. CHECK_COND(page->lru.next->prev != &page->lru);
  248. CHECK_COND(page->lru.prev->next != &page->lru);
  249. }
  250. return 0;
  251. }
  252. /**
  253. * head_page - page address of the first page in per_cpu buffer.
  254. *
  255. * head_page returns the page address of the first page in
  256. * a per_cpu buffer. This also preforms various consistency
  257. * checks to make sure the buffer has not been corrupted.
  258. */
  259. void *head_page(struct trace_array_cpu *data)
  260. {
  261. struct page *page;
  262. if (list_empty(&data->trace_pages))
  263. return NULL;
  264. page = list_entry(data->trace_pages.next, struct page, lru);
  265. BUG_ON(&page->lru == &data->trace_pages);
  266. return page_address(page);
  267. }
  268. /**
  269. * trace_seq_printf - sequence printing of trace information
  270. * @s: trace sequence descriptor
  271. * @fmt: printf format string
  272. *
  273. * The tracer may use either sequence operations or its own
  274. * copy to user routines. To simplify formating of a trace
  275. * trace_seq_printf is used to store strings into a special
  276. * buffer (@s). Then the output may be either used by
  277. * the sequencer or pulled into another buffer.
  278. */
  279. int
  280. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  281. {
  282. int len = (PAGE_SIZE - 1) - s->len;
  283. va_list ap;
  284. int ret;
  285. if (!len)
  286. return 0;
  287. va_start(ap, fmt);
  288. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  289. va_end(ap);
  290. /* If we can't write it all, don't bother writing anything */
  291. if (ret >= len)
  292. return 0;
  293. s->len += ret;
  294. return len;
  295. }
  296. /**
  297. * trace_seq_puts - trace sequence printing of simple string
  298. * @s: trace sequence descriptor
  299. * @str: simple string to record
  300. *
  301. * The tracer may use either the sequence operations or its own
  302. * copy to user routines. This function records a simple string
  303. * into a special buffer (@s) for later retrieval by a sequencer
  304. * or other mechanism.
  305. */
  306. static int
  307. trace_seq_puts(struct trace_seq *s, const char *str)
  308. {
  309. int len = strlen(str);
  310. if (len > ((PAGE_SIZE - 1) - s->len))
  311. return 0;
  312. memcpy(s->buffer + s->len, str, len);
  313. s->len += len;
  314. return len;
  315. }
  316. static int
  317. trace_seq_putc(struct trace_seq *s, unsigned char c)
  318. {
  319. if (s->len >= (PAGE_SIZE - 1))
  320. return 0;
  321. s->buffer[s->len++] = c;
  322. return 1;
  323. }
  324. static int
  325. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  326. {
  327. if (len > ((PAGE_SIZE - 1) - s->len))
  328. return 0;
  329. memcpy(s->buffer + s->len, mem, len);
  330. s->len += len;
  331. return len;
  332. }
  333. #define HEX_CHARS 17
  334. static const char hex2asc[] = "0123456789abcdef";
  335. static int
  336. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  337. {
  338. unsigned char hex[HEX_CHARS];
  339. unsigned char *data = mem;
  340. unsigned char byte;
  341. int i, j;
  342. BUG_ON(len >= HEX_CHARS);
  343. #ifdef __BIG_ENDIAN
  344. for (i = 0, j = 0; i < len; i++) {
  345. #else
  346. for (i = len-1, j = 0; i >= 0; i--) {
  347. #endif
  348. byte = data[i];
  349. hex[j++] = hex2asc[byte & 0x0f];
  350. hex[j++] = hex2asc[byte >> 4];
  351. }
  352. hex[j++] = ' ';
  353. return trace_seq_putmem(s, hex, j);
  354. }
  355. static void
  356. trace_seq_reset(struct trace_seq *s)
  357. {
  358. s->len = 0;
  359. s->readpos = 0;
  360. }
  361. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  362. {
  363. int len;
  364. int ret;
  365. if (s->len <= s->readpos)
  366. return -EBUSY;
  367. len = s->len - s->readpos;
  368. if (cnt > len)
  369. cnt = len;
  370. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  371. if (ret)
  372. return -EFAULT;
  373. s->readpos += len;
  374. return cnt;
  375. }
  376. static void
  377. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  378. {
  379. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  380. s->buffer[len] = 0;
  381. seq_puts(m, s->buffer);
  382. trace_seq_reset(s);
  383. }
  384. /*
  385. * flip the trace buffers between two trace descriptors.
  386. * This usually is the buffers between the global_trace and
  387. * the max_tr to record a snapshot of a current trace.
  388. *
  389. * The ftrace_max_lock must be held.
  390. */
  391. static void
  392. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  393. {
  394. struct list_head flip_pages;
  395. INIT_LIST_HEAD(&flip_pages);
  396. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  397. sizeof(struct trace_array_cpu) -
  398. offsetof(struct trace_array_cpu, trace_head_idx));
  399. check_pages(tr1);
  400. check_pages(tr2);
  401. list_splice_init(&tr1->trace_pages, &flip_pages);
  402. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  403. list_splice_init(&flip_pages, &tr2->trace_pages);
  404. BUG_ON(!list_empty(&flip_pages));
  405. check_pages(tr1);
  406. check_pages(tr2);
  407. }
  408. /**
  409. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  410. * @tr: tracer
  411. * @tsk: the task with the latency
  412. * @cpu: The cpu that initiated the trace.
  413. *
  414. * Flip the buffers between the @tr and the max_tr and record information
  415. * about which task was the cause of this latency.
  416. */
  417. void
  418. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  419. {
  420. struct trace_array_cpu *data;
  421. int i;
  422. WARN_ON_ONCE(!irqs_disabled());
  423. __raw_spin_lock(&ftrace_max_lock);
  424. /* clear out all the previous traces */
  425. for_each_tracing_cpu(i) {
  426. data = tr->data[i];
  427. flip_trace(max_tr.data[i], data);
  428. tracing_reset(data);
  429. }
  430. __update_max_tr(tr, tsk, cpu);
  431. __raw_spin_unlock(&ftrace_max_lock);
  432. }
  433. /**
  434. * update_max_tr_single - only copy one trace over, and reset the rest
  435. * @tr - tracer
  436. * @tsk - task with the latency
  437. * @cpu - the cpu of the buffer to copy.
  438. *
  439. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  440. */
  441. void
  442. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  443. {
  444. struct trace_array_cpu *data = tr->data[cpu];
  445. int i;
  446. WARN_ON_ONCE(!irqs_disabled());
  447. __raw_spin_lock(&ftrace_max_lock);
  448. for_each_tracing_cpu(i)
  449. tracing_reset(max_tr.data[i]);
  450. flip_trace(max_tr.data[cpu], data);
  451. tracing_reset(data);
  452. __update_max_tr(tr, tsk, cpu);
  453. __raw_spin_unlock(&ftrace_max_lock);
  454. }
  455. /**
  456. * register_tracer - register a tracer with the ftrace system.
  457. * @type - the plugin for the tracer
  458. *
  459. * Register a new plugin tracer.
  460. */
  461. int register_tracer(struct tracer *type)
  462. {
  463. struct tracer *t;
  464. int len;
  465. int ret = 0;
  466. if (!type->name) {
  467. pr_info("Tracer must have a name\n");
  468. return -1;
  469. }
  470. mutex_lock(&trace_types_lock);
  471. for (t = trace_types; t; t = t->next) {
  472. if (strcmp(type->name, t->name) == 0) {
  473. /* already found */
  474. pr_info("Trace %s already registered\n",
  475. type->name);
  476. ret = -1;
  477. goto out;
  478. }
  479. }
  480. #ifdef CONFIG_FTRACE_STARTUP_TEST
  481. if (type->selftest) {
  482. struct tracer *saved_tracer = current_trace;
  483. struct trace_array_cpu *data;
  484. struct trace_array *tr = &global_trace;
  485. int saved_ctrl = tr->ctrl;
  486. int i;
  487. /*
  488. * Run a selftest on this tracer.
  489. * Here we reset the trace buffer, and set the current
  490. * tracer to be this tracer. The tracer can then run some
  491. * internal tracing to verify that everything is in order.
  492. * If we fail, we do not register this tracer.
  493. */
  494. for_each_tracing_cpu(i) {
  495. data = tr->data[i];
  496. if (!head_page(data))
  497. continue;
  498. tracing_reset(data);
  499. }
  500. current_trace = type;
  501. tr->ctrl = 0;
  502. /* the test is responsible for initializing and enabling */
  503. pr_info("Testing tracer %s: ", type->name);
  504. ret = type->selftest(type, tr);
  505. /* the test is responsible for resetting too */
  506. current_trace = saved_tracer;
  507. tr->ctrl = saved_ctrl;
  508. if (ret) {
  509. printk(KERN_CONT "FAILED!\n");
  510. goto out;
  511. }
  512. /* Only reset on passing, to avoid touching corrupted buffers */
  513. for_each_tracing_cpu(i) {
  514. data = tr->data[i];
  515. if (!head_page(data))
  516. continue;
  517. tracing_reset(data);
  518. }
  519. printk(KERN_CONT "PASSED\n");
  520. }
  521. #endif
  522. type->next = trace_types;
  523. trace_types = type;
  524. len = strlen(type->name);
  525. if (len > max_tracer_type_len)
  526. max_tracer_type_len = len;
  527. out:
  528. mutex_unlock(&trace_types_lock);
  529. return ret;
  530. }
  531. void unregister_tracer(struct tracer *type)
  532. {
  533. struct tracer **t;
  534. int len;
  535. mutex_lock(&trace_types_lock);
  536. for (t = &trace_types; *t; t = &(*t)->next) {
  537. if (*t == type)
  538. goto found;
  539. }
  540. pr_info("Trace %s not registered\n", type->name);
  541. goto out;
  542. found:
  543. *t = (*t)->next;
  544. if (strlen(type->name) != max_tracer_type_len)
  545. goto out;
  546. max_tracer_type_len = 0;
  547. for (t = &trace_types; *t; t = &(*t)->next) {
  548. len = strlen((*t)->name);
  549. if (len > max_tracer_type_len)
  550. max_tracer_type_len = len;
  551. }
  552. out:
  553. mutex_unlock(&trace_types_lock);
  554. }
  555. void tracing_reset(struct trace_array_cpu *data)
  556. {
  557. data->trace_idx = 0;
  558. data->overrun = 0;
  559. data->trace_head = data->trace_tail = head_page(data);
  560. data->trace_head_idx = 0;
  561. data->trace_tail_idx = 0;
  562. }
  563. #define SAVED_CMDLINES 128
  564. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  565. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  566. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  567. static int cmdline_idx;
  568. static DEFINE_SPINLOCK(trace_cmdline_lock);
  569. /* temporary disable recording */
  570. atomic_t trace_record_cmdline_disabled __read_mostly;
  571. static void trace_init_cmdlines(void)
  572. {
  573. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  574. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  575. cmdline_idx = 0;
  576. }
  577. void trace_stop_cmdline_recording(void);
  578. static void trace_save_cmdline(struct task_struct *tsk)
  579. {
  580. unsigned map;
  581. unsigned idx;
  582. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  583. return;
  584. /*
  585. * It's not the end of the world if we don't get
  586. * the lock, but we also don't want to spin
  587. * nor do we want to disable interrupts,
  588. * so if we miss here, then better luck next time.
  589. */
  590. if (!spin_trylock(&trace_cmdline_lock))
  591. return;
  592. idx = map_pid_to_cmdline[tsk->pid];
  593. if (idx >= SAVED_CMDLINES) {
  594. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  595. map = map_cmdline_to_pid[idx];
  596. if (map <= PID_MAX_DEFAULT)
  597. map_pid_to_cmdline[map] = (unsigned)-1;
  598. map_pid_to_cmdline[tsk->pid] = idx;
  599. cmdline_idx = idx;
  600. }
  601. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  602. spin_unlock(&trace_cmdline_lock);
  603. }
  604. static char *trace_find_cmdline(int pid)
  605. {
  606. char *cmdline = "<...>";
  607. unsigned map;
  608. if (!pid)
  609. return "<idle>";
  610. if (pid > PID_MAX_DEFAULT)
  611. goto out;
  612. map = map_pid_to_cmdline[pid];
  613. if (map >= SAVED_CMDLINES)
  614. goto out;
  615. cmdline = saved_cmdlines[map];
  616. out:
  617. return cmdline;
  618. }
  619. void tracing_record_cmdline(struct task_struct *tsk)
  620. {
  621. if (atomic_read(&trace_record_cmdline_disabled))
  622. return;
  623. trace_save_cmdline(tsk);
  624. }
  625. static inline struct list_head *
  626. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  627. {
  628. /*
  629. * Roundrobin - but skip the head (which is not a real page):
  630. */
  631. next = next->next;
  632. if (unlikely(next == &data->trace_pages))
  633. next = next->next;
  634. BUG_ON(next == &data->trace_pages);
  635. return next;
  636. }
  637. static inline void *
  638. trace_next_page(struct trace_array_cpu *data, void *addr)
  639. {
  640. struct list_head *next;
  641. struct page *page;
  642. page = virt_to_page(addr);
  643. next = trace_next_list(data, &page->lru);
  644. page = list_entry(next, struct page, lru);
  645. return page_address(page);
  646. }
  647. static inline struct trace_entry *
  648. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  649. {
  650. unsigned long idx, idx_next;
  651. struct trace_entry *entry;
  652. data->trace_idx++;
  653. idx = data->trace_head_idx;
  654. idx_next = idx + 1;
  655. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  656. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  657. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  658. data->trace_head = trace_next_page(data, data->trace_head);
  659. idx_next = 0;
  660. }
  661. if (data->trace_head == data->trace_tail &&
  662. idx_next == data->trace_tail_idx) {
  663. /* overrun */
  664. data->overrun++;
  665. data->trace_tail_idx++;
  666. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  667. data->trace_tail =
  668. trace_next_page(data, data->trace_tail);
  669. data->trace_tail_idx = 0;
  670. }
  671. }
  672. data->trace_head_idx = idx_next;
  673. return entry;
  674. }
  675. static inline void
  676. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  677. {
  678. struct task_struct *tsk = current;
  679. unsigned long pc;
  680. pc = preempt_count();
  681. entry->field.preempt_count = pc & 0xff;
  682. entry->field.pid = (tsk) ? tsk->pid : 0;
  683. entry->field.t = ftrace_now(raw_smp_processor_id());
  684. entry->field.flags =
  685. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  686. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  687. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  688. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  689. }
  690. void
  691. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  692. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  693. {
  694. struct trace_entry *entry;
  695. unsigned long irq_flags;
  696. raw_local_irq_save(irq_flags);
  697. __raw_spin_lock(&data->lock);
  698. entry = tracing_get_trace_entry(tr, data);
  699. tracing_generic_entry_update(entry, flags);
  700. entry->type = TRACE_FN;
  701. entry->field.fn.ip = ip;
  702. entry->field.fn.parent_ip = parent_ip;
  703. __raw_spin_unlock(&data->lock);
  704. raw_local_irq_restore(irq_flags);
  705. }
  706. void
  707. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  708. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  709. {
  710. if (likely(!atomic_read(&data->disabled)))
  711. trace_function(tr, data, ip, parent_ip, flags);
  712. }
  713. #ifdef CONFIG_MMIOTRACE
  714. void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
  715. struct mmiotrace_rw *rw)
  716. {
  717. struct trace_entry *entry;
  718. unsigned long irq_flags;
  719. raw_local_irq_save(irq_flags);
  720. __raw_spin_lock(&data->lock);
  721. entry = tracing_get_trace_entry(tr, data);
  722. tracing_generic_entry_update(entry, 0);
  723. entry->type = TRACE_MMIO_RW;
  724. entry->field.mmiorw = *rw;
  725. __raw_spin_unlock(&data->lock);
  726. raw_local_irq_restore(irq_flags);
  727. trace_wake_up();
  728. }
  729. void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
  730. struct mmiotrace_map *map)
  731. {
  732. struct trace_entry *entry;
  733. unsigned long irq_flags;
  734. raw_local_irq_save(irq_flags);
  735. __raw_spin_lock(&data->lock);
  736. entry = tracing_get_trace_entry(tr, data);
  737. tracing_generic_entry_update(entry, 0);
  738. entry->type = TRACE_MMIO_MAP;
  739. entry->field.mmiomap = *map;
  740. __raw_spin_unlock(&data->lock);
  741. raw_local_irq_restore(irq_flags);
  742. trace_wake_up();
  743. }
  744. #endif
  745. void __trace_stack(struct trace_array *tr,
  746. struct trace_array_cpu *data,
  747. unsigned long flags,
  748. int skip)
  749. {
  750. struct trace_entry *entry;
  751. struct stack_trace trace;
  752. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  753. return;
  754. entry = tracing_get_trace_entry(tr, data);
  755. tracing_generic_entry_update(entry, flags);
  756. entry->type = TRACE_STACK;
  757. memset(&entry->field.stack, 0, sizeof(entry->field.stack));
  758. trace.nr_entries = 0;
  759. trace.max_entries = FTRACE_STACK_ENTRIES;
  760. trace.skip = skip;
  761. trace.entries = entry->field.stack.caller;
  762. save_stack_trace(&trace);
  763. }
  764. void
  765. __trace_special(void *__tr, void *__data,
  766. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  767. {
  768. struct trace_array_cpu *data = __data;
  769. struct trace_array *tr = __tr;
  770. struct trace_entry *entry;
  771. unsigned long irq_flags;
  772. raw_local_irq_save(irq_flags);
  773. __raw_spin_lock(&data->lock);
  774. entry = tracing_get_trace_entry(tr, data);
  775. tracing_generic_entry_update(entry, 0);
  776. entry->type = TRACE_SPECIAL;
  777. entry->field.special.arg1 = arg1;
  778. entry->field.special.arg2 = arg2;
  779. entry->field.special.arg3 = arg3;
  780. __trace_stack(tr, data, irq_flags, 4);
  781. __raw_spin_unlock(&data->lock);
  782. raw_local_irq_restore(irq_flags);
  783. trace_wake_up();
  784. }
  785. void
  786. tracing_sched_switch_trace(struct trace_array *tr,
  787. struct trace_array_cpu *data,
  788. struct task_struct *prev,
  789. struct task_struct *next,
  790. unsigned long flags)
  791. {
  792. struct trace_entry *entry;
  793. unsigned long irq_flags;
  794. raw_local_irq_save(irq_flags);
  795. __raw_spin_lock(&data->lock);
  796. entry = tracing_get_trace_entry(tr, data);
  797. tracing_generic_entry_update(entry, flags);
  798. entry->type = TRACE_CTX;
  799. entry->field.ctx.prev_pid = prev->pid;
  800. entry->field.ctx.prev_prio = prev->prio;
  801. entry->field.ctx.prev_state = prev->state;
  802. entry->field.ctx.next_pid = next->pid;
  803. entry->field.ctx.next_prio = next->prio;
  804. entry->field.ctx.next_state = next->state;
  805. __trace_stack(tr, data, flags, 5);
  806. __raw_spin_unlock(&data->lock);
  807. raw_local_irq_restore(irq_flags);
  808. }
  809. void
  810. tracing_sched_wakeup_trace(struct trace_array *tr,
  811. struct trace_array_cpu *data,
  812. struct task_struct *wakee,
  813. struct task_struct *curr,
  814. unsigned long flags)
  815. {
  816. struct trace_entry *entry;
  817. unsigned long irq_flags;
  818. raw_local_irq_save(irq_flags);
  819. __raw_spin_lock(&data->lock);
  820. entry = tracing_get_trace_entry(tr, data);
  821. tracing_generic_entry_update(entry, flags);
  822. entry->type = TRACE_WAKE;
  823. entry->field.ctx.prev_pid = curr->pid;
  824. entry->field.ctx.prev_prio = curr->prio;
  825. entry->field.ctx.prev_state = curr->state;
  826. entry->field.ctx.next_pid = wakee->pid;
  827. entry->field.ctx.next_prio = wakee->prio;
  828. entry->field.ctx.next_state = wakee->state;
  829. __trace_stack(tr, data, flags, 6);
  830. __raw_spin_unlock(&data->lock);
  831. raw_local_irq_restore(irq_flags);
  832. trace_wake_up();
  833. }
  834. void
  835. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  836. {
  837. struct trace_array *tr = &global_trace;
  838. struct trace_array_cpu *data;
  839. unsigned long flags;
  840. long disabled;
  841. int cpu;
  842. if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
  843. return;
  844. local_irq_save(flags);
  845. cpu = raw_smp_processor_id();
  846. data = tr->data[cpu];
  847. disabled = atomic_inc_return(&data->disabled);
  848. if (likely(disabled == 1))
  849. __trace_special(tr, data, arg1, arg2, arg3);
  850. atomic_dec(&data->disabled);
  851. local_irq_restore(flags);
  852. }
  853. #ifdef CONFIG_FTRACE
  854. static void
  855. function_trace_call(unsigned long ip, unsigned long parent_ip)
  856. {
  857. struct trace_array *tr = &global_trace;
  858. struct trace_array_cpu *data;
  859. unsigned long flags;
  860. long disabled;
  861. int cpu;
  862. if (unlikely(!ftrace_function_enabled))
  863. return;
  864. if (skip_trace(ip))
  865. return;
  866. local_irq_save(flags);
  867. cpu = raw_smp_processor_id();
  868. data = tr->data[cpu];
  869. disabled = atomic_inc_return(&data->disabled);
  870. if (likely(disabled == 1))
  871. trace_function(tr, data, ip, parent_ip, flags);
  872. atomic_dec(&data->disabled);
  873. local_irq_restore(flags);
  874. }
  875. static struct ftrace_ops trace_ops __read_mostly =
  876. {
  877. .func = function_trace_call,
  878. };
  879. void tracing_start_function_trace(void)
  880. {
  881. ftrace_function_enabled = 0;
  882. register_ftrace_function(&trace_ops);
  883. if (tracer_enabled)
  884. ftrace_function_enabled = 1;
  885. }
  886. void tracing_stop_function_trace(void)
  887. {
  888. ftrace_function_enabled = 0;
  889. unregister_ftrace_function(&trace_ops);
  890. }
  891. #endif
  892. enum trace_file_type {
  893. TRACE_FILE_LAT_FMT = 1,
  894. };
  895. /* Return the current entry. */
  896. static struct trace_entry *
  897. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  898. struct trace_iterator *iter, int cpu)
  899. {
  900. struct page *page;
  901. struct trace_entry *array;
  902. if (iter->next_idx[cpu] >= tr->entries ||
  903. iter->next_idx[cpu] >= data->trace_idx ||
  904. (data->trace_head == data->trace_tail &&
  905. data->trace_head_idx == data->trace_tail_idx))
  906. return NULL;
  907. if (!iter->next_page[cpu]) {
  908. /* Initialize the iterator for this cpu trace buffer */
  909. WARN_ON(!data->trace_tail);
  910. page = virt_to_page(data->trace_tail);
  911. iter->next_page[cpu] = &page->lru;
  912. iter->next_page_idx[cpu] = data->trace_tail_idx;
  913. }
  914. page = list_entry(iter->next_page[cpu], struct page, lru);
  915. BUG_ON(&data->trace_pages == &page->lru);
  916. array = page_address(page);
  917. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  918. return &array[iter->next_page_idx[cpu]];
  919. }
  920. /* Increment the index counter of an iterator by one */
  921. static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
  922. {
  923. iter->idx++;
  924. iter->next_idx[cpu]++;
  925. iter->next_page_idx[cpu]++;
  926. if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
  927. struct trace_array_cpu *data = iter->tr->data[cpu];
  928. iter->next_page_idx[cpu] = 0;
  929. iter->next_page[cpu] =
  930. trace_next_list(data, iter->next_page[cpu]);
  931. }
  932. }
  933. static struct trace_entry *
  934. trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
  935. struct trace_iterator *iter, int cpu)
  936. {
  937. struct list_head *next_page;
  938. struct trace_entry *ent;
  939. int idx, next_idx, next_page_idx;
  940. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  941. if (likely(!ent || ent->type != TRACE_CONT))
  942. return ent;
  943. /* save the iterator details */
  944. idx = iter->idx;
  945. next_idx = iter->next_idx[cpu];
  946. next_page_idx = iter->next_page_idx[cpu];
  947. next_page = iter->next_page[cpu];
  948. /* find a real entry */
  949. do {
  950. trace_iterator_increment(iter, cpu);
  951. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  952. } while (ent && ent->type != TRACE_CONT);
  953. /* reset the iterator */
  954. iter->idx = idx;
  955. iter->next_idx[cpu] = next_idx;
  956. iter->next_page_idx[cpu] = next_page_idx;
  957. iter->next_page[cpu] = next_page;
  958. return ent;
  959. }
  960. static struct trace_entry *
  961. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
  962. {
  963. struct trace_array *tr = iter->tr;
  964. struct trace_entry *ent, *next = NULL;
  965. int next_cpu = -1;
  966. int cpu;
  967. for_each_tracing_cpu(cpu) {
  968. if (!head_page(tr->data[cpu]))
  969. continue;
  970. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  971. if (ent && ent->type == TRACE_CONT) {
  972. struct trace_array_cpu *data = tr->data[cpu];
  973. if (!inc)
  974. ent = trace_entry_next(tr, data, iter, cpu);
  975. else {
  976. while (ent && ent->type == TRACE_CONT) {
  977. trace_iterator_increment(iter, cpu);
  978. ent = trace_entry_idx(tr, tr->data[cpu],
  979. iter, cpu);
  980. }
  981. }
  982. }
  983. /*
  984. * Pick the entry with the smallest timestamp:
  985. */
  986. if (ent && (!next || ent->field.t < next->field.t)) {
  987. next = ent;
  988. next_cpu = cpu;
  989. }
  990. }
  991. if (ent_cpu)
  992. *ent_cpu = next_cpu;
  993. return next;
  994. }
  995. /* Find the next real entry, without updating the iterator itself */
  996. static struct trace_entry *
  997. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  998. {
  999. return __find_next_entry(iter, ent_cpu, 0);
  1000. }
  1001. /* Find the next real entry, and increment the iterator to the next entry */
  1002. static void *find_next_entry_inc(struct trace_iterator *iter)
  1003. {
  1004. struct trace_entry *next;
  1005. int next_cpu = -1;
  1006. next = __find_next_entry(iter, &next_cpu, 1);
  1007. iter->prev_ent = iter->ent;
  1008. iter->prev_cpu = iter->cpu;
  1009. iter->ent = next;
  1010. iter->cpu = next_cpu;
  1011. if (next)
  1012. trace_iterator_increment(iter, iter->cpu);
  1013. return next ? iter : NULL;
  1014. }
  1015. static void trace_consume(struct trace_iterator *iter)
  1016. {
  1017. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  1018. struct trace_entry *ent;
  1019. again:
  1020. data->trace_tail_idx++;
  1021. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  1022. data->trace_tail = trace_next_page(data, data->trace_tail);
  1023. data->trace_tail_idx = 0;
  1024. }
  1025. /* Check if we empty it, then reset the index */
  1026. if (data->trace_head == data->trace_tail &&
  1027. data->trace_head_idx == data->trace_tail_idx)
  1028. data->trace_idx = 0;
  1029. ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
  1030. iter, iter->cpu);
  1031. if (ent && ent->type == TRACE_CONT)
  1032. goto again;
  1033. }
  1034. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1035. {
  1036. struct trace_iterator *iter = m->private;
  1037. int i = (int)*pos;
  1038. void *ent;
  1039. (*pos)++;
  1040. /* can't go backwards */
  1041. if (iter->idx > i)
  1042. return NULL;
  1043. if (iter->idx < 0)
  1044. ent = find_next_entry_inc(iter);
  1045. else
  1046. ent = iter;
  1047. while (ent && iter->idx < i)
  1048. ent = find_next_entry_inc(iter);
  1049. iter->pos = *pos;
  1050. return ent;
  1051. }
  1052. static void *s_start(struct seq_file *m, loff_t *pos)
  1053. {
  1054. struct trace_iterator *iter = m->private;
  1055. void *p = NULL;
  1056. loff_t l = 0;
  1057. int i;
  1058. mutex_lock(&trace_types_lock);
  1059. if (!current_trace || current_trace != iter->trace) {
  1060. mutex_unlock(&trace_types_lock);
  1061. return NULL;
  1062. }
  1063. atomic_inc(&trace_record_cmdline_disabled);
  1064. /* let the tracer grab locks here if needed */
  1065. if (current_trace->start)
  1066. current_trace->start(iter);
  1067. if (*pos != iter->pos) {
  1068. iter->ent = NULL;
  1069. iter->cpu = 0;
  1070. iter->idx = -1;
  1071. iter->prev_ent = NULL;
  1072. iter->prev_cpu = -1;
  1073. for_each_tracing_cpu(i) {
  1074. iter->next_idx[i] = 0;
  1075. iter->next_page[i] = NULL;
  1076. }
  1077. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1078. ;
  1079. } else {
  1080. l = *pos - 1;
  1081. p = s_next(m, p, &l);
  1082. }
  1083. return p;
  1084. }
  1085. static void s_stop(struct seq_file *m, void *p)
  1086. {
  1087. struct trace_iterator *iter = m->private;
  1088. atomic_dec(&trace_record_cmdline_disabled);
  1089. /* let the tracer release locks here if needed */
  1090. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  1091. iter->trace->stop(iter);
  1092. mutex_unlock(&trace_types_lock);
  1093. }
  1094. #define KRETPROBE_MSG "[unknown/kretprobe'd]"
  1095. #ifdef CONFIG_KRETPROBES
  1096. static inline int kretprobed(unsigned long addr)
  1097. {
  1098. return addr == (unsigned long)kretprobe_trampoline;
  1099. }
  1100. #else
  1101. static inline int kretprobed(unsigned long addr)
  1102. {
  1103. return 0;
  1104. }
  1105. #endif /* CONFIG_KRETPROBES */
  1106. static int
  1107. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  1108. {
  1109. #ifdef CONFIG_KALLSYMS
  1110. char str[KSYM_SYMBOL_LEN];
  1111. kallsyms_lookup(address, NULL, NULL, NULL, str);
  1112. return trace_seq_printf(s, fmt, str);
  1113. #endif
  1114. return 1;
  1115. }
  1116. static int
  1117. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  1118. unsigned long address)
  1119. {
  1120. #ifdef CONFIG_KALLSYMS
  1121. char str[KSYM_SYMBOL_LEN];
  1122. sprint_symbol(str, address);
  1123. return trace_seq_printf(s, fmt, str);
  1124. #endif
  1125. return 1;
  1126. }
  1127. #ifndef CONFIG_64BIT
  1128. # define IP_FMT "%08lx"
  1129. #else
  1130. # define IP_FMT "%016lx"
  1131. #endif
  1132. static int
  1133. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  1134. {
  1135. int ret;
  1136. if (!ip)
  1137. return trace_seq_printf(s, "0");
  1138. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  1139. ret = seq_print_sym_offset(s, "%s", ip);
  1140. else
  1141. ret = seq_print_sym_short(s, "%s", ip);
  1142. if (!ret)
  1143. return 0;
  1144. if (sym_flags & TRACE_ITER_SYM_ADDR)
  1145. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1146. return ret;
  1147. }
  1148. static void print_lat_help_header(struct seq_file *m)
  1149. {
  1150. seq_puts(m, "# _------=> CPU# \n");
  1151. seq_puts(m, "# / _-----=> irqs-off \n");
  1152. seq_puts(m, "# | / _----=> need-resched \n");
  1153. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1154. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1155. seq_puts(m, "# |||| / \n");
  1156. seq_puts(m, "# ||||| delay \n");
  1157. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1158. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1159. }
  1160. static void print_func_help_header(struct seq_file *m)
  1161. {
  1162. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1163. seq_puts(m, "# | | | | |\n");
  1164. }
  1165. static void
  1166. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1167. {
  1168. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1169. struct trace_array *tr = iter->tr;
  1170. struct trace_array_cpu *data = tr->data[tr->cpu];
  1171. struct tracer *type = current_trace;
  1172. unsigned long total = 0;
  1173. unsigned long entries = 0;
  1174. int cpu;
  1175. const char *name = "preemption";
  1176. if (type)
  1177. name = type->name;
  1178. for_each_tracing_cpu(cpu) {
  1179. if (head_page(tr->data[cpu])) {
  1180. total += tr->data[cpu]->trace_idx;
  1181. if (tr->data[cpu]->trace_idx > tr->entries)
  1182. entries += tr->entries;
  1183. else
  1184. entries += tr->data[cpu]->trace_idx;
  1185. }
  1186. }
  1187. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1188. name, UTS_RELEASE);
  1189. seq_puts(m, "-----------------------------------"
  1190. "---------------------------------\n");
  1191. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1192. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1193. nsecs_to_usecs(data->saved_latency),
  1194. entries,
  1195. total,
  1196. tr->cpu,
  1197. #if defined(CONFIG_PREEMPT_NONE)
  1198. "server",
  1199. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1200. "desktop",
  1201. #elif defined(CONFIG_PREEMPT)
  1202. "preempt",
  1203. #else
  1204. "unknown",
  1205. #endif
  1206. /* These are reserved for later use */
  1207. 0, 0, 0, 0);
  1208. #ifdef CONFIG_SMP
  1209. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1210. #else
  1211. seq_puts(m, ")\n");
  1212. #endif
  1213. seq_puts(m, " -----------------\n");
  1214. seq_printf(m, " | task: %.16s-%d "
  1215. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1216. data->comm, data->pid, data->uid, data->nice,
  1217. data->policy, data->rt_priority);
  1218. seq_puts(m, " -----------------\n");
  1219. if (data->critical_start) {
  1220. seq_puts(m, " => started at: ");
  1221. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1222. trace_print_seq(m, &iter->seq);
  1223. seq_puts(m, "\n => ended at: ");
  1224. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1225. trace_print_seq(m, &iter->seq);
  1226. seq_puts(m, "\n");
  1227. }
  1228. seq_puts(m, "\n");
  1229. }
  1230. static void
  1231. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1232. {
  1233. struct trace_field *field = &entry->field;
  1234. int hardirq, softirq;
  1235. char *comm;
  1236. comm = trace_find_cmdline(field->pid);
  1237. trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
  1238. trace_seq_printf(s, "%d", cpu);
  1239. trace_seq_printf(s, "%c%c",
  1240. (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  1241. ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1242. hardirq = field->flags & TRACE_FLAG_HARDIRQ;
  1243. softirq = field->flags & TRACE_FLAG_SOFTIRQ;
  1244. if (hardirq && softirq) {
  1245. trace_seq_putc(s, 'H');
  1246. } else {
  1247. if (hardirq) {
  1248. trace_seq_putc(s, 'h');
  1249. } else {
  1250. if (softirq)
  1251. trace_seq_putc(s, 's');
  1252. else
  1253. trace_seq_putc(s, '.');
  1254. }
  1255. }
  1256. if (field->preempt_count)
  1257. trace_seq_printf(s, "%x", field->preempt_count);
  1258. else
  1259. trace_seq_puts(s, ".");
  1260. }
  1261. unsigned long preempt_mark_thresh = 100;
  1262. static void
  1263. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  1264. unsigned long rel_usecs)
  1265. {
  1266. trace_seq_printf(s, " %4lldus", abs_usecs);
  1267. if (rel_usecs > preempt_mark_thresh)
  1268. trace_seq_puts(s, "!: ");
  1269. else if (rel_usecs > 1)
  1270. trace_seq_puts(s, "+: ");
  1271. else
  1272. trace_seq_puts(s, " : ");
  1273. }
  1274. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1275. static void
  1276. trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1277. {
  1278. struct trace_array *tr = iter->tr;
  1279. struct trace_array_cpu *data = tr->data[iter->cpu];
  1280. struct trace_entry *ent;
  1281. ent = trace_entry_idx(tr, data, iter, iter->cpu);
  1282. if (!ent || ent->type != TRACE_CONT) {
  1283. trace_seq_putc(s, '\n');
  1284. return;
  1285. }
  1286. do {
  1287. trace_seq_printf(s, "%s", ent->cont.buf);
  1288. trace_iterator_increment(iter, iter->cpu);
  1289. ent = trace_entry_idx(tr, data, iter, iter->cpu);
  1290. } while (ent && ent->type == TRACE_CONT);
  1291. }
  1292. static int
  1293. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1294. {
  1295. struct trace_seq *s = &iter->seq;
  1296. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1297. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  1298. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1299. struct trace_entry *entry = iter->ent;
  1300. struct trace_field *field = &entry->field;
  1301. unsigned long abs_usecs;
  1302. unsigned long rel_usecs;
  1303. char *comm;
  1304. int S, T;
  1305. int i;
  1306. unsigned state;
  1307. if (!next_entry)
  1308. next_entry = entry;
  1309. if (entry->type == TRACE_CONT)
  1310. return 1;
  1311. rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
  1312. abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
  1313. if (verbose) {
  1314. comm = trace_find_cmdline(field->pid);
  1315. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  1316. " %ld.%03ldms (+%ld.%03ldms): ",
  1317. comm,
  1318. field->pid, cpu, field->flags,
  1319. field->preempt_count, trace_idx,
  1320. ns2usecs(field->t),
  1321. abs_usecs/1000,
  1322. abs_usecs % 1000, rel_usecs/1000,
  1323. rel_usecs % 1000);
  1324. } else {
  1325. lat_print_generic(s, entry, cpu);
  1326. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1327. }
  1328. switch (entry->type) {
  1329. case TRACE_FN:
  1330. seq_print_ip_sym(s, field->fn.ip, sym_flags);
  1331. trace_seq_puts(s, " (");
  1332. if (kretprobed(field->fn.parent_ip))
  1333. trace_seq_puts(s, KRETPROBE_MSG);
  1334. else
  1335. seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
  1336. trace_seq_puts(s, ")\n");
  1337. break;
  1338. case TRACE_CTX:
  1339. case TRACE_WAKE:
  1340. T = field->ctx.next_state < sizeof(state_to_char) ?
  1341. state_to_char[field->ctx.next_state] : 'X';
  1342. state = field->ctx.prev_state ?
  1343. __ffs(field->ctx.prev_state) + 1 : 0;
  1344. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1345. comm = trace_find_cmdline(field->ctx.next_pid);
  1346. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1347. field->ctx.prev_pid,
  1348. field->ctx.prev_prio,
  1349. S, entry->type == TRACE_CTX ? "==>" : " +",
  1350. field->ctx.next_pid,
  1351. field->ctx.next_prio,
  1352. T, comm);
  1353. break;
  1354. case TRACE_SPECIAL:
  1355. trace_seq_printf(s, "# %ld %ld %ld\n",
  1356. field->special.arg1,
  1357. field->special.arg2,
  1358. field->special.arg3);
  1359. break;
  1360. case TRACE_STACK:
  1361. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1362. if (i)
  1363. trace_seq_puts(s, " <= ");
  1364. seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
  1365. }
  1366. trace_seq_puts(s, "\n");
  1367. break;
  1368. case TRACE_PRINT:
  1369. seq_print_ip_sym(s, field->print.ip, sym_flags);
  1370. trace_seq_printf(s, ": %s", field->print.buf);
  1371. if (field->flags && TRACE_FLAG_CONT)
  1372. trace_seq_print_cont(s, iter);
  1373. break;
  1374. default:
  1375. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1376. }
  1377. return 1;
  1378. }
  1379. static int print_trace_fmt(struct trace_iterator *iter)
  1380. {
  1381. struct trace_seq *s = &iter->seq;
  1382. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1383. struct trace_entry *entry;
  1384. struct trace_field *field;
  1385. unsigned long usec_rem;
  1386. unsigned long long t;
  1387. unsigned long secs;
  1388. char *comm;
  1389. int ret;
  1390. int S, T;
  1391. int i;
  1392. entry = iter->ent;
  1393. if (entry->type == TRACE_CONT)
  1394. return 1;
  1395. field = &entry->field;
  1396. comm = trace_find_cmdline(iter->ent->field.pid);
  1397. t = ns2usecs(field->t);
  1398. usec_rem = do_div(t, 1000000ULL);
  1399. secs = (unsigned long)t;
  1400. ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
  1401. if (!ret)
  1402. return 0;
  1403. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1404. if (!ret)
  1405. return 0;
  1406. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1407. if (!ret)
  1408. return 0;
  1409. switch (entry->type) {
  1410. case TRACE_FN:
  1411. ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
  1412. if (!ret)
  1413. return 0;
  1414. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1415. field->fn.parent_ip) {
  1416. ret = trace_seq_printf(s, " <-");
  1417. if (!ret)
  1418. return 0;
  1419. if (kretprobed(field->fn.parent_ip))
  1420. ret = trace_seq_puts(s, KRETPROBE_MSG);
  1421. else
  1422. ret = seq_print_ip_sym(s,
  1423. field->fn.parent_ip,
  1424. sym_flags);
  1425. if (!ret)
  1426. return 0;
  1427. }
  1428. ret = trace_seq_printf(s, "\n");
  1429. if (!ret)
  1430. return 0;
  1431. break;
  1432. case TRACE_CTX:
  1433. case TRACE_WAKE:
  1434. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1435. state_to_char[field->ctx.prev_state] : 'X';
  1436. T = field->ctx.next_state < sizeof(state_to_char) ?
  1437. state_to_char[field->ctx.next_state] : 'X';
  1438. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1439. field->ctx.prev_pid,
  1440. field->ctx.prev_prio,
  1441. S,
  1442. entry->type == TRACE_CTX ? "==>" : " +",
  1443. field->ctx.next_pid,
  1444. field->ctx.next_prio,
  1445. T);
  1446. if (!ret)
  1447. return 0;
  1448. break;
  1449. case TRACE_SPECIAL:
  1450. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1451. field->special.arg1,
  1452. field->special.arg2,
  1453. field->special.arg3);
  1454. if (!ret)
  1455. return 0;
  1456. break;
  1457. case TRACE_STACK:
  1458. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1459. if (i) {
  1460. ret = trace_seq_puts(s, " <= ");
  1461. if (!ret)
  1462. return 0;
  1463. }
  1464. ret = seq_print_ip_sym(s, field->stack.caller[i],
  1465. sym_flags);
  1466. if (!ret)
  1467. return 0;
  1468. }
  1469. ret = trace_seq_puts(s, "\n");
  1470. if (!ret)
  1471. return 0;
  1472. break;
  1473. case TRACE_PRINT:
  1474. seq_print_ip_sym(s, field->print.ip, sym_flags);
  1475. trace_seq_printf(s, ": %s", field->print.buf);
  1476. if (field->flags && TRACE_FLAG_CONT)
  1477. trace_seq_print_cont(s, iter);
  1478. break;
  1479. }
  1480. return 1;
  1481. }
  1482. static int print_raw_fmt(struct trace_iterator *iter)
  1483. {
  1484. struct trace_seq *s = &iter->seq;
  1485. struct trace_entry *entry;
  1486. struct trace_field *field;
  1487. int ret;
  1488. int S, T;
  1489. entry = iter->ent;
  1490. if (entry->type == TRACE_CONT)
  1491. return 1;
  1492. field = &entry->field;
  1493. ret = trace_seq_printf(s, "%d %d %llu ",
  1494. field->pid, iter->cpu, field->t);
  1495. if (!ret)
  1496. return 0;
  1497. switch (entry->type) {
  1498. case TRACE_FN:
  1499. ret = trace_seq_printf(s, "%x %x\n",
  1500. field->fn.ip,
  1501. field->fn.parent_ip);
  1502. if (!ret)
  1503. return 0;
  1504. break;
  1505. case TRACE_CTX:
  1506. case TRACE_WAKE:
  1507. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1508. state_to_char[field->ctx.prev_state] : 'X';
  1509. T = field->ctx.next_state < sizeof(state_to_char) ?
  1510. state_to_char[field->ctx.next_state] : 'X';
  1511. if (entry->type == TRACE_WAKE)
  1512. S = '+';
  1513. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1514. field->ctx.prev_pid,
  1515. field->ctx.prev_prio,
  1516. S,
  1517. field->ctx.next_pid,
  1518. field->ctx.next_prio,
  1519. T);
  1520. if (!ret)
  1521. return 0;
  1522. break;
  1523. case TRACE_SPECIAL:
  1524. case TRACE_STACK:
  1525. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1526. field->special.arg1,
  1527. field->special.arg2,
  1528. field->special.arg3);
  1529. if (!ret)
  1530. return 0;
  1531. break;
  1532. case TRACE_PRINT:
  1533. trace_seq_printf(s, "# %lx %s",
  1534. field->print.ip, field->print.buf);
  1535. if (field->flags && TRACE_FLAG_CONT)
  1536. trace_seq_print_cont(s, iter);
  1537. break;
  1538. }
  1539. return 1;
  1540. }
  1541. #define SEQ_PUT_FIELD_RET(s, x) \
  1542. do { \
  1543. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1544. return 0; \
  1545. } while (0)
  1546. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1547. do { \
  1548. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1549. return 0; \
  1550. } while (0)
  1551. static int print_hex_fmt(struct trace_iterator *iter)
  1552. {
  1553. struct trace_seq *s = &iter->seq;
  1554. unsigned char newline = '\n';
  1555. struct trace_entry *entry;
  1556. struct trace_field *field;
  1557. int S, T;
  1558. entry = iter->ent;
  1559. if (entry->type == TRACE_CONT)
  1560. return 1;
  1561. field = &entry->field;
  1562. SEQ_PUT_HEX_FIELD_RET(s, field->pid);
  1563. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1564. SEQ_PUT_HEX_FIELD_RET(s, field->t);
  1565. switch (entry->type) {
  1566. case TRACE_FN:
  1567. SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
  1568. SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
  1569. break;
  1570. case TRACE_CTX:
  1571. case TRACE_WAKE:
  1572. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1573. state_to_char[field->ctx.prev_state] : 'X';
  1574. T = field->ctx.next_state < sizeof(state_to_char) ?
  1575. state_to_char[field->ctx.next_state] : 'X';
  1576. if (entry->type == TRACE_WAKE)
  1577. S = '+';
  1578. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
  1579. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
  1580. SEQ_PUT_HEX_FIELD_RET(s, S);
  1581. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
  1582. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
  1583. SEQ_PUT_HEX_FIELD_RET(s, T);
  1584. break;
  1585. case TRACE_SPECIAL:
  1586. case TRACE_STACK:
  1587. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
  1588. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
  1589. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
  1590. break;
  1591. }
  1592. SEQ_PUT_FIELD_RET(s, newline);
  1593. return 1;
  1594. }
  1595. static int print_bin_fmt(struct trace_iterator *iter)
  1596. {
  1597. struct trace_seq *s = &iter->seq;
  1598. struct trace_entry *entry;
  1599. struct trace_field *field;
  1600. entry = iter->ent;
  1601. if (entry->type == TRACE_CONT)
  1602. return 1;
  1603. field = &entry->field;
  1604. SEQ_PUT_FIELD_RET(s, field->pid);
  1605. SEQ_PUT_FIELD_RET(s, field->cpu);
  1606. SEQ_PUT_FIELD_RET(s, field->t);
  1607. switch (entry->type) {
  1608. case TRACE_FN:
  1609. SEQ_PUT_FIELD_RET(s, field->fn.ip);
  1610. SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
  1611. break;
  1612. case TRACE_CTX:
  1613. SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
  1614. SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
  1615. SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
  1616. SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
  1617. SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
  1618. SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
  1619. break;
  1620. case TRACE_SPECIAL:
  1621. case TRACE_STACK:
  1622. SEQ_PUT_FIELD_RET(s, field->special.arg1);
  1623. SEQ_PUT_FIELD_RET(s, field->special.arg2);
  1624. SEQ_PUT_FIELD_RET(s, field->special.arg3);
  1625. break;
  1626. }
  1627. return 1;
  1628. }
  1629. static int trace_empty(struct trace_iterator *iter)
  1630. {
  1631. struct trace_array_cpu *data;
  1632. int cpu;
  1633. for_each_tracing_cpu(cpu) {
  1634. data = iter->tr->data[cpu];
  1635. if (head_page(data) && data->trace_idx &&
  1636. (data->trace_tail != data->trace_head ||
  1637. data->trace_tail_idx != data->trace_head_idx))
  1638. return 0;
  1639. }
  1640. return 1;
  1641. }
  1642. static int print_trace_line(struct trace_iterator *iter)
  1643. {
  1644. if (iter->trace && iter->trace->print_line)
  1645. return iter->trace->print_line(iter);
  1646. if (trace_flags & TRACE_ITER_BIN)
  1647. return print_bin_fmt(iter);
  1648. if (trace_flags & TRACE_ITER_HEX)
  1649. return print_hex_fmt(iter);
  1650. if (trace_flags & TRACE_ITER_RAW)
  1651. return print_raw_fmt(iter);
  1652. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1653. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1654. return print_trace_fmt(iter);
  1655. }
  1656. static int s_show(struct seq_file *m, void *v)
  1657. {
  1658. struct trace_iterator *iter = v;
  1659. if (iter->ent == NULL) {
  1660. if (iter->tr) {
  1661. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1662. seq_puts(m, "#\n");
  1663. }
  1664. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1665. /* print nothing if the buffers are empty */
  1666. if (trace_empty(iter))
  1667. return 0;
  1668. print_trace_header(m, iter);
  1669. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1670. print_lat_help_header(m);
  1671. } else {
  1672. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1673. print_func_help_header(m);
  1674. }
  1675. } else {
  1676. print_trace_line(iter);
  1677. trace_print_seq(m, &iter->seq);
  1678. }
  1679. return 0;
  1680. }
  1681. static struct seq_operations tracer_seq_ops = {
  1682. .start = s_start,
  1683. .next = s_next,
  1684. .stop = s_stop,
  1685. .show = s_show,
  1686. };
  1687. static struct trace_iterator *
  1688. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1689. {
  1690. struct trace_iterator *iter;
  1691. if (tracing_disabled) {
  1692. *ret = -ENODEV;
  1693. return NULL;
  1694. }
  1695. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1696. if (!iter) {
  1697. *ret = -ENOMEM;
  1698. goto out;
  1699. }
  1700. mutex_lock(&trace_types_lock);
  1701. if (current_trace && current_trace->print_max)
  1702. iter->tr = &max_tr;
  1703. else
  1704. iter->tr = inode->i_private;
  1705. iter->trace = current_trace;
  1706. iter->pos = -1;
  1707. /* TODO stop tracer */
  1708. *ret = seq_open(file, &tracer_seq_ops);
  1709. if (!*ret) {
  1710. struct seq_file *m = file->private_data;
  1711. m->private = iter;
  1712. /* stop the trace while dumping */
  1713. if (iter->tr->ctrl) {
  1714. tracer_enabled = 0;
  1715. ftrace_function_enabled = 0;
  1716. }
  1717. if (iter->trace && iter->trace->open)
  1718. iter->trace->open(iter);
  1719. } else {
  1720. kfree(iter);
  1721. iter = NULL;
  1722. }
  1723. mutex_unlock(&trace_types_lock);
  1724. out:
  1725. return iter;
  1726. }
  1727. int tracing_open_generic(struct inode *inode, struct file *filp)
  1728. {
  1729. if (tracing_disabled)
  1730. return -ENODEV;
  1731. filp->private_data = inode->i_private;
  1732. return 0;
  1733. }
  1734. int tracing_release(struct inode *inode, struct file *file)
  1735. {
  1736. struct seq_file *m = (struct seq_file *)file->private_data;
  1737. struct trace_iterator *iter = m->private;
  1738. mutex_lock(&trace_types_lock);
  1739. if (iter->trace && iter->trace->close)
  1740. iter->trace->close(iter);
  1741. /* reenable tracing if it was previously enabled */
  1742. if (iter->tr->ctrl) {
  1743. tracer_enabled = 1;
  1744. /*
  1745. * It is safe to enable function tracing even if it
  1746. * isn't used
  1747. */
  1748. ftrace_function_enabled = 1;
  1749. }
  1750. mutex_unlock(&trace_types_lock);
  1751. seq_release(inode, file);
  1752. kfree(iter);
  1753. return 0;
  1754. }
  1755. static int tracing_open(struct inode *inode, struct file *file)
  1756. {
  1757. int ret;
  1758. __tracing_open(inode, file, &ret);
  1759. return ret;
  1760. }
  1761. static int tracing_lt_open(struct inode *inode, struct file *file)
  1762. {
  1763. struct trace_iterator *iter;
  1764. int ret;
  1765. iter = __tracing_open(inode, file, &ret);
  1766. if (!ret)
  1767. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1768. return ret;
  1769. }
  1770. static void *
  1771. t_next(struct seq_file *m, void *v, loff_t *pos)
  1772. {
  1773. struct tracer *t = m->private;
  1774. (*pos)++;
  1775. if (t)
  1776. t = t->next;
  1777. m->private = t;
  1778. return t;
  1779. }
  1780. static void *t_start(struct seq_file *m, loff_t *pos)
  1781. {
  1782. struct tracer *t = m->private;
  1783. loff_t l = 0;
  1784. mutex_lock(&trace_types_lock);
  1785. for (; t && l < *pos; t = t_next(m, t, &l))
  1786. ;
  1787. return t;
  1788. }
  1789. static void t_stop(struct seq_file *m, void *p)
  1790. {
  1791. mutex_unlock(&trace_types_lock);
  1792. }
  1793. static int t_show(struct seq_file *m, void *v)
  1794. {
  1795. struct tracer *t = v;
  1796. if (!t)
  1797. return 0;
  1798. seq_printf(m, "%s", t->name);
  1799. if (t->next)
  1800. seq_putc(m, ' ');
  1801. else
  1802. seq_putc(m, '\n');
  1803. return 0;
  1804. }
  1805. static struct seq_operations show_traces_seq_ops = {
  1806. .start = t_start,
  1807. .next = t_next,
  1808. .stop = t_stop,
  1809. .show = t_show,
  1810. };
  1811. static int show_traces_open(struct inode *inode, struct file *file)
  1812. {
  1813. int ret;
  1814. if (tracing_disabled)
  1815. return -ENODEV;
  1816. ret = seq_open(file, &show_traces_seq_ops);
  1817. if (!ret) {
  1818. struct seq_file *m = file->private_data;
  1819. m->private = trace_types;
  1820. }
  1821. return ret;
  1822. }
  1823. static struct file_operations tracing_fops = {
  1824. .open = tracing_open,
  1825. .read = seq_read,
  1826. .llseek = seq_lseek,
  1827. .release = tracing_release,
  1828. };
  1829. static struct file_operations tracing_lt_fops = {
  1830. .open = tracing_lt_open,
  1831. .read = seq_read,
  1832. .llseek = seq_lseek,
  1833. .release = tracing_release,
  1834. };
  1835. static struct file_operations show_traces_fops = {
  1836. .open = show_traces_open,
  1837. .read = seq_read,
  1838. .release = seq_release,
  1839. };
  1840. /*
  1841. * Only trace on a CPU if the bitmask is set:
  1842. */
  1843. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1844. /*
  1845. * When tracing/tracing_cpu_mask is modified then this holds
  1846. * the new bitmask we are about to install:
  1847. */
  1848. static cpumask_t tracing_cpumask_new;
  1849. /*
  1850. * The tracer itself will not take this lock, but still we want
  1851. * to provide a consistent cpumask to user-space:
  1852. */
  1853. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1854. /*
  1855. * Temporary storage for the character representation of the
  1856. * CPU bitmask (and one more byte for the newline):
  1857. */
  1858. static char mask_str[NR_CPUS + 1];
  1859. static ssize_t
  1860. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1861. size_t count, loff_t *ppos)
  1862. {
  1863. int len;
  1864. mutex_lock(&tracing_cpumask_update_lock);
  1865. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1866. if (count - len < 2) {
  1867. count = -EINVAL;
  1868. goto out_err;
  1869. }
  1870. len += sprintf(mask_str + len, "\n");
  1871. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1872. out_err:
  1873. mutex_unlock(&tracing_cpumask_update_lock);
  1874. return count;
  1875. }
  1876. static ssize_t
  1877. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1878. size_t count, loff_t *ppos)
  1879. {
  1880. int err, cpu;
  1881. mutex_lock(&tracing_cpumask_update_lock);
  1882. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1883. if (err)
  1884. goto err_unlock;
  1885. raw_local_irq_disable();
  1886. __raw_spin_lock(&ftrace_max_lock);
  1887. for_each_tracing_cpu(cpu) {
  1888. /*
  1889. * Increase/decrease the disabled counter if we are
  1890. * about to flip a bit in the cpumask:
  1891. */
  1892. if (cpu_isset(cpu, tracing_cpumask) &&
  1893. !cpu_isset(cpu, tracing_cpumask_new)) {
  1894. atomic_inc(&global_trace.data[cpu]->disabled);
  1895. }
  1896. if (!cpu_isset(cpu, tracing_cpumask) &&
  1897. cpu_isset(cpu, tracing_cpumask_new)) {
  1898. atomic_dec(&global_trace.data[cpu]->disabled);
  1899. }
  1900. }
  1901. __raw_spin_unlock(&ftrace_max_lock);
  1902. raw_local_irq_enable();
  1903. tracing_cpumask = tracing_cpumask_new;
  1904. mutex_unlock(&tracing_cpumask_update_lock);
  1905. return count;
  1906. err_unlock:
  1907. mutex_unlock(&tracing_cpumask_update_lock);
  1908. return err;
  1909. }
  1910. static struct file_operations tracing_cpumask_fops = {
  1911. .open = tracing_open_generic,
  1912. .read = tracing_cpumask_read,
  1913. .write = tracing_cpumask_write,
  1914. };
  1915. static ssize_t
  1916. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1917. size_t cnt, loff_t *ppos)
  1918. {
  1919. char *buf;
  1920. int r = 0;
  1921. int len = 0;
  1922. int i;
  1923. /* calulate max size */
  1924. for (i = 0; trace_options[i]; i++) {
  1925. len += strlen(trace_options[i]);
  1926. len += 3; /* "no" and space */
  1927. }
  1928. /* +2 for \n and \0 */
  1929. buf = kmalloc(len + 2, GFP_KERNEL);
  1930. if (!buf)
  1931. return -ENOMEM;
  1932. for (i = 0; trace_options[i]; i++) {
  1933. if (trace_flags & (1 << i))
  1934. r += sprintf(buf + r, "%s ", trace_options[i]);
  1935. else
  1936. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1937. }
  1938. r += sprintf(buf + r, "\n");
  1939. WARN_ON(r >= len + 2);
  1940. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1941. kfree(buf);
  1942. return r;
  1943. }
  1944. static ssize_t
  1945. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1946. size_t cnt, loff_t *ppos)
  1947. {
  1948. char buf[64];
  1949. char *cmp = buf;
  1950. int neg = 0;
  1951. int i;
  1952. if (cnt >= sizeof(buf))
  1953. return -EINVAL;
  1954. if (copy_from_user(&buf, ubuf, cnt))
  1955. return -EFAULT;
  1956. buf[cnt] = 0;
  1957. if (strncmp(buf, "no", 2) == 0) {
  1958. neg = 1;
  1959. cmp += 2;
  1960. }
  1961. for (i = 0; trace_options[i]; i++) {
  1962. int len = strlen(trace_options[i]);
  1963. if (strncmp(cmp, trace_options[i], len) == 0) {
  1964. if (neg)
  1965. trace_flags &= ~(1 << i);
  1966. else
  1967. trace_flags |= (1 << i);
  1968. break;
  1969. }
  1970. }
  1971. /*
  1972. * If no option could be set, return an error:
  1973. */
  1974. if (!trace_options[i])
  1975. return -EINVAL;
  1976. filp->f_pos += cnt;
  1977. return cnt;
  1978. }
  1979. static struct file_operations tracing_iter_fops = {
  1980. .open = tracing_open_generic,
  1981. .read = tracing_iter_ctrl_read,
  1982. .write = tracing_iter_ctrl_write,
  1983. };
  1984. static const char readme_msg[] =
  1985. "tracing mini-HOWTO:\n\n"
  1986. "# mkdir /debug\n"
  1987. "# mount -t debugfs nodev /debug\n\n"
  1988. "# cat /debug/tracing/available_tracers\n"
  1989. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1990. "# cat /debug/tracing/current_tracer\n"
  1991. "none\n"
  1992. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1993. "# cat /debug/tracing/current_tracer\n"
  1994. "sched_switch\n"
  1995. "# cat /debug/tracing/iter_ctrl\n"
  1996. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1997. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1998. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1999. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  2000. "echo 0 > /debug/tracing/tracing_enabled\n"
  2001. ;
  2002. static ssize_t
  2003. tracing_readme_read(struct file *filp, char __user *ubuf,
  2004. size_t cnt, loff_t *ppos)
  2005. {
  2006. return simple_read_from_buffer(ubuf, cnt, ppos,
  2007. readme_msg, strlen(readme_msg));
  2008. }
  2009. static struct file_operations tracing_readme_fops = {
  2010. .open = tracing_open_generic,
  2011. .read = tracing_readme_read,
  2012. };
  2013. static ssize_t
  2014. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2015. size_t cnt, loff_t *ppos)
  2016. {
  2017. struct trace_array *tr = filp->private_data;
  2018. char buf[64];
  2019. int r;
  2020. r = sprintf(buf, "%ld\n", tr->ctrl);
  2021. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2022. }
  2023. static ssize_t
  2024. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2025. size_t cnt, loff_t *ppos)
  2026. {
  2027. struct trace_array *tr = filp->private_data;
  2028. char buf[64];
  2029. long val;
  2030. int ret;
  2031. if (cnt >= sizeof(buf))
  2032. return -EINVAL;
  2033. if (copy_from_user(&buf, ubuf, cnt))
  2034. return -EFAULT;
  2035. buf[cnt] = 0;
  2036. ret = strict_strtoul(buf, 10, &val);
  2037. if (ret < 0)
  2038. return ret;
  2039. val = !!val;
  2040. mutex_lock(&trace_types_lock);
  2041. if (tr->ctrl ^ val) {
  2042. if (val)
  2043. tracer_enabled = 1;
  2044. else
  2045. tracer_enabled = 0;
  2046. tr->ctrl = val;
  2047. if (current_trace && current_trace->ctrl_update)
  2048. current_trace->ctrl_update(tr);
  2049. }
  2050. mutex_unlock(&trace_types_lock);
  2051. filp->f_pos += cnt;
  2052. return cnt;
  2053. }
  2054. static ssize_t
  2055. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2056. size_t cnt, loff_t *ppos)
  2057. {
  2058. char buf[max_tracer_type_len+2];
  2059. int r;
  2060. mutex_lock(&trace_types_lock);
  2061. if (current_trace)
  2062. r = sprintf(buf, "%s\n", current_trace->name);
  2063. else
  2064. r = sprintf(buf, "\n");
  2065. mutex_unlock(&trace_types_lock);
  2066. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2067. }
  2068. static ssize_t
  2069. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2070. size_t cnt, loff_t *ppos)
  2071. {
  2072. struct trace_array *tr = &global_trace;
  2073. struct tracer *t;
  2074. char buf[max_tracer_type_len+1];
  2075. int i;
  2076. if (cnt > max_tracer_type_len)
  2077. cnt = max_tracer_type_len;
  2078. if (copy_from_user(&buf, ubuf, cnt))
  2079. return -EFAULT;
  2080. buf[cnt] = 0;
  2081. /* strip ending whitespace. */
  2082. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2083. buf[i] = 0;
  2084. mutex_lock(&trace_types_lock);
  2085. for (t = trace_types; t; t = t->next) {
  2086. if (strcmp(t->name, buf) == 0)
  2087. break;
  2088. }
  2089. if (!t || t == current_trace)
  2090. goto out;
  2091. if (current_trace && current_trace->reset)
  2092. current_trace->reset(tr);
  2093. current_trace = t;
  2094. if (t->init)
  2095. t->init(tr);
  2096. out:
  2097. mutex_unlock(&trace_types_lock);
  2098. filp->f_pos += cnt;
  2099. return cnt;
  2100. }
  2101. static ssize_t
  2102. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2103. size_t cnt, loff_t *ppos)
  2104. {
  2105. unsigned long *ptr = filp->private_data;
  2106. char buf[64];
  2107. int r;
  2108. r = snprintf(buf, sizeof(buf), "%ld\n",
  2109. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2110. if (r > sizeof(buf))
  2111. r = sizeof(buf);
  2112. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2113. }
  2114. static ssize_t
  2115. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2116. size_t cnt, loff_t *ppos)
  2117. {
  2118. long *ptr = filp->private_data;
  2119. char buf[64];
  2120. long val;
  2121. int ret;
  2122. if (cnt >= sizeof(buf))
  2123. return -EINVAL;
  2124. if (copy_from_user(&buf, ubuf, cnt))
  2125. return -EFAULT;
  2126. buf[cnt] = 0;
  2127. ret = strict_strtoul(buf, 10, &val);
  2128. if (ret < 0)
  2129. return ret;
  2130. *ptr = val * 1000;
  2131. return cnt;
  2132. }
  2133. static atomic_t tracing_reader;
  2134. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2135. {
  2136. struct trace_iterator *iter;
  2137. if (tracing_disabled)
  2138. return -ENODEV;
  2139. /* We only allow for reader of the pipe */
  2140. if (atomic_inc_return(&tracing_reader) != 1) {
  2141. atomic_dec(&tracing_reader);
  2142. return -EBUSY;
  2143. }
  2144. /* create a buffer to store the information to pass to userspace */
  2145. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2146. if (!iter)
  2147. return -ENOMEM;
  2148. mutex_lock(&trace_types_lock);
  2149. iter->tr = &global_trace;
  2150. iter->trace = current_trace;
  2151. filp->private_data = iter;
  2152. if (iter->trace->pipe_open)
  2153. iter->trace->pipe_open(iter);
  2154. mutex_unlock(&trace_types_lock);
  2155. return 0;
  2156. }
  2157. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2158. {
  2159. struct trace_iterator *iter = file->private_data;
  2160. kfree(iter);
  2161. atomic_dec(&tracing_reader);
  2162. return 0;
  2163. }
  2164. static unsigned int
  2165. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2166. {
  2167. struct trace_iterator *iter = filp->private_data;
  2168. if (trace_flags & TRACE_ITER_BLOCK) {
  2169. /*
  2170. * Always select as readable when in blocking mode
  2171. */
  2172. return POLLIN | POLLRDNORM;
  2173. } else {
  2174. if (!trace_empty(iter))
  2175. return POLLIN | POLLRDNORM;
  2176. poll_wait(filp, &trace_wait, poll_table);
  2177. if (!trace_empty(iter))
  2178. return POLLIN | POLLRDNORM;
  2179. return 0;
  2180. }
  2181. }
  2182. /*
  2183. * Consumer reader.
  2184. */
  2185. static ssize_t
  2186. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2187. size_t cnt, loff_t *ppos)
  2188. {
  2189. struct trace_iterator *iter = filp->private_data;
  2190. struct trace_array_cpu *data;
  2191. static cpumask_t mask;
  2192. unsigned long flags;
  2193. #ifdef CONFIG_FTRACE
  2194. int ftrace_save;
  2195. #endif
  2196. int cpu;
  2197. ssize_t sret;
  2198. /* return any leftover data */
  2199. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2200. if (sret != -EBUSY)
  2201. return sret;
  2202. sret = 0;
  2203. trace_seq_reset(&iter->seq);
  2204. mutex_lock(&trace_types_lock);
  2205. if (iter->trace->read) {
  2206. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2207. if (sret)
  2208. goto out;
  2209. }
  2210. while (trace_empty(iter)) {
  2211. if ((filp->f_flags & O_NONBLOCK)) {
  2212. sret = -EAGAIN;
  2213. goto out;
  2214. }
  2215. /*
  2216. * This is a make-shift waitqueue. The reason we don't use
  2217. * an actual wait queue is because:
  2218. * 1) we only ever have one waiter
  2219. * 2) the tracing, traces all functions, we don't want
  2220. * the overhead of calling wake_up and friends
  2221. * (and tracing them too)
  2222. * Anyway, this is really very primitive wakeup.
  2223. */
  2224. set_current_state(TASK_INTERRUPTIBLE);
  2225. iter->tr->waiter = current;
  2226. mutex_unlock(&trace_types_lock);
  2227. /* sleep for 100 msecs, and try again. */
  2228. schedule_timeout(HZ/10);
  2229. mutex_lock(&trace_types_lock);
  2230. iter->tr->waiter = NULL;
  2231. if (signal_pending(current)) {
  2232. sret = -EINTR;
  2233. goto out;
  2234. }
  2235. if (iter->trace != current_trace)
  2236. goto out;
  2237. /*
  2238. * We block until we read something and tracing is disabled.
  2239. * We still block if tracing is disabled, but we have never
  2240. * read anything. This allows a user to cat this file, and
  2241. * then enable tracing. But after we have read something,
  2242. * we give an EOF when tracing is again disabled.
  2243. *
  2244. * iter->pos will be 0 if we haven't read anything.
  2245. */
  2246. if (!tracer_enabled && iter->pos)
  2247. break;
  2248. continue;
  2249. }
  2250. /* stop when tracing is finished */
  2251. if (trace_empty(iter))
  2252. goto out;
  2253. if (cnt >= PAGE_SIZE)
  2254. cnt = PAGE_SIZE - 1;
  2255. /* reset all but tr, trace, and overruns */
  2256. memset(&iter->seq, 0,
  2257. sizeof(struct trace_iterator) -
  2258. offsetof(struct trace_iterator, seq));
  2259. iter->pos = -1;
  2260. /*
  2261. * We need to stop all tracing on all CPUS to read the
  2262. * the next buffer. This is a bit expensive, but is
  2263. * not done often. We fill all what we can read,
  2264. * and then release the locks again.
  2265. */
  2266. cpus_clear(mask);
  2267. local_irq_save(flags);
  2268. #ifdef CONFIG_FTRACE
  2269. ftrace_save = ftrace_enabled;
  2270. ftrace_enabled = 0;
  2271. #endif
  2272. smp_wmb();
  2273. for_each_tracing_cpu(cpu) {
  2274. data = iter->tr->data[cpu];
  2275. if (!head_page(data) || !data->trace_idx)
  2276. continue;
  2277. atomic_inc(&data->disabled);
  2278. cpu_set(cpu, mask);
  2279. }
  2280. for_each_cpu_mask(cpu, mask) {
  2281. data = iter->tr->data[cpu];
  2282. __raw_spin_lock(&data->lock);
  2283. if (data->overrun > iter->last_overrun[cpu])
  2284. iter->overrun[cpu] +=
  2285. data->overrun - iter->last_overrun[cpu];
  2286. iter->last_overrun[cpu] = data->overrun;
  2287. }
  2288. while (find_next_entry_inc(iter) != NULL) {
  2289. int ret;
  2290. int len = iter->seq.len;
  2291. ret = print_trace_line(iter);
  2292. if (!ret) {
  2293. /* don't print partial lines */
  2294. iter->seq.len = len;
  2295. break;
  2296. }
  2297. trace_consume(iter);
  2298. if (iter->seq.len >= cnt)
  2299. break;
  2300. }
  2301. for_each_cpu_mask(cpu, mask) {
  2302. data = iter->tr->data[cpu];
  2303. __raw_spin_unlock(&data->lock);
  2304. }
  2305. for_each_cpu_mask(cpu, mask) {
  2306. data = iter->tr->data[cpu];
  2307. atomic_dec(&data->disabled);
  2308. }
  2309. #ifdef CONFIG_FTRACE
  2310. ftrace_enabled = ftrace_save;
  2311. #endif
  2312. local_irq_restore(flags);
  2313. /* Now copy what we have to the user */
  2314. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2315. if (iter->seq.readpos >= iter->seq.len)
  2316. trace_seq_reset(&iter->seq);
  2317. if (sret == -EBUSY)
  2318. sret = 0;
  2319. out:
  2320. mutex_unlock(&trace_types_lock);
  2321. return sret;
  2322. }
  2323. static ssize_t
  2324. tracing_entries_read(struct file *filp, char __user *ubuf,
  2325. size_t cnt, loff_t *ppos)
  2326. {
  2327. struct trace_array *tr = filp->private_data;
  2328. char buf[64];
  2329. int r;
  2330. r = sprintf(buf, "%lu\n", tr->entries);
  2331. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2332. }
  2333. static ssize_t
  2334. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2335. size_t cnt, loff_t *ppos)
  2336. {
  2337. unsigned long val;
  2338. char buf[64];
  2339. int i, ret;
  2340. if (cnt >= sizeof(buf))
  2341. return -EINVAL;
  2342. if (copy_from_user(&buf, ubuf, cnt))
  2343. return -EFAULT;
  2344. buf[cnt] = 0;
  2345. ret = strict_strtoul(buf, 10, &val);
  2346. if (ret < 0)
  2347. return ret;
  2348. /* must have at least 1 entry */
  2349. if (!val)
  2350. return -EINVAL;
  2351. mutex_lock(&trace_types_lock);
  2352. if (current_trace != &no_tracer) {
  2353. cnt = -EBUSY;
  2354. pr_info("ftrace: set current_tracer to none"
  2355. " before modifying buffer size\n");
  2356. goto out;
  2357. }
  2358. if (val > global_trace.entries) {
  2359. long pages_requested;
  2360. unsigned long freeable_pages;
  2361. /* make sure we have enough memory before mapping */
  2362. pages_requested =
  2363. (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
  2364. /* account for each buffer (and max_tr) */
  2365. pages_requested *= tracing_nr_buffers * 2;
  2366. /* Check for overflow */
  2367. if (pages_requested < 0) {
  2368. cnt = -ENOMEM;
  2369. goto out;
  2370. }
  2371. freeable_pages = determine_dirtyable_memory();
  2372. /* we only allow to request 1/4 of useable memory */
  2373. if (pages_requested >
  2374. ((freeable_pages + tracing_pages_allocated) / 4)) {
  2375. cnt = -ENOMEM;
  2376. goto out;
  2377. }
  2378. while (global_trace.entries < val) {
  2379. if (trace_alloc_page()) {
  2380. cnt = -ENOMEM;
  2381. goto out;
  2382. }
  2383. /* double check that we don't go over the known pages */
  2384. if (tracing_pages_allocated > pages_requested)
  2385. break;
  2386. }
  2387. } else {
  2388. /* include the number of entries in val (inc of page entries) */
  2389. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  2390. trace_free_page();
  2391. }
  2392. /* check integrity */
  2393. for_each_tracing_cpu(i)
  2394. check_pages(global_trace.data[i]);
  2395. filp->f_pos += cnt;
  2396. /* If check pages failed, return ENOMEM */
  2397. if (tracing_disabled)
  2398. cnt = -ENOMEM;
  2399. out:
  2400. max_tr.entries = global_trace.entries;
  2401. mutex_unlock(&trace_types_lock);
  2402. return cnt;
  2403. }
  2404. static struct file_operations tracing_max_lat_fops = {
  2405. .open = tracing_open_generic,
  2406. .read = tracing_max_lat_read,
  2407. .write = tracing_max_lat_write,
  2408. };
  2409. static struct file_operations tracing_ctrl_fops = {
  2410. .open = tracing_open_generic,
  2411. .read = tracing_ctrl_read,
  2412. .write = tracing_ctrl_write,
  2413. };
  2414. static struct file_operations set_tracer_fops = {
  2415. .open = tracing_open_generic,
  2416. .read = tracing_set_trace_read,
  2417. .write = tracing_set_trace_write,
  2418. };
  2419. static struct file_operations tracing_pipe_fops = {
  2420. .open = tracing_open_pipe,
  2421. .poll = tracing_poll_pipe,
  2422. .read = tracing_read_pipe,
  2423. .release = tracing_release_pipe,
  2424. };
  2425. static struct file_operations tracing_entries_fops = {
  2426. .open = tracing_open_generic,
  2427. .read = tracing_entries_read,
  2428. .write = tracing_entries_write,
  2429. };
  2430. #ifdef CONFIG_DYNAMIC_FTRACE
  2431. static ssize_t
  2432. tracing_read_long(struct file *filp, char __user *ubuf,
  2433. size_t cnt, loff_t *ppos)
  2434. {
  2435. unsigned long *p = filp->private_data;
  2436. char buf[64];
  2437. int r;
  2438. r = sprintf(buf, "%ld\n", *p);
  2439. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2440. }
  2441. static struct file_operations tracing_read_long_fops = {
  2442. .open = tracing_open_generic,
  2443. .read = tracing_read_long,
  2444. };
  2445. #endif
  2446. static struct dentry *d_tracer;
  2447. struct dentry *tracing_init_dentry(void)
  2448. {
  2449. static int once;
  2450. if (d_tracer)
  2451. return d_tracer;
  2452. d_tracer = debugfs_create_dir("tracing", NULL);
  2453. if (!d_tracer && !once) {
  2454. once = 1;
  2455. pr_warning("Could not create debugfs directory 'tracing'\n");
  2456. return NULL;
  2457. }
  2458. return d_tracer;
  2459. }
  2460. #ifdef CONFIG_FTRACE_SELFTEST
  2461. /* Let selftest have access to static functions in this file */
  2462. #include "trace_selftest.c"
  2463. #endif
  2464. static __init void tracer_init_debugfs(void)
  2465. {
  2466. struct dentry *d_tracer;
  2467. struct dentry *entry;
  2468. d_tracer = tracing_init_dentry();
  2469. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2470. &global_trace, &tracing_ctrl_fops);
  2471. if (!entry)
  2472. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2473. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2474. NULL, &tracing_iter_fops);
  2475. if (!entry)
  2476. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2477. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2478. NULL, &tracing_cpumask_fops);
  2479. if (!entry)
  2480. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2481. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2482. &global_trace, &tracing_lt_fops);
  2483. if (!entry)
  2484. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2485. entry = debugfs_create_file("trace", 0444, d_tracer,
  2486. &global_trace, &tracing_fops);
  2487. if (!entry)
  2488. pr_warning("Could not create debugfs 'trace' entry\n");
  2489. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2490. &global_trace, &show_traces_fops);
  2491. if (!entry)
  2492. pr_warning("Could not create debugfs 'trace' entry\n");
  2493. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2494. &global_trace, &set_tracer_fops);
  2495. if (!entry)
  2496. pr_warning("Could not create debugfs 'trace' entry\n");
  2497. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2498. &tracing_max_latency,
  2499. &tracing_max_lat_fops);
  2500. if (!entry)
  2501. pr_warning("Could not create debugfs "
  2502. "'tracing_max_latency' entry\n");
  2503. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2504. &tracing_thresh, &tracing_max_lat_fops);
  2505. if (!entry)
  2506. pr_warning("Could not create debugfs "
  2507. "'tracing_threash' entry\n");
  2508. entry = debugfs_create_file("README", 0644, d_tracer,
  2509. NULL, &tracing_readme_fops);
  2510. if (!entry)
  2511. pr_warning("Could not create debugfs 'README' entry\n");
  2512. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2513. NULL, &tracing_pipe_fops);
  2514. if (!entry)
  2515. pr_warning("Could not create debugfs "
  2516. "'tracing_threash' entry\n");
  2517. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2518. &global_trace, &tracing_entries_fops);
  2519. if (!entry)
  2520. pr_warning("Could not create debugfs "
  2521. "'tracing_threash' entry\n");
  2522. #ifdef CONFIG_DYNAMIC_FTRACE
  2523. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2524. &ftrace_update_tot_cnt,
  2525. &tracing_read_long_fops);
  2526. if (!entry)
  2527. pr_warning("Could not create debugfs "
  2528. "'dyn_ftrace_total_info' entry\n");
  2529. #endif
  2530. #ifdef CONFIG_SYSPROF_TRACER
  2531. init_tracer_sysprof_debugfs(d_tracer);
  2532. #endif
  2533. }
  2534. #define TRACE_BUF_SIZE 1024
  2535. #define TRACE_PRINT_BUF_SIZE \
  2536. (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
  2537. #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
  2538. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2539. {
  2540. struct trace_array *tr = &global_trace;
  2541. static DEFINE_SPINLOCK(trace_buf_lock);
  2542. static char trace_buf[TRACE_BUF_SIZE];
  2543. struct trace_array_cpu *data;
  2544. struct trace_entry *entry;
  2545. unsigned long flags;
  2546. long disabled;
  2547. va_list ap;
  2548. int cpu, len = 0, write, written = 0;
  2549. if (likely(!ftrace_function_enabled))
  2550. return 0;
  2551. local_irq_save(flags);
  2552. cpu = raw_smp_processor_id();
  2553. data = tr->data[cpu];
  2554. disabled = atomic_inc_return(&data->disabled);
  2555. if (unlikely(disabled != 1 || !ftrace_function_enabled))
  2556. goto out;
  2557. spin_lock(&trace_buf_lock);
  2558. va_start(ap, fmt);
  2559. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
  2560. va_end(ap);
  2561. len = min(len, TRACE_BUF_SIZE-1);
  2562. trace_buf[len] = 0;
  2563. __raw_spin_lock(&data->lock);
  2564. entry = tracing_get_trace_entry(tr, data);
  2565. tracing_generic_entry_update(entry, flags);
  2566. entry->type = TRACE_PRINT;
  2567. entry->field.print.ip = ip;
  2568. write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
  2569. memcpy(&entry->field.print.buf, trace_buf, write);
  2570. entry->field.print.buf[write] = 0;
  2571. written = write;
  2572. if (written != len)
  2573. entry->field.flags |= TRACE_FLAG_CONT;
  2574. while (written != len) {
  2575. entry = tracing_get_trace_entry(tr, data);
  2576. entry->type = TRACE_CONT;
  2577. write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
  2578. memcpy(&entry->cont.buf, trace_buf+written, write);
  2579. entry->cont.buf[write] = 0;
  2580. written += write;
  2581. }
  2582. __raw_spin_unlock(&data->lock);
  2583. spin_unlock(&trace_buf_lock);
  2584. out:
  2585. atomic_dec(&data->disabled);
  2586. local_irq_restore(flags);
  2587. return len;
  2588. }
  2589. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2590. static int trace_alloc_page(void)
  2591. {
  2592. struct trace_array_cpu *data;
  2593. struct page *page, *tmp;
  2594. LIST_HEAD(pages);
  2595. void *array;
  2596. unsigned pages_allocated = 0;
  2597. int i;
  2598. /* first allocate a page for each CPU */
  2599. for_each_tracing_cpu(i) {
  2600. array = (void *)__get_free_page(GFP_KERNEL);
  2601. if (array == NULL) {
  2602. printk(KERN_ERR "tracer: failed to allocate page"
  2603. "for trace buffer!\n");
  2604. goto free_pages;
  2605. }
  2606. pages_allocated++;
  2607. page = virt_to_page(array);
  2608. list_add(&page->lru, &pages);
  2609. /* Only allocate if we are actually using the max trace */
  2610. #ifdef CONFIG_TRACER_MAX_TRACE
  2611. array = (void *)__get_free_page(GFP_KERNEL);
  2612. if (array == NULL) {
  2613. printk(KERN_ERR "tracer: failed to allocate page"
  2614. "for trace buffer!\n");
  2615. goto free_pages;
  2616. }
  2617. pages_allocated++;
  2618. page = virt_to_page(array);
  2619. list_add(&page->lru, &pages);
  2620. #endif
  2621. }
  2622. /* Now that we successfully allocate a page per CPU, add them */
  2623. for_each_tracing_cpu(i) {
  2624. data = global_trace.data[i];
  2625. page = list_entry(pages.next, struct page, lru);
  2626. list_del_init(&page->lru);
  2627. list_add_tail(&page->lru, &data->trace_pages);
  2628. ClearPageLRU(page);
  2629. #ifdef CONFIG_TRACER_MAX_TRACE
  2630. data = max_tr.data[i];
  2631. page = list_entry(pages.next, struct page, lru);
  2632. list_del_init(&page->lru);
  2633. list_add_tail(&page->lru, &data->trace_pages);
  2634. SetPageLRU(page);
  2635. #endif
  2636. }
  2637. tracing_pages_allocated += pages_allocated;
  2638. global_trace.entries += ENTRIES_PER_PAGE;
  2639. return 0;
  2640. free_pages:
  2641. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2642. list_del_init(&page->lru);
  2643. __free_page(page);
  2644. }
  2645. return -ENOMEM;
  2646. }
  2647. static int trace_free_page(void)
  2648. {
  2649. struct trace_array_cpu *data;
  2650. struct page *page;
  2651. struct list_head *p;
  2652. int i;
  2653. int ret = 0;
  2654. /* free one page from each buffer */
  2655. for_each_tracing_cpu(i) {
  2656. data = global_trace.data[i];
  2657. p = data->trace_pages.next;
  2658. if (p == &data->trace_pages) {
  2659. /* should never happen */
  2660. WARN_ON(1);
  2661. tracing_disabled = 1;
  2662. ret = -1;
  2663. break;
  2664. }
  2665. page = list_entry(p, struct page, lru);
  2666. ClearPageLRU(page);
  2667. list_del(&page->lru);
  2668. tracing_pages_allocated--;
  2669. tracing_pages_allocated--;
  2670. __free_page(page);
  2671. tracing_reset(data);
  2672. #ifdef CONFIG_TRACER_MAX_TRACE
  2673. data = max_tr.data[i];
  2674. p = data->trace_pages.next;
  2675. if (p == &data->trace_pages) {
  2676. /* should never happen */
  2677. WARN_ON(1);
  2678. tracing_disabled = 1;
  2679. ret = -1;
  2680. break;
  2681. }
  2682. page = list_entry(p, struct page, lru);
  2683. ClearPageLRU(page);
  2684. list_del(&page->lru);
  2685. __free_page(page);
  2686. tracing_reset(data);
  2687. #endif
  2688. }
  2689. global_trace.entries -= ENTRIES_PER_PAGE;
  2690. return ret;
  2691. }
  2692. __init static int tracer_alloc_buffers(void)
  2693. {
  2694. struct trace_array_cpu *data;
  2695. void *array;
  2696. struct page *page;
  2697. int pages = 0;
  2698. int ret = -ENOMEM;
  2699. int i;
  2700. /* TODO: make the number of buffers hot pluggable with CPUS */
  2701. tracing_nr_buffers = num_possible_cpus();
  2702. tracing_buffer_mask = cpu_possible_map;
  2703. /* Allocate the first page for all buffers */
  2704. for_each_tracing_cpu(i) {
  2705. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2706. max_tr.data[i] = &per_cpu(max_data, i);
  2707. array = (void *)__get_free_page(GFP_KERNEL);
  2708. if (array == NULL) {
  2709. printk(KERN_ERR "tracer: failed to allocate page"
  2710. "for trace buffer!\n");
  2711. goto free_buffers;
  2712. }
  2713. /* set the array to the list */
  2714. INIT_LIST_HEAD(&data->trace_pages);
  2715. page = virt_to_page(array);
  2716. list_add(&page->lru, &data->trace_pages);
  2717. /* use the LRU flag to differentiate the two buffers */
  2718. ClearPageLRU(page);
  2719. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2720. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2721. /* Only allocate if we are actually using the max trace */
  2722. #ifdef CONFIG_TRACER_MAX_TRACE
  2723. array = (void *)__get_free_page(GFP_KERNEL);
  2724. if (array == NULL) {
  2725. printk(KERN_ERR "tracer: failed to allocate page"
  2726. "for trace buffer!\n");
  2727. goto free_buffers;
  2728. }
  2729. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2730. page = virt_to_page(array);
  2731. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2732. SetPageLRU(page);
  2733. #endif
  2734. }
  2735. /*
  2736. * Since we allocate by orders of pages, we may be able to
  2737. * round up a bit.
  2738. */
  2739. global_trace.entries = ENTRIES_PER_PAGE;
  2740. pages++;
  2741. while (global_trace.entries < trace_nr_entries) {
  2742. if (trace_alloc_page())
  2743. break;
  2744. pages++;
  2745. }
  2746. max_tr.entries = global_trace.entries;
  2747. pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
  2748. pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
  2749. pr_info(" actual entries %ld\n", global_trace.entries);
  2750. tracer_init_debugfs();
  2751. trace_init_cmdlines();
  2752. register_tracer(&no_tracer);
  2753. current_trace = &no_tracer;
  2754. /* All seems OK, enable tracing */
  2755. global_trace.ctrl = tracer_enabled;
  2756. tracing_disabled = 0;
  2757. return 0;
  2758. free_buffers:
  2759. for (i-- ; i >= 0; i--) {
  2760. struct page *page, *tmp;
  2761. struct trace_array_cpu *data = global_trace.data[i];
  2762. if (data) {
  2763. list_for_each_entry_safe(page, tmp,
  2764. &data->trace_pages, lru) {
  2765. list_del_init(&page->lru);
  2766. __free_page(page);
  2767. }
  2768. }
  2769. #ifdef CONFIG_TRACER_MAX_TRACE
  2770. data = max_tr.data[i];
  2771. if (data) {
  2772. list_for_each_entry_safe(page, tmp,
  2773. &data->trace_pages, lru) {
  2774. list_del_init(&page->lru);
  2775. __free_page(page);
  2776. }
  2777. }
  2778. #endif
  2779. }
  2780. return ret;
  2781. }
  2782. fs_initcall(tracer_alloc_buffers);