trace.c 91 KB

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