trace.c 80 KB

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