DbgModule.cpp 199 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. mId = 0;
  1941. mStartSubprogramIdx = 0;
  1942. mEndSubprogramIdx = 0;
  1943. mCodeAddress = NULL;
  1944. mMayBeOld = false;
  1945. mTimeStamp = 0;
  1946. mExpectedFileSize = 0;
  1947. mBfTypeType = NULL;
  1948. mBfTypesInfoAddr = 0;
  1949. mImageBase = 0;
  1950. mPreferredImageBase = 0;
  1951. mImageSize = 0;
  1952. mOrigImageData = NULL;
  1953. mDeleting = false;
  1954. mAllocSizeData = 0;
  1955. mParsedSymbolData = false;
  1956. mParsedTypeData = false;
  1957. mParsedGlobalsData = false;
  1958. mPopulatedStaticVariables = false;
  1959. mParsedFrameDescriptors = false;
  1960. mTLSAddr = 0;
  1961. mTLSSize = 0;
  1962. mTLSExtraAddr = 0;
  1963. mTLSExtraSize = 0;
  1964. mTLSIndexAddr = 0;
  1965. mDbgFlavor = DbgFlavor_Unknown;
  1966. mMasterCompileUnit = NULL;
  1967. }
  1968. DbgModule::~DbgModule()
  1969. {
  1970. delete mMemReporter;
  1971. for (auto dwSrcFile : mEmptySrcFiles)
  1972. delete dwSrcFile;
  1973. for (auto dwCompileUnit : mCompileUnits)
  1974. delete dwCompileUnit;
  1975. delete mSymbolData;
  1976. delete mStringTable;
  1977. delete mDebugLineData;
  1978. delete mDebugInfoData;
  1979. delete mDebugPubNames;
  1980. delete mDebugFrameData;
  1981. delete mDebugLocationData;
  1982. delete mDebugRangesData;
  1983. delete mDebugAbbrevData;
  1984. delete mDebugAbbrevPtrData;
  1985. delete mDebugStrData;
  1986. for (auto entry : mExceptionDirectory)
  1987. delete entry.mData;
  1988. delete mEHFrameData;
  1989. delete mOrigImageData;
  1990. if ((IsObjectFile()) && (mImageBase != 0))
  1991. {
  1992. mDebugger->ReleaseHotTargetMemory((addr_target)mImageBase, (int)mImageSize);
  1993. }
  1994. for (auto data : mOwnedSectionData)
  1995. delete data;
  1996. }
  1997. DbgSubprogram* DbgModule::FindSubprogram(DbgType* dbgType, const char * methodName)
  1998. {
  1999. dbgType = dbgType->GetPrimaryType();
  2000. dbgType->PopulateType();
  2001. if (dbgType->mNeedsGlobalsPopulated)
  2002. PopulateTypeGlobals(dbgType);
  2003. for (auto methodNameEntry : dbgType->mMethodNameList)
  2004. {
  2005. if ((methodNameEntry->mCompileUnitId != -1) && (strcmp(methodNameEntry->mName, methodName) == 0))
  2006. {
  2007. // If we hot-replaced this type then we replaced and parsed all the methods too
  2008. if (!dbgType->mCompileUnit->mDbgModule->IsObjectFile())
  2009. dbgType->mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  2010. methodNameEntry->mCompileUnitId = -1;
  2011. }
  2012. }
  2013. DbgSubprogram* result = NULL;
  2014. for (auto method : dbgType->mMethodList)
  2015. {
  2016. if (strcmp(method->mName, methodName) == 0)
  2017. {
  2018. method->PopulateSubprogram();
  2019. if ((result == NULL) || (method->mBlock.mLowPC != 0))
  2020. result = method;
  2021. }
  2022. }
  2023. return result;
  2024. }
  2025. void DbgModule::Fail(const StringImpl& error)
  2026. {
  2027. if (mFailMsgPtr != NULL)
  2028. {
  2029. if (mFailMsgPtr->IsEmpty())
  2030. *mFailMsgPtr = error;
  2031. }
  2032. String errorStr = "error ";
  2033. if (!mFilePath.IsEmpty())
  2034. {
  2035. errorStr += "Error in ";
  2036. errorStr += mFilePath;
  2037. errorStr += ": ";
  2038. }
  2039. errorStr += error;
  2040. errorStr += "\n";
  2041. mDebugger->OutputRawMessage(errorStr);
  2042. mFailed = true;
  2043. }
  2044. void DbgModule::HardFail(const StringImpl& error)
  2045. {
  2046. if (mFailMsgPtr != NULL)
  2047. {
  2048. if (mFailMsgPtr->IsEmpty())
  2049. *mFailMsgPtr = error;
  2050. }
  2051. String errorStr;
  2052. if (!mFilePath.IsEmpty())
  2053. {
  2054. errorStr += "Error in ";
  2055. errorStr += mFilePath;
  2056. errorStr += ": ";
  2057. }
  2058. errorStr += error;
  2059. errorStr += "\n";
  2060. BF_FATAL(errorStr.c_str());
  2061. }
  2062. char* DbgModule::DbgDupString(const char* str, const char* allocName)
  2063. {
  2064. int strLen = (int)strlen(str);
  2065. if (strLen == 0)
  2066. return NULL;
  2067. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, (allocName != NULL) ? allocName : "DbgDupString");
  2068. memcpy(dupStr, str, strLen);
  2069. return dupStr;
  2070. }
  2071. DbgModule* DbgModule::GetLinkedModule()
  2072. {
  2073. if (IsObjectFile())
  2074. return mDebugTarget->mTargetBinary;
  2075. return this;
  2076. }
  2077. addr_target DbgModule::GetTargetImageBase()
  2078. {
  2079. if (IsObjectFile())
  2080. return (addr_target)mDebugTarget->mTargetBinary->mImageBase;
  2081. return (addr_target)mImageBase;
  2082. }
  2083. void DbgModule::ParseGlobalsData()
  2084. {
  2085. mParsedGlobalsData = true;
  2086. }
  2087. void DbgModule::ParseSymbolData()
  2088. {
  2089. mParsedSymbolData = true;
  2090. }
  2091. void DbgModule::ParseTypeData()
  2092. {
  2093. mParsedTypeData = true;
  2094. }
  2095. DbgCompileUnit* DbgModule::ParseCompileUnit(int compileUnitId)
  2096. {
  2097. return NULL;
  2098. }
  2099. void DbgModule::MapCompileUnitMethods(DbgCompileUnit * compileUnit)
  2100. {
  2101. }
  2102. void DbgModule::MapCompileUnitMethods(int compileUnitId)
  2103. {
  2104. }
  2105. void DbgModule::PopulateType(DbgType* dbgType)
  2106. {
  2107. }
  2108. void DbgModule::PopulateTypeGlobals(DbgType* dbgType)
  2109. {
  2110. }
  2111. void DbgModule::PopulateStaticVariableMap()
  2112. {
  2113. if (mPopulatedStaticVariables)
  2114. return;
  2115. for (auto staticVariable : mStaticVariables)
  2116. {
  2117. mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  2118. }
  2119. mPopulatedStaticVariables = true;
  2120. }
  2121. void DbgModule::ProcessDebugInfo()
  2122. {
  2123. }
  2124. addr_target DbgModule::RemapAddr(addr_target addr)
  2125. {
  2126. if ((addr != 0) && (mPreferredImageBase != 0) && (mImageBase != 0))
  2127. return addr + (intptr_target)(mImageBase - mPreferredImageBase);
  2128. return addr;
  2129. }
  2130. void DbgModule::ParseAbbrevData(const uint8* data)
  2131. {
  2132. while (true)
  2133. {
  2134. int abbrevIdx = (int)DecodeULEB128(data);
  2135. mDebugAbbrevPtrData[abbrevIdx] = data;
  2136. if (abbrevIdx == 0)
  2137. break;
  2138. int entryTag = (int)DecodeULEB128(data);
  2139. bool hasChildren = GET(char) == DW_CHILDREN_yes;
  2140. while (true)
  2141. {
  2142. int attrName = (int)DecodeULEB128(data);
  2143. int form = (int)DecodeULEB128(data);
  2144. if ((attrName == 0) && (form == 0))
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. void DbgModule::ParseExceptionData()
  2150. {
  2151. if (mExceptionDirectory.IsEmpty())
  2152. return;
  2153. BP_ZONE("DbgModule::ParseExceptionData");
  2154. for (auto entry : mExceptionDirectory)
  2155. {
  2156. const uint8* data = entry.mData;
  2157. const uint8* dataEnd = data + entry.mSize;
  2158. static int entryCount = 0;
  2159. addr_target imageBase = GetTargetImageBase();
  2160. while (data < dataEnd)
  2161. {
  2162. addr_target beginAddress = GET(uint32);
  2163. addr_target endAddress = GET(uint32);
  2164. uint32 unwindData = GET(uint32);
  2165. //TODO: Apparently unwindData can refer to another runtime entry in the .pdata if the LSB is set to 1?
  2166. beginAddress += (addr_target)imageBase;
  2167. endAddress += (addr_target)imageBase;
  2168. int exSize = (int)(endAddress - beginAddress);
  2169. for (int exOffset = 0; true; exOffset += DBG_MAX_LOOKBACK)
  2170. {
  2171. int curSize = exSize - exOffset;
  2172. if (curSize <= 0)
  2173. break;
  2174. BP_ALLOC_T(DbgExceptionDirectoryEntry);
  2175. DbgExceptionDirectoryEntry* exceptionDirectoryEntry = mAlloc.Alloc<DbgExceptionDirectoryEntry>();
  2176. exceptionDirectoryEntry->mAddress = beginAddress + exOffset;
  2177. exceptionDirectoryEntry->mOrigAddressOffset = exOffset;
  2178. exceptionDirectoryEntry->mAddressLength = curSize;
  2179. exceptionDirectoryEntry->mExceptionPos = (int)unwindData;
  2180. exceptionDirectoryEntry->mDbgModule = this;
  2181. mDebugTarget->mExceptionDirectoryMap.Insert(exceptionDirectoryEntry);
  2182. entryCount++;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. static int gIdx = 0;
  2188. template <typename T> static bool IsTypeSigned() { return false; }
  2189. template <> bool IsTypeSigned<int8>() { return true; }
  2190. template <> bool IsTypeSigned<int16>() { return true; }
  2191. template <> bool IsTypeSigned<int32>() { return true; }
  2192. template <> bool IsTypeSigned<int64>() { return true; }
  2193. #pragma warning(push)
  2194. #pragma warning(disable:4302)
  2195. #pragma warning(disable:4311)
  2196. #pragma warning(disable:4312)
  2197. #pragma warning(disable:4800)
  2198. #pragma warning(disable:4800)
  2199. template <typename T>
  2200. T DbgModule::ReadValue(const uint8*& data, int form, int refOffset, const uint8** extraData, const uint8* startData)
  2201. {
  2202. gIdx++;
  2203. switch (form)
  2204. {
  2205. case DW_FORM_strp:
  2206. {
  2207. int strOffset = GET(int);
  2208. BF_ASSERT(mDebugStrData != NULL);
  2209. const char* str = (const char*)mDebugStrData + strOffset;
  2210. return (T)(intptr)str;
  2211. }
  2212. break;
  2213. case DW_FORM_data1:
  2214. {
  2215. if (IsTypeSigned<T>())
  2216. return (T)(intptr)GET(int8);
  2217. else
  2218. return (T)(uintptr)GET(uint8);
  2219. }
  2220. break;
  2221. case DW_FORM_data2:
  2222. {
  2223. if (IsTypeSigned<T>())
  2224. return (T)(intptr)GET(int16);
  2225. else
  2226. return (T)(uintptr)GET(uint16);
  2227. }
  2228. break;
  2229. case DW_FORM_data4:
  2230. {
  2231. if (IsTypeSigned<T>())
  2232. return (T)(intptr)GET(int32);
  2233. else
  2234. return (T)(uintptr)GET(uint32);
  2235. }
  2236. break;
  2237. case DW_FORM_data8:
  2238. {
  2239. if (IsTypeSigned<T>())
  2240. return (T)GET(int64);
  2241. else
  2242. return (T)GET(uint64);
  2243. }
  2244. break;
  2245. case DW_FORM_ref1:
  2246. {
  2247. return (T)(intptr)GET(int8) + refOffset;
  2248. }
  2249. break;
  2250. case DW_FORM_ref2:
  2251. {
  2252. return (T)(intptr)GET(int16) + refOffset;
  2253. }
  2254. break;
  2255. case DW_FORM_ref4:
  2256. {
  2257. return (T)(intptr)GET(int32) + refOffset;
  2258. }
  2259. break;
  2260. case DW_FORM_sec_offset:
  2261. {
  2262. intptr_target offset;
  2263. if (mIsDwarf64)
  2264. offset = (intptr_target)GET(int64);
  2265. else
  2266. offset = GET(int32);
  2267. if (extraData != NULL)
  2268. {
  2269. *extraData = mDebugLocationData + offset;
  2270. return 0;
  2271. }
  2272. return (T)offset;
  2273. }
  2274. break;
  2275. case DW_FORM_addr:
  2276. {
  2277. return (T)GET(addr_target);
  2278. }
  2279. break;
  2280. case DW_FORM_exprloc:
  2281. {
  2282. int64_t exprLen = DecodeULEB128(data);
  2283. const uint8* endData = data + exprLen;
  2284. if (extraData != NULL)
  2285. *extraData = data;
  2286. data = endData;
  2287. return (T)exprLen;
  2288. }
  2289. break;
  2290. case DW_FORM_flag_present:
  2291. {
  2292. //
  2293. return (T)1;
  2294. }
  2295. break;
  2296. case DW_FORM_flag:
  2297. {
  2298. //
  2299. return (T)(intptr)GET(char);
  2300. }
  2301. break;
  2302. case DW_FORM_sdata:
  2303. return (T)DecodeSLEB128(data);
  2304. case DW_FORM_udata:
  2305. return (T)DecodeULEB128(data);
  2306. case DW_FORM_string:
  2307. {
  2308. const char* str = (const char*)data;
  2309. while (true)
  2310. {
  2311. uint8 val = *data;
  2312. data++;
  2313. if (val == 0)
  2314. return (T)(intptr)str;
  2315. }
  2316. }
  2317. case DW_FORM_block:
  2318. {
  2319. int blockLen = (int)DecodeULEB128(data);
  2320. const uint8* retVal = data;
  2321. data += blockLen;
  2322. return (T)(intptr)retVal;
  2323. }
  2324. case DW_FORM_block1:
  2325. {
  2326. int blockLen = (int)*((uint8*)data);
  2327. data += sizeof(uint8);
  2328. const uint8* retVal = data;
  2329. data += blockLen;
  2330. return (T)(intptr)retVal;
  2331. }
  2332. default:
  2333. assert("Not covered!" == 0);
  2334. break;
  2335. }
  2336. return (T)0;
  2337. }
  2338. #pragma warning(pop)
  2339. static int gAbbrevNum = 0;
  2340. DbgType* DbgModule::GetOrCreateType(int typeIdx, DbgDataMap& dataMap)
  2341. {
  2342. if (typeIdx == 0)
  2343. return NULL;
  2344. DbgModule* linkedModule = GetLinkedModule();
  2345. DbgType* dbgType = dataMap.Get<DbgType*>(typeIdx);
  2346. if (dbgType != NULL)
  2347. return dbgType;
  2348. dbgType = mAlloc.Alloc<DbgType>();
  2349. dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
  2350. linkedModule->mTypes.push_back(dbgType);
  2351. dataMap.Set(typeIdx, dbgType);
  2352. return dbgType;
  2353. }
  2354. typedef std::pair<DbgClassType, void*> DataPair;
  2355. typedef llvm::SmallVector<DataPair, 16> DataStack;
  2356. template <typename T>
  2357. T DbgModule::GetOrCreate(int idx, DbgDataMap& dataMap)
  2358. {
  2359. if (idx == 0)
  2360. return NULL;
  2361. T val = dataMap.Get<T>(idx);
  2362. if (val != NULL)
  2363. return val;
  2364. val = mAlloc.Alloc<typename RemoveTypePointer<T>::type >();
  2365. dataMap.Set(idx, val);
  2366. return val;
  2367. }
  2368. template <typename T>
  2369. static T GetStackTop(DataStack* dataStack)
  2370. {
  2371. auto dataPair = dataStack->back();
  2372. if (dataPair.first == RemoveTypePointer<T>::type::ClassType)
  2373. return (T)dataPair.second;
  2374. return NULL;
  2375. }
  2376. template <>
  2377. DbgBlock* GetStackTop<DbgBlock*>(DataStack* dataStack)
  2378. {
  2379. auto dataPair = dataStack->back();
  2380. if (dataPair.first == DbgBlock::ClassType)
  2381. return (DbgBlock*)dataPair.second;
  2382. if (dataPair.first == DbgSubprogram::ClassType)
  2383. return &((DbgSubprogram*)dataPair.second)->mBlock;
  2384. if (dataPair.first == DbgType::ClassType)
  2385. return ((DbgType*)dataPair.second)->mBlockParam;
  2386. return NULL;
  2387. }
  2388. template <typename T>
  2389. static bool StackHasType(DataStack* dataStack)
  2390. {
  2391. for (auto itr : *dataStack)
  2392. if (itr.first == RemoveTypePointer<T>::type::ClassType)
  2393. return true;
  2394. return false;
  2395. }
  2396. template <typename T>
  2397. static T GetStackLast(DataStack* dataStack)
  2398. {
  2399. for (int i = (int)dataStack->size() - 1; i >= 0; i--)
  2400. {
  2401. if ((*dataStack)[i].first == RemoveTypePointer<T>::type::ClassType)
  2402. return (T)(*dataStack)[i].second;
  2403. }
  2404. return NULL;
  2405. }
  2406. template <typename T>
  2407. static DataPair MakeDataPair(T* data)
  2408. {
  2409. return DataPair(T::ClassType, data);
  2410. }
  2411. void DbgModule::FixupInnerTypes(int startingTypeIdx)
  2412. {
  2413. BP_ZONE("DbgModule_FixupInnerTypes");
  2414. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2415. {
  2416. DbgType* dbgType = mTypes[typeIdx];
  2417. if ((dbgType->mPriority == DbgTypePriority_Primary_Implicit) && (dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) &&
  2418. (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2419. {
  2420. auto primaryParent = dbgType->mParent->GetPrimaryType();
  2421. dbgType->mParent->mSubTypeList.Clear();
  2422. dbgType->mParent = primaryParent;
  2423. primaryParent->mSubTypeList.PushBack(dbgType);
  2424. }
  2425. }
  2426. }
  2427. void DbgModule::MapTypes(int startingTypeIdx)
  2428. {
  2429. BP_ZONE("DbgModule_MapTypes");
  2430. bool needsInnerFixups = false;
  2431. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2432. {
  2433. DbgType* dbgType = mTypes[typeIdx];
  2434. BF_ASSERT(dbgType->mTypeCode != DbgType_Null);
  2435. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mPriority < DbgTypePriority_Primary_Implicit))
  2436. continue;
  2437. //TODO: Always valid?
  2438. if (dbgType->mIsDeclaration)
  2439. continue;
  2440. // We were avoiding adding '<' names before, but that made it impossible to look up auto-named primary types ,
  2441. // like in-place unions like '<unnamed-type-u>'
  2442. if ((dbgType->mTypeName == NULL) || (dbgType->mName == NULL) /*|| (dbgType->mTypeName[0] == '<')*/)
  2443. continue;
  2444. if (dbgType->mTypeCode > DbgType_DefinitionEnd)
  2445. {
  2446. // Only add "definition types"
  2447. continue;
  2448. }
  2449. if (dbgType->mTypeCode == DbgType_Namespace)
  2450. {
  2451. bool isQualifiedNamespace = false;
  2452. for (const char* cPtr = dbgType->mTypeName; *cPtr != '\0'; cPtr++)
  2453. if (*cPtr == '.')
  2454. isQualifiedNamespace = true;
  2455. if (isQualifiedNamespace)
  2456. continue; // Don't add fully qualified namespaces (they come from the 'using' implementation)*
  2457. }
  2458. if (dbgType->mHasStaticMembers)
  2459. {
  2460. for (auto member : dbgType->mMemberList)
  2461. if ((member->mIsStatic) && (member->mLocationData != NULL))
  2462. dbgType->mDefinedMembersSize++;
  2463. }
  2464. if ((dbgType->mTypeName != NULL) && (strcmp(dbgType->mTypeName, "@") == 0))
  2465. {
  2466. // Globals type.
  2467. continue;
  2468. }
  2469. auto prevTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  2470. // Only replace previous instance if its a declaration
  2471. if (prevTypeEntry != NULL)
  2472. {
  2473. auto prevType = prevTypeEntry->mValue;
  2474. if (dbgType->mCompileUnit->mDbgModule != prevType->mCompileUnit->mDbgModule)
  2475. {
  2476. // Don't replace original types with hot types -- those need to be inserted in the the hot alternates list
  2477. BF_ASSERT(dbgType->mCompileUnit->mDbgModule->IsObjectFile());
  2478. prevType->mHotNewType = dbgType;
  2479. continue;
  2480. }
  2481. // Never override explicit primaries
  2482. if (prevType->mPriority == DbgTypePriority_Primary_Explicit)
  2483. continue;
  2484. if (dbgType->mTypeCode == DbgType_TypeDef)
  2485. {
  2486. // Typedef can never override anything
  2487. continue;
  2488. }
  2489. if (prevType->mTypeCode == DbgType_TypeDef)
  2490. {
  2491. if (dbgType->mTypeCode != DbgType_TypeDef)
  2492. {
  2493. // Allow this to override
  2494. prevTypeEntry->mValue = dbgType;
  2495. }
  2496. continue;
  2497. }
  2498. // Don't replace a ptr to an BfObject with a BfObject
  2499. if ((prevType->mTypeCode == DbgType_Ptr) && (dbgType->mTypeCode == DbgType_Struct))
  2500. continue;
  2501. if ((prevType->mTypeCode == DbgType_Struct) && (dbgType->mTypeCode == DbgType_Ptr))
  2502. {
  2503. // Allow this to override
  2504. prevTypeEntry->mValue = dbgType;
  2505. continue;
  2506. }
  2507. if (prevType->mTypeCode == DbgType_Namespace)
  2508. {
  2509. if (dbgType->mTypeCode != DbgType_Namespace)
  2510. {
  2511. // Old type was namespace but new isn't? Replace old type.
  2512. while (!prevType->mSubTypeList.IsEmpty())
  2513. {
  2514. DbgType* subType = prevType->mSubTypeList.PopFront();
  2515. subType->mParent = dbgType;
  2516. dbgType->mSubTypeList.PushBack(subType);
  2517. }
  2518. prevType->mPriority = DbgTypePriority_Normal;
  2519. if (dbgType->mPriority < DbgTypePriority_Primary_Implicit)
  2520. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2521. prevTypeEntry->mValue = dbgType;
  2522. continue;
  2523. }
  2524. // We definitely didn't want to do this for MS. For DWARF?
  2525. //prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2526. continue;
  2527. }
  2528. else
  2529. {
  2530. // New type is namespace but old wasn't? Ignore new type.
  2531. if (dbgType->mTypeCode == DbgType_Namespace)
  2532. continue;
  2533. if (dbgType->mIsDeclaration)
  2534. continue;
  2535. if (!prevType->mIsDeclaration)
  2536. {
  2537. if ((prevType->mCompileUnit == NULL) || (dbgType->mLanguage < prevType->mLanguage))
  2538. {
  2539. // We always want 'Beef' types to supersede 'C' types, but don't override the built-in primitive types
  2540. continue;
  2541. }
  2542. if (prevType->mDefinedMembersSize > 0)
  2543. {
  2544. if (dbgType->mDefinedMembersSize > 0)
  2545. {
  2546. // We create an 'alternates' list for all types that define at least one static field
  2547. if (prevType->mHasStaticMembers)
  2548. prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2549. }
  2550. continue;
  2551. }
  2552. // if (prevType->mDefinedMembersSize > dbgType->mDefinedMembersSize)
  2553. // {
  2554. // continue;
  2555. // }
  2556. if (prevType->mMethodsWithParamsCount > dbgType->mMethodsWithParamsCount)
  2557. {
  2558. // This handles a special case where methods without line data like <Enum>.HasFlags doesn't show containing
  2559. // params in cases where it gets inlined
  2560. continue;
  2561. }
  2562. // Types with method lists are preferred
  2563. if ((!prevType->mMethodList.IsEmpty()) && (dbgType->mMethodList.IsEmpty()))
  2564. continue;
  2565. if ((prevType->mTypeCode == DbgType_Ptr) && (prevType->mTypeParam != NULL) && (!prevType->mTypeParam->mMethodList.IsEmpty()))
  2566. continue;
  2567. }
  2568. // Replace type
  2569. if (!prevType->mSubTypeList.IsEmpty())
  2570. needsInnerFixups = true;
  2571. prevType->mPriority = DbgTypePriority_Normal;
  2572. if (dbgType->mPriority == DbgTypePriority_Normal)
  2573. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2574. prevTypeEntry->mValue = dbgType;
  2575. continue;
  2576. }
  2577. }
  2578. if ((dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) && (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2579. needsInnerFixups = true;
  2580. if (dbgType->mPriority == DbgTypePriority_Normal)
  2581. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2582. mTypeMap.Insert(dbgType);
  2583. }
  2584. if (needsInnerFixups)
  2585. FixupInnerTypes(startingTypeIdx);
  2586. }
  2587. void DbgModule::CreateNamespaces()
  2588. {
  2589. BP_ZONE("DbgModule::CreateNamespaces");
  2590. int startLength = (int)mTypes.size();
  2591. for (int typeIdx = 0; typeIdx < startLength; typeIdx++)
  2592. {
  2593. DbgType* dbgType = mTypes[typeIdx];
  2594. if (dbgType->mName == NULL)
  2595. continue;
  2596. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mTagIdx != 0))
  2597. {
  2598. auto namespaceTypeEntry = FindType(dbgType->mName, dbgType->GetLanguage());
  2599. DbgType* namespaceType;
  2600. if (namespaceTypeEntry == NULL)
  2601. {
  2602. namespaceType = mAlloc.Alloc<DbgType>();
  2603. namespaceType->mTypeCode = DbgType_Namespace;
  2604. namespaceType->mLanguage = dbgType->mLanguage;
  2605. namespaceType->mCompileUnit = dbgType->mCompileUnit;
  2606. namespaceType->mTypeIdx = (int)mTypes.size();
  2607. namespaceType->mPriority = DbgTypePriority_Primary_Explicit;
  2608. namespaceType->mName = dbgType->mName;
  2609. namespaceType->mTypeName = dbgType->mTypeName;
  2610. if (dbgType->mParent != NULL)
  2611. {
  2612. namespaceType->mParent = dbgType->mParent->GetPrimaryType();
  2613. namespaceType->mParent->mSubTypeList.PushBack(namespaceType);
  2614. }
  2615. else
  2616. {
  2617. namespaceType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(namespaceType);
  2618. }
  2619. mTypes.push_back(namespaceType);
  2620. mTypeMap.Insert(namespaceType);
  2621. }
  2622. else
  2623. namespaceType = namespaceTypeEntry->mValue;
  2624. while (!dbgType->mMemberList.IsEmpty())
  2625. {
  2626. DbgVariable* curVar = dbgType->mMemberList.PopFront();
  2627. namespaceType->mMemberList.PushBack(curVar);
  2628. }
  2629. DbgType* prevType = NULL;
  2630. DbgType* curType = dbgType->mSubTypeList.mHead;
  2631. while (curType != NULL)
  2632. {
  2633. DbgType* nextType = curType->mNext;
  2634. if (curType->mPriority >= DbgTypePriority_Primary_Implicit)
  2635. {
  2636. dbgType->mSubTypeList.Remove(curType, prevType);
  2637. namespaceType->mSubTypeList.PushBack(curType);
  2638. }
  2639. prevType = curType;
  2640. curType = nextType;
  2641. }
  2642. continue;
  2643. }
  2644. }
  2645. // If we didn't have a parent type for a namespace (ie: if System.Collections wasn't linked to System) then we wait
  2646. // until the end and move those from the global list to the parent list
  2647. for (int typeIdx = startLength; typeIdx < (int)mTypes.size(); typeIdx++)
  2648. {
  2649. DbgType* dbgType = mTypes[typeIdx];
  2650. if (dbgType->mParent != NULL)
  2651. continue;
  2652. char* typeName = (char*)dbgType->mTypeName;
  2653. int lastDotIdx = -1;
  2654. for (int i = 0; true; i++)
  2655. {
  2656. char c = typeName[i];
  2657. if (c == 0)
  2658. break;
  2659. if (c == '.')
  2660. lastDotIdx = i;
  2661. }
  2662. if (lastDotIdx == -1)
  2663. continue;
  2664. typeName[lastDotIdx] = 0;
  2665. dbgType->mTypeName = typeName + lastDotIdx + 1;
  2666. auto parentEntry = FindType(typeName, dbgType->GetLanguage());
  2667. typeName[lastDotIdx] = '.';
  2668. if (parentEntry == NULL)
  2669. continue;
  2670. auto parentType = parentEntry->mValue;
  2671. dbgType->mCompileUnit->mGlobalType->mSubTypeList.Remove(dbgType);
  2672. dbgType->mParent = parentType;
  2673. parentType->mSubTypeList.PushBack(dbgType);
  2674. }
  2675. }
  2676. void DbgModule::FindTemplateStr(const char*& name, int& templateNameIdx)
  2677. {
  2678. if (templateNameIdx == 0)
  2679. {
  2680. for (int i = 0; name[i] != 0; i++)
  2681. {
  2682. if (name[i] == '<')
  2683. {
  2684. templateNameIdx = i;
  2685. return;
  2686. }
  2687. }
  2688. templateNameIdx = -1;
  2689. }
  2690. }
  2691. void DbgModule::TempRemoveTemplateStr(const char*& name, int& templateNameIdx)
  2692. {
  2693. if (templateNameIdx == 0)
  2694. FindTemplateStr(name, templateNameIdx);
  2695. if (templateNameIdx == -1)
  2696. return;
  2697. if (!DbgIsStrMutable(name))
  2698. name = DbgDupString(name);
  2699. ((char*)name)[templateNameIdx] = 0;
  2700. }
  2701. void DbgModule::ReplaceTemplateStr(const char*& name, int& templateNameIdx)
  2702. {
  2703. if (templateNameIdx > 0)
  2704. ((char*)name)[templateNameIdx] = '<';
  2705. }
  2706. void DbgModule::MapSubprogram(DbgSubprogram* dbgSubprogram)
  2707. {
  2708. if (dbgSubprogram->mBlock.IsEmpty())
  2709. return;
  2710. int progSize = (int)(dbgSubprogram->mBlock.mHighPC - dbgSubprogram->mBlock.mLowPC);
  2711. for (int progOffset = 0; true; progOffset += DBG_MAX_LOOKBACK)
  2712. {
  2713. int curSize = progSize - progOffset;
  2714. if (curSize <= 0)
  2715. break;
  2716. BP_ALLOC_T(DbgSubprogramMapEntry);
  2717. DbgSubprogramMapEntry* subprogramMapEntry = mAlloc.Alloc<DbgSubprogramMapEntry>();
  2718. subprogramMapEntry->mAddress = dbgSubprogram->mBlock.mLowPC + progOffset;
  2719. subprogramMapEntry->mEntry = dbgSubprogram;
  2720. mDebugTarget->mSubprogramMap.Insert(subprogramMapEntry);
  2721. }
  2722. }
  2723. bool DbgModule::ParseDWARF(const uint8*& dataPtr)
  2724. {
  2725. BP_ZONE("ParseDWARF");
  2726. const uint8* data = dataPtr;
  2727. const uint8* startData = mDebugInfoData;
  2728. int dataOfs = (int)(data - mDebugInfoData);
  2729. intptr_target length = GET(int);
  2730. DbgModule* linkedModule = GetLinkedModule();
  2731. if (length == -1)
  2732. {
  2733. mIsDwarf64 = true;
  2734. length = (intptr_target)GET(int64);
  2735. }
  2736. else
  2737. mIsDwarf64 = false;
  2738. if (length == 0)
  2739. return false;
  2740. const uint8* dataEnd = data + length;
  2741. int version = GET(short);
  2742. int abbrevOffset = GET(int);
  2743. char pointerSize = GET(char);
  2744. ParseAbbrevData(mDebugAbbrevData + abbrevOffset);
  2745. DbgCompileUnit* compileUnit = new DbgCompileUnit(this);
  2746. mDbgFlavor = DbgFlavor_GNU;
  2747. compileUnit->mDbgModule = this;
  2748. mCompileUnits.push_back(compileUnit);
  2749. DbgSubprogram* subProgram = NULL;
  2750. //std::map<int, DbgType*> typeMap;
  2751. //std::map<int, DbgSubprogram*> subprogramMap;
  2752. int tagStart = (int)(data - startData);
  2753. int tagEnd = (int)(dataEnd - startData);
  2754. DbgDataMap dataMap(tagStart, tagEnd);
  2755. DataStack dataStack;
  2756. Array<AbstractOriginEntry> abstractOriginReplaceList;
  2757. Array<int> deferredArrayDims;
  2758. int startingTypeIdx = (int)linkedModule->mTypes.size();
  2759. while (data < dataEnd)
  2760. {
  2761. gAbbrevNum++;
  2762. const uint8* tagDataStart = data;
  2763. int tagIdx = (int)(tagDataStart - startData);
  2764. int abbrevIdx = (int)DecodeULEB128(data);
  2765. const uint8* abbrevData = mDebugAbbrevPtrData[abbrevIdx];
  2766. if (abbrevIdx == 0)
  2767. {
  2768. if (deferredArrayDims.size() > 0)
  2769. {
  2770. DbgType* arrType = GetStackTop<DbgType*>(&dataStack);
  2771. BF_ASSERT(arrType->mTypeCode == DbgType_SizedArray);
  2772. arrType->mSize = deferredArrayDims[0]; // Byte count still needs to be multiplied by the underlying type size
  2773. DbgType* rootArrType = arrType;
  2774. for (int dimIdx = 0; dimIdx < (int)deferredArrayDims.size() - 1; dimIdx++)
  2775. {
  2776. int dimSize = deferredArrayDims[dimIdx];
  2777. DbgType* subArrType = mAlloc.Alloc<DbgType>();
  2778. subArrType->mCompileUnit = compileUnit;
  2779. subArrType->mLanguage = compileUnit->mLanguage;
  2780. subArrType->mTypeIdx = (int)linkedModule->mTypes.size();
  2781. linkedModule->mTypes.push_back(subArrType);
  2782. subArrType->mTypeCode = DbgType_SizedArray;
  2783. subArrType->mTypeParam = arrType->mTypeParam;
  2784. subArrType->mSize = deferredArrayDims[dimIdx + 1];
  2785. arrType->mTypeParam = subArrType;
  2786. arrType = subArrType;
  2787. }
  2788. deferredArrayDims.Clear();
  2789. }
  2790. dataStack.pop_back();
  2791. continue;
  2792. }
  2793. int entryTag = (int) DecodeULEB128(abbrevData);
  2794. bool hasChildren = GET_FROM(abbrevData, char) == DW_CHILDREN_yes;
  2795. int64 atLowPC = 0;
  2796. int64 atHighPC = 0;
  2797. int64 atRanges = 0;
  2798. bool hasRanges = false;
  2799. const uint8* atFrameBase = NULL;
  2800. int64_t atFrameBaseLength = 0;
  2801. int64 atLocationLen = 0;
  2802. const uint8* atLocationData = 0;
  2803. const char* atProducer = NULL;
  2804. const char* atName = NULL;
  2805. const char* atCompDir = NULL;
  2806. const char* atLinkageName = NULL;
  2807. int64 atConstValue = 0;
  2808. int atDataMemberLocation = 0;
  2809. const uint8* atDataMemberData = NULL;
  2810. int atDeclFile = 0;
  2811. int atDeclLine = 0;
  2812. int atCallFile = 0;
  2813. int atCallLine = 0;
  2814. int atCount = 0;
  2815. int atType = 0;
  2816. int atImport = 0;
  2817. int atInline = 0;
  2818. int atArtificial = 0;
  2819. int atExternal = 0;
  2820. int atByteSize = -1;
  2821. int atEncoding = 0;
  2822. int atSpecification = 0;
  2823. int atObjectPointer = 0;
  2824. int atBitOffset = 0;
  2825. int atBitSize = 0;
  2826. int atAbstractOrigin = 0;
  2827. const uint8* atVirtualLocData = NULL;
  2828. bool atDeclaration = false;
  2829. bool atVirtual = false;
  2830. bool hadConstValue = false;
  2831. bool hadMemberLocation = false;
  2832. bool isOptimized = false;
  2833. DataPair newDataPair;
  2834. while (true)
  2835. {
  2836. int attrName = (int)DecodeULEB128(abbrevData);
  2837. int form = (int)DecodeULEB128(abbrevData);
  2838. if ((attrName == 0) && (form == 0))
  2839. break;
  2840. switch (attrName)
  2841. {
  2842. case DW_AT_sibling:
  2843. ReadValue<char>(data, form);
  2844. break;
  2845. case DW_AT_location:
  2846. atLocationLen = (int)ReadValue<uint>(data, form, dataOfs, &atLocationData, startData);
  2847. break;
  2848. case DW_AT_name:
  2849. atName = ReadValue<const char*>(data, form);
  2850. break;
  2851. case DW_AT_ordering:
  2852. /*TODO:*/ ReadValue<int>(data, form);
  2853. break;
  2854. case DW_AT_byte_size:
  2855. atByteSize = ReadValue<int>(data, form);
  2856. break;
  2857. case DW_AT_bit_offset:
  2858. atBitOffset = ReadValue<int>(data, form);
  2859. break;
  2860. case DW_AT_bit_size:
  2861. atBitSize = ReadValue<int>(data, form);
  2862. break;
  2863. case DW_AT_stmt_list:
  2864. ReadValue<int64_t>(data, form);
  2865. break;
  2866. case DW_AT_low_pc:
  2867. atLowPC = RemapAddr((addr_target)ReadValue<int64_t>(data, form));
  2868. break;
  2869. case DW_AT_high_pc:
  2870. atHighPC = ReadValue<int64_t>(data, form);
  2871. break;
  2872. case DW_AT_language:
  2873. /*TODO:*/ ReadValue<int>(data, form);
  2874. break;
  2875. case DW_AT_discr:
  2876. /*TODO:*/ ReadValue<int>(data, form);
  2877. break;
  2878. case DW_AT_discr_value:
  2879. /*TODO:*/ ReadValue<int>(data, form);
  2880. break;
  2881. case DW_AT_visibility:
  2882. /*TODO:*/ ReadValue<int>(data, form);
  2883. break;
  2884. case DW_AT_import:
  2885. atImport = ReadValue<int>(data, form) + dataOfs;
  2886. break;
  2887. case DW_AT_string_length:
  2888. /*TODO:*/ ReadValue<int>(data, form);
  2889. break;
  2890. case DW_AT_common_reference:
  2891. /*TODO:*/ ReadValue<int>(data, form);
  2892. break;
  2893. case DW_AT_comp_dir:
  2894. atCompDir = ReadValue<const char*>(data, form);
  2895. break;
  2896. case DW_AT_const_value:
  2897. atConstValue = ReadValue<int64>(data, form);
  2898. hadConstValue = true;
  2899. break;
  2900. case DW_AT_containing_type:
  2901. /*TODO:*/ ReadValue<int>(data, form);
  2902. break;
  2903. case DW_AT_default_value:
  2904. /*TODO:*/ ReadValue<int>(data, form);
  2905. break;
  2906. case DW_AT_inline:
  2907. atInline = ReadValue<int>(data, form);
  2908. break;
  2909. case DW_AT_is_optional:
  2910. /*TODO:*/ ReadValue<int>(data, form);
  2911. break;
  2912. case DW_AT_lower_bound:
  2913. /*TODO:*/ ReadValue<int>(data, form);
  2914. break;
  2915. case DW_AT_producer:
  2916. atProducer = ReadValue<const char*>(data, form);
  2917. break;
  2918. case DW_AT_prototyped:
  2919. /*TODO:*/ ReadValue<int>(data, form);
  2920. break;
  2921. case DW_AT_return_addr:
  2922. /*TODO:*/ ReadValue<int>(data, form);
  2923. break;
  2924. case DW_AT_start_scope:
  2925. /*TODO:*/ ReadValue<int>(data, form);
  2926. break;
  2927. case DW_AT_bit_stride:
  2928. /*TODO:*/ ReadValue<int>(data, form);
  2929. break;
  2930. case DW_AT_upper_bound:
  2931. // Lower bound not supported
  2932. atCount = ReadValue<int>(data, form);
  2933. break;
  2934. case DW_AT_abstract_origin:
  2935. atAbstractOrigin = ReadValue<int>(data, form, dataOfs);
  2936. break;
  2937. case DW_AT_accessibility:
  2938. /*TODO:*/ ReadValue<int>(data, form);
  2939. break;
  2940. case DW_AT_address_class:
  2941. /*TODO:*/ ReadValue<int>(data, form);
  2942. break;
  2943. case DW_AT_artificial:
  2944. atArtificial = ReadValue<int>(data, form);
  2945. break;
  2946. case DW_AT_base_types:
  2947. /*TODO:*/ ReadValue<int>(data, form);
  2948. break;
  2949. case DW_AT_calling_convention:
  2950. /*TODO:*/ ReadValue<int>(data, form);
  2951. break;
  2952. case DW_AT_count:
  2953. atCount = ReadValue<uint>(data, form);
  2954. break;
  2955. case DW_AT_data_member_location:
  2956. if (form == DW_FORM_exprloc)
  2957. {
  2958. atDataMemberLocation = (int)ReadValue<uint>(data, form, dataOfs, &atDataMemberData);
  2959. hadMemberLocation = true;
  2960. }
  2961. else
  2962. {
  2963. atDataMemberLocation = (int)ReadValue<uint>(data, form);
  2964. hadMemberLocation = true;
  2965. }
  2966. break;
  2967. case DW_AT_decl_column:
  2968. /*TODO:*/ ReadValue<uint32>(data, form);
  2969. break;
  2970. case DW_AT_decl_file:
  2971. atDeclFile = ReadValue<uint32>(data, form);
  2972. break;
  2973. case DW_AT_decl_line:
  2974. atDeclLine = ReadValue<uint32>(data, form);
  2975. break;
  2976. case DW_AT_declaration:
  2977. atDeclaration = ReadValue<bool>(data, form);
  2978. break;
  2979. case DW_AT_discr_list:
  2980. /*TODO:*/ ReadValue<int>(data, form);
  2981. break;
  2982. case DW_AT_encoding:
  2983. atEncoding = ReadValue<int>(data, form);
  2984. break;
  2985. case DW_AT_external:
  2986. atExternal = ReadValue<int>(data, form);
  2987. break;
  2988. case DW_AT_frame_base:
  2989. atFrameBaseLength = (int64_t)ReadValue<uint64_t>(data, form, dataOfs, &atFrameBase);
  2990. break;
  2991. case DW_AT_friend:
  2992. /*TODO:*/ ReadValue<int>(data, form);
  2993. break;
  2994. case DW_AT_identifier_case:
  2995. /*TODO:*/ ReadValue<int>(data, form);
  2996. break;
  2997. case DW_AT_macro_info:
  2998. /*TODO:*/ ReadValue<int>(data, form);
  2999. break;
  3000. case DW_AT_namelist_item:
  3001. /*TODO:*/ ReadValue<int>(data, form);
  3002. break;
  3003. case DW_AT_priority:
  3004. /*TODO:*/ ReadValue<int>(data, form);
  3005. break;
  3006. case DW_AT_segment:
  3007. /*TODO:*/ ReadValue<int>(data, form);
  3008. break;
  3009. case DW_AT_specification:
  3010. atSpecification = ReadValue<int>(data, form, dataOfs);
  3011. break;
  3012. case DW_AT_static_link:
  3013. /*TODO:*/ ReadValue<int>(data, form);
  3014. break;
  3015. case DW_AT_type:
  3016. atType = ReadValue<int>(data, form, dataOfs);
  3017. break;
  3018. case DW_AT_use_location:
  3019. /*TODO:*/ ReadValue<int>(data, form);
  3020. break;
  3021. case DW_AT_variable_parameter:
  3022. /*TODO:*/ ReadValue<int>(data, form);
  3023. break;
  3024. case DW_AT_virtuality:
  3025. atVirtual = ReadValue<int>(data, form) != 0;
  3026. break;
  3027. case DW_AT_vtable_elem_location:
  3028. ReadValue<uint64_t>(data, form, dataOfs, &atVirtualLocData);
  3029. break;
  3030. case DW_AT_allocated:
  3031. /*TODO:*/ ReadValue<int>(data, form);
  3032. break;
  3033. case DW_AT_associated:
  3034. /*TODO:*/ ReadValue<int>(data, form);
  3035. break;
  3036. case DW_AT_data_location:
  3037. /*TODO:*/ ReadValue<int>(data, form);
  3038. break;
  3039. case DW_AT_byte_stride:
  3040. /*TODO:*/ ReadValue<int>(data, form);
  3041. break;
  3042. case DW_AT_entry_pc:
  3043. /*TODO:*/ ReadValue<int>(data, form);
  3044. break;
  3045. case DW_AT_use_UTF8:
  3046. /*TODO:*/ ReadValue<int>(data, form);
  3047. break;
  3048. case DW_AT_extension:
  3049. /*TODO:*/ ReadValue<int>(data, form);
  3050. break;
  3051. case DW_AT_ranges:
  3052. atRanges = (int)ReadValue<uint>(data, form);
  3053. hasRanges = true;
  3054. break;
  3055. case DW_AT_trampoline:
  3056. /*TODO:*/ ReadValue<int>(data, form);
  3057. break;
  3058. case DW_AT_call_column:
  3059. /*TODO:*/ ReadValue<int>(data, form);
  3060. break;
  3061. case DW_AT_call_file:
  3062. atCallFile = ReadValue<uint32>(data, form);
  3063. break;
  3064. case DW_AT_call_line:
  3065. atCallLine = ReadValue<uint32>(data, form);
  3066. break;
  3067. case DW_AT_description:
  3068. /*TODO:*/ ReadValue<int>(data, form);
  3069. break;
  3070. case DW_AT_binary_scale:
  3071. /*TODO:*/ ReadValue<int>(data, form);
  3072. break;
  3073. case DW_AT_decimal_scale:
  3074. /*TODO:*/ ReadValue<int>(data, form);
  3075. break;
  3076. case DW_AT_small:
  3077. /*TODO:*/ ReadValue<int>(data, form);
  3078. break;
  3079. case DW_AT_decimal_sign:
  3080. /*TODO:*/ ReadValue<int>(data, form);
  3081. break;
  3082. case DW_AT_digit_count:
  3083. /*TODO:*/ ReadValue<int>(data, form);
  3084. break;
  3085. case DW_AT_picture_string:
  3086. /*TODO:*/ ReadValue<int>(data, form);
  3087. break;
  3088. case DW_AT_mutable:
  3089. /*TODO:*/ ReadValue<int>(data, form);
  3090. break;
  3091. case DW_AT_threads_scaled:
  3092. /*TODO:*/ ReadValue<int>(data, form);
  3093. break;
  3094. case DW_AT_explicit:
  3095. /*TODO:*/ ReadValue<int>(data, form);
  3096. break;
  3097. case DW_AT_object_pointer:
  3098. atObjectPointer = ReadValue<int>(data, form);
  3099. break;
  3100. case DW_AT_endianity:
  3101. /*TODO:*/ ReadValue<int>(data, form);
  3102. break;
  3103. case DW_AT_elemental:
  3104. /*TODO:*/ ReadValue<int>(data, form);
  3105. break;
  3106. case DW_AT_pure:
  3107. /*TODO:*/ ReadValue<int>(data, form);
  3108. break;
  3109. case DW_AT_recursive:
  3110. /*TODO:*/ ReadValue<int>(data, form);
  3111. break;
  3112. case DW_AT_signature:
  3113. /*TODO:*/ ReadValue<int>(data, form);
  3114. break;
  3115. case DW_AT_main_subprogram:
  3116. /*TODO:*/ ReadValue<int>(data, form);
  3117. break;
  3118. case DW_AT_data_bit_offset:
  3119. /*TODO:*/ ReadValue<int>(data, form);
  3120. break;
  3121. case DW_AT_const_expr:
  3122. /*TODO:*/ ReadValue<int>(data, form);
  3123. break;
  3124. case DW_AT_enum_class:
  3125. /*TODO:*/ ReadValue<int>(data, form);
  3126. break;
  3127. case DW_AT_linkage_name:
  3128. atLinkageName = ReadValue<const char*>(data, form);
  3129. break;
  3130. //
  3131. case DW_AT_MIPS_linkage_name:
  3132. atLinkageName = ReadValue<const char*>(data, form);
  3133. break;
  3134. case DW_AT_APPLE_optimized:
  3135. isOptimized = ReadValue<bool>(data, form);
  3136. break;
  3137. default:
  3138. ReadValue<int>(data, form);
  3139. break;
  3140. }
  3141. }
  3142. if ((hasRanges) && (atLowPC == 0))
  3143. {
  3144. addr_target* rangeData = (addr_target*)(mDebugRangesData + atRanges);
  3145. while (true)
  3146. {
  3147. addr_target lowPC = *(rangeData++);
  3148. if (lowPC == 0)
  3149. break;
  3150. addr_target highPC = *(rangeData++);
  3151. if (compileUnit->mLowPC != (addr_target)-1)
  3152. {
  3153. // These are sometimes relative to the compile unit and sometimes absolute
  3154. if (highPC + compileUnit->mLowPC <= compileUnit->mHighPC)
  3155. {
  3156. lowPC += compileUnit->mLowPC;
  3157. highPC += compileUnit->mLowPC;
  3158. }
  3159. }
  3160. highPC -= lowPC;
  3161. // Select the largest range. We have some cases where some hoisting and such will
  3162. // give us a small inlining aberration much earlier than expected so this ignores that
  3163. if ((int64)highPC > atHighPC)
  3164. {
  3165. atLowPC = lowPC;
  3166. atHighPC = highPC;
  3167. }
  3168. /*if ((atLowPC == 0) || (lowPC < (addr_target)atLowPC))
  3169. atLowPC = lowPC;
  3170. if (highPC > (addr_target)atHighPC)
  3171. atHighPC = highPC;*/
  3172. }
  3173. }
  3174. switch (entryTag)
  3175. {
  3176. case DW_TAG_compile_unit:
  3177. {
  3178. newDataPair = MakeDataPair(compileUnit);
  3179. compileUnit->mName = atName;
  3180. compileUnit->mProducer = atProducer;
  3181. if (atCompDir != NULL)
  3182. compileUnit->mCompileDir = atCompDir;
  3183. if (atLowPC != 0)
  3184. {
  3185. compileUnit->mLowPC = (addr_target)atLowPC;
  3186. compileUnit->mHighPC = (addr_target)(atLowPC + atHighPC);
  3187. }
  3188. if (compileUnit->mProducer.IndexOf("Beef") != -1)
  3189. {
  3190. compileUnit->mLanguage = DbgLanguage_Beef;
  3191. }
  3192. else
  3193. {
  3194. compileUnit->mLanguage = DbgLanguage_C;
  3195. }
  3196. compileUnit->mGlobalType->mLanguage = compileUnit->mLanguage;
  3197. }
  3198. break;
  3199. case DW_TAG_imported_module:
  3200. {
  3201. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3202. DbgType* importType = GetOrCreateType(atImport, dataMap);
  3203. if (parentType != NULL) // Parent type is NULL for Clang DbgModule info
  3204. parentType->mUsingNamespaces.PushFront(importType, &mAlloc);
  3205. }
  3206. break;
  3207. case DW_TAG_inlined_subroutine:
  3208. case DW_TAG_subprogram:
  3209. {
  3210. /*//TODO: This is a test. See if it breaks anything.
  3211. if ((atExternal != 0) && (atLowPC == 0))
  3212. break;*/
  3213. if (atSpecification == 0)
  3214. {
  3215. subProgram = GetOrCreate<DbgSubprogram*>(tagIdx, dataMap);
  3216. subProgram->mCompileUnit = compileUnit;
  3217. subProgram->mVirtual = atVirtual;
  3218. subProgram->mIsOptimized = isOptimized;
  3219. if (atVirtualLocData != NULL)
  3220. {
  3221. const uint8* opPtr = atVirtualLocData;
  3222. if (*(opPtr++) == DW_OP_constu)
  3223. {
  3224. subProgram->mVTableLoc = (int)DecodeSLEB128(opPtr) * sizeof(addr_target);
  3225. }
  3226. }
  3227. //subProgram->mVTableLoc = atVirtualLoc * sizeof(addr_target);
  3228. //SplitName(atName, subProgram->mName, subProgram->mTemplateName);
  3229. subProgram->mName = atName;
  3230. subProgram->mLinkName = atLinkageName;
  3231. if (atAbstractOrigin != NULL)
  3232. {
  3233. DbgSubprogram* originSubProgram = GetOrCreate<DbgSubprogram*>(atAbstractOrigin, dataMap);
  3234. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgSubprogram::ClassType, subProgram, originSubProgram);
  3235. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3236. }
  3237. subProgram->mParentType = GetStackTop<DbgType*>(&dataStack);
  3238. newDataPair = MakeDataPair(subProgram);
  3239. //if ((atLinkageName != NULL) && (subProgram->mParentType != NULL))
  3240. //subProgram->mParentType->mDefinedMembersCount++;
  3241. mSubprograms.push_back(subProgram);
  3242. if (subProgram->mParentType != NULL)
  3243. {
  3244. subProgram->mParentType->mMethodList.PushBack(subProgram);
  3245. }
  3246. else
  3247. {
  3248. compileUnit->mGlobalType->mMethodList.PushBack(subProgram);
  3249. }
  3250. }
  3251. else
  3252. {
  3253. subProgram = dataMap.Get<DbgSubprogram*>(atSpecification);
  3254. BF_ASSERT(subProgram != NULL);
  3255. // We remove params form the declaration and re-add the real ones here
  3256. subProgram->mParams.Clear();
  3257. }
  3258. newDataPair = MakeDataPair(subProgram);
  3259. DbgBlock* dwBlock = &subProgram->mBlock;
  3260. if (atType != 0)
  3261. subProgram->mReturnType = GetOrCreateType(atType, dataMap);
  3262. if (!atDeclaration)
  3263. {
  3264. dwBlock->mLowPC = (addr_target)atLowPC;
  3265. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3266. if (dwBlock->mLowPC != 0)
  3267. {
  3268. compileUnit->mLowPC = std::min(compileUnit->mLowPC, dwBlock->mLowPC);
  3269. compileUnit->mHighPC = std::max(compileUnit->mHighPC, dwBlock->mHighPC);
  3270. }
  3271. if (atObjectPointer != 0)
  3272. subProgram->mHasThis = true;
  3273. subProgram->mFrameBaseLen = (int)atFrameBaseLength;
  3274. subProgram->mFrameBaseData = atFrameBase;
  3275. if (atHighPC > 0)
  3276. {
  3277. MapSubprogram(subProgram);
  3278. }
  3279. }
  3280. if (entryTag == DW_TAG_inlined_subroutine)
  3281. {
  3282. DbgSubprogram* parentSubProgram = GetStackLast<DbgSubprogram*>(&dataStack);
  3283. subProgram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
  3284. subProgram->mInlineeInfo->mInlineParent = parentSubProgram;
  3285. subProgram->mInlineeInfo->mRootInliner = parentSubProgram->GetRootInlineParent();
  3286. subProgram->mFrameBaseData = parentSubProgram->mFrameBaseData;
  3287. subProgram->mFrameBaseLen = parentSubProgram->mFrameBaseLen;
  3288. }
  3289. //if (subProgram->mParentType != NULL)
  3290. //subProgram->mParentType->mDefinedMembersCount++;
  3291. }
  3292. break;
  3293. case DW_TAG_lexical_block:
  3294. {
  3295. DbgBlock* prevBlock = GetStackTop<DbgBlock*>(&dataStack);
  3296. DbgBlock* dwBlock = mAlloc.Alloc<DbgBlock>();
  3297. if (hasRanges)
  3298. {
  3299. dwBlock->mLowPC = -1;
  3300. dwBlock->mHighPC = (addr_target)atRanges;
  3301. }
  3302. else
  3303. {
  3304. dwBlock->mLowPC = (addr_target)atLowPC;
  3305. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3306. }
  3307. newDataPair = MakeDataPair(dwBlock);
  3308. prevBlock->mSubBlocks.PushBack(dwBlock);
  3309. }
  3310. break;
  3311. case DW_TAG_variable:
  3312. {
  3313. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3314. if (atName && !strncmp(atName, "__asmLines", 10))
  3315. {
  3316. const char* ptr = strchr(atName, '.');
  3317. if (!ptr)
  3318. break;
  3319. int declLine = atDeclLine;
  3320. Array<int> asmLines;
  3321. int curAsmLine = 0;
  3322. int curRunCount = 1; // initial value is starting line, with an assumed run count of 1
  3323. bool parity = true; // starting line is standalone; everything afterwards is in pairs
  3324. while (true)
  3325. {
  3326. ++ptr;
  3327. if (!*ptr)
  3328. break;
  3329. String s;
  3330. if (*ptr == '$')
  3331. {
  3332. ++ptr;
  3333. const char* dollarPtr = strchr(ptr, '$');
  3334. if (!dollarPtr)
  3335. break;
  3336. s = String(ptr, (int)(dollarPtr - ptr));
  3337. ptr = dollarPtr;
  3338. }
  3339. else
  3340. {
  3341. s += *ptr;
  3342. }
  3343. //int asmLine = atoi(s.c_str());
  3344. //asmLines.push_back(asmLine);
  3345. const char* sPtr = s.c_str();
  3346. int decodedValue = (int)DecodeULEB32(sPtr);
  3347. if (!parity)
  3348. {
  3349. curRunCount = decodedValue;
  3350. }
  3351. else
  3352. {
  3353. for (int iLine=0; iLine<curRunCount; ++iLine)
  3354. {
  3355. curAsmLine += decodedValue;
  3356. asmLines.push_back(curAsmLine);
  3357. }
  3358. }
  3359. parity = !parity;
  3360. }
  3361. BF_ASSERT(!parity);
  3362. if (dwBlock->mAsmDebugLineMap == NULL)
  3363. {
  3364. mAsmDebugLineMaps.resize(mAsmDebugLineMaps.size() + 1);
  3365. dwBlock->mAsmDebugLineMap = &mAsmDebugLineMaps.back();
  3366. }
  3367. auto mapIter = dwBlock->mAsmDebugLineMap->find(declLine);
  3368. if (mapIter != dwBlock->mAsmDebugLineMap->end())
  3369. {
  3370. auto& dstVec = mapIter->second;
  3371. dstVec.Reserve(dstVec.size() + asmLines.size());
  3372. //dstVec.insert(dstVec.end(), asmLines.begin(), asmLines.end());
  3373. if (!asmLines.IsEmpty())
  3374. dstVec.Insert(dstVec.size(), &asmLines[0], asmLines.size());
  3375. }
  3376. else
  3377. {
  3378. (*dwBlock->mAsmDebugLineMap)[declLine] = std::move(asmLines);
  3379. }
  3380. break;
  3381. }
  3382. bool addToGlobalVarMap = false;
  3383. bool isNewVariable = true;
  3384. DbgVariable* dbgVariable = NULL;
  3385. if (atSpecification != 0)
  3386. {
  3387. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3388. //BF_ASSERT(dbgVariable != NULL);
  3389. dbgVariable = GetOrCreate<DbgVariable*>(atSpecification, dataMap);
  3390. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3391. //BF_ASSERT(dbgVariable != NULL);
  3392. }
  3393. else if (dwBlock != NULL)
  3394. {
  3395. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3396. dwBlock->mVariables.PushBack(dbgVariable);
  3397. }
  3398. else
  3399. {
  3400. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3401. bool wantGlobal = true;
  3402. if (compileUnit->mLanguage == DbgLanguage_Beef)
  3403. {
  3404. // Don't show certain global variables in Beef -- that includes things like VTable data
  3405. if (atName[0] == '_')
  3406. wantGlobal = false;
  3407. }
  3408. if ((dbgType == NULL) && (wantGlobal))
  3409. {
  3410. /*DbgCompileUnit* topCompileUnit = GetStackTop<DbgCompileUnit*>(&dataStack);
  3411. if (topCompileUnit != NULL)
  3412. dbgType = &topCompileUnit->mGlobalType;*/
  3413. dbgType = linkedModule->mMasterCompileUnit->mGlobalType;
  3414. auto foundEntry = mGlobalVarMap.Find(atName);
  3415. if (foundEntry != NULL)
  3416. {
  3417. isNewVariable = false;
  3418. dbgVariable = foundEntry->mValue;
  3419. }
  3420. else
  3421. {
  3422. addToGlobalVarMap = true;
  3423. }
  3424. }
  3425. if (dbgVariable == NULL)
  3426. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3427. dbgVariable->mIsStatic = true;
  3428. //TODO: dbgType can be NULL. This only (apparently?) happens for DW_TAG_inlined_subroutine, which we don't handle right now...
  3429. if (dbgType != NULL)
  3430. {
  3431. BF_ASSERT(dbgType->IsNamespace() || (dbgType->mTypeCode == DbgType_Root));
  3432. if (isNewVariable)
  3433. dbgType->mMemberList.PushBack(dbgVariable);
  3434. }
  3435. }
  3436. if (dbgVariable != NULL)
  3437. {
  3438. if (atSpecification == 0)
  3439. {
  3440. dbgVariable->mIsParam = false;
  3441. dbgVariable->mName = atName;
  3442. dbgVariable->mConstValue = atConstValue;
  3443. dbgVariable->mType = GetOrCreateType(atType, dataMap);
  3444. dbgVariable->mIsConst = hadConstValue;
  3445. dbgVariable->mIsStatic = !hadMemberLocation;
  3446. dbgVariable->mIsExtern = atExternal != 0;
  3447. }
  3448. if (atLinkageName != NULL)
  3449. dbgVariable->mLinkName = atLinkageName;
  3450. dbgVariable->mLocationLen = (int8)atLocationLen;
  3451. dbgVariable->mLocationData = atLocationData;
  3452. dbgVariable->mCompileUnit = compileUnit;
  3453. /*if (dbgVariable->mIsStatic && !dbgVariable->mIsConst && (dbgVariable->mLocationLen > 0) && (dbgVariable->mIsExtern))
  3454. {
  3455. DbgAddrType addrType = DbgAddrType_Value;
  3456. //
  3457. addr_target valAddr = mDebugTarget->EvaluateLocation(dbgVariable->mCompileUnit->mDbgModule, NULL, dbgVariable->mLocationData, dbgVariable->mLocationLen, NULL, &addrType);
  3458. if ((addrType == DbgAddrType_Target) && (valAddr != 0))
  3459. {
  3460. dbgVariable->mStaticCachedAddr = valAddr;
  3461. if (dbgVariable->mLinkName != NULL)
  3462. mStaticVariables.push_back(dbgVariable);
  3463. }
  3464. else
  3465. dbgVariable->mIsStatic = false;
  3466. }*/
  3467. // We had to remove the above for hot loading, calculate the mStaticCachedAddr later. Just put into mStaticVariables for now
  3468. mStaticVariables.push_back(dbgVariable);
  3469. if (atAbstractOrigin != NULL)
  3470. {
  3471. DbgVariable* originVariable = GetOrCreate<DbgVariable*>(atAbstractOrigin, dataMap);
  3472. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgVariable::ClassType, dbgVariable, originVariable);
  3473. if (atAbstractOrigin < tagIdx)
  3474. abstractOriginEntry.Replace();
  3475. else
  3476. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3477. }
  3478. else if (dbgVariable->mName == NULL)
  3479. dbgVariable->mName = "_unnamed";
  3480. if (addToGlobalVarMap)
  3481. mGlobalVarMap.Insert(dbgVariable);
  3482. newDataPair = MakeDataPair(dbgVariable);
  3483. }
  3484. }
  3485. break;
  3486. case DW_TAG_formal_parameter:
  3487. {
  3488. DbgSubprogram* dwSubprogram = GetStackTop<DbgSubprogram*>(&dataStack);
  3489. if (dwSubprogram == NULL)
  3490. {
  3491. if ((atName == NULL) && (atAbstractOrigin == 0))
  3492. {
  3493. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3494. if ((dbgType == NULL) || (dbgType->mTypeCode != DbgType_Subroutine))
  3495. break;
  3496. //TODO: Add params to subroutine type
  3497. break;
  3498. }
  3499. break;
  3500. }
  3501. if ((dwSubprogram->mParams.IsEmpty()) && (dwSubprogram->mParentType != 0))
  3502. dwSubprogram->mParentType->mMethodsWithParamsCount++;
  3503. //DbgVariable* dbgVariable = mAlloc.Alloc<DbgVariable>();
  3504. DbgVariable* dwVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3505. dwSubprogram->mParams.PushBack(dwVariable);
  3506. if (atArtificial != 0)
  3507. {
  3508. dwSubprogram->mHasThis = true;
  3509. if (atName == NULL)
  3510. atName = "this";
  3511. }
  3512. dwVariable->mCompileUnit = compileUnit;
  3513. dwVariable->mIsParam = true;
  3514. dwVariable->mName = atName;
  3515. dwVariable->mLocationLen = (int)atLocationLen;
  3516. dwVariable->mLocationData = atLocationData;
  3517. dwVariable->mType = GetOrCreateType(atType, dataMap);
  3518. if (atAbstractOrigin != 0)
  3519. {
  3520. }
  3521. }
  3522. break;
  3523. case DW_TAG_enumerator:
  3524. {
  3525. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3526. member->mCompileUnit = compileUnit;
  3527. member->mConstValue = atConstValue;
  3528. member->mName = atName;
  3529. member->mIsStatic = true;
  3530. member->mIsConst = true;
  3531. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3532. parentType->mMemberList.PushBack(member);
  3533. member->mMemberOffset = atDataMemberLocation;
  3534. //member->mType = parentType->mTypeParam;
  3535. member->mType = parentType;
  3536. // Insert into parent's namespace
  3537. auto prevTop = dataStack.back();
  3538. dataStack.pop_back();
  3539. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3540. dataStack.push_back(prevTop);
  3541. if (dwBlock != NULL)
  3542. {
  3543. DbgVariable* dwVariable = mAlloc.Alloc<DbgVariable>();
  3544. dwBlock->mVariables.PushBack(dwVariable);
  3545. if (atSpecification == 0)
  3546. {
  3547. dwVariable->mIsParam = false;
  3548. dwVariable->mName = atName;
  3549. dwVariable->mConstValue = atConstValue;
  3550. dwVariable->mType = parentType->mTypeParam;
  3551. dwVariable->mIsConst = hadConstValue;
  3552. dwVariable->mIsStatic = !hadMemberLocation;
  3553. }
  3554. dwVariable->mLocationLen = (int)atLocationLen;
  3555. dwVariable->mLocationData = atLocationData;
  3556. dwVariable->mCompileUnit = compileUnit;
  3557. BF_ASSERT(dwVariable->mName != 0);
  3558. newDataPair = MakeDataPair(dwVariable);
  3559. }
  3560. }
  3561. break;
  3562. /*case DW_TAG_subrange_type:
  3563. {
  3564. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3565. parentType->mArraySize = atUpperBound;
  3566. }
  3567. break;*/
  3568. case DW_TAG_inheritance:
  3569. {
  3570. DbgType* derivedType = GetStackTop<DbgType*>(&dataStack);
  3571. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  3572. baseTypeEntry->mBaseType = GetOrCreateType(atType, dataMap);
  3573. if (atDataMemberData != NULL)
  3574. {
  3575. bool foundVirtOffset = false;
  3576. const uint8* opPtr = atDataMemberData;
  3577. if (*(opPtr++) == DW_OP_dup)
  3578. {
  3579. if (*(opPtr++) == DW_OP_deref)
  3580. {
  3581. if (*(opPtr++) == DW_OP_constu)
  3582. {
  3583. baseTypeEntry->mVTableOffset = (int)DecodeSLEB128(opPtr) / sizeof(int32);
  3584. foundVirtOffset = true;
  3585. if (*(opPtr++) == DW_OP_minus)
  3586. baseTypeEntry->mVTableOffset = -baseTypeEntry->mVTableOffset;
  3587. }
  3588. }
  3589. }
  3590. BF_ASSERT(foundVirtOffset);
  3591. }
  3592. else
  3593. baseTypeEntry->mThisOffset = atDataMemberLocation;
  3594. derivedType->mBaseTypes.PushBack(baseTypeEntry);
  3595. }
  3596. break;
  3597. case DW_TAG_member:
  3598. {
  3599. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3600. if ((atName != NULL) && (strncmp(atName, "_vptr$", 6) == 0))
  3601. {
  3602. parentType->mHasVTable = true;
  3603. break;
  3604. }
  3605. //DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3606. DbgVariable* member = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3607. member->mIsMember = true;
  3608. member->mCompileUnit = compileUnit;
  3609. member->mName = atName;
  3610. member->mType = GetOrCreateType(atType, dataMap);
  3611. member->mConstValue = atConstValue;
  3612. member->mIsConst = hadConstValue;
  3613. member->mIsStatic = !hadMemberLocation;
  3614. member->mBitSize = atBitSize;
  3615. member->mBitOffset = atBitOffset;
  3616. member->mIsExtern = atExternal != 0;
  3617. parentType->mMemberList.PushBack(member);
  3618. member->mMemberOffset = atDataMemberLocation;
  3619. if ((member->mIsStatic) && (!member->mIsConst))
  3620. parentType->mHasStaticMembers = true;
  3621. /*if ((member->mIsStatic) && (!member->mIsConst))
  3622. mStaticVariables.push_back(member);*/
  3623. newDataPair = MakeDataPair(member);
  3624. //dataMap.Set(tagIdx, member);
  3625. }
  3626. break;
  3627. case DW_TAG_subrange_type:
  3628. {
  3629. int typeIdx = (int)(tagDataStart - startData);
  3630. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3631. int arrSize = atCount;
  3632. deferredArrayDims.push_back(arrSize);
  3633. }
  3634. break;
  3635. case DW_TAG_namespace:
  3636. case DW_TAG_const_type:
  3637. case DW_TAG_base_type:
  3638. case DW_TAG_pointer_type:
  3639. case DW_TAG_ptr_to_member_type:
  3640. case DW_TAG_array_type:
  3641. case DW_TAG_reference_type:
  3642. case DW_TAG_rvalue_reference_type:
  3643. case DW_TAG_unspecified_type:
  3644. case DW_TAG_class_type:
  3645. case DW_TAG_enumeration_type:
  3646. case DW_TAG_structure_type:
  3647. case DW_TAG_union_type:
  3648. case DW_TAG_typedef:
  3649. case DW_TAG_volatile_type:
  3650. case DW_TAG_subroutine_type:
  3651. //case DW_TAG_subrange_type:
  3652. case DW_TAG_restrict_type:
  3653. {
  3654. int typeIdx = (int)(tagDataStart - startData);
  3655. DbgType* dbgType = GetOrCreateType(typeIdx, dataMap);
  3656. const char* nameSep = (compileUnit->mLanguage == DbgLanguage_Beef) ? "." : "::";
  3657. if ((atName != NULL) &&
  3658. ((entryTag == DW_TAG_structure_type) || (entryTag == DW_TAG_class_type) ||
  3659. (entryTag == DW_TAG_typedef) || (entryTag == DW_TAG_union_type) || (entryTag == DW_TAG_enumeration_type) ||
  3660. (entryTag == DW_TAG_namespace)))
  3661. {
  3662. BF_ASSERT(dbgType->mTypeCode == DbgType_Null);
  3663. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3664. if (parentType != NULL)
  3665. {
  3666. dbgType->mParent = parentType;
  3667. dbgType->mParent->mSubTypeList.PushBack(dbgType);
  3668. /*if (dbgType->mParent->mName != NULL)
  3669. {
  3670. if (atName == NULL)
  3671. {
  3672. dbgType->mName = dbgType->mParent->mName; // Extend from name of parent if we're anonymous
  3673. }
  3674. else
  3675. {
  3676. int nameSepLen = strlen(nameSep);
  3677. int parentNameLen = strlen(dbgType->mParent->mName);
  3678. int nameLen = strlen(atName);
  3679. char* name = (char*)mAlloc.AllocBytes(parentNameLen + nameSepLen + nameLen + 1);
  3680. memcpy(name, dbgType->mParent->mName, parentNameLen);
  3681. memcpy(name + parentNameLen, nameSep, nameSepLen);
  3682. memcpy(name + parentNameLen + nameSepLen, atName, nameLen);
  3683. dbgType->mName = name;
  3684. }
  3685. }*/
  3686. }
  3687. else
  3688. {
  3689. // Add to global subtype list but don't set dbgType->mParent
  3690. compileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
  3691. }
  3692. }
  3693. const char* useName = atName;
  3694. /*if ((useName != NULL) && (strcmp(useName, "@") == 0))
  3695. useName = NULL;*/
  3696. dbgType->mCompileUnit = compileUnit;
  3697. dbgType->mLanguage = compileUnit->mLanguage;
  3698. //SplitName(atName, dbgType->mTypeName, dbgType->mTemplateParams);
  3699. dbgType->mName = useName;
  3700. if (dbgType->mTypeName == NULL)
  3701. dbgType->mTypeName = useName;
  3702. //if (dbgType->mName == NULL)
  3703. //dbgType->mName = atName;
  3704. int parentNameLen = ((dbgType->mParent != NULL) && (dbgType->mParent->mName != NULL)) ? (int)strlen(dbgType->mParent->mName) : 0;
  3705. int typeNameLen = (dbgType->mTypeName != NULL) ? (int)strlen(dbgType->mTypeName) : 0;
  3706. //int templateParamsLen = (dbgType->mTemplateParams != NULL) ? strlen(dbgType->mTemplateParams) : 0;
  3707. if ((parentNameLen != 0) /*&& (templateParamsLen == 0)*/)
  3708. {
  3709. int nameSepLen = (int)strlen(nameSep);
  3710. int nameLen = parentNameLen + typeNameLen /*+ templateParamsLen*/;
  3711. if ((parentNameLen > 0) && (nameLen > 0))
  3712. nameLen += nameSepLen;
  3713. char* namePtr = (char*)mAlloc.AllocBytes(nameLen + 1, "DWARF");
  3714. dbgType->mName = namePtr;
  3715. if (parentNameLen > 0)
  3716. {
  3717. memcpy(namePtr, dbgType->mParent->mName, parentNameLen);
  3718. namePtr += parentNameLen;
  3719. if (nameLen > 0)
  3720. {
  3721. memcpy(namePtr, nameSep, nameSepLen);
  3722. namePtr += nameSepLen;
  3723. }
  3724. }
  3725. if (nameLen > 0)
  3726. {
  3727. memcpy(namePtr, useName, typeNameLen);
  3728. namePtr += typeNameLen;
  3729. }
  3730. /*if (templateParamsLen > 0)
  3731. {
  3732. memcpy(namePtr, dbgType->mTemplateParams, templateParamsLen);
  3733. namePtr += templateParamsLen;
  3734. }*/
  3735. }
  3736. dbgType->mTypeCode = DbgType_Null;
  3737. dbgType->mIsDeclaration = atDeclaration;
  3738. if (atByteSize != -1)
  3739. {
  3740. dbgType->mSize = atByteSize;
  3741. dbgType->mSizeCalculated = true;
  3742. }
  3743. switch (entryTag)
  3744. {
  3745. case DW_TAG_base_type:
  3746. // Types that may do fallover to int/uints on size mismatch
  3747. switch (atEncoding)
  3748. {
  3749. case DW_ATE_UTF:
  3750. if (atByteSize == 1)
  3751. dbgType->mTypeCode = DbgType_Utf8;
  3752. else if (atByteSize == 2)
  3753. dbgType->mTypeCode = DbgType_Utf16;
  3754. else
  3755. dbgType->mTypeCode = DbgType_Utf32;
  3756. break;
  3757. case DW_ATE_signed_char:
  3758. if (atByteSize == 1)
  3759. dbgType->mTypeCode = DbgType_SChar;
  3760. else if (atByteSize == 2)
  3761. dbgType->mTypeCode = DbgType_SChar16;
  3762. else if (atByteSize == 4)
  3763. dbgType->mTypeCode = DbgType_SChar32;
  3764. else
  3765. atEncoding = DW_ATE_signed;
  3766. break;
  3767. case DW_ATE_unsigned_char:
  3768. if (atByteSize == 1)
  3769. dbgType->mTypeCode = DbgType_UChar;
  3770. else if (atByteSize == 2)
  3771. dbgType->mTypeCode = DbgType_UChar16;
  3772. else if (atByteSize == 4)
  3773. dbgType->mTypeCode = DbgType_UChar32;
  3774. atEncoding = DW_ATE_unsigned;
  3775. break;
  3776. case DW_ATE_boolean:
  3777. if (atByteSize == 1)
  3778. dbgType->mTypeCode = DbgType_Bool;
  3779. else
  3780. atEncoding = DW_ATE_unsigned;
  3781. break;
  3782. }
  3783. if (dbgType->mTypeCode == DbgType_Null)
  3784. {
  3785. switch (atEncoding)
  3786. {
  3787. case DW_ATE_address:
  3788. if (atByteSize == 0)
  3789. dbgType->mTypeCode = DbgType_Void;
  3790. break;
  3791. case DW_ATE_boolean:
  3792. if (atByteSize == 1)
  3793. {
  3794. dbgType->mTypeCode = DbgType_Bool;
  3795. break;
  3796. }
  3797. //Fall through
  3798. case DW_ATE_signed:
  3799. switch (atByteSize)
  3800. {
  3801. case 1:
  3802. dbgType->mTypeCode = DbgType_i8;
  3803. break;
  3804. case 2:
  3805. dbgType->mTypeCode = DbgType_i16;
  3806. break;
  3807. case 4:
  3808. dbgType->mTypeCode = DbgType_i32;
  3809. break;
  3810. case 8:
  3811. dbgType->mTypeCode = DbgType_i64;
  3812. break;
  3813. case 16:
  3814. dbgType->mTypeCode = DbgType_i128;
  3815. break;
  3816. }
  3817. break;
  3818. case DW_ATE_unsigned:
  3819. switch (atByteSize)
  3820. {
  3821. case 1:
  3822. dbgType->mTypeCode = DbgType_u8;
  3823. break;
  3824. case 2:
  3825. if ((atName != NULL) && (strcmp(atName, "wchar_t") == 0))
  3826. dbgType->mTypeCode = DbgType_UChar16;
  3827. else
  3828. dbgType->mTypeCode = DbgType_u16;
  3829. break;
  3830. case 4:
  3831. dbgType->mTypeCode = DbgType_u32;
  3832. break;
  3833. case 8:
  3834. dbgType->mTypeCode = DbgType_u64;
  3835. break;
  3836. case 16:
  3837. dbgType->mTypeCode = DbgType_u128;
  3838. break;
  3839. }
  3840. break;
  3841. case DW_ATE_float:
  3842. if (atByteSize == 4)
  3843. dbgType->mTypeCode = DbgType_Single;
  3844. else if (atByteSize == 8)
  3845. dbgType->mTypeCode = DbgType_Double;
  3846. else if (atByteSize == 12)
  3847. dbgType->mTypeCode = DbgType_Float96;
  3848. else if (atByteSize == 16)
  3849. dbgType->mTypeCode = DbgType_Float128;
  3850. break;
  3851. case DW_ATE_complex_float:
  3852. if (atByteSize == 8)
  3853. dbgType->mTypeCode = DbgType_ComplexFloat;
  3854. else if (atByteSize == 16)
  3855. dbgType->mTypeCode = DbgType_ComplexDouble;
  3856. else if (atByteSize == 24)
  3857. dbgType->mTypeCode = DbgType_ComplexDouble96;
  3858. else if (atByteSize == 32)
  3859. dbgType->mTypeCode = DbgType_ComplexDouble128;
  3860. break;
  3861. default:
  3862. BF_FATAL("Unknown DW_ATE type");
  3863. break;
  3864. }
  3865. }
  3866. break;
  3867. case DW_TAG_enumeration_type: //TODO: Handle these differently
  3868. dbgType->mTypeCode = DbgType_Enum;
  3869. dbgType->mTypeParam = mAlloc.Alloc<DbgType>();
  3870. if (atByteSize == 8)
  3871. dbgType->mTypeParam->mTypeCode = DbgType_i64;
  3872. else if (atByteSize == 4)
  3873. dbgType->mTypeParam->mTypeCode = DbgType_i32;
  3874. else if (atByteSize == 2)
  3875. dbgType->mTypeParam->mTypeCode = DbgType_i16;
  3876. else if (atByteSize == 1)
  3877. dbgType->mTypeParam->mTypeCode = DbgType_i8;
  3878. else
  3879. {
  3880. BF_DBG_FATAL("Invalid enum type");
  3881. }
  3882. break;
  3883. case DW_TAG_namespace:
  3884. dbgType->mTypeCode = DbgType_Namespace;
  3885. break;
  3886. case DW_TAG_const_type:
  3887. dbgType->mTypeCode = DbgType_Const;
  3888. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3889. break;
  3890. case DW_TAG_rvalue_reference_type:
  3891. dbgType->mTypeCode = DbgType_RValueReference;
  3892. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3893. break;
  3894. case DW_TAG_unspecified_type:
  3895. dbgType->mTypeCode = DbgType_Unspecified;
  3896. dbgType->mTypeName = atName;
  3897. break;
  3898. case DW_TAG_reference_type:
  3899. dbgType->mTypeCode = DbgType_Ref;
  3900. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3901. break;
  3902. case DW_TAG_pointer_type:
  3903. dbgType->mTypeCode = DbgType_Ptr;
  3904. dbgType->mSize = sizeof(addr_target);
  3905. dbgType->mSizeCalculated = true;
  3906. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3907. if (dbgType->mTypeParam != NULL)
  3908. dbgType->mTypeParam->mPtrType = dbgType;
  3909. break;
  3910. case DW_TAG_ptr_to_member_type:
  3911. dbgType->mTypeCode = DbgType_PtrToMember;
  3912. dbgType->mSize = sizeof(addr_target);
  3913. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3914. if (dbgType->mTypeParam != NULL)
  3915. dbgType->mTypeParam->mPtrType = dbgType;
  3916. break;
  3917. case DW_TAG_array_type:
  3918. dbgType->mTypeCode = DbgType_SizedArray;
  3919. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3920. break;
  3921. case DW_TAG_structure_type:
  3922. dbgType->mTypeCode = DbgType_Struct;
  3923. break;
  3924. case DW_TAG_class_type:
  3925. dbgType->mTypeCode = DbgType_Class;
  3926. break;
  3927. case DW_TAG_union_type:
  3928. dbgType->mTypeCode = DbgType_Union;
  3929. break;
  3930. case DW_TAG_typedef:
  3931. dbgType->mTypeCode = DbgType_TypeDef;
  3932. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3933. break;
  3934. case DW_TAG_volatile_type:
  3935. dbgType->mTypeCode = DbgType_Volatile;
  3936. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3937. break;
  3938. case DW_TAG_subroutine_type:
  3939. dbgType->mTypeCode = DbgType_Subroutine;
  3940. if (atType != 0) // Return value
  3941. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3942. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  3943. break;
  3944. case DW_TAG_restrict_type:
  3945. dbgType->mTypeCode = DbgType_Restrict;
  3946. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3947. break;
  3948. }
  3949. newDataPair = MakeDataPair(dbgType);
  3950. }
  3951. break;
  3952. }
  3953. if (hasChildren)
  3954. dataStack.push_back(newDataPair);
  3955. }
  3956. for (auto& abstractOriginEntry : abstractOriginReplaceList)
  3957. abstractOriginEntry.Replace();
  3958. GetLinkedModule()->MapTypes(startingTypeIdx);
  3959. dataPtr = dataEnd;
  3960. return true;
  3961. }
  3962. void DbgModule::ParseDebugFrameData()
  3963. {
  3964. BP_ZONE("ParseDebugFrameData");
  3965. const uint8* data = mDebugFrameData;
  3966. if (data == NULL)
  3967. return;
  3968. mParsedFrameDescriptors = true;
  3969. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  3970. while (true)
  3971. {
  3972. addr_target relSectionAddr = (addr_target)(data - mDebugFrameData);
  3973. int length = GET(int);
  3974. if (length == 0)
  3975. break;
  3976. const uint8* dataEnd = data + length;
  3977. int cieID = GET(int);
  3978. if (cieID < 0)
  3979. {
  3980. BP_ALLOC_T(DwCommonFrameDescriptor);
  3981. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  3982. char version = GET(char);
  3983. commonFrameDescriptor->mDbgModule = this;
  3984. commonFrameDescriptor->mAugmentation = DataGetString(data);
  3985. if (version >= 4)
  3986. {
  3987. commonFrameDescriptor->mPointerSize = GET(int8);
  3988. commonFrameDescriptor->mSegmentSize = GET(int8);
  3989. }
  3990. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  3991. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  3992. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  3993. commonFrameDescriptor->mInstData = data;
  3994. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  3995. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  3996. if (version < 3)
  3997. commonFrameDescriptorMap[relSectionAddr] = commonFrameDescriptor;
  3998. else
  3999. commonFrameDescriptorMap[mDebugFrameAddress + relSectionAddr] = commonFrameDescriptor;
  4000. }
  4001. else
  4002. {
  4003. addr_target lowPC = GET(addr_target);
  4004. addr_target highPC = lowPC + GET(addr_target);
  4005. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)cieID];
  4006. BF_ASSERT(commonFrameDescriptor != NULL);
  4007. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  4008. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  4009. auto frameDescriptor = &resultPair.first->second;
  4010. //frameDescriptor->
  4011. frameDescriptor->mLowPC = lowPC;
  4012. frameDescriptor->mHighPC = highPC;
  4013. frameDescriptor->mInstData = data;
  4014. frameDescriptor->mInstLen = (int)(dataEnd - data);
  4015. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  4016. }
  4017. data = dataEnd;
  4018. }
  4019. }
  4020. void DbgModule::ParseEHFrameData()
  4021. {
  4022. const uint8* data = mEHFrameData;
  4023. if (data == NULL)
  4024. return;
  4025. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  4026. while (true)
  4027. {
  4028. addr_target sectionAddress = (addr_target)(data - mEHFrameData);
  4029. int length = GET(int);
  4030. if (length == 0)
  4031. break;
  4032. const uint8* dataEnd = data + length;
  4033. int cieID = GET(int);
  4034. if (cieID <= 0)
  4035. {
  4036. BP_ALLOC_T(DwCommonFrameDescriptor);
  4037. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  4038. char version = GET(char);
  4039. const char* augmentation = DataGetString(data);
  4040. commonFrameDescriptor->mDbgModule = this;
  4041. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  4042. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  4043. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  4044. commonFrameDescriptor->mAugmentation = augmentation;
  4045. if (*augmentation == 'z')
  4046. {
  4047. ++augmentation;
  4048. int augLen = (int)DecodeULEB128(data);
  4049. commonFrameDescriptor->mAugmentationLength = augLen;
  4050. const uint8* augEnd = data + augLen;
  4051. while (*augmentation != '\0')
  4052. {
  4053. if (*augmentation == 'R')
  4054. commonFrameDescriptor->mAddressPointerEncoding = (int) GET(uint8);
  4055. else if (*augmentation == 'P')
  4056. {
  4057. int encodingType = GET(uint8);
  4058. BF_ASSERT(encodingType == 0);
  4059. commonFrameDescriptor->mLSDARoutine = GET(addr_target);
  4060. }
  4061. else if (*augmentation == 'L')
  4062. commonFrameDescriptor->mLSDAPointerEncodingFDE = GET(uint8);
  4063. else if (*augmentation == 'S')
  4064. {
  4065. // mIsSignalHandler - on return from stack frame, CFA is before next instruction rather than after it
  4066. }
  4067. else
  4068. BF_FATAL("Unknown CIE augmentation");
  4069. ++augmentation;
  4070. }
  4071. data = augEnd;
  4072. }
  4073. commonFrameDescriptor->mInstData = data;
  4074. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  4075. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  4076. commonFrameDescriptorMap[sectionAddress] = commonFrameDescriptor;
  4077. }
  4078. else
  4079. {
  4080. int ciePos = (int)(sectionAddress - cieID) + 4;
  4081. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)ciePos];
  4082. addr_target lowPC;
  4083. addr_target highPC;
  4084. if (commonFrameDescriptor->mAddressPointerEncoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4))
  4085. {
  4086. lowPC = GET(int);
  4087. lowPC += mEHFrameAddress + sectionAddress + 8;
  4088. highPC = lowPC + GET(int);
  4089. }
  4090. else
  4091. {
  4092. lowPC = GET(int);
  4093. highPC = lowPC + GET(int);
  4094. }
  4095. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  4096. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  4097. auto frameDescriptor = &resultPair.first->second;
  4098. frameDescriptor->mLSDARoutine = commonFrameDescriptor->mLSDARoutine;
  4099. const char* augmentation = commonFrameDescriptor->mAugmentation;
  4100. if (*augmentation == 'z')
  4101. {
  4102. int augLen = GET(uint8);
  4103. const uint8* augEnd = data + augLen;
  4104. ++augmentation;
  4105. while (*augmentation != '\0')
  4106. {
  4107. if (*augmentation == 'R')
  4108. {
  4109. }
  4110. else if (*augmentation == 'P')
  4111. {
  4112. }
  4113. else if (*augmentation == 'L')
  4114. {
  4115. BF_ASSERT(commonFrameDescriptor->mLSDAPointerEncodingFDE == 0);
  4116. frameDescriptor->mLSDARoutine = GET(addr_target);
  4117. }
  4118. else if (*augmentation == 'S')
  4119. {
  4120. }
  4121. else
  4122. BF_FATAL("Unknown CIE augmentation");
  4123. augmentation++;
  4124. }
  4125. data = augEnd;
  4126. }
  4127. frameDescriptor->mLowPC = lowPC;
  4128. frameDescriptor->mHighPC = highPC;
  4129. frameDescriptor->mInstData = data;
  4130. frameDescriptor->mInstLen = (int)(dataEnd - data);
  4131. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  4132. }
  4133. data = dataEnd;
  4134. }
  4135. }
  4136. void DbgModule::FlushLineData(DbgSubprogram* curSubprogram, std::list<DbgLineData>& queuedLineData)
  4137. {
  4138. }
  4139. DbgSrcFile* DbgModule::AddSrcFile(DbgCompileUnit* compileUnit, const String& srcFilePath)
  4140. {
  4141. DbgSrcFile* dwSrcFile = mDebugTarget->AddSrcFile(srcFilePath);
  4142. if (compileUnit != NULL)
  4143. {
  4144. DbgSrcFileReference srcFileRef;
  4145. srcFileRef.mSrcFile = dwSrcFile;
  4146. srcFileRef.mCompileUnit = compileUnit;
  4147. compileUnit->mSrcFileRefs.push_back(srcFileRef);
  4148. }
  4149. return dwSrcFile;
  4150. }
  4151. bool DbgModule::ParseDebugLineInfo(const uint8*& dataPtr, int compileUnitIdx)
  4152. {
  4153. BP_ZONE("ParseDebugLineInfo");
  4154. const uint8* data = dataPtr;
  4155. const int startOffset = (int)(data - mDebugLineData);
  4156. int length = GET(int);
  4157. if (length == 0)
  4158. return false;
  4159. DbgCompileUnit* dwCompileUnit = mCompileUnits[compileUnitIdx];
  4160. const uint8* dataEnd = data + length;
  4161. short version = GET(short);
  4162. int headerLength = GET(int);
  4163. char minimumInstructionLength = GET(char);
  4164. int maximumOperationsPerInstruction = 1;
  4165. char defaultIsStmt = GET(char);
  4166. char lineBase = GET(char);
  4167. char lineRange = GET(char);
  4168. char opcodeBase = GET(char);
  4169. for (int i = 0; i < opcodeBase - 1; i++)
  4170. {
  4171. char standardOpcodeLengths = GET(char);
  4172. }
  4173. Array<const char*> directoryNames;
  4174. while (true)
  4175. {
  4176. const char* name = DataGetString(data);
  4177. if (name[0] == 0)
  4178. break;
  4179. directoryNames.push_back(name);
  4180. }
  4181. DbgSrcFileReference* dwSrcFileRef = NULL;
  4182. HashSet<String> foundPathSet;
  4183. int curFileIdx = 0;
  4184. DbgSubprogram* curSubprogram = NULL;
  4185. #define ADD_LINEDATA(lineData) \
  4186. lineBuilder.Add(dwCompileUnit, lineData, dwSrcFileRef->mSrcFile, NULL);
  4187. while (true)
  4188. {
  4189. const char* path = DataGetString(data);
  4190. if (path[0] == 0)
  4191. break;
  4192. int directoryIdx = (int)DecodeULEB128(data);
  4193. int lastModificationTime = (int)DecodeULEB128(data);
  4194. int fileLength = (int)DecodeULEB128(data);
  4195. String filePath;
  4196. if (directoryIdx > 0)
  4197. filePath = String(directoryNames[directoryIdx - 1]) + "/";
  4198. filePath += path;
  4199. filePath = GetAbsPath(filePath, dwCompileUnit->mCompileDir);
  4200. AddSrcFile(dwCompileUnit, filePath.c_str());
  4201. }
  4202. if (dwCompileUnit->mSrcFileRefs.size() > 0)
  4203. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs.front();
  4204. DbgLineDataBuilder lineBuilder(this);
  4205. bool queuedPostPrologue = false;
  4206. DbgLineDataState dwLineData;
  4207. dwLineData.mLine = 0;
  4208. dwLineData.mRelAddress = 0;
  4209. dwLineData.mOpIndex = 0;
  4210. dwLineData.mBasicBlock = false;
  4211. dwLineData.mDiscriminator = 0;
  4212. dwLineData.mIsStmt = defaultIsStmt != 0;
  4213. dwLineData.mIsa = 0;
  4214. dwLineData.mColumn = -2;
  4215. while (data < dataEnd)
  4216. {
  4217. uint8_t opcode = GET(uint8_t);
  4218. switch (opcode)
  4219. {
  4220. case DW_LNS_extended_op:
  4221. {
  4222. int len = (int)DecodeULEB128(data);
  4223. uint8_t exOpcode = GET(uint8_t);
  4224. switch (exOpcode)
  4225. {
  4226. case DW_LNE_end_sequence:
  4227. {
  4228. ADD_LINEDATA(dwLineData);
  4229. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[0];
  4230. dwLineData.mLine = 0;
  4231. dwLineData.mRelAddress = 0;
  4232. dwLineData.mOpIndex = 0;
  4233. dwLineData.mBasicBlock = false;
  4234. dwLineData.mDiscriminator = 0;
  4235. dwLineData.mIsStmt = defaultIsStmt != 0;
  4236. dwLineData.mIsa = 0;
  4237. dwLineData.mColumn = -2;
  4238. }
  4239. break;
  4240. case DW_LNE_set_address:
  4241. dwLineData.mRelAddress = (uint32)(RemapAddr(GET(addr_target)) - mImageBase);
  4242. break;
  4243. case DW_LNE_define_file:
  4244. {
  4245. const char* path = DataGetString(data);
  4246. int directoryIdx = (int)DecodeULEB128(data);
  4247. int lastModificationTime = (int)DecodeULEB128(data);
  4248. int fileLength = (int)DecodeULEB128(data);
  4249. }
  4250. break;
  4251. case DW_LNE_set_discriminator:
  4252. dwLineData.mDiscriminator = (int)DecodeULEB128(data);
  4253. break;
  4254. }
  4255. }
  4256. break;
  4257. case DW_LNS_copy:
  4258. ADD_LINEDATA(dwLineData);
  4259. dwLineData.mDiscriminator = 0;
  4260. dwLineData.mBasicBlock = false;
  4261. break;
  4262. case DW_LNS_advance_pc:
  4263. {
  4264. int advance = (int)DecodeULEB128(data);
  4265. dwLineData.mRelAddress += advance;
  4266. // How to advance opCode addr?
  4267. }
  4268. break;
  4269. case DW_LNS_advance_line:
  4270. {
  4271. int advance = (int)DecodeSLEB128(data);
  4272. dwLineData.mLine += advance;
  4273. }
  4274. break;
  4275. case DW_LNS_set_file:
  4276. {
  4277. curFileIdx = (int)DecodeULEB128(data) - 1;
  4278. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[curFileIdx];
  4279. //dwLineData.mSrcFileRef = dwSrcFileRef;
  4280. }
  4281. break;
  4282. case DW_LNS_set_column:
  4283. {
  4284. dwLineData.mColumn = (int)DecodeULEB128(data) - 1;
  4285. }
  4286. break;
  4287. case DW_LNS_negate_stmt:
  4288. {
  4289. dwLineData.mIsStmt = !dwLineData.mIsStmt;
  4290. }
  4291. break;
  4292. case DW_LNS_set_basic_block:
  4293. {
  4294. dwLineData.mBasicBlock = true;
  4295. }
  4296. break;
  4297. case DW_LNS_const_add_pc:
  4298. {
  4299. int adjustedOpcode = 255 - opcodeBase;
  4300. int opAdvance = adjustedOpcode / lineRange;
  4301. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4302. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4303. dwLineData.mRelAddress = newAddress;
  4304. dwLineData.mOpIndex = newOpIndex;
  4305. }
  4306. break;
  4307. case DW_LNS_fixed_advance_pc:
  4308. {
  4309. uint16_t advance = GET(uint16_t);
  4310. dwLineData.mRelAddress += advance;
  4311. dwLineData.mOpIndex = 0;
  4312. }
  4313. break;
  4314. case DW_LNS_set_prologue_end:
  4315. {
  4316. queuedPostPrologue = true;
  4317. }
  4318. break;
  4319. case DW_LNS_set_epilogue_begin:
  4320. {
  4321. dwLineData.mColumn = -2;
  4322. }
  4323. break;
  4324. case DW_LNS_set_isa:
  4325. {
  4326. dwLineData.mIsa = (int)DecodeULEB128(data);
  4327. }
  4328. break;
  4329. default:
  4330. {
  4331. // Special opcode
  4332. int adjustedOpcode = opcode - opcodeBase;
  4333. int opAdvance = adjustedOpcode / lineRange;
  4334. uint32 oldAddress = dwLineData.mRelAddress;
  4335. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4336. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4337. int lineIncrement = lineBase + (adjustedOpcode % lineRange);
  4338. dwLineData.mLine += lineIncrement;
  4339. dwLineData.mRelAddress = newAddress;
  4340. dwLineData.mOpIndex = newOpIndex;
  4341. DbgLineData* lastLineData = NULL;
  4342. if ((newAddress == oldAddress) && (queuedPostPrologue) && (curSubprogram != NULL) && (curSubprogram->mBlock.mLowPC == newAddress))
  4343. {
  4344. // Adjust this line later
  4345. ADD_LINEDATA(dwLineData);
  4346. }
  4347. queuedPostPrologue = false;
  4348. }
  4349. break;
  4350. }
  4351. }
  4352. lineBuilder.Commit();
  4353. dataPtr = data;
  4354. return true;
  4355. }
  4356. addr_target DbgModule::GetHotTargetAddress(DbgHotTargetSection* hotTargetSection)
  4357. {
  4358. if ((hotTargetSection->mTargetSectionAddr == NULL) && (hotTargetSection->mDataSize > 0))
  4359. {
  4360. if (hotTargetSection->mNoTargetAlloc)
  4361. return 0;
  4362. hotTargetSection->mTargetSectionAddr = mDebugger->AllocHotTargetMemory(hotTargetSection->mDataSize, hotTargetSection->mCanExecute, hotTargetSection->mCanWrite, &hotTargetSection->mTargetSectionSize);
  4363. hotTargetSection->mImageOffset = (int)mImageSize;
  4364. if (mImageBase == NULL)
  4365. {
  4366. mImageBase = hotTargetSection->mTargetSectionAddr;
  4367. mOrigImageData->mAddr = mImageBase;
  4368. }
  4369. mImageSize += hotTargetSection->mTargetSectionSize;
  4370. /*if (mExceptionData == hotTargetSection->mData)
  4371. mExceptionDataRVA = (addr_target)(hotTargetSection->mTargetSectionAddr - mImageBase);*/
  4372. }
  4373. return hotTargetSection->mTargetSectionAddr;
  4374. }
  4375. uint8* DbgModule::GetHotTargetData(addr_target address)
  4376. {
  4377. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4378. {
  4379. if (mHotTargetSections[sectNum] != NULL)
  4380. {
  4381. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4382. if ((address >= hotTargetSection->mTargetSectionAddr) && (address < hotTargetSection->mTargetSectionAddr + hotTargetSection->mTargetSectionSize))
  4383. {
  4384. return hotTargetSection->mData + (address - hotTargetSection->mTargetSectionAddr);
  4385. }
  4386. }
  4387. }
  4388. return NULL;
  4389. }
  4390. void DbgModule::DoReloc(DbgHotTargetSection* hotTargetSection, COFFRelocation& coffReloc, addr_target resolvedSymbolAddr, PE_SymInfo* symInfo)
  4391. {
  4392. #ifdef BF_DBG_32
  4393. if (coffReloc.mType == IMAGE_REL_I386_DIR32)
  4394. {
  4395. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4396. }
  4397. else if (coffReloc.mType == IMAGE_REL_I386_DIR32NB)
  4398. {
  4399. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4400. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4401. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4402. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4403. }
  4404. else if (coffReloc.mType == IMAGE_REL_I386_REL32)
  4405. {
  4406. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4407. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr - myAddr - sizeof(int32);
  4408. }
  4409. else if (coffReloc.mType == IMAGE_REL_I386_SECTION)
  4410. {
  4411. // auto linkedModule = GetLinkedModule();
  4412. // addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4413. // int* encodingPtr = NULL;
  4414. // if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4415. // {
  4416. // *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4417. // linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4418. // }
  4419. // *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4420. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0;
  4421. }
  4422. else if (coffReloc.mType == IMAGE_REL_I386_SECREL)
  4423. {
  4424. //*(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += symInfo->mValue;
  4425. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4426. }
  4427. else
  4428. {
  4429. BF_ASSERT(0=="Invalid COFF reloc type");
  4430. }
  4431. #else
  4432. // CodeView uses SECTION:SECREL locations, and we just want to find a mapping such that
  4433. // COFF::GetSectionAddr can map it to the 64-bit address. We do this by encoding the
  4434. // lower 31 bits in the SECREL (allowing a 31-bit offset at the destination as well)
  4435. // and then we use a 15-bit key to map the upper bits
  4436. if (coffReloc.mType == IMAGE_REL_AMD64_REL32)
  4437. {
  4438. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4439. intptr_target addrOffset = resolvedSymbolAddr - myAddr - sizeof(int32);
  4440. BF_ASSERT((int64)(int32)addrOffset == addrOffset);
  4441. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (int32)addrOffset;
  4442. }
  4443. else if (coffReloc.mType == IMAGE_REL_AMD64_SECTION)
  4444. {
  4445. /*if (symInfo != NULL)
  4446. {
  4447. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = symInfo->mSectionNum;
  4448. }
  4449. else*/
  4450. {
  4451. auto linkedModule = GetLinkedModule();
  4452. addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4453. /*auto pair = linkedModule->mSecRelEncodingMap.insert(std::make_pair(mappedAddr, (int)linkedModule->mSecRelEncodingMap.size()));
  4454. if (pair.second)
  4455. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);*/
  4456. int* encodingPtr = NULL;
  4457. if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4458. {
  4459. *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4460. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4461. }
  4462. //*(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | pair.first->second;
  4463. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4464. }
  4465. }
  4466. else if (coffReloc.mType == IMAGE_REL_AMD64_SECREL)
  4467. {
  4468. auto linkedModule = GetLinkedModule();
  4469. if ((resolvedSymbolAddr >= linkedModule->mTLSAddr) && (resolvedSymbolAddr < linkedModule->mTLSAddr + linkedModule->mTLSSize))
  4470. {
  4471. // Make relative to actual TLS data
  4472. resolvedSymbolAddr -= linkedModule->mTLSAddr;
  4473. }
  4474. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr & 0x7FFFFFF);
  4475. }
  4476. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR64)
  4477. {
  4478. *(uint64*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4479. }
  4480. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB)
  4481. {
  4482. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4483. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4484. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4485. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4486. //*(int32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += secRelAddr;
  4487. }
  4488. else
  4489. {
  4490. BF_ASSERT(0=="Invalid COFF reloc type");
  4491. }
  4492. #endif
  4493. }
  4494. bool DbgModule::IsHotSwapPreserve(const String& name)
  4495. {
  4496. // We have different rules for overwriting symbols in DWARF vs CodeView
  4497. // Since MS mangling includes return types, we know that a type change of a static
  4498. // member will mangle to a new name whereas with DWARF we DO want a new
  4499. // address if the type changes but we can't tell that based on the mangle alone,
  4500. // thus the reliance on the side table of mStaticVariables. We still do need
  4501. // to determine whether the symbol is data (and thus we do preserve) or a method
  4502. // (in which case we don't)
  4503. if ((mDbgFlavor == DbgFlavor_MS) && (BfDemangler::IsData(name)))
  4504. {
  4505. if ((!name.StartsWith("?")) && (name.Contains("sBfTypeData"))) // We DO need to replace the fields/methods/etc but not the base sBfTypeData
  4506. return false;
  4507. if (name.StartsWith("?bf_hs_replace_"))
  4508. return false;
  4509. return true;
  4510. }
  4511. const char* prefix = "bf_hs_preserve@";
  4512. return strncmp(name.c_str(), prefix, strlen(prefix)) == 0;
  4513. }
  4514. void DbgModule::ParseHotTargetSections(DataStream* stream, addr_target* resolvedSymbolAddrs)
  4515. {
  4516. auto mainModule = mDebugTarget->mTargetBinary;
  4517. mainModule->ParseSymbolData();
  4518. String name;
  4519. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4520. {
  4521. if (mHotTargetSections[sectNum] != NULL)
  4522. {
  4523. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4524. stream->SetPos(hotTargetSection->mPointerToRelocations);
  4525. for (int relocIdx = 0; relocIdx < hotTargetSection->mNumberOfRelocations; relocIdx++)
  4526. {
  4527. COFFRelocation coffReloc;
  4528. stream->Read(&coffReloc, sizeof(COFFRelocation));
  4529. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[coffReloc.mSymbolTableIndex * 18];
  4530. //const char* symName = mSymbolData[coffReloc.mSymbolTableIndex];
  4531. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  4532. if (symInfo->mNameOfs[0] != 0)
  4533. {
  4534. if (symInfo->mName[7] != 0)
  4535. {
  4536. // Name is exactly 8 chars, not null terminated yet
  4537. name = String(symInfo->mName, symInfo->mName + 8);
  4538. }
  4539. else
  4540. name = symInfo->mName;
  4541. }
  4542. else
  4543. name = mStringTable + symInfo->mNameOfs[1];
  4544. bool didNameMatch = false;
  4545. addr_target resolvedSymbolAddr = resolvedSymbolAddrs[coffReloc.mSymbolTableIndex];
  4546. #ifdef BF_DBG_32
  4547. 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);
  4548. if (name[0] == '_')
  4549. name.Remove(0, 1);
  4550. #else
  4551. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_AMD64_ADDR64) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB) ||
  4552. ((coffReloc.mType >= IMAGE_REL_AMD64_REL32) || (coffReloc.mType <= IMAGE_REL_AMD64_REL32_5));
  4553. #endif
  4554. bool isHsPrev = false;
  4555. if (name.StartsWith("bf_hs_prev@"))
  4556. {
  4557. isHsPrev = true;
  4558. name.Remove(0, 11);
  4559. }
  4560. bool deferResolve = false;
  4561. if ((resolvedSymbolAddr == 0) && (needsSymbolAddr))
  4562. {
  4563. bool isHotSwapPreserve = IsHotSwapPreserve(name);
  4564. if ((symInfo->mSectionNum == 0) || (isHotSwapPreserve) || (isHsPrev))
  4565. {
  4566. auto origSymbolEntry = mainModule->mSymbolNameMap.Find(name.c_str());
  4567. if (origSymbolEntry != NULL)
  4568. {
  4569. resolvedSymbolAddr = origSymbolEntry->mValue->mAddress;
  4570. }
  4571. else
  4572. {
  4573. //BF_FATAL("Symbol lookup error");
  4574. deferResolve = true;
  4575. }
  4576. }
  4577. if ((symInfo->mSectionNum != 0) && (resolvedSymbolAddr == NULL))
  4578. {
  4579. DbgHotTargetSection* refHotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  4580. resolvedSymbolAddr = GetHotTargetAddress(refHotTargetSection) + symInfo->mValue;
  4581. // Using the !hotTargetSection->mNoTargetAlloc check down here caused us to not properly remap reloaded
  4582. // static members in the debug info. Even though we parse the debug info before we apply the deferred
  4583. // resolves, the mLocData points into the original data so we still get it remapped when we use that
  4584. // mLocData
  4585. if (/*(!hotTargetSection->mNoTargetAlloc) &&*/ ((refHotTargetSection->mData == NULL) || (refHotTargetSection->mNoTargetAlloc)) &&
  4586. (!isStaticSymbol))
  4587. deferResolve = true;
  4588. else
  4589. deferResolve = false;
  4590. }
  4591. }
  4592. if (deferResolve)
  4593. {
  4594. // It's a static field, defer resolution, but don't bother replacing for debug info sections
  4595. DbgDeferredHotResolve* deferredResolve = mDeferredHotResolveList.Alloc();
  4596. deferredResolve->mHotTargetSection = hotTargetSection;
  4597. deferredResolve->mName = name;
  4598. deferredResolve->mNewAddr = resolvedSymbolAddr;
  4599. deferredResolve->mReloc = coffReloc;
  4600. continue;
  4601. }
  4602. else
  4603. {
  4604. resolvedSymbolAddrs[coffReloc.mSymbolTableIndex] = resolvedSymbolAddr;
  4605. DoReloc(hotTargetSection, coffReloc, resolvedSymbolAddr, symInfo);
  4606. }
  4607. }
  4608. }
  4609. }
  4610. }
  4611. void DbgModule::CommitHotTargetSections()
  4612. {
  4613. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4614. {
  4615. if (mHotTargetSections[sectNum] != NULL)
  4616. {
  4617. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4618. addr_target hotAddr = GetHotTargetAddress(hotTargetSection);
  4619. if (hotAddr != 0)
  4620. {
  4621. // void* imageDestPtr = mOrigImageData->mBlocks[0] + hotTargetSection->mImageOffset;
  4622. // if (hotTargetSection->mData != NULL)
  4623. // memcpy(imageDestPtr, hotTargetSection->mData, hotTargetSection->mDataSize);
  4624. // else
  4625. // memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4626. BF_ASSERT(mOrigImageData->mAddr != 0);
  4627. void* imageDestPtr = hotTargetSection->mData;
  4628. bool isTemp = false;
  4629. if (imageDestPtr == NULL)
  4630. {
  4631. imageDestPtr = new uint8[hotTargetSection->mDataSize];
  4632. memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4633. isTemp = true;
  4634. }
  4635. if (hotTargetSection->mCanExecute)
  4636. {
  4637. bool success = mDebugger->WriteInstructions(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4638. BF_ASSERT(success);
  4639. }
  4640. else
  4641. {
  4642. bool success = mDebugger->WriteMemory(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4643. BF_ASSERT(success);
  4644. }
  4645. if (isTemp)
  4646. delete imageDestPtr;
  4647. }
  4648. }
  4649. }
  4650. }
  4651. void DbgModule::HotReplaceType(DbgType* newType)
  4652. {
  4653. auto linkedModule = GetLinkedModule();
  4654. newType->PopulateType();
  4655. DbgType* primaryType = linkedModule->GetPrimaryType(newType);
  4656. if (primaryType == newType)
  4657. {
  4658. // There was no previous type
  4659. BF_ASSERT(primaryType->mHotNewType == NULL);
  4660. return;
  4661. }
  4662. if (primaryType->mHotNewType != newType)
  4663. {
  4664. // We have already pulled in the new data from a previous new type
  4665. BF_ASSERT(primaryType->mHotNewType == NULL);
  4666. return;
  4667. }
  4668. primaryType->mHotNewType = NULL;
  4669. primaryType->PopulateType();
  4670. linkedModule->ParseGlobalsData();
  4671. linkedModule->ParseSymbolData();
  4672. if (primaryType->mNeedsGlobalsPopulated)
  4673. {
  4674. // These aren't proper TPI types so we don't have any method declarations until we PopulateTypeGlobals
  4675. linkedModule->PopulateTypeGlobals(primaryType);
  4676. }
  4677. for (auto methodNameEntry : primaryType->mMethodNameList)
  4678. {
  4679. if (methodNameEntry->mCompileUnitId != -1)
  4680. {
  4681. linkedModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  4682. methodNameEntry->mCompileUnitId = -1;
  4683. }
  4684. }
  4685. // Now actually remove the linedata from the defining module
  4686. HashSet<DbgSrcFile*> checkedFiles;
  4687. for (auto method : primaryType->mMethodList)
  4688. {
  4689. //method->mWasModuleHotReplaced = true;
  4690. method->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Orphaned; // May be temporarily orphaned
  4691. if (method->mLineInfo == NULL)
  4692. continue;
  4693. //FIXME: Hot replacing lines
  4694. DbgSrcFile* lastSrcFile = NULL;
  4695. checkedFiles.Clear();
  4696. int prevCtx = -1;
  4697. auto inlineRoot = method->GetRootInlineParent();
  4698. for (int lineIdx = 0; lineIdx < method->mLineInfo->mLines.mSize; lineIdx++)
  4699. {
  4700. auto& lineData = method->mLineInfo->mLines[lineIdx];
  4701. if (lineData.mCtxIdx != prevCtx)
  4702. {
  4703. auto ctxInfo = inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx];
  4704. auto srcFile = ctxInfo.mSrcFile;
  4705. prevCtx = lineData.mCtxIdx;
  4706. if (srcFile != lastSrcFile)
  4707. {
  4708. if (checkedFiles.Add(srcFile))
  4709. {
  4710. // Remove linedata for old type
  4711. // These go into a hot-replaced list so we can still bind to them -- that is necessary because
  4712. // we may still have old versions of this method running (and may forever, if its in a loop on some thread)
  4713. // since we only patch entry points
  4714. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, primaryType->mCompileUnit, true);
  4715. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, method, true);
  4716. srcFile->RemoveLines(method->mCompileUnit->mDbgModule, method, true);
  4717. }
  4718. lastSrcFile = srcFile;
  4719. }
  4720. }
  4721. }
  4722. }
  4723. //DbgType* primaryType = newType->GetPrimaryType();
  4724. // We need to keep a persistent list of hot replaced methods so we can set hot jumps
  4725. // in old methods that may still be on the callstack. These entries get removed when
  4726. // we unload unused hot files in
  4727. while (!primaryType->mMethodList.IsEmpty())
  4728. {
  4729. auto method = primaryType->mMethodList.PopFront();
  4730. method->PopulateSubprogram();
  4731. primaryType->mHotReplacedMethodList.PushFront(method);
  4732. mHotPrimaryTypes.Add(primaryType);
  4733. }
  4734. Dictionary<StringView, DbgSubprogram*> oldProgramMap;
  4735. for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4736. {
  4737. oldMethod->PopulateSubprogram();
  4738. if (oldMethod->mBlock.IsEmpty())
  4739. continue;
  4740. auto symInfo = mDebugTarget->mSymbolMap.Get(oldMethod->mBlock.mLowPC);
  4741. if (symInfo != NULL)
  4742. {
  4743. oldProgramMap.TryAdd(symInfo->mName, oldMethod);
  4744. }
  4745. }
  4746. bool setHotJumpFailed = false;
  4747. while (!newType->mMethodList.IsEmpty())
  4748. {
  4749. DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4750. if (!newMethod->mBlock.IsEmpty())
  4751. {
  4752. BfLogDbg("Hot added new method %p %s Address:%p\n", newMethod, newMethod->mName, newMethod->mBlock.mLowPC);
  4753. newMethod->PopulateSubprogram();
  4754. auto symInfo = mDebugTarget->mSymbolMap.Get(newMethod->mBlock.mLowPC);
  4755. if (symInfo != NULL)
  4756. {
  4757. DbgSubprogram* oldMethod = NULL;
  4758. if (oldProgramMap.TryGetValue(symInfo->mName, &oldMethod))
  4759. {
  4760. bool doHotJump = false;
  4761. if (oldMethod->Equals(newMethod))
  4762. {
  4763. doHotJump = true;
  4764. }
  4765. else
  4766. {
  4767. // When mangles match but the actual signatures don't match, that can mean that the call signature was changed
  4768. // and thus it's actually a different method and shouldn't hot jump OR it could be lambda whose captures changed.
  4769. // When the lambda captures change, the user didn't actually enter a different signature so we want to do a hard
  4770. // fail if the old code gets called to avoid confusion of "why aren't my changes working?"
  4771. // If we removed captures then we can still do the hot jump. Otherwise we have to fail...
  4772. doHotJump = false;
  4773. if ((oldMethod->IsLambda()) && (oldMethod->Equals(newMethod, true)) &&
  4774. (oldMethod->mHasThis) && (newMethod->mHasThis))
  4775. {
  4776. auto oldParam = oldMethod->mParams.front();
  4777. auto newParam = newMethod->mParams.front();
  4778. if ((oldParam->mType->IsPointer()) && (newParam->mType->IsPointer()))
  4779. {
  4780. auto oldType = oldParam->mType->mTypeParam->GetPrimaryType();
  4781. oldType->PopulateType();
  4782. auto newType = newParam->mType->mTypeParam->GetPrimaryType();
  4783. newType->PopulateType();
  4784. if ((oldType->IsStruct()) && (newType->IsStruct()))
  4785. {
  4786. bool wasMatch = true;
  4787. auto oldMember = oldType->mMemberList.front();
  4788. auto newMember = newType->mMemberList.front();
  4789. while (newMember != NULL)
  4790. {
  4791. if (oldMember == NULL)
  4792. {
  4793. wasMatch = false;
  4794. break;
  4795. }
  4796. if ((oldMember->mName == NULL) || (newMember->mName == NULL))
  4797. {
  4798. wasMatch = false;
  4799. break;
  4800. }
  4801. if (strcmp(oldMember->mName, newMember->mName) != 0)
  4802. {
  4803. wasMatch = false;
  4804. break;
  4805. }
  4806. if (!oldMember->mType->Equals(newMember->mType))
  4807. {
  4808. wasMatch = false;
  4809. break;
  4810. }
  4811. oldMember = oldMember->mNext;
  4812. newMember = newMember->mNext;
  4813. }
  4814. if (wasMatch)
  4815. doHotJump = true;
  4816. }
  4817. }
  4818. if (!doHotJump)
  4819. {
  4820. mDebugTarget->mDebugger->PhysSetBreakpoint(oldMethod->mBlock.mLowPC);
  4821. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Invalid;
  4822. }
  4823. }
  4824. }
  4825. if (doHotJump)
  4826. {
  4827. if (!setHotJumpFailed)
  4828. {
  4829. if (!mDebugger->SetHotJump(oldMethod, newMethod->mBlock.mLowPC, (int)(newMethod->mBlock.mHighPC - newMethod->mBlock.mLowPC)))
  4830. setHotJumpFailed = true;
  4831. }
  4832. oldMethod->mHotReplaceKind = DbgSubprogram::HotReplaceKind_Replaced;
  4833. }
  4834. }
  4835. }
  4836. }
  4837. newMethod->mParentType = primaryType;
  4838. primaryType->mMethodList.PushBack(newMethod);
  4839. }
  4840. //mDebugTarget->mSymbolMap.Get()
  4841. // bool setHotJumpFailed = false;
  4842. // while (!newType->mMethodList.IsEmpty())
  4843. // {
  4844. // DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4845. // if (!newMethod->mBlock.IsEmpty())
  4846. // {
  4847. // newMethod->PopulateSubprogram();
  4848. //
  4849. // bool found = false;
  4850. // for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4851. // {
  4852. // if (oldMethod->mBlock.IsEmpty())
  4853. // continue;
  4854. // if (oldMethod->Equals(newMethod))
  4855. // {
  4856. // if (!setHotJumpFailed)
  4857. // {
  4858. // if (!mDebugger->SetHotJump(oldMethod, newMethod))
  4859. // setHotJumpFailed = true;
  4860. // oldMethod->mWasHotReplaced = true;
  4861. // }
  4862. // }
  4863. // }
  4864. // }
  4865. // newMethod->mParentType = primaryType;
  4866. // primaryType->mMethodList.PushBack(newMethod);
  4867. // }
  4868. primaryType->mCompileUnit->mWasHotReplaced = true;
  4869. primaryType->mNeedsGlobalsPopulated = newType->mNeedsGlobalsPopulated;
  4870. primaryType->mUsingNamespaces = newType->mUsingNamespaces;
  4871. primaryType->mMemberList = newType->mMemberList;
  4872. primaryType->mCompileUnit = newType->mCompileUnit;
  4873. }
  4874. bool DbgModule::CanRead(DataStream* stream, DebuggerResult* outResult)
  4875. {
  4876. PEHeader hdr;
  4877. memset(&hdr, 0, sizeof(hdr));
  4878. PE_NTHeaders ntHdr;
  4879. memset(&ntHdr, 0, sizeof(ntHdr));
  4880. stream->Read(&hdr, sizeof(PEHeader));
  4881. stream->SetPos(hdr.e_lfanew);
  4882. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4883. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4884. {
  4885. *outResult = DebuggerResult_UnknownError;
  4886. return false;
  4887. }
  4888. #ifdef BF_DBG_32
  4889. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4890. {
  4891. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X64)
  4892. *outResult = DebuggerResult_WrongBitSize;
  4893. else
  4894. *outResult = DebuggerResult_UnknownError;
  4895. return false;
  4896. }
  4897. #else
  4898. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4899. {
  4900. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X86)
  4901. *outResult = DebuggerResult_WrongBitSize;
  4902. else
  4903. *outResult = DebuggerResult_UnknownError;
  4904. return false;
  4905. }
  4906. #endif
  4907. return true;
  4908. }
  4909. const char* DbgModule::GetStringTable(DataStream* stream, int stringTablePos)
  4910. {
  4911. if (mStringTable == NULL)
  4912. {
  4913. int prevPos = stream->GetPos();
  4914. stream->SetPos(stringTablePos);
  4915. int strTableSize = 0;
  4916. stream->Read(&strTableSize, 4);
  4917. if (strTableSize != 0)
  4918. {
  4919. strTableSize -= 4;
  4920. char* strTableData = new char[strTableSize + 4];
  4921. memcpy(strTableData, &strTableSize, 4);
  4922. stream->Read(strTableData + 4, strTableSize);
  4923. mStringTable = strTableData;
  4924. }
  4925. stream->SetPos(prevPos);
  4926. }
  4927. return mStringTable;
  4928. }
  4929. bool DbgModule::ReadCOFF(DataStream* stream, DbgModuleKind moduleKind)
  4930. {
  4931. BP_ZONE("DbgModule::ReadCOFF");
  4932. //if (this == mDebugTarget->mTargetBinary)
  4933. //mMemReporter = new MemReporter();
  4934. BfLogDbg("DbgModule::ReadCOFF %s\n", mFilePath.c_str());
  4935. if (mMemReporter != NULL)
  4936. {
  4937. mMemReporter->BeginSection(StrFormat("Module: %s", mFilePath.c_str()));
  4938. mMemReporter->Add(mImageSize);
  4939. }
  4940. defer
  4941. (
  4942. if (mMemReporter != NULL)
  4943. mMemReporter->EndSection();
  4944. );
  4945. DbgModule* mainModule = mDebugTarget->mTargetBinary;
  4946. MiniDumpDebugger* miniDumpDebugger = NULL;
  4947. if (mDebugger->IsMiniDumpDebugger())
  4948. {
  4949. miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  4950. }
  4951. mModuleKind = moduleKind;
  4952. bool isHotSwap = mModuleKind == DbgModuleKind_HotObject;
  4953. bool isObjectFile = mModuleKind != DbgModuleKind_Module;
  4954. auto linkedModule = GetLinkedModule();
  4955. if (isObjectFile)
  4956. linkedModule->PopulateStaticVariableMap();
  4957. mStartTypeIdx = (int)linkedModule->mTypes.size();
  4958. int startSrcFile = (int)mDebugTarget->mSrcFiles.size();
  4959. mStartSubprogramIdx = (int)mSubprograms.size();
  4960. PEHeader hdr;
  4961. memset(&hdr, 0, sizeof(hdr));
  4962. PE_NTHeaders ntHdr;
  4963. memset(&ntHdr, 0, sizeof(ntHdr));
  4964. if (!isObjectFile)
  4965. {
  4966. stream->Read(&hdr, sizeof(PEHeader));
  4967. stream->SetPos(hdr.e_lfanew);
  4968. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4969. mPreferredImageBase = ntHdr.mOptionalHeader.mImageBase;
  4970. if (mImageBase == 0)
  4971. {
  4972. BF_ASSERT(this == mainModule);
  4973. mImageBase = mPreferredImageBase;
  4974. }
  4975. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4976. {
  4977. mLoadState = DbgModuleLoadState_Failed;
  4978. return false;
  4979. }
  4980. #ifdef BF_DBG_32
  4981. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4982. return false;
  4983. #else
  4984. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4985. {
  4986. mLoadState = DbgModuleLoadState_Failed;
  4987. return false;
  4988. }
  4989. #endif
  4990. int pos = hdr.e_lfanew + FIELD_OFFSET(PE_NTHeaders, mOptionalHeader) + ntHdr.mFileHeader.mSizeOfOptionalHeader;
  4991. stream->SetPos(pos);
  4992. }
  4993. else
  4994. {
  4995. stream->Read(&ntHdr.mFileHeader, sizeof(PEFileHeader));
  4996. if (mMemReporter != NULL)
  4997. mMemReporter->Add("PEFileHeader", sizeof(PEFileHeader));
  4998. #ifdef BF_DBG_32
  4999. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  5000. return false;
  5001. #else
  5002. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  5003. {
  5004. mLoadState = DbgModuleLoadState_Failed;
  5005. return false;
  5006. }
  5007. #endif
  5008. }
  5009. int sectionStartPos = stream->GetPos();
  5010. int sectionDataEndPos = 0;
  5011. if (miniDumpDebugger != NULL)
  5012. {
  5013. // Map header
  5014. miniDumpDebugger->MapMemory((addr_target)mImageBase, (uint8*)mMappedImageFile->mData, 0x1000);
  5015. }
  5016. bool wantStringTable = isObjectFile;
  5017. stream->SetPos(sectionStartPos);
  5018. for (int dirNum = 0; dirNum < (int) ntHdr.mFileHeader.mNumberOfSections; dirNum++)
  5019. {
  5020. PESectionHeader sectHdr;
  5021. char* name = sectHdr.mName;
  5022. stream->Read(&sectHdr, sizeof(PESectionHeader));
  5023. if (sectHdr.mSizeOfRawData > 0)
  5024. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRawData + sectHdr.mSizeOfRawData));
  5025. if (sectHdr.mNumberOfRelocations > 0)
  5026. sectionDataEndPos = BF_MAX(sectionDataEndPos, (int)(sectHdr.mPointerToRelocations + sectHdr.mNumberOfRelocations * sizeof(COFFRelocation)));
  5027. if (miniDumpDebugger != NULL)
  5028. {
  5029. miniDumpDebugger->MapMemory((addr_target)(mImageBase + sectHdr.mVirtualAddress), (uint8*)mMappedImageFile->mData + sectHdr.mPointerToRawData, sectHdr.mSizeOfRawData);
  5030. }
  5031. }
  5032. //fseek(fp, sectionDataEndPos + ntHdr.mFileHeader.mNumberOfSymbols * 18, SEEK_SET);
  5033. stream->SetPos(sectionDataEndPos);
  5034. uint8* symbolData = new uint8[ntHdr.mFileHeader.mNumberOfSymbols * 18];
  5035. mAllocSizeData += ntHdr.mFileHeader.mNumberOfSymbols * 18;
  5036. mSymbolData = symbolData;
  5037. stream->Read(symbolData, ntHdr.mFileHeader.mNumberOfSymbols * 18);
  5038. int curPos = stream->GetPos();
  5039. int stringTablePos = curPos;
  5040. if (isObjectFile)
  5041. GetStringTable(stream, stringTablePos);
  5042. int mDebugFrameDataLen = 0;
  5043. stream->SetPos(sectionStartPos);
  5044. PEDataDirectory* exportDataDir = &ntHdr.mOptionalHeader.mDataDirectory[0];
  5045. mHotTargetSections.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5046. Array<PESectionHeader> sectionHeaders;
  5047. sectionHeaders.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5048. mSectionRVAs.Resize(sectionHeaders.size() + 1);
  5049. Array<String> sectionNames;
  5050. sectionNames.Resize(ntHdr.mFileHeader.mNumberOfSections);
  5051. stream->Read(&sectionHeaders[0], sizeof(PESectionHeader) * ntHdr.mFileHeader.mNumberOfSections);
  5052. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5053. {
  5054. mSectionRVAs[sectNum] = sectionHeaders[sectNum].mVirtualAddress;
  5055. }
  5056. int tlsSection = -1;
  5057. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5058. {
  5059. //PEDataDirectory* dataDir = &ntHdr.mOptionalHeader.mDataDirectory[dirNum];
  5060. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5061. //stream->Read(&sectHdr, sizeof(PESectionHeader));
  5062. const char* name = sectHdr.mName;
  5063. if (name[0] == '/')
  5064. {
  5065. int strIdx = atoi(name + 1);
  5066. name = &GetStringTable(stream, stringTablePos)[strIdx];
  5067. }
  5068. sectionNames[sectNum] = name;
  5069. DbgHotTargetSection* targetSection = NULL;
  5070. if (IsObjectFile())
  5071. {
  5072. targetSection = new DbgHotTargetSection();
  5073. targetSection->mDataSize = sectHdr.mSizeOfRawData;
  5074. targetSection->mPointerToRelocations = sectHdr.mPointerToRelocations;
  5075. targetSection->mNumberOfRelocations = sectHdr.mNumberOfRelocations;
  5076. targetSection->mTargetSectionAddr = 0; // TODO: Allocate!
  5077. targetSection->mCanExecute = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5078. targetSection->mCanWrite = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_WRITE) != 0;
  5079. targetSection->mNoTargetAlloc = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0;
  5080. mHotTargetSections[sectNum] = targetSection;
  5081. }
  5082. DbgSection dwSection;
  5083. dwSection.mIsExecutable = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  5084. dwSection.mAddrStart = sectHdr.mVirtualAddress;
  5085. dwSection.mAddrLength = BF_MAX(sectHdr.mSizeOfRawData, sectHdr.mVirtualSize);
  5086. mSections.push_back(dwSection);
  5087. if (sectHdr.mPointerToRawData == 0)
  5088. continue;
  5089. if (strcmp(name, ".tls") == 0)
  5090. mTLSAddr = (addr_target)(sectHdr.mVirtualAddress + mImageBase);
  5091. if ((IsObjectFile()) && (strcmp(name, ".tls$") == 0))
  5092. {
  5093. tlsSection = sectNum;
  5094. mTLSSize = sectHdr.mSizeOfRawData;
  5095. targetSection->mNoTargetAlloc = true;
  5096. }
  5097. bool isExportDataDir = ((exportDataDir->mVirtualAddress != 0) && (exportDataDir->mVirtualAddress >= sectHdr.mVirtualAddress) && (exportDataDir->mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5098. if ((!IsObjectFile()) && (!isExportDataDir))
  5099. {
  5100. if (((strcmp(name, ".text")) == 0) ||
  5101. ((strcmp(name, ".textbss")) == 0) ||
  5102. ((strcmp(name, ".reloc")) == 0)/* ||
  5103. ((strcmp(name, ".data")) == 0)*/)
  5104. {
  5105. // Big unneeded sections
  5106. continue;
  5107. }
  5108. }
  5109. stream->SetPos(sectHdr.mPointerToRawData);
  5110. int dataSize = sectHdr.mSizeOfRawData + 8;
  5111. mAllocSizeData += dataSize;
  5112. uint8* data = new uint8[dataSize];
  5113. {
  5114. BP_ZONE("DbgModule::ReadCOFF_ReadSectionData");
  5115. stream->Read(data, sectHdr.mSizeOfRawData);
  5116. }
  5117. BfLogDbg("Read section data %s %p\n", name, data);
  5118. memset(data + sectHdr.mSizeOfRawData, 0, 8);
  5119. if (IsObjectFile())
  5120. targetSection->mData = data;
  5121. addr_target addrOffset = sectHdr.mVirtualAddress;
  5122. if (isExportDataDir)
  5123. {
  5124. BP_ZONE("DbgModule::ReadCOFF_SymbolMap");
  5125. IMAGE_EXPORT_DIRECTORY* imageExportDir = (IMAGE_EXPORT_DIRECTORY*)(data + (exportDataDir->mVirtualAddress - addrOffset));
  5126. for (int funcIdx = 0; funcIdx < (int)imageExportDir->NumberOfNames; funcIdx++)
  5127. {
  5128. //addr_target strAddr = *(addr_target*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(addr_target));
  5129. int32 strAddr = *(int32*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(int32));
  5130. const char* name = (const char*)(data + (strAddr - addrOffset));
  5131. #ifdef BF_DBG_32
  5132. if (name[0] == '_')
  5133. name++;
  5134. #endif
  5135. int funcOrd = *(uint16*)(data + (imageExportDir->AddressOfNameOrdinals - addrOffset) + funcIdx * sizeof(uint16));
  5136. addr_target funcAddr = *(uint32*)(data + (imageExportDir->AddressOfFunctions - addrOffset) + funcOrd * sizeof(int32));
  5137. int strLen = (int)strlen(name);
  5138. BP_ALLOC("ReadCOFF_SymbolMap", strLen + 1);
  5139. char* allocStr = (char*)mAlloc.AllocBytes(strLen + 1, "ReadCOFF_SymbolMap");
  5140. memcpy(allocStr, name, strLen);
  5141. BP_ALLOC_T(DbgSymbol);
  5142. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5143. dwSymbol->mDbgModule = this;
  5144. dwSymbol->mName = allocStr;
  5145. dwSymbol->mAddress = funcAddr;
  5146. if (strcmp(name, "_tls_index") == 0)
  5147. {
  5148. mTLSIndexAddr = funcAddr;
  5149. }
  5150. //TODO:
  5151. //mDeferredSymbols.PushFront(dwSymbol);
  5152. dwSymbol->mAddress = (addr_target)(dwSymbol->mAddress + mImageBase);
  5153. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5154. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5155. }
  5156. }
  5157. if ((IsObjectFile()) && (sectHdr.mNumberOfRelocations > 0))
  5158. {
  5159. //mDebugger->AllocTargetMemory(sectHdr.mSizeOfRawData, true, true);
  5160. }
  5161. if (strcmp(name, ".text") == 0)
  5162. {
  5163. if (!IsObjectFile())
  5164. mCodeAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5165. }
  5166. //if (strcmp(name, ".rdata") == 0)
  5167. {
  5168. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  5169. if (debugDirEntry.mSize > 0)
  5170. {
  5171. if (mMemReporter != NULL)
  5172. mMemReporter->Add("DataDirectory", debugDirEntry.mSize);
  5173. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5174. {
  5175. int count = debugDirEntry.mSize / sizeof(IMAGE_DEBUG_DIRECTORY);
  5176. for (int dirIdx = 0; dirIdx < count; dirIdx++)
  5177. {
  5178. IMAGE_DEBUG_DIRECTORY* debugDirectory = (IMAGE_DEBUG_DIRECTORY*)(data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress) + dirIdx;
  5179. if (debugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW)
  5180. {
  5181. struct _CodeViewEntry
  5182. {
  5183. public:
  5184. int32 mSig;
  5185. uint8 mGUID[16];
  5186. int32 mAge;
  5187. const char mPDBPath[1];
  5188. };
  5189. if (debugDirectory->AddressOfRawData != 0)
  5190. {
  5191. _CodeViewEntry* codeViewEntry = (_CodeViewEntry*)(data + debugDirectory->AddressOfRawData - sectHdr.mVirtualAddress);
  5192. if (codeViewEntry->mSig == 'SDSR')
  5193. {
  5194. LoadPDB(codeViewEntry->mPDBPath, codeViewEntry->mGUID, codeViewEntry->mAge);
  5195. }
  5196. }
  5197. }
  5198. }
  5199. }
  5200. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5201. }
  5202. }
  5203. //
  5204. {
  5205. PEDataDirectory& tlsDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
  5206. if (tlsDirEntry.mSize > 0)
  5207. {
  5208. if ((tlsDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (tlsDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5209. {
  5210. uint8* relPtr = data + tlsDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5211. uint8* endPtr = relPtr + tlsDirEntry.mSize;
  5212. addr_target tlsDataStart = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5213. addr_target tlsDataEnd = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  5214. mTLSAddr = (addr_target)(tlsDataStart + mImageBase);
  5215. mTLSSize = (int)(tlsDataEnd - tlsDataStart);
  5216. }
  5217. }
  5218. }
  5219. //
  5220. {
  5221. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  5222. if (debugDirEntry.mSize > 0)
  5223. {
  5224. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  5225. {
  5226. uint8* relPtr = data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  5227. uint8* endPtr = relPtr + debugDirEntry.mSize;
  5228. IMAGE_RESOURCE_DIRECTORY* typeDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr);
  5229. // Skip named entries
  5230. for (int typeIdx = 0; typeIdx < typeDir->NumberOfIdEntries; typeIdx++)
  5231. {
  5232. IMAGE_RESOURCE_DIRECTORY_ENTRY* typeEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)typeDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5233. (typeDir->NumberOfNamedEntries + typeIdx)*sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5234. if (typeEntry->Id == 0x10) //VERSION
  5235. {
  5236. IMAGE_RESOURCE_DIRECTORY* idDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (typeEntry->OffsetToData & 0x7FFFFFFF));
  5237. if (idDir->NumberOfIdEntries < 1)
  5238. break;
  5239. IMAGE_RESOURCE_DIRECTORY_ENTRY* idEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)idDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5240. (idDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5241. IMAGE_RESOURCE_DIRECTORY* langDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (idEntry->OffsetToData & 0x7FFFFFFF));
  5242. if (langDir->NumberOfIdEntries < 1)
  5243. break;
  5244. IMAGE_RESOURCE_DIRECTORY_ENTRY* langEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)langDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  5245. (langDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  5246. IMAGE_RESOURCE_DATA_ENTRY* dataEntry = (IMAGE_RESOURCE_DATA_ENTRY*)(relPtr + (langEntry->OffsetToData & 0x7FFFFFFF));
  5247. uint8* versionData = data + dataEntry->OffsetToData - sectHdr.mVirtualAddress;
  5248. uint8* vPtr = versionData;
  5249. auto vSize = GET_FROM(vPtr, uint16);
  5250. auto verEnd = vPtr + vSize;
  5251. auto vLength = GET_FROM(vPtr, uint16);
  5252. vPtr += 36; // "VS_VERSION_INFO"
  5253. auto fixedFileInfo = GET_FROM(vPtr, VS_FIXEDFILEINFO);
  5254. auto _GetString = [&]()
  5255. {
  5256. wchar_t* cPtr = (wchar_t*)vPtr;
  5257. int len = (int)wcslen(cPtr);
  5258. vPtr += (len + 1) * 2;
  5259. if (((intptr)vPtr & 3) != 0)
  5260. vPtr += 2;
  5261. UTF16String str16(cPtr, len);
  5262. return UTF8Encode(str16);
  5263. };
  5264. while (vPtr < verEnd)
  5265. {
  5266. auto size = GET_FROM(vPtr, uint16);
  5267. auto childEnd = vPtr + size;
  5268. auto valueLength = GET_FROM(vPtr, uint16);
  5269. auto type = GET_FROM(vPtr, uint16);
  5270. String infoType = _GetString();
  5271. if (infoType == "StringFileInfo")
  5272. {
  5273. while (vPtr < childEnd)
  5274. {
  5275. auto strsSize = GET_FROM(vPtr, uint16);
  5276. auto strsEnd = vPtr + strsSize;
  5277. auto strsLength = GET_FROM(vPtr, uint16);
  5278. auto strsType = GET_FROM(vPtr, uint16);
  5279. String hexNum = _GetString();
  5280. while (vPtr < strsEnd)
  5281. {
  5282. auto strSize = GET_FROM(vPtr, uint16);
  5283. auto strEnd = vPtr + strSize;
  5284. auto strLength = GET_FROM(vPtr, uint16);
  5285. auto strType = GET_FROM(vPtr, uint16);
  5286. String key = _GetString();
  5287. String value = _GetString();
  5288. if (key == "FileVersion")
  5289. mVersion = value;
  5290. }
  5291. }
  5292. }
  5293. vPtr = childEnd;
  5294. }
  5295. }
  5296. }
  5297. }
  5298. //stream->SetPos(debugDirEntry.mVirtualAddress);
  5299. }
  5300. }
  5301. bool usedData = true;
  5302. /*if (isUnwindSection)
  5303. {
  5304. mExceptionData = data;
  5305. mExceptionDataRVA = sectHdr.mVirtualAddress;
  5306. }*/
  5307. if (strcmp(name, ".pdata") == 0)
  5308. {
  5309. DbgSectionData entry;
  5310. entry.mData = data;
  5311. entry.mSize = sectHdr.mSizeOfRawData;
  5312. mExceptionDirectory.Add(entry);
  5313. }
  5314. // Old, unsupported DWARF debug info
  5315. /*
  5316. else if (strcmp(name, ".debug_info") == 0)
  5317. {
  5318. mDebugInfoData = data;
  5319. }
  5320. else if (strcmp(name, ".debug_line") == 0)
  5321. {
  5322. mDebugLineData = data;
  5323. }
  5324. else if (strcmp(name, ".debug_str") == 0)
  5325. {
  5326. mDebugStrData = data;
  5327. }
  5328. else if (strcmp(name, ".debug_frame") == 0)
  5329. {
  5330. mDebugFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5331. mDebugFrameData = data;
  5332. mDebugFrameDataLen = sectHdr.mSizeOfRawData;
  5333. }
  5334. else if (strcmp(name, ".eh_frame") == 0)
  5335. {
  5336. mEHFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  5337. mEHFrameData = data;
  5338. }
  5339. else if (strcmp(name, ".debug_abbrev") == 0)
  5340. {
  5341. mDebugAbbrevData = data;
  5342. mDebugAbbrevPtrData = new const uint8*[sectHdr.mSizeOfRawData];
  5343. }
  5344. else if (strcmp(name, ".debug_loc") == 0)
  5345. {
  5346. mDebugLocationData = data;
  5347. }
  5348. else if (strcmp(name, ".debug_ranges") == 0)
  5349. {
  5350. mDebugRangesData = data;
  5351. }
  5352. */
  5353. // else if (strcmp(name, ".rsrc") == 0)
  5354. // {
  5355. // //IMAGE_DIRECTORY_ENTRY_RESOURCE
  5356. // }
  5357. else if (CheckSection(name, data, sectHdr.mSizeOfRawData))
  5358. {
  5359. // Was used
  5360. }
  5361. else
  5362. {
  5363. /*if (isUnwindSection)
  5364. mOwnsExceptionData = true;
  5365. else*/
  5366. usedData = false;
  5367. }
  5368. if (!usedData)
  5369. {
  5370. if (IsObjectFile())
  5371. {
  5372. mOwnedSectionData.push_back(data);
  5373. }
  5374. else
  5375. {
  5376. mAllocSizeData -= dataSize;
  5377. delete [] data;
  5378. }
  5379. }
  5380. //stream->SetPos(prevPos);
  5381. }
  5382. int needHotTargetMemory = 0;
  5383. if (isObjectFile)
  5384. {
  5385. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5386. {
  5387. auto targetSection = mHotTargetSections[sectNum];
  5388. if (!targetSection->mNoTargetAlloc)
  5389. needHotTargetMemory += (targetSection->mDataSize + (mDebugger->mPageSize - 1)) & ~(mDebugger->mPageSize - 1);
  5390. }
  5391. mDebugger->ReserveHotTargetMemory(needHotTargetMemory);
  5392. // '0' address is temporary
  5393. //mOrigImageData = new DbgModuleMemoryCache(0, NULL, needHotTargetMemory, true);
  5394. mOrigImageData = new DbgModuleMemoryCache(0, needHotTargetMemory);
  5395. }
  5396. int numSections = ntHdr.mFileHeader.mNumberOfSections;
  5397. if (isObjectFile)
  5398. {
  5399. addr_target* resolvedSymbolAddrs = new addr_target[ntHdr.mFileHeader.mNumberOfSymbols];
  5400. memset(resolvedSymbolAddrs, 0, ntHdr.mFileHeader.mNumberOfSymbols * sizeof(addr_target));
  5401. ParseHotTargetSections(stream, resolvedSymbolAddrs);
  5402. delete [] resolvedSymbolAddrs;
  5403. }
  5404. ProcessDebugInfo();
  5405. if (mDebugInfoData != NULL)
  5406. {
  5407. mDbgFlavor = DbgFlavor_GNU;
  5408. mMasterCompileUnit = new DbgCompileUnit(this);
  5409. mMasterCompileUnit->mDbgModule = this;
  5410. mMasterCompileUnit->mIsMaster = true;
  5411. const uint8* data = mDebugInfoData;
  5412. while (ParseDWARF(data)) {}
  5413. CreateNamespaces();
  5414. // Must be added last so module reference indices still map correctly
  5415. mCompileUnits.push_back(mMasterCompileUnit);
  5416. }
  5417. ParseDebugFrameData();
  5418. ParseEHFrameData();
  5419. mEndTypeIdx = (int)linkedModule->mTypes.size();
  5420. mEndSubprogramIdx = (int)mSubprograms.size();
  5421. if (mDebugLineData != NULL)
  5422. {
  5423. const uint8* data = mDebugLineData;
  5424. for (int compileUnitIdx = 0; true; compileUnitIdx++)
  5425. if (!ParseDebugLineInfo(data, compileUnitIdx))
  5426. break;
  5427. }
  5428. {
  5429. BP_ZONE("ReadPE_ReadSymbols");
  5430. //linkedModule->mSymbolNameMap.reserve(linkedModule->mSymbolNameMap.size() + ntHdr.mFileHeader.mNumberOfSymbols * 2);
  5431. bool tlsFailed = false;
  5432. addr_target tlsMappingAddr = 0;
  5433. for (int symNum = 0; symNum < (int)ntHdr.mFileHeader.mNumberOfSymbols; symNum++)
  5434. {
  5435. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[symNum * 18];
  5436. char* name = symInfo->mName;
  5437. if (symInfo->mNameOfs[0] != 0)
  5438. {
  5439. if (name[7] != 0)
  5440. {
  5441. // Name is exactly 8 chars, not null terminated yet
  5442. name = (char*)mAlloc.AllocBytes(9, "PESymbol");
  5443. memcpy(name, symInfo->mName, 8);
  5444. name[8] = 0;
  5445. }
  5446. }
  5447. else
  5448. name = (char*)GetStringTable(stream, stringTablePos) + symInfo->mNameOfs[1];
  5449. if ((symInfo->mStorageClass == COFF_SYM_CLASS_EXTERNAL) ||
  5450. (symInfo->mStorageClass == COFF_SYM_CLASS_STATIC))
  5451. {
  5452. // 'static' in the C sense.
  5453. // It means local to the compile unit, so may have multiple copies of the same symbol name.
  5454. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  5455. if (symInfo->mSectionNum == 0xFFFF)
  5456. continue;
  5457. if (symInfo->mSectionNum > 0)
  5458. {
  5459. bool isTLS = false;
  5460. addr_target targetAddr = 0;
  5461. if (isObjectFile)
  5462. {
  5463. if (symInfo->mSectionNum - 1 == tlsSection)
  5464. {
  5465. isTLS = true;
  5466. }
  5467. else
  5468. {
  5469. auto hotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  5470. if (hotTargetSection != NULL)
  5471. targetAddr = GetHotTargetAddress(hotTargetSection) + symInfo->mValue;
  5472. }
  5473. }
  5474. else
  5475. targetAddr = mSectionRVAs[symInfo->mSectionNum - 1] + symInfo->mValue;
  5476. if (((targetAddr != 0) || (isTLS)) &&
  5477. (name[0] != '.'))
  5478. {
  5479. const char* symbolName = name;
  5480. #ifdef BF_DBG_32
  5481. if (symbolName[0] == '_')
  5482. symbolName++;
  5483. #endif
  5484. if (strcmp(symbolName, "_tls_index") == 0)
  5485. {
  5486. mTLSIndexAddr = (addr_target)(targetAddr + mImageBase);
  5487. }
  5488. if ((isStaticSymbol) && (IsHotSwapPreserve(symbolName)))
  5489. isStaticSymbol = false;
  5490. if ((isObjectFile) && (!isStaticSymbol))
  5491. {
  5492. DbgSymbol* dwSymbol = NULL;
  5493. linkedModule->ParseSymbolData() ;
  5494. BP_ALLOC_T(DbgSymbol);
  5495. dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5496. dwSymbol->mDbgModule = this;
  5497. dwSymbol->mName = symbolName;
  5498. dwSymbol->mAddress = targetAddr;
  5499. if (dwSymbol != NULL)
  5500. {
  5501. bool isHotSwapPreserve = IsHotSwapPreserve(dwSymbol->mName);
  5502. bool insertIntoNameMap = true;
  5503. bool oldFound = false;
  5504. auto nameMapEntry = linkedModule->mSymbolNameMap.Find(dwSymbol->mName);
  5505. if (nameMapEntry != NULL)
  5506. {
  5507. oldFound = true;
  5508. if (!isHotSwapPreserve)
  5509. {
  5510. nameMapEntry->mValue = dwSymbol;
  5511. }
  5512. else if (mDbgFlavor == DbgFlavor_MS)
  5513. {
  5514. // Store in our own map - this is needed for storing address of the new vdata
  5515. // so the new values can be copied in
  5516. mSymbolNameMap.Insert(dwSymbol);
  5517. }
  5518. }
  5519. else
  5520. {
  5521. if (isTLS)
  5522. {
  5523. if (mainModule->mTLSExtraAddr == 0)
  5524. {
  5525. auto extraSym = mainModule->mSymbolNameMap.Find("__BFTLS_EXTRA");
  5526. if (extraSym != NULL)
  5527. {
  5528. mainModule->ParseGlobalsData();
  5529. auto itr = mainModule->mStaticVariableMap.find("__BFTLS_EXTRA");
  5530. if (itr != mainModule->mStaticVariableMap.end())
  5531. {
  5532. auto staticVar = itr->second;
  5533. mainModule->mTLSExtraAddr = extraSym->mValue->mAddress;
  5534. mainModule->mTLSExtraSize = (int)staticVar->mType->GetByteCount();
  5535. }
  5536. }
  5537. }
  5538. if ((mainModule->mTLSExtraAddr != 0) && (tlsMappingAddr == 0))
  5539. {
  5540. // Take a chunk out of __BFTLS_EXTRA
  5541. if (mTLSSize <= mainModule->mTLSExtraSize)
  5542. {
  5543. tlsMappingAddr = mainModule->mTLSExtraAddr;
  5544. mainModule->mTLSExtraAddr += mTLSSize;
  5545. mainModule->mTLSExtraSize -= mTLSSize;
  5546. }
  5547. }
  5548. if (tlsMappingAddr != 0)
  5549. {
  5550. BF_ASSERT(symInfo->mValue < mTLSSize);
  5551. dwSymbol->mAddress = tlsMappingAddr + symInfo->mValue;
  5552. }
  5553. if (dwSymbol->mAddress == 0)
  5554. {
  5555. if (!tlsFailed)
  5556. {
  5557. Fail(StrFormat("Hot swapping failed to allocate TLS address for '%s'. Program restart required.", name));
  5558. }
  5559. dwSymbol->mAddress = (addr_target)0xCDCDCDCD;
  5560. tlsFailed = true;
  5561. }
  5562. }
  5563. }
  5564. if (dwSymbol->mAddress != 0)
  5565. {
  5566. if (!oldFound)
  5567. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5568. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5569. }
  5570. }
  5571. }
  5572. else
  5573. {
  5574. //TODO: We don't need to defer symbols anymore... we can just do a Fixup on their addr
  5575. //mDeferredSymbols.PushFront(dwSymbol);
  5576. BP_ALLOC_T(DbgSymbol);
  5577. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5578. dwSymbol->mDbgModule = this;
  5579. dwSymbol->mName = symbolName;
  5580. dwSymbol->mAddress = targetAddr;
  5581. if (!IsObjectFile())
  5582. dwSymbol->mAddress += (addr_target)mImageBase;
  5583. if (IsObjectFile())
  5584. BF_ASSERT((dwSymbol->mAddress >= mImageBase) && (dwSymbol->mAddress < mImageBase + mImageSize));
  5585. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5586. if (!isStaticSymbol)
  5587. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5588. }
  5589. }
  5590. }
  5591. }
  5592. if (symInfo->mStorageClass == COFF_SYM_CLASS_FILE)
  5593. {
  5594. const char* fileName = (const char*)&mSymbolData[(symNum + 1) * 18];
  5595. }
  5596. symNum += symInfo->mNumOfAuxSymbols;
  5597. }
  5598. }
  5599. int subProgramSizes = 0;
  5600. for (int subProgramIdx = mStartSubprogramIdx; subProgramIdx < mEndSubprogramIdx; subProgramIdx++)
  5601. {
  5602. auto dwSubprogram = mSubprograms[subProgramIdx];
  5603. subProgramSizes += (int)(dwSubprogram->mBlock.mHighPC - dwSubprogram->mBlock.mLowPC);
  5604. /*for (int i = 0; i < dwSubprogram->mLineDataArray.mSize; i++)
  5605. {
  5606. auto lineData = dwSubprogram->mLineDataArray.mData[i];
  5607. auto srcFile = lineData->mSrcFileRef->mSrcFile;
  5608. srcFile->mLineData.push_back(lineData);
  5609. srcFile->mHadLineData = true;
  5610. if ((srcFile->mFirstLineDataDbgModule == NULL) || (srcFile->mFirstLineDataDbgModule == this))
  5611. srcFile->mFirstLineDataDbgModule = this;
  5612. else
  5613. srcFile->mHasLineDataFromMultipleModules = true;
  5614. }*/
  5615. }
  5616. // Delete srcFiles without line data
  5617. int lineDataCount = 0;
  5618. /*for (int srcFileIdx = startSrcFile; srcFileIdx < (int)mDebugTarget->mSrcFiles.size(); srcFileIdx++)
  5619. {
  5620. if (!mDebugTarget->mSrcFiles[srcFileIdx]->mHadLineData)
  5621. {
  5622. mEmptySrcFiles.push_back(mDebugTarget->mSrcFiles[srcFileIdx]);
  5623. mDebugTarget->mSrcFiles.erase(mDebugTarget->mSrcFiles.begin() + srcFileIdx);
  5624. }
  5625. else
  5626. lineDataCount += (int)mDebugTarget->mSrcFiles[srcFileIdx]->mLineData.size();
  5627. }*/
  5628. auto srcFilesItr = mDebugTarget->mSrcFiles.begin();
  5629. while (srcFilesItr != mDebugTarget->mSrcFiles.end())
  5630. {
  5631. DbgSrcFile* srcFile = srcFilesItr->mValue;
  5632. if ((!srcFile->mHadLineData) && (srcFile->mLocalPath.IsEmpty()))
  5633. {
  5634. mEmptySrcFiles.push_back(srcFile);
  5635. srcFilesItr = mDebugTarget->mSrcFiles.Remove(srcFilesItr);
  5636. }
  5637. else
  5638. {
  5639. ++srcFilesItr;
  5640. }
  5641. }
  5642. if (!isObjectFile)
  5643. {
  5644. mImageSize = ntHdr.mOptionalHeader.mSizeOfImage;
  5645. mEntryPoint = ntHdr.mOptionalHeader.mAddressOfEntryPoint;
  5646. }
  5647. /*OutputDebugStrF("%s:\n CompileUnits:%d DebugLines: %d Types: %d (%d in map) SubPrograms: %d (%dk) AllocSize:%dk\n", mFilePath.c_str(), mCompileUnits.size(),
  5648. lineDataCount, mEndTypeIdx - mStartTypeIdx, (int)linkedModule->mTypes.size() - mStartTypeIdx, mEndSubprogramIdx - mStartSubprogramIdx, subProgramSizes / 1024, mAlloc.GetAllocSize() / 1024);*/
  5649. if (isHotSwap)
  5650. {
  5651. // In COFF, we don't necessarily add an actual primary type during MapCompileUnitMethods, so this fixes that
  5652. while (true)
  5653. {
  5654. bool didReplaceType = false;
  5655. for (auto itr = mHotPrimaryTypes.begin(); itr != mHotPrimaryTypes.end(); ++itr)
  5656. {
  5657. auto dbgType = *itr;
  5658. auto primaryType = dbgType->GetPrimaryType();
  5659. if (primaryType != dbgType)
  5660. {
  5661. mHotPrimaryTypes.Remove(itr);
  5662. mHotPrimaryTypes.Add(primaryType);
  5663. didReplaceType = true;
  5664. break;
  5665. }
  5666. }
  5667. if (!didReplaceType)
  5668. break;
  5669. }
  5670. BF_ASSERT(mTypes.size() == 0);
  5671. for (int typeIdx = mStartTypeIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
  5672. {
  5673. DbgType* newType = linkedModule->mTypes[typeIdx];
  5674. //if (!newType->mMethodList.IsEmpty())
  5675. if (!newType->mIsDeclaration)
  5676. HotReplaceType(newType);
  5677. }
  5678. }
  5679. if (needHotTargetMemory != 0)
  5680. {
  5681. BF_ASSERT(needHotTargetMemory >= (int)mImageSize);
  5682. }
  5683. //BF_ASSERT(mEndTypeIdx == (int)linkedModule->mTypes.size());
  5684. //BF_ASSERT(mEndSubprogramIdx == (int)mSubprograms.size());
  5685. ParseExceptionData();
  5686. mLoadState = DbgModuleLoadState_Loaded;
  5687. if (mMemReporter != NULL)
  5688. {
  5689. mMemReporter->BeginSection("Sections");
  5690. ParseSymbolData();
  5691. Array<DbgSymbol*> orderedSyms;
  5692. for (auto sym : mSymbolNameMap)
  5693. {
  5694. auto dbgSym = sym->mValue;
  5695. orderedSyms.Add(dbgSym);
  5696. }
  5697. orderedSyms.Sort([](DbgSymbol* lhs, DbgSymbol* rhs) { return lhs->mAddress < rhs->mAddress; });
  5698. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5699. {
  5700. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5701. mMemReporter->BeginSection(sectionNames[sectNum]);
  5702. DbgSymbol* lastSym = NULL;
  5703. for (auto dbgSym : orderedSyms)
  5704. {
  5705. if (dbgSym->mAddress < mImageBase + sectHdr.mVirtualAddress)
  5706. continue;
  5707. if (dbgSym->mAddress >= mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData)
  5708. break;
  5709. if (lastSym != NULL)
  5710. {
  5711. mMemReporter->Add(lastSym->mName, (int)(dbgSym->mAddress - lastSym->mAddress));
  5712. }
  5713. else
  5714. {
  5715. int startingOffset = (int)(dbgSym->mAddress - (mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5716. if (startingOffset > 0)
  5717. mMemReporter->Add("<StartData>", startingOffset);
  5718. }
  5719. lastSym = dbgSym;
  5720. }
  5721. if (lastSym != NULL)
  5722. mMemReporter->Add(lastSym->mName, (int)((mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData) - lastSym->mAddress));
  5723. else
  5724. {
  5725. mMemReporter->Add("<Unaccounted>", (int)(sectHdr.mSizeOfRawData));
  5726. }
  5727. mMemReporter->EndSection();
  5728. }
  5729. mMemReporter->EndSection();
  5730. mMemReporter->mShowInKB = false;
  5731. mMemReporter->Report();
  5732. }
  5733. return true;
  5734. }
  5735. void DbgModule::FinishHotSwap()
  5736. {
  5737. BF_ASSERT(IsObjectFile());
  5738. auto linkedModule = GetLinkedModule();
  5739. auto mainModule = mDebugTarget->mTargetBinary;
  5740. HashSet<String> failSet;
  5741. String findName;
  5742. for (auto deferredHotResolve : mDeferredHotResolveList)
  5743. {
  5744. addr_target resolveTargetAddr = deferredHotResolve->mNewAddr;
  5745. findName = deferredHotResolve->mName;
  5746. if (mDbgFlavor == DbgFlavor_MS)
  5747. {
  5748. // ... why do we need to find these variables in the variable map instead of the symbol name map?
  5749. }
  5750. auto itr = mainModule->mStaticVariableMap.find(findName.c_str());
  5751. if (itr != mainModule->mStaticVariableMap.end())
  5752. {
  5753. DbgVariable* variable = itr->second;
  5754. resolveTargetAddr = mDebugTarget->GetStaticAddress(variable);
  5755. }
  5756. else
  5757. {
  5758. auto symbolEntry = mainModule->mSymbolNameMap.Find(findName.c_str());
  5759. if (symbolEntry != NULL)
  5760. {
  5761. resolveTargetAddr = symbolEntry->mValue->mAddress;
  5762. }
  5763. else
  5764. {
  5765. if (deferredHotResolve->mName == "__ImageBase")
  5766. {
  5767. resolveTargetAddr = (addr_target)mainModule->mImageBase;
  5768. }
  5769. else
  5770. {
  5771. resolveTargetAddr = mainModule->LocateSymbol(deferredHotResolve->mName);
  5772. if (resolveTargetAddr == 0)
  5773. {
  5774. failSet.Add(deferredHotResolve->mName);
  5775. continue;
  5776. }
  5777. }
  5778. }
  5779. }
  5780. DoReloc(deferredHotResolve->mHotTargetSection, deferredHotResolve->mReloc, resolveTargetAddr, NULL);
  5781. }
  5782. mDeferredHotResolveList.Clear();
  5783. if (!failSet.IsEmpty())
  5784. {
  5785. bool handled = false;
  5786. if (!mDebugger->mDebugManager->mOutMessages.empty())
  5787. {
  5788. auto& str = mDebugger->mDebugManager->mOutMessages.back();
  5789. if (str.Contains("failed to resolve"))
  5790. {
  5791. for (auto& sym : failSet)
  5792. {
  5793. str += ", ";
  5794. str += sym;
  5795. }
  5796. handled = true;
  5797. }
  5798. }
  5799. if (!handled)
  5800. {
  5801. int symIdx = 0;
  5802. String str;
  5803. if (failSet.size() == 1)
  5804. str = "Hot swapping failed to resolve symbol: ";
  5805. else
  5806. str = "Hot swapping failed to resolve symbols: ";
  5807. for (auto& sym : failSet)
  5808. {
  5809. if (symIdx != 0)
  5810. str += ", ";
  5811. str += sym;
  5812. symIdx++;
  5813. }
  5814. mDebugger->Fail(str);
  5815. }
  5816. }
  5817. CommitHotTargetSections();
  5818. // 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)
  5819. // before we can finalize the class vdata.
  5820. ProcessHotSwapVariables();
  5821. for (auto hotTargetSection : mHotTargetSections)
  5822. delete hotTargetSection;
  5823. mHotTargetSections.Clear();
  5824. mSymbolNameMap.Clear();
  5825. }
  5826. addr_target DbgModule::ExecuteOps(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, CPURegisters* registers, DbgAddrType* outAddrType, DbgEvalLocFlags flags, addr_target* pushValue)
  5827. {
  5828. bool allowReg = (flags & DbgEvalLocFlag_IsParam) == 0;
  5829. const uint8* locDataEnd = locData + locDataLen;
  5830. int regNum = -1;
  5831. addr_target stackFrameData[256];
  5832. int stackIdx = 0;
  5833. if (pushValue != NULL)
  5834. stackFrameData[stackIdx++] = *pushValue;
  5835. while (locData < locDataEnd)
  5836. {
  5837. uint8 opCode = GET_FROM(locData, uint8);
  5838. switch (opCode)
  5839. {
  5840. case DW_OP_piece:
  5841. {
  5842. if (*outAddrType == DbgAddrType_Register)
  5843. *outAddrType = DbgAddrType_Value;
  5844. addr_target val = stackFrameData[--stackIdx];
  5845. int pieceSize = (int)DecodeULEB128(locData);
  5846. if (pieceSize == 4)
  5847. val &= 0xFFFFFFFF;
  5848. else if (pieceSize == 2)
  5849. val &= 0xFFFF;
  5850. else if (pieceSize == 1)
  5851. val &= 0xFF;
  5852. stackFrameData[stackIdx++] = val;
  5853. }
  5854. break;
  5855. case DW_OP_consts:
  5856. {
  5857. int64 val = DecodeSLEB128(locData);
  5858. stackFrameData[stackIdx++] = (addr_target)val;
  5859. }
  5860. break;
  5861. case DW_OP_stack_value:
  5862. {
  5863. *outAddrType = DbgAddrType_Value;
  5864. }
  5865. break;
  5866. case DW_OP_addr_noRemap:
  5867. {
  5868. addr_target addr = GET_FROM(locData, addr_target);
  5869. stackFrameData[stackIdx++] = addr;
  5870. //*outIsAddr = true;
  5871. *outAddrType = DbgAddrType_Target;
  5872. }
  5873. break;
  5874. case DW_OP_addr:
  5875. {
  5876. addr_target addr = GET_FROM(locData, addr_target);
  5877. //if (dwarf != NULL)
  5878. addr = RemapAddr(addr);
  5879. stackFrameData[stackIdx++] = addr;
  5880. //*outIsAddr = true;
  5881. *outAddrType = DbgAddrType_Target;
  5882. }
  5883. break;
  5884. case DW_OP_deref:
  5885. {
  5886. addr_target addr = stackFrameData[--stackIdx];
  5887. addr_target value = mDebugger->ReadMemory<addr_target>(addr);
  5888. stackFrameData[stackIdx++] = value;
  5889. }
  5890. break;
  5891. case DW_OP_fbreg:
  5892. {
  5893. if (registers == NULL)
  5894. return 0;
  5895. BF_ASSERT(dwSubprogram != NULL);
  5896. DbgSubprogram* nonInlinedSubProgram = dwSubprogram->GetRootInlineParent();
  5897. if (nonInlinedSubProgram->mFrameBaseData == NULL)
  5898. {
  5899. *outAddrType = DbgAddrType_Target; //TODO: why?
  5900. return 0;
  5901. }
  5902. BF_ASSERT(nonInlinedSubProgram->mFrameBaseData != NULL);
  5903. intptr loc = EvaluateLocation(nonInlinedSubProgram, nonInlinedSubProgram->mFrameBaseData, nonInlinedSubProgram->mFrameBaseLen, stackFrame, outAddrType, DbgEvalLocFlag_DisallowReg);
  5904. int64 offset = DecodeSLEB128(locData);
  5905. loc += offset;
  5906. //loc = BfDebuggerReadMemory(loc);
  5907. //*outIsAddr = true;
  5908. *outAddrType = DbgAddrType_Target;
  5909. stackFrameData[stackIdx++] = (addr_target)loc;
  5910. }
  5911. break;
  5912. case DW_OP_reg0:
  5913. case DW_OP_reg1:
  5914. case DW_OP_reg2:
  5915. case DW_OP_reg3:
  5916. case DW_OP_reg4:
  5917. case DW_OP_reg5:
  5918. case DW_OP_reg6:
  5919. case DW_OP_reg7:
  5920. case DW_OP_reg8:
  5921. case DW_OP_reg9:
  5922. case DW_OP_reg10:
  5923. case DW_OP_reg11:
  5924. case DW_OP_reg12:
  5925. case DW_OP_reg13:
  5926. case DW_OP_reg14:
  5927. case DW_OP_reg15:
  5928. if (registers == NULL)
  5929. return 0;
  5930. BF_ASSERT((opCode - DW_OP_reg0) < CPURegisters::kNumIntRegs);
  5931. regNum = opCode - DW_OP_reg0;
  5932. stackFrameData[stackIdx++] = registers->mIntRegsArray[regNum];
  5933. *outAddrType = DbgAddrType_Register;
  5934. break;
  5935. case DW_OP_reg21: //XMM0
  5936. BF_FATAL("XMM registers not supported yet");
  5937. break;
  5938. case DW_OP_breg0:
  5939. case DW_OP_breg1:
  5940. case DW_OP_breg2:
  5941. case DW_OP_breg3:
  5942. case DW_OP_breg4:
  5943. case DW_OP_breg5:
  5944. case DW_OP_breg6:
  5945. case DW_OP_breg7:
  5946. case DW_OP_breg8:
  5947. case DW_OP_breg9:
  5948. case DW_OP_breg10:
  5949. case DW_OP_breg11:
  5950. case DW_OP_breg12:
  5951. case DW_OP_breg13:
  5952. case DW_OP_breg14:
  5953. case DW_OP_breg15:
  5954. {
  5955. if (registers == NULL)
  5956. return 0;
  5957. int64 offset = DecodeSLEB128(locData);
  5958. BF_ASSERT((opCode - DW_OP_breg0) < CPURegisters::kNumIntRegs);
  5959. auto loc = registers->mIntRegsArray[opCode - DW_OP_breg0] + offset;
  5960. //loc = BfDebuggerReadMemory(loc);
  5961. //*outIsAddr = true;
  5962. *outAddrType = DbgAddrType_Target;
  5963. stackFrameData[stackIdx++] = (addr_target)loc;
  5964. }
  5965. break;
  5966. case DW_OP_bregx:
  5967. {
  5968. if (registers == NULL)
  5969. return 0;
  5970. int regNum = (int)DecodeULEB128(locData);
  5971. int64 offset = DecodeSLEB128(locData);
  5972. BF_ASSERT(regNum < CPURegisters::kNumIntRegs);
  5973. auto loc = registers->mIntRegsArray[regNum] + offset;
  5974. //loc = BfDebuggerReadMemory(loc);
  5975. //*outIsAddr = true;
  5976. *outAddrType = DbgAddrType_Target;
  5977. stackFrameData[stackIdx++] = (addr_target)loc;
  5978. }
  5979. break;
  5980. case DW_OP_const4u:
  5981. {
  5982. uint32 val = GET_FROM(locData, uint32);
  5983. stackFrameData[stackIdx++] = val;
  5984. }
  5985. break;
  5986. case DW_OP_const8u:
  5987. {
  5988. uint64 val = GET_FROM(locData, uint64);
  5989. stackFrameData[stackIdx++] = (addr_target)val;
  5990. }
  5991. break;
  5992. case DW_OP_GNU_push_tls_address:
  5993. {
  5994. if ((mTLSAddr == 0) || (mTLSIndexAddr == 0))
  5995. return 0;
  5996. int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
  5997. addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
  5998. intptr_target tlsValueIndex = stackFrameData[--stackIdx];
  5999. stackFrameData[stackIdx++] = (tlsValueIndex - mTLSAddr) + tlsEntry;
  6000. *outAddrType = DbgAddrType_Target;
  6001. }
  6002. break;
  6003. case DW_OP_nop:
  6004. break;
  6005. default:
  6006. BF_FATAL("Unknown DW_OP");
  6007. break;
  6008. }
  6009. }
  6010. if (*outAddrType == DbgAddrType_Register)
  6011. {
  6012. if (allowReg)
  6013. return regNum;
  6014. *outAddrType = DbgAddrType_Value;
  6015. }
  6016. //BF_ASSERT(stackIdx == 1);
  6017. return stackFrameData[--stackIdx];
  6018. }
  6019. intptr DbgModule::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, DbgEvalLocFlags flags)
  6020. {
  6021. BP_ZONE("DebugTarget::EvaluateLocation");
  6022. auto dbgModule = this;
  6023. if (locDataLen == DbgLocationLenKind_SegPlusOffset)
  6024. {
  6025. BF_ASSERT(dbgModule->mDbgFlavor == DbgFlavor_MS);
  6026. if (dbgModule->mDbgFlavor == DbgFlavor_MS)
  6027. {
  6028. COFF* coff = (COFF*)dbgModule;
  6029. struct SegOfsData
  6030. {
  6031. uint32 mOfs;
  6032. uint16 mSeg;
  6033. };
  6034. SegOfsData* segOfsData = (SegOfsData*)locData;
  6035. *outAddrType = DbgAddrType_Target;
  6036. return coff->GetSectionAddr(segOfsData->mSeg, segOfsData->mOfs);
  6037. }
  6038. else
  6039. {
  6040. *outAddrType = DbgAddrType_Target;
  6041. return 0;
  6042. }
  6043. }
  6044. CPURegisters* registers = NULL;
  6045. if (stackFrame != NULL)
  6046. registers = &stackFrame->mRegisters;
  6047. if (locDataLen < 0)
  6048. {
  6049. if (registers == NULL)
  6050. return 0;
  6051. int64 ipAddr = stackFrame->GetSourcePC();
  6052. const uint8* checkLocData = locData;
  6053. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6054. int64 endLoc = startLoc + GET_FROM(checkLocData, uint16);
  6055. BF_ASSERT(dwSubprogram != NULL);
  6056. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6057. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6058. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6059. {
  6060. locDataLen = -locDataLen - sizeof(addr_target) - sizeof(uint16);
  6061. locData = checkLocData;
  6062. }
  6063. else
  6064. {
  6065. *outAddrType = DbgAddrType_OptimizedOut;
  6066. return 0;
  6067. }
  6068. }
  6069. else if (locDataLen == 0)
  6070. {
  6071. if (registers == NULL)
  6072. return 0;
  6073. int64 ipAddr = stackFrame->GetSourcePC();
  6074. const uint8* checkLocData = locData;
  6075. while (true)
  6076. {
  6077. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  6078. int64 endLoc = (int64)GET_FROM(checkLocData, addr_target);
  6079. if ((startLoc == 0) && (endLoc == 0))
  6080. {
  6081. *outAddrType = DbgAddrType_OptimizedOut;
  6082. return 0;
  6083. }
  6084. BF_ASSERT(dwSubprogram != NULL);
  6085. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  6086. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  6087. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  6088. {
  6089. locDataLen = GET_FROM(checkLocData, int16);
  6090. locData = checkLocData;
  6091. break;
  6092. }
  6093. else
  6094. {
  6095. int len = GET_FROM(checkLocData, int16);;
  6096. checkLocData += len;
  6097. }
  6098. }
  6099. }
  6100. return ExecuteOps(dwSubprogram, locData, locDataLen, stackFrame, registers, outAddrType, flags);
  6101. }
  6102. void DbgModule::ProcessHotSwapVariables()
  6103. {
  6104. BP_ZONE("DbgModule::ProcessHotSwapVariables");
  6105. auto linkedModule = GetLinkedModule();
  6106. for (auto staticVariable : mStaticVariables)
  6107. {
  6108. bool replaceVariable = false;
  6109. const char* findName = staticVariable->GetMappedName();
  6110. auto itr = linkedModule->mStaticVariableMap.find(findName);
  6111. if (itr != linkedModule->mStaticVariableMap.end())
  6112. {
  6113. DbgVariable* oldVariable = itr->second;
  6114. // If the old static field has the same type as the new static field then we keep the same
  6115. // address, otherwise we use the new (zeroed-out) allocated space
  6116. auto _GetNewAddress = [&]()
  6117. {
  6118. addr_target newAddress = 0;
  6119. if (mDbgFlavor == DbgFlavor_GNU)
  6120. {
  6121. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6122. }
  6123. else
  6124. {
  6125. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6126. // the location in our own mSymbolNameMap
  6127. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6128. if (entry != NULL)
  6129. newAddress = entry->mValue->mAddress;
  6130. }
  6131. return newAddress;
  6132. };
  6133. if (oldVariable->mType->IsSizedArray())
  6134. {
  6135. mDebugTarget->GetCompilerSettings();
  6136. bool doMerge = strstr(oldVariable->mName, "sBfClassVData") != NULL;
  6137. bool keepInPlace = (doMerge) && (strstr(oldVariable->mName, ".vext") == NULL);
  6138. if (doMerge)
  6139. {
  6140. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6141. addr_target newAddress = _GetNewAddress();
  6142. if (newAddress == 0)
  6143. continue;
  6144. uint8* newData = GetHotTargetData(newAddress);
  6145. int newArraySize = (int)staticVariable->mType->GetByteCount();
  6146. int oldArraySize = (int)oldVariable->mType->GetByteCount();
  6147. int copySize = std::min(newArraySize, oldArraySize);
  6148. BF_ASSERT((oldArraySize & (sizeof(addr_target) - 1)) == 0);
  6149. DbgModule* defModule = oldVariable->mType->mCompileUnit->mDbgModule;
  6150. defModule->EnableWriting(oldAddress);
  6151. uint8* mergedData = new uint8[copySize];
  6152. mDebugger->ReadMemory(oldAddress, copySize, mergedData);
  6153. // The new vtable may have 0's in it when virtual methods are removed. Keep the old virtual addresses in those.
  6154. addr_target* newDataPtr = (addr_target*)newData;
  6155. addr_target* mergedPtr = (addr_target*)mergedData;
  6156. while (mergedPtr < (addr_target*)(mergedData + copySize))
  6157. {
  6158. if (*newDataPtr != 0)
  6159. *mergedPtr = *newDataPtr;
  6160. mergedPtr++;
  6161. newDataPtr++;
  6162. }
  6163. bool success;
  6164. success = mDebugger->WriteMemory(oldAddress, mergedData, copySize);
  6165. BF_ASSERT(success);
  6166. memcpy(newData, mergedData, copySize);
  6167. delete mergedData;
  6168. }
  6169. else if (strstr(oldVariable->mName, "sStringLiterals") != NULL)
  6170. {
  6171. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6172. addr_target newAddress = NULL;
  6173. if (mDbgFlavor == DbgFlavor_GNU)
  6174. {
  6175. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  6176. }
  6177. else
  6178. {
  6179. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  6180. // the location in our own mSymbolNameMap
  6181. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  6182. if (entry == NULL)
  6183. continue;
  6184. newAddress = entry->mValue->mAddress;
  6185. }
  6186. // Make sure newAddress doesn't have anything linked to it
  6187. addr_target val = 0;
  6188. bool success = mDebugger->ReadMemory((intptr)newAddress, sizeof(addr_target), &val);
  6189. BF_ASSERT(success);
  6190. BF_ASSERT(val == 0);
  6191. // Link the new table to the old extended table
  6192. addr_target prevLinkage = 0;
  6193. success = mDebugger->ReadMemory((intptr)oldAddress, sizeof(addr_target), &prevLinkage);
  6194. BF_ASSERT(success);
  6195. success = mDebugger->WriteMemory((intptr)newAddress, &prevLinkage, sizeof(addr_target));
  6196. BF_ASSERT(success);
  6197. mDebugger->EnableWriting((intptr)oldAddress, sizeof(addr_target));
  6198. success = mDebugger->WriteMemory((intptr)oldAddress, &newAddress, sizeof(addr_target));
  6199. BF_ASSERT(success);
  6200. keepInPlace = true;
  6201. }
  6202. if (keepInPlace)
  6203. {
  6204. // We have to maintain the OLD size because we can't overwrite the original bounds
  6205. staticVariable->mType = oldVariable->mType;
  6206. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6207. staticVariable->mLocationData = oldVariable->mLocationData;
  6208. staticVariable->mCompileUnit = oldVariable->mCompileUnit;
  6209. }
  6210. }
  6211. else if (oldVariable->mType->Equals(staticVariable->mType))
  6212. {
  6213. if (oldVariable->mType->IsStruct())
  6214. {
  6215. if ((strncmp(oldVariable->mName, "?sBfTypeData@", 13) == 0) || (strncmp(oldVariable->mName, "sBfTypeData.", 12) == 0))
  6216. {
  6217. int size = (int)staticVariable->mType->GetByteCount();
  6218. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6219. addr_target newAddress = _GetNewAddress();
  6220. if (newAddress == 0)
  6221. continue;
  6222. uint8* data = new uint8[size];
  6223. bool success = mDebugger->ReadMemory(newAddress, size, data);
  6224. if (success)
  6225. {
  6226. mDebugger->EnableWriting((intptr)oldAddress, size);
  6227. success = mDebugger->WriteMemory(oldAddress, data, size);
  6228. }
  6229. delete data;
  6230. BF_ASSERT(success);
  6231. staticVariable->mLocationLen = oldVariable->mLocationLen;
  6232. staticVariable->mLocationData = oldVariable->mLocationData;
  6233. }
  6234. }
  6235. //staticVariable->mLocationLen = oldVariable->mLocationLen;
  6236. //staticVariable->mLocationData = oldVariable->mLocationData;
  6237. replaceVariable = false;
  6238. }
  6239. else
  6240. {
  6241. BF_ASSERT(!oldVariable->mType->IsSizedArray());
  6242. }
  6243. if (!replaceVariable)
  6244. {
  6245. auto symbolVal = linkedModule->mSymbolNameMap.Find(staticVariable->GetMappedName());
  6246. if (symbolVal != NULL)
  6247. {
  6248. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  6249. DbgSymbol* oldSymbol = mDebugTarget->mSymbolMap.Get(oldAddress);
  6250. if (oldSymbol != NULL)
  6251. symbolVal->mValue = oldSymbol;
  6252. }
  6253. }
  6254. }
  6255. else // Not found - new variable
  6256. replaceVariable = true;
  6257. if (replaceVariable)
  6258. {
  6259. linkedModule->mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  6260. }
  6261. }
  6262. }
  6263. int64 DbgModule::GetImageSize()
  6264. {
  6265. return mImageSize;
  6266. }
  6267. /*const uint8* DbgModule::GetOrigImageData(addr_target address)
  6268. {
  6269. return mOrigImageData + (address - mImageBase);
  6270. }*/
  6271. DbgFileExistKind DbgModule::CheckSourceFileExist(const StringImpl& path)
  6272. {
  6273. DbgFileExistKind existsKind = DbgFileExistKind_NotFound;
  6274. if (path.StartsWith("$Emit"))
  6275. return DbgFileExistKind_Found;
  6276. if (FileExists(path))
  6277. existsKind = DbgFileExistKind_Found;
  6278. String oldSourceCommand = GetOldSourceCommand(path);
  6279. if (!oldSourceCommand.IsEmpty())
  6280. {
  6281. int crPos = (int)oldSourceCommand.IndexOf('\n');
  6282. if (crPos != -1)
  6283. {
  6284. String targetPath = oldSourceCommand.Substring(0, crPos);
  6285. if (FileExists(targetPath))
  6286. existsKind = DbgFileExistKind_Found;
  6287. else
  6288. existsKind = DbgFileExistKind_HasOldSourceCommand;
  6289. }
  6290. }
  6291. return existsKind;
  6292. }
  6293. void DbgModule::EnableWriting(addr_target address)
  6294. {
  6295. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6296. {
  6297. DbgSection* section = &mSections[sectionIdx];
  6298. if ((address >= mImageBase + section->mAddrStart) && (address < mImageBase + section->mAddrStart + section->mAddrLength))
  6299. {
  6300. if (!section->mWritingEnabled)
  6301. {
  6302. section->mOldProt = mDebugger->EnableWriting(mImageBase + section->mAddrStart, (int32)section->mAddrLength);
  6303. section->mWritingEnabled = true;
  6304. }
  6305. }
  6306. }
  6307. }
  6308. void DbgModule::RevertWritingEnable()
  6309. {
  6310. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  6311. {
  6312. DbgSection* section = &mSections[sectionIdx];
  6313. if (section->mWritingEnabled)
  6314. {
  6315. mDebugger->SetProtection(mImageBase + section->mAddrStart, (int32)section->mAddrLength, section->mOldProt);
  6316. section->mWritingEnabled = false;
  6317. }
  6318. }
  6319. }
  6320. template <typename TRadixMap>
  6321. static void RemoveInvalidRange(TRadixMap& radixMap, addr_target startAddr, int addrLength)
  6322. {
  6323. radixMap.RemoveRange(startAddr, addrLength);
  6324. }
  6325. template <typename TMap>
  6326. static void RemoveInvalidMapRange(TMap& map, addr_target startAddr, int addrLength)
  6327. {
  6328. auto itr = map.lower_bound(startAddr);
  6329. while (itr != map.end())
  6330. {
  6331. auto val = itr->first;
  6332. if (val >= startAddr + addrLength)
  6333. return;
  6334. itr = map.erase(itr);
  6335. }
  6336. }
  6337. void DbgModule::RemoveTargetData()
  6338. {
  6339. BP_ZONE("DbgModule::RemoveTargetData");
  6340. for (auto srcFileRef : mSrcFileDeferredRefs)
  6341. srcFileRef->RemoveDeferredRefs(this);
  6342. HashSet<DbgSrcFile*> visitedFiles;
  6343. for (auto compileUnit : mCompileUnits)
  6344. {
  6345. for (auto& fileRef : compileUnit->mSrcFileRefs)
  6346. {
  6347. if (visitedFiles.Add(fileRef.mSrcFile))
  6348. {
  6349. fileRef.mSrcFile->RemoveLines(this);
  6350. }
  6351. }
  6352. }
  6353. RemoveInvalidRange(mDebugTarget->mSymbolMap, (addr_target)mImageBase, (int32)mImageSize);
  6354. RemoveInvalidRange(mDebugTarget->mSubprogramMap, (addr_target)mImageBase, (int32)mImageSize);
  6355. RemoveInvalidRange(mDebugTarget->mExceptionDirectoryMap, (addr_target)mImageBase, (int32)mImageSize);
  6356. RemoveInvalidRange(mDebugTarget->mContribMap, (addr_target)mImageBase, (int32)mImageSize);
  6357. RemoveInvalidMapRange(mDebugTarget->mDwFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6358. RemoveInvalidMapRange(mDebugTarget->mCOFFFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  6359. //mDebugTarget->mDwFrameDescriptorMap.erase()
  6360. // Remove any of our entries from the mHotReplacedMethodList from 'primary modules' that are not going away
  6361. for (auto dbgType : mHotPrimaryTypes)
  6362. {
  6363. DbgSubprogram** nextSrc = &dbgType->mHotReplacedMethodList.mHead;
  6364. while (*nextSrc != NULL)
  6365. {
  6366. auto* subprogram = *nextSrc;
  6367. if (subprogram->mCompileUnit->mDbgModule == this)
  6368. *nextSrc = subprogram->mNext;
  6369. else
  6370. nextSrc = &(*nextSrc)->mNext;;
  6371. }
  6372. }
  6373. }
  6374. void DbgModule::ReportMemory(MemReporter* memReporter)
  6375. {
  6376. //memReporter->Add("BumpAlloc_Used", mAlloc.GetAllocSize());
  6377. //memReporter->Add("BumpAlloc_Unused", mAlloc.GetTotalAllocSize() - mAlloc.GetAllocSize());
  6378. memReporter->AddBumpAlloc("BumpAlloc", mAlloc);
  6379. memReporter->AddVec(mTypes);
  6380. memReporter->AddVec(mSubprograms);
  6381. //memReporter->Add("TypeMap", mTypeMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6382. memReporter->AddHashSet("TypeMap", mTypeMap.mMap);
  6383. memReporter->Add("SymbolNameMap", mSymbolNameMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  6384. if (mOrigImageData != NULL)
  6385. {
  6386. memReporter->BeginSection("OrigImageData");
  6387. mOrigImageData->ReportMemory(memReporter);
  6388. memReporter->EndSection();
  6389. }
  6390. }
  6391. DbgType* DbgModule::GetPointerType(DbgType* innerType)
  6392. {
  6393. auto linkedModule = GetLinkedModule();
  6394. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6395. if (innerType->mPtrType == NULL)
  6396. {
  6397. BP_ALLOC_T(DbgType);
  6398. auto ptrType = mAlloc.Alloc<DbgType>();
  6399. ptrType->mCompileUnit = innerType->mCompileUnit;
  6400. ptrType->mLanguage = innerType->mLanguage;
  6401. ptrType->mTypeCode = DbgType_Ptr;
  6402. ptrType->mTypeParam = innerType;
  6403. ptrType->mSize = sizeof(addr_target);
  6404. ptrType->mAlign = (int)ptrType->mSize;
  6405. ptrType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6406. linkedModule->mTypes.push_back(ptrType);
  6407. innerType->mPtrType = ptrType;
  6408. }
  6409. return innerType->mPtrType;
  6410. }
  6411. DbgType* DbgModule::GetConstType(DbgType* innerType)
  6412. {
  6413. auto linkedModule = GetLinkedModule();
  6414. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6415. /*auto itr = linkedModule->mConstTypes.find(innerType);
  6416. if (itr != linkedModule->mConstTypes.end())
  6417. return itr->second;*/
  6418. DbgType* constType = NULL;
  6419. if (linkedModule->mConstTypes.TryGetValue(innerType, &constType))
  6420. return constType;
  6421. BP_ALLOC_T(DbgType);
  6422. constType = mAlloc.Alloc<DbgType>();
  6423. constType->mCompileUnit = innerType->mCompileUnit;
  6424. constType->mLanguage = innerType->mLanguage;
  6425. constType->mTypeCode = DbgType_Const;
  6426. constType->mTypeParam = innerType;
  6427. constType->mSize = sizeof(addr_target);
  6428. constType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6429. linkedModule->mTypes.push_back(constType);
  6430. linkedModule->mConstTypes[innerType] = constType;
  6431. return constType;
  6432. }
  6433. DbgType* DbgModule::GetPrimaryType(DbgType* dbgType)
  6434. {
  6435. if (dbgType->mPriority <= DbgTypePriority_Normal)
  6436. {
  6437. if ((dbgType->mLanguage == DbgLanguage_Beef) && (dbgType->mName != NULL))
  6438. {
  6439. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6440. if (newTypeEntry != NULL)
  6441. {
  6442. DbgType* newType = newTypeEntry->mValue;
  6443. if ((newType->mTypeCode == DbgType_Ptr) && (newType->IsBfObjectPtr()))
  6444. newType = newType->mTypeParam;
  6445. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6446. return newType;
  6447. }
  6448. }
  6449. else if (dbgType->mName != NULL)
  6450. {
  6451. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6452. if (newTypeEntry != NULL)
  6453. {
  6454. DbgType* newType = newTypeEntry->mValue;
  6455. newType = newType->RemoveModifiers();
  6456. if (newType != dbgType)
  6457. newType = GetPrimaryType(newType);
  6458. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6459. return newType;
  6460. }
  6461. }
  6462. }
  6463. return dbgType;
  6464. }
  6465. DbgType* DbgModule::GetInnerTypeOrVoid(DbgType* dbgType)
  6466. {
  6467. if (dbgType->mTypeParam != NULL)
  6468. return dbgType->mTypeParam;
  6469. return GetPrimitiveType(DbgType_Void, dbgType->mLanguage);
  6470. }
  6471. DbgType* DbgModule::FindTypeHelper(const String& typeName, DbgType* checkType)
  6472. {
  6473. for (auto subType : checkType->mSubTypeList)
  6474. {
  6475. if (strcmp(subType->mTypeName, typeName.c_str()) == 0)
  6476. return subType;
  6477. }
  6478. for (auto baseType : checkType->mBaseTypes)
  6479. {
  6480. auto retType = FindTypeHelper(typeName, baseType->mBaseType);
  6481. if (retType != NULL)
  6482. return retType;
  6483. }
  6484. return NULL;
  6485. }
  6486. DbgType* DbgModule::FindType(const String& typeName, DbgType* contextType, DbgLanguage language, bool bfObjectPtr)
  6487. {
  6488. if ((language == DbgLanguage_Unknown) && (contextType != NULL))
  6489. language = contextType->mLanguage;
  6490. if (typeName.length() > 0)
  6491. {
  6492. if (typeName[typeName.length() - 1] == '*')
  6493. {
  6494. DbgType* dbgType = FindType(typeName.Substring(0, typeName.length() - 1), contextType, language, bfObjectPtr);
  6495. if (dbgType == NULL)
  6496. return NULL;
  6497. return GetPointerType(dbgType);
  6498. }
  6499. }
  6500. auto entry = GetLinkedModule()->mTypeMap.Find(typeName.c_str(), language);
  6501. if (entry != NULL)
  6502. {
  6503. if ((bfObjectPtr) && (entry->mValue->IsBfObject()))
  6504. return GetPointerType(entry->mValue);
  6505. return entry->mValue;
  6506. }
  6507. if (contextType != NULL)
  6508. {
  6509. DbgType* checkType = contextType;
  6510. if (checkType->IsPointer())
  6511. checkType = checkType->mTypeParam;
  6512. return FindTypeHelper(typeName, checkType);
  6513. }
  6514. return NULL;
  6515. }
  6516. DbgTypeMap::Entry* DbgModule::FindType(const char* typeName, DbgLanguage language)
  6517. {
  6518. return GetLinkedModule()->mTypeMap.Find(typeName, language);
  6519. /*auto& typeMap = GetLinkedModule()->mTypeMap;
  6520. auto dbgTypeEntry = typeMap.Find(typeName);
  6521. if (dbgTypeEntry == NULL)
  6522. return NULL;
  6523. if (dbgTypeEntry->mValue->mLanguage == language)
  6524. return dbgTypeEntry;
  6525. while (dbgTypeEntry != NULL)
  6526. {
  6527. DbgType* dbgType = dbgTypeEntry->mValue;
  6528. if ((dbgType->GetLanguage() == language) && (typeMap.StrEqual(dbgType->mName, typeName)))
  6529. return dbgTypeEntry;
  6530. dbgTypeEntry = dbgTypeEntry->mNext;
  6531. }*/
  6532. //return NULL;
  6533. }
  6534. DbgType* DbgModule::GetPrimitiveType(DbgTypeCode typeCode, DbgLanguage language)
  6535. {
  6536. if (language == DbgLanguage_Beef)
  6537. return mBfPrimitiveTypes[(int)typeCode];
  6538. else
  6539. return mCPrimitiveTypes[(int)typeCode];
  6540. }
  6541. DbgType* DbgModule::GetPrimitiveStructType(DbgTypeCode typeCode)
  6542. {
  6543. const char* name = mPrimitiveStructNames[typeCode];
  6544. if (name == NULL)
  6545. return NULL;
  6546. return FindType(name, NULL, DbgLanguage_Beef);
  6547. }
  6548. DbgType* DbgModule::GetSizedArrayType(DbgType * elementType, int count)
  6549. {
  6550. auto linkedModule = GetLinkedModule();
  6551. if ((linkedModule != NULL) && (linkedModule != this))
  6552. {
  6553. return linkedModule->GetSizedArrayType(elementType, count);
  6554. }
  6555. DbgType** sizedArrayTypePtr;
  6556. DbgSizedArrayEntry entry;
  6557. entry.mElementType = elementType;
  6558. entry.mCount = count;
  6559. if (mSizedArrayTypes.TryAdd(entry, NULL, &sizedArrayTypePtr))
  6560. {
  6561. BP_ALLOC_T(DbgType);
  6562. auto sizedArrayType = mAlloc.Alloc<DbgType>();
  6563. sizedArrayType->mCompileUnit = elementType->mCompileUnit;
  6564. sizedArrayType->mLanguage = elementType->mLanguage;
  6565. sizedArrayType->mTypeCode = DbgType_SizedArray;
  6566. sizedArrayType->mTypeParam = elementType;
  6567. sizedArrayType->mSize = count * elementType->GetStride();
  6568. sizedArrayType->mAlign = elementType->GetAlign();
  6569. sizedArrayType->mSizeCalculated = true;
  6570. sizedArrayType->mTypeIdx = (int32)mTypes.size();
  6571. linkedModule->mTypes.push_back(sizedArrayType);
  6572. *sizedArrayTypePtr = sizedArrayType;
  6573. }
  6574. return *sizedArrayTypePtr;
  6575. }