trace.c 99 KB

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