2
0

jeprof.in 175 KB

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