DbgModule.cpp 195 KB

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