trace.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761
  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;
  1215. down_read(&mm->mmap_sem);
  1216. vma = find_vma(mm, ip);
  1217. if (vma) {
  1218. file = vma->vm_file;
  1219. vmstart = vma->vm_start;
  1220. }
  1221. up_read(&mm->mmap_sem);
  1222. }
  1223. if (file) {
  1224. ret = trace_seq_path(s, &file->f_path);
  1225. if (ret)
  1226. ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart);
  1227. }
  1228. if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
  1229. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1230. return ret;
  1231. }
  1232. static int
  1233. seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
  1234. unsigned long sym_flags)
  1235. {
  1236. struct mm_struct *mm = NULL;
  1237. int ret = 1;
  1238. unsigned int i;
  1239. if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
  1240. struct task_struct *task;
  1241. /*
  1242. * we do the lookup on the thread group leader,
  1243. * since individual threads might have already quit!
  1244. */
  1245. rcu_read_lock();
  1246. task = find_task_by_vpid(entry->ent.tgid);
  1247. if (task)
  1248. mm = get_task_mm(task);
  1249. rcu_read_unlock();
  1250. }
  1251. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1252. unsigned long ip = entry->caller[i];
  1253. if (ip == ULONG_MAX || !ret)
  1254. break;
  1255. if (i && ret)
  1256. ret = trace_seq_puts(s, " <- ");
  1257. if (!ip) {
  1258. if (ret)
  1259. ret = trace_seq_puts(s, "??");
  1260. continue;
  1261. }
  1262. if (!ret)
  1263. break;
  1264. if (ret)
  1265. ret = seq_print_user_ip(s, mm, ip, sym_flags);
  1266. }
  1267. if (mm)
  1268. mmput(mm);
  1269. return ret;
  1270. }
  1271. static void print_lat_help_header(struct seq_file *m)
  1272. {
  1273. seq_puts(m, "# _------=> CPU# \n");
  1274. seq_puts(m, "# / _-----=> irqs-off \n");
  1275. seq_puts(m, "# | / _----=> need-resched \n");
  1276. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1277. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1278. seq_puts(m, "# |||| / \n");
  1279. seq_puts(m, "# ||||| delay \n");
  1280. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1281. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1282. }
  1283. static void print_func_help_header(struct seq_file *m)
  1284. {
  1285. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1286. seq_puts(m, "# | | | | |\n");
  1287. }
  1288. static void
  1289. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1290. {
  1291. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1292. struct trace_array *tr = iter->tr;
  1293. struct trace_array_cpu *data = tr->data[tr->cpu];
  1294. struct tracer *type = current_trace;
  1295. unsigned long total;
  1296. unsigned long entries;
  1297. const char *name = "preemption";
  1298. if (type)
  1299. name = type->name;
  1300. entries = ring_buffer_entries(iter->tr->buffer);
  1301. total = entries +
  1302. ring_buffer_overruns(iter->tr->buffer);
  1303. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1304. name, UTS_RELEASE);
  1305. seq_puts(m, "-----------------------------------"
  1306. "---------------------------------\n");
  1307. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1308. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1309. nsecs_to_usecs(data->saved_latency),
  1310. entries,
  1311. total,
  1312. tr->cpu,
  1313. #if defined(CONFIG_PREEMPT_NONE)
  1314. "server",
  1315. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1316. "desktop",
  1317. #elif defined(CONFIG_PREEMPT)
  1318. "preempt",
  1319. #else
  1320. "unknown",
  1321. #endif
  1322. /* These are reserved for later use */
  1323. 0, 0, 0, 0);
  1324. #ifdef CONFIG_SMP
  1325. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1326. #else
  1327. seq_puts(m, ")\n");
  1328. #endif
  1329. seq_puts(m, " -----------------\n");
  1330. seq_printf(m, " | task: %.16s-%d "
  1331. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1332. data->comm, data->pid, data->uid, data->nice,
  1333. data->policy, data->rt_priority);
  1334. seq_puts(m, " -----------------\n");
  1335. if (data->critical_start) {
  1336. seq_puts(m, " => started at: ");
  1337. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1338. trace_print_seq(m, &iter->seq);
  1339. seq_puts(m, "\n => ended at: ");
  1340. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1341. trace_print_seq(m, &iter->seq);
  1342. seq_puts(m, "\n");
  1343. }
  1344. seq_puts(m, "\n");
  1345. }
  1346. static void
  1347. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1348. {
  1349. int hardirq, softirq;
  1350. char *comm;
  1351. comm = trace_find_cmdline(entry->pid);
  1352. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1353. trace_seq_printf(s, "%3d", cpu);
  1354. trace_seq_printf(s, "%c%c",
  1355. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  1356. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
  1357. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1358. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1359. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1360. if (hardirq && softirq) {
  1361. trace_seq_putc(s, 'H');
  1362. } else {
  1363. if (hardirq) {
  1364. trace_seq_putc(s, 'h');
  1365. } else {
  1366. if (softirq)
  1367. trace_seq_putc(s, 's');
  1368. else
  1369. trace_seq_putc(s, '.');
  1370. }
  1371. }
  1372. if (entry->preempt_count)
  1373. trace_seq_printf(s, "%x", entry->preempt_count);
  1374. else
  1375. trace_seq_puts(s, ".");
  1376. }
  1377. unsigned long preempt_mark_thresh = 100;
  1378. static void
  1379. lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
  1380. unsigned long rel_usecs)
  1381. {
  1382. trace_seq_printf(s, " %4lldus", abs_usecs);
  1383. if (rel_usecs > preempt_mark_thresh)
  1384. trace_seq_puts(s, "!: ");
  1385. else if (rel_usecs > 1)
  1386. trace_seq_puts(s, "+: ");
  1387. else
  1388. trace_seq_puts(s, " : ");
  1389. }
  1390. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1391. /*
  1392. * The message is supposed to contain an ending newline.
  1393. * If the printing stops prematurely, try to add a newline of our own.
  1394. */
  1395. void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
  1396. {
  1397. struct trace_entry *ent;
  1398. struct trace_field_cont *cont;
  1399. bool ok = true;
  1400. ent = peek_next_entry(iter, iter->cpu, NULL);
  1401. if (!ent || ent->type != TRACE_CONT) {
  1402. trace_seq_putc(s, '\n');
  1403. return;
  1404. }
  1405. do {
  1406. cont = (struct trace_field_cont *)ent;
  1407. if (ok)
  1408. ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
  1409. ftrace_disable_cpu();
  1410. if (iter->buffer_iter[iter->cpu])
  1411. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1412. else
  1413. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  1414. ftrace_enable_cpu();
  1415. ent = peek_next_entry(iter, iter->cpu, NULL);
  1416. } while (ent && ent->type == TRACE_CONT);
  1417. if (!ok)
  1418. trace_seq_putc(s, '\n');
  1419. }
  1420. static void test_cpu_buff_start(struct trace_iterator *iter)
  1421. {
  1422. struct trace_seq *s = &iter->seq;
  1423. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1424. return;
  1425. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1426. return;
  1427. if (cpu_isset(iter->cpu, iter->started))
  1428. return;
  1429. cpu_set(iter->cpu, iter->started);
  1430. trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
  1431. }
  1432. static enum print_line_t
  1433. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1434. {
  1435. struct trace_seq *s = &iter->seq;
  1436. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1437. struct trace_entry *next_entry;
  1438. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1439. struct trace_entry *entry = iter->ent;
  1440. unsigned long abs_usecs;
  1441. unsigned long rel_usecs;
  1442. u64 next_ts;
  1443. char *comm;
  1444. int S, T;
  1445. int i;
  1446. unsigned state;
  1447. if (entry->type == TRACE_CONT)
  1448. return TRACE_TYPE_HANDLED;
  1449. test_cpu_buff_start(iter);
  1450. next_entry = find_next_entry(iter, NULL, &next_ts);
  1451. if (!next_entry)
  1452. next_ts = iter->ts;
  1453. rel_usecs = ns2usecs(next_ts - iter->ts);
  1454. abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
  1455. if (verbose) {
  1456. comm = trace_find_cmdline(entry->pid);
  1457. trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
  1458. " %ld.%03ldms (+%ld.%03ldms): ",
  1459. comm,
  1460. entry->pid, cpu, entry->flags,
  1461. entry->preempt_count, trace_idx,
  1462. ns2usecs(iter->ts),
  1463. abs_usecs/1000,
  1464. abs_usecs % 1000, rel_usecs/1000,
  1465. rel_usecs % 1000);
  1466. } else {
  1467. lat_print_generic(s, entry, cpu);
  1468. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1469. }
  1470. switch (entry->type) {
  1471. case TRACE_FN: {
  1472. struct ftrace_entry *field;
  1473. trace_assign_type(field, entry);
  1474. seq_print_ip_sym(s, field->ip, sym_flags);
  1475. trace_seq_puts(s, " (");
  1476. seq_print_ip_sym(s, field->parent_ip, sym_flags);
  1477. trace_seq_puts(s, ")\n");
  1478. break;
  1479. }
  1480. case TRACE_CTX:
  1481. case TRACE_WAKE: {
  1482. struct ctx_switch_entry *field;
  1483. trace_assign_type(field, entry);
  1484. T = field->next_state < sizeof(state_to_char) ?
  1485. state_to_char[field->next_state] : 'X';
  1486. state = field->prev_state ?
  1487. __ffs(field->prev_state) + 1 : 0;
  1488. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1489. comm = trace_find_cmdline(field->next_pid);
  1490. trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
  1491. field->prev_pid,
  1492. field->prev_prio,
  1493. S, entry->type == TRACE_CTX ? "==>" : " +",
  1494. field->next_cpu,
  1495. field->next_pid,
  1496. field->next_prio,
  1497. T, comm);
  1498. break;
  1499. }
  1500. case TRACE_SPECIAL: {
  1501. struct special_entry *field;
  1502. trace_assign_type(field, entry);
  1503. trace_seq_printf(s, "# %ld %ld %ld\n",
  1504. field->arg1,
  1505. field->arg2,
  1506. field->arg3);
  1507. break;
  1508. }
  1509. case TRACE_STACK: {
  1510. struct stack_entry *field;
  1511. trace_assign_type(field, entry);
  1512. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1513. if (i)
  1514. trace_seq_puts(s, " <= ");
  1515. seq_print_ip_sym(s, field->caller[i], sym_flags);
  1516. }
  1517. trace_seq_puts(s, "\n");
  1518. break;
  1519. }
  1520. case TRACE_PRINT: {
  1521. struct print_entry *field;
  1522. trace_assign_type(field, entry);
  1523. seq_print_ip_sym(s, field->ip, sym_flags);
  1524. trace_seq_printf(s, ": %s", field->buf);
  1525. if (entry->flags & TRACE_FLAG_CONT)
  1526. trace_seq_print_cont(s, iter);
  1527. break;
  1528. }
  1529. case TRACE_BRANCH: {
  1530. struct trace_branch *field;
  1531. trace_assign_type(field, entry);
  1532. trace_seq_printf(s, "[%s] %s:%s:%d\n",
  1533. field->correct ? " ok " : " MISS ",
  1534. field->func,
  1535. field->file,
  1536. field->line);
  1537. break;
  1538. }
  1539. case TRACE_USER_STACK: {
  1540. struct userstack_entry *field;
  1541. trace_assign_type(field, entry);
  1542. seq_print_userip_objs(field, s, sym_flags);
  1543. trace_seq_putc(s, '\n');
  1544. break;
  1545. }
  1546. default:
  1547. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1548. }
  1549. return TRACE_TYPE_HANDLED;
  1550. }
  1551. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1552. {
  1553. struct trace_seq *s = &iter->seq;
  1554. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1555. struct trace_entry *entry;
  1556. unsigned long usec_rem;
  1557. unsigned long long t;
  1558. unsigned long secs;
  1559. char *comm;
  1560. int ret;
  1561. int S, T;
  1562. int i;
  1563. entry = iter->ent;
  1564. if (entry->type == TRACE_CONT)
  1565. return TRACE_TYPE_HANDLED;
  1566. test_cpu_buff_start(iter);
  1567. comm = trace_find_cmdline(iter->ent->pid);
  1568. t = ns2usecs(iter->ts);
  1569. usec_rem = do_div(t, 1000000ULL);
  1570. secs = (unsigned long)t;
  1571. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1572. if (!ret)
  1573. return TRACE_TYPE_PARTIAL_LINE;
  1574. ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
  1575. if (!ret)
  1576. return TRACE_TYPE_PARTIAL_LINE;
  1577. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1578. if (!ret)
  1579. return TRACE_TYPE_PARTIAL_LINE;
  1580. switch (entry->type) {
  1581. case TRACE_FN: {
  1582. struct ftrace_entry *field;
  1583. trace_assign_type(field, entry);
  1584. ret = seq_print_ip_sym(s, field->ip, sym_flags);
  1585. if (!ret)
  1586. return TRACE_TYPE_PARTIAL_LINE;
  1587. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1588. field->parent_ip) {
  1589. ret = trace_seq_printf(s, " <-");
  1590. if (!ret)
  1591. return TRACE_TYPE_PARTIAL_LINE;
  1592. ret = seq_print_ip_sym(s,
  1593. field->parent_ip,
  1594. sym_flags);
  1595. if (!ret)
  1596. return TRACE_TYPE_PARTIAL_LINE;
  1597. }
  1598. ret = trace_seq_printf(s, "\n");
  1599. if (!ret)
  1600. return TRACE_TYPE_PARTIAL_LINE;
  1601. break;
  1602. }
  1603. case TRACE_CTX:
  1604. case TRACE_WAKE: {
  1605. struct ctx_switch_entry *field;
  1606. trace_assign_type(field, entry);
  1607. S = field->prev_state < sizeof(state_to_char) ?
  1608. state_to_char[field->prev_state] : 'X';
  1609. T = field->next_state < sizeof(state_to_char) ?
  1610. state_to_char[field->next_state] : 'X';
  1611. ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
  1612. field->prev_pid,
  1613. field->prev_prio,
  1614. S,
  1615. entry->type == TRACE_CTX ? "==>" : " +",
  1616. field->next_cpu,
  1617. field->next_pid,
  1618. field->next_prio,
  1619. T);
  1620. if (!ret)
  1621. return TRACE_TYPE_PARTIAL_LINE;
  1622. break;
  1623. }
  1624. case TRACE_SPECIAL: {
  1625. struct special_entry *field;
  1626. trace_assign_type(field, entry);
  1627. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1628. field->arg1,
  1629. field->arg2,
  1630. field->arg3);
  1631. if (!ret)
  1632. return TRACE_TYPE_PARTIAL_LINE;
  1633. break;
  1634. }
  1635. case TRACE_STACK: {
  1636. struct stack_entry *field;
  1637. trace_assign_type(field, entry);
  1638. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1639. if (i) {
  1640. ret = trace_seq_puts(s, " <= ");
  1641. if (!ret)
  1642. return TRACE_TYPE_PARTIAL_LINE;
  1643. }
  1644. ret = seq_print_ip_sym(s, field->caller[i],
  1645. sym_flags);
  1646. if (!ret)
  1647. return TRACE_TYPE_PARTIAL_LINE;
  1648. }
  1649. ret = trace_seq_puts(s, "\n");
  1650. if (!ret)
  1651. return TRACE_TYPE_PARTIAL_LINE;
  1652. break;
  1653. }
  1654. case TRACE_PRINT: {
  1655. struct print_entry *field;
  1656. trace_assign_type(field, entry);
  1657. seq_print_ip_sym(s, field->ip, sym_flags);
  1658. trace_seq_printf(s, ": %s", field->buf);
  1659. if (entry->flags & TRACE_FLAG_CONT)
  1660. trace_seq_print_cont(s, iter);
  1661. break;
  1662. }
  1663. case TRACE_FN_RET: {
  1664. return print_return_function(iter);
  1665. break;
  1666. }
  1667. case TRACE_BRANCH: {
  1668. struct trace_branch *field;
  1669. trace_assign_type(field, entry);
  1670. trace_seq_printf(s, "[%s] %s:%s:%d\n",
  1671. field->correct ? " ok " : " MISS ",
  1672. field->func,
  1673. field->file,
  1674. field->line);
  1675. break;
  1676. }
  1677. case TRACE_USER_STACK: {
  1678. struct userstack_entry *field;
  1679. trace_assign_type(field, entry);
  1680. ret = seq_print_userip_objs(field, s, sym_flags);
  1681. if (!ret)
  1682. return TRACE_TYPE_PARTIAL_LINE;
  1683. ret = trace_seq_putc(s, '\n');
  1684. if (!ret)
  1685. return TRACE_TYPE_PARTIAL_LINE;
  1686. break;
  1687. }
  1688. }
  1689. return TRACE_TYPE_HANDLED;
  1690. }
  1691. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1692. {
  1693. struct trace_seq *s = &iter->seq;
  1694. struct trace_entry *entry;
  1695. int ret;
  1696. int S, T;
  1697. entry = iter->ent;
  1698. if (entry->type == TRACE_CONT)
  1699. return TRACE_TYPE_HANDLED;
  1700. ret = trace_seq_printf(s, "%d %d %llu ",
  1701. entry->pid, iter->cpu, iter->ts);
  1702. if (!ret)
  1703. return TRACE_TYPE_PARTIAL_LINE;
  1704. switch (entry->type) {
  1705. case TRACE_FN: {
  1706. struct ftrace_entry *field;
  1707. trace_assign_type(field, entry);
  1708. ret = trace_seq_printf(s, "%x %x\n",
  1709. field->ip,
  1710. field->parent_ip);
  1711. if (!ret)
  1712. return TRACE_TYPE_PARTIAL_LINE;
  1713. break;
  1714. }
  1715. case TRACE_CTX:
  1716. case TRACE_WAKE: {
  1717. struct ctx_switch_entry *field;
  1718. trace_assign_type(field, entry);
  1719. S = field->prev_state < sizeof(state_to_char) ?
  1720. state_to_char[field->prev_state] : 'X';
  1721. T = field->next_state < sizeof(state_to_char) ?
  1722. state_to_char[field->next_state] : 'X';
  1723. if (entry->type == TRACE_WAKE)
  1724. S = '+';
  1725. ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
  1726. field->prev_pid,
  1727. field->prev_prio,
  1728. S,
  1729. field->next_cpu,
  1730. field->next_pid,
  1731. field->next_prio,
  1732. T);
  1733. if (!ret)
  1734. return TRACE_TYPE_PARTIAL_LINE;
  1735. break;
  1736. }
  1737. case TRACE_SPECIAL:
  1738. case TRACE_USER_STACK:
  1739. case TRACE_STACK: {
  1740. struct special_entry *field;
  1741. trace_assign_type(field, entry);
  1742. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1743. field->arg1,
  1744. field->arg2,
  1745. field->arg3);
  1746. if (!ret)
  1747. return TRACE_TYPE_PARTIAL_LINE;
  1748. break;
  1749. }
  1750. case TRACE_PRINT: {
  1751. struct print_entry *field;
  1752. trace_assign_type(field, entry);
  1753. trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
  1754. if (entry->flags & TRACE_FLAG_CONT)
  1755. trace_seq_print_cont(s, iter);
  1756. break;
  1757. }
  1758. }
  1759. return TRACE_TYPE_HANDLED;
  1760. }
  1761. #define SEQ_PUT_FIELD_RET(s, x) \
  1762. do { \
  1763. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1764. return 0; \
  1765. } while (0)
  1766. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1767. do { \
  1768. BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
  1769. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1770. return 0; \
  1771. } while (0)
  1772. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1773. {
  1774. struct trace_seq *s = &iter->seq;
  1775. unsigned char newline = '\n';
  1776. struct trace_entry *entry;
  1777. int S, T;
  1778. entry = iter->ent;
  1779. if (entry->type == TRACE_CONT)
  1780. return TRACE_TYPE_HANDLED;
  1781. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1782. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1783. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1784. switch (entry->type) {
  1785. case TRACE_FN: {
  1786. struct ftrace_entry *field;
  1787. trace_assign_type(field, entry);
  1788. SEQ_PUT_HEX_FIELD_RET(s, field->ip);
  1789. SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
  1790. break;
  1791. }
  1792. case TRACE_CTX:
  1793. case TRACE_WAKE: {
  1794. struct ctx_switch_entry *field;
  1795. trace_assign_type(field, entry);
  1796. S = field->prev_state < sizeof(state_to_char) ?
  1797. state_to_char[field->prev_state] : 'X';
  1798. T = field->next_state < sizeof(state_to_char) ?
  1799. state_to_char[field->next_state] : 'X';
  1800. if (entry->type == TRACE_WAKE)
  1801. S = '+';
  1802. SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
  1803. SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
  1804. SEQ_PUT_HEX_FIELD_RET(s, S);
  1805. SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
  1806. SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
  1807. SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
  1808. SEQ_PUT_HEX_FIELD_RET(s, T);
  1809. break;
  1810. }
  1811. case TRACE_SPECIAL:
  1812. case TRACE_USER_STACK:
  1813. case TRACE_STACK: {
  1814. struct special_entry *field;
  1815. trace_assign_type(field, entry);
  1816. SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
  1817. SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
  1818. SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
  1819. break;
  1820. }
  1821. }
  1822. SEQ_PUT_FIELD_RET(s, newline);
  1823. return TRACE_TYPE_HANDLED;
  1824. }
  1825. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1826. {
  1827. struct trace_seq *s = &iter->seq;
  1828. struct trace_entry *entry;
  1829. entry = iter->ent;
  1830. if (entry->type == TRACE_CONT)
  1831. return TRACE_TYPE_HANDLED;
  1832. SEQ_PUT_FIELD_RET(s, entry->pid);
  1833. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1834. SEQ_PUT_FIELD_RET(s, iter->ts);
  1835. switch (entry->type) {
  1836. case TRACE_FN: {
  1837. struct ftrace_entry *field;
  1838. trace_assign_type(field, entry);
  1839. SEQ_PUT_FIELD_RET(s, field->ip);
  1840. SEQ_PUT_FIELD_RET(s, field->parent_ip);
  1841. break;
  1842. }
  1843. case TRACE_CTX: {
  1844. struct ctx_switch_entry *field;
  1845. trace_assign_type(field, entry);
  1846. SEQ_PUT_FIELD_RET(s, field->prev_pid);
  1847. SEQ_PUT_FIELD_RET(s, field->prev_prio);
  1848. SEQ_PUT_FIELD_RET(s, field->prev_state);
  1849. SEQ_PUT_FIELD_RET(s, field->next_pid);
  1850. SEQ_PUT_FIELD_RET(s, field->next_prio);
  1851. SEQ_PUT_FIELD_RET(s, field->next_state);
  1852. break;
  1853. }
  1854. case TRACE_SPECIAL:
  1855. case TRACE_USER_STACK:
  1856. case TRACE_STACK: {
  1857. struct special_entry *field;
  1858. trace_assign_type(field, entry);
  1859. SEQ_PUT_FIELD_RET(s, field->arg1);
  1860. SEQ_PUT_FIELD_RET(s, field->arg2);
  1861. SEQ_PUT_FIELD_RET(s, field->arg3);
  1862. break;
  1863. }
  1864. }
  1865. return 1;
  1866. }
  1867. static int trace_empty(struct trace_iterator *iter)
  1868. {
  1869. int cpu;
  1870. for_each_tracing_cpu(cpu) {
  1871. if (iter->buffer_iter[cpu]) {
  1872. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1873. return 0;
  1874. } else {
  1875. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1876. return 0;
  1877. }
  1878. }
  1879. return 1;
  1880. }
  1881. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1882. {
  1883. enum print_line_t ret;
  1884. if (iter->trace && iter->trace->print_line) {
  1885. ret = iter->trace->print_line(iter);
  1886. if (ret != TRACE_TYPE_UNHANDLED)
  1887. return ret;
  1888. }
  1889. if (trace_flags & TRACE_ITER_BIN)
  1890. return print_bin_fmt(iter);
  1891. if (trace_flags & TRACE_ITER_HEX)
  1892. return print_hex_fmt(iter);
  1893. if (trace_flags & TRACE_ITER_RAW)
  1894. return print_raw_fmt(iter);
  1895. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1896. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1897. return print_trace_fmt(iter);
  1898. }
  1899. static int s_show(struct seq_file *m, void *v)
  1900. {
  1901. struct trace_iterator *iter = v;
  1902. if (iter->ent == NULL) {
  1903. if (iter->tr) {
  1904. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1905. seq_puts(m, "#\n");
  1906. }
  1907. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1908. /* print nothing if the buffers are empty */
  1909. if (trace_empty(iter))
  1910. return 0;
  1911. print_trace_header(m, iter);
  1912. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1913. print_lat_help_header(m);
  1914. } else {
  1915. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1916. print_func_help_header(m);
  1917. }
  1918. } else {
  1919. print_trace_line(iter);
  1920. trace_print_seq(m, &iter->seq);
  1921. }
  1922. return 0;
  1923. }
  1924. static struct seq_operations tracer_seq_ops = {
  1925. .start = s_start,
  1926. .next = s_next,
  1927. .stop = s_stop,
  1928. .show = s_show,
  1929. };
  1930. static struct trace_iterator *
  1931. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1932. {
  1933. struct trace_iterator *iter;
  1934. struct seq_file *m;
  1935. int cpu;
  1936. if (tracing_disabled) {
  1937. *ret = -ENODEV;
  1938. return NULL;
  1939. }
  1940. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1941. if (!iter) {
  1942. *ret = -ENOMEM;
  1943. goto out;
  1944. }
  1945. mutex_lock(&trace_types_lock);
  1946. if (current_trace && current_trace->print_max)
  1947. iter->tr = &max_tr;
  1948. else
  1949. iter->tr = inode->i_private;
  1950. iter->trace = current_trace;
  1951. iter->pos = -1;
  1952. /* Annotate start of buffers if we had overruns */
  1953. if (ring_buffer_overruns(iter->tr->buffer))
  1954. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1955. for_each_tracing_cpu(cpu) {
  1956. iter->buffer_iter[cpu] =
  1957. ring_buffer_read_start(iter->tr->buffer, cpu);
  1958. if (!iter->buffer_iter[cpu])
  1959. goto fail_buffer;
  1960. }
  1961. /* TODO stop tracer */
  1962. *ret = seq_open(file, &tracer_seq_ops);
  1963. if (*ret)
  1964. goto fail_buffer;
  1965. m = file->private_data;
  1966. m->private = iter;
  1967. /* stop the trace while dumping */
  1968. tracing_stop();
  1969. if (iter->trace && iter->trace->open)
  1970. iter->trace->open(iter);
  1971. mutex_unlock(&trace_types_lock);
  1972. out:
  1973. return iter;
  1974. fail_buffer:
  1975. for_each_tracing_cpu(cpu) {
  1976. if (iter->buffer_iter[cpu])
  1977. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1978. }
  1979. mutex_unlock(&trace_types_lock);
  1980. kfree(iter);
  1981. return ERR_PTR(-ENOMEM);
  1982. }
  1983. int tracing_open_generic(struct inode *inode, struct file *filp)
  1984. {
  1985. if (tracing_disabled)
  1986. return -ENODEV;
  1987. filp->private_data = inode->i_private;
  1988. return 0;
  1989. }
  1990. int tracing_release(struct inode *inode, struct file *file)
  1991. {
  1992. struct seq_file *m = (struct seq_file *)file->private_data;
  1993. struct trace_iterator *iter = m->private;
  1994. int cpu;
  1995. mutex_lock(&trace_types_lock);
  1996. for_each_tracing_cpu(cpu) {
  1997. if (iter->buffer_iter[cpu])
  1998. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1999. }
  2000. if (iter->trace && iter->trace->close)
  2001. iter->trace->close(iter);
  2002. /* reenable tracing if it was previously enabled */
  2003. tracing_start();
  2004. mutex_unlock(&trace_types_lock);
  2005. seq_release(inode, file);
  2006. kfree(iter);
  2007. return 0;
  2008. }
  2009. static int tracing_open(struct inode *inode, struct file *file)
  2010. {
  2011. int ret;
  2012. __tracing_open(inode, file, &ret);
  2013. return ret;
  2014. }
  2015. static int tracing_lt_open(struct inode *inode, struct file *file)
  2016. {
  2017. struct trace_iterator *iter;
  2018. int ret;
  2019. iter = __tracing_open(inode, file, &ret);
  2020. if (!ret)
  2021. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2022. return ret;
  2023. }
  2024. static void *
  2025. t_next(struct seq_file *m, void *v, loff_t *pos)
  2026. {
  2027. struct tracer *t = m->private;
  2028. (*pos)++;
  2029. if (t)
  2030. t = t->next;
  2031. m->private = t;
  2032. return t;
  2033. }
  2034. static void *t_start(struct seq_file *m, loff_t *pos)
  2035. {
  2036. struct tracer *t = m->private;
  2037. loff_t l = 0;
  2038. mutex_lock(&trace_types_lock);
  2039. for (; t && l < *pos; t = t_next(m, t, &l))
  2040. ;
  2041. return t;
  2042. }
  2043. static void t_stop(struct seq_file *m, void *p)
  2044. {
  2045. mutex_unlock(&trace_types_lock);
  2046. }
  2047. static int t_show(struct seq_file *m, void *v)
  2048. {
  2049. struct tracer *t = v;
  2050. if (!t)
  2051. return 0;
  2052. seq_printf(m, "%s", t->name);
  2053. if (t->next)
  2054. seq_putc(m, ' ');
  2055. else
  2056. seq_putc(m, '\n');
  2057. return 0;
  2058. }
  2059. static struct seq_operations show_traces_seq_ops = {
  2060. .start = t_start,
  2061. .next = t_next,
  2062. .stop = t_stop,
  2063. .show = t_show,
  2064. };
  2065. static int show_traces_open(struct inode *inode, struct file *file)
  2066. {
  2067. int ret;
  2068. if (tracing_disabled)
  2069. return -ENODEV;
  2070. ret = seq_open(file, &show_traces_seq_ops);
  2071. if (!ret) {
  2072. struct seq_file *m = file->private_data;
  2073. m->private = trace_types;
  2074. }
  2075. return ret;
  2076. }
  2077. static struct file_operations tracing_fops = {
  2078. .open = tracing_open,
  2079. .read = seq_read,
  2080. .llseek = seq_lseek,
  2081. .release = tracing_release,
  2082. };
  2083. static struct file_operations tracing_lt_fops = {
  2084. .open = tracing_lt_open,
  2085. .read = seq_read,
  2086. .llseek = seq_lseek,
  2087. .release = tracing_release,
  2088. };
  2089. static struct file_operations show_traces_fops = {
  2090. .open = show_traces_open,
  2091. .read = seq_read,
  2092. .release = seq_release,
  2093. };
  2094. /*
  2095. * Only trace on a CPU if the bitmask is set:
  2096. */
  2097. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  2098. /*
  2099. * When tracing/tracing_cpu_mask is modified then this holds
  2100. * the new bitmask we are about to install:
  2101. */
  2102. static cpumask_t tracing_cpumask_new;
  2103. /*
  2104. * The tracer itself will not take this lock, but still we want
  2105. * to provide a consistent cpumask to user-space:
  2106. */
  2107. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2108. /*
  2109. * Temporary storage for the character representation of the
  2110. * CPU bitmask (and one more byte for the newline):
  2111. */
  2112. static char mask_str[NR_CPUS + 1];
  2113. static ssize_t
  2114. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2115. size_t count, loff_t *ppos)
  2116. {
  2117. int len;
  2118. mutex_lock(&tracing_cpumask_update_lock);
  2119. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2120. if (count - len < 2) {
  2121. count = -EINVAL;
  2122. goto out_err;
  2123. }
  2124. len += sprintf(mask_str + len, "\n");
  2125. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2126. out_err:
  2127. mutex_unlock(&tracing_cpumask_update_lock);
  2128. return count;
  2129. }
  2130. static ssize_t
  2131. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2132. size_t count, loff_t *ppos)
  2133. {
  2134. int err, cpu;
  2135. mutex_lock(&tracing_cpumask_update_lock);
  2136. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2137. if (err)
  2138. goto err_unlock;
  2139. raw_local_irq_disable();
  2140. __raw_spin_lock(&ftrace_max_lock);
  2141. for_each_tracing_cpu(cpu) {
  2142. /*
  2143. * Increase/decrease the disabled counter if we are
  2144. * about to flip a bit in the cpumask:
  2145. */
  2146. if (cpu_isset(cpu, tracing_cpumask) &&
  2147. !cpu_isset(cpu, tracing_cpumask_new)) {
  2148. atomic_inc(&global_trace.data[cpu]->disabled);
  2149. }
  2150. if (!cpu_isset(cpu, tracing_cpumask) &&
  2151. cpu_isset(cpu, tracing_cpumask_new)) {
  2152. atomic_dec(&global_trace.data[cpu]->disabled);
  2153. }
  2154. }
  2155. __raw_spin_unlock(&ftrace_max_lock);
  2156. raw_local_irq_enable();
  2157. tracing_cpumask = tracing_cpumask_new;
  2158. mutex_unlock(&tracing_cpumask_update_lock);
  2159. return count;
  2160. err_unlock:
  2161. mutex_unlock(&tracing_cpumask_update_lock);
  2162. return err;
  2163. }
  2164. static struct file_operations tracing_cpumask_fops = {
  2165. .open = tracing_open_generic,
  2166. .read = tracing_cpumask_read,
  2167. .write = tracing_cpumask_write,
  2168. };
  2169. static ssize_t
  2170. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  2171. size_t cnt, loff_t *ppos)
  2172. {
  2173. int i;
  2174. char *buf;
  2175. int r = 0;
  2176. int len = 0;
  2177. u32 tracer_flags = current_trace->flags->val;
  2178. struct tracer_opt *trace_opts = current_trace->flags->opts;
  2179. /* calulate max size */
  2180. for (i = 0; trace_options[i]; i++) {
  2181. len += strlen(trace_options[i]);
  2182. len += 3; /* "no" and space */
  2183. }
  2184. /*
  2185. * Increase the size with names of options specific
  2186. * of the current tracer.
  2187. */
  2188. for (i = 0; trace_opts[i].name; i++) {
  2189. len += strlen(trace_opts[i].name);
  2190. len += 3; /* "no" and space */
  2191. }
  2192. /* +2 for \n and \0 */
  2193. buf = kmalloc(len + 2, GFP_KERNEL);
  2194. if (!buf)
  2195. return -ENOMEM;
  2196. for (i = 0; trace_options[i]; i++) {
  2197. if (trace_flags & (1 << i))
  2198. r += sprintf(buf + r, "%s ", trace_options[i]);
  2199. else
  2200. r += sprintf(buf + r, "no%s ", trace_options[i]);
  2201. }
  2202. for (i = 0; trace_opts[i].name; i++) {
  2203. if (tracer_flags & trace_opts[i].bit)
  2204. r += sprintf(buf + r, "%s ",
  2205. trace_opts[i].name);
  2206. else
  2207. r += sprintf(buf + r, "no%s ",
  2208. trace_opts[i].name);
  2209. }
  2210. r += sprintf(buf + r, "\n");
  2211. WARN_ON(r >= len + 2);
  2212. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2213. kfree(buf);
  2214. return r;
  2215. }
  2216. /* Try to assign a tracer specific option */
  2217. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2218. {
  2219. struct tracer_flags *trace_flags = trace->flags;
  2220. struct tracer_opt *opts = NULL;
  2221. int ret = 0, i = 0;
  2222. int len;
  2223. for (i = 0; trace_flags->opts[i].name; i++) {
  2224. opts = &trace_flags->opts[i];
  2225. len = strlen(opts->name);
  2226. if (strncmp(cmp, opts->name, len) == 0) {
  2227. ret = trace->set_flag(trace_flags->val,
  2228. opts->bit, !neg);
  2229. break;
  2230. }
  2231. }
  2232. /* Not found */
  2233. if (!trace_flags->opts[i].name)
  2234. return -EINVAL;
  2235. /* Refused to handle */
  2236. if (ret)
  2237. return ret;
  2238. if (neg)
  2239. trace_flags->val &= ~opts->bit;
  2240. else
  2241. trace_flags->val |= opts->bit;
  2242. return 0;
  2243. }
  2244. static ssize_t
  2245. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2246. size_t cnt, loff_t *ppos)
  2247. {
  2248. char buf[64];
  2249. char *cmp = buf;
  2250. int neg = 0;
  2251. int ret;
  2252. int i;
  2253. if (cnt >= sizeof(buf))
  2254. return -EINVAL;
  2255. if (copy_from_user(&buf, ubuf, cnt))
  2256. return -EFAULT;
  2257. buf[cnt] = 0;
  2258. if (strncmp(buf, "no", 2) == 0) {
  2259. neg = 1;
  2260. cmp += 2;
  2261. }
  2262. for (i = 0; trace_options[i]; i++) {
  2263. int len = strlen(trace_options[i]);
  2264. if (strncmp(cmp, trace_options[i], len) == 0) {
  2265. if (neg)
  2266. trace_flags &= ~(1 << i);
  2267. else
  2268. trace_flags |= (1 << i);
  2269. break;
  2270. }
  2271. }
  2272. /* If no option could be set, test the specific tracer options */
  2273. if (!trace_options[i]) {
  2274. ret = set_tracer_option(current_trace, cmp, neg);
  2275. if (ret)
  2276. return ret;
  2277. }
  2278. filp->f_pos += cnt;
  2279. return cnt;
  2280. }
  2281. static struct file_operations tracing_iter_fops = {
  2282. .open = tracing_open_generic,
  2283. .read = tracing_trace_options_read,
  2284. .write = tracing_trace_options_write,
  2285. };
  2286. static const char readme_msg[] =
  2287. "tracing mini-HOWTO:\n\n"
  2288. "# mkdir /debug\n"
  2289. "# mount -t debugfs nodev /debug\n\n"
  2290. "# cat /debug/tracing/available_tracers\n"
  2291. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  2292. "# cat /debug/tracing/current_tracer\n"
  2293. "none\n"
  2294. "# echo sched_switch > /debug/tracing/current_tracer\n"
  2295. "# cat /debug/tracing/current_tracer\n"
  2296. "sched_switch\n"
  2297. "# cat /debug/tracing/trace_options\n"
  2298. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2299. "# echo print-parent > /debug/tracing/trace_options\n"
  2300. "# echo 1 > /debug/tracing/tracing_enabled\n"
  2301. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  2302. "echo 0 > /debug/tracing/tracing_enabled\n"
  2303. ;
  2304. static ssize_t
  2305. tracing_readme_read(struct file *filp, char __user *ubuf,
  2306. size_t cnt, loff_t *ppos)
  2307. {
  2308. return simple_read_from_buffer(ubuf, cnt, ppos,
  2309. readme_msg, strlen(readme_msg));
  2310. }
  2311. static struct file_operations tracing_readme_fops = {
  2312. .open = tracing_open_generic,
  2313. .read = tracing_readme_read,
  2314. };
  2315. static ssize_t
  2316. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2317. size_t cnt, loff_t *ppos)
  2318. {
  2319. char buf[64];
  2320. int r;
  2321. r = sprintf(buf, "%u\n", tracer_enabled);
  2322. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2323. }
  2324. static ssize_t
  2325. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2326. size_t cnt, loff_t *ppos)
  2327. {
  2328. struct trace_array *tr = filp->private_data;
  2329. char buf[64];
  2330. long val;
  2331. int ret;
  2332. if (cnt >= sizeof(buf))
  2333. return -EINVAL;
  2334. if (copy_from_user(&buf, ubuf, cnt))
  2335. return -EFAULT;
  2336. buf[cnt] = 0;
  2337. ret = strict_strtoul(buf, 10, &val);
  2338. if (ret < 0)
  2339. return ret;
  2340. val = !!val;
  2341. mutex_lock(&trace_types_lock);
  2342. if (tracer_enabled ^ val) {
  2343. if (val) {
  2344. tracer_enabled = 1;
  2345. if (current_trace->start)
  2346. current_trace->start(tr);
  2347. tracing_start();
  2348. } else {
  2349. tracer_enabled = 0;
  2350. tracing_stop();
  2351. if (current_trace->stop)
  2352. current_trace->stop(tr);
  2353. }
  2354. }
  2355. mutex_unlock(&trace_types_lock);
  2356. filp->f_pos += cnt;
  2357. return cnt;
  2358. }
  2359. static ssize_t
  2360. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2361. size_t cnt, loff_t *ppos)
  2362. {
  2363. char buf[max_tracer_type_len+2];
  2364. int r;
  2365. mutex_lock(&trace_types_lock);
  2366. if (current_trace)
  2367. r = sprintf(buf, "%s\n", current_trace->name);
  2368. else
  2369. r = sprintf(buf, "\n");
  2370. mutex_unlock(&trace_types_lock);
  2371. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2372. }
  2373. static int tracing_set_tracer(char *buf)
  2374. {
  2375. struct trace_array *tr = &global_trace;
  2376. struct tracer *t;
  2377. int ret = 0;
  2378. mutex_lock(&trace_types_lock);
  2379. for (t = trace_types; t; t = t->next) {
  2380. if (strcmp(t->name, buf) == 0)
  2381. break;
  2382. }
  2383. if (!t) {
  2384. ret = -EINVAL;
  2385. goto out;
  2386. }
  2387. if (t == current_trace)
  2388. goto out;
  2389. trace_branch_disable();
  2390. if (current_trace && current_trace->reset)
  2391. current_trace->reset(tr);
  2392. current_trace = t;
  2393. if (t->init) {
  2394. ret = t->init(tr);
  2395. if (ret)
  2396. goto out;
  2397. }
  2398. trace_branch_enable(tr);
  2399. out:
  2400. mutex_unlock(&trace_types_lock);
  2401. return ret;
  2402. }
  2403. static ssize_t
  2404. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2405. size_t cnt, loff_t *ppos)
  2406. {
  2407. char buf[max_tracer_type_len+1];
  2408. int i;
  2409. size_t ret;
  2410. int err;
  2411. ret = cnt;
  2412. if (cnt > max_tracer_type_len)
  2413. cnt = max_tracer_type_len;
  2414. if (copy_from_user(&buf, ubuf, cnt))
  2415. return -EFAULT;
  2416. buf[cnt] = 0;
  2417. /* strip ending whitespace. */
  2418. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2419. buf[i] = 0;
  2420. err = tracing_set_tracer(buf);
  2421. if (err)
  2422. return err;
  2423. filp->f_pos += ret;
  2424. return ret;
  2425. }
  2426. static ssize_t
  2427. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2428. size_t cnt, loff_t *ppos)
  2429. {
  2430. unsigned long *ptr = filp->private_data;
  2431. char buf[64];
  2432. int r;
  2433. r = snprintf(buf, sizeof(buf), "%ld\n",
  2434. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2435. if (r > sizeof(buf))
  2436. r = sizeof(buf);
  2437. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2438. }
  2439. static ssize_t
  2440. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2441. size_t cnt, loff_t *ppos)
  2442. {
  2443. long *ptr = filp->private_data;
  2444. char buf[64];
  2445. long val;
  2446. int ret;
  2447. if (cnt >= sizeof(buf))
  2448. return -EINVAL;
  2449. if (copy_from_user(&buf, ubuf, cnt))
  2450. return -EFAULT;
  2451. buf[cnt] = 0;
  2452. ret = strict_strtoul(buf, 10, &val);
  2453. if (ret < 0)
  2454. return ret;
  2455. *ptr = val * 1000;
  2456. return cnt;
  2457. }
  2458. static atomic_t tracing_reader;
  2459. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2460. {
  2461. struct trace_iterator *iter;
  2462. if (tracing_disabled)
  2463. return -ENODEV;
  2464. /* We only allow for reader of the pipe */
  2465. if (atomic_inc_return(&tracing_reader) != 1) {
  2466. atomic_dec(&tracing_reader);
  2467. return -EBUSY;
  2468. }
  2469. /* create a buffer to store the information to pass to userspace */
  2470. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2471. if (!iter)
  2472. return -ENOMEM;
  2473. mutex_lock(&trace_types_lock);
  2474. /* trace pipe does not show start of buffer */
  2475. cpus_setall(iter->started);
  2476. iter->tr = &global_trace;
  2477. iter->trace = current_trace;
  2478. filp->private_data = iter;
  2479. if (iter->trace->pipe_open)
  2480. iter->trace->pipe_open(iter);
  2481. mutex_unlock(&trace_types_lock);
  2482. return 0;
  2483. }
  2484. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2485. {
  2486. struct trace_iterator *iter = file->private_data;
  2487. kfree(iter);
  2488. atomic_dec(&tracing_reader);
  2489. return 0;
  2490. }
  2491. static unsigned int
  2492. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2493. {
  2494. struct trace_iterator *iter = filp->private_data;
  2495. if (trace_flags & TRACE_ITER_BLOCK) {
  2496. /*
  2497. * Always select as readable when in blocking mode
  2498. */
  2499. return POLLIN | POLLRDNORM;
  2500. } else {
  2501. if (!trace_empty(iter))
  2502. return POLLIN | POLLRDNORM;
  2503. poll_wait(filp, &trace_wait, poll_table);
  2504. if (!trace_empty(iter))
  2505. return POLLIN | POLLRDNORM;
  2506. return 0;
  2507. }
  2508. }
  2509. /*
  2510. * Consumer reader.
  2511. */
  2512. static ssize_t
  2513. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2514. size_t cnt, loff_t *ppos)
  2515. {
  2516. struct trace_iterator *iter = filp->private_data;
  2517. ssize_t sret;
  2518. /* return any leftover data */
  2519. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2520. if (sret != -EBUSY)
  2521. return sret;
  2522. trace_seq_reset(&iter->seq);
  2523. mutex_lock(&trace_types_lock);
  2524. if (iter->trace->read) {
  2525. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2526. if (sret)
  2527. goto out;
  2528. }
  2529. waitagain:
  2530. sret = 0;
  2531. while (trace_empty(iter)) {
  2532. if ((filp->f_flags & O_NONBLOCK)) {
  2533. sret = -EAGAIN;
  2534. goto out;
  2535. }
  2536. /*
  2537. * This is a make-shift waitqueue. The reason we don't use
  2538. * an actual wait queue is because:
  2539. * 1) we only ever have one waiter
  2540. * 2) the tracing, traces all functions, we don't want
  2541. * the overhead of calling wake_up and friends
  2542. * (and tracing them too)
  2543. * Anyway, this is really very primitive wakeup.
  2544. */
  2545. set_current_state(TASK_INTERRUPTIBLE);
  2546. iter->tr->waiter = current;
  2547. mutex_unlock(&trace_types_lock);
  2548. /* sleep for 100 msecs, and try again. */
  2549. schedule_timeout(HZ/10);
  2550. mutex_lock(&trace_types_lock);
  2551. iter->tr->waiter = NULL;
  2552. if (signal_pending(current)) {
  2553. sret = -EINTR;
  2554. goto out;
  2555. }
  2556. if (iter->trace != current_trace)
  2557. goto out;
  2558. /*
  2559. * We block until we read something and tracing is disabled.
  2560. * We still block if tracing is disabled, but we have never
  2561. * read anything. This allows a user to cat this file, and
  2562. * then enable tracing. But after we have read something,
  2563. * we give an EOF when tracing is again disabled.
  2564. *
  2565. * iter->pos will be 0 if we haven't read anything.
  2566. */
  2567. if (!tracer_enabled && iter->pos)
  2568. break;
  2569. continue;
  2570. }
  2571. /* stop when tracing is finished */
  2572. if (trace_empty(iter))
  2573. goto out;
  2574. if (cnt >= PAGE_SIZE)
  2575. cnt = PAGE_SIZE - 1;
  2576. /* reset all but tr, trace, and overruns */
  2577. memset(&iter->seq, 0,
  2578. sizeof(struct trace_iterator) -
  2579. offsetof(struct trace_iterator, seq));
  2580. iter->pos = -1;
  2581. while (find_next_entry_inc(iter) != NULL) {
  2582. enum print_line_t ret;
  2583. int len = iter->seq.len;
  2584. ret = print_trace_line(iter);
  2585. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2586. /* don't print partial lines */
  2587. iter->seq.len = len;
  2588. break;
  2589. }
  2590. trace_consume(iter);
  2591. if (iter->seq.len >= cnt)
  2592. break;
  2593. }
  2594. /* Now copy what we have to the user */
  2595. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2596. if (iter->seq.readpos >= iter->seq.len)
  2597. trace_seq_reset(&iter->seq);
  2598. /*
  2599. * If there was nothing to send to user, inspite of consuming trace
  2600. * entries, go back to wait for more entries.
  2601. */
  2602. if (sret == -EBUSY)
  2603. goto waitagain;
  2604. out:
  2605. mutex_unlock(&trace_types_lock);
  2606. return sret;
  2607. }
  2608. static ssize_t
  2609. tracing_entries_read(struct file *filp, char __user *ubuf,
  2610. size_t cnt, loff_t *ppos)
  2611. {
  2612. struct trace_array *tr = filp->private_data;
  2613. char buf[64];
  2614. int r;
  2615. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2616. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2617. }
  2618. static ssize_t
  2619. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2620. size_t cnt, loff_t *ppos)
  2621. {
  2622. unsigned long val;
  2623. char buf[64];
  2624. int ret, cpu;
  2625. if (cnt >= sizeof(buf))
  2626. return -EINVAL;
  2627. if (copy_from_user(&buf, ubuf, cnt))
  2628. return -EFAULT;
  2629. buf[cnt] = 0;
  2630. ret = strict_strtoul(buf, 10, &val);
  2631. if (ret < 0)
  2632. return ret;
  2633. /* must have at least 1 entry */
  2634. if (!val)
  2635. return -EINVAL;
  2636. mutex_lock(&trace_types_lock);
  2637. tracing_stop();
  2638. /* disable all cpu buffers */
  2639. for_each_tracing_cpu(cpu) {
  2640. if (global_trace.data[cpu])
  2641. atomic_inc(&global_trace.data[cpu]->disabled);
  2642. if (max_tr.data[cpu])
  2643. atomic_inc(&max_tr.data[cpu]->disabled);
  2644. }
  2645. /* value is in KB */
  2646. val <<= 10;
  2647. if (val != global_trace.entries) {
  2648. ret = ring_buffer_resize(global_trace.buffer, val);
  2649. if (ret < 0) {
  2650. cnt = ret;
  2651. goto out;
  2652. }
  2653. ret = ring_buffer_resize(max_tr.buffer, val);
  2654. if (ret < 0) {
  2655. int r;
  2656. cnt = ret;
  2657. r = ring_buffer_resize(global_trace.buffer,
  2658. global_trace.entries);
  2659. if (r < 0) {
  2660. /* AARGH! We are left with different
  2661. * size max buffer!!!! */
  2662. WARN_ON(1);
  2663. tracing_disabled = 1;
  2664. }
  2665. goto out;
  2666. }
  2667. global_trace.entries = val;
  2668. }
  2669. filp->f_pos += cnt;
  2670. /* If check pages failed, return ENOMEM */
  2671. if (tracing_disabled)
  2672. cnt = -ENOMEM;
  2673. out:
  2674. for_each_tracing_cpu(cpu) {
  2675. if (global_trace.data[cpu])
  2676. atomic_dec(&global_trace.data[cpu]->disabled);
  2677. if (max_tr.data[cpu])
  2678. atomic_dec(&max_tr.data[cpu]->disabled);
  2679. }
  2680. tracing_start();
  2681. max_tr.entries = global_trace.entries;
  2682. mutex_unlock(&trace_types_lock);
  2683. return cnt;
  2684. }
  2685. static int mark_printk(const char *fmt, ...)
  2686. {
  2687. int ret;
  2688. va_list args;
  2689. va_start(args, fmt);
  2690. ret = trace_vprintk(0, fmt, args);
  2691. va_end(args);
  2692. return ret;
  2693. }
  2694. static ssize_t
  2695. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2696. size_t cnt, loff_t *fpos)
  2697. {
  2698. char *buf;
  2699. char *end;
  2700. if (tracing_disabled)
  2701. return -EINVAL;
  2702. if (cnt > TRACE_BUF_SIZE)
  2703. cnt = TRACE_BUF_SIZE;
  2704. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2705. if (buf == NULL)
  2706. return -ENOMEM;
  2707. if (copy_from_user(buf, ubuf, cnt)) {
  2708. kfree(buf);
  2709. return -EFAULT;
  2710. }
  2711. /* Cut from the first nil or newline. */
  2712. buf[cnt] = '\0';
  2713. end = strchr(buf, '\n');
  2714. if (end)
  2715. *end = '\0';
  2716. cnt = mark_printk("%s\n", buf);
  2717. kfree(buf);
  2718. *fpos += cnt;
  2719. return cnt;
  2720. }
  2721. static struct file_operations tracing_max_lat_fops = {
  2722. .open = tracing_open_generic,
  2723. .read = tracing_max_lat_read,
  2724. .write = tracing_max_lat_write,
  2725. };
  2726. static struct file_operations tracing_ctrl_fops = {
  2727. .open = tracing_open_generic,
  2728. .read = tracing_ctrl_read,
  2729. .write = tracing_ctrl_write,
  2730. };
  2731. static struct file_operations set_tracer_fops = {
  2732. .open = tracing_open_generic,
  2733. .read = tracing_set_trace_read,
  2734. .write = tracing_set_trace_write,
  2735. };
  2736. static struct file_operations tracing_pipe_fops = {
  2737. .open = tracing_open_pipe,
  2738. .poll = tracing_poll_pipe,
  2739. .read = tracing_read_pipe,
  2740. .release = tracing_release_pipe,
  2741. };
  2742. static struct file_operations tracing_entries_fops = {
  2743. .open = tracing_open_generic,
  2744. .read = tracing_entries_read,
  2745. .write = tracing_entries_write,
  2746. };
  2747. static struct file_operations tracing_mark_fops = {
  2748. .open = tracing_open_generic,
  2749. .write = tracing_mark_write,
  2750. };
  2751. #ifdef CONFIG_DYNAMIC_FTRACE
  2752. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2753. {
  2754. return 0;
  2755. }
  2756. static ssize_t
  2757. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2758. size_t cnt, loff_t *ppos)
  2759. {
  2760. static char ftrace_dyn_info_buffer[1024];
  2761. static DEFINE_MUTEX(dyn_info_mutex);
  2762. unsigned long *p = filp->private_data;
  2763. char *buf = ftrace_dyn_info_buffer;
  2764. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2765. int r;
  2766. mutex_lock(&dyn_info_mutex);
  2767. r = sprintf(buf, "%ld ", *p);
  2768. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2769. buf[r++] = '\n';
  2770. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2771. mutex_unlock(&dyn_info_mutex);
  2772. return r;
  2773. }
  2774. static struct file_operations tracing_dyn_info_fops = {
  2775. .open = tracing_open_generic,
  2776. .read = tracing_read_dyn_info,
  2777. };
  2778. #endif
  2779. static struct dentry *d_tracer;
  2780. struct dentry *tracing_init_dentry(void)
  2781. {
  2782. static int once;
  2783. if (d_tracer)
  2784. return d_tracer;
  2785. d_tracer = debugfs_create_dir("tracing", NULL);
  2786. if (!d_tracer && !once) {
  2787. once = 1;
  2788. pr_warning("Could not create debugfs directory 'tracing'\n");
  2789. return NULL;
  2790. }
  2791. return d_tracer;
  2792. }
  2793. #ifdef CONFIG_FTRACE_SELFTEST
  2794. /* Let selftest have access to static functions in this file */
  2795. #include "trace_selftest.c"
  2796. #endif
  2797. static __init int tracer_init_debugfs(void)
  2798. {
  2799. struct dentry *d_tracer;
  2800. struct dentry *entry;
  2801. d_tracer = tracing_init_dentry();
  2802. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2803. &global_trace, &tracing_ctrl_fops);
  2804. if (!entry)
  2805. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2806. entry = debugfs_create_file("trace_options", 0644, d_tracer,
  2807. NULL, &tracing_iter_fops);
  2808. if (!entry)
  2809. pr_warning("Could not create debugfs 'trace_options' entry\n");
  2810. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2811. NULL, &tracing_cpumask_fops);
  2812. if (!entry)
  2813. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2814. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2815. &global_trace, &tracing_lt_fops);
  2816. if (!entry)
  2817. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2818. entry = debugfs_create_file("trace", 0444, d_tracer,
  2819. &global_trace, &tracing_fops);
  2820. if (!entry)
  2821. pr_warning("Could not create debugfs 'trace' entry\n");
  2822. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2823. &global_trace, &show_traces_fops);
  2824. if (!entry)
  2825. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  2826. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2827. &global_trace, &set_tracer_fops);
  2828. if (!entry)
  2829. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  2830. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2831. &tracing_max_latency,
  2832. &tracing_max_lat_fops);
  2833. if (!entry)
  2834. pr_warning("Could not create debugfs "
  2835. "'tracing_max_latency' entry\n");
  2836. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2837. &tracing_thresh, &tracing_max_lat_fops);
  2838. if (!entry)
  2839. pr_warning("Could not create debugfs "
  2840. "'tracing_thresh' entry\n");
  2841. entry = debugfs_create_file("README", 0644, d_tracer,
  2842. NULL, &tracing_readme_fops);
  2843. if (!entry)
  2844. pr_warning("Could not create debugfs 'README' entry\n");
  2845. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2846. NULL, &tracing_pipe_fops);
  2847. if (!entry)
  2848. pr_warning("Could not create debugfs "
  2849. "'trace_pipe' entry\n");
  2850. entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
  2851. &global_trace, &tracing_entries_fops);
  2852. if (!entry)
  2853. pr_warning("Could not create debugfs "
  2854. "'buffer_size_kb' entry\n");
  2855. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  2856. NULL, &tracing_mark_fops);
  2857. if (!entry)
  2858. pr_warning("Could not create debugfs "
  2859. "'trace_marker' entry\n");
  2860. #ifdef CONFIG_DYNAMIC_FTRACE
  2861. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2862. &ftrace_update_tot_cnt,
  2863. &tracing_dyn_info_fops);
  2864. if (!entry)
  2865. pr_warning("Could not create debugfs "
  2866. "'dyn_ftrace_total_info' entry\n");
  2867. #endif
  2868. #ifdef CONFIG_SYSPROF_TRACER
  2869. init_tracer_sysprof_debugfs(d_tracer);
  2870. #endif
  2871. return 0;
  2872. }
  2873. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2874. {
  2875. static DEFINE_SPINLOCK(trace_buf_lock);
  2876. static char trace_buf[TRACE_BUF_SIZE];
  2877. struct ring_buffer_event *event;
  2878. struct trace_array *tr = &global_trace;
  2879. struct trace_array_cpu *data;
  2880. struct print_entry *entry;
  2881. unsigned long flags, irq_flags;
  2882. int cpu, len = 0, size, pc;
  2883. if (tracing_disabled)
  2884. return 0;
  2885. pc = preempt_count();
  2886. preempt_disable_notrace();
  2887. cpu = raw_smp_processor_id();
  2888. data = tr->data[cpu];
  2889. if (unlikely(atomic_read(&data->disabled)))
  2890. goto out;
  2891. spin_lock_irqsave(&trace_buf_lock, flags);
  2892. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  2893. len = min(len, TRACE_BUF_SIZE-1);
  2894. trace_buf[len] = 0;
  2895. size = sizeof(*entry) + len + 1;
  2896. event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
  2897. if (!event)
  2898. goto out_unlock;
  2899. entry = ring_buffer_event_data(event);
  2900. tracing_generic_entry_update(&entry->ent, flags, pc);
  2901. entry->ent.type = TRACE_PRINT;
  2902. entry->ip = ip;
  2903. memcpy(&entry->buf, trace_buf, len);
  2904. entry->buf[len] = 0;
  2905. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  2906. out_unlock:
  2907. spin_unlock_irqrestore(&trace_buf_lock, flags);
  2908. out:
  2909. preempt_enable_notrace();
  2910. return len;
  2911. }
  2912. EXPORT_SYMBOL_GPL(trace_vprintk);
  2913. int __ftrace_printk(unsigned long ip, const char *fmt, ...)
  2914. {
  2915. int ret;
  2916. va_list ap;
  2917. if (!(trace_flags & TRACE_ITER_PRINTK))
  2918. return 0;
  2919. va_start(ap, fmt);
  2920. ret = trace_vprintk(ip, fmt, ap);
  2921. va_end(ap);
  2922. return ret;
  2923. }
  2924. EXPORT_SYMBOL_GPL(__ftrace_printk);
  2925. static int trace_panic_handler(struct notifier_block *this,
  2926. unsigned long event, void *unused)
  2927. {
  2928. if (ftrace_dump_on_oops)
  2929. ftrace_dump();
  2930. return NOTIFY_OK;
  2931. }
  2932. static struct notifier_block trace_panic_notifier = {
  2933. .notifier_call = trace_panic_handler,
  2934. .next = NULL,
  2935. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  2936. };
  2937. static int trace_die_handler(struct notifier_block *self,
  2938. unsigned long val,
  2939. void *data)
  2940. {
  2941. switch (val) {
  2942. case DIE_OOPS:
  2943. if (ftrace_dump_on_oops)
  2944. ftrace_dump();
  2945. break;
  2946. default:
  2947. break;
  2948. }
  2949. return NOTIFY_OK;
  2950. }
  2951. static struct notifier_block trace_die_notifier = {
  2952. .notifier_call = trace_die_handler,
  2953. .priority = 200
  2954. };
  2955. /*
  2956. * printk is set to max of 1024, we really don't need it that big.
  2957. * Nothing should be printing 1000 characters anyway.
  2958. */
  2959. #define TRACE_MAX_PRINT 1000
  2960. /*
  2961. * Define here KERN_TRACE so that we have one place to modify
  2962. * it if we decide to change what log level the ftrace dump
  2963. * should be at.
  2964. */
  2965. #define KERN_TRACE KERN_INFO
  2966. static void
  2967. trace_printk_seq(struct trace_seq *s)
  2968. {
  2969. /* Probably should print a warning here. */
  2970. if (s->len >= 1000)
  2971. s->len = 1000;
  2972. /* should be zero ended, but we are paranoid. */
  2973. s->buffer[s->len] = 0;
  2974. printk(KERN_TRACE "%s", s->buffer);
  2975. trace_seq_reset(s);
  2976. }
  2977. void ftrace_dump(void)
  2978. {
  2979. static DEFINE_SPINLOCK(ftrace_dump_lock);
  2980. /* use static because iter can be a bit big for the stack */
  2981. static struct trace_iterator iter;
  2982. static cpumask_t mask;
  2983. static int dump_ran;
  2984. unsigned long flags;
  2985. int cnt = 0, cpu;
  2986. /* only one dump */
  2987. spin_lock_irqsave(&ftrace_dump_lock, flags);
  2988. if (dump_ran)
  2989. goto out;
  2990. dump_ran = 1;
  2991. /* No turning back! */
  2992. ftrace_kill();
  2993. for_each_tracing_cpu(cpu) {
  2994. atomic_inc(&global_trace.data[cpu]->disabled);
  2995. }
  2996. /* don't look at user memory in panic mode */
  2997. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  2998. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  2999. iter.tr = &global_trace;
  3000. iter.trace = current_trace;
  3001. /*
  3002. * We need to stop all tracing on all CPUS to read the
  3003. * the next buffer. This is a bit expensive, but is
  3004. * not done often. We fill all what we can read,
  3005. * and then release the locks again.
  3006. */
  3007. cpus_clear(mask);
  3008. while (!trace_empty(&iter)) {
  3009. if (!cnt)
  3010. printk(KERN_TRACE "---------------------------------\n");
  3011. cnt++;
  3012. /* reset all but tr, trace, and overruns */
  3013. memset(&iter.seq, 0,
  3014. sizeof(struct trace_iterator) -
  3015. offsetof(struct trace_iterator, seq));
  3016. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3017. iter.pos = -1;
  3018. if (find_next_entry_inc(&iter) != NULL) {
  3019. print_trace_line(&iter);
  3020. trace_consume(&iter);
  3021. }
  3022. trace_printk_seq(&iter.seq);
  3023. }
  3024. if (!cnt)
  3025. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3026. else
  3027. printk(KERN_TRACE "---------------------------------\n");
  3028. out:
  3029. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  3030. }
  3031. __init static int tracer_alloc_buffers(void)
  3032. {
  3033. struct trace_array_cpu *data;
  3034. int i;
  3035. /* TODO: make the number of buffers hot pluggable with CPUS */
  3036. tracing_buffer_mask = cpu_possible_map;
  3037. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  3038. TRACE_BUFFER_FLAGS);
  3039. if (!global_trace.buffer) {
  3040. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3041. WARN_ON(1);
  3042. return 0;
  3043. }
  3044. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3045. #ifdef CONFIG_TRACER_MAX_TRACE
  3046. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  3047. TRACE_BUFFER_FLAGS);
  3048. if (!max_tr.buffer) {
  3049. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3050. WARN_ON(1);
  3051. ring_buffer_free(global_trace.buffer);
  3052. return 0;
  3053. }
  3054. max_tr.entries = ring_buffer_size(max_tr.buffer);
  3055. WARN_ON(max_tr.entries != global_trace.entries);
  3056. #endif
  3057. /* Allocate the first page for all buffers */
  3058. for_each_tracing_cpu(i) {
  3059. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3060. max_tr.data[i] = &per_cpu(max_data, i);
  3061. }
  3062. trace_init_cmdlines();
  3063. register_tracer(&nop_trace);
  3064. #ifdef CONFIG_BOOT_TRACER
  3065. register_tracer(&boot_tracer);
  3066. current_trace = &boot_tracer;
  3067. current_trace->init(&global_trace);
  3068. #else
  3069. current_trace = &nop_trace;
  3070. #endif
  3071. /* All seems OK, enable tracing */
  3072. tracing_disabled = 0;
  3073. atomic_notifier_chain_register(&panic_notifier_list,
  3074. &trace_panic_notifier);
  3075. register_die_notifier(&trace_die_notifier);
  3076. return 0;
  3077. }
  3078. early_initcall(tracer_alloc_buffers);
  3079. fs_initcall(tracer_init_debugfs);