trace.c 120 KB

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