trace.c 118 KB

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