DbgModule.cpp 198 KB

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