trace.c 69 KB

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