trace.c 73 KB

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