trace.c 98 KB

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