trace.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  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. if (last_ent && !ent)
  995. seq_puts(m, "\n\nvim:ft=help\n");
  996. return ent;
  997. }
  998. static void *s_start(struct seq_file *m, loff_t *pos)
  999. {
  1000. struct trace_iterator *iter = m->private;
  1001. void *p = NULL;
  1002. loff_t l = 0;
  1003. int i;
  1004. mutex_lock(&trace_types_lock);
  1005. if (!current_trace || current_trace != iter->trace) {
  1006. mutex_unlock(&trace_types_lock);
  1007. return NULL;
  1008. }
  1009. atomic_inc(&trace_record_cmdline_disabled);
  1010. /* let the tracer grab locks here if needed */
  1011. if (current_trace->start)
  1012. current_trace->start(iter);
  1013. if (*pos != iter->pos) {
  1014. iter->ent = NULL;
  1015. iter->cpu = 0;
  1016. iter->idx = -1;
  1017. iter->prev_ent = NULL;
  1018. iter->prev_cpu = -1;
  1019. for_each_tracing_cpu(i) {
  1020. iter->next_idx[i] = 0;
  1021. iter->next_page[i] = NULL;
  1022. }
  1023. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1024. ;
  1025. } else {
  1026. l = *pos - 1;
  1027. p = s_next(m, p, &l);
  1028. }
  1029. return p;
  1030. }
  1031. static void s_stop(struct seq_file *m, void *p)
  1032. {
  1033. struct trace_iterator *iter = m->private;
  1034. atomic_dec(&trace_record_cmdline_disabled);
  1035. /* let the tracer release locks here if needed */
  1036. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  1037. iter->trace->stop(iter);
  1038. mutex_unlock(&trace_types_lock);
  1039. }
  1040. #define KRETPROBE_MSG "[unknown/kretprobe'd]"
  1041. #ifdef CONFIG_KRETPROBES
  1042. static inline int kretprobed(unsigned long addr)
  1043. {
  1044. return addr == (unsigned long)kretprobe_trampoline;
  1045. }
  1046. #else
  1047. static inline int kretprobed(unsigned long addr)
  1048. {
  1049. return 0;
  1050. }
  1051. #endif /* CONFIG_KRETPROBES */
  1052. static int
  1053. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  1054. {
  1055. #ifdef CONFIG_KALLSYMS
  1056. char str[KSYM_SYMBOL_LEN];
  1057. kallsyms_lookup(address, NULL, NULL, NULL, str);
  1058. return trace_seq_printf(s, fmt, str);
  1059. #endif
  1060. return 1;
  1061. }
  1062. static int
  1063. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  1064. unsigned long address)
  1065. {
  1066. #ifdef CONFIG_KALLSYMS
  1067. char str[KSYM_SYMBOL_LEN];
  1068. sprint_symbol(str, address);
  1069. return trace_seq_printf(s, fmt, str);
  1070. #endif
  1071. return 1;
  1072. }
  1073. #ifndef CONFIG_64BIT
  1074. # define IP_FMT "%08lx"
  1075. #else
  1076. # define IP_FMT "%016lx"
  1077. #endif
  1078. static int
  1079. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  1080. {
  1081. int ret;
  1082. if (!ip)
  1083. return trace_seq_printf(s, "0");
  1084. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  1085. ret = seq_print_sym_offset(s, "%s", ip);
  1086. else
  1087. ret = seq_print_sym_short(s, "%s", ip);
  1088. if (!ret)
  1089. return 0;
  1090. if (sym_flags & TRACE_ITER_SYM_ADDR)
  1091. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1092. return ret;
  1093. }
  1094. static void print_lat_help_header(struct seq_file *m)
  1095. {
  1096. seq_puts(m, "# _------=> CPU# \n");
  1097. seq_puts(m, "# / _-----=> irqs-off \n");
  1098. seq_puts(m, "# | / _----=> need-resched \n");
  1099. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1100. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1101. seq_puts(m, "# |||| / \n");
  1102. seq_puts(m, "# ||||| delay \n");
  1103. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1104. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1105. }
  1106. static void print_func_help_header(struct seq_file *m)
  1107. {
  1108. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1109. seq_puts(m, "# | | | | |\n");
  1110. }
  1111. static void
  1112. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1113. {
  1114. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1115. struct trace_array *tr = iter->tr;
  1116. struct trace_array_cpu *data = tr->data[tr->cpu];
  1117. struct tracer *type = current_trace;
  1118. unsigned long total = 0;
  1119. unsigned long entries = 0;
  1120. int cpu;
  1121. const char *name = "preemption";
  1122. if (type)
  1123. name = type->name;
  1124. for_each_tracing_cpu(cpu) {
  1125. if (head_page(tr->data[cpu])) {
  1126. total += tr->data[cpu]->trace_idx;
  1127. if (tr->data[cpu]->trace_idx > tr->entries)
  1128. entries += tr->entries;
  1129. else
  1130. entries += tr->data[cpu]->trace_idx;
  1131. }
  1132. }
  1133. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1134. name, UTS_RELEASE);
  1135. seq_puts(m, "-----------------------------------"
  1136. "---------------------------------\n");
  1137. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1138. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1139. nsecs_to_usecs(data->saved_latency),
  1140. entries,
  1141. total,
  1142. tr->cpu,
  1143. #if defined(CONFIG_PREEMPT_NONE)
  1144. "server",
  1145. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1146. "desktop",
  1147. #elif defined(CONFIG_PREEMPT)
  1148. "preempt",
  1149. #else
  1150. "unknown",
  1151. #endif
  1152. /* These are reserved for later use */
  1153. 0, 0, 0, 0);
  1154. #ifdef CONFIG_SMP
  1155. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1156. #else
  1157. seq_puts(m, ")\n");
  1158. #endif
  1159. seq_puts(m, " -----------------\n");
  1160. seq_printf(m, " | task: %.16s-%d "
  1161. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1162. data->comm, data->pid, data->uid, data->nice,
  1163. data->policy, data->rt_priority);
  1164. seq_puts(m, " -----------------\n");
  1165. if (data->critical_start) {
  1166. seq_puts(m, " => started at: ");
  1167. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1168. trace_print_seq(m, &iter->seq);
  1169. seq_puts(m, "\n => ended at: ");
  1170. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1171. trace_print_seq(m, &iter->seq);
  1172. seq_puts(m, "\n");
  1173. }
  1174. seq_puts(m, "\n");
  1175. }
  1176. static void
  1177. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1178. {
  1179. int hardirq, softirq;
  1180. char *comm;
  1181. comm = trace_find_cmdline(entry->pid);
  1182. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1183. trace_seq_printf(s, "%d", cpu);
  1184. trace_seq_printf(s, "%c%c",
  1185. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  1186. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1187. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1188. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1189. if (hardirq && softirq) {
  1190. trace_seq_putc(s, 'H');
  1191. } else {
  1192. if (hardirq) {
  1193. trace_seq_putc(s, 'h');
  1194. } else {
  1195. if (softirq)
  1196. trace_seq_putc(s, 's');
  1197. else
  1198. trace_seq_putc(s, '.');
  1199. }
  1200. }
  1201. if (entry->preempt_count)
  1202. trace_seq_printf(s, "%x", entry->preempt_count);
  1203. else
  1204. trace_seq_puts(s, ".");
  1205. }
  1206. unsigned long preempt_mark_thresh = 100;
  1207. static void
  1208. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  1209. unsigned long rel_usecs)
  1210. {
  1211. trace_seq_printf(s, " %4lldus", abs_usecs);
  1212. if (rel_usecs > preempt_mark_thresh)
  1213. trace_seq_puts(s, "!: ");
  1214. else if (rel_usecs > 1)
  1215. trace_seq_puts(s, "+: ");
  1216. else
  1217. trace_seq_puts(s, " : ");
  1218. }
  1219. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1220. static int
  1221. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1222. {
  1223. struct trace_seq *s = &iter->seq;
  1224. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1225. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  1226. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1227. struct trace_entry *entry = iter->ent;
  1228. unsigned long abs_usecs;
  1229. unsigned long rel_usecs;
  1230. char *comm;
  1231. int S, T;
  1232. int i;
  1233. unsigned state;
  1234. if (!next_entry)
  1235. next_entry = entry;
  1236. rel_usecs = ns2usecs(next_entry->t - entry->t);
  1237. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  1238. if (verbose) {
  1239. comm = trace_find_cmdline(entry->pid);
  1240. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  1241. " %ld.%03ldms (+%ld.%03ldms): ",
  1242. comm,
  1243. entry->pid, cpu, entry->flags,
  1244. entry->preempt_count, trace_idx,
  1245. ns2usecs(entry->t),
  1246. abs_usecs/1000,
  1247. abs_usecs % 1000, rel_usecs/1000,
  1248. rel_usecs % 1000);
  1249. } else {
  1250. lat_print_generic(s, entry, cpu);
  1251. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1252. }
  1253. switch (entry->type) {
  1254. case TRACE_FN:
  1255. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1256. trace_seq_puts(s, " (");
  1257. if (kretprobed(entry->fn.parent_ip))
  1258. trace_seq_puts(s, KRETPROBE_MSG);
  1259. else
  1260. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1261. trace_seq_puts(s, ")\n");
  1262. break;
  1263. case TRACE_CTX:
  1264. case TRACE_WAKE:
  1265. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1266. state_to_char[entry->ctx.next_state] : 'X';
  1267. state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
  1268. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1269. comm = trace_find_cmdline(entry->ctx.next_pid);
  1270. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1271. entry->ctx.prev_pid,
  1272. entry->ctx.prev_prio,
  1273. S, entry->type == TRACE_CTX ? "==>" : " +",
  1274. entry->ctx.next_pid,
  1275. entry->ctx.next_prio,
  1276. T, comm);
  1277. break;
  1278. case TRACE_SPECIAL:
  1279. trace_seq_printf(s, "# %ld %ld %ld\n",
  1280. entry->special.arg1,
  1281. entry->special.arg2,
  1282. entry->special.arg3);
  1283. break;
  1284. case TRACE_STACK:
  1285. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1286. if (i)
  1287. trace_seq_puts(s, " <= ");
  1288. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1289. }
  1290. trace_seq_puts(s, "\n");
  1291. break;
  1292. default:
  1293. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1294. }
  1295. return 1;
  1296. }
  1297. static int print_trace_fmt(struct trace_iterator *iter)
  1298. {
  1299. struct trace_seq *s = &iter->seq;
  1300. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1301. struct trace_entry *entry;
  1302. unsigned long usec_rem;
  1303. unsigned long long t;
  1304. unsigned long secs;
  1305. char *comm;
  1306. int ret;
  1307. int S, T;
  1308. int i;
  1309. entry = iter->ent;
  1310. comm = trace_find_cmdline(iter->ent->pid);
  1311. t = ns2usecs(entry->t);
  1312. usec_rem = do_div(t, 1000000ULL);
  1313. secs = (unsigned long)t;
  1314. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1315. if (!ret)
  1316. return 0;
  1317. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1318. if (!ret)
  1319. return 0;
  1320. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1321. if (!ret)
  1322. return 0;
  1323. switch (entry->type) {
  1324. case TRACE_FN:
  1325. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1326. if (!ret)
  1327. return 0;
  1328. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1329. entry->fn.parent_ip) {
  1330. ret = trace_seq_printf(s, " <-");
  1331. if (!ret)
  1332. return 0;
  1333. if (kretprobed(entry->fn.parent_ip))
  1334. ret = trace_seq_puts(s, KRETPROBE_MSG);
  1335. else
  1336. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1337. sym_flags);
  1338. if (!ret)
  1339. return 0;
  1340. }
  1341. ret = trace_seq_printf(s, "\n");
  1342. if (!ret)
  1343. return 0;
  1344. break;
  1345. case TRACE_CTX:
  1346. case TRACE_WAKE:
  1347. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1348. state_to_char[entry->ctx.prev_state] : 'X';
  1349. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1350. state_to_char[entry->ctx.next_state] : 'X';
  1351. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1352. entry->ctx.prev_pid,
  1353. entry->ctx.prev_prio,
  1354. S,
  1355. entry->type == TRACE_CTX ? "==>" : " +",
  1356. entry->ctx.next_pid,
  1357. entry->ctx.next_prio,
  1358. T);
  1359. if (!ret)
  1360. return 0;
  1361. break;
  1362. case TRACE_SPECIAL:
  1363. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1364. entry->special.arg1,
  1365. entry->special.arg2,
  1366. entry->special.arg3);
  1367. if (!ret)
  1368. return 0;
  1369. break;
  1370. case TRACE_STACK:
  1371. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1372. if (i) {
  1373. ret = trace_seq_puts(s, " <= ");
  1374. if (!ret)
  1375. return 0;
  1376. }
  1377. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1378. sym_flags);
  1379. if (!ret)
  1380. return 0;
  1381. }
  1382. ret = trace_seq_puts(s, "\n");
  1383. if (!ret)
  1384. return 0;
  1385. break;
  1386. }
  1387. return 1;
  1388. }
  1389. static int print_raw_fmt(struct trace_iterator *iter)
  1390. {
  1391. struct trace_seq *s = &iter->seq;
  1392. struct trace_entry *entry;
  1393. int ret;
  1394. int S, T;
  1395. entry = iter->ent;
  1396. ret = trace_seq_printf(s, "%d %d %llu ",
  1397. entry->pid, iter->cpu, entry->t);
  1398. if (!ret)
  1399. return 0;
  1400. switch (entry->type) {
  1401. case TRACE_FN:
  1402. ret = trace_seq_printf(s, "%x %x\n",
  1403. entry->fn.ip, entry->fn.parent_ip);
  1404. if (!ret)
  1405. return 0;
  1406. break;
  1407. case TRACE_CTX:
  1408. case TRACE_WAKE:
  1409. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1410. state_to_char[entry->ctx.prev_state] : 'X';
  1411. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1412. state_to_char[entry->ctx.next_state] : 'X';
  1413. if (entry->type == TRACE_WAKE)
  1414. S = '+';
  1415. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1416. entry->ctx.prev_pid,
  1417. entry->ctx.prev_prio,
  1418. S,
  1419. entry->ctx.next_pid,
  1420. entry->ctx.next_prio,
  1421. T);
  1422. if (!ret)
  1423. return 0;
  1424. break;
  1425. case TRACE_SPECIAL:
  1426. case TRACE_STACK:
  1427. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1428. entry->special.arg1,
  1429. entry->special.arg2,
  1430. entry->special.arg3);
  1431. if (!ret)
  1432. return 0;
  1433. break;
  1434. }
  1435. return 1;
  1436. }
  1437. #define SEQ_PUT_FIELD_RET(s, x) \
  1438. do { \
  1439. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1440. return 0; \
  1441. } while (0)
  1442. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1443. do { \
  1444. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1445. return 0; \
  1446. } while (0)
  1447. static int print_hex_fmt(struct trace_iterator *iter)
  1448. {
  1449. struct trace_seq *s = &iter->seq;
  1450. unsigned char newline = '\n';
  1451. struct trace_entry *entry;
  1452. int S, T;
  1453. entry = iter->ent;
  1454. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1455. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1456. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1457. switch (entry->type) {
  1458. case TRACE_FN:
  1459. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1460. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1461. break;
  1462. case TRACE_CTX:
  1463. case TRACE_WAKE:
  1464. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1465. state_to_char[entry->ctx.prev_state] : 'X';
  1466. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1467. state_to_char[entry->ctx.next_state] : 'X';
  1468. if (entry->type == TRACE_WAKE)
  1469. S = '+';
  1470. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1471. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1472. SEQ_PUT_HEX_FIELD_RET(s, S);
  1473. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1474. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1475. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1476. SEQ_PUT_HEX_FIELD_RET(s, T);
  1477. break;
  1478. case TRACE_SPECIAL:
  1479. case TRACE_STACK:
  1480. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1481. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1482. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1483. break;
  1484. }
  1485. SEQ_PUT_FIELD_RET(s, newline);
  1486. return 1;
  1487. }
  1488. static int print_bin_fmt(struct trace_iterator *iter)
  1489. {
  1490. struct trace_seq *s = &iter->seq;
  1491. struct trace_entry *entry;
  1492. entry = iter->ent;
  1493. SEQ_PUT_FIELD_RET(s, entry->pid);
  1494. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1495. SEQ_PUT_FIELD_RET(s, entry->t);
  1496. switch (entry->type) {
  1497. case TRACE_FN:
  1498. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1499. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1500. break;
  1501. case TRACE_CTX:
  1502. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1503. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1504. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1505. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1506. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1507. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1508. break;
  1509. case TRACE_SPECIAL:
  1510. case TRACE_STACK:
  1511. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1512. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1513. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1514. break;
  1515. }
  1516. return 1;
  1517. }
  1518. static int trace_empty(struct trace_iterator *iter)
  1519. {
  1520. struct trace_array_cpu *data;
  1521. int cpu;
  1522. for_each_tracing_cpu(cpu) {
  1523. data = iter->tr->data[cpu];
  1524. if (head_page(data) && data->trace_idx &&
  1525. (data->trace_tail != data->trace_head ||
  1526. data->trace_tail_idx != data->trace_head_idx))
  1527. return 0;
  1528. }
  1529. return 1;
  1530. }
  1531. static int print_trace_line(struct trace_iterator *iter)
  1532. {
  1533. if (iter->trace && iter->trace->print_line)
  1534. return iter->trace->print_line(iter);
  1535. if (trace_flags & TRACE_ITER_BIN)
  1536. return print_bin_fmt(iter);
  1537. if (trace_flags & TRACE_ITER_HEX)
  1538. return print_hex_fmt(iter);
  1539. if (trace_flags & TRACE_ITER_RAW)
  1540. return print_raw_fmt(iter);
  1541. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1542. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1543. return print_trace_fmt(iter);
  1544. }
  1545. static int s_show(struct seq_file *m, void *v)
  1546. {
  1547. struct trace_iterator *iter = v;
  1548. if (iter->ent == NULL) {
  1549. if (iter->tr) {
  1550. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1551. seq_puts(m, "#\n");
  1552. }
  1553. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1554. /* print nothing if the buffers are empty */
  1555. if (trace_empty(iter))
  1556. return 0;
  1557. print_trace_header(m, iter);
  1558. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1559. print_lat_help_header(m);
  1560. } else {
  1561. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1562. print_func_help_header(m);
  1563. }
  1564. } else {
  1565. print_trace_line(iter);
  1566. trace_print_seq(m, &iter->seq);
  1567. }
  1568. return 0;
  1569. }
  1570. static struct seq_operations tracer_seq_ops = {
  1571. .start = s_start,
  1572. .next = s_next,
  1573. .stop = s_stop,
  1574. .show = s_show,
  1575. };
  1576. static struct trace_iterator *
  1577. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1578. {
  1579. struct trace_iterator *iter;
  1580. if (tracing_disabled) {
  1581. *ret = -ENODEV;
  1582. return NULL;
  1583. }
  1584. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1585. if (!iter) {
  1586. *ret = -ENOMEM;
  1587. goto out;
  1588. }
  1589. mutex_lock(&trace_types_lock);
  1590. if (current_trace && current_trace->print_max)
  1591. iter->tr = &max_tr;
  1592. else
  1593. iter->tr = inode->i_private;
  1594. iter->trace = current_trace;
  1595. iter->pos = -1;
  1596. /* TODO stop tracer */
  1597. *ret = seq_open(file, &tracer_seq_ops);
  1598. if (!*ret) {
  1599. struct seq_file *m = file->private_data;
  1600. m->private = iter;
  1601. /* stop the trace while dumping */
  1602. if (iter->tr->ctrl) {
  1603. tracer_enabled = 0;
  1604. ftrace_function_enabled = 0;
  1605. }
  1606. if (iter->trace && iter->trace->open)
  1607. iter->trace->open(iter);
  1608. } else {
  1609. kfree(iter);
  1610. iter = NULL;
  1611. }
  1612. mutex_unlock(&trace_types_lock);
  1613. out:
  1614. return iter;
  1615. }
  1616. int tracing_open_generic(struct inode *inode, struct file *filp)
  1617. {
  1618. if (tracing_disabled)
  1619. return -ENODEV;
  1620. filp->private_data = inode->i_private;
  1621. return 0;
  1622. }
  1623. int tracing_release(struct inode *inode, struct file *file)
  1624. {
  1625. struct seq_file *m = (struct seq_file *)file->private_data;
  1626. struct trace_iterator *iter = m->private;
  1627. mutex_lock(&trace_types_lock);
  1628. if (iter->trace && iter->trace->close)
  1629. iter->trace->close(iter);
  1630. /* reenable tracing if it was previously enabled */
  1631. if (iter->tr->ctrl) {
  1632. tracer_enabled = 1;
  1633. /*
  1634. * It is safe to enable function tracing even if it
  1635. * isn't used
  1636. */
  1637. ftrace_function_enabled = 1;
  1638. }
  1639. mutex_unlock(&trace_types_lock);
  1640. seq_release(inode, file);
  1641. kfree(iter);
  1642. return 0;
  1643. }
  1644. static int tracing_open(struct inode *inode, struct file *file)
  1645. {
  1646. int ret;
  1647. __tracing_open(inode, file, &ret);
  1648. return ret;
  1649. }
  1650. static int tracing_lt_open(struct inode *inode, struct file *file)
  1651. {
  1652. struct trace_iterator *iter;
  1653. int ret;
  1654. iter = __tracing_open(inode, file, &ret);
  1655. if (!ret)
  1656. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1657. return ret;
  1658. }
  1659. static void *
  1660. t_next(struct seq_file *m, void *v, loff_t *pos)
  1661. {
  1662. struct tracer *t = m->private;
  1663. (*pos)++;
  1664. if (t)
  1665. t = t->next;
  1666. m->private = t;
  1667. return t;
  1668. }
  1669. static void *t_start(struct seq_file *m, loff_t *pos)
  1670. {
  1671. struct tracer *t = m->private;
  1672. loff_t l = 0;
  1673. mutex_lock(&trace_types_lock);
  1674. for (; t && l < *pos; t = t_next(m, t, &l))
  1675. ;
  1676. return t;
  1677. }
  1678. static void t_stop(struct seq_file *m, void *p)
  1679. {
  1680. mutex_unlock(&trace_types_lock);
  1681. }
  1682. static int t_show(struct seq_file *m, void *v)
  1683. {
  1684. struct tracer *t = v;
  1685. if (!t)
  1686. return 0;
  1687. seq_printf(m, "%s", t->name);
  1688. if (t->next)
  1689. seq_putc(m, ' ');
  1690. else
  1691. seq_putc(m, '\n');
  1692. return 0;
  1693. }
  1694. static struct seq_operations show_traces_seq_ops = {
  1695. .start = t_start,
  1696. .next = t_next,
  1697. .stop = t_stop,
  1698. .show = t_show,
  1699. };
  1700. static int show_traces_open(struct inode *inode, struct file *file)
  1701. {
  1702. int ret;
  1703. if (tracing_disabled)
  1704. return -ENODEV;
  1705. ret = seq_open(file, &show_traces_seq_ops);
  1706. if (!ret) {
  1707. struct seq_file *m = file->private_data;
  1708. m->private = trace_types;
  1709. }
  1710. return ret;
  1711. }
  1712. static struct file_operations tracing_fops = {
  1713. .open = tracing_open,
  1714. .read = seq_read,
  1715. .llseek = seq_lseek,
  1716. .release = tracing_release,
  1717. };
  1718. static struct file_operations tracing_lt_fops = {
  1719. .open = tracing_lt_open,
  1720. .read = seq_read,
  1721. .llseek = seq_lseek,
  1722. .release = tracing_release,
  1723. };
  1724. static struct file_operations show_traces_fops = {
  1725. .open = show_traces_open,
  1726. .read = seq_read,
  1727. .release = seq_release,
  1728. };
  1729. /*
  1730. * Only trace on a CPU if the bitmask is set:
  1731. */
  1732. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1733. /*
  1734. * When tracing/tracing_cpu_mask is modified then this holds
  1735. * the new bitmask we are about to install:
  1736. */
  1737. static cpumask_t tracing_cpumask_new;
  1738. /*
  1739. * The tracer itself will not take this lock, but still we want
  1740. * to provide a consistent cpumask to user-space:
  1741. */
  1742. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1743. /*
  1744. * Temporary storage for the character representation of the
  1745. * CPU bitmask (and one more byte for the newline):
  1746. */
  1747. static char mask_str[NR_CPUS + 1];
  1748. static ssize_t
  1749. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1750. size_t count, loff_t *ppos)
  1751. {
  1752. int len;
  1753. mutex_lock(&tracing_cpumask_update_lock);
  1754. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1755. if (count - len < 2) {
  1756. count = -EINVAL;
  1757. goto out_err;
  1758. }
  1759. len += sprintf(mask_str + len, "\n");
  1760. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1761. out_err:
  1762. mutex_unlock(&tracing_cpumask_update_lock);
  1763. return count;
  1764. }
  1765. static ssize_t
  1766. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1767. size_t count, loff_t *ppos)
  1768. {
  1769. int err, cpu;
  1770. mutex_lock(&tracing_cpumask_update_lock);
  1771. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1772. if (err)
  1773. goto err_unlock;
  1774. raw_local_irq_disable();
  1775. __raw_spin_lock(&ftrace_max_lock);
  1776. for_each_tracing_cpu(cpu) {
  1777. /*
  1778. * Increase/decrease the disabled counter if we are
  1779. * about to flip a bit in the cpumask:
  1780. */
  1781. if (cpu_isset(cpu, tracing_cpumask) &&
  1782. !cpu_isset(cpu, tracing_cpumask_new)) {
  1783. atomic_inc(&global_trace.data[cpu]->disabled);
  1784. }
  1785. if (!cpu_isset(cpu, tracing_cpumask) &&
  1786. cpu_isset(cpu, tracing_cpumask_new)) {
  1787. atomic_dec(&global_trace.data[cpu]->disabled);
  1788. }
  1789. }
  1790. __raw_spin_unlock(&ftrace_max_lock);
  1791. raw_local_irq_enable();
  1792. tracing_cpumask = tracing_cpumask_new;
  1793. mutex_unlock(&tracing_cpumask_update_lock);
  1794. return count;
  1795. err_unlock:
  1796. mutex_unlock(&tracing_cpumask_update_lock);
  1797. return err;
  1798. }
  1799. static struct file_operations tracing_cpumask_fops = {
  1800. .open = tracing_open_generic,
  1801. .read = tracing_cpumask_read,
  1802. .write = tracing_cpumask_write,
  1803. };
  1804. static ssize_t
  1805. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1806. size_t cnt, loff_t *ppos)
  1807. {
  1808. char *buf;
  1809. int r = 0;
  1810. int len = 0;
  1811. int i;
  1812. /* calulate max size */
  1813. for (i = 0; trace_options[i]; i++) {
  1814. len += strlen(trace_options[i]);
  1815. len += 3; /* "no" and space */
  1816. }
  1817. /* +2 for \n and \0 */
  1818. buf = kmalloc(len + 2, GFP_KERNEL);
  1819. if (!buf)
  1820. return -ENOMEM;
  1821. for (i = 0; trace_options[i]; i++) {
  1822. if (trace_flags & (1 << i))
  1823. r += sprintf(buf + r, "%s ", trace_options[i]);
  1824. else
  1825. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1826. }
  1827. r += sprintf(buf + r, "\n");
  1828. WARN_ON(r >= len + 2);
  1829. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1830. kfree(buf);
  1831. return r;
  1832. }
  1833. static ssize_t
  1834. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1835. size_t cnt, loff_t *ppos)
  1836. {
  1837. char buf[64];
  1838. char *cmp = buf;
  1839. int neg = 0;
  1840. int i;
  1841. if (cnt >= sizeof(buf))
  1842. return -EINVAL;
  1843. if (copy_from_user(&buf, ubuf, cnt))
  1844. return -EFAULT;
  1845. buf[cnt] = 0;
  1846. if (strncmp(buf, "no", 2) == 0) {
  1847. neg = 1;
  1848. cmp += 2;
  1849. }
  1850. for (i = 0; trace_options[i]; i++) {
  1851. int len = strlen(trace_options[i]);
  1852. if (strncmp(cmp, trace_options[i], len) == 0) {
  1853. if (neg)
  1854. trace_flags &= ~(1 << i);
  1855. else
  1856. trace_flags |= (1 << i);
  1857. break;
  1858. }
  1859. }
  1860. /*
  1861. * If no option could be set, return an error:
  1862. */
  1863. if (!trace_options[i])
  1864. return -EINVAL;
  1865. filp->f_pos += cnt;
  1866. return cnt;
  1867. }
  1868. static struct file_operations tracing_iter_fops = {
  1869. .open = tracing_open_generic,
  1870. .read = tracing_iter_ctrl_read,
  1871. .write = tracing_iter_ctrl_write,
  1872. };
  1873. static const char readme_msg[] =
  1874. "tracing mini-HOWTO:\n\n"
  1875. "# mkdir /debug\n"
  1876. "# mount -t debugfs nodev /debug\n\n"
  1877. "# cat /debug/tracing/available_tracers\n"
  1878. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1879. "# cat /debug/tracing/current_tracer\n"
  1880. "none\n"
  1881. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1882. "# cat /debug/tracing/current_tracer\n"
  1883. "sched_switch\n"
  1884. "# cat /debug/tracing/iter_ctrl\n"
  1885. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1886. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1887. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1888. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1889. "echo 0 > /debug/tracing/tracing_enabled\n"
  1890. ;
  1891. static ssize_t
  1892. tracing_readme_read(struct file *filp, char __user *ubuf,
  1893. size_t cnt, loff_t *ppos)
  1894. {
  1895. return simple_read_from_buffer(ubuf, cnt, ppos,
  1896. readme_msg, strlen(readme_msg));
  1897. }
  1898. static struct file_operations tracing_readme_fops = {
  1899. .open = tracing_open_generic,
  1900. .read = tracing_readme_read,
  1901. };
  1902. static ssize_t
  1903. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1904. size_t cnt, loff_t *ppos)
  1905. {
  1906. struct trace_array *tr = filp->private_data;
  1907. char buf[64];
  1908. int r;
  1909. r = sprintf(buf, "%ld\n", tr->ctrl);
  1910. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1911. }
  1912. static ssize_t
  1913. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1914. size_t cnt, loff_t *ppos)
  1915. {
  1916. struct trace_array *tr = filp->private_data;
  1917. char buf[64];
  1918. long val;
  1919. int ret;
  1920. if (cnt >= sizeof(buf))
  1921. return -EINVAL;
  1922. if (copy_from_user(&buf, ubuf, cnt))
  1923. return -EFAULT;
  1924. buf[cnt] = 0;
  1925. ret = strict_strtoul(buf, 10, &val);
  1926. if (ret < 0)
  1927. return ret;
  1928. val = !!val;
  1929. mutex_lock(&trace_types_lock);
  1930. if (tr->ctrl ^ val) {
  1931. if (val)
  1932. tracer_enabled = 1;
  1933. else
  1934. tracer_enabled = 0;
  1935. tr->ctrl = val;
  1936. if (current_trace && current_trace->ctrl_update)
  1937. current_trace->ctrl_update(tr);
  1938. }
  1939. mutex_unlock(&trace_types_lock);
  1940. filp->f_pos += cnt;
  1941. return cnt;
  1942. }
  1943. static ssize_t
  1944. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1945. size_t cnt, loff_t *ppos)
  1946. {
  1947. char buf[max_tracer_type_len+2];
  1948. int r;
  1949. mutex_lock(&trace_types_lock);
  1950. if (current_trace)
  1951. r = sprintf(buf, "%s\n", current_trace->name);
  1952. else
  1953. r = sprintf(buf, "\n");
  1954. mutex_unlock(&trace_types_lock);
  1955. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1956. }
  1957. static ssize_t
  1958. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1959. size_t cnt, loff_t *ppos)
  1960. {
  1961. struct trace_array *tr = &global_trace;
  1962. struct tracer *t;
  1963. char buf[max_tracer_type_len+1];
  1964. int i;
  1965. if (cnt > max_tracer_type_len)
  1966. cnt = max_tracer_type_len;
  1967. if (copy_from_user(&buf, ubuf, cnt))
  1968. return -EFAULT;
  1969. buf[cnt] = 0;
  1970. /* strip ending whitespace. */
  1971. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1972. buf[i] = 0;
  1973. mutex_lock(&trace_types_lock);
  1974. for (t = trace_types; t; t = t->next) {
  1975. if (strcmp(t->name, buf) == 0)
  1976. break;
  1977. }
  1978. if (!t || t == current_trace)
  1979. goto out;
  1980. if (current_trace && current_trace->reset)
  1981. current_trace->reset(tr);
  1982. current_trace = t;
  1983. if (t->init)
  1984. t->init(tr);
  1985. out:
  1986. mutex_unlock(&trace_types_lock);
  1987. filp->f_pos += cnt;
  1988. return cnt;
  1989. }
  1990. static ssize_t
  1991. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1992. size_t cnt, loff_t *ppos)
  1993. {
  1994. unsigned long *ptr = filp->private_data;
  1995. char buf[64];
  1996. int r;
  1997. r = snprintf(buf, sizeof(buf), "%ld\n",
  1998. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1999. if (r > sizeof(buf))
  2000. r = sizeof(buf);
  2001. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2002. }
  2003. static ssize_t
  2004. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2005. size_t cnt, loff_t *ppos)
  2006. {
  2007. long *ptr = filp->private_data;
  2008. char buf[64];
  2009. long val;
  2010. int ret;
  2011. if (cnt >= sizeof(buf))
  2012. return -EINVAL;
  2013. if (copy_from_user(&buf, ubuf, cnt))
  2014. return -EFAULT;
  2015. buf[cnt] = 0;
  2016. ret = strict_strtoul(buf, 10, &val);
  2017. if (ret < 0)
  2018. return ret;
  2019. *ptr = val * 1000;
  2020. return cnt;
  2021. }
  2022. static atomic_t tracing_reader;
  2023. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2024. {
  2025. struct trace_iterator *iter;
  2026. if (tracing_disabled)
  2027. return -ENODEV;
  2028. /* We only allow for reader of the pipe */
  2029. if (atomic_inc_return(&tracing_reader) != 1) {
  2030. atomic_dec(&tracing_reader);
  2031. return -EBUSY;
  2032. }
  2033. /* create a buffer to store the information to pass to userspace */
  2034. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2035. if (!iter)
  2036. return -ENOMEM;
  2037. mutex_lock(&trace_types_lock);
  2038. iter->tr = &global_trace;
  2039. iter->trace = current_trace;
  2040. filp->private_data = iter;
  2041. if (iter->trace->pipe_open)
  2042. iter->trace->pipe_open(iter);
  2043. mutex_unlock(&trace_types_lock);
  2044. return 0;
  2045. }
  2046. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2047. {
  2048. struct trace_iterator *iter = file->private_data;
  2049. kfree(iter);
  2050. atomic_dec(&tracing_reader);
  2051. return 0;
  2052. }
  2053. static unsigned int
  2054. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2055. {
  2056. struct trace_iterator *iter = filp->private_data;
  2057. if (trace_flags & TRACE_ITER_BLOCK) {
  2058. /*
  2059. * Always select as readable when in blocking mode
  2060. */
  2061. return POLLIN | POLLRDNORM;
  2062. } else {
  2063. if (!trace_empty(iter))
  2064. return POLLIN | POLLRDNORM;
  2065. poll_wait(filp, &trace_wait, poll_table);
  2066. if (!trace_empty(iter))
  2067. return POLLIN | POLLRDNORM;
  2068. return 0;
  2069. }
  2070. }
  2071. /*
  2072. * Consumer reader.
  2073. */
  2074. static ssize_t
  2075. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2076. size_t cnt, loff_t *ppos)
  2077. {
  2078. struct trace_iterator *iter = filp->private_data;
  2079. struct trace_array_cpu *data;
  2080. static cpumask_t mask;
  2081. unsigned long flags;
  2082. #ifdef CONFIG_FTRACE
  2083. int ftrace_save;
  2084. #endif
  2085. int cpu;
  2086. ssize_t sret;
  2087. /* return any leftover data */
  2088. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2089. if (sret != -EBUSY)
  2090. return sret;
  2091. sret = 0;
  2092. trace_seq_reset(&iter->seq);
  2093. mutex_lock(&trace_types_lock);
  2094. if (iter->trace->read) {
  2095. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2096. if (sret)
  2097. goto out;
  2098. }
  2099. while (trace_empty(iter)) {
  2100. if ((filp->f_flags & O_NONBLOCK)) {
  2101. sret = -EAGAIN;
  2102. goto out;
  2103. }
  2104. /*
  2105. * This is a make-shift waitqueue. The reason we don't use
  2106. * an actual wait queue is because:
  2107. * 1) we only ever have one waiter
  2108. * 2) the tracing, traces all functions, we don't want
  2109. * the overhead of calling wake_up and friends
  2110. * (and tracing them too)
  2111. * Anyway, this is really very primitive wakeup.
  2112. */
  2113. set_current_state(TASK_INTERRUPTIBLE);
  2114. iter->tr->waiter = current;
  2115. mutex_unlock(&trace_types_lock);
  2116. /* sleep for 100 msecs, and try again. */
  2117. schedule_timeout(HZ/10);
  2118. mutex_lock(&trace_types_lock);
  2119. iter->tr->waiter = NULL;
  2120. if (signal_pending(current)) {
  2121. sret = -EINTR;
  2122. goto out;
  2123. }
  2124. if (iter->trace != current_trace)
  2125. goto out;
  2126. /*
  2127. * We block until we read something and tracing is disabled.
  2128. * We still block if tracing is disabled, but we have never
  2129. * read anything. This allows a user to cat this file, and
  2130. * then enable tracing. But after we have read something,
  2131. * we give an EOF when tracing is again disabled.
  2132. *
  2133. * iter->pos will be 0 if we haven't read anything.
  2134. */
  2135. if (!tracer_enabled && iter->pos)
  2136. break;
  2137. continue;
  2138. }
  2139. /* stop when tracing is finished */
  2140. if (trace_empty(iter))
  2141. goto out;
  2142. if (cnt >= PAGE_SIZE)
  2143. cnt = PAGE_SIZE - 1;
  2144. /* reset all but tr, trace, and overruns */
  2145. memset(&iter->seq, 0,
  2146. sizeof(struct trace_iterator) -
  2147. offsetof(struct trace_iterator, seq));
  2148. iter->pos = -1;
  2149. /*
  2150. * We need to stop all tracing on all CPUS to read the
  2151. * the next buffer. This is a bit expensive, but is
  2152. * not done often. We fill all what we can read,
  2153. * and then release the locks again.
  2154. */
  2155. cpus_clear(mask);
  2156. local_irq_save(flags);
  2157. #ifdef CONFIG_FTRACE
  2158. ftrace_save = ftrace_enabled;
  2159. ftrace_enabled = 0;
  2160. #endif
  2161. smp_wmb();
  2162. for_each_tracing_cpu(cpu) {
  2163. data = iter->tr->data[cpu];
  2164. if (!head_page(data) || !data->trace_idx)
  2165. continue;
  2166. atomic_inc(&data->disabled);
  2167. cpu_set(cpu, mask);
  2168. }
  2169. for_each_cpu_mask(cpu, mask) {
  2170. data = iter->tr->data[cpu];
  2171. __raw_spin_lock(&data->lock);
  2172. if (data->overrun > iter->last_overrun[cpu])
  2173. iter->overrun[cpu] +=
  2174. data->overrun - iter->last_overrun[cpu];
  2175. iter->last_overrun[cpu] = data->overrun;
  2176. }
  2177. while (find_next_entry_inc(iter) != NULL) {
  2178. int ret;
  2179. int len = iter->seq.len;
  2180. ret = print_trace_line(iter);
  2181. if (!ret) {
  2182. /* don't print partial lines */
  2183. iter->seq.len = len;
  2184. break;
  2185. }
  2186. trace_consume(iter);
  2187. if (iter->seq.len >= cnt)
  2188. break;
  2189. }
  2190. for_each_cpu_mask(cpu, mask) {
  2191. data = iter->tr->data[cpu];
  2192. __raw_spin_unlock(&data->lock);
  2193. }
  2194. for_each_cpu_mask(cpu, mask) {
  2195. data = iter->tr->data[cpu];
  2196. atomic_dec(&data->disabled);
  2197. }
  2198. #ifdef CONFIG_FTRACE
  2199. ftrace_enabled = ftrace_save;
  2200. #endif
  2201. local_irq_restore(flags);
  2202. /* Now copy what we have to the user */
  2203. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2204. if (iter->seq.readpos >= iter->seq.len)
  2205. trace_seq_reset(&iter->seq);
  2206. if (sret == -EBUSY)
  2207. sret = 0;
  2208. out:
  2209. mutex_unlock(&trace_types_lock);
  2210. return sret;
  2211. }
  2212. static ssize_t
  2213. tracing_entries_read(struct file *filp, char __user *ubuf,
  2214. size_t cnt, loff_t *ppos)
  2215. {
  2216. struct trace_array *tr = filp->private_data;
  2217. char buf[64];
  2218. int r;
  2219. r = sprintf(buf, "%lu\n", tr->entries);
  2220. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2221. }
  2222. static ssize_t
  2223. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2224. size_t cnt, loff_t *ppos)
  2225. {
  2226. unsigned long val;
  2227. char buf[64];
  2228. int i, ret;
  2229. if (cnt >= sizeof(buf))
  2230. return -EINVAL;
  2231. if (copy_from_user(&buf, ubuf, cnt))
  2232. return -EFAULT;
  2233. buf[cnt] = 0;
  2234. ret = strict_strtoul(buf, 10, &val);
  2235. if (ret < 0)
  2236. return ret;
  2237. /* must have at least 1 entry */
  2238. if (!val)
  2239. return -EINVAL;
  2240. mutex_lock(&trace_types_lock);
  2241. if (current_trace != &no_tracer) {
  2242. cnt = -EBUSY;
  2243. pr_info("ftrace: set current_tracer to none"
  2244. " before modifying buffer size\n");
  2245. goto out;
  2246. }
  2247. if (val > global_trace.entries) {
  2248. long pages_requested;
  2249. unsigned long freeable_pages;
  2250. /* make sure we have enough memory before mapping */
  2251. pages_requested =
  2252. (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
  2253. /* account for each buffer (and max_tr) */
  2254. pages_requested *= tracing_nr_buffers * 2;
  2255. /* Check for overflow */
  2256. if (pages_requested < 0) {
  2257. cnt = -ENOMEM;
  2258. goto out;
  2259. }
  2260. freeable_pages = determine_dirtyable_memory();
  2261. /* we only allow to request 1/4 of useable memory */
  2262. if (pages_requested >
  2263. ((freeable_pages + tracing_pages_allocated) / 4)) {
  2264. cnt = -ENOMEM;
  2265. goto out;
  2266. }
  2267. while (global_trace.entries < val) {
  2268. if (trace_alloc_page()) {
  2269. cnt = -ENOMEM;
  2270. goto out;
  2271. }
  2272. /* double check that we don't go over the known pages */
  2273. if (tracing_pages_allocated > pages_requested)
  2274. break;
  2275. }
  2276. } else {
  2277. /* include the number of entries in val (inc of page entries) */
  2278. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  2279. trace_free_page();
  2280. }
  2281. /* check integrity */
  2282. for_each_tracing_cpu(i)
  2283. check_pages(global_trace.data[i]);
  2284. filp->f_pos += cnt;
  2285. /* If check pages failed, return ENOMEM */
  2286. if (tracing_disabled)
  2287. cnt = -ENOMEM;
  2288. out:
  2289. max_tr.entries = global_trace.entries;
  2290. mutex_unlock(&trace_types_lock);
  2291. return cnt;
  2292. }
  2293. static struct file_operations tracing_max_lat_fops = {
  2294. .open = tracing_open_generic,
  2295. .read = tracing_max_lat_read,
  2296. .write = tracing_max_lat_write,
  2297. };
  2298. static struct file_operations tracing_ctrl_fops = {
  2299. .open = tracing_open_generic,
  2300. .read = tracing_ctrl_read,
  2301. .write = tracing_ctrl_write,
  2302. };
  2303. static struct file_operations set_tracer_fops = {
  2304. .open = tracing_open_generic,
  2305. .read = tracing_set_trace_read,
  2306. .write = tracing_set_trace_write,
  2307. };
  2308. static struct file_operations tracing_pipe_fops = {
  2309. .open = tracing_open_pipe,
  2310. .poll = tracing_poll_pipe,
  2311. .read = tracing_read_pipe,
  2312. .release = tracing_release_pipe,
  2313. };
  2314. static struct file_operations tracing_entries_fops = {
  2315. .open = tracing_open_generic,
  2316. .read = tracing_entries_read,
  2317. .write = tracing_entries_write,
  2318. };
  2319. #ifdef CONFIG_DYNAMIC_FTRACE
  2320. static ssize_t
  2321. tracing_read_long(struct file *filp, char __user *ubuf,
  2322. size_t cnt, loff_t *ppos)
  2323. {
  2324. unsigned long *p = filp->private_data;
  2325. char buf[64];
  2326. int r;
  2327. r = sprintf(buf, "%ld\n", *p);
  2328. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2329. }
  2330. static struct file_operations tracing_read_long_fops = {
  2331. .open = tracing_open_generic,
  2332. .read = tracing_read_long,
  2333. };
  2334. #endif
  2335. static struct dentry *d_tracer;
  2336. struct dentry *tracing_init_dentry(void)
  2337. {
  2338. static int once;
  2339. if (d_tracer)
  2340. return d_tracer;
  2341. d_tracer = debugfs_create_dir("tracing", NULL);
  2342. if (!d_tracer && !once) {
  2343. once = 1;
  2344. pr_warning("Could not create debugfs directory 'tracing'\n");
  2345. return NULL;
  2346. }
  2347. return d_tracer;
  2348. }
  2349. #ifdef CONFIG_FTRACE_SELFTEST
  2350. /* Let selftest have access to static functions in this file */
  2351. #include "trace_selftest.c"
  2352. #endif
  2353. static __init void tracer_init_debugfs(void)
  2354. {
  2355. struct dentry *d_tracer;
  2356. struct dentry *entry;
  2357. d_tracer = tracing_init_dentry();
  2358. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2359. &global_trace, &tracing_ctrl_fops);
  2360. if (!entry)
  2361. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2362. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2363. NULL, &tracing_iter_fops);
  2364. if (!entry)
  2365. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2366. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2367. NULL, &tracing_cpumask_fops);
  2368. if (!entry)
  2369. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2370. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2371. &global_trace, &tracing_lt_fops);
  2372. if (!entry)
  2373. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2374. entry = debugfs_create_file("trace", 0444, d_tracer,
  2375. &global_trace, &tracing_fops);
  2376. if (!entry)
  2377. pr_warning("Could not create debugfs 'trace' entry\n");
  2378. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2379. &global_trace, &show_traces_fops);
  2380. if (!entry)
  2381. pr_warning("Could not create debugfs 'trace' entry\n");
  2382. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2383. &global_trace, &set_tracer_fops);
  2384. if (!entry)
  2385. pr_warning("Could not create debugfs 'trace' entry\n");
  2386. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2387. &tracing_max_latency,
  2388. &tracing_max_lat_fops);
  2389. if (!entry)
  2390. pr_warning("Could not create debugfs "
  2391. "'tracing_max_latency' entry\n");
  2392. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2393. &tracing_thresh, &tracing_max_lat_fops);
  2394. if (!entry)
  2395. pr_warning("Could not create debugfs "
  2396. "'tracing_threash' entry\n");
  2397. entry = debugfs_create_file("README", 0644, d_tracer,
  2398. NULL, &tracing_readme_fops);
  2399. if (!entry)
  2400. pr_warning("Could not create debugfs 'README' entry\n");
  2401. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2402. NULL, &tracing_pipe_fops);
  2403. if (!entry)
  2404. pr_warning("Could not create debugfs "
  2405. "'tracing_threash' entry\n");
  2406. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2407. &global_trace, &tracing_entries_fops);
  2408. if (!entry)
  2409. pr_warning("Could not create debugfs "
  2410. "'tracing_threash' entry\n");
  2411. #ifdef CONFIG_DYNAMIC_FTRACE
  2412. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2413. &ftrace_update_tot_cnt,
  2414. &tracing_read_long_fops);
  2415. if (!entry)
  2416. pr_warning("Could not create debugfs "
  2417. "'dyn_ftrace_total_info' entry\n");
  2418. #endif
  2419. #ifdef CONFIG_SYSPROF_TRACER
  2420. init_tracer_sysprof_debugfs(d_tracer);
  2421. #endif
  2422. }
  2423. static int trace_alloc_page(void)
  2424. {
  2425. struct trace_array_cpu *data;
  2426. struct page *page, *tmp;
  2427. LIST_HEAD(pages);
  2428. void *array;
  2429. unsigned pages_allocated = 0;
  2430. int i;
  2431. /* first allocate a page for each CPU */
  2432. for_each_tracing_cpu(i) {
  2433. array = (void *)__get_free_page(GFP_KERNEL);
  2434. if (array == NULL) {
  2435. printk(KERN_ERR "tracer: failed to allocate page"
  2436. "for trace buffer!\n");
  2437. goto free_pages;
  2438. }
  2439. pages_allocated++;
  2440. page = virt_to_page(array);
  2441. list_add(&page->lru, &pages);
  2442. /* Only allocate if we are actually using the max trace */
  2443. #ifdef CONFIG_TRACER_MAX_TRACE
  2444. array = (void *)__get_free_page(GFP_KERNEL);
  2445. if (array == NULL) {
  2446. printk(KERN_ERR "tracer: failed to allocate page"
  2447. "for trace buffer!\n");
  2448. goto free_pages;
  2449. }
  2450. pages_allocated++;
  2451. page = virt_to_page(array);
  2452. list_add(&page->lru, &pages);
  2453. #endif
  2454. }
  2455. /* Now that we successfully allocate a page per CPU, add them */
  2456. for_each_tracing_cpu(i) {
  2457. data = global_trace.data[i];
  2458. page = list_entry(pages.next, struct page, lru);
  2459. list_del_init(&page->lru);
  2460. list_add_tail(&page->lru, &data->trace_pages);
  2461. ClearPageLRU(page);
  2462. #ifdef CONFIG_TRACER_MAX_TRACE
  2463. data = max_tr.data[i];
  2464. page = list_entry(pages.next, struct page, lru);
  2465. list_del_init(&page->lru);
  2466. list_add_tail(&page->lru, &data->trace_pages);
  2467. SetPageLRU(page);
  2468. #endif
  2469. }
  2470. tracing_pages_allocated += pages_allocated;
  2471. global_trace.entries += ENTRIES_PER_PAGE;
  2472. return 0;
  2473. free_pages:
  2474. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2475. list_del_init(&page->lru);
  2476. __free_page(page);
  2477. }
  2478. return -ENOMEM;
  2479. }
  2480. static int trace_free_page(void)
  2481. {
  2482. struct trace_array_cpu *data;
  2483. struct page *page;
  2484. struct list_head *p;
  2485. int i;
  2486. int ret = 0;
  2487. /* free one page from each buffer */
  2488. for_each_tracing_cpu(i) {
  2489. data = global_trace.data[i];
  2490. p = data->trace_pages.next;
  2491. if (p == &data->trace_pages) {
  2492. /* should never happen */
  2493. WARN_ON(1);
  2494. tracing_disabled = 1;
  2495. ret = -1;
  2496. break;
  2497. }
  2498. page = list_entry(p, struct page, lru);
  2499. ClearPageLRU(page);
  2500. list_del(&page->lru);
  2501. tracing_pages_allocated--;
  2502. tracing_pages_allocated--;
  2503. __free_page(page);
  2504. tracing_reset(data);
  2505. #ifdef CONFIG_TRACER_MAX_TRACE
  2506. data = max_tr.data[i];
  2507. p = data->trace_pages.next;
  2508. if (p == &data->trace_pages) {
  2509. /* should never happen */
  2510. WARN_ON(1);
  2511. tracing_disabled = 1;
  2512. ret = -1;
  2513. break;
  2514. }
  2515. page = list_entry(p, struct page, lru);
  2516. ClearPageLRU(page);
  2517. list_del(&page->lru);
  2518. __free_page(page);
  2519. tracing_reset(data);
  2520. #endif
  2521. }
  2522. global_trace.entries -= ENTRIES_PER_PAGE;
  2523. return ret;
  2524. }
  2525. __init static int tracer_alloc_buffers(void)
  2526. {
  2527. struct trace_array_cpu *data;
  2528. void *array;
  2529. struct page *page;
  2530. int pages = 0;
  2531. int ret = -ENOMEM;
  2532. int i;
  2533. /* TODO: make the number of buffers hot pluggable with CPUS */
  2534. tracing_nr_buffers = num_possible_cpus();
  2535. tracing_buffer_mask = cpu_possible_map;
  2536. /* Allocate the first page for all buffers */
  2537. for_each_tracing_cpu(i) {
  2538. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2539. max_tr.data[i] = &per_cpu(max_data, i);
  2540. array = (void *)__get_free_page(GFP_KERNEL);
  2541. if (array == NULL) {
  2542. printk(KERN_ERR "tracer: failed to allocate page"
  2543. "for trace buffer!\n");
  2544. goto free_buffers;
  2545. }
  2546. /* set the array to the list */
  2547. INIT_LIST_HEAD(&data->trace_pages);
  2548. page = virt_to_page(array);
  2549. list_add(&page->lru, &data->trace_pages);
  2550. /* use the LRU flag to differentiate the two buffers */
  2551. ClearPageLRU(page);
  2552. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2553. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2554. /* Only allocate if we are actually using the max trace */
  2555. #ifdef CONFIG_TRACER_MAX_TRACE
  2556. array = (void *)__get_free_page(GFP_KERNEL);
  2557. if (array == NULL) {
  2558. printk(KERN_ERR "tracer: failed to allocate page"
  2559. "for trace buffer!\n");
  2560. goto free_buffers;
  2561. }
  2562. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2563. page = virt_to_page(array);
  2564. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2565. SetPageLRU(page);
  2566. #endif
  2567. }
  2568. /*
  2569. * Since we allocate by orders of pages, we may be able to
  2570. * round up a bit.
  2571. */
  2572. global_trace.entries = ENTRIES_PER_PAGE;
  2573. pages++;
  2574. while (global_trace.entries < trace_nr_entries) {
  2575. if (trace_alloc_page())
  2576. break;
  2577. pages++;
  2578. }
  2579. max_tr.entries = global_trace.entries;
  2580. pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
  2581. pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
  2582. pr_info(" actual entries %ld\n", global_trace.entries);
  2583. tracer_init_debugfs();
  2584. trace_init_cmdlines();
  2585. register_tracer(&no_tracer);
  2586. current_trace = &no_tracer;
  2587. /* All seems OK, enable tracing */
  2588. global_trace.ctrl = tracer_enabled;
  2589. tracing_disabled = 0;
  2590. return 0;
  2591. free_buffers:
  2592. for (i-- ; i >= 0; i--) {
  2593. struct page *page, *tmp;
  2594. struct trace_array_cpu *data = global_trace.data[i];
  2595. if (data) {
  2596. list_for_each_entry_safe(page, tmp,
  2597. &data->trace_pages, lru) {
  2598. list_del_init(&page->lru);
  2599. __free_page(page);
  2600. }
  2601. }
  2602. #ifdef CONFIG_TRACER_MAX_TRACE
  2603. data = max_tr.data[i];
  2604. if (data) {
  2605. list_for_each_entry_safe(page, tmp,
  2606. &data->trace_pages, lru) {
  2607. list_del_init(&page->lru);
  2608. __free_page(page);
  2609. }
  2610. }
  2611. #endif
  2612. }
  2613. return ret;
  2614. }
  2615. fs_initcall(tracer_alloc_buffers);