trace.c 100 KB

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