trace.c 123 KB

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