trace.c 95 KB

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