trace.c 77 KB

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