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