trace.c 122 KB

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