trace.c 73 KB

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