trace.c 103 KB

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