trace.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  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 = ftrace_preempt_disable();
  763. local_save_flags(flags);
  764. cpu = raw_smp_processor_id();
  765. data = tr->data[cpu];
  766. disabled = atomic_inc_return(&data->disabled);
  767. if (likely(disabled == 1))
  768. trace_function(tr, data, ip, parent_ip, flags, pc);
  769. atomic_dec(&data->disabled);
  770. ftrace_preempt_enable(resched);
  771. }
  772. static struct ftrace_ops trace_ops __read_mostly =
  773. {
  774. .func = function_trace_call,
  775. };
  776. void tracing_start_function_trace(void)
  777. {
  778. ftrace_function_enabled = 0;
  779. register_ftrace_function(&trace_ops);
  780. if (tracer_enabled)
  781. ftrace_function_enabled = 1;
  782. }
  783. void tracing_stop_function_trace(void)
  784. {
  785. ftrace_function_enabled = 0;
  786. unregister_ftrace_function(&trace_ops);
  787. }
  788. #endif
  789. enum trace_file_type {
  790. TRACE_FILE_LAT_FMT = 1,
  791. };
  792. static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
  793. {
  794. /* Don't allow ftrace to trace into the ring buffers */
  795. ftrace_disable_cpu();
  796. iter->idx++;
  797. if (iter->buffer_iter[iter->cpu])
  798. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  799. ftrace_enable_cpu();
  800. }
  801. static struct trace_entry *
  802. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  803. {
  804. struct ring_buffer_event *event;
  805. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  806. /* Don't allow ftrace to trace into the ring buffers */
  807. ftrace_disable_cpu();
  808. if (buf_iter)
  809. event = ring_buffer_iter_peek(buf_iter, ts);
  810. else
  811. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  812. ftrace_enable_cpu();
  813. return event ? ring_buffer_event_data(event) : NULL;
  814. }
  815. static struct trace_entry *
  816. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  817. {
  818. struct ring_buffer *buffer = iter->tr->buffer;
  819. struct trace_entry *ent, *next = NULL;
  820. u64 next_ts = 0, ts;
  821. int next_cpu = -1;
  822. int cpu;
  823. for_each_tracing_cpu(cpu) {
  824. if (ring_buffer_empty_cpu(buffer, cpu))
  825. continue;
  826. ent = peek_next_entry(iter, cpu, &ts);
  827. /*
  828. * Pick the entry with the smallest timestamp:
  829. */
  830. if (ent && (!next || ts < next_ts)) {
  831. next = ent;
  832. next_cpu = cpu;
  833. next_ts = ts;
  834. }
  835. }
  836. if (ent_cpu)
  837. *ent_cpu = next_cpu;
  838. if (ent_ts)
  839. *ent_ts = next_ts;
  840. return next;
  841. }
  842. /* Find the next real entry, without updating the iterator itself */
  843. static struct trace_entry *
  844. find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  845. {
  846. return __find_next_entry(iter, ent_cpu, ent_ts);
  847. }
  848. /* Find the next real entry, and increment the iterator to the next entry */
  849. static void *find_next_entry_inc(struct trace_iterator *iter)
  850. {
  851. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  852. if (iter->ent)
  853. trace_iterator_increment(iter, iter->cpu);
  854. return iter->ent ? iter : NULL;
  855. }
  856. static void trace_consume(struct trace_iterator *iter)
  857. {
  858. /* Don't allow ftrace to trace into the ring buffers */
  859. ftrace_disable_cpu();
  860. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  861. ftrace_enable_cpu();
  862. }
  863. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  864. {
  865. struct trace_iterator *iter = m->private;
  866. int i = (int)*pos;
  867. void *ent;
  868. (*pos)++;
  869. /* can't go backwards */
  870. if (iter->idx > i)
  871. return NULL;
  872. if (iter->idx < 0)
  873. ent = find_next_entry_inc(iter);
  874. else
  875. ent = iter;
  876. while (ent && iter->idx < i)
  877. ent = find_next_entry_inc(iter);
  878. iter->pos = *pos;
  879. return ent;
  880. }
  881. static void *s_start(struct seq_file *m, loff_t *pos)
  882. {
  883. struct trace_iterator *iter = m->private;
  884. void *p = NULL;
  885. loff_t l = 0;
  886. int cpu;
  887. mutex_lock(&trace_types_lock);
  888. if (!current_trace || current_trace != iter->trace) {
  889. mutex_unlock(&trace_types_lock);
  890. return NULL;
  891. }
  892. atomic_inc(&trace_record_cmdline_disabled);
  893. /* let the tracer grab locks here if needed */
  894. if (current_trace->start)
  895. current_trace->start(iter);
  896. if (*pos != iter->pos) {
  897. iter->ent = NULL;
  898. iter->cpu = 0;
  899. iter->idx = -1;
  900. ftrace_disable_cpu();
  901. for_each_tracing_cpu(cpu) {
  902. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  903. }
  904. ftrace_enable_cpu();
  905. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  906. ;
  907. } else {
  908. l = *pos - 1;
  909. p = s_next(m, p, &l);
  910. }
  911. return p;
  912. }
  913. static void s_stop(struct seq_file *m, void *p)
  914. {
  915. struct trace_iterator *iter = m->private;
  916. atomic_dec(&trace_record_cmdline_disabled);
  917. /* let the tracer release locks here if needed */
  918. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  919. iter->trace->stop(iter);
  920. mutex_unlock(&trace_types_lock);
  921. }
  922. #define KRETPROBE_MSG "[unknown/kretprobe'd]"
  923. #ifdef CONFIG_KRETPROBES
  924. static inline int kretprobed(unsigned long addr)
  925. {
  926. return addr == (unsigned long)kretprobe_trampoline;
  927. }
  928. #else
  929. static inline int kretprobed(unsigned long addr)
  930. {
  931. return 0;
  932. }
  933. #endif /* CONFIG_KRETPROBES */
  934. static int
  935. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  936. {
  937. #ifdef CONFIG_KALLSYMS
  938. char str[KSYM_SYMBOL_LEN];
  939. kallsyms_lookup(address, NULL, NULL, NULL, str);
  940. return trace_seq_printf(s, fmt, str);
  941. #endif
  942. return 1;
  943. }
  944. static int
  945. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  946. unsigned long address)
  947. {
  948. #ifdef CONFIG_KALLSYMS
  949. char str[KSYM_SYMBOL_LEN];
  950. sprint_symbol(str, address);
  951. return trace_seq_printf(s, fmt, str);
  952. #endif
  953. return 1;
  954. }
  955. #ifndef CONFIG_64BIT
  956. # define IP_FMT "%08lx"
  957. #else
  958. # define IP_FMT "%016lx"
  959. #endif
  960. static int
  961. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  962. {
  963. int ret;
  964. if (!ip)
  965. return trace_seq_printf(s, "0");
  966. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  967. ret = seq_print_sym_offset(s, "%s", ip);
  968. else
  969. ret = seq_print_sym_short(s, "%s", ip);
  970. if (!ret)
  971. return 0;
  972. if (sym_flags & TRACE_ITER_SYM_ADDR)
  973. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  974. return ret;
  975. }
  976. static void print_lat_help_header(struct seq_file *m)
  977. {
  978. seq_puts(m, "# _------=> CPU# \n");
  979. seq_puts(m, "# / _-----=> irqs-off \n");
  980. seq_puts(m, "# | / _----=> need-resched \n");
  981. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  982. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  983. seq_puts(m, "# |||| / \n");
  984. seq_puts(m, "# ||||| delay \n");
  985. seq_puts(m, "# cmd pid ||||| time | caller \n");
  986. seq_puts(m, "# \\ / ||||| \\ | / \n");
  987. }
  988. static void print_func_help_header(struct seq_file *m)
  989. {
  990. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  991. seq_puts(m, "# | | | | |\n");
  992. }
  993. static void
  994. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  995. {
  996. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  997. struct trace_array *tr = iter->tr;
  998. struct trace_array_cpu *data = tr->data[tr->cpu];
  999. struct tracer *type = current_trace;
  1000. unsigned long total;
  1001. unsigned long entries;
  1002. const char *name = "preemption";
  1003. if (type)
  1004. name = type->name;
  1005. entries = ring_buffer_entries(iter->tr->buffer);
  1006. total = entries +
  1007. ring_buffer_overruns(iter->tr->buffer);
  1008. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1009. name, UTS_RELEASE);
  1010. seq_puts(m, "-----------------------------------"
  1011. "---------------------------------\n");
  1012. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1013. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1014. nsecs_to_usecs(data->saved_latency),
  1015. entries,
  1016. total,
  1017. tr->cpu,
  1018. #if defined(CONFIG_PREEMPT_NONE)
  1019. "server",
  1020. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1021. "desktop",
  1022. #elif defined(CONFIG_PREEMPT)
  1023. "preempt",
  1024. #else
  1025. "unknown",
  1026. #endif
  1027. /* These are reserved for later use */
  1028. 0, 0, 0, 0);
  1029. #ifdef CONFIG_SMP
  1030. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1031. #else
  1032. seq_puts(m, ")\n");
  1033. #endif
  1034. seq_puts(m, " -----------------\n");
  1035. seq_printf(m, " | task: %.16s-%d "
  1036. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1037. data->comm, data->pid, data->uid, data->nice,
  1038. data->policy, data->rt_priority);
  1039. seq_puts(m, " -----------------\n");
  1040. if (data->critical_start) {
  1041. seq_puts(m, " => started at: ");
  1042. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1043. trace_print_seq(m, &iter->seq);
  1044. seq_puts(m, "\n => ended at: ");
  1045. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1046. trace_print_seq(m, &iter->seq);
  1047. seq_puts(m, "\n");
  1048. }
  1049. seq_puts(m, "\n");
  1050. }
  1051. static void
  1052. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1053. {
  1054. int hardirq, softirq;
  1055. char *comm;
  1056. comm = trace_find_cmdline(entry->pid);
  1057. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1058. trace_seq_printf(s, "%3d", cpu);
  1059. trace_seq_printf(s, "%c%c",
  1060. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  1061. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
  1062. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1063. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1064. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1065. if (hardirq && softirq) {
  1066. trace_seq_putc(s, 'H');
  1067. } else {
  1068. if (hardirq) {
  1069. trace_seq_putc(s, 'h');
  1070. } else {
  1071. if (softirq)
  1072. trace_seq_putc(s, 's');
  1073. else
  1074. trace_seq_putc(s, '.');
  1075. }
  1076. }
  1077. if (entry->preempt_count)
  1078. trace_seq_printf(s, "%x", entry->preempt_count);
  1079. else
  1080. trace_seq_puts(s, ".");
  1081. }
  1082. unsigned long preempt_mark_thresh = 100;
  1083. static void
  1084. lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
  1085. unsigned long rel_usecs)
  1086. {
  1087. trace_seq_printf(s, " %4lldus", abs_usecs);
  1088. if (rel_usecs > preempt_mark_thresh)
  1089. trace_seq_puts(s, "!: ");
  1090. else if (rel_usecs > 1)
  1091. trace_seq_puts(s, "+: ");
  1092. else
  1093. trace_seq_puts(s, " : ");
  1094. }
  1095. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1096. /*
  1097. * The message is supposed to contain an ending newline.
  1098. * If the printing stops prematurely, try to add a newline of our own.
  1099. */
  1100. void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1101. {
  1102. struct trace_entry *ent;
  1103. struct trace_field_cont *cont;
  1104. bool ok = true;
  1105. ent = peek_next_entry(iter, iter->cpu, NULL);
  1106. if (!ent || ent->type != TRACE_CONT) {
  1107. trace_seq_putc(s, '\n');
  1108. return;
  1109. }
  1110. do {
  1111. cont = (struct trace_field_cont *)ent;
  1112. if (ok)
  1113. ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
  1114. ftrace_disable_cpu();
  1115. if (iter->buffer_iter[iter->cpu])
  1116. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1117. else
  1118. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  1119. ftrace_enable_cpu();
  1120. ent = peek_next_entry(iter, iter->cpu, NULL);
  1121. } while (ent && ent->type == TRACE_CONT);
  1122. if (!ok)
  1123. trace_seq_putc(s, '\n');
  1124. }
  1125. static enum print_line_t
  1126. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1127. {
  1128. struct trace_seq *s = &iter->seq;
  1129. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1130. struct trace_entry *next_entry;
  1131. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1132. struct trace_entry *entry = iter->ent;
  1133. unsigned long abs_usecs;
  1134. unsigned long rel_usecs;
  1135. u64 next_ts;
  1136. char *comm;
  1137. int S, T;
  1138. int i;
  1139. unsigned state;
  1140. if (entry->type == TRACE_CONT)
  1141. return TRACE_TYPE_HANDLED;
  1142. next_entry = find_next_entry(iter, NULL, &next_ts);
  1143. if (!next_entry)
  1144. next_ts = iter->ts;
  1145. rel_usecs = ns2usecs(next_ts - iter->ts);
  1146. abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
  1147. if (verbose) {
  1148. comm = trace_find_cmdline(entry->pid);
  1149. trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
  1150. " %ld.%03ldms (+%ld.%03ldms): ",
  1151. comm,
  1152. entry->pid, cpu, entry->flags,
  1153. entry->preempt_count, trace_idx,
  1154. ns2usecs(iter->ts),
  1155. abs_usecs/1000,
  1156. abs_usecs % 1000, rel_usecs/1000,
  1157. rel_usecs % 1000);
  1158. } else {
  1159. lat_print_generic(s, entry, cpu);
  1160. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1161. }
  1162. switch (entry->type) {
  1163. case TRACE_FN: {
  1164. struct ftrace_entry *field;
  1165. trace_assign_type(field, entry);
  1166. seq_print_ip_sym(s, field->ip, sym_flags);
  1167. trace_seq_puts(s, " (");
  1168. if (kretprobed(field->parent_ip))
  1169. trace_seq_puts(s, KRETPROBE_MSG);
  1170. else
  1171. seq_print_ip_sym(s, field->parent_ip, sym_flags);
  1172. trace_seq_puts(s, ")\n");
  1173. break;
  1174. }
  1175. case TRACE_CTX:
  1176. case TRACE_WAKE: {
  1177. struct ctx_switch_entry *field;
  1178. trace_assign_type(field, entry);
  1179. T = field->next_state < sizeof(state_to_char) ?
  1180. state_to_char[field->next_state] : 'X';
  1181. state = field->prev_state ?
  1182. __ffs(field->prev_state) + 1 : 0;
  1183. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1184. comm = trace_find_cmdline(field->next_pid);
  1185. trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  1186. field->prev_pid,
  1187. field->prev_prio,
  1188. S, entry->type == TRACE_CTX ? "==>" : " +",
  1189. field->next_cpu,
  1190. field->next_pid,
  1191. field->next_prio,
  1192. T, comm);
  1193. break;
  1194. }
  1195. case TRACE_SPECIAL: {
  1196. struct special_entry *field;
  1197. trace_assign_type(field, entry);
  1198. trace_seq_printf(s, "# %ld %ld %ld\n",
  1199. field->arg1,
  1200. field->arg2,
  1201. field->arg3);
  1202. break;
  1203. }
  1204. case TRACE_STACK: {
  1205. struct stack_entry *field;
  1206. trace_assign_type(field, entry);
  1207. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1208. if (i)
  1209. trace_seq_puts(s, " <= ");
  1210. seq_print_ip_sym(s, field->caller[i], sym_flags);
  1211. }
  1212. trace_seq_puts(s, "\n");
  1213. break;
  1214. }
  1215. case TRACE_PRINT: {
  1216. struct print_entry *field;
  1217. trace_assign_type(field, entry);
  1218. seq_print_ip_sym(s, field->ip, sym_flags);
  1219. trace_seq_printf(s, ": %s", field->buf);
  1220. if (entry->flags & TRACE_FLAG_CONT)
  1221. trace_seq_print_cont(s, iter);
  1222. break;
  1223. }
  1224. default:
  1225. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1226. }
  1227. return TRACE_TYPE_HANDLED;
  1228. }
  1229. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1230. {
  1231. struct trace_seq *s = &iter->seq;
  1232. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1233. struct trace_entry *entry;
  1234. unsigned long usec_rem;
  1235. unsigned long long t;
  1236. unsigned long secs;
  1237. char *comm;
  1238. int ret;
  1239. int S, T;
  1240. int i;
  1241. entry = iter->ent;
  1242. if (entry->type == TRACE_CONT)
  1243. return TRACE_TYPE_HANDLED;
  1244. comm = trace_find_cmdline(iter->ent->pid);
  1245. t = ns2usecs(iter->ts);
  1246. usec_rem = do_div(t, 1000000ULL);
  1247. secs = (unsigned long)t;
  1248. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1249. if (!ret)
  1250. return TRACE_TYPE_PARTIAL_LINE;
  1251. ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
  1252. if (!ret)
  1253. return TRACE_TYPE_PARTIAL_LINE;
  1254. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1255. if (!ret)
  1256. return TRACE_TYPE_PARTIAL_LINE;
  1257. switch (entry->type) {
  1258. case TRACE_FN: {
  1259. struct ftrace_entry *field;
  1260. trace_assign_type(field, entry);
  1261. ret = seq_print_ip_sym(s, field->ip, sym_flags);
  1262. if (!ret)
  1263. return TRACE_TYPE_PARTIAL_LINE;
  1264. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1265. field->parent_ip) {
  1266. ret = trace_seq_printf(s, " <-");
  1267. if (!ret)
  1268. return TRACE_TYPE_PARTIAL_LINE;
  1269. if (kretprobed(field->parent_ip))
  1270. ret = trace_seq_puts(s, KRETPROBE_MSG);
  1271. else
  1272. ret = seq_print_ip_sym(s,
  1273. field->parent_ip,
  1274. sym_flags);
  1275. if (!ret)
  1276. return TRACE_TYPE_PARTIAL_LINE;
  1277. }
  1278. ret = trace_seq_printf(s, "\n");
  1279. if (!ret)
  1280. return TRACE_TYPE_PARTIAL_LINE;
  1281. break;
  1282. }
  1283. case TRACE_CTX:
  1284. case TRACE_WAKE: {
  1285. struct ctx_switch_entry *field;
  1286. trace_assign_type(field, entry);
  1287. S = field->prev_state < sizeof(state_to_char) ?
  1288. state_to_char[field->prev_state] : 'X';
  1289. T = field->next_state < sizeof(state_to_char) ?
  1290. state_to_char[field->next_state] : 'X';
  1291. ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
  1292. field->prev_pid,
  1293. field->prev_prio,
  1294. S,
  1295. entry->type == TRACE_CTX ? "==>" : " +",
  1296. field->next_cpu,
  1297. field->next_pid,
  1298. field->next_prio,
  1299. T);
  1300. if (!ret)
  1301. return TRACE_TYPE_PARTIAL_LINE;
  1302. break;
  1303. }
  1304. case TRACE_SPECIAL: {
  1305. struct special_entry *field;
  1306. trace_assign_type(field, entry);
  1307. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1308. field->arg1,
  1309. field->arg2,
  1310. field->arg3);
  1311. if (!ret)
  1312. return TRACE_TYPE_PARTIAL_LINE;
  1313. break;
  1314. }
  1315. case TRACE_STACK: {
  1316. struct stack_entry *field;
  1317. trace_assign_type(field, entry);
  1318. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1319. if (i) {
  1320. ret = trace_seq_puts(s, " <= ");
  1321. if (!ret)
  1322. return TRACE_TYPE_PARTIAL_LINE;
  1323. }
  1324. ret = seq_print_ip_sym(s, field->caller[i],
  1325. sym_flags);
  1326. if (!ret)
  1327. return TRACE_TYPE_PARTIAL_LINE;
  1328. }
  1329. ret = trace_seq_puts(s, "\n");
  1330. if (!ret)
  1331. return TRACE_TYPE_PARTIAL_LINE;
  1332. break;
  1333. }
  1334. case TRACE_PRINT: {
  1335. struct print_entry *field;
  1336. trace_assign_type(field, entry);
  1337. seq_print_ip_sym(s, field->ip, sym_flags);
  1338. trace_seq_printf(s, ": %s", field->buf);
  1339. if (entry->flags & TRACE_FLAG_CONT)
  1340. trace_seq_print_cont(s, iter);
  1341. break;
  1342. }
  1343. }
  1344. return TRACE_TYPE_HANDLED;
  1345. }
  1346. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1347. {
  1348. struct trace_seq *s = &iter->seq;
  1349. struct trace_entry *entry;
  1350. int ret;
  1351. int S, T;
  1352. entry = iter->ent;
  1353. if (entry->type == TRACE_CONT)
  1354. return TRACE_TYPE_HANDLED;
  1355. ret = trace_seq_printf(s, "%d %d %llu ",
  1356. entry->pid, iter->cpu, iter->ts);
  1357. if (!ret)
  1358. return TRACE_TYPE_PARTIAL_LINE;
  1359. switch (entry->type) {
  1360. case TRACE_FN: {
  1361. struct ftrace_entry *field;
  1362. trace_assign_type(field, entry);
  1363. ret = trace_seq_printf(s, "%x %x\n",
  1364. field->ip,
  1365. field->parent_ip);
  1366. if (!ret)
  1367. return TRACE_TYPE_PARTIAL_LINE;
  1368. break;
  1369. }
  1370. case TRACE_CTX:
  1371. case TRACE_WAKE: {
  1372. struct ctx_switch_entry *field;
  1373. trace_assign_type(field, entry);
  1374. S = field->prev_state < sizeof(state_to_char) ?
  1375. state_to_char[field->prev_state] : 'X';
  1376. T = field->next_state < sizeof(state_to_char) ?
  1377. state_to_char[field->next_state] : 'X';
  1378. if (entry->type == TRACE_WAKE)
  1379. S = '+';
  1380. ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
  1381. field->prev_pid,
  1382. field->prev_prio,
  1383. S,
  1384. field->next_cpu,
  1385. field->next_pid,
  1386. field->next_prio,
  1387. T);
  1388. if (!ret)
  1389. return TRACE_TYPE_PARTIAL_LINE;
  1390. break;
  1391. }
  1392. case TRACE_SPECIAL:
  1393. case TRACE_STACK: {
  1394. struct special_entry *field;
  1395. trace_assign_type(field, entry);
  1396. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1397. field->arg1,
  1398. field->arg2,
  1399. field->arg3);
  1400. if (!ret)
  1401. return TRACE_TYPE_PARTIAL_LINE;
  1402. break;
  1403. }
  1404. case TRACE_PRINT: {
  1405. struct print_entry *field;
  1406. trace_assign_type(field, entry);
  1407. trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
  1408. if (entry->flags & TRACE_FLAG_CONT)
  1409. trace_seq_print_cont(s, iter);
  1410. break;
  1411. }
  1412. }
  1413. return TRACE_TYPE_HANDLED;
  1414. }
  1415. #define SEQ_PUT_FIELD_RET(s, x) \
  1416. do { \
  1417. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1418. return 0; \
  1419. } while (0)
  1420. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1421. do { \
  1422. BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
  1423. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1424. return 0; \
  1425. } while (0)
  1426. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1427. {
  1428. struct trace_seq *s = &iter->seq;
  1429. unsigned char newline = '\n';
  1430. struct trace_entry *entry;
  1431. int S, T;
  1432. entry = iter->ent;
  1433. if (entry->type == TRACE_CONT)
  1434. return TRACE_TYPE_HANDLED;
  1435. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1436. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1437. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1438. switch (entry->type) {
  1439. case TRACE_FN: {
  1440. struct ftrace_entry *field;
  1441. trace_assign_type(field, entry);
  1442. SEQ_PUT_HEX_FIELD_RET(s, field->ip);
  1443. SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
  1444. break;
  1445. }
  1446. case TRACE_CTX:
  1447. case TRACE_WAKE: {
  1448. struct ctx_switch_entry *field;
  1449. trace_assign_type(field, entry);
  1450. S = field->prev_state < sizeof(state_to_char) ?
  1451. state_to_char[field->prev_state] : 'X';
  1452. T = field->next_state < sizeof(state_to_char) ?
  1453. state_to_char[field->next_state] : 'X';
  1454. if (entry->type == TRACE_WAKE)
  1455. S = '+';
  1456. SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
  1457. SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
  1458. SEQ_PUT_HEX_FIELD_RET(s, S);
  1459. SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
  1460. SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
  1461. SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
  1462. SEQ_PUT_HEX_FIELD_RET(s, T);
  1463. break;
  1464. }
  1465. case TRACE_SPECIAL:
  1466. case TRACE_STACK: {
  1467. struct special_entry *field;
  1468. trace_assign_type(field, entry);
  1469. SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
  1470. SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
  1471. SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
  1472. break;
  1473. }
  1474. }
  1475. SEQ_PUT_FIELD_RET(s, newline);
  1476. return TRACE_TYPE_HANDLED;
  1477. }
  1478. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1479. {
  1480. struct trace_seq *s = &iter->seq;
  1481. struct trace_entry *entry;
  1482. entry = iter->ent;
  1483. if (entry->type == TRACE_CONT)
  1484. return TRACE_TYPE_HANDLED;
  1485. SEQ_PUT_FIELD_RET(s, entry->pid);
  1486. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1487. SEQ_PUT_FIELD_RET(s, iter->ts);
  1488. switch (entry->type) {
  1489. case TRACE_FN: {
  1490. struct ftrace_entry *field;
  1491. trace_assign_type(field, entry);
  1492. SEQ_PUT_FIELD_RET(s, field->ip);
  1493. SEQ_PUT_FIELD_RET(s, field->parent_ip);
  1494. break;
  1495. }
  1496. case TRACE_CTX: {
  1497. struct ctx_switch_entry *field;
  1498. trace_assign_type(field, entry);
  1499. SEQ_PUT_FIELD_RET(s, field->prev_pid);
  1500. SEQ_PUT_FIELD_RET(s, field->prev_prio);
  1501. SEQ_PUT_FIELD_RET(s, field->prev_state);
  1502. SEQ_PUT_FIELD_RET(s, field->next_pid);
  1503. SEQ_PUT_FIELD_RET(s, field->next_prio);
  1504. SEQ_PUT_FIELD_RET(s, field->next_state);
  1505. break;
  1506. }
  1507. case TRACE_SPECIAL:
  1508. case TRACE_STACK: {
  1509. struct special_entry *field;
  1510. trace_assign_type(field, entry);
  1511. SEQ_PUT_FIELD_RET(s, field->arg1);
  1512. SEQ_PUT_FIELD_RET(s, field->arg2);
  1513. SEQ_PUT_FIELD_RET(s, field->arg3);
  1514. break;
  1515. }
  1516. }
  1517. return 1;
  1518. }
  1519. static int trace_empty(struct trace_iterator *iter)
  1520. {
  1521. int cpu;
  1522. for_each_tracing_cpu(cpu) {
  1523. if (iter->buffer_iter[cpu]) {
  1524. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1525. return 0;
  1526. } else {
  1527. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1528. return 0;
  1529. }
  1530. }
  1531. return 1;
  1532. }
  1533. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1534. {
  1535. enum print_line_t ret;
  1536. if (iter->trace && iter->trace->print_line) {
  1537. ret = iter->trace->print_line(iter);
  1538. if (ret != TRACE_TYPE_UNHANDLED)
  1539. return ret;
  1540. }
  1541. if (trace_flags & TRACE_ITER_BIN)
  1542. return print_bin_fmt(iter);
  1543. if (trace_flags & TRACE_ITER_HEX)
  1544. return print_hex_fmt(iter);
  1545. if (trace_flags & TRACE_ITER_RAW)
  1546. return print_raw_fmt(iter);
  1547. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1548. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1549. return print_trace_fmt(iter);
  1550. }
  1551. static int s_show(struct seq_file *m, void *v)
  1552. {
  1553. struct trace_iterator *iter = v;
  1554. if (iter->ent == NULL) {
  1555. if (iter->tr) {
  1556. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1557. seq_puts(m, "#\n");
  1558. }
  1559. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1560. /* print nothing if the buffers are empty */
  1561. if (trace_empty(iter))
  1562. return 0;
  1563. print_trace_header(m, iter);
  1564. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1565. print_lat_help_header(m);
  1566. } else {
  1567. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1568. print_func_help_header(m);
  1569. }
  1570. } else {
  1571. print_trace_line(iter);
  1572. trace_print_seq(m, &iter->seq);
  1573. }
  1574. return 0;
  1575. }
  1576. static struct seq_operations tracer_seq_ops = {
  1577. .start = s_start,
  1578. .next = s_next,
  1579. .stop = s_stop,
  1580. .show = s_show,
  1581. };
  1582. static struct trace_iterator *
  1583. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1584. {
  1585. struct trace_iterator *iter;
  1586. struct seq_file *m;
  1587. int cpu;
  1588. if (tracing_disabled) {
  1589. *ret = -ENODEV;
  1590. return NULL;
  1591. }
  1592. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1593. if (!iter) {
  1594. *ret = -ENOMEM;
  1595. goto out;
  1596. }
  1597. mutex_lock(&trace_types_lock);
  1598. if (current_trace && current_trace->print_max)
  1599. iter->tr = &max_tr;
  1600. else
  1601. iter->tr = inode->i_private;
  1602. iter->trace = current_trace;
  1603. iter->pos = -1;
  1604. for_each_tracing_cpu(cpu) {
  1605. iter->buffer_iter[cpu] =
  1606. ring_buffer_read_start(iter->tr->buffer, cpu);
  1607. if (!iter->buffer_iter[cpu])
  1608. goto fail_buffer;
  1609. }
  1610. /* TODO stop tracer */
  1611. *ret = seq_open(file, &tracer_seq_ops);
  1612. if (*ret)
  1613. goto fail_buffer;
  1614. m = file->private_data;
  1615. m->private = iter;
  1616. /* stop the trace while dumping */
  1617. if (iter->tr->ctrl) {
  1618. tracer_enabled = 0;
  1619. ftrace_function_enabled = 0;
  1620. }
  1621. if (iter->trace && iter->trace->open)
  1622. iter->trace->open(iter);
  1623. mutex_unlock(&trace_types_lock);
  1624. out:
  1625. return iter;
  1626. fail_buffer:
  1627. for_each_tracing_cpu(cpu) {
  1628. if (iter->buffer_iter[cpu])
  1629. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1630. }
  1631. mutex_unlock(&trace_types_lock);
  1632. return ERR_PTR(-ENOMEM);
  1633. }
  1634. int tracing_open_generic(struct inode *inode, struct file *filp)
  1635. {
  1636. if (tracing_disabled)
  1637. return -ENODEV;
  1638. filp->private_data = inode->i_private;
  1639. return 0;
  1640. }
  1641. int tracing_release(struct inode *inode, struct file *file)
  1642. {
  1643. struct seq_file *m = (struct seq_file *)file->private_data;
  1644. struct trace_iterator *iter = m->private;
  1645. int cpu;
  1646. mutex_lock(&trace_types_lock);
  1647. for_each_tracing_cpu(cpu) {
  1648. if (iter->buffer_iter[cpu])
  1649. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1650. }
  1651. if (iter->trace && iter->trace->close)
  1652. iter->trace->close(iter);
  1653. /* reenable tracing if it was previously enabled */
  1654. if (iter->tr->ctrl) {
  1655. tracer_enabled = 1;
  1656. /*
  1657. * It is safe to enable function tracing even if it
  1658. * isn't used
  1659. */
  1660. ftrace_function_enabled = 1;
  1661. }
  1662. mutex_unlock(&trace_types_lock);
  1663. seq_release(inode, file);
  1664. kfree(iter);
  1665. return 0;
  1666. }
  1667. static int tracing_open(struct inode *inode, struct file *file)
  1668. {
  1669. int ret;
  1670. __tracing_open(inode, file, &ret);
  1671. return ret;
  1672. }
  1673. static int tracing_lt_open(struct inode *inode, struct file *file)
  1674. {
  1675. struct trace_iterator *iter;
  1676. int ret;
  1677. iter = __tracing_open(inode, file, &ret);
  1678. if (!ret)
  1679. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1680. return ret;
  1681. }
  1682. static void *
  1683. t_next(struct seq_file *m, void *v, loff_t *pos)
  1684. {
  1685. struct tracer *t = m->private;
  1686. (*pos)++;
  1687. if (t)
  1688. t = t->next;
  1689. m->private = t;
  1690. return t;
  1691. }
  1692. static void *t_start(struct seq_file *m, loff_t *pos)
  1693. {
  1694. struct tracer *t = m->private;
  1695. loff_t l = 0;
  1696. mutex_lock(&trace_types_lock);
  1697. for (; t && l < *pos; t = t_next(m, t, &l))
  1698. ;
  1699. return t;
  1700. }
  1701. static void t_stop(struct seq_file *m, void *p)
  1702. {
  1703. mutex_unlock(&trace_types_lock);
  1704. }
  1705. static int t_show(struct seq_file *m, void *v)
  1706. {
  1707. struct tracer *t = v;
  1708. if (!t)
  1709. return 0;
  1710. seq_printf(m, "%s", t->name);
  1711. if (t->next)
  1712. seq_putc(m, ' ');
  1713. else
  1714. seq_putc(m, '\n');
  1715. return 0;
  1716. }
  1717. static struct seq_operations show_traces_seq_ops = {
  1718. .start = t_start,
  1719. .next = t_next,
  1720. .stop = t_stop,
  1721. .show = t_show,
  1722. };
  1723. static int show_traces_open(struct inode *inode, struct file *file)
  1724. {
  1725. int ret;
  1726. if (tracing_disabled)
  1727. return -ENODEV;
  1728. ret = seq_open(file, &show_traces_seq_ops);
  1729. if (!ret) {
  1730. struct seq_file *m = file->private_data;
  1731. m->private = trace_types;
  1732. }
  1733. return ret;
  1734. }
  1735. static struct file_operations tracing_fops = {
  1736. .open = tracing_open,
  1737. .read = seq_read,
  1738. .llseek = seq_lseek,
  1739. .release = tracing_release,
  1740. };
  1741. static struct file_operations tracing_lt_fops = {
  1742. .open = tracing_lt_open,
  1743. .read = seq_read,
  1744. .llseek = seq_lseek,
  1745. .release = tracing_release,
  1746. };
  1747. static struct file_operations show_traces_fops = {
  1748. .open = show_traces_open,
  1749. .read = seq_read,
  1750. .release = seq_release,
  1751. };
  1752. /*
  1753. * Only trace on a CPU if the bitmask is set:
  1754. */
  1755. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1756. /*
  1757. * When tracing/tracing_cpu_mask is modified then this holds
  1758. * the new bitmask we are about to install:
  1759. */
  1760. static cpumask_t tracing_cpumask_new;
  1761. /*
  1762. * The tracer itself will not take this lock, but still we want
  1763. * to provide a consistent cpumask to user-space:
  1764. */
  1765. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1766. /*
  1767. * Temporary storage for the character representation of the
  1768. * CPU bitmask (and one more byte for the newline):
  1769. */
  1770. static char mask_str[NR_CPUS + 1];
  1771. static ssize_t
  1772. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1773. size_t count, loff_t *ppos)
  1774. {
  1775. int len;
  1776. mutex_lock(&tracing_cpumask_update_lock);
  1777. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1778. if (count - len < 2) {
  1779. count = -EINVAL;
  1780. goto out_err;
  1781. }
  1782. len += sprintf(mask_str + len, "\n");
  1783. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1784. out_err:
  1785. mutex_unlock(&tracing_cpumask_update_lock);
  1786. return count;
  1787. }
  1788. static ssize_t
  1789. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1790. size_t count, loff_t *ppos)
  1791. {
  1792. int err, cpu;
  1793. mutex_lock(&tracing_cpumask_update_lock);
  1794. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1795. if (err)
  1796. goto err_unlock;
  1797. raw_local_irq_disable();
  1798. __raw_spin_lock(&ftrace_max_lock);
  1799. for_each_tracing_cpu(cpu) {
  1800. /*
  1801. * Increase/decrease the disabled counter if we are
  1802. * about to flip a bit in the cpumask:
  1803. */
  1804. if (cpu_isset(cpu, tracing_cpumask) &&
  1805. !cpu_isset(cpu, tracing_cpumask_new)) {
  1806. atomic_inc(&global_trace.data[cpu]->disabled);
  1807. }
  1808. if (!cpu_isset(cpu, tracing_cpumask) &&
  1809. cpu_isset(cpu, tracing_cpumask_new)) {
  1810. atomic_dec(&global_trace.data[cpu]->disabled);
  1811. }
  1812. }
  1813. __raw_spin_unlock(&ftrace_max_lock);
  1814. raw_local_irq_enable();
  1815. tracing_cpumask = tracing_cpumask_new;
  1816. mutex_unlock(&tracing_cpumask_update_lock);
  1817. return count;
  1818. err_unlock:
  1819. mutex_unlock(&tracing_cpumask_update_lock);
  1820. return err;
  1821. }
  1822. static struct file_operations tracing_cpumask_fops = {
  1823. .open = tracing_open_generic,
  1824. .read = tracing_cpumask_read,
  1825. .write = tracing_cpumask_write,
  1826. };
  1827. static ssize_t
  1828. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1829. size_t cnt, loff_t *ppos)
  1830. {
  1831. char *buf;
  1832. int r = 0;
  1833. int len = 0;
  1834. int i;
  1835. /* calulate max size */
  1836. for (i = 0; trace_options[i]; i++) {
  1837. len += strlen(trace_options[i]);
  1838. len += 3; /* "no" and space */
  1839. }
  1840. /* +2 for \n and \0 */
  1841. buf = kmalloc(len + 2, GFP_KERNEL);
  1842. if (!buf)
  1843. return -ENOMEM;
  1844. for (i = 0; trace_options[i]; i++) {
  1845. if (trace_flags & (1 << i))
  1846. r += sprintf(buf + r, "%s ", trace_options[i]);
  1847. else
  1848. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1849. }
  1850. r += sprintf(buf + r, "\n");
  1851. WARN_ON(r >= len + 2);
  1852. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1853. kfree(buf);
  1854. return r;
  1855. }
  1856. static ssize_t
  1857. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1858. size_t cnt, loff_t *ppos)
  1859. {
  1860. char buf[64];
  1861. char *cmp = buf;
  1862. int neg = 0;
  1863. int i;
  1864. if (cnt >= sizeof(buf))
  1865. return -EINVAL;
  1866. if (copy_from_user(&buf, ubuf, cnt))
  1867. return -EFAULT;
  1868. buf[cnt] = 0;
  1869. if (strncmp(buf, "no", 2) == 0) {
  1870. neg = 1;
  1871. cmp += 2;
  1872. }
  1873. for (i = 0; trace_options[i]; i++) {
  1874. int len = strlen(trace_options[i]);
  1875. if (strncmp(cmp, trace_options[i], len) == 0) {
  1876. if (neg)
  1877. trace_flags &= ~(1 << i);
  1878. else
  1879. trace_flags |= (1 << i);
  1880. break;
  1881. }
  1882. }
  1883. /*
  1884. * If no option could be set, return an error:
  1885. */
  1886. if (!trace_options[i])
  1887. return -EINVAL;
  1888. filp->f_pos += cnt;
  1889. return cnt;
  1890. }
  1891. static struct file_operations tracing_iter_fops = {
  1892. .open = tracing_open_generic,
  1893. .read = tracing_iter_ctrl_read,
  1894. .write = tracing_iter_ctrl_write,
  1895. };
  1896. static const char readme_msg[] =
  1897. "tracing mini-HOWTO:\n\n"
  1898. "# mkdir /debug\n"
  1899. "# mount -t debugfs nodev /debug\n\n"
  1900. "# cat /debug/tracing/available_tracers\n"
  1901. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1902. "# cat /debug/tracing/current_tracer\n"
  1903. "none\n"
  1904. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1905. "# cat /debug/tracing/current_tracer\n"
  1906. "sched_switch\n"
  1907. "# cat /debug/tracing/iter_ctrl\n"
  1908. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1909. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1910. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1911. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1912. "echo 0 > /debug/tracing/tracing_enabled\n"
  1913. ;
  1914. static ssize_t
  1915. tracing_readme_read(struct file *filp, char __user *ubuf,
  1916. size_t cnt, loff_t *ppos)
  1917. {
  1918. return simple_read_from_buffer(ubuf, cnt, ppos,
  1919. readme_msg, strlen(readme_msg));
  1920. }
  1921. static struct file_operations tracing_readme_fops = {
  1922. .open = tracing_open_generic,
  1923. .read = tracing_readme_read,
  1924. };
  1925. static ssize_t
  1926. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1927. size_t cnt, loff_t *ppos)
  1928. {
  1929. struct trace_array *tr = filp->private_data;
  1930. char buf[64];
  1931. int r;
  1932. r = sprintf(buf, "%ld\n", tr->ctrl);
  1933. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1934. }
  1935. static ssize_t
  1936. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1937. size_t cnt, loff_t *ppos)
  1938. {
  1939. struct trace_array *tr = filp->private_data;
  1940. char buf[64];
  1941. long val;
  1942. int ret;
  1943. if (cnt >= sizeof(buf))
  1944. return -EINVAL;
  1945. if (copy_from_user(&buf, ubuf, cnt))
  1946. return -EFAULT;
  1947. buf[cnt] = 0;
  1948. ret = strict_strtoul(buf, 10, &val);
  1949. if (ret < 0)
  1950. return ret;
  1951. val = !!val;
  1952. mutex_lock(&trace_types_lock);
  1953. if (tr->ctrl ^ val) {
  1954. if (val)
  1955. tracer_enabled = 1;
  1956. else
  1957. tracer_enabled = 0;
  1958. tr->ctrl = val;
  1959. if (current_trace && current_trace->ctrl_update)
  1960. current_trace->ctrl_update(tr);
  1961. }
  1962. mutex_unlock(&trace_types_lock);
  1963. filp->f_pos += cnt;
  1964. return cnt;
  1965. }
  1966. static ssize_t
  1967. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1968. size_t cnt, loff_t *ppos)
  1969. {
  1970. char buf[max_tracer_type_len+2];
  1971. int r;
  1972. mutex_lock(&trace_types_lock);
  1973. if (current_trace)
  1974. r = sprintf(buf, "%s\n", current_trace->name);
  1975. else
  1976. r = sprintf(buf, "\n");
  1977. mutex_unlock(&trace_types_lock);
  1978. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1979. }
  1980. static int tracing_set_tracer(char *buf)
  1981. {
  1982. struct trace_array *tr = &global_trace;
  1983. struct tracer *t;
  1984. int ret = 0;
  1985. mutex_lock(&trace_types_lock);
  1986. for (t = trace_types; t; t = t->next) {
  1987. if (strcmp(t->name, buf) == 0)
  1988. break;
  1989. }
  1990. if (!t) {
  1991. ret = -EINVAL;
  1992. goto out;
  1993. }
  1994. if (t == current_trace)
  1995. goto out;
  1996. if (current_trace && current_trace->reset)
  1997. current_trace->reset(tr);
  1998. current_trace = t;
  1999. if (t->init)
  2000. t->init(tr);
  2001. out:
  2002. mutex_unlock(&trace_types_lock);
  2003. return ret;
  2004. }
  2005. static ssize_t
  2006. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2007. size_t cnt, loff_t *ppos)
  2008. {
  2009. char buf[max_tracer_type_len+1];
  2010. int i;
  2011. size_t ret;
  2012. if (cnt > max_tracer_type_len)
  2013. cnt = max_tracer_type_len;
  2014. if (copy_from_user(&buf, ubuf, cnt))
  2015. return -EFAULT;
  2016. buf[cnt] = 0;
  2017. /* strip ending whitespace. */
  2018. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2019. buf[i] = 0;
  2020. ret = tracing_set_tracer(buf);
  2021. if (!ret)
  2022. ret = cnt;
  2023. if (ret > 0)
  2024. filp->f_pos += ret;
  2025. return ret;
  2026. }
  2027. static ssize_t
  2028. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2029. size_t cnt, loff_t *ppos)
  2030. {
  2031. unsigned long *ptr = filp->private_data;
  2032. char buf[64];
  2033. int r;
  2034. r = snprintf(buf, sizeof(buf), "%ld\n",
  2035. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2036. if (r > sizeof(buf))
  2037. r = sizeof(buf);
  2038. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2039. }
  2040. static ssize_t
  2041. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2042. size_t cnt, loff_t *ppos)
  2043. {
  2044. long *ptr = filp->private_data;
  2045. char buf[64];
  2046. long val;
  2047. int ret;
  2048. if (cnt >= sizeof(buf))
  2049. return -EINVAL;
  2050. if (copy_from_user(&buf, ubuf, cnt))
  2051. return -EFAULT;
  2052. buf[cnt] = 0;
  2053. ret = strict_strtoul(buf, 10, &val);
  2054. if (ret < 0)
  2055. return ret;
  2056. *ptr = val * 1000;
  2057. return cnt;
  2058. }
  2059. static atomic_t tracing_reader;
  2060. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2061. {
  2062. struct trace_iterator *iter;
  2063. if (tracing_disabled)
  2064. return -ENODEV;
  2065. /* We only allow for reader of the pipe */
  2066. if (atomic_inc_return(&tracing_reader) != 1) {
  2067. atomic_dec(&tracing_reader);
  2068. return -EBUSY;
  2069. }
  2070. /* create a buffer to store the information to pass to userspace */
  2071. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2072. if (!iter)
  2073. return -ENOMEM;
  2074. mutex_lock(&trace_types_lock);
  2075. iter->tr = &global_trace;
  2076. iter->trace = current_trace;
  2077. filp->private_data = iter;
  2078. if (iter->trace->pipe_open)
  2079. iter->trace->pipe_open(iter);
  2080. mutex_unlock(&trace_types_lock);
  2081. return 0;
  2082. }
  2083. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2084. {
  2085. struct trace_iterator *iter = file->private_data;
  2086. kfree(iter);
  2087. atomic_dec(&tracing_reader);
  2088. return 0;
  2089. }
  2090. static unsigned int
  2091. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2092. {
  2093. struct trace_iterator *iter = filp->private_data;
  2094. if (trace_flags & TRACE_ITER_BLOCK) {
  2095. /*
  2096. * Always select as readable when in blocking mode
  2097. */
  2098. return POLLIN | POLLRDNORM;
  2099. } else {
  2100. if (!trace_empty(iter))
  2101. return POLLIN | POLLRDNORM;
  2102. poll_wait(filp, &trace_wait, poll_table);
  2103. if (!trace_empty(iter))
  2104. return POLLIN | POLLRDNORM;
  2105. return 0;
  2106. }
  2107. }
  2108. /*
  2109. * Consumer reader.
  2110. */
  2111. static ssize_t
  2112. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2113. size_t cnt, loff_t *ppos)
  2114. {
  2115. struct trace_iterator *iter = filp->private_data;
  2116. ssize_t sret;
  2117. /* return any leftover data */
  2118. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2119. if (sret != -EBUSY)
  2120. return sret;
  2121. trace_seq_reset(&iter->seq);
  2122. mutex_lock(&trace_types_lock);
  2123. if (iter->trace->read) {
  2124. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2125. if (sret)
  2126. goto out;
  2127. }
  2128. waitagain:
  2129. sret = 0;
  2130. while (trace_empty(iter)) {
  2131. if ((filp->f_flags & O_NONBLOCK)) {
  2132. sret = -EAGAIN;
  2133. goto out;
  2134. }
  2135. /*
  2136. * This is a make-shift waitqueue. The reason we don't use
  2137. * an actual wait queue is because:
  2138. * 1) we only ever have one waiter
  2139. * 2) the tracing, traces all functions, we don't want
  2140. * the overhead of calling wake_up and friends
  2141. * (and tracing them too)
  2142. * Anyway, this is really very primitive wakeup.
  2143. */
  2144. set_current_state(TASK_INTERRUPTIBLE);
  2145. iter->tr->waiter = current;
  2146. mutex_unlock(&trace_types_lock);
  2147. /* sleep for 100 msecs, and try again. */
  2148. schedule_timeout(HZ/10);
  2149. mutex_lock(&trace_types_lock);
  2150. iter->tr->waiter = NULL;
  2151. if (signal_pending(current)) {
  2152. sret = -EINTR;
  2153. goto out;
  2154. }
  2155. if (iter->trace != current_trace)
  2156. goto out;
  2157. /*
  2158. * We block until we read something and tracing is disabled.
  2159. * We still block if tracing is disabled, but we have never
  2160. * read anything. This allows a user to cat this file, and
  2161. * then enable tracing. But after we have read something,
  2162. * we give an EOF when tracing is again disabled.
  2163. *
  2164. * iter->pos will be 0 if we haven't read anything.
  2165. */
  2166. if (!tracer_enabled && iter->pos)
  2167. break;
  2168. continue;
  2169. }
  2170. /* stop when tracing is finished */
  2171. if (trace_empty(iter))
  2172. goto out;
  2173. if (cnt >= PAGE_SIZE)
  2174. cnt = PAGE_SIZE - 1;
  2175. /* reset all but tr, trace, and overruns */
  2176. memset(&iter->seq, 0,
  2177. sizeof(struct trace_iterator) -
  2178. offsetof(struct trace_iterator, seq));
  2179. iter->pos = -1;
  2180. while (find_next_entry_inc(iter) != NULL) {
  2181. enum print_line_t ret;
  2182. int len = iter->seq.len;
  2183. ret = print_trace_line(iter);
  2184. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2185. /* don't print partial lines */
  2186. iter->seq.len = len;
  2187. break;
  2188. }
  2189. trace_consume(iter);
  2190. if (iter->seq.len >= cnt)
  2191. break;
  2192. }
  2193. /* Now copy what we have to the user */
  2194. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2195. if (iter->seq.readpos >= iter->seq.len)
  2196. trace_seq_reset(&iter->seq);
  2197. /*
  2198. * If there was nothing to send to user, inspite of consuming trace
  2199. * entries, go back to wait for more entries.
  2200. */
  2201. if (sret == -EBUSY)
  2202. goto waitagain;
  2203. out:
  2204. mutex_unlock(&trace_types_lock);
  2205. return sret;
  2206. }
  2207. static ssize_t
  2208. tracing_entries_read(struct file *filp, char __user *ubuf,
  2209. size_t cnt, loff_t *ppos)
  2210. {
  2211. struct trace_array *tr = filp->private_data;
  2212. char buf[64];
  2213. int r;
  2214. r = sprintf(buf, "%lu\n", tr->entries);
  2215. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2216. }
  2217. static ssize_t
  2218. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2219. size_t cnt, loff_t *ppos)
  2220. {
  2221. unsigned long val;
  2222. char buf[64];
  2223. int ret;
  2224. struct trace_array *tr = filp->private_data;
  2225. if (cnt >= sizeof(buf))
  2226. return -EINVAL;
  2227. if (copy_from_user(&buf, ubuf, cnt))
  2228. return -EFAULT;
  2229. buf[cnt] = 0;
  2230. ret = strict_strtoul(buf, 10, &val);
  2231. if (ret < 0)
  2232. return ret;
  2233. /* must have at least 1 entry */
  2234. if (!val)
  2235. return -EINVAL;
  2236. mutex_lock(&trace_types_lock);
  2237. if (tr->ctrl) {
  2238. cnt = -EBUSY;
  2239. pr_info("ftrace: please disable tracing"
  2240. " before modifying buffer size\n");
  2241. goto out;
  2242. }
  2243. if (val != global_trace.entries) {
  2244. ret = ring_buffer_resize(global_trace.buffer, val);
  2245. if (ret < 0) {
  2246. cnt = ret;
  2247. goto out;
  2248. }
  2249. ret = ring_buffer_resize(max_tr.buffer, val);
  2250. if (ret < 0) {
  2251. int r;
  2252. cnt = ret;
  2253. r = ring_buffer_resize(global_trace.buffer,
  2254. global_trace.entries);
  2255. if (r < 0) {
  2256. /* AARGH! We are left with different
  2257. * size max buffer!!!! */
  2258. WARN_ON(1);
  2259. tracing_disabled = 1;
  2260. }
  2261. goto out;
  2262. }
  2263. global_trace.entries = val;
  2264. }
  2265. filp->f_pos += cnt;
  2266. /* If check pages failed, return ENOMEM */
  2267. if (tracing_disabled)
  2268. cnt = -ENOMEM;
  2269. out:
  2270. max_tr.entries = global_trace.entries;
  2271. mutex_unlock(&trace_types_lock);
  2272. return cnt;
  2273. }
  2274. static int mark_printk(const char *fmt, ...)
  2275. {
  2276. int ret;
  2277. va_list args;
  2278. va_start(args, fmt);
  2279. ret = trace_vprintk(0, fmt, args);
  2280. va_end(args);
  2281. return ret;
  2282. }
  2283. static ssize_t
  2284. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2285. size_t cnt, loff_t *fpos)
  2286. {
  2287. char *buf;
  2288. char *end;
  2289. struct trace_array *tr = &global_trace;
  2290. if (!tr->ctrl || tracing_disabled)
  2291. return -EINVAL;
  2292. if (cnt > TRACE_BUF_SIZE)
  2293. cnt = TRACE_BUF_SIZE;
  2294. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2295. if (buf == NULL)
  2296. return -ENOMEM;
  2297. if (copy_from_user(buf, ubuf, cnt)) {
  2298. kfree(buf);
  2299. return -EFAULT;
  2300. }
  2301. /* Cut from the first nil or newline. */
  2302. buf[cnt] = '\0';
  2303. end = strchr(buf, '\n');
  2304. if (end)
  2305. *end = '\0';
  2306. cnt = mark_printk("%s\n", buf);
  2307. kfree(buf);
  2308. *fpos += cnt;
  2309. return cnt;
  2310. }
  2311. static struct file_operations tracing_max_lat_fops = {
  2312. .open = tracing_open_generic,
  2313. .read = tracing_max_lat_read,
  2314. .write = tracing_max_lat_write,
  2315. };
  2316. static struct file_operations tracing_ctrl_fops = {
  2317. .open = tracing_open_generic,
  2318. .read = tracing_ctrl_read,
  2319. .write = tracing_ctrl_write,
  2320. };
  2321. static struct file_operations set_tracer_fops = {
  2322. .open = tracing_open_generic,
  2323. .read = tracing_set_trace_read,
  2324. .write = tracing_set_trace_write,
  2325. };
  2326. static struct file_operations tracing_pipe_fops = {
  2327. .open = tracing_open_pipe,
  2328. .poll = tracing_poll_pipe,
  2329. .read = tracing_read_pipe,
  2330. .release = tracing_release_pipe,
  2331. };
  2332. static struct file_operations tracing_entries_fops = {
  2333. .open = tracing_open_generic,
  2334. .read = tracing_entries_read,
  2335. .write = tracing_entries_write,
  2336. };
  2337. static struct file_operations tracing_mark_fops = {
  2338. .open = tracing_open_generic,
  2339. .write = tracing_mark_write,
  2340. };
  2341. #ifdef CONFIG_DYNAMIC_FTRACE
  2342. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2343. {
  2344. return 0;
  2345. }
  2346. static ssize_t
  2347. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2348. size_t cnt, loff_t *ppos)
  2349. {
  2350. static char ftrace_dyn_info_buffer[1024];
  2351. static DEFINE_MUTEX(dyn_info_mutex);
  2352. unsigned long *p = filp->private_data;
  2353. char *buf = ftrace_dyn_info_buffer;
  2354. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2355. int r;
  2356. mutex_lock(&dyn_info_mutex);
  2357. r = sprintf(buf, "%ld ", *p);
  2358. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2359. buf[r++] = '\n';
  2360. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2361. mutex_unlock(&dyn_info_mutex);
  2362. return r;
  2363. }
  2364. static struct file_operations tracing_dyn_info_fops = {
  2365. .open = tracing_open_generic,
  2366. .read = tracing_read_dyn_info,
  2367. };
  2368. #endif
  2369. static struct dentry *d_tracer;
  2370. struct dentry *tracing_init_dentry(void)
  2371. {
  2372. static int once;
  2373. if (d_tracer)
  2374. return d_tracer;
  2375. d_tracer = debugfs_create_dir("tracing", NULL);
  2376. if (!d_tracer && !once) {
  2377. once = 1;
  2378. pr_warning("Could not create debugfs directory 'tracing'\n");
  2379. return NULL;
  2380. }
  2381. return d_tracer;
  2382. }
  2383. #ifdef CONFIG_FTRACE_SELFTEST
  2384. /* Let selftest have access to static functions in this file */
  2385. #include "trace_selftest.c"
  2386. #endif
  2387. static __init int tracer_init_debugfs(void)
  2388. {
  2389. struct dentry *d_tracer;
  2390. struct dentry *entry;
  2391. d_tracer = tracing_init_dentry();
  2392. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2393. &global_trace, &tracing_ctrl_fops);
  2394. if (!entry)
  2395. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2396. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2397. NULL, &tracing_iter_fops);
  2398. if (!entry)
  2399. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2400. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2401. NULL, &tracing_cpumask_fops);
  2402. if (!entry)
  2403. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2404. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2405. &global_trace, &tracing_lt_fops);
  2406. if (!entry)
  2407. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2408. entry = debugfs_create_file("trace", 0444, d_tracer,
  2409. &global_trace, &tracing_fops);
  2410. if (!entry)
  2411. pr_warning("Could not create debugfs 'trace' entry\n");
  2412. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2413. &global_trace, &show_traces_fops);
  2414. if (!entry)
  2415. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2416. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2417. &global_trace, &set_tracer_fops);
  2418. if (!entry)
  2419. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2420. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2421. &tracing_max_latency,
  2422. &tracing_max_lat_fops);
  2423. if (!entry)
  2424. pr_warning("Could not create debugfs "
  2425. "'tracing_max_latency' entry\n");
  2426. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2427. &tracing_thresh, &tracing_max_lat_fops);
  2428. if (!entry)
  2429. pr_warning("Could not create debugfs "
  2430. "'tracing_thresh' entry\n");
  2431. entry = debugfs_create_file("README", 0644, d_tracer,
  2432. NULL, &tracing_readme_fops);
  2433. if (!entry)
  2434. pr_warning("Could not create debugfs 'README' entry\n");
  2435. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2436. NULL, &tracing_pipe_fops);
  2437. if (!entry)
  2438. pr_warning("Could not create debugfs "
  2439. "'trace_pipe' entry\n");
  2440. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2441. &global_trace, &tracing_entries_fops);
  2442. if (!entry)
  2443. pr_warning("Could not create debugfs "
  2444. "'trace_entries' entry\n");
  2445. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  2446. NULL, &tracing_mark_fops);
  2447. if (!entry)
  2448. pr_warning("Could not create debugfs "
  2449. "'trace_marker' entry\n");
  2450. #ifdef CONFIG_DYNAMIC_FTRACE
  2451. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2452. &ftrace_update_tot_cnt,
  2453. &tracing_dyn_info_fops);
  2454. if (!entry)
  2455. pr_warning("Could not create debugfs "
  2456. "'dyn_ftrace_total_info' entry\n");
  2457. #endif
  2458. #ifdef CONFIG_SYSPROF_TRACER
  2459. init_tracer_sysprof_debugfs(d_tracer);
  2460. #endif
  2461. return 0;
  2462. }
  2463. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2464. {
  2465. static DEFINE_SPINLOCK(trace_buf_lock);
  2466. static char trace_buf[TRACE_BUF_SIZE];
  2467. struct ring_buffer_event *event;
  2468. struct trace_array *tr = &global_trace;
  2469. struct trace_array_cpu *data;
  2470. struct print_entry *entry;
  2471. unsigned long flags, irq_flags;
  2472. int cpu, len = 0, size, pc;
  2473. if (!tr->ctrl || tracing_disabled)
  2474. return 0;
  2475. pc = preempt_count();
  2476. preempt_disable_notrace();
  2477. cpu = raw_smp_processor_id();
  2478. data = tr->data[cpu];
  2479. if (unlikely(atomic_read(&data->disabled)))
  2480. goto out;
  2481. spin_lock_irqsave(&trace_buf_lock, flags);
  2482. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  2483. len = min(len, TRACE_BUF_SIZE-1);
  2484. trace_buf[len] = 0;
  2485. size = sizeof(*entry) + len + 1;
  2486. event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
  2487. if (!event)
  2488. goto out_unlock;
  2489. entry = ring_buffer_event_data(event);
  2490. tracing_generic_entry_update(&entry->ent, flags, pc);
  2491. entry->ent.type = TRACE_PRINT;
  2492. entry->ip = ip;
  2493. memcpy(&entry->buf, trace_buf, len);
  2494. entry->buf[len] = 0;
  2495. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  2496. out_unlock:
  2497. spin_unlock_irqrestore(&trace_buf_lock, flags);
  2498. out:
  2499. preempt_enable_notrace();
  2500. return len;
  2501. }
  2502. EXPORT_SYMBOL_GPL(trace_vprintk);
  2503. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2504. {
  2505. int ret;
  2506. va_list ap;
  2507. if (!(trace_flags & TRACE_ITER_PRINTK))
  2508. return 0;
  2509. va_start(ap, fmt);
  2510. ret = trace_vprintk(ip, fmt, ap);
  2511. va_end(ap);
  2512. return ret;
  2513. }
  2514. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2515. static int trace_panic_handler(struct notifier_block *this,
  2516. unsigned long event, void *unused)
  2517. {
  2518. if (ftrace_dump_on_oops)
  2519. ftrace_dump();
  2520. return NOTIFY_OK;
  2521. }
  2522. static struct notifier_block trace_panic_notifier = {
  2523. .notifier_call = trace_panic_handler,
  2524. .next = NULL,
  2525. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2526. };
  2527. static int trace_die_handler(struct notifier_block *self,
  2528. unsigned long val,
  2529. void *data)
  2530. {
  2531. switch (val) {
  2532. case DIE_OOPS:
  2533. if (ftrace_dump_on_oops)
  2534. ftrace_dump();
  2535. break;
  2536. default:
  2537. break;
  2538. }
  2539. return NOTIFY_OK;
  2540. }
  2541. static struct notifier_block trace_die_notifier = {
  2542. .notifier_call = trace_die_handler,
  2543. .priority = 200
  2544. };
  2545. /*
  2546. * printk is set to max of 1024, we really don't need it that big.
  2547. * Nothing should be printing 1000 characters anyway.
  2548. */
  2549. #define TRACE_MAX_PRINT 1000
  2550. /*
  2551. * Define here KERN_TRACE so that we have one place to modify
  2552. * it if we decide to change what log level the ftrace dump
  2553. * should be at.
  2554. */
  2555. #define KERN_TRACE KERN_INFO
  2556. static void
  2557. trace_printk_seq(struct trace_seq *s)
  2558. {
  2559. /* Probably should print a warning here. */
  2560. if (s->len >= 1000)
  2561. s->len = 1000;
  2562. /* should be zero ended, but we are paranoid. */
  2563. s->buffer[s->len] = 0;
  2564. printk(KERN_TRACE "%s", s->buffer);
  2565. trace_seq_reset(s);
  2566. }
  2567. void ftrace_dump(void)
  2568. {
  2569. static DEFINE_SPINLOCK(ftrace_dump_lock);
  2570. /* use static because iter can be a bit big for the stack */
  2571. static struct trace_iterator iter;
  2572. static cpumask_t mask;
  2573. static int dump_ran;
  2574. unsigned long flags;
  2575. int cnt = 0, cpu;
  2576. /* only one dump */
  2577. spin_lock_irqsave(&ftrace_dump_lock, flags);
  2578. if (dump_ran)
  2579. goto out;
  2580. dump_ran = 1;
  2581. /* No turning back! */
  2582. ftrace_kill();
  2583. for_each_tracing_cpu(cpu) {
  2584. atomic_inc(&global_trace.data[cpu]->disabled);
  2585. }
  2586. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  2587. iter.tr = &global_trace;
  2588. iter.trace = current_trace;
  2589. /*
  2590. * We need to stop all tracing on all CPUS to read the
  2591. * the next buffer. This is a bit expensive, but is
  2592. * not done often. We fill all what we can read,
  2593. * and then release the locks again.
  2594. */
  2595. cpus_clear(mask);
  2596. while (!trace_empty(&iter)) {
  2597. if (!cnt)
  2598. printk(KERN_TRACE "---------------------------------\n");
  2599. cnt++;
  2600. /* reset all but tr, trace, and overruns */
  2601. memset(&iter.seq, 0,
  2602. sizeof(struct trace_iterator) -
  2603. offsetof(struct trace_iterator, seq));
  2604. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  2605. iter.pos = -1;
  2606. if (find_next_entry_inc(&iter) != NULL) {
  2607. print_trace_line(&iter);
  2608. trace_consume(&iter);
  2609. }
  2610. trace_printk_seq(&iter.seq);
  2611. }
  2612. if (!cnt)
  2613. printk(KERN_TRACE " (ftrace buffer empty)\n");
  2614. else
  2615. printk(KERN_TRACE "---------------------------------\n");
  2616. out:
  2617. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  2618. }
  2619. __init static int tracer_alloc_buffers(void)
  2620. {
  2621. struct trace_array_cpu *data;
  2622. int i;
  2623. /* TODO: make the number of buffers hot pluggable with CPUS */
  2624. tracing_buffer_mask = cpu_possible_map;
  2625. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  2626. TRACE_BUFFER_FLAGS);
  2627. if (!global_trace.buffer) {
  2628. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  2629. WARN_ON(1);
  2630. return 0;
  2631. }
  2632. global_trace.entries = ring_buffer_size(global_trace.buffer);
  2633. #ifdef CONFIG_TRACER_MAX_TRACE
  2634. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  2635. TRACE_BUFFER_FLAGS);
  2636. if (!max_tr.buffer) {
  2637. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  2638. WARN_ON(1);
  2639. ring_buffer_free(global_trace.buffer);
  2640. return 0;
  2641. }
  2642. max_tr.entries = ring_buffer_size(max_tr.buffer);
  2643. WARN_ON(max_tr.entries != global_trace.entries);
  2644. #endif
  2645. /* Allocate the first page for all buffers */
  2646. for_each_tracing_cpu(i) {
  2647. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2648. max_tr.data[i] = &per_cpu(max_data, i);
  2649. }
  2650. trace_init_cmdlines();
  2651. register_tracer(&nop_trace);
  2652. #ifdef CONFIG_BOOT_TRACER
  2653. register_tracer(&boot_tracer);
  2654. current_trace = &boot_tracer;
  2655. current_trace->init(&global_trace);
  2656. #else
  2657. current_trace = &nop_trace;
  2658. #endif
  2659. /* All seems OK, enable tracing */
  2660. global_trace.ctrl = tracer_enabled;
  2661. tracing_disabled = 0;
  2662. atomic_notifier_chain_register(&panic_notifier_list,
  2663. &trace_panic_notifier);
  2664. register_die_notifier(&trace_die_notifier);
  2665. return 0;
  2666. }
  2667. early_initcall(tracer_alloc_buffers);
  2668. fs_initcall(tracer_init_debugfs);