trace.c 117 KB

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