trace.c 93 KB

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