DbgModule.cpp 189 KB

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