DbgModule.cpp 199 KB

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