trace.c 104 KB

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