trace.c 83 KB

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