trace.c 101 KB

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