trace.c 71 KB

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