trace.c 87 KB

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