trace.c 102 KB

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