12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028 |
- #pragma warning(push)
- #pragma warning(disable:4146)
- #pragma warning(disable:4996)
- #pragma warning(disable:4800)
- #pragma warning(disable:4244)
- #include "DbgModule.h"
- #include "DWARFInfo.h"
- #include <windows.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <string>
- #include <inttypes.h>
- #include <assert.h>
- #include <vector>
- #include "WinDebugger.h"
- #include "DebugManager.h"
- #include "DebugTarget.h"
- #include "COFFData.h"
- #include "Compiler/BfDemangler.h"
- #include "BeefySysLib/util/Hash.h"
- #include "BeefySysLib/util/BeefPerf.h"
- #include "DbgSymSrv.h"
- #include "MiniDumpDebugger.h"
- #pragma warning(pop)
- #pragma warning(disable:4996)
- #include "BeefySysLib/util/AllocDebug.h"
- USING_NS_BF_DBG;
- void SetBreakpoint(int64_t address);
- NS_BF_DBG_BEGIN
- #ifdef BF_DBG_32
- typedef PEOptionalHeader32 PEOptionalHeader;
- typedef PE_NTHeaders32 PE_NTHeaders;
- #else
- typedef PEOptionalHeader64 PEOptionalHeader;
- typedef PE_NTHeaders64 PE_NTHeaders;
- #endif
- #define GET(T) *((T*)(data += sizeof(T)) - 1)
- #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
- //////////////////////////////////////////////////////////////////////////
- DbgCompileUnit::DbgCompileUnit(DbgModule* dbgModule)
- {
- mDbgModule = dbgModule;
- mLanguage = DbgLanguage_Unknown;
- mGlobalBlock = mDbgModule->mAlloc.Alloc<DbgBlock>();
- mGlobalType = mDbgModule->mAlloc.Alloc<DbgType>();
- mGlobalType->mTypeCode = DbgType_Root;
- mGlobalType->mPriority = DbgTypePriority_Primary_Explicit;
- mGlobalType->mCompileUnit = this;
- mLowPC = (addr_target)-1;
- mHighPC = 0;
- //mDoPrimaryRemapping = true;
- mNeedsLineDataFixup = true;
- mWasHotReplaced = false;
- mIsMaster = false;
- }
- //////////////////////////////////////////////////////////////////////////
- addr_target DbgLineDataEx::GetAddress()
- {
- return mSubprogram->GetLineAddr(*mLineData);
- }
- DbgSrcFile* DbgLineDataEx::GetSrcFile()
- {
- auto inlineRoot = mSubprogram->GetRootInlineParent();
- return inlineRoot->mLineInfo->mContexts[mLineData->mCtxIdx].mSrcFile;
- }
- addr_target DbgSubprogram::GetLineAddr(const DbgLineData& lineData)
- {
- return (addr_target)(lineData.mRelAddress + mCompileUnit->mDbgModule->mImageBase);
- }
- DbgSubprogram* DbgSubprogram::GetLineInlinee(const DbgLineData& lineData)
- {
- auto inlineRoot = GetRootInlineParent();
- return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mInlinee;
- }
- DbgSrcFile* DbgSubprogram::GetLineSrcFile(const DbgLineData& lineData)
- {
- auto inlineRoot = GetRootInlineParent();
- return inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx].mSrcFile;
- }
- void DbgSubprogram::PopulateSubprogram()
- {
- if (mDeferredInternalsSize == 0)
- return;
- mCompileUnit->mDbgModule->PopulateSubprogram(this);
- }
- //////////////////////////////////////////////////////////////////////////
- DbgLineDataBuilder::DbgLineDataBuilder(DbgModule* dbgModule)
- {
- mDbgModule = dbgModule;
- mCurSubprogram = NULL;
- mCurRecord = NULL;
- }
- DbgLineData* DbgLineDataBuilder::Add(DbgCompileUnit* compileUnit, DbgLineData& lineData, DbgSrcFile* srcFile, DbgSubprogram* inlinee)
- {
- addr_target address = (addr_target)(lineData.mRelAddress + mDbgModule->mImageBase);
- if ((compileUnit->mLowPC != (addr_target)-1) && ((address < (addr_target)compileUnit->mLowPC) || (address >= (addr_target)compileUnit->mHighPC)))
- return NULL;
-
- if ((mCurSubprogram == NULL) || (address < mCurSubprogram->mBlock.mLowPC) || (address >= mCurSubprogram->mBlock.mHighPC))
- {
- DbgSubprogramMapEntry* mapEntry = mDbgModule->mDebugTarget->mSubprogramMap.Get(address, DBG_MAX_LOOKBACK);
- if (mapEntry != NULL)
- {
- mCurSubprogram = mapEntry->mEntry;
-
- if (address > mCurSubprogram->mBlock.mHighPC)
- mCurSubprogram = NULL;
- if (mCurSubprogram != NULL)
- {
- SubprogramRecord** recordPtr = NULL;
- if (mRecords.TryAdd(mCurSubprogram, NULL, &recordPtr))
- {
- // It's not too expensive to over-reserve here, because these are just temporary structures that get copied
- // exactly sized when we Commit
- mCurRecord = mAlloc.Alloc<SubprogramRecord>();
- *recordPtr = mCurRecord;
- mCurRecord->mContexts.mAlloc = &mAlloc;
- mCurRecord->mContexts.Reserve(16);
- mCurRecord->mLines.mAlloc = &mAlloc;
- mCurRecord->mLines.Reserve(128);
- mCurRecord->mCurContext = -1;
- mCurRecord->mHasInlinees = false;
- }
- else
- mCurRecord = *recordPtr;
- }
- else
- mCurRecord = NULL;
- }
- }
- if (mCurSubprogram == NULL)
- return NULL;
-
- bool needsNewCtx = false;
- if (mCurRecord->mCurContext == -1)
- {
- needsNewCtx = true;
- }
- else
- {
- auto& curContext = mCurRecord->mContexts[mCurRecord->mCurContext];
- if ((curContext.mInlinee != inlinee) || (curContext.mSrcFile != srcFile))
- {
- needsNewCtx = true;
- for (int ctxIdx = 0; ctxIdx < (int)mCurRecord->mContexts.size(); ctxIdx++)
- {
- auto& ctx = mCurRecord->mContexts[ctxIdx];
- if ((ctx.mInlinee == inlinee) && (ctx.mSrcFile == srcFile))
- {
- needsNewCtx = false;
- mCurRecord->mCurContext = ctxIdx;
- break;
- }
- }
- }
- }
-
- if (needsNewCtx)
- {
- DbgLineInfoCtx ctx;
- ctx.mInlinee = inlinee;
- ctx.mSrcFile = srcFile;
- if (inlinee != NULL)
- mCurRecord->mHasInlinees = true;
- mCurRecord->mContexts.Add(ctx);
- mCurRecord->mCurContext = (int)mCurRecord->mContexts.size() - 1;
- }
- lineData.mCtxIdx = mCurRecord->mCurContext;
- if ((mCurSubprogram->mPrologueSize > 0) && (mCurRecord->mLines.size() == 1) && (inlinee == NULL))
- {
- auto& firstLine = mCurRecord->mLines[0];
- auto dbgStartAddr = firstLine.mRelAddress + mCurSubprogram->mPrologueSize;
- if (lineData.mRelAddress != dbgStartAddr)
- {
- DbgLineData dbgStartLine = firstLine;
- dbgStartLine.mRelAddress = dbgStartAddr;
- mCurRecord->mLines.Add(dbgStartLine);
- }
- firstLine.mColumn = -2; // Marker for 'in prologue'
- }
- if (inlinee != NULL)
- {
- if (inlinee->mInlineeInfo->mFirstLineData.mRelAddress == 0)
- inlinee->mInlineeInfo->mFirstLineData = lineData;
- inlinee->mInlineeInfo->mLastLineData = lineData;
- }
- mCurRecord->mLines.Add(lineData);
- return &mCurRecord->mLines.back();
- }
- void DbgLineDataBuilder::Commit()
- {
- HashSet<DbgSrcFile*> usedSrcFiles;
-
- for (auto& recordKV : mRecords)
- {
- auto dbgSubprogram = recordKV.mKey;
- auto record = recordKV.mValue;
- usedSrcFiles.Clear();
- for (auto& ctx : record->mContexts)
- {
- if (usedSrcFiles.Add(ctx.mSrcFile))
- {
- ctx.mSrcFile->mLineDataRefs.Add(dbgSubprogram);
- }
- }
-
- for (int lineIdx = 0; lineIdx < (int)record->mLines.size() - 1; lineIdx++)
- {
- auto& lineData = record->mLines[lineIdx];
- auto& nextLineData = record->mLines[lineIdx + 1];
- if ((lineData.mContribSize == 0) && (lineData.mCtxIdx == nextLineData.mCtxIdx))
- {
- lineData.mContribSize = (uint32)(nextLineData.mRelAddress - lineData.mRelAddress);
- }
- bool sameInliner = lineData.mCtxIdx == nextLineData.mCtxIdx;
- if (!sameInliner)
- {
- auto ctx = record->mContexts[lineData.mCtxIdx];
- auto nextCtx = record->mContexts[lineData.mCtxIdx];
- sameInliner = ctx.mInlinee == nextCtx.mInlinee;
- }
- if ((sameInliner) && (lineData.mRelAddress + lineData.mContribSize < nextLineData.mRelAddress))
- {
- auto ctx = record->mContexts[lineData.mCtxIdx];
- if (ctx.mInlinee != NULL)
- ctx.mInlinee->mHasLineAddrGaps = true;
- }
- }
- DbgLineData* lastLine = NULL;
- for (int lineIdx = 0; lineIdx < (int)record->mLines.size(); lineIdx++)
- {
- auto& lineData = record->mLines[lineIdx];
- if (lineData.mContribSize == 0)
- {
- auto ctx = record->mContexts[lineData.mCtxIdx];
- if (ctx.mInlinee == NULL)
- lastLine = &lineData;
- }
- }
- if (lastLine != NULL)
- lastLine->mContribSize = (uint32)(dbgSubprogram->mBlock.mHighPC - (mDbgModule->mImageBase + lastLine->mRelAddress));
- BF_ASSERT(dbgSubprogram->mLineInfo == NULL);
- dbgSubprogram->mLineInfo = mDbgModule->mAlloc.Alloc<DbgLineInfo>();
- dbgSubprogram->mLineInfo->mLines.CopyFrom(&record->mLines[0], (int)record->mLines.size(), mDbgModule->mAlloc);
- BfSizedArray<DbgLineInfoCtx> contexts;
- contexts.CopyFrom(&record->mContexts[0], (int)record->mContexts.size(), mDbgModule->mAlloc);
- dbgSubprogram->mLineInfo->mContexts = contexts.mVals;
- dbgSubprogram->mLineInfo->mHasInlinees = record->mHasInlinees;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- static const char* DataGetString(const uint8*& data)
- {
- const char* prevVal = (const char*)data;
- while (*data != 0)
- data++;
- data++;
- return prevVal;
- }
- struct AbstractOriginEntry
- {
- public:
- int mClassType;
- DbgDebugData* mDestination;
- DbgDebugData* mAbstractOrigin;
- private:
- AbstractOriginEntry()
- {
- }
- public:
- static AbstractOriginEntry Create(int classType, DbgDebugData* destination, DbgDebugData* abstractOrigin)
- {
- AbstractOriginEntry abstractOriginEntry;
- abstractOriginEntry.mClassType = classType;
- abstractOriginEntry.mDestination = destination;
- abstractOriginEntry.mAbstractOrigin = abstractOrigin;
- return abstractOriginEntry;
- }
- void Replace()
- {
- if (mClassType == DbgSubprogram::ClassType)
- {
- DbgSubprogram* destSubprogram = (DbgSubprogram*)mDestination;
- DbgSubprogram* originSubprogram = (DbgSubprogram*)mAbstractOrigin;
- if (destSubprogram->mName == NULL)
- {
- destSubprogram->mName = originSubprogram->mName;
- destSubprogram->mParentType = originSubprogram->mParentType;
- }
- destSubprogram->mHasThis = originSubprogram->mHasThis;
- if (destSubprogram->mFrameBaseData == NULL)
- {
- destSubprogram->mFrameBaseData = originSubprogram->mFrameBaseData;
- destSubprogram->mFrameBaseLen = originSubprogram->mFrameBaseLen;
- }
- destSubprogram->mReturnType = originSubprogram->mReturnType;
- auto originItr = originSubprogram->mParams.begin();
- for (auto destParam : destSubprogram->mParams)
- {
- DbgVariable* originParam = *originItr;
- if (originParam != NULL)
- {
- if (destParam->mName == NULL)
- destParam->mName = originParam->mName;
- if (destParam->mType == NULL)
- destParam->mType = originParam->mType;
- }
- ++originItr;
- }
- //BF_ASSERT(originItr == originSubprogram->mParams.end());
- }
- else if (mClassType == DbgVariable::ClassType)
- {
- DbgVariable* destVariable = (DbgVariable*)mDestination;
- DbgVariable* originVariable = (DbgVariable*)mAbstractOrigin;
- if (destVariable->mName == NULL)
- destVariable->mName = originVariable->mName;
- if (destVariable->mType == NULL)
- destVariable->mType = originVariable->mType;
- }
- else
- {
- BF_FATAL("Unhandled");
- }
- }
- };
- NS_BF_DBG_END
- //////////////////////////////////////////////////////////////////////////
- String DbgSubprogram::ToString()
- {
- if ((mInlineeInfo != NULL) && (mInlineeInfo->mInlineeId != 0))
- mCompileUnit->mDbgModule->FixupInlinee(this);
- PopulateSubprogram();
- String str;
- auto language = GetLanguage();
- if (mName == NULL)
- {
- if (mLinkName[0] == '<')
- return mLinkName;
- str = BfDemangler::Demangle(mLinkName, language);
- // Strip off the params since we need to generate those ourselves
- int parenPos = (int)str.IndexOf('(');
- if (parenPos != -1)
- str = str.Substring(0, parenPos);
- }
- else if (mHasQualifiedName)
- {
- const char* cPtr = mName;
- if (strncmp(cPtr, "_bf::", 5) == 0)
- {
- cPtr += 5;
- for ( ; true; cPtr++)
- {
- char c = *cPtr;
- if (c == 0)
- break;
- if ((c == '_') && (cPtr[-1] == ':'))
- {
- if (strcmp(cPtr, "__BfCtor") == 0)
- {
- str += "this";
- break;
- }
- if (strcmp(cPtr, "__BfStaticCtor") == 0)
- {
- str += "this$static";
- break;
- }
- if (strcmp(cPtr, "__BfCtorClear") == 0)
- {
- str += "this$clear";
- break;
- }
- }
- if ((c == ':') && (cPtr[1] == ':'))
- {
- str.Append('.');
- cPtr++;
- }
- else
- str.Append(c);
- }
- }
- else
- str += mName;
- }
- else
- {
- if (mParentType != NULL)
- {
- str += mParentType->ToString();
- if (!str.empty())
- {
- if (language == DbgLanguage_Beef)
- str += ".";
- else
- str += "::";
- }
- }
- if ((language == DbgLanguage_Beef) && (mParentType != NULL) && (mParentType->mTypeName != NULL) && (strcmp(mName, mParentType->mTypeName) == 0))
- str += "this";
- else if ((language == DbgLanguage_Beef) && (mName[0] == '~'))
- str += "~this";
- else if (strncmp(mName, "_bf::", 5) == 0)
- str += mName + 5;
- else
- {
- bool handled = false;
- if ((language == DbgLanguage_Beef) && (mName[0] == '_'))
- {
- if (strcmp(mName, "__BfCtor") == 0)
- {
- str += "this";
- handled = true;
- }
- else if (strcmp(mName, "__BfStaticCtor") == 0)
- {
- str += "this";
- handled = true;
- }
- else if (strcmp(mName, "__BfCtorClear") == 0)
- {
- str += "this$clear";
- handled = true;
- }
- }
- if (!handled)
- str += mName;
- }
- }
- //if (mTemplateName != NULL)
- //str += mTemplateName;
- if (str.empty())
- str += "`anon";
- if ((str[str.length() - 1] == '!') || (str[0] == '<'))
- {
- if (language == DbgLanguage_Beef)
- {
- // It's a mixin - assert that there's no params
- //BF_ASSERT(mParams.Size() == 0);
- }
- //return str;
- }
- str += "(";
- bool showedParam = false;
- int i = 0;
- for (auto variable : mParams)
- {
- if ((variable->mName != NULL) && (strcmp(variable->mName, "this") == 0))
- continue;
- if (showedParam)
- str += ", ";
- if (variable->mType != NULL)
- {
- auto varType = variable->mType;
- if (varType->mTypeCode == DbgType_Const)
- varType = varType->mTypeParam;
- if (variable->mSigNoPointer)
- {
- BF_ASSERT(varType->IsPointer());
- varType = varType->mTypeParam;
- }
- str += varType->ToString(language);
- if (variable->mName != NULL)
- str += " ";
- }
- if (variable->mName != NULL)
- str += variable->mName;
- showedParam = true;
- i++;
- }
- str += ")";
- return str;
- }
- // For inlined subprograms, the "root" inliner means the bottom-most non-inlined function. This subprogram contains
- // all the line data for it's own non-inlined instructions, PLUS line data for all inlined functions that it calls.
- // The inlined functions has empty mLineInfo structures.
- //
- // When we pass a non-NULL value into inlinedSubprogram, we are requesting to ONLY return lines that were emitted from
- // that subprogram (inlined or not).
- //
- // If we call FindClosestLine on an inlined subprogram, we only want results of functions that are inside or inlined by
- // the 'this' subprogram. Thus, we do a "get any line" call on the root inliner and then filter the results based
- // on whether they are relevant.
- DbgLineData* DbgSubprogram::FindClosestLine(addr_target addr, DbgSubprogram** inlinedSubprogram, DbgSrcFile** srcFile, int* outLineIdx)
- {
- if (mLineInfo == NULL)
- {
- if (mInlineeInfo == NULL)
- return NULL;
-
- if ((inlinedSubprogram != NULL) && (*inlinedSubprogram != NULL))
- {
- // Keep explicit inlinee requirement
- return mInlineeInfo->mRootInliner->FindClosestLine(addr, inlinedSubprogram, srcFile, outLineIdx);
- }
- else
- {
- DbgSubprogram* rootInlinedSubprogram = NULL;
- auto result = mInlineeInfo->mRootInliner->FindClosestLine(addr, &rootInlinedSubprogram, srcFile, outLineIdx);
- if (result == NULL)
- return NULL;
- if (rootInlinedSubprogram == NULL) // Do not allow root parent, as we cannot be a parent to the root parent (duh)
- return NULL;
- // We need to check to see if we are a parent of the found line
- auto checkSubprogram = rootInlinedSubprogram;
- while ((checkSubprogram != NULL) && (checkSubprogram->mInlineeInfo != NULL))
- {
- if (checkSubprogram == this)
- {
- if (inlinedSubprogram != NULL)
- *inlinedSubprogram = rootInlinedSubprogram;
- return result;
- }
- checkSubprogram = checkSubprogram->mInlineeInfo->mInlineParent;
- }
- return NULL;
- }
- }
- // Binary search - lineData is sorted
- int first = 0;
- int last = (int)mLineInfo->mLines.mSize - 1;
- int middle = (first + last) / 2;
- int useIdx = -1;
- while (first <= last)
- {
- addr_target midAddr = (addr_target)(mLineInfo->mLines.mVals[middle].mRelAddress + mCompileUnit->mDbgModule->mImageBase);
- if (midAddr < addr)
- first = middle + 1;
- else if (midAddr == addr)
- {
- useIdx = middle;
- break;
- }
- else
- last = middle - 1;
- middle = (first + last) / 2;
- }
- if (useIdx == -1)
- useIdx = last;
- if (last == -1)
- return NULL;
-
- // If we have lines with the same addr, take the more inner one
- while (true)
- {
- auto lineData = &mLineInfo->mLines.mVals[useIdx];
- if (useIdx + 1 < mLineInfo->mLines.mSize)
- {
- auto peekNext = &mLineInfo->mLines.mVals[useIdx + 1];
- if (lineData->mRelAddress != peekNext->mRelAddress)
- break;
- useIdx++;
- }
- else
- {
- break;
- }
- }
- while (true)
- {
- auto lineData = &mLineInfo->mLines.mVals[useIdx];
- if (addr < lineData->mRelAddress + lineData->mContribSize + mCompileUnit->mDbgModule->mImageBase)
- {
- auto& ctx = mLineInfo->mContexts[lineData->mCtxIdx];
- if (srcFile != NULL)
- *srcFile = ctx.mSrcFile;
- if (inlinedSubprogram != NULL)
- {
- auto subprogram = (ctx.mInlinee != NULL) ? ctx.mInlinee : this;
- if (*inlinedSubprogram != NULL)
- {
- // Strictness check
- if (subprogram == *inlinedSubprogram)
- {
- if (outLineIdx != NULL)
- *outLineIdx = useIdx;
- return lineData;
- }
- }
- else
- {
- *inlinedSubprogram = subprogram;
- if (outLineIdx != NULL)
- *outLineIdx = useIdx;
- return lineData;
- }
- }
- else
- {
- if (outLineIdx != NULL)
- *outLineIdx = useIdx;
- return lineData;
- }
- }
- // Hope we can find an earlier entry whose "contribution" is still valid
- if (--useIdx < 0)
- break;
- }
- return NULL;
- }
- DbgType* DbgSubprogram::GetParent()
- {
- if ((mParentType == NULL) && (mCompileUnit != NULL))
- mCompileUnit->mDbgModule->MapCompileUnitMethods(mCompileUnit);
- return mParentType;
- }
- DbgType* DbgSubprogram::GetTargetType()
- {
- if (!mHasThis)
- return mParentType;
- auto thisType = mParams.mHead->mType;
- if (thisType == NULL)
- return mParentType;
- if (thisType->IsPointer())
- return thisType->mTypeParam;
- return thisType;
- }
- DbgLanguage DbgSubprogram::GetLanguage()
- {
- if (mParentType != NULL)
- return mParentType->GetLanguage();
- if (mCompileUnit->mLanguage != DbgLanguage_Unknown)
- return mCompileUnit->mLanguage;
- return DbgLanguage_C; // Parent type would have been set for Beef, so it must be C
- }
- bool DbgSubprogram::Equals(DbgSubprogram* checkMethod)
- {
- if ((mLinkName != NULL) && (checkMethod->mLinkName != NULL))
- {
- return strcmp(mLinkName, checkMethod->mLinkName) == 0;
- }
-
- if (strcmp(mName, checkMethod->mName) != 0)
- return false;
- auto param = mParams.mHead;
- auto checkParam = checkMethod->mParams.mHead;
- while ((param != NULL) && (checkParam != NULL))
- {
- if ((param->mType != checkParam->mType) && (!param->mType->Equals(checkParam->mType)))
- return false;
- param = param->mNext;
- checkParam = checkParam->mNext;
- }
- if ((param != NULL) || (checkParam != NULL))
- return false;
- if (!mReturnType->Equals(checkMethod->mReturnType))
- return false;
- return true;
- }
- int DbgSubprogram::GetParamCount()
- {
- int paramCount = mParams.Size();
- if (mHasThis)
- paramCount--;
- return paramCount;
- }
- String DbgSubprogram::GetParamName(int paramIdx)
- {
- auto param = mParams[paramIdx];
- if (param->mName != NULL)
- {
- String name = "'";
- name += param->mName;
- name += "'";
- return name;
- }
- return StrFormat("%d", paramIdx + 1);
- }
- bool DbgSubprogram::IsGenericMethod()
- {
- if (mName == NULL)
- return false;
- for (const char* cPtr = mName; true; cPtr++)
- {
- char c = *cPtr;
- if (c == '\0')
- break;
- if (c == '<')
- return true;
- }
- return false;
- }
- bool DbgSubprogram::ThisIsSplat()
- {
- if (mBlock.mVariables.mHead == NULL)
- return false;
- return strncmp(mBlock.mVariables.mHead->mName, "$this$", 6) == 0;
- }
- //////////////////////////////////////////////////////////////////////////
- DbgSubprogram::~DbgSubprogram()
- {
- BfLogDbg("DbgSubprogram::~DbgSubprogram %p\n", this);
- }
- ////////////////////
- bool DbgSrcFile::IsBeef()
- {
- int dotPos = (int)mFilePath.LastIndexOf('.');
- if (dotPos == -1)
- return false;
- const char* ext = mFilePath.c_str() + dotPos;
- // The ".cs" is legacy. Remove that eventually.
- return (stricmp(ext, ".bf") == 0) || (stricmp(ext, ".cs") == 0);
- }
- DbgSrcFile::~DbgSrcFile()
- {
- for (auto replacedLineInfo : mHotReplacedDbgLineInfo)
- delete replacedLineInfo;
- }
- void DbgSrcFile::RemoveDeferredRefs(DbgModule* debugModule)
- {
- for (int deferredIdx = 0; deferredIdx < (int)mDeferredRefs.size(); )
- {
- if (mDeferredRefs[deferredIdx].mDbgModule == debugModule)
- {
- // Fast remove
- mDeferredRefs[deferredIdx] = mDeferredRefs.back();
- mDeferredRefs.pop_back();
- }
- else
- deferredIdx++;
- }
- }
- void DbgSrcFile::RemoveLines(DbgModule* debugModule)
- {
- if (!mHasLineDataFromMultipleModules)
- {
- // Fast-out case
- mLineDataRefs.Clear();
- mFirstLineDataDbgModule = NULL;
- return;
- }
- for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
- {
- auto dbgSubprogram = mLineDataRefs[idx];
- if (dbgSubprogram->mCompileUnit->mDbgModule == debugModule)
- {
- mLineDataRefs.RemoveAtFast(idx);
- idx--;
- }
- }
- }
- void DbgSrcFile::RemoveLines(DbgModule* debugModule, DbgSubprogram* dbgSubprogram, bool isHotReplaced)
- {
- debugModule->mDebugTarget->mPendingSrcFileRehup.Add(this);
- if (isHotReplaced)
- {
- int vecIdx = dbgSubprogram->mCompileUnit->mDbgModule->mHotIdx;
- BF_ASSERT(vecIdx >= 0);
- while (vecIdx >= (int)mHotReplacedDbgLineInfo.size())
- mHotReplacedDbgLineInfo.push_back(new HotReplacedLineInfo());
- auto hotReplacedLineInfo = mHotReplacedDbgLineInfo[vecIdx];
- HotReplacedLineInfo::Entry entry;
- entry.mSubprogram = dbgSubprogram;
- entry.mLineInfo = dbgSubprogram->mLineInfo;
- hotReplacedLineInfo->mEntries.Add(entry);
- }
- }
- void DbgSrcFile::RehupLineData()
- {
- for (int idx = 0; idx < (int)mLineDataRefs.size(); idx++)
- {
- auto dbgSubprogram = mLineDataRefs[idx];
- if (dbgSubprogram->mWasModuleHotReplaced)
- {
- mLineDataRefs.RemoveAtFast(idx);
- idx--;
- }
- }
- }
- const String& DbgSrcFile::GetLocalPath()
- {
- return (!mLocalPath.IsEmpty()) ? mLocalPath : mFilePath;
- }
- //////////////////////////////////////////////////////////////////////////
- DbgType::DbgType()
- {
- mTypeIdx = -1;
- //mHash = 0;
- mIsDeclaration = false;
- mParent = NULL;
- //mName = NULL;
- mTypeName = NULL;
- mTypeCode = DbgType_Null;
- mSize = 0;
- //mArraySize = 0;
- mDeclLine = 0;
- mPtrType = NULL;
- mTypeParam = NULL;
- mBlockParam = NULL;
- mNext = NULL;
- mPriority = DbgTypePriority_Normal;
- }
- DbgType::~DbgType()
- {
- BfLogDbg("DbgType::~DWType %p\n", this);
- }
- DbgType* DbgType::ResolveTypeDef()
- {
- if (mTypeCode == DbgType_TypeDef)
- return mTypeParam->ResolveTypeDef();
- return this;
- }
- bool DbgType::Equals(DbgType* dbgType)
- {
- if (dbgType == NULL)
- return false;
- if (mTypeCode != dbgType->mTypeCode)
- return false;
- if ((mName == NULL) != (dbgType->mName == NULL))
- return false;
- if (mName != NULL)
- {
- if (dbgType->mFixedName)
- FixName();
- else if (mFixedName)
- dbgType->FixName();
- if (strcmp(mName, dbgType->mName) != 0)
- return false;
- }
- if ((mTypeParam != NULL) && (!mTypeParam->Equals(dbgType->mTypeParam)))
- return false;
-
- // Did mName already include the parent name?
- if (mCompileUnit->mDbgModule->mDbgFlavor == DbgFlavor_MS)
- return true;
- if ((mParent != NULL) != (dbgType->mParent != NULL))
- return false;
- if (mParent != NULL)
- return mParent->Equals(dbgType->mParent);
- return true;
- }
- bool DbgType::IsStruct()
- {
- return mTypeCode == DbgType_Struct;
- }
- bool DbgType::IsPrimitiveType()
- {
- return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_Bool);
- }
- bool DbgType::IsNull()
- {
- return mTypeCode == DbgType_Null;
- }
- bool DbgType::IsVoid()
- {
- return (mTypeCode == DbgType_Void);
- }
- bool DbgType::IsValuelessType()
- {
- return ((mTypeCode == DbgType_Struct) && (GetByteCount() == 0)) || (mTypeCode == DbgType_Void);
- }
- bool DbgType::IsValueType()
- {
- return (mTypeCode <= DbgType_DefinitionEnd);
- }
- bool DbgType::IsTypedPrimitive()
- {
- if (mTypeCode != DbgType_Struct)
- return false;
- auto baseType = GetBaseType();
- if (baseType == NULL)
- return false;
- if (!baseType->IsPrimitiveType() && !baseType->IsTypedPrimitive())
- return false;
- if (mTypeParam == NULL)
- mTypeParam = baseType;
- return true;
- }
- bool DbgType::IsBoolean()
- {
- return mTypeCode == DbgType_Bool;
- }
- bool DbgType::IsInteger()
- {
- return (mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64);
- }
- bool DbgType::IsIntegral()
- {
- return ((mTypeCode >= DbgType_i8) && (mTypeCode <= DbgType_u64)) ||
- ((mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32));
- }
- bool DbgType::IsChar()
- {
- return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_UChar32);
- }
- bool DbgType::IsChar(DbgLanguage language)
- {
- if (language == DbgLanguage_Beef)
- return (mTypeCode >= DbgType_UChar) && (mTypeCode <= DbgType_UChar32);
- return (mTypeCode >= DbgType_SChar) && (mTypeCode <= DbgType_SChar32);
- }
- bool DbgType::IsFloat()
- {
- return (mTypeCode == DbgType_Single) || (mTypeCode == DbgType_Double);
- }
- // "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
- bool DbgType::IsCompositeType()
- {
- if (((mTypeCode == DbgType_TypeDef) || (mTypeCode == DbgType_Const)) && (mTypeParam != NULL))
- return mTypeParam->IsCompositeType();
- return ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_SizedArray));
- }
- bool DbgType::WantsRefThis()
- {
- return (GetLanguage() == DbgLanguage_Beef) && (!IsBfObject());
- }
- bool DbgType::IsBfObjectPtr()
- {
- if ((mTypeCode == DbgType_Ptr) && (mTypeParam != NULL))
- return mTypeParam->IsBfObject();
- return false;
- }
- DbgExtType DbgType::CalcExtType()
- {
- auto language = GetLanguage();
- if ((!mFixedName) && (language == DbgLanguage_Beef))
- {
- FixName();
- }
- auto primaryType = GetPrimaryType();
- if (this != primaryType)
- {
- return primaryType->CalcExtType();
- }
- if (mCompileUnit == NULL)
- return DbgExtType_Normal;
- if (language != DbgLanguage_Beef)
- return DbgExtType_Normal;
- if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
- return DbgExtType_Normal;
- PopulateType();
- if (mExtType != DbgExtType_Unknown)
- return mExtType;
- auto baseType = GetBaseType();
- if (baseType == NULL)
- {
- if (mParent == NULL)
- return DbgExtType_Normal;
- if (mParent->mTypeCode != DbgType_Namespace)
- return DbgExtType_Normal;
- if (mParent->mParent != NULL)
- return DbgExtType_Normal;
- if (strcmp(mParent->mTypeName, "System") != 0)
- return DbgExtType_Normal;
- if (strcmp(mTypeName, "Object") != 0)
- return DbgExtType_Normal;
- return DbgExtType_BfObject;
- }
- else
- {
- if (strcmp(baseType->mTypeName, "Enum") == 0)
- {
- for (auto member : mMemberList)
- {
- if (strcmp(member->mName, "__bftag") == 0)
- return DbgExtType_BfPayloadEnum;
- }
- return DbgExtType_Normal;
- }
- else if (strcmp(baseType->mTypeName, "ValueType") == 0)
- {
- for (auto member : mMemberList)
- {
- if (strcmp(member->mName, "__bfunion") == 0)
- return DbgExtType_BfUnion;
- }
- }
- }
- auto baseExtType = baseType->CalcExtType();
- if ((baseExtType == DbgExtType_BfObject) && (mSize == 0))
- baseExtType = DbgExtType_Interface;
- return baseExtType;
- }
- DbgLanguage DbgType::GetLanguage()
- {
- return mLanguage;
- }
- void DbgType::FixName()
- {
- if (mFixedName)
- return;
-
- int depthCount = 0;
- auto dbgModule = mCompileUnit->mDbgModule;
- if ((dbgModule->mDbgFlavor == DbgFlavor_MS) && (mName != NULL) && (strlen(mName) > 0))
- {
- bool modified = false;
- if (!dbgModule->DbgIsStrMutable(mName))
- mName = dbgModule->DbgDupString(mName);
- const char* typeNamePtr = mTypeName;
- char* nameP = (char*)mName;
- // Fix the name
- char* inPtr = nameP;
- char* outPtr = nameP;
- while (true)
- {
- char c = *(inPtr++);
- if ((c == '<') || (c == '('))
- depthCount++;
- else if ((c == '>') || (c == ')'))
- depthCount--;
- if ((c == ':') && (inPtr[0] == ':'))
- {
- modified = true;
- inPtr++;
- *(outPtr++) = '.';
- if (depthCount == 0)
- typeNamePtr = outPtr;
- }
- else if (modified)
- *(outPtr++) = c;
- else
- outPtr++;
- if (c == 0)
- break;
- }
- if ((modified) && (mName != mTypeName) && (typeNamePtr != NULL))
- {
- mTypeName = typeNamePtr;
- }
- }
- mFixedName = true;
- }
- bool DbgType::IsBfObject()
- {
- if (mExtType == DbgExtType_Unknown)
- mExtType = CalcExtType();
- return (mExtType == DbgExtType_BfObject) || (mExtType == DbgExtType_Interface);
- }
- bool DbgType::IsBfPayloadEnum()
- {
- if (mExtType == DbgExtType_Unknown)
- mExtType = CalcExtType();
- return mExtType == DbgExtType_BfPayloadEnum;
- }
- bool DbgType::IsBfUnion()
- {
- if (mExtType == DbgExtType_Unknown)
- mExtType = CalcExtType();
- return mExtType == DbgExtType_BfUnion;
- }
- bool DbgType::IsBfEnum()
- {
- if (mTypeCode != DbgType_Struct)
- return false;
- auto baseType = GetBaseType();
- if (baseType == NULL)
- {
- if (mParent == NULL)
- return false;
- if (mParent->mTypeCode != DbgType_Namespace)
- return false;
- if (mParent->mParent != NULL)
- return false;
- if (strcmp(mParent->mTypeName, "System") != 0)
- return false;
- return strcmp(mTypeName, "Enum") == 0;
- }
- return baseType->IsBfEnum();
- }
- bool DbgType::IsBfTuple()
- {
- if (mTypeCode != DbgType_Struct)
- return false;
- if (GetLanguage() != DbgLanguage_Beef)
- return false;
- if (mName == NULL)
- return false;
- return mName[0] == '(';
- }
- bool DbgType::HasCPPVTable()
- {
- if ((mTypeCode != DbgType_Struct) && (mTypeCode != DbgType_Class))
- return false;
- /*if (!mMemberList.IsEmpty())
- {
- //TODO: We commented this out at some point- why did we do that?
- if ((mMemberList.mHead->mName != NULL) && (strncmp(mMemberList.mHead->mName, "_vptr$", 6) == 0))
- return true;
- }*/
- if (mHasVTable)
- return true;
-
- if (GetLanguage() == DbgLanguage_Beef)
- return false;
- for (auto checkBaseType : mBaseTypes)
- {
- if (checkBaseType->mBaseType->HasCPPVTable())
- return true;
- }
- return false;
- }
- bool DbgType::IsBaseBfObject()
- {
- auto baseType = GetBaseType();
- return (baseType == NULL) && (IsBfObject());
- }
- bool DbgType::IsInterface()
- {
- if (mExtType == DbgExtType_Unknown)
- mExtType = CalcExtType();
- return mExtType == DbgExtType_Interface;
- }
- bool DbgType::IsNamespace()
- {
- return mTypeCode == DbgType_Namespace;
- }
- bool DbgType::IsEnum()
- {
- return (mTypeCode == DbgType_Enum);
- }
- bool DbgType::IsRoot()
- {
- return (mTypeCode == DbgType_Root);
- }
- bool DbgType::IsRef()
- {
- return
- (mTypeCode == DbgType_Ref) ||
- (mTypeCode == DbgType_RValueReference);
- }
- bool DbgType::IsSigned()
- {
- return
- (mTypeCode == DbgType_i8) ||
- (mTypeCode == DbgType_i16) ||
- (mTypeCode == DbgType_i32) ||
- (mTypeCode == DbgType_i64);
- }
- bool DbgType::IsConst()
- {
- if ((mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_Ref))
- {
- if (mTypeParam != NULL)
- return mTypeParam->IsConst();
- }
- return mTypeCode == DbgType_Const;
- }
- bool DbgType::IsPointer(bool includeBfObjectPointer)
- {
- if (mTypeCode != DbgType_Ptr)
- return false;
- if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
- return false;
- return true;
- }
- bool DbgType::HasPointer(bool includeBfObjectPointer)
- {
- if (((mTypeCode == DbgType_Const) || (mTypeCode == DbgType_Ref)) && (mTypeParam != NULL))
- return mTypeParam->IsPointer(includeBfObjectPointer);
- return IsPointer(includeBfObjectPointer);
- }
- bool DbgType::IsPointerOrRef(bool includeBfObjectPointer)
- {
- if ((mTypeCode != DbgType_Ptr) && (mTypeCode != DbgType_Ref) && (mTypeCode != DbgType_RValueReference))
- return false;
- if ((!includeBfObjectPointer) && (mTypeParam != NULL) && (mTypeParam->IsBfObject()))
- return false;
- return true;
- }
- bool DbgType::IsSizedArray()
- {
- return (mTypeCode == DbgType_SizedArray);
- }
- bool DbgType::IsAnonymous()
- {
- return (mTypeName == NULL) || (mTypeName[0] == '<');
- }
- bool DbgType::IsGlobalsContainer()
- {
- return (mTypeName != NULL) && (mTypeName[0] == 'G') && (mTypeName[1] == '$');
- }
- DbgType* DbgType::GetUnderlyingType()
- {
- return mTypeParam;
- }
- void DbgType::PopulateType()
- {
- if (mIsIncomplete)
- {
- mCompileUnit->mDbgModule->PopulateType(this);
- mIsIncomplete = false;
- }
- }
- DbgModule* DbgType::GetDbgModule()
- {
- if (mCompileUnit == NULL)
- return NULL;
- return mCompileUnit->mDbgModule;
- }
- DbgType* DbgType::GetPrimaryType()
- {
- if (mPrimaryType != NULL)
- return mPrimaryType;
- mPrimaryType = this;
- if (mPriority <= DbgTypePriority_Normal)
- {
- if ((mCompileUnit != NULL) &&
- ((mCompileUnit->mLanguage == DbgLanguage_Beef) || (mTypeCode == DbgType_Namespace) || (mIsDeclaration)))
- {
- mPrimaryType = mCompileUnit->mDbgModule->GetPrimaryType(this);
- }
- }
- return mPrimaryType;
- }
- DbgType* DbgType::GetBaseType()
- {
- auto primaryType = GetPrimaryType();
- if (primaryType != this)
- return primaryType->GetBaseType();
- PopulateType();
- if (mBaseTypes.mHead == NULL)
- return NULL;
- if (GetLanguage() != DbgLanguage_Beef)
- return NULL;
- auto baseType = mBaseTypes.mHead->mBaseType;
- BF_ASSERT(!baseType->IsInterface());
-
- if ((baseType == NULL) || (baseType->mPriority > DbgTypePriority_Normal))
- return baseType;
- baseType = mCompileUnit->mDbgModule->GetPrimaryType(baseType);
- mBaseTypes.mHead->mBaseType = baseType;
- return baseType;
- }
- DbgType* DbgType::GetRootBaseType()
- {
- auto baseType = GetBaseType();
- if (baseType != NULL)
- return baseType->GetRootBaseType();
- return this;
- }
- DbgType* DbgType::RemoveModifiers(bool* hadRef)
- {
- DbgType* dbgType = this;
- while (dbgType != NULL)
- {
- bool curHadRef = (dbgType->mTypeCode == DbgType_Ref) || (dbgType->mTypeCode == DbgType_RValueReference);
- if ((curHadRef) && (hadRef != NULL))
- *hadRef = true;
- if ((dbgType->mTypeCode == DbgType_Const) || (dbgType->mTypeCode == DbgType_TypeDef) || (dbgType->mTypeCode == DbgType_Volatile) || (dbgType->mTypeCode == DbgType_Bitfield) ||
- (dbgType->mTypeCode == DbgType_Unaligned) || (curHadRef))
- {
- if (dbgType->mTypeParam == NULL)
- break;
- dbgType = dbgType->mTypeParam;
- }
- else
- break;
- }
- return dbgType;
- }
- String DbgType::ToStringRaw(DbgLanguage language)
- {
- if (mTypeIdx != -1)
- return StrFormat("_T_%d", mTypeIdx);
- return ToString(language);
- }
- String DbgType::ToString(DbgLanguage language, bool allowDirectBfObject)
- {
- if (language == DbgLanguage_Unknown)
- language = GetLanguage();
- if (language == DbgLanguage_Beef)
- {
- switch (mTypeCode)
- {
- case DbgType_UChar:
- return "char8";
- case DbgType_UChar16:
- return "char16";
- case DbgType_UChar32:
- return "char32";
- case DbgType_i8:
- return "int8";
- case DbgType_u8:
- return "uint8";
- case DbgType_i16:
- return "int16";
- case DbgType_u16:
- return "uint16";
- case DbgType_i32:
- return "int32";
- case DbgType_u32:
- return "uint32";
- case DbgType_i64:
- return "int64";
- case DbgType_u64:
- return "uint64";
- }
- }
- else
- {
- switch (mTypeCode)
- {
- case DbgType_SChar:
- return "char";
- case DbgType_SChar16:
- return "wchar_t";
- case DbgType_SChar32:
- return "int32_t";
- case DbgType_UChar:
- return "uint8_t";
- case DbgType_UChar16:
- return "uint16_t";
- case DbgType_UChar32:
- return "uint32_t";
- case DbgType_i8:
- return "char";
- case DbgType_u8:
- return "uint8_t";
- case DbgType_i16:
- return "short";
- case DbgType_u16:
- return "uint16_t";
- case DbgType_i32:
- return "int";
- case DbgType_u32:
- return "uint32_t";
- case DbgType_i64:
- return "int64_t";
- case DbgType_u64:
- return "uint64_t";
- }
- }
- if (mTypeName != NULL)
- {
- if ((!allowDirectBfObject) && (IsBfObject()))
- {
- // Only use the '#' for testing
- //return ToString(true) + "#";
- return ToString(DbgLanguage_Unknown, true);
- }
- if (IsGlobalsContainer())
- {
- if (mParent != NULL)
- return mParent->ToString(language);
- return "";
- }
- char* nameP = (char*)mTypeName;
- //String combName;
- /*if (mTemplateParams != NULL)
- {
- combName = nameP;
- combName += mTemplateParams;
- nameP = combName.c_str();
- }*/
- if ((!mFixedName) && (language == DbgLanguage_Beef))
- {
- FixName();
- }
- if (mParent == NULL)
- {
- if (strncmp(nameP, "Box<", 4) == 0)
- return String(nameP + 4, nameP + strlen(nameP) - 1) + "^";
- // For declarations, may also include namespaces
- return mName;
- }
- if (GetLanguage() == DbgLanguage_Beef)
- return mParent->ToString(language) + "." + nameP;
- else
- return mParent->ToString(language) + "::" + nameP;
- }
- switch (mTypeCode)
- {
- case DbgType_Struct:
- {
- if ((mTypeName == NULL) && (mParent != NULL))
- return mParent->ToString(language);
- return "@struct";
- }
- case DbgType_Class:
- {
- return "@class";
- }
- case DbgType_TypeDef:
- {
- return "@typedef";
- }
- case DbgType_Const:
- {
- if (language == DbgLanguage_Beef)
- {
- if (mTypeParam == NULL)
- return "readonly";
- return "readonly " + mTypeParam->ToString(language);
- }
- if (mTypeParam == NULL)
- return "const";
- return "const " + mTypeParam->ToString(language);
- }
- case DbgType_Volatile:
- {
- if (mTypeParam == NULL)
- return "volatile";
- return "volatile " + mTypeParam->ToString(language);
- }
- case DbgType_Unaligned:
- {
- if (mTypeParam == NULL)
- return "unaligned";
- return "unaligned " + mTypeParam->ToString(language);
- }
- case DbgType_Restrict:
- {
- if (mTypeParam == NULL)
- return "restrict";
- return "restrict " + mTypeParam->ToString(language);
- }
- case DbgType_Ptr:
- {
- if (mTypeParam == NULL)
- return "void*";
-
- if (mTypeParam->IsBfObject())
- return mTypeParam->ToString(DbgLanguage_Unknown, true);
-
- // Don't put a "*" on the end of a function type, it's implicit
- if (mTypeParam->mTypeCode == DbgType_Subroutine)
- return mTypeParam->ToString(language);
- return mTypeParam->ToString(language) + "*";
- }
- case DbgType_Ref:
- {
- if (language == DbgLanguage_Beef)
- {
- if (mTypeParam == NULL)
- return "ref";
- return "ref " + mTypeParam->ToString(language);
- }
- if (mTypeParam == NULL)
- return "&";
- return mTypeParam->ToString(language) + "&";
- }
- case DbgType_RValueReference:
- {
- if (language == DbgLanguage_Beef)
- {
- // Ignore this - this is used for passing structs when we're not using the 'byval' attribute
- return mTypeParam->ToString(language);
- }
-
- if (mTypeParam == NULL)
- return "&&";
- return mTypeParam->ToString(language) + "&&";
- }
- case DbgType_Unspecified:
- return mTypeName;
- case DbgType_SizedArray:
- {
- String name;
- auto checkType = this;
- while (checkType->mTypeCode == DbgType_SizedArray)
- {
- int innerSize = checkType->mTypeParam->GetStride();
- int arrSize = 0;
- if (innerSize > 0)
- {
- arrSize = checkType->GetStride() / innerSize;
- }
- name += StrFormat("[%d]", arrSize);
- checkType = checkType->mTypeParam;
- }
- name = checkType->ToString(language) + name;
- return name;
- }
- case DbgType_Union:
- {
- if (mTypeName != NULL)
- return String("union ") + mTypeName;
- return "union";
- }
- case DbgType_Single:
- return "float";
- case DbgType_Double:
- return "double";
- case DbgType_Null:
- return "void";
- case DbgType_Subroutine:
- {
- String str;
- str += mTypeParam->ToString(language);
- str += " (";
- int paramIdx = 0;
- for (auto param : mBlockParam->mVariables)
- {
- if (paramIdx > 0)
- str += ", ";
- str += param->mType->ToString(language);
- paramIdx++;
- }
- str += ")";
- return str;
- }
- case DbgType_VTable:
- return "@vtable";
- case DbgType_Enum:
- return "@enum";
- case DbgType_Namespace:
- {
- // Anonymous
- return "`anon`";
- }
- case DbgType_PtrToMember:
- return "@ptrToMember";
- case DbgType_Bitfield:
- {
- auto dbgBitfieldType = (DbgBitfieldType*)this;
- return mTypeParam->ToString(language) + StrFormat("{%d:%d}", dbgBitfieldType->mPosition, dbgBitfieldType->mLength);
- }
- default:
- break;
- }
- BF_FATAL("Unhandled type");
- return "???";
- }
- int DbgType::GetByteCount()
- {
- if (!mSizeCalculated)
- {
- PopulateType();
- if ((mSize == 0) && (GetLanguage() == DbgLanguage_Beef))
- CalcExtType();
- if ((mTypeCode == DbgType_Struct) || (mTypeCode == DbgType_Class) || (mTypeCode == DbgType_Union))
- {
- if (mPriority <= DbgTypePriority_Normal)
- {
- auto primaryType = GetPrimaryType();
- if (primaryType != this)
- {
- mSize = primaryType->GetByteCount();
- mAlign = primaryType->mAlign;
- }
- }
- }
- else if ((mTypeCode == DbgType_Ref) || (mTypeCode == DbgType_Ptr) || (mTypeCode == DbgType_PtrToMember))
- {
- #ifdef BF_DBG_32
- mSize = 4;
- #else
- mSize = 8;
- #endif
- }
- else if (mTypeCode == DbgType_SizedArray)
- {
- auto language = GetLanguage();
- if (language == DbgLanguage_Beef)
- {
- if (mTypeParam->mAlign == 0)
- {
- NOP;
- }
- auto primaryType = mTypeParam->GetPrimaryType();
- if (primaryType->mAlign == 0)
- {
- NOP;
- }
- else
- {
- int elemCount = BF_ALIGN(mSize, primaryType->mAlign) / primaryType->GetStride();
- if (elemCount > 0)
- {
- mSize = ((elemCount - 1) * primaryType->GetStride()) + primaryType->GetByteCount();
- }
- }
- mAlign = primaryType->mAlign;
- }
- }
- else if (mTypeParam != NULL) // typedef, const, volatile, restrict, etc
- mSize = mTypeParam->GetByteCount();
- mSizeCalculated = true;
- }
- return mSize;
- }
- int DbgType::GetStride()
- {
- return BF_ALIGN(GetByteCount(), GetAlign());
- }
- int DbgType::GetAlign()
- {
- if (mAlign == 0)
- {
- auto primaryType = GetPrimaryType();
- if (primaryType != this)
- return primaryType->GetAlign();
- if (IsCompositeType())
- {
- PopulateType();
- }
- }
- if (mAlign != 0)
- return mAlign;
- return 1;
- }
- void DbgType::EnsureMethodsMapped()
- {
- for (auto methodNameEntry : mMethodNameList)
- {
- if (methodNameEntry->mCompileUnitId != -1)
- {
- mCompileUnit->mDbgModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
- methodNameEntry->mCompileUnitId = -1;
- }
- }
- }
- #define CREATE_PRIMITIVE_C(typeCode, cTypeName, type) \
- dbgType = mAlloc.Alloc<DbgType>(); \
- dbgType->mCompileUnit = &mDefaultCompileUnit; \
- dbgType->mName = cTypeName; \
- dbgType->mLanguage = DbgLanguage_C;\
- dbgType->mTypeName = cTypeName; \
- dbgType->mTypeCode = typeCode; \
- dbgType->mSize = sizeof(type); \
- dbgType->mAlign = sizeof(type); \
- mCPrimitiveTypes[typeCode] = dbgType; \
- mTypeMap.Insert(dbgType);
- #define CREATE_PRIMITIVE(typeCode, cTypeName, bfTypeName, structName, type) \
- dbgType = mAlloc.Alloc<DbgType>(); \
- dbgType->mCompileUnit = &mDefaultCompileUnit; \
- dbgType->mName = cTypeName; \
- dbgType->mLanguage = DbgLanguage_C;\
- dbgType->mTypeName = cTypeName; \
- dbgType->mTypeCode = typeCode; \
- dbgType->mSize = sizeof(type); \
- dbgType->mAlign = sizeof(type); \
- mCPrimitiveTypes[typeCode] = dbgType; \
- mTypeMap.Insert(dbgType); \
- dbgType = mAlloc.Alloc<DbgType>(); \
- dbgType->mCompileUnit = &mDefaultCompileUnit; \
- dbgType->mName = bfTypeName; \
- dbgType->mLanguage = DbgLanguage_Beef;\
- dbgType->mTypeName = bfTypeName; \
- dbgType->mTypeCode = typeCode; \
- dbgType->mSize = sizeof(type); \
- dbgType->mAlign = sizeof(type); \
- mBfPrimitiveTypes[typeCode] = dbgType; \
- mPrimitiveStructNames[typeCode] = structName; \
- mTypeMap.Insert(dbgType);
- DbgModule::DbgModule(DebugTarget* debugTarget) : mDefaultCompileUnit(this)
- {
- mMemReporter = NULL;
- mLoadState = DbgModuleLoadState_NotLoaded;
- mMappedImageFile = NULL;
- mEntryPoint = 0;
- mFailMsgPtr = NULL;
- mFailed = false;
- for (int i = 0; i < DbgType_COUNT; i++)
- {
- mBfPrimitiveTypes[i] = NULL;
- mCPrimitiveTypes[i] = NULL;
- mPrimitiveStructNames[i] = NULL;
- }
- DbgType* dbgType;
- mDefaultCompileUnit.mLanguage = DbgLanguage_Beef;
- mDefaultCompileUnit.mDbgModule = this;
- if (debugTarget != NULL)
- {
- CREATE_PRIMITIVE(DbgType_Void, "void", "void", "void", void*);
- dbgType->mSize = 0;
- dbgType->mAlign = 0;
- CREATE_PRIMITIVE(DbgType_Null, "null", "null", "null", void*);
- CREATE_PRIMITIVE(DbgType_IntPtr_Alias, "intptr_t", "int", "System.Int", intptr_target);
- CREATE_PRIMITIVE(DbgType_UIntPtr_Alias, "uintptr_t", "uint", "System.UInt", addr_target);
- CREATE_PRIMITIVE(DbgType_SChar, "char", "char", "System.Char", char);
- CREATE_PRIMITIVE(DbgType_SChar16, "wchar_t", "wchar", "System.Char16", wchar_t);
- CREATE_PRIMITIVE(DbgType_i8, "int8_t", "int8", "System.SByte", int8);
- CREATE_PRIMITIVE(DbgType_i16, "short", "int16", "System.Int16", int16);
- CREATE_PRIMITIVE(DbgType_i32, "int", "int32", "System.Int32", int32);
- CREATE_PRIMITIVE(DbgType_i64, "int64_t", "int64", "System.Int64", int64);
- CREATE_PRIMITIVE(DbgType_u8, "uint8_t", "uint8", "System.UInt8", uint8);
- CREATE_PRIMITIVE(DbgType_u16, "uint16_t", "uint16", "System.UInt16", uint16);
- CREATE_PRIMITIVE(DbgType_u32, "uint32_t", "uint32", "System.UInt32", uint32);
- CREATE_PRIMITIVE(DbgType_u64, "uint64_t", "uint64", "System.UInt64", uint64);
- CREATE_PRIMITIVE(DbgType_Single, "float", "float", "System.Single", float);
- CREATE_PRIMITIVE(DbgType_Double, "double", "double", "System.Double", double);
- CREATE_PRIMITIVE(DbgType_UChar, "char8", "char8", "System.Char", char);
- CREATE_PRIMITIVE(DbgType_UChar16, "char16", "char16", "System.Char16", short);
- CREATE_PRIMITIVE(DbgType_UChar32, "char32", "char32", "System.Char32", int);
- CREATE_PRIMITIVE(DbgType_Bool, "bool", "bool", "System.Boolean", bool);
- CREATE_PRIMITIVE(DbgType_Subroutine, "@Func", "@Func", "@Func", bool);
- CREATE_PRIMITIVE(DbgType_RawText, "@RawText", "@RawText", "@RawText", bool);
- CREATE_PRIMITIVE(DbgType_RegGroup, "@RegGroup", "@RegGroup", "@RegGroup", void*);
-
- CREATE_PRIMITIVE_C(DbgType_i8, "int16_t", int16_t);
- CREATE_PRIMITIVE_C(DbgType_i8, "int32_t", int32_t);
- CREATE_PRIMITIVE_C(DbgType_i64, "__int64", int64);
- CREATE_PRIMITIVE_C(DbgType_u64, "unsigned __int64", uint64);
- CREATE_PRIMITIVE_C(DbgType_u8, "unsigned char", uint8);
- CREATE_PRIMITIVE_C(DbgType_u16, "unsigned short", uint16);
- CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int", uint32);
- CREATE_PRIMITIVE_C(DbgType_u32, "unsigned int32_t", uint32_t);
- CREATE_PRIMITIVE_C(DbgType_u32, "unsigned long", uint32);
- CREATE_PRIMITIVE_C(DbgType_u64, "unsigned int64_t", uint64);
- }
- mIsDwarf64 = false;
- mDebugTarget = debugTarget;
- if (debugTarget != NULL)
- mDebugger = debugTarget->mDebugger;
- else
- mDebugger = NULL;
- mDebugLineData = NULL;
- mDebugInfoData = NULL;
- mDebugPubNames = NULL;
- mDebugFrameAddress = 0;
- mDebugFrameData = NULL;
- mDebugLocationData = NULL;
- mDebugRangesData = NULL;
- mDebugAbbrevData = NULL;
- mDebugStrData = NULL;
- mExceptionDirectoryData = NULL;
- mExceptionDirectoryDataLen = 0;
- mDebugAbbrevPtrData = NULL;
- mEHFrameData = NULL;
- mEHFrameAddress = 0;
- mStringTable = NULL;
- mSymbolData = NULL;
- mCheckedBfObject = false;
- mBfObjectHasFlags = false;
- mIsHotObjectFile = false;
- mStartTypeIdx = 0;
- mEndTypeIdx = 0;
- mHotIdx = 0;
- mStartSubprogramIdx = 0;
- mEndSubprogramIdx = 0;
- mCodeAddress = NULL;
- mMayBeOld = false;
- mTimeStamp = 0;
- mExpectedFileSize = 0;
-
- mImageBase = 0;
- mPreferredImageBase = 0;
- mImageSize = 0;
- mOrigImageData = NULL;
- mDeleting = false;
- mAllocSizeData = 0;
- mParsedSymbolData = false;
- mParsedTypeData = false;
- mParsedGlobalsData = false;
- mPopulatedStaticVariables = false;
- mParsedFrameDescriptors = false;
- mTLSAddr = 0;
- mTLSSize = 0;
- mTLSExtraAddr = 0;
- mTLSExtraSize = 0;
- mTLSIndexAddr = 0;
- mDbgFlavor = DbgFlavor_Unknown;
- mMasterCompileUnit = NULL;
- }
- DbgModule::~DbgModule()
- {
- delete mMemReporter;
- for (auto dwSrcFile : mEmptySrcFiles)
- delete dwSrcFile;
- for (auto dwCompileUnit : mCompileUnits)
- delete dwCompileUnit;
-
- delete mSymbolData;
- delete mStringTable;
- delete mDebugLineData;
- delete mDebugInfoData;
- delete mDebugPubNames;
- delete mDebugFrameData;
- delete mDebugLocationData;
- delete mDebugRangesData;
- delete mDebugAbbrevData;
- delete mDebugAbbrevPtrData;
- delete mDebugStrData;
- delete mExceptionDirectoryData;
- delete mEHFrameData;
-
- delete mOrigImageData;
- if ((mIsHotObjectFile) && (mImageBase != 0))
- {
- mDebugger->ReleaseHotTargetMemory((addr_target)mImageBase, (int)mImageSize);
- }
- for (auto data : mOwnedSectionData)
- delete data;
- }
- void DbgModule::Fail(const StringImpl& error)
- {
- if (mFailMsgPtr != NULL)
- {
- if (mFailMsgPtr->IsEmpty())
- *mFailMsgPtr = error;
- }
- mDebugger->OutputRawMessage("error " + error + "\n");
- mFailed = true;
- }
- char* DbgModule::DbgDupString(const char* str, const char* allocName)
- {
- int strLen = (int)strlen(str);
- if (strLen == 0)
- return NULL;
- char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, (allocName != NULL) ? allocName : "DbgDupString");
- memcpy(dupStr, str, strLen);
- return dupStr;
- }
- DbgModule* DbgModule::GetLinkedModule()
- {
- if (mIsHotObjectFile)
- return mDebugTarget->mTargetBinary;
- return this;
- }
- addr_target DbgModule::GetTargetImageBase()
- {
- if (mIsHotObjectFile)
- return (addr_target)mDebugTarget->mTargetBinary->mImageBase;
- return (addr_target)mImageBase;
- }
- void DbgModule::ParseGlobalsData()
- {
- mParsedGlobalsData = true;
- }
- void DbgModule::ParseSymbolData()
- {
- mParsedSymbolData = true;
- }
- void DbgModule::ParseTypeData()
- {
- mParsedTypeData = true;
- }
- DbgCompileUnit* DbgModule::ParseCompileUnit(int compileUnitId)
- {
- return NULL;
- }
- void DbgModule::MapCompileUnitMethods(DbgCompileUnit * compileUnit)
- {
- }
- void DbgModule::MapCompileUnitMethods(int compileUnitId)
- {
- }
- void DbgModule::PopulateType(DbgType* dbgType)
- {
- }
- void DbgModule::PopulateTypeGlobals(DbgType* dbgType)
- {
- }
- void DbgModule::PopulateStaticVariableMap()
- {
- if (mPopulatedStaticVariables)
- return;
- for (auto staticVariable : mStaticVariables)
- {
- mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
- }
- mPopulatedStaticVariables = true;
- }
- void DbgModule::ProcessDebugInfo()
- {
- }
- addr_target DbgModule::RemapAddr(addr_target addr)
- {
- if ((addr != 0) && (mPreferredImageBase != 0) && (mImageBase != 0))
- return addr + (intptr_target)(mImageBase - mPreferredImageBase);
- return addr;
- }
- void DbgModule::ParseAbbrevData(const uint8* data)
- {
- while (true)
- {
- int abbrevIdx = (int)DecodeULEB128(data);
- mDebugAbbrevPtrData[abbrevIdx] = data;
- if (abbrevIdx == 0)
- break;
- int entryTag = (int)DecodeULEB128(data);
- bool hasChildren = GET(char) == DW_CHILDREN_yes;
- while (true)
- {
- int attrName = (int)DecodeULEB128(data);
- int form = (int)DecodeULEB128(data);
- if ((attrName == 0) && (form == 0))
- break;
- }
- }
- }
- void DbgModule::ParseExceptionData()
- {
- if (mExceptionDirectoryData == NULL)
- return;
- BP_ZONE("DbgModule::ParseExceptionData");
- const uint8* data = mExceptionDirectoryData;
- const uint8* dataEnd = data + mExceptionDirectoryDataLen;
- static int entryCount = 0;
- addr_target imageBase = GetTargetImageBase();
- while (data < dataEnd)
- {
- addr_target beginAddress = GET(uint32);
- addr_target endAddress = GET(uint32);
- uint32 unwindData = GET(uint32);
-
- //TODO: Apparently unwindData can refer to another runtime entry in the .pdata if the LSB is set to 1?
- beginAddress += (addr_target)imageBase;
- endAddress += (addr_target)imageBase;
-
- int exSize = (int)(endAddress - beginAddress);
- for (int exOffset = 0; true; exOffset += DBG_MAX_LOOKBACK)
- {
- int curSize = exSize - exOffset;
- if (curSize <= 0)
- break;
-
- BP_ALLOC_T(DbgExceptionDirectoryEntry);
- DbgExceptionDirectoryEntry* exceptionDirectoryEntry = mAlloc.Alloc<DbgExceptionDirectoryEntry>();
- exceptionDirectoryEntry->mAddress = beginAddress + exOffset;
- exceptionDirectoryEntry->mOrigAddressOffset = exOffset;
- exceptionDirectoryEntry->mAddressLength = curSize;
- exceptionDirectoryEntry->mExceptionPos = (int)unwindData;
- exceptionDirectoryEntry->mDbgModule = this;
- mDebugTarget->mExceptionDirectoryMap.Insert(exceptionDirectoryEntry);
- entryCount++;
- }
- }
- }
- static int gIdx = 0;
- template <typename T> static bool IsTypeSigned() { return false; }
- template <> bool IsTypeSigned<int8>() { return true; }
- template <> bool IsTypeSigned<int16>() { return true; }
- template <> bool IsTypeSigned<int32>() { return true; }
- template <> bool IsTypeSigned<int64>() { return true; }
- #pragma warning(push)
- #pragma warning(disable:4302)
- #pragma warning(disable:4311)
- #pragma warning(disable:4312)
- #pragma warning(disable:4800)
- #pragma warning(disable:4800)
- template <typename T>
- T DbgModule::ReadValue(const uint8*& data, int form, int refOffset, const uint8** extraData, const uint8* startData)
- {
- gIdx++;
- switch (form)
- {
- case DW_FORM_strp:
- {
- int strOffset = GET(int);
- BF_ASSERT(mDebugStrData != NULL);
- const char* str = (const char*)mDebugStrData + strOffset;
- return (T)(intptr)str;
- }
- break;
- case DW_FORM_data1:
- {
- if (IsTypeSigned<T>())
- return (T)(intptr)GET(int8);
- else
- return (T)(uintptr)GET(uint8);
- }
- break;
- case DW_FORM_data2:
- {
- if (IsTypeSigned<T>())
- return (T)(intptr)GET(int16);
- else
- return (T)(uintptr)GET(uint16);
- }
- break;
- case DW_FORM_data4:
- {
- if (IsTypeSigned<T>())
- return (T)(intptr)GET(int32);
- else
- return (T)(uintptr)GET(uint32);
- }
- break;
- case DW_FORM_data8:
- {
- if (IsTypeSigned<T>())
- return (T)GET(int64);
- else
- return (T)GET(uint64);
- }
- break;
- case DW_FORM_ref1:
- {
- return (T)(intptr)GET(int8) + refOffset;
- }
- break;
- case DW_FORM_ref2:
- {
- return (T)(intptr)GET(int16) + refOffset;
- }
- break;
- case DW_FORM_ref4:
- {
- return (T)(intptr)GET(int32) + refOffset;
- }
- break;
- case DW_FORM_sec_offset:
- {
- intptr_target offset;
- if (mIsDwarf64)
- offset = (intptr_target)GET(int64);
- else
- offset = GET(int32);
- if (extraData != NULL)
- {
- *extraData = mDebugLocationData + offset;
- return 0;
- }
- return (T)offset;
- }
- break;
- case DW_FORM_addr:
- {
- return (T)GET(addr_target);
- }
- break;
- case DW_FORM_exprloc:
- {
- int64_t exprLen = DecodeULEB128(data);
- const uint8* endData = data + exprLen;
-
- if (extraData != NULL)
- *extraData = data;
-
- data = endData;
- return (T)exprLen;
- }
- break;
- case DW_FORM_flag_present:
- {
- //
- return (T)1;
- }
- break;
- case DW_FORM_flag:
- {
- //
- return (T)(intptr)GET(char);
- }
- break;
- case DW_FORM_sdata:
- return (T)DecodeSLEB128(data);
- case DW_FORM_udata:
- return (T)DecodeULEB128(data);
- case DW_FORM_string:
- {
- const char* str = (const char*)data;
- while (true)
- {
- uint8 val = *data;
- data++;
- if (val == 0)
- return (T)(intptr)str;
- }
- }
- case DW_FORM_block:
- {
- int blockLen = (int)DecodeULEB128(data);
- const uint8* retVal = data;
- data += blockLen;
- return (T)(intptr)retVal;
- }
- case DW_FORM_block1:
- {
- int blockLen = (int)*((uint8*)data);
- data += sizeof(uint8);
- const uint8* retVal = data;
- data += blockLen;
- return (T)(intptr)retVal;
- }
- default:
- assert("Not covered!" == 0);
- break;
- }
- return (T)0;
- }
- #pragma warning(pop)
- static int gAbbrevNum = 0;
- DbgType* DbgModule::GetOrCreateType(int typeIdx, DbgDataMap& dataMap)
- {
- if (typeIdx == 0)
- return NULL;
- DbgModule* linkedModule = GetLinkedModule();
- DbgType* dbgType = dataMap.Get<DbgType*>(typeIdx);
- if (dbgType != NULL)
- return dbgType;
- dbgType = mAlloc.Alloc<DbgType>();
- dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
- linkedModule->mTypes.push_back(dbgType);
- dataMap.Set(typeIdx, dbgType);
- return dbgType;
- }
- typedef std::pair<DbgClassType, void*> DataPair;
- typedef llvm::SmallVector<DataPair, 16> DataStack;
- template <typename T>
- T DbgModule::GetOrCreate(int idx, DbgDataMap& dataMap)
- {
- if (idx == 0)
- return NULL;
- T val = dataMap.Get<T>(idx);
- if (val != NULL)
- return val;
- val = mAlloc.Alloc<typename RemoveTypePointer<T>::type >();
- dataMap.Set(idx, val);
- return val;
- }
- template <typename T>
- static T GetStackTop(DataStack* dataStack)
- {
- auto dataPair = dataStack->back();
- if (dataPair.first == RemoveTypePointer<T>::type::ClassType)
- return (T)dataPair.second;
- return NULL;
- }
- template <>
- DbgBlock* GetStackTop<DbgBlock*>(DataStack* dataStack)
- {
- auto dataPair = dataStack->back();
- if (dataPair.first == DbgBlock::ClassType)
- return (DbgBlock*)dataPair.second;
- if (dataPair.first == DbgSubprogram::ClassType)
- return &((DbgSubprogram*)dataPair.second)->mBlock;
- if (dataPair.first == DbgType::ClassType)
- return ((DbgType*)dataPair.second)->mBlockParam;
- return NULL;
- }
- template <typename T>
- static bool StackHasType(DataStack* dataStack)
- {
- for (auto itr : *dataStack)
- if (itr.first == RemoveTypePointer<T>::type::ClassType)
- return true;
- return false;
- }
- template <typename T>
- static T GetStackLast(DataStack* dataStack)
- {
- for (int i = (int)dataStack->size() - 1; i >= 0; i--)
- {
- if ((*dataStack)[i].first == RemoveTypePointer<T>::type::ClassType)
- return (T)(*dataStack)[i].second;
- }
- return NULL;
- }
- template <typename T>
- static DataPair MakeDataPair(T* data)
- {
- return DataPair(T::ClassType, data);
- }
- void DbgModule::FixupInnerTypes(int startingTypeIdx)
- {
- BP_ZONE("DbgModule_FixupInnerTypes");
- for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
- {
- DbgType* dbgType = mTypes[typeIdx];
- if ((dbgType->mPriority == DbgTypePriority_Primary_Implicit) && (dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) &&
- (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
- {
- auto primaryParent = dbgType->mParent->GetPrimaryType();
- dbgType->mParent->mSubTypeList.Clear();
- dbgType->mParent = primaryParent;
- primaryParent->mSubTypeList.PushBack(dbgType);
- }
- }
- }
- void DbgModule::MapTypes(int startingTypeIdx)
- {
- BP_ZONE("DbgModule_MapTypes");
-
- bool needsInnerFixups = false;
- for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
- {
- DbgType* dbgType = mTypes[typeIdx];
- BF_ASSERT(dbgType->mTypeCode != DbgType_Null);
-
- if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mPriority < DbgTypePriority_Primary_Implicit))
- continue;
-
- //TODO: Always valid?
- if (dbgType->mIsDeclaration)
- continue;
- // We were avoiding adding '<' names before, but that made it impossible to look up auto-named primary types ,
- // like in-place unions like '<unnamed-type-u>'
- if ((dbgType->mTypeName == NULL) || (dbgType->mName == NULL) /*|| (dbgType->mTypeName[0] == '<')*/)
- continue;
- if (dbgType->mTypeCode > DbgType_DefinitionEnd)
- {
- // Only add "definition types"
- continue;
- }
- if (dbgType->mTypeCode == DbgType_Namespace)
- {
- bool isQualifiedNamespace = false;
- for (const char* cPtr = dbgType->mTypeName; *cPtr != '\0'; cPtr++)
- if (*cPtr == '.')
- isQualifiedNamespace = true;
- if (isQualifiedNamespace)
- continue; // Don't add fully qualified namespaces (they come from the 'using' implementation)*
- }
- if (dbgType->mHasStaticMembers)
- {
- for (auto member : dbgType->mMemberList)
- if ((member->mIsStatic) && (member->mLocationData != NULL))
- dbgType->mDefinedMembersCount++;
- }
-
- if ((dbgType->mTypeName != NULL) && (strcmp(dbgType->mTypeName, "@") == 0))
- {
- // Globals type.
- continue;
- }
- auto prevTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
- // Only replace previous instance if its a declaration
- if (prevTypeEntry != NULL)
- {
- auto prevType = prevTypeEntry->mValue;
- if (dbgType->mCompileUnit->mDbgModule != prevType->mCompileUnit->mDbgModule)
- {
- // Don't replace original types with hot types -- those need to be inserted in the the hot alternates list
- BF_ASSERT(dbgType->mCompileUnit->mDbgModule->mIsHotObjectFile);
- prevType->mHotNewType = dbgType;
- continue;
- }
- // Never override explicit primaries
- if (prevType->mPriority == DbgTypePriority_Primary_Explicit)
- continue;
- if (dbgType->mTypeCode == DbgType_TypeDef)
- {
- // Typedef can never override anything
- continue;
- }
- if (prevType->mTypeCode == DbgType_TypeDef)
- {
- if (dbgType->mTypeCode != DbgType_TypeDef)
- {
- // Allow this to override
- prevTypeEntry->mValue = dbgType;
- }
- continue;
- }
- // Don't replace a ptr to an BfObject with a BfObject
- if ((prevType->mTypeCode == DbgType_Ptr) && (dbgType->mTypeCode == DbgType_Struct))
- continue;
- if ((prevType->mTypeCode == DbgType_Struct) && (dbgType->mTypeCode == DbgType_Ptr))
- {
- // Allow this to override
- prevTypeEntry->mValue = dbgType;
- continue;
- }
- if (prevType->mTypeCode == DbgType_Namespace)
- {
- if (dbgType->mTypeCode != DbgType_Namespace)
- {
- // Old type was namespace but new isn't? Replace old type.
- while (!prevType->mSubTypeList.IsEmpty())
- {
- DbgType* subType = prevType->mSubTypeList.PopFront();
- subType->mParent = dbgType;
- dbgType->mSubTypeList.PushBack(subType);
- }
- prevType->mPriority = DbgTypePriority_Normal;
- if (dbgType->mPriority < DbgTypePriority_Primary_Implicit)
- dbgType->mPriority = DbgTypePriority_Primary_Implicit;
- prevTypeEntry->mValue = dbgType;
- continue;
- }
- // We definitely didn't want to do this for MS. For DWARF?
- //prevType->mAlternates.PushFront(dbgType, &mAlloc);
- continue;
- }
- else
- {
- // New type is namespace but old wasn't? Ignore new type.
- if (dbgType->mTypeCode == DbgType_Namespace)
- continue;
- if (dbgType->mIsDeclaration)
- continue;
- if (!prevType->mIsDeclaration)
- {
- if ((prevType->mCompileUnit == NULL) || (dbgType->mLanguage < prevType->mLanguage))
- {
- // We always want 'Beef' types to supersede 'C' types, but don't override the built-in primitive types
- continue;
- }
-
- if (prevType->mDefinedMembersCount > 0)
- {
- if (dbgType->mDefinedMembersCount > 0)
- {
- // We create an 'alternates' list for all types that define at least one static field
- prevType->mAlternates.PushFront(dbgType, &mAlloc);
- }
- continue;
- }
- if (prevType->mMethodsWithParamsCount > dbgType->mMethodsWithParamsCount)
- {
- // This handles a special case where methods without line data like <Enum>.HasFlags doesn't show containing
- // params in cases where it gets inlined
- continue;
- }
- // Types with method lists are preferred
- if ((!prevType->mMethodList.IsEmpty()) && (dbgType->mMethodList.IsEmpty()))
- continue;
- if ((prevType->mTypeCode == DbgType_Ptr) && (prevType->mTypeParam != NULL) && (!prevType->mTypeParam->mMethodList.IsEmpty()))
- continue;
- }
- // Replace type
- if (!prevType->mSubTypeList.IsEmpty())
- needsInnerFixups = true;
- prevType->mPriority = DbgTypePriority_Normal;
- if (dbgType->mPriority == DbgTypePriority_Normal)
- dbgType->mPriority = DbgTypePriority_Primary_Implicit;
- prevTypeEntry->mValue = dbgType;
- continue;
- }
- }
- if ((dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) && (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
- needsInnerFixups = true;
- if (dbgType->mPriority == DbgTypePriority_Normal)
- dbgType->mPriority = DbgTypePriority_Primary_Implicit;
- mTypeMap.Insert(dbgType);
- }
- if (needsInnerFixups)
- FixupInnerTypes(startingTypeIdx);
- }
- void DbgModule::CreateNamespaces()
- {
- BP_ZONE("DbgModule::CreateNamespaces");
- int startLength = (int)mTypes.size();
- for (int typeIdx = 0; typeIdx < startLength; typeIdx++)
- {
- DbgType* dbgType = mTypes[typeIdx];
-
- if (dbgType->mName == NULL)
- continue;
- if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mTagIdx != 0))
- {
- auto namespaceTypeEntry = FindType(dbgType->mName, dbgType->GetLanguage());
- DbgType* namespaceType;
- if (namespaceTypeEntry == NULL)
- {
- namespaceType = mAlloc.Alloc<DbgType>();
- namespaceType->mTypeCode = DbgType_Namespace;
- namespaceType->mLanguage = dbgType->mLanguage;
- namespaceType->mCompileUnit = dbgType->mCompileUnit;
- namespaceType->mTypeIdx = (int)mTypes.size();
- namespaceType->mPriority = DbgTypePriority_Primary_Explicit;
- namespaceType->mName = dbgType->mName;
- namespaceType->mTypeName = dbgType->mTypeName;
- if (dbgType->mParent != NULL)
- {
- namespaceType->mParent = dbgType->mParent->GetPrimaryType();
- namespaceType->mParent->mSubTypeList.PushBack(namespaceType);
- }
- else
- {
- namespaceType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(namespaceType);
- }
- mTypes.push_back(namespaceType);
- mTypeMap.Insert(namespaceType);
- }
- else
- namespaceType = namespaceTypeEntry->mValue;
-
- while (!dbgType->mMemberList.IsEmpty())
- {
- DbgVariable* curVar = dbgType->mMemberList.PopFront();
- namespaceType->mMemberList.PushBack(curVar);
- }
- DbgType* prevType = NULL;
- DbgType* curType = dbgType->mSubTypeList.mHead;
- while (curType != NULL)
- {
- DbgType* nextType = curType->mNext;
- if (curType->mPriority >= DbgTypePriority_Primary_Implicit)
- {
- dbgType->mSubTypeList.Remove(curType, prevType);
- namespaceType->mSubTypeList.PushBack(curType);
- }
- prevType = curType;
- curType = nextType;
- }
- continue;
- }
- }
- // If we didn't have a parent type for a namespace (ie: if System.Collections wasn't linked to System) then we wait
- // until the end and move those from the global list to the parent list
- for (int typeIdx = startLength; typeIdx < (int)mTypes.size(); typeIdx++)
- {
- DbgType* dbgType = mTypes[typeIdx];
- if (dbgType->mParent != NULL)
- continue;
-
- char* typeName = (char*)dbgType->mTypeName;
- int lastDotIdx = -1;
- for (int i = 0; true; i++)
- {
- char c = typeName[i];
- if (c == 0)
- break;
- if (c == '.')
- lastDotIdx = i;
- }
- if (lastDotIdx == -1)
- continue;
- typeName[lastDotIdx] = 0;
- dbgType->mTypeName = typeName + lastDotIdx + 1;
- auto parentEntry = FindType(typeName, dbgType->GetLanguage());
- typeName[lastDotIdx] = '.';
- if (parentEntry == NULL)
- continue;
- auto parentType = parentEntry->mValue;
- dbgType->mCompileUnit->mGlobalType->mSubTypeList.Remove(dbgType);
- dbgType->mParent = parentType;
- parentType->mSubTypeList.PushBack(dbgType);
- }
- }
- void DbgModule::FindTemplateStr(const char*& name, int& templateNameIdx)
- {
- if (templateNameIdx == 0)
- {
- for (int i = 0; name[i] != 0; i++)
- {
- if (name[i] == '<')
- {
- templateNameIdx = i;
- return;
- }
- }
- templateNameIdx = -1;
- }
- }
- void DbgModule::TempRemoveTemplateStr(const char*& name, int& templateNameIdx)
- {
- if (templateNameIdx == 0)
- FindTemplateStr(name, templateNameIdx);
- if (templateNameIdx == -1)
- return;
- if (!DbgIsStrMutable(name))
- name = DbgDupString(name);
- ((char*)name)[templateNameIdx] = 0;
- }
- void DbgModule::ReplaceTemplateStr(const char*& name, int& templateNameIdx)
- {
- if (templateNameIdx > 0)
- ((char*)name)[templateNameIdx] = '<';
- }
- void DbgModule::MapSubprogram(DbgSubprogram* dbgSubprogram)
- {
- if (dbgSubprogram->mBlock.IsEmpty())
- return;
- int progSize = (int)(dbgSubprogram->mBlock.mHighPC - dbgSubprogram->mBlock.mLowPC);
- for (int progOffset = 0; true; progOffset += DBG_MAX_LOOKBACK)
- {
- int curSize = progSize - progOffset;
- if (curSize <= 0)
- break;
-
- BP_ALLOC_T(DbgSubprogramMapEntry);
- DbgSubprogramMapEntry* subprogramMapEntry = mAlloc.Alloc<DbgSubprogramMapEntry>();
- subprogramMapEntry->mAddress = dbgSubprogram->mBlock.mLowPC + progOffset;
- subprogramMapEntry->mEntry = dbgSubprogram;
- mDebugTarget->mSubprogramMap.Insert(subprogramMapEntry);
- }
- }
- bool DbgModule::ParseDWARF(const uint8*& dataPtr)
- {
- BP_ZONE("ParseDWARF");
- const uint8* data = dataPtr;
- const uint8* startData = mDebugInfoData;
- int dataOfs = (int)(data - mDebugInfoData);
- intptr_target length = GET(int);
-
- DbgModule* linkedModule = GetLinkedModule();
- if (length == -1)
- {
- mIsDwarf64 = true;
- length = (intptr_target)GET(int64);
- }
- else
- mIsDwarf64 = false;
- if (length == 0)
- return false;
- const uint8* dataEnd = data + length;
- int version = GET(short);
- int abbrevOffset = GET(int);
- char pointerSize = GET(char);
- ParseAbbrevData(mDebugAbbrevData + abbrevOffset);
- DbgCompileUnit* compileUnit = new DbgCompileUnit(this);
- mDbgFlavor = DbgFlavor_GNU;
- compileUnit->mDbgModule = this;
- mCompileUnits.push_back(compileUnit);
-
- DbgSubprogram* subProgram = NULL;
-
- //std::map<int, DbgType*> typeMap;
- //std::map<int, DbgSubprogram*> subprogramMap;
- int tagStart = (int)(data - startData);
- int tagEnd = (int)(dataEnd - startData);
- DbgDataMap dataMap(tagStart, tagEnd);
- DataStack dataStack;
- Array<AbstractOriginEntry> abstractOriginReplaceList;
-
- Array<int> deferredArrayDims;
- int startingTypeIdx = (int)linkedModule->mTypes.size();
- while (data < dataEnd)
- {
- gAbbrevNum++;
- const uint8* tagDataStart = data;
- int tagIdx = (int)(tagDataStart - startData);
-
- int abbrevIdx = (int)DecodeULEB128(data);
- const uint8* abbrevData = mDebugAbbrevPtrData[abbrevIdx];
-
- if (abbrevIdx == 0)
- {
- if (deferredArrayDims.size() > 0)
- {
- DbgType* arrType = GetStackTop<DbgType*>(&dataStack);
- BF_ASSERT(arrType->mTypeCode == DbgType_SizedArray);
- arrType->mSize = deferredArrayDims[0]; // Byte count still needs to be multiplied by the underlying type size
-
- DbgType* rootArrType = arrType;
- for (int dimIdx = 0; dimIdx < (int)deferredArrayDims.size() - 1; dimIdx++)
- {
- int dimSize = deferredArrayDims[dimIdx];
-
- DbgType* subArrType = mAlloc.Alloc<DbgType>();
- subArrType->mCompileUnit = compileUnit;
- subArrType->mLanguage = compileUnit->mLanguage;
- subArrType->mTypeIdx = (int)linkedModule->mTypes.size();
- linkedModule->mTypes.push_back(subArrType);
-
- subArrType->mTypeCode = DbgType_SizedArray;
- subArrType->mTypeParam = arrType->mTypeParam;
- subArrType->mSize = deferredArrayDims[dimIdx + 1];
- arrType->mTypeParam = subArrType;
- arrType = subArrType;
- }
- deferredArrayDims.Clear();
- }
- dataStack.pop_back();
- continue;
- }
-
- int entryTag = (int) DecodeULEB128(abbrevData);
- bool hasChildren = GET_FROM(abbrevData, char) == DW_CHILDREN_yes;
- int64 atLowPC = 0;
- int64 atHighPC = 0;
- int64 atRanges = 0;
- bool hasRanges = false;
- const uint8* atFrameBase = NULL;
- int64_t atFrameBaseLength = 0;
- int64 atLocationLen = 0;
- const uint8* atLocationData = 0;
- const char* atProducer = NULL;
- const char* atName = NULL;
- const char* atCompDir = NULL;
- const char* atLinkageName = NULL;
- int64 atConstValue = 0;
- int atDataMemberLocation = 0;
- const uint8* atDataMemberData = NULL;
- int atDeclFile = 0;
- int atDeclLine = 0;
- int atCallFile = 0;
- int atCallLine = 0;
- int atCount = 0;
- int atType = 0;
- int atImport = 0;
- int atInline = 0;
- int atArtificial = 0;
- int atExternal = 0;
- int atByteSize = -1;
- int atEncoding = 0;
- int atSpecification = 0;
- int atObjectPointer = 0;
- int atBitOffset = 0;
- int atBitSize = 0;
- int atAbstractOrigin = 0;
- const uint8* atVirtualLocData = NULL;
- bool atDeclaration = false;
- bool atVirtual = false;
- bool hadConstValue = false;
- bool hadMemberLocation = false;
- bool isOptimized = false;
- DataPair newDataPair;
- while (true)
- {
- int attrName = (int)DecodeULEB128(abbrevData);
- int form = (int)DecodeULEB128(abbrevData);
- if ((attrName == 0) && (form == 0))
- break;
- switch (attrName)
- {
- case DW_AT_sibling:
- ReadValue<char>(data, form);
- break;
- case DW_AT_location:
- atLocationLen = (int)ReadValue<uint>(data, form, dataOfs, &atLocationData, startData);
- break;
- case DW_AT_name:
- atName = ReadValue<const char*>(data, form);
- break;
- case DW_AT_ordering:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_byte_size:
- atByteSize = ReadValue<int>(data, form);
- break;
- case DW_AT_bit_offset:
- atBitOffset = ReadValue<int>(data, form);
- break;
- case DW_AT_bit_size:
- atBitSize = ReadValue<int>(data, form);
- break;
- case DW_AT_stmt_list:
- ReadValue<int64_t>(data, form);
- break;
- case DW_AT_low_pc:
- atLowPC = RemapAddr((addr_target)ReadValue<int64_t>(data, form));
- break;
- case DW_AT_high_pc:
- atHighPC = ReadValue<int64_t>(data, form);
- break;
- case DW_AT_language:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_discr:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_discr_value:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_visibility:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_import:
- atImport = ReadValue<int>(data, form) + dataOfs;
- break;
- case DW_AT_string_length:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_common_reference:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_comp_dir:
- atCompDir = ReadValue<const char*>(data, form);
- break;
- case DW_AT_const_value:
- atConstValue = ReadValue<int64>(data, form);
- hadConstValue = true;
- break;
- case DW_AT_containing_type:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_default_value:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_inline:
- atInline = ReadValue<int>(data, form);
- break;
- case DW_AT_is_optional:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_lower_bound:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_producer:
- atProducer = ReadValue<const char*>(data, form);
- break;
- case DW_AT_prototyped:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_return_addr:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_start_scope:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_bit_stride:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_upper_bound:
- // Lower bound not supported
- atCount = ReadValue<int>(data, form);
- break;
- case DW_AT_abstract_origin:
- atAbstractOrigin = ReadValue<int>(data, form, dataOfs);
- break;
- case DW_AT_accessibility:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_address_class:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_artificial:
- atArtificial = ReadValue<int>(data, form);
- break;
- case DW_AT_base_types:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_calling_convention:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_count:
- atCount = ReadValue<uint>(data, form);
- break;
- case DW_AT_data_member_location:
- if (form == DW_FORM_exprloc)
- {
- atDataMemberLocation = (int)ReadValue<uint>(data, form, dataOfs, &atDataMemberData);
- hadMemberLocation = true;
- }
- else
- {
- atDataMemberLocation = (int)ReadValue<uint>(data, form);
- hadMemberLocation = true;
- }
- break;
- case DW_AT_decl_column:
- /*TODO:*/ ReadValue<uint32>(data, form);
- break;
- case DW_AT_decl_file:
- atDeclFile = ReadValue<uint32>(data, form);
- break;
- case DW_AT_decl_line:
- atDeclLine = ReadValue<uint32>(data, form);
- break;
- case DW_AT_declaration:
- atDeclaration = ReadValue<bool>(data, form);
- break;
- case DW_AT_discr_list:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_encoding:
- atEncoding = ReadValue<int>(data, form);
- break;
- case DW_AT_external:
- atExternal = ReadValue<int>(data, form);
- break;
- case DW_AT_frame_base:
- atFrameBaseLength = (int64_t)ReadValue<uint64_t>(data, form, dataOfs, &atFrameBase);
- break;
- case DW_AT_friend:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_identifier_case:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_macro_info:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_namelist_item:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_priority:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_segment:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_specification:
- atSpecification = ReadValue<int>(data, form, dataOfs);
- break;
- case DW_AT_static_link:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_type:
- atType = ReadValue<int>(data, form, dataOfs);
- break;
- case DW_AT_use_location:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_variable_parameter:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_virtuality:
- atVirtual = ReadValue<int>(data, form) != 0;
- break;
- case DW_AT_vtable_elem_location:
- ReadValue<uint64_t>(data, form, dataOfs, &atVirtualLocData);
- break;
- case DW_AT_allocated:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_associated:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_data_location:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_byte_stride:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_entry_pc:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_use_UTF8:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_extension:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_ranges:
- atRanges = (int)ReadValue<uint>(data, form);
- hasRanges = true;
- break;
- case DW_AT_trampoline:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_call_column:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_call_file:
- atCallFile = ReadValue<uint32>(data, form);
- break;
- case DW_AT_call_line:
- atCallLine = ReadValue<uint32>(data, form);
- break;
- case DW_AT_description:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_binary_scale:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_decimal_scale:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_small:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_decimal_sign:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_digit_count:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_picture_string:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_mutable:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_threads_scaled:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_explicit:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_object_pointer:
- atObjectPointer = ReadValue<int>(data, form);
- break;
- case DW_AT_endianity:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_elemental:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_pure:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_recursive:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_signature:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_main_subprogram:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_data_bit_offset:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_const_expr:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_enum_class:
- /*TODO:*/ ReadValue<int>(data, form);
- break;
- case DW_AT_linkage_name:
- atLinkageName = ReadValue<const char*>(data, form);
- break;
- //
- case DW_AT_MIPS_linkage_name:
- atLinkageName = ReadValue<const char*>(data, form);
- break;
- case DW_AT_APPLE_optimized:
- isOptimized = ReadValue<bool>(data, form);
- break;
- default:
- ReadValue<int>(data, form);
- break;
- }
- }
- if ((hasRanges) && (atLowPC == 0))
- {
- addr_target* rangeData = (addr_target*)(mDebugRangesData + atRanges);
- while (true)
- {
- addr_target lowPC = *(rangeData++);
- if (lowPC == 0)
- break;
- addr_target highPC = *(rangeData++);
- if (compileUnit->mLowPC != (addr_target)-1)
- {
- // These are sometimes relative to the compile unit and sometimes absolute
- if (highPC + compileUnit->mLowPC <= compileUnit->mHighPC)
- {
- lowPC += compileUnit->mLowPC;
- highPC += compileUnit->mLowPC;
- }
- }
- highPC -= lowPC;
- // Select the largest range. We have some cases where some hoisting and such will
- // give us a small inlining aberration much earlier than expected so this ignores that
- if ((int64)highPC > atHighPC)
- {
- atLowPC = lowPC;
- atHighPC = highPC;
- }
- /*if ((atLowPC == 0) || (lowPC < (addr_target)atLowPC))
- atLowPC = lowPC;
- if (highPC > (addr_target)atHighPC)
- atHighPC = highPC;*/
- }
-
- }
- switch (entryTag)
- {
- case DW_TAG_compile_unit:
- {
- newDataPair = MakeDataPair(compileUnit);
- compileUnit->mName = atName;
- compileUnit->mProducer = atProducer;
- if (atCompDir != NULL)
- compileUnit->mCompileDir = atCompDir;
- if (atLowPC != 0)
- {
- compileUnit->mLowPC = (addr_target)atLowPC;
- compileUnit->mHighPC = (addr_target)(atLowPC + atHighPC);
- }
- if (compileUnit->mProducer.IndexOf("Beef") != -1)
- {
- compileUnit->mLanguage = DbgLanguage_Beef;
- }
- else
- {
- compileUnit->mLanguage = DbgLanguage_C;
- }
- compileUnit->mGlobalType->mLanguage = compileUnit->mLanguage;
- }
- break;
- case DW_TAG_imported_module:
- {
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
- DbgType* importType = GetOrCreateType(atImport, dataMap);
- if (parentType != NULL) // Parent type is NULL for Clang DbgModule info
- parentType->mUsingNamespaces.PushFront(importType, &mAlloc);
- }
- break;
- case DW_TAG_inlined_subroutine:
- case DW_TAG_subprogram:
- {
- /*//TODO: This is a test. See if it breaks anything.
- if ((atExternal != 0) && (atLowPC == 0))
- break;*/
- if (atSpecification == 0)
- {
- subProgram = GetOrCreate<DbgSubprogram*>(tagIdx, dataMap);
- subProgram->mCompileUnit = compileUnit;
- subProgram->mVirtual = atVirtual;
- subProgram->mIsOptimized = isOptimized;
- if (atVirtualLocData != NULL)
- {
- const uint8* opPtr = atVirtualLocData;
- if (*(opPtr++) == DW_OP_constu)
- {
- subProgram->mVTableLoc = (int)DecodeSLEB128(opPtr) * sizeof(addr_target);
- }
- }
- //subProgram->mVTableLoc = atVirtualLoc * sizeof(addr_target);
- //SplitName(atName, subProgram->mName, subProgram->mTemplateName);
- subProgram->mName = atName;
- subProgram->mLinkName = atLinkageName;
- if (atAbstractOrigin != NULL)
- {
- DbgSubprogram* originSubProgram = GetOrCreate<DbgSubprogram*>(atAbstractOrigin, dataMap);
- auto abstractOriginEntry = AbstractOriginEntry::Create(DbgSubprogram::ClassType, subProgram, originSubProgram);
- abstractOriginReplaceList.push_back(abstractOriginEntry);
- }
- subProgram->mParentType = GetStackTop<DbgType*>(&dataStack);
- newDataPair = MakeDataPair(subProgram);
-
- //if ((atLinkageName != NULL) && (subProgram->mParentType != NULL))
- //subProgram->mParentType->mDefinedMembersCount++;
- mSubprograms.push_back(subProgram);
- if (subProgram->mParentType != NULL)
- {
- subProgram->mParentType->mMethodList.PushBack(subProgram);
- }
- else
- {
- compileUnit->mGlobalType->mMethodList.PushBack(subProgram);
- }
- }
- else
- {
- subProgram = dataMap.Get<DbgSubprogram*>(atSpecification);
- BF_ASSERT(subProgram != NULL);
- // We remove params form the declaration and re-add the real ones here
- subProgram->mParams.Clear();
- }
-
- newDataPair = MakeDataPair(subProgram);
- DbgBlock* dwBlock = &subProgram->mBlock;
-
- if (atType != 0)
- subProgram->mReturnType = GetOrCreateType(atType, dataMap);
- if (!atDeclaration)
- {
- dwBlock->mLowPC = (addr_target)atLowPC;
- dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
- if (dwBlock->mLowPC != 0)
- {
- compileUnit->mLowPC = std::min(compileUnit->mLowPC, dwBlock->mLowPC);
- compileUnit->mHighPC = std::max(compileUnit->mHighPC, dwBlock->mHighPC);
- }
- if (atObjectPointer != 0)
- subProgram->mHasThis = true;
- subProgram->mFrameBaseLen = (int)atFrameBaseLength;
- subProgram->mFrameBaseData = atFrameBase;
-
- if (atHighPC > 0)
- {
- MapSubprogram(subProgram);
- }
- }
- if (entryTag == DW_TAG_inlined_subroutine)
- {
- DbgSubprogram* parentSubProgram = GetStackLast<DbgSubprogram*>(&dataStack);
- subProgram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
- subProgram->mInlineeInfo->mInlineParent = parentSubProgram;
- subProgram->mInlineeInfo->mRootInliner = parentSubProgram->GetRootInlineParent();
- subProgram->mFrameBaseData = parentSubProgram->mFrameBaseData;
- subProgram->mFrameBaseLen = parentSubProgram->mFrameBaseLen;
- }
-
- //if (subProgram->mParentType != NULL)
- //subProgram->mParentType->mDefinedMembersCount++;
- }
- break;
- case DW_TAG_lexical_block:
- {
- DbgBlock* prevBlock = GetStackTop<DbgBlock*>(&dataStack);
- DbgBlock* dwBlock = mAlloc.Alloc<DbgBlock>();
- if (hasRanges)
- {
- dwBlock->mLowPC = -1;
- dwBlock->mHighPC = (addr_target)atRanges;
- }
- else
- {
- dwBlock->mLowPC = (addr_target)atLowPC;
- dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
- }
- newDataPair = MakeDataPair(dwBlock);
- prevBlock->mSubBlocks.PushBack(dwBlock);
- }
- break;
- case DW_TAG_variable:
- {
- DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
- if (atName && !strncmp(atName, "__asmLines", 10))
- {
- const char* ptr = strchr(atName, '.');
- if (!ptr)
- break;
- int declLine = atDeclLine;
- Array<int> asmLines;
- int curAsmLine = 0;
- int curRunCount = 1; // initial value is starting line, with an assumed run count of 1
- bool parity = true; // starting line is standalone; everything afterwards is in pairs
- while (true)
- {
- ++ptr;
- if (!*ptr)
- break;
- String s;
- if (*ptr == '$')
- {
- ++ptr;
- const char* dollarPtr = strchr(ptr, '$');
- if (!dollarPtr)
- break;
- s = String(ptr, (int)(dollarPtr - ptr));
- ptr = dollarPtr;
- }
- else
- {
- s += *ptr;
- }
-
- //int asmLine = atoi(s.c_str());
- //asmLines.push_back(asmLine);
- const char* sPtr = s.c_str();
- int decodedValue = (int)DecodeULEB32(sPtr);
- if (!parity)
- {
- curRunCount = decodedValue;
- }
- else
- {
- for (int iLine=0; iLine<curRunCount; ++iLine)
- {
- curAsmLine += decodedValue;
- asmLines.push_back(curAsmLine);
- }
- }
- parity = !parity;
- }
- BF_ASSERT(!parity);
- if (dwBlock->mAsmDebugLineMap == NULL)
- {
- mAsmDebugLineMaps.resize(mAsmDebugLineMaps.size() + 1);
- dwBlock->mAsmDebugLineMap = &mAsmDebugLineMaps.back();
- }
- auto mapIter = dwBlock->mAsmDebugLineMap->find(declLine);
- if (mapIter != dwBlock->mAsmDebugLineMap->end())
- {
- auto& dstVec = mapIter->second;
- dstVec.Reserve(dstVec.size() + asmLines.size());
- //dstVec.insert(dstVec.end(), asmLines.begin(), asmLines.end());
- if (!asmLines.IsEmpty())
- dstVec.Insert(dstVec.size(), &asmLines[0], asmLines.size());
- }
- else
- {
- (*dwBlock->mAsmDebugLineMap)[declLine] = std::move(asmLines);
- }
- break;
- }
- bool addToGlobalVarMap = false;
- bool isNewVariable = true;
- DbgVariable* dbgVariable = NULL;
- if (atSpecification != 0)
- {
- //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
- //BF_ASSERT(dbgVariable != NULL);
- dbgVariable = GetOrCreate<DbgVariable*>(atSpecification, dataMap);
- //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
- //BF_ASSERT(dbgVariable != NULL);
- }
- else if (dwBlock != NULL)
- {
- dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
- dwBlock->mVariables.PushBack(dbgVariable);
- }
- else
- {
- DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
- bool wantGlobal = true;
- if (compileUnit->mLanguage == DbgLanguage_Beef)
- {
- // Don't show certain global variables in Beef -- that includes things like VTable data
- if (atName[0] == '_')
- wantGlobal = false;
- }
- if ((dbgType == NULL) && (wantGlobal))
- {
- /*DbgCompileUnit* topCompileUnit = GetStackTop<DbgCompileUnit*>(&dataStack);
- if (topCompileUnit != NULL)
- dbgType = &topCompileUnit->mGlobalType;*/
- dbgType = linkedModule->mMasterCompileUnit->mGlobalType;
- auto foundEntry = mGlobalVarMap.Find(atName);
- if (foundEntry != NULL)
- {
- isNewVariable = false;
- dbgVariable = foundEntry->mValue;
- }
- else
- {
- addToGlobalVarMap = true;
- }
- }
- if (dbgVariable == NULL)
- dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
- dbgVariable->mIsStatic = true;
- //TODO: dbgType can be NULL. This only (apparently?) happens for DW_TAG_inlined_subroutine, which we don't handle right now...
- if (dbgType != NULL)
- {
- BF_ASSERT(dbgType->IsNamespace() || (dbgType->mTypeCode == DbgType_Root));
-
- if (isNewVariable)
- dbgType->mMemberList.PushBack(dbgVariable);
- }
- }
- if (dbgVariable != NULL)
- {
- if (atSpecification == 0)
- {
- dbgVariable->mIsParam = false;
- dbgVariable->mName = atName;
- dbgVariable->mConstValue = atConstValue;
- dbgVariable->mType = GetOrCreateType(atType, dataMap);
- dbgVariable->mIsConst = hadConstValue;
- dbgVariable->mIsStatic = !hadMemberLocation;
- dbgVariable->mIsExtern = atExternal != 0;
- }
- if (atLinkageName != NULL)
- dbgVariable->mLinkName = atLinkageName;
- dbgVariable->mLocationLen = (int8)atLocationLen;
- dbgVariable->mLocationData = atLocationData;
- dbgVariable->mCompileUnit = compileUnit;
- /*if (dbgVariable->mIsStatic && !dbgVariable->mIsConst && (dbgVariable->mLocationLen > 0) && (dbgVariable->mIsExtern))
- {
- DbgAddrType addrType = DbgAddrType_Value;
- //
- addr_target valAddr = mDebugTarget->EvaluateLocation(dbgVariable->mCompileUnit->mDbgModule, NULL, dbgVariable->mLocationData, dbgVariable->mLocationLen, NULL, &addrType);
- if ((addrType == DbgAddrType_Target) && (valAddr != 0))
- {
- dbgVariable->mStaticCachedAddr = valAddr;
- if (dbgVariable->mLinkName != NULL)
- mStaticVariables.push_back(dbgVariable);
- }
- else
- dbgVariable->mIsStatic = false;
- }*/
- // We had to remove the above for hot loading, calculate the mStaticCachedAddr later. Just put into mStaticVariables for now
- mStaticVariables.push_back(dbgVariable);
- if (atAbstractOrigin != NULL)
- {
- DbgVariable* originVariable = GetOrCreate<DbgVariable*>(atAbstractOrigin, dataMap);
- auto abstractOriginEntry = AbstractOriginEntry::Create(DbgVariable::ClassType, dbgVariable, originVariable);
- if (atAbstractOrigin < tagIdx)
- abstractOriginEntry.Replace();
- else
- abstractOriginReplaceList.push_back(abstractOriginEntry);
- }
- else if (dbgVariable->mName == NULL)
- dbgVariable->mName = "_unnamed";
- if (addToGlobalVarMap)
- mGlobalVarMap.Insert(dbgVariable);
- newDataPair = MakeDataPair(dbgVariable);
- }
- }
- break;
- case DW_TAG_formal_parameter:
- {
- DbgSubprogram* dwSubprogram = GetStackTop<DbgSubprogram*>(&dataStack);
- if (dwSubprogram == NULL)
- {
- if ((atName == NULL) && (atAbstractOrigin == 0))
- {
- DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
- if ((dbgType == NULL) || (dbgType->mTypeCode != DbgType_Subroutine))
- break;
- //TODO: Add params to subroutine type
- break;
- }
- break;
- }
-
- if ((dwSubprogram->mParams.IsEmpty()) && (dwSubprogram->mParentType != 0))
- dwSubprogram->mParentType->mMethodsWithParamsCount++;
- //DbgVariable* dbgVariable = mAlloc.Alloc<DbgVariable>();
- DbgVariable* dwVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
- dwSubprogram->mParams.PushBack(dwVariable);
- if (atArtificial != 0)
- {
- dwSubprogram->mHasThis = true;
- if (atName == NULL)
- atName = "this";
- }
- dwVariable->mCompileUnit = compileUnit;
- dwVariable->mIsParam = true;
- dwVariable->mName = atName;
- dwVariable->mLocationLen = (int)atLocationLen;
- dwVariable->mLocationData = atLocationData;
- dwVariable->mType = GetOrCreateType(atType, dataMap);
- if (atAbstractOrigin != 0)
- {
-
- }
- }
- break;
- case DW_TAG_enumerator:
- {
- DbgVariable* member = mAlloc.Alloc<DbgVariable>();
- member->mCompileUnit = compileUnit;
- member->mConstValue = atConstValue;
- member->mName = atName;
- member->mIsStatic = true;
- member->mIsConst = true;
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
- parentType->mMemberList.PushBack(member);
- member->mMemberOffset = atDataMemberLocation;
- //member->mType = parentType->mTypeParam;
- member->mType = parentType;
-
- // Insert into parent's namespace
- auto prevTop = dataStack.back();
- dataStack.pop_back();
- DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
- dataStack.push_back(prevTop);
- if (dwBlock != NULL)
- {
- DbgVariable* dwVariable = mAlloc.Alloc<DbgVariable>();
- dwBlock->mVariables.PushBack(dwVariable);
- if (atSpecification == 0)
- {
- dwVariable->mIsParam = false;
- dwVariable->mName = atName;
- dwVariable->mConstValue = atConstValue;
- dwVariable->mType = parentType->mTypeParam;
- dwVariable->mIsConst = hadConstValue;
- dwVariable->mIsStatic = !hadMemberLocation;
- }
- dwVariable->mLocationLen = (int)atLocationLen;
- dwVariable->mLocationData = atLocationData;
- dwVariable->mCompileUnit = compileUnit;
- BF_ASSERT(dwVariable->mName != 0);
- newDataPair = MakeDataPair(dwVariable);
- }
- }
- break;
- /*case DW_TAG_subrange_type:
- {
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
- parentType->mArraySize = atUpperBound;
- }
- break;*/
- case DW_TAG_inheritance:
- {
- DbgType* derivedType = GetStackTop<DbgType*>(&dataStack);
- DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
- baseTypeEntry->mBaseType = GetOrCreateType(atType, dataMap);
- if (atDataMemberData != NULL)
- {
- bool foundVirtOffset = false;
- const uint8* opPtr = atDataMemberData;
- if (*(opPtr++) == DW_OP_dup)
- {
- if (*(opPtr++) == DW_OP_deref)
- {
- if (*(opPtr++) == DW_OP_constu)
- {
- baseTypeEntry->mVTableOffset = (int)DecodeSLEB128(opPtr) / sizeof(int32);
- foundVirtOffset = true;
- if (*(opPtr++) == DW_OP_minus)
- baseTypeEntry->mVTableOffset = -baseTypeEntry->mVTableOffset;
- }
- }
- }
-
- BF_ASSERT(foundVirtOffset);
- }
- else
- baseTypeEntry->mThisOffset = atDataMemberLocation;
- derivedType->mBaseTypes.PushBack(baseTypeEntry);
- }
- break;
- case DW_TAG_member:
- {
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
- if ((atName != NULL) && (strncmp(atName, "_vptr$", 6) == 0))
- {
- parentType->mHasVTable = true;
- break;
- }
- //DbgVariable* member = mAlloc.Alloc<DbgVariable>();
- DbgVariable* member = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
- member->mIsMember = true;
- member->mCompileUnit = compileUnit;
- member->mName = atName;
- member->mType = GetOrCreateType(atType, dataMap);
- member->mConstValue = atConstValue;
- member->mIsConst = hadConstValue;
- member->mIsStatic = !hadMemberLocation;
- member->mBitSize = atBitSize;
- member->mBitOffset = atBitOffset;
- member->mIsExtern = atExternal != 0;
-
- parentType->mMemberList.PushBack(member);
- member->mMemberOffset = atDataMemberLocation;
-
- if ((member->mIsStatic) && (!member->mIsConst))
- parentType->mHasStaticMembers = true;
- /*if ((member->mIsStatic) && (!member->mIsConst))
- mStaticVariables.push_back(member);*/
- newDataPair = MakeDataPair(member);
- //dataMap.Set(tagIdx, member);
- }
- break;
- case DW_TAG_subrange_type:
- {
- int typeIdx = (int)(tagDataStart - startData);
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
- int arrSize = atCount;
- deferredArrayDims.push_back(arrSize);
- }
- break;
- case DW_TAG_namespace:
- case DW_TAG_const_type:
- case DW_TAG_base_type:
- case DW_TAG_pointer_type:
- case DW_TAG_ptr_to_member_type:
- case DW_TAG_array_type:
- case DW_TAG_reference_type:
- case DW_TAG_rvalue_reference_type:
- case DW_TAG_unspecified_type:
- case DW_TAG_class_type:
- case DW_TAG_enumeration_type:
- case DW_TAG_structure_type:
- case DW_TAG_union_type:
- case DW_TAG_typedef:
- case DW_TAG_volatile_type:
- case DW_TAG_subroutine_type:
- //case DW_TAG_subrange_type:
- case DW_TAG_restrict_type:
- {
- int typeIdx = (int)(tagDataStart - startData);
- DbgType* dbgType = GetOrCreateType(typeIdx, dataMap);
- const char* nameSep = (compileUnit->mLanguage == DbgLanguage_Beef) ? "." : "::";
- if ((atName != NULL) &&
- ((entryTag == DW_TAG_structure_type) || (entryTag == DW_TAG_class_type) ||
- (entryTag == DW_TAG_typedef) || (entryTag == DW_TAG_union_type) || (entryTag == DW_TAG_enumeration_type) ||
- (entryTag == DW_TAG_namespace)))
- {
- BF_ASSERT(dbgType->mTypeCode == DbgType_Null);
- DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
-
- if (parentType != NULL)
- {
- dbgType->mParent = parentType;
- dbgType->mParent->mSubTypeList.PushBack(dbgType);
- /*if (dbgType->mParent->mName != NULL)
- {
- if (atName == NULL)
- {
- dbgType->mName = dbgType->mParent->mName; // Extend from name of parent if we're anonymous
- }
- else
- {
- int nameSepLen = strlen(nameSep);
- int parentNameLen = strlen(dbgType->mParent->mName);
- int nameLen = strlen(atName);
- char* name = (char*)mAlloc.AllocBytes(parentNameLen + nameSepLen + nameLen + 1);
- memcpy(name, dbgType->mParent->mName, parentNameLen);
- memcpy(name + parentNameLen, nameSep, nameSepLen);
- memcpy(name + parentNameLen + nameSepLen, atName, nameLen);
- dbgType->mName = name;
- }
- }*/
- }
- else
- {
- // Add to global subtype list but don't set dbgType->mParent
- compileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
- }
- }
-
- const char* useName = atName;
- /*if ((useName != NULL) && (strcmp(useName, "@") == 0))
- useName = NULL;*/
- dbgType->mCompileUnit = compileUnit;
- dbgType->mLanguage = compileUnit->mLanguage;
- //SplitName(atName, dbgType->mTypeName, dbgType->mTemplateParams);
- dbgType->mName = useName;
- if (dbgType->mTypeName == NULL)
- dbgType->mTypeName = useName;
- //if (dbgType->mName == NULL)
- //dbgType->mName = atName;
- int parentNameLen = ((dbgType->mParent != NULL) && (dbgType->mParent->mName != NULL)) ? (int)strlen(dbgType->mParent->mName) : 0;
- int typeNameLen = (dbgType->mTypeName != NULL) ? (int)strlen(dbgType->mTypeName) : 0;
- //int templateParamsLen = (dbgType->mTemplateParams != NULL) ? strlen(dbgType->mTemplateParams) : 0;
- if ((parentNameLen != 0) /*&& (templateParamsLen == 0)*/)
- {
- int nameSepLen = (int)strlen(nameSep);
-
- int nameLen = parentNameLen + typeNameLen /*+ templateParamsLen*/;
- if ((parentNameLen > 0) && (nameLen > 0))
- nameLen += nameSepLen;
- char* namePtr = (char*)mAlloc.AllocBytes(nameLen + 1, "DWARF");
- dbgType->mName = namePtr;
- if (parentNameLen > 0)
- {
- memcpy(namePtr, dbgType->mParent->mName, parentNameLen);
- namePtr += parentNameLen;
- if (nameLen > 0)
- {
- memcpy(namePtr, nameSep, nameSepLen);
- namePtr += nameSepLen;
- }
- }
- if (nameLen > 0)
- {
- memcpy(namePtr, useName, typeNameLen);
- namePtr += typeNameLen;
- }
- /*if (templateParamsLen > 0)
- {
- memcpy(namePtr, dbgType->mTemplateParams, templateParamsLen);
- namePtr += templateParamsLen;
- }*/
- }
-
- dbgType->mTypeCode = DbgType_Null;
- dbgType->mIsDeclaration = atDeclaration;
- if (atByteSize != -1)
- {
- dbgType->mSize = atByteSize;
- dbgType->mSizeCalculated = true;
- }
-
- switch (entryTag)
- {
- case DW_TAG_base_type:
- // Types that may do fallover to int/uints on size mismatch
- switch (atEncoding)
- {
- case DW_ATE_UTF:
- if (atByteSize == 1)
- dbgType->mTypeCode = DbgType_Utf8;
- else if (atByteSize == 2)
- dbgType->mTypeCode = DbgType_Utf16;
- else
- dbgType->mTypeCode = DbgType_Utf32;
- break;
- case DW_ATE_signed_char:
- if (atByteSize == 1)
- dbgType->mTypeCode = DbgType_SChar;
- else if (atByteSize == 2)
- dbgType->mTypeCode = DbgType_SChar16;
- else if (atByteSize == 4)
- dbgType->mTypeCode = DbgType_SChar32;
- else
- atEncoding = DW_ATE_signed;
- break;
- case DW_ATE_unsigned_char:
- if (atByteSize == 1)
- dbgType->mTypeCode = DbgType_UChar;
- else if (atByteSize == 2)
- dbgType->mTypeCode = DbgType_UChar16;
- else if (atByteSize == 4)
- dbgType->mTypeCode = DbgType_UChar32;
- atEncoding = DW_ATE_unsigned;
- break;
- case DW_ATE_boolean:
- if (atByteSize == 1)
- dbgType->mTypeCode = DbgType_Bool;
- else
- atEncoding = DW_ATE_unsigned;
- break;
- }
- if (dbgType->mTypeCode == DbgType_Null)
- {
- switch (atEncoding)
- {
- case DW_ATE_address:
- if (atByteSize == 0)
- dbgType->mTypeCode = DbgType_Void;
- break;
- case DW_ATE_boolean:
- if (atByteSize == 1)
- {
- dbgType->mTypeCode = DbgType_Bool;
- break;
- }
- //Fall through
- case DW_ATE_signed:
- switch (atByteSize)
- {
- case 1:
- dbgType->mTypeCode = DbgType_i8;
- break;
- case 2:
- dbgType->mTypeCode = DbgType_i16;
- break;
- case 4:
- dbgType->mTypeCode = DbgType_i32;
- break;
- case 8:
- dbgType->mTypeCode = DbgType_i64;
- break;
- case 16:
- dbgType->mTypeCode = DbgType_i128;
- break;
- }
- break;
- case DW_ATE_unsigned:
- switch (atByteSize)
- {
- case 1:
- dbgType->mTypeCode = DbgType_u8;
- break;
- case 2:
- if ((atName != NULL) && (strcmp(atName, "wchar_t") == 0))
- dbgType->mTypeCode = DbgType_UChar16;
- else
- dbgType->mTypeCode = DbgType_u16;
- break;
- case 4:
- dbgType->mTypeCode = DbgType_u32;
- break;
- case 8:
- dbgType->mTypeCode = DbgType_u64;
- break;
- case 16:
- dbgType->mTypeCode = DbgType_u128;
- break;
- }
- break;
- case DW_ATE_float:
- if (atByteSize == 4)
- dbgType->mTypeCode = DbgType_Single;
- else if (atByteSize == 8)
- dbgType->mTypeCode = DbgType_Double;
- else if (atByteSize == 12)
- dbgType->mTypeCode = DbgType_Float96;
- else if (atByteSize == 16)
- dbgType->mTypeCode = DbgType_Float128;
- break;
- case DW_ATE_complex_float:
- if (atByteSize == 8)
- dbgType->mTypeCode = DbgType_ComplexFloat;
- else if (atByteSize == 16)
- dbgType->mTypeCode = DbgType_ComplexDouble;
- else if (atByteSize == 24)
- dbgType->mTypeCode = DbgType_ComplexDouble96;
- else if (atByteSize == 32)
- dbgType->mTypeCode = DbgType_ComplexDouble128;
- break;
- default:
- BF_FATAL("Unknown DW_ATE type");
- break;
- }
- }
- break;
- case DW_TAG_enumeration_type: //TODO: Handle these differently
- dbgType->mTypeCode = DbgType_Enum;
- dbgType->mTypeParam = mAlloc.Alloc<DbgType>();
- if (atByteSize == 8)
- dbgType->mTypeParam->mTypeCode = DbgType_i64;
- else if (atByteSize == 4)
- dbgType->mTypeParam->mTypeCode = DbgType_i32;
- else if (atByteSize == 2)
- dbgType->mTypeParam->mTypeCode = DbgType_i16;
- else if (atByteSize == 1)
- dbgType->mTypeParam->mTypeCode = DbgType_i8;
- else
- {
- BF_DBG_FATAL("Invalid enum type");
- }
- break;
- case DW_TAG_namespace:
- dbgType->mTypeCode = DbgType_Namespace;
- break;
- case DW_TAG_const_type:
- dbgType->mTypeCode = DbgType_Const;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_rvalue_reference_type:
- dbgType->mTypeCode = DbgType_RValueReference;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_unspecified_type:
- dbgType->mTypeCode = DbgType_Unspecified;
- dbgType->mTypeName = atName;
- break;
- case DW_TAG_reference_type:
- dbgType->mTypeCode = DbgType_Ref;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_pointer_type:
- dbgType->mTypeCode = DbgType_Ptr;
- dbgType->mSize = sizeof(addr_target);
- dbgType->mSizeCalculated = true;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- if (dbgType->mTypeParam != NULL)
- dbgType->mTypeParam->mPtrType = dbgType;
- break;
- case DW_TAG_ptr_to_member_type:
- dbgType->mTypeCode = DbgType_PtrToMember;
- dbgType->mSize = sizeof(addr_target);
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- if (dbgType->mTypeParam != NULL)
- dbgType->mTypeParam->mPtrType = dbgType;
- break;
- case DW_TAG_array_type:
- dbgType->mTypeCode = DbgType_SizedArray;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_structure_type:
- dbgType->mTypeCode = DbgType_Struct;
- break;
- case DW_TAG_class_type:
- dbgType->mTypeCode = DbgType_Class;
- break;
- case DW_TAG_union_type:
- dbgType->mTypeCode = DbgType_Union;
- break;
- case DW_TAG_typedef:
- dbgType->mTypeCode = DbgType_TypeDef;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_volatile_type:
- dbgType->mTypeCode = DbgType_Volatile;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- case DW_TAG_subroutine_type:
- dbgType->mTypeCode = DbgType_Subroutine;
- if (atType != 0) // Return value
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
- break;
- case DW_TAG_restrict_type:
- dbgType->mTypeCode = DbgType_Restrict;
- dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
- break;
- }
-
- newDataPair = MakeDataPair(dbgType);
- }
- break;
- }
-
- if (hasChildren)
- dataStack.push_back(newDataPair);
- }
- for (auto& abstractOriginEntry : abstractOriginReplaceList)
- abstractOriginEntry.Replace();
- GetLinkedModule()->MapTypes(startingTypeIdx);
-
- dataPtr = dataEnd;
- return true;
- }
- void DbgModule::ParseDebugFrameData()
- {
- BP_ZONE("ParseDebugFrameData");
- const uint8* data = mDebugFrameData;
- if (data == NULL)
- return;
- mParsedFrameDescriptors = true;
- Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
- while (true)
- {
- addr_target relSectionAddr = (addr_target)(data - mDebugFrameData);
- int length = GET(int);
- if (length == 0)
- break;
-
- const uint8* dataEnd = data + length;
- int cieID = GET(int);
-
- if (cieID < 0)
- {
- BP_ALLOC_T(DwCommonFrameDescriptor);
- DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
- char version = GET(char);
- commonFrameDescriptor->mDbgModule = this;
- commonFrameDescriptor->mAugmentation = DataGetString(data);
-
- if (version >= 4)
- {
- commonFrameDescriptor->mPointerSize = GET(int8);
- commonFrameDescriptor->mSegmentSize = GET(int8);
- }
- commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
- commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
- commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
- commonFrameDescriptor->mInstData = data;
- commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
- mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
- if (version < 3)
- commonFrameDescriptorMap[relSectionAddr] = commonFrameDescriptor;
- else
- commonFrameDescriptorMap[mDebugFrameAddress + relSectionAddr] = commonFrameDescriptor;
- }
- else
- {
- addr_target lowPC = GET(addr_target);
- addr_target highPC = lowPC + GET(addr_target);
- DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)cieID];
- BF_ASSERT(commonFrameDescriptor != NULL);
- typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
- auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
- auto frameDescriptor = &resultPair.first->second;
- //frameDescriptor->
- frameDescriptor->mLowPC = lowPC;
- frameDescriptor->mHighPC = highPC;
- frameDescriptor->mInstData = data;
- frameDescriptor->mInstLen = (int)(dataEnd - data);
- frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
- }
- data = dataEnd;
- }
- }
- void DbgModule::ParseEHFrameData()
- {
- const uint8* data = mEHFrameData;
- if (data == NULL)
- return;
- Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
- while (true)
- {
- addr_target sectionAddress = (addr_target)(data - mEHFrameData);
- int length = GET(int);
- if (length == 0)
- break;
- const uint8* dataEnd = data + length;
- int cieID = GET(int);
- if (cieID <= 0)
- {
- BP_ALLOC_T(DwCommonFrameDescriptor);
- DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
- char version = GET(char);
- const char* augmentation = DataGetString(data);
- commonFrameDescriptor->mDbgModule = this;
- commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
- commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
- commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
- commonFrameDescriptor->mAugmentation = augmentation;
-
- if (*augmentation == 'z')
- {
- ++augmentation;
- int augLen = (int)DecodeULEB128(data);
- commonFrameDescriptor->mAugmentationLength = augLen;
- const uint8* augEnd = data + augLen;
- while (*augmentation != '\0')
- {
- if (*augmentation == 'R')
- commonFrameDescriptor->mAddressPointerEncoding = (int) GET(uint8);
- else if (*augmentation == 'P')
- {
- int encodingType = GET(uint8);
- BF_ASSERT(encodingType == 0);
- commonFrameDescriptor->mLSDARoutine = GET(addr_target);
- }
- else if (*augmentation == 'L')
- commonFrameDescriptor->mLSDAPointerEncodingFDE = GET(uint8);
- else if (*augmentation == 'S')
- {
- // mIsSignalHandler - on return from stack frame, CFA is before next instruction rather than after it
- }
- else
- BF_FATAL("Unknown CIE augmentation");
- ++augmentation;
- }
- data = augEnd;
- }
- commonFrameDescriptor->mInstData = data;
- commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
-
- mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
- commonFrameDescriptorMap[sectionAddress] = commonFrameDescriptor;
- }
- else
- {
- int ciePos = (int)(sectionAddress - cieID) + 4;
- DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)ciePos];
- addr_target lowPC;
- addr_target highPC;
-
- if (commonFrameDescriptor->mAddressPointerEncoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4))
- {
- lowPC = GET(int);
- lowPC += mEHFrameAddress + sectionAddress + 8;
- highPC = lowPC + GET(int);
- }
- else
- {
- lowPC = GET(int);
- highPC = lowPC + GET(int);
- }
- typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
- auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
- auto frameDescriptor = &resultPair.first->second;
- frameDescriptor->mLSDARoutine = commonFrameDescriptor->mLSDARoutine;
- const char* augmentation = commonFrameDescriptor->mAugmentation;
- if (*augmentation == 'z')
- {
- int augLen = GET(uint8);
- const uint8* augEnd = data + augLen;
- ++augmentation;
- while (*augmentation != '\0')
- {
- if (*augmentation == 'R')
- {
- }
- else if (*augmentation == 'P')
- {
- }
- else if (*augmentation == 'L')
- {
- BF_ASSERT(commonFrameDescriptor->mLSDAPointerEncodingFDE == 0);
- frameDescriptor->mLSDARoutine = GET(addr_target);
- }
- else if (*augmentation == 'S')
- {
- }
- else
- BF_FATAL("Unknown CIE augmentation");
- augmentation++;
- }
- data = augEnd;
- }
-
- frameDescriptor->mLowPC = lowPC;
- frameDescriptor->mHighPC = highPC;
- frameDescriptor->mInstData = data;
- frameDescriptor->mInstLen = (int)(dataEnd - data);
- frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
- }
- data = dataEnd;
- }
- }
- void DbgModule::FlushLineData(DbgSubprogram* curSubprogram, std::list<DbgLineData>& queuedLineData)
- {
-
- }
- DbgSrcFile* DbgModule::AddSrcFile(DbgCompileUnit* compileUnit, const String& srcFilePath)
- {
- DbgSrcFile* dwSrcFile = mDebugTarget->AddSrcFile(srcFilePath);
- if (compileUnit != NULL)
- {
- DbgSrcFileReference srcFileRef;
- srcFileRef.mSrcFile = dwSrcFile;
- srcFileRef.mCompileUnit = compileUnit;
- compileUnit->mSrcFileRefs.push_back(srcFileRef);
- }
- return dwSrcFile;
- }
- bool DbgModule::ParseDebugLineInfo(const uint8*& dataPtr, int compileUnitIdx)
- {
- BP_ZONE("ParseDebugLineInfo");
-
- const uint8* data = dataPtr;
- const int startOffset = (int)(data - mDebugLineData);
- int length = GET(int);
- if (length == 0)
- return false;
- DbgCompileUnit* dwCompileUnit = mCompileUnits[compileUnitIdx];
- const uint8* dataEnd = data + length;
- short version = GET(short);
- int headerLength = GET(int);
- char minimumInstructionLength = GET(char);
- int maximumOperationsPerInstruction = 1;
- char defaultIsStmt = GET(char);
- char lineBase = GET(char);
- char lineRange = GET(char);
- char opcodeBase = GET(char);
- for (int i = 0; i < opcodeBase - 1; i++)
- {
- char standardOpcodeLengths = GET(char);
- }
- Array<const char*> directoryNames;
- while (true)
- {
- const char* name = DataGetString(data);
- if (name[0] == 0)
- break;
- directoryNames.push_back(name);
- }
- DbgSrcFileReference* dwSrcFileRef = NULL;
- HashSet<String> foundPathSet;
- int curFileIdx = 0;
- DbgSubprogram* curSubprogram = NULL;
-
- #define ADD_LINEDATA(lineData) \
- lineBuilder.Add(dwCompileUnit, lineData, dwSrcFileRef->mSrcFile, NULL);
- while (true)
- {
- const char* path = DataGetString(data);
- if (path[0] == 0)
- break;
- int directoryIdx = (int)DecodeULEB128(data);
- int lastModificationTime = (int)DecodeULEB128(data);
- int fileLength = (int)DecodeULEB128(data);
- String filePath;
- if (directoryIdx > 0)
- filePath = String(directoryNames[directoryIdx - 1]) + "/";
- filePath += path;
- filePath = GetAbsPath(filePath, dwCompileUnit->mCompileDir);
- AddSrcFile(dwCompileUnit, filePath.c_str());
- }
- if (dwCompileUnit->mSrcFileRefs.size() > 0)
- dwSrcFileRef = &dwCompileUnit->mSrcFileRefs.front();
-
- DbgLineDataBuilder lineBuilder(this);
- bool queuedPostPrologue = false;
- DbgLineDataState dwLineData;
- dwLineData.mLine = 0;
- dwLineData.mRelAddress = 0;
- dwLineData.mOpIndex = 0;
- dwLineData.mBasicBlock = false;
- dwLineData.mDiscriminator = 0;
- dwLineData.mIsStmt = defaultIsStmt != 0;
- dwLineData.mIsa = 0;
- dwLineData.mColumn = -2;
- while (data < dataEnd)
- {
- uint8_t opcode = GET(uint8_t);
- switch (opcode)
- {
- case DW_LNS_extended_op:
- {
- int len = (int)DecodeULEB128(data);
- uint8_t exOpcode = GET(uint8_t);
- switch (exOpcode)
- {
- case DW_LNE_end_sequence:
- {
- ADD_LINEDATA(dwLineData);
-
- dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[0];
- dwLineData.mLine = 0;
- dwLineData.mRelAddress = 0;
- dwLineData.mOpIndex = 0;
- dwLineData.mBasicBlock = false;
- dwLineData.mDiscriminator = 0;
- dwLineData.mIsStmt = defaultIsStmt != 0;
- dwLineData.mIsa = 0;
- dwLineData.mColumn = -2;
- }
- break;
- case DW_LNE_set_address:
- dwLineData.mRelAddress = (uint32)(RemapAddr(GET(addr_target)) - mImageBase);
- break;
- case DW_LNE_define_file:
- {
- const char* path = DataGetString(data);
- int directoryIdx = (int)DecodeULEB128(data);
- int lastModificationTime = (int)DecodeULEB128(data);
- int fileLength = (int)DecodeULEB128(data);
- }
- break;
- case DW_LNE_set_discriminator:
- dwLineData.mDiscriminator = (int)DecodeULEB128(data);
- break;
- }
- }
- break;
- case DW_LNS_copy:
- ADD_LINEDATA(dwLineData);
-
- dwLineData.mDiscriminator = 0;
- dwLineData.mBasicBlock = false;
- break;
- case DW_LNS_advance_pc:
- {
- int advance = (int)DecodeULEB128(data);
- dwLineData.mRelAddress += advance;
- // How to advance opCode addr?
- }
- break;
- case DW_LNS_advance_line:
- {
- int advance = (int)DecodeSLEB128(data);
- dwLineData.mLine += advance;
- }
- break;
- case DW_LNS_set_file:
- {
- curFileIdx = (int)DecodeULEB128(data) - 1;
- dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[curFileIdx];
- //dwLineData.mSrcFileRef = dwSrcFileRef;
- }
- break;
- case DW_LNS_set_column:
- {
- dwLineData.mColumn = (int)DecodeULEB128(data) - 1;
- }
- break;
- case DW_LNS_negate_stmt:
- {
- dwLineData.mIsStmt = !dwLineData.mIsStmt;
- }
- break;
- case DW_LNS_set_basic_block:
- {
- dwLineData.mBasicBlock = true;
- }
- break;
- case DW_LNS_const_add_pc:
- {
- int adjustedOpcode = 255 - opcodeBase;
- int opAdvance = adjustedOpcode / lineRange;
- uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
- int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
-
- dwLineData.mRelAddress = newAddress;
- dwLineData.mOpIndex = newOpIndex;
- }
- break;
- case DW_LNS_fixed_advance_pc:
- {
- uint16_t advance = GET(uint16_t);
- dwLineData.mRelAddress += advance;
- dwLineData.mOpIndex = 0;
- }
- break;
- case DW_LNS_set_prologue_end:
- {
- queuedPostPrologue = true;
- }
- break;
- case DW_LNS_set_epilogue_begin:
- {
- dwLineData.mColumn = -2;
- }
- break;
- case DW_LNS_set_isa:
- {
- dwLineData.mIsa = (int)DecodeULEB128(data);
- }
- break;
- default:
- {
- // Special opcode
- int adjustedOpcode = opcode - opcodeBase;
- int opAdvance = adjustedOpcode / lineRange;
- uint32 oldAddress = dwLineData.mRelAddress;
- uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
- int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
- int lineIncrement = lineBase + (adjustedOpcode % lineRange);
- dwLineData.mLine += lineIncrement;
- dwLineData.mRelAddress = newAddress;
- dwLineData.mOpIndex = newOpIndex;
- DbgLineData* lastLineData = NULL;
-
- if ((newAddress == oldAddress) && (queuedPostPrologue) && (curSubprogram != NULL) && (curSubprogram->mBlock.mLowPC == newAddress))
- {
- // Adjust this line later
- ADD_LINEDATA(dwLineData);
- }
- queuedPostPrologue = false;
-
- }
- break;
- }
- }
- lineBuilder.Commit();
- dataPtr = data;
- return true;
- }
- addr_target DbgModule::GetHotTargetAddress(DbgHotTargetSection* hotTargetSection)
- {
- if ((hotTargetSection->mTargetSectionAddr == NULL) && (hotTargetSection->mDataSize > 0))
- {
- if (hotTargetSection->mNoTargetAlloc)
- return 0;
- hotTargetSection->mTargetSectionAddr = mDebugger->AllocHotTargetMemory(hotTargetSection->mDataSize, hotTargetSection->mCanExecute, hotTargetSection->mCanWrite, &hotTargetSection->mTargetSectionSize);
- hotTargetSection->mImageOffset = (int)mImageSize;
- if (mImageBase == NULL)
- {
- mImageBase = hotTargetSection->mTargetSectionAddr;
- mOrigImageData->mAddr = mImageBase;
- }
- mImageSize += hotTargetSection->mTargetSectionSize;
- /*if (mExceptionData == hotTargetSection->mData)
- mExceptionDataRVA = (addr_target)(hotTargetSection->mTargetSectionAddr - mImageBase);*/
- }
- return hotTargetSection->mTargetSectionAddr;
- }
- uint8* DbgModule::GetHotTargetData(addr_target address)
- {
- for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
- {
- if (mHotTargetSections[sectNum] != NULL)
- {
- DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
- if ((address >= hotTargetSection->mTargetSectionAddr) && (address < hotTargetSection->mTargetSectionAddr + hotTargetSection->mTargetSectionSize))
- {
- return hotTargetSection->mData + (address - hotTargetSection->mTargetSectionAddr);
- }
- }
- }
- return NULL;
- }
- void DbgModule::DoReloc(DbgHotTargetSection* hotTargetSection, COFFRelocation& coffReloc, addr_target resolvedSymbolAddr, PE_SymInfo* symInfo)
- {
- #ifdef BF_DBG_32
- if (coffReloc.mType == IMAGE_REL_I386_DIR32)
- {
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
- }
- else if (coffReloc.mType == IMAGE_REL_I386_DIR32NB)
- {
- GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
- // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
- // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
- }
- else if (coffReloc.mType == IMAGE_REL_I386_REL32)
- {
- addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr - myAddr - sizeof(int32);
- }
- else if (coffReloc.mType == IMAGE_REL_I386_SECTION)
- {
- // auto linkedModule = GetLinkedModule();
- // addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
- // int* encodingPtr = NULL;
- // if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
- // {
- // *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
- // linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
- // }
- // *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
- *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0;
- }
- else if (coffReloc.mType == IMAGE_REL_I386_SECREL)
- {
- //*(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += symInfo->mValue;
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
- }
- else
- {
- BF_ASSERT(0=="Invalid COFF reloc type");
- }
- #else
- // CodeView uses SECTION:SECREL locations, and we just want to find a mapping such that
- // COFF::GetSectionAddr can map it to the 64-bit address. We do this by encoding the
- // lower 31 bits in the SECREL (allowing a 31-bit offset at the destination as well)
- // and then we use a 15-bit key to map the upper bits
- if (coffReloc.mType == IMAGE_REL_AMD64_REL32)
- {
- addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
- intptr_target addrOffset = resolvedSymbolAddr - myAddr - sizeof(int32);
- BF_ASSERT((int64)(int32)addrOffset == addrOffset);
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (int32)addrOffset;
- }
- else if (coffReloc.mType == IMAGE_REL_AMD64_SECTION)
- {
- /*if (symInfo != NULL)
- {
- *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = symInfo->mSectionNum;
- }
- else*/
- {
- auto linkedModule = GetLinkedModule();
- addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
- /*auto pair = linkedModule->mSecRelEncodingMap.insert(std::make_pair(mappedAddr, (int)linkedModule->mSecRelEncodingMap.size()));
- if (pair.second)
- linkedModule->mSecRelEncodingVec.push_back(mappedAddr);*/
- int* encodingPtr = NULL;
- if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
- {
- *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
- linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
- }
- //*(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | pair.first->second;
- *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
- }
- }
- else if (coffReloc.mType == IMAGE_REL_AMD64_SECREL)
- {
- auto linkedModule = GetLinkedModule();
- if ((resolvedSymbolAddr >= linkedModule->mTLSAddr) && (resolvedSymbolAddr < linkedModule->mTLSAddr + linkedModule->mTLSSize))
- {
- // Make relative to actual TLS data
- resolvedSymbolAddr -= linkedModule->mTLSAddr;
- }
-
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr & 0x7FFFFFF);
-
- }
- else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR64)
- {
- *(uint64*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
- }
- else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB)
- {
- GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
- // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
- // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
- *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
- //*(int32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += secRelAddr;
- }
- else
- {
- BF_ASSERT(0=="Invalid COFF reloc type");
- }
- #endif
- }
- bool DbgModule::IsHotSwapPreserve(const String& name)
- {
- // We have different rules for overwriting symbols in DWARF vs CodeView
- // Since MS mangling includes return types, we know that a type change of a static
- // member will mangle to a new name whereas with DWARF we DO want a new
- // address if the type changes but we can't tell that based on the mangle alone,
- // thus the reliance on the side table of mStaticVariables. We still do need
- // to determine whether the symbol is data (and thus we do preserve) or a method
- // (in which case we don't)
- if ((mDbgFlavor == DbgFlavor_MS) && (BfDemangler::IsData(name)))
- {
- if ((!name.StartsWith("?")) && (name.Contains("sBfTypeData"))) // We DO need to replace the fields/methods/etc but not the base sBfTypeData
- return false;
- if (name.StartsWith("?bf_hs_replace_"))
- return false;
- return true;
- }
- const char* prefix = "bf_hs_preserve@";
- return strncmp(name.c_str(), prefix, strlen(prefix)) == 0;
- }
- void DbgModule::ParseHotTargetSections(DataStream* stream, addr_target* resolvedSymbolAddrs)
- {
- auto mainModule = mDebugTarget->mTargetBinary;
- mainModule->ParseSymbolData();
- String name;
- for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
- {
- if (mHotTargetSections[sectNum] != NULL)
- {
- DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
- stream->SetPos(hotTargetSection->mPointerToRelocations);
- for (int relocIdx = 0; relocIdx < hotTargetSection->mNumberOfRelocations; relocIdx++)
- {
- COFFRelocation coffReloc;
- stream->Read(&coffReloc, sizeof(COFFRelocation));
- PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[coffReloc.mSymbolTableIndex * 18];
- //const char* symName = mSymbolData[coffReloc.mSymbolTableIndex];
- bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
- if (symInfo->mNameOfs[0] != 0)
- {
- if (symInfo->mName[7] != 0)
- {
- // Name is exactly 8 chars, not null terminated yet
- name = String(symInfo->mName, symInfo->mName + 8);
- }
- else
- name = symInfo->mName;
- }
- else
- name = mStringTable + symInfo->mNameOfs[1];
- bool didNameMatch = false;
-
- addr_target resolvedSymbolAddr = resolvedSymbolAddrs[coffReloc.mSymbolTableIndex];
- #ifdef BF_DBG_32
- 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);
- if (name[0] == '_')
- name.Remove(0, 1);
- #else
- bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_AMD64_ADDR64) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB) ||
- ((coffReloc.mType >= IMAGE_REL_AMD64_REL32) || (coffReloc.mType <= IMAGE_REL_AMD64_REL32_5));
- #endif
- bool isHsPrev = false;
- if (name.StartsWith("bf_hs_prev@"))
- {
- isHsPrev = true;
- name.Remove(0, 11);
- }
- bool deferResolve = false;
- if ((resolvedSymbolAddr == 0) && (needsSymbolAddr))
- {
- bool isHotSwapPreserve = IsHotSwapPreserve(name);
- if ((symInfo->mSectionNum == 0) || (isHotSwapPreserve) || (isHsPrev))
- {
- auto origSymbolEntry = mainModule->mSymbolNameMap.Find(name.c_str());
- if (origSymbolEntry != NULL)
- {
- resolvedSymbolAddr = origSymbolEntry->mValue->mAddress;
- }
- else
- {
- //BF_FATAL("Symbol lookup error");
- deferResolve = true;
- }
- }
-
- if ((symInfo->mSectionNum != 0) && (resolvedSymbolAddr == NULL))
- {
- DbgHotTargetSection* refHotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
- resolvedSymbolAddr = GetHotTargetAddress(refHotTargetSection) + symInfo->mValue;
- // Using the !hotTargetSection->mNoTargetAlloc check down here caused us to not properly remap reloaded
- // static members in the debug info. Even though we parse the debug info before we apply the deferred
- // resolves, the mLocData points into the original data so we still get it remapped when we use that
- // mLocData
- if (/*(!hotTargetSection->mNoTargetAlloc) &&*/ ((refHotTargetSection->mData == NULL) || (refHotTargetSection->mNoTargetAlloc)) &&
- (!isStaticSymbol))
- deferResolve = true;
- else
- deferResolve = false;
- }
- }
- if (deferResolve)
- {
- // It's a static field, defer resolution, but don't bother replacing for debug info sections
- DbgDeferredHotResolve* deferredResolve = mDeferredHotResolveList.Alloc();
- deferredResolve->mHotTargetSection = hotTargetSection;
- deferredResolve->mName = name;
- deferredResolve->mNewAddr = resolvedSymbolAddr;
- deferredResolve->mReloc = coffReloc;
- continue;
- }
- else
- {
- resolvedSymbolAddrs[coffReloc.mSymbolTableIndex] = resolvedSymbolAddr;
- DoReloc(hotTargetSection, coffReloc, resolvedSymbolAddr, symInfo);
- }
- }
- }
- }
- }
- void DbgModule::CommitHotTargetSections()
- {
- for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
- {
- if (mHotTargetSections[sectNum] != NULL)
- {
- DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
- addr_target hotAddr = GetHotTargetAddress(hotTargetSection);
- if (hotAddr != 0)
- {
- // void* imageDestPtr = mOrigImageData->mBlocks[0] + hotTargetSection->mImageOffset;
- // if (hotTargetSection->mData != NULL)
- // memcpy(imageDestPtr, hotTargetSection->mData, hotTargetSection->mDataSize);
- // else
- // memset(imageDestPtr, 0, hotTargetSection->mDataSize);
- BF_ASSERT(mOrigImageData->mAddr != 0);
- void* imageDestPtr = hotTargetSection->mData;
- bool isTemp = false;
- if (imageDestPtr == NULL)
- {
- imageDestPtr = new uint8[hotTargetSection->mDataSize];
- memset(imageDestPtr, 0, hotTargetSection->mDataSize);
- isTemp = true;
- }
- if (hotTargetSection->mCanExecute)
- {
- bool success = mDebugger->WriteInstructions(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
- BF_ASSERT(success);
- }
- else
- {
- bool success = mDebugger->WriteMemory(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
- BF_ASSERT(success);
- }
- if (isTemp)
- delete imageDestPtr;
- }
- }
- }
- }
- void DbgModule::HotReplaceType(DbgType* newType)
- {
- if (!newType->IsCompositeType())
- return;
- auto linkedModule = GetLinkedModule();
- newType->PopulateType();
- DbgType* primaryType = linkedModule->GetPrimaryType(newType);
- if (primaryType == newType)
- {
- // There was no previous type
- BF_ASSERT(primaryType->mHotNewType == NULL);
- return;
- }
- if (primaryType->mHotNewType != newType)
- {
- // We have already pulled in the new data from a previous new type
- BF_ASSERT(primaryType->mHotNewType == NULL);
- return;
- }
- primaryType->mHotNewType = NULL;
- primaryType->PopulateType();
- linkedModule->ParseGlobalsData();
- linkedModule->ParseSymbolData();
- if (primaryType->mNeedsGlobalsPopulated)
- {
- // These aren't proper TPI types so we don't have any method declarations until we PopulateTypeGlobals
- linkedModule->PopulateTypeGlobals(primaryType);
- }
- for (auto methodNameEntry : primaryType->mMethodNameList)
- {
- if (methodNameEntry->mCompileUnitId != -1)
- {
- linkedModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
- methodNameEntry->mCompileUnitId = -1;
- }
- }
-
- // Now actually remove the linedata from the defining module
- HashSet<DbgSrcFile*> checkedFiles;
- for (auto method : primaryType->mMethodList)
- {
- method->mWasModuleHotReplaced = true;
- if (method->mLineInfo == NULL)
- continue;
- //FIXME: Hot replacing lines
- DbgSrcFile* lastSrcFile = NULL;
- checkedFiles.Clear();
- int prevCtx = -1;
- auto inlineRoot = method->GetRootInlineParent();
- for (int lineIdx = 0; lineIdx < method->mLineInfo->mLines.mSize; lineIdx++)
- {
- auto& lineData = method->mLineInfo->mLines[lineIdx];
- if (lineData.mCtxIdx != prevCtx)
- {
- auto ctxInfo = inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx];
- auto srcFile = ctxInfo.mSrcFile;
- prevCtx = lineData.mCtxIdx;
- if (srcFile != lastSrcFile)
- {
- if (checkedFiles.Add(srcFile))
- {
- // Remove linedata for old type
- // These go into a hot-replaced list so we can still bind to them -- that is necessary because
- // we may still have old versions of this method running (and may forever, if its in a loop on some thread)
- // since we only patch entry points
- //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, primaryType->mCompileUnit, true);
- //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, method, true);
- srcFile->RemoveLines(method->mCompileUnit->mDbgModule, method, true);
- }
- lastSrcFile = srcFile;
- }
- }
- }
- }
- //DbgType* primaryType = newType->GetPrimaryType();
- // We need to keep a persistent list of hot replaced methods so we can set hot jumps
- // in old methods that may still be on the callstack. These entries get removed when
- // we unload unused hot files in
- while (!primaryType->mMethodList.IsEmpty())
- {
- auto method = primaryType->mMethodList.PopFront();
- method->PopulateSubprogram();
- primaryType->mHotReplacedMethodList.PushFront(method);
- mHotPrimaryTypes.Add(primaryType);
- }
-
- bool setHotJumpFailed = false;
- while (!newType->mMethodList.IsEmpty())
- {
- DbgSubprogram* newMethod = newType->mMethodList.PopFront();
- if (!newMethod->mBlock.IsEmpty())
- {
- newMethod->PopulateSubprogram();
- bool found = false;
- for (auto oldMethod : primaryType->mHotReplacedMethodList)
- {
- if (oldMethod->mBlock.IsEmpty())
- continue;
- if (oldMethod->Equals(newMethod))
- {
- if (!setHotJumpFailed)
- {
- if (!mDebugger->SetHotJump(oldMethod, newMethod))
- setHotJumpFailed = true;
- oldMethod->mWasHotReplaced = true;
- }
- }
- }
- }
- newMethod->mParentType = primaryType;
- primaryType->mMethodList.PushBack(newMethod);
- }
- primaryType->mCompileUnit->mWasHotReplaced = true;
- primaryType->mNeedsGlobalsPopulated = newType->mNeedsGlobalsPopulated;
- primaryType->mUsingNamespaces = newType->mUsingNamespaces;
- primaryType->mMemberList = newType->mMemberList;
- primaryType->mCompileUnit = newType->mCompileUnit;
- }
- bool DbgModule::CanRead(DataStream* stream, DebuggerResult* outResult)
- {
- PEHeader hdr;
- memset(&hdr, 0, sizeof(hdr));
- PE_NTHeaders ntHdr;
- memset(&ntHdr, 0, sizeof(ntHdr));
- stream->Read(&hdr, sizeof(PEHeader));
- stream->SetPos(hdr.e_lfanew);
- stream->Read(&ntHdr, sizeof(PE_NTHeaders));
- if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
- {
- *outResult = DebuggerResult_UnknownError;
- return false;
- }
- #ifdef BF_DBG_32
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
- {
- if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X64)
- *outResult = DebuggerResult_WrongBitSize;
- else
- *outResult = DebuggerResult_UnknownError;
- return false;
- }
- #else
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
- {
- if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X86)
- *outResult = DebuggerResult_WrongBitSize;
- else
- *outResult = DebuggerResult_UnknownError;
- return false;
- }
- #endif
- return true;
- }
- bool DbgModule::ReadCOFF(DataStream* stream, bool isHotObjectFile)
- {
- BP_ZONE("DbgModule::ReadCOFF");
- //if (this == mDebugTarget->mTargetBinary)
- //mMemReporter = new MemReporter();
- if (mMemReporter != NULL)
- {
- mMemReporter->BeginSection(StrFormat("Module: %s", mFilePath.c_str()));
- mMemReporter->Add(mImageSize);
- }
- defer
- {
- if (mMemReporter != NULL)
- mMemReporter->EndSection();
- };
-
- DbgModule* mainModule = mDebugTarget->mTargetBinary;
-
- MiniDumpDebugger* miniDumpDebugger = NULL;
- if (mDebugger->IsMiniDumpDebugger())
- {
- miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
- }
- mIsHotObjectFile = isHotObjectFile;
- auto linkedModule = GetLinkedModule();
- if (mIsHotObjectFile)
- linkedModule->PopulateStaticVariableMap();
-
- mStartTypeIdx = (int)linkedModule->mTypes.size();
- int startSrcFile = (int)mDebugTarget->mSrcFiles.size();
- mStartSubprogramIdx = (int)mSubprograms.size();
-
- PEHeader hdr;
- memset(&hdr, 0, sizeof(hdr));
- PE_NTHeaders ntHdr;
- memset(&ntHdr, 0, sizeof(ntHdr));
- if (!mIsHotObjectFile)
- {
- stream->Read(&hdr, sizeof(PEHeader));
- stream->SetPos(hdr.e_lfanew);
-
- stream->Read(&ntHdr, sizeof(PE_NTHeaders));
- mPreferredImageBase = ntHdr.mOptionalHeader.mImageBase;
- if (mImageBase == 0)
- {
- BF_ASSERT(this == mainModule);
- mImageBase = mPreferredImageBase;
- }
- if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
- {
- mLoadState = DbgModuleLoadState_Failed;
- return false;
- }
- #ifdef BF_DBG_32
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
- return false;
- #else
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
- {
- mLoadState = DbgModuleLoadState_Failed;
- return false;
- }
- #endif
- int pos = hdr.e_lfanew + FIELD_OFFSET(PE_NTHeaders, mOptionalHeader) + ntHdr.mFileHeader.mSizeOfOptionalHeader;
- stream->SetPos(pos);
- }
- else
- {
- stream->Read(&ntHdr.mFileHeader, sizeof(PEFileHeader));
- if (mMemReporter != NULL)
- mMemReporter->Add("PEFileHeader", sizeof(PEFileHeader));
- #ifdef BF_DBG_32
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
- return false;
- #else
- if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
- {
- mLoadState = DbgModuleLoadState_Failed;
- return false;
- }
- #endif
- }
-
- int sectionStartPos = stream->GetPos();
- int sectionDataEndPos = 0;
- if (miniDumpDebugger != NULL)
- {
- // Map header
- miniDumpDebugger->MapMemory((addr_target)mImageBase, (uint8*)mMappedImageFile->mData, 0x1000);
- }
- stream->SetPos(sectionStartPos);
- for (int dirNum = 0; dirNum < (int) ntHdr.mFileHeader.mNumberOfSections; dirNum++)
- {
- PESectionHeader sectHdr;
- stream->Read(§Hdr, sizeof(PESectionHeader));
- if (sectHdr.mSizeOfRawData > 0)
- sectionDataEndPos = sectHdr.mPointerToRawData + sectHdr.mSizeOfRawData;
- if (sectHdr.mNumberOfRelocations > 0)
- sectionDataEndPos = sectHdr.mPointerToRelocations + sectHdr.mNumberOfRelocations * sizeof(COFFRelocation);
-
- if (miniDumpDebugger != NULL)
- {
- miniDumpDebugger->MapMemory((addr_target)(mImageBase + sectHdr.mVirtualAddress), (uint8*)mMappedImageFile->mData + sectHdr.mPointerToRawData, sectHdr.mSizeOfRawData);
- }
- }
-
- //fseek(fp, sectionDataEndPos + ntHdr.mFileHeader.mNumberOfSymbols * 18, SEEK_SET);
- stream->SetPos(sectionDataEndPos);
- uint8* symbolData = new uint8[ntHdr.mFileHeader.mNumberOfSymbols * 18];
- mAllocSizeData += ntHdr.mFileHeader.mNumberOfSymbols * 18;
- mSymbolData = symbolData;
- stream->Read(symbolData, ntHdr.mFileHeader.mNumberOfSymbols * 18);
- int curPos = stream->GetPos();
- int strTableSize = 0;
- char* strTableData = NULL;
- if (!stream->Eof())
- {
- stream->Read(&strTableSize, 4);
- if (strTableSize != 0)
- {
- strTableSize -= 4;
-
- strTableData = new char[strTableSize + 4];
- memcpy(strTableData, &strTableSize, 4);
- stream->Read(strTableData + 4, strTableSize);
- mStringTable = strTableData;
- }
- }
-
- int mDebugFrameDataLen = 0;
-
- stream->SetPos(sectionStartPos);
-
- PEDataDirectory* exportDataDir = &ntHdr.mOptionalHeader.mDataDirectory[0];
-
- mHotTargetSections.Resize(ntHdr.mFileHeader.mNumberOfSections);
-
- Array<PESectionHeader> sectionHeaders;
- sectionHeaders.Resize(ntHdr.mFileHeader.mNumberOfSections);
- mSectionRVAs.Resize(sectionHeaders.size() + 1);
- Array<String> sectionNames;
- sectionNames.Resize(ntHdr.mFileHeader.mNumberOfSections);
- stream->Read(§ionHeaders[0], sizeof(PESectionHeader) * ntHdr.mFileHeader.mNumberOfSections);
- for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
- {
- mSectionRVAs[sectNum] = sectionHeaders[sectNum].mVirtualAddress;
- }
-
- int tlsSection = -1;
- for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
- {
- //PEDataDirectory* dataDir = &ntHdr.mOptionalHeader.mDataDirectory[dirNum];
- PESectionHeader& sectHdr = sectionHeaders[sectNum];
- //stream->Read(§Hdr, sizeof(PESectionHeader));
- char* name = sectHdr.mName;
- if (name[0] == '/')
- {
- int strIdx = atoi(name + 1);
- name = &strTableData[strIdx];
- }
- sectionNames[sectNum] = name;
- DbgHotTargetSection* targetSection = NULL;
- if (mIsHotObjectFile)
- {
- targetSection = new DbgHotTargetSection();
- targetSection->mDataSize = sectHdr.mSizeOfRawData;
- targetSection->mPointerToRelocations = sectHdr.mPointerToRelocations;
- targetSection->mNumberOfRelocations = sectHdr.mNumberOfRelocations;
- targetSection->mTargetSectionAddr = 0; // TODO: Allocate!
- targetSection->mCanExecute = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
- targetSection->mCanWrite = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_WRITE) != 0;
- targetSection->mNoTargetAlloc = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0;
- mHotTargetSections[sectNum] = targetSection;
- }
-
- DbgSection dwSection;
- dwSection.mIsExecutable = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
- dwSection.mAddrStart = sectHdr.mVirtualAddress;
- dwSection.mAddrLength = BF_MAX(sectHdr.mSizeOfRawData, sectHdr.mVirtualSize);
- mSections.push_back(dwSection);
- if (sectHdr.mPointerToRawData == 0)
- continue;
- if (strcmp(name, ".tls") == 0)
- mTLSAddr = (addr_target)(sectHdr.mVirtualAddress + mImageBase);
- if ((mIsHotObjectFile) && (strcmp(name, ".tls$") == 0))
- {
- tlsSection = sectNum;
- mTLSSize = sectHdr.mSizeOfRawData;
- targetSection->mNoTargetAlloc = true;
- }
- if ((!mIsHotObjectFile) /*&& (!isUnwindSection)*/)
- {
- if (((strcmp(name, ".text")) == 0) ||
- ((strcmp(name, ".textbss")) == 0) ||
- ((strcmp(name, ".reloc")) == 0)/* ||
- ((strcmp(name, ".data")) == 0)*/)
- {
- // Big unneeded sections
- continue;
- }
- }
- stream->SetPos(sectHdr.mPointerToRawData);
- int dataSize = sectHdr.mSizeOfRawData + 8;
- mAllocSizeData += dataSize;
- uint8* data = new uint8[dataSize];
- {
- BP_ZONE("DbgModule::ReadCOFF_ReadSectionData");
- stream->Read(data, sectHdr.mSizeOfRawData);
- }
- BfLogDbg("Read section data %s %p\n", name, data);
- memset(data + sectHdr.mSizeOfRawData, 0, 8);
- if (mIsHotObjectFile)
- targetSection->mData = data;
- addr_target addrOffset = sectHdr.mVirtualAddress;
- if ((exportDataDir->mVirtualAddress != 0) && (exportDataDir->mVirtualAddress >= sectHdr.mVirtualAddress) && (exportDataDir->mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
- {
- BP_ZONE("DbgModule::ReadCOFF_SymbolMap");
- IMAGE_EXPORT_DIRECTORY* imageExportDir = (IMAGE_EXPORT_DIRECTORY*)(data + (exportDataDir->mVirtualAddress - addrOffset));
- for (int funcIdx = 0; funcIdx < (int)imageExportDir->NumberOfNames; funcIdx++)
- {
- //addr_target strAddr = *(addr_target*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(addr_target));
- int32 strAddr = *(int32*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(int32));
- const char* name = (const char*)(data + (strAddr - addrOffset));
- #ifdef BF_DBG_32
- if (name[0] == '_')
- name++;
- #endif
- int funcOrd = *(uint16*)(data + (imageExportDir->AddressOfNameOrdinals - addrOffset) + funcIdx * sizeof(uint16));
- addr_target funcAddr = *(uint32*)(data + (imageExportDir->AddressOfFunctions - addrOffset) + funcOrd * sizeof(int32));
- int strLen = (int)strlen(name);
- BP_ALLOC("ReadCOFF_SymbolMap", strLen + 1);
- char* allocStr = (char*)mAlloc.AllocBytes(strLen + 1, "ReadCOFF_SymbolMap");
- memcpy(allocStr, name, strLen);
- BP_ALLOC_T(DbgSymbol);
- DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
- dwSymbol->mDbgModule = this;
- dwSymbol->mName = allocStr;
- dwSymbol->mAddress = funcAddr;
- if (strcmp(name, "_tls_index") == 0)
- {
- mTLSIndexAddr = funcAddr;
- }
- //TODO:
- //mDeferredSymbols.PushFront(dwSymbol);
- dwSymbol->mAddress = (addr_target)(dwSymbol->mAddress + mImageBase);
- mDebugTarget->mSymbolMap.Insert(dwSymbol);
- linkedModule->mSymbolNameMap.Insert(dwSymbol);
- }
- }
- if ((mIsHotObjectFile) && (sectHdr.mNumberOfRelocations > 0))
- {
- //mDebugger->AllocTargetMemory(sectHdr.mSizeOfRawData, true, true);
- }
- if (strcmp(name, ".text") == 0)
- {
- if (!mIsHotObjectFile)
- mCodeAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
- }
- //if (strcmp(name, ".rdata") == 0)
- {
- PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
- if (debugDirEntry.mSize > 0)
- {
- if (mMemReporter != NULL)
- mMemReporter->Add("DataDirectory", debugDirEntry.mSize);
- if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
- {
- int count = debugDirEntry.mSize / sizeof(IMAGE_DEBUG_DIRECTORY);
- for (int dirIdx = 0; dirIdx < count; dirIdx++)
- {
- IMAGE_DEBUG_DIRECTORY* debugDirectory = (IMAGE_DEBUG_DIRECTORY*)(data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress) + dirIdx;
- if (debugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW)
- {
- struct _CodeViewEntry
- {
- public:
- int32 mSig;
- uint8 mGUID[16];
- int32 mAge;
- const char mPDBPath[1];
- };
- if (debugDirectory->AddressOfRawData != 0)
- {
- _CodeViewEntry* codeViewEntry = (_CodeViewEntry*)(data + debugDirectory->AddressOfRawData - sectHdr.mVirtualAddress);
- if (codeViewEntry->mSig == 'SDSR')
- {
- LoadPDB(codeViewEntry->mPDBPath, codeViewEntry->mGUID, codeViewEntry->mAge);
- }
- }
- }
- }
- }
- //stream->SetPos(debugDirEntry.mVirtualAddress);
- }
- }
- //
- {
- PEDataDirectory& tlsDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
- if (tlsDirEntry.mSize > 0)
- {
- if ((tlsDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (tlsDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
- {
- uint8* relPtr = data + tlsDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
- uint8* endPtr = relPtr + tlsDirEntry.mSize;
- addr_target tlsDataStart = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
- addr_target tlsDataEnd = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
- mTLSAddr = (addr_target)(tlsDataStart + mImageBase);
- mTLSSize = (int)(tlsDataEnd - tlsDataStart);
- }
- }
- }
- //
- {
- PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
- if (debugDirEntry.mSize > 0)
- {
- if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
- {
- uint8* relPtr = data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
- uint8* endPtr = relPtr + debugDirEntry.mSize;
- IMAGE_RESOURCE_DIRECTORY* typeDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr);
-
- // Skip named entries
- for (int typeIdx = 0; typeIdx < typeDir->NumberOfIdEntries; typeIdx++)
- {
- IMAGE_RESOURCE_DIRECTORY_ENTRY* typeEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)typeDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
- (typeDir->NumberOfNamedEntries + typeIdx)*sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
- if (typeEntry->Id == 0x10) //VERSION
- {
- IMAGE_RESOURCE_DIRECTORY* idDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (typeEntry->OffsetToData & 0x7FFFFFFF));
- if (idDir->NumberOfIdEntries < 1)
- break;
- IMAGE_RESOURCE_DIRECTORY_ENTRY* idEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)idDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
- (idDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
- IMAGE_RESOURCE_DIRECTORY* langDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (idEntry->OffsetToData & 0x7FFFFFFF));
- if (langDir->NumberOfIdEntries < 1)
- break;
- IMAGE_RESOURCE_DIRECTORY_ENTRY* langEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)langDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
- (langDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
- IMAGE_RESOURCE_DATA_ENTRY* dataEntry = (IMAGE_RESOURCE_DATA_ENTRY*)(relPtr + (langEntry->OffsetToData & 0x7FFFFFFF));
- uint8* versionData = data + dataEntry->OffsetToData - sectHdr.mVirtualAddress;
- uint8* vPtr = versionData;
-
- auto vSize = GET_FROM(vPtr, uint16);
- auto verEnd = vPtr + vSize;
- auto vLength = GET_FROM(vPtr, uint16);
- vPtr += 36; // "VS_VERSION_INFO"
- auto fixedFileInfo = GET_FROM(vPtr, VS_FIXEDFILEINFO);
- auto _GetString = [&]()
- {
- wchar_t* cPtr = (wchar_t*)vPtr;
- int len = (int)wcslen(cPtr);
- vPtr += (len + 1) * 2;
-
- if (((intptr)vPtr & 3) != 0)
- vPtr += 2;
- UTF16String str16(cPtr, len);
- return UTF8Encode(str16);
- };
- while (vPtr < verEnd)
- {
- auto size = GET_FROM(vPtr, uint16);
- auto childEnd = vPtr + size;
- auto valueLength = GET_FROM(vPtr, uint16);
- auto type = GET_FROM(vPtr, uint16);
- String infoType = _GetString();
- if (infoType == "StringFileInfo")
- {
- while (vPtr < childEnd)
- {
- auto strsSize = GET_FROM(vPtr, uint16);
- auto strsEnd = vPtr + strsSize;
- auto strsLength = GET_FROM(vPtr, uint16);
- auto strsType = GET_FROM(vPtr, uint16);
- String hexNum = _GetString();
- while (vPtr < strsEnd)
- {
- auto strSize = GET_FROM(vPtr, uint16);
- auto strEnd = vPtr + strSize;
- auto strLength = GET_FROM(vPtr, uint16);
- auto strType = GET_FROM(vPtr, uint16);
- String key = _GetString();
- String value = _GetString();
- if (key == "FileVersion")
- mVersion = value;
- }
- }
- }
- vPtr = childEnd;
- }
- }
- }
- }
- //stream->SetPos(debugDirEntry.mVirtualAddress);
- }
- }
-
- bool usedData = true;
- /*if (isUnwindSection)
- {
- mExceptionData = data;
- mExceptionDataRVA = sectHdr.mVirtualAddress;
- }*/
- if (strcmp(name, ".pdata") == 0)
- {
- mExceptionDirectoryData = data;
- mExceptionDirectoryDataLen = sectHdr.mSizeOfRawData;
- }
- /*else if (strcmp(name, ".rdata") == 0)
- {
- if (mExceptionData == NULL)
- {
- mExceptionData = data;
- mExceptionDataRVA = sectHdr.mVirtualAddress;
- }
- else
- {
- usedData = false;
- }
- }
- else if (strcmp(name, ".xdata") == 0)
- {
- if (mExceptionData != NULL)
- {
- if (mIsHotObjectFile)
- {
- mOwnedSectionData.push_back(mExceptionData);
- }
- else
- {
- // xdata section overrides rdata exception data
- delete[] mExceptionData;
- }
- }
- mExceptionData = data;
- mExceptionDataRVA = sectHdr.mVirtualAddress;
- }*/
- else if (strcmp(name, ".debug_info") == 0)
- {
- mDebugInfoData = data;
- }
- else if (strcmp(name, ".debug_line") == 0)
- {
- mDebugLineData = data;
- }
- else if (strcmp(name, ".debug_str") == 0)
- {
- mDebugStrData = data;
- }
- else if (strcmp(name, ".debug_frame") == 0)
- {
- mDebugFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
- mDebugFrameData = data;
- mDebugFrameDataLen = sectHdr.mSizeOfRawData;
- }
- else if (strcmp(name, ".eh_frame") == 0)
- {
- mEHFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
- mEHFrameData = data;
- }
- else if (strcmp(name, ".debug_abbrev") == 0)
- {
- mDebugAbbrevData = data;
- mDebugAbbrevPtrData = new const uint8*[sectHdr.mSizeOfRawData];
- }
- else if (strcmp(name, ".debug_loc") == 0)
- {
- mDebugLocationData = data;
- }
- else if (strcmp(name, ".debug_ranges") == 0)
- {
- mDebugRangesData = data;
- }
- // else if (strcmp(name, ".rsrc") == 0)
- // {
- // //IMAGE_DIRECTORY_ENTRY_RESOURCE
- // }
- else if (CheckSection(name, data, sectHdr.mSizeOfRawData))
- {
- // Was used
- }
- else
- {
- /*if (isUnwindSection)
- mOwnsExceptionData = true;
- else*/
- usedData = false;
- }
- if (!usedData)
- {
- if (mIsHotObjectFile)
- {
- mOwnedSectionData.push_back(data);
- }
- else
- {
- mAllocSizeData -= dataSize;
- delete [] data;
- }
- }
- //stream->SetPos(prevPos);
- }
- int needHotTargetMemory = 0;
- if (mIsHotObjectFile)
- {
- for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
- {
- auto targetSection = mHotTargetSections[sectNum];
- if (!targetSection->mNoTargetAlloc)
- needHotTargetMemory += (targetSection->mDataSize + (mDebugger->mPageSize - 1)) & ~(mDebugger->mPageSize - 1);
- }
- mDebugger->ReserveHotTargetMemory(needHotTargetMemory);
-
- // '0' address is temporary
- //mOrigImageData = new DbgModuleMemoryCache(0, NULL, needHotTargetMemory, true);
- mOrigImageData = new DbgModuleMemoryCache(0, needHotTargetMemory);
- }
- int numSections = ntHdr.mFileHeader.mNumberOfSections;
- if (mIsHotObjectFile)
- {
- addr_target* resolvedSymbolAddrs = new addr_target[ntHdr.mFileHeader.mNumberOfSymbols];
- memset(resolvedSymbolAddrs, 0, ntHdr.mFileHeader.mNumberOfSymbols * sizeof(addr_target));
- ParseHotTargetSections(stream, resolvedSymbolAddrs);
- delete [] resolvedSymbolAddrs;
- }
- ProcessDebugInfo();
- if (mDebugInfoData != NULL)
- {
- mDbgFlavor = DbgFlavor_GNU;
- mMasterCompileUnit = new DbgCompileUnit(this);
- mMasterCompileUnit->mDbgModule = this;
- mMasterCompileUnit->mIsMaster = true;
- const uint8* data = mDebugInfoData;
- while (ParseDWARF(data)) {}
- CreateNamespaces();
- // Must be added last so module reference indices still map correctly
- mCompileUnits.push_back(mMasterCompileUnit);
- }
- ParseDebugFrameData();
- ParseEHFrameData();
- mEndTypeIdx = (int)linkedModule->mTypes.size();
- mEndSubprogramIdx = (int)mSubprograms.size();
- if (mDebugLineData != NULL)
- {
- const uint8* data = mDebugLineData;
- for (int compileUnitIdx = 0; true; compileUnitIdx++)
- if (!ParseDebugLineInfo(data, compileUnitIdx))
- break;
- }
-
- {
- BP_ZONE("ReadPE_ReadSymbols");
- //linkedModule->mSymbolNameMap.reserve(linkedModule->mSymbolNameMap.size() + ntHdr.mFileHeader.mNumberOfSymbols * 2);
- bool tlsFailed = false;
- addr_target tlsMappingAddr = 0;
- for (int symNum = 0; symNum < (int)ntHdr.mFileHeader.mNumberOfSymbols; symNum++)
- {
- PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[symNum * 18];
- char* name = symInfo->mName;
- if (symInfo->mNameOfs[0] != 0)
- {
- if (name[7] != 0)
- {
- // Name is exactly 8 chars, not null terminated yet
- name = (char*)mAlloc.AllocBytes(9, "PESymbol");
- memcpy(name, symInfo->mName, 8);
- name[8] = 0;
- }
- }
- else
- name = strTableData + symInfo->mNameOfs[1];
- if ((symInfo->mStorageClass == COFF_SYM_CLASS_EXTERNAL) ||
- (symInfo->mStorageClass == COFF_SYM_CLASS_STATIC))
- {
- // 'static' in the C sense.
- // It means local to the compile unit, so may have multiple copies of the same symbol name.
- bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
- if (symInfo->mSectionNum == 0xFFFF)
- continue;
- if (symInfo->mSectionNum > 0)
- {
- bool isTLS = false;
- addr_target targetAddr = 0;
- if (mIsHotObjectFile)
- {
- if (symInfo->mSectionNum - 1 == tlsSection)
- {
- isTLS = true;
- }
- else
- {
- auto hotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
- if (hotTargetSection != NULL)
- targetAddr = GetHotTargetAddress(hotTargetSection) + symInfo->mValue;
- }
- }
- else
- targetAddr = mSectionRVAs[symInfo->mSectionNum - 1] + symInfo->mValue;
-
- if (((targetAddr != 0) || (isTLS)) &&
- (name[0] != '.'))
- {
- const char* symbolName = name;
- #ifdef BF_DBG_32
- if (symbolName[0] == '_')
- symbolName++;
- #endif
- if (strcmp(symbolName, "_tls_index") == 0)
- {
- mTLSIndexAddr = (addr_target)(targetAddr + mImageBase);
- }
-
- if ((isStaticSymbol) && (IsHotSwapPreserve(symbolName)))
- isStaticSymbol = false;
- if ((mIsHotObjectFile) && (!isStaticSymbol))
- {
- DbgSymbol* dwSymbol = NULL;
- linkedModule->ParseSymbolData() ;
- BP_ALLOC_T(DbgSymbol);
- dwSymbol = mAlloc.Alloc<DbgSymbol>();
- dwSymbol->mDbgModule = this;
- dwSymbol->mName = symbolName;
- dwSymbol->mAddress = targetAddr;
- if (dwSymbol != NULL)
- {
- bool isHotSwapPreserve = IsHotSwapPreserve(dwSymbol->mName);
- bool insertIntoNameMap = true;
-
- bool oldFound = false;
-
- auto nameMapEntry = linkedModule->mSymbolNameMap.Find(dwSymbol->mName);
- if (nameMapEntry != NULL)
- {
- oldFound = true;
- if (!isHotSwapPreserve)
- {
- nameMapEntry->mValue = dwSymbol;
- }
- else if (mDbgFlavor == DbgFlavor_MS)
- {
- // Store in our own map - this is needed for storing address of the new vdata
- // so the new values can be copied in
- mSymbolNameMap.Insert(dwSymbol);
- }
- }
- else
- {
- if (isTLS)
- {
- if (mainModule->mTLSExtraAddr == 0)
- {
- auto extraSym = mainModule->mSymbolNameMap.Find("__BFTLS_EXTRA");
- if (extraSym != NULL)
- {
- mainModule->ParseGlobalsData();
- auto itr = mainModule->mStaticVariableMap.find("__BFTLS_EXTRA");
- if (itr != mainModule->mStaticVariableMap.end())
- {
- auto staticVar = itr->second;
- mainModule->mTLSExtraAddr = extraSym->mValue->mAddress;
- mainModule->mTLSExtraSize = staticVar->mType->GetByteCount();
- }
- }
- }
- if ((mainModule->mTLSExtraAddr != 0) && (tlsMappingAddr == 0))
- {
- // Take a chunk out of __BFTLS_EXTRA
- if (mTLSSize <= mainModule->mTLSExtraSize)
- {
- tlsMappingAddr = mainModule->mTLSExtraAddr;
- mainModule->mTLSExtraAddr += mTLSSize;
- mainModule->mTLSExtraSize -= mTLSSize;
- }
- }
- if (tlsMappingAddr != 0)
- {
- BF_ASSERT(symInfo->mValue < mTLSSize);
- dwSymbol->mAddress = tlsMappingAddr + symInfo->mValue;
- }
- if (dwSymbol->mAddress == 0)
- {
- if (!tlsFailed)
- {
- Fail(StrFormat("Hot swapping failed to allocate TLS address for '%s'. Program restart required.", name));
- }
- dwSymbol->mAddress = (addr_target)0xCDCDCDCD;
- tlsFailed = true;
- }
- }
- }
-
- if (dwSymbol->mAddress != 0)
- {
- if (!oldFound)
- linkedModule->mSymbolNameMap.Insert(dwSymbol);
- mDebugTarget->mSymbolMap.Insert(dwSymbol);
- }
- }
- }
- else
- {
- //TODO: We don't need to defer symbols anymore... we can just do a Fixup on their addr
- //mDeferredSymbols.PushFront(dwSymbol);
- BP_ALLOC_T(DbgSymbol);
- DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
- dwSymbol->mDbgModule = this;
- dwSymbol->mName = symbolName;
- dwSymbol->mAddress = targetAddr;
- if (!mIsHotObjectFile)
- dwSymbol->mAddress += (addr_target)mImageBase;
- if (mIsHotObjectFile)
- BF_ASSERT((dwSymbol->mAddress >= mImageBase) && (dwSymbol->mAddress < mImageBase + mImageSize));
- mDebugTarget->mSymbolMap.Insert(dwSymbol);
- if (!isStaticSymbol)
- linkedModule->mSymbolNameMap.Insert(dwSymbol);
- }
- }
- }
- }
- if (symInfo->mStorageClass == COFF_SYM_CLASS_FILE)
- {
- const char* fileName = (const char*)&mSymbolData[(symNum + 1) * 18];
- }
- symNum += symInfo->mNumOfAuxSymbols;
- }
- }
- int subProgramSizes = 0;
- for (int subProgramIdx = mStartSubprogramIdx; subProgramIdx < mEndSubprogramIdx; subProgramIdx++)
- {
- auto dwSubprogram = mSubprograms[subProgramIdx];
- subProgramSizes += (int)(dwSubprogram->mBlock.mHighPC - dwSubprogram->mBlock.mLowPC);
-
- /*for (int i = 0; i < dwSubprogram->mLineDataArray.mSize; i++)
- {
- auto lineData = dwSubprogram->mLineDataArray.mData[i];
- auto srcFile = lineData->mSrcFileRef->mSrcFile;
- srcFile->mLineData.push_back(lineData);
- srcFile->mHadLineData = true;
- if ((srcFile->mFirstLineDataDbgModule == NULL) || (srcFile->mFirstLineDataDbgModule == this))
- srcFile->mFirstLineDataDbgModule = this;
- else
- srcFile->mHasLineDataFromMultipleModules = true;
- }*/
- }
- // Delete srcFiles without line data
- int lineDataCount = 0;
- /*for (int srcFileIdx = startSrcFile; srcFileIdx < (int)mDebugTarget->mSrcFiles.size(); srcFileIdx++)
- {
- if (!mDebugTarget->mSrcFiles[srcFileIdx]->mHadLineData)
- {
- mEmptySrcFiles.push_back(mDebugTarget->mSrcFiles[srcFileIdx]);
- mDebugTarget->mSrcFiles.erase(mDebugTarget->mSrcFiles.begin() + srcFileIdx);
- }
- else
- lineDataCount += (int)mDebugTarget->mSrcFiles[srcFileIdx]->mLineData.size();
- }*/
- auto srcFilesItr = mDebugTarget->mSrcFiles.begin();
- while (srcFilesItr != mDebugTarget->mSrcFiles.end())
- {
- DbgSrcFile* srcFile = srcFilesItr->mValue;
- if ((!srcFile->mHadLineData) && (srcFile->mLocalPath.IsEmpty()))
- {
- mEmptySrcFiles.push_back(srcFile);
- srcFilesItr = mDebugTarget->mSrcFiles.Remove(srcFilesItr);
- }
- else
- {
- ++srcFilesItr;
- }
- }
- if (!mIsHotObjectFile)
- {
- mImageSize = ntHdr.mOptionalHeader.mSizeOfImage;
- mEntryPoint = ntHdr.mOptionalHeader.mAddressOfEntryPoint;
- }
-
- /*OutputDebugStrF("%s:\n CompileUnits:%d DebugLines: %d Types: %d (%d in map) SubPrograms: %d (%dk) AllocSize:%dk\n", mFilePath.c_str(), mCompileUnits.size(),
- lineDataCount, mEndTypeIdx - mStartTypeIdx, (int)linkedModule->mTypes.size() - mStartTypeIdx, mEndSubprogramIdx - mStartSubprogramIdx, subProgramSizes / 1024, mAlloc.GetAllocSize() / 1024);*/
-
- if (mIsHotObjectFile)
- {
- // In COFF, we don't necessarily add an actual primary type during MapCompileUnitMethods, so this fixes that
- while (true)
- {
- bool didReplaceType = false;
- for (auto itr = mHotPrimaryTypes.begin(); itr != mHotPrimaryTypes.end(); ++itr)
- {
- auto dbgType = *itr;
- auto primaryType = dbgType->GetPrimaryType();
- if (primaryType != dbgType)
- {
- mHotPrimaryTypes.Remove(itr);
- mHotPrimaryTypes.Add(primaryType);
- didReplaceType = true;
- break;
- }
- }
- if (!didReplaceType)
- break;
- }
- BF_ASSERT(mTypes.size() == 0);
- for (int typeIdx = mStartTypeIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
- {
- DbgType* newType = linkedModule->mTypes[typeIdx];
- //if (!newType->mMethodList.IsEmpty())
- if (!newType->mIsDeclaration)
- HotReplaceType(newType);
- }
- }
-
- if (needHotTargetMemory != 0)
- {
- BF_ASSERT(needHotTargetMemory == mImageSize);
- }
- //BF_ASSERT(mEndTypeIdx == (int)linkedModule->mTypes.size());
- //BF_ASSERT(mEndSubprogramIdx == (int)mSubprograms.size());
- ParseExceptionData();
- mLoadState = DbgModuleLoadState_Loaded;
- if (mMemReporter != NULL)
- {
- mMemReporter->BeginSection("Sections");
- ParseSymbolData();
- Array<DbgSymbol*> orderedSyms;
- for (auto sym : mSymbolNameMap)
- {
- auto dbgSym = sym->mValue;
- orderedSyms.Add(dbgSym);
- }
- orderedSyms.Sort([](DbgSymbol* lhs, DbgSymbol* rhs) { return lhs->mAddress < rhs->mAddress; });
-
- for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
- {
- PESectionHeader& sectHdr = sectionHeaders[sectNum];
-
- mMemReporter->BeginSection(sectionNames[sectNum]);
- DbgSymbol* lastSym = NULL;
- for (auto dbgSym : orderedSyms)
- {
- if (dbgSym->mAddress < mImageBase + sectHdr.mVirtualAddress)
- continue;
- if (dbgSym->mAddress >= mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData)
- break;
-
- if (lastSym != NULL)
- {
- mMemReporter->Add(lastSym->mName, (int)(dbgSym->mAddress - lastSym->mAddress));
- }
- else
- {
- int startingOffset = (int)(dbgSym->mAddress - (mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
- if (startingOffset > 0)
- mMemReporter->Add("<StartData>", startingOffset);
- }
- lastSym = dbgSym;
- }
- if (lastSym != NULL)
- mMemReporter->Add(lastSym->mName, (int)((mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData) - lastSym->mAddress));
- else
- {
- mMemReporter->Add("<Unaccounted>", (int)(sectHdr.mSizeOfRawData));
- }
- mMemReporter->EndSection();
- }
- mMemReporter->EndSection();
- mMemReporter->mShowInKB = false;
- mMemReporter->Report();
- }
- return true;
- }
- void DbgModule::FinishHotSwap()
- {
- BF_ASSERT(mIsHotObjectFile);
- auto linkedModule = GetLinkedModule();
- auto mainModule = mDebugTarget->mTargetBinary;
- HashSet<String> failSet;
- String findName;
- for (auto deferredHotResolve : mDeferredHotResolveList)
- {
- addr_target resolveTargetAddr = deferredHotResolve->mNewAddr;
- findName = deferredHotResolve->mName;
- if (mDbgFlavor == DbgFlavor_MS)
- {
- // ... why do we need to find these variables in the variable map instead of the symbol name map?
- }
- auto itr = mainModule->mStaticVariableMap.find(findName.c_str());
- if (itr != mainModule->mStaticVariableMap.end())
- {
- DbgVariable* variable = itr->second;
- resolveTargetAddr = mDebugTarget->GetStaticAddress(variable);
- }
- else
- {
- auto symbolEntry = mainModule->mSymbolNameMap.Find(findName.c_str());
- if (symbolEntry != NULL)
- {
- resolveTargetAddr = symbolEntry->mValue->mAddress;
- }
- else
- {
- if (deferredHotResolve->mName == "__ImageBase")
- {
- resolveTargetAddr = (addr_target)mainModule->mImageBase;
- }
- else
- {
- resolveTargetAddr = mainModule->LocateSymbol(deferredHotResolve->mName);
- if (resolveTargetAddr == 0)
- {
- failSet.Add(deferredHotResolve->mName);
- continue;
- }
- }
- }
- }
- DoReloc(deferredHotResolve->mHotTargetSection, deferredHotResolve->mReloc, resolveTargetAddr, NULL);
- }
- mDeferredHotResolveList.Clear();
- if (!failSet.IsEmpty())
- {
- bool handled = false;
- if (!mDebugger->mDebugManager->mOutMessages.empty())
- {
- auto& str = mDebugger->mDebugManager->mOutMessages.back();
- if (str.Contains("failed to resolve"))
- {
- for (auto& sym : failSet)
- {
- str += ", ";
- str += sym;
- }
- handled = true;
- }
- }
- if (!handled)
- {
- int symIdx = 0;
- String str;
- if (failSet.size() == 1)
- str = "Hot swapping failed to resolve symbol: ";
- else
- str = "Hot swapping failed to resolve symbols: ";
- for (auto& sym : failSet)
- {
- if (symIdx != 0)
- str += ", ";
- str += sym;
- symIdx++;
- }
- mDebugger->Fail(str);
- }
- }
- CommitHotTargetSections();
- // 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)
- // before we can finalize the class vdata.
- ProcessHotSwapVariables();
- for (auto hotTargetSection : mHotTargetSections)
- delete hotTargetSection;
- mHotTargetSections.Clear();
- mSymbolNameMap.Clear();
- }
- addr_target DbgModule::ExecuteOps(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, CPURegisters* registers, DbgAddrType* outAddrType, bool allowReg, addr_target* pushValue)
- {
- const uint8* locDataEnd = locData + locDataLen;
- int regNum = -1;
- addr_target stackFrameData[256];
- int stackIdx = 0;
- if (pushValue != NULL)
- stackFrameData[stackIdx++] = *pushValue;
- while (locData < locDataEnd)
- {
- uint8 opCode = GET_FROM(locData, uint8);
- switch (opCode)
- {
- case DW_OP_piece:
- {
- if (*outAddrType == DbgAddrType_Register)
- *outAddrType = DbgAddrType_Value;
- addr_target val = stackFrameData[--stackIdx];
- int pieceSize = (int)DecodeULEB128(locData);
- if (pieceSize == 4)
- val &= 0xFFFFFFFF;
- else if (pieceSize == 2)
- val &= 0xFFFF;
- else if (pieceSize == 1)
- val &= 0xFF;
- stackFrameData[stackIdx++] = val;
- }
- break;
- case DW_OP_consts:
- {
- int64 val = DecodeSLEB128(locData);
- stackFrameData[stackIdx++] = (addr_target)val;
- }
- break;
- case DW_OP_stack_value:
- {
- *outAddrType = DbgAddrType_Value;
- }
- break;
- case DW_OP_addr_noRemap:
- {
- addr_target addr = GET_FROM(locData, addr_target);
- stackFrameData[stackIdx++] = addr;
- //*outIsAddr = true;
- *outAddrType = DbgAddrType_Target;
- }
- break;
- case DW_OP_addr:
- {
- addr_target addr = GET_FROM(locData, addr_target);
- //if (dwarf != NULL)
- addr = RemapAddr(addr);
- stackFrameData[stackIdx++] = addr;
- //*outIsAddr = true;
- *outAddrType = DbgAddrType_Target;
- }
- break;
- case DW_OP_deref:
- {
- addr_target addr = stackFrameData[--stackIdx];
- addr_target value = mDebugger->ReadMemory<addr_target>(addr);
- stackFrameData[stackIdx++] = value;
- }
- break;
- case DW_OP_fbreg:
- {
- if (registers == NULL)
- return 0;
- BF_ASSERT(dwSubprogram != NULL);
- DbgSubprogram* nonInlinedSubProgram = dwSubprogram->GetRootInlineParent();
-
- if (nonInlinedSubProgram->mFrameBaseData == NULL)
- {
- *outAddrType = DbgAddrType_Target; //TODO: why?
- return 0;
- }
- BF_ASSERT(nonInlinedSubProgram->mFrameBaseData != NULL);
- uint64 loc = EvaluateLocation(nonInlinedSubProgram, nonInlinedSubProgram->mFrameBaseData, nonInlinedSubProgram->mFrameBaseLen, stackFrame, outAddrType, false);
- int64 offset = DecodeSLEB128(locData);
- loc += offset;
- //loc = BfDebuggerReadMemory(loc);
- //*outIsAddr = true;
- *outAddrType = DbgAddrType_Target;
- stackFrameData[stackIdx++] = (addr_target)loc;
- }
- break;
- case DW_OP_reg0:
- case DW_OP_reg1:
- case DW_OP_reg2:
- case DW_OP_reg3:
- case DW_OP_reg4:
- case DW_OP_reg5:
- case DW_OP_reg6:
- case DW_OP_reg7:
- case DW_OP_reg8:
- case DW_OP_reg9:
- case DW_OP_reg10:
- case DW_OP_reg11:
- case DW_OP_reg12:
- case DW_OP_reg13:
- case DW_OP_reg14:
- case DW_OP_reg15:
- if (registers == NULL)
- return 0;
- BF_ASSERT((opCode - DW_OP_reg0) < CPURegisters::kNumIntRegs);
- regNum = opCode - DW_OP_reg0;
- stackFrameData[stackIdx++] = registers->mIntRegsArray[regNum];
- *outAddrType = DbgAddrType_Register;
- break;
- case DW_OP_reg21: //XMM0
- BF_FATAL("XMM registers not supported yet");
- break;
- case DW_OP_breg0:
- case DW_OP_breg1:
- case DW_OP_breg2:
- case DW_OP_breg3:
- case DW_OP_breg4:
- case DW_OP_breg5:
- case DW_OP_breg6:
- case DW_OP_breg7:
- case DW_OP_breg8:
- case DW_OP_breg9:
- case DW_OP_breg10:
- case DW_OP_breg11:
- case DW_OP_breg12:
- case DW_OP_breg13:
- case DW_OP_breg14:
- case DW_OP_breg15:
- {
- if (registers == NULL)
- return 0;
- int64 offset = DecodeSLEB128(locData);
- BF_ASSERT((opCode - DW_OP_breg0) < CPURegisters::kNumIntRegs);
- auto loc = registers->mIntRegsArray[opCode - DW_OP_breg0] + offset;
- //loc = BfDebuggerReadMemory(loc);
- //*outIsAddr = true;
- *outAddrType = DbgAddrType_Target;
- stackFrameData[stackIdx++] = (addr_target)loc;
- }
- break;
- case DW_OP_bregx:
- {
- if (registers == NULL)
- return 0;
- int regNum = (int)DecodeULEB128(locData);
- int64 offset = DecodeSLEB128(locData);
- BF_ASSERT(regNum < CPURegisters::kNumIntRegs);
- auto loc = registers->mIntRegsArray[regNum] + offset;
- //loc = BfDebuggerReadMemory(loc);
- //*outIsAddr = true;
- *outAddrType = DbgAddrType_Target;
- stackFrameData[stackIdx++] = (addr_target)loc;
- }
- break;
- case DW_OP_const4u:
- {
- uint32 val = GET_FROM(locData, uint32);
- stackFrameData[stackIdx++] = val;
- }
- break;
- case DW_OP_const8u:
- {
- uint64 val = GET_FROM(locData, uint64);
- stackFrameData[stackIdx++] = (addr_target)val;
- }
- break;
- case DW_OP_GNU_push_tls_address:
- {
- if ((mTLSAddr == 0) || (mTLSIndexAddr == 0))
- return 0;
- int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
- addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
- intptr_target tlsValueIndex = stackFrameData[--stackIdx];
- stackFrameData[stackIdx++] = (tlsValueIndex - mTLSAddr) + tlsEntry;
- *outAddrType = DbgAddrType_Target;
- }
- break;
- case DW_OP_nop:
- break;
- default:
- BF_FATAL("Unknown DW_OP");
- break;
- }
- }
- if (*outAddrType == DbgAddrType_Register)
- {
- if (allowReg)
- return regNum;
- *outAddrType = DbgAddrType_Value;
- }
- //BF_ASSERT(stackIdx == 1);
- return stackFrameData[--stackIdx];
- }
- addr_target DbgModule::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, bool allowReg)
- {
- BP_ZONE("DebugTarget::EvaluateLocation");
-
- auto dbgModule = this;
- if (locDataLen == DbgLocationLenKind_SegPlusOffset)
- {
- BF_ASSERT(dbgModule->mDbgFlavor == DbgFlavor_MS);
- if (dbgModule->mDbgFlavor == DbgFlavor_MS)
- {
- COFF* coff = (COFF*)dbgModule;
- struct SegOfsData
- {
- uint32 mOfs;
- uint16 mSeg;
- };
- SegOfsData* segOfsData = (SegOfsData*)locData;
- *outAddrType = DbgAddrType_Target;
- return coff->GetSectionAddr(segOfsData->mSeg, segOfsData->mOfs);
- }
- else
- {
- *outAddrType = DbgAddrType_Target;
- return 0;
- }
- }
- CPURegisters* registers = NULL;
- if (stackFrame != NULL)
- registers = &stackFrame->mRegisters;
- if (locDataLen < 0)
- {
- if (registers == NULL)
- return 0;
- int64 ipAddr = stackFrame->GetSourcePC();
- const uint8* checkLocData = locData;
- int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
- int64 endLoc = startLoc + GET_FROM(checkLocData, uint16);
- BF_ASSERT(dwSubprogram != NULL);
- startLoc += dwSubprogram->mCompileUnit->mLowPC;
- endLoc += dwSubprogram->mCompileUnit->mLowPC;
- if ((ipAddr >= startLoc) && (ipAddr < endLoc))
- {
- locDataLen = -locDataLen - sizeof(addr_target) - sizeof(uint16);
- locData = checkLocData;
- }
- else
- {
- *outAddrType = DbgAddrType_OptimizedOut;
- return 0;
- }
- }
- else if (locDataLen == 0)
- {
- if (registers == NULL)
- return 0;
- int64 ipAddr = stackFrame->GetSourcePC();
- const uint8* checkLocData = locData;
- while (true)
- {
- int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
- int64 endLoc = (int64)GET_FROM(checkLocData, addr_target);
- if ((startLoc == 0) && (endLoc == 0))
- {
- *outAddrType = DbgAddrType_OptimizedOut;
- return 0;
- }
- BF_ASSERT(dwSubprogram != NULL);
- startLoc += dwSubprogram->mCompileUnit->mLowPC;
- endLoc += dwSubprogram->mCompileUnit->mLowPC;
- if ((ipAddr >= startLoc) && (ipAddr < endLoc))
- {
- locDataLen = GET_FROM(checkLocData, int16);
- locData = checkLocData;
- break;
- }
- else
- {
- int len = GET_FROM(checkLocData, int16);;
- checkLocData += len;
- }
- }
- }
- return ExecuteOps(dwSubprogram, locData, locDataLen, stackFrame, registers, outAddrType, allowReg);
- }
- void DbgModule::ProcessHotSwapVariables()
- {
- BP_ZONE("DbgModule::ProcessHotSwapVariables");
- auto linkedModule = GetLinkedModule();
- for (auto staticVariable : mStaticVariables)
- {
- bool replaceVariable = false;
-
- const char* findName = staticVariable->GetMappedName();
- auto itr = linkedModule->mStaticVariableMap.find(findName);
- if (itr != linkedModule->mStaticVariableMap.end())
- {
- DbgVariable* oldVariable = itr->second;
- // If the old static field has the same type as the new static field then we keep the same
- // address, otherwise we use the new (zeroed-out) allocated space
- auto _GetNewAddress = [&]()
- {
- addr_target newAddress = 0;
- if (mDbgFlavor == DbgFlavor_GNU)
- {
- newAddress = mDebugTarget->GetStaticAddress(staticVariable);
- }
- else
- {
- // In CodeView, the newVariable ends up pointing to the old address, so we need to store
- // the location in our own mSymbolNameMap
- auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
- if (entry != NULL)
- newAddress = entry->mValue->mAddress;
- }
- return newAddress;
- };
- if (oldVariable->mType->IsSizedArray())
- {
- mDebugTarget->GetCompilerSettings();
-
- bool doMerge = strstr(oldVariable->mName, "sBfClassVData") != NULL;
- bool keepInPlace = (doMerge) && (strstr(oldVariable->mName, ".vext") == NULL);
- if (doMerge)
- {
- addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
- addr_target newAddress = _GetNewAddress();
- if (newAddress == 0)
- continue;
-
- uint8* newData = GetHotTargetData(newAddress);
- int newArraySize = staticVariable->mType->GetByteCount();
- int oldArraySize = oldVariable->mType->GetByteCount();
- int copySize = std::min(newArraySize, oldArraySize);
- BF_ASSERT((oldArraySize & (sizeof(addr_target) - 1)) == 0);
- DbgModule* defModule = oldVariable->mType->mCompileUnit->mDbgModule;
- defModule->EnableWriting(oldAddress);
- uint8* mergedData = new uint8[copySize];
- mDebugger->ReadMemory(oldAddress, copySize, mergedData);
- // The new vtable may have 0's in it when virtual methods are removed. Keep the old virtual addresses in those.
- addr_target* newDataPtr = (addr_target*)newData;
- addr_target* mergedPtr = (addr_target*)mergedData;
- while (mergedPtr < (addr_target*)(mergedData + copySize))
- {
- if (*newDataPtr != 0)
- *mergedPtr = *newDataPtr;
- mergedPtr++;
- newDataPtr++;
- }
- bool success;
- success = mDebugger->WriteMemory(oldAddress, mergedData, copySize);
- BF_ASSERT(success);
- memcpy(newData, mergedData, copySize);
- delete mergedData;
- }
- else if (strstr(oldVariable->mName, "sStringLiterals") != NULL)
- {
- addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
- addr_target newAddress = NULL;
- if (mDbgFlavor == DbgFlavor_GNU)
- {
- newAddress = mDebugTarget->GetStaticAddress(staticVariable);
- }
- else
- {
- // In CodeView, the newVariable ends up pointing to the old address, so we need to store
- // the location in our own mSymbolNameMap
- auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
- if (entry == NULL)
- continue;
- newAddress = entry->mValue->mAddress;
- }
- // Make sure newAddress doesn't have anything linked to it
- addr_target val = 0;
- bool success = mDebugger->ReadMemory((intptr)newAddress, sizeof(addr_target), &val);
- BF_ASSERT(success);
- BF_ASSERT(val == 0);
- // Link the new table to the old extended table
- addr_target prevLinkage = 0;
- success = mDebugger->ReadMemory((intptr)oldAddress, sizeof(addr_target), &prevLinkage);
- BF_ASSERT(success);
- success = mDebugger->WriteMemory((intptr)newAddress, &prevLinkage, sizeof(addr_target));
- BF_ASSERT(success);
- mDebugger->EnableWriting((intptr)oldAddress, sizeof(addr_target));
- success = mDebugger->WriteMemory((intptr)oldAddress, &newAddress, sizeof(addr_target));
- BF_ASSERT(success);
- keepInPlace = true;
- }
-
- if (keepInPlace)
- {
- // We have to maintain the OLD size because we can't overwrite the original bounds
- staticVariable->mType = oldVariable->mType;
- staticVariable->mLocationLen = oldVariable->mLocationLen;
- staticVariable->mLocationData = oldVariable->mLocationData;
- staticVariable->mCompileUnit = oldVariable->mCompileUnit;
- }
- }
- else if (oldVariable->mType->Equals(staticVariable->mType))
- {
- if (oldVariable->mType->IsStruct())
- {
- if ((strncmp(oldVariable->mName, "?sBfTypeData@", 13) == 0) || (strncmp(oldVariable->mName, "sBfTypeData.", 12) == 0))
- {
- int size = staticVariable->mType->GetByteCount();
- addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
- addr_target newAddress = _GetNewAddress();
- if (newAddress == 0)
- continue;
- uint8* data = new uint8[size];
- bool success = mDebugger->ReadMemory(newAddress, size, data);
- if (success)
- {
- mDebugger->EnableWriting((intptr)oldAddress, size);
- success = mDebugger->WriteMemory(oldAddress, data, size);
- }
- delete data;
- BF_ASSERT(success);
- staticVariable->mLocationLen = oldVariable->mLocationLen;
- staticVariable->mLocationData = oldVariable->mLocationData;
- }
- }
- //staticVariable->mLocationLen = oldVariable->mLocationLen;
- //staticVariable->mLocationData = oldVariable->mLocationData;
- replaceVariable = false;
- }
- else
- {
- BF_ASSERT(!oldVariable->mType->IsSizedArray());
- }
- if (!replaceVariable)
- {
- auto symbolVal = linkedModule->mSymbolNameMap.Find(staticVariable->GetMappedName());
- if (symbolVal != NULL)
- {
- addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
- DbgSymbol* oldSymbol = mDebugTarget->mSymbolMap.Get(oldAddress);
- if (oldSymbol != NULL)
- symbolVal->mValue = oldSymbol;
- }
- }
- }
- else // Not found - new variable
- replaceVariable = true;
- if (replaceVariable)
- {
- linkedModule->mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
- }
- }
- }
- int64 DbgModule::GetImageSize()
- {
- return mImageSize;
- }
- /*const uint8* DbgModule::GetOrigImageData(addr_target address)
- {
- return mOrigImageData + (address - mImageBase);
- }*/
- DbgFileExistKind DbgModule::CheckSourceFileExist(const StringImpl& path)
- {
- DbgFileExistKind existsKind = DbgFileExistKind_NotFound;
- if (FileExists(path))
- existsKind = DbgFileExistKind_Found;
- String oldSourceCommand = GetOldSourceCommand(path);
- if (!oldSourceCommand.IsEmpty())
- {
- int crPos = (int)oldSourceCommand.IndexOf('\n');
- if (crPos != -1)
- {
- String targetPath = oldSourceCommand.Substring(0, crPos);
- if (FileExists(targetPath))
- existsKind = DbgFileExistKind_Found;
- else
- existsKind = DbgFileExistKind_HasOldSourceCommand;
- }
- }
- return existsKind;
- }
- void DbgModule::EnableWriting(addr_target address)
- {
- for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
- {
- DbgSection* section = &mSections[sectionIdx];
- if ((address >= mImageBase + section->mAddrStart) && (address < mImageBase + section->mAddrStart + section->mAddrLength))
- {
- if (!section->mWritingEnabled)
- {
- section->mOldProt = mDebugger->EnableWriting(mImageBase + section->mAddrStart, (int32)section->mAddrLength);
- section->mWritingEnabled = true;
- }
- }
- }
- }
- void DbgModule::RevertWritingEnable()
- {
- for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
- {
- DbgSection* section = &mSections[sectionIdx];
- if (section->mWritingEnabled)
- {
- mDebugger->SetProtection(mImageBase + section->mAddrStart, (int32)section->mAddrLength, section->mOldProt);
- section->mWritingEnabled = false;
- }
- }
- }
- template <typename TRadixMap>
- static void RemoveInvalidRange(TRadixMap& radixMap, addr_target startAddr, int addrLength)
- {
- radixMap.RemoveRange(startAddr, addrLength);
- }
- template <typename TMap>
- static void RemoveInvalidMapRange(TMap& map, addr_target startAddr, int addrLength)
- {
- auto itr = map.lower_bound(startAddr);
- while (itr != map.end())
- {
- auto val = itr->first;
- if (val >= startAddr + addrLength)
- return;
- itr = map.erase(itr);
- }
- }
- void DbgModule::RemoveTargetData()
- {
- BP_ZONE("DbgModule::RemoveTargetData");
- for (auto srcFileRef : mSrcFileDeferredRefs)
- srcFileRef->RemoveDeferredRefs(this);
- HashSet<DbgSrcFile*> visitedFiles;
- for (auto compileUnit : mCompileUnits)
- {
- for (auto& fileRef : compileUnit->mSrcFileRefs)
- {
- if (visitedFiles.Add(fileRef.mSrcFile))
- {
- fileRef.mSrcFile->RemoveLines(this);
- }
- }
- }
- RemoveInvalidRange(mDebugTarget->mSymbolMap, (addr_target)mImageBase, (int32)mImageSize);
- RemoveInvalidRange(mDebugTarget->mSubprogramMap, (addr_target)mImageBase, (int32)mImageSize);
- RemoveInvalidRange(mDebugTarget->mExceptionDirectoryMap, (addr_target)mImageBase, (int32)mImageSize);
- RemoveInvalidRange(mDebugTarget->mContribMap, (addr_target)mImageBase, (int32)mImageSize);
- RemoveInvalidMapRange(mDebugTarget->mDwFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
- RemoveInvalidMapRange(mDebugTarget->mCOFFFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
- //mDebugTarget->mDwFrameDescriptorMap.erase()
- // Remove any of our entries from the mHotReplacedMethodList from 'primary modules' that are not going away
- for (auto dbgType : mHotPrimaryTypes)
- {
- DbgSubprogram** nextSrc = &dbgType->mHotReplacedMethodList.mHead;
- while (*nextSrc != NULL)
- {
- auto* subprogram = *nextSrc;
- if (subprogram->mCompileUnit->mDbgModule == this)
- *nextSrc = subprogram->mNext;
- else
- nextSrc = &(*nextSrc)->mNext;;
- }
- }
- }
- void DbgModule::ReportMemory(MemReporter* memReporter)
- {
- //memReporter->Add("BumpAlloc_Used", mAlloc.GetAllocSize());
- //memReporter->Add("BumpAlloc_Unused", mAlloc.GetTotalAllocSize() - mAlloc.GetAllocSize());
- memReporter->AddBumpAlloc("BumpAlloc", mAlloc);
- memReporter->AddVec(mTypes);
- memReporter->AddVec(mSubprograms);
- //memReporter->Add("TypeMap", mTypeMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
- memReporter->AddHashSet("TypeMap", mTypeMap.mMap);
- memReporter->Add("SymbolNameMap", mSymbolNameMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
- if (mOrigImageData != NULL)
- {
- memReporter->BeginSection("OrigImageData");
- mOrigImageData->ReportMemory(memReporter);
- memReporter->EndSection();
- }
- }
- DbgType* DbgModule::GetPointerType(DbgType* innerType)
- {
- auto linkedModule = GetLinkedModule();
- BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
- if (innerType->mPtrType == NULL)
- {
- BP_ALLOC_T(DbgType);
- auto ptrType = mAlloc.Alloc<DbgType>();
- ptrType->mCompileUnit = innerType->mCompileUnit;
- ptrType->mLanguage = innerType->mLanguage;
- ptrType->mTypeCode = DbgType_Ptr;
- ptrType->mTypeParam = innerType;
- ptrType->mSize = sizeof(addr_target);
- ptrType->mTypeIdx = (int32)linkedModule->mTypes.size();
- linkedModule->mTypes.push_back(ptrType);
- innerType->mPtrType = ptrType;
- }
- return innerType->mPtrType;
- }
- DbgType* DbgModule::GetConstType(DbgType* innerType)
- {
- auto linkedModule = GetLinkedModule();
- BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
-
- /*auto itr = linkedModule->mConstTypes.find(innerType);
- if (itr != linkedModule->mConstTypes.end())
- return itr->second;*/
- DbgType* constType = NULL;
- if (linkedModule->mConstTypes.TryGetValue(innerType, &constType))
- return constType;
- BP_ALLOC_T(DbgType);
- constType = mAlloc.Alloc<DbgType>();
- constType->mCompileUnit = innerType->mCompileUnit;
- constType->mLanguage = innerType->mLanguage;
- constType->mTypeCode = DbgType_Const;
- constType->mTypeParam = innerType;
- constType->mSize = sizeof(addr_target);
- constType->mTypeIdx = (int32)linkedModule->mTypes.size();
- linkedModule->mTypes.push_back(constType);
- linkedModule->mConstTypes[innerType] = constType;
- return constType;
- }
- DbgType* DbgModule::GetPrimaryType(DbgType* dbgType)
- {
- if (dbgType->mPriority <= DbgTypePriority_Normal)
- {
- if ((dbgType->mLanguage == DbgLanguage_Beef) && (dbgType->mName != NULL))
- {
- auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
- if (newTypeEntry != NULL)
- {
- DbgType* newType = newTypeEntry->mValue;
- if ((newType->mTypeCode == DbgType_Ptr) && (newType->IsBfObjectPtr()))
- newType = newType->mTypeParam;
- newType->mPriority = DbgTypePriority_Primary_Implicit;
- return newType;
- }
- }
- else if (dbgType->mName != NULL)
- {
- auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
- if (newTypeEntry != NULL)
- {
- DbgType* newType = newTypeEntry->mValue;
- newType = newType->RemoveModifiers();
- if (newType != dbgType)
- newType = GetPrimaryType(newType);
- newType->mPriority = DbgTypePriority_Primary_Implicit;
- return newType;
- }
- }
- }
- return dbgType;
- }
- DbgType* DbgModule::GetInnerTypeOrVoid(DbgType* dbgType)
- {
- if (dbgType->mTypeParam != NULL)
- return dbgType->mTypeParam;
- return GetPrimitiveType(DbgType_Void, dbgType->mLanguage);
- }
- DbgType* DbgModule::FindTypeHelper(const String& typeName, DbgType* checkType)
- {
- for (auto subType : checkType->mSubTypeList)
- {
- if (strcmp(subType->mTypeName, typeName.c_str()) == 0)
- return subType;
- }
- for (auto baseType : checkType->mBaseTypes)
- {
- auto retType = FindTypeHelper(typeName, baseType->mBaseType);
- if (retType != NULL)
- return retType;
- }
- return NULL;
- }
- DbgType* DbgModule::FindType(const String& typeName, DbgType* contextType, DbgLanguage language)
- {
- if ((language == DbgLanguage_Unknown) && (contextType != NULL))
- language = contextType->mLanguage;
- if (typeName.length() > 0)
- {
- if (typeName[typeName.length() - 1] == '*')
- {
- DbgType* dbgType = FindType(typeName.Substring(0, typeName.length() - 1), contextType, language);
- if (dbgType == NULL)
- return NULL;
- return GetPointerType(dbgType);
- }
- }
- auto entry = GetLinkedModule()->mTypeMap.Find(typeName.c_str(), language);
- if (entry != NULL)
- return entry->mValue;
- if (contextType != NULL)
- {
- DbgType* checkType = contextType;
- if (checkType->IsPointer())
- checkType = checkType->mTypeParam;
- return FindTypeHelper(typeName, checkType);
- }
- return NULL;
- }
- DbgTypeMap::Entry* DbgModule::FindType(const char* typeName, DbgLanguage language)
- {
- return GetLinkedModule()->mTypeMap.Find(typeName, language);
- /*auto& typeMap = GetLinkedModule()->mTypeMap;
- auto dbgTypeEntry = typeMap.Find(typeName);
- if (dbgTypeEntry == NULL)
- return NULL;
- if (dbgTypeEntry->mValue->mLanguage == language)
- return dbgTypeEntry;
- while (dbgTypeEntry != NULL)
- {
- DbgType* dbgType = dbgTypeEntry->mValue;
- if ((dbgType->GetLanguage() == language) && (typeMap.StrEqual(dbgType->mName, typeName)))
- return dbgTypeEntry;
- dbgTypeEntry = dbgTypeEntry->mNext;
- }*/
- //return NULL;
- }
- DbgType* DbgModule::GetPrimitiveType(DbgTypeCode typeCode, DbgLanguage language)
- {
- if (language == DbgLanguage_Beef)
- return mBfPrimitiveTypes[(int)typeCode];
- else
- return mCPrimitiveTypes[(int)typeCode];
- }
- DbgType* DbgModule::GetPrimitiveStructType(DbgTypeCode typeCode)
- {
- const char* name = mPrimitiveStructNames[typeCode];
- if (name == NULL)
- return NULL;
- return FindType(name, NULL, DbgLanguage_Beef);
- }
- DbgType* DbgModule::GetSizedArrayType(DbgType * elementType, int count)
- {
- auto linkedModule = GetLinkedModule();
- if ((linkedModule != NULL) && (linkedModule != this))
- {
- return linkedModule->GetSizedArrayType(elementType, count);
- }
- DbgType** sizedArrayTypePtr;
- DbgSizedArrayEntry entry;
- entry.mElementType = elementType;
- entry.mCount = count;
- if (mSizedArrayTypes.TryAdd(entry, NULL, &sizedArrayTypePtr))
- {
- BP_ALLOC_T(DbgType);
- auto sizedArrayType = mAlloc.Alloc<DbgType>();
- sizedArrayType->mCompileUnit = elementType->mCompileUnit;
- sizedArrayType->mLanguage = elementType->mLanguage;
- sizedArrayType->mTypeCode = DbgType_SizedArray;
- sizedArrayType->mTypeParam = elementType;
- sizedArrayType->mSize = count * elementType->GetStride();
- sizedArrayType->mAlign = elementType->GetAlign();
- sizedArrayType->mSizeCalculated = true;
- sizedArrayType->mTypeIdx = (int32)mTypes.size();
- linkedModule->mTypes.push_back(sizedArrayType);
- *sizedArrayTypePtr = sizedArrayType;
- }
- return *sizedArrayTypePtr;
- }
|