trace.c 71 KB

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