trace.c 98 KB

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