trace.c 74 KB

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