trace.c 90 KB

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