trace.c 117 KB

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