trace.c 103 KB

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