trace.c 117 KB

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