trace.c 103 KB

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