trace.c 118 KB

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