trace.c 102 KB

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