trace.c 86 KB

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