trace.c 80 KB

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