trace.c 101 KB

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