trace.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012
  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/ring_buffer.h>
  15. #include <linux/utsrelease.h>
  16. #include <linux/stacktrace.h>
  17. #include <linux/writeback.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/notifier.h>
  21. #include <linux/irqflags.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/linkage.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kprobes.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu.h>
  31. #include <linux/splice.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/ctype.h>
  34. #include <linux/init.h>
  35. #include <linux/poll.h>
  36. #include <linux/gfp.h>
  37. #include <linux/fs.h>
  38. #include "trace.h"
  39. #include "trace_output.h"
  40. #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
  41. unsigned long __read_mostly tracing_max_latency;
  42. unsigned long __read_mostly tracing_thresh;
  43. /*
  44. * We need to change this state when a selftest is running.
  45. * A selftest will lurk into the ring-buffer to count the
  46. * entries inserted during the selftest although some concurrent
  47. * insertions into the ring-buffer such as trace_printk could occurred
  48. * at the same time, giving false positive or negative results.
  49. */
  50. static bool __read_mostly tracing_selftest_running;
  51. /*
  52. * If a tracer is running, we do not want to run SELFTEST.
  53. */
  54. static bool __read_mostly tracing_selftest_disabled;
  55. /* For tracers that don't implement custom flags */
  56. static struct tracer_opt dummy_tracer_opt[] = {
  57. { }
  58. };
  59. static struct tracer_flags dummy_tracer_flags = {
  60. .val = 0,
  61. .opts = dummy_tracer_opt
  62. };
  63. static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  64. {
  65. return 0;
  66. }
  67. /*
  68. * Kill all tracing for good (never come back).
  69. * It is initialized to 1 but will turn to zero if the initialization
  70. * of the tracer is successful. But that is the only place that sets
  71. * this back to zero.
  72. */
  73. static int tracing_disabled = 1;
  74. static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
  75. static inline void ftrace_disable_cpu(void)
  76. {
  77. preempt_disable();
  78. local_inc(&__get_cpu_var(ftrace_cpu_disabled));
  79. }
  80. static inline void ftrace_enable_cpu(void)
  81. {
  82. local_dec(&__get_cpu_var(ftrace_cpu_disabled));
  83. preempt_enable();
  84. }
  85. static cpumask_var_t __read_mostly tracing_buffer_mask;
  86. /* Define which cpu buffers are currently read in trace_pipe */
  87. static cpumask_var_t tracing_reader_cpumask;
  88. #define for_each_tracing_cpu(cpu) \
  89. for_each_cpu(cpu, tracing_buffer_mask)
  90. /*
  91. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  92. *
  93. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  94. * is set, then ftrace_dump is called. This will output the contents
  95. * of the ftrace buffers to the console. This is very useful for
  96. * capturing traces that lead to crashes and outputing it to a
  97. * serial console.
  98. *
  99. * It is default off, but you can enable it with either specifying
  100. * "ftrace_dump_on_oops" in the kernel command line, or setting
  101. * /proc/sys/kernel/ftrace_dump_on_oops to true.
  102. */
  103. int ftrace_dump_on_oops;
  104. static int tracing_set_tracer(const char *buf);
  105. #define BOOTUP_TRACER_SIZE 100
  106. static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
  107. static char *default_bootup_tracer;
  108. static int __init set_ftrace(char *str)
  109. {
  110. strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
  111. default_bootup_tracer = bootup_tracer_buf;
  112. return 1;
  113. }
  114. __setup("ftrace=", set_ftrace);
  115. static int __init set_ftrace_dump_on_oops(char *str)
  116. {
  117. ftrace_dump_on_oops = 1;
  118. return 1;
  119. }
  120. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  121. long
  122. ns2usecs(cycle_t nsec)
  123. {
  124. nsec += 500;
  125. do_div(nsec, 1000);
  126. return nsec;
  127. }
  128. cycle_t ftrace_now(int cpu)
  129. {
  130. u64 ts = ring_buffer_time_stamp(cpu);
  131. ring_buffer_normalize_time_stamp(cpu, &ts);
  132. return ts;
  133. }
  134. /*
  135. * The global_trace is the descriptor that holds the tracing
  136. * buffers for the live tracing. For each CPU, it contains
  137. * a link list of pages that will store trace entries. The
  138. * page descriptor of the pages in the memory is used to hold
  139. * the link list by linking the lru item in the page descriptor
  140. * to each of the pages in the buffer per CPU.
  141. *
  142. * For each active CPU there is a data field that holds the
  143. * pages for the buffer for that CPU. Each CPU has the same number
  144. * of pages allocated for its buffer.
  145. */
  146. static struct trace_array global_trace;
  147. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  148. /*
  149. * The max_tr is used to snapshot the global_trace when a maximum
  150. * latency is reached. Some tracers will use this to store a maximum
  151. * trace while it continues examining live traces.
  152. *
  153. * The buffers for the max_tr are set up the same as the global_trace.
  154. * When a snapshot is taken, the link list of the max_tr is swapped
  155. * with the link list of the global_trace and the buffers are reset for
  156. * the global_trace so the tracing can continue.
  157. */
  158. static struct trace_array max_tr;
  159. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  160. /* tracer_enabled is used to toggle activation of a tracer */
  161. static int tracer_enabled = 1;
  162. /**
  163. * tracing_is_enabled - return tracer_enabled status
  164. *
  165. * This function is used by other tracers to know the status
  166. * of the tracer_enabled flag. Tracers may use this function
  167. * to know if it should enable their features when starting
  168. * up. See irqsoff tracer for an example (start_irqsoff_tracer).
  169. */
  170. int tracing_is_enabled(void)
  171. {
  172. return tracer_enabled;
  173. }
  174. /*
  175. * trace_buf_size is the size in bytes that is allocated
  176. * for a buffer. Note, the number of bytes is always rounded
  177. * to page size.
  178. *
  179. * This number is purposely set to a low number of 16384.
  180. * If the dump on oops happens, it will be much appreciated
  181. * to not have to wait for all that output. Anyway this can be
  182. * boot time and run time configurable.
  183. */
  184. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  185. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  186. /* trace_types holds a link list of available tracers. */
  187. static struct tracer *trace_types __read_mostly;
  188. /* current_trace points to the tracer that is currently active */
  189. static struct tracer *current_trace __read_mostly;
  190. /*
  191. * max_tracer_type_len is used to simplify the allocating of
  192. * buffers to read userspace tracer names. We keep track of
  193. * the longest tracer name registered.
  194. */
  195. static int max_tracer_type_len;
  196. /*
  197. * trace_types_lock is used to protect the trace_types list.
  198. * This lock is also used to keep user access serialized.
  199. * Accesses from userspace will grab this lock while userspace
  200. * activities happen inside the kernel.
  201. */
  202. static DEFINE_MUTEX(trace_types_lock);
  203. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  204. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  205. /* trace_flags holds trace_options default values */
  206. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
  207. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
  208. /**
  209. * trace_wake_up - wake up tasks waiting for trace input
  210. *
  211. * Simply wakes up any task that is blocked on the trace_wait
  212. * queue. These is used with trace_poll for tasks polling the trace.
  213. */
  214. void trace_wake_up(void)
  215. {
  216. /*
  217. * The runqueue_is_locked() can fail, but this is the best we
  218. * have for now:
  219. */
  220. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  221. wake_up(&trace_wait);
  222. }
  223. static int __init set_buf_size(char *str)
  224. {
  225. unsigned long buf_size;
  226. int ret;
  227. if (!str)
  228. return 0;
  229. ret = strict_strtoul(str, 0, &buf_size);
  230. /* nr_entries can not be zero */
  231. if (ret < 0 || buf_size == 0)
  232. return 0;
  233. trace_buf_size = buf_size;
  234. return 1;
  235. }
  236. __setup("trace_buf_size=", set_buf_size);
  237. unsigned long nsecs_to_usecs(unsigned long nsecs)
  238. {
  239. return nsecs / 1000;
  240. }
  241. /* These must match the bit postions in trace_iterator_flags */
  242. static const char *trace_options[] = {
  243. "print-parent",
  244. "sym-offset",
  245. "sym-addr",
  246. "verbose",
  247. "raw",
  248. "hex",
  249. "bin",
  250. "block",
  251. "stacktrace",
  252. "sched-tree",
  253. "trace_printk",
  254. "ftrace_preempt",
  255. "branch",
  256. "annotate",
  257. "userstacktrace",
  258. "sym-userobj",
  259. "printk-msg-only",
  260. "context-info",
  261. "latency-format",
  262. NULL
  263. };
  264. /*
  265. * ftrace_max_lock is used to protect the swapping of buffers
  266. * when taking a max snapshot. The buffers themselves are
  267. * protected by per_cpu spinlocks. But the action of the swap
  268. * needs its own lock.
  269. *
  270. * This is defined as a raw_spinlock_t in order to help
  271. * with performance when lockdep debugging is enabled.
  272. */
  273. static raw_spinlock_t ftrace_max_lock =
  274. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  275. /*
  276. * Copy the new maximum trace into the separate maximum-trace
  277. * structure. (this way the maximum trace is permanently saved,
  278. * for later retrieval via /debugfs/tracing/latency_trace)
  279. */
  280. static void
  281. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  282. {
  283. struct trace_array_cpu *data = tr->data[cpu];
  284. max_tr.cpu = cpu;
  285. max_tr.time_start = data->preempt_timestamp;
  286. data = max_tr.data[cpu];
  287. data->saved_latency = tracing_max_latency;
  288. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  289. data->pid = tsk->pid;
  290. data->uid = task_uid(tsk);
  291. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  292. data->policy = tsk->policy;
  293. data->rt_priority = tsk->rt_priority;
  294. /* record this tasks comm */
  295. tracing_record_cmdline(tsk);
  296. }
  297. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  298. {
  299. int len;
  300. int ret;
  301. if (!cnt)
  302. return 0;
  303. if (s->len <= s->readpos)
  304. return -EBUSY;
  305. len = s->len - s->readpos;
  306. if (cnt > len)
  307. cnt = len;
  308. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  309. if (ret == cnt)
  310. return -EFAULT;
  311. cnt -= ret;
  312. s->readpos += cnt;
  313. return cnt;
  314. }
  315. ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  316. {
  317. int len;
  318. void *ret;
  319. if (s->len <= s->readpos)
  320. return -EBUSY;
  321. len = s->len - s->readpos;
  322. if (cnt > len)
  323. cnt = len;
  324. ret = memcpy(buf, s->buffer + s->readpos, cnt);
  325. if (!ret)
  326. return -EFAULT;
  327. s->readpos += cnt;
  328. return cnt;
  329. }
  330. static void
  331. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  332. {
  333. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  334. s->buffer[len] = 0;
  335. seq_puts(m, s->buffer);
  336. trace_seq_init(s);
  337. }
  338. /**
  339. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  340. * @tr: tracer
  341. * @tsk: the task with the latency
  342. * @cpu: The cpu that initiated the trace.
  343. *
  344. * Flip the buffers between the @tr and the max_tr and record information
  345. * about which task was the cause of this latency.
  346. */
  347. void
  348. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  349. {
  350. struct ring_buffer *buf = tr->buffer;
  351. WARN_ON_ONCE(!irqs_disabled());
  352. __raw_spin_lock(&ftrace_max_lock);
  353. tr->buffer = max_tr.buffer;
  354. max_tr.buffer = buf;
  355. ftrace_disable_cpu();
  356. ring_buffer_reset(tr->buffer);
  357. ftrace_enable_cpu();
  358. __update_max_tr(tr, tsk, cpu);
  359. __raw_spin_unlock(&ftrace_max_lock);
  360. }
  361. /**
  362. * update_max_tr_single - only copy one trace over, and reset the rest
  363. * @tr - tracer
  364. * @tsk - task with the latency
  365. * @cpu - the cpu of the buffer to copy.
  366. *
  367. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  368. */
  369. void
  370. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  371. {
  372. int ret;
  373. WARN_ON_ONCE(!irqs_disabled());
  374. __raw_spin_lock(&ftrace_max_lock);
  375. ftrace_disable_cpu();
  376. ring_buffer_reset(max_tr.buffer);
  377. ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
  378. ftrace_enable_cpu();
  379. WARN_ON_ONCE(ret && ret != -EAGAIN);
  380. __update_max_tr(tr, tsk, cpu);
  381. __raw_spin_unlock(&ftrace_max_lock);
  382. }
  383. /**
  384. * register_tracer - register a tracer with the ftrace system.
  385. * @type - the plugin for the tracer
  386. *
  387. * Register a new plugin tracer.
  388. */
  389. int register_tracer(struct tracer *type)
  390. __releases(kernel_lock)
  391. __acquires(kernel_lock)
  392. {
  393. struct tracer *t;
  394. int len;
  395. int ret = 0;
  396. if (!type->name) {
  397. pr_info("Tracer must have a name\n");
  398. return -1;
  399. }
  400. /*
  401. * When this gets called we hold the BKL which means that
  402. * preemption is disabled. Various trace selftests however
  403. * need to disable and enable preemption for successful tests.
  404. * So we drop the BKL here and grab it after the tests again.
  405. */
  406. unlock_kernel();
  407. mutex_lock(&trace_types_lock);
  408. tracing_selftest_running = true;
  409. for (t = trace_types; t; t = t->next) {
  410. if (strcmp(type->name, t->name) == 0) {
  411. /* already found */
  412. pr_info("Trace %s already registered\n",
  413. type->name);
  414. ret = -1;
  415. goto out;
  416. }
  417. }
  418. if (!type->set_flag)
  419. type->set_flag = &dummy_set_flag;
  420. if (!type->flags)
  421. type->flags = &dummy_tracer_flags;
  422. else
  423. if (!type->flags->opts)
  424. type->flags->opts = dummy_tracer_opt;
  425. if (!type->wait_pipe)
  426. type->wait_pipe = default_wait_pipe;
  427. #ifdef CONFIG_FTRACE_STARTUP_TEST
  428. if (type->selftest && !tracing_selftest_disabled) {
  429. struct tracer *saved_tracer = current_trace;
  430. struct trace_array *tr = &global_trace;
  431. int i;
  432. /*
  433. * Run a selftest on this tracer.
  434. * Here we reset the trace buffer, and set the current
  435. * tracer to be this tracer. The tracer can then run some
  436. * internal tracing to verify that everything is in order.
  437. * If we fail, we do not register this tracer.
  438. */
  439. for_each_tracing_cpu(i)
  440. tracing_reset(tr, i);
  441. current_trace = type;
  442. /* the test is responsible for initializing and enabling */
  443. pr_info("Testing tracer %s: ", type->name);
  444. ret = type->selftest(type, tr);
  445. /* the test is responsible for resetting too */
  446. current_trace = saved_tracer;
  447. if (ret) {
  448. printk(KERN_CONT "FAILED!\n");
  449. goto out;
  450. }
  451. /* Only reset on passing, to avoid touching corrupted buffers */
  452. for_each_tracing_cpu(i)
  453. tracing_reset(tr, i);
  454. printk(KERN_CONT "PASSED\n");
  455. }
  456. #endif
  457. type->next = trace_types;
  458. trace_types = type;
  459. len = strlen(type->name);
  460. if (len > max_tracer_type_len)
  461. max_tracer_type_len = len;
  462. out:
  463. tracing_selftest_running = false;
  464. mutex_unlock(&trace_types_lock);
  465. if (ret || !default_bootup_tracer)
  466. goto out_unlock;
  467. if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
  468. goto out_unlock;
  469. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  470. /* Do we want this tracer to start on bootup? */
  471. tracing_set_tracer(type->name);
  472. default_bootup_tracer = NULL;
  473. /* disable other selftests, since this will break it. */
  474. tracing_selftest_disabled = 1;
  475. #ifdef CONFIG_FTRACE_STARTUP_TEST
  476. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  477. type->name);
  478. #endif
  479. out_unlock:
  480. lock_kernel();
  481. return ret;
  482. }
  483. void unregister_tracer(struct tracer *type)
  484. {
  485. struct tracer **t;
  486. int len;
  487. mutex_lock(&trace_types_lock);
  488. for (t = &trace_types; *t; t = &(*t)->next) {
  489. if (*t == type)
  490. goto found;
  491. }
  492. pr_info("Trace %s not registered\n", type->name);
  493. goto out;
  494. found:
  495. *t = (*t)->next;
  496. if (type == current_trace && tracer_enabled) {
  497. tracer_enabled = 0;
  498. tracing_stop();
  499. if (current_trace->stop)
  500. current_trace->stop(&global_trace);
  501. current_trace = &nop_trace;
  502. }
  503. if (strlen(type->name) != max_tracer_type_len)
  504. goto out;
  505. max_tracer_type_len = 0;
  506. for (t = &trace_types; *t; t = &(*t)->next) {
  507. len = strlen((*t)->name);
  508. if (len > max_tracer_type_len)
  509. max_tracer_type_len = len;
  510. }
  511. out:
  512. mutex_unlock(&trace_types_lock);
  513. }
  514. void tracing_reset(struct trace_array *tr, int cpu)
  515. {
  516. ftrace_disable_cpu();
  517. ring_buffer_reset_cpu(tr->buffer, cpu);
  518. ftrace_enable_cpu();
  519. }
  520. void tracing_reset_online_cpus(struct trace_array *tr)
  521. {
  522. int cpu;
  523. tr->time_start = ftrace_now(tr->cpu);
  524. for_each_online_cpu(cpu)
  525. tracing_reset(tr, cpu);
  526. }
  527. #define SAVED_CMDLINES 128
  528. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  529. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  530. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  531. static int cmdline_idx;
  532. static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
  533. /* temporary disable recording */
  534. static atomic_t trace_record_cmdline_disabled __read_mostly;
  535. static void trace_init_cmdlines(void)
  536. {
  537. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  538. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  539. cmdline_idx = 0;
  540. }
  541. static int trace_stop_count;
  542. static DEFINE_SPINLOCK(tracing_start_lock);
  543. /**
  544. * ftrace_off_permanent - disable all ftrace code permanently
  545. *
  546. * This should only be called when a serious anomally has
  547. * been detected. This will turn off the function tracing,
  548. * ring buffers, and other tracing utilites. It takes no
  549. * locks and can be called from any context.
  550. */
  551. void ftrace_off_permanent(void)
  552. {
  553. tracing_disabled = 1;
  554. ftrace_stop();
  555. tracing_off_permanent();
  556. }
  557. /**
  558. * tracing_start - quick start of the tracer
  559. *
  560. * If tracing is enabled but was stopped by tracing_stop,
  561. * this will start the tracer back up.
  562. */
  563. void tracing_start(void)
  564. {
  565. struct ring_buffer *buffer;
  566. unsigned long flags;
  567. if (tracing_disabled)
  568. return;
  569. spin_lock_irqsave(&tracing_start_lock, flags);
  570. if (--trace_stop_count) {
  571. if (trace_stop_count < 0) {
  572. /* Someone screwed up their debugging */
  573. WARN_ON_ONCE(1);
  574. trace_stop_count = 0;
  575. }
  576. goto out;
  577. }
  578. buffer = global_trace.buffer;
  579. if (buffer)
  580. ring_buffer_record_enable(buffer);
  581. buffer = max_tr.buffer;
  582. if (buffer)
  583. ring_buffer_record_enable(buffer);
  584. ftrace_start();
  585. out:
  586. spin_unlock_irqrestore(&tracing_start_lock, flags);
  587. }
  588. /**
  589. * tracing_stop - quick stop of the tracer
  590. *
  591. * Light weight way to stop tracing. Use in conjunction with
  592. * tracing_start.
  593. */
  594. void tracing_stop(void)
  595. {
  596. struct ring_buffer *buffer;
  597. unsigned long flags;
  598. ftrace_stop();
  599. spin_lock_irqsave(&tracing_start_lock, flags);
  600. if (trace_stop_count++)
  601. goto out;
  602. buffer = global_trace.buffer;
  603. if (buffer)
  604. ring_buffer_record_disable(buffer);
  605. buffer = max_tr.buffer;
  606. if (buffer)
  607. ring_buffer_record_disable(buffer);
  608. out:
  609. spin_unlock_irqrestore(&tracing_start_lock, flags);
  610. }
  611. void trace_stop_cmdline_recording(void);
  612. static void trace_save_cmdline(struct task_struct *tsk)
  613. {
  614. unsigned map;
  615. unsigned idx;
  616. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  617. return;
  618. /*
  619. * It's not the end of the world if we don't get
  620. * the lock, but we also don't want to spin
  621. * nor do we want to disable interrupts,
  622. * so if we miss here, then better luck next time.
  623. */
  624. if (!__raw_spin_trylock(&trace_cmdline_lock))
  625. return;
  626. idx = map_pid_to_cmdline[tsk->pid];
  627. if (idx >= SAVED_CMDLINES) {
  628. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  629. map = map_cmdline_to_pid[idx];
  630. if (map <= PID_MAX_DEFAULT)
  631. map_pid_to_cmdline[map] = (unsigned)-1;
  632. map_pid_to_cmdline[tsk->pid] = idx;
  633. cmdline_idx = idx;
  634. }
  635. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  636. __raw_spin_unlock(&trace_cmdline_lock);
  637. }
  638. char *trace_find_cmdline(int pid)
  639. {
  640. char *cmdline = "<...>";
  641. unsigned map;
  642. if (!pid)
  643. return "<idle>";
  644. if (pid > PID_MAX_DEFAULT)
  645. goto out;
  646. map = map_pid_to_cmdline[pid];
  647. if (map >= SAVED_CMDLINES)
  648. goto out;
  649. cmdline = saved_cmdlines[map];
  650. out:
  651. return cmdline;
  652. }
  653. void tracing_record_cmdline(struct task_struct *tsk)
  654. {
  655. if (atomic_read(&trace_record_cmdline_disabled))
  656. return;
  657. trace_save_cmdline(tsk);
  658. }
  659. void
  660. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  661. int pc)
  662. {
  663. struct task_struct *tsk = current;
  664. entry->preempt_count = pc & 0xff;
  665. entry->pid = (tsk) ? tsk->pid : 0;
  666. entry->tgid = (tsk) ? tsk->tgid : 0;
  667. entry->flags =
  668. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  669. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  670. #else
  671. TRACE_FLAG_IRQS_NOSUPPORT |
  672. #endif
  673. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  674. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  675. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  676. }
  677. struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
  678. unsigned char type,
  679. unsigned long len,
  680. unsigned long flags, int pc)
  681. {
  682. struct ring_buffer_event *event;
  683. event = ring_buffer_lock_reserve(tr->buffer, len);
  684. if (event != NULL) {
  685. struct trace_entry *ent = ring_buffer_event_data(event);
  686. tracing_generic_entry_update(ent, flags, pc);
  687. ent->type = type;
  688. }
  689. return event;
  690. }
  691. static void ftrace_trace_stack(struct trace_array *tr,
  692. unsigned long flags, int skip, int pc);
  693. static void ftrace_trace_userstack(struct trace_array *tr,
  694. unsigned long flags, int pc);
  695. void trace_buffer_unlock_commit(struct trace_array *tr,
  696. struct ring_buffer_event *event,
  697. unsigned long flags, int pc)
  698. {
  699. ring_buffer_unlock_commit(tr->buffer, event);
  700. ftrace_trace_stack(tr, flags, 6, pc);
  701. ftrace_trace_userstack(tr, flags, pc);
  702. trace_wake_up();
  703. }
  704. struct ring_buffer_event *
  705. trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
  706. unsigned long flags, int pc)
  707. {
  708. return trace_buffer_lock_reserve(&global_trace,
  709. type, len, flags, pc);
  710. }
  711. void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
  712. unsigned long flags, int pc)
  713. {
  714. return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
  715. }
  716. void
  717. trace_function(struct trace_array *tr,
  718. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  719. int pc)
  720. {
  721. struct ring_buffer_event *event;
  722. struct ftrace_entry *entry;
  723. /* If we are reading the ring buffer, don't trace */
  724. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  725. return;
  726. event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
  727. flags, pc);
  728. if (!event)
  729. return;
  730. entry = ring_buffer_event_data(event);
  731. entry->ip = ip;
  732. entry->parent_ip = parent_ip;
  733. ring_buffer_unlock_commit(tr->buffer, event);
  734. }
  735. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  736. static void __trace_graph_entry(struct trace_array *tr,
  737. struct ftrace_graph_ent *trace,
  738. unsigned long flags,
  739. int pc)
  740. {
  741. struct ring_buffer_event *event;
  742. struct ftrace_graph_ent_entry *entry;
  743. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  744. return;
  745. event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
  746. sizeof(*entry), flags, pc);
  747. if (!event)
  748. return;
  749. entry = ring_buffer_event_data(event);
  750. entry->graph_ent = *trace;
  751. ring_buffer_unlock_commit(global_trace.buffer, event);
  752. }
  753. static void __trace_graph_return(struct trace_array *tr,
  754. struct ftrace_graph_ret *trace,
  755. unsigned long flags,
  756. int pc)
  757. {
  758. struct ring_buffer_event *event;
  759. struct ftrace_graph_ret_entry *entry;
  760. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  761. return;
  762. event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
  763. sizeof(*entry), flags, pc);
  764. if (!event)
  765. return;
  766. entry = ring_buffer_event_data(event);
  767. entry->ret = *trace;
  768. ring_buffer_unlock_commit(global_trace.buffer, event);
  769. }
  770. #endif
  771. void
  772. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  773. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  774. int pc)
  775. {
  776. if (likely(!atomic_read(&data->disabled)))
  777. trace_function(tr, ip, parent_ip, flags, pc);
  778. }
  779. static void __ftrace_trace_stack(struct trace_array *tr,
  780. unsigned long flags,
  781. int skip, int pc)
  782. {
  783. #ifdef CONFIG_STACKTRACE
  784. struct ring_buffer_event *event;
  785. struct stack_entry *entry;
  786. struct stack_trace trace;
  787. event = trace_buffer_lock_reserve(tr, TRACE_STACK,
  788. sizeof(*entry), flags, pc);
  789. if (!event)
  790. return;
  791. entry = ring_buffer_event_data(event);
  792. memset(&entry->caller, 0, sizeof(entry->caller));
  793. trace.nr_entries = 0;
  794. trace.max_entries = FTRACE_STACK_ENTRIES;
  795. trace.skip = skip;
  796. trace.entries = entry->caller;
  797. save_stack_trace(&trace);
  798. ring_buffer_unlock_commit(tr->buffer, event);
  799. #endif
  800. }
  801. static void ftrace_trace_stack(struct trace_array *tr,
  802. unsigned long flags,
  803. int skip, int pc)
  804. {
  805. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  806. return;
  807. __ftrace_trace_stack(tr, flags, skip, pc);
  808. }
  809. void __trace_stack(struct trace_array *tr,
  810. unsigned long flags,
  811. int skip, int pc)
  812. {
  813. __ftrace_trace_stack(tr, flags, skip, pc);
  814. }
  815. static void ftrace_trace_userstack(struct trace_array *tr,
  816. unsigned long flags, int pc)
  817. {
  818. #ifdef CONFIG_STACKTRACE
  819. struct ring_buffer_event *event;
  820. struct userstack_entry *entry;
  821. struct stack_trace trace;
  822. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  823. return;
  824. event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
  825. sizeof(*entry), flags, pc);
  826. if (!event)
  827. return;
  828. entry = ring_buffer_event_data(event);
  829. memset(&entry->caller, 0, sizeof(entry->caller));
  830. trace.nr_entries = 0;
  831. trace.max_entries = FTRACE_STACK_ENTRIES;
  832. trace.skip = 0;
  833. trace.entries = entry->caller;
  834. save_stack_trace_user(&trace);
  835. ring_buffer_unlock_commit(tr->buffer, event);
  836. #endif
  837. }
  838. #ifdef UNUSED
  839. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  840. {
  841. ftrace_trace_userstack(tr, flags, preempt_count());
  842. }
  843. #endif /* UNUSED */
  844. static void
  845. ftrace_trace_special(void *__tr,
  846. unsigned long arg1, unsigned long arg2, unsigned long arg3,
  847. int pc)
  848. {
  849. struct ring_buffer_event *event;
  850. struct trace_array *tr = __tr;
  851. struct special_entry *entry;
  852. event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
  853. sizeof(*entry), 0, pc);
  854. if (!event)
  855. return;
  856. entry = ring_buffer_event_data(event);
  857. entry->arg1 = arg1;
  858. entry->arg2 = arg2;
  859. entry->arg3 = arg3;
  860. trace_buffer_unlock_commit(tr, event, 0, pc);
  861. }
  862. void
  863. __trace_special(void *__tr, void *__data,
  864. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  865. {
  866. ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
  867. }
  868. void
  869. tracing_sched_switch_trace(struct trace_array *tr,
  870. struct task_struct *prev,
  871. struct task_struct *next,
  872. unsigned long flags, int pc)
  873. {
  874. struct ring_buffer_event *event;
  875. struct ctx_switch_entry *entry;
  876. event = trace_buffer_lock_reserve(tr, TRACE_CTX,
  877. sizeof(*entry), flags, pc);
  878. if (!event)
  879. return;
  880. entry = ring_buffer_event_data(event);
  881. entry->prev_pid = prev->pid;
  882. entry->prev_prio = prev->prio;
  883. entry->prev_state = prev->state;
  884. entry->next_pid = next->pid;
  885. entry->next_prio = next->prio;
  886. entry->next_state = next->state;
  887. entry->next_cpu = task_cpu(next);
  888. trace_buffer_unlock_commit(tr, event, flags, pc);
  889. }
  890. void
  891. tracing_sched_wakeup_trace(struct trace_array *tr,
  892. struct task_struct *wakee,
  893. struct task_struct *curr,
  894. unsigned long flags, int pc)
  895. {
  896. struct ring_buffer_event *event;
  897. struct ctx_switch_entry *entry;
  898. event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
  899. sizeof(*entry), flags, pc);
  900. if (!event)
  901. return;
  902. entry = ring_buffer_event_data(event);
  903. entry->prev_pid = curr->pid;
  904. entry->prev_prio = curr->prio;
  905. entry->prev_state = curr->state;
  906. entry->next_pid = wakee->pid;
  907. entry->next_prio = wakee->prio;
  908. entry->next_state = wakee->state;
  909. entry->next_cpu = task_cpu(wakee);
  910. ring_buffer_unlock_commit(tr->buffer, event);
  911. ftrace_trace_stack(tr, flags, 6, pc);
  912. ftrace_trace_userstack(tr, flags, pc);
  913. }
  914. void
  915. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
  916. {
  917. struct trace_array *tr = &global_trace;
  918. struct trace_array_cpu *data;
  919. unsigned long flags;
  920. int cpu;
  921. int pc;
  922. if (tracing_disabled)
  923. return;
  924. pc = preempt_count();
  925. local_irq_save(flags);
  926. cpu = raw_smp_processor_id();
  927. data = tr->data[cpu];
  928. if (likely(atomic_inc_return(&data->disabled) == 1))
  929. ftrace_trace_special(tr, arg1, arg2, arg3, pc);
  930. atomic_dec(&data->disabled);
  931. local_irq_restore(flags);
  932. }
  933. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  934. int trace_graph_entry(struct ftrace_graph_ent *trace)
  935. {
  936. struct trace_array *tr = &global_trace;
  937. struct trace_array_cpu *data;
  938. unsigned long flags;
  939. long disabled;
  940. int cpu;
  941. int pc;
  942. if (!ftrace_trace_task(current))
  943. return 0;
  944. if (!ftrace_graph_addr(trace->func))
  945. return 0;
  946. local_irq_save(flags);
  947. cpu = raw_smp_processor_id();
  948. data = tr->data[cpu];
  949. disabled = atomic_inc_return(&data->disabled);
  950. if (likely(disabled == 1)) {
  951. pc = preempt_count();
  952. __trace_graph_entry(tr, trace, flags, pc);
  953. }
  954. /* Only do the atomic if it is not already set */
  955. if (!test_tsk_trace_graph(current))
  956. set_tsk_trace_graph(current);
  957. atomic_dec(&data->disabled);
  958. local_irq_restore(flags);
  959. return 1;
  960. }
  961. void trace_graph_return(struct ftrace_graph_ret *trace)
  962. {
  963. struct trace_array *tr = &global_trace;
  964. struct trace_array_cpu *data;
  965. unsigned long flags;
  966. long disabled;
  967. int cpu;
  968. int pc;
  969. local_irq_save(flags);
  970. cpu = raw_smp_processor_id();
  971. data = tr->data[cpu];
  972. disabled = atomic_inc_return(&data->disabled);
  973. if (likely(disabled == 1)) {
  974. pc = preempt_count();
  975. __trace_graph_return(tr, trace, flags, pc);
  976. }
  977. if (!trace->depth)
  978. clear_tsk_trace_graph(current);
  979. atomic_dec(&data->disabled);
  980. local_irq_restore(flags);
  981. }
  982. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  983. /**
  984. * trace_vprintk - write binary msg to tracing buffer
  985. *
  986. */
  987. int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
  988. {
  989. static raw_spinlock_t trace_buf_lock =
  990. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  991. static u32 trace_buf[TRACE_BUF_SIZE];
  992. struct ring_buffer_event *event;
  993. struct trace_array *tr = &global_trace;
  994. struct trace_array_cpu *data;
  995. struct print_entry *entry;
  996. unsigned long flags;
  997. int resched;
  998. int cpu, len = 0, size, pc;
  999. if (unlikely(tracing_selftest_running || tracing_disabled))
  1000. return 0;
  1001. /* Don't pollute graph traces with trace_vprintk internals */
  1002. pause_graph_tracing();
  1003. pc = preempt_count();
  1004. resched = ftrace_preempt_disable();
  1005. cpu = raw_smp_processor_id();
  1006. data = tr->data[cpu];
  1007. if (unlikely(atomic_read(&data->disabled)))
  1008. goto out;
  1009. /* Lockdep uses trace_printk for lock tracing */
  1010. local_irq_save(flags);
  1011. __raw_spin_lock(&trace_buf_lock);
  1012. len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1013. if (len > TRACE_BUF_SIZE || len < 0)
  1014. goto out_unlock;
  1015. size = sizeof(*entry) + sizeof(u32) * len;
  1016. event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
  1017. if (!event)
  1018. goto out_unlock;
  1019. entry = ring_buffer_event_data(event);
  1020. entry->ip = ip;
  1021. entry->depth = depth;
  1022. entry->fmt = fmt;
  1023. memcpy(entry->buf, trace_buf, sizeof(u32) * len);
  1024. ring_buffer_unlock_commit(tr->buffer, event);
  1025. out_unlock:
  1026. __raw_spin_unlock(&trace_buf_lock);
  1027. local_irq_restore(flags);
  1028. out:
  1029. ftrace_preempt_enable(resched);
  1030. unpause_graph_tracing();
  1031. return len;
  1032. }
  1033. EXPORT_SYMBOL_GPL(trace_vprintk);
  1034. enum trace_file_type {
  1035. TRACE_FILE_LAT_FMT = 1,
  1036. TRACE_FILE_ANNOTATE = 2,
  1037. };
  1038. static void trace_iterator_increment(struct trace_iterator *iter)
  1039. {
  1040. /* Don't allow ftrace to trace into the ring buffers */
  1041. ftrace_disable_cpu();
  1042. iter->idx++;
  1043. if (iter->buffer_iter[iter->cpu])
  1044. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1045. ftrace_enable_cpu();
  1046. }
  1047. static struct trace_entry *
  1048. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
  1049. {
  1050. struct ring_buffer_event *event;
  1051. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  1052. /* Don't allow ftrace to trace into the ring buffers */
  1053. ftrace_disable_cpu();
  1054. if (buf_iter)
  1055. event = ring_buffer_iter_peek(buf_iter, ts);
  1056. else
  1057. event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
  1058. ftrace_enable_cpu();
  1059. return event ? ring_buffer_event_data(event) : NULL;
  1060. }
  1061. static struct trace_entry *
  1062. __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
  1063. {
  1064. struct ring_buffer *buffer = iter->tr->buffer;
  1065. struct trace_entry *ent, *next = NULL;
  1066. int cpu_file = iter->cpu_file;
  1067. u64 next_ts = 0, ts;
  1068. int next_cpu = -1;
  1069. int cpu;
  1070. /*
  1071. * If we are in a per_cpu trace file, don't bother by iterating over
  1072. * all cpu and peek directly.
  1073. */
  1074. if (cpu_file > TRACE_PIPE_ALL_CPU) {
  1075. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1076. return NULL;
  1077. ent = peek_next_entry(iter, cpu_file, ent_ts);
  1078. if (ent_cpu)
  1079. *ent_cpu = cpu_file;
  1080. return ent;
  1081. }
  1082. for_each_tracing_cpu(cpu) {
  1083. if (ring_buffer_empty_cpu(buffer, cpu))
  1084. continue;
  1085. ent = peek_next_entry(iter, cpu, &ts);
  1086. /*
  1087. * Pick the entry with the smallest timestamp:
  1088. */
  1089. if (ent && (!next || ts < next_ts)) {
  1090. next = ent;
  1091. next_cpu = cpu;
  1092. next_ts = ts;
  1093. }
  1094. }
  1095. if (ent_cpu)
  1096. *ent_cpu = next_cpu;
  1097. if (ent_ts)
  1098. *ent_ts = next_ts;
  1099. return next;
  1100. }
  1101. /* Find the next real entry, without updating the iterator itself */
  1102. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1103. int *ent_cpu, u64 *ent_ts)
  1104. {
  1105. return __find_next_entry(iter, ent_cpu, ent_ts);
  1106. }
  1107. /* Find the next real entry, and increment the iterator to the next entry */
  1108. static void *find_next_entry_inc(struct trace_iterator *iter)
  1109. {
  1110. iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
  1111. if (iter->ent)
  1112. trace_iterator_increment(iter);
  1113. return iter->ent ? iter : NULL;
  1114. }
  1115. static void trace_consume(struct trace_iterator *iter)
  1116. {
  1117. /* Don't allow ftrace to trace into the ring buffers */
  1118. ftrace_disable_cpu();
  1119. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
  1120. ftrace_enable_cpu();
  1121. }
  1122. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1123. {
  1124. struct trace_iterator *iter = m->private;
  1125. int i = (int)*pos;
  1126. void *ent;
  1127. (*pos)++;
  1128. /* can't go backwards */
  1129. if (iter->idx > i)
  1130. return NULL;
  1131. if (iter->idx < 0)
  1132. ent = find_next_entry_inc(iter);
  1133. else
  1134. ent = iter;
  1135. while (ent && iter->idx < i)
  1136. ent = find_next_entry_inc(iter);
  1137. iter->pos = *pos;
  1138. return ent;
  1139. }
  1140. /*
  1141. * No necessary locking here. The worst thing which can
  1142. * happen is loosing events consumed at the same time
  1143. * by a trace_pipe reader.
  1144. * Other than that, we don't risk to crash the ring buffer
  1145. * because it serializes the readers.
  1146. *
  1147. * The current tracer is copied to avoid a global locking
  1148. * all around.
  1149. */
  1150. static void *s_start(struct seq_file *m, loff_t *pos)
  1151. {
  1152. struct trace_iterator *iter = m->private;
  1153. static struct tracer *old_tracer;
  1154. int cpu_file = iter->cpu_file;
  1155. void *p = NULL;
  1156. loff_t l = 0;
  1157. int cpu;
  1158. /* copy the tracer to avoid using a global lock all around */
  1159. mutex_lock(&trace_types_lock);
  1160. if (unlikely(old_tracer != current_trace && current_trace)) {
  1161. old_tracer = current_trace;
  1162. *iter->trace = *current_trace;
  1163. }
  1164. mutex_unlock(&trace_types_lock);
  1165. atomic_inc(&trace_record_cmdline_disabled);
  1166. if (*pos != iter->pos) {
  1167. iter->ent = NULL;
  1168. iter->cpu = 0;
  1169. iter->idx = -1;
  1170. ftrace_disable_cpu();
  1171. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  1172. for_each_tracing_cpu(cpu)
  1173. ring_buffer_iter_reset(iter->buffer_iter[cpu]);
  1174. } else
  1175. ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
  1176. ftrace_enable_cpu();
  1177. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1178. ;
  1179. } else {
  1180. l = *pos - 1;
  1181. p = s_next(m, p, &l);
  1182. }
  1183. return p;
  1184. }
  1185. static void s_stop(struct seq_file *m, void *p)
  1186. {
  1187. atomic_dec(&trace_record_cmdline_disabled);
  1188. }
  1189. static void print_lat_help_header(struct seq_file *m)
  1190. {
  1191. seq_puts(m, "# _------=> CPU# \n");
  1192. seq_puts(m, "# / _-----=> irqs-off \n");
  1193. seq_puts(m, "# | / _----=> need-resched \n");
  1194. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1195. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1196. seq_puts(m, "# |||| / \n");
  1197. seq_puts(m, "# ||||| delay \n");
  1198. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1199. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1200. }
  1201. static void print_func_help_header(struct seq_file *m)
  1202. {
  1203. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1204. seq_puts(m, "# | | | | |\n");
  1205. }
  1206. static void
  1207. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1208. {
  1209. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1210. struct trace_array *tr = iter->tr;
  1211. struct trace_array_cpu *data = tr->data[tr->cpu];
  1212. struct tracer *type = current_trace;
  1213. unsigned long total;
  1214. unsigned long entries;
  1215. const char *name = "preemption";
  1216. if (type)
  1217. name = type->name;
  1218. entries = ring_buffer_entries(iter->tr->buffer);
  1219. total = entries +
  1220. ring_buffer_overruns(iter->tr->buffer);
  1221. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1222. name, UTS_RELEASE);
  1223. seq_puts(m, "# -----------------------------------"
  1224. "---------------------------------\n");
  1225. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1226. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1227. nsecs_to_usecs(data->saved_latency),
  1228. entries,
  1229. total,
  1230. tr->cpu,
  1231. #if defined(CONFIG_PREEMPT_NONE)
  1232. "server",
  1233. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1234. "desktop",
  1235. #elif defined(CONFIG_PREEMPT)
  1236. "preempt",
  1237. #else
  1238. "unknown",
  1239. #endif
  1240. /* These are reserved for later use */
  1241. 0, 0, 0, 0);
  1242. #ifdef CONFIG_SMP
  1243. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1244. #else
  1245. seq_puts(m, ")\n");
  1246. #endif
  1247. seq_puts(m, "# -----------------\n");
  1248. seq_printf(m, "# | task: %.16s-%d "
  1249. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1250. data->comm, data->pid, data->uid, data->nice,
  1251. data->policy, data->rt_priority);
  1252. seq_puts(m, "# -----------------\n");
  1253. if (data->critical_start) {
  1254. seq_puts(m, "# => started at: ");
  1255. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1256. trace_print_seq(m, &iter->seq);
  1257. seq_puts(m, "\n# => ended at: ");
  1258. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1259. trace_print_seq(m, &iter->seq);
  1260. seq_puts(m, "#\n");
  1261. }
  1262. seq_puts(m, "#\n");
  1263. }
  1264. static void test_cpu_buff_start(struct trace_iterator *iter)
  1265. {
  1266. struct trace_seq *s = &iter->seq;
  1267. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1268. return;
  1269. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1270. return;
  1271. if (cpumask_test_cpu(iter->cpu, iter->started))
  1272. return;
  1273. cpumask_set_cpu(iter->cpu, iter->started);
  1274. trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
  1275. }
  1276. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1277. {
  1278. struct trace_seq *s = &iter->seq;
  1279. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1280. struct trace_entry *entry;
  1281. struct trace_event *event;
  1282. entry = iter->ent;
  1283. test_cpu_buff_start(iter);
  1284. event = ftrace_find_event(entry->type);
  1285. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1286. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1287. if (!trace_print_lat_context(iter))
  1288. goto partial;
  1289. } else {
  1290. if (!trace_print_context(iter))
  1291. goto partial;
  1292. }
  1293. }
  1294. if (event)
  1295. return event->trace(iter, sym_flags);
  1296. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1297. goto partial;
  1298. return TRACE_TYPE_HANDLED;
  1299. partial:
  1300. return TRACE_TYPE_PARTIAL_LINE;
  1301. }
  1302. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1303. {
  1304. struct trace_seq *s = &iter->seq;
  1305. struct trace_entry *entry;
  1306. struct trace_event *event;
  1307. entry = iter->ent;
  1308. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1309. if (!trace_seq_printf(s, "%d %d %llu ",
  1310. entry->pid, iter->cpu, iter->ts))
  1311. goto partial;
  1312. }
  1313. event = ftrace_find_event(entry->type);
  1314. if (event)
  1315. return event->raw(iter, 0);
  1316. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1317. goto partial;
  1318. return TRACE_TYPE_HANDLED;
  1319. partial:
  1320. return TRACE_TYPE_PARTIAL_LINE;
  1321. }
  1322. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1323. {
  1324. struct trace_seq *s = &iter->seq;
  1325. unsigned char newline = '\n';
  1326. struct trace_entry *entry;
  1327. struct trace_event *event;
  1328. entry = iter->ent;
  1329. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1330. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1331. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1332. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1333. }
  1334. event = ftrace_find_event(entry->type);
  1335. if (event) {
  1336. enum print_line_t ret = event->hex(iter, 0);
  1337. if (ret != TRACE_TYPE_HANDLED)
  1338. return ret;
  1339. }
  1340. SEQ_PUT_FIELD_RET(s, newline);
  1341. return TRACE_TYPE_HANDLED;
  1342. }
  1343. static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
  1344. {
  1345. struct trace_seq *s = &iter->seq;
  1346. struct trace_entry *entry = iter->ent;
  1347. struct print_entry *field;
  1348. int ret;
  1349. trace_assign_type(field, entry);
  1350. ret = trace_seq_bprintf(s, field->fmt, field->buf);
  1351. if (!ret)
  1352. return TRACE_TYPE_PARTIAL_LINE;
  1353. return TRACE_TYPE_HANDLED;
  1354. }
  1355. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1356. {
  1357. struct trace_seq *s = &iter->seq;
  1358. struct trace_entry *entry;
  1359. struct trace_event *event;
  1360. entry = iter->ent;
  1361. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1362. SEQ_PUT_FIELD_RET(s, entry->pid);
  1363. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1364. SEQ_PUT_FIELD_RET(s, iter->ts);
  1365. }
  1366. event = ftrace_find_event(entry->type);
  1367. return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
  1368. }
  1369. static int trace_empty(struct trace_iterator *iter)
  1370. {
  1371. int cpu;
  1372. for_each_tracing_cpu(cpu) {
  1373. if (iter->buffer_iter[cpu]) {
  1374. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1375. return 0;
  1376. } else {
  1377. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1378. return 0;
  1379. }
  1380. }
  1381. return 1;
  1382. }
  1383. static enum print_line_t print_trace_line(struct trace_iterator *iter)
  1384. {
  1385. enum print_line_t ret;
  1386. if (iter->trace && iter->trace->print_line) {
  1387. ret = iter->trace->print_line(iter);
  1388. if (ret != TRACE_TYPE_UNHANDLED)
  1389. return ret;
  1390. }
  1391. if (iter->ent->type == TRACE_PRINT &&
  1392. trace_flags & TRACE_ITER_PRINTK &&
  1393. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1394. return print_printk_msg_only(iter);
  1395. if (trace_flags & TRACE_ITER_BIN)
  1396. return print_bin_fmt(iter);
  1397. if (trace_flags & TRACE_ITER_HEX)
  1398. return print_hex_fmt(iter);
  1399. if (trace_flags & TRACE_ITER_RAW)
  1400. return print_raw_fmt(iter);
  1401. return print_trace_fmt(iter);
  1402. }
  1403. static int s_show(struct seq_file *m, void *v)
  1404. {
  1405. struct trace_iterator *iter = v;
  1406. if (iter->ent == NULL) {
  1407. if (iter->tr) {
  1408. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1409. seq_puts(m, "#\n");
  1410. }
  1411. if (iter->trace && iter->trace->print_header)
  1412. iter->trace->print_header(m);
  1413. else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1414. /* print nothing if the buffers are empty */
  1415. if (trace_empty(iter))
  1416. return 0;
  1417. print_trace_header(m, iter);
  1418. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1419. print_lat_help_header(m);
  1420. } else {
  1421. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1422. print_func_help_header(m);
  1423. }
  1424. } else {
  1425. print_trace_line(iter);
  1426. trace_print_seq(m, &iter->seq);
  1427. }
  1428. return 0;
  1429. }
  1430. static struct seq_operations tracer_seq_ops = {
  1431. .start = s_start,
  1432. .next = s_next,
  1433. .stop = s_stop,
  1434. .show = s_show,
  1435. };
  1436. static struct trace_iterator *
  1437. __tracing_open(struct inode *inode, struct file *file)
  1438. {
  1439. long cpu_file = (long) inode->i_private;
  1440. void *fail_ret = ERR_PTR(-ENOMEM);
  1441. struct trace_iterator *iter;
  1442. struct seq_file *m;
  1443. int cpu, ret;
  1444. if (tracing_disabled)
  1445. return ERR_PTR(-ENODEV);
  1446. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1447. if (!iter)
  1448. return ERR_PTR(-ENOMEM);
  1449. /*
  1450. * We make a copy of the current tracer to avoid concurrent
  1451. * changes on it while we are reading.
  1452. */
  1453. mutex_lock(&trace_types_lock);
  1454. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1455. if (!iter->trace)
  1456. goto fail;
  1457. if (current_trace)
  1458. *iter->trace = *current_trace;
  1459. if (current_trace && current_trace->print_max)
  1460. iter->tr = &max_tr;
  1461. else
  1462. iter->tr = &global_trace;
  1463. iter->pos = -1;
  1464. mutex_init(&iter->mutex);
  1465. iter->cpu_file = cpu_file;
  1466. /* Notify the tracer early; before we stop tracing. */
  1467. if (iter->trace && iter->trace->open)
  1468. iter->trace->open(iter);
  1469. /* Annotate start of buffers if we had overruns */
  1470. if (ring_buffer_overruns(iter->tr->buffer))
  1471. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1472. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  1473. for_each_tracing_cpu(cpu) {
  1474. iter->buffer_iter[cpu] =
  1475. ring_buffer_read_start(iter->tr->buffer, cpu);
  1476. if (!iter->buffer_iter[cpu])
  1477. goto fail_buffer;
  1478. }
  1479. } else {
  1480. cpu = iter->cpu_file;
  1481. iter->buffer_iter[cpu] =
  1482. ring_buffer_read_start(iter->tr->buffer, cpu);
  1483. if (!iter->buffer_iter[cpu])
  1484. goto fail;
  1485. }
  1486. /* TODO stop tracer */
  1487. ret = seq_open(file, &tracer_seq_ops);
  1488. if (ret < 0) {
  1489. fail_ret = ERR_PTR(ret);
  1490. goto fail_buffer;
  1491. }
  1492. m = file->private_data;
  1493. m->private = iter;
  1494. /* stop the trace while dumping */
  1495. tracing_stop();
  1496. mutex_unlock(&trace_types_lock);
  1497. return iter;
  1498. fail_buffer:
  1499. for_each_tracing_cpu(cpu) {
  1500. if (iter->buffer_iter[cpu])
  1501. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1502. }
  1503. fail:
  1504. mutex_unlock(&trace_types_lock);
  1505. kfree(iter->trace);
  1506. kfree(iter);
  1507. return fail_ret;
  1508. }
  1509. int tracing_open_generic(struct inode *inode, struct file *filp)
  1510. {
  1511. if (tracing_disabled)
  1512. return -ENODEV;
  1513. filp->private_data = inode->i_private;
  1514. return 0;
  1515. }
  1516. static int tracing_release(struct inode *inode, struct file *file)
  1517. {
  1518. struct seq_file *m = (struct seq_file *)file->private_data;
  1519. struct trace_iterator *iter = m->private;
  1520. int cpu;
  1521. mutex_lock(&trace_types_lock);
  1522. for_each_tracing_cpu(cpu) {
  1523. if (iter->buffer_iter[cpu])
  1524. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1525. }
  1526. if (iter->trace && iter->trace->close)
  1527. iter->trace->close(iter);
  1528. /* reenable tracing if it was previously enabled */
  1529. tracing_start();
  1530. mutex_unlock(&trace_types_lock);
  1531. seq_release(inode, file);
  1532. mutex_destroy(&iter->mutex);
  1533. kfree(iter->trace);
  1534. kfree(iter);
  1535. return 0;
  1536. }
  1537. static int tracing_open(struct inode *inode, struct file *file)
  1538. {
  1539. struct trace_iterator *iter;
  1540. int ret = 0;
  1541. iter = __tracing_open(inode, file);
  1542. if (IS_ERR(iter))
  1543. ret = PTR_ERR(iter);
  1544. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  1545. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1546. return ret;
  1547. }
  1548. static void *
  1549. t_next(struct seq_file *m, void *v, loff_t *pos)
  1550. {
  1551. struct tracer *t = m->private;
  1552. (*pos)++;
  1553. if (t)
  1554. t = t->next;
  1555. m->private = t;
  1556. return t;
  1557. }
  1558. static void *t_start(struct seq_file *m, loff_t *pos)
  1559. {
  1560. struct tracer *t = m->private;
  1561. loff_t l = 0;
  1562. mutex_lock(&trace_types_lock);
  1563. for (; t && l < *pos; t = t_next(m, t, &l))
  1564. ;
  1565. return t;
  1566. }
  1567. static void t_stop(struct seq_file *m, void *p)
  1568. {
  1569. mutex_unlock(&trace_types_lock);
  1570. }
  1571. static int t_show(struct seq_file *m, void *v)
  1572. {
  1573. struct tracer *t = v;
  1574. if (!t)
  1575. return 0;
  1576. seq_printf(m, "%s", t->name);
  1577. if (t->next)
  1578. seq_putc(m, ' ');
  1579. else
  1580. seq_putc(m, '\n');
  1581. return 0;
  1582. }
  1583. static struct seq_operations show_traces_seq_ops = {
  1584. .start = t_start,
  1585. .next = t_next,
  1586. .stop = t_stop,
  1587. .show = t_show,
  1588. };
  1589. static int show_traces_open(struct inode *inode, struct file *file)
  1590. {
  1591. int ret;
  1592. if (tracing_disabled)
  1593. return -ENODEV;
  1594. ret = seq_open(file, &show_traces_seq_ops);
  1595. if (!ret) {
  1596. struct seq_file *m = file->private_data;
  1597. m->private = trace_types;
  1598. }
  1599. return ret;
  1600. }
  1601. static const struct file_operations tracing_fops = {
  1602. .open = tracing_open,
  1603. .read = seq_read,
  1604. .llseek = seq_lseek,
  1605. .release = tracing_release,
  1606. };
  1607. static const struct file_operations show_traces_fops = {
  1608. .open = show_traces_open,
  1609. .read = seq_read,
  1610. .release = seq_release,
  1611. };
  1612. /*
  1613. * Only trace on a CPU if the bitmask is set:
  1614. */
  1615. static cpumask_var_t tracing_cpumask;
  1616. /*
  1617. * The tracer itself will not take this lock, but still we want
  1618. * to provide a consistent cpumask to user-space:
  1619. */
  1620. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1621. /*
  1622. * Temporary storage for the character representation of the
  1623. * CPU bitmask (and one more byte for the newline):
  1624. */
  1625. static char mask_str[NR_CPUS + 1];
  1626. static ssize_t
  1627. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1628. size_t count, loff_t *ppos)
  1629. {
  1630. int len;
  1631. mutex_lock(&tracing_cpumask_update_lock);
  1632. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1633. if (count - len < 2) {
  1634. count = -EINVAL;
  1635. goto out_err;
  1636. }
  1637. len += sprintf(mask_str + len, "\n");
  1638. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1639. out_err:
  1640. mutex_unlock(&tracing_cpumask_update_lock);
  1641. return count;
  1642. }
  1643. static ssize_t
  1644. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1645. size_t count, loff_t *ppos)
  1646. {
  1647. int err, cpu;
  1648. cpumask_var_t tracing_cpumask_new;
  1649. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  1650. return -ENOMEM;
  1651. mutex_lock(&tracing_cpumask_update_lock);
  1652. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1653. if (err)
  1654. goto err_unlock;
  1655. local_irq_disable();
  1656. __raw_spin_lock(&ftrace_max_lock);
  1657. for_each_tracing_cpu(cpu) {
  1658. /*
  1659. * Increase/decrease the disabled counter if we are
  1660. * about to flip a bit in the cpumask:
  1661. */
  1662. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  1663. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1664. atomic_inc(&global_trace.data[cpu]->disabled);
  1665. }
  1666. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  1667. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1668. atomic_dec(&global_trace.data[cpu]->disabled);
  1669. }
  1670. }
  1671. __raw_spin_unlock(&ftrace_max_lock);
  1672. local_irq_enable();
  1673. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  1674. mutex_unlock(&tracing_cpumask_update_lock);
  1675. free_cpumask_var(tracing_cpumask_new);
  1676. return count;
  1677. err_unlock:
  1678. mutex_unlock(&tracing_cpumask_update_lock);
  1679. free_cpumask_var(tracing_cpumask);
  1680. return err;
  1681. }
  1682. static const struct file_operations tracing_cpumask_fops = {
  1683. .open = tracing_open_generic,
  1684. .read = tracing_cpumask_read,
  1685. .write = tracing_cpumask_write,
  1686. };
  1687. static ssize_t
  1688. tracing_trace_options_read(struct file *filp, char __user *ubuf,
  1689. size_t cnt, loff_t *ppos)
  1690. {
  1691. struct tracer_opt *trace_opts;
  1692. u32 tracer_flags;
  1693. int len = 0;
  1694. char *buf;
  1695. int r = 0;
  1696. int i;
  1697. /* calculate max size */
  1698. for (i = 0; trace_options[i]; i++) {
  1699. len += strlen(trace_options[i]);
  1700. len += 3; /* "no" and newline */
  1701. }
  1702. mutex_lock(&trace_types_lock);
  1703. tracer_flags = current_trace->flags->val;
  1704. trace_opts = current_trace->flags->opts;
  1705. /*
  1706. * Increase the size with names of options specific
  1707. * of the current tracer.
  1708. */
  1709. for (i = 0; trace_opts[i].name; i++) {
  1710. len += strlen(trace_opts[i].name);
  1711. len += 3; /* "no" and newline */
  1712. }
  1713. /* +2 for \n and \0 */
  1714. buf = kmalloc(len + 2, GFP_KERNEL);
  1715. if (!buf) {
  1716. mutex_unlock(&trace_types_lock);
  1717. return -ENOMEM;
  1718. }
  1719. for (i = 0; trace_options[i]; i++) {
  1720. if (trace_flags & (1 << i))
  1721. r += sprintf(buf + r, "%s\n", trace_options[i]);
  1722. else
  1723. r += sprintf(buf + r, "no%s\n", trace_options[i]);
  1724. }
  1725. for (i = 0; trace_opts[i].name; i++) {
  1726. if (tracer_flags & trace_opts[i].bit)
  1727. r += sprintf(buf + r, "%s\n",
  1728. trace_opts[i].name);
  1729. else
  1730. r += sprintf(buf + r, "no%s\n",
  1731. trace_opts[i].name);
  1732. }
  1733. mutex_unlock(&trace_types_lock);
  1734. WARN_ON(r >= len + 2);
  1735. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1736. kfree(buf);
  1737. return r;
  1738. }
  1739. /* Try to assign a tracer specific option */
  1740. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  1741. {
  1742. struct tracer_flags *trace_flags = trace->flags;
  1743. struct tracer_opt *opts = NULL;
  1744. int ret = 0, i = 0;
  1745. int len;
  1746. for (i = 0; trace_flags->opts[i].name; i++) {
  1747. opts = &trace_flags->opts[i];
  1748. len = strlen(opts->name);
  1749. if (strncmp(cmp, opts->name, len) == 0) {
  1750. ret = trace->set_flag(trace_flags->val,
  1751. opts->bit, !neg);
  1752. break;
  1753. }
  1754. }
  1755. /* Not found */
  1756. if (!trace_flags->opts[i].name)
  1757. return -EINVAL;
  1758. /* Refused to handle */
  1759. if (ret)
  1760. return ret;
  1761. if (neg)
  1762. trace_flags->val &= ~opts->bit;
  1763. else
  1764. trace_flags->val |= opts->bit;
  1765. return 0;
  1766. }
  1767. static ssize_t
  1768. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  1769. size_t cnt, loff_t *ppos)
  1770. {
  1771. char buf[64];
  1772. char *cmp = buf;
  1773. int neg = 0;
  1774. int ret;
  1775. int i;
  1776. if (cnt >= sizeof(buf))
  1777. return -EINVAL;
  1778. if (copy_from_user(&buf, ubuf, cnt))
  1779. return -EFAULT;
  1780. buf[cnt] = 0;
  1781. if (strncmp(buf, "no", 2) == 0) {
  1782. neg = 1;
  1783. cmp += 2;
  1784. }
  1785. for (i = 0; trace_options[i]; i++) {
  1786. int len = strlen(trace_options[i]);
  1787. if (strncmp(cmp, trace_options[i], len) == 0) {
  1788. if (neg)
  1789. trace_flags &= ~(1 << i);
  1790. else
  1791. trace_flags |= (1 << i);
  1792. break;
  1793. }
  1794. }
  1795. /* If no option could be set, test the specific tracer options */
  1796. if (!trace_options[i]) {
  1797. mutex_lock(&trace_types_lock);
  1798. ret = set_tracer_option(current_trace, cmp, neg);
  1799. mutex_unlock(&trace_types_lock);
  1800. if (ret)
  1801. return ret;
  1802. }
  1803. filp->f_pos += cnt;
  1804. return cnt;
  1805. }
  1806. static const struct file_operations tracing_iter_fops = {
  1807. .open = tracing_open_generic,
  1808. .read = tracing_trace_options_read,
  1809. .write = tracing_trace_options_write,
  1810. };
  1811. static const char readme_msg[] =
  1812. "tracing mini-HOWTO:\n\n"
  1813. "# mkdir /debug\n"
  1814. "# mount -t debugfs nodev /debug\n\n"
  1815. "# cat /debug/tracing/available_tracers\n"
  1816. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1817. "# cat /debug/tracing/current_tracer\n"
  1818. "none\n"
  1819. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1820. "# cat /debug/tracing/current_tracer\n"
  1821. "sched_switch\n"
  1822. "# cat /debug/tracing/trace_options\n"
  1823. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1824. "# echo print-parent > /debug/tracing/trace_options\n"
  1825. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1826. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1827. "echo 0 > /debug/tracing/tracing_enabled\n"
  1828. ;
  1829. static ssize_t
  1830. tracing_readme_read(struct file *filp, char __user *ubuf,
  1831. size_t cnt, loff_t *ppos)
  1832. {
  1833. return simple_read_from_buffer(ubuf, cnt, ppos,
  1834. readme_msg, strlen(readme_msg));
  1835. }
  1836. static const struct file_operations tracing_readme_fops = {
  1837. .open = tracing_open_generic,
  1838. .read = tracing_readme_read,
  1839. };
  1840. static ssize_t
  1841. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1842. size_t cnt, loff_t *ppos)
  1843. {
  1844. char buf[64];
  1845. int r;
  1846. r = sprintf(buf, "%u\n", tracer_enabled);
  1847. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1848. }
  1849. static ssize_t
  1850. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1851. size_t cnt, loff_t *ppos)
  1852. {
  1853. struct trace_array *tr = filp->private_data;
  1854. char buf[64];
  1855. unsigned long val;
  1856. int ret;
  1857. if (cnt >= sizeof(buf))
  1858. return -EINVAL;
  1859. if (copy_from_user(&buf, ubuf, cnt))
  1860. return -EFAULT;
  1861. buf[cnt] = 0;
  1862. ret = strict_strtoul(buf, 10, &val);
  1863. if (ret < 0)
  1864. return ret;
  1865. val = !!val;
  1866. mutex_lock(&trace_types_lock);
  1867. if (tracer_enabled ^ val) {
  1868. if (val) {
  1869. tracer_enabled = 1;
  1870. if (current_trace->start)
  1871. current_trace->start(tr);
  1872. tracing_start();
  1873. } else {
  1874. tracer_enabled = 0;
  1875. tracing_stop();
  1876. if (current_trace->stop)
  1877. current_trace->stop(tr);
  1878. }
  1879. }
  1880. mutex_unlock(&trace_types_lock);
  1881. filp->f_pos += cnt;
  1882. return cnt;
  1883. }
  1884. static ssize_t
  1885. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1886. size_t cnt, loff_t *ppos)
  1887. {
  1888. char buf[max_tracer_type_len+2];
  1889. int r;
  1890. mutex_lock(&trace_types_lock);
  1891. if (current_trace)
  1892. r = sprintf(buf, "%s\n", current_trace->name);
  1893. else
  1894. r = sprintf(buf, "\n");
  1895. mutex_unlock(&trace_types_lock);
  1896. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1897. }
  1898. int tracer_init(struct tracer *t, struct trace_array *tr)
  1899. {
  1900. tracing_reset_online_cpus(tr);
  1901. return t->init(tr);
  1902. }
  1903. struct trace_option_dentry;
  1904. static struct trace_option_dentry *
  1905. create_trace_option_files(struct tracer *tracer);
  1906. static void
  1907. destroy_trace_option_files(struct trace_option_dentry *topts);
  1908. static int tracing_set_tracer(const char *buf)
  1909. {
  1910. static struct trace_option_dentry *topts;
  1911. struct trace_array *tr = &global_trace;
  1912. struct tracer *t;
  1913. int ret = 0;
  1914. mutex_lock(&trace_types_lock);
  1915. for (t = trace_types; t; t = t->next) {
  1916. if (strcmp(t->name, buf) == 0)
  1917. break;
  1918. }
  1919. if (!t) {
  1920. ret = -EINVAL;
  1921. goto out;
  1922. }
  1923. if (t == current_trace)
  1924. goto out;
  1925. trace_branch_disable();
  1926. if (current_trace && current_trace->reset)
  1927. current_trace->reset(tr);
  1928. destroy_trace_option_files(topts);
  1929. current_trace = t;
  1930. topts = create_trace_option_files(current_trace);
  1931. if (t->init) {
  1932. ret = tracer_init(t, tr);
  1933. if (ret)
  1934. goto out;
  1935. }
  1936. trace_branch_enable(tr);
  1937. out:
  1938. mutex_unlock(&trace_types_lock);
  1939. return ret;
  1940. }
  1941. static ssize_t
  1942. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1943. size_t cnt, loff_t *ppos)
  1944. {
  1945. char buf[max_tracer_type_len+1];
  1946. int i;
  1947. size_t ret;
  1948. int err;
  1949. ret = cnt;
  1950. if (cnt > max_tracer_type_len)
  1951. cnt = max_tracer_type_len;
  1952. if (copy_from_user(&buf, ubuf, cnt))
  1953. return -EFAULT;
  1954. buf[cnt] = 0;
  1955. /* strip ending whitespace. */
  1956. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1957. buf[i] = 0;
  1958. err = tracing_set_tracer(buf);
  1959. if (err)
  1960. return err;
  1961. filp->f_pos += ret;
  1962. return ret;
  1963. }
  1964. static ssize_t
  1965. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1966. size_t cnt, loff_t *ppos)
  1967. {
  1968. unsigned long *ptr = filp->private_data;
  1969. char buf[64];
  1970. int r;
  1971. r = snprintf(buf, sizeof(buf), "%ld\n",
  1972. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1973. if (r > sizeof(buf))
  1974. r = sizeof(buf);
  1975. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1976. }
  1977. static ssize_t
  1978. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1979. size_t cnt, loff_t *ppos)
  1980. {
  1981. unsigned long *ptr = filp->private_data;
  1982. char buf[64];
  1983. unsigned long val;
  1984. int ret;
  1985. if (cnt >= sizeof(buf))
  1986. return -EINVAL;
  1987. if (copy_from_user(&buf, ubuf, cnt))
  1988. return -EFAULT;
  1989. buf[cnt] = 0;
  1990. ret = strict_strtoul(buf, 10, &val);
  1991. if (ret < 0)
  1992. return ret;
  1993. *ptr = val * 1000;
  1994. return cnt;
  1995. }
  1996. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1997. {
  1998. long cpu_file = (long) inode->i_private;
  1999. struct trace_iterator *iter;
  2000. int ret = 0;
  2001. if (tracing_disabled)
  2002. return -ENODEV;
  2003. mutex_lock(&trace_types_lock);
  2004. /* We only allow one reader per cpu */
  2005. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  2006. if (!cpumask_empty(tracing_reader_cpumask)) {
  2007. ret = -EBUSY;
  2008. goto out;
  2009. }
  2010. cpumask_setall(tracing_reader_cpumask);
  2011. } else {
  2012. if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
  2013. cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
  2014. else {
  2015. ret = -EBUSY;
  2016. goto out;
  2017. }
  2018. }
  2019. /* create a buffer to store the information to pass to userspace */
  2020. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2021. if (!iter) {
  2022. ret = -ENOMEM;
  2023. goto out;
  2024. }
  2025. /*
  2026. * We make a copy of the current tracer to avoid concurrent
  2027. * changes on it while we are reading.
  2028. */
  2029. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2030. if (!iter->trace) {
  2031. ret = -ENOMEM;
  2032. goto fail;
  2033. }
  2034. if (current_trace)
  2035. *iter->trace = *current_trace;
  2036. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2037. ret = -ENOMEM;
  2038. goto fail;
  2039. }
  2040. /* trace pipe does not show start of buffer */
  2041. cpumask_setall(iter->started);
  2042. iter->cpu_file = cpu_file;
  2043. iter->tr = &global_trace;
  2044. mutex_init(&iter->mutex);
  2045. filp->private_data = iter;
  2046. if (iter->trace->pipe_open)
  2047. iter->trace->pipe_open(iter);
  2048. out:
  2049. mutex_unlock(&trace_types_lock);
  2050. return ret;
  2051. fail:
  2052. kfree(iter->trace);
  2053. kfree(iter);
  2054. mutex_unlock(&trace_types_lock);
  2055. return ret;
  2056. }
  2057. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2058. {
  2059. struct trace_iterator *iter = file->private_data;
  2060. mutex_lock(&trace_types_lock);
  2061. if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
  2062. cpumask_clear(tracing_reader_cpumask);
  2063. else
  2064. cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
  2065. mutex_unlock(&trace_types_lock);
  2066. free_cpumask_var(iter->started);
  2067. mutex_destroy(&iter->mutex);
  2068. kfree(iter->trace);
  2069. kfree(iter);
  2070. return 0;
  2071. }
  2072. static unsigned int
  2073. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2074. {
  2075. struct trace_iterator *iter = filp->private_data;
  2076. if (trace_flags & TRACE_ITER_BLOCK) {
  2077. /*
  2078. * Always select as readable when in blocking mode
  2079. */
  2080. return POLLIN | POLLRDNORM;
  2081. } else {
  2082. if (!trace_empty(iter))
  2083. return POLLIN | POLLRDNORM;
  2084. poll_wait(filp, &trace_wait, poll_table);
  2085. if (!trace_empty(iter))
  2086. return POLLIN | POLLRDNORM;
  2087. return 0;
  2088. }
  2089. }
  2090. void default_wait_pipe(struct trace_iterator *iter)
  2091. {
  2092. DEFINE_WAIT(wait);
  2093. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2094. if (trace_empty(iter))
  2095. schedule();
  2096. finish_wait(&trace_wait, &wait);
  2097. }
  2098. /*
  2099. * This is a make-shift waitqueue.
  2100. * A tracer might use this callback on some rare cases:
  2101. *
  2102. * 1) the current tracer might hold the runqueue lock when it wakes up
  2103. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2104. * 2) the function tracers, trace all functions, we don't want
  2105. * the overhead of calling wake_up and friends
  2106. * (and tracing them too)
  2107. *
  2108. * Anyway, this is really very primitive wakeup.
  2109. */
  2110. void poll_wait_pipe(struct trace_iterator *iter)
  2111. {
  2112. set_current_state(TASK_INTERRUPTIBLE);
  2113. /* sleep for 100 msecs, and try again. */
  2114. schedule_timeout(HZ / 10);
  2115. }
  2116. /* Must be called with trace_types_lock mutex held. */
  2117. static int tracing_wait_pipe(struct file *filp)
  2118. {
  2119. struct trace_iterator *iter = filp->private_data;
  2120. while (trace_empty(iter)) {
  2121. if ((filp->f_flags & O_NONBLOCK)) {
  2122. return -EAGAIN;
  2123. }
  2124. mutex_unlock(&iter->mutex);
  2125. iter->trace->wait_pipe(iter);
  2126. mutex_lock(&iter->mutex);
  2127. if (signal_pending(current))
  2128. return -EINTR;
  2129. /*
  2130. * We block until we read something and tracing is disabled.
  2131. * We still block if tracing is disabled, but we have never
  2132. * read anything. This allows a user to cat this file, and
  2133. * then enable tracing. But after we have read something,
  2134. * we give an EOF when tracing is again disabled.
  2135. *
  2136. * iter->pos will be 0 if we haven't read anything.
  2137. */
  2138. if (!tracer_enabled && iter->pos)
  2139. break;
  2140. }
  2141. return 1;
  2142. }
  2143. /*
  2144. * Consumer reader.
  2145. */
  2146. static ssize_t
  2147. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2148. size_t cnt, loff_t *ppos)
  2149. {
  2150. struct trace_iterator *iter = filp->private_data;
  2151. static struct tracer *old_tracer;
  2152. ssize_t sret;
  2153. /* return any leftover data */
  2154. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2155. if (sret != -EBUSY)
  2156. return sret;
  2157. trace_seq_init(&iter->seq);
  2158. /* copy the tracer to avoid using a global lock all around */
  2159. mutex_lock(&trace_types_lock);
  2160. if (unlikely(old_tracer != current_trace && current_trace)) {
  2161. old_tracer = current_trace;
  2162. *iter->trace = *current_trace;
  2163. }
  2164. mutex_unlock(&trace_types_lock);
  2165. /*
  2166. * Avoid more than one consumer on a single file descriptor
  2167. * This is just a matter of traces coherency, the ring buffer itself
  2168. * is protected.
  2169. */
  2170. mutex_lock(&iter->mutex);
  2171. if (iter->trace->read) {
  2172. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2173. if (sret)
  2174. goto out;
  2175. }
  2176. waitagain:
  2177. sret = tracing_wait_pipe(filp);
  2178. if (sret <= 0)
  2179. goto out;
  2180. /* stop when tracing is finished */
  2181. if (trace_empty(iter)) {
  2182. sret = 0;
  2183. goto out;
  2184. }
  2185. if (cnt >= PAGE_SIZE)
  2186. cnt = PAGE_SIZE - 1;
  2187. /* reset all but tr, trace, and overruns */
  2188. memset(&iter->seq, 0,
  2189. sizeof(struct trace_iterator) -
  2190. offsetof(struct trace_iterator, seq));
  2191. iter->pos = -1;
  2192. while (find_next_entry_inc(iter) != NULL) {
  2193. enum print_line_t ret;
  2194. int len = iter->seq.len;
  2195. ret = print_trace_line(iter);
  2196. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2197. /* don't print partial lines */
  2198. iter->seq.len = len;
  2199. break;
  2200. }
  2201. if (ret != TRACE_TYPE_NO_CONSUME)
  2202. trace_consume(iter);
  2203. if (iter->seq.len >= cnt)
  2204. break;
  2205. }
  2206. /* Now copy what we have to the user */
  2207. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2208. if (iter->seq.readpos >= iter->seq.len)
  2209. trace_seq_init(&iter->seq);
  2210. /*
  2211. * If there was nothing to send to user, inspite of consuming trace
  2212. * entries, go back to wait for more entries.
  2213. */
  2214. if (sret == -EBUSY)
  2215. goto waitagain;
  2216. out:
  2217. mutex_unlock(&iter->mutex);
  2218. return sret;
  2219. }
  2220. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2221. struct pipe_buffer *buf)
  2222. {
  2223. __free_page(buf->page);
  2224. }
  2225. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2226. unsigned int idx)
  2227. {
  2228. __free_page(spd->pages[idx]);
  2229. }
  2230. static struct pipe_buf_operations tracing_pipe_buf_ops = {
  2231. .can_merge = 0,
  2232. .map = generic_pipe_buf_map,
  2233. .unmap = generic_pipe_buf_unmap,
  2234. .confirm = generic_pipe_buf_confirm,
  2235. .release = tracing_pipe_buf_release,
  2236. .steal = generic_pipe_buf_steal,
  2237. .get = generic_pipe_buf_get,
  2238. };
  2239. static size_t
  2240. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2241. {
  2242. size_t count;
  2243. int ret;
  2244. /* Seq buffer is page-sized, exactly what we need. */
  2245. for (;;) {
  2246. count = iter->seq.len;
  2247. ret = print_trace_line(iter);
  2248. count = iter->seq.len - count;
  2249. if (rem < count) {
  2250. rem = 0;
  2251. iter->seq.len -= count;
  2252. break;
  2253. }
  2254. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2255. iter->seq.len -= count;
  2256. break;
  2257. }
  2258. trace_consume(iter);
  2259. rem -= count;
  2260. if (!find_next_entry_inc(iter)) {
  2261. rem = 0;
  2262. iter->ent = NULL;
  2263. break;
  2264. }
  2265. }
  2266. return rem;
  2267. }
  2268. static ssize_t tracing_splice_read_pipe(struct file *filp,
  2269. loff_t *ppos,
  2270. struct pipe_inode_info *pipe,
  2271. size_t len,
  2272. unsigned int flags)
  2273. {
  2274. struct page *pages[PIPE_BUFFERS];
  2275. struct partial_page partial[PIPE_BUFFERS];
  2276. struct trace_iterator *iter = filp->private_data;
  2277. struct splice_pipe_desc spd = {
  2278. .pages = pages,
  2279. .partial = partial,
  2280. .nr_pages = 0, /* This gets updated below. */
  2281. .flags = flags,
  2282. .ops = &tracing_pipe_buf_ops,
  2283. .spd_release = tracing_spd_release_pipe,
  2284. };
  2285. static struct tracer *old_tracer;
  2286. ssize_t ret;
  2287. size_t rem;
  2288. unsigned int i;
  2289. /* copy the tracer to avoid using a global lock all around */
  2290. mutex_lock(&trace_types_lock);
  2291. if (unlikely(old_tracer != current_trace && current_trace)) {
  2292. old_tracer = current_trace;
  2293. *iter->trace = *current_trace;
  2294. }
  2295. mutex_unlock(&trace_types_lock);
  2296. mutex_lock(&iter->mutex);
  2297. if (iter->trace->splice_read) {
  2298. ret = iter->trace->splice_read(iter, filp,
  2299. ppos, pipe, len, flags);
  2300. if (ret)
  2301. goto out_err;
  2302. }
  2303. ret = tracing_wait_pipe(filp);
  2304. if (ret <= 0)
  2305. goto out_err;
  2306. if (!iter->ent && !find_next_entry_inc(iter)) {
  2307. ret = -EFAULT;
  2308. goto out_err;
  2309. }
  2310. /* Fill as many pages as possible. */
  2311. for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
  2312. pages[i] = alloc_page(GFP_KERNEL);
  2313. if (!pages[i])
  2314. break;
  2315. rem = tracing_fill_pipe_page(rem, iter);
  2316. /* Copy the data into the page, so we can start over. */
  2317. ret = trace_seq_to_buffer(&iter->seq,
  2318. page_address(pages[i]),
  2319. iter->seq.len);
  2320. if (ret < 0) {
  2321. __free_page(pages[i]);
  2322. break;
  2323. }
  2324. partial[i].offset = 0;
  2325. partial[i].len = iter->seq.len;
  2326. trace_seq_init(&iter->seq);
  2327. }
  2328. mutex_unlock(&iter->mutex);
  2329. spd.nr_pages = i;
  2330. return splice_to_pipe(pipe, &spd);
  2331. out_err:
  2332. mutex_unlock(&iter->mutex);
  2333. return ret;
  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 >> 10);
  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 ret, cpu;
  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. tracing_stop();
  2365. /* disable all cpu buffers */
  2366. for_each_tracing_cpu(cpu) {
  2367. if (global_trace.data[cpu])
  2368. atomic_inc(&global_trace.data[cpu]->disabled);
  2369. if (max_tr.data[cpu])
  2370. atomic_inc(&max_tr.data[cpu]->disabled);
  2371. }
  2372. /* value is in KB */
  2373. val <<= 10;
  2374. if (val != global_trace.entries) {
  2375. ret = ring_buffer_resize(global_trace.buffer, val);
  2376. if (ret < 0) {
  2377. cnt = ret;
  2378. goto out;
  2379. }
  2380. ret = ring_buffer_resize(max_tr.buffer, val);
  2381. if (ret < 0) {
  2382. int r;
  2383. cnt = ret;
  2384. r = ring_buffer_resize(global_trace.buffer,
  2385. global_trace.entries);
  2386. if (r < 0) {
  2387. /* AARGH! We are left with different
  2388. * size max buffer!!!! */
  2389. WARN_ON(1);
  2390. tracing_disabled = 1;
  2391. }
  2392. goto out;
  2393. }
  2394. global_trace.entries = val;
  2395. }
  2396. filp->f_pos += cnt;
  2397. /* If check pages failed, return ENOMEM */
  2398. if (tracing_disabled)
  2399. cnt = -ENOMEM;
  2400. out:
  2401. for_each_tracing_cpu(cpu) {
  2402. if (global_trace.data[cpu])
  2403. atomic_dec(&global_trace.data[cpu]->disabled);
  2404. if (max_tr.data[cpu])
  2405. atomic_dec(&max_tr.data[cpu]->disabled);
  2406. }
  2407. tracing_start();
  2408. max_tr.entries = global_trace.entries;
  2409. mutex_unlock(&trace_types_lock);
  2410. return cnt;
  2411. }
  2412. static int mark_printk(const char *fmt, ...)
  2413. {
  2414. int ret;
  2415. va_list args;
  2416. va_start(args, fmt);
  2417. ret = trace_vprintk(0, -1, fmt, args);
  2418. va_end(args);
  2419. return ret;
  2420. }
  2421. static ssize_t
  2422. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2423. size_t cnt, loff_t *fpos)
  2424. {
  2425. char *buf;
  2426. char *end;
  2427. if (tracing_disabled)
  2428. return -EINVAL;
  2429. if (cnt > TRACE_BUF_SIZE)
  2430. cnt = TRACE_BUF_SIZE;
  2431. buf = kmalloc(cnt + 1, GFP_KERNEL);
  2432. if (buf == NULL)
  2433. return -ENOMEM;
  2434. if (copy_from_user(buf, ubuf, cnt)) {
  2435. kfree(buf);
  2436. return -EFAULT;
  2437. }
  2438. /* Cut from the first nil or newline. */
  2439. buf[cnt] = '\0';
  2440. end = strchr(buf, '\n');
  2441. if (end)
  2442. *end = '\0';
  2443. cnt = mark_printk("%s\n", buf);
  2444. kfree(buf);
  2445. *fpos += cnt;
  2446. return cnt;
  2447. }
  2448. static const struct file_operations tracing_max_lat_fops = {
  2449. .open = tracing_open_generic,
  2450. .read = tracing_max_lat_read,
  2451. .write = tracing_max_lat_write,
  2452. };
  2453. static const struct file_operations tracing_ctrl_fops = {
  2454. .open = tracing_open_generic,
  2455. .read = tracing_ctrl_read,
  2456. .write = tracing_ctrl_write,
  2457. };
  2458. static const struct file_operations set_tracer_fops = {
  2459. .open = tracing_open_generic,
  2460. .read = tracing_set_trace_read,
  2461. .write = tracing_set_trace_write,
  2462. };
  2463. static const struct file_operations tracing_pipe_fops = {
  2464. .open = tracing_open_pipe,
  2465. .poll = tracing_poll_pipe,
  2466. .read = tracing_read_pipe,
  2467. .splice_read = tracing_splice_read_pipe,
  2468. .release = tracing_release_pipe,
  2469. };
  2470. static const struct file_operations tracing_entries_fops = {
  2471. .open = tracing_open_generic,
  2472. .read = tracing_entries_read,
  2473. .write = tracing_entries_write,
  2474. };
  2475. static const struct file_operations tracing_mark_fops = {
  2476. .open = tracing_open_generic,
  2477. .write = tracing_mark_write,
  2478. };
  2479. struct ftrace_buffer_info {
  2480. struct trace_array *tr;
  2481. void *spare;
  2482. int cpu;
  2483. unsigned int read;
  2484. };
  2485. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  2486. {
  2487. int cpu = (int)(long)inode->i_private;
  2488. struct ftrace_buffer_info *info;
  2489. if (tracing_disabled)
  2490. return -ENODEV;
  2491. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2492. if (!info)
  2493. return -ENOMEM;
  2494. info->tr = &global_trace;
  2495. info->cpu = cpu;
  2496. info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
  2497. /* Force reading ring buffer for first read */
  2498. info->read = (unsigned int)-1;
  2499. if (!info->spare)
  2500. goto out;
  2501. filp->private_data = info;
  2502. return 0;
  2503. out:
  2504. kfree(info);
  2505. return -ENOMEM;
  2506. }
  2507. static ssize_t
  2508. tracing_buffers_read(struct file *filp, char __user *ubuf,
  2509. size_t count, loff_t *ppos)
  2510. {
  2511. struct ftrace_buffer_info *info = filp->private_data;
  2512. unsigned int pos;
  2513. ssize_t ret;
  2514. size_t size;
  2515. if (!count)
  2516. return 0;
  2517. /* Do we have previous read data to read? */
  2518. if (info->read < PAGE_SIZE)
  2519. goto read;
  2520. info->read = 0;
  2521. ret = ring_buffer_read_page(info->tr->buffer,
  2522. &info->spare,
  2523. count,
  2524. info->cpu, 0);
  2525. if (ret < 0)
  2526. return 0;
  2527. pos = ring_buffer_page_len(info->spare);
  2528. if (pos < PAGE_SIZE)
  2529. memset(info->spare + pos, 0, PAGE_SIZE - pos);
  2530. read:
  2531. size = PAGE_SIZE - info->read;
  2532. if (size > count)
  2533. size = count;
  2534. ret = copy_to_user(ubuf, info->spare + info->read, size);
  2535. if (ret == size)
  2536. return -EFAULT;
  2537. size -= ret;
  2538. *ppos += size;
  2539. info->read += size;
  2540. return size;
  2541. }
  2542. static int tracing_buffers_release(struct inode *inode, struct file *file)
  2543. {
  2544. struct ftrace_buffer_info *info = file->private_data;
  2545. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  2546. kfree(info);
  2547. return 0;
  2548. }
  2549. struct buffer_ref {
  2550. struct ring_buffer *buffer;
  2551. void *page;
  2552. int ref;
  2553. };
  2554. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  2555. struct pipe_buffer *buf)
  2556. {
  2557. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2558. if (--ref->ref)
  2559. return;
  2560. ring_buffer_free_read_page(ref->buffer, ref->page);
  2561. kfree(ref);
  2562. buf->private = 0;
  2563. }
  2564. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  2565. struct pipe_buffer *buf)
  2566. {
  2567. return 1;
  2568. }
  2569. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  2570. struct pipe_buffer *buf)
  2571. {
  2572. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  2573. ref->ref++;
  2574. }
  2575. /* Pipe buffer operations for a buffer. */
  2576. static struct pipe_buf_operations buffer_pipe_buf_ops = {
  2577. .can_merge = 0,
  2578. .map = generic_pipe_buf_map,
  2579. .unmap = generic_pipe_buf_unmap,
  2580. .confirm = generic_pipe_buf_confirm,
  2581. .release = buffer_pipe_buf_release,
  2582. .steal = buffer_pipe_buf_steal,
  2583. .get = buffer_pipe_buf_get,
  2584. };
  2585. /*
  2586. * Callback from splice_to_pipe(), if we need to release some pages
  2587. * at the end of the spd in case we error'ed out in filling the pipe.
  2588. */
  2589. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  2590. {
  2591. struct buffer_ref *ref =
  2592. (struct buffer_ref *)spd->partial[i].private;
  2593. if (--ref->ref)
  2594. return;
  2595. ring_buffer_free_read_page(ref->buffer, ref->page);
  2596. kfree(ref);
  2597. spd->partial[i].private = 0;
  2598. }
  2599. static ssize_t
  2600. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  2601. struct pipe_inode_info *pipe, size_t len,
  2602. unsigned int flags)
  2603. {
  2604. struct ftrace_buffer_info *info = file->private_data;
  2605. struct partial_page partial[PIPE_BUFFERS];
  2606. struct page *pages[PIPE_BUFFERS];
  2607. struct splice_pipe_desc spd = {
  2608. .pages = pages,
  2609. .partial = partial,
  2610. .flags = flags,
  2611. .ops = &buffer_pipe_buf_ops,
  2612. .spd_release = buffer_spd_release,
  2613. };
  2614. struct buffer_ref *ref;
  2615. int size, i;
  2616. size_t ret;
  2617. /*
  2618. * We can't seek on a buffer input
  2619. */
  2620. if (unlikely(*ppos))
  2621. return -ESPIPE;
  2622. for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
  2623. struct page *page;
  2624. int r;
  2625. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  2626. if (!ref)
  2627. break;
  2628. ref->buffer = info->tr->buffer;
  2629. ref->page = ring_buffer_alloc_read_page(ref->buffer);
  2630. if (!ref->page) {
  2631. kfree(ref);
  2632. break;
  2633. }
  2634. r = ring_buffer_read_page(ref->buffer, &ref->page,
  2635. len, info->cpu, 0);
  2636. if (r < 0) {
  2637. ring_buffer_free_read_page(ref->buffer,
  2638. ref->page);
  2639. kfree(ref);
  2640. break;
  2641. }
  2642. /*
  2643. * zero out any left over data, this is going to
  2644. * user land.
  2645. */
  2646. size = ring_buffer_page_len(ref->page);
  2647. if (size < PAGE_SIZE)
  2648. memset(ref->page + size, 0, PAGE_SIZE - size);
  2649. page = virt_to_page(ref->page);
  2650. spd.pages[i] = page;
  2651. spd.partial[i].len = PAGE_SIZE;
  2652. spd.partial[i].offset = 0;
  2653. spd.partial[i].private = (unsigned long)ref;
  2654. spd.nr_pages++;
  2655. }
  2656. spd.nr_pages = i;
  2657. /* did we read anything? */
  2658. if (!spd.nr_pages) {
  2659. if (flags & SPLICE_F_NONBLOCK)
  2660. ret = -EAGAIN;
  2661. else
  2662. ret = 0;
  2663. /* TODO: block */
  2664. return ret;
  2665. }
  2666. ret = splice_to_pipe(pipe, &spd);
  2667. return ret;
  2668. }
  2669. static const struct file_operations tracing_buffers_fops = {
  2670. .open = tracing_buffers_open,
  2671. .read = tracing_buffers_read,
  2672. .release = tracing_buffers_release,
  2673. .splice_read = tracing_buffers_splice_read,
  2674. .llseek = no_llseek,
  2675. };
  2676. #ifdef CONFIG_DYNAMIC_FTRACE
  2677. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  2678. {
  2679. return 0;
  2680. }
  2681. static ssize_t
  2682. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  2683. size_t cnt, loff_t *ppos)
  2684. {
  2685. static char ftrace_dyn_info_buffer[1024];
  2686. static DEFINE_MUTEX(dyn_info_mutex);
  2687. unsigned long *p = filp->private_data;
  2688. char *buf = ftrace_dyn_info_buffer;
  2689. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  2690. int r;
  2691. mutex_lock(&dyn_info_mutex);
  2692. r = sprintf(buf, "%ld ", *p);
  2693. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  2694. buf[r++] = '\n';
  2695. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2696. mutex_unlock(&dyn_info_mutex);
  2697. return r;
  2698. }
  2699. static const struct file_operations tracing_dyn_info_fops = {
  2700. .open = tracing_open_generic,
  2701. .read = tracing_read_dyn_info,
  2702. };
  2703. #endif
  2704. static struct dentry *d_tracer;
  2705. struct dentry *tracing_init_dentry(void)
  2706. {
  2707. static int once;
  2708. if (d_tracer)
  2709. return d_tracer;
  2710. d_tracer = debugfs_create_dir("tracing", NULL);
  2711. if (!d_tracer && !once) {
  2712. once = 1;
  2713. pr_warning("Could not create debugfs directory 'tracing'\n");
  2714. return NULL;
  2715. }
  2716. return d_tracer;
  2717. }
  2718. static struct dentry *d_percpu;
  2719. struct dentry *tracing_dentry_percpu(void)
  2720. {
  2721. static int once;
  2722. struct dentry *d_tracer;
  2723. if (d_percpu)
  2724. return d_percpu;
  2725. d_tracer = tracing_init_dentry();
  2726. if (!d_tracer)
  2727. return NULL;
  2728. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  2729. if (!d_percpu && !once) {
  2730. once = 1;
  2731. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  2732. return NULL;
  2733. }
  2734. return d_percpu;
  2735. }
  2736. static void tracing_init_debugfs_percpu(long cpu)
  2737. {
  2738. struct dentry *d_percpu = tracing_dentry_percpu();
  2739. struct dentry *entry, *d_cpu;
  2740. /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
  2741. char cpu_dir[7];
  2742. if (cpu > 999 || cpu < 0)
  2743. return;
  2744. sprintf(cpu_dir, "cpu%ld", cpu);
  2745. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  2746. if (!d_cpu) {
  2747. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  2748. return;
  2749. }
  2750. /* per cpu trace_pipe */
  2751. entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
  2752. (void *) cpu, &tracing_pipe_fops);
  2753. if (!entry)
  2754. pr_warning("Could not create debugfs 'trace_pipe' entry\n");
  2755. /* per cpu trace */
  2756. entry = debugfs_create_file("trace", 0444, d_cpu,
  2757. (void *) cpu, &tracing_fops);
  2758. if (!entry)
  2759. pr_warning("Could not create debugfs 'trace' entry\n");
  2760. }
  2761. #ifdef CONFIG_FTRACE_SELFTEST
  2762. /* Let selftest have access to static functions in this file */
  2763. #include "trace_selftest.c"
  2764. #endif
  2765. struct trace_option_dentry {
  2766. struct tracer_opt *opt;
  2767. struct tracer_flags *flags;
  2768. struct dentry *entry;
  2769. };
  2770. static ssize_t
  2771. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  2772. loff_t *ppos)
  2773. {
  2774. struct trace_option_dentry *topt = filp->private_data;
  2775. char *buf;
  2776. if (topt->flags->val & topt->opt->bit)
  2777. buf = "1\n";
  2778. else
  2779. buf = "0\n";
  2780. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  2781. }
  2782. static ssize_t
  2783. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  2784. loff_t *ppos)
  2785. {
  2786. struct trace_option_dentry *topt = filp->private_data;
  2787. unsigned long val;
  2788. char buf[64];
  2789. int ret;
  2790. if (cnt >= sizeof(buf))
  2791. return -EINVAL;
  2792. if (copy_from_user(&buf, ubuf, cnt))
  2793. return -EFAULT;
  2794. buf[cnt] = 0;
  2795. ret = strict_strtoul(buf, 10, &val);
  2796. if (ret < 0)
  2797. return ret;
  2798. ret = 0;
  2799. switch (val) {
  2800. case 0:
  2801. /* do nothing if already cleared */
  2802. if (!(topt->flags->val & topt->opt->bit))
  2803. break;
  2804. mutex_lock(&trace_types_lock);
  2805. if (current_trace->set_flag)
  2806. ret = current_trace->set_flag(topt->flags->val,
  2807. topt->opt->bit, 0);
  2808. mutex_unlock(&trace_types_lock);
  2809. if (ret)
  2810. return ret;
  2811. topt->flags->val &= ~topt->opt->bit;
  2812. break;
  2813. case 1:
  2814. /* do nothing if already set */
  2815. if (topt->flags->val & topt->opt->bit)
  2816. break;
  2817. mutex_lock(&trace_types_lock);
  2818. if (current_trace->set_flag)
  2819. ret = current_trace->set_flag(topt->flags->val,
  2820. topt->opt->bit, 1);
  2821. mutex_unlock(&trace_types_lock);
  2822. if (ret)
  2823. return ret;
  2824. topt->flags->val |= topt->opt->bit;
  2825. break;
  2826. default:
  2827. return -EINVAL;
  2828. }
  2829. *ppos += cnt;
  2830. return cnt;
  2831. }
  2832. static const struct file_operations trace_options_fops = {
  2833. .open = tracing_open_generic,
  2834. .read = trace_options_read,
  2835. .write = trace_options_write,
  2836. };
  2837. static ssize_t
  2838. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  2839. loff_t *ppos)
  2840. {
  2841. long index = (long)filp->private_data;
  2842. char *buf;
  2843. if (trace_flags & (1 << index))
  2844. buf = "1\n";
  2845. else
  2846. buf = "0\n";
  2847. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  2848. }
  2849. static ssize_t
  2850. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  2851. loff_t *ppos)
  2852. {
  2853. long index = (long)filp->private_data;
  2854. char buf[64];
  2855. unsigned long val;
  2856. int ret;
  2857. if (cnt >= sizeof(buf))
  2858. return -EINVAL;
  2859. if (copy_from_user(&buf, ubuf, cnt))
  2860. return -EFAULT;
  2861. buf[cnt] = 0;
  2862. ret = strict_strtoul(buf, 10, &val);
  2863. if (ret < 0)
  2864. return ret;
  2865. switch (val) {
  2866. case 0:
  2867. trace_flags &= ~(1 << index);
  2868. break;
  2869. case 1:
  2870. trace_flags |= 1 << index;
  2871. break;
  2872. default:
  2873. return -EINVAL;
  2874. }
  2875. *ppos += cnt;
  2876. return cnt;
  2877. }
  2878. static const struct file_operations trace_options_core_fops = {
  2879. .open = tracing_open_generic,
  2880. .read = trace_options_core_read,
  2881. .write = trace_options_core_write,
  2882. };
  2883. static struct dentry *trace_options_init_dentry(void)
  2884. {
  2885. struct dentry *d_tracer;
  2886. static struct dentry *t_options;
  2887. if (t_options)
  2888. return t_options;
  2889. d_tracer = tracing_init_dentry();
  2890. if (!d_tracer)
  2891. return NULL;
  2892. t_options = debugfs_create_dir("options", d_tracer);
  2893. if (!t_options) {
  2894. pr_warning("Could not create debugfs directory 'options'\n");
  2895. return NULL;
  2896. }
  2897. return t_options;
  2898. }
  2899. static void
  2900. create_trace_option_file(struct trace_option_dentry *topt,
  2901. struct tracer_flags *flags,
  2902. struct tracer_opt *opt)
  2903. {
  2904. struct dentry *t_options;
  2905. struct dentry *entry;
  2906. t_options = trace_options_init_dentry();
  2907. if (!t_options)
  2908. return;
  2909. topt->flags = flags;
  2910. topt->opt = opt;
  2911. entry = debugfs_create_file(opt->name, 0644, t_options, topt,
  2912. &trace_options_fops);
  2913. topt->entry = entry;
  2914. }
  2915. static struct trace_option_dentry *
  2916. create_trace_option_files(struct tracer *tracer)
  2917. {
  2918. struct trace_option_dentry *topts;
  2919. struct tracer_flags *flags;
  2920. struct tracer_opt *opts;
  2921. int cnt;
  2922. if (!tracer)
  2923. return NULL;
  2924. flags = tracer->flags;
  2925. if (!flags || !flags->opts)
  2926. return NULL;
  2927. opts = flags->opts;
  2928. for (cnt = 0; opts[cnt].name; cnt++)
  2929. ;
  2930. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  2931. if (!topts)
  2932. return NULL;
  2933. for (cnt = 0; opts[cnt].name; cnt++)
  2934. create_trace_option_file(&topts[cnt], flags,
  2935. &opts[cnt]);
  2936. return topts;
  2937. }
  2938. static void
  2939. destroy_trace_option_files(struct trace_option_dentry *topts)
  2940. {
  2941. int cnt;
  2942. if (!topts)
  2943. return;
  2944. for (cnt = 0; topts[cnt].opt; cnt++) {
  2945. if (topts[cnt].entry)
  2946. debugfs_remove(topts[cnt].entry);
  2947. }
  2948. kfree(topts);
  2949. }
  2950. static struct dentry *
  2951. create_trace_option_core_file(const char *option, long index)
  2952. {
  2953. struct dentry *t_options;
  2954. struct dentry *entry;
  2955. t_options = trace_options_init_dentry();
  2956. if (!t_options)
  2957. return NULL;
  2958. entry = debugfs_create_file(option, 0644, t_options, (void *)index,
  2959. &trace_options_core_fops);
  2960. return entry;
  2961. }
  2962. static __init void create_trace_options_dir(void)
  2963. {
  2964. struct dentry *t_options;
  2965. struct dentry *entry;
  2966. int i;
  2967. t_options = trace_options_init_dentry();
  2968. if (!t_options)
  2969. return;
  2970. for (i = 0; trace_options[i]; i++) {
  2971. entry = create_trace_option_core_file(trace_options[i], i);
  2972. if (!entry)
  2973. pr_warning("Could not create debugfs %s entry\n",
  2974. trace_options[i]);
  2975. }
  2976. }
  2977. static __init int tracer_init_debugfs(void)
  2978. {
  2979. struct dentry *d_tracer;
  2980. struct dentry *buffers;
  2981. struct dentry *entry;
  2982. int cpu;
  2983. d_tracer = tracing_init_dentry();
  2984. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2985. &global_trace, &tracing_ctrl_fops);
  2986. if (!entry)
  2987. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2988. entry = debugfs_create_file("trace_options", 0644, d_tracer,
  2989. NULL, &tracing_iter_fops);
  2990. if (!entry)
  2991. pr_warning("Could not create debugfs 'trace_options' entry\n");
  2992. create_trace_options_dir();
  2993. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2994. NULL, &tracing_cpumask_fops);
  2995. if (!entry)
  2996. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2997. entry = debugfs_create_file("trace", 0444, d_tracer,
  2998. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  2999. if (!entry)
  3000. pr_warning("Could not create debugfs 'trace' entry\n");
  3001. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  3002. &global_trace, &show_traces_fops);
  3003. if (!entry)
  3004. pr_warning("Could not create debugfs 'available_tracers' entry\n");
  3005. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  3006. &global_trace, &set_tracer_fops);
  3007. if (!entry)
  3008. pr_warning("Could not create debugfs 'current_tracer' entry\n");
  3009. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  3010. &tracing_max_latency,
  3011. &tracing_max_lat_fops);
  3012. if (!entry)
  3013. pr_warning("Could not create debugfs "
  3014. "'tracing_max_latency' entry\n");
  3015. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  3016. &tracing_thresh, &tracing_max_lat_fops);
  3017. if (!entry)
  3018. pr_warning("Could not create debugfs "
  3019. "'tracing_thresh' entry\n");
  3020. entry = debugfs_create_file("README", 0644, d_tracer,
  3021. NULL, &tracing_readme_fops);
  3022. if (!entry)
  3023. pr_warning("Could not create debugfs 'README' entry\n");
  3024. entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
  3025. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3026. if (!entry)
  3027. pr_warning("Could not create debugfs "
  3028. "'trace_pipe' entry\n");
  3029. entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
  3030. &global_trace, &tracing_entries_fops);
  3031. if (!entry)
  3032. pr_warning("Could not create debugfs "
  3033. "'buffer_size_kb' entry\n");
  3034. entry = debugfs_create_file("trace_marker", 0220, d_tracer,
  3035. NULL, &tracing_mark_fops);
  3036. if (!entry)
  3037. pr_warning("Could not create debugfs "
  3038. "'trace_marker' entry\n");
  3039. buffers = debugfs_create_dir("binary_buffers", d_tracer);
  3040. if (!buffers)
  3041. pr_warning("Could not create buffers directory\n");
  3042. else {
  3043. int cpu;
  3044. char buf[64];
  3045. for_each_tracing_cpu(cpu) {
  3046. sprintf(buf, "%d", cpu);
  3047. entry = debugfs_create_file(buf, 0444, buffers,
  3048. (void *)(long)cpu,
  3049. &tracing_buffers_fops);
  3050. if (!entry)
  3051. pr_warning("Could not create debugfs buffers "
  3052. "'%s' entry\n", buf);
  3053. }
  3054. }
  3055. #ifdef CONFIG_DYNAMIC_FTRACE
  3056. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3057. &ftrace_update_tot_cnt,
  3058. &tracing_dyn_info_fops);
  3059. if (!entry)
  3060. pr_warning("Could not create debugfs "
  3061. "'dyn_ftrace_total_info' entry\n");
  3062. #endif
  3063. #ifdef CONFIG_SYSPROF_TRACER
  3064. init_tracer_sysprof_debugfs(d_tracer);
  3065. #endif
  3066. for_each_tracing_cpu(cpu)
  3067. tracing_init_debugfs_percpu(cpu);
  3068. return 0;
  3069. }
  3070. static int trace_panic_handler(struct notifier_block *this,
  3071. unsigned long event, void *unused)
  3072. {
  3073. if (ftrace_dump_on_oops)
  3074. ftrace_dump();
  3075. return NOTIFY_OK;
  3076. }
  3077. static struct notifier_block trace_panic_notifier = {
  3078. .notifier_call = trace_panic_handler,
  3079. .next = NULL,
  3080. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3081. };
  3082. static int trace_die_handler(struct notifier_block *self,
  3083. unsigned long val,
  3084. void *data)
  3085. {
  3086. switch (val) {
  3087. case DIE_OOPS:
  3088. if (ftrace_dump_on_oops)
  3089. ftrace_dump();
  3090. break;
  3091. default:
  3092. break;
  3093. }
  3094. return NOTIFY_OK;
  3095. }
  3096. static struct notifier_block trace_die_notifier = {
  3097. .notifier_call = trace_die_handler,
  3098. .priority = 200
  3099. };
  3100. /*
  3101. * printk is set to max of 1024, we really don't need it that big.
  3102. * Nothing should be printing 1000 characters anyway.
  3103. */
  3104. #define TRACE_MAX_PRINT 1000
  3105. /*
  3106. * Define here KERN_TRACE so that we have one place to modify
  3107. * it if we decide to change what log level the ftrace dump
  3108. * should be at.
  3109. */
  3110. #define KERN_TRACE KERN_EMERG
  3111. static void
  3112. trace_printk_seq(struct trace_seq *s)
  3113. {
  3114. /* Probably should print a warning here. */
  3115. if (s->len >= 1000)
  3116. s->len = 1000;
  3117. /* should be zero ended, but we are paranoid. */
  3118. s->buffer[s->len] = 0;
  3119. printk(KERN_TRACE "%s", s->buffer);
  3120. trace_seq_init(s);
  3121. }
  3122. void ftrace_dump(void)
  3123. {
  3124. static DEFINE_SPINLOCK(ftrace_dump_lock);
  3125. /* use static because iter can be a bit big for the stack */
  3126. static struct trace_iterator iter;
  3127. static int dump_ran;
  3128. unsigned long flags;
  3129. int cnt = 0, cpu;
  3130. /* only one dump */
  3131. spin_lock_irqsave(&ftrace_dump_lock, flags);
  3132. if (dump_ran)
  3133. goto out;
  3134. dump_ran = 1;
  3135. /* No turning back! */
  3136. tracing_off();
  3137. ftrace_kill();
  3138. for_each_tracing_cpu(cpu) {
  3139. atomic_inc(&global_trace.data[cpu]->disabled);
  3140. }
  3141. /* don't look at user memory in panic mode */
  3142. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3143. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3144. /* Simulate the iterator */
  3145. iter.tr = &global_trace;
  3146. iter.trace = current_trace;
  3147. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3148. /*
  3149. * We need to stop all tracing on all CPUS to read the
  3150. * the next buffer. This is a bit expensive, but is
  3151. * not done often. We fill all what we can read,
  3152. * and then release the locks again.
  3153. */
  3154. while (!trace_empty(&iter)) {
  3155. if (!cnt)
  3156. printk(KERN_TRACE "---------------------------------\n");
  3157. cnt++;
  3158. /* reset all but tr, trace, and overruns */
  3159. memset(&iter.seq, 0,
  3160. sizeof(struct trace_iterator) -
  3161. offsetof(struct trace_iterator, seq));
  3162. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3163. iter.pos = -1;
  3164. if (find_next_entry_inc(&iter) != NULL) {
  3165. print_trace_line(&iter);
  3166. trace_consume(&iter);
  3167. }
  3168. trace_printk_seq(&iter.seq);
  3169. }
  3170. if (!cnt)
  3171. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3172. else
  3173. printk(KERN_TRACE "---------------------------------\n");
  3174. out:
  3175. spin_unlock_irqrestore(&ftrace_dump_lock, flags);
  3176. }
  3177. __init static int tracer_alloc_buffers(void)
  3178. {
  3179. struct trace_array_cpu *data;
  3180. int i;
  3181. int ret = -ENOMEM;
  3182. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  3183. goto out;
  3184. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  3185. goto out_free_buffer_mask;
  3186. if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
  3187. goto out_free_tracing_cpumask;
  3188. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  3189. cpumask_copy(tracing_cpumask, cpu_all_mask);
  3190. cpumask_clear(tracing_reader_cpumask);
  3191. /* TODO: make the number of buffers hot pluggable with CPUS */
  3192. global_trace.buffer = ring_buffer_alloc(trace_buf_size,
  3193. TRACE_BUFFER_FLAGS);
  3194. if (!global_trace.buffer) {
  3195. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3196. WARN_ON(1);
  3197. goto out_free_cpumask;
  3198. }
  3199. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3200. #ifdef CONFIG_TRACER_MAX_TRACE
  3201. max_tr.buffer = ring_buffer_alloc(trace_buf_size,
  3202. TRACE_BUFFER_FLAGS);
  3203. if (!max_tr.buffer) {
  3204. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3205. WARN_ON(1);
  3206. ring_buffer_free(global_trace.buffer);
  3207. goto out_free_cpumask;
  3208. }
  3209. max_tr.entries = ring_buffer_size(max_tr.buffer);
  3210. WARN_ON(max_tr.entries != global_trace.entries);
  3211. #endif
  3212. /* Allocate the first page for all buffers */
  3213. for_each_tracing_cpu(i) {
  3214. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3215. max_tr.data[i] = &per_cpu(max_data, i);
  3216. }
  3217. trace_init_cmdlines();
  3218. register_tracer(&nop_trace);
  3219. current_trace = &nop_trace;
  3220. #ifdef CONFIG_BOOT_TRACER
  3221. register_tracer(&boot_tracer);
  3222. #endif
  3223. /* All seems OK, enable tracing */
  3224. tracing_disabled = 0;
  3225. atomic_notifier_chain_register(&panic_notifier_list,
  3226. &trace_panic_notifier);
  3227. register_die_notifier(&trace_die_notifier);
  3228. ret = 0;
  3229. out_free_cpumask:
  3230. free_cpumask_var(tracing_reader_cpumask);
  3231. out_free_tracing_cpumask:
  3232. free_cpumask_var(tracing_cpumask);
  3233. out_free_buffer_mask:
  3234. free_cpumask_var(tracing_buffer_mask);
  3235. out:
  3236. return ret;
  3237. }
  3238. __init static int clear_boot_tracer(void)
  3239. {
  3240. /*
  3241. * The default tracer at boot buffer is an init section.
  3242. * This function is called in lateinit. If we did not
  3243. * find the boot tracer, then clear it out, to prevent
  3244. * later registration from accessing the buffer that is
  3245. * about to be freed.
  3246. */
  3247. if (!default_bootup_tracer)
  3248. return 0;
  3249. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  3250. default_bootup_tracer);
  3251. default_bootup_tracer = NULL;
  3252. return 0;
  3253. }
  3254. early_initcall(tracer_alloc_buffers);
  3255. fs_initcall(tracer_init_debugfs);
  3256. late_initcall(clear_boot_tracer);