trace.c 72 KB

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