trace.c 100 KB

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