trace.c 94 KB

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