trace.c 110 KB

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