trace.c 118 KB

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