trace.c 95 KB

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