trace.c 127 KB

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