trace.c 80 KB

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