trace.c 103 KB

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