trace.c 72 KB

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