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