trace.c 119 KB

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