trace.c 85 KB

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