trace.c 112 KB

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