pprof-symbolize 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580
  1. #! /usr/bin/env perl
  2. # Copyright (c) 1998-2007, Google Inc.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. # ---
  31. # Program for printing the profile generated by common/profiler.cc,
  32. # or by the heap profiler (common/debugallocation.cc)
  33. #
  34. # The profile contains a sequence of entries of the form:
  35. # <count> <stack trace>
  36. # This program parses the profile, and generates user-readable
  37. # output.
  38. #
  39. # Examples:
  40. #
  41. # % tools/pprof "program" "profile"
  42. # Enters "interactive" mode
  43. #
  44. # % tools/pprof --text "program" "profile"
  45. # Generates one line per procedure
  46. #
  47. # % tools/pprof --gv "program" "profile"
  48. # Generates annotated call-graph and displays via "gv"
  49. #
  50. # % tools/pprof --gv --focus=Mutex "program" "profile"
  51. # Restrict to code paths that involve an entry that matches "Mutex"
  52. #
  53. # % tools/pprof --gv --focus=Mutex --ignore=string "program" "profile"
  54. # Restrict to code paths that involve an entry that matches "Mutex"
  55. # and does not match "string"
  56. #
  57. # % tools/pprof --list=IBF_CheckDocid "program" "profile"
  58. # Generates disassembly listing of all routines with at least one
  59. # sample that match the --list=<regexp> pattern. The listing is
  60. # annotated with the flat and cumulative sample counts at each line.
  61. #
  62. # % tools/pprof --disasm=IBF_CheckDocid "program" "profile"
  63. # Generates disassembly listing of all routines with at least one
  64. # sample that match the --disasm=<regexp> pattern. The listing is
  65. # annotated with the flat and cumulative sample counts at each PC value.
  66. #
  67. # TODO: Use color to indicate files?
  68. use strict;
  69. use warnings;
  70. use Getopt::Long;
  71. use Cwd;
  72. use POSIX;
  73. my $PPROF_VERSION = "2.0";
  74. # These are the object tools we use which can come from a
  75. # user-specified location using --tools, from the PPROF_TOOLS
  76. # environment variable, or from the environment.
  77. my %obj_tool_map = (
  78. "objdump" => "objdump",
  79. "nm" => "nm",
  80. "addr2line" => "addr2line",
  81. "c++filt" => "c++filt",
  82. ## ConfigureObjTools may add architecture-specific entries:
  83. #"nm_pdb" => "nm-pdb", # for reading windows (PDB-format) executables
  84. #"addr2line_pdb" => "addr2line-pdb", # ditto
  85. #"otool" => "otool", # equivalent of objdump on OS X
  86. );
  87. # NOTE: these are lists, so you can put in commandline flags if you want.
  88. my @DOT = ("dot"); # leave non-absolute, since it may be in /usr/local
  89. my @GV = ("gv");
  90. my @EVINCE = ("evince"); # could also be xpdf or perhaps acroread
  91. my @KCACHEGRIND = ("kcachegrind");
  92. my @PS2PDF = ("ps2pdf");
  93. # These are used for dynamic profiles
  94. my @URL_FETCHER = ("curl", "-s");
  95. # These are the web pages that servers need to support for dynamic profiles
  96. my $HEAP_PAGE = "/pprof/heap";
  97. my $PROFILE_PAGE = "/pprof/profile"; # must support cgi-param "?seconds=#"
  98. my $PMUPROFILE_PAGE = "/pprof/pmuprofile(?:\\?.*)?"; # must support cgi-param
  99. # ?seconds=#&event=x&period=n
  100. my $GROWTH_PAGE = "/pprof/growth";
  101. my $CONTENTION_PAGE = "/pprof/contention";
  102. my $WALL_PAGE = "/pprof/wall(?:\\?.*)?"; # accepts options like namefilter
  103. my $FILTEREDPROFILE_PAGE = "/pprof/filteredprofile(?:\\?.*)?";
  104. my $CENSUSPROFILE_PAGE = "/pprof/censusprofile(?:\\?.*)?"; # must support cgi-param
  105. # "?seconds=#",
  106. # "?tags_regexp=#" and
  107. # "?type=#".
  108. my $SYMBOL_PAGE = "/pprof/symbol"; # must support symbol lookup via POST
  109. my $PROGRAM_NAME_PAGE = "/pprof/cmdline";
  110. # These are the web pages that can be named on the command line.
  111. # All the alternatives must begin with /.
  112. my $PROFILES = "($HEAP_PAGE|$PROFILE_PAGE|$PMUPROFILE_PAGE|" .
  113. "$GROWTH_PAGE|$CONTENTION_PAGE|$WALL_PAGE|" .
  114. "$FILTEREDPROFILE_PAGE|$CENSUSPROFILE_PAGE)";
  115. # default binary name
  116. my $UNKNOWN_BINARY = "(unknown)";
  117. # There is a pervasive dependency on the length (in hex characters,
  118. # i.e., nibbles) of an address, distinguishing between 32-bit and
  119. # 64-bit profiles. To err on the safe size, default to 64-bit here:
  120. my $address_length = 16;
  121. my $dev_null = "/dev/null";
  122. if (! -e $dev_null && $^O =~ /MSWin/) { # $^O is the OS perl was built for
  123. $dev_null = "nul";
  124. }
  125. # A list of paths to search for shared object files
  126. my @prefix_list = ();
  127. # Special routine name that should not have any symbols.
  128. # Used as separator to parse "addr2line -i" output.
  129. my $sep_symbol = '_fini';
  130. my $sep_address = undef;
  131. my @stackTraces;
  132. ##### Argument parsing #####
  133. sub usage_string {
  134. return <<EOF;
  135. Usage:
  136. $0 [options] <program> <profiles>
  137. <profiles> is a space separated list of profile names.
  138. $0 [options] <symbolized-profiles>
  139. <symbolized-profiles> is a list of profile files where each file contains
  140. the necessary symbol mappings as well as profile data (likely generated
  141. with --raw).
  142. $0 [options] <profile>
  143. <profile> is a remote form. Symbols are obtained from host:port$SYMBOL_PAGE
  144. Each name can be:
  145. /path/to/profile - a path to a profile file
  146. host:port[/<service>] - a location of a service to get profile from
  147. The /<service> can be $HEAP_PAGE, $PROFILE_PAGE, /pprof/pmuprofile,
  148. $GROWTH_PAGE, $CONTENTION_PAGE, /pprof/wall,
  149. $CENSUSPROFILE_PAGE, or /pprof/filteredprofile.
  150. For instance:
  151. $0 http://myserver.com:80$HEAP_PAGE
  152. If /<service> is omitted, the service defaults to $PROFILE_PAGE (cpu profiling).
  153. $0 --symbols <program>
  154. Maps addresses to symbol names. In this mode, stdin should be a
  155. list of library mappings, in the same format as is found in the heap-
  156. and cpu-profile files (this loosely matches that of /proc/self/maps
  157. on linux), followed by a list of hex addresses to map, one per line.
  158. For more help with querying remote servers, including how to add the
  159. necessary server-side support code, see this filename (or one like it):
  160. /usr/doc/gperftools-$PPROF_VERSION/pprof_remote_servers.html
  161. Options:
  162. --cum Sort by cumulative data
  163. --base=<base> Subtract <base> from <profile> before display
  164. --interactive Run in interactive mode (interactive "help" gives help) [default]
  165. --seconds=<n> Length of time for dynamic profiles [default=30 secs]
  166. --add_lib=<file> Read additional symbols and line info from the given library
  167. --lib_prefix=<dir> Comma separated list of library path prefixes
  168. --no_strip_temp Do not strip template arguments from function names
  169. Reporting Granularity:
  170. --addresses Report at address level
  171. --lines Report at source line level
  172. --functions Report at function level [default]
  173. --files Report at source file level
  174. Output type:
  175. --text Generate text report
  176. --stacks Generate stack traces similar to the heap profiler (requires --text)
  177. --callgrind Generate callgrind format to stdout
  178. --gv Generate Postscript and display
  179. --evince Generate PDF and display
  180. --web Generate SVG and display
  181. --list=<regexp> Generate source listing of matching routines
  182. --disasm=<regexp> Generate disassembly of matching routines
  183. --symbols Print demangled symbol names found at given addresses
  184. --dot Generate DOT file to stdout
  185. --ps Generate Postscript to stdout
  186. --pdf Generate PDF to stdout
  187. --svg Generate SVG to stdout
  188. --gif Generate GIF to stdout
  189. --raw Generate symbolized pprof data (useful with remote fetch)
  190. --collapsed Generate collapsed stacks for building flame graphs
  191. (see http://www.brendangregg.com/flamegraphs.html)
  192. Heap-Profile Options:
  193. --inuse_space Display in-use (mega)bytes [default]
  194. --inuse_objects Display in-use objects
  195. --alloc_space Display allocated (mega)bytes
  196. --alloc_objects Display allocated objects
  197. --show_bytes Display space in bytes
  198. --drop_negative Ignore negative differences
  199. Contention-profile options:
  200. --total_delay Display total delay at each region [default]
  201. --contentions Display number of delays at each region
  202. --mean_delay Display mean delay at each region
  203. Call-graph Options:
  204. --nodecount=<n> Show at most so many nodes [default=80]
  205. --nodefraction=<f> Hide nodes below <f>*total [default=.005]
  206. --edgefraction=<f> Hide edges below <f>*total [default=.001]
  207. --maxdegree=<n> Max incoming/outgoing edges per node [default=8]
  208. --focus=<regexp> Focus on nodes matching <regexp>
  209. --ignore=<regexp> Ignore nodes matching <regexp>
  210. --scale=<n> Set GV scaling [default=0]
  211. --heapcheck Make nodes with non-0 object counts
  212. (i.e. direct leak generators) more visible
  213. Miscellaneous:
  214. --no-auto-signal-frm Automatically drop 2nd frame that is always same (cpu-only)
  215. (assuming that it is artifact of bad stack captures
  216. which include signal handler frames)
  217. --show_addresses Always show addresses when applicable
  218. --tools=<prefix or binary:fullpath>[,...] \$PATH for object tool pathnames
  219. --test Run unit tests
  220. --help This message
  221. --version Version information
  222. Environment Variables:
  223. PPROF_TMPDIR Profiles directory. Defaults to \$HOME/pprof
  224. PPROF_TOOLS Prefix for object tools pathnames
  225. Examples:
  226. $0 /bin/ls ls.prof
  227. Enters "interactive" mode
  228. $0 --text /bin/ls ls.prof
  229. Outputs one line per procedure
  230. $0 --web /bin/ls ls.prof
  231. Displays annotated call-graph in web browser
  232. $0 --gv /bin/ls ls.prof
  233. Displays annotated call-graph via 'gv'
  234. $0 --gv --focus=Mutex /bin/ls ls.prof
  235. Restricts to code paths including a .*Mutex.* entry
  236. $0 --gv --focus=Mutex --ignore=string /bin/ls ls.prof
  237. Code paths including Mutex but not string
  238. $0 --list=getdir /bin/ls ls.prof
  239. (Per-line) annotated source listing for getdir()
  240. $0 --disasm=getdir /bin/ls ls.prof
  241. (Per-PC) annotated disassembly for getdir()
  242. $0 http://localhost:1234/
  243. Enters "interactive" mode
  244. $0 --text localhost:1234
  245. Outputs one line per procedure for localhost:1234
  246. $0 --raw localhost:1234 > ./local.raw
  247. $0 --text ./local.raw
  248. Fetches a remote profile for later analysis and then
  249. analyzes it in text mode.
  250. EOF
  251. }
  252. sub version_string {
  253. return <<EOF
  254. pprof (part of gperftools $PPROF_VERSION)
  255. Copyright 1998-2007 Google Inc.
  256. This is BSD licensed software; see the source for copying conditions
  257. and license information.
  258. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
  259. PARTICULAR PURPOSE.
  260. EOF
  261. }
  262. sub usage {
  263. my $msg = shift;
  264. print STDERR "$msg\n\n";
  265. print STDERR usage_string();
  266. exit(1);
  267. }
  268. sub Init() {
  269. # Setup tmp-file name and handler to clean it up.
  270. # We do this in the very beginning so that we can use
  271. # error() and cleanup() function anytime here after.
  272. $main::tmpfile_sym = "/tmp/pprof$$.sym";
  273. $main::tmpfile_ps = "/tmp/pprof$$";
  274. $main::next_tmpfile = 0;
  275. $SIG{'INT'} = \&sighandler;
  276. # Cache from filename/linenumber to source code
  277. $main::source_cache = ();
  278. $main::opt_help = 0;
  279. $main::opt_version = 0;
  280. $main::opt_show_addresses = 0;
  281. $main::opt_no_auto_signal_frames = 0;
  282. $main::opt_cum = 0;
  283. $main::opt_base = '';
  284. $main::opt_addresses = 0;
  285. $main::opt_lines = 0;
  286. $main::opt_functions = 0;
  287. $main::opt_files = 0;
  288. $main::opt_lib_prefix = "";
  289. $main::opt_text = 0;
  290. $main::opt_stacks = 0;
  291. $main::opt_callgrind = 0;
  292. $main::opt_list = "";
  293. $main::opt_disasm = "";
  294. $main::opt_symbols = 0;
  295. $main::opt_gv = 0;
  296. $main::opt_evince = 0;
  297. $main::opt_web = 0;
  298. $main::opt_dot = 0;
  299. $main::opt_ps = 0;
  300. $main::opt_pdf = 0;
  301. $main::opt_gif = 0;
  302. $main::opt_svg = 0;
  303. $main::opt_raw = 0;
  304. $main::opt_collapsed = 0;
  305. $main::opt_nodecount = 80;
  306. $main::opt_nodefraction = 0.005;
  307. $main::opt_edgefraction = 0.001;
  308. $main::opt_maxdegree = 8;
  309. $main::opt_focus = '';
  310. $main::opt_ignore = '';
  311. $main::opt_scale = 0;
  312. $main::opt_heapcheck = 0;
  313. $main::opt_seconds = 30;
  314. $main::opt_lib = "";
  315. $main::opt_inuse_space = 0;
  316. $main::opt_inuse_objects = 0;
  317. $main::opt_alloc_space = 0;
  318. $main::opt_alloc_objects = 0;
  319. $main::opt_show_bytes = 0;
  320. $main::opt_drop_negative = 0;
  321. $main::opt_interactive = 0;
  322. $main::opt_total_delay = 0;
  323. $main::opt_contentions = 0;
  324. $main::opt_mean_delay = 0;
  325. $main::opt_tools = "";
  326. $main::opt_debug = 0;
  327. $main::opt_test = 0;
  328. # Do not strip template argument in function names
  329. $main::opt_no_strip_temp = 0;
  330. # These are undocumented flags used only by unittests.
  331. $main::opt_test_stride = 0;
  332. # Are we using $SYMBOL_PAGE?
  333. $main::use_symbol_page = 0;
  334. # Files returned by TempName.
  335. %main::tempnames = ();
  336. # Type of profile we are dealing with
  337. # Supported types:
  338. # cpu
  339. # heap
  340. # growth
  341. # contention
  342. $main::profile_type = ''; # Empty type means "unknown"
  343. GetOptions("help!" => \$main::opt_help,
  344. "version!" => \$main::opt_version,
  345. "show_addresses!"=> \$main::opt_show_addresses,
  346. "no-auto-signal-frm!"=> \$main::opt_no_auto_signal_frames,
  347. "cum!" => \$main::opt_cum,
  348. "base=s" => \$main::opt_base,
  349. "seconds=i" => \$main::opt_seconds,
  350. "add_lib=s" => \$main::opt_lib,
  351. "lib_prefix=s" => \$main::opt_lib_prefix,
  352. "functions!" => \$main::opt_functions,
  353. "lines!" => \$main::opt_lines,
  354. "addresses!" => \$main::opt_addresses,
  355. "files!" => \$main::opt_files,
  356. "text!" => \$main::opt_text,
  357. "stacks!" => \$main::opt_stacks,
  358. "callgrind!" => \$main::opt_callgrind,
  359. "list=s" => \$main::opt_list,
  360. "disasm=s" => \$main::opt_disasm,
  361. "symbols!" => \$main::opt_symbols,
  362. "gv!" => \$main::opt_gv,
  363. "evince!" => \$main::opt_evince,
  364. "web!" => \$main::opt_web,
  365. "dot!" => \$main::opt_dot,
  366. "ps!" => \$main::opt_ps,
  367. "pdf!" => \$main::opt_pdf,
  368. "svg!" => \$main::opt_svg,
  369. "gif!" => \$main::opt_gif,
  370. "raw!" => \$main::opt_raw,
  371. "collapsed!" => \$main::opt_collapsed,
  372. "interactive!" => \$main::opt_interactive,
  373. "nodecount=i" => \$main::opt_nodecount,
  374. "nodefraction=f" => \$main::opt_nodefraction,
  375. "edgefraction=f" => \$main::opt_edgefraction,
  376. "maxdegree=i" => \$main::opt_maxdegree,
  377. "focus=s" => \$main::opt_focus,
  378. "ignore=s" => \$main::opt_ignore,
  379. "scale=i" => \$main::opt_scale,
  380. "heapcheck" => \$main::opt_heapcheck,
  381. "inuse_space!" => \$main::opt_inuse_space,
  382. "inuse_objects!" => \$main::opt_inuse_objects,
  383. "alloc_space!" => \$main::opt_alloc_space,
  384. "alloc_objects!" => \$main::opt_alloc_objects,
  385. "show_bytes!" => \$main::opt_show_bytes,
  386. "drop_negative!" => \$main::opt_drop_negative,
  387. "total_delay!" => \$main::opt_total_delay,
  388. "contentions!" => \$main::opt_contentions,
  389. "mean_delay!" => \$main::opt_mean_delay,
  390. "tools=s" => \$main::opt_tools,
  391. "no_strip_temp!" => \$main::opt_no_strip_temp,
  392. "test!" => \$main::opt_test,
  393. "debug!" => \$main::opt_debug,
  394. # Undocumented flags used only by unittests:
  395. "test_stride=i" => \$main::opt_test_stride,
  396. ) || usage("Invalid option(s)");
  397. # Deal with the standard --help and --version
  398. if ($main::opt_help) {
  399. print usage_string();
  400. exit(0);
  401. }
  402. if ($main::opt_version) {
  403. print version_string();
  404. exit(0);
  405. }
  406. # Disassembly/listing/symbols mode requires address-level info
  407. if ($main::opt_disasm || $main::opt_list || $main::opt_symbols) {
  408. $main::opt_functions = 0;
  409. $main::opt_lines = 0;
  410. $main::opt_addresses = 1;
  411. $main::opt_files = 0;
  412. }
  413. # Check heap-profiling flags
  414. if ($main::opt_inuse_space +
  415. $main::opt_inuse_objects +
  416. $main::opt_alloc_space +
  417. $main::opt_alloc_objects > 1) {
  418. usage("Specify at most on of --inuse/--alloc options");
  419. }
  420. # Check output granularities
  421. my $grains =
  422. $main::opt_functions +
  423. $main::opt_lines +
  424. $main::opt_addresses +
  425. $main::opt_files +
  426. 0;
  427. if ($grains > 1) {
  428. usage("Only specify one output granularity option");
  429. }
  430. if ($grains == 0) {
  431. $main::opt_functions = 1;
  432. }
  433. # Check output modes
  434. my $modes =
  435. $main::opt_text +
  436. $main::opt_callgrind +
  437. ($main::opt_list eq '' ? 0 : 1) +
  438. ($main::opt_disasm eq '' ? 0 : 1) +
  439. ($main::opt_symbols == 0 ? 0 : 1) +
  440. $main::opt_gv +
  441. $main::opt_evince +
  442. $main::opt_web +
  443. $main::opt_dot +
  444. $main::opt_ps +
  445. $main::opt_pdf +
  446. $main::opt_svg +
  447. $main::opt_gif +
  448. $main::opt_raw +
  449. $main::opt_collapsed +
  450. $main::opt_interactive +
  451. 0;
  452. if ($modes > 1) {
  453. usage("Only specify one output mode");
  454. }
  455. if ($modes == 0) {
  456. if (-t STDOUT) { # If STDOUT is a tty, activate interactive mode
  457. $main::opt_interactive = 1;
  458. } else {
  459. $main::opt_text = 1;
  460. }
  461. }
  462. if ($main::opt_test) {
  463. RunUnitTests();
  464. # Should not return
  465. exit(1);
  466. }
  467. # Binary name and profile arguments list
  468. $main::prog = "";
  469. @main::pfile_args = ();
  470. # Remote profiling without a binary (using $SYMBOL_PAGE instead)
  471. if (@ARGV > 0) {
  472. if (IsProfileURL($ARGV[0])) {
  473. printf STDERR "Using remote profile at $ARGV[0].\n";
  474. $main::use_symbol_page = 1;
  475. } elsif (IsSymbolizedProfileFile($ARGV[0])) {
  476. $main::use_symbolized_profile = 1;
  477. $main::prog = $UNKNOWN_BINARY; # will be set later from the profile file
  478. }
  479. }
  480. if ($main::use_symbol_page || $main::use_symbolized_profile) {
  481. # We don't need a binary!
  482. my %disabled = ('--lines' => $main::opt_lines,
  483. '--disasm' => $main::opt_disasm);
  484. for my $option (keys %disabled) {
  485. usage("$option cannot be used without a binary") if $disabled{$option};
  486. }
  487. # Set $main::prog later...
  488. scalar(@ARGV) || usage("Did not specify profile file");
  489. } elsif ($main::opt_symbols) {
  490. # --symbols needs a binary-name (to run nm on, etc) but not profiles
  491. $main::prog = shift(@ARGV) || usage("Did not specify program");
  492. } else {
  493. $main::prog = shift(@ARGV) || usage("Did not specify program");
  494. scalar(@ARGV) || usage("Did not specify profile file");
  495. }
  496. # Parse profile file/location arguments
  497. foreach my $farg (@ARGV) {
  498. if ($farg =~ m/(.*)\@([0-9]+)(|\/.*)$/ ) {
  499. my $machine = $1;
  500. my $num_machines = $2;
  501. my $path = $3;
  502. for (my $i = 0; $i < $num_machines; $i++) {
  503. unshift(@main::pfile_args, "$i.$machine$path");
  504. }
  505. } else {
  506. unshift(@main::pfile_args, $farg);
  507. }
  508. }
  509. if ($main::use_symbol_page) {
  510. unless (IsProfileURL($main::pfile_args[0])) {
  511. error("The first profile should be a remote form to use $SYMBOL_PAGE\n");
  512. }
  513. CheckSymbolPage();
  514. $main::prog = FetchProgramName();
  515. } elsif (!$main::use_symbolized_profile) { # may not need objtools!
  516. ConfigureObjTools($main::prog)
  517. }
  518. # Break the opt_lib_prefix into the prefix_list array
  519. @prefix_list = split (',', $main::opt_lib_prefix);
  520. # Remove trailing / from the prefixes, in the list to prevent
  521. # searching things like /my/path//lib/mylib.so
  522. foreach (@prefix_list) {
  523. s|/+$||;
  524. }
  525. }
  526. sub Main() {
  527. Init();
  528. $main::collected_profile = undef;
  529. @main::profile_files = ();
  530. $main::op_time = time();
  531. # Printing symbols is special and requires a lot less info that most.
  532. if ($main::opt_symbols) {
  533. PrintSymbols(*STDIN); # Get /proc/maps and symbols output from stdin
  534. return;
  535. }
  536. # Fetch all profile data
  537. FetchDynamicProfiles();
  538. # this will hold symbols that we read from the profile files
  539. my $symbol_map = {};
  540. # Read one profile, pick the last item on the list
  541. my $data = ReadProfile($main::prog, pop(@main::profile_files));
  542. my $profile = $data->{profile};
  543. my $pcs = $data->{pcs};
  544. my $libs = $data->{libs}; # Info about main program and shared libraries
  545. $symbol_map = MergeSymbols($symbol_map, $data->{symbols});
  546. # Add additional profiles, if available.
  547. if (scalar(@main::profile_files) > 0) {
  548. foreach my $pname (@main::profile_files) {
  549. my $data2 = ReadProfile($main::prog, $pname);
  550. $profile = AddProfile($profile, $data2->{profile});
  551. $pcs = AddPcs($pcs, $data2->{pcs});
  552. $symbol_map = MergeSymbols($symbol_map, $data2->{symbols});
  553. }
  554. }
  555. # Subtract base from profile, if specified
  556. if ($main::opt_base ne '') {
  557. my $base = ReadProfile($main::prog, $main::opt_base);
  558. $profile = SubtractProfile($profile, $base->{profile});
  559. $pcs = AddPcs($pcs, $base->{pcs});
  560. $symbol_map = MergeSymbols($symbol_map, $base->{symbols});
  561. }
  562. # Get total data in profile
  563. my $total = TotalProfile($profile);
  564. # Collect symbols
  565. my $symbols;
  566. if ($main::use_symbolized_profile) {
  567. $symbols = FetchSymbols($pcs, $symbol_map);
  568. } elsif ($main::use_symbol_page) {
  569. $symbols = FetchSymbols($pcs);
  570. } else {
  571. # TODO(csilvers): $libs uses the /proc/self/maps data from profile1,
  572. # which may differ from the data from subsequent profiles, especially
  573. # if they were run on different machines. Use appropriate libs for
  574. # each pc somehow.
  575. $symbols = ExtractSymbols($libs, $pcs);
  576. }
  577. # Remove uniniteresting stack items
  578. $profile = RemoveUninterestingFrames($symbols, $profile);
  579. # Focus?
  580. if ($main::opt_focus ne '') {
  581. $profile = FocusProfile($symbols, $profile, $main::opt_focus);
  582. }
  583. # Ignore?
  584. if ($main::opt_ignore ne '') {
  585. $profile = IgnoreProfile($symbols, $profile, $main::opt_ignore);
  586. }
  587. my $calls = ExtractCalls($symbols, $profile);
  588. # Reduce profiles to required output granularity, and also clean
  589. # each stack trace so a given entry exists at most once.
  590. my $reduced = ReduceProfile($symbols, $profile);
  591. # Get derived profiles
  592. my $flat = FlatProfile($reduced);
  593. my $cumulative = CumulativeProfile($reduced);
  594. # Print
  595. if (!$main::opt_interactive) {
  596. if ($main::opt_disasm) {
  597. PrintDisassembly($libs, $flat, $cumulative, $main::opt_disasm);
  598. } elsif ($main::opt_list) {
  599. PrintListing($total, $libs, $flat, $cumulative, $main::opt_list, 0);
  600. } elsif ($main::opt_text) {
  601. # Make sure the output is empty when have nothing to report
  602. # (only matters when --heapcheck is given but we must be
  603. # compatible with old branches that did not pass --heapcheck always):
  604. if ($total != 0) {
  605. printf("Total: %s %s\n", Unparse($total), Units());
  606. }
  607. if ($main::opt_stacks) {
  608. printf("Stacks:\n\n");
  609. PrintStacksForText($symbols, $profile);
  610. }
  611. PrintText($symbols, $flat, $cumulative, -1);
  612. } elsif ($main::opt_raw) {
  613. PrintSymbolizedProfile($symbols, $profile, $main::prog);
  614. } elsif ($main::opt_collapsed) {
  615. PrintCollapsedStacks($symbols, $profile);
  616. } elsif ($main::opt_callgrind) {
  617. PrintCallgrind($calls);
  618. } else {
  619. if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {
  620. if ($main::opt_gv) {
  621. RunGV(TempName($main::next_tmpfile, "ps"), "");
  622. } elsif ($main::opt_evince) {
  623. RunEvince(TempName($main::next_tmpfile, "pdf"), "");
  624. } elsif ($main::opt_web) {
  625. my $tmp = TempName($main::next_tmpfile, "svg");
  626. RunWeb($tmp);
  627. # The command we run might hand the file name off
  628. # to an already running browser instance and then exit.
  629. # Normally, we'd remove $tmp on exit (right now),
  630. # but fork a child to remove $tmp a little later, so that the
  631. # browser has time to load it first.
  632. delete $main::tempnames{$tmp};
  633. if (fork() == 0) {
  634. sleep 5;
  635. unlink($tmp);
  636. exit(0);
  637. }
  638. }
  639. } else {
  640. cleanup();
  641. exit(1);
  642. }
  643. }
  644. } else {
  645. InteractiveMode($profile, $symbols, $libs, $total);
  646. }
  647. cleanup();
  648. exit(0);
  649. }
  650. ##### Entry Point #####
  651. Main();
  652. # Temporary code to detect if we're running on a Goobuntu system.
  653. # These systems don't have the right stuff installed for the special
  654. # Readline libraries to work, so as a temporary workaround, we default
  655. # to using the normal stdio code, rather than the fancier readline-based
  656. # code
  657. sub ReadlineMightFail {
  658. if (-e '/lib/libtermcap.so.2') {
  659. return 0; # libtermcap exists, so readline should be okay
  660. } else {
  661. return 1;
  662. }
  663. }
  664. sub RunGV {
  665. my $fname = shift;
  666. my $bg = shift; # "" or " &" if we should run in background
  667. if (!system(ShellEscape(@GV, "--version") . " >$dev_null 2>&1")) {
  668. # Options using double dash are supported by this gv version.
  669. # Also, turn on noantialias to better handle bug in gv for
  670. # postscript files with large dimensions.
  671. # TODO: Maybe we should not pass the --noantialias flag
  672. # if the gv version is known to work properly without the flag.
  673. system(ShellEscape(@GV, "--scale=$main::opt_scale", "--noantialias", $fname)
  674. . $bg);
  675. } else {
  676. # Old gv version - only supports options that use single dash.
  677. print STDERR ShellEscape(@GV, "-scale", $main::opt_scale) . "\n";
  678. system(ShellEscape(@GV, "-scale", "$main::opt_scale", $fname) . $bg);
  679. }
  680. }
  681. sub RunEvince {
  682. my $fname = shift;
  683. my $bg = shift; # "" or " &" if we should run in background
  684. system(ShellEscape(@EVINCE, $fname) . $bg);
  685. }
  686. sub RunWeb {
  687. my $fname = shift;
  688. print STDERR "Loading web page file:///$fname\n";
  689. if (`uname` =~ /Darwin/) {
  690. # OS X: open will use standard preference for SVG files.
  691. system("/usr/bin/open", $fname);
  692. return;
  693. }
  694. if (`uname` =~ /MINGW/) {
  695. # Windows(MinGW): open will use standard preference for SVG files.
  696. system("cmd", "/c", "start", $fname);
  697. return;
  698. }
  699. # Some kind of Unix; try generic symlinks, then specific browsers.
  700. # (Stop once we find one.)
  701. # Works best if the browser is already running.
  702. my @alt = (
  703. "/etc/alternatives/gnome-www-browser",
  704. "/etc/alternatives/x-www-browser",
  705. "google-chrome",
  706. "firefox",
  707. );
  708. foreach my $b (@alt) {
  709. if (system($b, $fname) == 0) {
  710. return;
  711. }
  712. }
  713. print STDERR "Could not load web browser.\n";
  714. }
  715. sub RunKcachegrind {
  716. my $fname = shift;
  717. my $bg = shift; # "" or " &" if we should run in background
  718. print STDERR "Starting '@KCACHEGRIND " . $fname . $bg . "'\n";
  719. system(ShellEscape(@KCACHEGRIND, $fname) . $bg);
  720. }
  721. ##### Interactive helper routines #####
  722. sub InteractiveMode {
  723. $| = 1; # Make output unbuffered for interactive mode
  724. my ($orig_profile, $symbols, $libs, $total) = @_;
  725. print STDERR "Welcome to pprof! For help, type 'help'.\n";
  726. # Use ReadLine if it's installed and input comes from a console.
  727. if ( -t STDIN &&
  728. !ReadlineMightFail() &&
  729. defined(eval {require Term::ReadLine}) ) {
  730. my $term = new Term::ReadLine 'pprof';
  731. while ( defined ($_ = $term->readline('(pprof) '))) {
  732. $term->addhistory($_) if /\S/;
  733. if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {
  734. last; # exit when we get an interactive command to quit
  735. }
  736. }
  737. } else { # don't have readline
  738. while (1) {
  739. print STDERR "(pprof) ";
  740. $_ = <STDIN>;
  741. last if ! defined $_ ;
  742. s/\r//g; # turn windows-looking lines into unix-looking lines
  743. # Save some flags that might be reset by InteractiveCommand()
  744. my $save_opt_lines = $main::opt_lines;
  745. if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {
  746. last; # exit when we get an interactive command to quit
  747. }
  748. # Restore flags
  749. $main::opt_lines = $save_opt_lines;
  750. }
  751. }
  752. }
  753. # Takes two args: orig profile, and command to run.
  754. # Returns 1 if we should keep going, or 0 if we were asked to quit
  755. sub InteractiveCommand {
  756. my($orig_profile, $symbols, $libs, $total, $command) = @_;
  757. $_ = $command; # just to make future m//'s easier
  758. if (!defined($_)) {
  759. print STDERR "\n";
  760. return 0;
  761. }
  762. if (m/^\s*quit/) {
  763. return 0;
  764. }
  765. if (m/^\s*help/) {
  766. InteractiveHelpMessage();
  767. return 1;
  768. }
  769. # Clear all the mode options -- mode is controlled by "$command"
  770. $main::opt_text = 0;
  771. $main::opt_callgrind = 0;
  772. $main::opt_disasm = 0;
  773. $main::opt_list = 0;
  774. $main::opt_gv = 0;
  775. $main::opt_evince = 0;
  776. $main::opt_cum = 0;
  777. if (m/^\s*(text|top)(\d*)\s*(.*)/) {
  778. $main::opt_text = 1;
  779. my $line_limit = ($2 ne "") ? int($2) : 10;
  780. my $routine;
  781. my $ignore;
  782. ($routine, $ignore) = ParseInteractiveArgs($3);
  783. my $profile = ProcessProfile($total, $orig_profile, $symbols, "", $ignore);
  784. my $reduced = ReduceProfile($symbols, $profile);
  785. # Get derived profiles
  786. my $flat = FlatProfile($reduced);
  787. my $cumulative = CumulativeProfile($reduced);
  788. PrintText($symbols, $flat, $cumulative, $line_limit);
  789. return 1;
  790. }
  791. if (m/^\s*callgrind\s*([^ \n]*)/) {
  792. $main::opt_callgrind = 1;
  793. # Get derived profiles
  794. my $calls = ExtractCalls($symbols, $orig_profile);
  795. my $filename = $1;
  796. if ( $1 eq '' ) {
  797. $filename = TempName($main::next_tmpfile, "callgrind");
  798. }
  799. PrintCallgrind($calls, $filename);
  800. if ( $1 eq '' ) {
  801. RunKcachegrind($filename, " & ");
  802. $main::next_tmpfile++;
  803. }
  804. return 1;
  805. }
  806. if (m/^\s*(web)?list\s*(.+)/) {
  807. my $html = (defined($1) && ($1 eq "web"));
  808. $main::opt_list = 1;
  809. my $routine;
  810. my $ignore;
  811. ($routine, $ignore) = ParseInteractiveArgs($2);
  812. my $profile = ProcessProfile($total, $orig_profile, $symbols, "", $ignore);
  813. my $reduced = ReduceProfile($symbols, $profile);
  814. # Get derived profiles
  815. my $flat = FlatProfile($reduced);
  816. my $cumulative = CumulativeProfile($reduced);
  817. PrintListing($total, $libs, $flat, $cumulative, $routine, $html);
  818. return 1;
  819. }
  820. if (m/^\s*disasm\s*(.+)/) {
  821. $main::opt_disasm = 1;
  822. my $routine;
  823. my $ignore;
  824. ($routine, $ignore) = ParseInteractiveArgs($1);
  825. # Process current profile to account for various settings
  826. my $profile = ProcessProfile($total, $orig_profile, $symbols, "", $ignore);
  827. my $reduced = ReduceProfile($symbols, $profile);
  828. # Get derived profiles
  829. my $flat = FlatProfile($reduced);
  830. my $cumulative = CumulativeProfile($reduced);
  831. PrintDisassembly($libs, $flat, $cumulative, $routine);
  832. return 1;
  833. }
  834. if (m/^\s*(gv|web|evince)\s*(.*)/) {
  835. $main::opt_gv = 0;
  836. $main::opt_evince = 0;
  837. $main::opt_web = 0;
  838. if ($1 eq "gv") {
  839. $main::opt_gv = 1;
  840. } elsif ($1 eq "evince") {
  841. $main::opt_evince = 1;
  842. } elsif ($1 eq "web") {
  843. $main::opt_web = 1;
  844. }
  845. my $focus;
  846. my $ignore;
  847. ($focus, $ignore) = ParseInteractiveArgs($2);
  848. # Process current profile to account for various settings
  849. my $profile = ProcessProfile($total, $orig_profile, $symbols,
  850. $focus, $ignore);
  851. my $reduced = ReduceProfile($symbols, $profile);
  852. # Get derived profiles
  853. my $flat = FlatProfile($reduced);
  854. my $cumulative = CumulativeProfile($reduced);
  855. if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {
  856. if ($main::opt_gv) {
  857. RunGV(TempName($main::next_tmpfile, "ps"), " &");
  858. } elsif ($main::opt_evince) {
  859. RunEvince(TempName($main::next_tmpfile, "pdf"), " &");
  860. } elsif ($main::opt_web) {
  861. RunWeb(TempName($main::next_tmpfile, "svg"));
  862. }
  863. $main::next_tmpfile++;
  864. }
  865. return 1;
  866. }
  867. if (m/^\s*$/) {
  868. return 1;
  869. }
  870. print STDERR "Unknown command: try 'help'.\n";
  871. return 1;
  872. }
  873. sub ProcessProfile {
  874. my $total_count = shift;
  875. my $orig_profile = shift;
  876. my $symbols = shift;
  877. my $focus = shift;
  878. my $ignore = shift;
  879. # Process current profile to account for various settings
  880. my $profile = $orig_profile;
  881. printf("Total: %s %s\n", Unparse($total_count), Units());
  882. if ($focus ne '') {
  883. $profile = FocusProfile($symbols, $profile, $focus);
  884. my $focus_count = TotalProfile($profile);
  885. printf("After focusing on '%s': %s %s of %s (%0.1f%%)\n",
  886. $focus,
  887. Unparse($focus_count), Units(),
  888. Unparse($total_count), ($focus_count*100.0) / $total_count);
  889. }
  890. if ($ignore ne '') {
  891. $profile = IgnoreProfile($symbols, $profile, $ignore);
  892. my $ignore_count = TotalProfile($profile);
  893. printf("After ignoring '%s': %s %s of %s (%0.1f%%)\n",
  894. $ignore,
  895. Unparse($ignore_count), Units(),
  896. Unparse($total_count),
  897. ($ignore_count*100.0) / $total_count);
  898. }
  899. return $profile;
  900. }
  901. sub InteractiveHelpMessage {
  902. print STDERR <<ENDOFHELP;
  903. Interactive pprof mode
  904. Commands:
  905. gv
  906. gv [focus] [-ignore1] [-ignore2]
  907. Show graphical hierarchical display of current profile. Without
  908. any arguments, shows all samples in the profile. With the optional
  909. "focus" argument, restricts the samples shown to just those where
  910. the "focus" regular expression matches a routine name on the stack
  911. trace.
  912. web
  913. web [focus] [-ignore1] [-ignore2]
  914. Like GV, but displays profile in your web browser instead of using
  915. Ghostview. Works best if your web browser is already running.
  916. To change the browser that gets used:
  917. On Linux, set the /etc/alternatives/gnome-www-browser symlink.
  918. On OS X, change the Finder association for SVG files.
  919. list [routine_regexp] [-ignore1] [-ignore2]
  920. Show source listing of routines whose names match "routine_regexp"
  921. weblist [routine_regexp] [-ignore1] [-ignore2]
  922. Displays a source listing of routines whose names match "routine_regexp"
  923. in a web browser. You can click on source lines to view the
  924. corresponding disassembly.
  925. top [--cum] [-ignore1] [-ignore2]
  926. top20 [--cum] [-ignore1] [-ignore2]
  927. top37 [--cum] [-ignore1] [-ignore2]
  928. Show top lines ordered by flat profile count, or cumulative count
  929. if --cum is specified. If a number is present after 'top', the
  930. top K routines will be shown (defaults to showing the top 10)
  931. disasm [routine_regexp] [-ignore1] [-ignore2]
  932. Show disassembly of routines whose names match "routine_regexp",
  933. annotated with sample counts.
  934. callgrind
  935. callgrind [filename]
  936. Generates callgrind file. If no filename is given, kcachegrind is called.
  937. help - This listing
  938. quit or ^D - End pprof
  939. For commands that accept optional -ignore tags, samples where any routine in
  940. the stack trace matches the regular expression in any of the -ignore
  941. parameters will be ignored.
  942. Further pprof details are available at this location (or one similar):
  943. /usr/doc/gperftools-$PPROF_VERSION/cpu_profiler.html
  944. /usr/doc/gperftools-$PPROF_VERSION/heap_profiler.html
  945. ENDOFHELP
  946. }
  947. sub ParseInteractiveArgs {
  948. my $args = shift;
  949. my $focus = "";
  950. my $ignore = "";
  951. my @x = split(/ +/, $args);
  952. foreach $a (@x) {
  953. if ($a =~ m/^(--|-)lines$/) {
  954. $main::opt_lines = 1;
  955. } elsif ($a =~ m/^(--|-)cum$/) {
  956. $main::opt_cum = 1;
  957. } elsif ($a =~ m/^-(.*)/) {
  958. $ignore .= (($ignore ne "") ? "|" : "" ) . $1;
  959. } else {
  960. $focus .= (($focus ne "") ? "|" : "" ) . $a;
  961. }
  962. }
  963. if ($ignore ne "") {
  964. print STDERR "Ignoring samples in call stacks that match '$ignore'\n";
  965. }
  966. return ($focus, $ignore);
  967. }
  968. ##### Output code #####
  969. sub TempName {
  970. my $fnum = shift;
  971. my $ext = shift;
  972. my $file = "$main::tmpfile_ps.$fnum.$ext";
  973. $main::tempnames{$file} = 1;
  974. return $file;
  975. }
  976. # Print profile data in packed binary format (64-bit) to standard out
  977. sub PrintProfileData {
  978. my $profile = shift;
  979. my $big_endian = pack("L", 1) eq pack("N", 1);
  980. # print header (64-bit style)
  981. # (zero) (header-size) (version) (sample-period) (zero)
  982. if ($big_endian) {
  983. print pack('L*', 0, 0, 0, 3, 0, 0, 0, 1, 0, 0);
  984. }
  985. else {
  986. print pack('L*', 0, 0, 3, 0, 0, 0, 1, 0, 0, 0);
  987. }
  988. foreach my $k (keys(%{$profile})) {
  989. my $count = $profile->{$k};
  990. my @addrs = split(/\n/, $k);
  991. if ($#addrs >= 0) {
  992. my $depth = $#addrs + 1;
  993. # int(foo / 2**32) is the only reliable way to get rid of bottom
  994. # 32 bits on both 32- and 64-bit systems.
  995. if ($big_endian) {
  996. print pack('L*', int($count / 2**32), $count & 0xFFFFFFFF);
  997. print pack('L*', int($depth / 2**32), $depth & 0xFFFFFFFF);
  998. }
  999. else {
  1000. print pack('L*', $count & 0xFFFFFFFF, int($count / 2**32));
  1001. print pack('L*', $depth & 0xFFFFFFFF, int($depth / 2**32));
  1002. }
  1003. foreach my $full_addr (@addrs) {
  1004. my $addr = $full_addr;
  1005. $addr =~ s/0x0*//; # strip off leading 0x, zeroes
  1006. if (length($addr) > 16) {
  1007. print STDERR "Invalid address in profile: $full_addr\n";
  1008. next;
  1009. }
  1010. my $low_addr = substr($addr, -8); # get last 8 hex chars
  1011. my $high_addr = substr($addr, -16, 8); # get up to 8 more hex chars
  1012. if ($big_endian) {
  1013. print pack('L*', hex('0x' . $high_addr), hex('0x' . $low_addr));
  1014. }
  1015. else {
  1016. print pack('L*', hex('0x' . $low_addr), hex('0x' . $high_addr));
  1017. }
  1018. }
  1019. }
  1020. }
  1021. }
  1022. # Print symbols and profile data
  1023. sub PrintSymbolizedProfile {
  1024. my $symbols = shift;
  1025. my $profile = shift;
  1026. my $prog = shift;
  1027. $SYMBOL_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  1028. my $symbol_marker = $&;
  1029. print '--- ', $symbol_marker, "\n";
  1030. if (defined($prog)) {
  1031. print 'binary=', $prog, "\n";
  1032. }
  1033. while (my ($pc, $name) = each(%{$symbols})) {
  1034. my $sep = ' ';
  1035. print '0x', $pc;
  1036. # We have a list of function names, which include the inlined
  1037. # calls. They are separated (and terminated) by --, which is
  1038. # illegal in function names.
  1039. for (my $j = 2; $j <= $#{$name}; $j += 3) {
  1040. print $sep, $name->[$j];
  1041. $sep = '--';
  1042. }
  1043. print "\n";
  1044. }
  1045. print '---', "\n";
  1046. $PROFILE_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  1047. my $profile_marker = $&;
  1048. print '--- ', $profile_marker, "\n";
  1049. if (defined($main::collected_profile)) {
  1050. # if used with remote fetch, simply dump the collected profile to output.
  1051. open(SRC, "<$main::collected_profile");
  1052. while (<SRC>) {
  1053. print $_;
  1054. }
  1055. close(SRC);
  1056. } else {
  1057. # dump a cpu-format profile to standard out
  1058. PrintProfileData($profile);
  1059. }
  1060. }
  1061. # Print text output
  1062. sub PrintText {
  1063. my $symbols = shift;
  1064. my $flat = shift;
  1065. my $cumulative = shift;
  1066. my $line_limit = shift;
  1067. if ($main::opt_stacks && @stackTraces) {
  1068. foreach (sort { (split " ", $b)[1] <=> (split " ", $a)[1]; } @stackTraces) {
  1069. print "$_\n" if $main::opt_debug;
  1070. my ($n1, $s1, $n2, $s2, @addrs) = split;
  1071. print "Leak of $s1 bytes in $n1 objects allocated from:\n";
  1072. foreach my $pcstr (@addrs) {
  1073. $pcstr =~ s/^0x//;
  1074. my $sym;
  1075. if (! defined $symbols->{$pcstr}) {
  1076. $sym = "unknown";
  1077. } else {
  1078. $sym = "$symbols->{$pcstr}[0] $symbols->{$pcstr}[1]";
  1079. }
  1080. print "\t@ $pcstr $sym\n";
  1081. }
  1082. }
  1083. print "\n";
  1084. }
  1085. my $total = TotalProfile($flat);
  1086. # Which profile to sort by?
  1087. my $s = $main::opt_cum ? $cumulative : $flat;
  1088. my $running_sum = 0;
  1089. my $lines = 0;
  1090. foreach my $k (sort { GetEntry($s, $b) <=> GetEntry($s, $a) || $a cmp $b }
  1091. keys(%{$cumulative})) {
  1092. my $f = GetEntry($flat, $k);
  1093. my $c = GetEntry($cumulative, $k);
  1094. $running_sum += $f;
  1095. my $sym = $k;
  1096. if (exists($symbols->{$k})) {
  1097. $sym = $symbols->{$k}->[0] . " " . $symbols->{$k}->[1];
  1098. if ($main::opt_addresses) {
  1099. $sym = $k . " " . $sym;
  1100. }
  1101. }
  1102. if ($f != 0 || $c != 0) {
  1103. printf("%8s %6s %6s %8s %6s %s\n",
  1104. Unparse($f),
  1105. Percent($f, $total),
  1106. Percent($running_sum, $total),
  1107. Unparse($c),
  1108. Percent($c, $total),
  1109. $sym);
  1110. }
  1111. $lines++;
  1112. last if ($line_limit >= 0 && $lines >= $line_limit);
  1113. }
  1114. }
  1115. # Callgrind format has a compression for repeated function and file
  1116. # names. You show the name the first time, and just use its number
  1117. # subsequently. This can cut down the file to about a third or a
  1118. # quarter of its uncompressed size. $key and $val are the key/value
  1119. # pair that would normally be printed by callgrind; $map is a map from
  1120. # value to number.
  1121. sub CompressedCGName {
  1122. my($key, $val, $map) = @_;
  1123. my $idx = $map->{$val};
  1124. # For very short keys, providing an index hurts rather than helps.
  1125. if (length($val) <= 3) {
  1126. return "$key=$val\n";
  1127. } elsif (defined($idx)) {
  1128. return "$key=($idx)\n";
  1129. } else {
  1130. # scalar(keys $map) gives the number of items in the map.
  1131. $idx = scalar(keys(%{$map})) + 1;
  1132. $map->{$val} = $idx;
  1133. return "$key=($idx) $val\n";
  1134. }
  1135. }
  1136. # Print the call graph in a way that's suiteable for callgrind.
  1137. sub PrintCallgrind {
  1138. my $calls = shift;
  1139. my $filename;
  1140. my %filename_to_index_map;
  1141. my %fnname_to_index_map;
  1142. if ($main::opt_interactive) {
  1143. $filename = shift;
  1144. print STDERR "Writing callgrind file to '$filename'.\n"
  1145. } else {
  1146. $filename = "&STDOUT";
  1147. }
  1148. open(CG, ">$filename");
  1149. print CG ("events: Hits\n\n");
  1150. foreach my $call ( map { $_->[0] }
  1151. sort { $a->[1] cmp $b ->[1] ||
  1152. $a->[2] <=> $b->[2] }
  1153. map { /([^:]+):(\d+):([^ ]+)( -> ([^:]+):(\d+):(.+))?/;
  1154. [$_, $1, $2] }
  1155. keys %$calls ) {
  1156. my $count = int($calls->{$call});
  1157. $call =~ /([^:]+):(\d+):([^ ]+)( -> ([^:]+):(\d+):(.+))?/;
  1158. my ( $caller_file, $caller_line, $caller_function,
  1159. $callee_file, $callee_line, $callee_function ) =
  1160. ( $1, $2, $3, $5, $6, $7 );
  1161. # TODO(csilvers): for better compression, collect all the
  1162. # caller/callee_files and functions first, before printing
  1163. # anything, and only compress those referenced more than once.
  1164. print CG CompressedCGName("fl", $caller_file, \%filename_to_index_map);
  1165. print CG CompressedCGName("fn", $caller_function, \%fnname_to_index_map);
  1166. if (defined $6) {
  1167. print CG CompressedCGName("cfl", $callee_file, \%filename_to_index_map);
  1168. print CG CompressedCGName("cfn", $callee_function, \%fnname_to_index_map);
  1169. print CG ("calls=$count $callee_line\n");
  1170. }
  1171. print CG ("$caller_line $count\n\n");
  1172. }
  1173. }
  1174. # Print disassembly for all all routines that match $main::opt_disasm
  1175. sub PrintDisassembly {
  1176. my $libs = shift;
  1177. my $flat = shift;
  1178. my $cumulative = shift;
  1179. my $disasm_opts = shift;
  1180. my $total = TotalProfile($flat);
  1181. foreach my $lib (@{$libs}) {
  1182. my $symbol_table = GetProcedureBoundaries($lib->[0], $disasm_opts);
  1183. my $offset = AddressSub($lib->[1], $lib->[3]);
  1184. foreach my $routine (sort ByName keys(%{$symbol_table})) {
  1185. my $start_addr = $symbol_table->{$routine}->[0];
  1186. my $end_addr = $symbol_table->{$routine}->[1];
  1187. # See if there are any samples in this routine
  1188. my $length = hex(AddressSub($end_addr, $start_addr));
  1189. my $addr = AddressAdd($start_addr, $offset);
  1190. for (my $i = 0; $i < $length; $i++) {
  1191. if (defined($cumulative->{$addr})) {
  1192. PrintDisassembledFunction($lib->[0], $offset,
  1193. $routine, $flat, $cumulative,
  1194. $start_addr, $end_addr, $total);
  1195. last;
  1196. }
  1197. $addr = AddressInc($addr);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. # Return reference to array of tuples of the form:
  1203. # [start_address, filename, linenumber, instruction, limit_address]
  1204. # E.g.,
  1205. # ["0x806c43d", "/foo/bar.cc", 131, "ret", "0x806c440"]
  1206. sub Disassemble {
  1207. my $prog = shift;
  1208. my $offset = shift;
  1209. my $start_addr = shift;
  1210. my $end_addr = shift;
  1211. my $objdump = $obj_tool_map{"objdump"};
  1212. my $cmd = ShellEscape($objdump, "-C", "-d", "-l", "--no-show-raw-insn",
  1213. "--start-address=0x$start_addr",
  1214. "--stop-address=0x$end_addr", $prog);
  1215. open(OBJDUMP, "$cmd |") || error("$cmd: $!\n");
  1216. my @result = ();
  1217. my $filename = "";
  1218. my $linenumber = -1;
  1219. my $last = ["", "", "", ""];
  1220. while (<OBJDUMP>) {
  1221. s/\r//g; # turn windows-looking lines into unix-looking lines
  1222. chop;
  1223. if (m|\s*([^:\s]+):(\d+)\s*$|) {
  1224. # Location line of the form:
  1225. # <filename>:<linenumber>
  1226. $filename = $1;
  1227. $linenumber = $2;
  1228. } elsif (m/^ +([0-9a-f]+):\s*(.*)/) {
  1229. # Disassembly line -- zero-extend address to full length
  1230. my $addr = HexExtend($1);
  1231. my $k = AddressAdd($addr, $offset);
  1232. $last->[4] = $k; # Store ending address for previous instruction
  1233. $last = [$k, $filename, $linenumber, $2, $end_addr];
  1234. push(@result, $last);
  1235. }
  1236. }
  1237. close(OBJDUMP);
  1238. return @result;
  1239. }
  1240. # The input file should contain lines of the form /proc/maps-like
  1241. # output (same format as expected from the profiles) or that looks
  1242. # like hex addresses (like "0xDEADBEEF"). We will parse all
  1243. # /proc/maps output, and for all the hex addresses, we will output
  1244. # "short" symbol names, one per line, in the same order as the input.
  1245. sub PrintSymbols {
  1246. my $maps_and_symbols_file = shift;
  1247. # ParseLibraries expects pcs to be in a set. Fine by us...
  1248. my @pclist = (); # pcs in sorted order
  1249. my $pcs = {};
  1250. my $map = "";
  1251. foreach my $line (<$maps_and_symbols_file>) {
  1252. $line =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  1253. if ($line =~ /\b(0x[0-9a-f]+)\b/i) {
  1254. push(@pclist, HexExtend($1));
  1255. $pcs->{$pclist[-1]} = 1;
  1256. } else {
  1257. $map .= $line;
  1258. }
  1259. }
  1260. my $libs = ParseLibraries($main::prog, $map, $pcs);
  1261. my $symbols = ExtractSymbols($libs, $pcs);
  1262. foreach my $pc (@pclist) {
  1263. # ->[0] is the shortname, ->[2] is the full name
  1264. print(($symbols->{$pc}->[0] || "??") . "\n");
  1265. }
  1266. }
  1267. # For sorting functions by name
  1268. sub ByName {
  1269. return ShortFunctionName($a) cmp ShortFunctionName($b);
  1270. }
  1271. # Print source-listing for all all routines that match $list_opts
  1272. sub PrintListing {
  1273. my $total = shift;
  1274. my $libs = shift;
  1275. my $flat = shift;
  1276. my $cumulative = shift;
  1277. my $list_opts = shift;
  1278. my $html = shift;
  1279. my $output = \*STDOUT;
  1280. my $fname = "";
  1281. if ($html) {
  1282. # Arrange to write the output to a temporary file
  1283. $fname = TempName($main::next_tmpfile, "html");
  1284. $main::next_tmpfile++;
  1285. if (!open(TEMP, ">$fname")) {
  1286. print STDERR "$fname: $!\n";
  1287. return;
  1288. }
  1289. $output = \*TEMP;
  1290. print $output HtmlListingHeader();
  1291. printf $output ("<div class=\"legend\">%s<br>Total: %s %s</div>\n",
  1292. $main::prog, Unparse($total), Units());
  1293. }
  1294. my $listed = 0;
  1295. foreach my $lib (@{$libs}) {
  1296. my $symbol_table = GetProcedureBoundaries($lib->[0], $list_opts);
  1297. my $offset = AddressSub($lib->[1], $lib->[3]);
  1298. foreach my $routine (sort ByName keys(%{$symbol_table})) {
  1299. # Print if there are any samples in this routine
  1300. my $start_addr = $symbol_table->{$routine}->[0];
  1301. my $end_addr = $symbol_table->{$routine}->[1];
  1302. my $length = hex(AddressSub($end_addr, $start_addr));
  1303. my $addr = AddressAdd($start_addr, $offset);
  1304. for (my $i = 0; $i < $length; $i++) {
  1305. if (defined($cumulative->{$addr})) {
  1306. $listed += PrintSource(
  1307. $lib->[0], $offset,
  1308. $routine, $flat, $cumulative,
  1309. $start_addr, $end_addr,
  1310. $html,
  1311. $output);
  1312. last;
  1313. }
  1314. $addr = AddressInc($addr);
  1315. }
  1316. }
  1317. }
  1318. if ($html) {
  1319. if ($listed > 0) {
  1320. print $output HtmlListingFooter();
  1321. close($output);
  1322. RunWeb($fname);
  1323. } else {
  1324. close($output);
  1325. unlink($fname);
  1326. }
  1327. }
  1328. }
  1329. sub HtmlListingHeader {
  1330. return <<'EOF';
  1331. <DOCTYPE html>
  1332. <html>
  1333. <head>
  1334. <title>Pprof listing</title>
  1335. <style type="text/css">
  1336. body {
  1337. font-family: sans-serif;
  1338. }
  1339. h1 {
  1340. font-size: 1.5em;
  1341. margin-bottom: 4px;
  1342. }
  1343. .legend {
  1344. font-size: 1.25em;
  1345. }
  1346. .line {
  1347. color: #aaaaaa;
  1348. }
  1349. .nop {
  1350. color: #aaaaaa;
  1351. }
  1352. .unimportant {
  1353. color: #cccccc;
  1354. }
  1355. .disasmloc {
  1356. color: #000000;
  1357. }
  1358. .deadsrc {
  1359. cursor: pointer;
  1360. }
  1361. .deadsrc:hover {
  1362. background-color: #eeeeee;
  1363. }
  1364. .livesrc {
  1365. color: #0000ff;
  1366. cursor: pointer;
  1367. }
  1368. .livesrc:hover {
  1369. background-color: #eeeeee;
  1370. }
  1371. .asm {
  1372. color: #008800;
  1373. display: none;
  1374. }
  1375. </style>
  1376. <script type="text/javascript">
  1377. function pprof_toggle_asm(e) {
  1378. var target;
  1379. if (!e) e = window.event;
  1380. if (e.target) target = e.target;
  1381. else if (e.srcElement) target = e.srcElement;
  1382. if (target) {
  1383. var asm = target.nextSibling;
  1384. if (asm && asm.className == "asm") {
  1385. asm.style.display = (asm.style.display == "block" ? "" : "block");
  1386. e.preventDefault();
  1387. return false;
  1388. }
  1389. }
  1390. }
  1391. </script>
  1392. </head>
  1393. <body>
  1394. EOF
  1395. }
  1396. sub HtmlListingFooter {
  1397. return <<'EOF';
  1398. </body>
  1399. </html>
  1400. EOF
  1401. }
  1402. sub HtmlEscape {
  1403. my $text = shift;
  1404. $text =~ s/&/&amp;/g;
  1405. $text =~ s/</&lt;/g;
  1406. $text =~ s/>/&gt;/g;
  1407. return $text;
  1408. }
  1409. # Returns the indentation of the line, if it has any non-whitespace
  1410. # characters. Otherwise, returns -1.
  1411. sub Indentation {
  1412. my $line = shift;
  1413. if (m/^(\s*)\S/) {
  1414. return length($1);
  1415. } else {
  1416. return -1;
  1417. }
  1418. }
  1419. # If the symbol table contains inlining info, Disassemble() may tag an
  1420. # instruction with a location inside an inlined function. But for
  1421. # source listings, we prefer to use the location in the function we
  1422. # are listing. So use MapToSymbols() to fetch full location
  1423. # information for each instruction and then pick out the first
  1424. # location from a location list (location list contains callers before
  1425. # callees in case of inlining).
  1426. #
  1427. # After this routine has run, each entry in $instructions contains:
  1428. # [0] start address
  1429. # [1] filename for function we are listing
  1430. # [2] line number for function we are listing
  1431. # [3] disassembly
  1432. # [4] limit address
  1433. # [5] most specific filename (may be different from [1] due to inlining)
  1434. # [6] most specific line number (may be different from [2] due to inlining)
  1435. sub GetTopLevelLineNumbers {
  1436. my ($lib, $offset, $instructions) = @_;
  1437. my $pcs = [];
  1438. for (my $i = 0; $i <= $#{$instructions}; $i++) {
  1439. push(@{$pcs}, $instructions->[$i]->[0]);
  1440. }
  1441. my $symbols = {};
  1442. MapToSymbols($lib, $offset, $pcs, $symbols);
  1443. for (my $i = 0; $i <= $#{$instructions}; $i++) {
  1444. my $e = $instructions->[$i];
  1445. push(@{$e}, $e->[1]);
  1446. push(@{$e}, $e->[2]);
  1447. my $addr = $e->[0];
  1448. my $sym = $symbols->{$addr};
  1449. if (defined($sym)) {
  1450. if ($#{$sym} >= 2 && $sym->[1] =~ m/^(.*):(\d+)$/) {
  1451. $e->[1] = $1; # File name
  1452. $e->[2] = $2; # Line number
  1453. }
  1454. }
  1455. }
  1456. }
  1457. # Print source-listing for one routine
  1458. sub PrintSource {
  1459. my $prog = shift;
  1460. my $offset = shift;
  1461. my $routine = shift;
  1462. my $flat = shift;
  1463. my $cumulative = shift;
  1464. my $start_addr = shift;
  1465. my $end_addr = shift;
  1466. my $html = shift;
  1467. my $output = shift;
  1468. # Disassemble all instructions (just to get line numbers)
  1469. my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);
  1470. GetTopLevelLineNumbers($prog, $offset, \@instructions);
  1471. # Hack 1: assume that the first source file encountered in the
  1472. # disassembly contains the routine
  1473. my $filename = undef;
  1474. for (my $i = 0; $i <= $#instructions; $i++) {
  1475. if ($instructions[$i]->[2] >= 0) {
  1476. $filename = $instructions[$i]->[1];
  1477. last;
  1478. }
  1479. }
  1480. if (!defined($filename)) {
  1481. print STDERR "no filename found in $routine\n";
  1482. return 0;
  1483. }
  1484. # Hack 2: assume that the largest line number from $filename is the
  1485. # end of the procedure. This is typically safe since if P1 contains
  1486. # an inlined call to P2, then P2 usually occurs earlier in the
  1487. # source file. If this does not work, we might have to compute a
  1488. # density profile or just print all regions we find.
  1489. my $lastline = 0;
  1490. for (my $i = 0; $i <= $#instructions; $i++) {
  1491. my $f = $instructions[$i]->[1];
  1492. my $l = $instructions[$i]->[2];
  1493. if (($f eq $filename) && ($l > $lastline)) {
  1494. $lastline = $l;
  1495. }
  1496. }
  1497. # Hack 3: assume the first source location from "filename" is the start of
  1498. # the source code.
  1499. my $firstline = 1;
  1500. for (my $i = 0; $i <= $#instructions; $i++) {
  1501. if ($instructions[$i]->[1] eq $filename) {
  1502. $firstline = $instructions[$i]->[2];
  1503. last;
  1504. }
  1505. }
  1506. # Hack 4: Extend last line forward until its indentation is less than
  1507. # the indentation we saw on $firstline
  1508. my $oldlastline = $lastline;
  1509. {
  1510. if (!open(FILE, "<$filename")) {
  1511. print STDERR "$filename: $!\n";
  1512. return 0;
  1513. }
  1514. my $l = 0;
  1515. my $first_indentation = -1;
  1516. while (<FILE>) {
  1517. s/\r//g; # turn windows-looking lines into unix-looking lines
  1518. $l++;
  1519. my $indent = Indentation($_);
  1520. if ($l >= $firstline) {
  1521. if ($first_indentation < 0 && $indent >= 0) {
  1522. $first_indentation = $indent;
  1523. last if ($first_indentation == 0);
  1524. }
  1525. }
  1526. if ($l >= $lastline && $indent >= 0) {
  1527. if ($indent >= $first_indentation) {
  1528. $lastline = $l+1;
  1529. } else {
  1530. last;
  1531. }
  1532. }
  1533. }
  1534. close(FILE);
  1535. }
  1536. # Assign all samples to the range $firstline,$lastline,
  1537. # Hack 4: If an instruction does not occur in the range, its samples
  1538. # are moved to the next instruction that occurs in the range.
  1539. my $samples1 = {}; # Map from line number to flat count
  1540. my $samples2 = {}; # Map from line number to cumulative count
  1541. my $running1 = 0; # Unassigned flat counts
  1542. my $running2 = 0; # Unassigned cumulative counts
  1543. my $total1 = 0; # Total flat counts
  1544. my $total2 = 0; # Total cumulative counts
  1545. my %disasm = (); # Map from line number to disassembly
  1546. my $running_disasm = ""; # Unassigned disassembly
  1547. my $skip_marker = "---\n";
  1548. if ($html) {
  1549. $skip_marker = "";
  1550. for (my $l = $firstline; $l <= $lastline; $l++) {
  1551. $disasm{$l} = "";
  1552. }
  1553. }
  1554. my $last_dis_filename = '';
  1555. my $last_dis_linenum = -1;
  1556. my $last_touched_line = -1; # To detect gaps in disassembly for a line
  1557. foreach my $e (@instructions) {
  1558. # Add up counts for all address that fall inside this instruction
  1559. my $c1 = 0;
  1560. my $c2 = 0;
  1561. for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {
  1562. $c1 += GetEntry($flat, $a);
  1563. $c2 += GetEntry($cumulative, $a);
  1564. }
  1565. if ($html) {
  1566. my $dis = sprintf(" %6s %6s \t\t%8s: %s ",
  1567. HtmlPrintNumber($c1),
  1568. HtmlPrintNumber($c2),
  1569. UnparseAddress($offset, $e->[0]),
  1570. CleanDisassembly($e->[3]));
  1571. # Append the most specific source line associated with this instruction
  1572. if (length($dis) < 80) { $dis .= (' ' x (80 - length($dis))) };
  1573. $dis = HtmlEscape($dis);
  1574. my $f = $e->[5];
  1575. my $l = $e->[6];
  1576. if ($f ne $last_dis_filename) {
  1577. $dis .= sprintf("<span class=disasmloc>%s:%d</span>",
  1578. HtmlEscape(CleanFileName($f)), $l);
  1579. } elsif ($l ne $last_dis_linenum) {
  1580. # De-emphasize the unchanged file name portion
  1581. $dis .= sprintf("<span class=unimportant>%s</span>" .
  1582. "<span class=disasmloc>:%d</span>",
  1583. HtmlEscape(CleanFileName($f)), $l);
  1584. } else {
  1585. # De-emphasize the entire location
  1586. $dis .= sprintf("<span class=unimportant>%s:%d</span>",
  1587. HtmlEscape(CleanFileName($f)), $l);
  1588. }
  1589. $last_dis_filename = $f;
  1590. $last_dis_linenum = $l;
  1591. $running_disasm .= $dis;
  1592. $running_disasm .= "\n";
  1593. }
  1594. $running1 += $c1;
  1595. $running2 += $c2;
  1596. $total1 += $c1;
  1597. $total2 += $c2;
  1598. my $file = $e->[1];
  1599. my $line = $e->[2];
  1600. if (($file eq $filename) &&
  1601. ($line >= $firstline) &&
  1602. ($line <= $lastline)) {
  1603. # Assign all accumulated samples to this line
  1604. AddEntry($samples1, $line, $running1);
  1605. AddEntry($samples2, $line, $running2);
  1606. $running1 = 0;
  1607. $running2 = 0;
  1608. if ($html) {
  1609. if ($line != $last_touched_line && $disasm{$line} ne '') {
  1610. $disasm{$line} .= "\n";
  1611. }
  1612. $disasm{$line} .= $running_disasm;
  1613. $running_disasm = '';
  1614. $last_touched_line = $line;
  1615. }
  1616. }
  1617. }
  1618. # Assign any leftover samples to $lastline
  1619. AddEntry($samples1, $lastline, $running1);
  1620. AddEntry($samples2, $lastline, $running2);
  1621. if ($html) {
  1622. if ($lastline != $last_touched_line && $disasm{$lastline} ne '') {
  1623. $disasm{$lastline} .= "\n";
  1624. }
  1625. $disasm{$lastline} .= $running_disasm;
  1626. }
  1627. if ($html) {
  1628. printf $output (
  1629. "<h1>%s</h1>%s\n<pre onClick=\"pprof_toggle_asm()\">\n" .
  1630. "Total:%6s %6s (flat / cumulative %s)\n",
  1631. HtmlEscape(ShortFunctionName($routine)),
  1632. HtmlEscape(CleanFileName($filename)),
  1633. Unparse($total1),
  1634. Unparse($total2),
  1635. Units());
  1636. } else {
  1637. printf $output (
  1638. "ROUTINE ====================== %s in %s\n" .
  1639. "%6s %6s Total %s (flat / cumulative)\n",
  1640. ShortFunctionName($routine),
  1641. CleanFileName($filename),
  1642. Unparse($total1),
  1643. Unparse($total2),
  1644. Units());
  1645. }
  1646. if (!open(FILE, "<$filename")) {
  1647. print STDERR "$filename: $!\n";
  1648. return 0;
  1649. }
  1650. my $l = 0;
  1651. while (<FILE>) {
  1652. s/\r//g; # turn windows-looking lines into unix-looking lines
  1653. $l++;
  1654. if ($l >= $firstline - 5 &&
  1655. (($l <= $oldlastline + 5) || ($l <= $lastline))) {
  1656. chop;
  1657. my $text = $_;
  1658. if ($l == $firstline) { print $output $skip_marker; }
  1659. my $n1 = GetEntry($samples1, $l);
  1660. my $n2 = GetEntry($samples2, $l);
  1661. if ($html) {
  1662. # Emit a span that has one of the following classes:
  1663. # livesrc -- has samples
  1664. # deadsrc -- has disassembly, but with no samples
  1665. # nop -- has no matching disasembly
  1666. # Also emit an optional span containing disassembly.
  1667. my $dis = $disasm{$l};
  1668. my $asm = "";
  1669. if (defined($dis) && $dis ne '') {
  1670. $asm = "<span class=\"asm\">" . $dis . "</span>";
  1671. }
  1672. my $source_class = (($n1 + $n2 > 0)
  1673. ? "livesrc"
  1674. : (($asm ne "") ? "deadsrc" : "nop"));
  1675. printf $output (
  1676. "<span class=\"line\">%5d</span> " .
  1677. "<span class=\"%s\">%6s %6s %s</span>%s\n",
  1678. $l, $source_class,
  1679. HtmlPrintNumber($n1),
  1680. HtmlPrintNumber($n2),
  1681. HtmlEscape($text),
  1682. $asm);
  1683. } else {
  1684. printf $output(
  1685. "%6s %6s %4d: %s\n",
  1686. UnparseAlt($n1),
  1687. UnparseAlt($n2),
  1688. $l,
  1689. $text);
  1690. }
  1691. if ($l == $lastline) { print $output $skip_marker; }
  1692. };
  1693. }
  1694. close(FILE);
  1695. if ($html) {
  1696. print $output "</pre>\n";
  1697. }
  1698. return 1;
  1699. }
  1700. # Return the source line for the specified file/linenumber.
  1701. # Returns undef if not found.
  1702. sub SourceLine {
  1703. my $file = shift;
  1704. my $line = shift;
  1705. # Look in cache
  1706. if (!defined($main::source_cache{$file})) {
  1707. if (100 < scalar keys(%main::source_cache)) {
  1708. # Clear the cache when it gets too big
  1709. $main::source_cache = ();
  1710. }
  1711. # Read all lines from the file
  1712. if (!open(FILE, "<$file")) {
  1713. print STDERR "$file: $!\n";
  1714. $main::source_cache{$file} = []; # Cache the negative result
  1715. return undef;
  1716. }
  1717. my $lines = [];
  1718. push(@{$lines}, ""); # So we can use 1-based line numbers as indices
  1719. while (<FILE>) {
  1720. push(@{$lines}, $_);
  1721. }
  1722. close(FILE);
  1723. # Save the lines in the cache
  1724. $main::source_cache{$file} = $lines;
  1725. }
  1726. my $lines = $main::source_cache{$file};
  1727. if (($line < 0) || ($line > $#{$lines})) {
  1728. return undef;
  1729. } else {
  1730. return $lines->[$line];
  1731. }
  1732. }
  1733. # Print disassembly for one routine with interspersed source if available
  1734. sub PrintDisassembledFunction {
  1735. my $prog = shift;
  1736. my $offset = shift;
  1737. my $routine = shift;
  1738. my $flat = shift;
  1739. my $cumulative = shift;
  1740. my $start_addr = shift;
  1741. my $end_addr = shift;
  1742. my $total = shift;
  1743. # Disassemble all instructions
  1744. my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);
  1745. # Make array of counts per instruction
  1746. my @flat_count = ();
  1747. my @cum_count = ();
  1748. my $flat_total = 0;
  1749. my $cum_total = 0;
  1750. foreach my $e (@instructions) {
  1751. # Add up counts for all address that fall inside this instruction
  1752. my $c1 = 0;
  1753. my $c2 = 0;
  1754. for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {
  1755. $c1 += GetEntry($flat, $a);
  1756. $c2 += GetEntry($cumulative, $a);
  1757. }
  1758. push(@flat_count, $c1);
  1759. push(@cum_count, $c2);
  1760. $flat_total += $c1;
  1761. $cum_total += $c2;
  1762. }
  1763. # Print header with total counts
  1764. printf("ROUTINE ====================== %s\n" .
  1765. "%6s %6s %s (flat, cumulative) %.1f%% of total\n",
  1766. ShortFunctionName($routine),
  1767. Unparse($flat_total),
  1768. Unparse($cum_total),
  1769. Units(),
  1770. ($cum_total * 100.0) / $total);
  1771. # Process instructions in order
  1772. my $current_file = "";
  1773. for (my $i = 0; $i <= $#instructions; ) {
  1774. my $e = $instructions[$i];
  1775. # Print the new file name whenever we switch files
  1776. if ($e->[1] ne $current_file) {
  1777. $current_file = $e->[1];
  1778. my $fname = $current_file;
  1779. $fname =~ s|^\./||; # Trim leading "./"
  1780. # Shorten long file names
  1781. if (length($fname) >= 58) {
  1782. $fname = "..." . substr($fname, -55);
  1783. }
  1784. printf("-------------------- %s\n", $fname);
  1785. }
  1786. # TODO: Compute range of lines to print together to deal with
  1787. # small reorderings.
  1788. my $first_line = $e->[2];
  1789. my $last_line = $first_line;
  1790. my %flat_sum = ();
  1791. my %cum_sum = ();
  1792. for (my $l = $first_line; $l <= $last_line; $l++) {
  1793. $flat_sum{$l} = 0;
  1794. $cum_sum{$l} = 0;
  1795. }
  1796. # Find run of instructions for this range of source lines
  1797. my $first_inst = $i;
  1798. while (($i <= $#instructions) &&
  1799. ($instructions[$i]->[2] >= $first_line) &&
  1800. ($instructions[$i]->[2] <= $last_line)) {
  1801. $e = $instructions[$i];
  1802. $flat_sum{$e->[2]} += $flat_count[$i];
  1803. $cum_sum{$e->[2]} += $cum_count[$i];
  1804. $i++;
  1805. }
  1806. my $last_inst = $i - 1;
  1807. # Print source lines
  1808. for (my $l = $first_line; $l <= $last_line; $l++) {
  1809. my $line = SourceLine($current_file, $l);
  1810. if (!defined($line)) {
  1811. $line = "?\n";
  1812. next;
  1813. } else {
  1814. $line =~ s/^\s+//;
  1815. }
  1816. printf("%6s %6s %5d: %s",
  1817. UnparseAlt($flat_sum{$l}),
  1818. UnparseAlt($cum_sum{$l}),
  1819. $l,
  1820. $line);
  1821. }
  1822. # Print disassembly
  1823. for (my $x = $first_inst; $x <= $last_inst; $x++) {
  1824. my $e = $instructions[$x];
  1825. printf("%6s %6s %8s: %6s\n",
  1826. UnparseAlt($flat_count[$x]),
  1827. UnparseAlt($cum_count[$x]),
  1828. UnparseAddress($offset, $e->[0]),
  1829. CleanDisassembly($e->[3]));
  1830. }
  1831. }
  1832. }
  1833. # Print DOT graph
  1834. sub PrintDot {
  1835. my $prog = shift;
  1836. my $symbols = shift;
  1837. my $raw = shift;
  1838. my $flat = shift;
  1839. my $cumulative = shift;
  1840. my $overall_total = shift;
  1841. # Get total
  1842. my $local_total = TotalProfile($flat);
  1843. my $nodelimit = int($main::opt_nodefraction * $local_total);
  1844. my $edgelimit = int($main::opt_edgefraction * $local_total);
  1845. my $nodecount = $main::opt_nodecount;
  1846. # Find nodes to include
  1847. my @list = (sort { abs(GetEntry($cumulative, $b)) <=>
  1848. abs(GetEntry($cumulative, $a))
  1849. || $a cmp $b }
  1850. keys(%{$cumulative}));
  1851. my $last = $nodecount - 1;
  1852. if ($last > $#list) {
  1853. $last = $#list;
  1854. }
  1855. while (($last >= 0) &&
  1856. (abs(GetEntry($cumulative, $list[$last])) <= $nodelimit)) {
  1857. $last--;
  1858. }
  1859. if ($last < 0) {
  1860. print STDERR "No nodes to print\n";
  1861. return 0;
  1862. }
  1863. if ($nodelimit > 0 || $edgelimit > 0) {
  1864. printf STDERR ("Dropping nodes with <= %s %s; edges with <= %s abs(%s)\n",
  1865. Unparse($nodelimit), Units(),
  1866. Unparse($edgelimit), Units());
  1867. }
  1868. # Open DOT output file
  1869. my $output;
  1870. my $escaped_dot = ShellEscape(@DOT);
  1871. my $escaped_ps2pdf = ShellEscape(@PS2PDF);
  1872. if ($main::opt_gv) {
  1873. my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, "ps"));
  1874. $output = "| $escaped_dot -Tps2 >$escaped_outfile";
  1875. } elsif ($main::opt_evince) {
  1876. my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, "pdf"));
  1877. $output = "| $escaped_dot -Tps2 | $escaped_ps2pdf - $escaped_outfile";
  1878. } elsif ($main::opt_ps) {
  1879. $output = "| $escaped_dot -Tps2";
  1880. } elsif ($main::opt_pdf) {
  1881. $output = "| $escaped_dot -Tps2 | $escaped_ps2pdf - -";
  1882. } elsif ($main::opt_web || $main::opt_svg) {
  1883. # We need to post-process the SVG, so write to a temporary file always.
  1884. my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, "svg"));
  1885. $output = "| $escaped_dot -Tsvg >$escaped_outfile";
  1886. } elsif ($main::opt_gif) {
  1887. $output = "| $escaped_dot -Tgif";
  1888. } else {
  1889. $output = ">&STDOUT";
  1890. }
  1891. open(DOT, $output) || error("$output: $!\n");
  1892. # Title
  1893. printf DOT ("digraph \"%s; %s %s\" {\n",
  1894. $prog,
  1895. Unparse($overall_total),
  1896. Units());
  1897. if ($main::opt_pdf) {
  1898. # The output is more printable if we set the page size for dot.
  1899. printf DOT ("size=\"8,11\"\n");
  1900. }
  1901. printf DOT ("node [width=0.375,height=0.25];\n");
  1902. # Print legend
  1903. printf DOT ("Legend [shape=box,fontsize=24,shape=plaintext," .
  1904. "label=\"%s\\l%s\\l%s\\l%s\\l%s\\l\"];\n",
  1905. $prog,
  1906. sprintf("Total %s: %s", Units(), Unparse($overall_total)),
  1907. sprintf("Focusing on: %s", Unparse($local_total)),
  1908. sprintf("Dropped nodes with <= %s abs(%s)",
  1909. Unparse($nodelimit), Units()),
  1910. sprintf("Dropped edges with <= %s %s",
  1911. Unparse($edgelimit), Units())
  1912. );
  1913. # Print nodes
  1914. my %node = ();
  1915. my $nextnode = 1;
  1916. foreach my $a (@list[0..$last]) {
  1917. # Pick font size
  1918. my $f = GetEntry($flat, $a);
  1919. my $c = GetEntry($cumulative, $a);
  1920. my $fs = 8;
  1921. if ($local_total > 0) {
  1922. $fs = 8 + (50.0 * sqrt(abs($f * 1.0 / $local_total)));
  1923. }
  1924. $node{$a} = $nextnode++;
  1925. my $sym = $a;
  1926. $sym =~ s/\s+/\\n/g;
  1927. $sym =~ s/::/\\n/g;
  1928. # Extra cumulative info to print for non-leaves
  1929. my $extra = "";
  1930. if ($f != $c) {
  1931. $extra = sprintf("\\rof %s (%s)",
  1932. Unparse($c),
  1933. Percent($c, $local_total));
  1934. }
  1935. my $style = "";
  1936. if ($main::opt_heapcheck) {
  1937. if ($f > 0) {
  1938. # make leak-causing nodes more visible (add a background)
  1939. $style = ",style=filled,fillcolor=gray"
  1940. } elsif ($f < 0) {
  1941. # make anti-leak-causing nodes (which almost never occur)
  1942. # stand out as well (triple border)
  1943. $style = ",peripheries=3"
  1944. }
  1945. }
  1946. printf DOT ("N%d [label=\"%s\\n%s (%s)%s\\r" .
  1947. "\",shape=box,fontsize=%.1f%s];\n",
  1948. $node{$a},
  1949. $sym,
  1950. Unparse($f),
  1951. Percent($f, $local_total),
  1952. $extra,
  1953. $fs,
  1954. $style,
  1955. );
  1956. }
  1957. # Get edges and counts per edge
  1958. my %edge = ();
  1959. my $n;
  1960. my $fullname_to_shortname_map = {};
  1961. FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);
  1962. foreach my $k (keys(%{$raw})) {
  1963. # TODO: omit low %age edges
  1964. $n = $raw->{$k};
  1965. my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);
  1966. for (my $i = 1; $i <= $#translated; $i++) {
  1967. my $src = $translated[$i];
  1968. my $dst = $translated[$i-1];
  1969. #next if ($src eq $dst); # Avoid self-edges?
  1970. if (exists($node{$src}) && exists($node{$dst})) {
  1971. my $edge_label = "$src\001$dst";
  1972. if (!exists($edge{$edge_label})) {
  1973. $edge{$edge_label} = 0;
  1974. }
  1975. $edge{$edge_label} += $n;
  1976. }
  1977. }
  1978. }
  1979. # Print edges (process in order of decreasing counts)
  1980. my %indegree = (); # Number of incoming edges added per node so far
  1981. my %outdegree = (); # Number of outgoing edges added per node so far
  1982. foreach my $e (sort { $edge{$b} <=> $edge{$a} } keys(%edge)) {
  1983. my @x = split(/\001/, $e);
  1984. $n = $edge{$e};
  1985. # Initialize degree of kept incoming and outgoing edges if necessary
  1986. my $src = $x[0];
  1987. my $dst = $x[1];
  1988. if (!exists($outdegree{$src})) { $outdegree{$src} = 0; }
  1989. if (!exists($indegree{$dst})) { $indegree{$dst} = 0; }
  1990. my $keep;
  1991. if ($indegree{$dst} == 0) {
  1992. # Keep edge if needed for reachability
  1993. $keep = 1;
  1994. } elsif (abs($n) <= $edgelimit) {
  1995. # Drop if we are below --edgefraction
  1996. $keep = 0;
  1997. } elsif ($outdegree{$src} >= $main::opt_maxdegree ||
  1998. $indegree{$dst} >= $main::opt_maxdegree) {
  1999. # Keep limited number of in/out edges per node
  2000. $keep = 0;
  2001. } else {
  2002. $keep = 1;
  2003. }
  2004. if ($keep) {
  2005. $outdegree{$src}++;
  2006. $indegree{$dst}++;
  2007. # Compute line width based on edge count
  2008. my $fraction = abs($local_total ? (3 * ($n / $local_total)) : 0);
  2009. if ($fraction > 1) { $fraction = 1; }
  2010. my $w = $fraction * 2;
  2011. if ($w < 1 && ($main::opt_web || $main::opt_svg)) {
  2012. # SVG output treats line widths < 1 poorly.
  2013. $w = 1;
  2014. }
  2015. # Dot sometimes segfaults if given edge weights that are too large, so
  2016. # we cap the weights at a large value
  2017. my $edgeweight = abs($n) ** 0.7;
  2018. if ($edgeweight > 100000) { $edgeweight = 100000; }
  2019. $edgeweight = int($edgeweight);
  2020. my $style = sprintf("setlinewidth(%f)", $w);
  2021. if ($x[1] =~ m/\(inline\)/) {
  2022. $style .= ",dashed";
  2023. }
  2024. # Use a slightly squashed function of the edge count as the weight
  2025. printf DOT ("N%s -> N%s [label=%s, weight=%d, style=\"%s\"];\n",
  2026. $node{$x[0]},
  2027. $node{$x[1]},
  2028. Unparse($n),
  2029. $edgeweight,
  2030. $style);
  2031. }
  2032. }
  2033. print DOT ("}\n");
  2034. close(DOT);
  2035. if ($main::opt_web || $main::opt_svg) {
  2036. # Rewrite SVG to be more usable inside web browser.
  2037. RewriteSvg(TempName($main::next_tmpfile, "svg"));
  2038. }
  2039. return 1;
  2040. }
  2041. sub RewriteSvg {
  2042. my $svgfile = shift;
  2043. open(SVG, $svgfile) || die "open temp svg: $!";
  2044. my @svg = <SVG>;
  2045. close(SVG);
  2046. unlink $svgfile;
  2047. my $svg = join('', @svg);
  2048. # Dot's SVG output is
  2049. #
  2050. # <svg width="___" height="___"
  2051. # viewBox="___" xmlns=...>
  2052. # <g id="graph0" transform="...">
  2053. # ...
  2054. # </g>
  2055. # </svg>
  2056. #
  2057. # Change it to
  2058. #
  2059. # <svg width="100%" height="100%"
  2060. # xmlns=...>
  2061. # $svg_javascript
  2062. # <g id="viewport" transform="translate(0,0)">
  2063. # <g id="graph0" transform="...">
  2064. # ...
  2065. # </g>
  2066. # </g>
  2067. # </svg>
  2068. # Fix width, height; drop viewBox.
  2069. $svg =~ s/(?s)<svg width="[^"]+" height="[^"]+"(.*?)viewBox="[^"]+"/<svg width="100%" height="100%"$1/;
  2070. # Insert script, viewport <g> above first <g>
  2071. my $svg_javascript = SvgJavascript();
  2072. my $viewport = "<g id=\"viewport\" transform=\"translate(0,0)\">\n";
  2073. $svg =~ s/<g id="graph\d"/$svg_javascript$viewport$&/;
  2074. # Insert final </g> above </svg>.
  2075. $svg =~ s/(.*)(<\/svg>)/$1<\/g>$2/;
  2076. $svg =~ s/<g id="graph\d"(.*?)/<g id="viewport"$1/;
  2077. if ($main::opt_svg) {
  2078. # --svg: write to standard output.
  2079. print $svg;
  2080. } else {
  2081. # Write back to temporary file.
  2082. open(SVG, ">$svgfile") || die "open $svgfile: $!";
  2083. print SVG $svg;
  2084. close(SVG);
  2085. }
  2086. }
  2087. sub SvgJavascript {
  2088. return <<'EOF';
  2089. <script type="text/ecmascript"><![CDATA[
  2090. // SVGPan
  2091. // http://www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/
  2092. // Local modification: if(true || ...) below to force panning, never moving.
  2093. /**
  2094. * SVGPan library 1.2
  2095. * ====================
  2096. *
  2097. * Given an unique existing element with id "viewport", including the
  2098. * the library into any SVG adds the following capabilities:
  2099. *
  2100. * - Mouse panning
  2101. * - Mouse zooming (using the wheel)
  2102. * - Object dargging
  2103. *
  2104. * Known issues:
  2105. *
  2106. * - Zooming (while panning) on Safari has still some issues
  2107. *
  2108. * Releases:
  2109. *
  2110. * 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui
  2111. * Fixed a bug with browser mouse handler interaction
  2112. *
  2113. * 1.1, Wed Feb 3 17:39:33 GMT 2010, Zeng Xiaohui
  2114. * Updated the zoom code to support the mouse wheel on Safari/Chrome
  2115. *
  2116. * 1.0, Andrea Leofreddi
  2117. * First release
  2118. *
  2119. * This code is licensed under the following BSD license:
  2120. *
  2121. * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
  2122. *
  2123. * Redistribution and use in source and binary forms, with or without modification, are
  2124. * permitted provided that the following conditions are met:
  2125. *
  2126. * 1. Redistributions of source code must retain the above copyright notice, this list of
  2127. * conditions and the following disclaimer.
  2128. *
  2129. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  2130. * of conditions and the following disclaimer in the documentation and/or other materials
  2131. * provided with the distribution.
  2132. *
  2133. * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
  2134. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2135. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
  2136. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  2137. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  2138. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  2139. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2140. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  2141. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2142. *
  2143. * The views and conclusions contained in the software and documentation are those of the
  2144. * authors and should not be interpreted as representing official policies, either expressed
  2145. * or implied, of Andrea Leofreddi.
  2146. */
  2147. var root = document.documentElement;
  2148. var state = 'none', stateTarget, stateOrigin, stateTf;
  2149. setupHandlers(root);
  2150. /**
  2151. * Register handlers
  2152. */
  2153. function setupHandlers(root){
  2154. setAttributes(root, {
  2155. "onmouseup" : "add(evt)",
  2156. "onmousedown" : "handleMouseDown(evt)",
  2157. "onmousemove" : "handleMouseMove(evt)",
  2158. "onmouseup" : "handleMouseUp(evt)",
  2159. //"onmouseout" : "handleMouseUp(evt)", // Decomment this to stop the pan functionality when dragging out of the SVG element
  2160. });
  2161. if(navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)
  2162. window.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari
  2163. else
  2164. window.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others
  2165. var g = svgDoc.getElementById("svg");
  2166. g.width = "100%";
  2167. g.height = "100%";
  2168. }
  2169. /**
  2170. * Instance an SVGPoint object with given event coordinates.
  2171. */
  2172. function getEventPoint(evt) {
  2173. var p = root.createSVGPoint();
  2174. p.x = evt.clientX;
  2175. p.y = evt.clientY;
  2176. return p;
  2177. }
  2178. /**
  2179. * Sets the current transform matrix of an element.
  2180. */
  2181. function setCTM(element, matrix) {
  2182. var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
  2183. element.setAttribute("transform", s);
  2184. }
  2185. /**
  2186. * Dumps a matrix to a string (useful for debug).
  2187. */
  2188. function dumpMatrix(matrix) {
  2189. var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
  2190. return s;
  2191. }
  2192. /**
  2193. * Sets attributes of an element.
  2194. */
  2195. function setAttributes(element, attributes){
  2196. for (i in attributes)
  2197. element.setAttributeNS(null, i, attributes[i]);
  2198. }
  2199. /**
  2200. * Handle mouse move event.
  2201. */
  2202. function handleMouseWheel(evt) {
  2203. if(evt.preventDefault)
  2204. evt.preventDefault();
  2205. evt.returnValue = false;
  2206. var svgDoc = evt.target.ownerDocument;
  2207. var delta;
  2208. if(evt.wheelDelta)
  2209. delta = evt.wheelDelta / 3600; // Chrome/Safari
  2210. else
  2211. delta = evt.detail / -90; // Mozilla
  2212. var z = 1 + delta; // Zoom factor: 0.9/1.1
  2213. var g = svgDoc.getElementById("viewport");
  2214. var p = getEventPoint(evt);
  2215. p = p.matrixTransform(g.getCTM().inverse());
  2216. // Compute new scale matrix in current mouse position
  2217. var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
  2218. setCTM(g, g.getCTM().multiply(k));
  2219. stateTf = stateTf.multiply(k.inverse());
  2220. }
  2221. /**
  2222. * Handle mouse move event.
  2223. */
  2224. function handleMouseMove(evt) {
  2225. if(evt.preventDefault)
  2226. evt.preventDefault();
  2227. evt.returnValue = false;
  2228. var svgDoc = evt.target.ownerDocument;
  2229. var g = svgDoc.getElementById("viewport");
  2230. if(state == 'pan') {
  2231. // Pan mode
  2232. var p = getEventPoint(evt).matrixTransform(stateTf);
  2233. setCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));
  2234. } else if(state == 'move') {
  2235. // Move mode
  2236. var p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());
  2237. setCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));
  2238. stateOrigin = p;
  2239. }
  2240. }
  2241. /**
  2242. * Handle click event.
  2243. */
  2244. function handleMouseDown(evt) {
  2245. if(evt.preventDefault)
  2246. evt.preventDefault();
  2247. evt.returnValue = false;
  2248. var svgDoc = evt.target.ownerDocument;
  2249. var g = svgDoc.getElementById("viewport");
  2250. if(true || evt.target.tagName == "svg") {
  2251. // Pan mode
  2252. state = 'pan';
  2253. stateTf = g.getCTM().inverse();
  2254. stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
  2255. } else {
  2256. // Move mode
  2257. state = 'move';
  2258. stateTarget = evt.target;
  2259. stateTf = g.getCTM().inverse();
  2260. stateOrigin = getEventPoint(evt).matrixTransform(stateTf);
  2261. }
  2262. }
  2263. /**
  2264. * Handle mouse button release event.
  2265. */
  2266. function handleMouseUp(evt) {
  2267. if(evt.preventDefault)
  2268. evt.preventDefault();
  2269. evt.returnValue = false;
  2270. var svgDoc = evt.target.ownerDocument;
  2271. if(state == 'pan' || state == 'move') {
  2272. // Quit pan mode
  2273. state = '';
  2274. }
  2275. }
  2276. ]]></script>
  2277. EOF
  2278. }
  2279. # Provides a map from fullname to shortname for cases where the
  2280. # shortname is ambiguous. The symlist has both the fullname and
  2281. # shortname for all symbols, which is usually fine, but sometimes --
  2282. # such as overloaded functions -- two different fullnames can map to
  2283. # the same shortname. In that case, we use the address of the
  2284. # function to disambiguate the two. This function fills in a map that
  2285. # maps fullnames to modified shortnames in such cases. If a fullname
  2286. # is not present in the map, the 'normal' shortname provided by the
  2287. # symlist is the appropriate one to use.
  2288. sub FillFullnameToShortnameMap {
  2289. my $symbols = shift;
  2290. my $fullname_to_shortname_map = shift;
  2291. my $shortnames_seen_once = {};
  2292. my $shortnames_seen_more_than_once = {};
  2293. foreach my $symlist (values(%{$symbols})) {
  2294. # TODO(csilvers): deal with inlined symbols too.
  2295. my $shortname = $symlist->[0];
  2296. my $fullname = $symlist->[2];
  2297. if ($fullname !~ /<[0-9a-fA-F]+>$/) { # fullname doesn't end in an address
  2298. next; # the only collisions we care about are when addresses differ
  2299. }
  2300. if (defined($shortnames_seen_once->{$shortname}) &&
  2301. $shortnames_seen_once->{$shortname} ne $fullname) {
  2302. $shortnames_seen_more_than_once->{$shortname} = 1;
  2303. } else {
  2304. $shortnames_seen_once->{$shortname} = $fullname;
  2305. }
  2306. }
  2307. foreach my $symlist (values(%{$symbols})) {
  2308. my $shortname = $symlist->[0];
  2309. my $fullname = $symlist->[2];
  2310. # TODO(csilvers): take in a list of addresses we care about, and only
  2311. # store in the map if $symlist->[1] is in that list. Saves space.
  2312. next if defined($fullname_to_shortname_map->{$fullname});
  2313. if (defined($shortnames_seen_more_than_once->{$shortname})) {
  2314. if ($fullname =~ /<0*([^>]*)>$/) { # fullname has address at end of it
  2315. $fullname_to_shortname_map->{$fullname} = "$shortname\@$1";
  2316. }
  2317. }
  2318. }
  2319. }
  2320. # Return a small number that identifies the argument.
  2321. # Multiple calls with the same argument will return the same number.
  2322. # Calls with different arguments will return different numbers.
  2323. sub ShortIdFor {
  2324. my $key = shift;
  2325. my $id = $main::uniqueid{$key};
  2326. if (!defined($id)) {
  2327. $id = keys(%main::uniqueid) + 1;
  2328. $main::uniqueid{$key} = $id;
  2329. }
  2330. return $id;
  2331. }
  2332. # Translate a stack of addresses into a stack of symbols
  2333. sub TranslateStack {
  2334. my $symbols = shift;
  2335. my $fullname_to_shortname_map = shift;
  2336. my $k = shift;
  2337. my @addrs = split(/\n/, $k);
  2338. my @result = ();
  2339. for (my $i = 0; $i <= $#addrs; $i++) {
  2340. my $a = $addrs[$i];
  2341. # Skip large addresses since they sometimes show up as fake entries on RH9
  2342. if (length($a) > 8 && $a gt "7fffffffffffffff") {
  2343. next;
  2344. }
  2345. if ($main::opt_disasm || $main::opt_list) {
  2346. # We want just the address for the key
  2347. push(@result, $a);
  2348. next;
  2349. }
  2350. my $symlist = $symbols->{$a};
  2351. if (!defined($symlist)) {
  2352. $symlist = [$a, "", $a];
  2353. }
  2354. # We can have a sequence of symbols for a particular entry
  2355. # (more than one symbol in the case of inlining). Callers
  2356. # come before callees in symlist, so walk backwards since
  2357. # the translated stack should contain callees before callers.
  2358. for (my $j = $#{$symlist}; $j >= 2; $j -= 3) {
  2359. my $func = $symlist->[$j-2];
  2360. my $fileline = $symlist->[$j-1];
  2361. my $fullfunc = $symlist->[$j];
  2362. if (defined($fullname_to_shortname_map->{$fullfunc})) {
  2363. $func = $fullname_to_shortname_map->{$fullfunc};
  2364. }
  2365. if ($j > 2) {
  2366. $func = "$func (inline)";
  2367. }
  2368. # Do not merge nodes corresponding to Callback::Run since that
  2369. # causes confusing cycles in dot display. Instead, we synthesize
  2370. # a unique name for this frame per caller.
  2371. if ($func =~ m/Callback.*::Run$/) {
  2372. my $caller = ($i > 0) ? $addrs[$i-1] : 0;
  2373. $func = "Run#" . ShortIdFor($caller);
  2374. }
  2375. if ($main::opt_addresses) {
  2376. push(@result, "$a $func $fileline");
  2377. } elsif ($main::opt_lines) {
  2378. if ($func eq '??' && $fileline eq '??:0') {
  2379. push(@result, "$a");
  2380. } elsif (!$main::opt_show_addresses) {
  2381. push(@result, "$func $fileline");
  2382. } else {
  2383. push(@result, "$func $fileline ($a)");
  2384. }
  2385. } elsif ($main::opt_functions) {
  2386. if ($func eq '??') {
  2387. push(@result, "$a");
  2388. } elsif (!$main::opt_show_addresses) {
  2389. push(@result, $func);
  2390. } else {
  2391. push(@result, "$func ($a)");
  2392. }
  2393. } elsif ($main::opt_files) {
  2394. if ($fileline eq '??:0' || $fileline eq '') {
  2395. push(@result, "$a");
  2396. } else {
  2397. my $f = $fileline;
  2398. $f =~ s/:\d+$//;
  2399. push(@result, $f);
  2400. }
  2401. } else {
  2402. push(@result, $a);
  2403. last; # Do not print inlined info
  2404. }
  2405. }
  2406. }
  2407. # print join(",", @addrs), " => ", join(",", @result), "\n";
  2408. return @result;
  2409. }
  2410. # Generate percent string for a number and a total
  2411. sub Percent {
  2412. my $num = shift;
  2413. my $tot = shift;
  2414. if ($tot != 0) {
  2415. return sprintf("%.1f%%", $num * 100.0 / $tot);
  2416. } else {
  2417. return ($num == 0) ? "nan" : (($num > 0) ? "+inf" : "-inf");
  2418. }
  2419. }
  2420. # Generate pretty-printed form of number
  2421. sub Unparse {
  2422. my $num = shift;
  2423. if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {
  2424. if ($main::opt_inuse_objects || $main::opt_alloc_objects) {
  2425. return sprintf("%d", $num);
  2426. } else {
  2427. if ($main::opt_show_bytes) {
  2428. return sprintf("%d", $num);
  2429. } else {
  2430. return sprintf("%.1f", $num / 1048576.0);
  2431. }
  2432. }
  2433. } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {
  2434. return sprintf("%.3f", $num / 1e9); # Convert nanoseconds to seconds
  2435. } else {
  2436. return sprintf("%d", $num);
  2437. }
  2438. }
  2439. # Alternate pretty-printed form: 0 maps to "."
  2440. sub UnparseAlt {
  2441. my $num = shift;
  2442. if ($num == 0) {
  2443. return ".";
  2444. } else {
  2445. return Unparse($num);
  2446. }
  2447. }
  2448. # Alternate pretty-printed form: 0 maps to ""
  2449. sub HtmlPrintNumber {
  2450. my $num = shift;
  2451. if ($num == 0) {
  2452. return "";
  2453. } else {
  2454. return Unparse($num);
  2455. }
  2456. }
  2457. # Return output units
  2458. sub Units {
  2459. if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {
  2460. if ($main::opt_inuse_objects || $main::opt_alloc_objects) {
  2461. return "objects";
  2462. } else {
  2463. if ($main::opt_show_bytes) {
  2464. return "B";
  2465. } else {
  2466. return "MB";
  2467. }
  2468. }
  2469. } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {
  2470. return "seconds";
  2471. } else {
  2472. return "samples";
  2473. }
  2474. }
  2475. ##### Profile manipulation code #####
  2476. # Generate flattened profile:
  2477. # If count is charged to stack [a,b,c,d], in generated profile,
  2478. # it will be charged to [a]
  2479. sub FlatProfile {
  2480. my $profile = shift;
  2481. my $result = {};
  2482. foreach my $k (keys(%{$profile})) {
  2483. my $count = $profile->{$k};
  2484. my @addrs = split(/\n/, $k);
  2485. if ($#addrs >= 0) {
  2486. AddEntry($result, $addrs[0], $count);
  2487. }
  2488. }
  2489. return $result;
  2490. }
  2491. # Generate cumulative profile:
  2492. # If count is charged to stack [a,b,c,d], in generated profile,
  2493. # it will be charged to [a], [b], [c], [d]
  2494. sub CumulativeProfile {
  2495. my $profile = shift;
  2496. my $result = {};
  2497. foreach my $k (keys(%{$profile})) {
  2498. my $count = $profile->{$k};
  2499. my @addrs = split(/\n/, $k);
  2500. foreach my $a (@addrs) {
  2501. AddEntry($result, $a, $count);
  2502. }
  2503. }
  2504. return $result;
  2505. }
  2506. # If the second-youngest PC on the stack is always the same, returns
  2507. # that pc. Otherwise, returns undef.
  2508. sub IsSecondPcAlwaysTheSame {
  2509. my $profile = shift;
  2510. my $second_pc = undef;
  2511. foreach my $k (keys(%{$profile})) {
  2512. my @addrs = split(/\n/, $k);
  2513. if ($#addrs < 1) {
  2514. return undef;
  2515. }
  2516. if (not defined $second_pc) {
  2517. $second_pc = $addrs[1];
  2518. } else {
  2519. if ($second_pc ne $addrs[1]) {
  2520. return undef;
  2521. }
  2522. }
  2523. }
  2524. return $second_pc;
  2525. }
  2526. sub ExtractSymbolLocationInlineStack {
  2527. my $symbols = shift;
  2528. my $address = shift;
  2529. my $stack = shift;
  2530. # 'addr2line' outputs "??:0" for unknown locations; we do the
  2531. # same to be consistent.
  2532. if (exists $symbols->{$address}) {
  2533. my @localinlinestack = @{$symbols->{$address}};
  2534. for (my $i = $#localinlinestack; $i > 0; $i-=3) {
  2535. my $file = $localinlinestack[$i-1];
  2536. my $fn = $localinlinestack[$i-2];
  2537. if ($file eq "?" || $file eq ":0") {
  2538. $file = "??:0";
  2539. }
  2540. my $suffix = "[inline]";
  2541. if ($i == 2) {
  2542. $suffix = "";
  2543. }
  2544. push (@$stack, $file.":".$fn.$suffix);
  2545. }
  2546. }
  2547. else {
  2548. push (@$stack, "??:0:unknown");
  2549. }
  2550. }
  2551. sub ExtractSymbolNameInlineStack {
  2552. my $symbols = shift;
  2553. my $address = shift;
  2554. my @stack = ();
  2555. if (exists $symbols->{$address}) {
  2556. my @localinlinestack = @{$symbols->{$address}};
  2557. for (my $i = $#localinlinestack; $i > 0; $i-=3) {
  2558. my $file = $localinlinestack[$i-1];
  2559. my $fn = $localinlinestack[$i-0];
  2560. if ($file eq "?" || $file eq ":0") {
  2561. $file = "??:0";
  2562. }
  2563. if ($fn eq '??') {
  2564. # If we can't get the symbol name, at least use the file information.
  2565. $fn = $file;
  2566. }
  2567. my $suffix = "[inline]";
  2568. if ($i == 2) {
  2569. $suffix = "";
  2570. }
  2571. push (@stack, $fn.$suffix);
  2572. }
  2573. }
  2574. else {
  2575. # If we can't get a symbol name, at least fill in the address.
  2576. push (@stack, $address);
  2577. }
  2578. return @stack;
  2579. }
  2580. sub ExtractSymbolLocation {
  2581. my $symbols = shift;
  2582. my $address = shift;
  2583. # 'addr2line' outputs "??:0" for unknown locations; we do the
  2584. # same to be consistent.
  2585. my $location = "??:0:unknown";
  2586. if (exists $symbols->{$address}) {
  2587. my $file = $symbols->{$address}->[1];
  2588. if ($file eq "?" || $file eq ":0") {
  2589. $file = "??:0"
  2590. }
  2591. $location = $file . ":" . $symbols->{$address}->[0];
  2592. }
  2593. return $location;
  2594. }
  2595. # Extracts a graph of calls.
  2596. sub ExtractCalls {
  2597. my $symbols = shift;
  2598. my $profile = shift;
  2599. my $calls = {};
  2600. while( my ($stack_trace, $count) = each %$profile ) {
  2601. my @address = split(/\n/, $stack_trace);
  2602. my @stack = ();
  2603. ExtractSymbolLocationInlineStack($symbols, $address[0], \@stack);
  2604. for (my $i = 1; $i <= $#address; $i++) {
  2605. ExtractSymbolLocationInlineStack($symbols, $address[$i], \@stack);
  2606. }
  2607. AddEntry($calls, $stack[0], $count);
  2608. for (my $i = 1; $i < $#address; $i++) {
  2609. AddEntry($calls, "$stack[$i] -> $stack[$i-1]", $count);
  2610. }
  2611. }
  2612. return $calls;
  2613. }
  2614. sub PrintStacksForText {
  2615. my $symbols = shift;
  2616. my $profile = shift;
  2617. while (my ($stack_trace, $count) = each %$profile) {
  2618. my @address = split(/\n/, $stack_trace);
  2619. for (my $i = 0; $i <= $#address; $i++) {
  2620. $address[$i] = sprintf("(%s) %s", $address[$i], ExtractSymbolLocation($symbols, $address[$i]));
  2621. }
  2622. printf("%-8d %s\n\n", $count, join("\n ", @address));
  2623. }
  2624. }
  2625. sub PrintCollapsedStacks {
  2626. my $symbols = shift;
  2627. my $profile = shift;
  2628. while (my ($stack_trace, $count) = each %$profile) {
  2629. my @address = split(/\n/, $stack_trace);
  2630. my @names = reverse ( map { ExtractSymbolNameInlineStack($symbols, $_) } @address );
  2631. printf("%s %d\n", join(";", @names), $count);
  2632. }
  2633. }
  2634. sub RemoveUninterestingFrames {
  2635. my $symbols = shift;
  2636. my $profile = shift;
  2637. # List of function names to skip
  2638. my %skip = ();
  2639. my $skip_regexp = 'NOMATCH';
  2640. if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {
  2641. foreach my $name ('calloc',
  2642. 'cfree',
  2643. 'malloc',
  2644. 'free',
  2645. 'memalign',
  2646. 'posix_memalign',
  2647. 'pvalloc',
  2648. 'valloc',
  2649. 'realloc',
  2650. 'tc_calloc',
  2651. 'tc_cfree',
  2652. 'tc_malloc',
  2653. 'tc_free',
  2654. 'tc_memalign',
  2655. 'tc_posix_memalign',
  2656. 'tc_pvalloc',
  2657. 'tc_valloc',
  2658. 'tc_realloc',
  2659. 'tc_new',
  2660. 'tc_delete',
  2661. 'tc_newarray',
  2662. 'tc_deletearray',
  2663. 'tc_new_nothrow',
  2664. 'tc_newarray_nothrow',
  2665. 'do_malloc',
  2666. '::do_malloc', # new name -- got moved to an unnamed ns
  2667. '::do_malloc_or_cpp_alloc',
  2668. 'DoSampledAllocation',
  2669. 'simple_alloc::allocate',
  2670. '__malloc_alloc_template::allocate',
  2671. '__builtin_delete',
  2672. '__builtin_new',
  2673. '__builtin_vec_delete',
  2674. '__builtin_vec_new',
  2675. 'operator new',
  2676. 'operator new[]',
  2677. # The entry to our memory-allocation routines on OS X
  2678. 'malloc_zone_malloc',
  2679. 'malloc_zone_calloc',
  2680. 'malloc_zone_valloc',
  2681. 'malloc_zone_realloc',
  2682. 'malloc_zone_memalign',
  2683. 'malloc_zone_free',
  2684. # These mark the beginning/end of our custom sections
  2685. '__start_google_malloc',
  2686. '__stop_google_malloc',
  2687. '__start_malloc_hook',
  2688. '__stop_malloc_hook') {
  2689. $skip{$name} = 1;
  2690. $skip{"_" . $name} = 1; # Mach (OS X) adds a _ prefix to everything
  2691. }
  2692. # TODO: Remove TCMalloc once everything has been
  2693. # moved into the tcmalloc:: namespace and we have flushed
  2694. # old code out of the system.
  2695. $skip_regexp = "TCMalloc|^tcmalloc::";
  2696. } elsif ($main::profile_type eq 'contention') {
  2697. foreach my $vname ('base::RecordLockProfileData',
  2698. 'base::SubmitMutexProfileData',
  2699. 'base::SubmitSpinLockProfileData',
  2700. 'Mutex::Unlock',
  2701. 'Mutex::UnlockSlow',
  2702. 'Mutex::ReaderUnlock',
  2703. 'MutexLock::~MutexLock',
  2704. 'SpinLock::Unlock',
  2705. 'SpinLock::SlowUnlock',
  2706. 'SpinLockHolder::~SpinLockHolder') {
  2707. $skip{$vname} = 1;
  2708. }
  2709. } elsif ($main::profile_type eq 'cpu' && !$main::opt_no_auto_signal_frames) {
  2710. # Drop signal handlers used for CPU profile collection
  2711. # TODO(dpeng): this should not be necessary; it's taken
  2712. # care of by the general 2nd-pc mechanism below.
  2713. foreach my $name ('ProfileData::Add', # historical
  2714. 'ProfileData::prof_handler', # historical
  2715. 'CpuProfiler::prof_handler',
  2716. '__FRAME_END__',
  2717. '__pthread_sighandler',
  2718. '__restore') {
  2719. $skip{$name} = 1;
  2720. }
  2721. } else {
  2722. # Nothing skipped for unknown types
  2723. }
  2724. if ($main::profile_type eq 'cpu') {
  2725. # If all the second-youngest program counters are the same,
  2726. # this STRONGLY suggests that it is an artifact of measurement,
  2727. # i.e., stack frames pushed by the CPU profiler signal handler.
  2728. # Hence, we delete them.
  2729. # (The topmost PC is read from the signal structure, not from
  2730. # the stack, so it does not get involved.)
  2731. while (my $second_pc = IsSecondPcAlwaysTheSame($profile)) {
  2732. my $result = {};
  2733. my $func = '';
  2734. if (exists($symbols->{$second_pc})) {
  2735. $second_pc = $symbols->{$second_pc}->[0];
  2736. }
  2737. if ($main::opt_no_auto_signal_frames) {
  2738. print STDERR "All second stack frames are same: `$second_pc'.\nMight be stack trace capturing bug.\n";
  2739. last;
  2740. }
  2741. print STDERR "Removing $second_pc from all stack traces.\n";
  2742. foreach my $k (keys(%{$profile})) {
  2743. my $count = $profile->{$k};
  2744. my @addrs = split(/\n/, $k);
  2745. my $topaddr = POSIX::strtoul($addrs[0], 16);
  2746. splice @addrs, 1, 1;
  2747. if ($#addrs > 1) {
  2748. my $subtopaddr = POSIX::strtoul($addrs[1], 16);
  2749. if ($subtopaddr + 1 == $topaddr) {
  2750. splice @addrs, 1, 1;
  2751. }
  2752. }
  2753. my $reduced_path = join("\n", @addrs);
  2754. AddEntry($result, $reduced_path, $count);
  2755. }
  2756. $profile = $result;
  2757. }
  2758. }
  2759. my $result = {};
  2760. foreach my $k (keys(%{$profile})) {
  2761. my $count = $profile->{$k};
  2762. my @addrs = split(/\n/, $k);
  2763. my @path = ();
  2764. foreach my $a (@addrs) {
  2765. if (exists($symbols->{$a})) {
  2766. my $func = $symbols->{$a}->[0];
  2767. if ($skip{$func} || ($func =~ m/$skip_regexp/)) {
  2768. next;
  2769. }
  2770. }
  2771. push(@path, $a);
  2772. }
  2773. my $reduced_path = join("\n", @path);
  2774. AddEntry($result, $reduced_path, $count);
  2775. }
  2776. return $result;
  2777. }
  2778. # Reduce profile to granularity given by user
  2779. sub ReduceProfile {
  2780. my $symbols = shift;
  2781. my $profile = shift;
  2782. my $result = {};
  2783. my $fullname_to_shortname_map = {};
  2784. FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);
  2785. foreach my $k (keys(%{$profile})) {
  2786. my $count = $profile->{$k};
  2787. my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);
  2788. my @path = ();
  2789. my %seen = ();
  2790. $seen{''} = 1; # So that empty keys are skipped
  2791. foreach my $e (@translated) {
  2792. # To avoid double-counting due to recursion, skip a stack-trace
  2793. # entry if it has already been seen
  2794. if (!$seen{$e}) {
  2795. $seen{$e} = 1;
  2796. push(@path, $e);
  2797. }
  2798. }
  2799. my $reduced_path = join("\n", @path);
  2800. AddEntry($result, $reduced_path, $count);
  2801. }
  2802. return $result;
  2803. }
  2804. # Does the specified symbol array match the regexp?
  2805. sub SymbolMatches {
  2806. my $sym = shift;
  2807. my $re = shift;
  2808. if (defined($sym)) {
  2809. for (my $i = 0; $i < $#{$sym}; $i += 3) {
  2810. if ($sym->[$i] =~ m/$re/ || $sym->[$i+1] =~ m/$re/) {
  2811. return 1;
  2812. }
  2813. }
  2814. }
  2815. return 0;
  2816. }
  2817. # Focus only on paths involving specified regexps
  2818. sub FocusProfile {
  2819. my $symbols = shift;
  2820. my $profile = shift;
  2821. my $focus = shift;
  2822. my $result = {};
  2823. foreach my $k (keys(%{$profile})) {
  2824. my $count = $profile->{$k};
  2825. my @addrs = split(/\n/, $k);
  2826. foreach my $a (@addrs) {
  2827. # Reply if it matches either the address/shortname/fileline
  2828. if (($a =~ m/$focus/) || SymbolMatches($symbols->{$a}, $focus)) {
  2829. AddEntry($result, $k, $count);
  2830. last;
  2831. }
  2832. }
  2833. }
  2834. return $result;
  2835. }
  2836. # Focus only on paths not involving specified regexps
  2837. sub IgnoreProfile {
  2838. my $symbols = shift;
  2839. my $profile = shift;
  2840. my $ignore = shift;
  2841. my $result = {};
  2842. foreach my $k (keys(%{$profile})) {
  2843. my $count = $profile->{$k};
  2844. my @addrs = split(/\n/, $k);
  2845. my $matched = 0;
  2846. foreach my $a (@addrs) {
  2847. # Reply if it matches either the address/shortname/fileline
  2848. if (($a =~ m/$ignore/) || SymbolMatches($symbols->{$a}, $ignore)) {
  2849. $matched = 1;
  2850. last;
  2851. }
  2852. }
  2853. if (!$matched) {
  2854. AddEntry($result, $k, $count);
  2855. }
  2856. }
  2857. return $result;
  2858. }
  2859. # Get total count in profile
  2860. sub TotalProfile {
  2861. my $profile = shift;
  2862. my $result = 0;
  2863. foreach my $k (keys(%{$profile})) {
  2864. $result += $profile->{$k};
  2865. }
  2866. return $result;
  2867. }
  2868. # Add A to B
  2869. sub AddProfile {
  2870. my $A = shift;
  2871. my $B = shift;
  2872. my $R = {};
  2873. # add all keys in A
  2874. foreach my $k (keys(%{$A})) {
  2875. my $v = $A->{$k};
  2876. AddEntry($R, $k, $v);
  2877. }
  2878. # add all keys in B
  2879. foreach my $k (keys(%{$B})) {
  2880. my $v = $B->{$k};
  2881. AddEntry($R, $k, $v);
  2882. }
  2883. return $R;
  2884. }
  2885. # Merges symbol maps
  2886. sub MergeSymbols {
  2887. my $A = shift;
  2888. my $B = shift;
  2889. my $R = {};
  2890. foreach my $k (keys(%{$A})) {
  2891. $R->{$k} = $A->{$k};
  2892. }
  2893. if (defined($B)) {
  2894. foreach my $k (keys(%{$B})) {
  2895. $R->{$k} = $B->{$k};
  2896. }
  2897. }
  2898. return $R;
  2899. }
  2900. # Add A to B
  2901. sub AddPcs {
  2902. my $A = shift;
  2903. my $B = shift;
  2904. my $R = {};
  2905. # add all keys in A
  2906. foreach my $k (keys(%{$A})) {
  2907. $R->{$k} = 1
  2908. }
  2909. # add all keys in B
  2910. foreach my $k (keys(%{$B})) {
  2911. $R->{$k} = 1
  2912. }
  2913. return $R;
  2914. }
  2915. # Subtract B from A
  2916. sub SubtractProfile {
  2917. my $A = shift;
  2918. my $B = shift;
  2919. my $R = {};
  2920. foreach my $k (keys(%{$A})) {
  2921. my $v = $A->{$k} - GetEntry($B, $k);
  2922. if ($v < 0 && $main::opt_drop_negative) {
  2923. $v = 0;
  2924. }
  2925. AddEntry($R, $k, $v);
  2926. }
  2927. if (!$main::opt_drop_negative) {
  2928. # Take care of when subtracted profile has more entries
  2929. foreach my $k (keys(%{$B})) {
  2930. if (!exists($A->{$k})) {
  2931. AddEntry($R, $k, 0 - $B->{$k});
  2932. }
  2933. }
  2934. }
  2935. return $R;
  2936. }
  2937. # Get entry from profile; zero if not present
  2938. sub GetEntry {
  2939. my $profile = shift;
  2940. my $k = shift;
  2941. if (exists($profile->{$k})) {
  2942. return $profile->{$k};
  2943. } else {
  2944. return 0;
  2945. }
  2946. }
  2947. # Add entry to specified profile
  2948. sub AddEntry {
  2949. my $profile = shift;
  2950. my $k = shift;
  2951. my $n = shift;
  2952. if (!exists($profile->{$k})) {
  2953. $profile->{$k} = 0;
  2954. }
  2955. $profile->{$k} += $n;
  2956. }
  2957. # Add a stack of entries to specified profile, and add them to the $pcs
  2958. # list.
  2959. sub AddEntries {
  2960. my $profile = shift;
  2961. my $pcs = shift;
  2962. my $stack = shift;
  2963. my $count = shift;
  2964. my @k = ();
  2965. foreach my $e (split(/\s+/, $stack)) {
  2966. my $pc = HexExtend($e);
  2967. $pcs->{$pc} = 1;
  2968. push @k, $pc;
  2969. }
  2970. AddEntry($profile, (join "\n", @k), $count);
  2971. }
  2972. ##### Code to profile a server dynamically #####
  2973. sub CheckSymbolPage {
  2974. my $url = SymbolPageURL();
  2975. my $command = ShellEscape(@URL_FETCHER, $url);
  2976. open(SYMBOL, "$command |") or error($command);
  2977. my $line = <SYMBOL>;
  2978. $line =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  2979. close(SYMBOL);
  2980. unless (defined($line)) {
  2981. error("$url doesn't exist\n");
  2982. }
  2983. if ($line =~ /^num_symbols:\s+(\d+)$/) {
  2984. if ($1 == 0) {
  2985. error("Stripped binary. No symbols available.\n");
  2986. }
  2987. } else {
  2988. error("Failed to get the number of symbols from $url\n");
  2989. }
  2990. }
  2991. sub IsProfileURL {
  2992. my $profile_name = shift;
  2993. if (-f $profile_name) {
  2994. printf STDERR "Using local file $profile_name.\n";
  2995. return 0;
  2996. }
  2997. return 1;
  2998. }
  2999. sub ParseProfileURL {
  3000. my $profile_name = shift;
  3001. if (!defined($profile_name) || $profile_name eq "") {
  3002. return ();
  3003. }
  3004. # Split profile URL - matches all non-empty strings, so no test.
  3005. $profile_name =~ m,^(https?://)?([^/]+)(.*?)(/|$PROFILES)?$,;
  3006. my $proto = $1 || "http://";
  3007. my $hostport = $2;
  3008. my $prefix = $3;
  3009. my $profile = $4 || "/";
  3010. my $host = $hostport;
  3011. $host =~ s/:.*//;
  3012. my $baseurl = "$proto$hostport$prefix";
  3013. return ($host, $baseurl, $profile);
  3014. }
  3015. # We fetch symbols from the first profile argument.
  3016. sub SymbolPageURL {
  3017. my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);
  3018. return "$baseURL$SYMBOL_PAGE";
  3019. }
  3020. sub FetchProgramName() {
  3021. my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);
  3022. my $url = "$baseURL$PROGRAM_NAME_PAGE";
  3023. my $command_line = ShellEscape(@URL_FETCHER, $url);
  3024. open(CMDLINE, "$command_line |") or error($command_line);
  3025. my $cmdline = <CMDLINE>;
  3026. $cmdline =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  3027. close(CMDLINE);
  3028. error("Failed to get program name from $url\n") unless defined($cmdline);
  3029. $cmdline =~ s/\x00.+//; # Remove argv[1] and latters.
  3030. $cmdline =~ s!\n!!g; # Remove LFs.
  3031. return $cmdline;
  3032. }
  3033. # Gee, curl's -L (--location) option isn't reliable at least
  3034. # with its 7.12.3 version. Curl will forget to post data if
  3035. # there is a redirection. This function is a workaround for
  3036. # curl. Redirection happens on borg hosts.
  3037. sub ResolveRedirectionForCurl {
  3038. my $url = shift;
  3039. my $command_line = ShellEscape(@URL_FETCHER, "--head", $url);
  3040. open(CMDLINE, "$command_line |") or error($command_line);
  3041. while (<CMDLINE>) {
  3042. s/\r//g; # turn windows-looking lines into unix-looking lines
  3043. if (/^Location: (.*)/) {
  3044. $url = $1;
  3045. }
  3046. }
  3047. close(CMDLINE);
  3048. return $url;
  3049. }
  3050. # Add a timeout flat to URL_FETCHER. Returns a new list.
  3051. sub AddFetchTimeout {
  3052. my $timeout = shift;
  3053. my @fetcher = @_;
  3054. if (defined($timeout)) {
  3055. if (join(" ", @fetcher) =~ m/\bcurl -s/) {
  3056. push(@fetcher, "--max-time", sprintf("%d", $timeout));
  3057. } elsif (join(" ", @fetcher) =~ m/\brpcget\b/) {
  3058. push(@fetcher, sprintf("--deadline=%d", $timeout));
  3059. }
  3060. }
  3061. return @fetcher;
  3062. }
  3063. # Reads a symbol map from the file handle name given as $1, returning
  3064. # the resulting symbol map. Also processes variables relating to symbols.
  3065. # Currently, the only variable processed is 'binary=<value>' which updates
  3066. # $main::prog to have the correct program name.
  3067. sub ReadSymbols {
  3068. my $in = shift;
  3069. my $map = {};
  3070. while (<$in>) {
  3071. s/\r//g; # turn windows-looking lines into unix-looking lines
  3072. # Removes all the leading zeroes from the symbols, see comment below.
  3073. if (m/^0x0*([0-9a-f]+)\s+(.+)/) {
  3074. $map->{$1} = $2;
  3075. } elsif (m/^---/) {
  3076. last;
  3077. } elsif (m/^([a-z][^=]*)=(.*)$/ ) {
  3078. my ($variable, $value) = ($1, $2);
  3079. for ($variable, $value) {
  3080. s/^\s+//;
  3081. s/\s+$//;
  3082. }
  3083. if ($variable eq "binary") {
  3084. if ($main::prog ne $UNKNOWN_BINARY && $main::prog ne $value) {
  3085. printf STDERR ("Warning: Mismatched binary name '%s', using '%s'.\n",
  3086. $main::prog, $value);
  3087. }
  3088. $main::prog = $value;
  3089. } else {
  3090. printf STDERR ("Ignoring unknown variable in symbols list: " .
  3091. "'%s' = '%s'\n", $variable, $value);
  3092. }
  3093. }
  3094. }
  3095. return $map;
  3096. }
  3097. # Fetches and processes symbols to prepare them for use in the profile output
  3098. # code. If the optional 'symbol_map' arg is not given, fetches symbols from
  3099. # $SYMBOL_PAGE for all PC values found in profile. Otherwise, the raw symbols
  3100. # are assumed to have already been fetched into 'symbol_map' and are simply
  3101. # extracted and processed.
  3102. sub FetchSymbols {
  3103. my $pcset = shift;
  3104. my $symbol_map = shift;
  3105. my %seen = ();
  3106. my @pcs = grep { !$seen{$_}++ } keys(%$pcset); # uniq
  3107. if (!defined($symbol_map)) {
  3108. my $post_data = join("+", sort((map {"0x" . "$_"} @pcs)));
  3109. open(POSTFILE, ">$main::tmpfile_sym");
  3110. print POSTFILE $post_data;
  3111. close(POSTFILE);
  3112. my $url = SymbolPageURL();
  3113. my $command_line;
  3114. if (join(" ", @URL_FETCHER) =~ m/\bcurl -s/) {
  3115. $url = ResolveRedirectionForCurl($url);
  3116. $command_line = ShellEscape(@URL_FETCHER, "-d", "\@$main::tmpfile_sym",
  3117. $url);
  3118. } else {
  3119. $command_line = (ShellEscape(@URL_FETCHER, "--post", $url)
  3120. . " < " . ShellEscape($main::tmpfile_sym));
  3121. }
  3122. # We use c++filt in case $SYMBOL_PAGE gives us mangled symbols.
  3123. my $escaped_cppfilt = ShellEscape($obj_tool_map{"c++filt"});
  3124. open(SYMBOL, "$command_line | $escaped_cppfilt |") or error($command_line);
  3125. $symbol_map = ReadSymbols(*SYMBOL{IO});
  3126. close(SYMBOL);
  3127. }
  3128. my $symbols = {};
  3129. foreach my $pc (@pcs) {
  3130. my $fullname;
  3131. # For 64 bits binaries, symbols are extracted with 8 leading zeroes.
  3132. # Then /symbol reads the long symbols in as uint64, and outputs
  3133. # the result with a "0x%08llx" format which get rid of the zeroes.
  3134. # By removing all the leading zeroes in both $pc and the symbols from
  3135. # /symbol, the symbols match and are retrievable from the map.
  3136. my $shortpc = $pc;
  3137. $shortpc =~ s/^0*//;
  3138. # Each line may have a list of names, which includes the function
  3139. # and also other functions it has inlined. They are separated (in
  3140. # PrintSymbolizedProfile), by --, which is illegal in function names.
  3141. my $fullnames;
  3142. if (defined($symbol_map->{$shortpc})) {
  3143. $fullnames = $symbol_map->{$shortpc};
  3144. } else {
  3145. $fullnames = "0x" . $pc; # Just use addresses
  3146. }
  3147. my $sym = [];
  3148. $symbols->{$pc} = $sym;
  3149. foreach my $fullname (split("--", $fullnames)) {
  3150. my $name = ShortFunctionName($fullname);
  3151. push(@{$sym}, $name, "?", $fullname);
  3152. }
  3153. }
  3154. return $symbols;
  3155. }
  3156. sub BaseName {
  3157. my $file_name = shift;
  3158. $file_name =~ s!^.*/!!; # Remove directory name
  3159. return $file_name;
  3160. }
  3161. sub MakeProfileBaseName {
  3162. my ($binary_name, $profile_name) = @_;
  3163. my ($host, $baseURL, $path) = ParseProfileURL($profile_name);
  3164. my $binary_shortname = BaseName($binary_name);
  3165. return sprintf("%s.%s.%s",
  3166. $binary_shortname, $main::op_time, $host);
  3167. }
  3168. sub FetchDynamicProfile {
  3169. my $binary_name = shift;
  3170. my $profile_name = shift;
  3171. my $fetch_name_only = shift;
  3172. my $encourage_patience = shift;
  3173. if (!IsProfileURL($profile_name)) {
  3174. return $profile_name;
  3175. } else {
  3176. my ($host, $baseURL, $path) = ParseProfileURL($profile_name);
  3177. if ($path eq "" || $path eq "/") {
  3178. # Missing type specifier defaults to cpu-profile
  3179. $path = $PROFILE_PAGE;
  3180. }
  3181. my $profile_file = MakeProfileBaseName($binary_name, $profile_name);
  3182. my $url = "$baseURL$path";
  3183. my $fetch_timeout = undef;
  3184. if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE/) {
  3185. if ($path =~ m/[?]/) {
  3186. $url .= "&";
  3187. } else {
  3188. $url .= "?";
  3189. }
  3190. $url .= sprintf("seconds=%d", $main::opt_seconds);
  3191. $fetch_timeout = $main::opt_seconds * 1.01 + 60;
  3192. } else {
  3193. # For non-CPU profiles, we add a type-extension to
  3194. # the target profile file name.
  3195. my $suffix = $path;
  3196. $suffix =~ s,/,.,g;
  3197. $profile_file .= $suffix;
  3198. }
  3199. my $profile_dir = $ENV{"PPROF_TMPDIR"} || ($ENV{HOME} . "/pprof");
  3200. if (! -d $profile_dir) {
  3201. mkdir($profile_dir)
  3202. || die("Unable to create profile directory $profile_dir: $!\n");
  3203. }
  3204. my $tmp_profile = "$profile_dir/.tmp.$profile_file";
  3205. my $real_profile = "$profile_dir/$profile_file";
  3206. if ($fetch_name_only > 0) {
  3207. return $real_profile;
  3208. }
  3209. my @fetcher = AddFetchTimeout($fetch_timeout, @URL_FETCHER);
  3210. my $cmd = ShellEscape(@fetcher, $url) . " > " . ShellEscape($tmp_profile);
  3211. if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE|$CENSUSPROFILE_PAGE/){
  3212. print STDERR "Gathering CPU profile from $url for $main::opt_seconds seconds to\n ${real_profile}\n";
  3213. if ($encourage_patience) {
  3214. print STDERR "Be patient...\n";
  3215. }
  3216. } else {
  3217. print STDERR "Fetching $path profile from $url to\n ${real_profile}\n";
  3218. }
  3219. (system($cmd) == 0) || error("Failed to get profile: $cmd: $!\n");
  3220. (system("mv", $tmp_profile, $real_profile) == 0) || error("Unable to rename profile\n");
  3221. print STDERR "Wrote profile to $real_profile\n";
  3222. $main::collected_profile = $real_profile;
  3223. return $main::collected_profile;
  3224. }
  3225. }
  3226. # Collect profiles in parallel
  3227. sub FetchDynamicProfiles {
  3228. my $items = scalar(@main::pfile_args);
  3229. my $levels = log($items) / log(2);
  3230. if ($items == 1) {
  3231. $main::profile_files[0] = FetchDynamicProfile($main::prog, $main::pfile_args[0], 0, 1);
  3232. } else {
  3233. # math rounding issues
  3234. if ((2 ** $levels) < $items) {
  3235. $levels++;
  3236. }
  3237. my $count = scalar(@main::pfile_args);
  3238. for (my $i = 0; $i < $count; $i++) {
  3239. $main::profile_files[$i] = FetchDynamicProfile($main::prog, $main::pfile_args[$i], 1, 0);
  3240. }
  3241. print STDERR "Fetching $count profiles, Be patient...\n";
  3242. FetchDynamicProfilesRecurse($levels, 0, 0);
  3243. $main::collected_profile = join(" \\\n ", @main::profile_files);
  3244. }
  3245. }
  3246. # Recursively fork a process to get enough processes
  3247. # collecting profiles
  3248. sub FetchDynamicProfilesRecurse {
  3249. my $maxlevel = shift;
  3250. my $level = shift;
  3251. my $position = shift;
  3252. if (my $pid = fork()) {
  3253. $position = 0 | ($position << 1);
  3254. TryCollectProfile($maxlevel, $level, $position);
  3255. wait;
  3256. } else {
  3257. $position = 1 | ($position << 1);
  3258. TryCollectProfile($maxlevel, $level, $position);
  3259. cleanup();
  3260. exit(0);
  3261. }
  3262. }
  3263. # Collect a single profile
  3264. sub TryCollectProfile {
  3265. my $maxlevel = shift;
  3266. my $level = shift;
  3267. my $position = shift;
  3268. if ($level >= ($maxlevel - 1)) {
  3269. if ($position < scalar(@main::pfile_args)) {
  3270. FetchDynamicProfile($main::prog, $main::pfile_args[$position], 0, 0);
  3271. }
  3272. } else {
  3273. FetchDynamicProfilesRecurse($maxlevel, $level+1, $position);
  3274. }
  3275. }
  3276. ##### Parsing code #####
  3277. # Provide a small streaming-read module to handle very large
  3278. # cpu-profile files. Stream in chunks along a sliding window.
  3279. # Provides an interface to get one 'slot', correctly handling
  3280. # endian-ness differences. A slot is one 32-bit or 64-bit word
  3281. # (depending on the input profile). We tell endianness and bit-size
  3282. # for the profile by looking at the first 8 bytes: in cpu profiles,
  3283. # the second slot is always 3 (we'll accept anything that's not 0).
  3284. BEGIN {
  3285. package CpuProfileStream;
  3286. sub new {
  3287. my ($class, $file, $fname) = @_;
  3288. my $self = { file => $file,
  3289. base => 0,
  3290. stride => 512 * 1024, # must be a multiple of bitsize/8
  3291. slots => [],
  3292. unpack_code => "", # N for big-endian, V for little
  3293. perl_is_64bit => 1, # matters if profile is 64-bit
  3294. };
  3295. bless $self, $class;
  3296. # Let unittests adjust the stride
  3297. if ($main::opt_test_stride > 0) {
  3298. $self->{stride} = $main::opt_test_stride;
  3299. }
  3300. # Read the first two slots to figure out bitsize and endianness.
  3301. my $slots = $self->{slots};
  3302. my $str;
  3303. read($self->{file}, $str, 8);
  3304. # Set the global $address_length based on what we see here.
  3305. # 8 is 32-bit (8 hexadecimal chars); 16 is 64-bit (16 hexadecimal chars).
  3306. $address_length = ($str eq (chr(0)x8)) ? 16 : 8;
  3307. if ($address_length == 8) {
  3308. if (substr($str, 6, 2) eq chr(0)x2) {
  3309. $self->{unpack_code} = 'V'; # Little-endian.
  3310. } elsif (substr($str, 4, 2) eq chr(0)x2) {
  3311. $self->{unpack_code} = 'N'; # Big-endian
  3312. } else {
  3313. ::error("$fname: header size >= 2**16\n");
  3314. }
  3315. @$slots = unpack($self->{unpack_code} . "*", $str);
  3316. } else {
  3317. # If we're a 64-bit profile, check if we're a 64-bit-capable
  3318. # perl. Otherwise, each slot will be represented as a float
  3319. # instead of an int64, losing precision and making all the
  3320. # 64-bit addresses wrong. We won't complain yet, but will
  3321. # later if we ever see a value that doesn't fit in 32 bits.
  3322. my $has_q = 0;
  3323. eval { $has_q = pack("Q", "1") ? 1 : 1; };
  3324. if (!$has_q) {
  3325. $self->{perl_is_64bit} = 0;
  3326. }
  3327. read($self->{file}, $str, 8);
  3328. if (substr($str, 4, 4) eq chr(0)x4) {
  3329. # We'd love to use 'Q', but it's a) not universal, b) not endian-proof.
  3330. $self->{unpack_code} = 'V'; # Little-endian.
  3331. } elsif (substr($str, 0, 4) eq chr(0)x4) {
  3332. $self->{unpack_code} = 'N'; # Big-endian
  3333. } else {
  3334. ::error("$fname: header size >= 2**32\n");
  3335. }
  3336. my @pair = unpack($self->{unpack_code} . "*", $str);
  3337. # Since we know one of the pair is 0, it's fine to just add them.
  3338. @$slots = (0, $pair[0] + $pair[1]);
  3339. }
  3340. return $self;
  3341. }
  3342. # Load more data when we access slots->get(X) which is not yet in memory.
  3343. sub overflow {
  3344. my ($self) = @_;
  3345. my $slots = $self->{slots};
  3346. $self->{base} += $#$slots + 1; # skip over data we're replacing
  3347. my $str;
  3348. read($self->{file}, $str, $self->{stride});
  3349. if ($address_length == 8) { # the 32-bit case
  3350. # This is the easy case: unpack provides 32-bit unpacking primitives.
  3351. @$slots = unpack($self->{unpack_code} . "*", $str);
  3352. } else {
  3353. # We need to unpack 32 bits at a time and combine.
  3354. my @b32_values = unpack($self->{unpack_code} . "*", $str);
  3355. my @b64_values = ();
  3356. for (my $i = 0; $i < $#b32_values; $i += 2) {
  3357. # TODO(csilvers): if this is a 32-bit perl, the math below
  3358. # could end up in a too-large int, which perl will promote
  3359. # to a double, losing necessary precision. Deal with that.
  3360. # Right now, we just die.
  3361. my ($lo, $hi) = ($b32_values[$i], $b32_values[$i+1]);
  3362. if ($self->{unpack_code} eq 'N') { # big-endian
  3363. ($lo, $hi) = ($hi, $lo);
  3364. }
  3365. my $value = $lo + $hi * (2**32);
  3366. if (!$self->{perl_is_64bit} && # check value is exactly represented
  3367. (($value % (2**32)) != $lo || int($value / (2**32)) != $hi)) {
  3368. ::error("Need a 64-bit perl to process this 64-bit profile.\n");
  3369. }
  3370. push(@b64_values, $value);
  3371. }
  3372. @$slots = @b64_values;
  3373. }
  3374. }
  3375. # Access the i-th long in the file (logically), or -1 at EOF.
  3376. sub get {
  3377. my ($self, $idx) = @_;
  3378. my $slots = $self->{slots};
  3379. while ($#$slots >= 0) {
  3380. if ($idx < $self->{base}) {
  3381. # The only time we expect a reference to $slots[$i - something]
  3382. # after referencing $slots[$i] is reading the very first header.
  3383. # Since $stride > |header|, that shouldn't cause any lookback
  3384. # errors. And everything after the header is sequential.
  3385. print STDERR "Unexpected look-back reading CPU profile";
  3386. return -1; # shrug, don't know what better to return
  3387. } elsif ($idx > $self->{base} + $#$slots) {
  3388. $self->overflow();
  3389. } else {
  3390. return $slots->[$idx - $self->{base}];
  3391. }
  3392. }
  3393. # If we get here, $slots is [], which means we've reached EOF
  3394. return -1; # unique since slots is supposed to hold unsigned numbers
  3395. }
  3396. }
  3397. # Reads the top, 'header' section of a profile, and returns the last
  3398. # line of the header, commonly called a 'header line'. The header
  3399. # section of a profile consists of zero or more 'command' lines that
  3400. # are instructions to pprof, which pprof executes when reading the
  3401. # header. All 'command' lines start with a %. After the command
  3402. # lines is the 'header line', which is a profile-specific line that
  3403. # indicates what type of profile it is, and perhaps other global
  3404. # information about the profile. For instance, here's a header line
  3405. # for a heap profile:
  3406. # heap profile: 53: 38236 [ 5525: 1284029] @ heapprofile
  3407. # For historical reasons, the CPU profile does not contain a text-
  3408. # readable header line. If the profile looks like a CPU profile,
  3409. # this function returns "". If no header line could be found, this
  3410. # function returns undef.
  3411. #
  3412. # The following commands are recognized:
  3413. # %warn -- emit the rest of this line to stderr, prefixed by 'WARNING:'
  3414. #
  3415. # The input file should be in binmode.
  3416. sub ReadProfileHeader {
  3417. local *PROFILE = shift;
  3418. my $firstchar = "";
  3419. my $line = "";
  3420. read(PROFILE, $firstchar, 1);
  3421. seek(PROFILE, -1, 1); # unread the firstchar
  3422. if ($firstchar !~ /[[:print:]]/) { # is not a text character
  3423. return "";
  3424. }
  3425. while (defined($line = <PROFILE>)) {
  3426. $line =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  3427. if ($line =~ /^%warn\s+(.*)/) { # 'warn' command
  3428. # Note this matches both '%warn blah\n' and '%warn\n'.
  3429. print STDERR "WARNING: $1\n"; # print the rest of the line
  3430. } elsif ($line =~ /^%/) {
  3431. print STDERR "Ignoring unknown command from profile header: $line";
  3432. } else {
  3433. # End of commands, must be the header line.
  3434. return $line;
  3435. }
  3436. }
  3437. return undef; # got to EOF without seeing a header line
  3438. }
  3439. sub IsSymbolizedProfileFile {
  3440. my $file_name = shift;
  3441. if (!(-e $file_name) || !(-r $file_name)) {
  3442. return 0;
  3443. }
  3444. # Check if the file contains a symbol-section marker.
  3445. open(TFILE, "<$file_name");
  3446. binmode TFILE;
  3447. my $firstline = ReadProfileHeader(*TFILE);
  3448. close(TFILE);
  3449. if (!$firstline) {
  3450. return 0;
  3451. }
  3452. $SYMBOL_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  3453. my $symbol_marker = $&;
  3454. return $firstline =~ /^--- *$symbol_marker/;
  3455. }
  3456. # Parse profile generated by common/profiler.cc and return a reference
  3457. # to a map:
  3458. # $result->{version} Version number of profile file
  3459. # $result->{period} Sampling period (in microseconds)
  3460. # $result->{profile} Profile object
  3461. # $result->{map} Memory map info from profile
  3462. # $result->{pcs} Hash of all PC values seen, key is hex address
  3463. sub ReadProfile {
  3464. my $prog = shift;
  3465. my $fname = shift;
  3466. my $result; # return value
  3467. $CONTENTION_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  3468. my $contention_marker = $&;
  3469. $GROWTH_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  3470. my $growth_marker = $&;
  3471. $SYMBOL_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  3472. my $symbol_marker = $&;
  3473. $PROFILE_PAGE =~ m,[^/]+$,; # matches everything after the last slash
  3474. my $profile_marker = $&;
  3475. # Look at first line to see if it is a heap or a CPU profile.
  3476. # CPU profile may start with no header at all, and just binary data
  3477. # (starting with \0\0\0\0) -- in that case, don't try to read the
  3478. # whole firstline, since it may be gigabytes(!) of data.
  3479. open(PROFILE, "<$fname") || error("$fname: $!\n");
  3480. binmode PROFILE; # New perls do UTF-8 processing
  3481. my $header = ReadProfileHeader(*PROFILE);
  3482. if (!defined($header)) { # means "at EOF"
  3483. error("Profile is empty.\n");
  3484. }
  3485. my $symbols;
  3486. if ($header =~ m/^--- *$symbol_marker/o) {
  3487. # Verify that the user asked for a symbolized profile
  3488. if (!$main::use_symbolized_profile) {
  3489. # we have both a binary and symbolized profiles, abort
  3490. error("FATAL ERROR: Symbolized profile\n $fname\ncannot be used with " .
  3491. "a binary arg. Try again without passing\n $prog\n");
  3492. }
  3493. # Read the symbol section of the symbolized profile file.
  3494. $symbols = ReadSymbols(*PROFILE{IO});
  3495. # Read the next line to get the header for the remaining profile.
  3496. $header = ReadProfileHeader(*PROFILE) || "";
  3497. }
  3498. $main::profile_type = '';
  3499. if ($header =~ m/^heap profile:.*$growth_marker/o) {
  3500. $main::profile_type = 'growth';
  3501. $result = ReadHeapProfile($prog, *PROFILE, $header);
  3502. } elsif ($header =~ m/^heap profile:/) {
  3503. $main::profile_type = 'heap';
  3504. $result = ReadHeapProfile($prog, *PROFILE, $header);
  3505. } elsif ($header =~ m/^--- *$contention_marker/o) {
  3506. $main::profile_type = 'contention';
  3507. $result = ReadSynchProfile($prog, *PROFILE);
  3508. } elsif ($header =~ m/^--- *Stacks:/) {
  3509. print STDERR
  3510. "Old format contention profile: mistakenly reports " .
  3511. "condition variable signals as lock contentions.\n";
  3512. $main::profile_type = 'contention';
  3513. $result = ReadSynchProfile($prog, *PROFILE);
  3514. } elsif ($header =~ m/^--- *$profile_marker/) {
  3515. # the binary cpu profile data starts immediately after this line
  3516. $main::profile_type = 'cpu';
  3517. $result = ReadCPUProfile($prog, $fname, *PROFILE);
  3518. } else {
  3519. if (defined($symbols)) {
  3520. # a symbolized profile contains a format we don't recognize, bail out
  3521. error("$fname: Cannot recognize profile section after symbols.\n");
  3522. }
  3523. # no ascii header present -- must be a CPU profile
  3524. $main::profile_type = 'cpu';
  3525. $result = ReadCPUProfile($prog, $fname, *PROFILE);
  3526. }
  3527. close(PROFILE);
  3528. # if we got symbols along with the profile, return those as well
  3529. if (defined($symbols)) {
  3530. $result->{symbols} = $symbols;
  3531. }
  3532. return $result;
  3533. }
  3534. # Subtract one from caller pc so we map back to call instr.
  3535. # However, don't do this if we're reading a symbolized profile
  3536. # file, in which case the subtract-one was done when the file
  3537. # was written.
  3538. #
  3539. # We apply the same logic to all readers, though ReadCPUProfile uses an
  3540. # independent implementation.
  3541. sub FixCallerAddresses {
  3542. my $stack = shift;
  3543. if ($main::use_symbolized_profile) {
  3544. return $stack;
  3545. } else {
  3546. $stack =~ /(\s)/;
  3547. my $delimiter = $1;
  3548. my @addrs = split(' ', $stack);
  3549. my @fixedaddrs;
  3550. $#fixedaddrs = $#addrs;
  3551. if ($#addrs >= 0) {
  3552. $fixedaddrs[0] = $addrs[0];
  3553. }
  3554. for (my $i = 1; $i <= $#addrs; $i++) {
  3555. $fixedaddrs[$i] = AddressSub($addrs[$i], "0x1");
  3556. }
  3557. return join $delimiter, @fixedaddrs;
  3558. }
  3559. }
  3560. # CPU profile reader
  3561. sub ReadCPUProfile {
  3562. my $prog = shift;
  3563. my $fname = shift; # just used for logging
  3564. local *PROFILE = shift;
  3565. my $version;
  3566. my $period;
  3567. my $i;
  3568. my $profile = {};
  3569. my $pcs = {};
  3570. # Parse string into array of slots.
  3571. my $slots = CpuProfileStream->new(*PROFILE, $fname);
  3572. # Read header. The current header version is a 5-element structure
  3573. # containing:
  3574. # 0: header count (always 0)
  3575. # 1: header "words" (after this one: 3)
  3576. # 2: format version (0)
  3577. # 3: sampling period (usec)
  3578. # 4: unused padding (always 0)
  3579. if ($slots->get(0) != 0 ) {
  3580. error("$fname: not a profile file, or old format profile file\n");
  3581. }
  3582. $i = 2 + $slots->get(1);
  3583. $version = $slots->get(2);
  3584. $period = $slots->get(3);
  3585. # Do some sanity checking on these header values.
  3586. if ($version > (2**32) || $period > (2**32) || $i > (2**32) || $i < 5) {
  3587. error("$fname: not a profile file, or corrupted profile file\n");
  3588. }
  3589. # Parse profile
  3590. while ($slots->get($i) != -1) {
  3591. my $n = $slots->get($i++);
  3592. my $d = $slots->get($i++);
  3593. if ($d > (2**16)) { # TODO(csilvers): what's a reasonable max-stack-depth?
  3594. my $addr = sprintf("0%o", $i * ($address_length == 8 ? 4 : 8));
  3595. print STDERR "At index $i (address $addr):\n";
  3596. error("$fname: stack trace depth >= 2**32\n");
  3597. }
  3598. if ($slots->get($i) == 0) {
  3599. # End of profile data marker
  3600. $i += $d;
  3601. last;
  3602. }
  3603. # Make key out of the stack entries
  3604. my @k = ();
  3605. for (my $j = 0; $j < $d; $j++) {
  3606. my $pc = $slots->get($i+$j);
  3607. # Subtract one from caller pc so we map back to call instr.
  3608. # However, don't do this if we're reading a symbolized profile
  3609. # file, in which case the subtract-one was done when the file
  3610. # was written.
  3611. if ($j > 0 && !$main::use_symbolized_profile) {
  3612. $pc--;
  3613. }
  3614. $pc = sprintf("%0*x", $address_length, $pc);
  3615. $pcs->{$pc} = 1;
  3616. push @k, $pc;
  3617. }
  3618. AddEntry($profile, (join "\n", @k), $n);
  3619. $i += $d;
  3620. }
  3621. # Parse map
  3622. my $map = '';
  3623. seek(PROFILE, $i * ($address_length / 2), 0);
  3624. read(PROFILE, $map, (stat PROFILE)[7]);
  3625. my $r = {};
  3626. $r->{version} = $version;
  3627. $r->{period} = $period;
  3628. $r->{profile} = $profile;
  3629. $r->{libs} = ParseLibraries($prog, $map, $pcs);
  3630. $r->{pcs} = $pcs;
  3631. return $r;
  3632. }
  3633. sub ReadHeapProfile {
  3634. my $prog = shift;
  3635. local *PROFILE = shift;
  3636. my $header = shift;
  3637. my $index = 1;
  3638. if ($main::opt_inuse_space) {
  3639. $index = 1;
  3640. } elsif ($main::opt_inuse_objects) {
  3641. $index = 0;
  3642. } elsif ($main::opt_alloc_space) {
  3643. $index = 3;
  3644. } elsif ($main::opt_alloc_objects) {
  3645. $index = 2;
  3646. }
  3647. # Find the type of this profile. The header line looks like:
  3648. # heap profile: 1246: 8800744 [ 1246: 8800744] @ <heap-url>/266053
  3649. # There are two pairs <count: size>, the first inuse objects/space, and the
  3650. # second allocated objects/space. This is followed optionally by a profile
  3651. # type, and if that is present, optionally by a sampling frequency.
  3652. # For remote heap profiles (v1):
  3653. # The interpretation of the sampling frequency is that the profiler, for
  3654. # each sample, calculates a uniformly distributed random integer less than
  3655. # the given value, and records the next sample after that many bytes have
  3656. # been allocated. Therefore, the expected sample interval is half of the
  3657. # given frequency. By default, if not specified, the expected sample
  3658. # interval is 128KB. Only remote-heap-page profiles are adjusted for
  3659. # sample size.
  3660. # For remote heap profiles (v2):
  3661. # The sampling frequency is the rate of a Poisson process. This means that
  3662. # the probability of sampling an allocation of size X with sampling rate Y
  3663. # is 1 - exp(-X/Y)
  3664. # For version 2, a typical header line might look like this:
  3665. # heap profile: 1922: 127792360 [ 1922: 127792360] @ <heap-url>_v2/524288
  3666. # the trailing number (524288) is the sampling rate. (Version 1 showed
  3667. # double the 'rate' here)
  3668. my $sampling_algorithm = 0;
  3669. my $sample_adjustment = 0;
  3670. chomp($header);
  3671. my $type = "unknown";
  3672. if ($header =~ m"^heap profile:\s*(\d+):\s+(\d+)\s+\[\s*(\d+):\s+(\d+)\](\s*@\s*([^/]*)(/(\d+))?)?") {
  3673. if (defined($6) && ($6 ne '')) {
  3674. $type = $6;
  3675. my $sample_period = $8;
  3676. # $type is "heapprofile" for profiles generated by the
  3677. # heap-profiler, and either "heap" or "heap_v2" for profiles
  3678. # generated by sampling directly within tcmalloc. It can also
  3679. # be "growth" for heap-growth profiles. The first is typically
  3680. # found for profiles generated locally, and the others for
  3681. # remote profiles.
  3682. if (($type eq "heapprofile") || ($type !~ /heap/) ) {
  3683. # No need to adjust for the sampling rate with heap-profiler-derived data
  3684. $sampling_algorithm = 0;
  3685. } elsif ($type =~ /_v2/) {
  3686. $sampling_algorithm = 2; # version 2 sampling
  3687. if (defined($sample_period) && ($sample_period ne '')) {
  3688. $sample_adjustment = int($sample_period);
  3689. }
  3690. } else {
  3691. $sampling_algorithm = 1; # version 1 sampling
  3692. if (defined($sample_period) && ($sample_period ne '')) {
  3693. $sample_adjustment = int($sample_period)/2;
  3694. }
  3695. }
  3696. } else {
  3697. # We detect whether or not this is a remote-heap profile by checking
  3698. # that the total-allocated stats ($n2,$s2) are exactly the
  3699. # same as the in-use stats ($n1,$s1). It is remotely conceivable
  3700. # that a non-remote-heap profile may pass this check, but it is hard
  3701. # to imagine how that could happen.
  3702. # In this case it's so old it's guaranteed to be remote-heap version 1.
  3703. my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);
  3704. if (($n1 == $n2) && ($s1 == $s2)) {
  3705. # This is likely to be a remote-heap based sample profile
  3706. $sampling_algorithm = 1;
  3707. }
  3708. }
  3709. }
  3710. if ($sampling_algorithm > 0) {
  3711. # For remote-heap generated profiles, adjust the counts and sizes to
  3712. # account for the sample rate (we sample once every 128KB by default).
  3713. if ($sample_adjustment == 0) {
  3714. # Turn on profile adjustment.
  3715. $sample_adjustment = 128*1024;
  3716. print STDERR "Adjusting heap profiles for 1-in-128KB sampling rate\n";
  3717. } else {
  3718. printf STDERR ("Adjusting heap profiles for 1-in-%d sampling rate\n",
  3719. $sample_adjustment);
  3720. }
  3721. if ($sampling_algorithm > 1) {
  3722. # We don't bother printing anything for the original version (version 1)
  3723. printf STDERR "Heap version $sampling_algorithm\n";
  3724. }
  3725. }
  3726. my $profile = {};
  3727. my $pcs = {};
  3728. my $map = "";
  3729. while (<PROFILE>) {
  3730. s/\r//g; # turn windows-looking lines into unix-looking lines
  3731. if (/^MAPPED_LIBRARIES:/) {
  3732. # Read the /proc/self/maps data
  3733. while (<PROFILE>) {
  3734. s/\r//g; # turn windows-looking lines into unix-looking lines
  3735. $map .= $_;
  3736. }
  3737. last;
  3738. }
  3739. if (/^--- Memory map:/) {
  3740. # Read /proc/self/maps data as formatted by DumpAddressMap()
  3741. my $buildvar = "";
  3742. while (<PROFILE>) {
  3743. s/\r//g; # turn windows-looking lines into unix-looking lines
  3744. # Parse "build=<dir>" specification if supplied
  3745. if (m/^\s*build=(.*)\n/) {
  3746. $buildvar = $1;
  3747. }
  3748. # Expand "$build" variable if available
  3749. $_ =~ s/\$build\b/$buildvar/g;
  3750. $map .= $_;
  3751. }
  3752. last;
  3753. }
  3754. # Read entry of the form:
  3755. # <count1>: <bytes1> [<count2>: <bytes2>] @ a1 a2 a3 ... an
  3756. s/^\s*//;
  3757. s/\s*$//;
  3758. if (m/^\s*(\d+):\s+(\d+)\s+\[\s*(\d+):\s+(\d+)\]\s+@\s+(.*)$/) {
  3759. my $stack = $5;
  3760. my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);
  3761. if ($sample_adjustment) {
  3762. if ($sampling_algorithm == 2) {
  3763. # Remote-heap version 2
  3764. # The sampling frequency is the rate of a Poisson process.
  3765. # This means that the probability of sampling an allocation of
  3766. # size X with sampling rate Y is 1 - exp(-X/Y)
  3767. if ($n1 != 0) {
  3768. my $ratio = (($s1*1.0)/$n1)/($sample_adjustment);
  3769. my $scale_factor = 1/(1 - exp(-$ratio));
  3770. $n1 *= $scale_factor;
  3771. $s1 *= $scale_factor;
  3772. }
  3773. if ($n2 != 0) {
  3774. my $ratio = (($s2*1.0)/$n2)/($sample_adjustment);
  3775. my $scale_factor = 1/(1 - exp(-$ratio));
  3776. $n2 *= $scale_factor;
  3777. $s2 *= $scale_factor;
  3778. }
  3779. } else {
  3780. # Remote-heap version 1
  3781. my $ratio;
  3782. $ratio = (($s1*1.0)/$n1)/($sample_adjustment);
  3783. if ($ratio < 1) {
  3784. $n1 /= $ratio;
  3785. $s1 /= $ratio;
  3786. }
  3787. $ratio = (($s2*1.0)/$n2)/($sample_adjustment);
  3788. if ($ratio < 1) {
  3789. $n2 /= $ratio;
  3790. $s2 /= $ratio;
  3791. }
  3792. }
  3793. }
  3794. my @counts = ($n1, $s1, $n2, $s2);
  3795. $stack = FixCallerAddresses($stack);
  3796. push @stackTraces, "$n1 $s1 $n2 $s2 $stack";
  3797. AddEntries($profile, $pcs, $stack, $counts[$index]);
  3798. }
  3799. }
  3800. my $r = {};
  3801. $r->{version} = "heap";
  3802. $r->{period} = 1;
  3803. $r->{profile} = $profile;
  3804. $r->{libs} = ParseLibraries($prog, $map, $pcs);
  3805. $r->{pcs} = $pcs;
  3806. return $r;
  3807. }
  3808. sub ReadSynchProfile {
  3809. my $prog = shift;
  3810. local *PROFILE = shift;
  3811. my $header = shift;
  3812. my $map = '';
  3813. my $profile = {};
  3814. my $pcs = {};
  3815. my $sampling_period = 1;
  3816. my $cyclespernanosec = 2.8; # Default assumption for old binaries
  3817. my $seen_clockrate = 0;
  3818. my $line;
  3819. my $index = 0;
  3820. if ($main::opt_total_delay) {
  3821. $index = 0;
  3822. } elsif ($main::opt_contentions) {
  3823. $index = 1;
  3824. } elsif ($main::opt_mean_delay) {
  3825. $index = 2;
  3826. }
  3827. while ( $line = <PROFILE> ) {
  3828. $line =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  3829. if ( $line =~ /^\s*(\d+)\s+(\d+) \@\s*(.*?)\s*$/ ) {
  3830. my ($cycles, $count, $stack) = ($1, $2, $3);
  3831. # Convert cycles to nanoseconds
  3832. $cycles /= $cyclespernanosec;
  3833. # Adjust for sampling done by application
  3834. $cycles *= $sampling_period;
  3835. $count *= $sampling_period;
  3836. my @values = ($cycles, $count, $cycles / $count);
  3837. AddEntries($profile, $pcs, FixCallerAddresses($stack), $values[$index]);
  3838. } elsif ( $line =~ /^(slow release).*thread \d+ \@\s*(.*?)\s*$/ ||
  3839. $line =~ /^\s*(\d+) \@\s*(.*?)\s*$/ ) {
  3840. my ($cycles, $stack) = ($1, $2);
  3841. if ($cycles !~ /^\d+$/) {
  3842. next;
  3843. }
  3844. # Convert cycles to nanoseconds
  3845. $cycles /= $cyclespernanosec;
  3846. # Adjust for sampling done by application
  3847. $cycles *= $sampling_period;
  3848. AddEntries($profile, $pcs, FixCallerAddresses($stack), $cycles);
  3849. } elsif ( $line =~ m/^([a-z][^=]*)=(.*)$/ ) {
  3850. my ($variable, $value) = ($1,$2);
  3851. for ($variable, $value) {
  3852. s/^\s+//;
  3853. s/\s+$//;
  3854. }
  3855. if ($variable eq "cycles/second") {
  3856. $cyclespernanosec = $value / 1e9;
  3857. $seen_clockrate = 1;
  3858. } elsif ($variable eq "sampling period") {
  3859. $sampling_period = $value;
  3860. } elsif ($variable eq "ms since reset") {
  3861. # Currently nothing is done with this value in pprof
  3862. # So we just silently ignore it for now
  3863. } elsif ($variable eq "discarded samples") {
  3864. # Currently nothing is done with this value in pprof
  3865. # So we just silently ignore it for now
  3866. } else {
  3867. printf STDERR ("Ignoring unnknown variable in /contention output: " .
  3868. "'%s' = '%s'\n",$variable,$value);
  3869. }
  3870. } else {
  3871. # Memory map entry
  3872. $map .= $line;
  3873. }
  3874. }
  3875. if (!$seen_clockrate) {
  3876. printf STDERR ("No cycles/second entry in profile; Guessing %.1f GHz\n",
  3877. $cyclespernanosec);
  3878. }
  3879. my $r = {};
  3880. $r->{version} = 0;
  3881. $r->{period} = $sampling_period;
  3882. $r->{profile} = $profile;
  3883. $r->{libs} = ParseLibraries($prog, $map, $pcs);
  3884. $r->{pcs} = $pcs;
  3885. return $r;
  3886. }
  3887. # Given a hex value in the form "0x1abcd" or "1abcd", return either
  3888. # "0001abcd" or "000000000001abcd", depending on the current (global)
  3889. # address length.
  3890. sub HexExtend {
  3891. my $addr = shift;
  3892. $addr =~ s/^(0x)?0*//;
  3893. my $zeros_needed = $address_length - length($addr);
  3894. if ($zeros_needed < 0) {
  3895. printf STDERR "Warning: address $addr is longer than address length $address_length\n";
  3896. return $addr;
  3897. }
  3898. return ("0" x $zeros_needed) . $addr;
  3899. }
  3900. ##### Symbol extraction #####
  3901. # Aggressively search the lib_prefix values for the given library
  3902. # If all else fails, just return the name of the library unmodified.
  3903. # If the lib_prefix is "/my/path,/other/path" and $file is "/lib/dir/mylib.so"
  3904. # it will search the following locations in this order, until it finds a file:
  3905. # /my/path/lib/dir/mylib.so
  3906. # /other/path/lib/dir/mylib.so
  3907. # /my/path/dir/mylib.so
  3908. # /other/path/dir/mylib.so
  3909. # /my/path/mylib.so
  3910. # /other/path/mylib.so
  3911. # /lib/dir/mylib.so (returned as last resort)
  3912. sub FindLibrary {
  3913. my $file = shift;
  3914. my $suffix = $file;
  3915. # Search for the library as described above
  3916. do {
  3917. foreach my $prefix (@prefix_list) {
  3918. my $fullpath = $prefix . $suffix;
  3919. if (-e $fullpath) {
  3920. return $fullpath;
  3921. }
  3922. }
  3923. } while ($suffix =~ s|^/[^/]+/|/|);
  3924. return $file;
  3925. }
  3926. # Return path to library with debugging symbols.
  3927. # For libc libraries, the copy in /usr/lib/debug contains debugging symbols
  3928. sub DebuggingLibrary {
  3929. my $file = shift;
  3930. if ($file =~ m|^/| && -f "/usr/lib/debug$file") {
  3931. return "/usr/lib/debug$file";
  3932. }
  3933. if ($file =~ m|^/| && -f "/usr/lib/debug$file.debug") {
  3934. return "/usr/lib/debug$file.debug";
  3935. }
  3936. return undef;
  3937. }
  3938. # Parse text section header of a library using objdump
  3939. sub ParseTextSectionHeaderFromObjdump {
  3940. my $lib = shift;
  3941. my $size = undef;
  3942. my $vma;
  3943. my $file_offset;
  3944. # Get objdump output from the library file to figure out how to
  3945. # map between mapped addresses and addresses in the library.
  3946. my $cmd = ShellEscape($obj_tool_map{"objdump"}, "-h", $lib);
  3947. open(OBJDUMP, "$cmd |") || error("$cmd: $!\n");
  3948. while (<OBJDUMP>) {
  3949. s/\r//g; # turn windows-looking lines into unix-looking lines
  3950. # Idx Name Size VMA LMA File off Algn
  3951. # 10 .text 00104b2c 420156f0 420156f0 000156f0 2**4
  3952. # For 64-bit objects, VMA and LMA will be 16 hex digits, size and file
  3953. # offset may still be 8. But AddressSub below will still handle that.
  3954. my @x = split;
  3955. if (($#x >= 6) && ($x[1] eq '.text')) {
  3956. $size = $x[2];
  3957. $vma = $x[3];
  3958. $file_offset = $x[5];
  3959. last;
  3960. }
  3961. }
  3962. close(OBJDUMP);
  3963. if (!defined($size)) {
  3964. return undef;
  3965. }
  3966. my $r = {};
  3967. $r->{size} = $size;
  3968. $r->{vma} = $vma;
  3969. $r->{file_offset} = $file_offset;
  3970. return $r;
  3971. }
  3972. # Parse text section header of a library using otool (on OS X)
  3973. sub ParseTextSectionHeaderFromOtool {
  3974. my $lib = shift;
  3975. my $size = undef;
  3976. my $vma = undef;
  3977. my $file_offset = undef;
  3978. # Get otool output from the library file to figure out how to
  3979. # map between mapped addresses and addresses in the library.
  3980. my $command = ShellEscape($obj_tool_map{"otool"}, "-l", $lib);
  3981. open(OTOOL, "$command |") || error("$command: $!\n");
  3982. my $cmd = "";
  3983. my $sectname = "";
  3984. my $segname = "";
  3985. foreach my $line (<OTOOL>) {
  3986. $line =~ s/\r//g; # turn windows-looking lines into unix-looking lines
  3987. # Load command <#>
  3988. # cmd LC_SEGMENT
  3989. # [...]
  3990. # Section
  3991. # sectname __text
  3992. # segname __TEXT
  3993. # addr 0x000009f8
  3994. # size 0x00018b9e
  3995. # offset 2552
  3996. # align 2^2 (4)
  3997. # We will need to strip off the leading 0x from the hex addresses,
  3998. # and convert the offset into hex.
  3999. if ($line =~ /Load command/) {
  4000. $cmd = "";
  4001. $sectname = "";
  4002. $segname = "";
  4003. } elsif ($line =~ /Section/) {
  4004. $sectname = "";
  4005. $segname = "";
  4006. } elsif ($line =~ /cmd (\w+)/) {
  4007. $cmd = $1;
  4008. } elsif ($line =~ /sectname (\w+)/) {
  4009. $sectname = $1;
  4010. } elsif ($line =~ /segname (\w+)/) {
  4011. $segname = $1;
  4012. } elsif (!(($cmd eq "LC_SEGMENT" || $cmd eq "LC_SEGMENT_64") &&
  4013. $sectname eq "__text" &&
  4014. $segname eq "__TEXT")) {
  4015. next;
  4016. } elsif ($line =~ /\baddr 0x([0-9a-fA-F]+)/) {
  4017. $vma = $1;
  4018. } elsif ($line =~ /\bsize 0x([0-9a-fA-F]+)/) {
  4019. $size = $1;
  4020. } elsif ($line =~ /\boffset ([0-9]+)/) {
  4021. $file_offset = sprintf("%016x", $1);
  4022. }
  4023. if (defined($vma) && defined($size) && defined($file_offset)) {
  4024. last;
  4025. }
  4026. }
  4027. close(OTOOL);
  4028. if (!defined($vma) || !defined($size) || !defined($file_offset)) {
  4029. return undef;
  4030. }
  4031. my $r = {};
  4032. $r->{size} = $size;
  4033. $r->{vma} = $vma;
  4034. $r->{file_offset} = $file_offset;
  4035. return $r;
  4036. }
  4037. sub ParseTextSectionHeader {
  4038. # obj_tool_map("otool") is only defined if we're in a Mach-O environment
  4039. if (defined($obj_tool_map{"otool"})) {
  4040. my $r = ParseTextSectionHeaderFromOtool(@_);
  4041. if (defined($r)){
  4042. return $r;
  4043. }
  4044. }
  4045. # If otool doesn't work, or we don't have it, fall back to objdump
  4046. return ParseTextSectionHeaderFromObjdump(@_);
  4047. }
  4048. # Split /proc/pid/maps dump into a list of libraries
  4049. sub ParseLibraries {
  4050. return if $main::use_symbol_page; # We don't need libraries info.
  4051. my $prog = Cwd::abs_path(shift);
  4052. my $map = shift;
  4053. my $pcs = shift;
  4054. my $result = [];
  4055. my $h = "[a-f0-9]+";
  4056. my $zero_offset = HexExtend("0");
  4057. my $buildvar = "";
  4058. foreach my $l (split("\n", $map)) {
  4059. if ($l =~ m/^\s*build=(.*)$/) {
  4060. $buildvar = $1;
  4061. }
  4062. my $start;
  4063. my $finish;
  4064. my $offset;
  4065. my $lib;
  4066. if ($l =~ /^($h)-($h)\s+..x.\s+($h)\s+\S+:\S+\s+\d+\s+(.+\.(so|dll|dylib|bundle|node)((\.\d+)+\w*(\.\d+){0,3})?)$/i) {
  4067. # Full line from /proc/self/maps. Example:
  4068. # 40000000-40015000 r-xp 00000000 03:01 12845071 /lib/ld-2.3.2.so
  4069. $start = HexExtend($1);
  4070. $finish = HexExtend($2);
  4071. $offset = HexExtend($3);
  4072. $lib = $4;
  4073. $lib =~ s|\\|/|g; # turn windows-style paths into unix-style paths
  4074. } elsif ($l =~ /^\s*($h)-($h):\s*(\S+\.so(\.\d+)*)/) {
  4075. # Cooked line from DumpAddressMap. Example:
  4076. # 40000000-40015000: /lib/ld-2.3.2.so
  4077. $start = HexExtend($1);
  4078. $finish = HexExtend($2);
  4079. $offset = $zero_offset;
  4080. $lib = $3;
  4081. } elsif (($l =~ /^($h)-($h)\s+..x.\s+($h)\s+\S+:\S+\s+\d+\s+(\S+)$/i) && ($4 eq $prog)) {
  4082. # PIEs and address space randomization do not play well with our
  4083. # default assumption that main executable is at lowest
  4084. # addresses. So we're detecting main executable in
  4085. # /proc/self/maps as well.
  4086. $start = HexExtend($1);
  4087. $finish = HexExtend($2);
  4088. $offset = HexExtend($3);
  4089. $lib = $4;
  4090. $lib =~ s|\\|/|g; # turn windows-style paths into unix-style paths
  4091. } else {
  4092. next;
  4093. }
  4094. # Expand "$build" variable if available
  4095. $lib =~ s/\$build\b/$buildvar/g;
  4096. $lib = FindLibrary($lib);
  4097. # Check for pre-relocated libraries, which use pre-relocated symbol tables
  4098. # and thus require adjusting the offset that we'll use to translate
  4099. # VM addresses into symbol table addresses.
  4100. # Only do this if we're not going to fetch the symbol table from a
  4101. # debugging copy of the library.
  4102. if (!DebuggingLibrary($lib)) {
  4103. my $text = ParseTextSectionHeader($lib);
  4104. if (defined($text)) {
  4105. my $vma_offset = AddressSub($text->{vma}, $text->{file_offset});
  4106. $offset = AddressAdd($offset, $vma_offset);
  4107. }
  4108. }
  4109. push(@{$result}, [$lib, $start, $finish, $offset]);
  4110. }
  4111. # Append special entry for additional library (not relocated)
  4112. if ($main::opt_lib ne "") {
  4113. my $text = ParseTextSectionHeader($main::opt_lib);
  4114. if (defined($text)) {
  4115. my $start = $text->{vma};
  4116. my $finish = AddressAdd($start, $text->{size});
  4117. push(@{$result}, [$main::opt_lib, $start, $finish, $start]);
  4118. }
  4119. }
  4120. # Append special entry for the main program. This covers
  4121. # 0..max_pc_value_seen, so that we assume pc values not found in one
  4122. # of the library ranges will be treated as coming from the main
  4123. # program binary.
  4124. my $min_pc = HexExtend("0");
  4125. my $max_pc = $min_pc; # find the maximal PC value in any sample
  4126. foreach my $pc (keys(%{$pcs})) {
  4127. if (HexExtend($pc) gt $max_pc) { $max_pc = HexExtend($pc); }
  4128. }
  4129. push(@{$result}, [$prog, $min_pc, $max_pc, $zero_offset]);
  4130. return $result;
  4131. }
  4132. # Add two hex addresses of length $address_length.
  4133. # Run pprof --test for unit test if this is changed.
  4134. sub AddressAdd {
  4135. my $addr1 = shift;
  4136. my $addr2 = shift;
  4137. my $sum;
  4138. if ($address_length == 8) {
  4139. # Perl doesn't cope with wraparound arithmetic, so do it explicitly:
  4140. $sum = (hex($addr1)+hex($addr2)) % (0x10000000 * 16);
  4141. return sprintf("%08x", $sum);
  4142. } else {
  4143. # Do the addition in 7-nibble chunks to trivialize carry handling.
  4144. if ($main::opt_debug and $main::opt_test) {
  4145. print STDERR "AddressAdd $addr1 + $addr2 = ";
  4146. }
  4147. my $a1 = substr($addr1,-7);
  4148. $addr1 = substr($addr1,0,-7);
  4149. my $a2 = substr($addr2,-7);
  4150. $addr2 = substr($addr2,0,-7);
  4151. $sum = hex($a1) + hex($a2);
  4152. my $c = 0;
  4153. if ($sum > 0xfffffff) {
  4154. $c = 1;
  4155. $sum -= 0x10000000;
  4156. }
  4157. my $r = sprintf("%07x", $sum);
  4158. $a1 = substr($addr1,-7);
  4159. $addr1 = substr($addr1,0,-7);
  4160. $a2 = substr($addr2,-7);
  4161. $addr2 = substr($addr2,0,-7);
  4162. $sum = hex($a1) + hex($a2) + $c;
  4163. $c = 0;
  4164. if ($sum > 0xfffffff) {
  4165. $c = 1;
  4166. $sum -= 0x10000000;
  4167. }
  4168. $r = sprintf("%07x", $sum) . $r;
  4169. $sum = hex($addr1) + hex($addr2) + $c;
  4170. if ($sum > 0xff) { $sum -= 0x100; }
  4171. $r = sprintf("%02x", $sum) . $r;
  4172. if ($main::opt_debug and $main::opt_test) { print STDERR "$r\n"; }
  4173. return $r;
  4174. }
  4175. }
  4176. # Subtract two hex addresses of length $address_length.
  4177. # Run pprof --test for unit test if this is changed.
  4178. sub AddressSub {
  4179. my $addr1 = shift;
  4180. my $addr2 = shift;
  4181. my $diff;
  4182. if ($address_length == 8) {
  4183. # Perl doesn't cope with wraparound arithmetic, so do it explicitly:
  4184. $diff = (hex($addr1)-hex($addr2)) % (0x10000000 * 16);
  4185. return sprintf("%08x", $diff);
  4186. } else {
  4187. # Do the addition in 7-nibble chunks to trivialize borrow handling.
  4188. # if ($main::opt_debug) { print STDERR "AddressSub $addr1 - $addr2 = "; }
  4189. my $a1 = hex(substr($addr1,-7));
  4190. $addr1 = substr($addr1,0,-7);
  4191. my $a2 = hex(substr($addr2,-7));
  4192. $addr2 = substr($addr2,0,-7);
  4193. my $b = 0;
  4194. if ($a2 > $a1) {
  4195. $b = 1;
  4196. $a1 += 0x10000000;
  4197. }
  4198. $diff = $a1 - $a2;
  4199. my $r = sprintf("%07x", $diff);
  4200. $a1 = hex(substr($addr1,-7));
  4201. $addr1 = substr($addr1,0,-7);
  4202. $a2 = hex(substr($addr2,-7)) + $b;
  4203. $addr2 = substr($addr2,0,-7);
  4204. $b = 0;
  4205. if ($a2 > $a1) {
  4206. $b = 1;
  4207. $a1 += 0x10000000;
  4208. }
  4209. $diff = $a1 - $a2;
  4210. $r = sprintf("%07x", $diff) . $r;
  4211. $a1 = hex($addr1);
  4212. $a2 = hex($addr2) + $b;
  4213. if ($a2 > $a1) { $a1 += 0x100; }
  4214. $diff = $a1 - $a2;
  4215. $r = sprintf("%02x", $diff) . $r;
  4216. # if ($main::opt_debug) { print STDERR "$r\n"; }
  4217. return $r;
  4218. }
  4219. }
  4220. # Increment a hex addresses of length $address_length.
  4221. # Run pprof --test for unit test if this is changed.
  4222. sub AddressInc {
  4223. my $addr = shift;
  4224. my $sum;
  4225. if ($address_length == 8) {
  4226. # Perl doesn't cope with wraparound arithmetic, so do it explicitly:
  4227. $sum = (hex($addr)+1) % (0x10000000 * 16);
  4228. return sprintf("%08x", $sum);
  4229. } else {
  4230. # Do the addition in 7-nibble chunks to trivialize carry handling.
  4231. # We are always doing this to step through the addresses in a function,
  4232. # and will almost never overflow the first chunk, so we check for this
  4233. # case and exit early.
  4234. # if ($main::opt_debug) { print STDERR "AddressInc $addr1 = "; }
  4235. my $a1 = substr($addr,-7);
  4236. $addr = substr($addr,0,-7);
  4237. $sum = hex($a1) + 1;
  4238. my $r = sprintf("%07x", $sum);
  4239. if ($sum <= 0xfffffff) {
  4240. $r = $addr . $r;
  4241. # if ($main::opt_debug) { print STDERR "$r\n"; }
  4242. return HexExtend($r);
  4243. } else {
  4244. $r = "0000000";
  4245. }
  4246. $a1 = substr($addr,-7);
  4247. $addr = substr($addr,0,-7);
  4248. $sum = hex($a1) + 1;
  4249. $r = sprintf("%07x", $sum) . $r;
  4250. if ($sum <= 0xfffffff) {
  4251. $r = $addr . $r;
  4252. # if ($main::opt_debug) { print STDERR "$r\n"; }
  4253. return HexExtend($r);
  4254. } else {
  4255. $r = "00000000000000";
  4256. }
  4257. $sum = hex($addr) + 1;
  4258. if ($sum > 0xff) { $sum -= 0x100; }
  4259. $r = sprintf("%02x", $sum) . $r;
  4260. # if ($main::opt_debug) { print STDERR "$r\n"; }
  4261. return $r;
  4262. }
  4263. }
  4264. # Extract symbols for all PC values found in profile
  4265. sub ExtractSymbols {
  4266. my $libs = shift;
  4267. my $pcset = shift;
  4268. my $symbols = {};
  4269. # Map each PC value to the containing library. To make this faster,
  4270. # we sort libraries by their starting pc value (highest first), and
  4271. # advance through the libraries as we advance the pc. Sometimes the
  4272. # addresses of libraries may overlap with the addresses of the main
  4273. # binary, so to make sure the libraries 'win', we iterate over the
  4274. # libraries in reverse order (which assumes the binary doesn't start
  4275. # in the middle of a library, which seems a fair assumption).
  4276. my @pcs = (sort { $a cmp $b } keys(%{$pcset})); # pcset is 0-extended strings
  4277. foreach my $lib (sort {$b->[1] cmp $a->[1]} @{$libs}) {
  4278. my $libname = $lib->[0];
  4279. my $start = $lib->[1];
  4280. my $finish = $lib->[2];
  4281. my $offset = $lib->[3];
  4282. # Get list of pcs that belong in this library.
  4283. my $contained = [];
  4284. my ($start_pc_index, $finish_pc_index);
  4285. # Find smallest finish_pc_index such that $finish < $pc[$finish_pc_index].
  4286. for ($finish_pc_index = $#pcs + 1; $finish_pc_index > 0;
  4287. $finish_pc_index--) {
  4288. last if $pcs[$finish_pc_index - 1] le $finish;
  4289. }
  4290. # Find smallest start_pc_index such that $start <= $pc[$start_pc_index].
  4291. for ($start_pc_index = $finish_pc_index; $start_pc_index > 0;
  4292. $start_pc_index--) {
  4293. last if $pcs[$start_pc_index - 1] lt $start;
  4294. }
  4295. # This keeps PC values higher than $pc[$finish_pc_index] in @pcs,
  4296. # in case there are overlaps in libraries and the main binary.
  4297. @{$contained} = splice(@pcs, $start_pc_index,
  4298. $finish_pc_index - $start_pc_index);
  4299. # Map to symbols
  4300. MapToSymbols($libname, AddressSub($start, $offset), $contained, $symbols);
  4301. }
  4302. return $symbols;
  4303. }
  4304. # Map list of PC values to symbols for a given image
  4305. sub MapToSymbols {
  4306. my $image = shift;
  4307. my $offset = shift;
  4308. my $pclist = shift;
  4309. my $symbols = shift;
  4310. my $debug = 0;
  4311. # For libc (and other) libraries, the copy in /usr/lib/debug contains debugging symbols
  4312. my $debugging = DebuggingLibrary($image);
  4313. if ($debugging) {
  4314. $image = $debugging;
  4315. }
  4316. # Ignore empty binaries
  4317. if ($#{$pclist} < 0) { return; }
  4318. # Figure out the addr2line command to use
  4319. my $addr2line = $obj_tool_map{"addr2line"};
  4320. my $cmd = ShellEscape($addr2line, "-f", "-C", "-e", $image);
  4321. if (exists $obj_tool_map{"addr2line_pdb"}) {
  4322. $addr2line = $obj_tool_map{"addr2line_pdb"};
  4323. $cmd = ShellEscape($addr2line, "--demangle", "-f", "-C", "-e", $image);
  4324. }
  4325. # If "addr2line" isn't installed on the system at all, just use
  4326. # nm to get what info we can (function names, but not line numbers).
  4327. if (system(ShellEscape($addr2line, "--help") . " >$dev_null 2>&1") != 0) {
  4328. MapSymbolsWithNM($image, $offset, $pclist, $symbols);
  4329. return;
  4330. }
  4331. # "addr2line -i" can produce a variable number of lines per input
  4332. # address, with no separator that allows us to tell when data for
  4333. # the next address starts. So we find the address for a special
  4334. # symbol (_fini) and interleave this address between all real
  4335. # addresses passed to addr2line. The name of this special symbol
  4336. # can then be used as a separator.
  4337. $sep_address = undef; # May be filled in by MapSymbolsWithNM()
  4338. my $nm_symbols = {};
  4339. MapSymbolsWithNM($image, $offset, $pclist, $nm_symbols);
  4340. if (defined($sep_address)) {
  4341. # Only add " -i" to addr2line if the binary supports it.
  4342. # addr2line --help returns 0, but not if it sees an unknown flag first.
  4343. if (system("$cmd -i --help >$dev_null 2>&1") == 0) {
  4344. $cmd .= " -i";
  4345. } else {
  4346. $sep_address = undef; # no need for sep_address if we don't support -i
  4347. }
  4348. }
  4349. # Make file with all PC values with intervening 'sep_address' so
  4350. # that we can reliably detect the end of inlined function list
  4351. open(ADDRESSES, ">$main::tmpfile_sym") || error("$main::tmpfile_sym: $!\n");
  4352. if ($debug) { print("---- $image ---\n"); }
  4353. for (my $i = 0; $i <= $#{$pclist}; $i++) {
  4354. # addr2line always reads hex addresses, and does not need '0x' prefix.
  4355. if ($debug) { printf STDERR ("%s\n", $pclist->[$i]); }
  4356. printf ADDRESSES ("%s\n", AddressSub($pclist->[$i], $offset));
  4357. if (defined($sep_address)) {
  4358. printf ADDRESSES ("%s\n", $sep_address);
  4359. }
  4360. }
  4361. close(ADDRESSES);
  4362. if ($debug) {
  4363. print("----\n");
  4364. system("cat", $main::tmpfile_sym);
  4365. print("---- $cmd ---\n");
  4366. system("$cmd < " . ShellEscape($main::tmpfile_sym));
  4367. print("----\n");
  4368. }
  4369. open(SYMBOLS, "$cmd <" . ShellEscape($main::tmpfile_sym) . " |")
  4370. || error("$cmd: $!\n");
  4371. my $count = 0; # Index in pclist
  4372. while (<SYMBOLS>) {
  4373. # Read fullfunction and filelineinfo from next pair of lines
  4374. s/\r?\n$//g;
  4375. my $fullfunction = $_;
  4376. $_ = <SYMBOLS>;
  4377. s/\r?\n$//g;
  4378. my $filelinenum = $_;
  4379. if (defined($sep_address) && $fullfunction eq $sep_symbol) {
  4380. # Terminating marker for data for this address
  4381. $count++;
  4382. next;
  4383. }
  4384. $filelinenum =~ s|\\|/|g; # turn windows-style paths into unix-style paths
  4385. # Remove discriminator markers as this comes after the line number and
  4386. # confuses the rest of this script.
  4387. $filelinenum =~ s/ \(discriminator \d+\)$//;
  4388. # Convert unknown line numbers into line 0.
  4389. $filelinenum =~ s/:\?$/:0/;
  4390. my $pcstr = $pclist->[$count];
  4391. my $function = ShortFunctionName($fullfunction);
  4392. my $nms = $nm_symbols->{$pcstr};
  4393. if (defined($nms)) {
  4394. if ($fullfunction eq '??') {
  4395. # nm found a symbol for us.
  4396. $function = $nms->[0];
  4397. $fullfunction = $nms->[2];
  4398. } else {
  4399. # MapSymbolsWithNM tags each routine with its starting address,
  4400. # useful in case the image has multiple occurrences of this
  4401. # routine. (It uses a syntax that resembles template paramters,
  4402. # that are automatically stripped out by ShortFunctionName().)
  4403. # addr2line does not provide the same information. So we check
  4404. # if nm disambiguated our symbol, and if so take the annotated
  4405. # (nm) version of the routine-name. TODO(csilvers): this won't
  4406. # catch overloaded, inlined symbols, which nm doesn't see.
  4407. # Better would be to do a check similar to nm's, in this fn.
  4408. if ($nms->[2] =~ m/^\Q$function\E/) { # sanity check it's the right fn
  4409. $function = $nms->[0];
  4410. $fullfunction = $nms->[2];
  4411. }
  4412. }
  4413. }
  4414. # Prepend to accumulated symbols for pcstr
  4415. # (so that caller comes before callee)
  4416. my $sym = $symbols->{$pcstr};
  4417. if (!defined($sym)) {
  4418. $sym = [];
  4419. $symbols->{$pcstr} = $sym;
  4420. }
  4421. unshift(@{$sym}, $function, $filelinenum, $fullfunction);
  4422. if ($debug) { printf STDERR ("%s => [%s]\n", $pcstr, join(" ", @{$sym})); }
  4423. if (!defined($sep_address)) {
  4424. # Inlining is off, so this entry ends immediately
  4425. $count++;
  4426. }
  4427. }
  4428. close(SYMBOLS);
  4429. }
  4430. # Use nm to map the list of referenced PCs to symbols. Return true iff we
  4431. # are able to read procedure information via nm.
  4432. sub MapSymbolsWithNM {
  4433. my $image = shift;
  4434. my $offset = shift;
  4435. my $pclist = shift;
  4436. my $symbols = shift;
  4437. # Get nm output sorted by increasing address
  4438. my $symbol_table = GetProcedureBoundaries($image, ".");
  4439. if (!%{$symbol_table}) {
  4440. return 0;
  4441. }
  4442. # Start addresses are already the right length (8 or 16 hex digits).
  4443. my @names = sort { $symbol_table->{$a}->[0] cmp $symbol_table->{$b}->[0] }
  4444. keys(%{$symbol_table});
  4445. if ($#names < 0) {
  4446. # No symbols: just use addresses
  4447. foreach my $pc (@{$pclist}) {
  4448. my $pcstr = "0x" . $pc;
  4449. $symbols->{$pc} = [$pcstr, "?", $pcstr];
  4450. }
  4451. return 0;
  4452. }
  4453. # Sort addresses so we can do a join against nm output
  4454. my $index = 0;
  4455. my $fullname = $names[0];
  4456. my $name = ShortFunctionName($fullname);
  4457. foreach my $pc (sort { $a cmp $b } @{$pclist}) {
  4458. # Adjust for mapped offset
  4459. my $mpc = AddressSub($pc, $offset);
  4460. while (($index < $#names) && ($mpc ge $symbol_table->{$fullname}->[1])){
  4461. $index++;
  4462. $fullname = $names[$index];
  4463. $name = ShortFunctionName($fullname);
  4464. }
  4465. if ($mpc lt $symbol_table->{$fullname}->[1]) {
  4466. $symbols->{$pc} = [$name, "?", $fullname];
  4467. } else {
  4468. my $pcstr = "0x" . $pc;
  4469. $symbols->{$pc} = [$pcstr, "?", $pcstr];
  4470. }
  4471. }
  4472. return 1;
  4473. }
  4474. sub ShortFunctionName {
  4475. my $function = shift;
  4476. while ($function =~ s/\([^()]*\)(\s*const)?//g) { } # Argument types
  4477. $function =~ s/<[0-9a-f]*>$//g; # Remove Address
  4478. if (!$main::opt_no_strip_temp) {
  4479. while ($function =~ s/<[^<>]*>//g) { } # Remove template arguments
  4480. }
  4481. $function =~ s/^.*\s+(\w+::)/$1/; # Remove leading type
  4482. return $function;
  4483. }
  4484. # Trim overly long symbols found in disassembler output
  4485. sub CleanDisassembly {
  4486. my $d = shift;
  4487. while ($d =~ s/\([^()%]*\)(\s*const)?//g) { } # Argument types, not (%rax)
  4488. while ($d =~ s/(\w+)<[^<>]*>/$1/g) { } # Remove template arguments
  4489. return $d;
  4490. }
  4491. # Clean file name for display
  4492. sub CleanFileName {
  4493. my ($f) = @_;
  4494. $f =~ s|^/proc/self/cwd/||;
  4495. $f =~ s|^\./||;
  4496. return $f;
  4497. }
  4498. # Make address relative to section and clean up for display
  4499. sub UnparseAddress {
  4500. my ($offset, $address) = @_;
  4501. $address = AddressSub($address, $offset);
  4502. $address =~ s/^0x//;
  4503. $address =~ s/^0*//;
  4504. return $address;
  4505. }
  4506. ##### Miscellaneous #####
  4507. # Find the right versions of the above object tools to use. The
  4508. # argument is the program file being analyzed, and should be an ELF
  4509. # 32-bit or ELF 64-bit executable file. The location of the tools
  4510. # is determined by considering the following options in this order:
  4511. # 1) --tools option, if set
  4512. # 2) PPROF_TOOLS environment variable, if set
  4513. # 3) the environment
  4514. sub ConfigureObjTools {
  4515. my $prog_file = shift;
  4516. # Check for the existence of $prog_file because /usr/bin/file does not
  4517. # predictably return error status in prod.
  4518. (-e $prog_file) || error("$prog_file does not exist.\n");
  4519. my $file_type = undef;
  4520. if (-e "/usr/bin/file") {
  4521. # Follow symlinks (at least for systems where "file" supports that).
  4522. my $escaped_prog_file = ShellEscape($prog_file);
  4523. $file_type = `/usr/bin/file -L $escaped_prog_file 2>$dev_null ||
  4524. /usr/bin/file $escaped_prog_file`;
  4525. } elsif ($^O == "MSWin32") {
  4526. $file_type = "MS Windows";
  4527. } else {
  4528. print STDERR "WARNING: Can't determine the file type of $prog_file";
  4529. }
  4530. if ($file_type =~ /64-bit/) {
  4531. # Change $address_length to 16 if the program file is ELF 64-bit.
  4532. # We can't detect this from many (most?) heap or lock contention
  4533. # profiles, since the actual addresses referenced are generally in low
  4534. # memory even for 64-bit programs.
  4535. $address_length = 16;
  4536. }
  4537. if ($file_type =~ /MS Windows/) {
  4538. # For windows, we provide a version of nm and addr2line as part of
  4539. # the opensource release, which is capable of parsing
  4540. # Windows-style PDB executables. It should live in the path, or
  4541. # in the same directory as pprof.
  4542. $obj_tool_map{"nm_pdb"} = "nm-pdb";
  4543. $obj_tool_map{"addr2line_pdb"} = "addr2line-pdb";
  4544. }
  4545. if ($file_type =~ /Mach-O/) {
  4546. # OS X uses otool to examine Mach-O files, rather than objdump.
  4547. $obj_tool_map{"otool"} = "otool";
  4548. $obj_tool_map{"addr2line"} = "false"; # no addr2line
  4549. $obj_tool_map{"objdump"} = "false"; # no objdump
  4550. }
  4551. # Go fill in %obj_tool_map with the pathnames to use:
  4552. foreach my $tool (keys %obj_tool_map) {
  4553. $obj_tool_map{$tool} = ConfigureTool($obj_tool_map{$tool});
  4554. }
  4555. }
  4556. # Returns the path of a caller-specified object tool. If --tools or
  4557. # PPROF_TOOLS are specified, then returns the full path to the tool
  4558. # with that prefix. Otherwise, returns the path unmodified (which
  4559. # means we will look for it on PATH).
  4560. sub ConfigureTool {
  4561. my $tool = shift;
  4562. my $path;
  4563. # --tools (or $PPROF_TOOLS) is a comma separated list, where each
  4564. # item is either a) a pathname prefix, or b) a map of the form
  4565. # <tool>:<path>. First we look for an entry of type (b) for our
  4566. # tool. If one is found, we use it. Otherwise, we consider all the
  4567. # pathname prefixes in turn, until one yields an existing file. If
  4568. # none does, we use a default path.
  4569. my $tools = $main::opt_tools || $ENV{"PPROF_TOOLS"} || "";
  4570. if ($tools =~ m/(,|^)\Q$tool\E:([^,]*)/) {
  4571. $path = $2;
  4572. # TODO(csilvers): sanity-check that $path exists? Hard if it's relative.
  4573. } elsif ($tools ne '') {
  4574. foreach my $prefix (split(',', $tools)) {
  4575. next if ($prefix =~ /:/); # ignore "tool:fullpath" entries in the list
  4576. if (-x $prefix . $tool) {
  4577. $path = $prefix . $tool;
  4578. last;
  4579. }
  4580. }
  4581. if (!$path) {
  4582. error("No '$tool' found with prefix specified by " .
  4583. "--tools (or \$PPROF_TOOLS) '$tools'\n");
  4584. }
  4585. } else {
  4586. # ... otherwise use the version that exists in the same directory as
  4587. # pprof. If there's nothing there, use $PATH.
  4588. $0 =~ m,[^/]*$,; # this is everything after the last slash
  4589. my $dirname = $`; # this is everything up to and including the last slash
  4590. if (-x "$dirname$tool") {
  4591. $path = "$dirname$tool";
  4592. } else {
  4593. $path = $tool;
  4594. }
  4595. }
  4596. if ($main::opt_debug) { print STDERR "Using '$path' for '$tool'.\n"; }
  4597. return $path;
  4598. }
  4599. sub ShellEscape {
  4600. my @escaped_words = ();
  4601. foreach my $word (@_) {
  4602. my $escaped_word = $word;
  4603. if ($word =~ m![^a-zA-Z0-9/.,_=-]!) { # check for anything not in whitelist
  4604. $escaped_word =~ s/'/'\\''/;
  4605. $escaped_word = "'$escaped_word'";
  4606. }
  4607. push(@escaped_words, $escaped_word);
  4608. }
  4609. return join(" ", @escaped_words);
  4610. }
  4611. sub cleanup {
  4612. unlink($main::tmpfile_sym);
  4613. unlink(keys %main::tempnames);
  4614. # We leave any collected profiles in $HOME/pprof in case the user wants
  4615. # to look at them later. We print a message informing them of this.
  4616. if ((scalar(@main::profile_files) > 0) &&
  4617. defined($main::collected_profile)) {
  4618. if (scalar(@main::profile_files) == 1) {
  4619. print STDERR "Dynamically gathered profile is in $main::collected_profile\n";
  4620. }
  4621. print STDERR "If you want to investigate this profile further, you can do:\n";
  4622. print STDERR "\n";
  4623. print STDERR " $0 \\\n";
  4624. print STDERR " $main::prog \\\n";
  4625. print STDERR " $main::collected_profile\n";
  4626. print STDERR "\n";
  4627. }
  4628. }
  4629. sub sighandler {
  4630. cleanup();
  4631. exit(1);
  4632. }
  4633. sub error {
  4634. my $msg = shift;
  4635. print STDERR $msg;
  4636. cleanup();
  4637. exit(1);
  4638. }
  4639. # Run $nm_command and get all the resulting procedure boundaries whose
  4640. # names match "$regexp" and returns them in a hashtable mapping from
  4641. # procedure name to a two-element vector of [start address, end address]
  4642. sub GetProcedureBoundariesViaNm {
  4643. my $escaped_nm_command = shift; # shell-escaped
  4644. my $regexp = shift;
  4645. my $image = shift;
  4646. my $symbol_table = {};
  4647. open(NM, "$escaped_nm_command |") || error("$escaped_nm_command: $!\n");
  4648. my $last_start = "0";
  4649. my $routine = "";
  4650. while (<NM>) {
  4651. s/\r//g; # turn windows-looking lines into unix-looking lines
  4652. if (m/^\s*([0-9a-f]+) (.) (..*)/) {
  4653. my $start_val = $1;
  4654. my $type = $2;
  4655. my $this_routine = $3;
  4656. # It's possible for two symbols to share the same address, if
  4657. # one is a zero-length variable (like __start_google_malloc) or
  4658. # one symbol is a weak alias to another (like __libc_malloc).
  4659. # In such cases, we want to ignore all values except for the
  4660. # actual symbol, which in nm-speak has type "T". The logic
  4661. # below does this, though it's a bit tricky: what happens when
  4662. # we have a series of lines with the same address, is the first
  4663. # one gets queued up to be processed. However, it won't
  4664. # *actually* be processed until later, when we read a line with
  4665. # a different address. That means that as long as we're reading
  4666. # lines with the same address, we have a chance to replace that
  4667. # item in the queue, which we do whenever we see a 'T' entry --
  4668. # that is, a line with type 'T'. If we never see a 'T' entry,
  4669. # we'll just go ahead and process the first entry (which never
  4670. # got touched in the queue), and ignore the others.
  4671. if ($start_val eq $last_start && $type =~ /t/i) {
  4672. # We are the 'T' symbol at this address, replace previous symbol.
  4673. $routine = $this_routine;
  4674. next;
  4675. } elsif ($start_val eq $last_start) {
  4676. # We're not the 'T' symbol at this address, so ignore us.
  4677. next;
  4678. }
  4679. if ($this_routine eq $sep_symbol) {
  4680. $sep_address = HexExtend($start_val);
  4681. }
  4682. # Tag this routine with the starting address in case the image
  4683. # has multiple occurrences of this routine. We use a syntax
  4684. # that resembles template paramters that are automatically
  4685. # stripped out by ShortFunctionName()
  4686. $this_routine .= "<$start_val>";
  4687. if (defined($routine) && $routine =~ m/$regexp/) {
  4688. $symbol_table->{$routine} = [HexExtend($last_start),
  4689. HexExtend($start_val)];
  4690. }
  4691. $last_start = $start_val;
  4692. $routine = $this_routine;
  4693. } elsif (m/^Loaded image name: (.+)/) {
  4694. # The win32 nm workalike emits information about the binary it is using.
  4695. if ($main::opt_debug) { print STDERR "Using Image $1\n"; }
  4696. } elsif (m/^PDB file name: (.+)/) {
  4697. # The win32 nm workalike emits information about the pdb it is using.
  4698. if ($main::opt_debug) { print STDERR "Using PDB $1\n"; }
  4699. }
  4700. }
  4701. close(NM);
  4702. # Handle the last line in the nm output. Unfortunately, we don't know
  4703. # how big this last symbol is, because we don't know how big the file
  4704. # is. For now, we just give it a size of 0.
  4705. # TODO(csilvers): do better here.
  4706. if (defined($routine) && $routine =~ m/$regexp/) {
  4707. $symbol_table->{$routine} = [HexExtend($last_start),
  4708. HexExtend($last_start)];
  4709. }
  4710. # Verify if addr2line can find the $sep_symbol. If not, we use objdump
  4711. # to find the address for the $sep_symbol on code section which addr2line
  4712. # can find.
  4713. if (defined($sep_address)){
  4714. my $start_val = $sep_address;
  4715. my $addr2line = $obj_tool_map{"addr2line"};
  4716. my $cmd = ShellEscape($addr2line, "-f", "-C", "-e", $image, "-i");
  4717. open(FINI, "echo $start_val | $cmd |")
  4718. || error("echo $start_val | $cmd: $!\n");
  4719. $_ = <FINI>;
  4720. s/\r?\n$//g;
  4721. my $fini = $_;
  4722. close(FINI);
  4723. if ($fini ne $sep_symbol){
  4724. my $objdump = $obj_tool_map{"objdump"};
  4725. $cmd = ShellEscape($objdump, "-d", $image);
  4726. my $grep = ShellEscape("grep", $sep_symbol);
  4727. my $tail = ShellEscape("tail", "-n", "1");
  4728. open(FINI, "$cmd | $grep | $tail |")
  4729. || error("$cmd | $grep | $tail: $!\n");
  4730. s/\r//g; # turn windows-looking lines into unix-looking lines
  4731. my $data = <FINI>;
  4732. if (defined($data)){
  4733. ($start_val, $fini) = split(/ </,$data);
  4734. }
  4735. close(FINI);
  4736. }
  4737. $sep_address = HexExtend($start_val);
  4738. }
  4739. return $symbol_table;
  4740. }
  4741. # Gets the procedure boundaries for all routines in "$image" whose names
  4742. # match "$regexp" and returns them in a hashtable mapping from procedure
  4743. # name to a two-element vector of [start address, end address].
  4744. # Will return an empty map if nm is not installed or not working properly.
  4745. sub GetProcedureBoundaries {
  4746. my $image = shift;
  4747. my $regexp = shift;
  4748. # If $image doesn't start with /, then put ./ in front of it. This works
  4749. # around an obnoxious bug in our probing of nm -f behavior.
  4750. # "nm -f $image" is supposed to fail on GNU nm, but if:
  4751. #
  4752. # a. $image starts with [BbSsPp] (for example, bin/foo/bar), AND
  4753. # b. you have a.out in your current directory (a not uncommon occurrence)
  4754. #
  4755. # then "nm -f $image" succeeds because -f only looks at the first letter of
  4756. # the argument, which looks valid because it's [BbSsPp], and then since
  4757. # there's no image provided, it looks for a.out and finds it.
  4758. #
  4759. # This regex makes sure that $image starts with . or /, forcing the -f
  4760. # parsing to fail since . and / are not valid formats.
  4761. $image =~ s#^[^/]#./$&#;
  4762. # For libc libraries, the copy in /usr/lib/debug contains debugging symbols
  4763. my $debugging = DebuggingLibrary($image);
  4764. if ($debugging) {
  4765. $image = $debugging;
  4766. }
  4767. my $nm = $obj_tool_map{"nm"};
  4768. my $cppfilt = $obj_tool_map{"c++filt"};
  4769. # nm can fail for two reasons: 1) $image isn't a debug library; 2) nm
  4770. # binary doesn't support --demangle. In addition, for OS X we need
  4771. # to use the -f flag to get 'flat' nm output (otherwise we don't sort
  4772. # properly and get incorrect results). Unfortunately, GNU nm uses -f
  4773. # in an incompatible way. So first we test whether our nm supports
  4774. # --demangle and -f.
  4775. my $demangle_flag = "";
  4776. my $cppfilt_flag = "";
  4777. my $to_devnull = ">$dev_null 2>&1";
  4778. if (system(ShellEscape($nm, "--demangle", $image) . $to_devnull) == 0) {
  4779. # In this mode, we do "nm --demangle <foo>"
  4780. $demangle_flag = "--demangle";
  4781. $cppfilt_flag = "";
  4782. } elsif (system(ShellEscape($cppfilt, $image) . $to_devnull) == 0) {
  4783. # In this mode, we do "nm <foo> | c++filt"
  4784. $cppfilt_flag = " | " . ShellEscape($cppfilt);
  4785. };
  4786. my $flatten_flag = "";
  4787. if (system(ShellEscape($nm, "-f", $image) . $to_devnull) == 0) {
  4788. $flatten_flag = "-f";
  4789. }
  4790. # Finally, in the case $imagie isn't a debug library, we try again with
  4791. # -D to at least get *exported* symbols. If we can't use --demangle,
  4792. # we use c++filt instead, if it exists on this system.
  4793. my @nm_commands = (ShellEscape($nm, "-n", $flatten_flag, $demangle_flag,
  4794. $image) . " 2>$dev_null $cppfilt_flag",
  4795. ShellEscape($nm, "-D", "-n", $flatten_flag, $demangle_flag,
  4796. $image) . " 2>$dev_null $cppfilt_flag",
  4797. # 6nm is for Go binaries
  4798. ShellEscape("6nm", "$image") . " 2>$dev_null | sort",
  4799. );
  4800. # If the executable is an MS Windows PDB-format executable, we'll
  4801. # have set up obj_tool_map("nm_pdb"). In this case, we actually
  4802. # want to use both unix nm and windows-specific nm_pdb, since
  4803. # PDB-format executables can apparently include dwarf .o files.
  4804. if (exists $obj_tool_map{"nm_pdb"}) {
  4805. push(@nm_commands,
  4806. ShellEscape($obj_tool_map{"nm_pdb"}, "--demangle", $image)
  4807. . " 2>$dev_null");
  4808. }
  4809. foreach my $nm_command (@nm_commands) {
  4810. my $symbol_table = GetProcedureBoundariesViaNm($nm_command, $regexp, $image);
  4811. return $symbol_table if (%{$symbol_table});
  4812. }
  4813. my $symbol_table = {};
  4814. return $symbol_table;
  4815. }
  4816. # The test vectors for AddressAdd/Sub/Inc are 8-16-nibble hex strings.
  4817. # To make them more readable, we add underscores at interesting places.
  4818. # This routine removes the underscores, producing the canonical representation
  4819. # used by pprof to represent addresses, particularly in the tested routines.
  4820. sub CanonicalHex {
  4821. my $arg = shift;
  4822. return join '', (split '_',$arg);
  4823. }
  4824. # Unit test for AddressAdd:
  4825. sub AddressAddUnitTest {
  4826. my $test_data_8 = shift;
  4827. my $test_data_16 = shift;
  4828. my $error_count = 0;
  4829. my $fail_count = 0;
  4830. my $pass_count = 0;
  4831. # print STDERR "AddressAddUnitTest: ", 1+$#{$test_data_8}, " tests\n";
  4832. # First a few 8-nibble addresses. Note that this implementation uses
  4833. # plain old arithmetic, so a quick sanity check along with verifying what
  4834. # happens to overflow (we want it to wrap):
  4835. $address_length = 8;
  4836. foreach my $row (@{$test_data_8}) {
  4837. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4838. my $sum = AddressAdd ($row->[0], $row->[1]);
  4839. if ($sum ne $row->[2]) {
  4840. printf STDERR "ERROR: %s != %s + %s = %s\n", $sum,
  4841. $row->[0], $row->[1], $row->[2];
  4842. ++$fail_count;
  4843. } else {
  4844. ++$pass_count;
  4845. }
  4846. }
  4847. printf STDERR "AddressAdd 32-bit tests: %d passes, %d failures\n",
  4848. $pass_count, $fail_count;
  4849. $error_count = $fail_count;
  4850. $fail_count = 0;
  4851. $pass_count = 0;
  4852. # Now 16-nibble addresses.
  4853. $address_length = 16;
  4854. foreach my $row (@{$test_data_16}) {
  4855. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4856. my $sum = AddressAdd (CanonicalHex($row->[0]), CanonicalHex($row->[1]));
  4857. my $expected = join '', (split '_',$row->[2]);
  4858. if ($sum ne CanonicalHex($row->[2])) {
  4859. printf STDERR "ERROR: %s != %s + %s = %s\n", $sum,
  4860. $row->[0], $row->[1], $row->[2];
  4861. ++$fail_count;
  4862. } else {
  4863. ++$pass_count;
  4864. }
  4865. }
  4866. printf STDERR "AddressAdd 64-bit tests: %d passes, %d failures\n",
  4867. $pass_count, $fail_count;
  4868. $error_count += $fail_count;
  4869. return $error_count;
  4870. }
  4871. # Unit test for AddressSub:
  4872. sub AddressSubUnitTest {
  4873. my $test_data_8 = shift;
  4874. my $test_data_16 = shift;
  4875. my $error_count = 0;
  4876. my $fail_count = 0;
  4877. my $pass_count = 0;
  4878. # print STDERR "AddressSubUnitTest: ", 1+$#{$test_data_8}, " tests\n";
  4879. # First a few 8-nibble addresses. Note that this implementation uses
  4880. # plain old arithmetic, so a quick sanity check along with verifying what
  4881. # happens to overflow (we want it to wrap):
  4882. $address_length = 8;
  4883. foreach my $row (@{$test_data_8}) {
  4884. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4885. my $sum = AddressSub ($row->[0], $row->[1]);
  4886. if ($sum ne $row->[3]) {
  4887. printf STDERR "ERROR: %s != %s - %s = %s\n", $sum,
  4888. $row->[0], $row->[1], $row->[3];
  4889. ++$fail_count;
  4890. } else {
  4891. ++$pass_count;
  4892. }
  4893. }
  4894. printf STDERR "AddressSub 32-bit tests: %d passes, %d failures\n",
  4895. $pass_count, $fail_count;
  4896. $error_count = $fail_count;
  4897. $fail_count = 0;
  4898. $pass_count = 0;
  4899. # Now 16-nibble addresses.
  4900. $address_length = 16;
  4901. foreach my $row (@{$test_data_16}) {
  4902. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4903. my $sum = AddressSub (CanonicalHex($row->[0]), CanonicalHex($row->[1]));
  4904. if ($sum ne CanonicalHex($row->[3])) {
  4905. printf STDERR "ERROR: %s != %s - %s = %s\n", $sum,
  4906. $row->[0], $row->[1], $row->[3];
  4907. ++$fail_count;
  4908. } else {
  4909. ++$pass_count;
  4910. }
  4911. }
  4912. printf STDERR "AddressSub 64-bit tests: %d passes, %d failures\n",
  4913. $pass_count, $fail_count;
  4914. $error_count += $fail_count;
  4915. return $error_count;
  4916. }
  4917. # Unit test for AddressInc:
  4918. sub AddressIncUnitTest {
  4919. my $test_data_8 = shift;
  4920. my $test_data_16 = shift;
  4921. my $error_count = 0;
  4922. my $fail_count = 0;
  4923. my $pass_count = 0;
  4924. # print STDERR "AddressIncUnitTest: ", 1+$#{$test_data_8}, " tests\n";
  4925. # First a few 8-nibble addresses. Note that this implementation uses
  4926. # plain old arithmetic, so a quick sanity check along with verifying what
  4927. # happens to overflow (we want it to wrap):
  4928. $address_length = 8;
  4929. foreach my $row (@{$test_data_8}) {
  4930. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4931. my $sum = AddressInc ($row->[0]);
  4932. if ($sum ne $row->[4]) {
  4933. printf STDERR "ERROR: %s != %s + 1 = %s\n", $sum,
  4934. $row->[0], $row->[4];
  4935. ++$fail_count;
  4936. } else {
  4937. ++$pass_count;
  4938. }
  4939. }
  4940. printf STDERR "AddressInc 32-bit tests: %d passes, %d failures\n",
  4941. $pass_count, $fail_count;
  4942. $error_count = $fail_count;
  4943. $fail_count = 0;
  4944. $pass_count = 0;
  4945. # Now 16-nibble addresses.
  4946. $address_length = 16;
  4947. foreach my $row (@{$test_data_16}) {
  4948. if ($main::opt_debug and $main::opt_test) { print STDERR "@{$row}\n"; }
  4949. my $sum = AddressInc (CanonicalHex($row->[0]));
  4950. if ($sum ne CanonicalHex($row->[4])) {
  4951. printf STDERR "ERROR: %s != %s + 1 = %s\n", $sum,
  4952. $row->[0], $row->[4];
  4953. ++$fail_count;
  4954. } else {
  4955. ++$pass_count;
  4956. }
  4957. }
  4958. printf STDERR "AddressInc 64-bit tests: %d passes, %d failures\n",
  4959. $pass_count, $fail_count;
  4960. $error_count += $fail_count;
  4961. return $error_count;
  4962. }
  4963. # Driver for unit tests.
  4964. # Currently just the address add/subtract/increment routines for 64-bit.
  4965. sub RunUnitTests {
  4966. my $error_count = 0;
  4967. # This is a list of tuples [a, b, a+b, a-b, a+1]
  4968. my $unit_test_data_8 = [
  4969. [qw(aaaaaaaa 50505050 fafafafa 5a5a5a5a aaaaaaab)],
  4970. [qw(50505050 aaaaaaaa fafafafa a5a5a5a6 50505051)],
  4971. [qw(ffffffff aaaaaaaa aaaaaaa9 55555555 00000000)],
  4972. [qw(00000001 ffffffff 00000000 00000002 00000002)],
  4973. [qw(00000001 fffffff0 fffffff1 00000011 00000002)],
  4974. ];
  4975. my $unit_test_data_16 = [
  4976. # The implementation handles data in 7-nibble chunks, so those are the
  4977. # interesting boundaries.
  4978. [qw(aaaaaaaa 50505050
  4979. 00_000000f_afafafa 00_0000005_a5a5a5a 00_000000a_aaaaaab)],
  4980. [qw(50505050 aaaaaaaa
  4981. 00_000000f_afafafa ff_ffffffa_5a5a5a6 00_0000005_0505051)],
  4982. [qw(ffffffff aaaaaaaa
  4983. 00_000001a_aaaaaa9 00_0000005_5555555 00_0000010_0000000)],
  4984. [qw(00000001 ffffffff
  4985. 00_0000010_0000000 ff_ffffff0_0000002 00_0000000_0000002)],
  4986. [qw(00000001 fffffff0
  4987. 00_000000f_ffffff1 ff_ffffff0_0000011 00_0000000_0000002)],
  4988. [qw(00_a00000a_aaaaaaa 50505050
  4989. 00_a00000f_afafafa 00_a000005_a5a5a5a 00_a00000a_aaaaaab)],
  4990. [qw(0f_fff0005_0505050 aaaaaaaa
  4991. 0f_fff000f_afafafa 0f_ffefffa_5a5a5a6 0f_fff0005_0505051)],
  4992. [qw(00_000000f_fffffff 01_800000a_aaaaaaa
  4993. 01_800001a_aaaaaa9 fe_8000005_5555555 00_0000010_0000000)],
  4994. [qw(00_0000000_0000001 ff_fffffff_fffffff
  4995. 00_0000000_0000000 00_0000000_0000002 00_0000000_0000002)],
  4996. [qw(00_0000000_0000001 ff_fffffff_ffffff0
  4997. ff_fffffff_ffffff1 00_0000000_0000011 00_0000000_0000002)],
  4998. ];
  4999. $error_count += AddressAddUnitTest($unit_test_data_8, $unit_test_data_16);
  5000. $error_count += AddressSubUnitTest($unit_test_data_8, $unit_test_data_16);
  5001. $error_count += AddressIncUnitTest($unit_test_data_8, $unit_test_data_16);
  5002. if ($error_count > 0) {
  5003. print STDERR $error_count, " errors: FAILED\n";
  5004. } else {
  5005. print STDERR "PASS\n";
  5006. }
  5007. exit ($error_count);
  5008. }