trace.c 71 KB

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