trace.c 70 KB

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