trace.c 86 KB

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