trace.c 97 KB

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