trace.c 73 KB

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