trace.c 114 KB

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