trace.c 93 KB

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