trace.c 93 KB

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