trace.c 98 KB

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