trace.c 87 KB

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