trace.c 93 KB

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