trace.c 84 KB

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