trace.c 95 KB

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