trace.c 70 KB

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