trace.c 71 KB

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