DbgModule.cpp 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488
  1. #pragma warning(push)
  2. #pragma warning(disable:4146)
  3. #pragma warning(disable:4996)
  4. #pragma warning(disable:4800)
  5. #pragma warning(disable:4244)
  6. #include "DbgModule.h"
  7. #include "DWARFInfo.h"
  8. #include <windows.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <inttypes.h>
  13. #include <assert.h>
  14. #include <vector>
  15. #include "WinDebugger.h"
  16. #include "DebugManager.h"
  17. #include "DebugTarget.h"
  18. #include "COFFData.h"
  19. #include "Compiler/BfDemangler.h"
  20. #include "BeefySysLib/util/Hash.h"
  21. #include "BeefySysLib/util/BeefPerf.h"
  22. #include "DbgSymSrv.h"
  23. #include "MiniDumpDebugger.h"
  24. #pragma warning(pop)
  25. #pragma warning(disable:4996)
  26. #include "BeefySysLib/util/AllocDebug.h"
  27. USING_NS_BF_DBG;
  28. void SetBreakpoint(int64_t address);
  29. NS_BF_DBG_BEGIN
  30. #ifdef BF_DBG_32
  31. typedef PEOptionalHeader32 PEOptionalHeader;
  32. typedef PE_NTHeaders32 PE_NTHeaders;
  33. #else
  34. typedef PEOptionalHeader64 PEOptionalHeader;
  35. typedef PE_NTHeaders64 PE_NTHeaders;
  36. #endif
  37. #define GET(T) *((T*)(data += sizeof(T)) - 1)
  38. #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
  39. //////////////////////////////////////////////////////////////////////////
  40. DbgCompileUnit::DbgCompileUnit(DbgModule* dbgModule)
  41. {
  42. mDbgModule = dbgModule;
  43. mLanguage = DbgLanguage_Unknown;
  44. mGlobalBlock = mDbgModule->mAlloc.Alloc<DbgBlock>();
  45. mGlobalType = mDbgModule->mAlloc.Alloc<DbgType>();
  46. mGlobalType->mTypeCode = DbgType_Root;
  47. mGlobalType->mPriority = DbgTypePriority_Primary_Explicit;
  48. mGlobalType->mCompileUnit = this;
  49. mLowPC = (addr_target)-1;
  50. mHighPC = 0;
  51. //mDoPrimaryRemapping = true;
  52. mNeedsLineDataFixup = true;
  53. mWasHotReplaced = false;
  54. mIsMaster = false;
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. addr_target DbgLineDataEx::GetAddress()
  58. {
  59. return mSubprogram->GetLineAddr(*mLineData);
  60. }
  61. DbgSrcFile* DbgLineDataEx::GetSrcFile()
  62. {
  63. auto inlineRoot = mSubprogram->GetRootInlineParent();
  64. return inlineRoot->mLineInfo->mContexts[mLineData->mCtxIdx].mSrcFile;
  65. }
  66. addr_target DbgSubprogram::GetLineAddr(const DbgLineData& lineData)
  67. {
  68. return (addr_target)(lineData.mRelAddress + mCompileUnit->mDbgModule->mImageBase);
  69. }
  70. DbgSubprogram* DbgSubprogram::GetLineInlinee(const DbgLineData& lineData)
  71. {
  72. auto inlineRoot = GetRootInlineParent();
  73. return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mInlinee;
  74. }
  75. DbgSrcFile* DbgSubprogram::GetLineSrcFile(const DbgLineData& lineData)
  76. {
  77. auto inlineRoot = GetRootInlineParent();
  78. return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mSrcFile;
  79. }
  80. bool DbgSubprogram::HasValidLines()
  81. {
  82. auto inlineRoot = GetRootInlineParent();
  83. for (int lineIdx = 0; lineIdx < (int)inlineRoot->mLineInfo->mLines.size(); lineIdx++)
  84. {
  85. auto& lineInfo = inlineRoot->mLineInfo->mLines[lineIdx];
  86. if (lineInfo.mColumn >= 0)
  87. return true;
  88. }
  89. return false;
  90. }
  91. void DbgSubprogram::PopulateSubprogram()
  92. {
  93. if (mDeferredInternalsSize == 0)
  94. return;
  95. mCompileUnit->mDbgModule->PopulateSubprogram(this);
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. DbgLineDataBuilder::DbgLineDataBuilder(DbgModule* dbgModule)
  99. {
  100. mDbgModule = dbgModule;
  101. mCurSubprogram = NULL;
  102. mCurRecord = NULL;
  103. }
  104. DbgLineData* DbgLineDataBuilder::Add(DbgCompileUnit* compileUnit, DbgLineData& lineData, DbgSrcFile* srcFile, DbgSubprogram* inlinee)
  105. {
  106. addr_target address = (addr_target)(lineData.mRelAddress + mDbgModule->mImageBase);
  107. if ((compileUnit->mLowPC != (addr_target)-1) && ((address < (addr_target)compileUnit->mLowPC) || (address >= (addr_target)compileUnit->mHighPC)))
  108. return NULL;
  109. if ((mCurSubprogram == NULL) || (address < mCurSubprogram->mBlock.mLowPC) || (address >= mCurSubprogram->mBlock.mHighPC))
  110. {
  111. DbgSubprogramMapEntry* mapEntry = mDbgModule->mDebugTarget->mSubprogramMap.Get(address, DBG_MAX_LOOKBACK);
  112. if (mapEntry != NULL)
  113. {
  114. mCurSubprogram = mapEntry->mEntry;
  115. if (address > mCurSubprogram->mBlock.mHighPC)
  116. mCurSubprogram = NULL;
  117. if (mCurSubprogram != NULL)
  118. {
  119. SubprogramRecord** recordPtr = NULL;
  120. if (mRecords.TryAdd(mCurSubprogram, NULL, &recordPtr))
  121. {
  122. // It's not too expensive to over-reserve here, because these are just temporary structures that get copied
  123. // exactly sized when we Commit
  124. mCurRecord = mAlloc.Alloc<SubprogramRecord>();
  125. *recordPtr = mCurRecord;
  126. mCurRecord->mContexts.mAlloc = &mAlloc;
  127. mCurRecord->mContexts.Reserve(16);
  128. mCurRecord->mLines.mAlloc = &mAlloc;
  129. mCurRecord->mLines.Reserve(128);
  130. mCurRecord->mCurContext = -1;
  131. mCurRecord->mHasInlinees = false;
  132. }
  133. else
  134. mCurRecord = *recordPtr;
  135. }
  136. else
  137. mCurRecord = NULL;
  138. }
  139. }
  140. if (mCurSubprogram == NULL)
  141. return NULL;
  142. bool needsNewCtx = false;
  143. if (mCurRecord->mCurContext == -1)
  144. {
  145. needsNewCtx = true;
  146. }
  147. else
  148. {
  149. auto& curContext = mCurRecord->mContexts[mCurRecord->mCurContext];
  150. if ((curContext.mInlinee != inlinee) || (curContext.mSrcFile != srcFile))
  151. {
  152. needsNewCtx = true;
  153. for (int ctxIdx = 0; ctxIdx < (int)mCurRecord->mContexts.size(); ctxIdx++)
  154. {
  155. auto& ctx = mCurRecord->mContexts[ctxIdx];
  156. if ((ctx.mInlinee == inlinee) && (ctx.mSrcFile == srcFile))
  157. {
  158. needsNewCtx = false;
  159. mCurRecord->mCurContext = ctxIdx;
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. if (needsNewCtx)
  166. {
  167. DbgLineInfoCtx ctx;
  168. ctx.mInlinee = inlinee;
  169. ctx.mSrcFile = srcFile;
  170. if (inlinee != NULL)
  171. mCurRecord->mHasInlinees = true;
  172. mCurRecord->mContexts.Add(ctx);
  173. mCurRecord->mCurContext = (int)mCurRecord->mContexts.size() - 1;
  174. }
  175. lineData.mCtxIdx = mCurRecord->mCurContext;
  176. if ((mCurSubprogram->mPrologueSize > 0) && (mCurRecord->mLines.size() == 1) && (inlinee == NULL))
  177. {
  178. auto& firstLine = mCurRecord->mLines[0];
  179. auto dbgStartAddr = firstLine.mRelAddress + mCurSubprogram->mPrologueSize;
  180. if (lineData.mRelAddress != dbgStartAddr)
  181. {
  182. DbgLineData dbgStartLine = firstLine;
  183. dbgStartLine.mRelAddress = dbgStartAddr;
  184. mCurRecord->mLines.Add(dbgStartLine);
  185. }
  186. firstLine.mColumn = -2; // Marker for 'in prologue'
  187. }
  188. if (inlinee != NULL)
  189. {
  190. if (inlinee->mInlineeInfo->mFirstLineData.mRelAddress == 0)
  191. inlinee->mInlineeInfo->mFirstLineData = lineData;
  192. inlinee->mInlineeInfo->mLastLineData = lineData;
  193. }
  194. mCurRecord->mLines.Add(lineData);
  195. return &mCurRecord->mLines.back();
  196. }
  197. void DbgLineDataBuilder::Commit()
  198. {
  199. HashSet<DbgSrcFile*> usedSrcFiles;
  200. for (auto& recordKV : mRecords)
  201. {
  202. auto dbgSubprogram = recordKV.mKey;
  203. auto record = recordKV.mValue;
  204. usedSrcFiles.Clear();
  205. for (auto& ctx : record->mContexts)
  206. {
  207. if (usedSrcFiles.Add(ctx.mSrcFile))
  208. {
  209. ctx.mSrcFile->mLineDataRefs.Add(dbgSubprogram);
  210. }
  211. }
  212. for (int lineIdx = 0; lineIdx < (int)record->mLines.size() - 1; lineIdx++)
  213. {
  214. auto& lineData = record->mLines[lineIdx];
  215. auto& nextLineData = record->mLines[lineIdx + 1];
  216. if ((lineData.mContribSize == 0) && (lineData.mCtxIdx == nextLineData.mCtxIdx))
  217. {
  218. lineData.mContribSize = (uint32)(nextLineData.mRelAddress - lineData.mRelAddress);
  219. }
  220. bool sameInliner = lineData.mCtxIdx == nextLineData.mCtxIdx;
  221. if (!sameInliner)
  222. {
  223. auto ctx = record->mContexts[lineData.mCtxIdx];
  224. auto nextCtx = record->mContexts[lineData.mCtxIdx];
  225. sameInliner = ctx.mInlinee == nextCtx.mInlinee;
  226. }
  227. if ((sameInliner) && (lineData.mRelAddress + lineData.mContribSize < nextLineData.mRelAddress))
  228. {
  229. auto ctx = record->mContexts[lineData.mCtxIdx];
  230. if (ctx.mInlinee != NULL)
  231. ctx.mInlinee->mHasLineAddrGaps = true;
  232. }
  233. }
  234. DbgLineData* lastLine = NULL;
  235. for (int lineIdx = 0; lineIdx < (int)record->mLines.size(); lineIdx++)
  236. {
  237. auto& lineData = record->mLines[lineIdx];
  238. if (lineData.mContribSize == 0)
  239. {
  240. auto ctx = record->mContexts[lineData.mCtxIdx];
  241. if (ctx.mInlinee == NULL)
  242. lastLine = &lineData;
  243. }
  244. }
  245. if (lastLine != NULL)
  246. lastLine->mContribSize = (uint32)(dbgSubprogram->mBlock.mHighPC - (mDbgModule->mImageBase + lastLine->mRelAddress));
  247. BF_ASSERT(dbgSubprogram->mLineInfo == NULL);
  248. dbgSubprogram->mLineInfo = mDbgModule->mAlloc.Alloc<DbgLineInfo>();
  249. dbgSubprogram->mLineInfo->mLines.CopyFrom(&record->mLines[0], (int)record->mLines.size(), mDbgModule->mAlloc);
  250. BfSizedArray<DbgLineInfoCtx> contexts;
  251. contexts.CopyFrom(&record->mContexts[0], (int)record->mContexts.size(), mDbgModule->mAlloc);
  252. dbgSubprogram->mLineInfo->mContexts = contexts.mVals;
  253. dbgSubprogram->mLineInfo->mHasInlinees = record->mHasInlinees;
  254. }
  255. }
  256. //////////////////////////////////////////////////////////////////////////
  257. static const char* DataGetString(const uint8*& data)
  258. {
  259. const char* prevVal = (const char*)data;
  260. while (*data != 0)
  261. data++;
  262. data++;
  263. return prevVal;
  264. }
  265. struct AbstractOriginEntry
  266. {
  267. public:
  268. int mClassType;
  269. DbgDebugData* mDestination;
  270. DbgDebugData* mAbstractOrigin;
  271. private:
  272. AbstractOriginEntry()
  273. {
  274. }
  275. public:
  276. static AbstractOriginEntry Create(int classType, DbgDebugData* destination, DbgDebugData* abstractOrigin)
  277. {
  278. AbstractOriginEntry abstractOriginEntry;
  279. abstractOriginEntry.mClassType = classType;
  280. abstractOriginEntry.mDestination = destination;
  281. abstractOriginEntry.mAbstractOrigin = abstractOrigin;
  282. return abstractOriginEntry;
  283. }
  284. void Replace()
  285. {
  286. if (mClassType == DbgSubprogram::ClassType)
  287. {
  288. DbgSubprogram* destSubprogram = (DbgSubprogram*)mDestination;
  289. DbgSubprogram* originSubprogram = (DbgSubprogram*)mAbstractOrigin;
  290. if (destSubprogram->mName == NULL)
  291. {
  292. destSubprogram->mName = originSubprogram->mName;
  293. destSubprogram->mParentType = originSubprogram->mParentType;
  294. }
  295. destSubprogram->mHasThis = originSubprogram->mHasThis;
  296. if (destSubprogram->mFrameBaseData == NULL)
  297. {
  298. destSubprogram->mFrameBaseData = originSubprogram->mFrameBaseData;
  299. destSubprogram->mFrameBaseLen = originSubprogram->mFrameBaseLen;
  300. }
  301. destSubprogram->mReturnType = originSubprogram->mReturnType;
  302. auto originItr = originSubprogram->mParams.begin();
  303. for (auto destParam : destSubprogram->mParams)
  304. {
  305. DbgVariable* originParam = *originItr;
  306. if (originParam != NULL)
  307. {
  308. if (destParam->mName == NULL)
  309. destParam->mName = originParam->mName;
  310. if (destParam->mType == NULL)
  311. destParam->mType = originParam->mType;
  312. }
  313. ++originItr;
  314. }
  315. //BF_ASSERT(originItr == originSubprogram->mParams.end());
  316. }
  317. else if (mClassType == DbgVariable::ClassType)
  318. {
  319. DbgVariable* destVariable = (DbgVariable*)mDestination;
  320. DbgVariable* originVariable = (DbgVariable*)mAbstractOrigin;
  321. if (destVariable->mName == NULL)
  322. destVariable->mName = originVariable->mName;
  323. if (destVariable->mType == NULL)
  324. destVariable->mType = originVariable->mType;
  325. }
  326. else
  327. {
  328. BF_FATAL("Unhandled");
  329. }
  330. }
  331. };
  332. NS_BF_DBG_END
  333. //////////////////////////////////////////////////////////////////////////
  334. void DbgSubprogram::ToString(StringImpl& str, bool internalName)
  335. {
  336. if ((mInlineeInfo != NULL) && (mInlineeInfo->mInlineeId != 0))
  337. mCompileUnit->mDbgModule->FixupInlinee(this);
  338. PopulateSubprogram();
  339. if (mCheckedKind == BfCheckedKind_Checked)
  340. str += "[Checked] ";
  341. else if (mCheckedKind == BfCheckedKind_Unchecked)
  342. str += "[Unchecked] ";
  343. auto language = GetLanguage();
  344. if (mName == NULL)
  345. {
  346. if (mLinkName[0] == '<')
  347. {
  348. str += mLinkName;
  349. return;
  350. }
  351. str = BfDemangler::Demangle(StringImpl::MakeRef(mLinkName), language);
  352. // Strip off the params since we need to generate those ourselves
  353. int parenPos = (int)str.IndexOf('(');
  354. if (parenPos != -1)
  355. str = str.Substring(0, parenPos);
  356. }
  357. else if ((mHasQualifiedName) && (!internalName))
  358. {
  359. const char* cPtr = mName;
  360. if (strncmp(cPtr, "_bf::", 5) == 0)
  361. {
  362. cPtr += 5;
  363. for ( ; true; cPtr++)
  364. {
  365. char c = *cPtr;
  366. if (c == 0)
  367. break;
  368. if ((c == '_') && (cPtr[-1] == ':'))
  369. {
  370. if (strcmp(cPtr, "__BfCtor") == 0)
  371. {
  372. str += "this";
  373. break;
  374. }
  375. if (strcmp(cPtr, "__BfStaticCtor") == 0)
  376. {
  377. str += "this$static";
  378. break;
  379. }
  380. if (strcmp(cPtr, "__BfCtorClear") == 0)
  381. {
  382. str += "this$clear";
  383. break;
  384. }
  385. }
  386. if ((c == ':') && (cPtr[1] == ':'))
  387. {
  388. str.Append('.');
  389. cPtr++;
  390. }
  391. else
  392. str.Append(c);
  393. }
  394. }
  395. else
  396. str += mName;
  397. }
  398. else
  399. {
  400. if (mParentType != NULL)
  401. {
  402. mParentType->ToString(str, language, true, internalName);
  403. if (!str.empty())
  404. {
  405. if (language == DbgLanguage_Beef)
  406. str += ".";
  407. else
  408. str += "::";
  409. }
  410. }
  411. const char* name = mName;
  412. if (mHasQualifiedName)
  413. {
  414. const char* cPtr = name;
  415. for (; true; cPtr++)
  416. {
  417. char c = *cPtr;
  418. if (c == 0)
  419. break;
  420. if ((c == ':') && (cPtr[1] == ':'))
  421. {
  422. name = cPtr + 2;
  423. }
  424. }
  425. }
  426. if ((language == DbgLanguage_Beef) && (mParentType != NULL) && (mParentType->mTypeName != NULL) && (strcmp(name, mParentType->mTypeName) == 0))
  427. str += "this";
  428. else if ((language == DbgLanguage_Beef) && (name[0] == '~'))
  429. str += "~this";
  430. else if (strncmp(name, "_bf::", 5) == 0)
  431. str += name + 5;
  432. else
  433. {
  434. bool handled = false;
  435. if ((language == DbgLanguage_Beef) && (name[0] == '_'))
  436. {
  437. if (strcmp(name, "__BfCtor") == 0)
  438. {
  439. str += "this";
  440. handled = true;
  441. }
  442. else if (strcmp(name, "__BfStaticCtor") == 0)
  443. {
  444. str += "this";
  445. handled = true;
  446. }
  447. else if (strcmp(name, "__BfCtorClear") == 0)
  448. {
  449. str += "this$clear";
  450. handled = true;
  451. }
  452. }
  453. if (!handled)
  454. str += name;
  455. }
  456. }
  457. //if (mTemplateName != NULL)
  458. //str += mTemplateName;
  459. if (str.empty())
  460. str += "`anon";
  461. if ((str[str.length() - 1] == '!') || (str[0] == '<'))
  462. {
  463. if (language == DbgLanguage_Beef)
  464. {
  465. // It's a mixin - assert that there's no params
  466. //BF_ASSERT(mParams.Size() == 0);
  467. }
  468. //return str;
  469. }
  470. str += "(";
  471. bool showedParam = false;
  472. int i = 0;
  473. for (auto variable : mParams)
  474. {
  475. if ((variable->mName != NULL) && (strcmp(variable->mName, "this") == 0))
  476. continue;
  477. if (showedParam)
  478. str += ", ";
  479. if (variable->mType != NULL)
  480. {
  481. auto varType = variable->mType;
  482. if (varType->mTypeCode == DbgType_Const)
  483. varType = varType->mTypeParam;
  484. if (variable->mSigNoPointer)
  485. {
  486. BF_ASSERT(varType->IsPointer());
  487. varType = varType->mTypeParam;
  488. }
  489. varType->ToString(str, language, false, internalName);
  490. if (variable->mName != NULL)
  491. str += " ";
  492. }
  493. if (variable->mName != NULL)
  494. str += variable->mName;
  495. showedParam = true;
  496. i++;
  497. }
  498. str += ")";
  499. }
  500. String DbgSubprogram::ToString()
  501. {
  502. String str;
  503. ToString(str, false);
  504. return str;
  505. }
  506. // For inlined subprograms, the "root" inliner means the bottom-most non-inlined function. This subprogram contains
  507. // all the line data for it's own non-inlined instructions, PLUS line data for all inlined functions that it calls.
  508. // The inlined functions has empty mLineInfo structures.
  509. //
  510. // When we pass a non-NULL value into inlinedSubprogram, we are requesting to ONLY return lines that were emitted from
  511. // that subprogram (inlined or not).
  512. //
  513. // If we call FindClosestLine on an inlined subprogram, we only want results of functions that are inside or inlined by
  514. // the 'this' subprogram. Thus, we do a "get any line" call on the root inliner and then filter the results based
  515. // on whether they are relevant.
  516. DbgLineData* DbgSubprogram::FindClosestLine(addr_target addr, DbgSubprogram** inlinedSubprogram, DbgSrcFile** srcFile, int* outLineIdx)
  517. {
  518. if (mLineInfo == NULL)
  519. {
  520. if (mInlineeInfo == NULL)
  521. return NULL;
  522. if ((inlinedSubprogram != NULL) && (*inlinedSubprogram != NULL))
  523. {
  524. // Keep explicit inlinee requirement
  525. return mInlineeInfo->mRootInliner->FindClosestLine(addr, inlinedSubprogram, srcFile, outLineIdx);
  526. }
  527. else
  528. {
  529. DbgSubprogram* rootInlinedSubprogram = NULL;
  530. auto result = mInlineeInfo->mRootInliner->FindClosestLine(addr, &rootInlinedSubprogram, srcFile, outLineIdx);
  531. if (result == NULL)
  532. return NULL;
  533. if (rootInlinedSubprogram == NULL) // Do not allow root parent, as we cannot be a parent to the root parent (duh)
  534. return NULL;
  535. // We need to check to see if we are a parent of the found line
  536. auto checkSubprogram = rootInlinedSubprogram;
  537. while ((checkSubprogram != NULL) && (checkSubprogram->mInlineeInfo != NULL))
  538. {
  539. if (checkSubprogram == this)
  540. {
  541. if (inlinedSubprogram != NULL)
  542. *inlinedSubprogram = rootInlinedSubprogram;
  543. return result;
  544. }
  545. checkSubprogram = checkSubprogram->mInlineeInfo->mInlineParent;
  546. }
  547. return NULL;
  548. }
  549. }
  550. // Binary search - lineData is sorted
  551. int first = 0;
  552. int last = (int)mLineInfo->mLines.mSize - 1;
  553. int middle = (first + last) / 2;
  554. int useIdx = -1;
  555. while (first <= last)
  556. {
  557. addr_target midAddr = (addr_target)(mLineInfo->mLines.mVals[middle].mRelAddress + mCompileUnit->mDbgModule->mImageBase);
  558. if (midAddr < addr)
  559. first = middle + 1;
  560. else if (midAddr == addr)
  561. {
  562. useIdx = middle;
  563. break;
  564. }
  565. else
  566. last = middle - 1;
  567. middle = (first + last) / 2;
  568. }
  569. if (useIdx == -1)
  570. useIdx = last;
  571. if (last == -1)
  572. return NULL;
  573. // If we have lines with the same addr, take the more inner one
  574. while (true)
  575. {
  576. auto lineData = &mLineInfo->mLines.mVals[useIdx];
  577. if (useIdx + 1 < mLineInfo->mLines.mSize)
  578. {
  579. auto peekNext = &mLineInfo->mLines.mVals[useIdx + 1];
  580. if (lineData->mRelAddress != peekNext->mRelAddress)
  581. break;
  582. useIdx++;
  583. }
  584. else
  585. {
  586. break;
  587. }
  588. }
  589. while (true)
  590. {
  591. auto lineData = &mLineInfo->mLines.mVals[useIdx];
  592. if (addr < lineData->mRelAddress + lineData->mContribSize + mCompileUnit->mDbgModule->mImageBase)
  593. {
  594. auto& ctx = mLineInfo->mContexts[lineData->mCtxIdx];
  595. if (srcFile != NULL)
  596. *srcFile = ctx.mSrcFile;
  597. if (inlinedSubprogram != NULL)
  598. {
  599. auto subprogram = (ctx.mInlinee != NULL) ? ctx.mInlinee : this;
  600. if (*inlinedSubprogram != NULL)
  601. {
  602. // Strictness check
  603. if (subprogram == *inlinedSubprogram)
  604. {
  605. if (outLineIdx != NULL)
  606. *outLineIdx = useIdx;
  607. return lineData;
  608. }
  609. }
  610. else
  611. {
  612. *inlinedSubprogram = subprogram;
  613. if (outLineIdx != NULL)
  614. *outLineIdx = useIdx;
  615. return lineData;
  616. }
  617. }
  618. else
  619. {
  620. if (outLineIdx != NULL)
  621. *outLineIdx = useIdx;
  622. return lineData;
  623. }
  624. }
  625. // Hope we can find an earlier entry whose "contribution" is still valid
  626. if (--useIdx < 0)
  627. break;
  628. }
  629. return NULL;
  630. }
  631. DbgType* DbgSubprogram::GetParent()
  632. {
  633. if ((mParentType == NULL) && (mCompileUnit != NULL))
  634. mCompileUnit->mDbgModule->MapCompileUnitMethods(mCompileUnit);
  635. return mParentType;
  636. }
  637. DbgType* DbgSubprogram::GetTargetType()
  638. {
  639. if (!mHasThis)
  640. return mParentType;
  641. auto thisType = mParams.mHead->mType;
  642. if (thisType == NULL)
  643. return mParentType;
  644. if (thisType->IsPointer())
  645. return thisType->mTypeParam;
  646. return thisType;
  647. }
  648. DbgLanguage DbgSubprogram::GetLanguage()
  649. {
  650. if (mParentType != NULL)
  651. return mParentType->GetLanguage();
  652. if (mCompileUnit->mLanguage != DbgLanguage_Unknown)
  653. return mCompileUnit->mLanguage;
  654. return DbgLanguage_C; // Parent type would have been set for Beef, so it must be C
  655. }
  656. bool DbgSubprogram::Equals(DbgSubprogram* checkMethod, bool allowThisMismatch)
  657. {
  658. if ((mLinkName != NULL) && (checkMethod->mLinkName != NULL))
  659. {
  660. return strcmp(mLinkName, checkMethod->mLinkName) == 0;
  661. }
  662. if (strcmp(mName, checkMethod->mName) != 0)
  663. return false;
  664. if (mHasThis != checkMethod->mHasThis)
  665. return false;
  666. int paramIdx = 0;
  667. auto param = mParams.mHead;
  668. auto checkParam = checkMethod->mParams.mHead;
  669. while ((param != NULL) && (checkParam != NULL))
  670. {
  671. if ((paramIdx == 0) && (allowThisMismatch))
  672. {
  673. // Allow
  674. }
  675. else if ((param->mType != checkParam->mType) && (!param->mType->Equals(checkParam->mType)))
  676. return false;
  677. param = param->mNext;
  678. checkParam = checkParam->mNext;
  679. paramIdx++;
  680. }
  681. if ((param != NULL) || (checkParam != NULL))
  682. return false;
  683. if (!mReturnType->Equals(checkMethod->mReturnType))
  684. return false;
  685. return true;
  686. }
  687. int DbgSubprogram::GetParamCount()
  688. {
  689. int paramCount = mParams.Size();
  690. if (mHasThis)
  691. paramCount--;
  692. return paramCount;
  693. }
  694. String DbgSubprogram::GetParamName(int paramIdx)
  695. {
  696. auto param = mParams[paramIdx];
  697. if (param->mName != NULL)
  698. {
  699. String name = "'";
  700. name += param->mName;
  701. name += "'";
  702. return name;
  703. }
  704. return StrFormat("%d", paramIdx + 1);
  705. }
  706. bool DbgSubprogram::IsGenericMethod()
  707. {
  708. if (mName == NULL)
  709. return false;
  710. for (const char* cPtr = mName; true; cPtr++)
  711. {
  712. char c = *cPtr;
  713. if (c == '\0')
  714. break;
  715. if (c == '<')
  716. return true;
  717. }
  718. return false;
  719. }
  720. bool DbgSubprogram::ThisIsSplat()
  721. {
  722. if (mBlock.mVariables.mHead == NULL)
  723. return false;
  724. return strncmp(mBlock.mVariables.mHead->mName, "$this$", 6) == 0;
  725. }
  726. bool DbgSubprogram::IsLambda()
  727. {
  728. if (mName == NULL)
  729. return false;
  730. return StringView(mName).Contains('$');
  731. }
  732. //////////////////////////////////////////////////////////////////////////
  733. DbgSubprogram::~DbgSubprogram()
  734. {
  735. BfLogDbg("DbgSubprogram::~DbgSubprogram %p\n", this);
  736. }
  737. ////////////////////
  738. bool DbgSrcFile::IsBeef()
  739. {
  740. int dotPos = (int)mFilePath.LastIndexOf('.');
  741. if (dotPos == -1)
  742. return false;
  743. const char* ext = mFilePath.c_str() + dotPos;
  744. // The ".cs" is legacy. Remove that eventually.
  745. return (stricmp(ext, ".bf") == 0) || (stricmp(ext, ".cs") == 0);
  746. }
  747. DbgSrcFile::~DbgSrcFile()
  748. {
  749. for (auto replacedLineInfo : mHotReplacedDbgLineInfo)
  750. delete replacedLineInfo;
  751. }
  752. void DbgSrcFile::RemoveDeferredRefs(DbgModule* debugModule)
  753. {
  754. for (int deferredIdx = 0; deferredIdx < (int)mDeferredRefs.size(); )
  755. {
  756. if (mDeferredRefs[deferredIdx].mDbgModule == debugModule)
  757. {
  758. // Fast remove
  759. mDeferredRefs[deferredIdx] = mDeferredRefs.back();
  760. mDeferredRefs.pop_back();
  761. }
  762. else
  763. deferredIdx++;
  764. }
  765. }
  766. void DbgSrcFile::RemoveLines(DbgModule* debugModule)
  767. {
  768. if (!mHasLineDataFromMultipleModules)
  769. {
  770. // Fast-out case
  771. mLineDataRefs.Clear();
  772. mFirstLineDataDbgModule = NULL;
  773. return;
  774. }
  775. for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
  776. {
  777. auto dbgSubprogram = mLineDataRefs[idx];
  778. if (dbgSubprogram->mCompileUnit->mDbgModule == debugModule)
  779. {
  780. mLineDataRefs.RemoveAtFast(idx);
  781. idx--;
  782. }
  783. }
  784. }
  785. void DbgSrcFile::RemoveLines(DbgModule* debugModule, DbgSubprogram* dbgSubprogram, bool isHotReplaced)
  786. {
  787. debugModule->mDebugTarget->mPendingSrcFileRehup.Add(this);
  788. if (isHotReplaced)
  789. {
  790. int vecIdx = dbgSubprogram->mCompileUnit->mDbgModule->mHotIdx;
  791. BF_ASSERT(vecIdx >= 0);
  792. while (vecIdx >= (int)mHotReplacedDbgLineInfo.size())
  793. mHotReplacedDbgLineInfo.push_back(new HotReplacedLineInfo());
  794. auto hotReplacedLineInfo = mHotReplacedDbgLineInfo[vecIdx];
  795. HotReplacedLineInfo::Entry entry;
  796. entry.mSubprogram = dbgSubprogram;
  797. entry.mLineInfo = dbgSubprogram->mLineInfo;
  798. hotReplacedLineInfo->mEntries.Add(entry);
  799. }
  800. }
  801. void DbgSrcFile::RehupLineData()
  802. {
  803. for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
  804. {
  805. auto dbgSubprogram = mLineDataRefs[idx];
  806. if (dbgSubprogram->mHotReplaceKind != DbgSubprogram::HotReplaceKind_None)
  807. {
  808. mLineDataRefs.RemoveAtFast(idx);
  809. idx--;
  810. }
  811. }
  812. }
  813. const String& DbgSrcFile::GetLocalPath()
  814. {
  815. return (!mLocalPath.IsEmpty()) ? mLocalPath : mFilePath;
  816. }
  817. void DbgSrcFile::GetHash(String& outStr)
  818. {
  819. if (mHashKind == DbgHashKind_MD5)
  820. {
  821. for (int i = 0; i < 16; i++)
  822. {
  823. outStr += StrFormat("%02X", mHash[i]);
  824. }
  825. }
  826. else if (mHashKind == DbgHashKind_SHA256)
  827. {
  828. for (int i = 0; i < 32; i++)
  829. {
  830. outStr += StrFormat("%02X", mHash[i]);
  831. }
  832. }
  833. }
  834. //////////////////////////////////////////////////////////////////////////
  835. DbgType::DbgType()
  836. {
  837. mTypeIdx = -1;
  838. mIsDeclaration = false;
  839. mParent = NULL;
  840. mTypeName = NULL;
  841. mTypeCode = DbgType_Null;
  842. mSize = 0;
  843. mPtrType = NULL;
  844. mTypeParam = NULL;
  845. mBlockParam = NULL;
  846. mNext = NULL;
  847. mPriority = DbgTypePriority_Normal;
  848. }
  849. DbgType::~DbgType()
  850. {
  851. BfLogDbg("DbgType::~DWType %p\n", this);
  852. }
  853. DbgType* DbgType::ResolveTypeDef()
  854. {
  855. if (mTypeCode == DbgType_TypeDef)
  856. return mTypeParam->ResolveTypeDef();
  857. return this;
  858. }
  859. bool DbgType::Equals(DbgType* dbgType)
  860. {
  861. if (dbgType == NULL)
  862. return false;
  863. if (mTypeCode != dbgType->mTypeCode)
  864. {
  865. if ((mTypeCode == DbgType_Enum) || (dbgType->mTypeCode == DbgType_Enum))
  866. {
  867. // These may change mTypeCode, so redo the check afterward
  868. GetPrimaryType();
  869. dbgType->GetPrimaryType();
  870. }
  871. if (mTypeCode != dbgType->mTypeCode)
  872. return false;
  873. }
  874. if ((mName == NULL) != (dbgType->mName == NULL))
  875. return false;
  876. if (mName != NULL)
  877. {
  878. if (dbgType->mFixedName)
  879. FixName();
  880. else if (mFixedName)
  881. dbgType->FixName();
  882. if (strcmp(mName, dbgType->mName) != 0)
  883. return false;
  884. }
  885. if ((mTypeParam != NULL) && (!mTypeParam->Equals(dbgType->mTypeParam)))
  886. return false;
  887. // Did mName already include the parent name?
  888. if (mCompileUnit->mDbgModule->mDbgFlavor == DbgFlavor_MS)
  889. return true;
  890. if ((mParent != NULL) != (dbgType->mParent != NULL))
  891. return false;
  892. if (mParent != NULL)
  893. return mParent->Equals(dbgType->mParent);
  894. return true;
  895. }
  896. bool DbgType::IsStruct()
  897. {
  898. return mTypeCode == DbgType_Struct;
  899. }
  900. bool DbgType::IsPrimitiveType()
  901. {
  902. return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_Bool);
  903. }
  904. bool DbgType::IsNull()
  905. {
  906. return mTypeCode == DbgType_Null;
  907. }
  908. bool DbgType::IsVoid()
  909. {
  910. return (mTypeCode == DbgType_Void);
  911. }
  912. bool DbgType::IsValuelessType()
  913. {
  914. return ((mTypeCode == DbgType_Struct) && (GetByteCount() == 0)) || (mTypeCode == DbgType_Void);
  915. }
  916. bool DbgType::IsValueType()
  917. {
  918. return (mTypeCode <= DbgType_DefinitionEnd);
  919. }
  920. bool DbgType::IsTypedPrimitive()
  921. {
  922. PopulateType();
  923. if (mTypeCode != DbgType_Struct)
  924. return false;
  925. if (mTypeParam != NULL)
  926. return true;
  927. auto baseType = GetBaseType();
  928. if (baseType == NULL)
  929. return false;
  930. if (!baseType->IsTypedPrimitive())
  931. return false;
  932. mTypeParam = baseType->mTypeParam;
  933. return true;
  934. }
  935. bool DbgType::IsBoolean()
  936. {
  937. return mTypeCode == DbgType_Bool;
  938. }
  939. bool DbgType::IsInteger()
  940. {
  941. return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64);
  942. }
  943. bool DbgType::IsIntegral()
  944. {
  945. return ((mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64)) ||
  946. ((mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32));
  947. }
  948. bool DbgType::IsChar()
  949. {
  950. return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32);
  951. }
  952. bool DbgType::IsChar(DbgLanguage language)
  953. {
  954. if (language == DbgLanguage_Beef)
  955. return (mTypeCode >= DbgType_UChar) && (mTypeCode <= DbgType_UChar32);
  956. return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_SChar32);
  957. }
  958. bool DbgType::IsFloat()
  959. {
  960. return (mTypeCode == DbgType_Single) || (mTypeCode == DbgType_Double);
  961. }
  962. // "Struct" in this sense means that we do NOT have a pointer to this value, but it may or may not be a Beef Struct
  963. bool DbgType::IsCompositeType()
  964. {
  965. if (((mTypeCode == DbgType_TypeDef) || (mTypeCode == DbgType_Const)) && (mTypeParam != NULL))
  966. return mTypeParam->IsCompositeType();
  967. return ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_SizedArray));
  968. }
  969. bool DbgType::WantsRefThis()
  970. {
  971. return (GetLanguage() == DbgLanguage_Beef) && (!IsBfObject());
  972. }
  973. bool DbgType::IsBfObjectPtr()
  974. {
  975. if ((mTypeCode == DbgType_Ptr) && (mTypeParam != NULL))
  976. return mTypeParam->IsBfObject();
  977. return false;
  978. }
  979. DbgExtType DbgType::CalcExtType()
  980. {
  981. auto language = GetLanguage();
  982. if ((!mFixedName) && (language == DbgLanguage_Beef))
  983. {
  984. FixName();
  985. }
  986. auto primaryType = GetPrimaryType();
  987. if (this != primaryType)
  988. {
  989. return primaryType->CalcExtType();
  990. }
  991. if (mCompileUnit == NULL)
  992. return DbgExtType_Normal;
  993. if (language != DbgLanguage_Beef)
  994. return DbgExtType_Normal;
  995. if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
  996. return DbgExtType_Normal;
  997. PopulateType();
  998. if (mExtType != DbgExtType_Unknown)
  999. return mExtType;
  1000. auto baseType = GetBaseType();
  1001. if (baseType == NULL)
  1002. {
  1003. if (mParent == NULL)
  1004. return DbgExtType_Normal;
  1005. if (mParent->mTypeCode != DbgType_Namespace)
  1006. return DbgExtType_Normal;
  1007. if (mParent->mParent != NULL)
  1008. return DbgExtType_Normal;
  1009. if (strcmp(mParent->mTypeName, "System") != 0)
  1010. return DbgExtType_Normal;
  1011. if (strcmp(mTypeName, "Object") != 0)
  1012. return DbgExtType_Normal;
  1013. return DbgExtType_BfObject;
  1014. }
  1015. else
  1016. {
  1017. if (strcmp(baseType->mTypeName, "Enum") == 0)
  1018. {
  1019. for (auto member : mMemberList)
  1020. {
  1021. if (strcmp(member->mName, "__bftag") == 0)
  1022. return DbgExtType_BfPayloadEnum;
  1023. }
  1024. return DbgExtType_Normal;
  1025. }
  1026. else if (strcmp(baseType->mTypeName, "ValueType") == 0)
  1027. {
  1028. for (auto member : mMemberList)
  1029. {
  1030. if (strcmp(member->mName, "$bfunion") == 0)
  1031. return DbgExtType_BfUnion;
  1032. }
  1033. }
  1034. }
  1035. auto baseExtType = baseType->CalcExtType();
  1036. if ((baseExtType == DbgExtType_BfObject) && (GetByteCount() == 0))
  1037. baseExtType = DbgExtType_Interface;
  1038. return baseExtType;
  1039. }
  1040. DbgLanguage DbgType::GetLanguage()
  1041. {
  1042. return mLanguage;
  1043. }
  1044. void DbgType::FixName()
  1045. {
  1046. if (mFixedName)
  1047. return;
  1048. int depthCount = 0;
  1049. auto dbgModule = mCompileUnit->mDbgModule;
  1050. if ((dbgModule->mDbgFlavor == DbgFlavor_MS) && (mName != NULL) && (strlen(mName) > 0))
  1051. {
  1052. bool modified = false;
  1053. if (!dbgModule->DbgIsStrMutable(mName))
  1054. mName = dbgModule->DbgDupString(mName);
  1055. const char* typeNamePtr = mTypeName;
  1056. char* nameP = (char*)mName;
  1057. // Fix the name
  1058. char* inPtr = nameP;
  1059. char* outPtr = nameP;
  1060. while (true)
  1061. {
  1062. char c = *(inPtr++);
  1063. if ((c == '<') || (c == '('))
  1064. depthCount++;
  1065. else if ((c == '>') || (c == ')'))
  1066. depthCount--;
  1067. if ((c == ':') && (inPtr[0] == ':'))
  1068. {
  1069. if (mLanguage == DbgLanguage_Beef)
  1070. {
  1071. modified = true;
  1072. inPtr++;
  1073. *(outPtr++) = '.';
  1074. if (depthCount == 0)
  1075. typeNamePtr = outPtr;
  1076. }
  1077. else if (depthCount == 0)
  1078. mTypeName = inPtr + 1;
  1079. }
  1080. else if (modified)
  1081. *(outPtr++) = c;
  1082. else
  1083. outPtr++;
  1084. if (c == 0)
  1085. break;
  1086. }
  1087. if ((modified) && (mName != mTypeName) && (typeNamePtr != NULL))
  1088. {
  1089. mTypeName = typeNamePtr;
  1090. }
  1091. }
  1092. mFixedName = true;
  1093. }
  1094. bool DbgType::IsBfObject()
  1095. {
  1096. if (mExtType == DbgExtType_Unknown)
  1097. mExtType = CalcExtType();
  1098. return (mExtType == DbgExtType_BfObject) || (mExtType == DbgExtType_Interface);
  1099. }
  1100. bool DbgType::IsBfPayloadEnum()
  1101. {
  1102. if (mExtType == DbgExtType_Unknown)
  1103. mExtType = CalcExtType();
  1104. return mExtType == DbgExtType_BfPayloadEnum;
  1105. }
  1106. bool DbgType::IsBfUnion()
  1107. {
  1108. if (mExtType == DbgExtType_Unknown)
  1109. mExtType = CalcExtType();
  1110. return mExtType == DbgExtType_BfUnion;
  1111. }
  1112. bool DbgType::IsBfEnum()
  1113. {
  1114. if (mTypeCode != DbgType_Struct)
  1115. return false;
  1116. auto baseType = GetBaseType();
  1117. if (baseType == NULL)
  1118. {
  1119. if (mParent == NULL)
  1120. return false;
  1121. if (mParent->mTypeCode != DbgType_Namespace)
  1122. return false;
  1123. if (mParent->mParent != NULL)
  1124. return false;
  1125. if (strcmp(mParent->mTypeName, "System") != 0)
  1126. return false;
  1127. return strcmp(mTypeName, "Enum") == 0;
  1128. }
  1129. return baseType->IsBfEnum();
  1130. }
  1131. bool DbgType::IsBfTuple()
  1132. {
  1133. if (mTypeCode != DbgType_Struct)
  1134. return false;
  1135. if (GetLanguage() != DbgLanguage_Beef)
  1136. return false;
  1137. if (mName == NULL)
  1138. return false;
  1139. return mName[0] == '(';
  1140. }
  1141. bool DbgType::HasCPPVTable()
  1142. {
  1143. if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
  1144. return false;
  1145. /*if (!mMemberList.IsEmpty())
  1146. {
  1147. //TODO: We commented this out at some point- why did we do that?
  1148. if ((mMemberList.mHead->mName != NULL) && (strncmp(mMemberList.mHead->mName, "_vptr$", 6) == 0))
  1149. return true;
  1150. }*/
  1151. if (mHasVTable)
  1152. return true;
  1153. if (GetLanguage() == DbgLanguage_Beef)
  1154. return false;
  1155. for (auto checkBaseType : mBaseTypes)
  1156. {
  1157. if (checkBaseType->mBaseType->HasCPPVTable())
  1158. return true;
  1159. }
  1160. return false;
  1161. }
  1162. bool DbgType::IsBaseBfObject()
  1163. {
  1164. auto baseType = GetBaseType();
  1165. return (baseType == NULL) && (IsBfObject());
  1166. }
  1167. bool DbgType::IsInterface()
  1168. {
  1169. if (mExtType == DbgExtType_Unknown)
  1170. mExtType = CalcExtType();
  1171. return mExtType == DbgExtType_Interface;
  1172. }
  1173. bool DbgType::IsNamespace()
  1174. {
  1175. return mTypeCode == DbgType_Namespace;
  1176. }
  1177. bool DbgType::IsEnum()
  1178. {
  1179. return (mTypeCode == DbgType_Enum);
  1180. }
  1181. bool DbgType::IsRoot()
  1182. {
  1183. return (mTypeCode == DbgType_Root);
  1184. }
  1185. bool DbgType::IsRef()
  1186. {
  1187. return
  1188. (mTypeCode == DbgType_Ref) ||
  1189. (mTypeCode == DbgType_RValueReference);
  1190. }
  1191. bool DbgType::IsSigned()
  1192. {
  1193. return
  1194. (mTypeCode == DbgType_i8) ||
  1195. (mTypeCode == DbgType_i16) ||
  1196. (mTypeCode == DbgType_i32) ||
  1197. (mTypeCode == DbgType_i64);
  1198. }
  1199. bool DbgType::IsConst()
  1200. {
  1201. if ((mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_Ref))
  1202. {
  1203. if (mTypeParam != NULL)
  1204. return mTypeParam->IsConst();
  1205. }
  1206. return mTypeCode == DbgType_Const;
  1207. }
  1208. bool DbgType::IsPointer(bool includeBfObjectPointer)
  1209. {
  1210. if (mTypeCode != DbgType_Ptr)
  1211. return false;
  1212. if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
  1213. return false;
  1214. return true;
  1215. }
  1216. bool DbgType::HasPointer(bool includeBfObjectPointer)
  1217. {
  1218. if (((mTypeCode == DbgType_Const) || (mTypeCode == DbgType_Ref)) && (mTypeParam != NULL))
  1219. return mTypeParam->IsPointer(includeBfObjectPointer);
  1220. return IsPointer(includeBfObjectPointer);
  1221. }
  1222. bool DbgType::IsPointerOrRef(bool includeBfObjectPointer)
  1223. {
  1224. if ((mTypeCode != DbgType_Ptr) && (mTypeCode != DbgType_Ref) && (mTypeCode != DbgType_RValueReference))
  1225. return false;
  1226. if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
  1227. return false;
  1228. return true;
  1229. }
  1230. bool DbgType::IsSizedArray()
  1231. {
  1232. return (mTypeCode == DbgType_SizedArray);
  1233. }
  1234. bool DbgType::IsAnonymous()
  1235. {
  1236. return (mTypeName == NULL) || (mTypeName[0] == '<');
  1237. }
  1238. bool DbgType::IsGlobalsContainer()
  1239. {
  1240. return (mTypeName != NULL) && (mTypeName[0] == 'G') && (mTypeName[1] == '$');
  1241. }
  1242. DbgType* DbgType::GetUnderlyingType()
  1243. {
  1244. return mTypeParam;
  1245. }
  1246. void DbgType::PopulateType()
  1247. {
  1248. if (mIsIncomplete)
  1249. {
  1250. mCompileUnit->mDbgModule->PopulateType(this);
  1251. mIsIncomplete = false;
  1252. }
  1253. }
  1254. DbgModule* DbgType::GetDbgModule()
  1255. {
  1256. if (mCompileUnit == NULL)
  1257. return NULL;
  1258. return mCompileUnit->mDbgModule;
  1259. }
  1260. DbgType* DbgType::GetPrimaryType()
  1261. {
  1262. if (mPrimaryType != NULL)
  1263. return mPrimaryType;
  1264. mPrimaryType = this;
  1265. if (mPriority <= DbgTypePriority_Normal)
  1266. {
  1267. if ((mCompileUnit != NULL) &&
  1268. ((mCompileUnit->mLanguage == DbgLanguage_Beef)|| (mLanguage == DbgLanguage_Beef) ||
  1269. (mTypeCode == DbgType_Namespace) || (mIsDeclaration)))
  1270. {
  1271. mPrimaryType = mCompileUnit->mDbgModule->GetPrimaryType(this);
  1272. mPrimaryType->PopulateType();
  1273. mTypeCode = mPrimaryType->mTypeCode;
  1274. mTypeParam = mPrimaryType->mTypeParam;
  1275. }
  1276. }
  1277. return mPrimaryType;
  1278. }
  1279. DbgType* DbgType::GetBaseType()
  1280. {
  1281. auto primaryType = GetPrimaryType();
  1282. if (primaryType != this)
  1283. return primaryType->GetBaseType();
  1284. PopulateType();
  1285. if (mBaseTypes.mHead == NULL)
  1286. return NULL;
  1287. if (GetLanguage() != DbgLanguage_Beef)
  1288. return NULL;
  1289. auto baseType = mBaseTypes.mHead->mBaseType;
  1290. BF_ASSERT(!baseType->IsInterface());
  1291. if ((baseType == NULL) || (baseType->mPriority > DbgTypePriority_Normal))
  1292. return baseType;
  1293. baseType = mCompileUnit->mDbgModule->GetPrimaryType(baseType);
  1294. mBaseTypes.mHead->mBaseType = baseType;
  1295. if (baseType->mIsDeclaration)
  1296. {
  1297. // That's no good, try to fix it up
  1298. if (baseType->GetLanguage() == DbgLanguage_Beef)
  1299. {
  1300. if (baseType->GetBaseType() == NULL)
  1301. {
  1302. if (baseType->ToString() == "System.Function")
  1303. {
  1304. DbgBaseTypeEntry* baseTypeEntry = mCompileUnit->mDbgModule->mAlloc.Alloc<DbgBaseTypeEntry>();
  1305. baseTypeEntry->mBaseType = mCompileUnit->mDbgModule->GetPrimitiveType(DbgType_IntPtr_Alias, DbgLanguage_Beef);
  1306. baseType->mBaseTypes.PushBack(baseTypeEntry);
  1307. }
  1308. }
  1309. }
  1310. }
  1311. return baseType;
  1312. }
  1313. DbgType* DbgType::GetRootBaseType()
  1314. {
  1315. auto baseType = GetBaseType();
  1316. if (baseType != NULL)
  1317. return baseType->GetRootBaseType();
  1318. return this;
  1319. }
  1320. DbgType* DbgType::RemoveModifiers(bool* hadRef)
  1321. {
  1322. DbgType* dbgType = this;
  1323. while (dbgType != NULL)
  1324. {
  1325. bool curHadRef = (dbgType->mTypeCode == DbgType_Ref) || (dbgType->mTypeCode == DbgType_RValueReference);
  1326. if ((curHadRef) && (hadRef != NULL))
  1327. *hadRef = true;
  1328. if ((dbgType->mTypeCode == DbgType_Const) || (dbgType->mTypeCode == DbgType_TypeDef) || (dbgType->mTypeCode == DbgType_Volatile) || (dbgType->mTypeCode == DbgType_Bitfield) ||
  1329. (dbgType->mTypeCode == DbgType_Unaligned) || (curHadRef))
  1330. {
  1331. if (dbgType->mTypeParam == NULL)
  1332. break;
  1333. dbgType = dbgType->mTypeParam;
  1334. }
  1335. else
  1336. break;
  1337. }
  1338. return dbgType;
  1339. }
  1340. String DbgType::ToStringRaw(DbgLanguage language)
  1341. {
  1342. if (mTypeIdx != -1)
  1343. return StrFormat("_T_%d", mTypeIdx);
  1344. return ToString(language);
  1345. }
  1346. void DbgType::ToString(StringImpl& str, DbgLanguage language, bool allowDirectBfObject, bool internalName)
  1347. {
  1348. if (language == DbgLanguage_Unknown)
  1349. language = GetLanguage();
  1350. if (language == DbgLanguage_Beef)
  1351. {
  1352. switch (mTypeCode)
  1353. {
  1354. case DbgType_UChar:
  1355. str += "char8";
  1356. return;
  1357. case DbgType_UChar16:
  1358. str += "char16";
  1359. return;
  1360. case DbgType_UChar32:
  1361. str += "char32";
  1362. return;
  1363. case DbgType_i8:
  1364. str += "int8";
  1365. return;
  1366. case DbgType_u8:
  1367. str += "uint8";
  1368. return;
  1369. case DbgType_i16:
  1370. str += "int16";
  1371. return;
  1372. case DbgType_u16:
  1373. str += "uint16";
  1374. return;
  1375. case DbgType_i32:
  1376. str += "int32";
  1377. return;
  1378. case DbgType_u32:
  1379. str += "uint32";
  1380. return;
  1381. case DbgType_i64:
  1382. str += "int64";
  1383. return;
  1384. case DbgType_u64:
  1385. str += "uint64";
  1386. return;
  1387. }
  1388. }
  1389. else
  1390. {
  1391. switch (mTypeCode)
  1392. {
  1393. case DbgType_SChar:
  1394. str += "char";
  1395. return;
  1396. case DbgType_SChar16:
  1397. str += "wchar_t";
  1398. return;
  1399. case DbgType_SChar32:
  1400. str += "int32_t";
  1401. return;
  1402. case DbgType_UChar:
  1403. str += "uint8_t";
  1404. return;
  1405. case DbgType_UChar16:
  1406. str += "uint16_t";
  1407. return;
  1408. case DbgType_UChar32:
  1409. str += "uint32_t";
  1410. return;
  1411. case DbgType_i8:
  1412. str += "char";
  1413. return;
  1414. case DbgType_u8:
  1415. str += "uint8_t";
  1416. return;
  1417. case DbgType_i16:
  1418. str += "short";
  1419. return;
  1420. case DbgType_u16:
  1421. str += "uint16_t";
  1422. return;
  1423. case DbgType_i32:
  1424. str += "int";
  1425. return;
  1426. case DbgType_u32:
  1427. str += "uint32_t";
  1428. return;
  1429. case DbgType_i64:
  1430. str += "int64_t";
  1431. return;
  1432. case DbgType_u64:
  1433. str += "uint64_t";
  1434. return;
  1435. }
  1436. }
  1437. if (mTypeCode == DbgType_Namespace)
  1438. internalName = false;
  1439. auto parent = mParent;
  1440. if ((parent == NULL) && (internalName))
  1441. {
  1442. auto primaryType = GetPrimaryType();
  1443. parent = primaryType->mParent;
  1444. }
  1445. if (mTypeName != NULL)
  1446. {
  1447. if ((!allowDirectBfObject) && (IsBfObject()))
  1448. {
  1449. // Only use the '#' for testing
  1450. //return ToString(true) + "#";
  1451. ToString(str, DbgLanguage_Unknown, true, internalName);
  1452. return;
  1453. }
  1454. if (IsGlobalsContainer())
  1455. {
  1456. if (mParent != NULL)
  1457. {
  1458. mParent->ToString(str, language, false, internalName);
  1459. return;
  1460. }
  1461. return;
  1462. }
  1463. //String combName;
  1464. /*if (mTemplateParams != NULL)
  1465. {
  1466. combName = nameP;
  1467. combName += mTemplateParams;
  1468. nameP = combName.c_str();
  1469. }*/
  1470. if ((!mFixedName) /*&& (language == DbgLanguage_Beef)*/)
  1471. {
  1472. FixName();
  1473. }
  1474. char* nameP = (char*)mTypeName;
  1475. if (parent == NULL)
  1476. {
  1477. if (strncmp(nameP, "Box<", 4) == 0)
  1478. {
  1479. str += String(nameP + 4, nameP + strlen(nameP) - 1);
  1480. str += "^";
  1481. return;
  1482. }
  1483. // For declarations, may also include namespaces
  1484. str += mName;
  1485. return;
  1486. }
  1487. if (GetLanguage() == DbgLanguage_Beef)
  1488. {
  1489. parent->ToString(str, language, allowDirectBfObject, internalName);
  1490. if ((internalName) && (parent->mTypeCode != DbgType_Namespace))
  1491. str += "+";
  1492. else
  1493. str += ".";
  1494. str += nameP;
  1495. }
  1496. else
  1497. {
  1498. parent->ToString(str, language, allowDirectBfObject, internalName);
  1499. if ((internalName) && (parent->mTypeCode != DbgType_Namespace))
  1500. str += "+";
  1501. else
  1502. str += "::";
  1503. str += nameP;
  1504. }
  1505. return;
  1506. }
  1507. switch (mTypeCode)
  1508. {
  1509. case DbgType_Struct:
  1510. {
  1511. if ((mTypeName == NULL) && (parent != NULL))
  1512. {
  1513. parent->ToString(str, language, allowDirectBfObject, internalName);
  1514. return;
  1515. }
  1516. str += "@struct";
  1517. return;
  1518. }
  1519. case DbgType_Class:
  1520. {
  1521. str += "@class";
  1522. return;
  1523. }
  1524. case DbgType_TypeDef:
  1525. {
  1526. str += "@typedef";
  1527. return;
  1528. }
  1529. case DbgType_Const:
  1530. {
  1531. if (language == DbgLanguage_Beef)
  1532. {
  1533. str += "readonly";
  1534. if (mTypeParam != NULL)
  1535. {
  1536. str += " ";
  1537. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1538. }
  1539. return;
  1540. }
  1541. str += "const";
  1542. if (mTypeParam != NULL)
  1543. {
  1544. str += " ";
  1545. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1546. }
  1547. return;
  1548. }
  1549. case DbgType_Volatile:
  1550. {
  1551. str += "volatile";
  1552. if (mTypeParam != NULL)
  1553. {
  1554. str += " ";
  1555. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1556. }
  1557. return;
  1558. }
  1559. case DbgType_Unaligned:
  1560. {
  1561. str += "unaligned";
  1562. if (mTypeParam != NULL)
  1563. {
  1564. str += " ";
  1565. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1566. }
  1567. }
  1568. case DbgType_Restrict:
  1569. {
  1570. str += "restrict";
  1571. if (mTypeParam != NULL)
  1572. {
  1573. str += " ";
  1574. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1575. }
  1576. }
  1577. case DbgType_Ptr:
  1578. {
  1579. if (mTypeParam == NULL)
  1580. {
  1581. str += "void*";
  1582. return;
  1583. }
  1584. if (mTypeParam->IsBfObject())
  1585. {
  1586. mTypeParam->ToString(str, DbgLanguage_Unknown, true, internalName);
  1587. return;
  1588. }
  1589. // Don't put a "*" on the end of a function type, it's implicit
  1590. if (mTypeParam->mTypeCode == DbgType_Subroutine)
  1591. {
  1592. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1593. return;
  1594. }
  1595. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1596. str += "*";
  1597. return;
  1598. }
  1599. case DbgType_Ref:
  1600. {
  1601. if (language == DbgLanguage_Beef)
  1602. {
  1603. str += "ref";
  1604. if (mTypeParam != NULL)
  1605. {
  1606. str += " ";
  1607. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1608. }
  1609. return;
  1610. }
  1611. if (mTypeParam == NULL)
  1612. {
  1613. str += "&";
  1614. return;
  1615. }
  1616. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1617. str += "&";
  1618. return;
  1619. }
  1620. case DbgType_RValueReference:
  1621. {
  1622. if (language == DbgLanguage_Beef)
  1623. {
  1624. // Ignore this - this is used for passing structs when we're not using the 'byval' attribute
  1625. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1626. return;
  1627. }
  1628. if (mTypeParam == NULL)
  1629. {
  1630. str += "&&";
  1631. return;
  1632. }
  1633. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1634. str += "&&";
  1635. return;
  1636. }
  1637. case DbgType_Unspecified:
  1638. str += mTypeName;
  1639. return;
  1640. case DbgType_SizedArray:
  1641. {
  1642. StringT<128> name;
  1643. auto checkType = this;
  1644. while (checkType->mTypeCode == DbgType_SizedArray)
  1645. {
  1646. intptr innerSize = checkType->mTypeParam->GetStride();
  1647. intptr arrSize = 0;
  1648. if (innerSize > 0)
  1649. {
  1650. arrSize = checkType->GetStride() / innerSize;
  1651. }
  1652. name += StrFormat("[%lld]", arrSize);
  1653. checkType = checkType->mTypeParam;
  1654. }
  1655. checkType->ToString(str, language, allowDirectBfObject, internalName);
  1656. str += name;
  1657. return;
  1658. }
  1659. case DbgType_Union:
  1660. {
  1661. str += "union";
  1662. if (mTypeParam != NULL)
  1663. {
  1664. str += " ";
  1665. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1666. }
  1667. return;
  1668. }
  1669. case DbgType_Single:
  1670. str += "float";
  1671. return;
  1672. case DbgType_Double:
  1673. str += "double";
  1674. return;
  1675. case DbgType_Null:
  1676. str += "void";
  1677. return;
  1678. case DbgType_Subroutine:
  1679. {
  1680. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1681. str += " (";
  1682. int paramIdx = 0;
  1683. for (auto param : mBlockParam->mVariables)
  1684. {
  1685. if (paramIdx > 0)
  1686. str += ", ";
  1687. param->mType->ToString(str, language, allowDirectBfObject, internalName);
  1688. paramIdx++;
  1689. }
  1690. str += ")";
  1691. return;
  1692. }
  1693. case DbgType_VTable:
  1694. str += "@vtable";
  1695. return;
  1696. case DbgType_Enum:
  1697. str += "@enum";
  1698. return;
  1699. case DbgType_Namespace:
  1700. {
  1701. // Anonymous
  1702. str += "`anon`";
  1703. return;
  1704. }
  1705. case DbgType_PtrToMember:
  1706. str += "@ptrToMember";
  1707. return;
  1708. case DbgType_Bitfield:
  1709. {
  1710. auto dbgBitfieldType = (DbgBitfieldType*)this;
  1711. mTypeParam->ToString(str, language, allowDirectBfObject, internalName);
  1712. str += StrFormat("{%d:%d}", dbgBitfieldType->mPosition, dbgBitfieldType->mLength);
  1713. return;
  1714. }
  1715. default:
  1716. break;
  1717. }
  1718. BF_FATAL("Unhandled type");
  1719. str += "???";
  1720. }
  1721. String DbgType::ToString(DbgLanguage language, bool allowDirectBfObject)
  1722. {
  1723. String str;
  1724. ToString(str, language, allowDirectBfObject, false);
  1725. return str;
  1726. }
  1727. intptr DbgType::GetByteCount()
  1728. {
  1729. if (!mSizeCalculated)
  1730. {
  1731. PopulateType();
  1732. if ((mSize == 0) && (GetLanguage() == DbgLanguage_Beef))
  1733. CalcExtType();
  1734. if ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_Union))
  1735. {
  1736. if (mPriority <= DbgTypePriority_Normal)
  1737. {
  1738. auto primaryType = GetPrimaryType();
  1739. if (primaryType != this)
  1740. {
  1741. mSize = primaryType->GetByteCount();
  1742. mAlign = primaryType->mAlign;
  1743. }
  1744. }
  1745. }
  1746. else if ((mTypeCode == DbgType_Ref) || (mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_PtrToMember))
  1747. {
  1748. #ifdef BF_DBG_32
  1749. mSize = 4;
  1750. #else
  1751. mSize = 8;
  1752. #endif
  1753. }
  1754. else if (mTypeCode == DbgType_SizedArray)
  1755. {
  1756. auto language = GetLanguage();
  1757. if (language == DbgLanguage_Beef)
  1758. {
  1759. if (mTypeParam->mAlign == 0)
  1760. {
  1761. NOP;
  1762. }
  1763. auto primaryType = mTypeParam->GetPrimaryType();
  1764. if (primaryType->mAlign == 0)
  1765. {
  1766. NOP;
  1767. }
  1768. else
  1769. {
  1770. intptr elemCount = BF_ALIGN(mSize, primaryType->mAlign) / primaryType->GetStride();
  1771. if (elemCount > 0)
  1772. {
  1773. mSize = ((elemCount - 1) * primaryType->GetStride()) + primaryType->GetByteCount();
  1774. }
  1775. }
  1776. mAlign = primaryType->mAlign;
  1777. }
  1778. }
  1779. else if (mTypeParam != NULL) // typedef, const, volatile, restrict, etc
  1780. mSize = mTypeParam->GetByteCount();
  1781. mSizeCalculated = true;
  1782. }
  1783. return mSize;
  1784. }
  1785. intptr DbgType::GetStride()
  1786. {
  1787. return BF_ALIGN(GetByteCount(), GetAlign());
  1788. }
  1789. int DbgType::GetAlign()
  1790. {
  1791. if (mAlign == 0)
  1792. {
  1793. auto primaryType = GetPrimaryType();
  1794. if (primaryType != this)
  1795. return primaryType->GetAlign();
  1796. if (IsCompositeType())
  1797. {
  1798. PopulateType();
  1799. }
  1800. }
  1801. if (mAlign != 0)
  1802. return mAlign;
  1803. return 1;
  1804. }
  1805. void DbgType::EnsureMethodsMapped()
  1806. {
  1807. for (auto methodNameEntry : mMethodNameList)
  1808. {
  1809. if (methodNameEntry->mCompileUnitId != -1)
  1810. {
  1811. mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  1812. methodNameEntry->mCompileUnitId = -1;
  1813. }
  1814. }
  1815. }
  1816. #define CREATE_PRIMITIVE_C(typeCode, cTypeName, type) \
  1817. dbgType = mAlloc.Alloc<DbgType>(); \
  1818. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1819. dbgType->mName = cTypeName; \
  1820. dbgType->mLanguage = DbgLanguage_C;\
  1821. dbgType->mTypeName = cTypeName; \
  1822. dbgType->mTypeCode = typeCode; \
  1823. dbgType->mSize = sizeof(type); \
  1824. dbgType->mAlign = sizeof(type); \
  1825. mCPrimitiveTypes[typeCode] = dbgType; \
  1826. mTypeMap.Insert(dbgType);
  1827. #define CREATE_PRIMITIVE(typeCode, cTypeName, bfTypeName, structName, type) \
  1828. dbgType = mAlloc.Alloc<DbgType>(); \
  1829. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1830. dbgType->mName = cTypeName; \
  1831. dbgType->mLanguage = DbgLanguage_C;\
  1832. dbgType->mTypeName = cTypeName; \
  1833. dbgType->mTypeCode = typeCode; \
  1834. dbgType->mSize = sizeof(type); \
  1835. dbgType->mAlign = sizeof(type); \
  1836. mCPrimitiveTypes[typeCode] = dbgType; \
  1837. mTypeMap.Insert(dbgType); \
  1838. dbgType = mAlloc.Alloc<DbgType>(); \
  1839. dbgType->mCompileUnit = &mDefaultCompileUnit; \
  1840. dbgType->mName = bfTypeName; \
  1841. dbgType->mLanguage = DbgLanguage_Beef;\
  1842. dbgType->mTypeName = bfTypeName; \
  1843. dbgType->mTypeCode = typeCode; \
  1844. dbgType->mSize = sizeof(type); \
  1845. dbgType->mAlign = sizeof(type); \
  1846. mBfPrimitiveTypes[typeCode] = dbgType; \
  1847. mPrimitiveStructNames[typeCode] = structName; \
  1848. mTypeMap.Insert(dbgType);
  1849. DbgModule::DbgModule(DebugTarget* debugTarget) : mDefaultCompileUnit(this)
  1850. {
  1851. mMemReporter = NULL;
  1852. mLoadState = DbgModuleLoadState_NotLoaded;
  1853. mMappedImageFile = NULL;
  1854. mEntryPoint = 0;
  1855. mFailMsgPtr = NULL;
  1856. mFailed = false;
  1857. for (int i = 0; i < DbgType_COUNT; i++)
  1858. {
  1859. mBfPrimitiveTypes[i] = NULL;
  1860. mCPrimitiveTypes[i] = NULL;
  1861. mPrimitiveStructNames[i] = NULL;
  1862. }
  1863. DbgType* dbgType;
  1864. mDefaultCompileUnit.mLanguage = DbgLanguage_Beef;
  1865. mDefaultCompileUnit.mDbgModule = this;
  1866. if (debugTarget != NULL)
  1867. {
  1868. // These are 'alias' definitions for C, but get overwritten by their official
  1869. // stdint.h versions (ie: int8_t)
  1870. CREATE_PRIMITIVE_C(DbgType_i8, "int8", int8);
  1871. CREATE_PRIMITIVE_C(DbgType_i16, "int16", int16);
  1872. CREATE_PRIMITIVE_C(DbgType_i32, "int32", int32);
  1873. CREATE_PRIMITIVE_C(DbgType_i64, "int64", int64);
  1874. CREATE_PRIMITIVE_C(DbgType_i8, "uint8", uint8);
  1875. CREATE_PRIMITIVE_C(DbgType_i16, "uint16", uint16);
  1876. CREATE_PRIMITIVE_C(DbgType_i32, "uint32", uint32);
  1877. CREATE_PRIMITIVE_C(DbgType_i64, "uint64", uint64);
  1878. CREATE_PRIMITIVE(DbgType_Void, "void", "void", "void", void*);
  1879. dbgType->mSize = 0;
  1880. dbgType->mAlign = 0;
  1881. CREATE_PRIMITIVE(DbgType_Null, "null", "null", "null", void*);
  1882. CREATE_PRIMITIVE(DbgType_IntPtr_Alias, "intptr_t", "int", "System.Int", intptr_target);
  1883. CREATE_PRIMITIVE(DbgType_UIntPtr_Alias, "uintptr_t", "uint", "System.UInt", addr_target);
  1884. CREATE_PRIMITIVE(DbgType_SChar, "char", "char", "System.Char", char);
  1885. CREATE_PRIMITIVE(DbgType_SChar16, "wchar_t", "wchar", "System.Char16", wchar_t);
  1886. CREATE_PRIMITIVE(DbgType_i8, "int8_t", "int8", "System.SByte", int8);
  1887. CREATE_PRIMITIVE(DbgType_i16, "short", "int16", "System.Int16", int16);
  1888. CREATE_PRIMITIVE(DbgType_i32, "int", "int32", "System.Int32", int32);
  1889. CREATE_PRIMITIVE(DbgType_i64, "int64_t", "int64", "System.Int64", int64);
  1890. CREATE_PRIMITIVE(DbgType_u8, "uint8_t", "uint8", "System.UInt8", uint8);
  1891. CREATE_PRIMITIVE(DbgType_u16, "uint16_t", "uint16", "System.UInt16", uint16);
  1892. CREATE_PRIMITIVE(DbgType_u32, "uint32_t", "uint32", "System.UInt32", uint32);
  1893. CREATE_PRIMITIVE(DbgType_u64, "uint64_t", "uint64", "System.UInt64", uint64);
  1894. CREATE_PRIMITIVE(DbgType_Single, "float", "float", "System.Single", float);
  1895. CREATE_PRIMITIVE(DbgType_Double, "double", "double", "System.Double", double);
  1896. CREATE_PRIMITIVE(DbgType_UChar, "char8", "char8", "System.Char", char);
  1897. CREATE_PRIMITIVE(DbgType_UChar16, "char16", "char16", "System.Char16", short);
  1898. CREATE_PRIMITIVE(DbgType_UChar32, "char32", "char32", "System.Char32", int);
  1899. CREATE_PRIMITIVE(DbgType_Bool, "bool", "bool", "System.Boolean", bool);
  1900. CREATE_PRIMITIVE(DbgType_Subroutine, "@Func", "@Func", "@Func", bool);
  1901. CREATE_PRIMITIVE(DbgType_RawText, "@RawText", "@RawText", "@RawText", bool);
  1902. CREATE_PRIMITIVE(DbgType_RegGroup, "@RegGroup", "@RegGroup", "@RegGroup", void*);
  1903. CREATE_PRIMITIVE_C(DbgType_i16, "int16_t", int16_t);
  1904. CREATE_PRIMITIVE_C(DbgType_i32, "int32_t", int32_t);
  1905. CREATE_PRIMITIVE_C(DbgType_i64, "__int64", int64);
  1906. CREATE_PRIMITIVE_C(DbgType_u64, "unsigned __int64", uint64);
  1907. CREATE_PRIMITIVE_C(DbgType_u8, "unsigned char", uint8);
  1908. CREATE_PRIMITIVE_C(DbgType_u16, "unsigned short", uint16);
  1909. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int", uint32);
  1910. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int32_t", uint32_t);
  1911. CREATE_PRIMITIVE_C(DbgType_u32, "unsigned long", uint32);
  1912. CREATE_PRIMITIVE_C(DbgType_u64, "unsigned int64_t", uint64);
  1913. }
  1914. mIsDwarf64 = false;
  1915. mDebugTarget = debugTarget;
  1916. if (debugTarget != NULL)
  1917. mDebugger = debugTarget->mDebugger;
  1918. else
  1919. mDebugger = NULL;
  1920. mDebugLineData = NULL;
  1921. mDebugInfoData = NULL;
  1922. mDebugPubNames = NULL;
  1923. mDebugFrameAddress = 0;
  1924. mDebugFrameData = NULL;
  1925. mDebugLocationData = NULL;
  1926. mDebugRangesData = NULL;
  1927. mDebugAbbrevData = NULL;
  1928. mDebugStrData = NULL;
  1929. mDebugAbbrevPtrData = NULL;
  1930. mEHFrameData = NULL;
  1931. mEHFrameAddress = 0;
  1932. mStringTable = NULL;
  1933. mSymbolData = NULL;
  1934. mCheckedBfObject = false;
  1935. mBfObjectHasFlags = false;
  1936. mModuleKind = DbgModuleKind_Module;
  1937. mStartTypeIdx = 0;
  1938. mEndTypeIdx = 0;
  1939. mHotIdx = 0;
  1940. mStartSubprogramIdx = 0;
  1941. mEndSubprogramIdx = 0;
  1942. mCodeAddress = NULL;
  1943. mMayBeOld = false;
  1944. mTimeStamp = 0;
  1945. mExpectedFileSize = 0;
  1946. mBfTypeType = NULL;
  1947. mBfTypesInfoAddr = 0;
  1948. mImageBase = 0;
  1949. mPreferredImageBase = 0;
  1950. mImageSize = 0;
  1951. mOrigImageData = NULL;
  1952. mDeleting = false;
  1953. mAllocSizeData = 0;
  1954. mParsedSymbolData = false;
  1955. mParsedTypeData = false;
  1956. mParsedGlobalsData = false;
  1957. mPopulatedStaticVariables = false;
  1958. mParsedFrameDescriptors = false;
  1959. mTLSAddr = 0;
  1960. mTLSSize = 0;
  1961. mTLSExtraAddr = 0;
  1962. mTLSExtraSize = 0;
  1963. mTLSIndexAddr = 0;
  1964. mDbgFlavor = DbgFlavor_Unknown;
  1965. mMasterCompileUnit = NULL;
  1966. }
  1967. DbgModule::~DbgModule()
  1968. {
  1969. delete mMemReporter;
  1970. for (auto dwSrcFile : mEmptySrcFiles)
  1971. delete dwSrcFile;
  1972. for (auto dwCompileUnit : mCompileUnits)
  1973. delete dwCompileUnit;
  1974. delete mSymbolData;
  1975. delete mStringTable;
  1976. delete mDebugLineData;
  1977. delete mDebugInfoData;
  1978. delete mDebugPubNames;
  1979. delete mDebugFrameData;
  1980. delete mDebugLocationData;
  1981. delete mDebugRangesData;
  1982. delete mDebugAbbrevData;
  1983. delete mDebugAbbrevPtrData;
  1984. delete mDebugStrData;
  1985. for (auto entry : mExceptionDirectory)
  1986. delete entry.mData;
  1987. delete mEHFrameData;
  1988. delete mOrigImageData;
  1989. if ((IsObjectFile()) && (mImageBase != 0))
  1990. {
  1991. mDebugger->ReleaseHotTargetMemory((addr_target)mImageBase, (int)mImageSize);
  1992. }
  1993. for (auto data : mOwnedSectionData)
  1994. delete data;
  1995. }
  1996. DbgSubprogram* DbgModule::FindSubprogram(DbgType* dbgType, const char * methodName)
  1997. {
  1998. dbgType = dbgType->GetPrimaryType();
  1999. dbgType->PopulateType();
  2000. if (dbgType->mNeedsGlobalsPopulated)
  2001. PopulateTypeGlobals(dbgType);
  2002. for (auto methodNameEntry : dbgType->mMethodNameList)
  2003. {
  2004. if ((methodNameEntry->mCompileUnitId != -1) && (strcmp(methodNameEntry->mName, methodName) == 0))
  2005. {
  2006. // If we hot-replaced this type then we replaced and parsed all the methods too
  2007. if (!dbgType->mCompileUnit->mDbgModule->IsObjectFile())
  2008. dbgType->mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  2009. methodNameEntry->mCompileUnitId = -1;
  2010. }
  2011. }
  2012. DbgSubprogram* result = NULL;
  2013. for (auto method : dbgType->mMethodList)
  2014. {
  2015. if (strcmp(method->mName, methodName) == 0)
  2016. {
  2017. method->PopulateSubprogram();
  2018. if ((result == NULL) || (method->mBlock.mLowPC != 0))
  2019. result = method;
  2020. }
  2021. }
  2022. return result;
  2023. }
  2024. void DbgModule::Fail(const StringImpl& error)
  2025. {
  2026. if (mFailMsgPtr != NULL)
  2027. {
  2028. if (mFailMsgPtr->IsEmpty())
  2029. *mFailMsgPtr = error;
  2030. }
  2031. String errorStr = "error ";
  2032. if (!mFilePath.IsEmpty())
  2033. {
  2034. errorStr += "Error in ";
  2035. errorStr += mFilePath;
  2036. errorStr += ": ";
  2037. }
  2038. errorStr += error;
  2039. errorStr += "\n";
  2040. mDebugger->OutputRawMessage(errorStr);
  2041. mFailed = true;
  2042. }
  2043. char* DbgModule::DbgDupString(const char* str, const char* allocName)
  2044. {
  2045. int strLen = (int)strlen(str);
  2046. if (strLen == 0)
  2047. return NULL;
  2048. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, (allocName != NULL) ? allocName : "DbgDupString");
  2049. memcpy(dupStr, str, strLen);
  2050. return dupStr;
  2051. }
  2052. DbgModule* DbgModule::GetLinkedModule()
  2053. {
  2054. if (IsObjectFile())
  2055. return mDebugTarget->mTargetBinary;
  2056. return this;
  2057. }
  2058. addr_target DbgModule::GetTargetImageBase()
  2059. {
  2060. if (IsObjectFile())
  2061. return (addr_target)mDebugTarget->mTargetBinary->mImageBase;
  2062. return (addr_target)mImageBase;
  2063. }
  2064. void DbgModule::ParseGlobalsData()
  2065. {
  2066. mParsedGlobalsData = true;
  2067. }
  2068. void DbgModule::ParseSymbolData()
  2069. {
  2070. mParsedSymbolData = true;
  2071. }
  2072. void DbgModule::ParseTypeData()
  2073. {
  2074. mParsedTypeData = true;
  2075. }
  2076. DbgCompileUnit* DbgModule::ParseCompileUnit(int compileUnitId)
  2077. {
  2078. return NULL;
  2079. }
  2080. void DbgModule::MapCompileUnitMethods(DbgCompileUnit * compileUnit)
  2081. {
  2082. }
  2083. void DbgModule::MapCompileUnitMethods(int compileUnitId)
  2084. {
  2085. }
  2086. void DbgModule::PopulateType(DbgType* dbgType)
  2087. {
  2088. }
  2089. void DbgModule::PopulateTypeGlobals(DbgType* dbgType)
  2090. {
  2091. }
  2092. void DbgModule::PopulateStaticVariableMap()
  2093. {
  2094. if (mPopulatedStaticVariables)
  2095. return;
  2096. for (auto staticVariable : mStaticVariables)
  2097. {
  2098. mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  2099. }
  2100. mPopulatedStaticVariables = true;
  2101. }
  2102. void DbgModule::ProcessDebugInfo()
  2103. {
  2104. }
  2105. addr_target DbgModule::RemapAddr(addr_target addr)
  2106. {
  2107. if ((addr != 0) && (mPreferredImageBase != 0) && (mImageBase != 0))
  2108. return addr + (intptr_target)(mImageBase - mPreferredImageBase);
  2109. return addr;
  2110. }
  2111. void DbgModule::ParseAbbrevData(const uint8* data)
  2112. {
  2113. while (true)
  2114. {
  2115. int abbrevIdx = (int)DecodeULEB128(data);
  2116. mDebugAbbrevPtrData[abbrevIdx] = data;
  2117. if (abbrevIdx == 0)
  2118. break;
  2119. int entryTag = (int)DecodeULEB128(data);
  2120. bool hasChildren = GET(char) == DW_CHILDREN_yes;
  2121. while (true)
  2122. {
  2123. int attrName = (int)DecodeULEB128(data);
  2124. int form = (int)DecodeULEB128(data);
  2125. if ((attrName == 0) && (form == 0))
  2126. break;
  2127. }
  2128. }
  2129. }
  2130. void DbgModule::ParseExceptionData()
  2131. {
  2132. if (mExceptionDirectory.IsEmpty())
  2133. return;
  2134. BP_ZONE("DbgModule::ParseExceptionData");
  2135. for (auto entry : mExceptionDirectory)
  2136. {
  2137. const uint8* data = entry.mData;
  2138. const uint8* dataEnd = data + entry.mSize;
  2139. static int entryCount = 0;
  2140. addr_target imageBase = GetTargetImageBase();
  2141. while (data < dataEnd)
  2142. {
  2143. addr_target beginAddress = GET(uint32);
  2144. addr_target endAddress = GET(uint32);
  2145. uint32 unwindData = GET(uint32);
  2146. //TODO: Apparently unwindData can refer to another runtime entry in the .pdata if the LSB is set to 1?
  2147. beginAddress += (addr_target)imageBase;
  2148. endAddress += (addr_target)imageBase;
  2149. int exSize = (int)(endAddress - beginAddress);
  2150. for (int exOffset = 0; true; exOffset += DBG_MAX_LOOKBACK)
  2151. {
  2152. int curSize = exSize - exOffset;
  2153. if (curSize <= 0)
  2154. break;
  2155. BP_ALLOC_T(DbgExceptionDirectoryEntry);
  2156. DbgExceptionDirectoryEntry* exceptionDirectoryEntry = mAlloc.Alloc<DbgExceptionDirectoryEntry>();
  2157. exceptionDirectoryEntry->mAddress = beginAddress + exOffset;
  2158. exceptionDirectoryEntry->mOrigAddressOffset = exOffset;
  2159. exceptionDirectoryEntry->mAddressLength = curSize;
  2160. exceptionDirectoryEntry->mExceptionPos = (int)unwindData;
  2161. exceptionDirectoryEntry->mDbgModule = this;
  2162. mDebugTarget->mExceptionDirectoryMap.Insert(exceptionDirectoryEntry);
  2163. entryCount++;
  2164. }
  2165. }
  2166. }
  2167. }
  2168. static int gIdx = 0;
  2169. template <typename T> static bool IsTypeSigned() { return false; }
  2170. template <> bool IsTypeSigned<int8>() { return true; }
  2171. template <> bool IsTypeSigned<int16>() { return true; }
  2172. template <> bool IsTypeSigned<int32>() { return true; }
  2173. template <> bool IsTypeSigned<int64>() { return true; }
  2174. #pragma warning(push)
  2175. #pragma warning(disable:4302)
  2176. #pragma warning(disable:4311)
  2177. #pragma warning(disable:4312)
  2178. #pragma warning(disable:4800)
  2179. #pragma warning(disable:4800)
  2180. template <typename T>
  2181. T DbgModule::ReadValue(const uint8*& data, int form, int refOffset, const uint8** extraData, const uint8* startData)
  2182. {
  2183. gIdx++;
  2184. switch (form)
  2185. {
  2186. case DW_FORM_strp:
  2187. {
  2188. int strOffset = GET(int);
  2189. BF_ASSERT(mDebugStrData != NULL);
  2190. const char* str = (const char*)mDebugStrData + strOffset;
  2191. return (T)(intptr)str;
  2192. }
  2193. break;
  2194. case DW_FORM_data1:
  2195. {
  2196. if (IsTypeSigned<T>())
  2197. return (T)(intptr)GET(int8);
  2198. else
  2199. return (T)(uintptr)GET(uint8);
  2200. }
  2201. break;
  2202. case DW_FORM_data2:
  2203. {
  2204. if (IsTypeSigned<T>())
  2205. return (T)(intptr)GET(int16);
  2206. else
  2207. return (T)(uintptr)GET(uint16);
  2208. }
  2209. break;
  2210. case DW_FORM_data4:
  2211. {
  2212. if (IsTypeSigned<T>())
  2213. return (T)(intptr)GET(int32);
  2214. else
  2215. return (T)(uintptr)GET(uint32);
  2216. }
  2217. break;
  2218. case DW_FORM_data8:
  2219. {
  2220. if (IsTypeSigned<T>())
  2221. return (T)GET(int64);
  2222. else
  2223. return (T)GET(uint64);
  2224. }
  2225. break;
  2226. case DW_FORM_ref1:
  2227. {
  2228. return (T)(intptr)GET(int8) + refOffset;
  2229. }
  2230. break;
  2231. case DW_FORM_ref2:
  2232. {
  2233. return (T)(intptr)GET(int16) + refOffset;
  2234. }
  2235. break;
  2236. case DW_FORM_ref4:
  2237. {
  2238. return (T)(intptr)GET(int32) + refOffset;
  2239. }
  2240. break;
  2241. case DW_FORM_sec_offset:
  2242. {
  2243. intptr_target offset;
  2244. if (mIsDwarf64)
  2245. offset = (intptr_target)GET(int64);
  2246. else
  2247. offset = GET(int32);
  2248. if (extraData != NULL)
  2249. {
  2250. *extraData = mDebugLocationData + offset;
  2251. return 0;
  2252. }
  2253. return (T)offset;
  2254. }
  2255. break;
  2256. case DW_FORM_addr:
  2257. {
  2258. return (T)GET(addr_target);
  2259. }
  2260. break;
  2261. case DW_FORM_exprloc:
  2262. {
  2263. int64_t exprLen = DecodeULEB128(data);
  2264. const uint8* endData = data + exprLen;
  2265. if (extraData != NULL)
  2266. *extraData = data;
  2267. data = endData;
  2268. return (T)exprLen;
  2269. }
  2270. break;
  2271. case DW_FORM_flag_present:
  2272. {
  2273. //
  2274. return (T)1;
  2275. }
  2276. break;
  2277. case DW_FORM_flag:
  2278. {
  2279. //
  2280. return (T)(intptr)GET(char);
  2281. }
  2282. break;
  2283. case DW_FORM_sdata:
  2284. return (T)DecodeSLEB128(data);
  2285. case DW_FORM_udata:
  2286. return (T)DecodeULEB128(data);
  2287. case DW_FORM_string:
  2288. {
  2289. const char* str = (const char*)data;
  2290. while (true)
  2291. {
  2292. uint8 val = *data;
  2293. data++;
  2294. if (val == 0)
  2295. return (T)(intptr)str;
  2296. }
  2297. }
  2298. case DW_FORM_block:
  2299. {
  2300. int blockLen = (int)DecodeULEB128(data);
  2301. const uint8* retVal = data;
  2302. data += blockLen;
  2303. return (T)(intptr)retVal;
  2304. }
  2305. case DW_FORM_block1:
  2306. {
  2307. int blockLen = (int)*((uint8*)data);
  2308. data += sizeof(uint8);
  2309. const uint8* retVal = data;
  2310. data += blockLen;
  2311. return (T)(intptr)retVal;
  2312. }
  2313. default:
  2314. assert("Not covered!" == 0);
  2315. break;
  2316. }
  2317. return (T)0;
  2318. }
  2319. #pragma warning(pop)
  2320. static int gAbbrevNum = 0;
  2321. DbgType* DbgModule::GetOrCreateType(int typeIdx, DbgDataMap& dataMap)
  2322. {
  2323. if (typeIdx == 0)
  2324. return NULL;
  2325. DbgModule* linkedModule = GetLinkedModule();
  2326. DbgType* dbgType = dataMap.Get<DbgType*>(typeIdx);
  2327. if (dbgType != NULL)
  2328. return dbgType;
  2329. dbgType = mAlloc.Alloc<DbgType>();
  2330. dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
  2331. linkedModule->mTypes.push_back(dbgType);
  2332. dataMap.Set(typeIdx, dbgType);
  2333. return dbgType;
  2334. }
  2335. typedef std::pair<DbgClassType, void*> DataPair;
  2336. typedef llvm::SmallVector<DataPair, 16> DataStack;
  2337. template <typename T>
  2338. T DbgModule::GetOrCreate(int idx, DbgDataMap& dataMap)
  2339. {
  2340. if (idx == 0)
  2341. return NULL;
  2342. T val = dataMap.Get<T>(idx);
  2343. if (val != NULL)
  2344. return val;
  2345. val = mAlloc.Alloc<typename RemoveTypePointer<T>::type >();
  2346. dataMap.Set(idx, val);
  2347. return val;
  2348. }
  2349. template <typename T>
  2350. static T GetStackTop(DataStack* dataStack)
  2351. {
  2352. auto dataPair = dataStack->back();
  2353. if (dataPair.first == RemoveTypePointer<T>::type::ClassType)
  2354. return (T)dataPair.second;
  2355. return NULL;
  2356. }
  2357. template <>
  2358. DbgBlock* GetStackTop<DbgBlock*>(DataStack* dataStack)
  2359. {
  2360. auto dataPair = dataStack->back();
  2361. if (dataPair.first == DbgBlock::ClassType)
  2362. return (DbgBlock*)dataPair.second;
  2363. if (dataPair.first == DbgSubprogram::ClassType)
  2364. return &((DbgSubprogram*)dataPair.second)->mBlock;
  2365. if (dataPair.first == DbgType::ClassType)
  2366. return ((DbgType*)dataPair.second)->mBlockParam;
  2367. return NULL;
  2368. }
  2369. template <typename T>
  2370. static bool StackHasType(DataStack* dataStack)
  2371. {
  2372. for (auto itr : *dataStack)
  2373. if (itr.first == RemoveTypePointer<T>::type::ClassType)
  2374. return true;
  2375. return false;
  2376. }
  2377. template <typename T>
  2378. static T GetStackLast(DataStack* dataStack)
  2379. {
  2380. for (int i = (int)dataStack->size() - 1; i >= 0; i--)
  2381. {
  2382. if ((*dataStack)[i].first == RemoveTypePointer<T>::type::ClassType)
  2383. return (T)(*dataStack)[i].second;
  2384. }
  2385. return NULL;
  2386. }
  2387. template <typename T>
  2388. static DataPair MakeDataPair(T* data)
  2389. {
  2390. return DataPair(T::ClassType, data);
  2391. }
  2392. void DbgModule::FixupInnerTypes(int startingTypeIdx)
  2393. {
  2394. BP_ZONE("DbgModule_FixupInnerTypes");
  2395. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2396. {
  2397. DbgType* dbgType = mTypes[typeIdx];
  2398. if ((dbgType->mPriority == DbgTypePriority_Primary_Implicit) && (dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) &&
  2399. (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2400. {
  2401. auto primaryParent = dbgType->mParent->GetPrimaryType();
  2402. dbgType->mParent->mSubTypeList.Clear();
  2403. dbgType->mParent = primaryParent;
  2404. primaryParent->mSubTypeList.PushBack(dbgType);
  2405. }
  2406. }
  2407. }
  2408. void DbgModule::MapTypes(int startingTypeIdx)
  2409. {
  2410. BP_ZONE("DbgModule_MapTypes");
  2411. bool needsInnerFixups = false;
  2412. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2413. {
  2414. DbgType* dbgType = mTypes[typeIdx];
  2415. BF_ASSERT(dbgType->mTypeCode != DbgType_Null);
  2416. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mPriority < DbgTypePriority_Primary_Implicit))
  2417. continue;
  2418. //TODO: Always valid?
  2419. if (dbgType->mIsDeclaration)
  2420. continue;
  2421. // We were avoiding adding '<' names before, but that made it impossible to look up auto-named primary types ,
  2422. // like in-place unions like '<unnamed-type-u>'
  2423. if ((dbgType->mTypeName == NULL) || (dbgType->mName == NULL) /*|| (dbgType->mTypeName[0] == '<')*/)
  2424. continue;
  2425. if (dbgType->mTypeCode > DbgType_DefinitionEnd)
  2426. {
  2427. // Only add "definition types"
  2428. continue;
  2429. }
  2430. if (dbgType->mTypeCode == DbgType_Namespace)
  2431. {
  2432. bool isQualifiedNamespace = false;
  2433. for (const char* cPtr = dbgType->mTypeName; *cPtr != '\0'; cPtr++)
  2434. if (*cPtr == '.')
  2435. isQualifiedNamespace = true;
  2436. if (isQualifiedNamespace)
  2437. continue; // Don't add fully qualified namespaces (they come from the 'using' implementation)*
  2438. }
  2439. if (dbgType->mHasStaticMembers)
  2440. {
  2441. for (auto member : dbgType->mMemberList)
  2442. if ((member->mIsStatic) && (member->mLocationData != NULL))
  2443. dbgType->mDefinedMembersSize++;
  2444. }
  2445. if ((dbgType->mTypeName != NULL) && (strcmp(dbgType->mTypeName, "@") == 0))
  2446. {
  2447. // Globals type.
  2448. continue;
  2449. }
  2450. auto prevTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  2451. // Only replace previous instance if its a declaration
  2452. if (prevTypeEntry != NULL)
  2453. {
  2454. auto prevType = prevTypeEntry->mValue;
  2455. if (dbgType->mCompileUnit->mDbgModule != prevType->mCompileUnit->mDbgModule)
  2456. {
  2457. // Don't replace original types with hot types -- those need to be inserted in the the hot alternates list
  2458. BF_ASSERT(dbgType->mCompileUnit->mDbgModule->IsObjectFile());
  2459. prevType->mHotNewType = dbgType;
  2460. continue;
  2461. }
  2462. // Never override explicit primaries
  2463. if (prevType->mPriority == DbgTypePriority_Primary_Explicit)
  2464. continue;
  2465. if (dbgType->mTypeCode == DbgType_TypeDef)
  2466. {
  2467. // Typedef can never override anything
  2468. continue;
  2469. }
  2470. if (prevType->mTypeCode == DbgType_TypeDef)
  2471. {
  2472. if (dbgType->mTypeCode != DbgType_TypeDef)
  2473. {
  2474. // Allow this to override
  2475. prevTypeEntry->mValue = dbgType;
  2476. }
  2477. continue;
  2478. }
  2479. // Don't replace a ptr to an BfObject with a BfObject
  2480. if ((prevType->mTypeCode == DbgType_Ptr) && (dbgType->mTypeCode == DbgType_Struct))
  2481. continue;
  2482. if ((prevType->mTypeCode == DbgType_Struct) && (dbgType->mTypeCode == DbgType_Ptr))
  2483. {
  2484. // Allow this to override
  2485. prevTypeEntry->mValue = dbgType;
  2486. continue;
  2487. }
  2488. if (prevType->mTypeCode == DbgType_Namespace)
  2489. {
  2490. if (dbgType->mTypeCode != DbgType_Namespace)
  2491. {
  2492. // Old type was namespace but new isn't? Replace old type.
  2493. while (!prevType->mSubTypeList.IsEmpty())
  2494. {
  2495. DbgType* subType = prevType->mSubTypeList.PopFront();
  2496. subType->mParent = dbgType;
  2497. dbgType->mSubTypeList.PushBack(subType);
  2498. }
  2499. prevType->mPriority = DbgTypePriority_Normal;
  2500. if (dbgType->mPriority < DbgTypePriority_Primary_Implicit)
  2501. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2502. prevTypeEntry->mValue = dbgType;
  2503. continue;
  2504. }
  2505. // We definitely didn't want to do this for MS. For DWARF?
  2506. //prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2507. continue;
  2508. }
  2509. else
  2510. {
  2511. // New type is namespace but old wasn't? Ignore new type.
  2512. if (dbgType->mTypeCode == DbgType_Namespace)
  2513. continue;
  2514. if (dbgType->mIsDeclaration)
  2515. continue;
  2516. if (!prevType->mIsDeclaration)
  2517. {
  2518. if ((prevType->mCompileUnit == NULL) || (dbgType->mLanguage < prevType->mLanguage))
  2519. {
  2520. // We always want 'Beef' types to supersede 'C' types, but don't override the built-in primitive types
  2521. continue;
  2522. }
  2523. if (prevType->mDefinedMembersSize > 0)
  2524. {
  2525. if (dbgType->mDefinedMembersSize > 0)
  2526. {
  2527. // We create an 'alternates' list for all types that define at least one static field
  2528. if (prevType->mHasStaticMembers)
  2529. prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2530. }
  2531. continue;
  2532. }
  2533. // if (prevType->mDefinedMembersSize > dbgType->mDefinedMembersSize)
  2534. // {
  2535. // continue;
  2536. // }
  2537. if (prevType->mMethodsWithParamsCount > dbgType->mMethodsWithParamsCount)
  2538. {
  2539. // This handles a special case where methods without line data like <Enum>.HasFlags doesn't show containing
  2540. // params in cases where it gets inlined
  2541. continue;
  2542. }
  2543. // Types with method lists are preferred
  2544. if ((!prevType->mMethodList.IsEmpty()) && (dbgType->mMethodList.IsEmpty()))
  2545. continue;
  2546. if ((prevType->mTypeCode == DbgType_Ptr) && (prevType->mTypeParam != NULL) && (!prevType->mTypeParam->mMethodList.IsEmpty()))
  2547. continue;
  2548. }
  2549. // Replace type
  2550. if (!prevType->mSubTypeList.IsEmpty())
  2551. needsInnerFixups = true;
  2552. prevType->mPriority = DbgTypePriority_Normal;
  2553. if (dbgType->mPriority == DbgTypePriority_Normal)
  2554. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2555. prevTypeEntry->mValue = dbgType;
  2556. continue;
  2557. }
  2558. }
  2559. if ((dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) && (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2560. needsInnerFixups = true;
  2561. if (dbgType->mPriority == DbgTypePriority_Normal)
  2562. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2563. mTypeMap.Insert(dbgType);
  2564. }
  2565. if (needsInnerFixups)
  2566. FixupInnerTypes(startingTypeIdx);
  2567. }
  2568. void DbgModule::CreateNamespaces()
  2569. {
  2570. BP_ZONE("DbgModule::CreateNamespaces");
  2571. int startLength = (int)mTypes.size();
  2572. for (int typeIdx = 0; typeIdx < startLength; typeIdx++)
  2573. {
  2574. DbgType* dbgType = mTypes[typeIdx];
  2575. if (dbgType->mName == NULL)
  2576. continue;
  2577. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mTagIdx != 0))
  2578. {
  2579. auto namespaceTypeEntry = FindType(dbgType->mName, dbgType->GetLanguage());
  2580. DbgType* namespaceType;
  2581. if (namespaceTypeEntry == NULL)
  2582. {
  2583. namespaceType = mAlloc.Alloc<DbgType>();
  2584. namespaceType->mTypeCode = DbgType_Namespace;
  2585. namespaceType->mLanguage = dbgType->mLanguage;
  2586. namespaceType->mCompileUnit = dbgType->mCompileUnit;
  2587. namespaceType->mTypeIdx = (int)mTypes.size();
  2588. namespaceType->mPriority = DbgTypePriority_Primary_Explicit;
  2589. namespaceType->mName = dbgType->mName;
  2590. namespaceType->mTypeName = dbgType->mTypeName;
  2591. if (dbgType->mParent != NULL)
  2592. {
  2593. namespaceType->mParent = dbgType->mParent->GetPrimaryType();
  2594. namespaceType->mParent->mSubTypeList.PushBack(namespaceType);
  2595. }
  2596. else
  2597. {
  2598. namespaceType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(namespaceType);
  2599. }
  2600. mTypes.push_back(namespaceType);
  2601. mTypeMap.Insert(namespaceType);
  2602. }
  2603. else
  2604. namespaceType = namespaceTypeEntry->mValue;
  2605. while (!dbgType->mMemberList.IsEmpty())
  2606. {
  2607. DbgVariable* curVar = dbgType->mMemberList.PopFront();
  2608. namespaceType->mMemberList.PushBack(curVar);
  2609. }
  2610. DbgType* prevType = NULL;
  2611. DbgType* curType = dbgType->mSubTypeList.mHead;
  2612. while (curType != NULL)
  2613. {
  2614. DbgType* nextType = curType->mNext;
  2615. if (curType->mPriority >= DbgTypePriority_Primary_Implicit)
  2616. {
  2617. dbgType->mSubTypeList.Remove(curType, prevType);
  2618. namespaceType->mSubTypeList.PushBack(curType);
  2619. }
  2620. prevType = curType;
  2621. curType = nextType;
  2622. }
  2623. continue;
  2624. }
  2625. }
  2626. // If we didn't have a parent type for a namespace (ie: if System.Collections wasn't linked to System) then we wait
  2627. // until the end and move those from the global list to the parent list
  2628. for (int typeIdx = startLength; typeIdx < (int)mTypes.size(); typeIdx++)
  2629. {
  2630. DbgType* dbgType = mTypes[typeIdx];
  2631. if (dbgType->mParent != NULL)
  2632. continue;
  2633. char* typeName = (char*)dbgType->mTypeName;
  2634. int lastDotIdx = -1;
  2635. for (int i = 0; true; i++)
  2636. {
  2637. char c = typeName[i];
  2638. if (c == 0)
  2639. break;
  2640. if (c == '.')
  2641. lastDotIdx = i;
  2642. }
  2643. if (lastDotIdx == -1)
  2644. continue;
  2645. typeName[lastDotIdx] = 0;
  2646. dbgType->mTypeName = typeName + lastDotIdx + 1;
  2647. auto parentEntry = FindType(typeName, dbgType->GetLanguage());
  2648. typeName[lastDotIdx] = '.';
  2649. if (parentEntry == NULL)
  2650. continue;
  2651. auto parentType = parentEntry->mValue;
  2652. dbgType->mCompileUnit->mGlobalType->mSubTypeList.Remove(dbgType);
  2653. dbgType->mParent = parentType;
  2654. parentType->mSubTypeList.PushBack(dbgType);
  2655. }
  2656. }
  2657. void DbgModule::FindTemplateStr(const char*& name, int& templateNameIdx)
  2658. {
  2659. if (templateNameIdx == 0)
  2660. {
  2661. for (int i = 0; name[i] != 0; i++)
  2662. {
  2663. if (name[i] == '<')
  2664. {
  2665. templateNameIdx = i;
  2666. return;
  2667. }
  2668. }
  2669. templateNameIdx = -1;
  2670. }
  2671. }
  2672. void DbgModule::TempRemoveTemplateStr(const char*& name, int& templateNameIdx)
  2673. {
  2674. if (templateNameIdx == 0)
  2675. FindTemplateStr(name, templateNameIdx);
  2676. if (templateNameIdx == -1)
  2677. return;
  2678. if (!DbgIsStrMutable(name))
  2679. name = DbgDupString(name);
  2680. ((char*)name)[templateNameIdx] = 0;
  2681. }
  2682. void DbgModule::ReplaceTemplateStr(const char*& name, int& templateNameIdx)
  2683. {
  2684. if (templateNameIdx > 0)
  2685. ((char*)name)[templateNameIdx] = '<';
  2686. }
  2687. void DbgModule::MapSubprogram(DbgSubprogram* dbgSubprogram)
  2688. {
  2689. if (dbgSubprogram->mBlock.IsEmpty())
  2690. return;
  2691. int progSize = (int)(dbgSubprogram->mBlock.mHighPC - dbgSubprogram->mBlock.mLowPC);
  2692. for (int progOffset = 0; true; progOffset += DBG_MAX_LOOKBACK)
  2693. {
  2694. int curSize = progSize - progOffset;
  2695. if (curSize <= 0)
  2696. break;
  2697. BP_ALLOC_T(DbgSubprogramMapEntry);
  2698. DbgSubprogramMapEntry* subprogramMapEntry = mAlloc.Alloc<DbgSubprogramMapEntry>();
  2699. subprogramMapEntry->mAddress = dbgSubprogram->mBlock.mLowPC + progOffset;
  2700. subprogramMapEntry->mEntry = dbgSubprogram;
  2701. mDebugTarget->mSubprogramMap.Insert(subprogramMapEntry);
  2702. }
  2703. }
  2704. bool DbgModule::ParseDWARF(const uint8*& dataPtr)
  2705. {
  2706. BP_ZONE("ParseDWARF");
  2707. const uint8* data = dataPtr;
  2708. const uint8* startData = mDebugInfoData;
  2709. int dataOfs = (int)(data - mDebugInfoData);
  2710. intptr_target length = GET(int);
  2711. DbgModule* linkedModule = GetLinkedModule();
  2712. if (length == -1)
  2713. {
  2714. mIsDwarf64 = true;
  2715. length = (intptr_target)GET(int64);
  2716. }
  2717. else
  2718. mIsDwarf64 = false;
  2719. if (length == 0)
  2720. return false;
  2721. const uint8* dataEnd = data + length;
  2722. int version = GET(short);
  2723. int abbrevOffset = GET(int);
  2724. char pointerSize = GET(char);
  2725. ParseAbbrevData(mDebugAbbrevData + abbrevOffset);
  2726. DbgCompileUnit* compileUnit = new DbgCompileUnit(this);
  2727. mDbgFlavor = DbgFlavor_GNU;
  2728. compileUnit->mDbgModule = this;
  2729. mCompileUnits.push_back(compileUnit);
  2730. DbgSubprogram* subProgram = NULL;
  2731. //std::map<int, DbgType*> typeMap;
  2732. //std::map<int, DbgSubprogram*> subprogramMap;
  2733. int tagStart = (int)(data - startData);
  2734. int tagEnd = (int)(dataEnd - startData);
  2735. DbgDataMap dataMap(tagStart, tagEnd);
  2736. DataStack dataStack;
  2737. Array<AbstractOriginEntry> abstractOriginReplaceList;
  2738. Array<int> deferredArrayDims;
  2739. int startingTypeIdx = (int)linkedModule->mTypes.size();
  2740. while (data < dataEnd)
  2741. {
  2742. gAbbrevNum++;
  2743. const uint8* tagDataStart = data;
  2744. int tagIdx = (int)(tagDataStart - startData);
  2745. int abbrevIdx = (int)DecodeULEB128(data);
  2746. const uint8* abbrevData = mDebugAbbrevPtrData[abbrevIdx];
  2747. if (abbrevIdx == 0)
  2748. {
  2749. if (deferredArrayDims.size() > 0)
  2750. {
  2751. DbgType* arrType = GetStackTop<DbgType*>(&dataStack);
  2752. BF_ASSERT(arrType->mTypeCode == DbgType_SizedArray);
  2753. arrType->mSize = deferredArrayDims[0]; // Byte count still needs to be multiplied by the underlying type size
  2754. DbgType* rootArrType = arrType;
  2755. for (int dimIdx = 0; dimIdx < (int)deferredArrayDims.size() - 1; dimIdx++)
  2756. {
  2757. int dimSize = deferredArrayDims[dimIdx];
  2758. DbgType* subArrType = mAlloc.Alloc<DbgType>();
  2759. subArrType->mCompileUnit = compileUnit;
  2760. subArrType->mLanguage = compileUnit->mLanguage;
  2761. subArrType->mTypeIdx = (int)linkedModule->mTypes.size();
  2762. linkedModule->mTypes.push_back(subArrType);
  2763. subArrType->mTypeCode = DbgType_SizedArray;
  2764. subArrType->mTypeParam = arrType->mTypeParam;
  2765. subArrType->mSize = deferredArrayDims[dimIdx + 1];
  2766. arrType->mTypeParam = subArrType;
  2767. arrType = subArrType;
  2768. }
  2769. deferredArrayDims.Clear();
  2770. }
  2771. dataStack.pop_back();
  2772. continue;
  2773. }
  2774. int entryTag = (int) DecodeULEB128(abbrevData);
  2775. bool hasChildren = GET_FROM(abbrevData, char) == DW_CHILDREN_yes;
  2776. int64 atLowPC = 0;
  2777. int64 atHighPC = 0;
  2778. int64 atRanges = 0;
  2779. bool hasRanges = false;
  2780. const uint8* atFrameBase = NULL;
  2781. int64_t atFrameBaseLength = 0;
  2782. int64 atLocationLen = 0;
  2783. const uint8* atLocationData = 0;
  2784. const char* atProducer = NULL;
  2785. const char* atName = NULL;
  2786. const char* atCompDir = NULL;
  2787. const char* atLinkageName = NULL;
  2788. int64 atConstValue = 0;
  2789. int atDataMemberLocation = 0;
  2790. const uint8* atDataMemberData = NULL;
  2791. int atDeclFile = 0;
  2792. int atDeclLine = 0;
  2793. int atCallFile = 0;
  2794. int atCallLine = 0;
  2795. int atCount = 0;
  2796. int atType = 0;
  2797. int atImport = 0;
  2798. int atInline = 0;
  2799. int atArtificial = 0;
  2800. int atExternal = 0;
  2801. int atByteSize = -1;
  2802. int atEncoding = 0;
  2803. int atSpecification = 0;
  2804. int atObjectPointer = 0;
  2805. int atBitOffset = 0;
  2806. int atBitSize = 0;
  2807. int atAbstractOrigin = 0;
  2808. const uint8* atVirtualLocData = NULL;
  2809. bool atDeclaration = false;
  2810. bool atVirtual = false;
  2811. bool hadConstValue = false;
  2812. bool hadMemberLocation = false;
  2813. bool isOptimized = false;
  2814. DataPair newDataPair;
  2815. while (true)
  2816. {
  2817. int attrName = (int)DecodeULEB128(abbrevData);
  2818. int form = (int)DecodeULEB128(abbrevData);
  2819. if ((attrName == 0) && (form == 0))
  2820. break;
  2821. switch (attrName)
  2822. {
  2823. case DW_AT_sibling:
  2824. ReadValue<char>(data, form);
  2825. break;
  2826. case DW_AT_location:
  2827. atLocationLen = (int)ReadValue<uint>(data, form, dataOfs, &atLocationData, startData);
  2828. break;
  2829. case DW_AT_name:
  2830. atName = ReadValue<const char*>(data, form);
  2831. break;
  2832. case DW_AT_ordering:
  2833. /*TODO:*/ ReadValue<int>(data, form);
  2834. break;
  2835. case DW_AT_byte_size:
  2836. atByteSize = ReadValue<int>(data, form);
  2837. break;
  2838. case DW_AT_bit_offset:
  2839. atBitOffset = ReadValue<int>(data, form);
  2840. break;
  2841. case DW_AT_bit_size:
  2842. atBitSize = ReadValue<int>(data, form);
  2843. break;
  2844. case DW_AT_stmt_list:
  2845. ReadValue<int64_t>(data, form);
  2846. break;
  2847. case DW_AT_low_pc:
  2848. atLowPC = RemapAddr((addr_target)ReadValue<int64_t>(data, form));
  2849. break;
  2850. case DW_AT_high_pc:
  2851. atHighPC = ReadValue<int64_t>(data, form);
  2852. break;
  2853. case DW_AT_language:
  2854. /*TODO:*/ ReadValue<int>(data, form);
  2855. break;
  2856. case DW_AT_discr:
  2857. /*TODO:*/ ReadValue<int>(data, form);
  2858. break;
  2859. case DW_AT_discr_value:
  2860. /*TODO:*/ ReadValue<int>(data, form);
  2861. break;
  2862. case DW_AT_visibility:
  2863. /*TODO:*/ ReadValue<int>(data, form);
  2864. break;
  2865. case DW_AT_import:
  2866. atImport = ReadValue<int>(data, form) + dataOfs;
  2867. break;
  2868. case DW_AT_string_length:
  2869. /*TODO:*/ ReadValue<int>(data, form);
  2870. break;
  2871. case DW_AT_common_reference:
  2872. /*TODO:*/ ReadValue<int>(data, form);
  2873. break;
  2874. case DW_AT_comp_dir:
  2875. atCompDir = ReadValue<const char*>(data, form);
  2876. break;
  2877. case DW_AT_const_value:
  2878. atConstValue = ReadValue<int64>(data, form);
  2879. hadConstValue = true;
  2880. break;
  2881. case DW_AT_containing_type:
  2882. /*TODO:*/ ReadValue<int>(data, form);
  2883. break;
  2884. case DW_AT_default_value:
  2885. /*TODO:*/ ReadValue<int>(data, form);
  2886. break;
  2887. case DW_AT_inline:
  2888. atInline = ReadValue<int>(data, form);
  2889. break;
  2890. case DW_AT_is_optional:
  2891. /*TODO:*/ ReadValue<int>(data, form);
  2892. break;
  2893. case DW_AT_lower_bound:
  2894. /*TODO:*/ ReadValue<int>(data, form);
  2895. break;
  2896. case DW_AT_producer:
  2897. atProducer = ReadValue<const char*>(data, form);
  2898. break;
  2899. case DW_AT_prototyped:
  2900. /*TODO:*/ ReadValue<int>(data, form);
  2901. break;
  2902. case DW_AT_return_addr:
  2903. /*TODO:*/ ReadValue<int>(data, form);
  2904. break;
  2905. case DW_AT_start_scope:
  2906. /*TODO:*/ ReadValue<int>(data, form);
  2907. break;
  2908. case DW_AT_bit_stride:
  2909. /*TODO:*/ ReadValue<int>(data, form);
  2910. break;
  2911. case DW_AT_upper_bound:
  2912. // Lower bound not supported
  2913. atCount = ReadValue<int>(data, form);
  2914. break;
  2915. case DW_AT_abstract_origin:
  2916. atAbstractOrigin = ReadValue<int>(data, form, dataOfs);
  2917. break;
  2918. case DW_AT_accessibility:
  2919. /*TODO:*/ ReadValue<int>(data, form);
  2920. break;
  2921. case DW_AT_address_class:
  2922. /*TODO:*/ ReadValue<int>(data, form);
  2923. break;
  2924. case DW_AT_artificial:
  2925. atArtificial = ReadValue<int>(data, form);
  2926. break;
  2927. case DW_AT_base_types:
  2928. /*TODO:*/ ReadValue<int>(data, form);
  2929. break;
  2930. case DW_AT_calling_convention:
  2931. /*TODO:*/ ReadValue<int>(data, form);
  2932. break;
  2933. case DW_AT_count:
  2934. atCount = ReadValue<uint>(data, form);
  2935. break;
  2936. case DW_AT_data_member_location:
  2937. if (form == DW_FORM_exprloc)
  2938. {
  2939. atDataMemberLocation = (int)ReadValue<uint>(data, form, dataOfs, &atDataMemberData);
  2940. hadMemberLocation = true;
  2941. }
  2942. else
  2943. {
  2944. atDataMemberLocation = (int)ReadValue<uint>(data, form);
  2945. hadMemberLocation = true;
  2946. }
  2947. break;
  2948. case DW_AT_decl_column:
  2949. /*TODO:*/ ReadValue<uint32>(data, form);
  2950. break;
  2951. case DW_AT_decl_file:
  2952. atDeclFile = ReadValue<uint32>(data, form);
  2953. break;
  2954. case DW_AT_decl_line:
  2955. atDeclLine = ReadValue<uint32>(data, form);
  2956. break;
  2957. case DW_AT_declaration:
  2958. atDeclaration = ReadValue<bool>(data, form);
  2959. break;
  2960. case DW_AT_discr_list:
  2961. /*TODO:*/ ReadValue<int>(data, form);
  2962. break;
  2963. case DW_AT_encoding:
  2964. atEncoding = ReadValue<int>(data, form);
  2965. break;
  2966. case DW_AT_external:
  2967. atExternal = ReadValue<int>(data, form);
  2968. break;
  2969. case DW_AT_frame_base:
  2970. atFrameBaseLength = (int64_t)ReadValue<uint64_t>(data, form, dataOfs, &atFrameBase);
  2971. break;
  2972. case DW_AT_friend:
  2973. /*TODO:*/ ReadValue<int>(data, form);
  2974. break;
  2975. case DW_AT_identifier_case:
  2976. /*TODO:*/ ReadValue<int>(data, form);
  2977. break;
  2978. case DW_AT_macro_info:
  2979. /*TODO:*/ ReadValue<int>(data, form);
  2980. break;
  2981. case DW_AT_namelist_item:
  2982. /*TODO:*/ ReadValue<int>(data, form);
  2983. break;
  2984. case DW_AT_priority:
  2985. /*TODO:*/ ReadValue<int>(data, form);
  2986. break;
  2987. case DW_AT_segment:
  2988. /*TODO:*/ ReadValue<int>(data, form);
  2989. break;
  2990. case DW_AT_specification:
  2991. atSpecification = ReadValue<int>(data, form, dataOfs);
  2992. break;
  2993. case DW_AT_static_link:
  2994. /*TODO:*/ ReadValue<int>(data, form);
  2995. break;
  2996. case DW_AT_type:
  2997. atType = ReadValue<int>(data, form, dataOfs);
  2998. break;
  2999. case DW_AT_use_location:
  3000. /*TODO:*/ ReadValue<int>(data, form);
  3001. break;
  3002. case DW_AT_variable_parameter:
  3003. /*TODO:*/ ReadValue<int>(data, form);
  3004. break;
  3005. case DW_AT_virtuality:
  3006. atVirtual = ReadValue<int>(data, form) != 0;
  3007. break;
  3008. case DW_AT_vtable_elem_location:
  3009. ReadValue<uint64_t>(data, form, dataOfs, &atVirtualLocData);
  3010. break;
  3011. case DW_AT_allocated:
  3012. /*TODO:*/ ReadValue<int>(data, form);
  3013. break;
  3014. case DW_AT_associated:
  3015. /*TODO:*/ ReadValue<int>(data, form);
  3016. break;
  3017. case DW_AT_data_location:
  3018. /*TODO:*/ ReadValue<int>(data, form);
  3019. break;
  3020. case DW_AT_byte_stride:
  3021. /*TODO:*/ ReadValue<int>(data, form);
  3022. break;
  3023. case DW_AT_entry_pc:
  3024. /*TODO:*/ ReadValue<int>(data, form);
  3025. break;
  3026. case DW_AT_use_UTF8:
  3027. /*TODO:*/ ReadValue<int>(data, form);
  3028. break;
  3029. case DW_AT_extension:
  3030. /*TODO:*/ ReadValue<int>(data, form);
  3031. break;
  3032. case DW_AT_ranges:
  3033. atRanges = (int)ReadValue<uint>(data, form);
  3034. hasRanges = true;
  3035. break;
  3036. case DW_AT_trampoline:
  3037. /*TODO:*/ ReadValue<int>(data, form);
  3038. break;
  3039. case DW_AT_call_column:
  3040. /*TODO:*/ ReadValue<int>(data, form);
  3041. break;
  3042. case DW_AT_call_file:
  3043. atCallFile = ReadValue<uint32>(data, form);
  3044. break;
  3045. case DW_AT_call_line:
  3046. atCallLine = ReadValue<uint32>(data, form);
  3047. break;
  3048. case DW_AT_description:
  3049. /*TODO:*/ ReadValue<int>(data, form);
  3050. break;
  3051. case DW_AT_binary_scale:
  3052. /*TODO:*/ ReadValue<int>(data, form);
  3053. break;
  3054. case DW_AT_decimal_scale:
  3055. /*TODO:*/ ReadValue<int>(data, form);
  3056. break;
  3057. case DW_AT_small:
  3058. /*TODO:*/ ReadValue<int>(data, form);
  3059. break;
  3060. case DW_AT_decimal_sign:
  3061. /*TODO:*/ ReadValue<int>(data, form);
  3062. break;
  3063. case DW_AT_digit_count:
  3064. /*TODO:*/ ReadValue<int>(data, form);
  3065. break;
  3066. case DW_AT_picture_string:
  3067. /*TODO:*/ ReadValue<int>(data, form);
  3068. break;
  3069. case DW_AT_mutable:
  3070. /*TODO:*/ ReadValue<int>(data, form);
  3071. break;
  3072. case DW_AT_threads_scaled:
  3073. /*TODO:*/ ReadValue<int>(data, form);
  3074. break;
  3075. case DW_AT_explicit:
  3076. /*TODO:*/ ReadValue<int>(data, form);
  3077. break;
  3078. case DW_AT_object_pointer:
  3079. atObjectPointer = ReadValue<int>(data, form);
  3080. break;
  3081. case DW_AT_endianity:
  3082. /*TODO:*/ ReadValue<int>(data, form);
  3083. break;
  3084. case DW_AT_elemental:
  3085. /*TODO:*/ ReadValue<int>(data, form);
  3086. break;
  3087. case DW_AT_pure:
  3088. /*TODO:*/ ReadValue<int>(data, form);
  3089. break;
  3090. case DW_AT_recursive:
  3091. /*TODO:*/ ReadValue<int>(data, form);
  3092. break;
  3093. case DW_AT_signature:
  3094. /*TODO:*/ ReadValue<int>(data, form);
  3095. break;
  3096. case DW_AT_main_subprogram:
  3097. /*TODO:*/ ReadValue<int>(data, form);
  3098. break;
  3099. case DW_AT_data_bit_offset:
  3100. /*TODO:*/ ReadValue<int>(data, form);
  3101. break;
  3102. case DW_AT_const_expr:
  3103. /*TODO:*/ ReadValue<int>(data, form);
  3104. break;
  3105. case DW_AT_enum_class:
  3106. /*TODO:*/ ReadValue<int>(data, form);
  3107. break;
  3108. case DW_AT_linkage_name:
  3109. atLinkageName = ReadValue<const char*>(data, form);
  3110. break;
  3111. //
  3112. case DW_AT_MIPS_linkage_name:
  3113. atLinkageName = ReadValue<const char*>(data, form);
  3114. break;
  3115. case DW_AT_APPLE_optimized:
  3116. isOptimized = ReadValue<bool>(data, form);
  3117. break;
  3118. default:
  3119. ReadValue<int>(data, form);
  3120. break;
  3121. }
  3122. }
  3123. if ((hasRanges) && (atLowPC == 0))
  3124. {
  3125. addr_target* rangeData = (addr_target*)(mDebugRangesData + atRanges);
  3126. while (true)
  3127. {
  3128. addr_target lowPC = *(rangeData++);
  3129. if (lowPC == 0)
  3130. break;
  3131. addr_target highPC = *(rangeData++);
  3132. if (compileUnit->mLowPC != (addr_target)-1)
  3133. {
  3134. // These are sometimes relative to the compile unit and sometimes absolute
  3135. if (highPC + compileUnit->mLowPC <= compileUnit->mHighPC)
  3136. {
  3137. lowPC += compileUnit->mLowPC;
  3138. highPC += compileUnit->mLowPC;
  3139. }
  3140. }
  3141. highPC -= lowPC;
  3142. // Select the largest range. We have some cases where some hoisting and such will
  3143. // give us a small inlining aberration much earlier than expected so this ignores that
  3144. if ((int64)highPC > atHighPC)
  3145. {
  3146. atLowPC = lowPC;
  3147. atHighPC = highPC;
  3148. }
  3149. /*if ((atLowPC == 0) || (lowPC < (addr_target)atLowPC))
  3150. atLowPC = lowPC;
  3151. if (highPC > (addr_target)atHighPC)
  3152. atHighPC = highPC;*/
  3153. }
  3154. }
  3155. switch (entryTag)
  3156. {
  3157. case DW_TAG_compile_unit:
  3158. {
  3159. newDataPair = MakeDataPair(compileUnit);
  3160. compileUnit->mName = atName;
  3161. compileUnit->mProducer = atProducer;
  3162. if (atCompDir != NULL)
  3163. compileUnit->mCompileDir = atCompDir;
  3164. if (atLowPC != 0)
  3165. {
  3166. compileUnit->mLowPC = (addr_target)atLowPC;
  3167. compileUnit->mHighPC = (addr_target)(atLowPC + atHighPC);
  3168. }
  3169. if (compileUnit->mProducer.IndexOf("Beef") != -1)
  3170. {
  3171. compileUnit->mLanguage = DbgLanguage_Beef;
  3172. }
  3173. else
  3174. {
  3175. compileUnit->mLanguage = DbgLanguage_C;
  3176. }
  3177. compileUnit->mGlobalType->mLanguage = compileUnit->mLanguage;
  3178. }
  3179. break;
  3180. case DW_TAG_imported_module:
  3181. {
  3182. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3183. DbgType* importType = GetOrCreateType(atImport, dataMap);
  3184. if (parentType != NULL) // Parent type is NULL for Clang DbgModule info
  3185. parentType->mUsingNamespaces.PushFront(importType, &mAlloc);
  3186. }
  3187. break;
  3188. case DW_TAG_inlined_subroutine:
  3189. case DW_TAG_subprogram:
  3190. {
  3191. /*//TODO: This is a test. See if it breaks anything.
  3192. if ((atExternal != 0) && (atLowPC == 0))
  3193. break;*/
  3194. if (atSpecification == 0)
  3195. {
  3196. subProgram = GetOrCreate<DbgSubprogram*>(tagIdx, dataMap);
  3197. subProgram->mCompileUnit = compileUnit;
  3198. subProgram->mVirtual = atVirtual;
  3199. subProgram->mIsOptimized = isOptimized;
  3200. if (atVirtualLocData != NULL)
  3201. {
  3202. const uint8* opPtr = atVirtualLocData;
  3203. if (*(opPtr++) == DW_OP_constu)
  3204. {
  3205. subProgram->mVTableLoc = (int)DecodeSLEB128(opPtr) * sizeof(addr_target);
  3206. }
  3207. }
  3208. //subProgram->mVTableLoc = atVirtualLoc * sizeof(addr_target);
  3209. //SplitName(atName, subProgram->mName, subProgram->mTemplateName);
  3210. subProgram->mName = atName;
  3211. subProgram->mLinkName = atLinkageName;
  3212. if (atAbstractOrigin != NULL)
  3213. {
  3214. DbgSubprogram* originSubProgram = GetOrCreate<DbgSubprogram*>(atAbstractOrigin, dataMap);
  3215. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgSubprogram::ClassType, subProgram, originSubProgram);
  3216. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3217. }
  3218. subProgram->mParentType = GetStackTop<DbgType*>(&dataStack);
  3219. newDataPair = MakeDataPair(subProgram);
  3220. //if ((atLinkageName != NULL) && (subProgram->mParentType != NULL))
  3221. //subProgram->mParentType->mDefinedMembersCount++;
  3222. mSubprograms.push_back(subProgram);
  3223. if (subProgram->mParentType != NULL)
  3224. {
  3225. subProgram->mParentType->mMethodList.PushBack(subProgram);
  3226. }
  3227. else
  3228. {
  3229. compileUnit->mGlobalType->mMethodList.PushBack(subProgram);
  3230. }
  3231. }
  3232. else
  3233. {
  3234. subProgram = dataMap.Get<DbgSubprogram*>(atSpecification);
  3235. BF_ASSERT(subProgram != NULL);
  3236. // We remove params form the declaration and re-add the real ones here
  3237. subProgram->mParams.Clear();
  3238. }
  3239. newDataPair = MakeDataPair(subProgram);
  3240. DbgBlock* dwBlock = &subProgram->mBlock;
  3241. if (atType != 0)
  3242. subProgram->mReturnType = GetOrCreateType(atType, dataMap);
  3243. if (!atDeclaration)
  3244. {
  3245. dwBlock->mLowPC = (addr_target)atLowPC;
  3246. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3247. if (dwBlock->mLowPC != 0)
  3248. {
  3249. compileUnit->mLowPC = std::min(compileUnit->mLowPC, dwBlock->mLowPC);
  3250. compileUnit->mHighPC = std::max(compileUnit->mHighPC, dwBlock->mHighPC);
  3251. }
  3252. if (atObjectPointer != 0)
  3253. subProgram->mHasThis = true;
  3254. subProgram->mFrameBaseLen = (int)atFrameBaseLength;
  3255. subProgram->mFrameBaseData = atFrameBase;
  3256. if (atHighPC > 0)
  3257. {
  3258. MapSubprogram(subProgram);
  3259. }
  3260. }
  3261. if (entryTag == DW_TAG_inlined_subroutine)
  3262. {
  3263. DbgSubprogram* parentSubProgram = GetStackLast<DbgSubprogram*>(&dataStack);
  3264. subProgram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
  3265. subProgram->mInlineeInfo->mInlineParent = parentSubProgram;
  3266. subProgram->mInlineeInfo->mRootInliner = parentSubProgram->GetRootInlineParent();
  3267. subProgram->mFrameBaseData = parentSubProgram->mFrameBaseData;
  3268. subProgram->mFrameBaseLen = parentSubProgram->mFrameBaseLen;
  3269. }
  3270. //if (subProgram->mParentType != NULL)
  3271. //subProgram->mParentType->mDefinedMembersCount++;
  3272. }
  3273. break;
  3274. case DW_TAG_lexical_block:
  3275. {
  3276. DbgBlock* prevBlock = GetStackTop<DbgBlock*>(&dataStack);
  3277. DbgBlock* dwBlock = mAlloc.Alloc<DbgBlock>();
  3278. if (hasRanges)
  3279. {
  3280. dwBlock->mLowPC = -1;
  3281. dwBlock->mHighPC = (addr_target)atRanges;
  3282. }
  3283. else
  3284. {
  3285. dwBlock->mLowPC = (addr_target)atLowPC;
  3286. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3287. }
  3288. newDataPair = MakeDataPair(dwBlock);
  3289. prevBlock->mSubBlocks.PushBack(dwBlock);
  3290. }
  3291. break;
  3292. case DW_TAG_variable:
  3293. {
  3294. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3295. if (atName && !strncmp(atName, "__asmLines", 10))
  3296. {
  3297. const char* ptr = strchr(atName, '.');
  3298. if (!ptr)
  3299. break;
  3300. int declLine = atDeclLine;
  3301. Array<int> asmLines;
  3302. int curAsmLine = 0;
  3303. int curRunCount = 1; // initial value is starting line, with an assumed run count of 1
  3304. bool parity = true; // starting line is standalone; everything afterwards is in pairs
  3305. while (true)
  3306. {
  3307. ++ptr;
  3308. if (!*ptr)
  3309. break;
  3310. String s;
  3311. if (*ptr == '$')
  3312. {
  3313. ++ptr;
  3314. const char* dollarPtr = strchr(ptr, '$');
  3315. if (!dollarPtr)
  3316. break;
  3317. s = String(ptr, (int)(dollarPtr - ptr));
  3318. ptr = dollarPtr;
  3319. }
  3320. else
  3321. {
  3322. s += *ptr;
  3323. }
  3324. //int asmLine = atoi(s.c_str());
  3325. //asmLines.push_back(asmLine);
  3326. const char* sPtr = s.c_str();
  3327. int decodedValue = (int)DecodeULEB32(sPtr);
  3328. if (!parity)
  3329. {
  3330. curRunCount = decodedValue;
  3331. }
  3332. else
  3333. {
  3334. for (int iLine=0; iLine<curRunCount; ++iLine)
  3335. {
  3336. curAsmLine += decodedValue;
  3337. asmLines.push_back(curAsmLine);
  3338. }
  3339. }
  3340. parity = !parity;
  3341. }
  3342. BF_ASSERT(!parity);
  3343. if (dwBlock->mAsmDebugLineMap == NULL)
  3344. {
  3345. mAsmDebugLineMaps.resize(mAsmDebugLineMaps.size() + 1);
  3346. dwBlock->mAsmDebugLineMap = &mAsmDebugLineMaps.back();
  3347. }
  3348. auto mapIter = dwBlock->mAsmDebugLineMap->find(declLine);
  3349. if (mapIter != dwBlock->mAsmDebugLineMap->end())
  3350. {
  3351. auto& dstVec = mapIter->second;
  3352. dstVec.Reserve(dstVec.size() + asmLines.size());
  3353. //dstVec.insert(dstVec.end(), asmLines.begin(), asmLines.end());
  3354. if (!asmLines.IsEmpty())
  3355. dstVec.Insert(dstVec.size(), &asmLines[0], asmLines.size());
  3356. }
  3357. else
  3358. {
  3359. (*dwBlock->mAsmDebugLineMap)[declLine] = std::move(asmLines);
  3360. }
  3361. break;
  3362. }
  3363. bool addToGlobalVarMap = false;
  3364. bool isNewVariable = true;
  3365. DbgVariable* dbgVariable = NULL;
  3366. if (atSpecification != 0)
  3367. {
  3368. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3369. //BF_ASSERT(dbgVariable != NULL);
  3370. dbgVariable = GetOrCreate<DbgVariable*>(atSpecification, dataMap);
  3371. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3372. //BF_ASSERT(dbgVariable != NULL);
  3373. }
  3374. else if (dwBlock != NULL)
  3375. {
  3376. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3377. dwBlock->mVariables.PushBack(dbgVariable);
  3378. }
  3379. else
  3380. {
  3381. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3382. bool wantGlobal = true;
  3383. if (compileUnit->mLanguage == DbgLanguage_Beef)
  3384. {
  3385. // Don't show certain global variables in Beef -- that includes things like VTable data
  3386. if (atName[0] == '_')
  3387. wantGlobal = false;
  3388. }
  3389. if ((dbgType == NULL) && (wantGlobal))
  3390. {
  3391. /*DbgCompileUnit* topCompileUnit = GetStackTop<DbgCompileUnit*>(&dataStack);
  3392. if (topCompileUnit != NULL)
  3393. dbgType = &topCompileUnit->mGlobalType;*/
  3394. dbgType = linkedModule->mMasterCompileUnit->mGlobalType;
  3395. auto foundEntry = mGlobalVarMap.Find(atName);
  3396. if (foundEntry != NULL)
  3397. {
  3398. isNewVariable = false;
  3399. dbgVariable = foundEntry->mValue;
  3400. }
  3401. else
  3402. {
  3403. addToGlobalVarMap = true;
  3404. }
  3405. }
  3406. if (dbgVariable == NULL)
  3407. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3408. dbgVariable->mIsStatic = true;
  3409. //TODO: dbgType can be NULL. This only (apparently?) happens for DW_TAG_inlined_subroutine, which we don't handle right now...
  3410. if (dbgType != NULL)
  3411. {
  3412. BF_ASSERT(dbgType->IsNamespace() || (dbgType->mTypeCode == DbgType_Root));
  3413. if (isNewVariable)
  3414. dbgType->mMemberList.PushBack(dbgVariable);
  3415. }
  3416. }
  3417. if (dbgVariable != NULL)
  3418. {
  3419. if (atSpecification == 0)
  3420. {
  3421. dbgVariable->mIsParam = false;
  3422. dbgVariable->mName = atName;
  3423. dbgVariable->mConstValue = atConstValue;
  3424. dbgVariable->mType = GetOrCreateType(atType, dataMap);
  3425. dbgVariable->mIsConst = hadConstValue;
  3426. dbgVariable->mIsStatic = !hadMemberLocation;
  3427. dbgVariable->mIsExtern = atExternal != 0;
  3428. }
  3429. if (atLinkageName != NULL)
  3430. dbgVariable->mLinkName = atLinkageName;
  3431. dbgVariable->mLocationLen = (int8)atLocationLen;
  3432. dbgVariable->mLocationData = atLocationData;
  3433. dbgVariable->mCompileUnit = compileUnit;
  3434. /*if (dbgVariable->mIsStatic && !dbgVariable->mIsConst && (dbgVariable->mLocationLen > 0) && (dbgVariable->mIsExtern))
  3435. {
  3436. DbgAddrType addrType = DbgAddrType_Value;
  3437. //
  3438. addr_target valAddr = mDebugTarget->EvaluateLocation(dbgVariable->mCompileUnit->mDbgModule, NULL, dbgVariable->mLocationData, dbgVariable->mLocationLen, NULL, &addrType);
  3439. if ((addrType == DbgAddrType_Target) && (valAddr != 0))
  3440. {
  3441. dbgVariable->mStaticCachedAddr = valAddr;
  3442. if (dbgVariable->mLinkName != NULL)
  3443. mStaticVariables.push_back(dbgVariable);
  3444. }
  3445. else
  3446. dbgVariable->mIsStatic = false;
  3447. }*/
  3448. // We had to remove the above for hot loading, calculate the mStaticCachedAddr later. Just put into mStaticVariables for now
  3449. mStaticVariables.push_back(dbgVariable);
  3450. if (atAbstractOrigin != NULL)
  3451. {
  3452. DbgVariable* originVariable = GetOrCreate<DbgVariable*>(atAbstractOrigin, dataMap);
  3453. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgVariable::ClassType, dbgVariable, originVariable);
  3454. if (atAbstractOrigin < tagIdx)
  3455. abstractOriginEntry.Replace();
  3456. else
  3457. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3458. }
  3459. else if (dbgVariable->mName == NULL)
  3460. dbgVariable->mName = "_unnamed";
  3461. if (addToGlobalVarMap)
  3462. mGlobalVarMap.Insert(dbgVariable);
  3463. newDataPair = MakeDataPair(dbgVariable);
  3464. }
  3465. }
  3466. break;
  3467. case DW_TAG_formal_parameter:
  3468. {
  3469. DbgSubprogram* dwSubprogram = GetStackTop<DbgSubprogram*>(&dataStack);
  3470. if (dwSubprogram == NULL)
  3471. {
  3472. if ((atName == NULL) && (atAbstractOrigin == 0))
  3473. {
  3474. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3475. if ((dbgType == NULL) || (dbgType->mTypeCode != DbgType_Subroutine))
  3476. break;
  3477. //TODO: Add params to subroutine type
  3478. break;
  3479. }
  3480. break;
  3481. }
  3482. if ((dwSubprogram->mParams.IsEmpty()) && (dwSubprogram->mParentType != 0))
  3483. dwSubprogram->mParentType->mMethodsWithParamsCount++;
  3484. //DbgVariable* dbgVariable = mAlloc.Alloc<DbgVariable>();
  3485. DbgVariable* dwVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3486. dwSubprogram->mParams.PushBack(dwVariable);
  3487. if (atArtificial != 0)
  3488. {
  3489. dwSubprogram->mHasThis = true;
  3490. if (atName == NULL)
  3491. atName = "this";
  3492. }
  3493. dwVariable->mCompileUnit = compileUnit;
  3494. dwVariable->mIsParam = true;
  3495. dwVariable->mName = atName;
  3496. dwVariable->mLocationLen = (int)atLocationLen;
  3497. dwVariable->mLocationData = atLocationData;
  3498. dwVariable->mType = GetOrCreateType(atType, dataMap);
  3499. if (atAbstractOrigin != 0)
  3500. {
  3501. }
  3502. }
  3503. break;
  3504. case DW_TAG_enumerator:
  3505. {
  3506. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3507. member->mCompileUnit = compileUnit;
  3508. member->mConstValue = atConstValue;
  3509. member->mName = atName;
  3510. member->mIsStatic = true;
  3511. member->mIsConst = true;
  3512. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3513. parentType->mMemberList.PushBack(member);
  3514. member->mMemberOffset = atDataMemberLocation;
  3515. //member->mType = parentType->mTypeParam;
  3516. member->mType = parentType;
  3517. // Insert into parent's namespace
  3518. auto prevTop = dataStack.back();
  3519. dataStack.pop_back();
  3520. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3521. dataStack.push_back(prevTop);
  3522. if (dwBlock != NULL)
  3523. {
  3524. DbgVariable* dwVariable = mAlloc.Alloc<DbgVariable>();
  3525. dwBlock->mVariables.PushBack(dwVariable);
  3526. if (atSpecification == 0)
  3527. {
  3528. dwVariable->mIsParam = false;
  3529. dwVariable->mName = atName;
  3530. dwVariable->mConstValue = atConstValue;
  3531. dwVariable->mType = parentType->mTypeParam;
  3532. dwVariable->mIsConst = hadConstValue;
  3533. dwVariable->mIsStatic = !hadMemberLocation;
  3534. }
  3535. dwVariable->mLocationLen = (int)atLocationLen;
  3536. dwVariable->mLocationData = atLocationData;
  3537. dwVariable->mCompileUnit = compileUnit;
  3538. BF_ASSERT(dwVariable->mName != 0);
  3539. newDataPair = MakeDataPair(dwVariable);
  3540. }
  3541. }
  3542. break;
  3543. /*case DW_TAG_subrange_type:
  3544. {
  3545. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3546. parentType->mArraySize = atUpperBound;
  3547. }
  3548. break;*/
  3549. case DW_TAG_inheritance:
  3550. {
  3551. DbgType* derivedType = GetStackTop<DbgType*>(&dataStack);
  3552. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  3553. baseTypeEntry->mBaseType = GetOrCreateType(atType, dataMap);
  3554. if (atDataMemberData != NULL)
  3555. {
  3556. bool foundVirtOffset = false;
  3557. const uint8* opPtr = atDataMemberData;
  3558. if (*(opPtr++) == DW_OP_dup)
  3559. {
  3560. if (*(opPtr++) == DW_OP_deref)
  3561. {
  3562. if (*(opPtr++) == DW_OP_constu)
  3563. {
  3564. baseTypeEntry->mVTableOffset = (int)DecodeSLEB128(opPtr) / sizeof(int32);
  3565. foundVirtOffset = true;
  3566. if (*(opPtr++) == DW_OP_minus)
  3567. baseTypeEntry->mVTableOffset = -baseTypeEntry->mVTableOffset;
  3568. }
  3569. }
  3570. }
  3571. BF_ASSERT(foundVirtOffset);
  3572. }
  3573. else
  3574. baseTypeEntry->mThisOffset = atDataMemberLocation;
  3575. derivedType->mBaseTypes.PushBack(baseTypeEntry);
  3576. }
  3577. break;
  3578. case DW_TAG_member:
  3579. {
  3580. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3581. if ((atName != NULL) && (strncmp(atName, "_vptr$", 6) == 0))
  3582. {
  3583. parentType->mHasVTable = true;
  3584. break;
  3585. }
  3586. //DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3587. DbgVariable* member = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3588. member->mIsMember = true;
  3589. member->mCompileUnit = compileUnit;
  3590. member->mName = atName;
  3591. member->mType = GetOrCreateType(atType, dataMap);
  3592. member->mConstValue = atConstValue;
  3593. member->mIsConst = hadConstValue;
  3594. member->mIsStatic = !hadMemberLocation;
  3595. member->mBitSize = atBitSize;
  3596. member->mBitOffset = atBitOffset;
  3597. member->mIsExtern = atExternal != 0;
  3598. parentType->mMemberList.PushBack(member);
  3599. member->mMemberOffset = atDataMemberLocation;
  3600. if ((member->mIsStatic) && (!member->mIsConst))
  3601. parentType->mHasStaticMembers = true;
  3602. /*if ((member->mIsStatic) && (!member->mIsConst))
  3603. mStaticVariables.push_back(member);*/
  3604. newDataPair = MakeDataPair(member);
  3605. //dataMap.Set(tagIdx, member);
  3606. }
  3607. break;
  3608. case DW_TAG_subrange_type:
  3609. {
  3610. int typeIdx = (int)(tagDataStart - startData);
  3611. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3612. int arrSize = atCount;
  3613. deferredArrayDims.push_back(arrSize);
  3614. }
  3615. break;
  3616. case DW_TAG_namespace:
  3617. case DW_TAG_const_type:
  3618. case DW_TAG_base_type:
  3619. case DW_TAG_pointer_type:
  3620. case DW_TAG_ptr_to_member_type:
  3621. case DW_TAG_array_type:
  3622. case DW_TAG_reference_type:
  3623. case DW_TAG_rvalue_reference_type:
  3624. case DW_TAG_unspecified_type:
  3625. case DW_TAG_class_type:
  3626. case DW_TAG_enumeration_type:
  3627. case DW_TAG_structure_type:
  3628. case DW_TAG_union_type:
  3629. case DW_TAG_typedef:
  3630. case DW_TAG_volatile_type:
  3631. case DW_TAG_subroutine_type:
  3632. //case DW_TAG_subrange_type:
  3633. case DW_TAG_restrict_type:
  3634. {
  3635. int typeIdx = (int)(tagDataStart - startData);
  3636. DbgType* dbgType = GetOrCreateType(typeIdx, dataMap);
  3637. const char* nameSep = (compileUnit->mLanguage == DbgLanguage_Beef) ? "." : "::";
  3638. if ((atName != NULL) &&
  3639. ((entryTag == DW_TAG_structure_type) || (entryTag == DW_TAG_class_type) ||
  3640. (entryTag == DW_TAG_typedef) || (entryTag == DW_TAG_union_type) || (entryTag == DW_TAG_enumeration_type) ||
  3641. (entryTag == DW_TAG_namespace)))
  3642. {
  3643. BF_ASSERT(dbgType->mTypeCode == DbgType_Null);
  3644. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3645. if (parentType != NULL)
  3646. {
  3647. dbgType->mParent = parentType;
  3648. dbgType->mParent->mSubTypeList.PushBack(dbgType);
  3649. /*if (dbgType->mParent->mName != NULL)
  3650. {
  3651. if (atName == NULL)
  3652. {
  3653. dbgType->mName = dbgType->mParent->mName; // Extend from name of parent if we're anonymous
  3654. }
  3655. else
  3656. {
  3657. int nameSepLen = strlen(nameSep);
  3658. int parentNameLen = strlen(dbgType->mParent->mName);
  3659. int nameLen = strlen(atName);
  3660. char* name = (char*)mAlloc.AllocBytes(parentNameLen + nameSepLen + nameLen + 1);
  3661. memcpy(name, dbgType->mParent->mName, parentNameLen);
  3662. memcpy(name + parentNameLen, nameSep, nameSepLen);
  3663. memcpy(name + parentNameLen + nameSepLen, atName, nameLen);
  3664. dbgType->mName = name;
  3665. }
  3666. }*/
  3667. }
  3668. else
  3669. {
  3670. // Add to global subtype list but don't set dbgType->mParent
  3671. compileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
  3672. }
  3673. }
  3674. const char* useName = atName;
  3675. /*if ((useName != NULL) && (strcmp(useName, "@") == 0))
  3676. useName = NULL;*/
  3677. dbgType->mCompileUnit = compileUnit;
  3678. dbgType->mLanguage = compileUnit->mLanguage;
  3679. //SplitName(atName, dbgType->mTypeName, dbgType->mTemplateParams);
  3680. dbgType->mName = useName;
  3681. if (dbgType->mTypeName == NULL)
  3682. dbgType->mTypeName = useName;
  3683. //if (dbgType->mName == NULL)
  3684. //dbgType->mName = atName;
  3685. int parentNameLen = ((dbgType->mParent != NULL) && (dbgType->mParent->mName != NULL)) ? (int)strlen(dbgType->mParent->mName) : 0;
  3686. int typeNameLen = (dbgType->mTypeName != NULL) ? (int)strlen(dbgType->mTypeName) : 0;
  3687. //int templateParamsLen = (dbgType->mTemplateParams != NULL) ? strlen(dbgType->mTemplateParams) : 0;
  3688. if ((parentNameLen != 0) /*&& (templateParamsLen == 0)*/)
  3689. {
  3690. int nameSepLen = (int)strlen(nameSep);
  3691. int nameLen = parentNameLen + typeNameLen /*+ templateParamsLen*/;
  3692. if ((parentNameLen > 0) && (nameLen > 0))
  3693. nameLen += nameSepLen;
  3694. char* namePtr = (char*)mAlloc.AllocBytes(nameLen + 1, "DWARF");
  3695. dbgType->mName = namePtr;
  3696. if (parentNameLen > 0)
  3697. {
  3698. memcpy(namePtr, dbgType->mParent->mName, parentNameLen);
  3699. namePtr += parentNameLen;
  3700. if (nameLen > 0)
  3701. {
  3702. memcpy(namePtr, nameSep, nameSepLen);
  3703. namePtr += nameSepLen;
  3704. }
  3705. }
  3706. if (nameLen > 0)
  3707. {
  3708. memcpy(namePtr, useName, typeNameLen);
  3709. namePtr += typeNameLen;
  3710. }
  3711. /*if (templateParamsLen > 0)
  3712. {
  3713. memcpy(namePtr, dbgType->mTemplateParams, templateParamsLen);
  3714. namePtr += templateParamsLen;
  3715. }*/
  3716. }
  3717. dbgType->mTypeCode = DbgType_Null;
  3718. dbgType->mIsDeclaration = atDeclaration;
  3719. if (atByteSize != -1)
  3720. {
  3721. dbgType->mSize = atByteSize;
  3722. dbgType->mSizeCalculated = true;
  3723. }
  3724. switch (entryTag)
  3725. {
  3726. case DW_TAG_base_type:
  3727. // Types that may do fallover to int/uints on size mismatch
  3728. switch (atEncoding)
  3729. {
  3730. case DW_ATE_UTF:
  3731. if (atByteSize == 1)
  3732. dbgType->mTypeCode = DbgType_Utf8;
  3733. else if (atByteSize == 2)
  3734. dbgType->mTypeCode = DbgType_Utf16;
  3735. else
  3736. dbgType->mTypeCode = DbgType_Utf32;
  3737. break;
  3738. case DW_ATE_signed_char:
  3739. if (atByteSize == 1)
  3740. dbgType->mTypeCode = DbgType_SChar;
  3741. else if (atByteSize == 2)
  3742. dbgType->mTypeCode = DbgType_SChar16;
  3743. else if (atByteSize == 4)
  3744. dbgType->mTypeCode = DbgType_SChar32;
  3745. else
  3746. atEncoding = DW_ATE_signed;
  3747. break;
  3748. case DW_ATE_unsigned_char:
  3749. if (atByteSize == 1)
  3750. dbgType->mTypeCode = DbgType_UChar;
  3751. else if (atByteSize == 2)
  3752. dbgType->mTypeCode = DbgType_UChar16;
  3753. else if (atByteSize == 4)
  3754. dbgType->mTypeCode = DbgType_UChar32;
  3755. atEncoding = DW_ATE_unsigned;
  3756. break;
  3757. case DW_ATE_boolean:
  3758. if (atByteSize == 1)
  3759. dbgType->mTypeCode = DbgType_Bool;
  3760. else
  3761. atEncoding = DW_ATE_unsigned;
  3762. break;
  3763. }
  3764. if (dbgType->mTypeCode == DbgType_Null)
  3765. {
  3766. switch (atEncoding)
  3767. {
  3768. case DW_ATE_address:
  3769. if (atByteSize == 0)
  3770. dbgType->mTypeCode = DbgType_Void;
  3771. break;
  3772. case DW_ATE_boolean:
  3773. if (atByteSize == 1)
  3774. {
  3775. dbgType->mTypeCode = DbgType_Bool;
  3776. break;
  3777. }
  3778. //Fall through
  3779. case DW_ATE_signed:
  3780. switch (atByteSize)
  3781. {
  3782. case 1:
  3783. dbgType->mTypeCode = DbgType_i8;
  3784. break;
  3785. case 2:
  3786. dbgType->mTypeCode = DbgType_i16;
  3787. break;
  3788. case 4:
  3789. dbgType->mTypeCode = DbgType_i32;
  3790. break;
  3791. case 8:
  3792. dbgType->mTypeCode = DbgType_i64;
  3793. break;
  3794. case 16:
  3795. dbgType->mTypeCode = DbgType_i128;
  3796. break;
  3797. }
  3798. break;
  3799. case DW_ATE_unsigned:
  3800. switch (atByteSize)
  3801. {
  3802. case 1:
  3803. dbgType->mTypeCode = DbgType_u8;
  3804. break;
  3805. case 2:
  3806. if ((atName != NULL) && (strcmp(atName, "wchar_t") == 0))
  3807. dbgType->mTypeCode = DbgType_UChar16;
  3808. else
  3809. dbgType->mTypeCode = DbgType_u16;
  3810. break;
  3811. case 4:
  3812. dbgType->mTypeCode = DbgType_u32;
  3813. break;
  3814. case 8:
  3815. dbgType->mTypeCode = DbgType_u64;
  3816. break;
  3817. case 16:
  3818. dbgType->mTypeCode = DbgType_u128;
  3819. break;
  3820. }
  3821. break;
  3822. case DW_ATE_float:
  3823. if (atByteSize == 4)
  3824. dbgType->mTypeCode = DbgType_Single;
  3825. else if (atByteSize == 8)
  3826. dbgType->mTypeCode = DbgType_Double;
  3827. else if (atByteSize == 12)
  3828. dbgType->mTypeCode = DbgType_Float96;
  3829. else if (atByteSize == 16)
  3830. dbgType->mTypeCode = DbgType_Float128;
  3831. break;
  3832. case DW_ATE_complex_float:
  3833. if (atByteSize == 8)
  3834. dbgType->mTypeCode = DbgType_ComplexFloat;
  3835. else if (atByteSize == 16)
  3836. dbgType->mTypeCode = DbgType_ComplexDouble;
  3837. else if (atByteSize == 24)
  3838. dbgType->mTypeCode = DbgType_ComplexDouble96;
  3839. else if (atByteSize == 32)
  3840. dbgType->mTypeCode = DbgType_ComplexDouble128;
  3841. break;
  3842. default:
  3843. BF_FATAL("Unknown DW_ATE type");
  3844. break;
  3845. }
  3846. }
  3847. break;
  3848. case DW_TAG_enumeration_type: //TODO: Handle these differently
  3849. dbgType->mTypeCode = DbgType_Enum;
  3850. dbgType->mTypeParam = mAlloc.Alloc<DbgType>();
  3851. if (atByteSize == 8)
  3852. dbgType->mTypeParam->mTypeCode = DbgType_i64;
  3853. else if (atByteSize == 4)
  3854. dbgType->mTypeParam->mTypeCode = DbgType_i32;
  3855. else if (atByteSize == 2)
  3856. dbgType->mTypeParam->mTypeCode = DbgType_i16;
  3857. else if (atByteSize == 1)
  3858. dbgType->mTypeParam->mTypeCode = DbgType_i8;
  3859. else
  3860. {
  3861. BF_DBG_FATAL("Invalid enum type");
  3862. }
  3863. break;
  3864. case DW_TAG_namespace:
  3865. dbgType->mTypeCode = DbgType_Namespace;
  3866. break;
  3867. case DW_TAG_const_type:
  3868. dbgType->mTypeCode = DbgType_Const;
  3869. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3870. break;
  3871. case DW_TAG_rvalue_reference_type:
  3872. dbgType->mTypeCode = DbgType_RValueReference;
  3873. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3874. break;
  3875. case DW_TAG_unspecified_type:
  3876. dbgType->mTypeCode = DbgType_Unspecified;
  3877. dbgType->mTypeName = atName;
  3878. break;
  3879. case DW_TAG_reference_type:
  3880. dbgType->mTypeCode = DbgType_Ref;
  3881. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3882. break;
  3883. case DW_TAG_pointer_type:
  3884. dbgType->mTypeCode = DbgType_Ptr;
  3885. dbgType->mSize = sizeof(addr_target);
  3886. dbgType->mSizeCalculated = true;
  3887. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3888. if (dbgType->mTypeParam != NULL)
  3889. dbgType->mTypeParam->mPtrType = dbgType;
  3890. break;
  3891. case DW_TAG_ptr_to_member_type:
  3892. dbgType->mTypeCode = DbgType_PtrToMember;
  3893. dbgType->mSize = sizeof(addr_target);
  3894. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3895. if (dbgType->mTypeParam != NULL)
  3896. dbgType->mTypeParam->mPtrType = dbgType;
  3897. break;
  3898. case DW_TAG_array_type:
  3899. dbgType->mTypeCode = DbgType_SizedArray;
  3900. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3901. break;
  3902. case DW_TAG_structure_type:
  3903. dbgType->mTypeCode = DbgType_Struct;
  3904. break;
  3905. case DW_TAG_class_type:
  3906. dbgType->mTypeCode = DbgType_Class;
  3907. break;
  3908. case DW_TAG_union_type:
  3909. dbgType->mTypeCode = DbgType_Union;
  3910. break;
  3911. case DW_TAG_typedef:
  3912. dbgType->mTypeCode = DbgType_TypeDef;
  3913. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3914. break;
  3915. case DW_TAG_volatile_type:
  3916. dbgType->mTypeCode = DbgType_Volatile;
  3917. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3918. break;
  3919. case DW_TAG_subroutine_type:
  3920. dbgType->mTypeCode = DbgType_Subroutine;
  3921. if (atType != 0) // Return value
  3922. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3923. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  3924. break;
  3925. case DW_TAG_restrict_type:
  3926. dbgType->mTypeCode = DbgType_Restrict;
  3927. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3928. break;
  3929. }
  3930. newDataPair = MakeDataPair(dbgType);
  3931. }
  3932. break;
  3933. }
  3934. if (hasChildren)
  3935. dataStack.push_back(newDataPair);
  3936. }
  3937. for (auto& abstractOriginEntry : abstractOriginReplaceList)
  3938. abstractOriginEntry.Replace();
  3939. GetLinkedModule()->MapTypes(startingTypeIdx);
  3940. dataPtr = dataEnd;
  3941. return true;
  3942. }
  3943. void DbgModule::ParseDebugFrameData()
  3944. {
  3945. BP_ZONE("ParseDebugFrameData");
  3946. const uint8* data = mDebugFrameData;
  3947. if (data == NULL)
  3948. return;
  3949. mParsedFrameDescriptors = true;
  3950. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  3951. while (true)
  3952. {
  3953. addr_target relSectionAddr = (addr_target)(data - mDebugFrameData);
  3954. int length = GET(int);
  3955. if (length == 0)
  3956. break;
  3957. const uint8* dataEnd = data + length;
  3958. int cieID = GET(int);
  3959. if (cieID < 0)
  3960. {
  3961. BP_ALLOC_T(DwCommonFrameDescriptor);
  3962. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  3963. char version = GET(char);
  3964. commonFrameDescriptor->mDbgModule = this;
  3965. commonFrameDescriptor->mAugmentation = DataGetString(data);
  3966. if (version >= 4)
  3967. {
  3968. commonFrameDescriptor->mPointerSize = GET(int8);
  3969. commonFrameDescriptor->mSegmentSize = GET(int8);
  3970. }
  3971. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  3972. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  3973. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  3974. commonFrameDescriptor->mInstData = data;
  3975. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  3976. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  3977. if (version < 3)
  3978. commonFrameDescriptorMap[relSectionAddr] = commonFrameDescriptor;
  3979. else
  3980. commonFrameDescriptorMap[mDebugFrameAddress + relSectionAddr] = commonFrameDescriptor;
  3981. }
  3982. else
  3983. {
  3984. addr_target lowPC = GET(addr_target);
  3985. addr_target highPC = lowPC + GET(addr_target);
  3986. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)cieID];
  3987. BF_ASSERT(commonFrameDescriptor != NULL);
  3988. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  3989. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  3990. auto frameDescriptor = &resultPair.first->second;
  3991. //frameDescriptor->
  3992. frameDescriptor->mLowPC = lowPC;
  3993. frameDescriptor->mHighPC = highPC;
  3994. frameDescriptor->mInstData = data;
  3995. frameDescriptor->mInstLen = (int)(dataEnd - data);
  3996. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  3997. }
  3998. data = dataEnd;
  3999. }
  4000. }
  4001. void DbgModule::ParseEHFrameData()
  4002. {
  4003. const uint8* data = mEHFrameData;
  4004. if (data == NULL)
  4005. return;
  4006. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  4007. while (true)
  4008. {
  4009. addr_target sectionAddress = (addr_target)(data - mEHFrameData);
  4010. int length = GET(int);
  4011. if (length == 0)
  4012. break;
  4013. const uint8* dataEnd = data + length;
  4014. int cieID = GET(int);
  4015. if (cieID <= 0)
  4016. {
  4017. BP_ALLOC_T(DwCommonFrameDescriptor);
  4018. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  4019. char version = GET(char);
  4020. const char* augmentation = DataGetString(data);
  4021. commonFrameDescriptor->mDbgModule = this;
  4022. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  4023. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  4024. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  4025. commonFrameDescriptor->mAugmentation = augmentation;
  4026. if (*augmentation == 'z')
  4027. {
  4028. ++augmentation;
  4029. int augLen = (int)DecodeULEB128(data);
  4030. commonFrameDescriptor->mAugmentationLength = augLen;
  4031. const uint8* augEnd = data + augLen;
  4032. while (*augmentation != '\0')
  4033. {
  4034. if (*augmentation == 'R')
  4035. commonFrameDescriptor->mAddressPointerEncoding = (int) GET(uint8);
  4036. else if (*augmentation == 'P')
  4037. {
  4038. int encodingType = GET(uint8);
  4039. BF_ASSERT(encodingType == 0);
  4040. commonFrameDescriptor->mLSDARoutine = GET(addr_target);
  4041. }
  4042. else if (*augmentation == 'L')
  4043. commonFrameDescriptor->mLSDAPointerEncodingFDE = GET(uint8);
  4044. else if (*augmentation == 'S')
  4045. {
  4046. // mIsSignalHandler - on return from stack frame, CFA is before next instruction rather than after it
  4047. }
  4048. else
  4049. BF_FATAL("Unknown CIE augmentation");
  4050. ++augmentation;
  4051. }
  4052. data = augEnd;
  4053. }
  4054. commonFrameDescriptor->mInstData = data;
  4055. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  4056. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  4057. commonFrameDescriptorMap[sectionAddress] = commonFrameDescriptor;
  4058. }
  4059. else
  4060. {
  4061. int ciePos = (int)(sectionAddress - cieID) + 4;
  4062. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)ciePos];
  4063. addr_target lowPC;
  4064. addr_target highPC;
  4065. if (commonFrameDescriptor->mAddressPointerEncoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4))
  4066. {
  4067. lowPC = GET(int);
  4068. lowPC += mEHFrameAddress + sectionAddress + 8;
  4069. highPC = lowPC + GET(int);
  4070. }
  4071. else
  4072. {
  4073. lowPC = GET(int);
  4074. highPC = lowPC + GET(int);
  4075. }
  4076. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  4077. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  4078. auto frameDescriptor = &resultPair.first->second;
  4079. frameDescriptor->mLSDARoutine = commonFrameDescriptor->mLSDARoutine;
  4080. const char* augmentation = commonFrameDescriptor->mAugmentation;
  4081. if (*augmentation == 'z')
  4082. {
  4083. int augLen = GET(uint8);
  4084. const uint8* augEnd = data + augLen;
  4085. ++augmentation;
  4086. while (*augmentation != '\0')
  4087. {
  4088. if (*augmentation == 'R')
  4089. {
  4090. }
  4091. else if (*augmentation == 'P')
  4092. {
  4093. }
  4094. else if (*augmentation == 'L')
  4095. {
  4096. BF_ASSERT(commonFrameDescriptor->mLSDAPointerEncodingFDE == 0);
  4097. frameDescriptor->mLSDARoutine = GET(addr_target);
  4098. }
  4099. else if (*augmentation == 'S')
  4100. {
  4101. }
  4102. else
  4103. BF_FATAL("Unknown CIE augmentation");
  4104. augmentation++;
  4105. }
  4106. data = augEnd;
  4107. }
  4108. frameDescriptor->mLowPC = lowPC;
  4109. frameDescriptor->mHighPC = highPC;
  4110. frameDescriptor->mInstData = data;
  4111. frameDescriptor->mInstLen = (int)(dataEnd - data);
  4112. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  4113. }
  4114. data = dataEnd;
  4115. }
  4116. }
  4117. void DbgModule::FlushLineData(DbgSubprogram* curSubprogram, std::list<DbgLineData>& queuedLineData)
  4118. {
  4119. }
  4120. DbgSrcFile* DbgModule::AddSrcFile(DbgCompileUnit* compileUnit, const String& srcFilePath)
  4121. {
  4122. DbgSrcFile* dwSrcFile = mDebugTarget->AddSrcFile(srcFilePath);
  4123. if (compileUnit != NULL)
  4124. {
  4125. DbgSrcFileReference srcFileRef;
  4126. srcFileRef.mSrcFile = dwSrcFile;
  4127. srcFileRef.mCompileUnit = compileUnit;
  4128. compileUnit->mSrcFileRefs.push_back(srcFileRef);
  4129. }
  4130. return dwSrcFile;
  4131. }
  4132. bool DbgModule::ParseDebugLineInfo(const uint8*& dataPtr, int compileUnitIdx)
  4133. {
  4134. BP_ZONE("ParseDebugLineInfo");
  4135. const uint8* data = dataPtr;
  4136. const int startOffset = (int)(data - mDebugLineData);
  4137. int length = GET(int);
  4138. if (length == 0)
  4139. return false;
  4140. DbgCompileUnit* dwCompileUnit = mCompileUnits[compileUnitIdx];
  4141. const uint8* dataEnd = data + length;
  4142. short version = GET(short);
  4143. int headerLength = GET(int);
  4144. char minimumInstructionLength = GET(char);
  4145. int maximumOperationsPerInstruction = 1;
  4146. char defaultIsStmt = GET(char);
  4147. char lineBase = GET(char);
  4148. char lineRange = GET(char);
  4149. char opcodeBase = GET(char);
  4150. for (int i = 0; i < opcodeBase - 1; i++)
  4151. {
  4152. char standardOpcodeLengths = GET(char);
  4153. }
  4154. Array<const char*> directoryNames;
  4155. while (true)
  4156. {
  4157. const char* name = DataGetString(data);
  4158. if (name[0] == 0)
  4159. break;
  4160. directoryNames.push_back(name);
  4161. }
  4162. DbgSrcFileReference* dwSrcFileRef = NULL;
  4163. HashSet<String> foundPathSet;
  4164. int curFileIdx = 0;
  4165. DbgSubprogram* curSubprogram = NULL;
  4166. #define ADD_LINEDATA(lineData) \
  4167. lineBuilder.Add(dwCompileUnit, lineData, dwSrcFileRef->mSrcFile, NULL);
  4168. while (true)
  4169. {
  4170. const char* path = DataGetString(data);
  4171. if (path[0] == 0)
  4172. break;
  4173. int directoryIdx = (int)DecodeULEB128(data);
  4174. int lastModificationTime = (int)DecodeULEB128(data);
  4175. int fileLength = (int)DecodeULEB128(data);
  4176. String filePath;
  4177. if (directoryIdx > 0)
  4178. filePath = String(directoryNames[directoryIdx - 1]) + "/";
  4179. filePath += path;
  4180. filePath = GetAbsPath(filePath, dwCompileUnit->mCompileDir);
  4181. AddSrcFile(dwCompileUnit, filePath.c_str());
  4182. }
  4183. if (dwCompileUnit->mSrcFileRefs.size() > 0)
  4184. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs.front();
  4185. DbgLineDataBuilder lineBuilder(this);
  4186. bool queuedPostPrologue = false;
  4187. DbgLineDataState dwLineData;
  4188. dwLineData.mLine = 0;
  4189. dwLineData.mRelAddress = 0;
  4190. dwLineData.mOpIndex = 0;
  4191. dwLineData.mBasicBlock = false;
  4192. dwLineData.mDiscriminator = 0;
  4193. dwLineData.mIsStmt = defaultIsStmt != 0;
  4194. dwLineData.mIsa = 0;
  4195. dwLineData.mColumn = -2;
  4196. while (data < dataEnd)
  4197. {
  4198. uint8_t opcode = GET(uint8_t);
  4199. switch (opcode)
  4200. {
  4201. case DW_LNS_extended_op:
  4202. {
  4203. int len = (int)DecodeULEB128(data);
  4204. uint8_t exOpcode = GET(uint8_t);
  4205. switch (exOpcode)
  4206. {
  4207. case DW_LNE_end_sequence:
  4208. {
  4209. ADD_LINEDATA(dwLineData);
  4210. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[0];
  4211. dwLineData.mLine = 0;
  4212. dwLineData.mRelAddress = 0;
  4213. dwLineData.mOpIndex = 0;
  4214. dwLineData.mBasicBlock = false;
  4215. dwLineData.mDiscriminator = 0;
  4216. dwLineData.mIsStmt = defaultIsStmt != 0;
  4217. dwLineData.mIsa = 0;
  4218. dwLineData.mColumn = -2;
  4219. }
  4220. break;
  4221. case DW_LNE_set_address:
  4222. dwLineData.mRelAddress = (uint32)(RemapAddr(GET(addr_target)) - mImageBase);
  4223. break;
  4224. case DW_LNE_define_file:
  4225. {
  4226. const char* path = DataGetString(data);
  4227. int directoryIdx = (int)DecodeULEB128(data);
  4228. int lastModificationTime = (int)DecodeULEB128(data);
  4229. int fileLength = (int)DecodeULEB128(data);
  4230. }
  4231. break;
  4232. case DW_LNE_set_discriminator:
  4233. dwLineData.mDiscriminator = (int)DecodeULEB128(data);
  4234. break;
  4235. }
  4236. }
  4237. break;
  4238. case DW_LNS_copy:
  4239. ADD_LINEDATA(dwLineData);
  4240. dwLineData.mDiscriminator = 0;
  4241. dwLineData.mBasicBlock = false;
  4242. break;
  4243. case DW_LNS_advance_pc:
  4244. {
  4245. int advance = (int)DecodeULEB128(data);
  4246. dwLineData.mRelAddress += advance;
  4247. // How to advance opCode addr?
  4248. }
  4249. break;
  4250. case DW_LNS_advance_line:
  4251. {
  4252. int advance = (int)DecodeSLEB128(data);
  4253. dwLineData.mLine += advance;
  4254. }
  4255. break;
  4256. case DW_LNS_set_file:
  4257. {
  4258. curFileIdx = (int)DecodeULEB128(data) - 1;
  4259. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[curFileIdx];
  4260. //dwLineData.mSrcFileRef = dwSrcFileRef;
  4261. }
  4262. break;
  4263. case DW_LNS_set_column:
  4264. {
  4265. dwLineData.mColumn = (int)DecodeULEB128(data) - 1;
  4266. }
  4267. break;
  4268. case DW_LNS_negate_stmt:
  4269. {
  4270. dwLineData.mIsStmt = !dwLineData.mIsStmt;
  4271. }
  4272. break;
  4273. case DW_LNS_set_basic_block:
  4274. {
  4275. dwLineData.mBasicBlock = true;
  4276. }
  4277. break;
  4278. case DW_LNS_const_add_pc:
  4279. {
  4280. int adjustedOpcode = 255 - opcodeBase;
  4281. int opAdvance = adjustedOpcode / lineRange;
  4282. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4283. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4284. dwLineData.mRelAddress = newAddress;
  4285. dwLineData.mOpIndex = newOpIndex;
  4286. }
  4287. break;
  4288. case DW_LNS_fixed_advance_pc:
  4289. {
  4290. uint16_t advance = GET(uint16_t);
  4291. dwLineData.mRelAddress += advance;
  4292. dwLineData.mOpIndex = 0;
  4293. }
  4294. break;
  4295. case DW_LNS_set_prologue_end:
  4296. {
  4297. queuedPostPrologue = true;
  4298. }
  4299. break;
  4300. case DW_LNS_set_epilogue_begin:
  4301. {
  4302. dwLineData.mColumn = -2;
  4303. }
  4304. break;
  4305. case DW_LNS_set_isa:
  4306. {
  4307. dwLineData.mIsa = (int)DecodeULEB128(data);
  4308. }
  4309. break;
  4310. default:
  4311. {
  4312. // Special opcode
  4313. int adjustedOpcode = opcode - opcodeBase;
  4314. int opAdvance = adjustedOpcode / lineRange;
  4315. uint32 oldAddress = dwLineData.mRelAddress;
  4316. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4317. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4318. int lineIncrement = lineBase + (adjustedOpcode % lineRange);
  4319. dwLineData.mLine += lineIncrement;
  4320. dwLineData.mRelAddress = newAddress;
  4321. dwLineData.mOpIndex = newOpIndex;
  4322. DbgLineData* lastLineData = NULL;
  4323. if ((newAddress == oldAddress) && (queuedPostPrologue) && (curSubprogram != NULL) && (curSubprogram->mBlock.mLowPC == newAddress))
  4324. {
  4325. // Adjust this line later
  4326. ADD_LINEDATA(dwLineData);
  4327. }
  4328. queuedPostPrologue = false;
  4329. }
  4330. break;
  4331. }
  4332. }
  4333. lineBuilder.Commit();
  4334. dataPtr = data;
  4335. return true;
  4336. }
  4337. addr_target DbgModule::GetHotTargetAddress(DbgHotTargetSection* hotTargetSection)
  4338. {
  4339. if ((hotTargetSection->mTargetSectionAddr == NULL) && (hotTargetSection->mDataSize > 0))
  4340. {
  4341. if (hotTargetSection->mNoTargetAlloc)
  4342. return 0;
  4343. hotTargetSection->mTargetSectionAddr = mDebugger->AllocHotTargetMemory(hotTargetSection->mDataSize, hotTargetSection->mCanExecute, hotTargetSection->mCanWrite, &hotTargetSection->mTargetSectionSize);
  4344. hotTargetSection->mImageOffset = (int)mImageSize;
  4345. if (mImageBase == NULL)
  4346. {
  4347. mImageBase = hotTargetSection->mTargetSectionAddr;
  4348. mOrigImageData->mAddr = mImageBase;
  4349. }
  4350. mImageSize += hotTargetSection->mTargetSectionSize;
  4351. /*if (mExceptionData == hotTargetSection->mData)
  4352. mExceptionDataRVA = (addr_target)(hotTargetSection->mTargetSectionAddr - mImageBase);*/
  4353. }
  4354. return hotTargetSection->mTargetSectionAddr;
  4355. }
  4356. uint8* DbgModule::GetHotTargetData(addr_target address)
  4357. {
  4358. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4359. {
  4360. if (mHotTargetSections[sectNum] != NULL)
  4361. {
  4362. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4363. if ((address >= hotTargetSection->mTargetSectionAddr) && (address < hotTargetSection->mTargetSectionAddr + hotTargetSection->mTargetSectionSize))
  4364. {
  4365. return hotTargetSection->mData + (address - hotTargetSection->mTargetSectionAddr);
  4366. }
  4367. }
  4368. }
  4369. return NULL;
  4370. }
  4371. void DbgModule::DoReloc(DbgHotTargetSection* hotTargetSection, COFFRelocation& coffReloc, addr_target resolvedSymbolAddr, PE_SymInfo* symInfo)
  4372. {
  4373. #ifdef BF_DBG_32
  4374. if (coffReloc.mType == IMAGE_REL_I386_DIR32)
  4375. {
  4376. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4377. }
  4378. else if (coffReloc.mType == IMAGE_REL_I386_DIR32NB)
  4379. {
  4380. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4381. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4382. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4383. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4384. }
  4385. else if (coffReloc.mType == IMAGE_REL_I386_REL32)
  4386. {
  4387. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4388. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr - myAddr - sizeof(int32);
  4389. }
  4390. else if (coffReloc.mType == IMAGE_REL_I386_SECTION)
  4391. {
  4392. // auto linkedModule = GetLinkedModule();
  4393. // addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4394. // int* encodingPtr = NULL;
  4395. // if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4396. // {
  4397. // *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4398. // linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4399. // }
  4400. // *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4401. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0;
  4402. }
  4403. else if (coffReloc.mType == IMAGE_REL_I386_SECREL)
  4404. {
  4405. //*(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += symInfo->mValue;
  4406. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4407. }
  4408. else
  4409. {
  4410. BF_ASSERT(0=="Invalid COFF reloc type");
  4411. }
  4412. #else
  4413. // CodeView uses SECTION:SECREL locations, and we just want to find a mapping such that
  4414. // COFF::GetSectionAddr can map it to the 64-bit address. We do this by encoding the
  4415. // lower 31 bits in the SECREL (allowing a 31-bit offset at the destination as well)
  4416. // and then we use a 15-bit key to map the upper bits
  4417. if (coffReloc.mType == IMAGE_REL_AMD64_REL32)
  4418. {
  4419. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4420. intptr_target addrOffset = resolvedSymbolAddr - myAddr - sizeof(int32);
  4421. BF_ASSERT((int64)(int32)addrOffset == addrOffset);
  4422. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (int32)addrOffset;
  4423. }
  4424. else if (coffReloc.mType == IMAGE_REL_AMD64_SECTION)
  4425. {
  4426. /*if (symInfo != NULL)
  4427. {
  4428. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = symInfo->mSectionNum;
  4429. }
  4430. else*/
  4431. {
  4432. auto linkedModule = GetLinkedModule();
  4433. addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4434. /*auto pair = linkedModule->mSecRelEncodingMap.insert(std::make_pair(mappedAddr, (int)linkedModule->mSecRelEncodingMap.size()));
  4435. if (pair.second)
  4436. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);*/
  4437. int* encodingPtr = NULL;
  4438. if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4439. {
  4440. *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4441. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4442. }
  4443. //*(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | pair.first->second;
  4444. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4445. }
  4446. }
  4447. else if (coffReloc.mType == IMAGE_REL_AMD64_SECREL)
  4448. {
  4449. auto linkedModule = GetLinkedModule();
  4450. if ((resolvedSymbolAddr >= linkedModule->mTLSAddr) && (resolvedSymbolAddr < linkedModule->mTLSAddr + linkedModule->mTLSSize))
  4451. {
  4452. // Make relative to actual TLS data
  4453. resolvedSymbolAddr -= linkedModule->mTLSAddr;
  4454. }
  4455. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr & 0x7FFFFFF);
  4456. }
  4457. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR64)
  4458. {
  4459. *(uint64*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4460. }
  4461. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB)
  4462. {
  4463. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4464. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4465. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4466. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4467. //*(int32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += secRelAddr;
  4468. }
  4469. else
  4470. {
  4471. BF_ASSERT(0=="Invalid COFF reloc type");
  4472. }
  4473. #endif
  4474. }
  4475. bool DbgModule::IsHotSwapPreserve(const String& name)
  4476. {
  4477. // We have different rules for overwriting symbols in DWARF vs CodeView
  4478. // Since MS mangling includes return types, we know that a type change of a static
  4479. // member will mangle to a new name whereas with DWARF we DO want a new
  4480. // address if the type changes but we can't tell that based on the mangle alone,
  4481. // thus the reliance on the side table of mStaticVariables. We still do need
  4482. // to determine whether the symbol is data (and thus we do preserve) or a method
  4483. // (in which case we don't)
  4484. if ((mDbgFlavor == DbgFlavor_MS) && (BfDemangler::IsData(name)))
  4485. {
  4486. if ((!name.StartsWith("?")) && (name.Contains("sBfTypeData"))) // We DO need to replace the fields/methods/etc but not the base sBfTypeData
  4487. return false;
  4488. if (name.StartsWith("?bf_hs_replace_"))
  4489. return false;
  4490. return true;
  4491. }
  4492. const char* prefix = "bf_hs_preserve@";
  4493. return strncmp(name.c_str(), prefix, strlen(prefix)) == 0;
  4494. }
  4495. void DbgModule::ParseHotTargetSections(DataStream* stream, addr_target* resolvedSymbolAddrs)
  4496. {
  4497. auto mainModule = mDebugTarget->mTargetBinary;
  4498. mainModule->ParseSymbolData();
  4499. String name;
  4500. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4501. {
  4502. if (mHotTargetSections[sectNum] != NULL)
  4503. {
  4504. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4505. stream->SetPos(hotTargetSection->mPointerToRelocations);
  4506. for (int relocIdx = 0; relocIdx < hotTargetSection->mNumberOfRelocations; relocIdx++)
  4507. {
  4508. COFFRelocation coffReloc;
  4509. stream->Read(&coffReloc, sizeof(COFFRelocation));
  4510. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[coffReloc.mSymbolTableIndex * 18];
  4511. //const char* symName = mSymbolData[coffReloc.mSymbolTableIndex];
  4512. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  4513. if (symInfo->mNameOfs[0] != 0)
  4514. {
  4515. if (symInfo->mName[7] != 0)
  4516. {
  4517. // Name is exactly 8 chars, not null terminated yet
  4518. name = String(symInfo->mName, symInfo->mName + 8);
  4519. }
  4520. else
  4521. name = symInfo->mName;
  4522. }
  4523. else
  4524. name = mStringTable + symInfo->mNameOfs[1];
  4525. bool didNameMatch = false;
  4526. addr_target resolvedSymbolAddr = resolvedSymbolAddrs[coffReloc.mSymbolTableIndex];
  4527. #ifdef BF_DBG_32
  4528. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_I386_DIR32) || (coffReloc.mType == IMAGE_REL_I386_REL32) || (coffReloc.mType == IMAGE_REL_I386_SECREL) || (coffReloc.mType == IMAGE_REL_I386_SECTION);
  4529. if (name[0] == '_')
  4530. name.Remove(0, 1);
  4531. #else
  4532. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_AMD64_ADDR64) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB) ||
  4533. ((coffReloc.mType >= IMAGE_REL_AMD64_REL32) || (coffReloc.mType <= IMAGE_REL_AMD64_REL32_5));
  4534. #endif
  4535. bool isHsPrev = false;
  4536. if (name.StartsWith("bf_hs_prev@"))
  4537. {
  4538. isHsPrev = true;
  4539. name.Remove(0, 11);
  4540. }
  4541. bool deferResolve = false;
  4542. if ((resolvedSymbolAddr == 0) && (needsSymbolAddr))
  4543. {
  4544. bool isHotSwapPreserve = IsHotSwapPreserve(name);
  4545. if ((symInfo->mSectionNum == 0) || (isHotSwapPreserve) || (isHsPrev))
  4546. {
  4547. auto origSymbolEntry = mainModule->mSymbolNameMap.Find(name.c_str());
  4548. if (origSymbolEntry != NULL)
  4549. {
  4550. resolvedSymbolAddr = origSymbolEntry->mValue->mAddress;
  4551. }
  4552. else
  4553. {
  4554. //BF_FATAL("Symbol lookup error");
  4555. deferResolve = true;
  4556. }
  4557. }
  4558. if ((symInfo->mSectionNum != 0) && (resolvedSymbolAddr == NULL))
  4559. {
  4560. DbgHotTargetSection* refHotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  4561. resolvedSymbolAddr = GetHotTargetAddress(refHotTargetSection) + symInfo->mValue;
  4562. // Using the !hotTargetSection->mNoTargetAlloc check down here caused us to not properly remap reloaded
  4563. // static members in the debug info. Even though we parse the debug info before we apply the deferred
  4564. // resolves, the mLocData points into the original data so we still get it remapped when we use that
  4565. // mLocData
  4566. if (/*(!hotTargetSection->mNoTargetAlloc) &&*/ ((refHotTargetSection->mData == NULL) || (refHotTargetSection->mNoTargetAlloc)) &&
  4567. (!isStaticSymbol))
  4568. deferResolve = true;
  4569. else
  4570. deferResolve = false;
  4571. }
  4572. }
  4573. if (deferResolve)
  4574. {
  4575. // It's a static field, defer resolution, but don't bother replacing for debug info sections
  4576. DbgDeferredHotResolve* deferredResolve = mDeferredHotResolveList.Alloc();
  4577. deferredResolve->mHotTargetSection = hotTargetSection;
  4578. deferredResolve->mName = name;
  4579. deferredResolve->mNewAddr = resolvedSymbolAddr;
  4580. deferredResolve->mReloc = coffReloc;
  4581. continue;
  4582. }
  4583. else
  4584. {
  4585. resolvedSymbolAddrs[coffReloc.mSymbolTableIndex] = resolvedSymbolAddr;
  4586. DoReloc(hotTargetSection, coffReloc, resolvedSymbolAddr, symInfo);
  4587. }
  4588. }
  4589. }
  4590. }
  4591. }
  4592. void DbgModule::CommitHotTargetSections()
  4593. {
  4594. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4595. {
  4596. if (mHotTargetSections[sectNum] != NULL)
  4597. {
  4598. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4599. addr_target hotAddr = GetHotTargetAddress(hotTargetSection);
  4600. if (hotAddr != 0)
  4601. {
  4602. // void* imageDestPtr = mOrigImageData->mBlocks[0] + hotTargetSection->mImageOffset;
  4603. // if (hotTargetSection->mData != NULL)
  4604. // memcpy(imageDestPtr, hotTargetSection->mData, hotTargetSection->mDataSize);
  4605. // else
  4606. // memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4607. BF_ASSERT(mOrigImageData->mAddr != 0);
  4608. void* imageDestPtr = hotTargetSection->mData;
  4609. bool isTemp = false;
  4610. if (imageDestPtr == NULL)
  4611. {
  4612. imageDestPtr = new uint8[hotTargetSection->mDataSize];
  4613. memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4614. isTemp = true;
  4615. }
  4616. if (hotTargetSection->mCanExecute)
  4617. {
  4618. bool success = mDebugger->WriteInstructions(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4619. BF_ASSERT(success);
  4620. }
  4621. else
  4622. {
  4623. bool success = mDebugger->WriteMemory(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4624. BF_ASSERT(success);
  4625. }
  4626. if (isTemp)
  4627. delete imageDestPtr;
  4628. }
  4629. }
  4630. }
  4631. }
  4632. void DbgModule::HotReplaceType(DbgType* newType)
  4633. {
  4634. auto linkedModule = GetLinkedModule();
  4635. newType->PopulateType();
  4636. DbgType* primaryType = linkedModule->GetPrimaryType(newType);
  4637. if (primaryType == newType)
  4638. {
  4639. // There was no previous type
  4640. BF_ASSERT(primaryType->mHotNewType == NULL);
  4641. return;
  4642. }
  4643. if (primaryType->mHotNewType != newType)
  4644. {
  4645. // We have already pulled in the new data from a previous new type
  4646. BF_ASSERT(primaryType->mHotNewType == NULL);
  4647. return;
  4648. }
  4649. primaryType->mHotNewType = NULL;
  4650. primaryType->PopulateType();
  4651. linkedModule->ParseGlobalsData();
  4652. linkedModule->ParseSymbolData();
  4653. if (primaryType->mNeedsGlobalsPopulated)
  4654. {
  4655. // These aren't proper TPI types so we don't have any method declarations until we PopulateTypeGlobals
  4656. linkedModule->PopulateTypeGlobals(primaryType);
  4657. }
  4658. for (auto methodNameEntry : primaryType->mMethodNameList)
  4659. {
  4660. if (methodNameEntry->mCompileUnitId != -1)
  4661. {
  4662. linkedModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  4663. methodNameEntry->mCompileUnitId = -1;
  4664. }
  4665. }
  4666. // Now actually remove the linedata from the defining module
  4667. HashSet<DbgSrcFile*> checkedFiles;
  4668. for (auto method : primaryType->mMethodList)
  4669. {
  4670. //method->mWasModuleHotReplaced = true;
  4671. method->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Orphaned; // May be temporarily orphaned
  4672. if (method->mLineInfo == NULL)
  4673. continue;
  4674. //FIXME: Hot replacing lines
  4675. DbgSrcFile* lastSrcFile = NULL;
  4676. checkedFiles.Clear();
  4677. int prevCtx = -1;
  4678. auto inlineRoot = method->GetRootInlineParent();
  4679. for (int lineIdx = 0; lineIdx < method->mLineInfo->mLines.mSize; lineIdx++)
  4680. {
  4681. auto& lineData = method->mLineInfo->mLines[lineIdx];
  4682. if (lineData.mCtxIdx != prevCtx)
  4683. {
  4684. auto ctxInfo = inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx];
  4685. auto srcFile = ctxInfo.mSrcFile;
  4686. prevCtx = lineData.mCtxIdx;
  4687. if (srcFile != lastSrcFile)
  4688. {
  4689. if (checkedFiles.Add(srcFile))
  4690. {
  4691. // Remove linedata for old type
  4692. // These go into a hot-replaced list so we can still bind to them -- that is necessary because
  4693. // we may still have old versions of this method running (and may forever, if its in a loop on some thread)
  4694. // since we only patch entry points
  4695. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, primaryType->mCompileUnit, true);
  4696. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, method, true);
  4697. srcFile->RemoveLines(method->mCompileUnit->mDbgModule, method, true);
  4698. }
  4699. lastSrcFile = srcFile;
  4700. }
  4701. }
  4702. }
  4703. }
  4704. //DbgType* primaryType = newType->GetPrimaryType();
  4705. // We need to keep a persistent list of hot replaced methods so we can set hot jumps
  4706. // in old methods that may still be on the callstack. These entries get removed when
  4707. // we unload unused hot files in
  4708. while (!primaryType->mMethodList.IsEmpty())
  4709. {
  4710. auto method = primaryType->mMethodList.PopFront();
  4711. method->PopulateSubprogram();
  4712. primaryType->mHotReplacedMethodList.PushFront(method);
  4713. mHotPrimaryTypes.Add(primaryType);
  4714. }
  4715. Dictionary<StringView, DbgSubprogram*> oldProgramMap;
  4716. for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4717. {
  4718. oldMethod->PopulateSubprogram();
  4719. if (oldMethod->mBlock.IsEmpty())
  4720. continue;
  4721. auto symInfo = mDebugTarget->mSymbolMap.Get(oldMethod->mBlock.mLowPC);
  4722. if (symInfo != NULL)
  4723. {
  4724. oldProgramMap.TryAdd(symInfo->mName, oldMethod);
  4725. }
  4726. }
  4727. bool setHotJumpFailed = false;
  4728. while (!newType->mMethodList.IsEmpty())
  4729. {
  4730. DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4731. if (!newMethod->mBlock.IsEmpty())
  4732. {
  4733. BfLogDbg("Hot added new method %p %s Address:%p\n", newMethod, newMethod->mName, newMethod->mBlock.mLowPC);
  4734. newMethod->PopulateSubprogram();
  4735. auto symInfo = mDebugTarget->mSymbolMap.Get(newMethod->mBlock.mLowPC);
  4736. if (symInfo != NULL)
  4737. {
  4738. DbgSubprogram* oldMethod = NULL;
  4739. if (oldProgramMap.TryGetValue(symInfo->mName, &oldMethod))
  4740. {
  4741. bool doHotJump = false;
  4742. if (oldMethod->Equals(newMethod))
  4743. {
  4744. doHotJump = true;
  4745. }
  4746. else
  4747. {
  4748. // When mangles match but the actual signatures don't match, that can mean that the call signature was changed
  4749. // and thus it's actually a different method and shouldn't hot jump OR it could be lambda whose captures changed.
  4750. // When the lambda captures change, the user didn't actually enter a different signature so we want to do a hard
  4751. // fail if the old code gets called to avoid confusion of "why aren't my changes working?"
  4752. // If we removed captures then we can still do the hot jump. Otherwise we have to fail...
  4753. doHotJump = false;
  4754. if ((oldMethod->IsLambda()) && (oldMethod->Equals(newMethod, true)) &&
  4755. (oldMethod->mHasThis) && (newMethod->mHasThis))
  4756. {
  4757. auto oldParam = oldMethod->mParams.front();
  4758. auto newParam = newMethod->mParams.front();
  4759. if ((oldParam->mType->IsPointer()) && (newParam->mType->IsPointer()))
  4760. {
  4761. auto oldType = oldParam->mType->mTypeParam->GetPrimaryType();
  4762. oldType->PopulateType();
  4763. auto newType = newParam->mType->mTypeParam->GetPrimaryType();
  4764. newType->PopulateType();
  4765. if ((oldType->IsStruct()) && (newType->IsStruct()))
  4766. {
  4767. bool wasMatch = true;
  4768. auto oldMember = oldType->mMemberList.front();
  4769. auto newMember = newType->mMemberList.front();
  4770. while (newMember != NULL)
  4771. {
  4772. if (oldMember == NULL)
  4773. {
  4774. wasMatch = false;
  4775. break;
  4776. }
  4777. if ((oldMember->mName == NULL) || (newMember->mName == NULL))
  4778. {
  4779. wasMatch = false;
  4780. break;
  4781. }
  4782. if (strcmp(oldMember->mName, newMember->mName) != 0)
  4783. {
  4784. wasMatch = false;
  4785. break;
  4786. }
  4787. if (!oldMember->mType->Equals(newMember->mType))
  4788. {
  4789. wasMatch = false;
  4790. break;
  4791. }
  4792. oldMember = oldMember->mNext;
  4793. newMember = newMember->mNext;
  4794. }
  4795. if (wasMatch)
  4796. doHotJump = true;
  4797. }
  4798. }
  4799. if (!doHotJump)
  4800. {
  4801. mDebugTarget->mDebugger->PhysSetBreakpoint(oldMethod->mBlock.mLowPC);
  4802. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Invalid;
  4803. }
  4804. }
  4805. }
  4806. if (doHotJump)
  4807. {
  4808. if (!setHotJumpFailed)
  4809. {
  4810. if (!mDebugger->SetHotJump(oldMethod, newMethod->mBlock.mLowPC, (int)(newMethod->mBlock.mHighPC - newMethod->mBlock.mLowPC)))
  4811. setHotJumpFailed = true;
  4812. }
  4813. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Replaced;
  4814. }
  4815. }
  4816. }
  4817. }
  4818. newMethod->mParentType = primaryType;
  4819. primaryType->mMethodList.PushBack(newMethod);
  4820. }
  4821. //mDebugTarget->mSymbolMap.Get()
  4822. // bool setHotJumpFailed = false;
  4823. // while (!newType->mMethodList.IsEmpty())
  4824. // {
  4825. // DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4826. // if (!newMethod->mBlock.IsEmpty())
  4827. // {
  4828. // newMethod->PopulateSubprogram();
  4829. //
  4830. // bool found = false;
  4831. // for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4832. // {
  4833. // if (oldMethod->mBlock.IsEmpty())
  4834. // continue;
  4835. // if (oldMethod->Equals(newMethod))
  4836. // {
  4837. // if (!setHotJumpFailed)
  4838. // {
  4839. // if (!mDebugger->SetHotJump(oldMethod, newMethod))
  4840. // setHotJumpFailed = true;
  4841. // oldMethod->mWasHotReplaced = true;
  4842. // }
  4843. // }
  4844. // }
  4845. // }
  4846. // newMethod->mParentType = primaryType;
  4847. // primaryType->mMethodList.PushBack(newMethod);
  4848. // }
  4849. primaryType->mCompileUnit->mWasHotReplaced = true;
  4850. primaryType->mNeedsGlobalsPopulated = newType->mNeedsGlobalsPopulated;
  4851. primaryType->mUsingNamespaces = newType->mUsingNamespaces;
  4852. primaryType->mMemberList = newType->mMemberList;
  4853. primaryType->mCompileUnit = newType->mCompileUnit;
  4854. }
  4855. bool DbgModule::CanRead(DataStream* stream, DebuggerResult* outResult)
  4856. {
  4857. PEHeader hdr;
  4858. memset(&hdr, 0, sizeof(hdr));
  4859. PE_NTHeaders ntHdr;
  4860. memset(&ntHdr, 0, sizeof(ntHdr));
  4861. stream->Read(&hdr, sizeof(PEHeader));
  4862. stream->SetPos(hdr.e_lfanew);
  4863. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4864. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4865. {
  4866. *outResult = DebuggerResult_UnknownError;
  4867. return false;
  4868. }
  4869. #ifdef BF_DBG_32
  4870. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4871. {
  4872. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X64)
  4873. *outResult = DebuggerResult_WrongBitSize;
  4874. else
  4875. *outResult = DebuggerResult_UnknownError;
  4876. return false;
  4877. }
  4878. #else
  4879. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4880. {
  4881. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X86)
  4882. *outResult = DebuggerResult_WrongBitSize;
  4883. else
  4884. *outResult = DebuggerResult_UnknownError;
  4885. return false;
  4886. }
  4887. #endif
  4888. return true;
  4889. }
  4890. const char* DbgModule::GetStringTable(DataStream* stream, int stringTablePos)
  4891. {
  4892. if (mStringTable == NULL)
  4893. {
  4894. int prevPos = stream->GetPos();
  4895. stream->SetPos(stringTablePos);
  4896. int strTableSize = 0;
  4897. stream->Read(&strTableSize, 4);
  4898. if (strTableSize != 0)
  4899. {
  4900. strTableSize -= 4;
  4901. char* strTableData = new char[strTableSize + 4];
  4902. memcpy(strTableData, &strTableSize, 4);
  4903. stream->Read(strTableData + 4, strTableSize);
  4904. mStringTable = strTableData;
  4905. }
  4906. stream->SetPos(prevPos);
  4907. }
  4908. return mStringTable;
  4909. }
  4910. bool DbgModule::ReadCOFF(DataStream* stream, DbgModuleKind moduleKind)
  4911. {
  4912. BP_ZONE("DbgModule::ReadCOFF");
  4913. //if (this == mDebugTarget->mTargetBinary)
  4914. //mMemReporter = new MemReporter();
  4915. BfLogDbg("DbgModule::ReadCOFF %s\n", mFilePath.c_str());
  4916. if (mMemReporter != NULL)
  4917. {
  4918. mMemReporter->BeginSection(StrFormat("Module: %s", mFilePath.c_str()));
  4919. mMemReporter->Add(mImageSize);
  4920. }
  4921. defer
  4922. (
  4923. if (mMemReporter != NULL)
  4924. mMemReporter->EndSection();
  4925. );
  4926. DbgModule* mainModule = mDebugTarget->mTargetBinary;
  4927. MiniDumpDebugger* miniDumpDebugger = NULL;
  4928. if (mDebugger->IsMiniDumpDebugger())
  4929. {
  4930. miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  4931. }
  4932. mModuleKind = moduleKind;
  4933. bool isHotSwap = mModuleKind == DbgModuleKind_HotObject;
  4934. bool isObjectFile = mModuleKind != DbgModuleKind_Module;
  4935. auto linkedModule = GetLinkedModule();
  4936. if (isObjectFile)
  4937. linkedModule->PopulateStaticVariableMap();
  4938. mStartTypeIdx = (int)linkedModule->mTypes.size();
  4939. int startSrcFile = (int)mDebugTarget->mSrcFiles.size();
  4940. mStartSubprogramIdx = (int)mSubprograms.size();
  4941. PEHeader hdr;
  4942. memset(&hdr, 0, sizeof(hdr));
  4943. PE_NTHeaders ntHdr;
  4944. memset(&ntHdr, 0, sizeof(ntHdr));
  4945. if (!isObjectFile)
  4946. {
  4947. stream->Read(&hdr, sizeof(PEHeader));
  4948. stream->SetPos(hdr.e_lfanew);
  4949. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4950. mPreferredImageBase = ntHdr.mOptionalHeader.mImageBase;
  4951. if (mImageBase == 0)
  4952. {
  4953. BF_ASSERT(this == mainModule);
  4954. mImageBase = mPreferredImageBase;
  4955. }
  4956. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4957. {
  4958. mLoadState = DbgModuleLoadState_Failed;
  4959. return false;
  4960. }
  4961. #ifdef BF_DBG_32
  4962. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4963. return false;
  4964. #else
  4965. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4966. {
  4967. mLoadState = DbgModuleLoadState_Failed;
  4968. return false;
  4969. }
  4970. #endif
  4971. int pos = hdr.e_lfanew + FIELD_OFFSET(PE_NTHeaders, mOptionalHeader) + ntHdr.mFileHeader.mSizeOfOptionalHeader;
  4972. stream->SetPos(pos);
  4973. }
  4974. else
  4975. {
  4976. stream->Read(&ntHdr.mFileHeader, sizeof(PEFileHeader));
  4977. if (mMemReporter != NULL)
  4978. mMemReporter->Add("PEFileHeader", sizeof(PEFileHeader));
  4979. #ifdef BF_DBG_32
  4980. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4981. return false;
  4982. #else
  4983. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4984. {
  4985. mLoadState = DbgModuleLoadState_Failed;
  4986. return false;
  4987. }
  4988. #endif
  4989. }
  4990. int sectionStartPos = stream->GetPos();
  4991. int sectionDataEndPos = 0;
  4992. if (miniDumpDebugger != NULL)
  4993. {
  4994. // Map header
  4995. miniDumpDebugger->MapMemory((addr_target)mImageBase, (uint8*)mMappedImageFile->mData, 0x1000);
  4996. }
  4997. bool wantStringTable = isObjectFile;
  4998. stream->SetPos(sectionStartPos);
  4999. for (int dirNum = 0; dirNum < (int) ntHdr.mFileHeader.mNumberOfSections; dirNum++)
  5000. {
  5001. PESectionHeader sectHdr;
  5002. char* name = sectHdr.mName;
  5003. stream->Read(&sectHdr, sizeof(PESectionHeader));
  5004. if (sectHdr.mSizeOfRawData > 0)
  5005. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRawData + sectHdr.mSizeOfRawData));
  5006. if (sectHdr.mNumberOfRelocations > 0)
  5007. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRelocations + sectHdr.mNumberOfRelocations * sizeof(COFFRelocation)));
  5008. if (miniDumpDebugger != NULL)
  5009. {
  5010. miniDumpDebugger->MapMemory((addr_target)(mImageBase + sectHdr.mVirtualAddress), (uint8*)mMappedImageFile->mData + sectHdr.mPointerToRawData, sectHdr.mSizeOfRawData);
  5011. }
  5012. }
  5013. //fseek(fp, sectionDataEndPos + ntHdr.mFileHeader.mNumberOfSymbols * 18, SEEK_SET);
  5014. stream->SetPos(sectionDataEndPos);
  5015. uint8* symbolData = new uint8[ntHdr.mFileHeader.mNumberOfSymbols * 18];
  5016. mAllocSizeData += ntHdr.mFileHeader.mNumberOfSymbols * 18;
  5017. mSymbolData = symbolData;
  5018. stream->Read(symbolData, ntHdr.mFileHeader.mNumberOfSymbols * 18);
  5019. int curPos = stream->GetPos();
  5020. int stringTablePos = curPos;
  5021. if (isObjectFile)
  5022. GetStringTable(stream, stringTablePos);
  5023. int mDebugFrameDataLen = 0;
  5024. stream->SetPos(sectionStartPos);
  5025. PEDataDirectory* exportDataDir = &ntHdr.mOptionalHeader.mDataDirectory[0];
  5026. mHotTargetSections.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5027. Array<PESectionHeader> sectionHeaders;
  5028. sectionHeaders.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5029. mSectionRVAs.Resize(sectionHeaders.size() + 1);
  5030. Array<String> sectionNames;
  5031. sectionNames.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5032. stream->Read(&sectionHeaders[0], sizeof(PESectionHeader) * ntHdr.mFileHeader.mNumberOfSections);
  5033. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5034. {
  5035. mSectionRVAs[sectNum] = sectionHeaders[sectNum].mVirtualAddress;
  5036. }
  5037. int tlsSection = -1;
  5038. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5039. {
  5040. //PEDataDirectory* dataDir = &ntHdr.mOptionalHeader.mDataDirectory[dirNum];
  5041. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5042. //stream->Read(&sectHdr, sizeof(PESectionHeader));
  5043. const char* name = sectHdr.mName;
  5044. if (name[0] == '/')
  5045. {
  5046. int strIdx = atoi(name + 1);
  5047. name = &GetStringTable(stream, stringTablePos)[strIdx];
  5048. }
  5049. sectionNames[sectNum] = name;
  5050. DbgHotTargetSection* targetSection = NULL;
  5051. if (IsObjectFile())
  5052. {
  5053. targetSection = new DbgHotTargetSection();
  5054. targetSection->mDataSize = sectHdr.mSizeOfRawData;
  5055. targetSection->mPointerToRelocations = sectHdr.mPointerToRelocations;
  5056. targetSection->mNumberOfRelocations = sectHdr.mNumberOfRelocations;
  5057. targetSection->mTargetSectionAddr = 0; // TODO: Allocate!
  5058. targetSection->mCanExecute = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5059. targetSection->mCanWrite = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_WRITE) != 0;
  5060. targetSection->mNoTargetAlloc = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0;
  5061. mHotTargetSections[sectNum] = targetSection;
  5062. }
  5063. DbgSection dwSection;
  5064. dwSection.mIsExecutable = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5065. dwSection.mAddrStart = sectHdr.mVirtualAddress;
  5066. dwSection.mAddrLength = BF_MAX(sectHdr.mSizeOfRawData, sectHdr.mVirtualSize);
  5067. mSections.push_back(dwSection);
  5068. if (sectHdr.mPointerToRawData == 0)
  5069. continue;
  5070. if (strcmp(name, ".tls") == 0)
  5071. mTLSAddr = (addr_target)(sectHdr.mVirtualAddress + mImageBase);
  5072. if ((IsObjectFile()) && (strcmp(name, ".tls$") == 0))
  5073. {
  5074. tlsSection = sectNum;
  5075. mTLSSize = sectHdr.mSizeOfRawData;
  5076. targetSection->mNoTargetAlloc = true;
  5077. }
  5078. bool isExportDataDir = ((exportDataDir->mVirtualAddress != 0) && (exportDataDir->mVirtualAddress >= sectHdr.mVirtualAddress) && (exportDataDir->mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5079. if ((!IsObjectFile()) && (!isExportDataDir))
  5080. {
  5081. if (((strcmp(name, ".text")) == 0) ||
  5082. ((strcmp(name, ".textbss")) == 0) ||
  5083. ((strcmp(name, ".reloc")) == 0)/* ||
  5084. ((strcmp(name, ".data")) == 0)*/)
  5085. {
  5086. // Big unneeded sections
  5087. continue;
  5088. }
  5089. }
  5090. stream->SetPos(sectHdr.mPointerToRawData);
  5091. int dataSize = sectHdr.mSizeOfRawData + 8;
  5092. mAllocSizeData += dataSize;
  5093. uint8* data = new uint8[dataSize];
  5094. {
  5095. BP_ZONE("DbgModule::ReadCOFF_ReadSectionData");
  5096. stream->Read(data, sectHdr.mSizeOfRawData);
  5097. }
  5098. BfLogDbg("Read section data %s %p\n", name, data);
  5099. memset(data + sectHdr.mSizeOfRawData, 0, 8);
  5100. if (IsObjectFile())
  5101. targetSection->mData = data;
  5102. addr_target addrOffset = sectHdr.mVirtualAddress;
  5103. if (isExportDataDir)
  5104. {
  5105. BP_ZONE("DbgModule::ReadCOFF_SymbolMap");
  5106. IMAGE_EXPORT_DIRECTORY* imageExportDir = (IMAGE_EXPORT_DIRECTORY*)(data + (exportDataDir->mVirtualAddress - addrOffset));
  5107. for (int funcIdx = 0; funcIdx < (int)imageExportDir->NumberOfNames; funcIdx++)
  5108. {
  5109. //addr_target strAddr = *(addr_target*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(addr_target));
  5110. int32 strAddr = *(int32*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(int32));
  5111. const char* name = (const char*)(data + (strAddr - addrOffset));
  5112. #ifdef BF_DBG_32
  5113. if (name[0] == '_')
  5114. name++;
  5115. #endif
  5116. int funcOrd = *(uint16*)(data + (imageExportDir->AddressOfNameOrdinals - addrOffset) + funcIdx * sizeof(uint16));
  5117. addr_target funcAddr = *(uint32*)(data + (imageExportDir->AddressOfFunctions - addrOffset) + funcOrd * sizeof(int32));
  5118. int strLen = (int)strlen(name);
  5119. BP_ALLOC("ReadCOFF_SymbolMap", strLen + 1);
  5120. char* allocStr = (char*)mAlloc.AllocBytes(strLen + 1, "ReadCOFF_SymbolMap");
  5121. memcpy(allocStr, name, strLen);
  5122. BP_ALLOC_T(DbgSymbol);
  5123. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5124. dwSymbol->mDbgModule = this;
  5125. dwSymbol->mName = allocStr;
  5126. dwSymbol->mAddress = funcAddr;
  5127. if (strcmp(name, "_tls_index") == 0)
  5128. {
  5129. mTLSIndexAddr = funcAddr;
  5130. }
  5131. //TODO:
  5132. //mDeferredSymbols.PushFront(dwSymbol);
  5133. dwSymbol->mAddress = (addr_target)(dwSymbol->mAddress + mImageBase);
  5134. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5135. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5136. }
  5137. }
  5138. if ((IsObjectFile()) && (sectHdr.mNumberOfRelocations > 0))
  5139. {
  5140. //mDebugger->AllocTargetMemory(sectHdr.mSizeOfRawData, true, true);
  5141. }
  5142. if (strcmp(name, ".text") == 0)
  5143. {
  5144. if (!IsObjectFile())
  5145. mCodeAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5146. }
  5147. //if (strcmp(name, ".rdata") == 0)
  5148. {
  5149. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  5150. if (debugDirEntry.mSize > 0)
  5151. {
  5152. if (mMemReporter != NULL)
  5153. mMemReporter->Add("DataDirectory", debugDirEntry.mSize);
  5154. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5155. {
  5156. int count = debugDirEntry.mSize / sizeof(IMAGE_DEBUG_DIRECTORY);
  5157. for (int dirIdx = 0; dirIdx < count; dirIdx++)
  5158. {
  5159. IMAGE_DEBUG_DIRECTORY* debugDirectory = (IMAGE_DEBUG_DIRECTORY*)(data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress) + dirIdx;
  5160. if (debugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW)
  5161. {
  5162. struct _CodeViewEntry
  5163. {
  5164. public:
  5165. int32 mSig;
  5166. uint8 mGUID[16];
  5167. int32 mAge;
  5168. const char mPDBPath[1];
  5169. };
  5170. if (debugDirectory->AddressOfRawData != 0)
  5171. {
  5172. _CodeViewEntry* codeViewEntry = (_CodeViewEntry*)(data + debugDirectory->AddressOfRawData - sectHdr.mVirtualAddress);
  5173. if (codeViewEntry->mSig == 'SDSR')
  5174. {
  5175. LoadPDB(codeViewEntry->mPDBPath, codeViewEntry->mGUID, codeViewEntry->mAge);
  5176. }
  5177. }
  5178. }
  5179. }
  5180. }
  5181. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5182. }
  5183. }
  5184. //
  5185. {
  5186. PEDataDirectory& tlsDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
  5187. if (tlsDirEntry.mSize > 0)
  5188. {
  5189. if ((tlsDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (tlsDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5190. {
  5191. uint8* relPtr = data + tlsDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5192. uint8* endPtr = relPtr + tlsDirEntry.mSize;
  5193. addr_target tlsDataStart = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5194. addr_target tlsDataEnd = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5195. mTLSAddr = (addr_target)(tlsDataStart + mImageBase);
  5196. mTLSSize = (int)(tlsDataEnd - tlsDataStart);
  5197. }
  5198. }
  5199. }
  5200. //
  5201. {
  5202. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  5203. if (debugDirEntry.mSize > 0)
  5204. {
  5205. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5206. {
  5207. uint8* relPtr = data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5208. uint8* endPtr = relPtr + debugDirEntry.mSize;
  5209. IMAGE_RESOURCE_DIRECTORY* typeDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr);
  5210. // Skip named entries
  5211. for (int typeIdx = 0; typeIdx < typeDir->NumberOfIdEntries; typeIdx++)
  5212. {
  5213. IMAGE_RESOURCE_DIRECTORY_ENTRY* typeEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)typeDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5214. (typeDir->NumberOfNamedEntries + typeIdx)*sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5215. if (typeEntry->Id == 0x10) //VERSION
  5216. {
  5217. IMAGE_RESOURCE_DIRECTORY* idDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (typeEntry->OffsetToData & 0x7FFFFFFF));
  5218. if (idDir->NumberOfIdEntries < 1)
  5219. break;
  5220. IMAGE_RESOURCE_DIRECTORY_ENTRY* idEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)idDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5221. (idDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5222. IMAGE_RESOURCE_DIRECTORY* langDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (idEntry->OffsetToData & 0x7FFFFFFF));
  5223. if (langDir->NumberOfIdEntries < 1)
  5224. break;
  5225. IMAGE_RESOURCE_DIRECTORY_ENTRY* langEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)langDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5226. (langDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5227. IMAGE_RESOURCE_DATA_ENTRY* dataEntry = (IMAGE_RESOURCE_DATA_ENTRY*)(relPtr + (langEntry->OffsetToData & 0x7FFFFFFF));
  5228. uint8* versionData = data + dataEntry->OffsetToData - sectHdr.mVirtualAddress;
  5229. uint8* vPtr = versionData;
  5230. auto vSize = GET_FROM(vPtr, uint16);
  5231. auto verEnd = vPtr + vSize;
  5232. auto vLength = GET_FROM(vPtr, uint16);
  5233. vPtr += 36; // "VS_VERSION_INFO"
  5234. auto fixedFileInfo = GET_FROM(vPtr, VS_FIXEDFILEINFO);
  5235. auto _GetString = [&]()
  5236. {
  5237. wchar_t* cPtr = (wchar_t*)vPtr;
  5238. int len = (int)wcslen(cPtr);
  5239. vPtr += (len + 1) * 2;
  5240. if (((intptr)vPtr & 3) != 0)
  5241. vPtr += 2;
  5242. UTF16String str16(cPtr, len);
  5243. return UTF8Encode(str16);
  5244. };
  5245. while (vPtr < verEnd)
  5246. {
  5247. auto size = GET_FROM(vPtr, uint16);
  5248. auto childEnd = vPtr + size;
  5249. auto valueLength = GET_FROM(vPtr, uint16);
  5250. auto type = GET_FROM(vPtr, uint16);
  5251. String infoType = _GetString();
  5252. if (infoType == "StringFileInfo")
  5253. {
  5254. while (vPtr < childEnd)
  5255. {
  5256. auto strsSize = GET_FROM(vPtr, uint16);
  5257. auto strsEnd = vPtr + strsSize;
  5258. auto strsLength = GET_FROM(vPtr, uint16);
  5259. auto strsType = GET_FROM(vPtr, uint16);
  5260. String hexNum = _GetString();
  5261. while (vPtr < strsEnd)
  5262. {
  5263. auto strSize = GET_FROM(vPtr, uint16);
  5264. auto strEnd = vPtr + strSize;
  5265. auto strLength = GET_FROM(vPtr, uint16);
  5266. auto strType = GET_FROM(vPtr, uint16);
  5267. String key = _GetString();
  5268. String value = _GetString();
  5269. if (key == "FileVersion")
  5270. mVersion = value;
  5271. }
  5272. }
  5273. }
  5274. vPtr = childEnd;
  5275. }
  5276. }
  5277. }
  5278. }
  5279. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5280. }
  5281. }
  5282. bool usedData = true;
  5283. /*if (isUnwindSection)
  5284. {
  5285. mExceptionData = data;
  5286. mExceptionDataRVA = sectHdr.mVirtualAddress;
  5287. }*/
  5288. if (strcmp(name, ".pdata") == 0)
  5289. {
  5290. DbgSectionData entry;
  5291. entry.mData = data;
  5292. entry.mSize = sectHdr.mSizeOfRawData;
  5293. mExceptionDirectory.Add(entry);
  5294. }
  5295. /*else if (strcmp(name, ".rdata") == 0)
  5296. {
  5297. if (mExceptionData == NULL)
  5298. {
  5299. mExceptionData = data;
  5300. mExceptionDataRVA = sectHdr.mVirtualAddress;
  5301. }
  5302. else
  5303. {
  5304. usedData = false;
  5305. }
  5306. }
  5307. else if (strcmp(name, ".xdata") == 0)
  5308. {
  5309. if (mExceptionData != NULL)
  5310. {
  5311. if (IsObjectFile())
  5312. {
  5313. mOwnedSectionData.push_back(mExceptionData);
  5314. }
  5315. else
  5316. {
  5317. // xdata section overrides rdata exception data
  5318. delete[] mExceptionData;
  5319. }
  5320. }
  5321. mExceptionData = data;
  5322. mExceptionDataRVA = sectHdr.mVirtualAddress;
  5323. }*/
  5324. else if (strcmp(name, ".debug_info") == 0)
  5325. {
  5326. mDebugInfoData = data;
  5327. }
  5328. else if (strcmp(name, ".debug_line") == 0)
  5329. {
  5330. mDebugLineData = data;
  5331. }
  5332. else if (strcmp(name, ".debug_str") == 0)
  5333. {
  5334. mDebugStrData = data;
  5335. }
  5336. else if (strcmp(name, ".debug_frame") == 0)
  5337. {
  5338. mDebugFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5339. mDebugFrameData = data;
  5340. mDebugFrameDataLen = sectHdr.mSizeOfRawData;
  5341. }
  5342. else if (strcmp(name, ".eh_frame") == 0)
  5343. {
  5344. mEHFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5345. mEHFrameData = data;
  5346. }
  5347. else if (strcmp(name, ".debug_abbrev") == 0)
  5348. {
  5349. mDebugAbbrevData = data;
  5350. mDebugAbbrevPtrData = new const uint8*[sectHdr.mSizeOfRawData];
  5351. }
  5352. else if (strcmp(name, ".debug_loc") == 0)
  5353. {
  5354. mDebugLocationData = data;
  5355. }
  5356. else if (strcmp(name, ".debug_ranges") == 0)
  5357. {
  5358. mDebugRangesData = data;
  5359. }
  5360. // else if (strcmp(name, ".rsrc") == 0)
  5361. // {
  5362. // //IMAGE_DIRECTORY_ENTRY_RESOURCE
  5363. // }
  5364. else if (CheckSection(name, data, sectHdr.mSizeOfRawData))
  5365. {
  5366. // Was used
  5367. }
  5368. else
  5369. {
  5370. /*if (isUnwindSection)
  5371. mOwnsExceptionData = true;
  5372. else*/
  5373. usedData = false;
  5374. }
  5375. if (!usedData)
  5376. {
  5377. if (IsObjectFile())
  5378. {
  5379. mOwnedSectionData.push_back(data);
  5380. }
  5381. else
  5382. {
  5383. mAllocSizeData -= dataSize;
  5384. delete [] data;
  5385. }
  5386. }
  5387. //stream->SetPos(prevPos);
  5388. }
  5389. int needHotTargetMemory = 0;
  5390. if (isObjectFile)
  5391. {
  5392. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5393. {
  5394. auto targetSection = mHotTargetSections[sectNum];
  5395. if (!targetSection->mNoTargetAlloc)
  5396. needHotTargetMemory += (targetSection->mDataSize + (mDebugger->mPageSize - 1)) & ~(mDebugger->mPageSize - 1);
  5397. }
  5398. mDebugger->ReserveHotTargetMemory(needHotTargetMemory);
  5399. // '0' address is temporary
  5400. //mOrigImageData = new DbgModuleMemoryCache(0, NULL, needHotTargetMemory, true);
  5401. mOrigImageData = new DbgModuleMemoryCache(0, needHotTargetMemory);
  5402. }
  5403. int numSections = ntHdr.mFileHeader.mNumberOfSections;
  5404. if (isObjectFile)
  5405. {
  5406. addr_target* resolvedSymbolAddrs = new addr_target[ntHdr.mFileHeader.mNumberOfSymbols];
  5407. memset(resolvedSymbolAddrs, 0, ntHdr.mFileHeader.mNumberOfSymbols * sizeof(addr_target));
  5408. ParseHotTargetSections(stream, resolvedSymbolAddrs);
  5409. delete [] resolvedSymbolAddrs;
  5410. }
  5411. ProcessDebugInfo();
  5412. if (mDebugInfoData != NULL)
  5413. {
  5414. mDbgFlavor = DbgFlavor_GNU;
  5415. mMasterCompileUnit = new DbgCompileUnit(this);
  5416. mMasterCompileUnit->mDbgModule = this;
  5417. mMasterCompileUnit->mIsMaster = true;
  5418. const uint8* data = mDebugInfoData;
  5419. while (ParseDWARF(data)) {}
  5420. CreateNamespaces();
  5421. // Must be added last so module reference indices still map correctly
  5422. mCompileUnits.push_back(mMasterCompileUnit);
  5423. }
  5424. ParseDebugFrameData();
  5425. ParseEHFrameData();
  5426. mEndTypeIdx = (int)linkedModule->mTypes.size();
  5427. mEndSubprogramIdx = (int)mSubprograms.size();
  5428. if (mDebugLineData != NULL)
  5429. {
  5430. const uint8* data = mDebugLineData;
  5431. for (int compileUnitIdx = 0; true; compileUnitIdx++)
  5432. if (!ParseDebugLineInfo(data, compileUnitIdx))
  5433. break;
  5434. }
  5435. {
  5436. BP_ZONE("ReadPE_ReadSymbols");
  5437. //linkedModule->mSymbolNameMap.reserve(linkedModule->mSymbolNameMap.size() + ntHdr.mFileHeader.mNumberOfSymbols * 2);
  5438. bool tlsFailed = false;
  5439. addr_target tlsMappingAddr = 0;
  5440. for (int symNum = 0; symNum < (int)ntHdr.mFileHeader.mNumberOfSymbols; symNum++)
  5441. {
  5442. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[symNum * 18];
  5443. char* name = symInfo->mName;
  5444. if (symInfo->mNameOfs[0] != 0)
  5445. {
  5446. if (name[7] != 0)
  5447. {
  5448. // Name is exactly 8 chars, not null terminated yet
  5449. name = (char*)mAlloc.AllocBytes(9, "PESymbol");
  5450. memcpy(name, symInfo->mName, 8);
  5451. name[8] = 0;
  5452. }
  5453. }
  5454. else
  5455. name = (char*)GetStringTable(stream, stringTablePos) + symInfo->mNameOfs[1];
  5456. if ((symInfo->mStorageClass == COFF_SYM_CLASS_EXTERNAL) ||
  5457. (symInfo->mStorageClass == COFF_SYM_CLASS_STATIC))
  5458. {
  5459. // 'static' in the C sense.
  5460. // It means local to the compile unit, so may have multiple copies of the same symbol name.
  5461. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  5462. if (symInfo->mSectionNum == 0xFFFF)
  5463. continue;
  5464. if (symInfo->mSectionNum > 0)
  5465. {
  5466. bool isTLS = false;
  5467. addr_target targetAddr = 0;
  5468. if (isObjectFile)
  5469. {
  5470. if (symInfo->mSectionNum - 1 == tlsSection)
  5471. {
  5472. isTLS = true;
  5473. }
  5474. else
  5475. {
  5476. auto hotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  5477. if (hotTargetSection != NULL)
  5478. targetAddr = GetHotTargetAddress(hotTargetSection) + symInfo->mValue;
  5479. }
  5480. }
  5481. else
  5482. targetAddr = mSectionRVAs[symInfo->mSectionNum - 1] + symInfo->mValue;
  5483. if (((targetAddr != 0) || (isTLS)) &&
  5484. (name[0] != '.'))
  5485. {
  5486. const char* symbolName = name;
  5487. #ifdef BF_DBG_32
  5488. if (symbolName[0] == '_')
  5489. symbolName++;
  5490. #endif
  5491. if (strcmp(symbolName, "_tls_index") == 0)
  5492. {
  5493. mTLSIndexAddr = (addr_target)(targetAddr + mImageBase);
  5494. }
  5495. if ((isStaticSymbol) && (IsHotSwapPreserve(symbolName)))
  5496. isStaticSymbol = false;
  5497. if ((isObjectFile) && (!isStaticSymbol))
  5498. {
  5499. DbgSymbol* dwSymbol = NULL;
  5500. linkedModule->ParseSymbolData() ;
  5501. BP_ALLOC_T(DbgSymbol);
  5502. dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5503. dwSymbol->mDbgModule = this;
  5504. dwSymbol->mName = symbolName;
  5505. dwSymbol->mAddress = targetAddr;
  5506. if (dwSymbol != NULL)
  5507. {
  5508. bool isHotSwapPreserve = IsHotSwapPreserve(dwSymbol->mName);
  5509. bool insertIntoNameMap = true;
  5510. bool oldFound = false;
  5511. auto nameMapEntry = linkedModule->mSymbolNameMap.Find(dwSymbol->mName);
  5512. if (nameMapEntry != NULL)
  5513. {
  5514. oldFound = true;
  5515. if (!isHotSwapPreserve)
  5516. {
  5517. nameMapEntry->mValue = dwSymbol;
  5518. }
  5519. else if (mDbgFlavor == DbgFlavor_MS)
  5520. {
  5521. // Store in our own map - this is needed for storing address of the new vdata
  5522. // so the new values can be copied in
  5523. mSymbolNameMap.Insert(dwSymbol);
  5524. }
  5525. }
  5526. else
  5527. {
  5528. if (isTLS)
  5529. {
  5530. if (mainModule->mTLSExtraAddr == 0)
  5531. {
  5532. auto extraSym = mainModule->mSymbolNameMap.Find("__BFTLS_EXTRA");
  5533. if (extraSym != NULL)
  5534. {
  5535. mainModule->ParseGlobalsData();
  5536. auto itr = mainModule->mStaticVariableMap.find("__BFTLS_EXTRA");
  5537. if (itr != mainModule->mStaticVariableMap.end())
  5538. {
  5539. auto staticVar = itr->second;
  5540. mainModule->mTLSExtraAddr = extraSym->mValue->mAddress;
  5541. mainModule->mTLSExtraSize = (int)staticVar->mType->GetByteCount();
  5542. }
  5543. }
  5544. }
  5545. if ((mainModule->mTLSExtraAddr != 0) && (tlsMappingAddr == 0))
  5546. {
  5547. // Take a chunk out of __BFTLS_EXTRA
  5548. if (mTLSSize <= mainModule->mTLSExtraSize)
  5549. {
  5550. tlsMappingAddr = mainModule->mTLSExtraAddr;
  5551. mainModule->mTLSExtraAddr += mTLSSize;
  5552. mainModule->mTLSExtraSize -= mTLSSize;
  5553. }
  5554. }
  5555. if (tlsMappingAddr != 0)
  5556. {
  5557. BF_ASSERT(symInfo->mValue < mTLSSize);
  5558. dwSymbol->mAddress = tlsMappingAddr + symInfo->mValue;
  5559. }
  5560. if (dwSymbol->mAddress == 0)
  5561. {
  5562. if (!tlsFailed)
  5563. {
  5564. Fail(StrFormat("Hot swapping failed to allocate TLS address for '%s'. Program restart required.", name));
  5565. }
  5566. dwSymbol->mAddress = (addr_target)0xCDCDCDCD;
  5567. tlsFailed = true;
  5568. }
  5569. }
  5570. }
  5571. if (dwSymbol->mAddress != 0)
  5572. {
  5573. if (!oldFound)
  5574. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5575. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5576. }
  5577. }
  5578. }
  5579. else
  5580. {
  5581. //TODO: We don't need to defer symbols anymore... we can just do a Fixup on their addr
  5582. //mDeferredSymbols.PushFront(dwSymbol);
  5583. BP_ALLOC_T(DbgSymbol);
  5584. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5585. dwSymbol->mDbgModule = this;
  5586. dwSymbol->mName = symbolName;
  5587. dwSymbol->mAddress = targetAddr;
  5588. if (!IsObjectFile())
  5589. dwSymbol->mAddress += (addr_target)mImageBase;
  5590. if (IsObjectFile())
  5591. BF_ASSERT((dwSymbol->mAddress >= mImageBase) && (dwSymbol->mAddress < mImageBase + mImageSize));
  5592. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5593. if (!isStaticSymbol)
  5594. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5595. }
  5596. }
  5597. }
  5598. }
  5599. if (symInfo->mStorageClass == COFF_SYM_CLASS_FILE)
  5600. {
  5601. const char* fileName = (const char*)&mSymbolData[(symNum + 1) * 18];
  5602. }
  5603. symNum += symInfo->mNumOfAuxSymbols;
  5604. }
  5605. }
  5606. int subProgramSizes = 0;
  5607. for (int subProgramIdx = mStartSubprogramIdx; subProgramIdx < mEndSubprogramIdx; subProgramIdx++)
  5608. {
  5609. auto dwSubprogram = mSubprograms[subProgramIdx];
  5610. subProgramSizes += (int)(dwSubprogram->mBlock.mHighPC - dwSubprogram->mBlock.mLowPC);
  5611. /*for (int i = 0; i < dwSubprogram->mLineDataArray.mSize; i++)
  5612. {
  5613. auto lineData = dwSubprogram->mLineDataArray.mData[i];
  5614. auto srcFile = lineData->mSrcFileRef->mSrcFile;
  5615. srcFile->mLineData.push_back(lineData);
  5616. srcFile->mHadLineData = true;
  5617. if ((srcFile->mFirstLineDataDbgModule == NULL) || (srcFile->mFirstLineDataDbgModule == this))
  5618. srcFile->mFirstLineDataDbgModule = this;
  5619. else
  5620. srcFile->mHasLineDataFromMultipleModules = true;
  5621. }*/
  5622. }
  5623. // Delete srcFiles without line data
  5624. int lineDataCount = 0;
  5625. /*for (int srcFileIdx = startSrcFile; srcFileIdx < (int)mDebugTarget->mSrcFiles.size(); srcFileIdx++)
  5626. {
  5627. if (!mDebugTarget->mSrcFiles[srcFileIdx]->mHadLineData)
  5628. {
  5629. mEmptySrcFiles.push_back(mDebugTarget->mSrcFiles[srcFileIdx]);
  5630. mDebugTarget->mSrcFiles.erase(mDebugTarget->mSrcFiles.begin() + srcFileIdx);
  5631. }
  5632. else
  5633. lineDataCount += (int)mDebugTarget->mSrcFiles[srcFileIdx]->mLineData.size();
  5634. }*/
  5635. auto srcFilesItr = mDebugTarget->mSrcFiles.begin();
  5636. while (srcFilesItr != mDebugTarget->mSrcFiles.end())
  5637. {
  5638. DbgSrcFile* srcFile = srcFilesItr->mValue;
  5639. if ((!srcFile->mHadLineData) && (srcFile->mLocalPath.IsEmpty()))
  5640. {
  5641. mEmptySrcFiles.push_back(srcFile);
  5642. srcFilesItr = mDebugTarget->mSrcFiles.Remove(srcFilesItr);
  5643. }
  5644. else
  5645. {
  5646. ++srcFilesItr;
  5647. }
  5648. }
  5649. if (!isObjectFile)
  5650. {
  5651. mImageSize = ntHdr.mOptionalHeader.mSizeOfImage;
  5652. mEntryPoint = ntHdr.mOptionalHeader.mAddressOfEntryPoint;
  5653. }
  5654. /*OutputDebugStrF("%s:\n CompileUnits:%d DebugLines: %d Types: %d (%d in map) SubPrograms: %d (%dk) AllocSize:%dk\n", mFilePath.c_str(), mCompileUnits.size(),
  5655. lineDataCount, mEndTypeIdx - mStartTypeIdx, (int)linkedModule->mTypes.size() - mStartTypeIdx, mEndSubprogramIdx - mStartSubprogramIdx, subProgramSizes / 1024, mAlloc.GetAllocSize() / 1024);*/
  5656. if (isHotSwap)
  5657. {
  5658. // In COFF, we don't necessarily add an actual primary type during MapCompileUnitMethods, so this fixes that
  5659. while (true)
  5660. {
  5661. bool didReplaceType = false;
  5662. for (auto itr = mHotPrimaryTypes.begin(); itr != mHotPrimaryTypes.end(); ++itr)
  5663. {
  5664. auto dbgType = *itr;
  5665. auto primaryType = dbgType->GetPrimaryType();
  5666. if (primaryType != dbgType)
  5667. {
  5668. mHotPrimaryTypes.Remove(itr);
  5669. mHotPrimaryTypes.Add(primaryType);
  5670. didReplaceType = true;
  5671. break;
  5672. }
  5673. }
  5674. if (!didReplaceType)
  5675. break;
  5676. }
  5677. BF_ASSERT(mTypes.size() == 0);
  5678. for (int typeIdx = mStartTypeIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
  5679. {
  5680. DbgType* newType = linkedModule->mTypes[typeIdx];
  5681. //if (!newType->mMethodList.IsEmpty())
  5682. if (!newType->mIsDeclaration)
  5683. HotReplaceType(newType);
  5684. }
  5685. }
  5686. if (needHotTargetMemory != 0)
  5687. {
  5688. BF_ASSERT(needHotTargetMemory >= (int)mImageSize);
  5689. }
  5690. //BF_ASSERT(mEndTypeIdx == (int)linkedModule->mTypes.size());
  5691. //BF_ASSERT(mEndSubprogramIdx == (int)mSubprograms.size());
  5692. ParseExceptionData();
  5693. mLoadState = DbgModuleLoadState_Loaded;
  5694. if (mMemReporter != NULL)
  5695. {
  5696. mMemReporter->BeginSection("Sections");
  5697. ParseSymbolData();
  5698. Array<DbgSymbol*> orderedSyms;
  5699. for (auto sym : mSymbolNameMap)
  5700. {
  5701. auto dbgSym = sym->mValue;
  5702. orderedSyms.Add(dbgSym);
  5703. }
  5704. orderedSyms.Sort([](DbgSymbol* lhs, DbgSymbol* rhs) { return lhs->mAddress < rhs->mAddress; });
  5705. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5706. {
  5707. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5708. mMemReporter->BeginSection(sectionNames[sectNum]);
  5709. DbgSymbol* lastSym = NULL;
  5710. for (auto dbgSym : orderedSyms)
  5711. {
  5712. if (dbgSym->mAddress < mImageBase + sectHdr.mVirtualAddress)
  5713. continue;
  5714. if (dbgSym->mAddress >= mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData)
  5715. break;
  5716. if (lastSym != NULL)
  5717. {
  5718. mMemReporter->Add(lastSym->mName, (int)(dbgSym->mAddress - lastSym->mAddress));
  5719. }
  5720. else
  5721. {
  5722. int startingOffset = (int)(dbgSym->mAddress - (mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5723. if (startingOffset > 0)
  5724. mMemReporter->Add("<StartData>", startingOffset);
  5725. }
  5726. lastSym = dbgSym;
  5727. }
  5728. if (lastSym != NULL)
  5729. mMemReporter->Add(lastSym->mName, (int)((mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData) - lastSym->mAddress));
  5730. else
  5731. {
  5732. mMemReporter->Add("<Unaccounted>", (int)(sectHdr.mSizeOfRawData));
  5733. }
  5734. mMemReporter->EndSection();
  5735. }
  5736. mMemReporter->EndSection();
  5737. mMemReporter->mShowInKB = false;
  5738. mMemReporter->Report();
  5739. }
  5740. return true;
  5741. }
  5742. void DbgModule::FinishHotSwap()
  5743. {
  5744. BF_ASSERT(IsObjectFile());
  5745. auto linkedModule = GetLinkedModule();
  5746. auto mainModule = mDebugTarget->mTargetBinary;
  5747. HashSet<String> failSet;
  5748. String findName;
  5749. for (auto deferredHotResolve : mDeferredHotResolveList)
  5750. {
  5751. addr_target resolveTargetAddr = deferredHotResolve->mNewAddr;
  5752. findName = deferredHotResolve->mName;
  5753. if (mDbgFlavor == DbgFlavor_MS)
  5754. {
  5755. // ... why do we need to find these variables in the variable map instead of the symbol name map?
  5756. }
  5757. auto itr = mainModule->mStaticVariableMap.find(findName.c_str());
  5758. if (itr != mainModule->mStaticVariableMap.end())
  5759. {
  5760. DbgVariable* variable = itr->second;
  5761. resolveTargetAddr = mDebugTarget->GetStaticAddress(variable);
  5762. }
  5763. else
  5764. {
  5765. auto symbolEntry = mainModule->mSymbolNameMap.Find(findName.c_str());
  5766. if (symbolEntry != NULL)
  5767. {
  5768. resolveTargetAddr = symbolEntry->mValue->mAddress;
  5769. }
  5770. else
  5771. {
  5772. if (deferredHotResolve->mName == "__ImageBase")
  5773. {
  5774. resolveTargetAddr = (addr_target)mainModule->mImageBase;
  5775. }
  5776. else
  5777. {
  5778. resolveTargetAddr = mainModule->LocateSymbol(deferredHotResolve->mName);
  5779. if (resolveTargetAddr == 0)
  5780. {
  5781. failSet.Add(deferredHotResolve->mName);
  5782. continue;
  5783. }
  5784. }
  5785. }
  5786. }
  5787. DoReloc(deferredHotResolve->mHotTargetSection, deferredHotResolve->mReloc, resolveTargetAddr, NULL);
  5788. }
  5789. mDeferredHotResolveList.Clear();
  5790. if (!failSet.IsEmpty())
  5791. {
  5792. bool handled = false;
  5793. if (!mDebugger->mDebugManager->mOutMessages.empty())
  5794. {
  5795. auto& str = mDebugger->mDebugManager->mOutMessages.back();
  5796. if (str.Contains("failed to resolve"))
  5797. {
  5798. for (auto& sym : failSet)
  5799. {
  5800. str += ", ";
  5801. str += sym;
  5802. }
  5803. handled = true;
  5804. }
  5805. }
  5806. if (!handled)
  5807. {
  5808. int symIdx = 0;
  5809. String str;
  5810. if (failSet.size() == 1)
  5811. str = "Hot swapping failed to resolve symbol: ";
  5812. else
  5813. str = "Hot swapping failed to resolve symbols: ";
  5814. for (auto& sym : failSet)
  5815. {
  5816. if (symIdx != 0)
  5817. str += ", ";
  5818. str += sym;
  5819. symIdx++;
  5820. }
  5821. mDebugger->Fail(str);
  5822. }
  5823. }
  5824. CommitHotTargetSections();
  5825. // We need this here because vdata gets loaded first, so we need to wait until we have the addrs for the new methods (from other modules)
  5826. // before we can finalize the class vdata.
  5827. ProcessHotSwapVariables();
  5828. for (auto hotTargetSection : mHotTargetSections)
  5829. delete hotTargetSection;
  5830. mHotTargetSections.Clear();
  5831. mSymbolNameMap.Clear();
  5832. }
  5833. addr_target DbgModule::ExecuteOps(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, CPURegisters* registers, DbgAddrType* outAddrType, DbgEvalLocFlags flags, addr_target* pushValue)
  5834. {
  5835. bool allowReg = (flags & DbgEvalLocFlag_IsParam) == 0;
  5836. const uint8* locDataEnd = locData + locDataLen;
  5837. int regNum = -1;
  5838. addr_target stackFrameData[256];
  5839. int stackIdx = 0;
  5840. if (pushValue != NULL)
  5841. stackFrameData[stackIdx++] = *pushValue;
  5842. while (locData < locDataEnd)
  5843. {
  5844. uint8 opCode = GET_FROM(locData, uint8);
  5845. switch (opCode)
  5846. {
  5847. case DW_OP_piece:
  5848. {
  5849. if (*outAddrType == DbgAddrType_Register)
  5850. *outAddrType = DbgAddrType_Value;
  5851. addr_target val = stackFrameData[--stackIdx];
  5852. int pieceSize = (int)DecodeULEB128(locData);
  5853. if (pieceSize == 4)
  5854. val &= 0xFFFFFFFF;
  5855. else if (pieceSize == 2)
  5856. val &= 0xFFFF;
  5857. else if (pieceSize == 1)
  5858. val &= 0xFF;
  5859. stackFrameData[stackIdx++] = val;
  5860. }
  5861. break;
  5862. case DW_OP_consts:
  5863. {
  5864. int64 val = DecodeSLEB128(locData);
  5865. stackFrameData[stackIdx++] = (addr_target)val;
  5866. }
  5867. break;
  5868. case DW_OP_stack_value:
  5869. {
  5870. *outAddrType = DbgAddrType_Value;
  5871. }
  5872. break;
  5873. case DW_OP_addr_noRemap:
  5874. {
  5875. addr_target addr = GET_FROM(locData, addr_target);
  5876. stackFrameData[stackIdx++] = addr;
  5877. //*outIsAddr = true;
  5878. *outAddrType = DbgAddrType_Target;
  5879. }
  5880. break;
  5881. case DW_OP_addr:
  5882. {
  5883. addr_target addr = GET_FROM(locData, addr_target);
  5884. //if (dwarf != NULL)
  5885. addr = RemapAddr(addr);
  5886. stackFrameData[stackIdx++] = addr;
  5887. //*outIsAddr = true;
  5888. *outAddrType = DbgAddrType_Target;
  5889. }
  5890. break;
  5891. case DW_OP_deref:
  5892. {
  5893. addr_target addr = stackFrameData[--stackIdx];
  5894. addr_target value = mDebugger->ReadMemory<addr_target>(addr);
  5895. stackFrameData[stackIdx++] = value;
  5896. }
  5897. break;
  5898. case DW_OP_fbreg:
  5899. {
  5900. if (registers == NULL)
  5901. return 0;
  5902. BF_ASSERT(dwSubprogram != NULL);
  5903. DbgSubprogram* nonInlinedSubProgram = dwSubprogram->GetRootInlineParent();
  5904. if (nonInlinedSubProgram->mFrameBaseData == NULL)
  5905. {
  5906. *outAddrType = DbgAddrType_Target; //TODO: why?
  5907. return 0;
  5908. }
  5909. BF_ASSERT(nonInlinedSubProgram->mFrameBaseData != NULL);
  5910. intptr loc = EvaluateLocation(nonInlinedSubProgram, nonInlinedSubProgram->mFrameBaseData, nonInlinedSubProgram->mFrameBaseLen, stackFrame, outAddrType, DbgEvalLocFlag_DisallowReg);
  5911. int64 offset = DecodeSLEB128(locData);
  5912. loc += offset;
  5913. //loc = BfDebuggerReadMemory(loc);
  5914. //*outIsAddr = true;
  5915. *outAddrType = DbgAddrType_Target;
  5916. stackFrameData[stackIdx++] = (addr_target)loc;
  5917. }
  5918. break;
  5919. case DW_OP_reg0:
  5920. case DW_OP_reg1:
  5921. case DW_OP_reg2:
  5922. case DW_OP_reg3:
  5923. case DW_OP_reg4:
  5924. case DW_OP_reg5:
  5925. case DW_OP_reg6:
  5926. case DW_OP_reg7:
  5927. case DW_OP_reg8:
  5928. case DW_OP_reg9:
  5929. case DW_OP_reg10:
  5930. case DW_OP_reg11:
  5931. case DW_OP_reg12:
  5932. case DW_OP_reg13:
  5933. case DW_OP_reg14:
  5934. case DW_OP_reg15:
  5935. if (registers == NULL)
  5936. return 0;
  5937. BF_ASSERT((opCode - DW_OP_reg0) < CPURegisters::kNumIntRegs);
  5938. regNum = opCode - DW_OP_reg0;
  5939. stackFrameData[stackIdx++] = registers->mIntRegsArray[regNum];
  5940. *outAddrType = DbgAddrType_Register;
  5941. break;
  5942. case DW_OP_reg21: //XMM0
  5943. BF_FATAL("XMM registers not supported yet");
  5944. break;
  5945. case DW_OP_breg0:
  5946. case DW_OP_breg1:
  5947. case DW_OP_breg2:
  5948. case DW_OP_breg3:
  5949. case DW_OP_breg4:
  5950. case DW_OP_breg5:
  5951. case DW_OP_breg6:
  5952. case DW_OP_breg7:
  5953. case DW_OP_breg8:
  5954. case DW_OP_breg9:
  5955. case DW_OP_breg10:
  5956. case DW_OP_breg11:
  5957. case DW_OP_breg12:
  5958. case DW_OP_breg13:
  5959. case DW_OP_breg14:
  5960. case DW_OP_breg15:
  5961. {
  5962. if (registers == NULL)
  5963. return 0;
  5964. int64 offset = DecodeSLEB128(locData);
  5965. BF_ASSERT((opCode - DW_OP_breg0) < CPURegisters::kNumIntRegs);
  5966. auto loc = registers->mIntRegsArray[opCode - DW_OP_breg0] + offset;
  5967. //loc = BfDebuggerReadMemory(loc);
  5968. //*outIsAddr = true;
  5969. *outAddrType = DbgAddrType_Target;
  5970. stackFrameData[stackIdx++] = (addr_target)loc;
  5971. }
  5972. break;
  5973. case DW_OP_bregx:
  5974. {
  5975. if (registers == NULL)
  5976. return 0;
  5977. int regNum = (int)DecodeULEB128(locData);
  5978. int64 offset = DecodeSLEB128(locData);
  5979. BF_ASSERT(regNum < CPURegisters::kNumIntRegs);
  5980. auto loc = registers->mIntRegsArray[regNum] + offset;
  5981. //loc = BfDebuggerReadMemory(loc);
  5982. //*outIsAddr = true;
  5983. *outAddrType = DbgAddrType_Target;
  5984. stackFrameData[stackIdx++] = (addr_target)loc;
  5985. }
  5986. break;
  5987. case DW_OP_const4u:
  5988. {
  5989. uint32 val = GET_FROM(locData, uint32);
  5990. stackFrameData[stackIdx++] = val;
  5991. }
  5992. break;
  5993. case DW_OP_const8u:
  5994. {
  5995. uint64 val = GET_FROM(locData, uint64);
  5996. stackFrameData[stackIdx++] = (addr_target)val;
  5997. }
  5998. break;
  5999. case DW_OP_GNU_push_tls_address:
  6000. {
  6001. if ((mTLSAddr == 0) || (mTLSIndexAddr == 0))
  6002. return 0;
  6003. int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
  6004. addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
  6005. intptr_target tlsValueIndex = stackFrameData[--stackIdx];
  6006. stackFrameData[stackIdx++] = (tlsValueIndex - mTLSAddr) + tlsEntry;
  6007. *outAddrType = DbgAddrType_Target;
  6008. }
  6009. break;
  6010. case DW_OP_nop:
  6011. break;
  6012. default:
  6013. BF_FATAL("Unknown DW_OP");
  6014. break;
  6015. }
  6016. }
  6017. if (*outAddrType == DbgAddrType_Register)
  6018. {
  6019. if (allowReg)
  6020. return regNum;
  6021. *outAddrType = DbgAddrType_Value;
  6022. }
  6023. //BF_ASSERT(stackIdx == 1);
  6024. return stackFrameData[--stackIdx];
  6025. }
  6026. intptr DbgModule::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, DbgEvalLocFlags flags)
  6027. {
  6028. BP_ZONE("DebugTarget::EvaluateLocation");
  6029. auto dbgModule = this;
  6030. if (locDataLen == DbgLocationLenKind_SegPlusOffset)
  6031. {
  6032. BF_ASSERT(dbgModule->mDbgFlavor == DbgFlavor_MS);
  6033. if (dbgModule->mDbgFlavor == DbgFlavor_MS)
  6034. {
  6035. COFF* coff = (COFF*)dbgModule;
  6036. struct SegOfsData
  6037. {
  6038. uint32 mOfs;
  6039. uint16 mSeg;
  6040. };
  6041. SegOfsData* segOfsData = (SegOfsData*)locData;
  6042. *outAddrType = DbgAddrType_Target;
  6043. return coff->GetSectionAddr(segOfsData->mSeg, segOfsData->mOfs);
  6044. }
  6045. else
  6046. {
  6047. *outAddrType = DbgAddrType_Target;
  6048. return 0;
  6049. }
  6050. }
  6051. CPURegisters* registers = NULL;
  6052. if (stackFrame != NULL)
  6053. registers = &stackFrame->mRegisters;
  6054. if (locDataLen < 0)
  6055. {
  6056. if (registers == NULL)
  6057. return 0;
  6058. int64 ipAddr = stackFrame->GetSourcePC();
  6059. const uint8* checkLocData = locData;
  6060. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6061. int64 endLoc = startLoc + GET_FROM(checkLocData, uint16);
  6062. BF_ASSERT(dwSubprogram != NULL);
  6063. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6064. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6065. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6066. {
  6067. locDataLen = -locDataLen - sizeof(addr_target) - sizeof(uint16);
  6068. locData = checkLocData;
  6069. }
  6070. else
  6071. {
  6072. *outAddrType = DbgAddrType_OptimizedOut;
  6073. return 0;
  6074. }
  6075. }
  6076. else if (locDataLen == 0)
  6077. {
  6078. if (registers == NULL)
  6079. return 0;
  6080. int64 ipAddr = stackFrame->GetSourcePC();
  6081. const uint8* checkLocData = locData;
  6082. while (true)
  6083. {
  6084. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6085. int64 endLoc = (int64)GET_FROM(checkLocData, addr_target);
  6086. if ((startLoc == 0) && (endLoc == 0))
  6087. {
  6088. *outAddrType = DbgAddrType_OptimizedOut;
  6089. return 0;
  6090. }
  6091. BF_ASSERT(dwSubprogram != NULL);
  6092. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6093. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6094. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6095. {
  6096. locDataLen = GET_FROM(checkLocData, int16);
  6097. locData = checkLocData;
  6098. break;
  6099. }
  6100. else
  6101. {
  6102. int len = GET_FROM(checkLocData, int16);;
  6103. checkLocData += len;
  6104. }
  6105. }
  6106. }
  6107. return ExecuteOps(dwSubprogram, locData, locDataLen, stackFrame, registers, outAddrType, flags);
  6108. }
  6109. void DbgModule::ProcessHotSwapVariables()
  6110. {
  6111. BP_ZONE("DbgModule::ProcessHotSwapVariables");
  6112. auto linkedModule = GetLinkedModule();
  6113. for (auto staticVariable : mStaticVariables)
  6114. {
  6115. bool replaceVariable = false;
  6116. const char* findName = staticVariable->GetMappedName();
  6117. auto itr = linkedModule->mStaticVariableMap.find(findName);
  6118. if (itr != linkedModule->mStaticVariableMap.end())
  6119. {
  6120. DbgVariable* oldVariable = itr->second;
  6121. // If the old static field has the same type as the new static field then we keep the same
  6122. // address, otherwise we use the new (zeroed-out) allocated space
  6123. auto _GetNewAddress = [&]()
  6124. {
  6125. addr_target newAddress = 0;
  6126. if (mDbgFlavor == DbgFlavor_GNU)
  6127. {
  6128. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6129. }
  6130. else
  6131. {
  6132. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6133. // the location in our own mSymbolNameMap
  6134. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6135. if (entry != NULL)
  6136. newAddress = entry->mValue->mAddress;
  6137. }
  6138. return newAddress;
  6139. };
  6140. if (oldVariable->mType->IsSizedArray())
  6141. {
  6142. mDebugTarget->GetCompilerSettings();
  6143. bool doMerge = strstr(oldVariable->mName, "sBfClassVData") != NULL;
  6144. bool keepInPlace = (doMerge) && (strstr(oldVariable->mName, ".vext") == NULL);
  6145. if (doMerge)
  6146. {
  6147. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6148. addr_target newAddress = _GetNewAddress();
  6149. if (newAddress == 0)
  6150. continue;
  6151. uint8* newData = GetHotTargetData(newAddress);
  6152. int newArraySize = (int)staticVariable->mType->GetByteCount();
  6153. int oldArraySize = (int)oldVariable->mType->GetByteCount();
  6154. int copySize = std::min(newArraySize, oldArraySize);
  6155. BF_ASSERT((oldArraySize & (sizeof(addr_target) - 1)) == 0);
  6156. DbgModule* defModule = oldVariable->mType->mCompileUnit->mDbgModule;
  6157. defModule->EnableWriting(oldAddress);
  6158. uint8* mergedData = new uint8[copySize];
  6159. mDebugger->ReadMemory(oldAddress, copySize, mergedData);
  6160. // The new vtable may have 0's in it when virtual methods are removed. Keep the old virtual addresses in those.
  6161. addr_target* newDataPtr = (addr_target*)newData;
  6162. addr_target* mergedPtr = (addr_target*)mergedData;
  6163. while (mergedPtr < (addr_target*)(mergedData + copySize))
  6164. {
  6165. if (*newDataPtr != 0)
  6166. *mergedPtr = *newDataPtr;
  6167. mergedPtr++;
  6168. newDataPtr++;
  6169. }
  6170. bool success;
  6171. success = mDebugger->WriteMemory(oldAddress, mergedData, copySize);
  6172. BF_ASSERT(success);
  6173. memcpy(newData, mergedData, copySize);
  6174. delete mergedData;
  6175. }
  6176. else if (strstr(oldVariable->mName, "sStringLiterals") != NULL)
  6177. {
  6178. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6179. addr_target newAddress = NULL;
  6180. if (mDbgFlavor == DbgFlavor_GNU)
  6181. {
  6182. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6183. }
  6184. else
  6185. {
  6186. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6187. // the location in our own mSymbolNameMap
  6188. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6189. if (entry == NULL)
  6190. continue;
  6191. newAddress = entry->mValue->mAddress;
  6192. }
  6193. // Make sure newAddress doesn't have anything linked to it
  6194. addr_target val = 0;
  6195. bool success = mDebugger->ReadMemory((intptr)newAddress, sizeof(addr_target), &val);
  6196. BF_ASSERT(success);
  6197. BF_ASSERT(val == 0);
  6198. // Link the new table to the old extended table
  6199. addr_target prevLinkage = 0;
  6200. success = mDebugger->ReadMemory((intptr)oldAddress, sizeof(addr_target), &prevLinkage);
  6201. BF_ASSERT(success);
  6202. success = mDebugger->WriteMemory((intptr)newAddress, &prevLinkage, sizeof(addr_target));
  6203. BF_ASSERT(success);
  6204. mDebugger->EnableWriting((intptr)oldAddress, sizeof(addr_target));
  6205. success = mDebugger->WriteMemory((intptr)oldAddress, &newAddress, sizeof(addr_target));
  6206. BF_ASSERT(success);
  6207. keepInPlace = true;
  6208. }
  6209. if (keepInPlace)
  6210. {
  6211. // We have to maintain the OLD size because we can't overwrite the original bounds
  6212. staticVariable->mType = oldVariable->mType;
  6213. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6214. staticVariable->mLocationData = oldVariable->mLocationData;
  6215. staticVariable->mCompileUnit = oldVariable->mCompileUnit;
  6216. }
  6217. }
  6218. else if (oldVariable->mType->Equals(staticVariable->mType))
  6219. {
  6220. if (oldVariable->mType->IsStruct())
  6221. {
  6222. if ((strncmp(oldVariable->mName, "?sBfTypeData@", 13) == 0) || (strncmp(oldVariable->mName, "sBfTypeData.", 12) == 0))
  6223. {
  6224. int size = (int)staticVariable->mType->GetByteCount();
  6225. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6226. addr_target newAddress = _GetNewAddress();
  6227. if (newAddress == 0)
  6228. continue;
  6229. uint8* data = new uint8[size];
  6230. bool success = mDebugger->ReadMemory(newAddress, size, data);
  6231. if (success)
  6232. {
  6233. mDebugger->EnableWriting((intptr)oldAddress, size);
  6234. success = mDebugger->WriteMemory(oldAddress, data, size);
  6235. }
  6236. delete data;
  6237. BF_ASSERT(success);
  6238. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6239. staticVariable->mLocationData = oldVariable->mLocationData;
  6240. }
  6241. }
  6242. //staticVariable->mLocationLen = oldVariable->mLocationLen;
  6243. //staticVariable->mLocationData = oldVariable->mLocationData;
  6244. replaceVariable = false;
  6245. }
  6246. else
  6247. {
  6248. BF_ASSERT(!oldVariable->mType->IsSizedArray());
  6249. }
  6250. if (!replaceVariable)
  6251. {
  6252. auto symbolVal = linkedModule->mSymbolNameMap.Find(staticVariable->GetMappedName());
  6253. if (symbolVal != NULL)
  6254. {
  6255. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6256. DbgSymbol* oldSymbol = mDebugTarget->mSymbolMap.Get(oldAddress);
  6257. if (oldSymbol != NULL)
  6258. symbolVal->mValue = oldSymbol;
  6259. }
  6260. }
  6261. }
  6262. else // Not found - new variable
  6263. replaceVariable = true;
  6264. if (replaceVariable)
  6265. {
  6266. linkedModule->mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  6267. }
  6268. }
  6269. }
  6270. int64 DbgModule::GetImageSize()
  6271. {
  6272. return mImageSize;
  6273. }
  6274. /*const uint8* DbgModule::GetOrigImageData(addr_target address)
  6275. {
  6276. return mOrigImageData + (address - mImageBase);
  6277. }*/
  6278. DbgFileExistKind DbgModule::CheckSourceFileExist(const StringImpl& path)
  6279. {
  6280. DbgFileExistKind existsKind = DbgFileExistKind_NotFound;
  6281. if (FileExists(path))
  6282. existsKind = DbgFileExistKind_Found;
  6283. String oldSourceCommand = GetOldSourceCommand(path);
  6284. if (!oldSourceCommand.IsEmpty())
  6285. {
  6286. int crPos = (int)oldSourceCommand.IndexOf('\n');
  6287. if (crPos != -1)
  6288. {
  6289. String targetPath = oldSourceCommand.Substring(0, crPos);
  6290. if (FileExists(targetPath))
  6291. existsKind = DbgFileExistKind_Found;
  6292. else
  6293. existsKind = DbgFileExistKind_HasOldSourceCommand;
  6294. }
  6295. }
  6296. return existsKind;
  6297. }
  6298. void DbgModule::EnableWriting(addr_target address)
  6299. {
  6300. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6301. {
  6302. DbgSection* section = &mSections[sectionIdx];
  6303. if ((address >= mImageBase + section->mAddrStart) && (address < mImageBase + section->mAddrStart + section->mAddrLength))
  6304. {
  6305. if (!section->mWritingEnabled)
  6306. {
  6307. section->mOldProt = mDebugger->EnableWriting(mImageBase + section->mAddrStart, (int32)section->mAddrLength);
  6308. section->mWritingEnabled = true;
  6309. }
  6310. }
  6311. }
  6312. }
  6313. void DbgModule::RevertWritingEnable()
  6314. {
  6315. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6316. {
  6317. DbgSection* section = &mSections[sectionIdx];
  6318. if (section->mWritingEnabled)
  6319. {
  6320. mDebugger->SetProtection(mImageBase + section->mAddrStart, (int32)section->mAddrLength, section->mOldProt);
  6321. section->mWritingEnabled = false;
  6322. }
  6323. }
  6324. }
  6325. template <typename TRadixMap>
  6326. static void RemoveInvalidRange(TRadixMap& radixMap, addr_target startAddr, int addrLength)
  6327. {
  6328. radixMap.RemoveRange(startAddr, addrLength);
  6329. }
  6330. template <typename TMap>
  6331. static void RemoveInvalidMapRange(TMap& map, addr_target startAddr, int addrLength)
  6332. {
  6333. auto itr = map.lower_bound(startAddr);
  6334. while (itr != map.end())
  6335. {
  6336. auto val = itr->first;
  6337. if (val >= startAddr + addrLength)
  6338. return;
  6339. itr = map.erase(itr);
  6340. }
  6341. }
  6342. void DbgModule::RemoveTargetData()
  6343. {
  6344. BP_ZONE("DbgModule::RemoveTargetData");
  6345. for (auto srcFileRef : mSrcFileDeferredRefs)
  6346. srcFileRef->RemoveDeferredRefs(this);
  6347. HashSet<DbgSrcFile*> visitedFiles;
  6348. for (auto compileUnit : mCompileUnits)
  6349. {
  6350. for (auto& fileRef : compileUnit->mSrcFileRefs)
  6351. {
  6352. if (visitedFiles.Add(fileRef.mSrcFile))
  6353. {
  6354. fileRef.mSrcFile->RemoveLines(this);
  6355. }
  6356. }
  6357. }
  6358. RemoveInvalidRange(mDebugTarget->mSymbolMap, (addr_target)mImageBase, (int32)mImageSize);
  6359. RemoveInvalidRange(mDebugTarget->mSubprogramMap, (addr_target)mImageBase, (int32)mImageSize);
  6360. RemoveInvalidRange(mDebugTarget->mExceptionDirectoryMap, (addr_target)mImageBase, (int32)mImageSize);
  6361. RemoveInvalidRange(mDebugTarget->mContribMap, (addr_target)mImageBase, (int32)mImageSize);
  6362. RemoveInvalidMapRange(mDebugTarget->mDwFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6363. RemoveInvalidMapRange(mDebugTarget->mCOFFFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6364. //mDebugTarget->mDwFrameDescriptorMap.erase()
  6365. // Remove any of our entries from the mHotReplacedMethodList from 'primary modules' that are not going away
  6366. for (auto dbgType : mHotPrimaryTypes)
  6367. {
  6368. DbgSubprogram** nextSrc = &dbgType->mHotReplacedMethodList.mHead;
  6369. while (*nextSrc != NULL)
  6370. {
  6371. auto* subprogram = *nextSrc;
  6372. if (subprogram->mCompileUnit->mDbgModule == this)
  6373. *nextSrc = subprogram->mNext;
  6374. else
  6375. nextSrc = &(*nextSrc)->mNext;;
  6376. }
  6377. }
  6378. }
  6379. void DbgModule::ReportMemory(MemReporter* memReporter)
  6380. {
  6381. //memReporter->Add("BumpAlloc_Used", mAlloc.GetAllocSize());
  6382. //memReporter->Add("BumpAlloc_Unused", mAlloc.GetTotalAllocSize() - mAlloc.GetAllocSize());
  6383. memReporter->AddBumpAlloc("BumpAlloc", mAlloc);
  6384. memReporter->AddVec(mTypes);
  6385. memReporter->AddVec(mSubprograms);
  6386. //memReporter->Add("TypeMap", mTypeMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6387. memReporter->AddHashSet("TypeMap", mTypeMap.mMap);
  6388. memReporter->Add("SymbolNameMap", mSymbolNameMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6389. if (mOrigImageData != NULL)
  6390. {
  6391. memReporter->BeginSection("OrigImageData");
  6392. mOrigImageData->ReportMemory(memReporter);
  6393. memReporter->EndSection();
  6394. }
  6395. }
  6396. DbgType* DbgModule::GetPointerType(DbgType* innerType)
  6397. {
  6398. auto linkedModule = GetLinkedModule();
  6399. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6400. if (innerType->mPtrType == NULL)
  6401. {
  6402. BP_ALLOC_T(DbgType);
  6403. auto ptrType = mAlloc.Alloc<DbgType>();
  6404. ptrType->mCompileUnit = innerType->mCompileUnit;
  6405. ptrType->mLanguage = innerType->mLanguage;
  6406. ptrType->mTypeCode = DbgType_Ptr;
  6407. ptrType->mTypeParam = innerType;
  6408. ptrType->mSize = sizeof(addr_target);
  6409. ptrType->mAlign = (int)ptrType->mSize;
  6410. ptrType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6411. linkedModule->mTypes.push_back(ptrType);
  6412. innerType->mPtrType = ptrType;
  6413. }
  6414. return innerType->mPtrType;
  6415. }
  6416. DbgType* DbgModule::GetConstType(DbgType* innerType)
  6417. {
  6418. auto linkedModule = GetLinkedModule();
  6419. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6420. /*auto itr = linkedModule->mConstTypes.find(innerType);
  6421. if (itr != linkedModule->mConstTypes.end())
  6422. return itr->second;*/
  6423. DbgType* constType = NULL;
  6424. if (linkedModule->mConstTypes.TryGetValue(innerType, &constType))
  6425. return constType;
  6426. BP_ALLOC_T(DbgType);
  6427. constType = mAlloc.Alloc<DbgType>();
  6428. constType->mCompileUnit = innerType->mCompileUnit;
  6429. constType->mLanguage = innerType->mLanguage;
  6430. constType->mTypeCode = DbgType_Const;
  6431. constType->mTypeParam = innerType;
  6432. constType->mSize = sizeof(addr_target);
  6433. constType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6434. linkedModule->mTypes.push_back(constType);
  6435. linkedModule->mConstTypes[innerType] = constType;
  6436. return constType;
  6437. }
  6438. DbgType* DbgModule::GetPrimaryType(DbgType* dbgType)
  6439. {
  6440. if (dbgType->mPriority <= DbgTypePriority_Normal)
  6441. {
  6442. if ((dbgType->mLanguage == DbgLanguage_Beef) && (dbgType->mName != NULL))
  6443. {
  6444. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6445. if (newTypeEntry != NULL)
  6446. {
  6447. DbgType* newType = newTypeEntry->mValue;
  6448. if ((newType->mTypeCode == DbgType_Ptr) && (newType->IsBfObjectPtr()))
  6449. newType = newType->mTypeParam;
  6450. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6451. return newType;
  6452. }
  6453. }
  6454. else if (dbgType->mName != NULL)
  6455. {
  6456. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6457. if (newTypeEntry != NULL)
  6458. {
  6459. DbgType* newType = newTypeEntry->mValue;
  6460. newType = newType->RemoveModifiers();
  6461. if (newType != dbgType)
  6462. newType = GetPrimaryType(newType);
  6463. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6464. return newType;
  6465. }
  6466. }
  6467. }
  6468. return dbgType;
  6469. }
  6470. DbgType* DbgModule::GetInnerTypeOrVoid(DbgType* dbgType)
  6471. {
  6472. if (dbgType->mTypeParam != NULL)
  6473. return dbgType->mTypeParam;
  6474. return GetPrimitiveType(DbgType_Void, dbgType->mLanguage);
  6475. }
  6476. DbgType* DbgModule::FindTypeHelper(const String& typeName, DbgType* checkType)
  6477. {
  6478. for (auto subType : checkType->mSubTypeList)
  6479. {
  6480. if (strcmp(subType->mTypeName, typeName.c_str()) == 0)
  6481. return subType;
  6482. }
  6483. for (auto baseType : checkType->mBaseTypes)
  6484. {
  6485. auto retType = FindTypeHelper(typeName, baseType->mBaseType);
  6486. if (retType != NULL)
  6487. return retType;
  6488. }
  6489. return NULL;
  6490. }
  6491. DbgType* DbgModule::FindType(const String& typeName, DbgType* contextType, DbgLanguage language, bool bfObjectPtr)
  6492. {
  6493. if ((language == DbgLanguage_Unknown) && (contextType != NULL))
  6494. language = contextType->mLanguage;
  6495. if (typeName.length() > 0)
  6496. {
  6497. if (typeName[typeName.length() - 1] == '*')
  6498. {
  6499. DbgType* dbgType = FindType(typeName.Substring(0, typeName.length() - 1), contextType, language, bfObjectPtr);
  6500. if (dbgType == NULL)
  6501. return NULL;
  6502. return GetPointerType(dbgType);
  6503. }
  6504. }
  6505. auto entry = GetLinkedModule()->mTypeMap.Find(typeName.c_str(), language);
  6506. if (entry != NULL)
  6507. {
  6508. if ((bfObjectPtr) && (entry->mValue->IsBfObject()))
  6509. return GetPointerType(entry->mValue);
  6510. return entry->mValue;
  6511. }
  6512. if (contextType != NULL)
  6513. {
  6514. DbgType* checkType = contextType;
  6515. if (checkType->IsPointer())
  6516. checkType = checkType->mTypeParam;
  6517. return FindTypeHelper(typeName, checkType);
  6518. }
  6519. return NULL;
  6520. }
  6521. DbgTypeMap::Entry* DbgModule::FindType(const char* typeName, DbgLanguage language)
  6522. {
  6523. return GetLinkedModule()->mTypeMap.Find(typeName, language);
  6524. /*auto& typeMap = GetLinkedModule()->mTypeMap;
  6525. auto dbgTypeEntry = typeMap.Find(typeName);
  6526. if (dbgTypeEntry == NULL)
  6527. return NULL;
  6528. if (dbgTypeEntry->mValue->mLanguage == language)
  6529. return dbgTypeEntry;
  6530. while (dbgTypeEntry != NULL)
  6531. {
  6532. DbgType* dbgType = dbgTypeEntry->mValue;
  6533. if ((dbgType->GetLanguage() == language) && (typeMap.StrEqual(dbgType->mName, typeName)))
  6534. return dbgTypeEntry;
  6535. dbgTypeEntry = dbgTypeEntry->mNext;
  6536. }*/
  6537. //return NULL;
  6538. }
  6539. DbgType* DbgModule::GetPrimitiveType(DbgTypeCode typeCode, DbgLanguage language)
  6540. {
  6541. if (language == DbgLanguage_Beef)
  6542. return mBfPrimitiveTypes[(int)typeCode];
  6543. else
  6544. return mCPrimitiveTypes[(int)typeCode];
  6545. }
  6546. DbgType* DbgModule::GetPrimitiveStructType(DbgTypeCode typeCode)
  6547. {
  6548. const char* name = mPrimitiveStructNames[typeCode];
  6549. if (name == NULL)
  6550. return NULL;
  6551. return FindType(name, NULL, DbgLanguage_Beef);
  6552. }
  6553. DbgType* DbgModule::GetSizedArrayType(DbgType * elementType, int count)
  6554. {
  6555. auto linkedModule = GetLinkedModule();
  6556. if ((linkedModule != NULL) && (linkedModule != this))
  6557. {
  6558. return linkedModule->GetSizedArrayType(elementType, count);
  6559. }
  6560. DbgType** sizedArrayTypePtr;
  6561. DbgSizedArrayEntry entry;
  6562. entry.mElementType = elementType;
  6563. entry.mCount = count;
  6564. if (mSizedArrayTypes.TryAdd(entry, NULL, &sizedArrayTypePtr))
  6565. {
  6566. BP_ALLOC_T(DbgType);
  6567. auto sizedArrayType = mAlloc.Alloc<DbgType>();
  6568. sizedArrayType->mCompileUnit = elementType->mCompileUnit;
  6569. sizedArrayType->mLanguage = elementType->mLanguage;
  6570. sizedArrayType->mTypeCode = DbgType_SizedArray;
  6571. sizedArrayType->mTypeParam = elementType;
  6572. sizedArrayType->mSize = count * elementType->GetStride();
  6573. sizedArrayType->mAlign = elementType->GetAlign();
  6574. sizedArrayType->mSizeCalculated = true;
  6575. sizedArrayType->mTypeIdx = (int32)mTypes.size();
  6576. linkedModule->mTypes.push_back(sizedArrayType);
  6577. *sizedArrayTypePtr = sizedArrayType;
  6578. }
  6579. return *sizedArrayTypePtr;
  6580. }