trace.c 124 KB

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