trace.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549
  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. static inline 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. static inline 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. #ifdef CONFIG_MMIOTRACE
  723. void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
  724. struct mmiotrace_rw *rw)
  725. {
  726. struct trace_entry *entry;
  727. unsigned long irq_flags;
  728. raw_local_irq_save(irq_flags);
  729. __raw_spin_lock(&data->lock);
  730. entry = tracing_get_trace_entry(tr, data);
  731. tracing_generic_entry_update(entry, 0);
  732. entry->type = TRACE_MMIO_RW;
  733. entry->field.mmiorw = *rw;
  734. __raw_spin_unlock(&data->lock);
  735. raw_local_irq_restore(irq_flags);
  736. trace_wake_up();
  737. }
  738. void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
  739. struct mmiotrace_map *map)
  740. {
  741. struct trace_entry *entry;
  742. unsigned long irq_flags;
  743. raw_local_irq_save(irq_flags);
  744. __raw_spin_lock(&data->lock);
  745. entry = tracing_get_trace_entry(tr, data);
  746. tracing_generic_entry_update(entry, 0);
  747. entry->type = TRACE_MMIO_MAP;
  748. entry->field.mmiomap = *map;
  749. __raw_spin_unlock(&data->lock);
  750. raw_local_irq_restore(irq_flags);
  751. trace_wake_up();
  752. }
  753. #endif
  754. void __trace_stack(struct trace_array *tr,
  755. struct trace_array_cpu *data,
  756. unsigned long flags,
  757. int skip)
  758. {
  759. struct trace_entry *entry;
  760. struct stack_trace trace;
  761. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  762. return;
  763. entry = tracing_get_trace_entry(tr, data);
  764. tracing_generic_entry_update(entry, flags);
  765. entry->type = TRACE_STACK;
  766. memset(&entry->field.stack, 0, sizeof(entry->field.stack));
  767. trace.nr_entries = 0;
  768. trace.max_entries = FTRACE_STACK_ENTRIES;
  769. trace.skip = skip;
  770. trace.entries = entry->field.stack.caller;
  771. save_stack_trace(&trace);
  772. }
  773. void
  774. __trace_special(void *__tr, void *__data,
  775. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  776. {
  777. struct trace_array_cpu *data = __data;
  778. struct trace_array *tr = __tr;
  779. struct trace_entry *entry;
  780. unsigned long irq_flags;
  781. raw_local_irq_save(irq_flags);
  782. __raw_spin_lock(&data->lock);
  783. entry = tracing_get_trace_entry(tr, data);
  784. tracing_generic_entry_update(entry, 0);
  785. entry->type = TRACE_SPECIAL;
  786. entry->field.special.arg1 = arg1;
  787. entry->field.special.arg2 = arg2;
  788. entry->field.special.arg3 = arg3;
  789. __trace_stack(tr, data, irq_flags, 4);
  790. __raw_spin_unlock(&data->lock);
  791. raw_local_irq_restore(irq_flags);
  792. trace_wake_up();
  793. }
  794. void
  795. tracing_sched_switch_trace(struct trace_array *tr,
  796. struct trace_array_cpu *data,
  797. struct task_struct *prev,
  798. struct task_struct *next,
  799. unsigned long flags)
  800. {
  801. struct trace_entry *entry;
  802. unsigned long irq_flags;
  803. raw_local_irq_save(irq_flags);
  804. __raw_spin_lock(&data->lock);
  805. entry = tracing_get_trace_entry(tr, data);
  806. tracing_generic_entry_update(entry, flags);
  807. entry->type = TRACE_CTX;
  808. entry->field.ctx.prev_pid = prev->pid;
  809. entry->field.ctx.prev_prio = prev->prio;
  810. entry->field.ctx.prev_state = prev->state;
  811. entry->field.ctx.next_pid = next->pid;
  812. entry->field.ctx.next_prio = next->prio;
  813. entry->field.ctx.next_state = next->state;
  814. __trace_stack(tr, data, flags, 5);
  815. __raw_spin_unlock(&data->lock);
  816. raw_local_irq_restore(irq_flags);
  817. }
  818. void
  819. tracing_sched_wakeup_trace(struct trace_array *tr,
  820. struct trace_array_cpu *data,
  821. struct task_struct *wakee,
  822. struct task_struct *curr,
  823. unsigned long flags)
  824. {
  825. struct trace_entry *entry;
  826. unsigned long irq_flags;
  827. raw_local_irq_save(irq_flags);
  828. __raw_spin_lock(&data->lock);
  829. entry = tracing_get_trace_entry(tr, data);
  830. tracing_generic_entry_update(entry, flags);
  831. entry->type = TRACE_WAKE;
  832. entry->field.ctx.prev_pid = curr->pid;
  833. entry->field.ctx.prev_prio = curr->prio;
  834. entry->field.ctx.prev_state = curr->state;
  835. entry->field.ctx.next_pid = wakee->pid;
  836. entry->field.ctx.next_prio = wakee->prio;
  837. entry->field.ctx.next_state = wakee->state;
  838. __trace_stack(tr, data, flags, 6);
  839. __raw_spin_unlock(&data->lock);
  840. raw_local_irq_restore(irq_flags);
  841. trace_wake_up();
  842. }
  843. void
  844. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  845. {
  846. struct trace_array *tr = &global_trace;
  847. struct trace_array_cpu *data;
  848. unsigned long flags;
  849. long disabled;
  850. int cpu;
  851. if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
  852. return;
  853. local_irq_save(flags);
  854. cpu = raw_smp_processor_id();
  855. data = tr->data[cpu];
  856. disabled = atomic_inc_return(&data->disabled);
  857. if (likely(disabled == 1))
  858. __trace_special(tr, data, arg1, arg2, arg3);
  859. atomic_dec(&data->disabled);
  860. local_irq_restore(flags);
  861. }
  862. #ifdef CONFIG_FTRACE
  863. static void
  864. function_trace_call(unsigned long ip, unsigned long parent_ip)
  865. {
  866. struct trace_array *tr = &global_trace;
  867. struct trace_array_cpu *data;
  868. unsigned long flags;
  869. long disabled;
  870. int cpu;
  871. if (unlikely(!ftrace_function_enabled))
  872. return;
  873. if (skip_trace(ip))
  874. return;
  875. local_irq_save(flags);
  876. cpu = raw_smp_processor_id();
  877. data = tr->data[cpu];
  878. disabled = atomic_inc_return(&data->disabled);
  879. if (likely(disabled == 1))
  880. trace_function(tr, data, ip, parent_ip, flags);
  881. atomic_dec(&data->disabled);
  882. local_irq_restore(flags);
  883. }
  884. static struct ftrace_ops trace_ops __read_mostly =
  885. {
  886. .func = function_trace_call,
  887. };
  888. void tracing_start_function_trace(void)
  889. {
  890. ftrace_function_enabled = 0;
  891. register_ftrace_function(&trace_ops);
  892. if (tracer_enabled)
  893. ftrace_function_enabled = 1;
  894. }
  895. void tracing_stop_function_trace(void)
  896. {
  897. ftrace_function_enabled = 0;
  898. unregister_ftrace_function(&trace_ops);
  899. }
  900. #endif
  901. enum trace_file_type {
  902. TRACE_FILE_LAT_FMT = 1,
  903. };
  904. /* Return the current entry. */
  905. static struct trace_entry *
  906. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  907. struct trace_iterator *iter, int cpu)
  908. {
  909. struct page *page;
  910. struct trace_entry *array;
  911. if (iter->next_idx[cpu] >= tr->entries ||
  912. iter->next_idx[cpu] >= data->trace_idx ||
  913. (data->trace_head == data->trace_tail &&
  914. data->trace_head_idx == data->trace_tail_idx))
  915. return NULL;
  916. if (!iter->next_page[cpu]) {
  917. /* Initialize the iterator for this cpu trace buffer */
  918. WARN_ON(!data->trace_tail);
  919. page = virt_to_page(data->trace_tail);
  920. iter->next_page[cpu] = &page->lru;
  921. iter->next_page_idx[cpu] = data->trace_tail_idx;
  922. }
  923. page = list_entry(iter->next_page[cpu], struct page, lru);
  924. BUG_ON(&data->trace_pages == &page->lru);
  925. array = page_address(page);
  926. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  927. return &array[iter->next_page_idx[cpu]];
  928. }
  929. /* Increment the index counter of an iterator by one */
  930. static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
  931. {
  932. iter->next_idx[cpu]++;
  933. iter->next_page_idx[cpu]++;
  934. if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
  935. struct trace_array_cpu *data = iter->tr->data[cpu];
  936. iter->next_page_idx[cpu] = 0;
  937. iter->next_page[cpu] =
  938. trace_next_list(data, iter->next_page[cpu]);
  939. }
  940. }
  941. static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
  942. {
  943. iter->idx++;
  944. __trace_iterator_increment(iter, cpu);
  945. }
  946. static struct trace_entry *
  947. trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
  948. struct trace_iterator *iter, int cpu)
  949. {
  950. struct list_head *next_page;
  951. struct trace_entry *ent;
  952. int idx, next_idx, next_page_idx;
  953. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  954. if (likely(!ent || ent->type != TRACE_CONT))
  955. return ent;
  956. /* save the iterator details */
  957. idx = iter->idx;
  958. next_idx = iter->next_idx[cpu];
  959. next_page_idx = iter->next_page_idx[cpu];
  960. next_page = iter->next_page[cpu];
  961. /* find a real entry */
  962. do {
  963. __trace_iterator_increment(iter, cpu);
  964. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  965. } while (ent && ent->type != TRACE_CONT);
  966. /* reset the iterator */
  967. iter->idx = idx;
  968. iter->next_idx[cpu] = next_idx;
  969. iter->next_page_idx[cpu] = next_page_idx;
  970. iter->next_page[cpu] = next_page;
  971. return ent;
  972. }
  973. static struct trace_entry *
  974. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
  975. {
  976. struct trace_array *tr = iter->tr;
  977. struct trace_entry *ent, *next = NULL;
  978. int next_cpu = -1;
  979. int cpu;
  980. for_each_tracing_cpu(cpu) {
  981. if (!head_page(tr->data[cpu]))
  982. continue;
  983. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  984. if (ent && ent->type == TRACE_CONT) {
  985. struct trace_array_cpu *data = tr->data[cpu];
  986. if (!inc)
  987. ent = trace_entry_next(tr, data, iter, cpu);
  988. else {
  989. while (ent && ent->type == TRACE_CONT) {
  990. __trace_iterator_increment(iter, cpu);
  991. ent = trace_entry_idx(tr, tr->data[cpu],
  992. iter, cpu);
  993. }
  994. }
  995. }
  996. /*
  997. * Pick the entry with the smallest timestamp:
  998. */
  999. if (ent && (!next || ent->field.t < next->field.t)) {
  1000. next = ent;
  1001. next_cpu = cpu;
  1002. }
  1003. }
  1004. if (ent_cpu)
  1005. *ent_cpu = next_cpu;
  1006. return next;
  1007. }
  1008. /* Find the next real entry, without updating the iterator itself */
  1009. static struct trace_entry *
  1010. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  1011. {
  1012. return __find_next_entry(iter, ent_cpu, 0);
  1013. }
  1014. /* Find the next real entry, and increment the iterator to the next entry */
  1015. static void *find_next_entry_inc(struct trace_iterator *iter)
  1016. {
  1017. struct trace_entry *next;
  1018. int next_cpu = -1;
  1019. next = __find_next_entry(iter, &next_cpu, 1);
  1020. iter->prev_ent = iter->ent;
  1021. iter->prev_cpu = iter->cpu;
  1022. iter->ent = next;
  1023. iter->cpu = next_cpu;
  1024. if (next)
  1025. trace_iterator_increment(iter, iter->cpu);
  1026. return next ? iter : NULL;
  1027. }
  1028. static void trace_consume(struct trace_iterator *iter)
  1029. {
  1030. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  1031. struct trace_entry *ent;
  1032. again:
  1033. data->trace_tail_idx++;
  1034. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  1035. data->trace_tail = trace_next_page(data, data->trace_tail);
  1036. data->trace_tail_idx = 0;
  1037. }
  1038. /* Check if we empty it, then reset the index */
  1039. if (data->trace_head == data->trace_tail &&
  1040. data->trace_head_idx == data->trace_tail_idx)
  1041. data->trace_idx = 0;
  1042. ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
  1043. iter, iter->cpu);
  1044. if (ent && ent->type == TRACE_CONT)
  1045. goto again;
  1046. }
  1047. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1048. {
  1049. struct trace_iterator *iter = m->private;
  1050. int i = (int)*pos;
  1051. void *ent;
  1052. (*pos)++;
  1053. /* can't go backwards */
  1054. if (iter->idx > i)
  1055. return NULL;
  1056. if (iter->idx < 0)
  1057. ent = find_next_entry_inc(iter);
  1058. else
  1059. ent = iter;
  1060. while (ent && iter->idx < i)
  1061. ent = find_next_entry_inc(iter);
  1062. iter->pos = *pos;
  1063. return ent;
  1064. }
  1065. static void *s_start(struct seq_file *m, loff_t *pos)
  1066. {
  1067. struct trace_iterator *iter = m->private;
  1068. void *p = NULL;
  1069. loff_t l = 0;
  1070. int i;
  1071. mutex_lock(&trace_types_lock);
  1072. if (!current_trace || current_trace != iter->trace) {
  1073. mutex_unlock(&trace_types_lock);
  1074. return NULL;
  1075. }
  1076. atomic_inc(&trace_record_cmdline_disabled);
  1077. /* let the tracer grab locks here if needed */
  1078. if (current_trace->start)
  1079. current_trace->start(iter);
  1080. if (*pos != iter->pos) {
  1081. iter->ent = NULL;
  1082. iter->cpu = 0;
  1083. iter->idx = -1;
  1084. iter->prev_ent = NULL;
  1085. iter->prev_cpu = -1;
  1086. for_each_tracing_cpu(i) {
  1087. iter->next_idx[i] = 0;
  1088. iter->next_page[i] = NULL;
  1089. }
  1090. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1091. ;
  1092. } else {
  1093. l = *pos - 1;
  1094. p = s_next(m, p, &l);
  1095. }
  1096. return p;
  1097. }
  1098. static void s_stop(struct seq_file *m, void *p)
  1099. {
  1100. struct trace_iterator *iter = m->private;
  1101. atomic_dec(&trace_record_cmdline_disabled);
  1102. /* let the tracer release locks here if needed */
  1103. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  1104. iter->trace->stop(iter);
  1105. mutex_unlock(&trace_types_lock);
  1106. }
  1107. #define KRETPROBE_MSG "[unknown/kretprobe'd]"
  1108. #ifdef CONFIG_KRETPROBES
  1109. static inline int kretprobed(unsigned long addr)
  1110. {
  1111. return addr == (unsigned long)kretprobe_trampoline;
  1112. }
  1113. #else
  1114. static inline int kretprobed(unsigned long addr)
  1115. {
  1116. return 0;
  1117. }
  1118. #endif /* CONFIG_KRETPROBES */
  1119. static int
  1120. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  1121. {
  1122. #ifdef CONFIG_KALLSYMS
  1123. char str[KSYM_SYMBOL_LEN];
  1124. kallsyms_lookup(address, NULL, NULL, NULL, str);
  1125. return trace_seq_printf(s, fmt, str);
  1126. #endif
  1127. return 1;
  1128. }
  1129. static int
  1130. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  1131. unsigned long address)
  1132. {
  1133. #ifdef CONFIG_KALLSYMS
  1134. char str[KSYM_SYMBOL_LEN];
  1135. sprint_symbol(str, address);
  1136. return trace_seq_printf(s, fmt, str);
  1137. #endif
  1138. return 1;
  1139. }
  1140. #ifndef CONFIG_64BIT
  1141. # define IP_FMT "%08lx"
  1142. #else
  1143. # define IP_FMT "%016lx"
  1144. #endif
  1145. static int
  1146. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  1147. {
  1148. int ret;
  1149. if (!ip)
  1150. return trace_seq_printf(s, "0");
  1151. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  1152. ret = seq_print_sym_offset(s, "%s", ip);
  1153. else
  1154. ret = seq_print_sym_short(s, "%s", ip);
  1155. if (!ret)
  1156. return 0;
  1157. if (sym_flags & TRACE_ITER_SYM_ADDR)
  1158. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1159. return ret;
  1160. }
  1161. static void print_lat_help_header(struct seq_file *m)
  1162. {
  1163. seq_puts(m, "# _------=> CPU# \n");
  1164. seq_puts(m, "# / _-----=> irqs-off \n");
  1165. seq_puts(m, "# | / _----=> need-resched \n");
  1166. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1167. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1168. seq_puts(m, "# |||| / \n");
  1169. seq_puts(m, "# ||||| delay \n");
  1170. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1171. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1172. }
  1173. static void print_func_help_header(struct seq_file *m)
  1174. {
  1175. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1176. seq_puts(m, "# | | | | |\n");
  1177. }
  1178. static void
  1179. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1180. {
  1181. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1182. struct trace_array *tr = iter->tr;
  1183. struct trace_array_cpu *data = tr->data[tr->cpu];
  1184. struct tracer *type = current_trace;
  1185. unsigned long total = 0;
  1186. unsigned long entries = 0;
  1187. int cpu;
  1188. const char *name = "preemption";
  1189. if (type)
  1190. name = type->name;
  1191. for_each_tracing_cpu(cpu) {
  1192. if (head_page(tr->data[cpu])) {
  1193. total += tr->data[cpu]->trace_idx;
  1194. if (tr->data[cpu]->trace_idx > tr->entries)
  1195. entries += tr->entries;
  1196. else
  1197. entries += tr->data[cpu]->trace_idx;
  1198. }
  1199. }
  1200. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1201. name, UTS_RELEASE);
  1202. seq_puts(m, "-----------------------------------"
  1203. "---------------------------------\n");
  1204. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1205. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1206. nsecs_to_usecs(data->saved_latency),
  1207. entries,
  1208. total,
  1209. tr->cpu,
  1210. #if defined(CONFIG_PREEMPT_NONE)
  1211. "server",
  1212. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1213. "desktop",
  1214. #elif defined(CONFIG_PREEMPT)
  1215. "preempt",
  1216. #else
  1217. "unknown",
  1218. #endif
  1219. /* These are reserved for later use */
  1220. 0, 0, 0, 0);
  1221. #ifdef CONFIG_SMP
  1222. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1223. #else
  1224. seq_puts(m, ")\n");
  1225. #endif
  1226. seq_puts(m, " -----------------\n");
  1227. seq_printf(m, " | task: %.16s-%d "
  1228. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1229. data->comm, data->pid, data->uid, data->nice,
  1230. data->policy, data->rt_priority);
  1231. seq_puts(m, " -----------------\n");
  1232. if (data->critical_start) {
  1233. seq_puts(m, " => started at: ");
  1234. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1235. trace_print_seq(m, &iter->seq);
  1236. seq_puts(m, "\n => ended at: ");
  1237. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1238. trace_print_seq(m, &iter->seq);
  1239. seq_puts(m, "\n");
  1240. }
  1241. seq_puts(m, "\n");
  1242. }
  1243. static void
  1244. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1245. {
  1246. struct trace_field *field = &entry->field;
  1247. int hardirq, softirq;
  1248. char *comm;
  1249. comm = trace_find_cmdline(field->pid);
  1250. trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
  1251. trace_seq_printf(s, "%3d", cpu);
  1252. trace_seq_printf(s, "%c%c",
  1253. (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  1254. ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1255. hardirq = field->flags & TRACE_FLAG_HARDIRQ;
  1256. softirq = field->flags & TRACE_FLAG_SOFTIRQ;
  1257. if (hardirq && softirq) {
  1258. trace_seq_putc(s, 'H');
  1259. } else {
  1260. if (hardirq) {
  1261. trace_seq_putc(s, 'h');
  1262. } else {
  1263. if (softirq)
  1264. trace_seq_putc(s, 's');
  1265. else
  1266. trace_seq_putc(s, '.');
  1267. }
  1268. }
  1269. if (field->preempt_count)
  1270. trace_seq_printf(s, "%x", field->preempt_count);
  1271. else
  1272. trace_seq_puts(s, ".");
  1273. }
  1274. unsigned long preempt_mark_thresh = 100;
  1275. static void
  1276. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  1277. unsigned long rel_usecs)
  1278. {
  1279. trace_seq_printf(s, " %4lldus", abs_usecs);
  1280. if (rel_usecs > preempt_mark_thresh)
  1281. trace_seq_puts(s, "!: ");
  1282. else if (rel_usecs > 1)
  1283. trace_seq_puts(s, "+: ");
  1284. else
  1285. trace_seq_puts(s, " : ");
  1286. }
  1287. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1288. static void
  1289. trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1290. {
  1291. struct trace_array *tr = iter->tr;
  1292. struct trace_array_cpu *data = tr->data[iter->cpu];
  1293. struct trace_entry *ent;
  1294. ent = trace_entry_idx(tr, data, iter, iter->cpu);
  1295. if (!ent || ent->type != TRACE_CONT) {
  1296. trace_seq_putc(s, '\n');
  1297. return;
  1298. }
  1299. do {
  1300. trace_seq_printf(s, "%s", ent->cont.buf);
  1301. __trace_iterator_increment(iter, iter->cpu);
  1302. ent = trace_entry_idx(tr, data, iter, iter->cpu);
  1303. } while (ent && ent->type == TRACE_CONT);
  1304. }
  1305. static int
  1306. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1307. {
  1308. struct trace_seq *s = &iter->seq;
  1309. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1310. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  1311. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1312. struct trace_entry *entry = iter->ent;
  1313. struct trace_field *field = &entry->field;
  1314. unsigned long abs_usecs;
  1315. unsigned long rel_usecs;
  1316. char *comm;
  1317. int S, T;
  1318. int i;
  1319. unsigned state;
  1320. if (!next_entry)
  1321. next_entry = entry;
  1322. if (entry->type == TRACE_CONT)
  1323. return 1;
  1324. rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
  1325. abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
  1326. if (verbose) {
  1327. comm = trace_find_cmdline(field->pid);
  1328. trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
  1329. " %ld.%03ldms (+%ld.%03ldms): ",
  1330. comm,
  1331. field->pid, cpu, field->flags,
  1332. field->preempt_count, trace_idx,
  1333. ns2usecs(field->t),
  1334. abs_usecs/1000,
  1335. abs_usecs % 1000, rel_usecs/1000,
  1336. rel_usecs % 1000);
  1337. } else {
  1338. lat_print_generic(s, entry, cpu);
  1339. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1340. }
  1341. switch (entry->type) {
  1342. case TRACE_FN:
  1343. seq_print_ip_sym(s, field->fn.ip, sym_flags);
  1344. trace_seq_puts(s, " (");
  1345. if (kretprobed(field->fn.parent_ip))
  1346. trace_seq_puts(s, KRETPROBE_MSG);
  1347. else
  1348. seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
  1349. trace_seq_puts(s, ")\n");
  1350. break;
  1351. case TRACE_CTX:
  1352. case TRACE_WAKE:
  1353. T = field->ctx.next_state < sizeof(state_to_char) ?
  1354. state_to_char[field->ctx.next_state] : 'X';
  1355. state = field->ctx.prev_state ?
  1356. __ffs(field->ctx.prev_state) + 1 : 0;
  1357. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1358. comm = trace_find_cmdline(field->ctx.next_pid);
  1359. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1360. field->ctx.prev_pid,
  1361. field->ctx.prev_prio,
  1362. S, entry->type == TRACE_CTX ? "==>" : " +",
  1363. field->ctx.next_pid,
  1364. field->ctx.next_prio,
  1365. T, comm);
  1366. break;
  1367. case TRACE_SPECIAL:
  1368. trace_seq_printf(s, "# %ld %ld %ld\n",
  1369. field->special.arg1,
  1370. field->special.arg2,
  1371. field->special.arg3);
  1372. break;
  1373. case TRACE_STACK:
  1374. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1375. if (i)
  1376. trace_seq_puts(s, " <= ");
  1377. seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
  1378. }
  1379. trace_seq_puts(s, "\n");
  1380. break;
  1381. case TRACE_PRINT:
  1382. seq_print_ip_sym(s, field->print.ip, sym_flags);
  1383. trace_seq_printf(s, ": %s", field->print.buf);
  1384. if (field->flags & TRACE_FLAG_CONT)
  1385. trace_seq_print_cont(s, iter);
  1386. break;
  1387. default:
  1388. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1389. }
  1390. return 1;
  1391. }
  1392. static int print_trace_fmt(struct trace_iterator *iter)
  1393. {
  1394. struct trace_seq *s = &iter->seq;
  1395. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1396. struct trace_entry *entry;
  1397. struct trace_field *field;
  1398. unsigned long usec_rem;
  1399. unsigned long long t;
  1400. unsigned long secs;
  1401. char *comm;
  1402. int ret;
  1403. int S, T;
  1404. int i;
  1405. entry = iter->ent;
  1406. if (entry->type == TRACE_CONT)
  1407. return 1;
  1408. field = &entry->field;
  1409. comm = trace_find_cmdline(iter->ent->field.pid);
  1410. t = ns2usecs(field->t);
  1411. usec_rem = do_div(t, 1000000ULL);
  1412. secs = (unsigned long)t;
  1413. ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
  1414. if (!ret)
  1415. return 0;
  1416. ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
  1417. if (!ret)
  1418. return 0;
  1419. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1420. if (!ret)
  1421. return 0;
  1422. switch (entry->type) {
  1423. case TRACE_FN:
  1424. ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
  1425. if (!ret)
  1426. return 0;
  1427. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1428. field->fn.parent_ip) {
  1429. ret = trace_seq_printf(s, " <-");
  1430. if (!ret)
  1431. return 0;
  1432. if (kretprobed(field->fn.parent_ip))
  1433. ret = trace_seq_puts(s, KRETPROBE_MSG);
  1434. else
  1435. ret = seq_print_ip_sym(s,
  1436. field->fn.parent_ip,
  1437. sym_flags);
  1438. if (!ret)
  1439. return 0;
  1440. }
  1441. ret = trace_seq_printf(s, "\n");
  1442. if (!ret)
  1443. return 0;
  1444. break;
  1445. case TRACE_CTX:
  1446. case TRACE_WAKE:
  1447. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1448. state_to_char[field->ctx.prev_state] : 'X';
  1449. T = field->ctx.next_state < sizeof(state_to_char) ?
  1450. state_to_char[field->ctx.next_state] : 'X';
  1451. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1452. field->ctx.prev_pid,
  1453. field->ctx.prev_prio,
  1454. S,
  1455. entry->type == TRACE_CTX ? "==>" : " +",
  1456. field->ctx.next_pid,
  1457. field->ctx.next_prio,
  1458. T);
  1459. if (!ret)
  1460. return 0;
  1461. break;
  1462. case TRACE_SPECIAL:
  1463. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1464. field->special.arg1,
  1465. field->special.arg2,
  1466. field->special.arg3);
  1467. if (!ret)
  1468. return 0;
  1469. break;
  1470. case TRACE_STACK:
  1471. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1472. if (i) {
  1473. ret = trace_seq_puts(s, " <= ");
  1474. if (!ret)
  1475. return 0;
  1476. }
  1477. ret = seq_print_ip_sym(s, field->stack.caller[i],
  1478. sym_flags);
  1479. if (!ret)
  1480. return 0;
  1481. }
  1482. ret = trace_seq_puts(s, "\n");
  1483. if (!ret)
  1484. return 0;
  1485. break;
  1486. case TRACE_PRINT:
  1487. seq_print_ip_sym(s, field->print.ip, sym_flags);
  1488. trace_seq_printf(s, ": %s", field->print.buf);
  1489. if (field->flags & TRACE_FLAG_CONT)
  1490. trace_seq_print_cont(s, iter);
  1491. break;
  1492. }
  1493. return 1;
  1494. }
  1495. static int print_raw_fmt(struct trace_iterator *iter)
  1496. {
  1497. struct trace_seq *s = &iter->seq;
  1498. struct trace_entry *entry;
  1499. struct trace_field *field;
  1500. int ret;
  1501. int S, T;
  1502. entry = iter->ent;
  1503. if (entry->type == TRACE_CONT)
  1504. return 1;
  1505. field = &entry->field;
  1506. ret = trace_seq_printf(s, "%d %d %llu ",
  1507. field->pid, iter->cpu, field->t);
  1508. if (!ret)
  1509. return 0;
  1510. switch (entry->type) {
  1511. case TRACE_FN:
  1512. ret = trace_seq_printf(s, "%x %x\n",
  1513. field->fn.ip,
  1514. field->fn.parent_ip);
  1515. if (!ret)
  1516. return 0;
  1517. break;
  1518. case TRACE_CTX:
  1519. case TRACE_WAKE:
  1520. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1521. state_to_char[field->ctx.prev_state] : 'X';
  1522. T = field->ctx.next_state < sizeof(state_to_char) ?
  1523. state_to_char[field->ctx.next_state] : 'X';
  1524. if (entry->type == TRACE_WAKE)
  1525. S = '+';
  1526. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1527. field->ctx.prev_pid,
  1528. field->ctx.prev_prio,
  1529. S,
  1530. field->ctx.next_pid,
  1531. field->ctx.next_prio,
  1532. T);
  1533. if (!ret)
  1534. return 0;
  1535. break;
  1536. case TRACE_SPECIAL:
  1537. case TRACE_STACK:
  1538. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1539. field->special.arg1,
  1540. field->special.arg2,
  1541. field->special.arg3);
  1542. if (!ret)
  1543. return 0;
  1544. break;
  1545. case TRACE_PRINT:
  1546. trace_seq_printf(s, "# %lx %s",
  1547. field->print.ip, field->print.buf);
  1548. if (field->flags & TRACE_FLAG_CONT)
  1549. trace_seq_print_cont(s, iter);
  1550. break;
  1551. }
  1552. return 1;
  1553. }
  1554. #define SEQ_PUT_FIELD_RET(s, x) \
  1555. do { \
  1556. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1557. return 0; \
  1558. } while (0)
  1559. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1560. do { \
  1561. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1562. return 0; \
  1563. } while (0)
  1564. static int print_hex_fmt(struct trace_iterator *iter)
  1565. {
  1566. struct trace_seq *s = &iter->seq;
  1567. unsigned char newline = '\n';
  1568. struct trace_entry *entry;
  1569. struct trace_field *field;
  1570. int S, T;
  1571. entry = iter->ent;
  1572. if (entry->type == TRACE_CONT)
  1573. return 1;
  1574. field = &entry->field;
  1575. SEQ_PUT_HEX_FIELD_RET(s, field->pid);
  1576. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1577. SEQ_PUT_HEX_FIELD_RET(s, field->t);
  1578. switch (entry->type) {
  1579. case TRACE_FN:
  1580. SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
  1581. SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
  1582. break;
  1583. case TRACE_CTX:
  1584. case TRACE_WAKE:
  1585. S = field->ctx.prev_state < sizeof(state_to_char) ?
  1586. state_to_char[field->ctx.prev_state] : 'X';
  1587. T = field->ctx.next_state < sizeof(state_to_char) ?
  1588. state_to_char[field->ctx.next_state] : 'X';
  1589. if (entry->type == TRACE_WAKE)
  1590. S = '+';
  1591. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
  1592. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
  1593. SEQ_PUT_HEX_FIELD_RET(s, S);
  1594. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
  1595. SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
  1596. SEQ_PUT_HEX_FIELD_RET(s, T);
  1597. break;
  1598. case TRACE_SPECIAL:
  1599. case TRACE_STACK:
  1600. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
  1601. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
  1602. SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
  1603. break;
  1604. }
  1605. SEQ_PUT_FIELD_RET(s, newline);
  1606. return 1;
  1607. }
  1608. static int print_bin_fmt(struct trace_iterator *iter)
  1609. {
  1610. struct trace_seq *s = &iter->seq;
  1611. struct trace_entry *entry;
  1612. struct trace_field *field;
  1613. entry = iter->ent;
  1614. if (entry->type == TRACE_CONT)
  1615. return 1;
  1616. field = &entry->field;
  1617. SEQ_PUT_FIELD_RET(s, field->pid);
  1618. SEQ_PUT_FIELD_RET(s, field->cpu);
  1619. SEQ_PUT_FIELD_RET(s, field->t);
  1620. switch (entry->type) {
  1621. case TRACE_FN:
  1622. SEQ_PUT_FIELD_RET(s, field->fn.ip);
  1623. SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
  1624. break;
  1625. case TRACE_CTX:
  1626. SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
  1627. SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
  1628. SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
  1629. SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
  1630. SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
  1631. SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
  1632. break;
  1633. case TRACE_SPECIAL:
  1634. case TRACE_STACK:
  1635. SEQ_PUT_FIELD_RET(s, field->special.arg1);
  1636. SEQ_PUT_FIELD_RET(s, field->special.arg2);
  1637. SEQ_PUT_FIELD_RET(s, field->special.arg3);
  1638. break;
  1639. }
  1640. return 1;
  1641. }
  1642. static int trace_empty(struct trace_iterator *iter)
  1643. {
  1644. struct trace_array_cpu *data;
  1645. int cpu;
  1646. for_each_tracing_cpu(cpu) {
  1647. data = iter->tr->data[cpu];
  1648. if (head_page(data) && data->trace_idx &&
  1649. (data->trace_tail != data->trace_head ||
  1650. data->trace_tail_idx != data->trace_head_idx))
  1651. return 0;
  1652. }
  1653. return 1;
  1654. }
  1655. static int print_trace_line(struct trace_iterator *iter)
  1656. {
  1657. if (iter->trace && iter->trace->print_line)
  1658. return iter->trace->print_line(iter);
  1659. if (trace_flags & TRACE_ITER_BIN)
  1660. return print_bin_fmt(iter);
  1661. if (trace_flags & TRACE_ITER_HEX)
  1662. return print_hex_fmt(iter);
  1663. if (trace_flags & TRACE_ITER_RAW)
  1664. return print_raw_fmt(iter);
  1665. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1666. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1667. return print_trace_fmt(iter);
  1668. }
  1669. static int s_show(struct seq_file *m, void *v)
  1670. {
  1671. struct trace_iterator *iter = v;
  1672. if (iter->ent == NULL) {
  1673. if (iter->tr) {
  1674. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1675. seq_puts(m, "#\n");
  1676. }
  1677. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1678. /* print nothing if the buffers are empty */
  1679. if (trace_empty(iter))
  1680. return 0;
  1681. print_trace_header(m, iter);
  1682. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1683. print_lat_help_header(m);
  1684. } else {
  1685. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1686. print_func_help_header(m);
  1687. }
  1688. } else {
  1689. print_trace_line(iter);
  1690. trace_print_seq(m, &iter->seq);
  1691. }
  1692. return 0;
  1693. }
  1694. static struct seq_operations tracer_seq_ops = {
  1695. .start = s_start,
  1696. .next = s_next,
  1697. .stop = s_stop,
  1698. .show = s_show,
  1699. };
  1700. static struct trace_iterator *
  1701. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1702. {
  1703. struct trace_iterator *iter;
  1704. if (tracing_disabled) {
  1705. *ret = -ENODEV;
  1706. return NULL;
  1707. }
  1708. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1709. if (!iter) {
  1710. *ret = -ENOMEM;
  1711. goto out;
  1712. }
  1713. mutex_lock(&trace_types_lock);
  1714. if (current_trace && current_trace->print_max)
  1715. iter->tr = &max_tr;
  1716. else
  1717. iter->tr = inode->i_private;
  1718. iter->trace = current_trace;
  1719. iter->pos = -1;
  1720. /* TODO stop tracer */
  1721. *ret = seq_open(file, &tracer_seq_ops);
  1722. if (!*ret) {
  1723. struct seq_file *m = file->private_data;
  1724. m->private = iter;
  1725. /* stop the trace while dumping */
  1726. if (iter->tr->ctrl) {
  1727. tracer_enabled = 0;
  1728. ftrace_function_enabled = 0;
  1729. }
  1730. if (iter->trace && iter->trace->open)
  1731. iter->trace->open(iter);
  1732. } else {
  1733. kfree(iter);
  1734. iter = NULL;
  1735. }
  1736. mutex_unlock(&trace_types_lock);
  1737. out:
  1738. return iter;
  1739. }
  1740. int tracing_open_generic(struct inode *inode, struct file *filp)
  1741. {
  1742. if (tracing_disabled)
  1743. return -ENODEV;
  1744. filp->private_data = inode->i_private;
  1745. return 0;
  1746. }
  1747. int tracing_release(struct inode *inode, struct file *file)
  1748. {
  1749. struct seq_file *m = (struct seq_file *)file->private_data;
  1750. struct trace_iterator *iter = m->private;
  1751. mutex_lock(&trace_types_lock);
  1752. if (iter->trace && iter->trace->close)
  1753. iter->trace->close(iter);
  1754. /* reenable tracing if it was previously enabled */
  1755. if (iter->tr->ctrl) {
  1756. tracer_enabled = 1;
  1757. /*
  1758. * It is safe to enable function tracing even if it
  1759. * isn't used
  1760. */
  1761. ftrace_function_enabled = 1;
  1762. }
  1763. mutex_unlock(&trace_types_lock);
  1764. seq_release(inode, file);
  1765. kfree(iter);
  1766. return 0;
  1767. }
  1768. static int tracing_open(struct inode *inode, struct file *file)
  1769. {
  1770. int ret;
  1771. __tracing_open(inode, file, &ret);
  1772. return ret;
  1773. }
  1774. static int tracing_lt_open(struct inode *inode, struct file *file)
  1775. {
  1776. struct trace_iterator *iter;
  1777. int ret;
  1778. iter = __tracing_open(inode, file, &ret);
  1779. if (!ret)
  1780. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1781. return ret;
  1782. }
  1783. static void *
  1784. t_next(struct seq_file *m, void *v, loff_t *pos)
  1785. {
  1786. struct tracer *t = m->private;
  1787. (*pos)++;
  1788. if (t)
  1789. t = t->next;
  1790. m->private = t;
  1791. return t;
  1792. }
  1793. static void *t_start(struct seq_file *m, loff_t *pos)
  1794. {
  1795. struct tracer *t = m->private;
  1796. loff_t l = 0;
  1797. mutex_lock(&trace_types_lock);
  1798. for (; t && l < *pos; t = t_next(m, t, &l))
  1799. ;
  1800. return t;
  1801. }
  1802. static void t_stop(struct seq_file *m, void *p)
  1803. {
  1804. mutex_unlock(&trace_types_lock);
  1805. }
  1806. static int t_show(struct seq_file *m, void *v)
  1807. {
  1808. struct tracer *t = v;
  1809. if (!t)
  1810. return 0;
  1811. seq_printf(m, "%s", t->name);
  1812. if (t->next)
  1813. seq_putc(m, ' ');
  1814. else
  1815. seq_putc(m, '\n');
  1816. return 0;
  1817. }
  1818. static struct seq_operations show_traces_seq_ops = {
  1819. .start = t_start,
  1820. .next = t_next,
  1821. .stop = t_stop,
  1822. .show = t_show,
  1823. };
  1824. static int show_traces_open(struct inode *inode, struct file *file)
  1825. {
  1826. int ret;
  1827. if (tracing_disabled)
  1828. return -ENODEV;
  1829. ret = seq_open(file, &show_traces_seq_ops);
  1830. if (!ret) {
  1831. struct seq_file *m = file->private_data;
  1832. m->private = trace_types;
  1833. }
  1834. return ret;
  1835. }
  1836. static struct file_operations tracing_fops = {
  1837. .open = tracing_open,
  1838. .read = seq_read,
  1839. .llseek = seq_lseek,
  1840. .release = tracing_release,
  1841. };
  1842. static struct file_operations tracing_lt_fops = {
  1843. .open = tracing_lt_open,
  1844. .read = seq_read,
  1845. .llseek = seq_lseek,
  1846. .release = tracing_release,
  1847. };
  1848. static struct file_operations show_traces_fops = {
  1849. .open = show_traces_open,
  1850. .read = seq_read,
  1851. .release = seq_release,
  1852. };
  1853. /*
  1854. * Only trace on a CPU if the bitmask is set:
  1855. */
  1856. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1857. /*
  1858. * When tracing/tracing_cpu_mask is modified then this holds
  1859. * the new bitmask we are about to install:
  1860. */
  1861. static cpumask_t tracing_cpumask_new;
  1862. /*
  1863. * The tracer itself will not take this lock, but still we want
  1864. * to provide a consistent cpumask to user-space:
  1865. */
  1866. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1867. /*
  1868. * Temporary storage for the character representation of the
  1869. * CPU bitmask (and one more byte for the newline):
  1870. */
  1871. static char mask_str[NR_CPUS + 1];
  1872. static ssize_t
  1873. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1874. size_t count, loff_t *ppos)
  1875. {
  1876. int len;
  1877. mutex_lock(&tracing_cpumask_update_lock);
  1878. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1879. if (count - len < 2) {
  1880. count = -EINVAL;
  1881. goto out_err;
  1882. }
  1883. len += sprintf(mask_str + len, "\n");
  1884. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1885. out_err:
  1886. mutex_unlock(&tracing_cpumask_update_lock);
  1887. return count;
  1888. }
  1889. static ssize_t
  1890. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1891. size_t count, loff_t *ppos)
  1892. {
  1893. int err, cpu;
  1894. mutex_lock(&tracing_cpumask_update_lock);
  1895. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1896. if (err)
  1897. goto err_unlock;
  1898. raw_local_irq_disable();
  1899. __raw_spin_lock(&ftrace_max_lock);
  1900. for_each_tracing_cpu(cpu) {
  1901. /*
  1902. * Increase/decrease the disabled counter if we are
  1903. * about to flip a bit in the cpumask:
  1904. */
  1905. if (cpu_isset(cpu, tracing_cpumask) &&
  1906. !cpu_isset(cpu, tracing_cpumask_new)) {
  1907. atomic_inc(&global_trace.data[cpu]->disabled);
  1908. }
  1909. if (!cpu_isset(cpu, tracing_cpumask) &&
  1910. cpu_isset(cpu, tracing_cpumask_new)) {
  1911. atomic_dec(&global_trace.data[cpu]->disabled);
  1912. }
  1913. }
  1914. __raw_spin_unlock(&ftrace_max_lock);
  1915. raw_local_irq_enable();
  1916. tracing_cpumask = tracing_cpumask_new;
  1917. mutex_unlock(&tracing_cpumask_update_lock);
  1918. return count;
  1919. err_unlock:
  1920. mutex_unlock(&tracing_cpumask_update_lock);
  1921. return err;
  1922. }
  1923. static struct file_operations tracing_cpumask_fops = {
  1924. .open = tracing_open_generic,
  1925. .read = tracing_cpumask_read,
  1926. .write = tracing_cpumask_write,
  1927. };
  1928. static ssize_t
  1929. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1930. size_t cnt, loff_t *ppos)
  1931. {
  1932. char *buf;
  1933. int r = 0;
  1934. int len = 0;
  1935. int i;
  1936. /* calulate max size */
  1937. for (i = 0; trace_options[i]; i++) {
  1938. len += strlen(trace_options[i]);
  1939. len += 3; /* "no" and space */
  1940. }
  1941. /* +2 for \n and \0 */
  1942. buf = kmalloc(len + 2, GFP_KERNEL);
  1943. if (!buf)
  1944. return -ENOMEM;
  1945. for (i = 0; trace_options[i]; i++) {
  1946. if (trace_flags & (1 << i))
  1947. r += sprintf(buf + r, "%s ", trace_options[i]);
  1948. else
  1949. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1950. }
  1951. r += sprintf(buf + r, "\n");
  1952. WARN_ON(r >= len + 2);
  1953. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1954. kfree(buf);
  1955. return r;
  1956. }
  1957. static ssize_t
  1958. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1959. size_t cnt, loff_t *ppos)
  1960. {
  1961. char buf[64];
  1962. char *cmp = buf;
  1963. int neg = 0;
  1964. int i;
  1965. if (cnt >= sizeof(buf))
  1966. return -EINVAL;
  1967. if (copy_from_user(&buf, ubuf, cnt))
  1968. return -EFAULT;
  1969. buf[cnt] = 0;
  1970. if (strncmp(buf, "no", 2) == 0) {
  1971. neg = 1;
  1972. cmp += 2;
  1973. }
  1974. for (i = 0; trace_options[i]; i++) {
  1975. int len = strlen(trace_options[i]);
  1976. if (strncmp(cmp, trace_options[i], len) == 0) {
  1977. if (neg)
  1978. trace_flags &= ~(1 << i);
  1979. else
  1980. trace_flags |= (1 << i);
  1981. break;
  1982. }
  1983. }
  1984. /*
  1985. * If no option could be set, return an error:
  1986. */
  1987. if (!trace_options[i])
  1988. return -EINVAL;
  1989. filp->f_pos += cnt;
  1990. return cnt;
  1991. }
  1992. static struct file_operations tracing_iter_fops = {
  1993. .open = tracing_open_generic,
  1994. .read = tracing_iter_ctrl_read,
  1995. .write = tracing_iter_ctrl_write,
  1996. };
  1997. static const char readme_msg[] =
  1998. "tracing mini-HOWTO:\n\n"
  1999. "# mkdir /debug\n"
  2000. "# mount -t debugfs nodev /debug\n\n"
  2001. "# cat /debug/tracing/available_tracers\n"
  2002. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  2003. "# cat /debug/tracing/current_tracer\n"
  2004. "none\n"
  2005. "# echo sched_switch > /debug/tracing/current_tracer\n"
  2006. "# cat /debug/tracing/current_tracer\n"
  2007. "sched_switch\n"
  2008. "# cat /debug/tracing/iter_ctrl\n"
  2009. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2010. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  2011. "# echo 1 > /debug/tracing/tracing_enabled\n"
  2012. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  2013. "echo 0 > /debug/tracing/tracing_enabled\n"
  2014. ;
  2015. static ssize_t
  2016. tracing_readme_read(struct file *filp, char __user *ubuf,
  2017. size_t cnt, loff_t *ppos)
  2018. {
  2019. return simple_read_from_buffer(ubuf, cnt, ppos,
  2020. readme_msg, strlen(readme_msg));
  2021. }
  2022. static struct file_operations tracing_readme_fops = {
  2023. .open = tracing_open_generic,
  2024. .read = tracing_readme_read,
  2025. };
  2026. static ssize_t
  2027. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2028. size_t cnt, loff_t *ppos)
  2029. {
  2030. struct trace_array *tr = filp->private_data;
  2031. char buf[64];
  2032. int r;
  2033. r = sprintf(buf, "%ld\n", tr->ctrl);
  2034. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2035. }
  2036. static ssize_t
  2037. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2038. size_t cnt, loff_t *ppos)
  2039. {
  2040. struct trace_array *tr = filp->private_data;
  2041. char buf[64];
  2042. long val;
  2043. int ret;
  2044. if (cnt >= sizeof(buf))
  2045. return -EINVAL;
  2046. if (copy_from_user(&buf, ubuf, cnt))
  2047. return -EFAULT;
  2048. buf[cnt] = 0;
  2049. ret = strict_strtoul(buf, 10, &val);
  2050. if (ret < 0)
  2051. return ret;
  2052. val = !!val;
  2053. mutex_lock(&trace_types_lock);
  2054. if (tr->ctrl ^ val) {
  2055. if (val)
  2056. tracer_enabled = 1;
  2057. else
  2058. tracer_enabled = 0;
  2059. tr->ctrl = val;
  2060. if (current_trace && current_trace->ctrl_update)
  2061. current_trace->ctrl_update(tr);
  2062. }
  2063. mutex_unlock(&trace_types_lock);
  2064. filp->f_pos += cnt;
  2065. return cnt;
  2066. }
  2067. static ssize_t
  2068. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2069. size_t cnt, loff_t *ppos)
  2070. {
  2071. char buf[max_tracer_type_len+2];
  2072. int r;
  2073. mutex_lock(&trace_types_lock);
  2074. if (current_trace)
  2075. r = sprintf(buf, "%s\n", current_trace->name);
  2076. else
  2077. r = sprintf(buf, "\n");
  2078. mutex_unlock(&trace_types_lock);
  2079. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2080. }
  2081. static ssize_t
  2082. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2083. size_t cnt, loff_t *ppos)
  2084. {
  2085. struct trace_array *tr = &global_trace;
  2086. struct tracer *t;
  2087. char buf[max_tracer_type_len+1];
  2088. int i;
  2089. if (cnt > max_tracer_type_len)
  2090. cnt = max_tracer_type_len;
  2091. if (copy_from_user(&buf, ubuf, cnt))
  2092. return -EFAULT;
  2093. buf[cnt] = 0;
  2094. /* strip ending whitespace. */
  2095. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2096. buf[i] = 0;
  2097. mutex_lock(&trace_types_lock);
  2098. for (t = trace_types; t; t = t->next) {
  2099. if (strcmp(t->name, buf) == 0)
  2100. break;
  2101. }
  2102. if (!t || t == current_trace)
  2103. goto out;
  2104. if (current_trace && current_trace->reset)
  2105. current_trace->reset(tr);
  2106. current_trace = t;
  2107. if (t->init)
  2108. t->init(tr);
  2109. out:
  2110. mutex_unlock(&trace_types_lock);
  2111. filp->f_pos += cnt;
  2112. return cnt;
  2113. }
  2114. static ssize_t
  2115. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2116. size_t cnt, loff_t *ppos)
  2117. {
  2118. unsigned long *ptr = filp->private_data;
  2119. char buf[64];
  2120. int r;
  2121. r = snprintf(buf, sizeof(buf), "%ld\n",
  2122. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2123. if (r > sizeof(buf))
  2124. r = sizeof(buf);
  2125. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2126. }
  2127. static ssize_t
  2128. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2129. size_t cnt, loff_t *ppos)
  2130. {
  2131. long *ptr = filp->private_data;
  2132. char buf[64];
  2133. long val;
  2134. int ret;
  2135. if (cnt >= sizeof(buf))
  2136. return -EINVAL;
  2137. if (copy_from_user(&buf, ubuf, cnt))
  2138. return -EFAULT;
  2139. buf[cnt] = 0;
  2140. ret = strict_strtoul(buf, 10, &val);
  2141. if (ret < 0)
  2142. return ret;
  2143. *ptr = val * 1000;
  2144. return cnt;
  2145. }
  2146. static atomic_t tracing_reader;
  2147. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2148. {
  2149. struct trace_iterator *iter;
  2150. if (tracing_disabled)
  2151. return -ENODEV;
  2152. /* We only allow for reader of the pipe */
  2153. if (atomic_inc_return(&tracing_reader) != 1) {
  2154. atomic_dec(&tracing_reader);
  2155. return -EBUSY;
  2156. }
  2157. /* create a buffer to store the information to pass to userspace */
  2158. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2159. if (!iter)
  2160. return -ENOMEM;
  2161. mutex_lock(&trace_types_lock);
  2162. iter->tr = &global_trace;
  2163. iter->trace = current_trace;
  2164. filp->private_data = iter;
  2165. if (iter->trace->pipe_open)
  2166. iter->trace->pipe_open(iter);
  2167. mutex_unlock(&trace_types_lock);
  2168. return 0;
  2169. }
  2170. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2171. {
  2172. struct trace_iterator *iter = file->private_data;
  2173. kfree(iter);
  2174. atomic_dec(&tracing_reader);
  2175. return 0;
  2176. }
  2177. static unsigned int
  2178. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2179. {
  2180. struct trace_iterator *iter = filp->private_data;
  2181. if (trace_flags & TRACE_ITER_BLOCK) {
  2182. /*
  2183. * Always select as readable when in blocking mode
  2184. */
  2185. return POLLIN | POLLRDNORM;
  2186. } else {
  2187. if (!trace_empty(iter))
  2188. return POLLIN | POLLRDNORM;
  2189. poll_wait(filp, &trace_wait, poll_table);
  2190. if (!trace_empty(iter))
  2191. return POLLIN | POLLRDNORM;
  2192. return 0;
  2193. }
  2194. }
  2195. /*
  2196. * Consumer reader.
  2197. */
  2198. static ssize_t
  2199. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2200. size_t cnt, loff_t *ppos)
  2201. {
  2202. struct trace_iterator *iter = filp->private_data;
  2203. struct trace_array_cpu *data;
  2204. static cpumask_t mask;
  2205. unsigned long flags;
  2206. #ifdef CONFIG_FTRACE
  2207. int ftrace_save;
  2208. #endif
  2209. int cpu;
  2210. ssize_t sret;
  2211. /* return any leftover data */
  2212. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2213. if (sret != -EBUSY)
  2214. return sret;
  2215. sret = 0;
  2216. trace_seq_reset(&iter->seq);
  2217. mutex_lock(&trace_types_lock);
  2218. if (iter->trace->read) {
  2219. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2220. if (sret)
  2221. goto out;
  2222. }
  2223. while (trace_empty(iter)) {
  2224. if ((filp->f_flags & O_NONBLOCK)) {
  2225. sret = -EAGAIN;
  2226. goto out;
  2227. }
  2228. /*
  2229. * This is a make-shift waitqueue. The reason we don't use
  2230. * an actual wait queue is because:
  2231. * 1) we only ever have one waiter
  2232. * 2) the tracing, traces all functions, we don't want
  2233. * the overhead of calling wake_up and friends
  2234. * (and tracing them too)
  2235. * Anyway, this is really very primitive wakeup.
  2236. */
  2237. set_current_state(TASK_INTERRUPTIBLE);
  2238. iter->tr->waiter = current;
  2239. mutex_unlock(&trace_types_lock);
  2240. /* sleep for 100 msecs, and try again. */
  2241. schedule_timeout(HZ/10);
  2242. mutex_lock(&trace_types_lock);
  2243. iter->tr->waiter = NULL;
  2244. if (signal_pending(current)) {
  2245. sret = -EINTR;
  2246. goto out;
  2247. }
  2248. if (iter->trace != current_trace)
  2249. goto out;
  2250. /*
  2251. * We block until we read something and tracing is disabled.
  2252. * We still block if tracing is disabled, but we have never
  2253. * read anything. This allows a user to cat this file, and
  2254. * then enable tracing. But after we have read something,
  2255. * we give an EOF when tracing is again disabled.
  2256. *
  2257. * iter->pos will be 0 if we haven't read anything.
  2258. */
  2259. if (!tracer_enabled && iter->pos)
  2260. break;
  2261. continue;
  2262. }
  2263. /* stop when tracing is finished */
  2264. if (trace_empty(iter))
  2265. goto out;
  2266. if (cnt >= PAGE_SIZE)
  2267. cnt = PAGE_SIZE - 1;
  2268. /* reset all but tr, trace, and overruns */
  2269. memset(&iter->seq, 0,
  2270. sizeof(struct trace_iterator) -
  2271. offsetof(struct trace_iterator, seq));
  2272. iter->pos = -1;
  2273. /*
  2274. * We need to stop all tracing on all CPUS to read the
  2275. * the next buffer. This is a bit expensive, but is
  2276. * not done often. We fill all what we can read,
  2277. * and then release the locks again.
  2278. */
  2279. cpus_clear(mask);
  2280. local_irq_save(flags);
  2281. #ifdef CONFIG_FTRACE
  2282. ftrace_save = ftrace_enabled;
  2283. ftrace_enabled = 0;
  2284. #endif
  2285. smp_wmb();
  2286. for_each_tracing_cpu(cpu) {
  2287. data = iter->tr->data[cpu];
  2288. if (!head_page(data) || !data->trace_idx)
  2289. continue;
  2290. atomic_inc(&data->disabled);
  2291. cpu_set(cpu, mask);
  2292. }
  2293. for_each_cpu_mask(cpu, mask) {
  2294. data = iter->tr->data[cpu];
  2295. __raw_spin_lock(&data->lock);
  2296. if (data->overrun > iter->last_overrun[cpu])
  2297. iter->overrun[cpu] +=
  2298. data->overrun - iter->last_overrun[cpu];
  2299. iter->last_overrun[cpu] = data->overrun;
  2300. }
  2301. while (find_next_entry_inc(iter) != NULL) {
  2302. int ret;
  2303. int len = iter->seq.len;
  2304. ret = print_trace_line(iter);
  2305. if (!ret) {
  2306. /* don't print partial lines */
  2307. iter->seq.len = len;
  2308. break;
  2309. }
  2310. trace_consume(iter);
  2311. if (iter->seq.len >= cnt)
  2312. break;
  2313. }
  2314. for_each_cpu_mask(cpu, mask) {
  2315. data = iter->tr->data[cpu];
  2316. __raw_spin_unlock(&data->lock);
  2317. }
  2318. for_each_cpu_mask(cpu, mask) {
  2319. data = iter->tr->data[cpu];
  2320. atomic_dec(&data->disabled);
  2321. }
  2322. #ifdef CONFIG_FTRACE
  2323. ftrace_enabled = ftrace_save;
  2324. #endif
  2325. local_irq_restore(flags);
  2326. /* Now copy what we have to the user */
  2327. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2328. if (iter->seq.readpos >= iter->seq.len)
  2329. trace_seq_reset(&iter->seq);
  2330. if (sret == -EBUSY)
  2331. sret = 0;
  2332. out:
  2333. mutex_unlock(&trace_types_lock);
  2334. return sret;
  2335. }
  2336. static ssize_t
  2337. tracing_entries_read(struct file *filp, char __user *ubuf,
  2338. size_t cnt, loff_t *ppos)
  2339. {
  2340. struct trace_array *tr = filp->private_data;
  2341. char buf[64];
  2342. int r;
  2343. r = sprintf(buf, "%lu\n", tr->entries);
  2344. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2345. }
  2346. static ssize_t
  2347. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2348. size_t cnt, loff_t *ppos)
  2349. {
  2350. unsigned long val;
  2351. char buf[64];
  2352. int i, ret;
  2353. if (cnt >= sizeof(buf))
  2354. return -EINVAL;
  2355. if (copy_from_user(&buf, ubuf, cnt))
  2356. return -EFAULT;
  2357. buf[cnt] = 0;
  2358. ret = strict_strtoul(buf, 10, &val);
  2359. if (ret < 0)
  2360. return ret;
  2361. /* must have at least 1 entry */
  2362. if (!val)
  2363. return -EINVAL;
  2364. mutex_lock(&trace_types_lock);
  2365. if (current_trace != &no_tracer) {
  2366. cnt = -EBUSY;
  2367. pr_info("ftrace: set current_tracer to none"
  2368. " before modifying buffer size\n");
  2369. goto out;
  2370. }
  2371. if (val > global_trace.entries) {
  2372. long pages_requested;
  2373. unsigned long freeable_pages;
  2374. /* make sure we have enough memory before mapping */
  2375. pages_requested =
  2376. (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
  2377. /* account for each buffer (and max_tr) */
  2378. pages_requested *= tracing_nr_buffers * 2;
  2379. /* Check for overflow */
  2380. if (pages_requested < 0) {
  2381. cnt = -ENOMEM;
  2382. goto out;
  2383. }
  2384. freeable_pages = determine_dirtyable_memory();
  2385. /* we only allow to request 1/4 of useable memory */
  2386. if (pages_requested >
  2387. ((freeable_pages + tracing_pages_allocated) / 4)) {
  2388. cnt = -ENOMEM;
  2389. goto out;
  2390. }
  2391. while (global_trace.entries < val) {
  2392. if (trace_alloc_page()) {
  2393. cnt = -ENOMEM;
  2394. goto out;
  2395. }
  2396. /* double check that we don't go over the known pages */
  2397. if (tracing_pages_allocated > pages_requested)
  2398. break;
  2399. }
  2400. } else {
  2401. /* include the number of entries in val (inc of page entries) */
  2402. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  2403. trace_free_page();
  2404. }
  2405. /* check integrity */
  2406. for_each_tracing_cpu(i)
  2407. check_pages(global_trace.data[i]);
  2408. filp->f_pos += cnt;
  2409. /* If check pages failed, return ENOMEM */
  2410. if (tracing_disabled)
  2411. cnt = -ENOMEM;
  2412. out:
  2413. max_tr.entries = global_trace.entries;
  2414. mutex_unlock(&trace_types_lock);
  2415. return cnt;
  2416. }
  2417. static struct file_operations tracing_max_lat_fops = {
  2418. .open = tracing_open_generic,
  2419. .read = tracing_max_lat_read,
  2420. .write = tracing_max_lat_write,
  2421. };
  2422. static struct file_operations tracing_ctrl_fops = {
  2423. .open = tracing_open_generic,
  2424. .read = tracing_ctrl_read,
  2425. .write = tracing_ctrl_write,
  2426. };
  2427. static struct file_operations set_tracer_fops = {
  2428. .open = tracing_open_generic,
  2429. .read = tracing_set_trace_read,
  2430. .write = tracing_set_trace_write,
  2431. };
  2432. static struct file_operations tracing_pipe_fops = {
  2433. .open = tracing_open_pipe,
  2434. .poll = tracing_poll_pipe,
  2435. .read = tracing_read_pipe,
  2436. .release = tracing_release_pipe,
  2437. };
  2438. static struct file_operations tracing_entries_fops = {
  2439. .open = tracing_open_generic,
  2440. .read = tracing_entries_read,
  2441. .write = tracing_entries_write,
  2442. };
  2443. #ifdef CONFIG_DYNAMIC_FTRACE
  2444. static ssize_t
  2445. tracing_read_long(struct file *filp, char __user *ubuf,
  2446. size_t cnt, loff_t *ppos)
  2447. {
  2448. unsigned long *p = filp->private_data;
  2449. char buf[64];
  2450. int r;
  2451. r = sprintf(buf, "%ld\n", *p);
  2452. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2453. }
  2454. static struct file_operations tracing_read_long_fops = {
  2455. .open = tracing_open_generic,
  2456. .read = tracing_read_long,
  2457. };
  2458. #endif
  2459. static struct dentry *d_tracer;
  2460. struct dentry *tracing_init_dentry(void)
  2461. {
  2462. static int once;
  2463. if (d_tracer)
  2464. return d_tracer;
  2465. d_tracer = debugfs_create_dir("tracing", NULL);
  2466. if (!d_tracer && !once) {
  2467. once = 1;
  2468. pr_warning("Could not create debugfs directory 'tracing'\n");
  2469. return NULL;
  2470. }
  2471. return d_tracer;
  2472. }
  2473. #ifdef CONFIG_FTRACE_SELFTEST
  2474. /* Let selftest have access to static functions in this file */
  2475. #include "trace_selftest.c"
  2476. #endif
  2477. static __init void tracer_init_debugfs(void)
  2478. {
  2479. struct dentry *d_tracer;
  2480. struct dentry *entry;
  2481. d_tracer = tracing_init_dentry();
  2482. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2483. &global_trace, &tracing_ctrl_fops);
  2484. if (!entry)
  2485. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2486. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2487. NULL, &tracing_iter_fops);
  2488. if (!entry)
  2489. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2490. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2491. NULL, &tracing_cpumask_fops);
  2492. if (!entry)
  2493. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2494. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2495. &global_trace, &tracing_lt_fops);
  2496. if (!entry)
  2497. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2498. entry = debugfs_create_file("trace", 0444, d_tracer,
  2499. &global_trace, &tracing_fops);
  2500. if (!entry)
  2501. pr_warning("Could not create debugfs 'trace' entry\n");
  2502. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2503. &global_trace, &show_traces_fops);
  2504. if (!entry)
  2505. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2506. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2507. &global_trace, &set_tracer_fops);
  2508. if (!entry)
  2509. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2510. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2511. &tracing_max_latency,
  2512. &tracing_max_lat_fops);
  2513. if (!entry)
  2514. pr_warning("Could not create debugfs "
  2515. "'tracing_max_latency' entry\n");
  2516. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2517. &tracing_thresh, &tracing_max_lat_fops);
  2518. if (!entry)
  2519. pr_warning("Could not create debugfs "
  2520. "'tracing_thresh' entry\n");
  2521. entry = debugfs_create_file("README", 0644, d_tracer,
  2522. NULL, &tracing_readme_fops);
  2523. if (!entry)
  2524. pr_warning("Could not create debugfs 'README' entry\n");
  2525. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2526. NULL, &tracing_pipe_fops);
  2527. if (!entry)
  2528. pr_warning("Could not create debugfs "
  2529. "'trace_pipe' entry\n");
  2530. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2531. &global_trace, &tracing_entries_fops);
  2532. if (!entry)
  2533. pr_warning("Could not create debugfs "
  2534. "'trace_entries' entry\n");
  2535. #ifdef CONFIG_DYNAMIC_FTRACE
  2536. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2537. &ftrace_update_tot_cnt,
  2538. &tracing_read_long_fops);
  2539. if (!entry)
  2540. pr_warning("Could not create debugfs "
  2541. "'dyn_ftrace_total_info' entry\n");
  2542. #endif
  2543. #ifdef CONFIG_SYSPROF_TRACER
  2544. init_tracer_sysprof_debugfs(d_tracer);
  2545. #endif
  2546. }
  2547. #define TRACE_BUF_SIZE 1024
  2548. #define TRACE_PRINT_BUF_SIZE \
  2549. (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
  2550. #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
  2551. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2552. {
  2553. static DEFINE_SPINLOCK(trace_buf_lock);
  2554. static char trace_buf[TRACE_BUF_SIZE];
  2555. struct trace_array *tr = &global_trace;
  2556. struct trace_array_cpu *data;
  2557. struct trace_entry *entry;
  2558. unsigned long flags;
  2559. long disabled;
  2560. va_list ap;
  2561. int cpu, len = 0, write, written = 0;
  2562. if (!(trace_flags & TRACE_ITER_PRINTK) || !tr->ctrl || tracing_disabled)
  2563. return 0;
  2564. local_irq_save(flags);
  2565. cpu = raw_smp_processor_id();
  2566. data = tr->data[cpu];
  2567. disabled = atomic_inc_return(&data->disabled);
  2568. if (unlikely(disabled != 1))
  2569. goto out;
  2570. spin_lock(&trace_buf_lock);
  2571. va_start(ap, fmt);
  2572. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
  2573. va_end(ap);
  2574. len = min(len, TRACE_BUF_SIZE-1);
  2575. trace_buf[len] = 0;
  2576. __raw_spin_lock(&data->lock);
  2577. entry = tracing_get_trace_entry(tr, data);
  2578. tracing_generic_entry_update(entry, flags);
  2579. entry->type = TRACE_PRINT;
  2580. entry->field.print.ip = ip;
  2581. write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
  2582. memcpy(&entry->field.print.buf, trace_buf, write);
  2583. entry->field.print.buf[write] = 0;
  2584. written = write;
  2585. if (written != len)
  2586. entry->field.flags |= TRACE_FLAG_CONT;
  2587. while (written != len) {
  2588. entry = tracing_get_trace_entry(tr, data);
  2589. entry->type = TRACE_CONT;
  2590. write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
  2591. memcpy(&entry->cont.buf, trace_buf+written, write);
  2592. entry->cont.buf[write] = 0;
  2593. written += write;
  2594. }
  2595. __raw_spin_unlock(&data->lock);
  2596. spin_unlock(&trace_buf_lock);
  2597. out:
  2598. atomic_dec(&data->disabled);
  2599. local_irq_restore(flags);
  2600. return len;
  2601. }
  2602. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2603. static int trace_panic_handler(struct notifier_block *this,
  2604. unsigned long event, void *unused)
  2605. {
  2606. ftrace_dump();
  2607. return NOTIFY_OK;
  2608. }
  2609. static struct notifier_block trace_panic_notifier = {
  2610. .notifier_call = trace_panic_handler,
  2611. .next = NULL,
  2612. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2613. };
  2614. static int trace_die_handler(struct notifier_block *self,
  2615. unsigned long val,
  2616. void *data)
  2617. {
  2618. switch (val) {
  2619. case DIE_OOPS:
  2620. ftrace_dump();
  2621. break;
  2622. default:
  2623. break;
  2624. }
  2625. return NOTIFY_OK;
  2626. }
  2627. static struct notifier_block trace_die_notifier = {
  2628. .notifier_call = trace_die_handler,
  2629. .priority = 200
  2630. };
  2631. /*
  2632. * printk is set to max of 1024, we really don't need it that big.
  2633. * Nothing should be printing 1000 characters anyway.
  2634. */
  2635. #define TRACE_MAX_PRINT 1000
  2636. /*
  2637. * Define here KERN_TRACE so that we have one place to modify
  2638. * it if we decide to change what log level the ftrace dump
  2639. * should be at.
  2640. */
  2641. #define KERN_TRACE KERN_INFO
  2642. static void
  2643. trace_printk_seq(struct trace_seq *s)
  2644. {
  2645. /* Probably should print a warning here. */
  2646. if (s->len >= 1000)
  2647. s->len = 1000;
  2648. /* should be zero ended, but we are paranoid. */
  2649. s->buffer[s->len] = 0;
  2650. printk(KERN_TRACE "%s", s->buffer);
  2651. trace_seq_reset(s);
  2652. }
  2653. void ftrace_dump(void)
  2654. {
  2655. static DEFINE_SPINLOCK(ftrace_dump_lock);
  2656. /* use static because iter can be a bit big for the stack */
  2657. static struct trace_iterator iter;
  2658. struct trace_array_cpu *data;
  2659. static cpumask_t mask;
  2660. static int dump_ran;
  2661. unsigned long flags;
  2662. int cnt = 0;
  2663. int cpu;
  2664. /* only one dump */
  2665. spin_lock_irqsave(&ftrace_dump_lock, flags);
  2666. if (dump_ran)
  2667. goto out;
  2668. dump_ran = 1;
  2669. /* No turning back! */
  2670. ftrace_kill_atomic();
  2671. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  2672. iter.tr = &global_trace;
  2673. iter.trace = current_trace;
  2674. /*
  2675. * We need to stop all tracing on all CPUS to read the
  2676. * the next buffer. This is a bit expensive, but is
  2677. * not done often. We fill all what we can read,
  2678. * and then release the locks again.
  2679. */
  2680. cpus_clear(mask);
  2681. for_each_tracing_cpu(cpu) {
  2682. data = iter.tr->data[cpu];
  2683. if (!head_page(data) || !data->trace_idx)
  2684. continue;
  2685. atomic_inc(&data->disabled);
  2686. cpu_set(cpu, mask);
  2687. }
  2688. for_each_cpu_mask(cpu, mask) {
  2689. data = iter.tr->data[cpu];
  2690. __raw_spin_lock(&data->lock);
  2691. if (data->overrun > iter.last_overrun[cpu])
  2692. iter.overrun[cpu] +=
  2693. data->overrun - iter.last_overrun[cpu];
  2694. iter.last_overrun[cpu] = data->overrun;
  2695. }
  2696. while (!trace_empty(&iter)) {
  2697. if (!cnt)
  2698. printk(KERN_TRACE "---------------------------------\n");
  2699. cnt++;
  2700. /* reset all but tr, trace, and overruns */
  2701. memset(&iter.seq, 0,
  2702. sizeof(struct trace_iterator) -
  2703. offsetof(struct trace_iterator, seq));
  2704. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  2705. iter.pos = -1;
  2706. if (find_next_entry_inc(&iter) != NULL) {
  2707. print_trace_line(&iter);
  2708. trace_consume(&iter);
  2709. }
  2710. trace_printk_seq(&iter.seq);
  2711. }
  2712. if (!cnt)
  2713. printk(KERN_TRACE " (ftrace buffer empty)\n");
  2714. else
  2715. printk(KERN_TRACE "---------------------------------\n");
  2716. for_each_cpu_mask(cpu, mask) {
  2717. data = iter.tr->data[cpu];
  2718. __raw_spin_unlock(&data->lock);
  2719. }
  2720. for_each_cpu_mask(cpu, mask) {
  2721. data = iter.tr->data[cpu];
  2722. atomic_dec(&data->disabled);
  2723. }
  2724. out:
  2725. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  2726. }
  2727. static int trace_alloc_page(void)
  2728. {
  2729. struct trace_array_cpu *data;
  2730. struct page *page, *tmp;
  2731. LIST_HEAD(pages);
  2732. void *array;
  2733. unsigned pages_allocated = 0;
  2734. int i;
  2735. /* first allocate a page for each CPU */
  2736. for_each_tracing_cpu(i) {
  2737. array = (void *)__get_free_page(GFP_KERNEL);
  2738. if (array == NULL) {
  2739. printk(KERN_ERR "tracer: failed to allocate page"
  2740. "for trace buffer!\n");
  2741. goto free_pages;
  2742. }
  2743. pages_allocated++;
  2744. page = virt_to_page(array);
  2745. list_add(&page->lru, &pages);
  2746. /* Only allocate if we are actually using the max trace */
  2747. #ifdef CONFIG_TRACER_MAX_TRACE
  2748. array = (void *)__get_free_page(GFP_KERNEL);
  2749. if (array == NULL) {
  2750. printk(KERN_ERR "tracer: failed to allocate page"
  2751. "for trace buffer!\n");
  2752. goto free_pages;
  2753. }
  2754. pages_allocated++;
  2755. page = virt_to_page(array);
  2756. list_add(&page->lru, &pages);
  2757. #endif
  2758. }
  2759. /* Now that we successfully allocate a page per CPU, add them */
  2760. for_each_tracing_cpu(i) {
  2761. data = global_trace.data[i];
  2762. page = list_entry(pages.next, struct page, lru);
  2763. list_del_init(&page->lru);
  2764. list_add_tail(&page->lru, &data->trace_pages);
  2765. ClearPageLRU(page);
  2766. #ifdef CONFIG_TRACER_MAX_TRACE
  2767. data = max_tr.data[i];
  2768. page = list_entry(pages.next, struct page, lru);
  2769. list_del_init(&page->lru);
  2770. list_add_tail(&page->lru, &data->trace_pages);
  2771. SetPageLRU(page);
  2772. #endif
  2773. }
  2774. tracing_pages_allocated += pages_allocated;
  2775. global_trace.entries += ENTRIES_PER_PAGE;
  2776. return 0;
  2777. free_pages:
  2778. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2779. list_del_init(&page->lru);
  2780. __free_page(page);
  2781. }
  2782. return -ENOMEM;
  2783. }
  2784. static int trace_free_page(void)
  2785. {
  2786. struct trace_array_cpu *data;
  2787. struct page *page;
  2788. struct list_head *p;
  2789. int i;
  2790. int ret = 0;
  2791. /* free one page from each buffer */
  2792. for_each_tracing_cpu(i) {
  2793. data = global_trace.data[i];
  2794. p = data->trace_pages.next;
  2795. if (p == &data->trace_pages) {
  2796. /* should never happen */
  2797. WARN_ON(1);
  2798. tracing_disabled = 1;
  2799. ret = -1;
  2800. break;
  2801. }
  2802. page = list_entry(p, struct page, lru);
  2803. ClearPageLRU(page);
  2804. list_del(&page->lru);
  2805. tracing_pages_allocated--;
  2806. tracing_pages_allocated--;
  2807. __free_page(page);
  2808. tracing_reset(data);
  2809. #ifdef CONFIG_TRACER_MAX_TRACE
  2810. data = max_tr.data[i];
  2811. p = data->trace_pages.next;
  2812. if (p == &data->trace_pages) {
  2813. /* should never happen */
  2814. WARN_ON(1);
  2815. tracing_disabled = 1;
  2816. ret = -1;
  2817. break;
  2818. }
  2819. page = list_entry(p, struct page, lru);
  2820. ClearPageLRU(page);
  2821. list_del(&page->lru);
  2822. __free_page(page);
  2823. tracing_reset(data);
  2824. #endif
  2825. }
  2826. global_trace.entries -= ENTRIES_PER_PAGE;
  2827. return ret;
  2828. }
  2829. __init static int tracer_alloc_buffers(void)
  2830. {
  2831. struct trace_array_cpu *data;
  2832. void *array;
  2833. struct page *page;
  2834. int pages = 0;
  2835. int ret = -ENOMEM;
  2836. int i;
  2837. /* TODO: make the number of buffers hot pluggable with CPUS */
  2838. tracing_nr_buffers = num_possible_cpus();
  2839. tracing_buffer_mask = cpu_possible_map;
  2840. /* Allocate the first page for all buffers */
  2841. for_each_tracing_cpu(i) {
  2842. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2843. max_tr.data[i] = &per_cpu(max_data, i);
  2844. array = (void *)__get_free_page(GFP_KERNEL);
  2845. if (array == NULL) {
  2846. printk(KERN_ERR "tracer: failed to allocate page"
  2847. "for trace buffer!\n");
  2848. goto free_buffers;
  2849. }
  2850. /* set the array to the list */
  2851. INIT_LIST_HEAD(&data->trace_pages);
  2852. page = virt_to_page(array);
  2853. list_add(&page->lru, &data->trace_pages);
  2854. /* use the LRU flag to differentiate the two buffers */
  2855. ClearPageLRU(page);
  2856. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2857. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2858. /* Only allocate if we are actually using the max trace */
  2859. #ifdef CONFIG_TRACER_MAX_TRACE
  2860. array = (void *)__get_free_page(GFP_KERNEL);
  2861. if (array == NULL) {
  2862. printk(KERN_ERR "tracer: failed to allocate page"
  2863. "for trace buffer!\n");
  2864. goto free_buffers;
  2865. }
  2866. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2867. page = virt_to_page(array);
  2868. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2869. SetPageLRU(page);
  2870. #endif
  2871. }
  2872. /*
  2873. * Since we allocate by orders of pages, we may be able to
  2874. * round up a bit.
  2875. */
  2876. global_trace.entries = ENTRIES_PER_PAGE;
  2877. pages++;
  2878. while (global_trace.entries < trace_nr_entries) {
  2879. if (trace_alloc_page())
  2880. break;
  2881. pages++;
  2882. }
  2883. max_tr.entries = global_trace.entries;
  2884. pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
  2885. pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
  2886. pr_info(" actual entries %ld\n", global_trace.entries);
  2887. tracer_init_debugfs();
  2888. trace_init_cmdlines();
  2889. register_tracer(&no_tracer);
  2890. current_trace = &no_tracer;
  2891. /* All seems OK, enable tracing */
  2892. global_trace.ctrl = tracer_enabled;
  2893. tracing_disabled = 0;
  2894. atomic_notifier_chain_register(&panic_notifier_list,
  2895. &trace_panic_notifier);
  2896. register_die_notifier(&trace_die_notifier);
  2897. return 0;
  2898. free_buffers:
  2899. for (i-- ; i >= 0; i--) {
  2900. struct page *page, *tmp;
  2901. struct trace_array_cpu *data = global_trace.data[i];
  2902. if (data) {
  2903. list_for_each_entry_safe(page, tmp,
  2904. &data->trace_pages, lru) {
  2905. list_del_init(&page->lru);
  2906. __free_page(page);
  2907. }
  2908. }
  2909. #ifdef CONFIG_TRACER_MAX_TRACE
  2910. data = max_tr.data[i];
  2911. if (data) {
  2912. list_for_each_entry_safe(page, tmp,
  2913. &data->trace_pages, lru) {
  2914. list_del_init(&page->lru);
  2915. __free_page(page);
  2916. }
  2917. }
  2918. #endif
  2919. }
  2920. return ret;
  2921. }
  2922. fs_initcall(tracer_alloc_buffers);