trace.c 103 KB

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