trace.c 73 KB

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