trace.c 71 KB

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