trace.c 93 KB

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