trace.c 119 KB

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