trace.c 84 KB

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