DbgModule.cpp 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034
  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. mDebugAbbrevPtrData = NULL;
  1693. mEHFrameData = NULL;
  1694. mEHFrameAddress = 0;
  1695. mStringTable = NULL;
  1696. mSymbolData = NULL;
  1697. mCheckedBfObject = false;
  1698. mBfObjectHasFlags = false;
  1699. mIsHotObjectFile = false;
  1700. mStartTypeIdx = 0;
  1701. mEndTypeIdx = 0;
  1702. mHotIdx = 0;
  1703. mStartSubprogramIdx = 0;
  1704. mEndSubprogramIdx = 0;
  1705. mCodeAddress = NULL;
  1706. mMayBeOld = false;
  1707. mTimeStamp = 0;
  1708. mExpectedFileSize = 0;
  1709. mImageBase = 0;
  1710. mPreferredImageBase = 0;
  1711. mImageSize = 0;
  1712. mOrigImageData = NULL;
  1713. mDeleting = false;
  1714. mAllocSizeData = 0;
  1715. mParsedSymbolData = false;
  1716. mParsedTypeData = false;
  1717. mParsedGlobalsData = false;
  1718. mPopulatedStaticVariables = false;
  1719. mParsedFrameDescriptors = false;
  1720. mTLSAddr = 0;
  1721. mTLSSize = 0;
  1722. mTLSExtraAddr = 0;
  1723. mTLSExtraSize = 0;
  1724. mTLSIndexAddr = 0;
  1725. mDbgFlavor = DbgFlavor_Unknown;
  1726. mMasterCompileUnit = NULL;
  1727. }
  1728. DbgModule::~DbgModule()
  1729. {
  1730. delete mMemReporter;
  1731. for (auto dwSrcFile : mEmptySrcFiles)
  1732. delete dwSrcFile;
  1733. for (auto dwCompileUnit : mCompileUnits)
  1734. delete dwCompileUnit;
  1735. delete mSymbolData;
  1736. delete mStringTable;
  1737. delete mDebugLineData;
  1738. delete mDebugInfoData;
  1739. delete mDebugPubNames;
  1740. delete mDebugFrameData;
  1741. delete mDebugLocationData;
  1742. delete mDebugRangesData;
  1743. delete mDebugAbbrevData;
  1744. delete mDebugAbbrevPtrData;
  1745. delete mDebugStrData;
  1746. for (auto entry : mExceptionDirectory)
  1747. delete entry.mData;
  1748. delete mEHFrameData;
  1749. delete mOrigImageData;
  1750. if ((mIsHotObjectFile) && (mImageBase != 0))
  1751. {
  1752. mDebugger->ReleaseHotTargetMemory((addr_target)mImageBase, (int)mImageSize);
  1753. }
  1754. for (auto data : mOwnedSectionData)
  1755. delete data;
  1756. }
  1757. void DbgModule::Fail(const StringImpl& error)
  1758. {
  1759. if (mFailMsgPtr != NULL)
  1760. {
  1761. if (mFailMsgPtr->IsEmpty())
  1762. *mFailMsgPtr = error;
  1763. }
  1764. mDebugger->OutputRawMessage("error " + error + "\n");
  1765. mFailed = true;
  1766. }
  1767. char* DbgModule::DbgDupString(const char* str, const char* allocName)
  1768. {
  1769. int strLen = (int)strlen(str);
  1770. if (strLen == 0)
  1771. return NULL;
  1772. char* dupStr = (char*)mAlloc.AllocBytes(strLen + 1, (allocName != NULL) ? allocName : "DbgDupString");
  1773. memcpy(dupStr, str, strLen);
  1774. return dupStr;
  1775. }
  1776. DbgModule* DbgModule::GetLinkedModule()
  1777. {
  1778. if (mIsHotObjectFile)
  1779. return mDebugTarget->mTargetBinary;
  1780. return this;
  1781. }
  1782. addr_target DbgModule::GetTargetImageBase()
  1783. {
  1784. if (mIsHotObjectFile)
  1785. return (addr_target)mDebugTarget->mTargetBinary->mImageBase;
  1786. return (addr_target)mImageBase;
  1787. }
  1788. void DbgModule::ParseGlobalsData()
  1789. {
  1790. mParsedGlobalsData = true;
  1791. }
  1792. void DbgModule::ParseSymbolData()
  1793. {
  1794. mParsedSymbolData = true;
  1795. }
  1796. void DbgModule::ParseTypeData()
  1797. {
  1798. mParsedTypeData = true;
  1799. }
  1800. DbgCompileUnit* DbgModule::ParseCompileUnit(int compileUnitId)
  1801. {
  1802. return NULL;
  1803. }
  1804. void DbgModule::MapCompileUnitMethods(DbgCompileUnit * compileUnit)
  1805. {
  1806. }
  1807. void DbgModule::MapCompileUnitMethods(int compileUnitId)
  1808. {
  1809. }
  1810. void DbgModule::PopulateType(DbgType* dbgType)
  1811. {
  1812. }
  1813. void DbgModule::PopulateTypeGlobals(DbgType* dbgType)
  1814. {
  1815. }
  1816. void DbgModule::PopulateStaticVariableMap()
  1817. {
  1818. if (mPopulatedStaticVariables)
  1819. return;
  1820. for (auto staticVariable : mStaticVariables)
  1821. {
  1822. mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  1823. }
  1824. mPopulatedStaticVariables = true;
  1825. }
  1826. void DbgModule::ProcessDebugInfo()
  1827. {
  1828. }
  1829. addr_target DbgModule::RemapAddr(addr_target addr)
  1830. {
  1831. if ((addr != 0) && (mPreferredImageBase != 0) && (mImageBase != 0))
  1832. return addr + (intptr_target)(mImageBase - mPreferredImageBase);
  1833. return addr;
  1834. }
  1835. void DbgModule::ParseAbbrevData(const uint8* data)
  1836. {
  1837. while (true)
  1838. {
  1839. int abbrevIdx = (int)DecodeULEB128(data);
  1840. mDebugAbbrevPtrData[abbrevIdx] = data;
  1841. if (abbrevIdx == 0)
  1842. break;
  1843. int entryTag = (int)DecodeULEB128(data);
  1844. bool hasChildren = GET(char) == DW_CHILDREN_yes;
  1845. while (true)
  1846. {
  1847. int attrName = (int)DecodeULEB128(data);
  1848. int form = (int)DecodeULEB128(data);
  1849. if ((attrName == 0) && (form == 0))
  1850. break;
  1851. }
  1852. }
  1853. }
  1854. void DbgModule::ParseExceptionData()
  1855. {
  1856. if (mExceptionDirectory.IsEmpty())
  1857. return;
  1858. BP_ZONE("DbgModule::ParseExceptionData");
  1859. for (auto entry : mExceptionDirectory)
  1860. {
  1861. const uint8* data = entry.mData;
  1862. const uint8* dataEnd = data + entry.mSize;
  1863. static int entryCount = 0;
  1864. addr_target imageBase = GetTargetImageBase();
  1865. while (data < dataEnd)
  1866. {
  1867. addr_target beginAddress = GET(uint32);
  1868. addr_target endAddress = GET(uint32);
  1869. uint32 unwindData = GET(uint32);
  1870. //TODO: Apparently unwindData can refer to another runtime entry in the .pdata if the LSB is set to 1?
  1871. beginAddress += (addr_target)imageBase;
  1872. endAddress += (addr_target)imageBase;
  1873. int exSize = (int)(endAddress - beginAddress);
  1874. for (int exOffset = 0; true; exOffset += DBG_MAX_LOOKBACK)
  1875. {
  1876. int curSize = exSize - exOffset;
  1877. if (curSize <= 0)
  1878. break;
  1879. BP_ALLOC_T(DbgExceptionDirectoryEntry);
  1880. DbgExceptionDirectoryEntry* exceptionDirectoryEntry = mAlloc.Alloc<DbgExceptionDirectoryEntry>();
  1881. exceptionDirectoryEntry->mAddress = beginAddress + exOffset;
  1882. exceptionDirectoryEntry->mOrigAddressOffset = exOffset;
  1883. exceptionDirectoryEntry->mAddressLength = curSize;
  1884. exceptionDirectoryEntry->mExceptionPos = (int)unwindData;
  1885. exceptionDirectoryEntry->mDbgModule = this;
  1886. mDebugTarget->mExceptionDirectoryMap.Insert(exceptionDirectoryEntry);
  1887. entryCount++;
  1888. }
  1889. }
  1890. }
  1891. }
  1892. static int gIdx = 0;
  1893. template <typename T> static bool IsTypeSigned() { return false; }
  1894. template <> bool IsTypeSigned<int8>() { return true; }
  1895. template <> bool IsTypeSigned<int16>() { return true; }
  1896. template <> bool IsTypeSigned<int32>() { return true; }
  1897. template <> bool IsTypeSigned<int64>() { return true; }
  1898. #pragma warning(push)
  1899. #pragma warning(disable:4302)
  1900. #pragma warning(disable:4311)
  1901. #pragma warning(disable:4312)
  1902. #pragma warning(disable:4800)
  1903. #pragma warning(disable:4800)
  1904. template <typename T>
  1905. T DbgModule::ReadValue(const uint8*& data, int form, int refOffset, const uint8** extraData, const uint8* startData)
  1906. {
  1907. gIdx++;
  1908. switch (form)
  1909. {
  1910. case DW_FORM_strp:
  1911. {
  1912. int strOffset = GET(int);
  1913. BF_ASSERT(mDebugStrData != NULL);
  1914. const char* str = (const char*)mDebugStrData + strOffset;
  1915. return (T)(intptr)str;
  1916. }
  1917. break;
  1918. case DW_FORM_data1:
  1919. {
  1920. if (IsTypeSigned<T>())
  1921. return (T)(intptr)GET(int8);
  1922. else
  1923. return (T)(uintptr)GET(uint8);
  1924. }
  1925. break;
  1926. case DW_FORM_data2:
  1927. {
  1928. if (IsTypeSigned<T>())
  1929. return (T)(intptr)GET(int16);
  1930. else
  1931. return (T)(uintptr)GET(uint16);
  1932. }
  1933. break;
  1934. case DW_FORM_data4:
  1935. {
  1936. if (IsTypeSigned<T>())
  1937. return (T)(intptr)GET(int32);
  1938. else
  1939. return (T)(uintptr)GET(uint32);
  1940. }
  1941. break;
  1942. case DW_FORM_data8:
  1943. {
  1944. if (IsTypeSigned<T>())
  1945. return (T)GET(int64);
  1946. else
  1947. return (T)GET(uint64);
  1948. }
  1949. break;
  1950. case DW_FORM_ref1:
  1951. {
  1952. return (T)(intptr)GET(int8) + refOffset;
  1953. }
  1954. break;
  1955. case DW_FORM_ref2:
  1956. {
  1957. return (T)(intptr)GET(int16) + refOffset;
  1958. }
  1959. break;
  1960. case DW_FORM_ref4:
  1961. {
  1962. return (T)(intptr)GET(int32) + refOffset;
  1963. }
  1964. break;
  1965. case DW_FORM_sec_offset:
  1966. {
  1967. intptr_target offset;
  1968. if (mIsDwarf64)
  1969. offset = (intptr_target)GET(int64);
  1970. else
  1971. offset = GET(int32);
  1972. if (extraData != NULL)
  1973. {
  1974. *extraData = mDebugLocationData + offset;
  1975. return 0;
  1976. }
  1977. return (T)offset;
  1978. }
  1979. break;
  1980. case DW_FORM_addr:
  1981. {
  1982. return (T)GET(addr_target);
  1983. }
  1984. break;
  1985. case DW_FORM_exprloc:
  1986. {
  1987. int64_t exprLen = DecodeULEB128(data);
  1988. const uint8* endData = data + exprLen;
  1989. if (extraData != NULL)
  1990. *extraData = data;
  1991. data = endData;
  1992. return (T)exprLen;
  1993. }
  1994. break;
  1995. case DW_FORM_flag_present:
  1996. {
  1997. //
  1998. return (T)1;
  1999. }
  2000. break;
  2001. case DW_FORM_flag:
  2002. {
  2003. //
  2004. return (T)(intptr)GET(char);
  2005. }
  2006. break;
  2007. case DW_FORM_sdata:
  2008. return (T)DecodeSLEB128(data);
  2009. case DW_FORM_udata:
  2010. return (T)DecodeULEB128(data);
  2011. case DW_FORM_string:
  2012. {
  2013. const char* str = (const char*)data;
  2014. while (true)
  2015. {
  2016. uint8 val = *data;
  2017. data++;
  2018. if (val == 0)
  2019. return (T)(intptr)str;
  2020. }
  2021. }
  2022. case DW_FORM_block:
  2023. {
  2024. int blockLen = (int)DecodeULEB128(data);
  2025. const uint8* retVal = data;
  2026. data += blockLen;
  2027. return (T)(intptr)retVal;
  2028. }
  2029. case DW_FORM_block1:
  2030. {
  2031. int blockLen = (int)*((uint8*)data);
  2032. data += sizeof(uint8);
  2033. const uint8* retVal = data;
  2034. data += blockLen;
  2035. return (T)(intptr)retVal;
  2036. }
  2037. default:
  2038. assert("Not covered!" == 0);
  2039. break;
  2040. }
  2041. return (T)0;
  2042. }
  2043. #pragma warning(pop)
  2044. static int gAbbrevNum = 0;
  2045. DbgType* DbgModule::GetOrCreateType(int typeIdx, DbgDataMap& dataMap)
  2046. {
  2047. if (typeIdx == 0)
  2048. return NULL;
  2049. DbgModule* linkedModule = GetLinkedModule();
  2050. DbgType* dbgType = dataMap.Get<DbgType*>(typeIdx);
  2051. if (dbgType != NULL)
  2052. return dbgType;
  2053. dbgType = mAlloc.Alloc<DbgType>();
  2054. dbgType->mTypeIdx = (int)linkedModule->mTypes.size();
  2055. linkedModule->mTypes.push_back(dbgType);
  2056. dataMap.Set(typeIdx, dbgType);
  2057. return dbgType;
  2058. }
  2059. typedef std::pair<DbgClassType, void*> DataPair;
  2060. typedef llvm::SmallVector<DataPair, 16> DataStack;
  2061. template <typename T>
  2062. T DbgModule::GetOrCreate(int idx, DbgDataMap& dataMap)
  2063. {
  2064. if (idx == 0)
  2065. return NULL;
  2066. T val = dataMap.Get<T>(idx);
  2067. if (val != NULL)
  2068. return val;
  2069. val = mAlloc.Alloc<typename RemoveTypePointer<T>::type >();
  2070. dataMap.Set(idx, val);
  2071. return val;
  2072. }
  2073. template <typename T>
  2074. static T GetStackTop(DataStack* dataStack)
  2075. {
  2076. auto dataPair = dataStack->back();
  2077. if (dataPair.first == RemoveTypePointer<T>::type::ClassType)
  2078. return (T)dataPair.second;
  2079. return NULL;
  2080. }
  2081. template <>
  2082. DbgBlock* GetStackTop<DbgBlock*>(DataStack* dataStack)
  2083. {
  2084. auto dataPair = dataStack->back();
  2085. if (dataPair.first == DbgBlock::ClassType)
  2086. return (DbgBlock*)dataPair.second;
  2087. if (dataPair.first == DbgSubprogram::ClassType)
  2088. return &((DbgSubprogram*)dataPair.second)->mBlock;
  2089. if (dataPair.first == DbgType::ClassType)
  2090. return ((DbgType*)dataPair.second)->mBlockParam;
  2091. return NULL;
  2092. }
  2093. template <typename T>
  2094. static bool StackHasType(DataStack* dataStack)
  2095. {
  2096. for (auto itr : *dataStack)
  2097. if (itr.first == RemoveTypePointer<T>::type::ClassType)
  2098. return true;
  2099. return false;
  2100. }
  2101. template <typename T>
  2102. static T GetStackLast(DataStack* dataStack)
  2103. {
  2104. for (int i = (int)dataStack->size() - 1; i >= 0; i--)
  2105. {
  2106. if ((*dataStack)[i].first == RemoveTypePointer<T>::type::ClassType)
  2107. return (T)(*dataStack)[i].second;
  2108. }
  2109. return NULL;
  2110. }
  2111. template <typename T>
  2112. static DataPair MakeDataPair(T* data)
  2113. {
  2114. return DataPair(T::ClassType, data);
  2115. }
  2116. void DbgModule::FixupInnerTypes(int startingTypeIdx)
  2117. {
  2118. BP_ZONE("DbgModule_FixupInnerTypes");
  2119. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2120. {
  2121. DbgType* dbgType = mTypes[typeIdx];
  2122. if ((dbgType->mPriority == DbgTypePriority_Primary_Implicit) && (dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) &&
  2123. (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2124. {
  2125. auto primaryParent = dbgType->mParent->GetPrimaryType();
  2126. dbgType->mParent->mSubTypeList.Clear();
  2127. dbgType->mParent = primaryParent;
  2128. primaryParent->mSubTypeList.PushBack(dbgType);
  2129. }
  2130. }
  2131. }
  2132. void DbgModule::MapTypes(int startingTypeIdx)
  2133. {
  2134. BP_ZONE("DbgModule_MapTypes");
  2135. bool needsInnerFixups = false;
  2136. for (int typeIdx = startingTypeIdx; typeIdx < (int)mTypes.size(); typeIdx++)
  2137. {
  2138. DbgType* dbgType = mTypes[typeIdx];
  2139. BF_ASSERT(dbgType->mTypeCode != DbgType_Null);
  2140. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mPriority < DbgTypePriority_Primary_Implicit))
  2141. continue;
  2142. //TODO: Always valid?
  2143. if (dbgType->mIsDeclaration)
  2144. continue;
  2145. // We were avoiding adding '<' names before, but that made it impossible to look up auto-named primary types ,
  2146. // like in-place unions like '<unnamed-type-u>'
  2147. if ((dbgType->mTypeName == NULL) || (dbgType->mName == NULL) /*|| (dbgType->mTypeName[0] == '<')*/)
  2148. continue;
  2149. if (dbgType->mTypeCode > DbgType_DefinitionEnd)
  2150. {
  2151. // Only add "definition types"
  2152. continue;
  2153. }
  2154. if (dbgType->mTypeCode == DbgType_Namespace)
  2155. {
  2156. bool isQualifiedNamespace = false;
  2157. for (const char* cPtr = dbgType->mTypeName; *cPtr != '\0'; cPtr++)
  2158. if (*cPtr == '.')
  2159. isQualifiedNamespace = true;
  2160. if (isQualifiedNamespace)
  2161. continue; // Don't add fully qualified namespaces (they come from the 'using' implementation)*
  2162. }
  2163. if (dbgType->mHasStaticMembers)
  2164. {
  2165. for (auto member : dbgType->mMemberList)
  2166. if ((member->mIsStatic) && (member->mLocationData != NULL))
  2167. dbgType->mDefinedMembersCount++;
  2168. }
  2169. if ((dbgType->mTypeName != NULL) && (strcmp(dbgType->mTypeName, "@") == 0))
  2170. {
  2171. // Globals type.
  2172. continue;
  2173. }
  2174. auto prevTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  2175. // Only replace previous instance if its a declaration
  2176. if (prevTypeEntry != NULL)
  2177. {
  2178. auto prevType = prevTypeEntry->mValue;
  2179. if (dbgType->mCompileUnit->mDbgModule != prevType->mCompileUnit->mDbgModule)
  2180. {
  2181. // Don't replace original types with hot types -- those need to be inserted in the the hot alternates list
  2182. BF_ASSERT(dbgType->mCompileUnit->mDbgModule->mIsHotObjectFile);
  2183. prevType->mHotNewType = dbgType;
  2184. continue;
  2185. }
  2186. // Never override explicit primaries
  2187. if (prevType->mPriority == DbgTypePriority_Primary_Explicit)
  2188. continue;
  2189. if (dbgType->mTypeCode == DbgType_TypeDef)
  2190. {
  2191. // Typedef can never override anything
  2192. continue;
  2193. }
  2194. if (prevType->mTypeCode == DbgType_TypeDef)
  2195. {
  2196. if (dbgType->mTypeCode != DbgType_TypeDef)
  2197. {
  2198. // Allow this to override
  2199. prevTypeEntry->mValue = dbgType;
  2200. }
  2201. continue;
  2202. }
  2203. // Don't replace a ptr to an BfObject with a BfObject
  2204. if ((prevType->mTypeCode == DbgType_Ptr) && (dbgType->mTypeCode == DbgType_Struct))
  2205. continue;
  2206. if ((prevType->mTypeCode == DbgType_Struct) && (dbgType->mTypeCode == DbgType_Ptr))
  2207. {
  2208. // Allow this to override
  2209. prevTypeEntry->mValue = dbgType;
  2210. continue;
  2211. }
  2212. if (prevType->mTypeCode == DbgType_Namespace)
  2213. {
  2214. if (dbgType->mTypeCode != DbgType_Namespace)
  2215. {
  2216. // Old type was namespace but new isn't? Replace old type.
  2217. while (!prevType->mSubTypeList.IsEmpty())
  2218. {
  2219. DbgType* subType = prevType->mSubTypeList.PopFront();
  2220. subType->mParent = dbgType;
  2221. dbgType->mSubTypeList.PushBack(subType);
  2222. }
  2223. prevType->mPriority = DbgTypePriority_Normal;
  2224. if (dbgType->mPriority < DbgTypePriority_Primary_Implicit)
  2225. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2226. prevTypeEntry->mValue = dbgType;
  2227. continue;
  2228. }
  2229. // We definitely didn't want to do this for MS. For DWARF?
  2230. //prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2231. continue;
  2232. }
  2233. else
  2234. {
  2235. // New type is namespace but old wasn't? Ignore new type.
  2236. if (dbgType->mTypeCode == DbgType_Namespace)
  2237. continue;
  2238. if (dbgType->mIsDeclaration)
  2239. continue;
  2240. if (!prevType->mIsDeclaration)
  2241. {
  2242. if ((prevType->mCompileUnit == NULL) || (dbgType->mLanguage < prevType->mLanguage))
  2243. {
  2244. // We always want 'Beef' types to supersede 'C' types, but don't override the built-in primitive types
  2245. continue;
  2246. }
  2247. if (prevType->mDefinedMembersCount > 0)
  2248. {
  2249. if (dbgType->mDefinedMembersCount > 0)
  2250. {
  2251. // We create an 'alternates' list for all types that define at least one static field
  2252. prevType->mAlternates.PushFront(dbgType, &mAlloc);
  2253. }
  2254. continue;
  2255. }
  2256. if (prevType->mMethodsWithParamsCount > dbgType->mMethodsWithParamsCount)
  2257. {
  2258. // This handles a special case where methods without line data like <Enum>.HasFlags doesn't show containing
  2259. // params in cases where it gets inlined
  2260. continue;
  2261. }
  2262. // Types with method lists are preferred
  2263. if ((!prevType->mMethodList.IsEmpty()) && (dbgType->mMethodList.IsEmpty()))
  2264. continue;
  2265. if ((prevType->mTypeCode == DbgType_Ptr) && (prevType->mTypeParam != NULL) && (!prevType->mTypeParam->mMethodList.IsEmpty()))
  2266. continue;
  2267. }
  2268. // Replace type
  2269. if (!prevType->mSubTypeList.IsEmpty())
  2270. needsInnerFixups = true;
  2271. prevType->mPriority = DbgTypePriority_Normal;
  2272. if (dbgType->mPriority == DbgTypePriority_Normal)
  2273. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2274. prevTypeEntry->mValue = dbgType;
  2275. continue;
  2276. }
  2277. }
  2278. if ((dbgType->mParent != NULL) && (dbgType->mParent->mTypeCode != DbgType_Namespace) && (dbgType->mParent->mPriority <= DbgTypePriority_Primary_Implicit))
  2279. needsInnerFixups = true;
  2280. if (dbgType->mPriority == DbgTypePriority_Normal)
  2281. dbgType->mPriority = DbgTypePriority_Primary_Implicit;
  2282. mTypeMap.Insert(dbgType);
  2283. }
  2284. if (needsInnerFixups)
  2285. FixupInnerTypes(startingTypeIdx);
  2286. }
  2287. void DbgModule::CreateNamespaces()
  2288. {
  2289. BP_ZONE("DbgModule::CreateNamespaces");
  2290. int startLength = (int)mTypes.size();
  2291. for (int typeIdx = 0; typeIdx < startLength; typeIdx++)
  2292. {
  2293. DbgType* dbgType = mTypes[typeIdx];
  2294. if (dbgType->mName == NULL)
  2295. continue;
  2296. if ((dbgType->mTypeCode == DbgType_Namespace) && (dbgType->mTagIdx != 0))
  2297. {
  2298. auto namespaceTypeEntry = FindType(dbgType->mName, dbgType->GetLanguage());
  2299. DbgType* namespaceType;
  2300. if (namespaceTypeEntry == NULL)
  2301. {
  2302. namespaceType = mAlloc.Alloc<DbgType>();
  2303. namespaceType->mTypeCode = DbgType_Namespace;
  2304. namespaceType->mLanguage = dbgType->mLanguage;
  2305. namespaceType->mCompileUnit = dbgType->mCompileUnit;
  2306. namespaceType->mTypeIdx = (int)mTypes.size();
  2307. namespaceType->mPriority = DbgTypePriority_Primary_Explicit;
  2308. namespaceType->mName = dbgType->mName;
  2309. namespaceType->mTypeName = dbgType->mTypeName;
  2310. if (dbgType->mParent != NULL)
  2311. {
  2312. namespaceType->mParent = dbgType->mParent->GetPrimaryType();
  2313. namespaceType->mParent->mSubTypeList.PushBack(namespaceType);
  2314. }
  2315. else
  2316. {
  2317. namespaceType->mCompileUnit->mGlobalType->mSubTypeList.PushBack(namespaceType);
  2318. }
  2319. mTypes.push_back(namespaceType);
  2320. mTypeMap.Insert(namespaceType);
  2321. }
  2322. else
  2323. namespaceType = namespaceTypeEntry->mValue;
  2324. while (!dbgType->mMemberList.IsEmpty())
  2325. {
  2326. DbgVariable* curVar = dbgType->mMemberList.PopFront();
  2327. namespaceType->mMemberList.PushBack(curVar);
  2328. }
  2329. DbgType* prevType = NULL;
  2330. DbgType* curType = dbgType->mSubTypeList.mHead;
  2331. while (curType != NULL)
  2332. {
  2333. DbgType* nextType = curType->mNext;
  2334. if (curType->mPriority >= DbgTypePriority_Primary_Implicit)
  2335. {
  2336. dbgType->mSubTypeList.Remove(curType, prevType);
  2337. namespaceType->mSubTypeList.PushBack(curType);
  2338. }
  2339. prevType = curType;
  2340. curType = nextType;
  2341. }
  2342. continue;
  2343. }
  2344. }
  2345. // If we didn't have a parent type for a namespace (ie: if System.Collections wasn't linked to System) then we wait
  2346. // until the end and move those from the global list to the parent list
  2347. for (int typeIdx = startLength; typeIdx < (int)mTypes.size(); typeIdx++)
  2348. {
  2349. DbgType* dbgType = mTypes[typeIdx];
  2350. if (dbgType->mParent != NULL)
  2351. continue;
  2352. char* typeName = (char*)dbgType->mTypeName;
  2353. int lastDotIdx = -1;
  2354. for (int i = 0; true; i++)
  2355. {
  2356. char c = typeName[i];
  2357. if (c == 0)
  2358. break;
  2359. if (c == '.')
  2360. lastDotIdx = i;
  2361. }
  2362. if (lastDotIdx == -1)
  2363. continue;
  2364. typeName[lastDotIdx] = 0;
  2365. dbgType->mTypeName = typeName + lastDotIdx + 1;
  2366. auto parentEntry = FindType(typeName, dbgType->GetLanguage());
  2367. typeName[lastDotIdx] = '.';
  2368. if (parentEntry == NULL)
  2369. continue;
  2370. auto parentType = parentEntry->mValue;
  2371. dbgType->mCompileUnit->mGlobalType->mSubTypeList.Remove(dbgType);
  2372. dbgType->mParent = parentType;
  2373. parentType->mSubTypeList.PushBack(dbgType);
  2374. }
  2375. }
  2376. void DbgModule::FindTemplateStr(const char*& name, int& templateNameIdx)
  2377. {
  2378. if (templateNameIdx == 0)
  2379. {
  2380. for (int i = 0; name[i] != 0; i++)
  2381. {
  2382. if (name[i] == '<')
  2383. {
  2384. templateNameIdx = i;
  2385. return;
  2386. }
  2387. }
  2388. templateNameIdx = -1;
  2389. }
  2390. }
  2391. void DbgModule::TempRemoveTemplateStr(const char*& name, int& templateNameIdx)
  2392. {
  2393. if (templateNameIdx == 0)
  2394. FindTemplateStr(name, templateNameIdx);
  2395. if (templateNameIdx == -1)
  2396. return;
  2397. if (!DbgIsStrMutable(name))
  2398. name = DbgDupString(name);
  2399. ((char*)name)[templateNameIdx] = 0;
  2400. }
  2401. void DbgModule::ReplaceTemplateStr(const char*& name, int& templateNameIdx)
  2402. {
  2403. if (templateNameIdx > 0)
  2404. ((char*)name)[templateNameIdx] = '<';
  2405. }
  2406. void DbgModule::MapSubprogram(DbgSubprogram* dbgSubprogram)
  2407. {
  2408. if (dbgSubprogram->mBlock.IsEmpty())
  2409. return;
  2410. int progSize = (int)(dbgSubprogram->mBlock.mHighPC - dbgSubprogram->mBlock.mLowPC);
  2411. for (int progOffset = 0; true; progOffset += DBG_MAX_LOOKBACK)
  2412. {
  2413. int curSize = progSize - progOffset;
  2414. if (curSize <= 0)
  2415. break;
  2416. BP_ALLOC_T(DbgSubprogramMapEntry);
  2417. DbgSubprogramMapEntry* subprogramMapEntry = mAlloc.Alloc<DbgSubprogramMapEntry>();
  2418. subprogramMapEntry->mAddress = dbgSubprogram->mBlock.mLowPC + progOffset;
  2419. subprogramMapEntry->mEntry = dbgSubprogram;
  2420. mDebugTarget->mSubprogramMap.Insert(subprogramMapEntry);
  2421. }
  2422. }
  2423. bool DbgModule::ParseDWARF(const uint8*& dataPtr)
  2424. {
  2425. BP_ZONE("ParseDWARF");
  2426. const uint8* data = dataPtr;
  2427. const uint8* startData = mDebugInfoData;
  2428. int dataOfs = (int)(data - mDebugInfoData);
  2429. intptr_target length = GET(int);
  2430. DbgModule* linkedModule = GetLinkedModule();
  2431. if (length == -1)
  2432. {
  2433. mIsDwarf64 = true;
  2434. length = (intptr_target)GET(int64);
  2435. }
  2436. else
  2437. mIsDwarf64 = false;
  2438. if (length == 0)
  2439. return false;
  2440. const uint8* dataEnd = data + length;
  2441. int version = GET(short);
  2442. int abbrevOffset = GET(int);
  2443. char pointerSize = GET(char);
  2444. ParseAbbrevData(mDebugAbbrevData + abbrevOffset);
  2445. DbgCompileUnit* compileUnit = new DbgCompileUnit(this);
  2446. mDbgFlavor = DbgFlavor_GNU;
  2447. compileUnit->mDbgModule = this;
  2448. mCompileUnits.push_back(compileUnit);
  2449. DbgSubprogram* subProgram = NULL;
  2450. //std::map<int, DbgType*> typeMap;
  2451. //std::map<int, DbgSubprogram*> subprogramMap;
  2452. int tagStart = (int)(data - startData);
  2453. int tagEnd = (int)(dataEnd - startData);
  2454. DbgDataMap dataMap(tagStart, tagEnd);
  2455. DataStack dataStack;
  2456. Array<AbstractOriginEntry> abstractOriginReplaceList;
  2457. Array<int> deferredArrayDims;
  2458. int startingTypeIdx = (int)linkedModule->mTypes.size();
  2459. while (data < dataEnd)
  2460. {
  2461. gAbbrevNum++;
  2462. const uint8* tagDataStart = data;
  2463. int tagIdx = (int)(tagDataStart - startData);
  2464. int abbrevIdx = (int)DecodeULEB128(data);
  2465. const uint8* abbrevData = mDebugAbbrevPtrData[abbrevIdx];
  2466. if (abbrevIdx == 0)
  2467. {
  2468. if (deferredArrayDims.size() > 0)
  2469. {
  2470. DbgType* arrType = GetStackTop<DbgType*>(&dataStack);
  2471. BF_ASSERT(arrType->mTypeCode == DbgType_SizedArray);
  2472. arrType->mSize = deferredArrayDims[0]; // Byte count still needs to be multiplied by the underlying type size
  2473. DbgType* rootArrType = arrType;
  2474. for (int dimIdx = 0; dimIdx < (int)deferredArrayDims.size() - 1; dimIdx++)
  2475. {
  2476. int dimSize = deferredArrayDims[dimIdx];
  2477. DbgType* subArrType = mAlloc.Alloc<DbgType>();
  2478. subArrType->mCompileUnit = compileUnit;
  2479. subArrType->mLanguage = compileUnit->mLanguage;
  2480. subArrType->mTypeIdx = (int)linkedModule->mTypes.size();
  2481. linkedModule->mTypes.push_back(subArrType);
  2482. subArrType->mTypeCode = DbgType_SizedArray;
  2483. subArrType->mTypeParam = arrType->mTypeParam;
  2484. subArrType->mSize = deferredArrayDims[dimIdx + 1];
  2485. arrType->mTypeParam = subArrType;
  2486. arrType = subArrType;
  2487. }
  2488. deferredArrayDims.Clear();
  2489. }
  2490. dataStack.pop_back();
  2491. continue;
  2492. }
  2493. int entryTag = (int) DecodeULEB128(abbrevData);
  2494. bool hasChildren = GET_FROM(abbrevData, char) == DW_CHILDREN_yes;
  2495. int64 atLowPC = 0;
  2496. int64 atHighPC = 0;
  2497. int64 atRanges = 0;
  2498. bool hasRanges = false;
  2499. const uint8* atFrameBase = NULL;
  2500. int64_t atFrameBaseLength = 0;
  2501. int64 atLocationLen = 0;
  2502. const uint8* atLocationData = 0;
  2503. const char* atProducer = NULL;
  2504. const char* atName = NULL;
  2505. const char* atCompDir = NULL;
  2506. const char* atLinkageName = NULL;
  2507. int64 atConstValue = 0;
  2508. int atDataMemberLocation = 0;
  2509. const uint8* atDataMemberData = NULL;
  2510. int atDeclFile = 0;
  2511. int atDeclLine = 0;
  2512. int atCallFile = 0;
  2513. int atCallLine = 0;
  2514. int atCount = 0;
  2515. int atType = 0;
  2516. int atImport = 0;
  2517. int atInline = 0;
  2518. int atArtificial = 0;
  2519. int atExternal = 0;
  2520. int atByteSize = -1;
  2521. int atEncoding = 0;
  2522. int atSpecification = 0;
  2523. int atObjectPointer = 0;
  2524. int atBitOffset = 0;
  2525. int atBitSize = 0;
  2526. int atAbstractOrigin = 0;
  2527. const uint8* atVirtualLocData = NULL;
  2528. bool atDeclaration = false;
  2529. bool atVirtual = false;
  2530. bool hadConstValue = false;
  2531. bool hadMemberLocation = false;
  2532. bool isOptimized = false;
  2533. DataPair newDataPair;
  2534. while (true)
  2535. {
  2536. int attrName = (int)DecodeULEB128(abbrevData);
  2537. int form = (int)DecodeULEB128(abbrevData);
  2538. if ((attrName == 0) && (form == 0))
  2539. break;
  2540. switch (attrName)
  2541. {
  2542. case DW_AT_sibling:
  2543. ReadValue<char>(data, form);
  2544. break;
  2545. case DW_AT_location:
  2546. atLocationLen = (int)ReadValue<uint>(data, form, dataOfs, &atLocationData, startData);
  2547. break;
  2548. case DW_AT_name:
  2549. atName = ReadValue<const char*>(data, form);
  2550. break;
  2551. case DW_AT_ordering:
  2552. /*TODO:*/ ReadValue<int>(data, form);
  2553. break;
  2554. case DW_AT_byte_size:
  2555. atByteSize = ReadValue<int>(data, form);
  2556. break;
  2557. case DW_AT_bit_offset:
  2558. atBitOffset = ReadValue<int>(data, form);
  2559. break;
  2560. case DW_AT_bit_size:
  2561. atBitSize = ReadValue<int>(data, form);
  2562. break;
  2563. case DW_AT_stmt_list:
  2564. ReadValue<int64_t>(data, form);
  2565. break;
  2566. case DW_AT_low_pc:
  2567. atLowPC = RemapAddr((addr_target)ReadValue<int64_t>(data, form));
  2568. break;
  2569. case DW_AT_high_pc:
  2570. atHighPC = ReadValue<int64_t>(data, form);
  2571. break;
  2572. case DW_AT_language:
  2573. /*TODO:*/ ReadValue<int>(data, form);
  2574. break;
  2575. case DW_AT_discr:
  2576. /*TODO:*/ ReadValue<int>(data, form);
  2577. break;
  2578. case DW_AT_discr_value:
  2579. /*TODO:*/ ReadValue<int>(data, form);
  2580. break;
  2581. case DW_AT_visibility:
  2582. /*TODO:*/ ReadValue<int>(data, form);
  2583. break;
  2584. case DW_AT_import:
  2585. atImport = ReadValue<int>(data, form) + dataOfs;
  2586. break;
  2587. case DW_AT_string_length:
  2588. /*TODO:*/ ReadValue<int>(data, form);
  2589. break;
  2590. case DW_AT_common_reference:
  2591. /*TODO:*/ ReadValue<int>(data, form);
  2592. break;
  2593. case DW_AT_comp_dir:
  2594. atCompDir = ReadValue<const char*>(data, form);
  2595. break;
  2596. case DW_AT_const_value:
  2597. atConstValue = ReadValue<int64>(data, form);
  2598. hadConstValue = true;
  2599. break;
  2600. case DW_AT_containing_type:
  2601. /*TODO:*/ ReadValue<int>(data, form);
  2602. break;
  2603. case DW_AT_default_value:
  2604. /*TODO:*/ ReadValue<int>(data, form);
  2605. break;
  2606. case DW_AT_inline:
  2607. atInline = ReadValue<int>(data, form);
  2608. break;
  2609. case DW_AT_is_optional:
  2610. /*TODO:*/ ReadValue<int>(data, form);
  2611. break;
  2612. case DW_AT_lower_bound:
  2613. /*TODO:*/ ReadValue<int>(data, form);
  2614. break;
  2615. case DW_AT_producer:
  2616. atProducer = ReadValue<const char*>(data, form);
  2617. break;
  2618. case DW_AT_prototyped:
  2619. /*TODO:*/ ReadValue<int>(data, form);
  2620. break;
  2621. case DW_AT_return_addr:
  2622. /*TODO:*/ ReadValue<int>(data, form);
  2623. break;
  2624. case DW_AT_start_scope:
  2625. /*TODO:*/ ReadValue<int>(data, form);
  2626. break;
  2627. case DW_AT_bit_stride:
  2628. /*TODO:*/ ReadValue<int>(data, form);
  2629. break;
  2630. case DW_AT_upper_bound:
  2631. // Lower bound not supported
  2632. atCount = ReadValue<int>(data, form);
  2633. break;
  2634. case DW_AT_abstract_origin:
  2635. atAbstractOrigin = ReadValue<int>(data, form, dataOfs);
  2636. break;
  2637. case DW_AT_accessibility:
  2638. /*TODO:*/ ReadValue<int>(data, form);
  2639. break;
  2640. case DW_AT_address_class:
  2641. /*TODO:*/ ReadValue<int>(data, form);
  2642. break;
  2643. case DW_AT_artificial:
  2644. atArtificial = ReadValue<int>(data, form);
  2645. break;
  2646. case DW_AT_base_types:
  2647. /*TODO:*/ ReadValue<int>(data, form);
  2648. break;
  2649. case DW_AT_calling_convention:
  2650. /*TODO:*/ ReadValue<int>(data, form);
  2651. break;
  2652. case DW_AT_count:
  2653. atCount = ReadValue<uint>(data, form);
  2654. break;
  2655. case DW_AT_data_member_location:
  2656. if (form == DW_FORM_exprloc)
  2657. {
  2658. atDataMemberLocation = (int)ReadValue<uint>(data, form, dataOfs, &atDataMemberData);
  2659. hadMemberLocation = true;
  2660. }
  2661. else
  2662. {
  2663. atDataMemberLocation = (int)ReadValue<uint>(data, form);
  2664. hadMemberLocation = true;
  2665. }
  2666. break;
  2667. case DW_AT_decl_column:
  2668. /*TODO:*/ ReadValue<uint32>(data, form);
  2669. break;
  2670. case DW_AT_decl_file:
  2671. atDeclFile = ReadValue<uint32>(data, form);
  2672. break;
  2673. case DW_AT_decl_line:
  2674. atDeclLine = ReadValue<uint32>(data, form);
  2675. break;
  2676. case DW_AT_declaration:
  2677. atDeclaration = ReadValue<bool>(data, form);
  2678. break;
  2679. case DW_AT_discr_list:
  2680. /*TODO:*/ ReadValue<int>(data, form);
  2681. break;
  2682. case DW_AT_encoding:
  2683. atEncoding = ReadValue<int>(data, form);
  2684. break;
  2685. case DW_AT_external:
  2686. atExternal = ReadValue<int>(data, form);
  2687. break;
  2688. case DW_AT_frame_base:
  2689. atFrameBaseLength = (int64_t)ReadValue<uint64_t>(data, form, dataOfs, &atFrameBase);
  2690. break;
  2691. case DW_AT_friend:
  2692. /*TODO:*/ ReadValue<int>(data, form);
  2693. break;
  2694. case DW_AT_identifier_case:
  2695. /*TODO:*/ ReadValue<int>(data, form);
  2696. break;
  2697. case DW_AT_macro_info:
  2698. /*TODO:*/ ReadValue<int>(data, form);
  2699. break;
  2700. case DW_AT_namelist_item:
  2701. /*TODO:*/ ReadValue<int>(data, form);
  2702. break;
  2703. case DW_AT_priority:
  2704. /*TODO:*/ ReadValue<int>(data, form);
  2705. break;
  2706. case DW_AT_segment:
  2707. /*TODO:*/ ReadValue<int>(data, form);
  2708. break;
  2709. case DW_AT_specification:
  2710. atSpecification = ReadValue<int>(data, form, dataOfs);
  2711. break;
  2712. case DW_AT_static_link:
  2713. /*TODO:*/ ReadValue<int>(data, form);
  2714. break;
  2715. case DW_AT_type:
  2716. atType = ReadValue<int>(data, form, dataOfs);
  2717. break;
  2718. case DW_AT_use_location:
  2719. /*TODO:*/ ReadValue<int>(data, form);
  2720. break;
  2721. case DW_AT_variable_parameter:
  2722. /*TODO:*/ ReadValue<int>(data, form);
  2723. break;
  2724. case DW_AT_virtuality:
  2725. atVirtual = ReadValue<int>(data, form) != 0;
  2726. break;
  2727. case DW_AT_vtable_elem_location:
  2728. ReadValue<uint64_t>(data, form, dataOfs, &atVirtualLocData);
  2729. break;
  2730. case DW_AT_allocated:
  2731. /*TODO:*/ ReadValue<int>(data, form);
  2732. break;
  2733. case DW_AT_associated:
  2734. /*TODO:*/ ReadValue<int>(data, form);
  2735. break;
  2736. case DW_AT_data_location:
  2737. /*TODO:*/ ReadValue<int>(data, form);
  2738. break;
  2739. case DW_AT_byte_stride:
  2740. /*TODO:*/ ReadValue<int>(data, form);
  2741. break;
  2742. case DW_AT_entry_pc:
  2743. /*TODO:*/ ReadValue<int>(data, form);
  2744. break;
  2745. case DW_AT_use_UTF8:
  2746. /*TODO:*/ ReadValue<int>(data, form);
  2747. break;
  2748. case DW_AT_extension:
  2749. /*TODO:*/ ReadValue<int>(data, form);
  2750. break;
  2751. case DW_AT_ranges:
  2752. atRanges = (int)ReadValue<uint>(data, form);
  2753. hasRanges = true;
  2754. break;
  2755. case DW_AT_trampoline:
  2756. /*TODO:*/ ReadValue<int>(data, form);
  2757. break;
  2758. case DW_AT_call_column:
  2759. /*TODO:*/ ReadValue<int>(data, form);
  2760. break;
  2761. case DW_AT_call_file:
  2762. atCallFile = ReadValue<uint32>(data, form);
  2763. break;
  2764. case DW_AT_call_line:
  2765. atCallLine = ReadValue<uint32>(data, form);
  2766. break;
  2767. case DW_AT_description:
  2768. /*TODO:*/ ReadValue<int>(data, form);
  2769. break;
  2770. case DW_AT_binary_scale:
  2771. /*TODO:*/ ReadValue<int>(data, form);
  2772. break;
  2773. case DW_AT_decimal_scale:
  2774. /*TODO:*/ ReadValue<int>(data, form);
  2775. break;
  2776. case DW_AT_small:
  2777. /*TODO:*/ ReadValue<int>(data, form);
  2778. break;
  2779. case DW_AT_decimal_sign:
  2780. /*TODO:*/ ReadValue<int>(data, form);
  2781. break;
  2782. case DW_AT_digit_count:
  2783. /*TODO:*/ ReadValue<int>(data, form);
  2784. break;
  2785. case DW_AT_picture_string:
  2786. /*TODO:*/ ReadValue<int>(data, form);
  2787. break;
  2788. case DW_AT_mutable:
  2789. /*TODO:*/ ReadValue<int>(data, form);
  2790. break;
  2791. case DW_AT_threads_scaled:
  2792. /*TODO:*/ ReadValue<int>(data, form);
  2793. break;
  2794. case DW_AT_explicit:
  2795. /*TODO:*/ ReadValue<int>(data, form);
  2796. break;
  2797. case DW_AT_object_pointer:
  2798. atObjectPointer = ReadValue<int>(data, form);
  2799. break;
  2800. case DW_AT_endianity:
  2801. /*TODO:*/ ReadValue<int>(data, form);
  2802. break;
  2803. case DW_AT_elemental:
  2804. /*TODO:*/ ReadValue<int>(data, form);
  2805. break;
  2806. case DW_AT_pure:
  2807. /*TODO:*/ ReadValue<int>(data, form);
  2808. break;
  2809. case DW_AT_recursive:
  2810. /*TODO:*/ ReadValue<int>(data, form);
  2811. break;
  2812. case DW_AT_signature:
  2813. /*TODO:*/ ReadValue<int>(data, form);
  2814. break;
  2815. case DW_AT_main_subprogram:
  2816. /*TODO:*/ ReadValue<int>(data, form);
  2817. break;
  2818. case DW_AT_data_bit_offset:
  2819. /*TODO:*/ ReadValue<int>(data, form);
  2820. break;
  2821. case DW_AT_const_expr:
  2822. /*TODO:*/ ReadValue<int>(data, form);
  2823. break;
  2824. case DW_AT_enum_class:
  2825. /*TODO:*/ ReadValue<int>(data, form);
  2826. break;
  2827. case DW_AT_linkage_name:
  2828. atLinkageName = ReadValue<const char*>(data, form);
  2829. break;
  2830. //
  2831. case DW_AT_MIPS_linkage_name:
  2832. atLinkageName = ReadValue<const char*>(data, form);
  2833. break;
  2834. case DW_AT_APPLE_optimized:
  2835. isOptimized = ReadValue<bool>(data, form);
  2836. break;
  2837. default:
  2838. ReadValue<int>(data, form);
  2839. break;
  2840. }
  2841. }
  2842. if ((hasRanges) && (atLowPC == 0))
  2843. {
  2844. addr_target* rangeData = (addr_target*)(mDebugRangesData + atRanges);
  2845. while (true)
  2846. {
  2847. addr_target lowPC = *(rangeData++);
  2848. if (lowPC == 0)
  2849. break;
  2850. addr_target highPC = *(rangeData++);
  2851. if (compileUnit->mLowPC != (addr_target)-1)
  2852. {
  2853. // These are sometimes relative to the compile unit and sometimes absolute
  2854. if (highPC + compileUnit->mLowPC <= compileUnit->mHighPC)
  2855. {
  2856. lowPC += compileUnit->mLowPC;
  2857. highPC += compileUnit->mLowPC;
  2858. }
  2859. }
  2860. highPC -= lowPC;
  2861. // Select the largest range. We have some cases where some hoisting and such will
  2862. // give us a small inlining aberration much earlier than expected so this ignores that
  2863. if ((int64)highPC > atHighPC)
  2864. {
  2865. atLowPC = lowPC;
  2866. atHighPC = highPC;
  2867. }
  2868. /*if ((atLowPC == 0) || (lowPC < (addr_target)atLowPC))
  2869. atLowPC = lowPC;
  2870. if (highPC > (addr_target)atHighPC)
  2871. atHighPC = highPC;*/
  2872. }
  2873. }
  2874. switch (entryTag)
  2875. {
  2876. case DW_TAG_compile_unit:
  2877. {
  2878. newDataPair = MakeDataPair(compileUnit);
  2879. compileUnit->mName = atName;
  2880. compileUnit->mProducer = atProducer;
  2881. if (atCompDir != NULL)
  2882. compileUnit->mCompileDir = atCompDir;
  2883. if (atLowPC != 0)
  2884. {
  2885. compileUnit->mLowPC = (addr_target)atLowPC;
  2886. compileUnit->mHighPC = (addr_target)(atLowPC + atHighPC);
  2887. }
  2888. if (compileUnit->mProducer.IndexOf("Beef") != -1)
  2889. {
  2890. compileUnit->mLanguage = DbgLanguage_Beef;
  2891. }
  2892. else
  2893. {
  2894. compileUnit->mLanguage = DbgLanguage_C;
  2895. }
  2896. compileUnit->mGlobalType->mLanguage = compileUnit->mLanguage;
  2897. }
  2898. break;
  2899. case DW_TAG_imported_module:
  2900. {
  2901. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  2902. DbgType* importType = GetOrCreateType(atImport, dataMap);
  2903. if (parentType != NULL) // Parent type is NULL for Clang DbgModule info
  2904. parentType->mUsingNamespaces.PushFront(importType, &mAlloc);
  2905. }
  2906. break;
  2907. case DW_TAG_inlined_subroutine:
  2908. case DW_TAG_subprogram:
  2909. {
  2910. /*//TODO: This is a test. See if it breaks anything.
  2911. if ((atExternal != 0) && (atLowPC == 0))
  2912. break;*/
  2913. if (atSpecification == 0)
  2914. {
  2915. subProgram = GetOrCreate<DbgSubprogram*>(tagIdx, dataMap);
  2916. subProgram->mCompileUnit = compileUnit;
  2917. subProgram->mVirtual = atVirtual;
  2918. subProgram->mIsOptimized = isOptimized;
  2919. if (atVirtualLocData != NULL)
  2920. {
  2921. const uint8* opPtr = atVirtualLocData;
  2922. if (*(opPtr++) == DW_OP_constu)
  2923. {
  2924. subProgram->mVTableLoc = (int)DecodeSLEB128(opPtr) * sizeof(addr_target);
  2925. }
  2926. }
  2927. //subProgram->mVTableLoc = atVirtualLoc * sizeof(addr_target);
  2928. //SplitName(atName, subProgram->mName, subProgram->mTemplateName);
  2929. subProgram->mName = atName;
  2930. subProgram->mLinkName = atLinkageName;
  2931. if (atAbstractOrigin != NULL)
  2932. {
  2933. DbgSubprogram* originSubProgram = GetOrCreate<DbgSubprogram*>(atAbstractOrigin, dataMap);
  2934. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgSubprogram::ClassType, subProgram, originSubProgram);
  2935. abstractOriginReplaceList.push_back(abstractOriginEntry);
  2936. }
  2937. subProgram->mParentType = GetStackTop<DbgType*>(&dataStack);
  2938. newDataPair = MakeDataPair(subProgram);
  2939. //if ((atLinkageName != NULL) && (subProgram->mParentType != NULL))
  2940. //subProgram->mParentType->mDefinedMembersCount++;
  2941. mSubprograms.push_back(subProgram);
  2942. if (subProgram->mParentType != NULL)
  2943. {
  2944. subProgram->mParentType->mMethodList.PushBack(subProgram);
  2945. }
  2946. else
  2947. {
  2948. compileUnit->mGlobalType->mMethodList.PushBack(subProgram);
  2949. }
  2950. }
  2951. else
  2952. {
  2953. subProgram = dataMap.Get<DbgSubprogram*>(atSpecification);
  2954. BF_ASSERT(subProgram != NULL);
  2955. // We remove params form the declaration and re-add the real ones here
  2956. subProgram->mParams.Clear();
  2957. }
  2958. newDataPair = MakeDataPair(subProgram);
  2959. DbgBlock* dwBlock = &subProgram->mBlock;
  2960. if (atType != 0)
  2961. subProgram->mReturnType = GetOrCreateType(atType, dataMap);
  2962. if (!atDeclaration)
  2963. {
  2964. dwBlock->mLowPC = (addr_target)atLowPC;
  2965. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  2966. if (dwBlock->mLowPC != 0)
  2967. {
  2968. compileUnit->mLowPC = std::min(compileUnit->mLowPC, dwBlock->mLowPC);
  2969. compileUnit->mHighPC = std::max(compileUnit->mHighPC, dwBlock->mHighPC);
  2970. }
  2971. if (atObjectPointer != 0)
  2972. subProgram->mHasThis = true;
  2973. subProgram->mFrameBaseLen = (int)atFrameBaseLength;
  2974. subProgram->mFrameBaseData = atFrameBase;
  2975. if (atHighPC > 0)
  2976. {
  2977. MapSubprogram(subProgram);
  2978. }
  2979. }
  2980. if (entryTag == DW_TAG_inlined_subroutine)
  2981. {
  2982. DbgSubprogram* parentSubProgram = GetStackLast<DbgSubprogram*>(&dataStack);
  2983. subProgram->mInlineeInfo = mAlloc.Alloc<DbgInlineeInfo>();
  2984. subProgram->mInlineeInfo->mInlineParent = parentSubProgram;
  2985. subProgram->mInlineeInfo->mRootInliner = parentSubProgram->GetRootInlineParent();
  2986. subProgram->mFrameBaseData = parentSubProgram->mFrameBaseData;
  2987. subProgram->mFrameBaseLen = parentSubProgram->mFrameBaseLen;
  2988. }
  2989. //if (subProgram->mParentType != NULL)
  2990. //subProgram->mParentType->mDefinedMembersCount++;
  2991. }
  2992. break;
  2993. case DW_TAG_lexical_block:
  2994. {
  2995. DbgBlock* prevBlock = GetStackTop<DbgBlock*>(&dataStack);
  2996. DbgBlock* dwBlock = mAlloc.Alloc<DbgBlock>();
  2997. if (hasRanges)
  2998. {
  2999. dwBlock->mLowPC = -1;
  3000. dwBlock->mHighPC = (addr_target)atRanges;
  3001. }
  3002. else
  3003. {
  3004. dwBlock->mLowPC = (addr_target)atLowPC;
  3005. dwBlock->mHighPC = (addr_target)(atLowPC + atHighPC);
  3006. }
  3007. newDataPair = MakeDataPair(dwBlock);
  3008. prevBlock->mSubBlocks.PushBack(dwBlock);
  3009. }
  3010. break;
  3011. case DW_TAG_variable:
  3012. {
  3013. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3014. if (atName && !strncmp(atName, "__asmLines", 10))
  3015. {
  3016. const char* ptr = strchr(atName, '.');
  3017. if (!ptr)
  3018. break;
  3019. int declLine = atDeclLine;
  3020. Array<int> asmLines;
  3021. int curAsmLine = 0;
  3022. int curRunCount = 1; // initial value is starting line, with an assumed run count of 1
  3023. bool parity = true; // starting line is standalone; everything afterwards is in pairs
  3024. while (true)
  3025. {
  3026. ++ptr;
  3027. if (!*ptr)
  3028. break;
  3029. String s;
  3030. if (*ptr == '$')
  3031. {
  3032. ++ptr;
  3033. const char* dollarPtr = strchr(ptr, '$');
  3034. if (!dollarPtr)
  3035. break;
  3036. s = String(ptr, (int)(dollarPtr - ptr));
  3037. ptr = dollarPtr;
  3038. }
  3039. else
  3040. {
  3041. s += *ptr;
  3042. }
  3043. //int asmLine = atoi(s.c_str());
  3044. //asmLines.push_back(asmLine);
  3045. const char* sPtr = s.c_str();
  3046. int decodedValue = (int)DecodeULEB32(sPtr);
  3047. if (!parity)
  3048. {
  3049. curRunCount = decodedValue;
  3050. }
  3051. else
  3052. {
  3053. for (int iLine=0; iLine<curRunCount; ++iLine)
  3054. {
  3055. curAsmLine += decodedValue;
  3056. asmLines.push_back(curAsmLine);
  3057. }
  3058. }
  3059. parity = !parity;
  3060. }
  3061. BF_ASSERT(!parity);
  3062. if (dwBlock->mAsmDebugLineMap == NULL)
  3063. {
  3064. mAsmDebugLineMaps.resize(mAsmDebugLineMaps.size() + 1);
  3065. dwBlock->mAsmDebugLineMap = &mAsmDebugLineMaps.back();
  3066. }
  3067. auto mapIter = dwBlock->mAsmDebugLineMap->find(declLine);
  3068. if (mapIter != dwBlock->mAsmDebugLineMap->end())
  3069. {
  3070. auto& dstVec = mapIter->second;
  3071. dstVec.Reserve(dstVec.size() + asmLines.size());
  3072. //dstVec.insert(dstVec.end(), asmLines.begin(), asmLines.end());
  3073. if (!asmLines.IsEmpty())
  3074. dstVec.Insert(dstVec.size(), &asmLines[0], asmLines.size());
  3075. }
  3076. else
  3077. {
  3078. (*dwBlock->mAsmDebugLineMap)[declLine] = std::move(asmLines);
  3079. }
  3080. break;
  3081. }
  3082. bool addToGlobalVarMap = false;
  3083. bool isNewVariable = true;
  3084. DbgVariable* dbgVariable = NULL;
  3085. if (atSpecification != 0)
  3086. {
  3087. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3088. //BF_ASSERT(dbgVariable != NULL);
  3089. dbgVariable = GetOrCreate<DbgVariable*>(atSpecification, dataMap);
  3090. //dbgVariable = dataMap.Get<DbgVariable*>(atSpecification);
  3091. //BF_ASSERT(dbgVariable != NULL);
  3092. }
  3093. else if (dwBlock != NULL)
  3094. {
  3095. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3096. dwBlock->mVariables.PushBack(dbgVariable);
  3097. }
  3098. else
  3099. {
  3100. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3101. bool wantGlobal = true;
  3102. if (compileUnit->mLanguage == DbgLanguage_Beef)
  3103. {
  3104. // Don't show certain global variables in Beef -- that includes things like VTable data
  3105. if (atName[0] == '_')
  3106. wantGlobal = false;
  3107. }
  3108. if ((dbgType == NULL) && (wantGlobal))
  3109. {
  3110. /*DbgCompileUnit* topCompileUnit = GetStackTop<DbgCompileUnit*>(&dataStack);
  3111. if (topCompileUnit != NULL)
  3112. dbgType = &topCompileUnit->mGlobalType;*/
  3113. dbgType = linkedModule->mMasterCompileUnit->mGlobalType;
  3114. auto foundEntry = mGlobalVarMap.Find(atName);
  3115. if (foundEntry != NULL)
  3116. {
  3117. isNewVariable = false;
  3118. dbgVariable = foundEntry->mValue;
  3119. }
  3120. else
  3121. {
  3122. addToGlobalVarMap = true;
  3123. }
  3124. }
  3125. if (dbgVariable == NULL)
  3126. dbgVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3127. dbgVariable->mIsStatic = true;
  3128. //TODO: dbgType can be NULL. This only (apparently?) happens for DW_TAG_inlined_subroutine, which we don't handle right now...
  3129. if (dbgType != NULL)
  3130. {
  3131. BF_ASSERT(dbgType->IsNamespace() || (dbgType->mTypeCode == DbgType_Root));
  3132. if (isNewVariable)
  3133. dbgType->mMemberList.PushBack(dbgVariable);
  3134. }
  3135. }
  3136. if (dbgVariable != NULL)
  3137. {
  3138. if (atSpecification == 0)
  3139. {
  3140. dbgVariable->mIsParam = false;
  3141. dbgVariable->mName = atName;
  3142. dbgVariable->mConstValue = atConstValue;
  3143. dbgVariable->mType = GetOrCreateType(atType, dataMap);
  3144. dbgVariable->mIsConst = hadConstValue;
  3145. dbgVariable->mIsStatic = !hadMemberLocation;
  3146. dbgVariable->mIsExtern = atExternal != 0;
  3147. }
  3148. if (atLinkageName != NULL)
  3149. dbgVariable->mLinkName = atLinkageName;
  3150. dbgVariable->mLocationLen = (int8)atLocationLen;
  3151. dbgVariable->mLocationData = atLocationData;
  3152. dbgVariable->mCompileUnit = compileUnit;
  3153. /*if (dbgVariable->mIsStatic && !dbgVariable->mIsConst && (dbgVariable->mLocationLen > 0) && (dbgVariable->mIsExtern))
  3154. {
  3155. DbgAddrType addrType = DbgAddrType_Value;
  3156. //
  3157. addr_target valAddr = mDebugTarget->EvaluateLocation(dbgVariable->mCompileUnit->mDbgModule, NULL, dbgVariable->mLocationData, dbgVariable->mLocationLen, NULL, &addrType);
  3158. if ((addrType == DbgAddrType_Target) && (valAddr != 0))
  3159. {
  3160. dbgVariable->mStaticCachedAddr = valAddr;
  3161. if (dbgVariable->mLinkName != NULL)
  3162. mStaticVariables.push_back(dbgVariable);
  3163. }
  3164. else
  3165. dbgVariable->mIsStatic = false;
  3166. }*/
  3167. // We had to remove the above for hot loading, calculate the mStaticCachedAddr later. Just put into mStaticVariables for now
  3168. mStaticVariables.push_back(dbgVariable);
  3169. if (atAbstractOrigin != NULL)
  3170. {
  3171. DbgVariable* originVariable = GetOrCreate<DbgVariable*>(atAbstractOrigin, dataMap);
  3172. auto abstractOriginEntry = AbstractOriginEntry::Create(DbgVariable::ClassType, dbgVariable, originVariable);
  3173. if (atAbstractOrigin < tagIdx)
  3174. abstractOriginEntry.Replace();
  3175. else
  3176. abstractOriginReplaceList.push_back(abstractOriginEntry);
  3177. }
  3178. else if (dbgVariable->mName == NULL)
  3179. dbgVariable->mName = "_unnamed";
  3180. if (addToGlobalVarMap)
  3181. mGlobalVarMap.Insert(dbgVariable);
  3182. newDataPair = MakeDataPair(dbgVariable);
  3183. }
  3184. }
  3185. break;
  3186. case DW_TAG_formal_parameter:
  3187. {
  3188. DbgSubprogram* dwSubprogram = GetStackTop<DbgSubprogram*>(&dataStack);
  3189. if (dwSubprogram == NULL)
  3190. {
  3191. if ((atName == NULL) && (atAbstractOrigin == 0))
  3192. {
  3193. DbgType* dbgType = GetStackTop<DbgType*>(&dataStack);
  3194. if ((dbgType == NULL) || (dbgType->mTypeCode != DbgType_Subroutine))
  3195. break;
  3196. //TODO: Add params to subroutine type
  3197. break;
  3198. }
  3199. break;
  3200. }
  3201. if ((dwSubprogram->mParams.IsEmpty()) && (dwSubprogram->mParentType != 0))
  3202. dwSubprogram->mParentType->mMethodsWithParamsCount++;
  3203. //DbgVariable* dbgVariable = mAlloc.Alloc<DbgVariable>();
  3204. DbgVariable* dwVariable = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3205. dwSubprogram->mParams.PushBack(dwVariable);
  3206. if (atArtificial != 0)
  3207. {
  3208. dwSubprogram->mHasThis = true;
  3209. if (atName == NULL)
  3210. atName = "this";
  3211. }
  3212. dwVariable->mCompileUnit = compileUnit;
  3213. dwVariable->mIsParam = true;
  3214. dwVariable->mName = atName;
  3215. dwVariable->mLocationLen = (int)atLocationLen;
  3216. dwVariable->mLocationData = atLocationData;
  3217. dwVariable->mType = GetOrCreateType(atType, dataMap);
  3218. if (atAbstractOrigin != 0)
  3219. {
  3220. }
  3221. }
  3222. break;
  3223. case DW_TAG_enumerator:
  3224. {
  3225. DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3226. member->mCompileUnit = compileUnit;
  3227. member->mConstValue = atConstValue;
  3228. member->mName = atName;
  3229. member->mIsStatic = true;
  3230. member->mIsConst = true;
  3231. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3232. parentType->mMemberList.PushBack(member);
  3233. member->mMemberOffset = atDataMemberLocation;
  3234. //member->mType = parentType->mTypeParam;
  3235. member->mType = parentType;
  3236. // Insert into parent's namespace
  3237. auto prevTop = dataStack.back();
  3238. dataStack.pop_back();
  3239. DbgBlock* dwBlock = GetStackTop<DbgBlock*>(&dataStack);
  3240. dataStack.push_back(prevTop);
  3241. if (dwBlock != NULL)
  3242. {
  3243. DbgVariable* dwVariable = mAlloc.Alloc<DbgVariable>();
  3244. dwBlock->mVariables.PushBack(dwVariable);
  3245. if (atSpecification == 0)
  3246. {
  3247. dwVariable->mIsParam = false;
  3248. dwVariable->mName = atName;
  3249. dwVariable->mConstValue = atConstValue;
  3250. dwVariable->mType = parentType->mTypeParam;
  3251. dwVariable->mIsConst = hadConstValue;
  3252. dwVariable->mIsStatic = !hadMemberLocation;
  3253. }
  3254. dwVariable->mLocationLen = (int)atLocationLen;
  3255. dwVariable->mLocationData = atLocationData;
  3256. dwVariable->mCompileUnit = compileUnit;
  3257. BF_ASSERT(dwVariable->mName != 0);
  3258. newDataPair = MakeDataPair(dwVariable);
  3259. }
  3260. }
  3261. break;
  3262. /*case DW_TAG_subrange_type:
  3263. {
  3264. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3265. parentType->mArraySize = atUpperBound;
  3266. }
  3267. break;*/
  3268. case DW_TAG_inheritance:
  3269. {
  3270. DbgType* derivedType = GetStackTop<DbgType*>(&dataStack);
  3271. DbgBaseTypeEntry* baseTypeEntry = mAlloc.Alloc<DbgBaseTypeEntry>();
  3272. baseTypeEntry->mBaseType = GetOrCreateType(atType, dataMap);
  3273. if (atDataMemberData != NULL)
  3274. {
  3275. bool foundVirtOffset = false;
  3276. const uint8* opPtr = atDataMemberData;
  3277. if (*(opPtr++) == DW_OP_dup)
  3278. {
  3279. if (*(opPtr++) == DW_OP_deref)
  3280. {
  3281. if (*(opPtr++) == DW_OP_constu)
  3282. {
  3283. baseTypeEntry->mVTableOffset = (int)DecodeSLEB128(opPtr) / sizeof(int32);
  3284. foundVirtOffset = true;
  3285. if (*(opPtr++) == DW_OP_minus)
  3286. baseTypeEntry->mVTableOffset = -baseTypeEntry->mVTableOffset;
  3287. }
  3288. }
  3289. }
  3290. BF_ASSERT(foundVirtOffset);
  3291. }
  3292. else
  3293. baseTypeEntry->mThisOffset = atDataMemberLocation;
  3294. derivedType->mBaseTypes.PushBack(baseTypeEntry);
  3295. }
  3296. break;
  3297. case DW_TAG_member:
  3298. {
  3299. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3300. if ((atName != NULL) && (strncmp(atName, "_vptr$", 6) == 0))
  3301. {
  3302. parentType->mHasVTable = true;
  3303. break;
  3304. }
  3305. //DbgVariable* member = mAlloc.Alloc<DbgVariable>();
  3306. DbgVariable* member = GetOrCreate<DbgVariable*>(tagIdx, dataMap);
  3307. member->mIsMember = true;
  3308. member->mCompileUnit = compileUnit;
  3309. member->mName = atName;
  3310. member->mType = GetOrCreateType(atType, dataMap);
  3311. member->mConstValue = atConstValue;
  3312. member->mIsConst = hadConstValue;
  3313. member->mIsStatic = !hadMemberLocation;
  3314. member->mBitSize = atBitSize;
  3315. member->mBitOffset = atBitOffset;
  3316. member->mIsExtern = atExternal != 0;
  3317. parentType->mMemberList.PushBack(member);
  3318. member->mMemberOffset = atDataMemberLocation;
  3319. if ((member->mIsStatic) && (!member->mIsConst))
  3320. parentType->mHasStaticMembers = true;
  3321. /*if ((member->mIsStatic) && (!member->mIsConst))
  3322. mStaticVariables.push_back(member);*/
  3323. newDataPair = MakeDataPair(member);
  3324. //dataMap.Set(tagIdx, member);
  3325. }
  3326. break;
  3327. case DW_TAG_subrange_type:
  3328. {
  3329. int typeIdx = (int)(tagDataStart - startData);
  3330. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3331. int arrSize = atCount;
  3332. deferredArrayDims.push_back(arrSize);
  3333. }
  3334. break;
  3335. case DW_TAG_namespace:
  3336. case DW_TAG_const_type:
  3337. case DW_TAG_base_type:
  3338. case DW_TAG_pointer_type:
  3339. case DW_TAG_ptr_to_member_type:
  3340. case DW_TAG_array_type:
  3341. case DW_TAG_reference_type:
  3342. case DW_TAG_rvalue_reference_type:
  3343. case DW_TAG_unspecified_type:
  3344. case DW_TAG_class_type:
  3345. case DW_TAG_enumeration_type:
  3346. case DW_TAG_structure_type:
  3347. case DW_TAG_union_type:
  3348. case DW_TAG_typedef:
  3349. case DW_TAG_volatile_type:
  3350. case DW_TAG_subroutine_type:
  3351. //case DW_TAG_subrange_type:
  3352. case DW_TAG_restrict_type:
  3353. {
  3354. int typeIdx = (int)(tagDataStart - startData);
  3355. DbgType* dbgType = GetOrCreateType(typeIdx, dataMap);
  3356. const char* nameSep = (compileUnit->mLanguage == DbgLanguage_Beef) ? "." : "::";
  3357. if ((atName != NULL) &&
  3358. ((entryTag == DW_TAG_structure_type) || (entryTag == DW_TAG_class_type) ||
  3359. (entryTag == DW_TAG_typedef) || (entryTag == DW_TAG_union_type) || (entryTag == DW_TAG_enumeration_type) ||
  3360. (entryTag == DW_TAG_namespace)))
  3361. {
  3362. BF_ASSERT(dbgType->mTypeCode == DbgType_Null);
  3363. DbgType* parentType = GetStackTop<DbgType*>(&dataStack);
  3364. if (parentType != NULL)
  3365. {
  3366. dbgType->mParent = parentType;
  3367. dbgType->mParent->mSubTypeList.PushBack(dbgType);
  3368. /*if (dbgType->mParent->mName != NULL)
  3369. {
  3370. if (atName == NULL)
  3371. {
  3372. dbgType->mName = dbgType->mParent->mName; // Extend from name of parent if we're anonymous
  3373. }
  3374. else
  3375. {
  3376. int nameSepLen = strlen(nameSep);
  3377. int parentNameLen = strlen(dbgType->mParent->mName);
  3378. int nameLen = strlen(atName);
  3379. char* name = (char*)mAlloc.AllocBytes(parentNameLen + nameSepLen + nameLen + 1);
  3380. memcpy(name, dbgType->mParent->mName, parentNameLen);
  3381. memcpy(name + parentNameLen, nameSep, nameSepLen);
  3382. memcpy(name + parentNameLen + nameSepLen, atName, nameLen);
  3383. dbgType->mName = name;
  3384. }
  3385. }*/
  3386. }
  3387. else
  3388. {
  3389. // Add to global subtype list but don't set dbgType->mParent
  3390. compileUnit->mGlobalType->mSubTypeList.PushBack(dbgType);
  3391. }
  3392. }
  3393. const char* useName = atName;
  3394. /*if ((useName != NULL) && (strcmp(useName, "@") == 0))
  3395. useName = NULL;*/
  3396. dbgType->mCompileUnit = compileUnit;
  3397. dbgType->mLanguage = compileUnit->mLanguage;
  3398. //SplitName(atName, dbgType->mTypeName, dbgType->mTemplateParams);
  3399. dbgType->mName = useName;
  3400. if (dbgType->mTypeName == NULL)
  3401. dbgType->mTypeName = useName;
  3402. //if (dbgType->mName == NULL)
  3403. //dbgType->mName = atName;
  3404. int parentNameLen = ((dbgType->mParent != NULL) && (dbgType->mParent->mName != NULL)) ? (int)strlen(dbgType->mParent->mName) : 0;
  3405. int typeNameLen = (dbgType->mTypeName != NULL) ? (int)strlen(dbgType->mTypeName) : 0;
  3406. //int templateParamsLen = (dbgType->mTemplateParams != NULL) ? strlen(dbgType->mTemplateParams) : 0;
  3407. if ((parentNameLen != 0) /*&& (templateParamsLen == 0)*/)
  3408. {
  3409. int nameSepLen = (int)strlen(nameSep);
  3410. int nameLen = parentNameLen + typeNameLen /*+ templateParamsLen*/;
  3411. if ((parentNameLen > 0) && (nameLen > 0))
  3412. nameLen += nameSepLen;
  3413. char* namePtr = (char*)mAlloc.AllocBytes(nameLen + 1, "DWARF");
  3414. dbgType->mName = namePtr;
  3415. if (parentNameLen > 0)
  3416. {
  3417. memcpy(namePtr, dbgType->mParent->mName, parentNameLen);
  3418. namePtr += parentNameLen;
  3419. if (nameLen > 0)
  3420. {
  3421. memcpy(namePtr, nameSep, nameSepLen);
  3422. namePtr += nameSepLen;
  3423. }
  3424. }
  3425. if (nameLen > 0)
  3426. {
  3427. memcpy(namePtr, useName, typeNameLen);
  3428. namePtr += typeNameLen;
  3429. }
  3430. /*if (templateParamsLen > 0)
  3431. {
  3432. memcpy(namePtr, dbgType->mTemplateParams, templateParamsLen);
  3433. namePtr += templateParamsLen;
  3434. }*/
  3435. }
  3436. dbgType->mTypeCode = DbgType_Null;
  3437. dbgType->mIsDeclaration = atDeclaration;
  3438. if (atByteSize != -1)
  3439. {
  3440. dbgType->mSize = atByteSize;
  3441. dbgType->mSizeCalculated = true;
  3442. }
  3443. switch (entryTag)
  3444. {
  3445. case DW_TAG_base_type:
  3446. // Types that may do fallover to int/uints on size mismatch
  3447. switch (atEncoding)
  3448. {
  3449. case DW_ATE_UTF:
  3450. if (atByteSize == 1)
  3451. dbgType->mTypeCode = DbgType_Utf8;
  3452. else if (atByteSize == 2)
  3453. dbgType->mTypeCode = DbgType_Utf16;
  3454. else
  3455. dbgType->mTypeCode = DbgType_Utf32;
  3456. break;
  3457. case DW_ATE_signed_char:
  3458. if (atByteSize == 1)
  3459. dbgType->mTypeCode = DbgType_SChar;
  3460. else if (atByteSize == 2)
  3461. dbgType->mTypeCode = DbgType_SChar16;
  3462. else if (atByteSize == 4)
  3463. dbgType->mTypeCode = DbgType_SChar32;
  3464. else
  3465. atEncoding = DW_ATE_signed;
  3466. break;
  3467. case DW_ATE_unsigned_char:
  3468. if (atByteSize == 1)
  3469. dbgType->mTypeCode = DbgType_UChar;
  3470. else if (atByteSize == 2)
  3471. dbgType->mTypeCode = DbgType_UChar16;
  3472. else if (atByteSize == 4)
  3473. dbgType->mTypeCode = DbgType_UChar32;
  3474. atEncoding = DW_ATE_unsigned;
  3475. break;
  3476. case DW_ATE_boolean:
  3477. if (atByteSize == 1)
  3478. dbgType->mTypeCode = DbgType_Bool;
  3479. else
  3480. atEncoding = DW_ATE_unsigned;
  3481. break;
  3482. }
  3483. if (dbgType->mTypeCode == DbgType_Null)
  3484. {
  3485. switch (atEncoding)
  3486. {
  3487. case DW_ATE_address:
  3488. if (atByteSize == 0)
  3489. dbgType->mTypeCode = DbgType_Void;
  3490. break;
  3491. case DW_ATE_boolean:
  3492. if (atByteSize == 1)
  3493. {
  3494. dbgType->mTypeCode = DbgType_Bool;
  3495. break;
  3496. }
  3497. //Fall through
  3498. case DW_ATE_signed:
  3499. switch (atByteSize)
  3500. {
  3501. case 1:
  3502. dbgType->mTypeCode = DbgType_i8;
  3503. break;
  3504. case 2:
  3505. dbgType->mTypeCode = DbgType_i16;
  3506. break;
  3507. case 4:
  3508. dbgType->mTypeCode = DbgType_i32;
  3509. break;
  3510. case 8:
  3511. dbgType->mTypeCode = DbgType_i64;
  3512. break;
  3513. case 16:
  3514. dbgType->mTypeCode = DbgType_i128;
  3515. break;
  3516. }
  3517. break;
  3518. case DW_ATE_unsigned:
  3519. switch (atByteSize)
  3520. {
  3521. case 1:
  3522. dbgType->mTypeCode = DbgType_u8;
  3523. break;
  3524. case 2:
  3525. if ((atName != NULL) && (strcmp(atName, "wchar_t") == 0))
  3526. dbgType->mTypeCode = DbgType_UChar16;
  3527. else
  3528. dbgType->mTypeCode = DbgType_u16;
  3529. break;
  3530. case 4:
  3531. dbgType->mTypeCode = DbgType_u32;
  3532. break;
  3533. case 8:
  3534. dbgType->mTypeCode = DbgType_u64;
  3535. break;
  3536. case 16:
  3537. dbgType->mTypeCode = DbgType_u128;
  3538. break;
  3539. }
  3540. break;
  3541. case DW_ATE_float:
  3542. if (atByteSize == 4)
  3543. dbgType->mTypeCode = DbgType_Single;
  3544. else if (atByteSize == 8)
  3545. dbgType->mTypeCode = DbgType_Double;
  3546. else if (atByteSize == 12)
  3547. dbgType->mTypeCode = DbgType_Float96;
  3548. else if (atByteSize == 16)
  3549. dbgType->mTypeCode = DbgType_Float128;
  3550. break;
  3551. case DW_ATE_complex_float:
  3552. if (atByteSize == 8)
  3553. dbgType->mTypeCode = DbgType_ComplexFloat;
  3554. else if (atByteSize == 16)
  3555. dbgType->mTypeCode = DbgType_ComplexDouble;
  3556. else if (atByteSize == 24)
  3557. dbgType->mTypeCode = DbgType_ComplexDouble96;
  3558. else if (atByteSize == 32)
  3559. dbgType->mTypeCode = DbgType_ComplexDouble128;
  3560. break;
  3561. default:
  3562. BF_FATAL("Unknown DW_ATE type");
  3563. break;
  3564. }
  3565. }
  3566. break;
  3567. case DW_TAG_enumeration_type: //TODO: Handle these differently
  3568. dbgType->mTypeCode = DbgType_Enum;
  3569. dbgType->mTypeParam = mAlloc.Alloc<DbgType>();
  3570. if (atByteSize == 8)
  3571. dbgType->mTypeParam->mTypeCode = DbgType_i64;
  3572. else if (atByteSize == 4)
  3573. dbgType->mTypeParam->mTypeCode = DbgType_i32;
  3574. else if (atByteSize == 2)
  3575. dbgType->mTypeParam->mTypeCode = DbgType_i16;
  3576. else if (atByteSize == 1)
  3577. dbgType->mTypeParam->mTypeCode = DbgType_i8;
  3578. else
  3579. {
  3580. BF_DBG_FATAL("Invalid enum type");
  3581. }
  3582. break;
  3583. case DW_TAG_namespace:
  3584. dbgType->mTypeCode = DbgType_Namespace;
  3585. break;
  3586. case DW_TAG_const_type:
  3587. dbgType->mTypeCode = DbgType_Const;
  3588. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3589. break;
  3590. case DW_TAG_rvalue_reference_type:
  3591. dbgType->mTypeCode = DbgType_RValueReference;
  3592. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3593. break;
  3594. case DW_TAG_unspecified_type:
  3595. dbgType->mTypeCode = DbgType_Unspecified;
  3596. dbgType->mTypeName = atName;
  3597. break;
  3598. case DW_TAG_reference_type:
  3599. dbgType->mTypeCode = DbgType_Ref;
  3600. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3601. break;
  3602. case DW_TAG_pointer_type:
  3603. dbgType->mTypeCode = DbgType_Ptr;
  3604. dbgType->mSize = sizeof(addr_target);
  3605. dbgType->mSizeCalculated = true;
  3606. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3607. if (dbgType->mTypeParam != NULL)
  3608. dbgType->mTypeParam->mPtrType = dbgType;
  3609. break;
  3610. case DW_TAG_ptr_to_member_type:
  3611. dbgType->mTypeCode = DbgType_PtrToMember;
  3612. dbgType->mSize = sizeof(addr_target);
  3613. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3614. if (dbgType->mTypeParam != NULL)
  3615. dbgType->mTypeParam->mPtrType = dbgType;
  3616. break;
  3617. case DW_TAG_array_type:
  3618. dbgType->mTypeCode = DbgType_SizedArray;
  3619. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3620. break;
  3621. case DW_TAG_structure_type:
  3622. dbgType->mTypeCode = DbgType_Struct;
  3623. break;
  3624. case DW_TAG_class_type:
  3625. dbgType->mTypeCode = DbgType_Class;
  3626. break;
  3627. case DW_TAG_union_type:
  3628. dbgType->mTypeCode = DbgType_Union;
  3629. break;
  3630. case DW_TAG_typedef:
  3631. dbgType->mTypeCode = DbgType_TypeDef;
  3632. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3633. break;
  3634. case DW_TAG_volatile_type:
  3635. dbgType->mTypeCode = DbgType_Volatile;
  3636. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3637. break;
  3638. case DW_TAG_subroutine_type:
  3639. dbgType->mTypeCode = DbgType_Subroutine;
  3640. if (atType != 0) // Return value
  3641. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3642. dbgType->mBlockParam = mAlloc.Alloc<DbgBlock>();
  3643. break;
  3644. case DW_TAG_restrict_type:
  3645. dbgType->mTypeCode = DbgType_Restrict;
  3646. dbgType->mTypeParam = GetOrCreateType(atType, dataMap);
  3647. break;
  3648. }
  3649. newDataPair = MakeDataPair(dbgType);
  3650. }
  3651. break;
  3652. }
  3653. if (hasChildren)
  3654. dataStack.push_back(newDataPair);
  3655. }
  3656. for (auto& abstractOriginEntry : abstractOriginReplaceList)
  3657. abstractOriginEntry.Replace();
  3658. GetLinkedModule()->MapTypes(startingTypeIdx);
  3659. dataPtr = dataEnd;
  3660. return true;
  3661. }
  3662. void DbgModule::ParseDebugFrameData()
  3663. {
  3664. BP_ZONE("ParseDebugFrameData");
  3665. const uint8* data = mDebugFrameData;
  3666. if (data == NULL)
  3667. return;
  3668. mParsedFrameDescriptors = true;
  3669. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  3670. while (true)
  3671. {
  3672. addr_target relSectionAddr = (addr_target)(data - mDebugFrameData);
  3673. int length = GET(int);
  3674. if (length == 0)
  3675. break;
  3676. const uint8* dataEnd = data + length;
  3677. int cieID = GET(int);
  3678. if (cieID < 0)
  3679. {
  3680. BP_ALLOC_T(DwCommonFrameDescriptor);
  3681. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  3682. char version = GET(char);
  3683. commonFrameDescriptor->mDbgModule = this;
  3684. commonFrameDescriptor->mAugmentation = DataGetString(data);
  3685. if (version >= 4)
  3686. {
  3687. commonFrameDescriptor->mPointerSize = GET(int8);
  3688. commonFrameDescriptor->mSegmentSize = GET(int8);
  3689. }
  3690. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  3691. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  3692. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  3693. commonFrameDescriptor->mInstData = data;
  3694. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  3695. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  3696. if (version < 3)
  3697. commonFrameDescriptorMap[relSectionAddr] = commonFrameDescriptor;
  3698. else
  3699. commonFrameDescriptorMap[mDebugFrameAddress + relSectionAddr] = commonFrameDescriptor;
  3700. }
  3701. else
  3702. {
  3703. addr_target lowPC = GET(addr_target);
  3704. addr_target highPC = lowPC + GET(addr_target);
  3705. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)cieID];
  3706. BF_ASSERT(commonFrameDescriptor != NULL);
  3707. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  3708. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  3709. auto frameDescriptor = &resultPair.first->second;
  3710. //frameDescriptor->
  3711. frameDescriptor->mLowPC = lowPC;
  3712. frameDescriptor->mHighPC = highPC;
  3713. frameDescriptor->mInstData = data;
  3714. frameDescriptor->mInstLen = (int)(dataEnd - data);
  3715. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  3716. }
  3717. data = dataEnd;
  3718. }
  3719. }
  3720. void DbgModule::ParseEHFrameData()
  3721. {
  3722. const uint8* data = mEHFrameData;
  3723. if (data == NULL)
  3724. return;
  3725. Dictionary<addr_target, DwCommonFrameDescriptor*> commonFrameDescriptorMap;
  3726. while (true)
  3727. {
  3728. addr_target sectionAddress = (addr_target)(data - mEHFrameData);
  3729. int length = GET(int);
  3730. if (length == 0)
  3731. break;
  3732. const uint8* dataEnd = data + length;
  3733. int cieID = GET(int);
  3734. if (cieID <= 0)
  3735. {
  3736. BP_ALLOC_T(DwCommonFrameDescriptor);
  3737. DwCommonFrameDescriptor* commonFrameDescriptor = mAlloc.Alloc<DwCommonFrameDescriptor>();
  3738. char version = GET(char);
  3739. const char* augmentation = DataGetString(data);
  3740. commonFrameDescriptor->mDbgModule = this;
  3741. commonFrameDescriptor->mCodeAlignmentFactor = (int)DecodeULEB128(data);
  3742. commonFrameDescriptor->mDataAlignmentFactor = (int)DecodeSLEB128(data);
  3743. commonFrameDescriptor->mReturnAddressColumn = (int)DecodeULEB128(data);
  3744. commonFrameDescriptor->mAugmentation = augmentation;
  3745. if (*augmentation == 'z')
  3746. {
  3747. ++augmentation;
  3748. int augLen = (int)DecodeULEB128(data);
  3749. commonFrameDescriptor->mAugmentationLength = augLen;
  3750. const uint8* augEnd = data + augLen;
  3751. while (*augmentation != '\0')
  3752. {
  3753. if (*augmentation == 'R')
  3754. commonFrameDescriptor->mAddressPointerEncoding = (int) GET(uint8);
  3755. else if (*augmentation == 'P')
  3756. {
  3757. int encodingType = GET(uint8);
  3758. BF_ASSERT(encodingType == 0);
  3759. commonFrameDescriptor->mLSDARoutine = GET(addr_target);
  3760. }
  3761. else if (*augmentation == 'L')
  3762. commonFrameDescriptor->mLSDAPointerEncodingFDE = GET(uint8);
  3763. else if (*augmentation == 'S')
  3764. {
  3765. // mIsSignalHandler - on return from stack frame, CFA is before next instruction rather than after it
  3766. }
  3767. else
  3768. BF_FATAL("Unknown CIE augmentation");
  3769. ++augmentation;
  3770. }
  3771. data = augEnd;
  3772. }
  3773. commonFrameDescriptor->mInstData = data;
  3774. commonFrameDescriptor->mInstLen = (int)(dataEnd - data);
  3775. mDebugTarget->mCommonFrameDescriptors.push_back(commonFrameDescriptor);
  3776. commonFrameDescriptorMap[sectionAddress] = commonFrameDescriptor;
  3777. }
  3778. else
  3779. {
  3780. int ciePos = (int)(sectionAddress - cieID) + 4;
  3781. DwCommonFrameDescriptor* commonFrameDescriptor = commonFrameDescriptorMap[(addr_target)ciePos];
  3782. addr_target lowPC;
  3783. addr_target highPC;
  3784. if (commonFrameDescriptor->mAddressPointerEncoding == (DW_EH_PE_pcrel | DW_EH_PE_sdata4))
  3785. {
  3786. lowPC = GET(int);
  3787. lowPC += mEHFrameAddress + sectionAddress + 8;
  3788. highPC = lowPC + GET(int);
  3789. }
  3790. else
  3791. {
  3792. lowPC = GET(int);
  3793. highPC = lowPC + GET(int);
  3794. }
  3795. typedef decltype(mDebugTarget->mDwFrameDescriptorMap) MapType;
  3796. auto resultPair = mDebugTarget->mDwFrameDescriptorMap.insert(MapType::value_type(lowPC, DwFrameDescriptor()));
  3797. auto frameDescriptor = &resultPair.first->second;
  3798. frameDescriptor->mLSDARoutine = commonFrameDescriptor->mLSDARoutine;
  3799. const char* augmentation = commonFrameDescriptor->mAugmentation;
  3800. if (*augmentation == 'z')
  3801. {
  3802. int augLen = GET(uint8);
  3803. const uint8* augEnd = data + augLen;
  3804. ++augmentation;
  3805. while (*augmentation != '\0')
  3806. {
  3807. if (*augmentation == 'R')
  3808. {
  3809. }
  3810. else if (*augmentation == 'P')
  3811. {
  3812. }
  3813. else if (*augmentation == 'L')
  3814. {
  3815. BF_ASSERT(commonFrameDescriptor->mLSDAPointerEncodingFDE == 0);
  3816. frameDescriptor->mLSDARoutine = GET(addr_target);
  3817. }
  3818. else if (*augmentation == 'S')
  3819. {
  3820. }
  3821. else
  3822. BF_FATAL("Unknown CIE augmentation");
  3823. augmentation++;
  3824. }
  3825. data = augEnd;
  3826. }
  3827. frameDescriptor->mLowPC = lowPC;
  3828. frameDescriptor->mHighPC = highPC;
  3829. frameDescriptor->mInstData = data;
  3830. frameDescriptor->mInstLen = (int)(dataEnd - data);
  3831. frameDescriptor->mCommonFrameDescriptor = commonFrameDescriptor;
  3832. }
  3833. data = dataEnd;
  3834. }
  3835. }
  3836. void DbgModule::FlushLineData(DbgSubprogram* curSubprogram, std::list<DbgLineData>& queuedLineData)
  3837. {
  3838. }
  3839. DbgSrcFile* DbgModule::AddSrcFile(DbgCompileUnit* compileUnit, const String& srcFilePath)
  3840. {
  3841. DbgSrcFile* dwSrcFile = mDebugTarget->AddSrcFile(srcFilePath);
  3842. if (compileUnit != NULL)
  3843. {
  3844. DbgSrcFileReference srcFileRef;
  3845. srcFileRef.mSrcFile = dwSrcFile;
  3846. srcFileRef.mCompileUnit = compileUnit;
  3847. compileUnit->mSrcFileRefs.push_back(srcFileRef);
  3848. }
  3849. return dwSrcFile;
  3850. }
  3851. bool DbgModule::ParseDebugLineInfo(const uint8*& dataPtr, int compileUnitIdx)
  3852. {
  3853. BP_ZONE("ParseDebugLineInfo");
  3854. const uint8* data = dataPtr;
  3855. const int startOffset = (int)(data - mDebugLineData);
  3856. int length = GET(int);
  3857. if (length == 0)
  3858. return false;
  3859. DbgCompileUnit* dwCompileUnit = mCompileUnits[compileUnitIdx];
  3860. const uint8* dataEnd = data + length;
  3861. short version = GET(short);
  3862. int headerLength = GET(int);
  3863. char minimumInstructionLength = GET(char);
  3864. int maximumOperationsPerInstruction = 1;
  3865. char defaultIsStmt = GET(char);
  3866. char lineBase = GET(char);
  3867. char lineRange = GET(char);
  3868. char opcodeBase = GET(char);
  3869. for (int i = 0; i < opcodeBase - 1; i++)
  3870. {
  3871. char standardOpcodeLengths = GET(char);
  3872. }
  3873. Array<const char*> directoryNames;
  3874. while (true)
  3875. {
  3876. const char* name = DataGetString(data);
  3877. if (name[0] == 0)
  3878. break;
  3879. directoryNames.push_back(name);
  3880. }
  3881. DbgSrcFileReference* dwSrcFileRef = NULL;
  3882. HashSet<String> foundPathSet;
  3883. int curFileIdx = 0;
  3884. DbgSubprogram* curSubprogram = NULL;
  3885. #define ADD_LINEDATA(lineData) \
  3886. lineBuilder.Add(dwCompileUnit, lineData, dwSrcFileRef->mSrcFile, NULL);
  3887. while (true)
  3888. {
  3889. const char* path = DataGetString(data);
  3890. if (path[0] == 0)
  3891. break;
  3892. int directoryIdx = (int)DecodeULEB128(data);
  3893. int lastModificationTime = (int)DecodeULEB128(data);
  3894. int fileLength = (int)DecodeULEB128(data);
  3895. String filePath;
  3896. if (directoryIdx > 0)
  3897. filePath = String(directoryNames[directoryIdx - 1]) + "/";
  3898. filePath += path;
  3899. filePath = GetAbsPath(filePath, dwCompileUnit->mCompileDir);
  3900. AddSrcFile(dwCompileUnit, filePath.c_str());
  3901. }
  3902. if (dwCompileUnit->mSrcFileRefs.size() > 0)
  3903. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs.front();
  3904. DbgLineDataBuilder lineBuilder(this);
  3905. bool queuedPostPrologue = false;
  3906. DbgLineDataState dwLineData;
  3907. dwLineData.mLine = 0;
  3908. dwLineData.mRelAddress = 0;
  3909. dwLineData.mOpIndex = 0;
  3910. dwLineData.mBasicBlock = false;
  3911. dwLineData.mDiscriminator = 0;
  3912. dwLineData.mIsStmt = defaultIsStmt != 0;
  3913. dwLineData.mIsa = 0;
  3914. dwLineData.mColumn = -2;
  3915. while (data < dataEnd)
  3916. {
  3917. uint8_t opcode = GET(uint8_t);
  3918. switch (opcode)
  3919. {
  3920. case DW_LNS_extended_op:
  3921. {
  3922. int len = (int)DecodeULEB128(data);
  3923. uint8_t exOpcode = GET(uint8_t);
  3924. switch (exOpcode)
  3925. {
  3926. case DW_LNE_end_sequence:
  3927. {
  3928. ADD_LINEDATA(dwLineData);
  3929. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[0];
  3930. dwLineData.mLine = 0;
  3931. dwLineData.mRelAddress = 0;
  3932. dwLineData.mOpIndex = 0;
  3933. dwLineData.mBasicBlock = false;
  3934. dwLineData.mDiscriminator = 0;
  3935. dwLineData.mIsStmt = defaultIsStmt != 0;
  3936. dwLineData.mIsa = 0;
  3937. dwLineData.mColumn = -2;
  3938. }
  3939. break;
  3940. case DW_LNE_set_address:
  3941. dwLineData.mRelAddress = (uint32)(RemapAddr(GET(addr_target)) - mImageBase);
  3942. break;
  3943. case DW_LNE_define_file:
  3944. {
  3945. const char* path = DataGetString(data);
  3946. int directoryIdx = (int)DecodeULEB128(data);
  3947. int lastModificationTime = (int)DecodeULEB128(data);
  3948. int fileLength = (int)DecodeULEB128(data);
  3949. }
  3950. break;
  3951. case DW_LNE_set_discriminator:
  3952. dwLineData.mDiscriminator = (int)DecodeULEB128(data);
  3953. break;
  3954. }
  3955. }
  3956. break;
  3957. case DW_LNS_copy:
  3958. ADD_LINEDATA(dwLineData);
  3959. dwLineData.mDiscriminator = 0;
  3960. dwLineData.mBasicBlock = false;
  3961. break;
  3962. case DW_LNS_advance_pc:
  3963. {
  3964. int advance = (int)DecodeULEB128(data);
  3965. dwLineData.mRelAddress += advance;
  3966. // How to advance opCode addr?
  3967. }
  3968. break;
  3969. case DW_LNS_advance_line:
  3970. {
  3971. int advance = (int)DecodeSLEB128(data);
  3972. dwLineData.mLine += advance;
  3973. }
  3974. break;
  3975. case DW_LNS_set_file:
  3976. {
  3977. curFileIdx = (int)DecodeULEB128(data) - 1;
  3978. dwSrcFileRef = &dwCompileUnit->mSrcFileRefs[curFileIdx];
  3979. //dwLineData.mSrcFileRef = dwSrcFileRef;
  3980. }
  3981. break;
  3982. case DW_LNS_set_column:
  3983. {
  3984. dwLineData.mColumn = (int)DecodeULEB128(data) - 1;
  3985. }
  3986. break;
  3987. case DW_LNS_negate_stmt:
  3988. {
  3989. dwLineData.mIsStmt = !dwLineData.mIsStmt;
  3990. }
  3991. break;
  3992. case DW_LNS_set_basic_block:
  3993. {
  3994. dwLineData.mBasicBlock = true;
  3995. }
  3996. break;
  3997. case DW_LNS_const_add_pc:
  3998. {
  3999. int adjustedOpcode = 255 - opcodeBase;
  4000. int opAdvance = adjustedOpcode / lineRange;
  4001. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4002. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4003. dwLineData.mRelAddress = newAddress;
  4004. dwLineData.mOpIndex = newOpIndex;
  4005. }
  4006. break;
  4007. case DW_LNS_fixed_advance_pc:
  4008. {
  4009. uint16_t advance = GET(uint16_t);
  4010. dwLineData.mRelAddress += advance;
  4011. dwLineData.mOpIndex = 0;
  4012. }
  4013. break;
  4014. case DW_LNS_set_prologue_end:
  4015. {
  4016. queuedPostPrologue = true;
  4017. }
  4018. break;
  4019. case DW_LNS_set_epilogue_begin:
  4020. {
  4021. dwLineData.mColumn = -2;
  4022. }
  4023. break;
  4024. case DW_LNS_set_isa:
  4025. {
  4026. dwLineData.mIsa = (int)DecodeULEB128(data);
  4027. }
  4028. break;
  4029. default:
  4030. {
  4031. // Special opcode
  4032. int adjustedOpcode = opcode - opcodeBase;
  4033. int opAdvance = adjustedOpcode / lineRange;
  4034. uint32 oldAddress = dwLineData.mRelAddress;
  4035. uint32 newAddress = dwLineData.mRelAddress + minimumInstructionLength * ((dwLineData.mOpIndex + opAdvance) / maximumOperationsPerInstruction);
  4036. int newOpIndex = (dwLineData.mOpIndex + opAdvance) % maximumOperationsPerInstruction;
  4037. int lineIncrement = lineBase + (adjustedOpcode % lineRange);
  4038. dwLineData.mLine += lineIncrement;
  4039. dwLineData.mRelAddress = newAddress;
  4040. dwLineData.mOpIndex = newOpIndex;
  4041. DbgLineData* lastLineData = NULL;
  4042. if ((newAddress == oldAddress) && (queuedPostPrologue) && (curSubprogram != NULL) && (curSubprogram->mBlock.mLowPC == newAddress))
  4043. {
  4044. // Adjust this line later
  4045. ADD_LINEDATA(dwLineData);
  4046. }
  4047. queuedPostPrologue = false;
  4048. }
  4049. break;
  4050. }
  4051. }
  4052. lineBuilder.Commit();
  4053. dataPtr = data;
  4054. return true;
  4055. }
  4056. addr_target DbgModule::GetHotTargetAddress(DbgHotTargetSection* hotTargetSection)
  4057. {
  4058. if ((hotTargetSection->mTargetSectionAddr == NULL) && (hotTargetSection->mDataSize > 0))
  4059. {
  4060. if (hotTargetSection->mNoTargetAlloc)
  4061. return 0;
  4062. hotTargetSection->mTargetSectionAddr = mDebugger->AllocHotTargetMemory(hotTargetSection->mDataSize, hotTargetSection->mCanExecute, hotTargetSection->mCanWrite, &hotTargetSection->mTargetSectionSize);
  4063. hotTargetSection->mImageOffset = (int)mImageSize;
  4064. if (mImageBase == NULL)
  4065. {
  4066. mImageBase = hotTargetSection->mTargetSectionAddr;
  4067. mOrigImageData->mAddr = mImageBase;
  4068. }
  4069. mImageSize += hotTargetSection->mTargetSectionSize;
  4070. /*if (mExceptionData == hotTargetSection->mData)
  4071. mExceptionDataRVA = (addr_target)(hotTargetSection->mTargetSectionAddr - mImageBase);*/
  4072. }
  4073. return hotTargetSection->mTargetSectionAddr;
  4074. }
  4075. uint8* DbgModule::GetHotTargetData(addr_target address)
  4076. {
  4077. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4078. {
  4079. if (mHotTargetSections[sectNum] != NULL)
  4080. {
  4081. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4082. if ((address >= hotTargetSection->mTargetSectionAddr) && (address < hotTargetSection->mTargetSectionAddr + hotTargetSection->mTargetSectionSize))
  4083. {
  4084. return hotTargetSection->mData + (address - hotTargetSection->mTargetSectionAddr);
  4085. }
  4086. }
  4087. }
  4088. return NULL;
  4089. }
  4090. void DbgModule::DoReloc(DbgHotTargetSection* hotTargetSection, COFFRelocation& coffReloc, addr_target resolvedSymbolAddr, PE_SymInfo* symInfo)
  4091. {
  4092. #ifdef BF_DBG_32
  4093. if (coffReloc.mType == IMAGE_REL_I386_DIR32)
  4094. {
  4095. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4096. }
  4097. else if (coffReloc.mType == IMAGE_REL_I386_DIR32NB)
  4098. {
  4099. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4100. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4101. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4102. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4103. }
  4104. else if (coffReloc.mType == IMAGE_REL_I386_REL32)
  4105. {
  4106. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4107. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr - myAddr - sizeof(int32);
  4108. }
  4109. else if (coffReloc.mType == IMAGE_REL_I386_SECTION)
  4110. {
  4111. // auto linkedModule = GetLinkedModule();
  4112. // addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4113. // int* encodingPtr = NULL;
  4114. // if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4115. // {
  4116. // *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4117. // linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4118. // }
  4119. // *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4120. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0;
  4121. }
  4122. else if (coffReloc.mType == IMAGE_REL_I386_SECREL)
  4123. {
  4124. //*(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += symInfo->mValue;
  4125. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4126. }
  4127. else
  4128. {
  4129. BF_ASSERT(0=="Invalid COFF reloc type");
  4130. }
  4131. #else
  4132. // CodeView uses SECTION:SECREL locations, and we just want to find a mapping such that
  4133. // COFF::GetSectionAddr can map it to the 64-bit address. We do this by encoding the
  4134. // lower 31 bits in the SECREL (allowing a 31-bit offset at the destination as well)
  4135. // and then we use a 15-bit key to map the upper bits
  4136. if (coffReloc.mType == IMAGE_REL_AMD64_REL32)
  4137. {
  4138. addr_target myAddr = GetHotTargetAddress(hotTargetSection) + coffReloc.mVirtualAddress;
  4139. intptr_target addrOffset = resolvedSymbolAddr - myAddr - sizeof(int32);
  4140. BF_ASSERT((int64)(int32)addrOffset == addrOffset);
  4141. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (int32)addrOffset;
  4142. }
  4143. else if (coffReloc.mType == IMAGE_REL_AMD64_SECTION)
  4144. {
  4145. /*if (symInfo != NULL)
  4146. {
  4147. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = symInfo->mSectionNum;
  4148. }
  4149. else*/
  4150. {
  4151. auto linkedModule = GetLinkedModule();
  4152. addr_target mappedAddr = resolvedSymbolAddr & ~0x7FFFFFF;
  4153. /*auto pair = linkedModule->mSecRelEncodingMap.insert(std::make_pair(mappedAddr, (int)linkedModule->mSecRelEncodingMap.size()));
  4154. if (pair.second)
  4155. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);*/
  4156. int* encodingPtr = NULL;
  4157. if (linkedModule->mSecRelEncodingMap.TryAdd(mappedAddr, NULL, &encodingPtr))
  4158. {
  4159. *encodingPtr = (int)linkedModule->mSecRelEncodingVec.size();
  4160. linkedModule->mSecRelEncodingVec.push_back(mappedAddr);
  4161. }
  4162. //*(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | pair.first->second;
  4163. *(uint16*)(hotTargetSection->mData + coffReloc.mVirtualAddress) = 0x8000 | *encodingPtr;
  4164. }
  4165. }
  4166. else if (coffReloc.mType == IMAGE_REL_AMD64_SECREL)
  4167. {
  4168. auto linkedModule = GetLinkedModule();
  4169. if ((resolvedSymbolAddr >= linkedModule->mTLSAddr) && (resolvedSymbolAddr < linkedModule->mTLSAddr + linkedModule->mTLSSize))
  4170. {
  4171. // Make relative to actual TLS data
  4172. resolvedSymbolAddr -= linkedModule->mTLSAddr;
  4173. }
  4174. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr & 0x7FFFFFF);
  4175. }
  4176. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR64)
  4177. {
  4178. *(uint64*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += resolvedSymbolAddr;
  4179. }
  4180. else if (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB)
  4181. {
  4182. GetHotTargetAddress(hotTargetSection); // Just to make sure we have mImageBase
  4183. // We were previously using mImageBase instead of mDebugTarget->mTargetBinary->mImageBase. Was there a reason for that?
  4184. // It was causing hot-loaded jump tables to have invalid addresses since the need to be relative to __ImageBase
  4185. *(uint32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += (uint32)(resolvedSymbolAddr - GetTargetImageBase());
  4186. //*(int32*)(hotTargetSection->mData + coffReloc.mVirtualAddress) += secRelAddr;
  4187. }
  4188. else
  4189. {
  4190. BF_ASSERT(0=="Invalid COFF reloc type");
  4191. }
  4192. #endif
  4193. }
  4194. bool DbgModule::IsHotSwapPreserve(const String& name)
  4195. {
  4196. // We have different rules for overwriting symbols in DWARF vs CodeView
  4197. // Since MS mangling includes return types, we know that a type change of a static
  4198. // member will mangle to a new name whereas with DWARF we DO want a new
  4199. // address if the type changes but we can't tell that based on the mangle alone,
  4200. // thus the reliance on the side table of mStaticVariables. We still do need
  4201. // to determine whether the symbol is data (and thus we do preserve) or a method
  4202. // (in which case we don't)
  4203. if ((mDbgFlavor == DbgFlavor_MS) && (BfDemangler::IsData(name)))
  4204. {
  4205. if ((!name.StartsWith("?")) && (name.Contains("sBfTypeData"))) // We DO need to replace the fields/methods/etc but not the base sBfTypeData
  4206. return false;
  4207. if (name.StartsWith("?bf_hs_replace_"))
  4208. return false;
  4209. return true;
  4210. }
  4211. const char* prefix = "bf_hs_preserve@";
  4212. return strncmp(name.c_str(), prefix, strlen(prefix)) == 0;
  4213. }
  4214. void DbgModule::ParseHotTargetSections(DataStream* stream, addr_target* resolvedSymbolAddrs)
  4215. {
  4216. auto mainModule = mDebugTarget->mTargetBinary;
  4217. mainModule->ParseSymbolData();
  4218. String name;
  4219. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4220. {
  4221. if (mHotTargetSections[sectNum] != NULL)
  4222. {
  4223. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4224. stream->SetPos(hotTargetSection->mPointerToRelocations);
  4225. for (int relocIdx = 0; relocIdx < hotTargetSection->mNumberOfRelocations; relocIdx++)
  4226. {
  4227. COFFRelocation coffReloc;
  4228. stream->Read(&coffReloc, sizeof(COFFRelocation));
  4229. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[coffReloc.mSymbolTableIndex * 18];
  4230. //const char* symName = mSymbolData[coffReloc.mSymbolTableIndex];
  4231. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  4232. if (symInfo->mNameOfs[0] != 0)
  4233. {
  4234. if (symInfo->mName[7] != 0)
  4235. {
  4236. // Name is exactly 8 chars, not null terminated yet
  4237. name = String(symInfo->mName, symInfo->mName + 8);
  4238. }
  4239. else
  4240. name = symInfo->mName;
  4241. }
  4242. else
  4243. name = mStringTable + symInfo->mNameOfs[1];
  4244. bool didNameMatch = false;
  4245. addr_target resolvedSymbolAddr = resolvedSymbolAddrs[coffReloc.mSymbolTableIndex];
  4246. #ifdef BF_DBG_32
  4247. 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);
  4248. if (name[0] == '_')
  4249. name.Remove(0, 1);
  4250. #else
  4251. bool needsSymbolAddr = (coffReloc.mType == IMAGE_REL_AMD64_ADDR64) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32) || (coffReloc.mType == IMAGE_REL_AMD64_ADDR32NB) ||
  4252. ((coffReloc.mType >= IMAGE_REL_AMD64_REL32) || (coffReloc.mType <= IMAGE_REL_AMD64_REL32_5));
  4253. #endif
  4254. bool isHsPrev = false;
  4255. if (name.StartsWith("bf_hs_prev@"))
  4256. {
  4257. isHsPrev = true;
  4258. name.Remove(0, 11);
  4259. }
  4260. bool deferResolve = false;
  4261. if ((resolvedSymbolAddr == 0) && (needsSymbolAddr))
  4262. {
  4263. bool isHotSwapPreserve = IsHotSwapPreserve(name);
  4264. if ((symInfo->mSectionNum == 0) || (isHotSwapPreserve) || (isHsPrev))
  4265. {
  4266. auto origSymbolEntry = mainModule->mSymbolNameMap.Find(name.c_str());
  4267. if (origSymbolEntry != NULL)
  4268. {
  4269. resolvedSymbolAddr = origSymbolEntry->mValue->mAddress;
  4270. }
  4271. else
  4272. {
  4273. //BF_FATAL("Symbol lookup error");
  4274. deferResolve = true;
  4275. }
  4276. }
  4277. if ((symInfo->mSectionNum != 0) && (resolvedSymbolAddr == NULL))
  4278. {
  4279. DbgHotTargetSection* refHotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  4280. resolvedSymbolAddr = GetHotTargetAddress(refHotTargetSection) + symInfo->mValue;
  4281. // Using the !hotTargetSection->mNoTargetAlloc check down here caused us to not properly remap reloaded
  4282. // static members in the debug info. Even though we parse the debug info before we apply the deferred
  4283. // resolves, the mLocData points into the original data so we still get it remapped when we use that
  4284. // mLocData
  4285. if (/*(!hotTargetSection->mNoTargetAlloc) &&*/ ((refHotTargetSection->mData == NULL) || (refHotTargetSection->mNoTargetAlloc)) &&
  4286. (!isStaticSymbol))
  4287. deferResolve = true;
  4288. else
  4289. deferResolve = false;
  4290. }
  4291. }
  4292. if (deferResolve)
  4293. {
  4294. // It's a static field, defer resolution, but don't bother replacing for debug info sections
  4295. DbgDeferredHotResolve* deferredResolve = mDeferredHotResolveList.Alloc();
  4296. deferredResolve->mHotTargetSection = hotTargetSection;
  4297. deferredResolve->mName = name;
  4298. deferredResolve->mNewAddr = resolvedSymbolAddr;
  4299. deferredResolve->mReloc = coffReloc;
  4300. continue;
  4301. }
  4302. else
  4303. {
  4304. resolvedSymbolAddrs[coffReloc.mSymbolTableIndex] = resolvedSymbolAddr;
  4305. DoReloc(hotTargetSection, coffReloc, resolvedSymbolAddr, symInfo);
  4306. }
  4307. }
  4308. }
  4309. }
  4310. }
  4311. void DbgModule::CommitHotTargetSections()
  4312. {
  4313. for (int sectNum = 0; sectNum < (int)mHotTargetSections.size(); sectNum++)
  4314. {
  4315. if (mHotTargetSections[sectNum] != NULL)
  4316. {
  4317. DbgHotTargetSection* hotTargetSection = mHotTargetSections[sectNum];
  4318. addr_target hotAddr = GetHotTargetAddress(hotTargetSection);
  4319. if (hotAddr != 0)
  4320. {
  4321. // void* imageDestPtr = mOrigImageData->mBlocks[0] + hotTargetSection->mImageOffset;
  4322. // if (hotTargetSection->mData != NULL)
  4323. // memcpy(imageDestPtr, hotTargetSection->mData, hotTargetSection->mDataSize);
  4324. // else
  4325. // memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4326. BF_ASSERT(mOrigImageData->mAddr != 0);
  4327. void* imageDestPtr = hotTargetSection->mData;
  4328. bool isTemp = false;
  4329. if (imageDestPtr == NULL)
  4330. {
  4331. imageDestPtr = new uint8[hotTargetSection->mDataSize];
  4332. memset(imageDestPtr, 0, hotTargetSection->mDataSize);
  4333. isTemp = true;
  4334. }
  4335. if (hotTargetSection->mCanExecute)
  4336. {
  4337. bool success = mDebugger->WriteInstructions(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4338. BF_ASSERT(success);
  4339. }
  4340. else
  4341. {
  4342. bool success = mDebugger->WriteMemory(hotAddr, imageDestPtr, hotTargetSection->mDataSize);
  4343. BF_ASSERT(success);
  4344. }
  4345. if (isTemp)
  4346. delete imageDestPtr;
  4347. }
  4348. }
  4349. }
  4350. }
  4351. void DbgModule::HotReplaceType(DbgType* newType)
  4352. {
  4353. if (!newType->IsCompositeType())
  4354. return;
  4355. auto linkedModule = GetLinkedModule();
  4356. newType->PopulateType();
  4357. DbgType* primaryType = linkedModule->GetPrimaryType(newType);
  4358. if (primaryType == newType)
  4359. {
  4360. // There was no previous type
  4361. BF_ASSERT(primaryType->mHotNewType == NULL);
  4362. return;
  4363. }
  4364. if (primaryType->mHotNewType != newType)
  4365. {
  4366. // We have already pulled in the new data from a previous new type
  4367. BF_ASSERT(primaryType->mHotNewType == NULL);
  4368. return;
  4369. }
  4370. primaryType->mHotNewType = NULL;
  4371. primaryType->PopulateType();
  4372. linkedModule->ParseGlobalsData();
  4373. linkedModule->ParseSymbolData();
  4374. if (primaryType->mNeedsGlobalsPopulated)
  4375. {
  4376. // These aren't proper TPI types so we don't have any method declarations until we PopulateTypeGlobals
  4377. linkedModule->PopulateTypeGlobals(primaryType);
  4378. }
  4379. for (auto methodNameEntry : primaryType->mMethodNameList)
  4380. {
  4381. if (methodNameEntry->mCompileUnitId != -1)
  4382. {
  4383. linkedModule->MapCompileUnitMethods(methodNameEntry->mCompileUnitId);
  4384. methodNameEntry->mCompileUnitId = -1;
  4385. }
  4386. }
  4387. // Now actually remove the linedata from the defining module
  4388. HashSet<DbgSrcFile*> checkedFiles;
  4389. for (auto method : primaryType->mMethodList)
  4390. {
  4391. method->mWasModuleHotReplaced = true;
  4392. if (method->mLineInfo == NULL)
  4393. continue;
  4394. //FIXME: Hot replacing lines
  4395. DbgSrcFile* lastSrcFile = NULL;
  4396. checkedFiles.Clear();
  4397. int prevCtx = -1;
  4398. auto inlineRoot = method->GetRootInlineParent();
  4399. for (int lineIdx = 0; lineIdx < method->mLineInfo->mLines.mSize; lineIdx++)
  4400. {
  4401. auto& lineData = method->mLineInfo->mLines[lineIdx];
  4402. if (lineData.mCtxIdx != prevCtx)
  4403. {
  4404. auto ctxInfo = inlineRoot->mLineInfo->mContexts[lineData.mCtxIdx];
  4405. auto srcFile = ctxInfo.mSrcFile;
  4406. prevCtx = lineData.mCtxIdx;
  4407. if (srcFile != lastSrcFile)
  4408. {
  4409. if (checkedFiles.Add(srcFile))
  4410. {
  4411. // Remove linedata for old type
  4412. // These go into a hot-replaced list so we can still bind to them -- that is necessary because
  4413. // we may still have old versions of this method running (and may forever, if its in a loop on some thread)
  4414. // since we only patch entry points
  4415. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, primaryType->mCompileUnit, true);
  4416. //srcFile->RemoveLines(primaryType->mCompileUnit->mDbgModule, method, true);
  4417. srcFile->RemoveLines(method->mCompileUnit->mDbgModule, method, true);
  4418. }
  4419. lastSrcFile = srcFile;
  4420. }
  4421. }
  4422. }
  4423. }
  4424. //DbgType* primaryType = newType->GetPrimaryType();
  4425. // We need to keep a persistent list of hot replaced methods so we can set hot jumps
  4426. // in old methods that may still be on the callstack. These entries get removed when
  4427. // we unload unused hot files in
  4428. while (!primaryType->mMethodList.IsEmpty())
  4429. {
  4430. auto method = primaryType->mMethodList.PopFront();
  4431. method->PopulateSubprogram();
  4432. primaryType->mHotReplacedMethodList.PushFront(method);
  4433. mHotPrimaryTypes.Add(primaryType);
  4434. }
  4435. bool setHotJumpFailed = false;
  4436. while (!newType->mMethodList.IsEmpty())
  4437. {
  4438. DbgSubprogram* newMethod = newType->mMethodList.PopFront();
  4439. if (!newMethod->mBlock.IsEmpty())
  4440. {
  4441. newMethod->PopulateSubprogram();
  4442. bool found = false;
  4443. for (auto oldMethod : primaryType->mHotReplacedMethodList)
  4444. {
  4445. if (oldMethod->mBlock.IsEmpty())
  4446. continue;
  4447. if (oldMethod->Equals(newMethod))
  4448. {
  4449. if (!setHotJumpFailed)
  4450. {
  4451. if (!mDebugger->SetHotJump(oldMethod, newMethod))
  4452. setHotJumpFailed = true;
  4453. oldMethod->mWasHotReplaced = true;
  4454. }
  4455. }
  4456. }
  4457. }
  4458. newMethod->mParentType = primaryType;
  4459. primaryType->mMethodList.PushBack(newMethod);
  4460. }
  4461. primaryType->mCompileUnit->mWasHotReplaced = true;
  4462. primaryType->mNeedsGlobalsPopulated = newType->mNeedsGlobalsPopulated;
  4463. primaryType->mUsingNamespaces = newType->mUsingNamespaces;
  4464. primaryType->mMemberList = newType->mMemberList;
  4465. primaryType->mCompileUnit = newType->mCompileUnit;
  4466. }
  4467. bool DbgModule::CanRead(DataStream* stream, DebuggerResult* outResult)
  4468. {
  4469. PEHeader hdr;
  4470. memset(&hdr, 0, sizeof(hdr));
  4471. PE_NTHeaders ntHdr;
  4472. memset(&ntHdr, 0, sizeof(ntHdr));
  4473. stream->Read(&hdr, sizeof(PEHeader));
  4474. stream->SetPos(hdr.e_lfanew);
  4475. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4476. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4477. {
  4478. *outResult = DebuggerResult_UnknownError;
  4479. return false;
  4480. }
  4481. #ifdef BF_DBG_32
  4482. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4483. {
  4484. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X64)
  4485. *outResult = DebuggerResult_WrongBitSize;
  4486. else
  4487. *outResult = DebuggerResult_UnknownError;
  4488. return false;
  4489. }
  4490. #else
  4491. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4492. {
  4493. if (ntHdr.mFileHeader.mMachine == PE_MACHINE_X86)
  4494. *outResult = DebuggerResult_WrongBitSize;
  4495. else
  4496. *outResult = DebuggerResult_UnknownError;
  4497. return false;
  4498. }
  4499. #endif
  4500. return true;
  4501. }
  4502. bool DbgModule::ReadCOFF(DataStream* stream, bool isHotObjectFile)
  4503. {
  4504. BP_ZONE("DbgModule::ReadCOFF");
  4505. //if (this == mDebugTarget->mTargetBinary)
  4506. //mMemReporter = new MemReporter();
  4507. if (mMemReporter != NULL)
  4508. {
  4509. mMemReporter->BeginSection(StrFormat("Module: %s", mFilePath.c_str()));
  4510. mMemReporter->Add(mImageSize);
  4511. }
  4512. defer
  4513. {
  4514. if (mMemReporter != NULL)
  4515. mMemReporter->EndSection();
  4516. };
  4517. DbgModule* mainModule = mDebugTarget->mTargetBinary;
  4518. MiniDumpDebugger* miniDumpDebugger = NULL;
  4519. if (mDebugger->IsMiniDumpDebugger())
  4520. {
  4521. miniDumpDebugger = (MiniDumpDebugger*)mDebugger;
  4522. }
  4523. mIsHotObjectFile = isHotObjectFile;
  4524. auto linkedModule = GetLinkedModule();
  4525. if (mIsHotObjectFile)
  4526. linkedModule->PopulateStaticVariableMap();
  4527. mStartTypeIdx = (int)linkedModule->mTypes.size();
  4528. int startSrcFile = (int)mDebugTarget->mSrcFiles.size();
  4529. mStartSubprogramIdx = (int)mSubprograms.size();
  4530. PEHeader hdr;
  4531. memset(&hdr, 0, sizeof(hdr));
  4532. PE_NTHeaders ntHdr;
  4533. memset(&ntHdr, 0, sizeof(ntHdr));
  4534. if (!mIsHotObjectFile)
  4535. {
  4536. stream->Read(&hdr, sizeof(PEHeader));
  4537. stream->SetPos(hdr.e_lfanew);
  4538. stream->Read(&ntHdr, sizeof(PE_NTHeaders));
  4539. mPreferredImageBase = ntHdr.mOptionalHeader.mImageBase;
  4540. if (mImageBase == 0)
  4541. {
  4542. BF_ASSERT(this == mainModule);
  4543. mImageBase = mPreferredImageBase;
  4544. }
  4545. if ((hdr.e_magic != PE_DOS_SIGNATURE) || (ntHdr.mSignature != PE_NT_SIGNATURE))
  4546. {
  4547. mLoadState = DbgModuleLoadState_Failed;
  4548. return false;
  4549. }
  4550. #ifdef BF_DBG_32
  4551. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4552. return false;
  4553. #else
  4554. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4555. {
  4556. mLoadState = DbgModuleLoadState_Failed;
  4557. return false;
  4558. }
  4559. #endif
  4560. int pos = hdr.e_lfanew + FIELD_OFFSET(PE_NTHeaders, mOptionalHeader) + ntHdr.mFileHeader.mSizeOfOptionalHeader;
  4561. stream->SetPos(pos);
  4562. }
  4563. else
  4564. {
  4565. stream->Read(&ntHdr.mFileHeader, sizeof(PEFileHeader));
  4566. if (mMemReporter != NULL)
  4567. mMemReporter->Add("PEFileHeader", sizeof(PEFileHeader));
  4568. #ifdef BF_DBG_32
  4569. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X86)
  4570. return false;
  4571. #else
  4572. if (ntHdr.mFileHeader.mMachine != PE_MACHINE_X64)
  4573. {
  4574. mLoadState = DbgModuleLoadState_Failed;
  4575. return false;
  4576. }
  4577. #endif
  4578. }
  4579. int sectionStartPos = stream->GetPos();
  4580. int sectionDataEndPos = 0;
  4581. if (miniDumpDebugger != NULL)
  4582. {
  4583. // Map header
  4584. miniDumpDebugger->MapMemory((addr_target)mImageBase, (uint8*)mMappedImageFile->mData, 0x1000);
  4585. }
  4586. stream->SetPos(sectionStartPos);
  4587. for (int dirNum = 0; dirNum < (int) ntHdr.mFileHeader.mNumberOfSections; dirNum++)
  4588. {
  4589. PESectionHeader sectHdr;
  4590. stream->Read(&sectHdr, sizeof(PESectionHeader));
  4591. if (sectHdr.mSizeOfRawData > 0)
  4592. sectionDataEndPos = sectHdr.mPointerToRawData + sectHdr.mSizeOfRawData;
  4593. if (sectHdr.mNumberOfRelocations > 0)
  4594. sectionDataEndPos = sectHdr.mPointerToRelocations + sectHdr.mNumberOfRelocations * sizeof(COFFRelocation);
  4595. if (miniDumpDebugger != NULL)
  4596. {
  4597. miniDumpDebugger->MapMemory((addr_target)(mImageBase + sectHdr.mVirtualAddress), (uint8*)mMappedImageFile->mData + sectHdr.mPointerToRawData, sectHdr.mSizeOfRawData);
  4598. }
  4599. }
  4600. //fseek(fp, sectionDataEndPos + ntHdr.mFileHeader.mNumberOfSymbols * 18, SEEK_SET);
  4601. stream->SetPos(sectionDataEndPos);
  4602. uint8* symbolData = new uint8[ntHdr.mFileHeader.mNumberOfSymbols * 18];
  4603. mAllocSizeData += ntHdr.mFileHeader.mNumberOfSymbols * 18;
  4604. mSymbolData = symbolData;
  4605. stream->Read(symbolData, ntHdr.mFileHeader.mNumberOfSymbols * 18);
  4606. int curPos = stream->GetPos();
  4607. int strTableSize = 0;
  4608. char* strTableData = NULL;
  4609. if (!stream->Eof())
  4610. {
  4611. stream->Read(&strTableSize, 4);
  4612. if (strTableSize != 0)
  4613. {
  4614. strTableSize -= 4;
  4615. strTableData = new char[strTableSize + 4];
  4616. memcpy(strTableData, &strTableSize, 4);
  4617. stream->Read(strTableData + 4, strTableSize);
  4618. mStringTable = strTableData;
  4619. }
  4620. }
  4621. int mDebugFrameDataLen = 0;
  4622. stream->SetPos(sectionStartPos);
  4623. PEDataDirectory* exportDataDir = &ntHdr.mOptionalHeader.mDataDirectory[0];
  4624. mHotTargetSections.Resize(ntHdr.mFileHeader.mNumberOfSections);
  4625. Array<PESectionHeader> sectionHeaders;
  4626. sectionHeaders.Resize(ntHdr.mFileHeader.mNumberOfSections);
  4627. mSectionRVAs.Resize(sectionHeaders.size() + 1);
  4628. Array<String> sectionNames;
  4629. sectionNames.Resize(ntHdr.mFileHeader.mNumberOfSections);
  4630. stream->Read(&sectionHeaders[0], sizeof(PESectionHeader) * ntHdr.mFileHeader.mNumberOfSections);
  4631. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  4632. {
  4633. mSectionRVAs[sectNum] = sectionHeaders[sectNum].mVirtualAddress;
  4634. }
  4635. int tlsSection = -1;
  4636. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  4637. {
  4638. //PEDataDirectory* dataDir = &ntHdr.mOptionalHeader.mDataDirectory[dirNum];
  4639. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  4640. //stream->Read(&sectHdr, sizeof(PESectionHeader));
  4641. char* name = sectHdr.mName;
  4642. if (name[0] == '/')
  4643. {
  4644. int strIdx = atoi(name + 1);
  4645. name = &strTableData[strIdx];
  4646. }
  4647. sectionNames[sectNum] = name;
  4648. DbgHotTargetSection* targetSection = NULL;
  4649. if (mIsHotObjectFile)
  4650. {
  4651. targetSection = new DbgHotTargetSection();
  4652. targetSection->mDataSize = sectHdr.mSizeOfRawData;
  4653. targetSection->mPointerToRelocations = sectHdr.mPointerToRelocations;
  4654. targetSection->mNumberOfRelocations = sectHdr.mNumberOfRelocations;
  4655. targetSection->mTargetSectionAddr = 0; // TODO: Allocate!
  4656. targetSection->mCanExecute = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  4657. targetSection->mCanWrite = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_WRITE) != 0;
  4658. targetSection->mNoTargetAlloc = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0;
  4659. mHotTargetSections[sectNum] = targetSection;
  4660. }
  4661. DbgSection dwSection;
  4662. dwSection.mIsExecutable = (sectHdr.mCharacteristics & IMAGE_SCN_MEM_EXECUTE) != 0;
  4663. dwSection.mAddrStart = sectHdr.mVirtualAddress;
  4664. dwSection.mAddrLength = BF_MAX(sectHdr.mSizeOfRawData, sectHdr.mVirtualSize);
  4665. mSections.push_back(dwSection);
  4666. if (sectHdr.mPointerToRawData == 0)
  4667. continue;
  4668. if (strcmp(name, ".tls") == 0)
  4669. mTLSAddr = (addr_target)(sectHdr.mVirtualAddress + mImageBase);
  4670. if ((mIsHotObjectFile) && (strcmp(name, ".tls$") == 0))
  4671. {
  4672. tlsSection = sectNum;
  4673. mTLSSize = sectHdr.mSizeOfRawData;
  4674. targetSection->mNoTargetAlloc = true;
  4675. }
  4676. bool isExportDataDir = ((exportDataDir->mVirtualAddress != 0) && (exportDataDir->mVirtualAddress >= sectHdr.mVirtualAddress) && (exportDataDir->mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  4677. if ((!mIsHotObjectFile) && (!isExportDataDir))
  4678. {
  4679. if (((strcmp(name, ".text")) == 0) ||
  4680. ((strcmp(name, ".textbss")) == 0) ||
  4681. ((strcmp(name, ".reloc")) == 0)/* ||
  4682. ((strcmp(name, ".data")) == 0)*/)
  4683. {
  4684. // Big unneeded sections
  4685. continue;
  4686. }
  4687. }
  4688. stream->SetPos(sectHdr.mPointerToRawData);
  4689. int dataSize = sectHdr.mSizeOfRawData + 8;
  4690. mAllocSizeData += dataSize;
  4691. uint8* data = new uint8[dataSize];
  4692. {
  4693. BP_ZONE("DbgModule::ReadCOFF_ReadSectionData");
  4694. stream->Read(data, sectHdr.mSizeOfRawData);
  4695. }
  4696. BfLogDbg("Read section data %s %p\n", name, data);
  4697. memset(data + sectHdr.mSizeOfRawData, 0, 8);
  4698. if (mIsHotObjectFile)
  4699. targetSection->mData = data;
  4700. addr_target addrOffset = sectHdr.mVirtualAddress;
  4701. if (isExportDataDir)
  4702. {
  4703. BP_ZONE("DbgModule::ReadCOFF_SymbolMap");
  4704. IMAGE_EXPORT_DIRECTORY* imageExportDir = (IMAGE_EXPORT_DIRECTORY*)(data + (exportDataDir->mVirtualAddress - addrOffset));
  4705. for (int funcIdx = 0; funcIdx < (int)imageExportDir->NumberOfNames; funcIdx++)
  4706. {
  4707. //addr_target strAddr = *(addr_target*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(addr_target));
  4708. int32 strAddr = *(int32*)(data + (imageExportDir->AddressOfNames - addrOffset) + funcIdx * sizeof(int32));
  4709. const char* name = (const char*)(data + (strAddr - addrOffset));
  4710. #ifdef BF_DBG_32
  4711. if (name[0] == '_')
  4712. name++;
  4713. #endif
  4714. int funcOrd = *(uint16*)(data + (imageExportDir->AddressOfNameOrdinals - addrOffset) + funcIdx * sizeof(uint16));
  4715. addr_target funcAddr = *(uint32*)(data + (imageExportDir->AddressOfFunctions - addrOffset) + funcOrd * sizeof(int32));
  4716. int strLen = (int)strlen(name);
  4717. BP_ALLOC("ReadCOFF_SymbolMap", strLen + 1);
  4718. char* allocStr = (char*)mAlloc.AllocBytes(strLen + 1, "ReadCOFF_SymbolMap");
  4719. memcpy(allocStr, name, strLen);
  4720. BP_ALLOC_T(DbgSymbol);
  4721. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  4722. dwSymbol->mDbgModule = this;
  4723. dwSymbol->mName = allocStr;
  4724. dwSymbol->mAddress = funcAddr;
  4725. if (strcmp(name, "_tls_index") == 0)
  4726. {
  4727. mTLSIndexAddr = funcAddr;
  4728. }
  4729. //TODO:
  4730. //mDeferredSymbols.PushFront(dwSymbol);
  4731. dwSymbol->mAddress = (addr_target)(dwSymbol->mAddress + mImageBase);
  4732. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  4733. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  4734. }
  4735. }
  4736. if ((mIsHotObjectFile) && (sectHdr.mNumberOfRelocations > 0))
  4737. {
  4738. //mDebugger->AllocTargetMemory(sectHdr.mSizeOfRawData, true, true);
  4739. }
  4740. if (strcmp(name, ".text") == 0)
  4741. {
  4742. if (!mIsHotObjectFile)
  4743. mCodeAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  4744. }
  4745. //if (strcmp(name, ".rdata") == 0)
  4746. {
  4747. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  4748. if (debugDirEntry.mSize > 0)
  4749. {
  4750. if (mMemReporter != NULL)
  4751. mMemReporter->Add("DataDirectory", debugDirEntry.mSize);
  4752. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  4753. {
  4754. int count = debugDirEntry.mSize / sizeof(IMAGE_DEBUG_DIRECTORY);
  4755. for (int dirIdx = 0; dirIdx < count; dirIdx++)
  4756. {
  4757. IMAGE_DEBUG_DIRECTORY* debugDirectory = (IMAGE_DEBUG_DIRECTORY*)(data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress) + dirIdx;
  4758. if (debugDirectory->Type == IMAGE_DEBUG_TYPE_CODEVIEW)
  4759. {
  4760. struct _CodeViewEntry
  4761. {
  4762. public:
  4763. int32 mSig;
  4764. uint8 mGUID[16];
  4765. int32 mAge;
  4766. const char mPDBPath[1];
  4767. };
  4768. if (debugDirectory->AddressOfRawData != 0)
  4769. {
  4770. _CodeViewEntry* codeViewEntry = (_CodeViewEntry*)(data + debugDirectory->AddressOfRawData - sectHdr.mVirtualAddress);
  4771. if (codeViewEntry->mSig == 'SDSR')
  4772. {
  4773. LoadPDB(codeViewEntry->mPDBPath, codeViewEntry->mGUID, codeViewEntry->mAge);
  4774. }
  4775. }
  4776. }
  4777. }
  4778. }
  4779. //stream->SetPos(debugDirEntry.mVirtualAddress);
  4780. }
  4781. }
  4782. //
  4783. {
  4784. PEDataDirectory& tlsDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
  4785. if (tlsDirEntry.mSize > 0)
  4786. {
  4787. if ((tlsDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (tlsDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  4788. {
  4789. uint8* relPtr = data + tlsDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  4790. uint8* endPtr = relPtr + tlsDirEntry.mSize;
  4791. addr_target tlsDataStart = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  4792. addr_target tlsDataEnd = GET_FROM(relPtr, addr_target) - ntHdr.mOptionalHeader.mImageBase;
  4793. mTLSAddr = (addr_target)(tlsDataStart + mImageBase);
  4794. mTLSSize = (int)(tlsDataEnd - tlsDataStart);
  4795. }
  4796. }
  4797. }
  4798. //
  4799. {
  4800. PEDataDirectory& debugDirEntry = ntHdr.mOptionalHeader.mDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  4801. if (debugDirEntry.mSize > 0)
  4802. {
  4803. if ((debugDirEntry.mVirtualAddress >= sectHdr.mVirtualAddress) && (debugDirEntry.mVirtualAddress < sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData))
  4804. {
  4805. uint8* relPtr = data + debugDirEntry.mVirtualAddress - sectHdr.mVirtualAddress;
  4806. uint8* endPtr = relPtr + debugDirEntry.mSize;
  4807. IMAGE_RESOURCE_DIRECTORY* typeDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr);
  4808. // Skip named entries
  4809. for (int typeIdx = 0; typeIdx < typeDir->NumberOfIdEntries; typeIdx++)
  4810. {
  4811. IMAGE_RESOURCE_DIRECTORY_ENTRY* typeEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)typeDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  4812. (typeDir->NumberOfNamedEntries + typeIdx)*sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  4813. if (typeEntry->Id == 0x10) //VERSION
  4814. {
  4815. IMAGE_RESOURCE_DIRECTORY* idDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (typeEntry->OffsetToData & 0x7FFFFFFF));
  4816. if (idDir->NumberOfIdEntries < 1)
  4817. break;
  4818. IMAGE_RESOURCE_DIRECTORY_ENTRY* idEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)idDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  4819. (idDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  4820. IMAGE_RESOURCE_DIRECTORY* langDir = (IMAGE_RESOURCE_DIRECTORY*)(relPtr + (idEntry->OffsetToData & 0x7FFFFFFF));
  4821. if (langDir->NumberOfIdEntries < 1)
  4822. break;
  4823. IMAGE_RESOURCE_DIRECTORY_ENTRY* langEntry = (IMAGE_RESOURCE_DIRECTORY_ENTRY*)((uint8*)langDir + sizeof(IMAGE_RESOURCE_DIRECTORY) +
  4824. (langDir->NumberOfNamedEntries + 0) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY));
  4825. IMAGE_RESOURCE_DATA_ENTRY* dataEntry = (IMAGE_RESOURCE_DATA_ENTRY*)(relPtr + (langEntry->OffsetToData & 0x7FFFFFFF));
  4826. uint8* versionData = data + dataEntry->OffsetToData - sectHdr.mVirtualAddress;
  4827. uint8* vPtr = versionData;
  4828. auto vSize = GET_FROM(vPtr, uint16);
  4829. auto verEnd = vPtr + vSize;
  4830. auto vLength = GET_FROM(vPtr, uint16);
  4831. vPtr += 36; // "VS_VERSION_INFO"
  4832. auto fixedFileInfo = GET_FROM(vPtr, VS_FIXEDFILEINFO);
  4833. auto _GetString = [&]()
  4834. {
  4835. wchar_t* cPtr = (wchar_t*)vPtr;
  4836. int len = (int)wcslen(cPtr);
  4837. vPtr += (len + 1) * 2;
  4838. if (((intptr)vPtr & 3) != 0)
  4839. vPtr += 2;
  4840. UTF16String str16(cPtr, len);
  4841. return UTF8Encode(str16);
  4842. };
  4843. while (vPtr < verEnd)
  4844. {
  4845. auto size = GET_FROM(vPtr, uint16);
  4846. auto childEnd = vPtr + size;
  4847. auto valueLength = GET_FROM(vPtr, uint16);
  4848. auto type = GET_FROM(vPtr, uint16);
  4849. String infoType = _GetString();
  4850. if (infoType == "StringFileInfo")
  4851. {
  4852. while (vPtr < childEnd)
  4853. {
  4854. auto strsSize = GET_FROM(vPtr, uint16);
  4855. auto strsEnd = vPtr + strsSize;
  4856. auto strsLength = GET_FROM(vPtr, uint16);
  4857. auto strsType = GET_FROM(vPtr, uint16);
  4858. String hexNum = _GetString();
  4859. while (vPtr < strsEnd)
  4860. {
  4861. auto strSize = GET_FROM(vPtr, uint16);
  4862. auto strEnd = vPtr + strSize;
  4863. auto strLength = GET_FROM(vPtr, uint16);
  4864. auto strType = GET_FROM(vPtr, uint16);
  4865. String key = _GetString();
  4866. String value = _GetString();
  4867. if (key == "FileVersion")
  4868. mVersion = value;
  4869. }
  4870. }
  4871. }
  4872. vPtr = childEnd;
  4873. }
  4874. }
  4875. }
  4876. }
  4877. //stream->SetPos(debugDirEntry.mVirtualAddress);
  4878. }
  4879. }
  4880. bool usedData = true;
  4881. /*if (isUnwindSection)
  4882. {
  4883. mExceptionData = data;
  4884. mExceptionDataRVA = sectHdr.mVirtualAddress;
  4885. }*/
  4886. if (strcmp(name, ".pdata") == 0)
  4887. {
  4888. DbgSectionData entry;
  4889. entry.mData = data;
  4890. entry.mSize = sectHdr.mSizeOfRawData;
  4891. mExceptionDirectory.Add(entry);
  4892. }
  4893. /*else if (strcmp(name, ".rdata") == 0)
  4894. {
  4895. if (mExceptionData == NULL)
  4896. {
  4897. mExceptionData = data;
  4898. mExceptionDataRVA = sectHdr.mVirtualAddress;
  4899. }
  4900. else
  4901. {
  4902. usedData = false;
  4903. }
  4904. }
  4905. else if (strcmp(name, ".xdata") == 0)
  4906. {
  4907. if (mExceptionData != NULL)
  4908. {
  4909. if (mIsHotObjectFile)
  4910. {
  4911. mOwnedSectionData.push_back(mExceptionData);
  4912. }
  4913. else
  4914. {
  4915. // xdata section overrides rdata exception data
  4916. delete[] mExceptionData;
  4917. }
  4918. }
  4919. mExceptionData = data;
  4920. mExceptionDataRVA = sectHdr.mVirtualAddress;
  4921. }*/
  4922. else if (strcmp(name, ".debug_info") == 0)
  4923. {
  4924. mDebugInfoData = data;
  4925. }
  4926. else if (strcmp(name, ".debug_line") == 0)
  4927. {
  4928. mDebugLineData = data;
  4929. }
  4930. else if (strcmp(name, ".debug_str") == 0)
  4931. {
  4932. mDebugStrData = data;
  4933. }
  4934. else if (strcmp(name, ".debug_frame") == 0)
  4935. {
  4936. mDebugFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  4937. mDebugFrameData = data;
  4938. mDebugFrameDataLen = sectHdr.mSizeOfRawData;
  4939. }
  4940. else if (strcmp(name, ".eh_frame") == 0)
  4941. {
  4942. mEHFrameAddress = ntHdr.mOptionalHeader.mImageBase + sectHdr.mVirtualAddress;
  4943. mEHFrameData = data;
  4944. }
  4945. else if (strcmp(name, ".debug_abbrev") == 0)
  4946. {
  4947. mDebugAbbrevData = data;
  4948. mDebugAbbrevPtrData = new const uint8*[sectHdr.mSizeOfRawData];
  4949. }
  4950. else if (strcmp(name, ".debug_loc") == 0)
  4951. {
  4952. mDebugLocationData = data;
  4953. }
  4954. else if (strcmp(name, ".debug_ranges") == 0)
  4955. {
  4956. mDebugRangesData = data;
  4957. }
  4958. // else if (strcmp(name, ".rsrc") == 0)
  4959. // {
  4960. // //IMAGE_DIRECTORY_ENTRY_RESOURCE
  4961. // }
  4962. else if (CheckSection(name, data, sectHdr.mSizeOfRawData))
  4963. {
  4964. // Was used
  4965. }
  4966. else
  4967. {
  4968. /*if (isUnwindSection)
  4969. mOwnsExceptionData = true;
  4970. else*/
  4971. usedData = false;
  4972. }
  4973. if (!usedData)
  4974. {
  4975. if (mIsHotObjectFile)
  4976. {
  4977. mOwnedSectionData.push_back(data);
  4978. }
  4979. else
  4980. {
  4981. mAllocSizeData -= dataSize;
  4982. delete [] data;
  4983. }
  4984. }
  4985. //stream->SetPos(prevPos);
  4986. }
  4987. int needHotTargetMemory = 0;
  4988. if (mIsHotObjectFile)
  4989. {
  4990. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  4991. {
  4992. auto targetSection = mHotTargetSections[sectNum];
  4993. if (!targetSection->mNoTargetAlloc)
  4994. needHotTargetMemory += (targetSection->mDataSize + (mDebugger->mPageSize - 1)) & ~(mDebugger->mPageSize - 1);
  4995. }
  4996. mDebugger->ReserveHotTargetMemory(needHotTargetMemory);
  4997. // '0' address is temporary
  4998. //mOrigImageData = new DbgModuleMemoryCache(0, NULL, needHotTargetMemory, true);
  4999. mOrigImageData = new DbgModuleMemoryCache(0, needHotTargetMemory);
  5000. }
  5001. int numSections = ntHdr.mFileHeader.mNumberOfSections;
  5002. if (mIsHotObjectFile)
  5003. {
  5004. addr_target* resolvedSymbolAddrs = new addr_target[ntHdr.mFileHeader.mNumberOfSymbols];
  5005. memset(resolvedSymbolAddrs, 0, ntHdr.mFileHeader.mNumberOfSymbols * sizeof(addr_target));
  5006. ParseHotTargetSections(stream, resolvedSymbolAddrs);
  5007. delete [] resolvedSymbolAddrs;
  5008. }
  5009. ProcessDebugInfo();
  5010. if (mDebugInfoData != NULL)
  5011. {
  5012. mDbgFlavor = DbgFlavor_GNU;
  5013. mMasterCompileUnit = new DbgCompileUnit(this);
  5014. mMasterCompileUnit->mDbgModule = this;
  5015. mMasterCompileUnit->mIsMaster = true;
  5016. const uint8* data = mDebugInfoData;
  5017. while (ParseDWARF(data)) {}
  5018. CreateNamespaces();
  5019. // Must be added last so module reference indices still map correctly
  5020. mCompileUnits.push_back(mMasterCompileUnit);
  5021. }
  5022. ParseDebugFrameData();
  5023. ParseEHFrameData();
  5024. mEndTypeIdx = (int)linkedModule->mTypes.size();
  5025. mEndSubprogramIdx = (int)mSubprograms.size();
  5026. if (mDebugLineData != NULL)
  5027. {
  5028. const uint8* data = mDebugLineData;
  5029. for (int compileUnitIdx = 0; true; compileUnitIdx++)
  5030. if (!ParseDebugLineInfo(data, compileUnitIdx))
  5031. break;
  5032. }
  5033. {
  5034. BP_ZONE("ReadPE_ReadSymbols");
  5035. //linkedModule->mSymbolNameMap.reserve(linkedModule->mSymbolNameMap.size() + ntHdr.mFileHeader.mNumberOfSymbols * 2);
  5036. bool tlsFailed = false;
  5037. addr_target tlsMappingAddr = 0;
  5038. for (int symNum = 0; symNum < (int)ntHdr.mFileHeader.mNumberOfSymbols; symNum++)
  5039. {
  5040. PE_SymInfo* symInfo = (PE_SymInfo*)&mSymbolData[symNum * 18];
  5041. char* name = symInfo->mName;
  5042. if (symInfo->mNameOfs[0] != 0)
  5043. {
  5044. if (name[7] != 0)
  5045. {
  5046. // Name is exactly 8 chars, not null terminated yet
  5047. name = (char*)mAlloc.AllocBytes(9, "PESymbol");
  5048. memcpy(name, symInfo->mName, 8);
  5049. name[8] = 0;
  5050. }
  5051. }
  5052. else
  5053. name = strTableData + symInfo->mNameOfs[1];
  5054. if ((symInfo->mStorageClass == COFF_SYM_CLASS_EXTERNAL) ||
  5055. (symInfo->mStorageClass == COFF_SYM_CLASS_STATIC))
  5056. {
  5057. // 'static' in the C sense.
  5058. // It means local to the compile unit, so may have multiple copies of the same symbol name.
  5059. bool isStaticSymbol = symInfo->mStorageClass == COFF_SYM_CLASS_STATIC;
  5060. if (symInfo->mSectionNum == 0xFFFF)
  5061. continue;
  5062. if (symInfo->mSectionNum > 0)
  5063. {
  5064. bool isTLS = false;
  5065. addr_target targetAddr = 0;
  5066. if (mIsHotObjectFile)
  5067. {
  5068. if (symInfo->mSectionNum - 1 == tlsSection)
  5069. {
  5070. isTLS = true;
  5071. }
  5072. else
  5073. {
  5074. auto hotTargetSection = mHotTargetSections[symInfo->mSectionNum - 1];
  5075. if (hotTargetSection != NULL)
  5076. targetAddr = GetHotTargetAddress(hotTargetSection) + symInfo->mValue;
  5077. }
  5078. }
  5079. else
  5080. targetAddr = mSectionRVAs[symInfo->mSectionNum - 1] + symInfo->mValue;
  5081. if (((targetAddr != 0) || (isTLS)) &&
  5082. (name[0] != '.'))
  5083. {
  5084. const char* symbolName = name;
  5085. #ifdef BF_DBG_32
  5086. if (symbolName[0] == '_')
  5087. symbolName++;
  5088. #endif
  5089. if (strcmp(symbolName, "_tls_index") == 0)
  5090. {
  5091. mTLSIndexAddr = (addr_target)(targetAddr + mImageBase);
  5092. }
  5093. if ((isStaticSymbol) && (IsHotSwapPreserve(symbolName)))
  5094. isStaticSymbol = false;
  5095. if ((mIsHotObjectFile) && (!isStaticSymbol))
  5096. {
  5097. DbgSymbol* dwSymbol = NULL;
  5098. linkedModule->ParseSymbolData() ;
  5099. BP_ALLOC_T(DbgSymbol);
  5100. dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5101. dwSymbol->mDbgModule = this;
  5102. dwSymbol->mName = symbolName;
  5103. dwSymbol->mAddress = targetAddr;
  5104. if (dwSymbol != NULL)
  5105. {
  5106. bool isHotSwapPreserve = IsHotSwapPreserve(dwSymbol->mName);
  5107. bool insertIntoNameMap = true;
  5108. bool oldFound = false;
  5109. auto nameMapEntry = linkedModule->mSymbolNameMap.Find(dwSymbol->mName);
  5110. if (nameMapEntry != NULL)
  5111. {
  5112. oldFound = true;
  5113. if (!isHotSwapPreserve)
  5114. {
  5115. nameMapEntry->mValue = dwSymbol;
  5116. }
  5117. else if (mDbgFlavor == DbgFlavor_MS)
  5118. {
  5119. // Store in our own map - this is needed for storing address of the new vdata
  5120. // so the new values can be copied in
  5121. mSymbolNameMap.Insert(dwSymbol);
  5122. }
  5123. }
  5124. else
  5125. {
  5126. if (isTLS)
  5127. {
  5128. if (mainModule->mTLSExtraAddr == 0)
  5129. {
  5130. auto extraSym = mainModule->mSymbolNameMap.Find("__BFTLS_EXTRA");
  5131. if (extraSym != NULL)
  5132. {
  5133. mainModule->ParseGlobalsData();
  5134. auto itr = mainModule->mStaticVariableMap.find("__BFTLS_EXTRA");
  5135. if (itr != mainModule->mStaticVariableMap.end())
  5136. {
  5137. auto staticVar = itr->second;
  5138. mainModule->mTLSExtraAddr = extraSym->mValue->mAddress;
  5139. mainModule->mTLSExtraSize = staticVar->mType->GetByteCount();
  5140. }
  5141. }
  5142. }
  5143. if ((mainModule->mTLSExtraAddr != 0) && (tlsMappingAddr == 0))
  5144. {
  5145. // Take a chunk out of __BFTLS_EXTRA
  5146. if (mTLSSize <= mainModule->mTLSExtraSize)
  5147. {
  5148. tlsMappingAddr = mainModule->mTLSExtraAddr;
  5149. mainModule->mTLSExtraAddr += mTLSSize;
  5150. mainModule->mTLSExtraSize -= mTLSSize;
  5151. }
  5152. }
  5153. if (tlsMappingAddr != 0)
  5154. {
  5155. BF_ASSERT(symInfo->mValue < mTLSSize);
  5156. dwSymbol->mAddress = tlsMappingAddr + symInfo->mValue;
  5157. }
  5158. if (dwSymbol->mAddress == 0)
  5159. {
  5160. if (!tlsFailed)
  5161. {
  5162. Fail(StrFormat("Hot swapping failed to allocate TLS address for '%s'. Program restart required.", name));
  5163. }
  5164. dwSymbol->mAddress = (addr_target)0xCDCDCDCD;
  5165. tlsFailed = true;
  5166. }
  5167. }
  5168. }
  5169. if (dwSymbol->mAddress != 0)
  5170. {
  5171. if (!oldFound)
  5172. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5173. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5174. }
  5175. }
  5176. }
  5177. else
  5178. {
  5179. //TODO: We don't need to defer symbols anymore... we can just do a Fixup on their addr
  5180. //mDeferredSymbols.PushFront(dwSymbol);
  5181. BP_ALLOC_T(DbgSymbol);
  5182. DbgSymbol* dwSymbol = mAlloc.Alloc<DbgSymbol>();
  5183. dwSymbol->mDbgModule = this;
  5184. dwSymbol->mName = symbolName;
  5185. dwSymbol->mAddress = targetAddr;
  5186. if (!mIsHotObjectFile)
  5187. dwSymbol->mAddress += (addr_target)mImageBase;
  5188. if (mIsHotObjectFile)
  5189. BF_ASSERT((dwSymbol->mAddress >= mImageBase) && (dwSymbol->mAddress < mImageBase + mImageSize));
  5190. mDebugTarget->mSymbolMap.Insert(dwSymbol);
  5191. if (!isStaticSymbol)
  5192. linkedModule->mSymbolNameMap.Insert(dwSymbol);
  5193. }
  5194. }
  5195. }
  5196. }
  5197. if (symInfo->mStorageClass == COFF_SYM_CLASS_FILE)
  5198. {
  5199. const char* fileName = (const char*)&mSymbolData[(symNum + 1) * 18];
  5200. }
  5201. symNum += symInfo->mNumOfAuxSymbols;
  5202. }
  5203. }
  5204. int subProgramSizes = 0;
  5205. for (int subProgramIdx = mStartSubprogramIdx; subProgramIdx < mEndSubprogramIdx; subProgramIdx++)
  5206. {
  5207. auto dwSubprogram = mSubprograms[subProgramIdx];
  5208. subProgramSizes += (int)(dwSubprogram->mBlock.mHighPC - dwSubprogram->mBlock.mLowPC);
  5209. /*for (int i = 0; i < dwSubprogram->mLineDataArray.mSize; i++)
  5210. {
  5211. auto lineData = dwSubprogram->mLineDataArray.mData[i];
  5212. auto srcFile = lineData->mSrcFileRef->mSrcFile;
  5213. srcFile->mLineData.push_back(lineData);
  5214. srcFile->mHadLineData = true;
  5215. if ((srcFile->mFirstLineDataDbgModule == NULL) || (srcFile->mFirstLineDataDbgModule == this))
  5216. srcFile->mFirstLineDataDbgModule = this;
  5217. else
  5218. srcFile->mHasLineDataFromMultipleModules = true;
  5219. }*/
  5220. }
  5221. // Delete srcFiles without line data
  5222. int lineDataCount = 0;
  5223. /*for (int srcFileIdx = startSrcFile; srcFileIdx < (int)mDebugTarget->mSrcFiles.size(); srcFileIdx++)
  5224. {
  5225. if (!mDebugTarget->mSrcFiles[srcFileIdx]->mHadLineData)
  5226. {
  5227. mEmptySrcFiles.push_back(mDebugTarget->mSrcFiles[srcFileIdx]);
  5228. mDebugTarget->mSrcFiles.erase(mDebugTarget->mSrcFiles.begin() + srcFileIdx);
  5229. }
  5230. else
  5231. lineDataCount += (int)mDebugTarget->mSrcFiles[srcFileIdx]->mLineData.size();
  5232. }*/
  5233. auto srcFilesItr = mDebugTarget->mSrcFiles.begin();
  5234. while (srcFilesItr != mDebugTarget->mSrcFiles.end())
  5235. {
  5236. DbgSrcFile* srcFile = srcFilesItr->mValue;
  5237. if ((!srcFile->mHadLineData) && (srcFile->mLocalPath.IsEmpty()))
  5238. {
  5239. mEmptySrcFiles.push_back(srcFile);
  5240. srcFilesItr = mDebugTarget->mSrcFiles.Remove(srcFilesItr);
  5241. }
  5242. else
  5243. {
  5244. ++srcFilesItr;
  5245. }
  5246. }
  5247. if (!mIsHotObjectFile)
  5248. {
  5249. mImageSize = ntHdr.mOptionalHeader.mSizeOfImage;
  5250. mEntryPoint = ntHdr.mOptionalHeader.mAddressOfEntryPoint;
  5251. }
  5252. /*OutputDebugStrF("%s:\n CompileUnits:%d DebugLines: %d Types: %d (%d in map) SubPrograms: %d (%dk) AllocSize:%dk\n", mFilePath.c_str(), mCompileUnits.size(),
  5253. lineDataCount, mEndTypeIdx - mStartTypeIdx, (int)linkedModule->mTypes.size() - mStartTypeIdx, mEndSubprogramIdx - mStartSubprogramIdx, subProgramSizes / 1024, mAlloc.GetAllocSize() / 1024);*/
  5254. if (mIsHotObjectFile)
  5255. {
  5256. // In COFF, we don't necessarily add an actual primary type during MapCompileUnitMethods, so this fixes that
  5257. while (true)
  5258. {
  5259. bool didReplaceType = false;
  5260. for (auto itr = mHotPrimaryTypes.begin(); itr != mHotPrimaryTypes.end(); ++itr)
  5261. {
  5262. auto dbgType = *itr;
  5263. auto primaryType = dbgType->GetPrimaryType();
  5264. if (primaryType != dbgType)
  5265. {
  5266. mHotPrimaryTypes.Remove(itr);
  5267. mHotPrimaryTypes.Add(primaryType);
  5268. didReplaceType = true;
  5269. break;
  5270. }
  5271. }
  5272. if (!didReplaceType)
  5273. break;
  5274. }
  5275. BF_ASSERT(mTypes.size() == 0);
  5276. for (int typeIdx = mStartTypeIdx; typeIdx < (int)linkedModule->mTypes.size(); typeIdx++)
  5277. {
  5278. DbgType* newType = linkedModule->mTypes[typeIdx];
  5279. //if (!newType->mMethodList.IsEmpty())
  5280. if (!newType->mIsDeclaration)
  5281. HotReplaceType(newType);
  5282. }
  5283. }
  5284. if (needHotTargetMemory != 0)
  5285. {
  5286. BF_ASSERT(needHotTargetMemory == mImageSize);
  5287. }
  5288. //BF_ASSERT(mEndTypeIdx == (int)linkedModule->mTypes.size());
  5289. //BF_ASSERT(mEndSubprogramIdx == (int)mSubprograms.size());
  5290. ParseExceptionData();
  5291. mLoadState = DbgModuleLoadState_Loaded;
  5292. if (mMemReporter != NULL)
  5293. {
  5294. mMemReporter->BeginSection("Sections");
  5295. ParseSymbolData();
  5296. Array<DbgSymbol*> orderedSyms;
  5297. for (auto sym : mSymbolNameMap)
  5298. {
  5299. auto dbgSym = sym->mValue;
  5300. orderedSyms.Add(dbgSym);
  5301. }
  5302. orderedSyms.Sort([](DbgSymbol* lhs, DbgSymbol* rhs) { return lhs->mAddress < rhs->mAddress; });
  5303. for (int sectNum = 0; sectNum < ntHdr.mFileHeader.mNumberOfSections; sectNum++)
  5304. {
  5305. PESectionHeader& sectHdr = sectionHeaders[sectNum];
  5306. mMemReporter->BeginSection(sectionNames[sectNum]);
  5307. DbgSymbol* lastSym = NULL;
  5308. for (auto dbgSym : orderedSyms)
  5309. {
  5310. if (dbgSym->mAddress < mImageBase + sectHdr.mVirtualAddress)
  5311. continue;
  5312. if (dbgSym->mAddress >= mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData)
  5313. break;
  5314. if (lastSym != NULL)
  5315. {
  5316. mMemReporter->Add(lastSym->mName, (int)(dbgSym->mAddress - lastSym->mAddress));
  5317. }
  5318. else
  5319. {
  5320. int startingOffset = (int)(dbgSym->mAddress - (mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData));
  5321. if (startingOffset > 0)
  5322. mMemReporter->Add("<StartData>", startingOffset);
  5323. }
  5324. lastSym = dbgSym;
  5325. }
  5326. if (lastSym != NULL)
  5327. mMemReporter->Add(lastSym->mName, (int)((mImageBase + sectHdr.mVirtualAddress + sectHdr.mSizeOfRawData) - lastSym->mAddress));
  5328. else
  5329. {
  5330. mMemReporter->Add("<Unaccounted>", (int)(sectHdr.mSizeOfRawData));
  5331. }
  5332. mMemReporter->EndSection();
  5333. }
  5334. mMemReporter->EndSection();
  5335. mMemReporter->mShowInKB = false;
  5336. mMemReporter->Report();
  5337. }
  5338. return true;
  5339. }
  5340. void DbgModule::FinishHotSwap()
  5341. {
  5342. BF_ASSERT(mIsHotObjectFile);
  5343. auto linkedModule = GetLinkedModule();
  5344. auto mainModule = mDebugTarget->mTargetBinary;
  5345. HashSet<String> failSet;
  5346. String findName;
  5347. for (auto deferredHotResolve : mDeferredHotResolveList)
  5348. {
  5349. addr_target resolveTargetAddr = deferredHotResolve->mNewAddr;
  5350. findName = deferredHotResolve->mName;
  5351. if (mDbgFlavor == DbgFlavor_MS)
  5352. {
  5353. // ... why do we need to find these variables in the variable map instead of the symbol name map?
  5354. }
  5355. auto itr = mainModule->mStaticVariableMap.find(findName.c_str());
  5356. if (itr != mainModule->mStaticVariableMap.end())
  5357. {
  5358. DbgVariable* variable = itr->second;
  5359. resolveTargetAddr = mDebugTarget->GetStaticAddress(variable);
  5360. }
  5361. else
  5362. {
  5363. auto symbolEntry = mainModule->mSymbolNameMap.Find(findName.c_str());
  5364. if (symbolEntry != NULL)
  5365. {
  5366. resolveTargetAddr = symbolEntry->mValue->mAddress;
  5367. }
  5368. else
  5369. {
  5370. if (deferredHotResolve->mName == "__ImageBase")
  5371. {
  5372. resolveTargetAddr = (addr_target)mainModule->mImageBase;
  5373. }
  5374. else
  5375. {
  5376. resolveTargetAddr = mainModule->LocateSymbol(deferredHotResolve->mName);
  5377. if (resolveTargetAddr == 0)
  5378. {
  5379. failSet.Add(deferredHotResolve->mName);
  5380. continue;
  5381. }
  5382. }
  5383. }
  5384. }
  5385. DoReloc(deferredHotResolve->mHotTargetSection, deferredHotResolve->mReloc, resolveTargetAddr, NULL);
  5386. }
  5387. mDeferredHotResolveList.Clear();
  5388. if (!failSet.IsEmpty())
  5389. {
  5390. bool handled = false;
  5391. if (!mDebugger->mDebugManager->mOutMessages.empty())
  5392. {
  5393. auto& str = mDebugger->mDebugManager->mOutMessages.back();
  5394. if (str.Contains("failed to resolve"))
  5395. {
  5396. for (auto& sym : failSet)
  5397. {
  5398. str += ", ";
  5399. str += sym;
  5400. }
  5401. handled = true;
  5402. }
  5403. }
  5404. if (!handled)
  5405. {
  5406. int symIdx = 0;
  5407. String str;
  5408. if (failSet.size() == 1)
  5409. str = "Hot swapping failed to resolve symbol: ";
  5410. else
  5411. str = "Hot swapping failed to resolve symbols: ";
  5412. for (auto& sym : failSet)
  5413. {
  5414. if (symIdx != 0)
  5415. str += ", ";
  5416. str += sym;
  5417. symIdx++;
  5418. }
  5419. mDebugger->Fail(str);
  5420. }
  5421. }
  5422. CommitHotTargetSections();
  5423. // 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)
  5424. // before we can finalize the class vdata.
  5425. ProcessHotSwapVariables();
  5426. for (auto hotTargetSection : mHotTargetSections)
  5427. delete hotTargetSection;
  5428. mHotTargetSections.Clear();
  5429. mSymbolNameMap.Clear();
  5430. }
  5431. addr_target DbgModule::ExecuteOps(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, CPURegisters* registers, DbgAddrType* outAddrType, bool allowReg, addr_target* pushValue)
  5432. {
  5433. const uint8* locDataEnd = locData + locDataLen;
  5434. int regNum = -1;
  5435. addr_target stackFrameData[256];
  5436. int stackIdx = 0;
  5437. if (pushValue != NULL)
  5438. stackFrameData[stackIdx++] = *pushValue;
  5439. while (locData < locDataEnd)
  5440. {
  5441. uint8 opCode = GET_FROM(locData, uint8);
  5442. switch (opCode)
  5443. {
  5444. case DW_OP_piece:
  5445. {
  5446. if (*outAddrType == DbgAddrType_Register)
  5447. *outAddrType = DbgAddrType_Value;
  5448. addr_target val = stackFrameData[--stackIdx];
  5449. int pieceSize = (int)DecodeULEB128(locData);
  5450. if (pieceSize == 4)
  5451. val &= 0xFFFFFFFF;
  5452. else if (pieceSize == 2)
  5453. val &= 0xFFFF;
  5454. else if (pieceSize == 1)
  5455. val &= 0xFF;
  5456. stackFrameData[stackIdx++] = val;
  5457. }
  5458. break;
  5459. case DW_OP_consts:
  5460. {
  5461. int64 val = DecodeSLEB128(locData);
  5462. stackFrameData[stackIdx++] = (addr_target)val;
  5463. }
  5464. break;
  5465. case DW_OP_stack_value:
  5466. {
  5467. *outAddrType = DbgAddrType_Value;
  5468. }
  5469. break;
  5470. case DW_OP_addr_noRemap:
  5471. {
  5472. addr_target addr = GET_FROM(locData, addr_target);
  5473. stackFrameData[stackIdx++] = addr;
  5474. //*outIsAddr = true;
  5475. *outAddrType = DbgAddrType_Target;
  5476. }
  5477. break;
  5478. case DW_OP_addr:
  5479. {
  5480. addr_target addr = GET_FROM(locData, addr_target);
  5481. //if (dwarf != NULL)
  5482. addr = RemapAddr(addr);
  5483. stackFrameData[stackIdx++] = addr;
  5484. //*outIsAddr = true;
  5485. *outAddrType = DbgAddrType_Target;
  5486. }
  5487. break;
  5488. case DW_OP_deref:
  5489. {
  5490. addr_target addr = stackFrameData[--stackIdx];
  5491. addr_target value = mDebugger->ReadMemory<addr_target>(addr);
  5492. stackFrameData[stackIdx++] = value;
  5493. }
  5494. break;
  5495. case DW_OP_fbreg:
  5496. {
  5497. if (registers == NULL)
  5498. return 0;
  5499. BF_ASSERT(dwSubprogram != NULL);
  5500. DbgSubprogram* nonInlinedSubProgram = dwSubprogram->GetRootInlineParent();
  5501. if (nonInlinedSubProgram->mFrameBaseData == NULL)
  5502. {
  5503. *outAddrType = DbgAddrType_Target; //TODO: why?
  5504. return 0;
  5505. }
  5506. BF_ASSERT(nonInlinedSubProgram->mFrameBaseData != NULL);
  5507. uint64 loc = EvaluateLocation(nonInlinedSubProgram, nonInlinedSubProgram->mFrameBaseData, nonInlinedSubProgram->mFrameBaseLen, stackFrame, outAddrType, false);
  5508. int64 offset = DecodeSLEB128(locData);
  5509. loc += offset;
  5510. //loc = BfDebuggerReadMemory(loc);
  5511. //*outIsAddr = true;
  5512. *outAddrType = DbgAddrType_Target;
  5513. stackFrameData[stackIdx++] = (addr_target)loc;
  5514. }
  5515. break;
  5516. case DW_OP_reg0:
  5517. case DW_OP_reg1:
  5518. case DW_OP_reg2:
  5519. case DW_OP_reg3:
  5520. case DW_OP_reg4:
  5521. case DW_OP_reg5:
  5522. case DW_OP_reg6:
  5523. case DW_OP_reg7:
  5524. case DW_OP_reg8:
  5525. case DW_OP_reg9:
  5526. case DW_OP_reg10:
  5527. case DW_OP_reg11:
  5528. case DW_OP_reg12:
  5529. case DW_OP_reg13:
  5530. case DW_OP_reg14:
  5531. case DW_OP_reg15:
  5532. if (registers == NULL)
  5533. return 0;
  5534. BF_ASSERT((opCode - DW_OP_reg0) < CPURegisters::kNumIntRegs);
  5535. regNum = opCode - DW_OP_reg0;
  5536. stackFrameData[stackIdx++] = registers->mIntRegsArray[regNum];
  5537. *outAddrType = DbgAddrType_Register;
  5538. break;
  5539. case DW_OP_reg21: //XMM0
  5540. BF_FATAL("XMM registers not supported yet");
  5541. break;
  5542. case DW_OP_breg0:
  5543. case DW_OP_breg1:
  5544. case DW_OP_breg2:
  5545. case DW_OP_breg3:
  5546. case DW_OP_breg4:
  5547. case DW_OP_breg5:
  5548. case DW_OP_breg6:
  5549. case DW_OP_breg7:
  5550. case DW_OP_breg8:
  5551. case DW_OP_breg9:
  5552. case DW_OP_breg10:
  5553. case DW_OP_breg11:
  5554. case DW_OP_breg12:
  5555. case DW_OP_breg13:
  5556. case DW_OP_breg14:
  5557. case DW_OP_breg15:
  5558. {
  5559. if (registers == NULL)
  5560. return 0;
  5561. int64 offset = DecodeSLEB128(locData);
  5562. BF_ASSERT((opCode - DW_OP_breg0) < CPURegisters::kNumIntRegs);
  5563. auto loc = registers->mIntRegsArray[opCode - DW_OP_breg0] + offset;
  5564. //loc = BfDebuggerReadMemory(loc);
  5565. //*outIsAddr = true;
  5566. *outAddrType = DbgAddrType_Target;
  5567. stackFrameData[stackIdx++] = (addr_target)loc;
  5568. }
  5569. break;
  5570. case DW_OP_bregx:
  5571. {
  5572. if (registers == NULL)
  5573. return 0;
  5574. int regNum = (int)DecodeULEB128(locData);
  5575. int64 offset = DecodeSLEB128(locData);
  5576. BF_ASSERT(regNum < CPURegisters::kNumIntRegs);
  5577. auto loc = registers->mIntRegsArray[regNum] + offset;
  5578. //loc = BfDebuggerReadMemory(loc);
  5579. //*outIsAddr = true;
  5580. *outAddrType = DbgAddrType_Target;
  5581. stackFrameData[stackIdx++] = (addr_target)loc;
  5582. }
  5583. break;
  5584. case DW_OP_const4u:
  5585. {
  5586. uint32 val = GET_FROM(locData, uint32);
  5587. stackFrameData[stackIdx++] = val;
  5588. }
  5589. break;
  5590. case DW_OP_const8u:
  5591. {
  5592. uint64 val = GET_FROM(locData, uint64);
  5593. stackFrameData[stackIdx++] = (addr_target)val;
  5594. }
  5595. break;
  5596. case DW_OP_GNU_push_tls_address:
  5597. {
  5598. if ((mTLSAddr == 0) || (mTLSIndexAddr == 0))
  5599. return 0;
  5600. int tlsIndex = mDebugger->ReadMemory<int>(mTLSIndexAddr);
  5601. addr_target tlsEntry = mDebugger->GetTLSOffset(tlsIndex);
  5602. intptr_target tlsValueIndex = stackFrameData[--stackIdx];
  5603. stackFrameData[stackIdx++] = (tlsValueIndex - mTLSAddr) + tlsEntry;
  5604. *outAddrType = DbgAddrType_Target;
  5605. }
  5606. break;
  5607. case DW_OP_nop:
  5608. break;
  5609. default:
  5610. BF_FATAL("Unknown DW_OP");
  5611. break;
  5612. }
  5613. }
  5614. if (*outAddrType == DbgAddrType_Register)
  5615. {
  5616. if (allowReg)
  5617. return regNum;
  5618. *outAddrType = DbgAddrType_Value;
  5619. }
  5620. //BF_ASSERT(stackIdx == 1);
  5621. return stackFrameData[--stackIdx];
  5622. }
  5623. addr_target DbgModule::EvaluateLocation(DbgSubprogram* dwSubprogram, const uint8* locData, int locDataLen, WdStackFrame* stackFrame, DbgAddrType* outAddrType, bool allowReg)
  5624. {
  5625. BP_ZONE("DebugTarget::EvaluateLocation");
  5626. auto dbgModule = this;
  5627. if (locDataLen == DbgLocationLenKind_SegPlusOffset)
  5628. {
  5629. BF_ASSERT(dbgModule->mDbgFlavor == DbgFlavor_MS);
  5630. if (dbgModule->mDbgFlavor == DbgFlavor_MS)
  5631. {
  5632. COFF* coff = (COFF*)dbgModule;
  5633. struct SegOfsData
  5634. {
  5635. uint32 mOfs;
  5636. uint16 mSeg;
  5637. };
  5638. SegOfsData* segOfsData = (SegOfsData*)locData;
  5639. *outAddrType = DbgAddrType_Target;
  5640. return coff->GetSectionAddr(segOfsData->mSeg, segOfsData->mOfs);
  5641. }
  5642. else
  5643. {
  5644. *outAddrType = DbgAddrType_Target;
  5645. return 0;
  5646. }
  5647. }
  5648. CPURegisters* registers = NULL;
  5649. if (stackFrame != NULL)
  5650. registers = &stackFrame->mRegisters;
  5651. if (locDataLen < 0)
  5652. {
  5653. if (registers == NULL)
  5654. return 0;
  5655. int64 ipAddr = stackFrame->GetSourcePC();
  5656. const uint8* checkLocData = locData;
  5657. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  5658. int64 endLoc = startLoc + GET_FROM(checkLocData, uint16);
  5659. BF_ASSERT(dwSubprogram != NULL);
  5660. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  5661. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  5662. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  5663. {
  5664. locDataLen = -locDataLen - sizeof(addr_target) - sizeof(uint16);
  5665. locData = checkLocData;
  5666. }
  5667. else
  5668. {
  5669. *outAddrType = DbgAddrType_OptimizedOut;
  5670. return 0;
  5671. }
  5672. }
  5673. else if (locDataLen == 0)
  5674. {
  5675. if (registers == NULL)
  5676. return 0;
  5677. int64 ipAddr = stackFrame->GetSourcePC();
  5678. const uint8* checkLocData = locData;
  5679. while (true)
  5680. {
  5681. int64 startLoc = (int64)GET_FROM(checkLocData, addr_target);
  5682. int64 endLoc = (int64)GET_FROM(checkLocData, addr_target);
  5683. if ((startLoc == 0) && (endLoc == 0))
  5684. {
  5685. *outAddrType = DbgAddrType_OptimizedOut;
  5686. return 0;
  5687. }
  5688. BF_ASSERT(dwSubprogram != NULL);
  5689. startLoc += dwSubprogram->mCompileUnit->mLowPC;
  5690. endLoc += dwSubprogram->mCompileUnit->mLowPC;
  5691. if ((ipAddr >= startLoc) && (ipAddr < endLoc))
  5692. {
  5693. locDataLen = GET_FROM(checkLocData, int16);
  5694. locData = checkLocData;
  5695. break;
  5696. }
  5697. else
  5698. {
  5699. int len = GET_FROM(checkLocData, int16);;
  5700. checkLocData += len;
  5701. }
  5702. }
  5703. }
  5704. return ExecuteOps(dwSubprogram, locData, locDataLen, stackFrame, registers, outAddrType, allowReg);
  5705. }
  5706. void DbgModule::ProcessHotSwapVariables()
  5707. {
  5708. BP_ZONE("DbgModule::ProcessHotSwapVariables");
  5709. auto linkedModule = GetLinkedModule();
  5710. for (auto staticVariable : mStaticVariables)
  5711. {
  5712. bool replaceVariable = false;
  5713. const char* findName = staticVariable->GetMappedName();
  5714. auto itr = linkedModule->mStaticVariableMap.find(findName);
  5715. if (itr != linkedModule->mStaticVariableMap.end())
  5716. {
  5717. DbgVariable* oldVariable = itr->second;
  5718. // If the old static field has the same type as the new static field then we keep the same
  5719. // address, otherwise we use the new (zeroed-out) allocated space
  5720. auto _GetNewAddress = [&]()
  5721. {
  5722. addr_target newAddress = 0;
  5723. if (mDbgFlavor == DbgFlavor_GNU)
  5724. {
  5725. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  5726. }
  5727. else
  5728. {
  5729. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  5730. // the location in our own mSymbolNameMap
  5731. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  5732. if (entry != NULL)
  5733. newAddress = entry->mValue->mAddress;
  5734. }
  5735. return newAddress;
  5736. };
  5737. if (oldVariable->mType->IsSizedArray())
  5738. {
  5739. mDebugTarget->GetCompilerSettings();
  5740. bool doMerge = strstr(oldVariable->mName, "sBfClassVData") != NULL;
  5741. bool keepInPlace = (doMerge) && (strstr(oldVariable->mName, ".vext") == NULL);
  5742. if (doMerge)
  5743. {
  5744. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  5745. addr_target newAddress = _GetNewAddress();
  5746. if (newAddress == 0)
  5747. continue;
  5748. uint8* newData = GetHotTargetData(newAddress);
  5749. int newArraySize = staticVariable->mType->GetByteCount();
  5750. int oldArraySize = oldVariable->mType->GetByteCount();
  5751. int copySize = std::min(newArraySize, oldArraySize);
  5752. BF_ASSERT((oldArraySize & (sizeof(addr_target) - 1)) == 0);
  5753. DbgModule* defModule = oldVariable->mType->mCompileUnit->mDbgModule;
  5754. defModule->EnableWriting(oldAddress);
  5755. uint8* mergedData = new uint8[copySize];
  5756. mDebugger->ReadMemory(oldAddress, copySize, mergedData);
  5757. // The new vtable may have 0's in it when virtual methods are removed. Keep the old virtual addresses in those.
  5758. addr_target* newDataPtr = (addr_target*)newData;
  5759. addr_target* mergedPtr = (addr_target*)mergedData;
  5760. while (mergedPtr < (addr_target*)(mergedData + copySize))
  5761. {
  5762. if (*newDataPtr != 0)
  5763. *mergedPtr = *newDataPtr;
  5764. mergedPtr++;
  5765. newDataPtr++;
  5766. }
  5767. bool success;
  5768. success = mDebugger->WriteMemory(oldAddress, mergedData, copySize);
  5769. BF_ASSERT(success);
  5770. memcpy(newData, mergedData, copySize);
  5771. delete mergedData;
  5772. }
  5773. else if (strstr(oldVariable->mName, "sStringLiterals") != NULL)
  5774. {
  5775. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  5776. addr_target newAddress = NULL;
  5777. if (mDbgFlavor == DbgFlavor_GNU)
  5778. {
  5779. newAddress = mDebugTarget->GetStaticAddress(staticVariable);
  5780. }
  5781. else
  5782. {
  5783. // In CodeView, the newVariable ends up pointing to the old address, so we need to store
  5784. // the location in our own mSymbolNameMap
  5785. auto entry = mSymbolNameMap.Find(oldVariable->mLinkName);
  5786. if (entry == NULL)
  5787. continue;
  5788. newAddress = entry->mValue->mAddress;
  5789. }
  5790. // Make sure newAddress doesn't have anything linked to it
  5791. addr_target val = 0;
  5792. bool success = mDebugger->ReadMemory((intptr)newAddress, sizeof(addr_target), &val);
  5793. BF_ASSERT(success);
  5794. BF_ASSERT(val == 0);
  5795. // Link the new table to the old extended table
  5796. addr_target prevLinkage = 0;
  5797. success = mDebugger->ReadMemory((intptr)oldAddress, sizeof(addr_target), &prevLinkage);
  5798. BF_ASSERT(success);
  5799. success = mDebugger->WriteMemory((intptr)newAddress, &prevLinkage, sizeof(addr_target));
  5800. BF_ASSERT(success);
  5801. mDebugger->EnableWriting((intptr)oldAddress, sizeof(addr_target));
  5802. success = mDebugger->WriteMemory((intptr)oldAddress, &newAddress, sizeof(addr_target));
  5803. BF_ASSERT(success);
  5804. keepInPlace = true;
  5805. }
  5806. if (keepInPlace)
  5807. {
  5808. // We have to maintain the OLD size because we can't overwrite the original bounds
  5809. staticVariable->mType = oldVariable->mType;
  5810. staticVariable->mLocationLen = oldVariable->mLocationLen;
  5811. staticVariable->mLocationData = oldVariable->mLocationData;
  5812. staticVariable->mCompileUnit = oldVariable->mCompileUnit;
  5813. }
  5814. }
  5815. else if (oldVariable->mType->Equals(staticVariable->mType))
  5816. {
  5817. if (oldVariable->mType->IsStruct())
  5818. {
  5819. if ((strncmp(oldVariable->mName, "?sBfTypeData@", 13) == 0) || (strncmp(oldVariable->mName, "sBfTypeData.", 12) == 0))
  5820. {
  5821. int size = staticVariable->mType->GetByteCount();
  5822. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  5823. addr_target newAddress = _GetNewAddress();
  5824. if (newAddress == 0)
  5825. continue;
  5826. uint8* data = new uint8[size];
  5827. bool success = mDebugger->ReadMemory(newAddress, size, data);
  5828. if (success)
  5829. {
  5830. mDebugger->EnableWriting((intptr)oldAddress, size);
  5831. success = mDebugger->WriteMemory(oldAddress, data, size);
  5832. }
  5833. delete data;
  5834. BF_ASSERT(success);
  5835. staticVariable->mLocationLen = oldVariable->mLocationLen;
  5836. staticVariable->mLocationData = oldVariable->mLocationData;
  5837. }
  5838. }
  5839. //staticVariable->mLocationLen = oldVariable->mLocationLen;
  5840. //staticVariable->mLocationData = oldVariable->mLocationData;
  5841. replaceVariable = false;
  5842. }
  5843. else
  5844. {
  5845. BF_ASSERT(!oldVariable->mType->IsSizedArray());
  5846. }
  5847. if (!replaceVariable)
  5848. {
  5849. auto symbolVal = linkedModule->mSymbolNameMap.Find(staticVariable->GetMappedName());
  5850. if (symbolVal != NULL)
  5851. {
  5852. addr_target oldAddress = mDebugTarget->GetStaticAddress(oldVariable);
  5853. DbgSymbol* oldSymbol = mDebugTarget->mSymbolMap.Get(oldAddress);
  5854. if (oldSymbol != NULL)
  5855. symbolVal->mValue = oldSymbol;
  5856. }
  5857. }
  5858. }
  5859. else // Not found - new variable
  5860. replaceVariable = true;
  5861. if (replaceVariable)
  5862. {
  5863. linkedModule->mStaticVariableMap[staticVariable->GetMappedName()] = staticVariable;
  5864. }
  5865. }
  5866. }
  5867. int64 DbgModule::GetImageSize()
  5868. {
  5869. return mImageSize;
  5870. }
  5871. /*const uint8* DbgModule::GetOrigImageData(addr_target address)
  5872. {
  5873. return mOrigImageData + (address - mImageBase);
  5874. }*/
  5875. DbgFileExistKind DbgModule::CheckSourceFileExist(const StringImpl& path)
  5876. {
  5877. DbgFileExistKind existsKind = DbgFileExistKind_NotFound;
  5878. if (FileExists(path))
  5879. existsKind = DbgFileExistKind_Found;
  5880. String oldSourceCommand = GetOldSourceCommand(path);
  5881. if (!oldSourceCommand.IsEmpty())
  5882. {
  5883. int crPos = (int)oldSourceCommand.IndexOf('\n');
  5884. if (crPos != -1)
  5885. {
  5886. String targetPath = oldSourceCommand.Substring(0, crPos);
  5887. if (FileExists(targetPath))
  5888. existsKind = DbgFileExistKind_Found;
  5889. else
  5890. existsKind = DbgFileExistKind_HasOldSourceCommand;
  5891. }
  5892. }
  5893. return existsKind;
  5894. }
  5895. void DbgModule::EnableWriting(addr_target address)
  5896. {
  5897. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  5898. {
  5899. DbgSection* section = &mSections[sectionIdx];
  5900. if ((address >= mImageBase + section->mAddrStart) && (address < mImageBase + section->mAddrStart + section->mAddrLength))
  5901. {
  5902. if (!section->mWritingEnabled)
  5903. {
  5904. section->mOldProt = mDebugger->EnableWriting(mImageBase + section->mAddrStart, (int32)section->mAddrLength);
  5905. section->mWritingEnabled = true;
  5906. }
  5907. }
  5908. }
  5909. }
  5910. void DbgModule::RevertWritingEnable()
  5911. {
  5912. for (int sectionIdx = 0; sectionIdx < (int)mSections.size(); sectionIdx++)
  5913. {
  5914. DbgSection* section = &mSections[sectionIdx];
  5915. if (section->mWritingEnabled)
  5916. {
  5917. mDebugger->SetProtection(mImageBase + section->mAddrStart, (int32)section->mAddrLength, section->mOldProt);
  5918. section->mWritingEnabled = false;
  5919. }
  5920. }
  5921. }
  5922. template <typename TRadixMap>
  5923. static void RemoveInvalidRange(TRadixMap& radixMap, addr_target startAddr, int addrLength)
  5924. {
  5925. radixMap.RemoveRange(startAddr, addrLength);
  5926. }
  5927. template <typename TMap>
  5928. static void RemoveInvalidMapRange(TMap& map, addr_target startAddr, int addrLength)
  5929. {
  5930. auto itr = map.lower_bound(startAddr);
  5931. while (itr != map.end())
  5932. {
  5933. auto val = itr->first;
  5934. if (val >= startAddr + addrLength)
  5935. return;
  5936. itr = map.erase(itr);
  5937. }
  5938. }
  5939. void DbgModule::RemoveTargetData()
  5940. {
  5941. BP_ZONE("DbgModule::RemoveTargetData");
  5942. for (auto srcFileRef : mSrcFileDeferredRefs)
  5943. srcFileRef->RemoveDeferredRefs(this);
  5944. HashSet<DbgSrcFile*> visitedFiles;
  5945. for (auto compileUnit : mCompileUnits)
  5946. {
  5947. for (auto& fileRef : compileUnit->mSrcFileRefs)
  5948. {
  5949. if (visitedFiles.Add(fileRef.mSrcFile))
  5950. {
  5951. fileRef.mSrcFile->RemoveLines(this);
  5952. }
  5953. }
  5954. }
  5955. RemoveInvalidRange(mDebugTarget->mSymbolMap, (addr_target)mImageBase, (int32)mImageSize);
  5956. RemoveInvalidRange(mDebugTarget->mSubprogramMap, (addr_target)mImageBase, (int32)mImageSize);
  5957. RemoveInvalidRange(mDebugTarget->mExceptionDirectoryMap, (addr_target)mImageBase, (int32)mImageSize);
  5958. RemoveInvalidRange(mDebugTarget->mContribMap, (addr_target)mImageBase, (int32)mImageSize);
  5959. RemoveInvalidMapRange(mDebugTarget->mDwFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  5960. RemoveInvalidMapRange(mDebugTarget->mCOFFFrameDescriptorMap, (addr_target)mImageBase, (int32)mImageSize);
  5961. //mDebugTarget->mDwFrameDescriptorMap.erase()
  5962. // Remove any of our entries from the mHotReplacedMethodList from 'primary modules' that are not going away
  5963. for (auto dbgType : mHotPrimaryTypes)
  5964. {
  5965. DbgSubprogram** nextSrc = &dbgType->mHotReplacedMethodList.mHead;
  5966. while (*nextSrc != NULL)
  5967. {
  5968. auto* subprogram = *nextSrc;
  5969. if (subprogram->mCompileUnit->mDbgModule == this)
  5970. *nextSrc = subprogram->mNext;
  5971. else
  5972. nextSrc = &(*nextSrc)->mNext;;
  5973. }
  5974. }
  5975. }
  5976. void DbgModule::ReportMemory(MemReporter* memReporter)
  5977. {
  5978. //memReporter->Add("BumpAlloc_Used", mAlloc.GetAllocSize());
  5979. //memReporter->Add("BumpAlloc_Unused", mAlloc.GetTotalAllocSize() - mAlloc.GetAllocSize());
  5980. memReporter->AddBumpAlloc("BumpAlloc", mAlloc);
  5981. memReporter->AddVec(mTypes);
  5982. memReporter->AddVec(mSubprograms);
  5983. //memReporter->Add("TypeMap", mTypeMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  5984. memReporter->AddHashSet("TypeMap", mTypeMap.mMap);
  5985. memReporter->Add("SymbolNameMap", mSymbolNameMap.mAlloc.GetTotalAllocSize() + sizeof(StrHashMap<DbgType*>));
  5986. if (mOrigImageData != NULL)
  5987. {
  5988. memReporter->BeginSection("OrigImageData");
  5989. mOrigImageData->ReportMemory(memReporter);
  5990. memReporter->EndSection();
  5991. }
  5992. }
  5993. DbgType* DbgModule::GetPointerType(DbgType* innerType)
  5994. {
  5995. auto linkedModule = GetLinkedModule();
  5996. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  5997. if (innerType->mPtrType == NULL)
  5998. {
  5999. BP_ALLOC_T(DbgType);
  6000. auto ptrType = mAlloc.Alloc<DbgType>();
  6001. ptrType->mCompileUnit = innerType->mCompileUnit;
  6002. ptrType->mLanguage = innerType->mLanguage;
  6003. ptrType->mTypeCode = DbgType_Ptr;
  6004. ptrType->mTypeParam = innerType;
  6005. ptrType->mSize = sizeof(addr_target);
  6006. ptrType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6007. linkedModule->mTypes.push_back(ptrType);
  6008. innerType->mPtrType = ptrType;
  6009. }
  6010. return innerType->mPtrType;
  6011. }
  6012. DbgType* DbgModule::GetConstType(DbgType* innerType)
  6013. {
  6014. auto linkedModule = GetLinkedModule();
  6015. BF_ASSERT(innerType->GetDbgModule()->GetLinkedModule() == linkedModule);
  6016. /*auto itr = linkedModule->mConstTypes.find(innerType);
  6017. if (itr != linkedModule->mConstTypes.end())
  6018. return itr->second;*/
  6019. DbgType* constType = NULL;
  6020. if (linkedModule->mConstTypes.TryGetValue(innerType, &constType))
  6021. return constType;
  6022. BP_ALLOC_T(DbgType);
  6023. constType = mAlloc.Alloc<DbgType>();
  6024. constType->mCompileUnit = innerType->mCompileUnit;
  6025. constType->mLanguage = innerType->mLanguage;
  6026. constType->mTypeCode = DbgType_Const;
  6027. constType->mTypeParam = innerType;
  6028. constType->mSize = sizeof(addr_target);
  6029. constType->mTypeIdx = (int32)linkedModule->mTypes.size();
  6030. linkedModule->mTypes.push_back(constType);
  6031. linkedModule->mConstTypes[innerType] = constType;
  6032. return constType;
  6033. }
  6034. DbgType* DbgModule::GetPrimaryType(DbgType* dbgType)
  6035. {
  6036. if (dbgType->mPriority <= DbgTypePriority_Normal)
  6037. {
  6038. if ((dbgType->mLanguage == DbgLanguage_Beef) && (dbgType->mName != NULL))
  6039. {
  6040. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6041. if (newTypeEntry != NULL)
  6042. {
  6043. DbgType* newType = newTypeEntry->mValue;
  6044. if ((newType->mTypeCode == DbgType_Ptr) && (newType->IsBfObjectPtr()))
  6045. newType = newType->mTypeParam;
  6046. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6047. return newType;
  6048. }
  6049. }
  6050. else if (dbgType->mName != NULL)
  6051. {
  6052. auto newTypeEntry = FindType(dbgType->mName, dbgType->mLanguage);
  6053. if (newTypeEntry != NULL)
  6054. {
  6055. DbgType* newType = newTypeEntry->mValue;
  6056. newType = newType->RemoveModifiers();
  6057. if (newType != dbgType)
  6058. newType = GetPrimaryType(newType);
  6059. newType->mPriority = DbgTypePriority_Primary_Implicit;
  6060. return newType;
  6061. }
  6062. }
  6063. }
  6064. return dbgType;
  6065. }
  6066. DbgType* DbgModule::GetInnerTypeOrVoid(DbgType* dbgType)
  6067. {
  6068. if (dbgType->mTypeParam != NULL)
  6069. return dbgType->mTypeParam;
  6070. return GetPrimitiveType(DbgType_Void, dbgType->mLanguage);
  6071. }
  6072. DbgType* DbgModule::FindTypeHelper(const String& typeName, DbgType* checkType)
  6073. {
  6074. for (auto subType : checkType->mSubTypeList)
  6075. {
  6076. if (strcmp(subType->mTypeName, typeName.c_str()) == 0)
  6077. return subType;
  6078. }
  6079. for (auto baseType : checkType->mBaseTypes)
  6080. {
  6081. auto retType = FindTypeHelper(typeName, baseType->mBaseType);
  6082. if (retType != NULL)
  6083. return retType;
  6084. }
  6085. return NULL;
  6086. }
  6087. DbgType* DbgModule::FindType(const String& typeName, DbgType* contextType, DbgLanguage language)
  6088. {
  6089. if ((language == DbgLanguage_Unknown) && (contextType != NULL))
  6090. language = contextType->mLanguage;
  6091. if (typeName.length() > 0)
  6092. {
  6093. if (typeName[typeName.length() - 1] == '*')
  6094. {
  6095. DbgType* dbgType = FindType(typeName.Substring(0, typeName.length() - 1), contextType, language);
  6096. if (dbgType == NULL)
  6097. return NULL;
  6098. return GetPointerType(dbgType);
  6099. }
  6100. }
  6101. auto entry = GetLinkedModule()->mTypeMap.Find(typeName.c_str(), language);
  6102. if (entry != NULL)
  6103. return entry->mValue;
  6104. if (contextType != NULL)
  6105. {
  6106. DbgType* checkType = contextType;
  6107. if (checkType->IsPointer())
  6108. checkType = checkType->mTypeParam;
  6109. return FindTypeHelper(typeName, checkType);
  6110. }
  6111. return NULL;
  6112. }
  6113. DbgTypeMap::Entry* DbgModule::FindType(const char* typeName, DbgLanguage language)
  6114. {
  6115. return GetLinkedModule()->mTypeMap.Find(typeName, language);
  6116. /*auto& typeMap = GetLinkedModule()->mTypeMap;
  6117. auto dbgTypeEntry = typeMap.Find(typeName);
  6118. if (dbgTypeEntry == NULL)
  6119. return NULL;
  6120. if (dbgTypeEntry->mValue->mLanguage == language)
  6121. return dbgTypeEntry;
  6122. while (dbgTypeEntry != NULL)
  6123. {
  6124. DbgType* dbgType = dbgTypeEntry->mValue;
  6125. if ((dbgType->GetLanguage() == language) && (typeMap.StrEqual(dbgType->mName, typeName)))
  6126. return dbgTypeEntry;
  6127. dbgTypeEntry = dbgTypeEntry->mNext;
  6128. }*/
  6129. //return NULL;
  6130. }
  6131. DbgType* DbgModule::GetPrimitiveType(DbgTypeCode typeCode, DbgLanguage language)
  6132. {
  6133. if (language == DbgLanguage_Beef)
  6134. return mBfPrimitiveTypes[(int)typeCode];
  6135. else
  6136. return mCPrimitiveTypes[(int)typeCode];
  6137. }
  6138. DbgType* DbgModule::GetPrimitiveStructType(DbgTypeCode typeCode)
  6139. {
  6140. const char* name = mPrimitiveStructNames[typeCode];
  6141. if (name == NULL)
  6142. return NULL;
  6143. return FindType(name, NULL, DbgLanguage_Beef);
  6144. }
  6145. DbgType* DbgModule::GetSizedArrayType(DbgType * elementType, int count)
  6146. {
  6147. auto linkedModule = GetLinkedModule();
  6148. if ((linkedModule != NULL) && (linkedModule != this))
  6149. {
  6150. return linkedModule->GetSizedArrayType(elementType, count);
  6151. }
  6152. DbgType** sizedArrayTypePtr;
  6153. DbgSizedArrayEntry entry;
  6154. entry.mElementType = elementType;
  6155. entry.mCount = count;
  6156. if (mSizedArrayTypes.TryAdd(entry, NULL, &sizedArrayTypePtr))
  6157. {
  6158. BP_ALLOC_T(DbgType);
  6159. auto sizedArrayType = mAlloc.Alloc<DbgType>();
  6160. sizedArrayType->mCompileUnit = elementType->mCompileUnit;
  6161. sizedArrayType->mLanguage = elementType->mLanguage;
  6162. sizedArrayType->mTypeCode = DbgType_SizedArray;
  6163. sizedArrayType->mTypeParam = elementType;
  6164. sizedArrayType->mSize = count * elementType->GetStride();
  6165. sizedArrayType->mAlign = elementType->GetAlign();
  6166. sizedArrayType->mSizeCalculated = true;
  6167. sizedArrayType->mTypeIdx = (int32)mTypes.size();
  6168. linkedModule->mTypes.push_back(sizedArrayType);
  6169. *sizedArrayTypePtr = sizedArrayType;
  6170. }
  6171. return *sizedArrayTypePtr;
  6172. }