trace.c 98 KB

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