trace.c 70 KB

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