trace.c 70 KB

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