trace.c 80 KB

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