trace.c 70 KB

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