trace.c 79 KB

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