trace.c 90 KB

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