trace.c 73 KB

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