trace.c 77 KB

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