trace.c 78 KB

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