trace.c 98 KB

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