DbgModule.cpp 195 KB

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