DbgModule.cpp 202 KB

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