trace.c 78 KB

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