CeDebugger.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950
  1. #include "CeDebugger.h"
  2. #include "CeMachine.h"
  3. #include "BfCompiler.h"
  4. #include "../DebugManager.h"
  5. #include "BeefySysLib/util/BeefPerf.h"
  6. #include "BfParser.h"
  7. #include "BfReducer.h"
  8. #include "BeefySysLib/util/UTF8.h"
  9. #include "BfUtil.h"
  10. #include "BfExprEvaluator.h"
  11. #include "BeefySysLib/util/BitSet.h"
  12. #include "../DebugVisualizers.h"
  13. #include "BfAutoComplete.h"
  14. #include "BfDemangler.h"
  15. USING_NS_BF;
  16. static addr_ce DecodeTargetDataPtr(const char*& strRef)
  17. {
  18. addr_ce val = (addr_ce)stouln(strRef, sizeof(addr_ce) * 2);
  19. strRef += sizeof(addr_ce) * 2;
  20. return val;
  21. }
  22. //////////////////////////////////////////////////////////////////////////
  23. CeBreakpointCondition::~CeBreakpointCondition()
  24. {
  25. delete mDbgEvaluationContext;
  26. }
  27. //////////////////////////////////////////////////////////////////////////
  28. CeBreakpoint::~CeBreakpoint()
  29. {
  30. delete mCondition;
  31. }
  32. //////////////////////////////////////////////////////////////////////////
  33. CePendingExpr::CePendingExpr()
  34. {
  35. mThreadId = -1;
  36. mCallStackIdx = -1;
  37. mPassInstance = NULL;
  38. mParser = NULL;
  39. mCursorPos = -1;
  40. mExprNode = NULL;
  41. mIdleTicks = 0;
  42. mExplitType = NULL;
  43. mExpressionFlags = DwEvalExpressionFlag_None;
  44. mDone = false;
  45. }
  46. CePendingExpr::~CePendingExpr()
  47. {
  48. delete mParser;
  49. delete mPassInstance;
  50. }
  51. //////////////////////////////////////////////////////////////////////////
  52. CeEvaluationContext::CeEvaluationContext(CeDebugger* winDebugger, const StringImpl& expr, CeFormatInfo* formatInfo, BfTypedValue contextValue)
  53. {
  54. Init(winDebugger, expr, formatInfo, contextValue);
  55. }
  56. void CeEvaluationContext::Init(CeDebugger* ceDebugger, const StringImpl& expr, CeFormatInfo* formatInfo, BfTypedValue contextValue)
  57. {
  58. mDebugger = ceDebugger;
  59. mCallStackIdx = 0;
  60. mParser = NULL;
  61. mReducer = NULL;
  62. mPassInstance = NULL;
  63. mExprEvaluator = NULL;
  64. mExprNode = NULL;
  65. if (expr.empty())
  66. return;
  67. int atPos = (int)expr.IndexOf('@');
  68. if ((atPos != -1) && (atPos < expr.mLength - 2) && (expr[atPos + 1] == '0') && (expr[atPos + 2] == 'x'))
  69. {
  70. bool isValid = true;
  71. for (int i = 0; i < atPos; i++)
  72. {
  73. char c = expr[i];
  74. if ((c < '0') || (c > '9'))
  75. {
  76. isValid = false;
  77. break;
  78. }
  79. }
  80. if (isValid)
  81. {
  82. int parseLength = expr.mLength;
  83. for (int i = 0; i < expr.mLength; i++)
  84. {
  85. if ((expr[i] == ',') || (::isspace((uint8)expr[i])))
  86. {
  87. parseLength = i;
  88. break;
  89. }
  90. }
  91. mExprString = expr.Substring(0, parseLength);
  92. String typeIdStr = expr.Substring(0, atPos);
  93. String addrStr = expr.Substring(atPos + 3, parseLength - atPos - 3);
  94. int typeId = strtol(typeIdStr.c_str(), NULL, 10);
  95. int64 addrVal = strtoll(addrStr.c_str(), NULL, 16);
  96. if ((typeId != 0) && (addrVal != 0))
  97. {
  98. auto type = ceDebugger->mCompiler->mContext->FindTypeById(typeId);
  99. if (type != NULL)
  100. {
  101. auto module = ceDebugger->mCeMachine->mCeModule;
  102. if (type->IsObjectOrInterface())
  103. {
  104. mResultOverride = BfTypedValue(module->mBfIRBuilder->CreateIntToPtr(
  105. module->mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, (uint64)addrVal), module->mBfIRBuilder->MapType(type)), type);
  106. }
  107. else
  108. {
  109. mResultOverride = BfTypedValue(module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapType(type), (addr_ce)addrVal), type, true);
  110. }
  111. }
  112. }
  113. return;
  114. }
  115. }
  116. mParser = new BfParser(ceDebugger->mCompiler->mSystem);
  117. mPassInstance = new BfPassInstance(ceDebugger->mCompiler->mSystem);
  118. auto terminatedExpr = expr + ";";
  119. mParser->SetSource(terminatedExpr.c_str(), (int)terminatedExpr.length());
  120. mParser->Parse(mPassInstance);
  121. mReducer = new BfReducer();
  122. mReducer->mAlloc = mParser->mAlloc;
  123. mReducer->mSystem = ceDebugger->mCompiler->mSystem;
  124. mReducer->mPassInstance = mPassInstance;
  125. mReducer->mVisitorPos = BfReducer::BfVisitorPos(mParser->mRootNode);
  126. mReducer->mVisitorPos.MoveNext();
  127. mReducer->mSource = mParser;
  128. mExprNode = mReducer->CreateExpression(mParser->mRootNode->GetFirst());
  129. mParser->Close();
  130. mExprEvaluator = new BfExprEvaluator(ceDebugger->mCeMachine->mCeModule);
  131. if ((formatInfo != NULL) && (mExprNode != NULL) && (mExprNode->GetSrcEnd() < (int)expr.length()))
  132. {
  133. String formatFlags = expr.Substring(mExprNode->GetSrcEnd());
  134. String errorString = "Invalid expression";
  135. if (!ceDebugger->ParseFormatInfo(formatFlags, formatInfo, mPassInstance, NULL, NULL, &errorString, contextValue))
  136. {
  137. mPassInstance->FailAt(errorString, mParser->mSourceData, mExprNode->GetSrcEnd(), (int)expr.length() - mExprNode->GetSrcEnd());
  138. formatFlags = "";
  139. }
  140. }
  141. if (formatInfo != NULL)
  142. {
  143. mExplicitThis = formatInfo->mExplicitThis;
  144. mCallStackIdx = formatInfo->mCallStackIdx;
  145. }
  146. mExprNode->ToString(mExprString);
  147. }
  148. bool CeEvaluationContext::HasExpression()
  149. {
  150. return !mExprString.IsEmpty();
  151. }
  152. CeEvaluationContext::~CeEvaluationContext()
  153. {
  154. delete mParser;
  155. delete mReducer;
  156. delete mExprEvaluator;
  157. delete mPassInstance;
  158. }
  159. BfTypedValue CeEvaluationContext::EvaluateInContext(BfTypedValue contextTypedValue, CeDbgState* dbgState)
  160. {
  161. if (mResultOverride)
  162. return mResultOverride;
  163. if (mExprNode == NULL)
  164. return BfTypedValue();
  165. mPassInstance->ClearErrors();
  166. auto ceFrame = mDebugger->GetFrame(mCallStackIdx);
  167. auto module = mDebugger->mCeMachine->mCeModule;
  168. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, ceFrame->mFunction->mMethodInstance->GetOwner());
  169. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(module->mCurMethodInstance, ceFrame->mFunction->mMethodInstance);
  170. SetAndRestoreValue<BfPassInstance*> prevPassInstance(mDebugger->mCompiler->mPassInstance, mPassInstance);
  171. SetAndRestoreValue<bool> prevIgnoreWrites(module->mBfIRBuilder->mIgnoreWrites, true);
  172. BfMethodState methodState;
  173. SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, &methodState);
  174. methodState.mTempKind = module->mCurMethodInstance->mMethodDef->mIsStatic ? BfMethodState::TempKind_Static : BfMethodState::TempKind_NonStatic;
  175. CeDbgState localDbgState;
  176. if (dbgState == NULL)
  177. dbgState = &localDbgState;
  178. dbgState->mActiveFrame = ceFrame;
  179. dbgState->mCeContext = mDebugger->mCeMachine->mCurContext;
  180. if (contextTypedValue)
  181. dbgState->mExplicitThis = contextTypedValue;
  182. else
  183. dbgState->mExplicitThis = mExplicitThis;
  184. SetAndRestoreValue<CeDbgState*> prevDbgState(mDebugger->mCurDbgState, dbgState);
  185. BfTypedValue exprResult;
  186. mExprEvaluator->VisitChildNoRef(mExprNode);
  187. auto result = mExprEvaluator->mResult;
  188. if ((result) && (!result.mType->IsComposite()))
  189. result = module->LoadValue(result);
  190. return result;
  191. }
  192. bool CeEvaluationContext::HadError()
  193. {
  194. return (mPassInstance != NULL) && (mPassInstance->mFailedIdx != 0);
  195. }
  196. String CeEvaluationContext::GetErrorStr()
  197. {
  198. if (mPassInstance == NULL)
  199. return "";
  200. String errorStr = mPassInstance->mErrors[0]->mError;
  201. if (mExprNode != NULL)
  202. {
  203. errorStr += ": ";
  204. errorStr += mExprNode->ToString();
  205. }
  206. return errorStr;
  207. }
  208. //////////////////////////////////////////////////////////////////////////
  209. CeDebugger::CeDebugger(DebugManager* debugManager, BfCompiler* bfCompiler)
  210. {
  211. mDebugManager = debugManager;
  212. mCompiler = bfCompiler;
  213. mCeMachine = bfCompiler->mCeMachine;
  214. mRunState = RunState_Running;
  215. mCeMachine->mDebugger = this;
  216. mCeMachine->mDebugEvent.Reset();
  217. mDebugPendingExpr = NULL;
  218. mCurDbgState = NULL;
  219. mBreakpointVersion = 0;
  220. mBreakpointCacheDirty = false;
  221. mBreakpointFramesDirty = false;
  222. mCurDisasmFuncId = 0;
  223. mActiveBreakpoint = NULL;
  224. mCurEvaluationContext = NULL;
  225. mPendingActiveFrameOffset = 0;
  226. }
  227. String CeDebugger::TypeToString(const BfTypedValue& typedValue)
  228. {
  229. if (typedValue.mType == NULL)
  230. return "null";
  231. if (typedValue.IsReadOnly())
  232. return String("readonly ") + mCeMachine->mCeModule->TypeToString(typedValue.mType);
  233. return mCeMachine->mCeModule->TypeToString(typedValue.mType);
  234. }
  235. String CeDebugger::TypeToString(BfType* type, CeTypeModKind typeModKind)
  236. {
  237. if (type == NULL)
  238. return "null";
  239. String str;
  240. if (typeModKind == CeTypeModKind_ReadOnly)
  241. str += "readonly ";
  242. else if (typeModKind == CeTypeModKind_Const)
  243. str += "const ";
  244. str += mCeMachine->mCeModule->TypeToString(type);
  245. return str;
  246. }
  247. CeDebugger::~CeDebugger()
  248. {
  249. mCeMachine->mDebugEvent.Set(true);
  250. mCeMachine->mDebugger = NULL;
  251. delete mDebugPendingExpr;
  252. for (auto breakpoint : mBreakpoints)
  253. delete breakpoint;
  254. for (auto kv : mFileInfo)
  255. delete kv.mValue;
  256. }
  257. void CeDebugger::OutputMessage(const StringImpl& msg)
  258. {
  259. if (this == NULL)
  260. return;
  261. AutoCrit autoCrit(mDebugManager->mCritSect);
  262. mDebugManager->mOutMessages.push_back("msg " + msg);
  263. }
  264. void CeDebugger::OutputRawMessage(const StringImpl& msg)
  265. {
  266. if (this == NULL)
  267. return;
  268. AutoCrit autoCrit(mDebugManager->mCritSect);
  269. mDebugManager->mOutMessages.push_back(msg);
  270. }
  271. int CeDebugger::GetAddrSize()
  272. {
  273. return sizeof(addr_ce);
  274. }
  275. bool CeDebugger::CanOpen(const StringImpl& fileName, DebuggerResult* outResult)
  276. {
  277. return false;
  278. }
  279. void CeDebugger::OpenFile(const StringImpl& launchPath, const StringImpl& targetPath, const StringImpl& args, const StringImpl& workingDir, const Array<uint8>& envBlock, bool hotSwapEnabled, DbgOpenFileFlags openFileFlags)
  280. {
  281. }
  282. bool CeDebugger::Attach(int processId, BfDbgAttachFlags attachFlags)
  283. {
  284. return false;
  285. }
  286. void CeDebugger::GetStdHandles(BfpFile** outStdIn, BfpFile** outStdOut, BfpFile** outStdErr)
  287. {
  288. }
  289. void CeDebugger::Run()
  290. {
  291. }
  292. void CeDebugger::HotLoad(const Array<String>& objectFiles, int hotIdx)
  293. {
  294. }
  295. void CeDebugger::InitiateHotResolve(DbgHotResolveFlags flags)
  296. {
  297. }
  298. intptr CeDebugger::GetDbgAllocHeapSize()
  299. {
  300. return intptr();
  301. }
  302. String CeDebugger::GetDbgAllocInfo()
  303. {
  304. return String();
  305. }
  306. void CeDebugger::Update()
  307. {
  308. AutoCrit autoCrit(mCeMachine->mCritSect);
  309. if ((mRunState == RunState_Terminated) || (mRunState == RunState_Terminating))
  310. return;
  311. if (mDebugPendingExpr != NULL)
  312. {
  313. if (mDebugPendingExpr->mDone)
  314. mRunState = RunState_DebugEval_Done;
  315. else
  316. mRunState = RunState_DebugEval;
  317. }
  318. else if (mCeMachine->mDbgPaused)
  319. mRunState = RunState_Paused;
  320. else
  321. mRunState = RunState_Running;
  322. }
  323. void CeDebugger::UpdateBreakpointFrames()
  324. {
  325. if (mBreakpointCacheDirty)
  326. UpdateBreakpointCache();
  327. for (auto& callStack : mCeMachine->mCurContext->mCallStack)
  328. {
  329. if (callStack.mFunction->mBreakpointVersion != mBreakpointVersion)
  330. UpdateBreakpoints(callStack.mFunction);
  331. }
  332. }
  333. void CeDebugger::ContinueDebugEvent()
  334. {
  335. AutoCrit autoCrit(mCeMachine->mCritSect);
  336. mRunState = RunState_Running;
  337. mActiveBreakpoint = NULL;
  338. mPendingActiveFrameOffset = 0;
  339. mDbgCallStack.Clear();
  340. if (mBreakpointFramesDirty)
  341. UpdateBreakpointFrames();
  342. mCeMachine->mDebugEvent.Set();
  343. }
  344. void CeDebugger::ForegroundTarget(int altProcessId)
  345. {
  346. }
  347. bool CeDebugger::CheckConditionalBreakpoint(CeBreakpoint* breakpoint)
  348. {
  349. auto _SplitExpr = [&](const StringImpl& expr, StringImpl& outExpr, StringImpl& outSubject)
  350. {
  351. int crPos = (int)expr.IndexOf('\n');
  352. if (crPos != -1)
  353. {
  354. outExpr += expr.Substring(0, crPos);
  355. outSubject += expr.Substring(crPos + 1);
  356. }
  357. else
  358. {
  359. outExpr += expr;
  360. }
  361. };
  362. if (breakpoint->mCondition != NULL)
  363. {
  364. ClearCallStack();
  365. auto conditional = breakpoint->mCondition;
  366. if (conditional->mDbgEvaluationContext == NULL)
  367. {
  368. StringT<256> expr;
  369. StringT<256> subjectExpr;
  370. _SplitExpr(conditional->mExpr, expr, subjectExpr);
  371. conditional->mDbgEvaluationContext = new CeEvaluationContext(this, expr);
  372. conditional->mDbgEvaluationContext->mCallStackIdx = -1;
  373. }
  374. CeDbgState dbgState;
  375. BfTypedValue result = conditional->mDbgEvaluationContext->EvaluateInContext(BfTypedValue(), &dbgState);
  376. if (conditional->mDbgEvaluationContext->mPassInstance->HasFailed())
  377. {
  378. String errorStr = "FAILED";
  379. for (auto error : conditional->mDbgEvaluationContext->mPassInstance->mErrors)
  380. {
  381. if (!error->mIsWarning)
  382. errorStr = error->mError;
  383. }
  384. String condError = StrFormat("error Conditional breakpoint expression '%s' failed: %s", conditional->mExpr.c_str(), errorStr.c_str());
  385. mDebugManager->mOutMessages.push_back(condError);
  386. return true;
  387. }
  388. else if (dbgState.mBlockedSideEffects)
  389. {
  390. mDebugManager->mOutMessages.push_back(StrFormat("error Conditional breakpoint expression '%s' contained function calls, which is not allowed", conditional->mExpr.c_str()));
  391. return true;
  392. }
  393. else if ((!result) || (!result.mType->IsBoolean()))
  394. {
  395. mDebugManager->mOutMessages.push_back(StrFormat("error Conditional breakpoint expression '%s' must result in a boolean value", conditional->mExpr.c_str()));
  396. return true;
  397. }
  398. else if (ValueToInt(result) <= 0)
  399. return false;
  400. }
  401. breakpoint->mHitCount++;
  402. switch (breakpoint->mHitCountBreakKind)
  403. {
  404. case DbgHitCountBreakKind_Equals:
  405. if (breakpoint->mHitCount != breakpoint->mTargetHitCount)
  406. return false;
  407. break;
  408. case DbgHitCountBreakKind_GreaterEquals:
  409. if (breakpoint->mHitCount < breakpoint->mTargetHitCount)
  410. return false;
  411. break;
  412. case DbgHitCountBreakKind_Multiple:
  413. if ((breakpoint->mHitCount % breakpoint->mTargetHitCount) != 0)
  414. return false;
  415. break;
  416. }
  417. if (!breakpoint->mLogging.IsEmpty())
  418. {
  419. auto ceContext = mCeMachine->mCurContext;
  420. CeDbgState dbgState;
  421. dbgState.mActiveFrame = &ceContext->mCallStack[0];
  422. dbgState.mCeContext = mCeMachine->mCurContext;
  423. SetAndRestoreValue<CeDbgState*> prevDbgState(mCurDbgState, &dbgState);
  424. ClearCallStack();
  425. CeFormatInfo formatInfo;
  426. formatInfo.mCallStackIdx = -1;
  427. auto prevRunState = mRunState;
  428. mRunState = RunState_Paused; // We need to be paused to avoid certain errors in the eval
  429. String displayString;
  430. String expr;
  431. _SplitExpr(breakpoint->mLogging, expr, formatInfo.mSubjectExpr);
  432. ProcessEvalString(BfTypedValue(), expr, displayString, formatInfo, NULL, false);
  433. mRunState = prevRunState;
  434. displayString.Insert(0, "log ");
  435. displayString.Append("\n");
  436. mDebugManager->mOutMessages.push_back(displayString);
  437. if (!breakpoint->mBreakAfterLogging)
  438. return false;
  439. }
  440. return true;
  441. }
  442. Breakpoint* CeDebugger::CreateBreakpoint(const StringImpl& fileName, int lineNum, int wantColumn, int instrOffset)
  443. {
  444. ClearBreakpointCache();
  445. auto breakpoint = new CeBreakpoint();
  446. breakpoint->mFilePath = fileName;
  447. breakpoint->mRequestedLineNum = lineNum;
  448. breakpoint->mLineNum = lineNum;
  449. breakpoint->mColumn = wantColumn;
  450. breakpoint->mInstrOffset = instrOffset;
  451. mBreakpoints.Add(breakpoint);
  452. mBreakpointVersion++;
  453. return breakpoint;
  454. }
  455. Breakpoint* CeDebugger::CreateMemoryBreakpoint(intptr addr, int byteCount)
  456. {
  457. return nullptr;
  458. }
  459. Breakpoint* CeDebugger::CreateSymbolBreakpoint(const StringImpl& symbolName)
  460. {
  461. return nullptr;
  462. }
  463. Breakpoint* CeDebugger::CreateAddressBreakpoint(intptr address)
  464. {
  465. return nullptr;
  466. }
  467. uintptr CeDebugger::GetBreakpointAddr(Breakpoint* breakpoint)
  468. {
  469. if (mBreakpointCacheDirty)
  470. {
  471. UpdateBreakpointCache();
  472. UpdateBreakpointAddrs();
  473. }
  474. return breakpoint->GetAddr();
  475. }
  476. void CeDebugger::CheckBreakpoint(Breakpoint* breakpoint)
  477. {
  478. }
  479. void CeDebugger::HotBindBreakpoint(Breakpoint* breakpoint, int lineNum, int hotIdx)
  480. {
  481. }
  482. int64 CeDebugger::ValueToInt(addr_ce addr, BfType* type)
  483. {
  484. if ((!type->IsInteger()) && (!type->IsBoolean()))
  485. return 0;
  486. auto primType = (BfPrimitiveType*)type;
  487. auto ceContext = mCeMachine->mCurContext;
  488. int64 val = 0;
  489. memcpy(&val, ceContext->mMemory.mVals + addr, type->mSize);
  490. switch (primType->mTypeDef->mTypeCode)
  491. {
  492. case BfTypeCode_Int8:
  493. val = *(int8*)&val;
  494. break;
  495. case BfTypeCode_Int16:
  496. val = *(int16*)&val;
  497. break;
  498. case BfTypeCode_Int32:
  499. val = *(int32*)&val;
  500. break;
  501. }
  502. return val;
  503. }
  504. int64 CeDebugger::ValueToInt(const BfTypedValue& typedVal)
  505. {
  506. auto ceModule = mCeMachine->mCeModule;
  507. auto constant = ceModule->mBfIRBuilder->GetConstant(typedVal.mValue);
  508. if (constant == NULL)
  509. return 0;
  510. if (typedVal.IsAddr())
  511. {
  512. BfType* type = typedVal.mType;
  513. if (type->IsTypedPrimitive())
  514. type = type->GetUnderlyingType();
  515. if ((type->IsInteger()) || (type->IsBoolean()))
  516. {
  517. auto ceTypedVal = GetAddr(constant);
  518. if (ceTypedVal)
  519. return ValueToInt((addr_ce)ceTypedVal.mAddr, type);
  520. }
  521. return 0;
  522. }
  523. if (constant->mConstType == BfConstType_ExtractValue)
  524. {
  525. auto fromConstGEP = (BfConstantExtractValue*)constant;
  526. auto fromTarget = ceModule->mBfIRBuilder->GetConstantById(fromConstGEP->mTarget);
  527. if (fromTarget->mConstType == BfConstType_AggCE)
  528. {
  529. auto aggCE = (BfConstantAggCE*)fromTarget;
  530. auto dbgTypeInfo = GetDbgTypeInfo(aggCE->mType);
  531. if (dbgTypeInfo == NULL)
  532. return 0;
  533. auto typeInst = dbgTypeInfo->mType->ToTypeInstance();
  534. if (typeInst == NULL)
  535. return 0;
  536. auto fieldInfo = &dbgTypeInfo->mFieldOffsets[fromConstGEP->mIdx0];
  537. return ValueToInt(aggCE->mCEAddr + fieldInfo->mDataOffset, fieldInfo->mType);
  538. }
  539. }
  540. if ((BfIRConstHolder::IsInt(constant->mTypeCode)) || (constant->mTypeCode == BfTypeCode_Boolean))
  541. return constant->mInt64;
  542. return 0;
  543. }
  544. void CeDebugger::DeleteBreakpoint(Breakpoint* breakpoint)
  545. {
  546. if (mActiveBreakpoint == breakpoint)
  547. mActiveBreakpoint = NULL;
  548. mBreakpoints.Remove((CeBreakpoint*)breakpoint);
  549. delete breakpoint;
  550. ClearBreakpointCache();
  551. mBreakpointVersion++;
  552. }
  553. void CeDebugger::DetachBreakpoint(Breakpoint* breakpoint)
  554. {
  555. }
  556. void CeDebugger::MoveBreakpoint(Breakpoint* breakpoint, int lineNum, int wantColumn, bool rebindNow)
  557. {
  558. breakpoint->mLineNum = lineNum;
  559. breakpoint->mColumn = wantColumn;
  560. ClearBreakpointCache();
  561. mBreakpointVersion++;
  562. }
  563. void CeDebugger::MoveMemoryBreakpoint(Breakpoint* breakpoint, intptr addr, int byteCount)
  564. {
  565. }
  566. void CeDebugger::DisableBreakpoint(Breakpoint* breakpoint)
  567. {
  568. }
  569. void CeDebugger::SetBreakpointCondition(Breakpoint* breakpoint, const StringImpl& conditionExpr)
  570. {
  571. AutoCrit autoCrit(mDebugManager->mCritSect);
  572. CeBreakpoint* wdBreakpoint = (CeBreakpoint*)breakpoint;
  573. BF_ASSERT(!wdBreakpoint->mIsLinkedSibling);
  574. if (conditionExpr.empty())
  575. {
  576. delete wdBreakpoint->mCondition;
  577. CeBreakpoint* curBreakpoint = wdBreakpoint;
  578. wdBreakpoint->mCondition = NULL;
  579. }
  580. else
  581. {
  582. delete wdBreakpoint->mCondition;
  583. auto condition = new CeBreakpointCondition();
  584. condition->mExpr = conditionExpr;
  585. wdBreakpoint->mCondition = condition;
  586. }
  587. }
  588. void CeDebugger::SetBreakpointLogging(Breakpoint* breakpoint, const StringImpl& logging, bool breakAfterLogging)
  589. {
  590. AutoCrit autoCrit(mDebugManager->mCritSect);
  591. CeBreakpoint* wdBreakpoint = (CeBreakpoint*)breakpoint;
  592. BF_ASSERT(!wdBreakpoint->mIsLinkedSibling);
  593. wdBreakpoint->mLogging = logging;
  594. wdBreakpoint->mBreakAfterLogging = breakAfterLogging;
  595. }
  596. Breakpoint* CeDebugger::FindBreakpointAt(intptr address)
  597. {
  598. return nullptr;
  599. }
  600. Breakpoint* CeDebugger::GetActiveBreakpoint()
  601. {
  602. return mActiveBreakpoint;
  603. }
  604. void CeDebugger::BreakAll()
  605. {
  606. mCeMachine->mSpecialCheck = true;
  607. mCeMachine->mDbgWantBreak = true;
  608. }
  609. bool CeDebugger::TryRunContinue()
  610. {
  611. return false;
  612. }
  613. bool CeDebugger::SetupStep(int frameIdx)
  614. {
  615. auto ceFrame = GetFrame(frameIdx);
  616. if (ceFrame == NULL)
  617. {
  618. ContinueDebugEvent();
  619. return false;
  620. }
  621. int entryIdx = 0;
  622. auto curEntry = ceFrame->mFunction->FindEmitEntry(ceFrame->GetInstIdx(), &entryIdx);
  623. if (curEntry == NULL)
  624. {
  625. ContinueDebugEvent();
  626. return false;
  627. }
  628. auto ceMachine = mCeMachine;
  629. auto ceContext = mCeMachine->mCurContext;
  630. if (entryIdx < ceFrame->mFunction->mEmitTable.mSize - 1)
  631. {
  632. int checkIdx = entryIdx + 1;
  633. while (checkIdx < ceFrame->mFunction->mEmitTable.mSize)
  634. {
  635. auto checkEntry = &ceFrame->mFunction->mEmitTable[checkIdx];
  636. ceMachine->mStepState.mNextInstIdx = checkEntry->mCodePos;
  637. if ((checkEntry->mScope != curEntry->mScope) || (checkEntry->mLine != curEntry->mLine))
  638. break;
  639. ++checkIdx;
  640. }
  641. }
  642. else
  643. ceMachine->mStepState.mNextInstIdx = ceFrame->mFunction->mCode.mSize;
  644. ceMachine->mStepState.mStartDepth = ceContext->mCallStack.mSize - frameIdx;
  645. ceMachine->mSpecialCheck = true;
  646. ContinueDebugEvent();
  647. return true;
  648. }
  649. void CeDebugger::StepInto(bool inAssembly)
  650. {
  651. if (!SetupStep())
  652. return;
  653. mCeMachine->mStepState.mKind = inAssembly ? CeStepState::Kind_StepInfo_Asm : CeStepState::Kind_StepInfo;
  654. }
  655. void CeDebugger::StepIntoSpecific(intptr addr)
  656. {
  657. }
  658. void CeDebugger::StepOver(bool inAssembly)
  659. {
  660. if (!SetupStep())
  661. return;
  662. mCeMachine->mStepState.mKind = inAssembly ? CeStepState::Kind_StepOver_Asm : CeStepState::Kind_StepOver;
  663. }
  664. void CeDebugger::StepOut(bool inAssembly)
  665. {
  666. if (!SetupStep(1))
  667. return;
  668. mCeMachine->mStepState.mKind = inAssembly ? CeStepState::Kind_StepOut_Asm : CeStepState::Kind_StepOut;
  669. }
  670. void CeDebugger::SetNextStatement(bool inAssembly, const StringImpl& fileName, int64 lineNumOrAsmAddr, int wantColumn)
  671. {
  672. auto ceFrame = GetFrame(0);
  673. if (ceFrame == NULL)
  674. return;
  675. if (inAssembly)
  676. {
  677. int32 instIdx = (int32)lineNumOrAsmAddr;
  678. if (instIdx < ceFrame->mFunction->mCode.mSize)
  679. {
  680. mCeMachine->mSpecialCheck = true;
  681. mCeMachine->mStepState.mKind = CeStepState::Kind_Jmp;
  682. mCeMachine->mStepState.mNextInstIdx = instIdx;
  683. ContinueDebugEvent();
  684. }
  685. }
  686. else
  687. {
  688. for (auto& emitEntry : ceFrame->mFunction->mEmitTable)
  689. {
  690. if (emitEntry.mScope == -1)
  691. continue;
  692. auto& scope = ceFrame->mFunction->mDbgScopes[emitEntry.mScope];
  693. if ((FileNameEquals(fileName, scope.mFilePath) && (emitEntry.mLine == lineNumOrAsmAddr)))
  694. {
  695. mCeMachine->mSpecialCheck = true;
  696. mCeMachine->mStepState.mKind = CeStepState::Kind_Jmp;
  697. mCeMachine->mStepState.mNextInstIdx = emitEntry.mCodePos;
  698. ContinueDebugEvent();
  699. return;
  700. }
  701. }
  702. }
  703. }
  704. CeFrame* CeDebugger::GetFrame(int callStackIdx)
  705. {
  706. auto ceContext = mCeMachine->mCurContext;
  707. if (ceContext == NULL)
  708. return NULL;
  709. if ((callStackIdx == -1) && (!ceContext->mCallStack.IsEmpty()))
  710. return &ceContext->mCallStack.back();
  711. if (callStackIdx < mDbgCallStack.mSize)
  712. {
  713. int frameIdx = mDbgCallStack[callStackIdx].mFrameIdx;
  714. if (frameIdx < 0)
  715. return NULL;
  716. auto ceFrame = &ceContext->mCallStack[mDbgCallStack[callStackIdx].mFrameIdx];
  717. return ceFrame;
  718. }
  719. return NULL;
  720. }
  721. String CeDebugger::GetAutocompleteOutput(BfAutoComplete& autoComplete)
  722. {
  723. String val = "\n:autocomplete\n";
  724. if (autoComplete.mInsertStartIdx != -1)
  725. {
  726. val += StrFormat("insertRange\t%d %d\n", autoComplete.mInsertStartIdx, autoComplete.mInsertEndIdx);
  727. }
  728. Array<AutoCompleteEntry*> entries;
  729. for (auto& entry : autoComplete.mEntriesSet)
  730. {
  731. entries.Add(&entry);
  732. }
  733. std::sort(entries.begin(), entries.end(), [](AutoCompleteEntry* lhs, AutoCompleteEntry* rhs)
  734. {
  735. return stricmp(lhs->mDisplay, rhs->mDisplay) < 0;
  736. });
  737. for (auto entry : entries)
  738. {
  739. val += String(entry->mEntryType);
  740. val += "\t";
  741. val += String(entry->mDisplay);
  742. val += "\n";
  743. }
  744. /*if (autoComplete.mEntries.size() != 0)
  745. {
  746. for (auto& entry : autoComplete.mEntries)
  747. {
  748. val += String(entry.mEntryType) + "\t" + String(entry.mDisplay) + "\n";
  749. }
  750. }*/
  751. return val;
  752. }
  753. String CeDebugger::DoEvaluate(CePendingExpr* pendingExpr, bool inCompilerThread)
  754. {
  755. auto ceFrame = GetFrame(pendingExpr->mCallStackIdx);
  756. if (ceFrame == NULL)
  757. {
  758. return "!No comptime stack frame selected";
  759. }
  760. if (pendingExpr->mExprNode == NULL)
  761. {
  762. return "!No expression";
  763. }
  764. auto module = mCeMachine->mCeModule;
  765. if (!mCeMachine->mDbgPaused)
  766. pendingExpr->mPassInstance->ClearErrors();
  767. SetAndRestoreValue<BfTypeState*> prevTypeState(module->mContext->mCurTypeState, NULL);
  768. SetAndRestoreValue<BfConstraintState*> prevConstraintState(module->mContext->mCurConstraintState, NULL);
  769. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(module->mCurTypeInstance, ceFrame->mFunction->mMethodInstance->GetOwner());
  770. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(module->mCurMethodInstance, ceFrame->mFunction->mMethodInstance);
  771. SetAndRestoreValue<BfPassInstance*> prevPassInstance(mCompiler->mPassInstance, pendingExpr->mPassInstance);
  772. SetAndRestoreValue<bool> prevIgnoreWrites(module->mBfIRBuilder->mIgnoreWrites, true);
  773. BfMethodState methodState;
  774. SetAndRestoreValue<BfMethodState*> prevMethodState(module->mCurMethodState, &methodState);
  775. methodState.mTempKind = module->mCurMethodInstance->mMethodDef->mIsStatic ? BfMethodState::TempKind_Static : BfMethodState::TempKind_NonStatic;
  776. CeDbgState dbgState;
  777. dbgState.mActiveFrame = ceFrame;
  778. dbgState.mCeContext = mCeMachine->mCurContext;
  779. dbgState.mExplicitThis = pendingExpr->mFormatInfo.mExplicitThis;
  780. dbgState.mDbgExpressionFlags = pendingExpr->mExpressionFlags;
  781. if (!inCompilerThread)
  782. dbgState.mDbgExpressionFlags = (DwEvalExpressionFlags)(dbgState.mDbgExpressionFlags & ~(DwEvalExpressionFlag_AllowCalls | DwEvalExpressionFlag_AllowPropertyEval));
  783. dbgState.mFormatInfo = &pendingExpr->mFormatInfo;
  784. SetAndRestoreValue<CeDbgState*> prevDbgState(mCurDbgState, &dbgState);
  785. BfAutoComplete autoComplete;
  786. autoComplete.mModule = module;
  787. autoComplete.mCompiler = module->mCompiler;
  788. autoComplete.mSystem = module->mSystem;
  789. BfResolvePassData resolvePass;
  790. resolvePass.mParsers.Add(pendingExpr->mParser);
  791. resolvePass.mAutoComplete = &autoComplete;
  792. SetAndRestoreValue<BfResolvePassData*> prevResolvePass;
  793. if (pendingExpr->mCursorPos != -1)
  794. {
  795. pendingExpr->mParser->mParserFlags = (BfParserFlag)(pendingExpr->mParser->mParserFlags | ParserFlag_Autocomplete);
  796. pendingExpr->mParser->mCursorIdx = pendingExpr->mCursorPos + 1;
  797. pendingExpr->mParser->mCursorCheckIdx = pendingExpr->mCursorPos + 1;
  798. prevResolvePass.Init(module->mCompiler->mResolvePassData, &resolvePass);
  799. }
  800. BfTypedValue exprResult;
  801. BfTypedValue origExprResult;
  802. if (auto typeRef = BfNodeDynCast<BfTypeReference>(pendingExpr->mExprNode))
  803. {
  804. auto resultType = mCeMachine->mCeModule->ResolveTypeRef(typeRef);
  805. if (resultType != NULL)
  806. exprResult = BfTypedValue(resultType);
  807. }
  808. else
  809. {
  810. BfExprEvaluator exprEvaluator(mCeMachine->mCeModule);
  811. exprEvaluator.mBfEvalExprFlags = (BfEvalExprFlags)(exprEvaluator.mBfEvalExprFlags | BfEvalExprFlags_Comptime);
  812. exprEvaluator.VisitChildNoRef(pendingExpr->mExprNode);
  813. exprResult = exprEvaluator.GetResult();
  814. origExprResult = exprResult;
  815. if ((exprResult) && (!exprResult.mType->IsComposite()))
  816. exprResult = module->LoadValue(exprResult);
  817. module->FixIntUnknown(exprResult);
  818. }
  819. if (dbgState.mBlockedSideEffects)
  820. {
  821. if ((mCeMachine->mDbgPaused) && ((pendingExpr->mExpressionFlags & DwEvalExpressionFlag_AllowCalls) != 0))
  822. {
  823. // Reprocess in compiler thread
  824. return "!pending";
  825. }
  826. return "!sideeffects";
  827. }
  828. if (!exprResult)
  829. {
  830. auto resultType = mCeMachine->mCeModule->ResolveTypeRef(pendingExpr->mExprNode, {}, BfPopulateType_Data, BfResolveTypeRefFlag_IgnoreLookupError);
  831. if (resultType != NULL)
  832. {
  833. exprResult = BfTypedValue(resultType);
  834. pendingExpr->mPassInstance->ClearErrors();
  835. }
  836. }
  837. if ((exprResult.mType == NULL) && (dbgState.mReferencedIncompleteTypes))
  838. {
  839. if ((mCeMachine->mDbgPaused) && ((pendingExpr->mExpressionFlags & DwEvalExpressionFlag_AllowCalls) != 0))
  840. {
  841. // Reprocess in compiler thread
  842. return "!pending";
  843. }
  844. return "!incomplete";
  845. }
  846. String val;
  847. if (pendingExpr->mPassInstance->HasFailed())
  848. {
  849. BfLogDbgExpr("Evaluate Failed: %s\n", pendingExpr->mPassInstance->mErrors[0]->mError.c_str());
  850. String errorStr = pendingExpr->mPassInstance->mErrors[0]->mError;
  851. for (auto moreInfo : pendingExpr->mPassInstance->mErrors[0]->mMoreInfo)
  852. {
  853. errorStr += " ";
  854. errorStr += moreInfo->mInfo;
  855. }
  856. errorStr.Replace('\n', ' ');
  857. val = StrFormat("!%d\t%d\t%s", pendingExpr->mPassInstance->mErrors[0]->GetSrcStart(), pendingExpr->mPassInstance->mErrors[0]->GetSrcLength(), errorStr.c_str());
  858. }
  859. else if ((!exprResult) && (!exprResult.IsNoValueType()))
  860. {
  861. return "!Debug evaluate failed";
  862. }
  863. else
  864. {
  865. CeTypeModKind typeModKind = CeTypeModKind_Normal;
  866. if ((origExprResult) && (!origExprResult.IsAddr()) && (origExprResult.mType->IsValueType()))
  867. typeModKind = CeTypeModKind_Const;
  868. else if (origExprResult.IsReadOnly())
  869. typeModKind = CeTypeModKind_ReadOnly;
  870. val = TypedValueToString(exprResult, pendingExpr->mExprNode->ToString(), pendingExpr->mFormatInfo, (pendingExpr->mExpressionFlags & DwEvalExpressionFlag_FullPrecision) != 0, typeModKind);
  871. if ((!val.empty()) && (val[0] == '!'))
  872. return val;
  873. if (pendingExpr->mFormatInfo.mRawString)
  874. return val;
  875. if (pendingExpr->mPassInstance->HasMessages())
  876. {
  877. for (auto error : pendingExpr->mPassInstance->mErrors)
  878. {
  879. if (error->mIsWarning)
  880. {
  881. val += "\n:warn\t";
  882. val += error->mError;
  883. }
  884. }
  885. }
  886. if (!pendingExpr->mFormatInfo.mReferenceId.empty())
  887. val += "\n:referenceId\t" + pendingExpr->mFormatInfo.mReferenceId;
  888. // if ((exprResult.mSrcAddress != 0) && (HasMemoryBreakpoint(exprResult.mSrcAddress, exprResult.mType->GetByteCount())))
  889. // val += StrFormat("\n:break\t%@", exprResult.mSrcAddress);
  890. auto checkType = exprResult.mType;
  891. if (checkType->IsObject())
  892. val += "\n:type\tobject";
  893. else if (checkType->IsPointer())
  894. val += "\n:type\tpointer";
  895. else if (checkType->IsInteger())
  896. val += "\n:type\tint";
  897. else if (checkType->IsFloat())
  898. val += "\n:type\tfloat";
  899. }
  900. if (dbgState.mHadSideEffects)
  901. val += "\n:sideeffects";
  902. // auto resultConstant = module->mBfIRBuilder->GetConstant(exprResult.mValue);
  903. // if (resultConstant != NULL)
  904. // {
  905. // auto ceResultTyped = GetAddr(resultConstant);
  906. // if (ceResultTyped.mAddr != 0)
  907. // val += "\n:canEdit";
  908. // }
  909. if ((origExprResult.mType != NULL) && (!origExprResult.mType->IsComposite()) && (!origExprResult.mType->IsRef()) &&
  910. (origExprResult.IsAddr()) && (!origExprResult.IsReadOnly()))
  911. {
  912. val += "\n:canEdit";
  913. }
  914. if (pendingExpr->mCursorPos != -1)
  915. val += GetAutocompleteOutput(autoComplete);
  916. return val;
  917. }
  918. String CeDebugger::Evaluate(const StringImpl& expr, CeFormatInfo formatInfo, int callStackIdx, int cursorPos, int language, DwEvalExpressionFlags expressionFlags)
  919. {
  920. BP_ZONE_F("WinDebugger::Evaluate %s", BP_DYN_STR(expr.c_str()));
  921. AutoCrit autoCrit(mCeMachine->mCritSect);
  922. if ((expressionFlags & DwEvalExpressionFlag_RawStr) != 0)
  923. {
  924. formatInfo.mRawString = true;
  925. }
  926. auto ceContext = mCeMachine->mCurContext;
  927. bool valIsAddr = false;
  928. BfParser* parser = new BfParser(mCompiler->mSystem);
  929. BfPassInstance* bfPassInstance = new BfPassInstance(mCompiler->mSystem);
  930. auto parseAsType = false;
  931. auto terminatedExpr = expr;
  932. terminatedExpr.Trim();
  933. if (terminatedExpr.StartsWith("!"))
  934. {
  935. if (terminatedExpr == "!info")
  936. {
  937. auto ceFrame = GetFrame(callStackIdx);
  938. if (ceFrame != NULL)
  939. {
  940. if (ceFrame->mFunction->mCeFunctionInfo->mMethodInstance != NULL)
  941. OutputMessage(StrFormat("Module: %s\n", ceFrame->mFunction->mCeFunctionInfo->mMethodInstance->GetOwner()->mModule->mModuleName.c_str()));
  942. OutputMessage(StrFormat("Link Name: %s\n", ceFrame->mFunction->mCeFunctionInfo->mName.c_str()));
  943. }
  944. return "";
  945. }
  946. }
  947. if (terminatedExpr.EndsWith(">"))
  948. parseAsType = true;
  949. else if ((terminatedExpr.StartsWith("comptype(")) && (!terminatedExpr.Contains('.')))
  950. parseAsType = true;
  951. if (!parseAsType)
  952. terminatedExpr += ";";
  953. if ((terminatedExpr.length() > 2) && (terminatedExpr[0] == '@'))
  954. {
  955. if (terminatedExpr[1] == '!') // Return string as error
  956. {
  957. int errorEnd = (int)terminatedExpr.IndexOf("@!", 2);
  958. if (errorEnd != -1)
  959. return terminatedExpr.Substring(1, errorEnd - 1);
  960. else
  961. return terminatedExpr.Substring(1);
  962. }
  963. else if (terminatedExpr[1] == '>') // Return string as text
  964. {
  965. int errorEnd = (int)terminatedExpr.IndexOf("@>", 2);
  966. if (errorEnd != -1)
  967. return terminatedExpr.Substring(2, errorEnd - 1);
  968. else
  969. return terminatedExpr.Substring(2);
  970. }
  971. }
  972. parser->SetSource(terminatedExpr.c_str(), (int)terminatedExpr.length());
  973. parser->Parse(bfPassInstance);
  974. BfReducer bfReducer;
  975. bfReducer.mAlloc = parser->mAlloc;
  976. bfReducer.mSystem = mCompiler->mSystem;
  977. bfReducer.mPassInstance = bfPassInstance;
  978. bfReducer.mVisitorPos = BfReducer::BfVisitorPos(parser->mRootNode);
  979. bfReducer.mVisitorPos.MoveNext();
  980. bfReducer.mSource = parser;
  981. BfAstNode* exprNode = NULL;
  982. if (parseAsType)
  983. exprNode = bfReducer.CreateTypeRef(parser->mRootNode->mChildArr.GetAs<BfAstNode*>(0));
  984. else
  985. exprNode = bfReducer.CreateExpression(parser->mRootNode->mChildArr.GetAs<BfAstNode*>(0));
  986. parser->Close();
  987. formatInfo.mCallStackIdx = callStackIdx;
  988. CePendingExpr* pendingExpr = new CePendingExpr();
  989. pendingExpr->mPassInstance = bfPassInstance;
  990. pendingExpr->mParser = parser;
  991. pendingExpr->mCallStackIdx = callStackIdx;
  992. pendingExpr->mCursorPos = cursorPos;
  993. pendingExpr->mExpressionFlags = expressionFlags;
  994. pendingExpr->mExprNode = exprNode;
  995. BfType* explicitType = NULL;
  996. String formatFlags;
  997. String assignExpr;
  998. int assignExprOffset = -1;
  999. if ((exprNode != NULL) && (exprNode->GetSrcEnd() < (int)expr.length()))
  1000. {
  1001. int formatOffset = exprNode->GetSrcEnd();
  1002. while (formatOffset < (int)expr.length())
  1003. {
  1004. char c = expr[formatOffset];
  1005. if (c == ' ')
  1006. formatOffset++;
  1007. else
  1008. break;
  1009. }
  1010. formatFlags = Trim(expr.Substring(formatOffset));
  1011. bool isComplexType = false;
  1012. for (char c : formatFlags)
  1013. if (c == '>')
  1014. isComplexType = true;
  1015. if (isComplexType)
  1016. {
  1017. //explicitType = dbgModule->FindType(expr);
  1018. }
  1019. if ((explicitType == NULL) && (formatFlags.length() > 0))
  1020. {
  1021. String errorString = "Invalid expression";
  1022. if (!ParseFormatInfo(formatFlags, &formatInfo, bfPassInstance, &assignExprOffset, &assignExpr, &errorString))
  1023. {
  1024. if (formatInfo.mRawString)
  1025. return "";
  1026. bfPassInstance->FailAt(errorString, parser->mSourceData, exprNode->GetSrcEnd(), (int)expr.length() - exprNode->GetSrcEnd());
  1027. formatFlags = "";
  1028. }
  1029. if (assignExprOffset != -1)
  1030. assignExprOffset += formatOffset;
  1031. }
  1032. }
  1033. if (assignExpr.length() > 0)
  1034. {
  1035. String newEvalStr = exprNode->ToString() + " = ";
  1036. int errorOffset = (int)newEvalStr.length();
  1037. newEvalStr += assignExpr;
  1038. String result = Evaluate(newEvalStr, formatInfo, callStackIdx, cursorPos, language, expressionFlags);
  1039. if (result[0] == '!')
  1040. {
  1041. int tabPos = (int)result.IndexOf('\t');
  1042. if (tabPos > 0)
  1043. {
  1044. int errorStart = atoi(result.Substring(1, tabPos - 1).c_str());
  1045. if (errorStart >= errorOffset)
  1046. {
  1047. result = StrFormat("!%d", errorStart - errorOffset + assignExprOffset) + result.Substring(tabPos);
  1048. }
  1049. }
  1050. }
  1051. return result;
  1052. }
  1053. pendingExpr->mExplitType = explicitType;
  1054. pendingExpr->mFormatInfo = formatInfo;
  1055. String result = DoEvaluate(pendingExpr, false);
  1056. if (result == "!pending")
  1057. {
  1058. BF_ASSERT(mDebugPendingExpr == NULL);
  1059. if (mDebugPendingExpr != NULL)
  1060. {
  1061. return "!retry"; // We already have a pending
  1062. }
  1063. mDebugPendingExpr = pendingExpr;
  1064. mCeMachine->mStepState.mKind = CeStepState::Kind_Evaluate;
  1065. mRunState = RunState_DebugEval;
  1066. ContinueDebugEvent();
  1067. }
  1068. else
  1069. delete pendingExpr;
  1070. return result;
  1071. }
  1072. void CeDebugger::ClearBreakpointCache()
  1073. {
  1074. if (!mCeMachine->mDbgPaused)
  1075. mCeMachine->mSpecialCheck = true;
  1076. mBreakpointFramesDirty = true;
  1077. mBreakpointCacheDirty = true;
  1078. for (auto kv : mFileInfo)
  1079. delete kv.mValue;
  1080. mFileInfo.Clear();
  1081. }
  1082. void CeDebugger::UpdateBreakpointAddrs()
  1083. {
  1084. for (auto breakpoint : mBreakpoints)
  1085. {
  1086. breakpoint->mCurBindAddr = 1;
  1087. }
  1088. CeFunction* ceFunction = NULL;
  1089. if (!mCeMachine->mFunctionIdMap.TryGetValue(mCurDisasmFuncId, &ceFunction))
  1090. return;
  1091. if (ceFunction->mBreakpointVersion != mBreakpointVersion)
  1092. UpdateBreakpoints(ceFunction);
  1093. for (auto kv : ceFunction->mBreakpoints)
  1094. kv.mValue.mBreakpoint->mCurBindAddr = ((intptr)ceFunction->mId << 32) | kv.mKey;
  1095. }
  1096. void CeDebugger::UpdateBreakpointCache()
  1097. {
  1098. AutoCrit autoCrit(mCeMachine->mCritSect);
  1099. mBreakpointCacheDirty = false;
  1100. if (!mFileInfo.IsEmpty())
  1101. return;
  1102. for (int i = 0; i < (int)mBreakpoints.mSize; i++)
  1103. {
  1104. auto breakpoint = mBreakpoints[i];
  1105. breakpoint->mIdx = i;
  1106. String fileName = breakpoint->mFilePath;
  1107. fileName = FixPathAndCase(fileName);
  1108. CeFileInfo** valuePtr;
  1109. CeFileInfo* fileInfo = NULL;
  1110. if (mFileInfo.TryAdd(fileName, NULL, &valuePtr))
  1111. {
  1112. fileInfo = new CeFileInfo();
  1113. *valuePtr = fileInfo;
  1114. }
  1115. else
  1116. fileInfo = *valuePtr;
  1117. fileInfo->mOrderedBreakpoints.Add(breakpoint);
  1118. }
  1119. for (auto kv : mFileInfo)
  1120. {
  1121. kv.mValue->mOrderedBreakpoints.Sort([](CeBreakpoint* lhs, CeBreakpoint* rhs)
  1122. {
  1123. return lhs->mLineNum < rhs->mLineNum;
  1124. });
  1125. }
  1126. }
  1127. static int CompareBreakpoint(CeBreakpoint* breakpoint, const int& lineNum)
  1128. {
  1129. return breakpoint->mRequestedLineNum - lineNum;
  1130. }
  1131. void CeDebugger::UpdateBreakpoints(CeFunction* ceFunction)
  1132. {
  1133. AutoCrit autoCrit(mCeMachine->mCritSect);
  1134. UpdateBreakpointCache();
  1135. ceFunction->UnbindBreakpoints();
  1136. String path;
  1137. int scope = -1;
  1138. CeFileInfo* ceFileInfo = NULL;
  1139. BitSet usedBreakpointSet(mBreakpoints.mSize);
  1140. for (auto& emitEntry : ceFunction->mEmitTable)
  1141. {
  1142. if (emitEntry.mScope != scope)
  1143. {
  1144. if (emitEntry.mScope != -1)
  1145. {
  1146. path = FixPathAndCase(ceFunction->mDbgScopes[emitEntry.mScope].mFilePath);
  1147. if (!mFileInfo.TryGetValue(path, &ceFileInfo))
  1148. ceFileInfo = NULL;
  1149. }
  1150. else
  1151. ceFileInfo = NULL;
  1152. scope = emitEntry.mScope;
  1153. }
  1154. if (ceFileInfo != NULL)
  1155. {
  1156. int idx = ceFileInfo->mOrderedBreakpoints.BinarySearchAlt<int>(emitEntry.mLine, CompareBreakpoint);
  1157. if (idx < 0)
  1158. idx = ~idx - 1;
  1159. while (idx > 0)
  1160. {
  1161. auto breakpoint = ceFileInfo->mOrderedBreakpoints[idx - 1];
  1162. if (breakpoint->mLineNum < emitEntry.mLine)
  1163. break;
  1164. idx--;
  1165. }
  1166. int tryBindCount = 0;
  1167. int bestRequestedBindLine = 0;
  1168. while ((idx >= 0) && (idx < ceFileInfo->mOrderedBreakpoints.mSize))
  1169. {
  1170. auto breakpoint = ceFileInfo->mOrderedBreakpoints[idx];
  1171. if (usedBreakpointSet.IsSet(breakpoint->mIdx))
  1172. {
  1173. idx++;
  1174. continue;
  1175. }
  1176. CeBreakpointBind* breakpointBind = NULL;
  1177. if (tryBindCount > 0)
  1178. {
  1179. if (breakpoint->mRequestedLineNum > bestRequestedBindLine)
  1180. break;
  1181. }
  1182. else
  1183. {
  1184. int lineDiff = emitEntry.mLine - breakpoint->mLineNum;
  1185. if ((lineDiff < 0) || (lineDiff > 4))
  1186. break;
  1187. if ((breakpoint->mHasBound) && (lineDiff != 0))
  1188. break;
  1189. bestRequestedBindLine = breakpoint->mRequestedLineNum;
  1190. }
  1191. tryBindCount++;
  1192. int codePos = emitEntry.mCodePos;
  1193. if (breakpoint->mInstrOffset > 0)
  1194. {
  1195. int instrOffsetLeft = breakpoint->mInstrOffset;
  1196. while (instrOffsetLeft > 0)
  1197. {
  1198. auto& opRef = *(CeOp*)(&ceFunction->mCode[codePos]);
  1199. int instSize = mCeMachine->GetInstSize(ceFunction, codePos);
  1200. codePos += instSize;
  1201. instrOffsetLeft--;
  1202. }
  1203. }
  1204. if (ceFunction->mBreakpoints.TryAdd(codePos, NULL, &breakpointBind))
  1205. {
  1206. usedBreakpointSet.Set(breakpoint->mIdx);
  1207. breakpoint->mLineNum = emitEntry.mLine;
  1208. breakpoint->mHasBound = true;
  1209. auto& opRef = *(CeOp*)(&ceFunction->mCode[codePos]);
  1210. breakpointBind->mPrevOpCode = opRef;
  1211. breakpointBind->mBreakpoint = breakpoint;
  1212. opRef = CeOp_DbgBreak;
  1213. }
  1214. idx++;
  1215. }
  1216. }
  1217. }
  1218. ceFunction->mBreakpointVersion = mBreakpointVersion;
  1219. }
  1220. CeDbgTypeInfo* CeDebugger::GetDbgTypeInfo(int typeId)
  1221. {
  1222. CeDbgTypeInfo* dbgTypeInfo = NULL;
  1223. if (mDbgTypeInfoMap.TryAdd(typeId, NULL, &dbgTypeInfo))
  1224. {
  1225. auto type = mCeMachine->mCeModule->mContext->FindTypeById(typeId);
  1226. if (type == NULL)
  1227. {
  1228. mDbgTypeInfoMap.Remove(typeId);
  1229. return NULL;
  1230. }
  1231. dbgTypeInfo->mType = type;
  1232. auto typeInst = type->ToTypeInstance();
  1233. if (typeInst != NULL)
  1234. {
  1235. for (int fieldIdx = 0; fieldIdx < typeInst->mFieldInstances.mSize; fieldIdx++)
  1236. {
  1237. auto& fieldInst = typeInst->mFieldInstances[fieldIdx];
  1238. if (fieldInst.mDataIdx >= 0)
  1239. {
  1240. while (fieldInst.mDataIdx >= dbgTypeInfo->mFieldOffsets.mSize)
  1241. dbgTypeInfo->mFieldOffsets.Add(CeDbgFieldEntry());
  1242. dbgTypeInfo->mFieldOffsets[fieldInst.mDataIdx].mType = fieldInst.mResolvedType;
  1243. dbgTypeInfo->mFieldOffsets[fieldInst.mDataIdx].mDataOffset = fieldInst.mDataOffset;
  1244. }
  1245. if (fieldInst.mConstIdx != -1)
  1246. {
  1247. auto constant = typeInst->mConstHolder->GetConstantById(fieldInst.mConstIdx);
  1248. if ((constant != NULL) && (BfIRConstHolder::IsInt(constant->mTypeCode)))
  1249. {
  1250. CeDbgTypeInfo::ConstIntEntry constIntEntry;
  1251. constIntEntry.mFieldIdx = fieldIdx;
  1252. constIntEntry.mVal = constant->mInt64;
  1253. dbgTypeInfo->mConstIntEntries.Add(constIntEntry);
  1254. }
  1255. }
  1256. }
  1257. }
  1258. }
  1259. return dbgTypeInfo;
  1260. }
  1261. CeDbgTypeInfo* CeDebugger::GetDbgTypeInfo(BfIRType irType)
  1262. {
  1263. if ((irType.mKind == BfIRTypeData::TypeKind_TypeId) || (irType.mKind == BfIRTypeData::TypeKind_TypeInstId))
  1264. return GetDbgTypeInfo(irType.mId);
  1265. if (irType.mKind == BfIRTypeData::TypeKind_TypeInstPtrId)
  1266. {
  1267. auto type = mCeMachine->mCeModule->mContext->FindTypeById(irType.mId);
  1268. if (type->IsObjectOrInterface())
  1269. return GetDbgTypeInfo(irType.mId);
  1270. else
  1271. return GetDbgTypeInfo(mCeMachine->mCeModule->CreatePointerType(type)->mTypeId);
  1272. }
  1273. return NULL;
  1274. }
  1275. static bool IsNormalChar(uint32 c)
  1276. {
  1277. return (c < 0x80);
  1278. }
  1279. template <typename T>
  1280. static String IntTypeToString(T val, const StringImpl& name, DwDisplayInfo* displayInfo, CeFormatInfo& formatInfo)
  1281. {
  1282. auto intDisplayType = displayInfo->mIntDisplayType;
  1283. if (formatInfo.mDisplayType == DwDisplayType_Decimal)
  1284. intDisplayType = DwIntDisplayType_Decimal;
  1285. else if (formatInfo.mDisplayType == DwDisplayType_HexUpper)
  1286. intDisplayType = DwIntDisplayType_HexadecimalUpper;
  1287. else if (formatInfo.mDisplayType == DwDisplayType_HexLower)
  1288. intDisplayType = DwIntDisplayType_HexadecimalLower;
  1289. if (intDisplayType == DwIntDisplayType_Binary)
  1290. {
  1291. String binary;
  1292. for (int i = 0; i < sizeof(T) * 8; i++)
  1293. {
  1294. if ((i != 0) && (i % 4 == 0))
  1295. binary = "'" + binary;
  1296. if ((i != 0) && (i % 16 == 0))
  1297. binary = "'" + binary;
  1298. binary = ((val & ((T)1 << i)) ? "1" : "0") + binary;
  1299. }
  1300. return StrFormat("0b'%s\n%s", binary.c_str(), name.c_str());
  1301. }
  1302. if (intDisplayType == DwIntDisplayType_Octal)
  1303. {
  1304. String format;
  1305. if (sizeof(T) == 8)
  1306. {
  1307. format = StrFormat("0o%%lo\n%s", name.c_str());
  1308. }
  1309. else
  1310. format = StrFormat("0o%%0%do\n%s", sizeof(val) * 2, name.c_str());
  1311. return StrFormat(format.c_str(), (typename std::make_unsigned<T>::type)(val));
  1312. }
  1313. if (intDisplayType == DwIntDisplayType_HexadecimalUpper)
  1314. {
  1315. String format;
  1316. if (sizeof(T) == 8)
  1317. {
  1318. format = StrFormat("0x%%l@\n%s", name.c_str());
  1319. }
  1320. else
  1321. format = StrFormat("0x%%0%dX\n%s", sizeof(val) * 2, name.c_str());
  1322. return StrFormat(format.c_str(), (typename std::make_unsigned<T>::type)(val));
  1323. }
  1324. //TODO: Implement HexadecimalLower
  1325. if (intDisplayType == DwIntDisplayType_HexadecimalLower)
  1326. {
  1327. String format;
  1328. if (sizeof(T) == 8)
  1329. {
  1330. format = StrFormat("0x%%l@\n%s", name.c_str());
  1331. }
  1332. else
  1333. format = StrFormat("0x%%0%dX\n%s", sizeof(val) * 2, name.c_str());
  1334. return StrFormat(format.c_str(), (typename std::make_unsigned<T>::type)(val));
  1335. }
  1336. if (std::is_unsigned<T>::value)
  1337. {
  1338. if (sizeof(T) == 8)
  1339. {
  1340. if (val > 0x7FFFFFFFF)
  1341. return StrFormat("%llu\n%s\n:editVal\t%lluUL", val, name.c_str(), val);
  1342. else
  1343. return StrFormat("%llu\n%s", val, name.c_str());
  1344. }
  1345. else
  1346. return StrFormat("%u\n%s", val, name.c_str());
  1347. }
  1348. else
  1349. {
  1350. if (sizeof(T) == 8)
  1351. {
  1352. if ((val > 0x7FFFFFFFF) || (val < -0x80000000LL))
  1353. return StrFormat("%lld\n%s\n:editVal\t%lldL", val, name.c_str(), val);
  1354. else
  1355. return StrFormat("%lld\n%s", val, name.c_str(), val);
  1356. }
  1357. else
  1358. return StrFormat("%d\n%s", val, name.c_str());
  1359. }
  1360. }
  1361. DwDisplayInfo* CeDebugger::GetDisplayInfo(const StringImpl& referenceId)
  1362. {
  1363. DwDisplayInfo* displayInfo = &mDebugManager->mDefaultDisplayInfo;
  1364. if (!referenceId.empty())
  1365. {
  1366. mDebugManager->mDisplayInfos.TryGetValue(referenceId, &displayInfo);
  1367. }
  1368. return displayInfo;
  1369. }
  1370. String CeDebugger::GetMemberList(BfType* type, addr_ce addr, addr_ce addrInst, bool isStatic)
  1371. {
  1372. auto typeInst = type->ToTypeInstance();
  1373. if (typeInst == NULL)
  1374. return "";
  1375. auto module = typeInst->mModule;
  1376. String retVal;
  1377. int fieldCount = 0;
  1378. if ((!isStatic) && (typeInst->mBaseType != NULL) && (!typeInst->mBaseType->IsInstanceOf(mCompiler->mValueTypeTypeDef)))
  1379. {
  1380. retVal += StrFormat("[base]\tthis,this=%d@0x%X, nd, na, nv", typeInst->mBaseType->mTypeId, addr);
  1381. fieldCount++;
  1382. }
  1383. auto ceContext = mCompiler->mCeMachine->mCurContext;
  1384. bool didStaticCtor = ceContext->mStaticCtorExecSet.Contains(type->mTypeId);
  1385. bool hasStaticFields = false;
  1386. for (auto& fieldInst : typeInst->mFieldInstances)
  1387. {
  1388. auto fieldDef = fieldInst.GetFieldDef();
  1389. if (fieldDef == NULL)
  1390. continue;
  1391. if (fieldDef->mIsStatic != isStatic)
  1392. {
  1393. if (fieldDef->mIsStatic)
  1394. hasStaticFields = true;
  1395. continue;
  1396. }
  1397. if (fieldCount > 0)
  1398. retVal += "\n";
  1399. retVal += fieldDef->mName;
  1400. if (fieldDef->mIsStatic)
  1401. {
  1402. if (didStaticCtor)
  1403. retVal += StrFormat("\tcomptype(%d).%s", type->mTypeId, fieldDef->mName.c_str());
  1404. else
  1405. retVal += StrFormat("\tcomptype(%d).%s", type->mTypeId, fieldDef->mName.c_str());
  1406. }
  1407. else
  1408. {
  1409. retVal += "\t";
  1410. retVal += fieldDef->mName;
  1411. retVal += StrFormat(",this=%d@0x%X", typeInst->mTypeId, addrInst);
  1412. }
  1413. fieldCount++;
  1414. }
  1415. if (hasStaticFields)
  1416. {
  1417. if (fieldCount > 0)
  1418. retVal += "\n";
  1419. retVal += StrFormat("Static values\tcomptype(%d)", typeInst->mTypeId);
  1420. }
  1421. return retVal;
  1422. }
  1423. bool CeDebugger::ParseFormatInfo(const StringImpl& formatInfoStr, CeFormatInfo* formatInfo, BfPassInstance* bfPassInstance, int* assignExprOffset, String* assignExprString, String* errorString, BfTypedValue contextTypedValue)
  1424. {
  1425. String formatFlags = formatInfoStr;
  1426. if (assignExprOffset != NULL)
  1427. *assignExprOffset = -1;
  1428. while (formatFlags.length() > 0)
  1429. {
  1430. formatFlags = Trim(formatFlags);
  1431. if (formatFlags.IsEmpty())
  1432. break;
  1433. if (formatFlags[0] != ',')
  1434. {
  1435. return false;
  1436. }
  1437. else
  1438. {
  1439. int nextComma = (int)formatFlags.IndexOf(',', 1);
  1440. int quotePos = (int)formatFlags.IndexOf('"', 1);
  1441. if ((quotePos != -1) && (quotePos < nextComma))
  1442. {
  1443. int nextQuotePos = (int)formatFlags.IndexOf('"', quotePos + 1);
  1444. if (nextQuotePos != -1)
  1445. nextComma = (int)formatFlags.IndexOf(',', nextQuotePos + 1);
  1446. }
  1447. if (nextComma == -1)
  1448. nextComma = (int)formatFlags.length();
  1449. String formatCmd = formatFlags.Substring(1, nextComma - 1);
  1450. formatCmd = Trim(formatCmd);
  1451. bool hadError = false;
  1452. if (strncmp(formatCmd.c_str(), "this=", 5) == 0)
  1453. {
  1454. formatCmd = formatFlags.Substring(1);
  1455. formatCmd = Trim(formatCmd);
  1456. String thisExpr = formatCmd.Substring(5);
  1457. if (thisExpr.empty())
  1458. break;
  1459. CeEvaluationContext dbgEvaluationContext(this, thisExpr, formatInfo);
  1460. formatInfo->mExplicitThis = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1461. if (dbgEvaluationContext.HadError())
  1462. {
  1463. if (errorString != NULL)
  1464. *errorString = dbgEvaluationContext.GetErrorStr();
  1465. return false;
  1466. }
  1467. formatFlags = thisExpr.Substring(dbgEvaluationContext.mExprString.GetLength());
  1468. continue;
  1469. }
  1470. else if (strncmp(formatCmd.c_str(), "count=", 6) == 0)
  1471. {
  1472. formatCmd = formatFlags.Substring(1);
  1473. formatCmd = Trim(formatCmd);
  1474. String countExpr = formatCmd.Substring(6);
  1475. if (countExpr.empty())
  1476. break;
  1477. CeEvaluationContext dbgEvaluationContext(this, countExpr, formatInfo);
  1478. BfTypedValue countValue = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1479. if ((countValue) && (countValue.mType->IsInteger()))
  1480. formatInfo->mOverrideCount = (intptr)ValueToInt(countValue);
  1481. if (dbgEvaluationContext.HadError())
  1482. {
  1483. if (errorString != NULL)
  1484. *errorString = dbgEvaluationContext.GetErrorStr();
  1485. return false;
  1486. }
  1487. formatFlags = countExpr.Substring(dbgEvaluationContext.mExprString.GetLength());
  1488. continue;
  1489. }
  1490. else if (strncmp(formatCmd.c_str(), "maxcount=", 9) == 0)
  1491. {
  1492. formatCmd = formatFlags.Substring(1);
  1493. formatCmd = Trim(formatCmd);
  1494. String countExpr = formatCmd.Substring(9);
  1495. if (countExpr.empty())
  1496. break;
  1497. CeEvaluationContext dbgEvaluationContext(this, countExpr, formatInfo);
  1498. BfTypedValue countValue = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1499. if ((countValue) && (countValue.mType->IsInteger()))
  1500. formatInfo->mMaxCount = (intptr)ValueToInt(countValue);
  1501. if (dbgEvaluationContext.HadError())
  1502. {
  1503. if (errorString != NULL)
  1504. *errorString = dbgEvaluationContext.GetErrorStr();
  1505. return false;
  1506. }
  1507. formatFlags = countExpr.Substring(dbgEvaluationContext.mExprString.GetLength());
  1508. continue;
  1509. }
  1510. else if (strncmp(formatCmd.c_str(), "arraysize=", 10) == 0)
  1511. {
  1512. formatCmd = formatFlags.Substring(1);
  1513. formatCmd = Trim(formatCmd);
  1514. String countExpr = formatCmd.Substring(10);
  1515. if (countExpr.empty())
  1516. break;
  1517. CeEvaluationContext dbgEvaluationContext(this, countExpr, formatInfo);
  1518. BfTypedValue countValue = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1519. if ((countValue) && (countValue.mType->IsInteger()))
  1520. formatInfo->mArrayLength = (intptr)ValueToInt(countValue);
  1521. if (dbgEvaluationContext.HadError())
  1522. {
  1523. if (errorString != NULL)
  1524. *errorString = dbgEvaluationContext.GetErrorStr();
  1525. return false;
  1526. }
  1527. formatFlags = countExpr.Substring(dbgEvaluationContext.mExprString.GetLength());
  1528. continue;
  1529. }
  1530. else if (strncmp(formatCmd.c_str(), "assign=", 7) == 0)
  1531. {
  1532. formatCmd = formatFlags.Substring(1);
  1533. formatCmd = Trim(formatCmd);
  1534. String assignExpr = formatCmd.Substring(7);
  1535. if (assignExpr.empty())
  1536. break;
  1537. CeEvaluationContext dbgEvaluationContext(this, assignExpr, formatInfo);
  1538. if (dbgEvaluationContext.HadError())
  1539. {
  1540. if (errorString != NULL)
  1541. *errorString = dbgEvaluationContext.GetErrorStr();
  1542. return false;
  1543. }
  1544. if (assignExprOffset != NULL)
  1545. {
  1546. //TODO: Keep track of the offset directly, this is a hack
  1547. *assignExprOffset = (int)formatInfoStr.IndexOf("assign=") + 7;
  1548. }
  1549. if (assignExprString != NULL)
  1550. *assignExprString = dbgEvaluationContext.mExprNode->ToString();
  1551. formatFlags = assignExpr.Substring(dbgEvaluationContext.mExprNode->GetSrcEnd());
  1552. continue;
  1553. }
  1554. else if (strncmp(formatCmd.c_str(), "refid=", 6) == 0)
  1555. {
  1556. formatInfo->mReferenceId = formatCmd.Substring(6);
  1557. if (formatInfo->mReferenceId[0] == '\"')
  1558. formatInfo->mReferenceId = formatInfo->mReferenceId.Substring(1, formatInfo->mReferenceId.length() - 2);
  1559. }
  1560. else if (strncmp(formatCmd.c_str(), "_=", 2) == 0)
  1561. {
  1562. formatInfo->mSubjectExpr = formatCmd.Substring(2);
  1563. if (formatInfo->mSubjectExpr[0] == '\"')
  1564. formatInfo->mSubjectExpr = formatInfo->mSubjectExpr.Substring(1, formatInfo->mSubjectExpr.length() - 2);
  1565. }
  1566. else if (strncmp(formatCmd.c_str(), "expectedType=", 13) == 0)
  1567. {
  1568. formatInfo->mExpectedType = formatCmd.Substring(13);
  1569. if (formatInfo->mExpectedType[0] == '\"')
  1570. formatInfo->mExpectedType = formatInfo->mExpectedType.Substring(1, formatInfo->mExpectedType.length() - 2);
  1571. }
  1572. else if (strncmp(formatCmd.c_str(), "namespaceSearch=", 16) == 0)
  1573. {
  1574. formatInfo->mNamespaceSearch = formatCmd.Substring(16);
  1575. if (formatInfo->mNamespaceSearch[0] == '\"')
  1576. formatInfo->mNamespaceSearch = formatInfo->mNamespaceSearch.Substring(1, formatInfo->mNamespaceSearch.length() - 2);
  1577. }
  1578. else if (formatCmd == "d")
  1579. {
  1580. formatInfo->mDisplayType = DwDisplayType_Decimal;
  1581. }
  1582. else if (formatCmd == "x")
  1583. {
  1584. formatInfo->mDisplayType = DwDisplayType_HexLower;
  1585. }
  1586. else if (formatCmd == "X")
  1587. {
  1588. formatInfo->mDisplayType = DwDisplayType_HexUpper;
  1589. }
  1590. else if (formatCmd == "s")
  1591. {
  1592. formatInfo->mHidePointers = true;
  1593. formatInfo->mDisplayType = DwDisplayType_Ascii;
  1594. }
  1595. else if (formatCmd == "s8")
  1596. {
  1597. formatInfo->mHidePointers = true;
  1598. formatInfo->mDisplayType = DwDisplayType_Utf8;
  1599. }
  1600. else if (formatCmd == "s16")
  1601. {
  1602. formatInfo->mHidePointers = true;
  1603. formatInfo->mDisplayType = DwDisplayType_Utf16;
  1604. }
  1605. else if (formatCmd == "s32")
  1606. {
  1607. formatInfo->mHidePointers = true;
  1608. formatInfo->mDisplayType = DwDisplayType_Utf32;
  1609. }
  1610. else if (formatCmd == "nd")
  1611. {
  1612. formatInfo->mIgnoreDerivedClassInfo = true;
  1613. }
  1614. else if (formatCmd == "na")
  1615. {
  1616. formatInfo->mHidePointers = true;
  1617. }
  1618. else if (formatCmd == "nm")
  1619. {
  1620. formatInfo->mNoMembers = true;
  1621. }
  1622. else if (formatCmd == "ne")
  1623. {
  1624. formatInfo->mNoEdit = true;
  1625. }
  1626. else if (formatCmd == "nv")
  1627. {
  1628. formatInfo->mNoVisualizers = true;
  1629. }
  1630. else if (formatCmd == "rawStr")
  1631. {
  1632. formatInfo->mRawString = true;
  1633. }
  1634. else if (((!formatCmd.IsEmpty()) && ((formatCmd[0] >= '0') && (formatCmd[0] <= '9'))) ||
  1635. (formatCmd.StartsWith("(")))
  1636. {
  1637. String countExpr = formatCmd;
  1638. if (countExpr.empty())
  1639. break;
  1640. CeEvaluationContext dbgEvaluationContext(this, countExpr, formatInfo);
  1641. BfTypedValue countValue = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1642. if ((countValue) && (countValue.mType->IsInteger()))
  1643. formatInfo->mArrayLength = (intptr)ValueToInt(countValue);
  1644. if (dbgEvaluationContext.HadError())
  1645. {
  1646. if (errorString != NULL)
  1647. *errorString = dbgEvaluationContext.GetErrorStr();
  1648. return false;
  1649. }
  1650. formatFlags = dbgEvaluationContext.mExprString;
  1651. continue;
  1652. }
  1653. else
  1654. hadError = true;
  1655. if (hadError)
  1656. {
  1657. if (errorString != NULL)
  1658. *errorString = "Invalid format flags";
  1659. return false;
  1660. }
  1661. formatFlags = formatFlags.Substring(nextComma);
  1662. }
  1663. }
  1664. return true;
  1665. }
  1666. String CeDebugger::MaybeQuoteFormatInfoParam(const StringImpl& str)
  1667. {
  1668. bool needsQuote = false;
  1669. for (int i = 0; i < (int)str.length(); i++)
  1670. {
  1671. char c = str[i];
  1672. if (c == ',')
  1673. needsQuote = true;
  1674. }
  1675. if (!needsQuote)
  1676. return str;
  1677. String qStr = "\"";
  1678. qStr += str;
  1679. qStr += "\"";
  1680. return qStr;
  1681. }
  1682. BfTypedValue CeDebugger::EvaluateInContext(const BfTypedValue& contextTypedValue, const StringImpl& subExpr, CeFormatInfo* formatInfo, String* outReferenceId, String* outErrors)
  1683. {
  1684. CeEvaluationContext dbgEvaluationContext(this, subExpr, formatInfo, contextTypedValue);
  1685. // if (formatInfo != NULL)
  1686. // {
  1687. // dbgEvaluationContext.mDbgExprEvaluator->mSubjectExpr = formatInfo->mSubjectExpr;
  1688. // }
  1689. //dbgEvaluationContext.mDbgExprEvaluator->mReferenceId = outReferenceId;
  1690. SetAndRestoreValue<CeEvaluationContext*> prevEvalContext(mCurEvaluationContext, &dbgEvaluationContext);
  1691. //mCountResultOverride = -1;
  1692. auto result = dbgEvaluationContext.EvaluateInContext(contextTypedValue);
  1693. // if ((formatInfo != NULL) && (dbgEvaluationContext.mDbgExprEvaluator->mCountResultOverride != -1))
  1694. // formatInfo->mOverrideCount = dbgEvaluationContext.mDbgExprEvaluator->mCountResultOverride;
  1695. if (dbgEvaluationContext.mPassInstance->HasFailed())
  1696. {
  1697. if (outErrors != NULL)
  1698. {
  1699. int errIdx = 0;
  1700. for (auto err : dbgEvaluationContext.mPassInstance->mErrors)
  1701. {
  1702. if (errIdx > 0)
  1703. (*outErrors) += "\n";
  1704. (*outErrors) += err->mError;
  1705. errIdx++;
  1706. }
  1707. }
  1708. return BfTypedValue();
  1709. }
  1710. return result;
  1711. }
  1712. void CeDebugger::DbgVisFailed(DebugVisualizerEntry* debugVis, const StringImpl& evalString, const StringImpl& errors)
  1713. {
  1714. bool onlyMemError = errors.StartsWith("Failed to read") && !errors.Contains('\n');
  1715. if ((!debugVis->mShowedError) && (!onlyMemError))
  1716. {
  1717. debugVis->mShowedError = true;
  1718. String errStr = StrFormat("DbgVis '%s' failed while evaluating condition '%s'\n", debugVis->mName.c_str(), evalString.c_str());
  1719. String spacedErrors = errors;
  1720. spacedErrors.Insert(0, " ");
  1721. spacedErrors.Replace("\n", "\n ");
  1722. errStr += spacedErrors;
  1723. OutputMessage(errStr);
  1724. }
  1725. }
  1726. bool CeDebugger::EvalCondition(DebugVisualizerEntry* debugVis, BfTypedValue typedVal, CeFormatInfo& formatInfo, const StringImpl& condition, const Array<String>& dbgVisWildcardCaptures, String& errorStr)
  1727. {
  1728. auto ceModule = mCeMachine->mCeModule;
  1729. CeFormatInfo displayStrFormatInfo = formatInfo;
  1730. displayStrFormatInfo.mHidePointers = false;
  1731. displayStrFormatInfo.mRawString = false;
  1732. String errors;
  1733. const String conditionStr = mDebugManager->mDebugVisualizers->DoStringReplace(condition, dbgVisWildcardCaptures);
  1734. BfTypedValue evalResult = EvaluateInContext(typedVal, conditionStr, &displayStrFormatInfo, NULL, &errors);
  1735. if ((!evalResult) || (!evalResult.mType->IsBoolean()))
  1736. {
  1737. if (formatInfo.mRawString)
  1738. return false;
  1739. errorStr += "<DbgVis Failed>";
  1740. DbgVisFailed(debugVis, conditionStr, errors);
  1741. return false;
  1742. }
  1743. evalResult = ceModule->LoadValue(evalResult);
  1744. if (auto constant = ceModule->mBfIRBuilder->GetConstant(evalResult.mValue))
  1745. {
  1746. if (constant->mTypeCode == BfTypeCode_Boolean)
  1747. return constant->mBool;
  1748. }
  1749. return false;
  1750. }
  1751. String CeDebugger::GetArrayItems(DebugVisualizerEntry* debugVis, BfType* valueType, BfTypedValue& curNode, int& count, String* outContinuationData)
  1752. {
  1753. CeEvaluationContext conditionEvaluationContext(this, debugVis->mCondition);
  1754. auto ceModule = mCeMachine->mCeModule;
  1755. String addrs;
  1756. bool checkLeft = true;
  1757. addr_ce curNodeAddr = 0;
  1758. int usedCount = 0;
  1759. while (usedCount < count)
  1760. {
  1761. if ((!curNode) || (!curNode.mType->IsPointer()))
  1762. break;
  1763. curNode = ceModule->LoadValue(curNode);
  1764. BfTypedValue condVal = conditionEvaluationContext.EvaluateInContext(curNode);
  1765. if (!condVal)
  1766. break;
  1767. auto ceTypedVal = GetAddr(curNode);
  1768. if (!ceTypedVal)
  1769. break;
  1770. if (ValueToInt(condVal) != 0)
  1771. {
  1772. auto val = curNode;
  1773. if (valueType == NULL)
  1774. {
  1775. //String typeAddr = val.mType->ToStringRaw();
  1776. String typeAddr = StrFormat("comptype(%d)", val.mType->mTypeId);
  1777. // RPad
  1778. typeAddr.Append(' ', sizeof(addr_ce) * 2 - (int)typeAddr.length());
  1779. addrs += typeAddr;
  1780. }
  1781. String addr = EncodeDataPtr((addr_ce)ceTypedVal.mAddr, false);
  1782. addrs += addr;
  1783. usedCount++;
  1784. }
  1785. auto elemType = curNode.mType->GetUnderlyingType();
  1786. curNodeAddr = (addr_ce)ceTypedVal.mAddr + elemType->GetStride();
  1787. curNode.mValue = ceModule->mBfIRBuilder->CreateIntToPtr(curNodeAddr, ceModule->mBfIRBuilder->MapType(curNode.mType));
  1788. }
  1789. count = usedCount;
  1790. if (outContinuationData != NULL)
  1791. {
  1792. *outContinuationData += EncodeDataPtr(debugVis, false) + EncodeDataPtr(valueType, false) +
  1793. EncodeDataPtr(curNode.mType, false) + EncodeDataPtr(curNodeAddr, false);
  1794. }
  1795. return addrs;
  1796. }
  1797. String CeDebugger::GetLinkedListItems(DebugVisualizerEntry* debugVis, addr_ce endNodePtr, BfType* valueType, BfTypedValue& curNode, int& count, String* outContinuationData)
  1798. {
  1799. CeEvaluationContext nextEvaluationContext(this, debugVis->mNextPointer);
  1800. CeEvaluationContext valueEvaluationContext(this, debugVis->mValuePointer);
  1801. auto ceModule = mCeMachine->mCeModule;
  1802. String addrs;
  1803. bool checkLeft = true;
  1804. int mapIdx;
  1805. for (mapIdx = 0; mapIdx < count; mapIdx++)
  1806. {
  1807. CeTypedValue ceNodeVal = GetAddr(curNode);
  1808. if (!ceNodeVal)
  1809. break;
  1810. if (ceNodeVal.mAddr == endNodePtr)
  1811. break;
  1812. BfTypedValue val = valueEvaluationContext.EvaluateInContext(curNode);
  1813. if (!val)
  1814. break;
  1815. auto ceVal = GetAddr(val);
  1816. if (!ceVal)
  1817. break;
  1818. if (ceVal.mAddr == 0)
  1819. break;
  1820. if (valueType == NULL)
  1821. {
  1822. String typeAddr = StrFormat("comptype(%d)", val.mType->mTypeId);
  1823. // RPad
  1824. typeAddr.Append(' ', sizeof(addr_ce) * 2 - (int)typeAddr.length());
  1825. addrs += typeAddr;
  1826. }
  1827. String addr = EncodeDataPtr((addr_ce)ceVal.mAddr, false);
  1828. addrs += addr;
  1829. curNode = nextEvaluationContext.EvaluateInContext(curNode);
  1830. }
  1831. count = mapIdx;
  1832. if (outContinuationData != NULL)
  1833. {
  1834. CeTypedValue ceNodeVal = GetAddr(curNode);
  1835. *outContinuationData += EncodeDataPtr(debugVis, false) + EncodeDataPtr(endNodePtr, false) + EncodeDataPtr(valueType, false) +
  1836. EncodeDataPtr(curNode.mType, false) + EncodeDataPtr((addr_ce)ceNodeVal.mAddr, false);
  1837. }
  1838. return addrs;
  1839. }
  1840. String CeDebugger::GetDictionaryItems(DebugVisualizerEntry* debugVis, BfTypedValue dictValue, int bucketIdx, int nodeIdx, int& count, String* outContinuationData)
  1841. {
  1842. CeEvaluationContext nextEvaluationContext(this, debugVis->mNextPointer);
  1843. auto ceModule = mCeMachine->mCeModule;
  1844. BfTypedValue bucketsPtr = EvaluateInContext(dictValue, debugVis->mBuckets);
  1845. BfTypedValue entriesPtr = EvaluateInContext(dictValue, debugVis->mEntries);
  1846. if ((!bucketsPtr) || (!entriesPtr))
  1847. {
  1848. count = -1;
  1849. return "";
  1850. }
  1851. auto ceDictTypedVal = GetAddr(dictValue);
  1852. if (!ceDictTypedVal)
  1853. return "";
  1854. auto ceBucketsTypedVal = GetAddr(bucketsPtr);
  1855. if (!ceBucketsTypedVal)
  1856. return "";
  1857. String addrs;
  1858. if ((!entriesPtr) || (!entriesPtr.mType->IsPointer()))
  1859. return "";
  1860. if ((!bucketsPtr) || (!bucketsPtr.mType->IsPointer()))
  1861. return "";
  1862. auto entryType = entriesPtr.mType->GetUnderlyingType();
  1863. int entrySize = entryType->GetStride();
  1864. int bucketIdxSize = bucketsPtr.mType->GetUnderlyingType()->GetStride();
  1865. auto ceElemTypedVal = GetAddr(entriesPtr);
  1866. if (!ceElemTypedVal)
  1867. return "";
  1868. bool checkLeft = true;
  1869. int encodeCount = 0;
  1870. while (encodeCount < count)
  1871. {
  1872. if (nodeIdx != -1)
  1873. {
  1874. addr_ce entryAddr = (addr_ce)ceElemTypedVal.mAddr + (nodeIdx * entrySize);
  1875. BfTypedValue entryValue = BfTypedValue(ceModule->mBfIRBuilder->CreateConstAggCE(ceModule->mBfIRBuilder->MapType(entryType), entryAddr), entryType);
  1876. addrs += EncodeDataPtr(entryAddr, false);
  1877. BfTypedValue nextValue = nextEvaluationContext.EvaluateInContext(entryValue);
  1878. if ((!nextValue) || (!nextValue.mType->IsInteger()))
  1879. {
  1880. break;
  1881. }
  1882. nodeIdx = (int)ValueToInt(nextValue);
  1883. encodeCount++;
  1884. }
  1885. else
  1886. {
  1887. if (bucketIdxSize == 4)
  1888. nodeIdx = ReadMemory<int>(ceBucketsTypedVal.mAddr + bucketIdx * sizeof(int32));
  1889. else
  1890. nodeIdx = (int)ReadMemory<int64>(ceBucketsTypedVal.mAddr + bucketIdx * sizeof(int64));
  1891. bucketIdx++;
  1892. }
  1893. }
  1894. count = encodeCount;
  1895. if (outContinuationData != NULL)
  1896. {
  1897. *outContinuationData += EncodeDataPtr(debugVis, false) + EncodeDataPtr(dictValue.mType, false) + EncodeDataPtr((addr_ce)ceDictTypedVal.mAddr, false) +
  1898. EncodeDataPtr((addr_ce)bucketIdx, false) + EncodeDataPtr((addr_ce)nodeIdx, false);
  1899. }
  1900. return addrs;
  1901. }
  1902. String CeDebugger::GetTreeItems(DebugVisualizerEntry* debugVis, Array<addr_ce>& parentList, BfType*& valueType, BfTypedValue& curNode, int count, String* outContinuationData)
  1903. {
  1904. CeEvaluationContext leftEvaluationContext(this, debugVis->mLeftPointer);
  1905. CeEvaluationContext rightEvaluationContext(this, debugVis->mRightPointer);
  1906. CeEvaluationContext valueEvaluationContext(this, debugVis->mValuePointer);
  1907. CeEvaluationContext conditionEvaluationContext(this, debugVis->mCondition);
  1908. String addrs;
  1909. //TODO:
  1910. // bool checkLeft = true;
  1911. //
  1912. // if ((curNode.mPtr & 2) != 0) // Flag from continuation
  1913. // {
  1914. // checkLeft = false;
  1915. // curNode.mPtr &= (addr_ce)~2;
  1916. // }
  1917. //
  1918. // HashSet<intptr> seenAddrs;
  1919. //
  1920. // for (int mapIdx = 0; mapIdx < count; mapIdx++)
  1921. // {
  1922. // BfTypedValue readNode;
  1923. // while (true)
  1924. // {
  1925. // bool checkNode = (curNode.mPtr & 1) == 0;
  1926. //
  1927. // readNode = curNode;
  1928. // readNode.mPtr &= (addr_ce)~1;
  1929. //
  1930. // if (checkLeft)
  1931. // {
  1932. // BfTypedValue leftValue = leftEvaluationContext.EvaluateInContext(readNode);
  1933. // bool isEmpty = leftValue.mPtr == NULL;
  1934. // if ((leftValue) && (conditionEvaluationContext.HasExpression()))
  1935. // {
  1936. // auto condValue = conditionEvaluationContext.EvaluateInContext(leftValue);
  1937. // if (condValue)
  1938. // isEmpty = !condValue.mBool;
  1939. // }
  1940. // if (isEmpty)
  1941. // {
  1942. // checkLeft = false;
  1943. // break; // Handle node
  1944. // }
  1945. //
  1946. // parentList.push_back(curNode.mPtr);
  1947. // curNode = leftValue;
  1948. // }
  1949. // else if (checkNode)
  1950. // {
  1951. // break; // Handle node
  1952. // }
  1953. // else
  1954. // {
  1955. // BfTypedValue rightValue = rightEvaluationContext.EvaluateInContext(readNode);
  1956. // bool isEmpty = rightValue.mPtr == NULL;
  1957. // if ((rightValue) && (conditionEvaluationContext.HasExpression()))
  1958. // {
  1959. // auto condValue = conditionEvaluationContext.EvaluateInContext(rightValue);
  1960. // if (condValue)
  1961. // isEmpty = !condValue.mBool;
  1962. // }
  1963. // if (!isEmpty)
  1964. // {
  1965. // curNode = rightValue;
  1966. // checkLeft = true;
  1967. // }
  1968. // else
  1969. // {
  1970. // if (parentList.size() == 0)
  1971. // {
  1972. // // Failed
  1973. // break;
  1974. // }
  1975. //
  1976. // curNode.mPtr = parentList.back();
  1977. // parentList.pop_back();
  1978. // continue; // Don't check against seenAddrs
  1979. // }
  1980. // }
  1981. //
  1982. // if (!seenAddrs.Add(curNode.mPtr))
  1983. // {
  1984. // // Failed!
  1985. // return "";
  1986. // }
  1987. // }
  1988. //
  1989. //
  1990. // BfTypedValue val = valueEvaluationContext.EvaluateInContext(readNode);
  1991. // if (valueType == NULL)
  1992. // valueType = val.mType;
  1993. //
  1994. // String addr = EncodeDataPtr(val.mPtr, false);
  1995. // addrs += addr;
  1996. //
  1997. // curNode.mPtr |= 1; // Node handled
  1998. // }
  1999. //
  2000. // if (!checkLeft)
  2001. // curNode.mPtr |= 2;
  2002. //
  2003. // if (outContinuationData != NULL)
  2004. // {
  2005. // *outContinuationData += EncodeDataPtr(debugVis, false) + EncodeDataPtr(valueType, false) + EncodeDataPtr(curNode.mType, false) + EncodeDataPtr(curNode.mPtr, false);
  2006. // for (auto parent : parentList)
  2007. // *outContinuationData += EncodeDataPtr(parent, false);
  2008. // }
  2009. return addrs;
  2010. }
  2011. String CeDebugger::GetCollectionContinuation(const StringImpl& continuationData, int callStackIdx, int count)
  2012. {
  2013. if (!mCeMachine->mDbgPaused)
  2014. return "";
  2015. auto ceModule = mCeMachine->mCeModule;
  2016. const char* dataPtr = continuationData.c_str();
  2017. DebugVisualizerEntry* debugVis = (DebugVisualizerEntry*)DecodeLocalDataPtr(dataPtr);
  2018. if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_TreeItems)
  2019. {
  2020. //TODO:
  2021. // DbgType* valueType = (DbgType*)DecodeLocalDataPtr(dataPtr);
  2022. // BfTypedValue curNode;
  2023. // curNode.mType = (DbgType*)DecodeLocalDataPtr(dataPtr);
  2024. // curNode.mPtr = DecodeTargetDataPtr(dataPtr);
  2025. //
  2026. // Array<addr_ce> parentList;
  2027. // String newContinuationData;
  2028. // while (*dataPtr != 0)
  2029. // parentList.push_back(DecodeTargetDataPtr(dataPtr));
  2030. //
  2031. // String retVal = GetTreeItems(dbgCompileUnit, debugVis, parentList, valueType, curNode, count, &newContinuationData);
  2032. // retVal += "\n" + newContinuationData;
  2033. // return retVal;
  2034. }
  2035. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_LinkedList)
  2036. {
  2037. addr_ce endNodePtr = DecodeTargetDataPtr(dataPtr);
  2038. BfType* valueType = (BfType*)DecodeLocalDataPtr(dataPtr);
  2039. BfType* nodeType = (BfType*)DecodeLocalDataPtr(dataPtr);
  2040. BfTypedValue curNode = BfTypedValue(
  2041. ceModule->mBfIRBuilder->CreateIntToPtr(DecodeTargetDataPtr(dataPtr), ceModule->mBfIRBuilder->MapType(nodeType)),
  2042. nodeType);
  2043. String newContinuationData;
  2044. if (count < 0)
  2045. count = 3;
  2046. String retVal = GetLinkedListItems(debugVis, endNodePtr, valueType, curNode, count, &newContinuationData);
  2047. retVal += "\n" + newContinuationData;
  2048. return retVal;
  2049. }
  2050. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_Array)
  2051. {
  2052. BfType* valueType = (BfType*)DecodeLocalDataPtr(dataPtr);
  2053. auto nodeType = (BfType*)DecodeLocalDataPtr(dataPtr);
  2054. BfTypedValue curNode = BfTypedValue(
  2055. ceModule->mBfIRBuilder->CreateIntToPtr(DecodeTargetDataPtr(dataPtr), ceModule->mBfIRBuilder->MapType(nodeType)),
  2056. nodeType);
  2057. String newContinuationData;
  2058. if (count < 0)
  2059. count = 3;
  2060. String retVal = GetArrayItems(debugVis, valueType, curNode, count, &newContinuationData);
  2061. retVal += "\n" + newContinuationData;
  2062. return retVal;
  2063. }
  2064. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_Dictionary)
  2065. {
  2066. auto dictValueType = (BfType*)DecodeLocalDataPtr(dataPtr);
  2067. BfTypedValue dictValue = BfTypedValue(
  2068. ceModule->mBfIRBuilder->CreateIntToPtr(DecodeTargetDataPtr(dataPtr), ceModule->mBfIRBuilder->MapType(dictValueType)),
  2069. dictValueType);
  2070. int bucketIdx = (int)DecodeTargetDataPtr(dataPtr);
  2071. int nodeIdx = (int)DecodeTargetDataPtr(dataPtr);
  2072. String newContinuationData;
  2073. String retVal = GetDictionaryItems(debugVis, dictValue, bucketIdx, nodeIdx, count, &newContinuationData);
  2074. retVal += "\n" + newContinuationData;
  2075. return retVal;
  2076. }
  2077. return "";
  2078. }
  2079. CeTypedValue CeDebugger::GetAddr(BfConstant* constant, BfType* type)
  2080. {
  2081. auto module = mCeMachine->mCeModule;
  2082. auto ceContext = mCeMachine->mCurContext;
  2083. if (constant->mConstType == BfConstType_GlobalVar)
  2084. {
  2085. auto globalVar = (BfGlobalVar*)constant;
  2086. String varName(globalVar->mName);
  2087. if (varName.StartsWith("__bfStrObj"))
  2088. {
  2089. int stringId = atoi(varName.c_str() + 10);
  2090. auto addr = ceContext->GetString(stringId);
  2091. return CeTypedValue(addr, globalVar->mType);
  2092. }
  2093. else if (varName.StartsWith("__bfStrData"))
  2094. {
  2095. auto stringType = module->ResolveTypeDef(module->mCompiler->mStringTypeDef)->ToTypeInstance();
  2096. int stringId = atoi(varName.c_str() + 11);
  2097. auto addr = ceContext->GetString(stringId) + stringType->mInstSize;
  2098. return CeTypedValue(addr, globalVar->mType);
  2099. }
  2100. CeStaticFieldInfo* fieldInfo = NULL;
  2101. if (ceContext->mStaticFieldMap.TryAdd(globalVar->mName, NULL, &fieldInfo))
  2102. {
  2103. auto dbgTypeInfo = GetDbgTypeInfo(globalVar->mType);
  2104. if (dbgTypeInfo != NULL)
  2105. {
  2106. uint8* ptr = ceContext->CeMalloc(dbgTypeInfo->mType->mSize);
  2107. if (dbgTypeInfo->mType->mSize > 0)
  2108. memset(ptr, 0, dbgTypeInfo->mType->mSize);
  2109. fieldInfo->mAddr = (addr_ce)(ptr - ceContext->mMemory.mVals);
  2110. }
  2111. else
  2112. fieldInfo->mAddr = 0;
  2113. }
  2114. return CeTypedValue(fieldInfo->mAddr, globalVar->mType);
  2115. }
  2116. else if (constant->mTypeCode == BfTypeCode_StringId)
  2117. {
  2118. auto stringType = module->ResolveTypeDef(module->mCompiler->mStringTypeDef)->ToTypeInstance();
  2119. if ((type != NULL) && (type->IsPointer()))
  2120. {
  2121. BfType* charType = module->GetPrimitiveType(BfTypeCode_Char8);
  2122. BfType* charPtrType = module->CreatePointerType(charType);
  2123. auto addr = ceContext->GetString(constant->mInt32) + stringType->mInstSize;
  2124. return CeTypedValue(addr, module->mBfIRBuilder->MapType(charPtrType));
  2125. }
  2126. else
  2127. {
  2128. auto addr = ceContext->GetString(constant->mInt32);
  2129. return CeTypedValue(addr, module->mBfIRBuilder->MapType(stringType));
  2130. }
  2131. }
  2132. else if (constant->mConstType == BfConstType_AggCE)
  2133. {
  2134. auto aggCE = (BfConstantAggCE*)constant;
  2135. return CeTypedValue(aggCE->mCEAddr, aggCE->mType);
  2136. }
  2137. else if (constant->mConstType == BfConstType_BitCast)
  2138. {
  2139. auto constBitCast = (BfConstantBitCast*)constant;
  2140. auto val = GetAddr(module->mBfIRBuilder->GetConstantById(constBitCast->mTarget));
  2141. if (!val)
  2142. return val;
  2143. return CeTypedValue(val.mAddr, constBitCast->mToType);
  2144. }
  2145. else if (constant->mConstType == BfConstType_IntToPtr)
  2146. {
  2147. auto fromPtrToInt = (BfConstantIntToPtr*)constant;
  2148. auto val = GetAddr(module->mBfIRBuilder->GetConstantById(fromPtrToInt->mTarget));
  2149. if (!val)
  2150. return val;
  2151. return CeTypedValue(val.mAddr, fromPtrToInt->mToType);
  2152. }
  2153. else if (constant->mConstType == BfConstType_GEP32_1)
  2154. {
  2155. auto gepConst = (BfConstantGEP32_1*)constant;
  2156. auto constant = module->mBfIRBuilder->GetConstantById(gepConst->mTarget);
  2157. auto typedVal = GetAddr(constant);
  2158. if (!typedVal)
  2159. return CeTypedValue();
  2160. auto dbgTypeInfo = GetDbgTypeInfo(typedVal.mType);
  2161. if (dbgTypeInfo == NULL)
  2162. return CeTypedValue();
  2163. auto addr = typedVal.mAddr;
  2164. if (gepConst->mIdx0 != 0)
  2165. addr += gepConst->mIdx0 * dbgTypeInfo->mType->GetUnderlyingType()->GetStride();
  2166. return CeTypedValue(addr, module->mBfIRBuilder->MapType(dbgTypeInfo->mType));
  2167. }
  2168. else if (constant->mConstType == BfConstType_GEP32_2)
  2169. {
  2170. auto gepConst = (BfConstantGEP32_2*)constant;
  2171. auto constant = module->mBfIRBuilder->GetConstantById(gepConst->mTarget);
  2172. auto typedVal = GetAddr(constant);
  2173. if (!typedVal.mAddr)
  2174. return CeTypedValue();
  2175. auto dbgTypeInfo = GetDbgTypeInfo(typedVal.mType);
  2176. if ((dbgTypeInfo != NULL) &&
  2177. ((dbgTypeInfo->mType->IsPointer()) || ((dbgTypeInfo->mType->IsRef()))))
  2178. dbgTypeInfo = GetDbgTypeInfo(dbgTypeInfo->mType->GetUnderlyingType()->mTypeId);
  2179. if (dbgTypeInfo == NULL)
  2180. return CeTypedValue();
  2181. auto addr = typedVal.mAddr;
  2182. if (gepConst->mIdx0 != 0)
  2183. addr += gepConst->mIdx0 * dbgTypeInfo->mType->GetStride();
  2184. if (gepConst->mIdx1 != 0)
  2185. addr += dbgTypeInfo->mFieldOffsets[gepConst->mIdx1].mDataOffset;
  2186. BfType* ptrType = NULL;
  2187. if (gepConst->mIdx1 == 0)
  2188. {
  2189. auto typeInst = dbgTypeInfo->mType->ToTypeInstance();
  2190. if ((typeInst != NULL) && (typeInst->mBaseType != NULL))
  2191. {
  2192. ptrType = typeInst->mBaseType;
  2193. if (!ptrType->IsValueType())
  2194. ptrType = module->CreatePointerType(ptrType);
  2195. }
  2196. }
  2197. if (ptrType == NULL)
  2198. {
  2199. if (gepConst->mIdx1 > dbgTypeInfo->mFieldOffsets.mSize)
  2200. return CeTypedValue();
  2201. ptrType = module->CreatePointerType(dbgTypeInfo->mFieldOffsets[gepConst->mIdx1].mType);
  2202. }
  2203. return CeTypedValue(addr, module->mBfIRBuilder->MapType(ptrType));
  2204. }
  2205. else if ((constant->mTypeCode == BfTypeCode_Int32) ||
  2206. (constant->mTypeCode == BfTypeCode_Int64) ||
  2207. (constant->mTypeCode == BfTypeCode_IntPtr))
  2208. {
  2209. return CeTypedValue(constant->mInt64, module->mBfIRBuilder->GetPrimitiveType(constant->mTypeCode));
  2210. }
  2211. return CeTypedValue();
  2212. }
  2213. CeTypedValue CeDebugger::GetAddr(const BfTypedValue typedVal)
  2214. {
  2215. auto constant = mCeMachine->mCeModule->mBfIRBuilder->GetConstant(typedVal.mValue);
  2216. if (constant == NULL)
  2217. return CeTypedValue();
  2218. return GetAddr(constant, typedVal.mType);
  2219. }
  2220. #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
  2221. String CeDebugger::ReadString(BfTypeCode charType, intptr addr, intptr maxLength, CeFormatInfo& formatInfo)
  2222. {
  2223. int origMaxLength = (int)maxLength;
  2224. if (addr == 0)
  2225. return "";
  2226. BP_ZONE("WinDebugger::ReadString");
  2227. String retVal = "\"";
  2228. bool wasTerminated = false;
  2229. String valString;
  2230. intptr maxShowSize = 255;
  2231. if (maxLength == -1)
  2232. maxLength = formatInfo.mOverrideCount;
  2233. else if (formatInfo.mOverrideCount != -1)
  2234. maxLength = BF_MIN(formatInfo.mOverrideCount, maxLength);
  2235. if (formatInfo.mMaxCount != -1)
  2236. maxLength = BF_MIN(formatInfo.mMaxCount, maxLength);
  2237. if (maxLength == -1)
  2238. maxLength = 8 * 1024 * 1024; // Is 8MB crazy?
  2239. if (!formatInfo.mRawString)
  2240. maxLength = BF_MIN(maxLength, maxShowSize);
  2241. //EnableMemCache();
  2242. bool readFailed = false;
  2243. intptr strPtr = addr;
  2244. int charLen = 1;
  2245. if (charType == BfTypeCode_Char16)
  2246. charLen = 2;
  2247. else if (charType == BfTypeCode_Char32)
  2248. charLen = 4;
  2249. bool isUTF8 = formatInfo.mDisplayType == DwDisplayType_Utf8;
  2250. int readSize = BF_MIN(1024, (int)maxLength * charLen);
  2251. uint8 buf[1024];
  2252. uint8* bufPtr = NULL;
  2253. uint8* bufEnd = NULL;
  2254. bool hasHighAscii = false;
  2255. int i;
  2256. for (i = 0; i < maxLength; i++)
  2257. {
  2258. if (bufPtr >= bufEnd)
  2259. {
  2260. while (true)
  2261. {
  2262. if (readSize < charLen)
  2263. {
  2264. readFailed = true;
  2265. break;
  2266. }
  2267. if (ReadMemory(strPtr, readSize, buf))
  2268. break;
  2269. readSize /= 2;
  2270. }
  2271. if (readFailed)
  2272. break;
  2273. bufPtr = buf;
  2274. bufEnd = buf + readSize;
  2275. }
  2276. switch (charLen)
  2277. {
  2278. case 1:
  2279. {
  2280. char c = GET_FROM(bufPtr, char);
  2281. if ((c != 0) || (formatInfo.mOverrideCount != -1))
  2282. {
  2283. if ((uint8)c >= 0x80)
  2284. hasHighAscii = true;
  2285. valString.Append(c);
  2286. }
  2287. else
  2288. wasTerminated = true;
  2289. }
  2290. break;
  2291. case 2:
  2292. {
  2293. uint16 c16 = GET_FROM(bufPtr, uint16);
  2294. if ((c16 != 0) || (formatInfo.mOverrideCount != -1))
  2295. {
  2296. char str[8];
  2297. u8_toutf8(str, 8, c16);
  2298. valString += str;
  2299. }
  2300. else
  2301. wasTerminated = true;
  2302. }
  2303. break;
  2304. case 4:
  2305. {
  2306. uint32 c32 = GET_FROM(bufPtr, uint32);
  2307. if ((c32 != 0) || (formatInfo.mOverrideCount != -1))
  2308. {
  2309. char str[8];
  2310. u8_toutf8(str, 8, c32);
  2311. valString += str;
  2312. }
  2313. else
  2314. wasTerminated = true;
  2315. }
  2316. break;
  2317. }
  2318. if ((wasTerminated) && (formatInfo.mOverrideCount != -1))
  2319. {
  2320. valString += '\x00';
  2321. wasTerminated = false;
  2322. }
  2323. if ((wasTerminated) || (readFailed))
  2324. {
  2325. break;
  2326. }
  2327. strPtr += charLen;
  2328. }
  2329. //DisableMemCache();
  2330. if (formatInfo.mOverrideCount != -1)
  2331. {
  2332. if (i == formatInfo.mOverrideCount)
  2333. wasTerminated = true;
  2334. }
  2335. if (strPtr == addr + origMaxLength)
  2336. wasTerminated = true;
  2337. if (valString.length() == formatInfo.mOverrideCount)
  2338. wasTerminated = true;
  2339. // if (formatInfo.mDisplayType == DwDisplayType_Ascii)
  2340. // {
  2341. // // Our encoding for retVal is already assumed to be UTF8, so the special case here actually Ascii
  2342. // valString = UTF8Encode(ToWString(valString));
  2343. // }
  2344. if (formatInfo.mRawString)
  2345. {
  2346. if ((formatInfo.mDisplayType == DwDisplayType_Utf8) || (!hasHighAscii))
  2347. return valString;
  2348. String utf8Str;
  2349. for (int i = 0; i < (int)valString.length(); i++)
  2350. {
  2351. char c = valString[i];
  2352. if ((uint8)c >= 0x80)
  2353. {
  2354. utf8Str += (char)(0xC0 | (((uint8)c & 0xFF) >> 6));
  2355. utf8Str += (char)(0x80 | ((uint8)c & 0x3F));
  2356. }
  2357. else
  2358. utf8Str += c;
  2359. }
  2360. return utf8Str;
  2361. }
  2362. if ((readFailed) && (valString.IsEmpty()))
  2363. return "< Failed to read string >";
  2364. retVal += SlashString(valString, true, true, true);
  2365. // We could go over 'maxShowSize' if we have a lot of slashed chars. An uninitialized string can be filled with '\xcc' chars
  2366. if ((!formatInfo.mRawString) && ((int)retVal.length() > maxShowSize))
  2367. {
  2368. retVal = retVal.Substring(0, maxShowSize);
  2369. wasTerminated = false;
  2370. }
  2371. if (wasTerminated)
  2372. retVal += "\"";
  2373. else
  2374. retVal += "...";
  2375. return retVal;
  2376. }
  2377. void CeDebugger::ProcessEvalString(BfTypedValue useTypedValue, String& evalStr, String& displayString, CeFormatInfo& formatInfo, DebugVisualizerEntry* debugVis, bool limitLength)
  2378. {
  2379. for (int i = 0; i < (int)evalStr.length(); i++)
  2380. {
  2381. char c = evalStr[i];
  2382. char nextC = 0;
  2383. if (i < (int)evalStr.length() - 1)
  2384. nextC = evalStr[i + 1];
  2385. if ((c == '{') && (nextC != '{'))
  2386. {
  2387. // Evaluate
  2388. int endIdx = i;
  2389. for (; endIdx < (int)evalStr.length(); endIdx++)
  2390. {
  2391. //TODO: Do better parsing - this paren could be inside a string, for example
  2392. if (evalStr[endIdx] == '}')
  2393. break;
  2394. }
  2395. CeFormatInfo displayStrFormatInfo = formatInfo;
  2396. displayStrFormatInfo.mTotalSummaryLength = formatInfo.mTotalSummaryLength + (int)displayString.length();
  2397. displayStrFormatInfo.mHidePointers = false;
  2398. if ((limitLength) && (displayStrFormatInfo.mTotalSummaryLength > 255))
  2399. {
  2400. displayString += "...";
  2401. }
  2402. else
  2403. {
  2404. String evalString = evalStr.Substring(i + 1, endIdx - i - 1);
  2405. String errors;
  2406. BfTypedValue evalResult = EvaluateInContext(useTypedValue, evalString, &displayStrFormatInfo, NULL, &errors);
  2407. if (evalResult)
  2408. {
  2409. if (displayStrFormatInfo.mNoEdit)
  2410. formatInfo.mNoEdit = true;
  2411. String result = TypedValueToString(evalResult, evalString, displayStrFormatInfo, NULL);
  2412. if ((formatInfo.mRawString) && (limitLength))
  2413. {
  2414. displayString = result;
  2415. return;
  2416. }
  2417. int crPos = (int)result.IndexOf('\n');
  2418. if (crPos != -1)
  2419. displayString += result.Substring(0, crPos);
  2420. else
  2421. displayString += result;
  2422. }
  2423. else if (debugVis != NULL)
  2424. {
  2425. displayString += "<DbgVis Failed>";
  2426. DbgVisFailed(debugVis, evalString, errors);
  2427. }
  2428. else
  2429. {
  2430. displayString += "<Eval Failed>";
  2431. }
  2432. }
  2433. i = endIdx;
  2434. continue;
  2435. }
  2436. else if ((c == '{') && (nextC == '{'))
  2437. {
  2438. // Skip next paren
  2439. i++;
  2440. }
  2441. else if ((c == '}') && (nextC == '}'))
  2442. {
  2443. // Skip next paren
  2444. i++;
  2445. }
  2446. displayString += c;
  2447. }
  2448. }
  2449. String CeDebugger::TypedValueToString(const BfTypedValue& origTypedValue, const StringImpl& expr, CeFormatInfo& formatInfo, bool fullPrecision, CeTypeModKind typeModKind)
  2450. {
  2451. BfTypedValue typedValue = origTypedValue;
  2452. auto module = mCeMachine->mCeModule;
  2453. String retVal;
  2454. if (typedValue.IsNoValueType())
  2455. {
  2456. String typeName = TypeToString(typedValue);
  2457. retVal += typeName;
  2458. retVal += "\n";
  2459. retVal += typeName;
  2460. retVal += "\n";
  2461. retVal += GetMemberList(typedValue.mType, 0, 0, true);
  2462. return retVal;
  2463. }
  2464. auto constant = module->mBfIRBuilder->GetConstant(typedValue.mValue);
  2465. auto ceContext = mCeMachine->mCurContext;
  2466. if (constant == NULL)
  2467. {
  2468. return "!Invalid expression";
  2469. }
  2470. bool didAlloc = false;
  2471. addr_ce addr = 0;
  2472. defer(
  2473. {
  2474. if (didAlloc)
  2475. mCurDbgState->mCeContext->CeFree(addr);
  2476. }
  2477. );
  2478. if (constant->mConstType == BfConstType_AggCE)
  2479. {
  2480. auto aggCE = (BfConstantAggCE*)constant;
  2481. addr = aggCE->mCEAddr;
  2482. }
  2483. else if ((typedValue.IsAddr()) || (typedValue.mType->IsObjectOrInterface()) || (typedValue.mType->IsPointer()))
  2484. {
  2485. CeTypedValue typedVal = GetAddr(constant, typedValue.mType);
  2486. addr = (addr_ce)typedVal.mAddr;
  2487. if (!typedVal)
  2488. {
  2489. return "!Invalid addr type";
  2490. }
  2491. }
  2492. else
  2493. {
  2494. int allocSize = typedValue.mType->mSize;
  2495. auto typeInst = typedValue.mType->ToTypeInstance();
  2496. if (typeInst != NULL)
  2497. allocSize = typeInst->mInstSize;
  2498. if (allocSize < 0)
  2499. return "!Invalid size";
  2500. addr = (addr_ce)(mCurDbgState->mCeContext->CeMalloc(allocSize) - mCurDbgState->mCeContext->mMemory.mVals);
  2501. didAlloc = true;
  2502. if (!mCurDbgState->mCeContext->WriteConstant(mCeMachine->mCeModule, addr, constant, typedValue.mType))
  2503. {
  2504. return StrFormat("!Failed to encode value");
  2505. }
  2506. }
  2507. DwDisplayInfo* displayInfo = GetDisplayInfo(formatInfo.mReferenceId);
  2508. char str[32];
  2509. String result;
  2510. auto memStart = mCurDbgState->mCeContext->mMemory.mVals;
  2511. int checkMemSize = typedValue.mType->mSize;
  2512. if (typedValue.mType->IsPointer())
  2513. checkMemSize = typedValue.mType->GetUnderlyingType()->mSize;
  2514. uint8* data = ceContext->GetMemoryPtr(addr, checkMemSize);
  2515. if ((addr != 0) && (data == NULL))
  2516. {
  2517. if (typedValue.mType->IsPointer())
  2518. return EncodeDataPtr(addr, true) + "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2519. else
  2520. return "!Invalid address";
  2521. }
  2522. addr_ce dataAddr = addr;
  2523. if ((typedValue.IsAddr()) && (typedValue.mType->IsObjectOrInterface()))
  2524. dataAddr = *(addr_ce*)data;
  2525. if (formatInfo.mRawString)
  2526. {
  2527. //if ((dwValueType->mTypeCode != DbgType_Struct) && (dwValueType->mTypeCode != DbgType_Class) && (dwValueType->mTypeCode != DbgType_Ptr) && (dwValueType->mTypeCode != DbgType_SizedArray))
  2528. if ((!typedValue.mType->IsPointer()) && (!typedValue.mType->IsObjectOrStruct()) && (!typedValue.mType->IsStruct()) && (!typedValue.mType->IsSizedArray()))
  2529. return "";
  2530. }
  2531. auto _ShowArraySummary = [&](String& retVal, addr_ce ptrVal, int64 arraySize, BfType* innerType)
  2532. {
  2533. auto ptrType = module->CreatePointerType(innerType);
  2534. String displayString;
  2535. displayString += "{";
  2536. for (int idx = 0; idx < arraySize; idx++)
  2537. {
  2538. if (formatInfo.mTotalSummaryLength + retVal.length() + displayString.length() > 255)
  2539. {
  2540. displayString += "...";
  2541. break;
  2542. }
  2543. if ((idx != 0) && (!displayString.EndsWith('{')))
  2544. displayString += ", ";
  2545. CeFormatInfo displayStrFormatInfo = formatInfo;
  2546. displayStrFormatInfo.mExpandItemDepth = 1;
  2547. displayStrFormatInfo.mTotalSummaryLength = formatInfo.mTotalSummaryLength + (int)retVal.length() + (int)displayString.length();
  2548. displayStrFormatInfo.mHidePointers = false;
  2549. displayStrFormatInfo.mArrayLength = -1;
  2550. // Why did we have this "na" on here? It made "void*[3]" type things show up as "{,,}"
  2551. //String evalStr = "((" + innerType->ToStringRaw(language) + "*)" + EncodeDataPtr(ptrVal, true) + StrFormat(")[%d], na", idx);
  2552. String evalStr = StrFormat("((comptype(%d))(void*)", ptrType->mTypeId) + EncodeDataPtr(ptrVal, true) + StrFormat(")[%lld]", idx);
  2553. BfTypedValue evalResult = EvaluateInContext(typedValue, evalStr, &displayStrFormatInfo);
  2554. String result;
  2555. if (evalResult)
  2556. {
  2557. result = TypedValueToString(evalResult, evalStr, displayStrFormatInfo, NULL);
  2558. int crPos = (int)result.IndexOf('\n');
  2559. if (crPos != -1)
  2560. result.RemoveToEnd(crPos);
  2561. }
  2562. else
  2563. result = "???";
  2564. displayString += result;
  2565. }
  2566. displayString += "}";
  2567. retVal += displayString;
  2568. };
  2569. if (formatInfo.mArrayLength != -1)
  2570. {
  2571. if (formatInfo.mRawString)
  2572. return "";
  2573. if (typedValue.mType->IsPointer())
  2574. {
  2575. auto elementType = typedValue.mType->GetUnderlyingType();
  2576. String retVal;
  2577. addr_ce ptrVal = addr;
  2578. if (!formatInfo.mHidePointers)
  2579. {
  2580. retVal = EncodeDataPtr(ptrVal, true) + " ";
  2581. retVal += module->TypeToString(elementType);
  2582. retVal += StrFormat("[%lld] ", (int64)formatInfo.mArrayLength);
  2583. }
  2584. _ShowArraySummary(retVal, ptrVal, formatInfo.mArrayLength, elementType);
  2585. String idxStr = "[{0}]";
  2586. retVal += "\n" + TypeToString(typedValue);
  2587. String evalStr = StrFormat("((comptype(%d))(void*)", typedValue.mType->mTypeId) + EncodeDataPtr(ptrVal, true) + ")[{0}]";
  2588. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, (int)BF_MAX(formatInfo.mArrayLength, 0), 10000) +
  2589. "\t" + idxStr + "\t" + evalStr;
  2590. return retVal;
  2591. }
  2592. else
  2593. {
  2594. CeFormatInfo newFormatInfo = formatInfo;
  2595. newFormatInfo.mArrayLength = -1;
  2596. String retVal = TypedValueToString(typedValue, expr, newFormatInfo);
  2597. int crPos = (int)retVal.IndexOf('\n');
  2598. if (crPos != -1)
  2599. retVal = "!Array length flag not valid with this type" + retVal.Substring(crPos);
  2600. return retVal;
  2601. }
  2602. }
  2603. if (typedValue.mType->IsRef())
  2604. {
  2605. if (dataAddr == 0)
  2606. return "<null>\n" + TypeToString(typedValue);
  2607. addr = *(addr_ce*)data;
  2608. dataAddr = addr;
  2609. data = ceContext->GetMemoryPtr(addr, checkMemSize);
  2610. if ((addr != 0) && (data == NULL))
  2611. return "!Invalid address";
  2612. typedValue = BfTypedValue(typedValue.mValue, typedValue.mType->GetUnderlyingType(), true);
  2613. }
  2614. if (typedValue.mType->IsPointer())
  2615. {
  2616. //ceContext->
  2617. addr_ce ptrVal = addr;
  2618. String retVal;
  2619. BfType* innerType = typedValue.mType->GetUnderlyingType();
  2620. if (innerType == NULL)
  2621. return EncodeDataPtr((uint32)ptrVal, true) + "\nvoid*";
  2622. bool isChar = false;
  2623. if (innerType->IsChar())
  2624. isChar = true;
  2625. if ((isChar) && (formatInfo.mArrayLength == -1))
  2626. {
  2627. auto primType = (BfPrimitiveType*)innerType;
  2628. if (!formatInfo.mHidePointers)
  2629. retVal = EncodeDataPtr(ptrVal, true);
  2630. int strLen = (int)formatInfo.mOverrideCount;
  2631. // if (typedValue.mIsLiteral)
  2632. // {
  2633. // if (strLen == -1)
  2634. // strLen = 0x7FFFFFFF;
  2635. // if (typedValue.mDataLen > 0)
  2636. // strLen = BF_MIN(strLen, typedValue.mDataLen);
  2637. // else
  2638. // strLen = BF_MIN(strLen, strlen(typedValue.mCharPtr));
  2639. // }
  2640. SetAndRestoreValue<intptr> prevOverrideLen(formatInfo.mOverrideCount, strLen);
  2641. String strResult = ReadString(primType->mTypeDef->mTypeCode, ptrVal, strLen, formatInfo);
  2642. if (formatInfo.mRawString)
  2643. return strResult;
  2644. if (!strResult.IsEmpty())
  2645. {
  2646. if (!retVal.IsEmpty())
  2647. retVal += " ";
  2648. retVal += strResult;
  2649. }
  2650. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2651. return retVal;
  2652. }
  2653. else if ((ptrVal != 0) && (innerType->IsComposite()))
  2654. {
  2655. // addr = *(addr_ce*)data;
  2656. // dataAddr = addr;
  2657. //
  2658. // data = ceContext->GetMemoryPtr(addr, checkMemSize);
  2659. // if ((addr != 0) && (data == NULL))
  2660. // return "!Invalid address";
  2661. typedValue = BfTypedValue(typedValue.mValue, typedValue.mType->GetUnderlyingType(), true);
  2662. }
  2663. else
  2664. {
  2665. if (formatInfo.mRawString)
  2666. return "";
  2667. String retVal;
  2668. if (!formatInfo.mHidePointers)
  2669. retVal = EncodeDataPtr((uint32)ptrVal, true);
  2670. if (ptrVal != 0)
  2671. {
  2672. BfTypedValue innerTypedVal = BfTypedValue(module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapType(innerType), (addr_ce)ptrVal), innerType, true);
  2673. innerTypedVal.mType = innerType;
  2674. if (innerTypedVal)
  2675. {
  2676. CeFormatInfo defaultFormatInfo;
  2677. defaultFormatInfo.mTotalSummaryLength = formatInfo.mTotalSummaryLength + 2; // Take into accout the necessary {}'s
  2678. defaultFormatInfo.mExpandItemDepth++;
  2679. defaultFormatInfo.mCallStackIdx = formatInfo.mCallStackIdx;
  2680. String innerStr = TypedValueToString(innerTypedVal, "", defaultFormatInfo);
  2681. int crIdx = (int)innerStr.IndexOf('\n');
  2682. if (crIdx != -1)
  2683. {
  2684. String innerDataStr = innerStr.Substring(0, crIdx);
  2685. if (!innerDataStr.empty())
  2686. {
  2687. if (!retVal.empty())
  2688. retVal += " ";
  2689. retVal += "{" + innerDataStr + "}";
  2690. }
  2691. }
  2692. else
  2693. {
  2694. retVal += " { ??? }";
  2695. }
  2696. }
  2697. }
  2698. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2699. module->PopulateType(innerType);
  2700. if ((ptrVal != 0) && (!innerType->IsValuelessType()))
  2701. {
  2702. //String ptrDataStr = StrFormat("(%s)", dwValueType->ToStringRaw(language).c_str()) + EncodeDataPtr(typedValue.mPtr, true);
  2703. retVal += "\n*\t";
  2704. // Why did we have this? It messed up a pointer to sized array
  2705. /*if (language == DbgLanguage_Beef)
  2706. retVal += "this";
  2707. else*/
  2708. retVal += "this";
  2709. if (!formatInfo.mReferenceId.empty())
  2710. retVal += ", refid=" + MaybeQuoteFormatInfoParam(formatInfo.mReferenceId);
  2711. retVal += StrFormat(", this=%d@0x%X", innerType->mTypeId, ptrVal);
  2712. }
  2713. retVal += "\n:canEdit\n:editVal\t" + EncodeDataPtr((uint32)addr, true);
  2714. return retVal;
  2715. }
  2716. }
  2717. if (typedValue.mType->IsPrimitiveType())
  2718. {
  2719. auto primType = (BfPrimitiveType*)typedValue.mType;
  2720. BfTypeCode typeCode = primType->mTypeDef->mTypeCode;
  2721. if (typeCode == BfTypeCode_IntPtr)
  2722. typeCode = (primType->mSize == 8) ? BfTypeCode_Int64 : BfTypeCode_Int32;
  2723. if (typeCode == BfTypeCode_UIntPtr)
  2724. typeCode = (primType->mSize == 8) ? BfTypeCode_UInt64 : BfTypeCode_UInt32;
  2725. switch (typeCode)
  2726. {
  2727. case BfTypeCode_None:
  2728. return "\nvoid";
  2729. case BfTypeCode_Boolean:
  2730. {
  2731. auto val = *(uint8*)(data);
  2732. if (val == 0)
  2733. return "false\n" + TypeToString(origTypedValue.mType, typeModKind);
  2734. else if (val == 1)
  2735. return "true\n" + TypeToString(origTypedValue.mType, typeModKind);
  2736. else
  2737. return StrFormat("true (%d)\n%s", val, TypeToString(origTypedValue.mType, typeModKind).c_str());
  2738. }
  2739. break;
  2740. case BfTypeCode_Char8:
  2741. {
  2742. auto val = *(uint8*)(data);
  2743. if (val != 0)
  2744. {
  2745. char str[2] = { (char)val };
  2746. result = SlashString(str, formatInfo.mDisplayType == DwDisplayType_Utf8, true);
  2747. if (!IsNormalChar(val))
  2748. result = StrFormat("'%s' (0x%02X)\n", result.c_str(), val);
  2749. else
  2750. result = StrFormat("'%s'\n", result.c_str());
  2751. }
  2752. else
  2753. result = "'\\0'\n";
  2754. return result + TypeToString(origTypedValue.mType, typeModKind);
  2755. }
  2756. break;
  2757. case BfTypeCode_Char16:
  2758. {
  2759. auto val = *(uint16*)(data);
  2760. if (val != 0)
  2761. {
  2762. u8_toutf8(str, 8, val);
  2763. result = SlashString(str, true, true);
  2764. if (!IsNormalChar(val))
  2765. result = StrFormat("'%s' (0x%02X)\n", result.c_str(), val);
  2766. else
  2767. result = StrFormat("'%s'\n", result.c_str());
  2768. }
  2769. else
  2770. result = "'\\0'\n";
  2771. return result + TypeToString(origTypedValue.mType, typeModKind);
  2772. }
  2773. break;
  2774. case BfTypeCode_Char32:
  2775. {
  2776. auto val = *(uint32*)(data);
  2777. if (val != 0)
  2778. {
  2779. u8_toutf8(str, 8, val);
  2780. result = SlashString(str, true, true);
  2781. if (!IsNormalChar(val))
  2782. result = StrFormat("'%s' (0x%02X)\n", result.c_str(), val);
  2783. else
  2784. result = StrFormat("'%s'\n", result.c_str());
  2785. }
  2786. else
  2787. result = "'\\0'\n";
  2788. return result + TypeToString(origTypedValue.mType, typeModKind);
  2789. }
  2790. break;
  2791. case BfTypeCode_Int8:
  2792. return IntTypeToString<int8>(*(int8*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2793. case BfTypeCode_UInt8:
  2794. return IntTypeToString<uint8>(*(uint8*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2795. case BfTypeCode_Int16:
  2796. return IntTypeToString<int16>(*(int16*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2797. case BfTypeCode_UInt16:
  2798. return IntTypeToString<uint16>(*(uint16*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2799. case BfTypeCode_Int32:
  2800. return IntTypeToString<int32>(*(int32*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2801. case BfTypeCode_UInt32:
  2802. return IntTypeToString<uint32>(*(uint32*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2803. case BfTypeCode_Int64:
  2804. return IntTypeToString<int64>(*(int64*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2805. case BfTypeCode_UInt64:
  2806. return IntTypeToString<uint64>(*(uint64*)(data), TypeToString(origTypedValue.mType, typeModKind), displayInfo, formatInfo);
  2807. case BfTypeCode_Float:
  2808. {
  2809. DwFloatDisplayType floatDisplayType = displayInfo->mFloatDisplayType;
  2810. if (floatDisplayType == DwFloatDisplayType_Default)
  2811. floatDisplayType = DwFloatDisplayType_Minimal;
  2812. if (floatDisplayType == DwFloatDisplayType_Minimal)
  2813. ExactMinimalFloatToStr(*(float*)data, str);
  2814. else if (floatDisplayType == DwFloatDisplayType_Full)
  2815. sprintf(str, "%1.9g", *(float*)data);
  2816. else if (floatDisplayType == DwFloatDisplayType_HexUpper)
  2817. sprintf(str, "0x%04X", *(uint32*)data);
  2818. else //if (floatDisplayType == DwFloatDisplayType_HexLower)
  2819. sprintf(str, "0x%04x", *(uint32*)data);
  2820. return StrFormat("%s\n%s", str, TypeToString(origTypedValue.mType, typeModKind).c_str());
  2821. }
  2822. case BfTypeCode_Double:
  2823. {
  2824. DwFloatDisplayType floatDisplayType = displayInfo->mFloatDisplayType;
  2825. if (floatDisplayType == DwFloatDisplayType_Default)
  2826. floatDisplayType = DwFloatDisplayType_Minimal;
  2827. if (floatDisplayType == DwFloatDisplayType_Minimal)
  2828. ExactMinimalDoubleToStr(*(double*)data, str);
  2829. else if (floatDisplayType == DwFloatDisplayType_Full)
  2830. sprintf(str, "%1.17g", *(double*)data);
  2831. else if (floatDisplayType == DwFloatDisplayType_HexUpper)
  2832. sprintf(str, "0x%08llX", *(uint64*)data);
  2833. else //if (floatDisplayType == DwFloatDisplayType_HexLower)
  2834. sprintf(str, "0x%08llx", *(uint64*)data);
  2835. return StrFormat("%s\n%s", str, TypeToString(origTypedValue.mType, typeModKind).c_str());
  2836. }
  2837. }
  2838. }
  2839. if (typedValue.mType->IsSizedArray())
  2840. {
  2841. auto arrayType = (BfSizedArrayType*)typedValue.mType;
  2842. auto innerType = arrayType->mElementType;
  2843. String retVal;
  2844. addr_ce ptrVal = addr;
  2845. intptr arraySize = arrayType->mElementCount;
  2846. intptr innerSize = innerType->GetStride();
  2847. String idxStr = "[{0}]";
  2848. if (innerType->IsChar())
  2849. {
  2850. auto primType = (BfPrimitiveType*)innerType;
  2851. String strVal = ReadString(primType->mTypeDef->mTypeCode, ptrVal, arraySize, formatInfo);
  2852. if (formatInfo.mRawString)
  2853. return strVal;
  2854. retVal += strVal;
  2855. }
  2856. else
  2857. {
  2858. if (formatInfo.mRawString)
  2859. return "";
  2860. _ShowArraySummary(retVal, ptrVal, arraySize, innerType);
  2861. }
  2862. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2863. String referenceId = TypeToString(typedValue.mType);
  2864. String evalStr;
  2865. // Why did we have the "na"? Do we not want to show addresses for all members?
  2866. auto ptrType = module->CreatePointerType(innerType);
  2867. evalStr = StrFormat("((comptype(%d))(void*)", ptrType->mTypeId) + EncodeDataPtr(ptrVal, true) + ")[{0}], refid=" + MaybeQuoteFormatInfoParam(referenceId + ".[]");
  2868. if (typedValue.IsReadOnly())
  2869. evalStr += ", ne";
  2870. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, (int)BF_MAX(arraySize, 0), 10000) +
  2871. "\t" + idxStr + "\t" + evalStr;
  2872. return retVal;
  2873. }
  2874. if (typedValue.mType->IsEnum())
  2875. {
  2876. if (formatInfo.mRawString)
  2877. return "";
  2878. String retVal;
  2879. if (typedValue.mType->IsTypedPrimitive())
  2880. {
  2881. int64 bitsLeft = ValueToInt(typedValue);
  2882. int valueCount = 0;
  2883. String editVal;
  2884. auto typeInst = typedValue.mType->ToTypeInstance();
  2885. auto dbgTypeInfo = GetDbgTypeInfo(typedValue.mType->mTypeId);
  2886. while ((dbgTypeInfo != NULL) && ((bitsLeft != 0) || (valueCount == 0)))
  2887. {
  2888. CeDbgTypeInfo::ConstIntEntry* bestMatch = NULL;
  2889. for (auto& constIntEntry : dbgTypeInfo->mConstIntEntries)
  2890. {
  2891. if (constIntEntry.mVal == bitsLeft)
  2892. {
  2893. bestMatch = &constIntEntry;
  2894. break;
  2895. }
  2896. }
  2897. if (bestMatch == NULL)
  2898. {
  2899. for (auto& constIntEntry : dbgTypeInfo->mConstIntEntries)
  2900. {
  2901. if ((constIntEntry.mVal != 0) &&
  2902. ((constIntEntry.mVal & bitsLeft) == constIntEntry.mVal))
  2903. {
  2904. bestMatch = &constIntEntry;
  2905. break;
  2906. }
  2907. }
  2908. }
  2909. if (bestMatch == NULL)
  2910. break;
  2911. if (valueCount > 0)
  2912. {
  2913. retVal += " | ";
  2914. }
  2915. auto bestFieldInstance = &typeInst->mFieldInstances[bestMatch->mFieldIdx];
  2916. retVal += ".";
  2917. retVal += bestFieldInstance->GetFieldDef()->mName;
  2918. valueCount++;
  2919. bitsLeft &= ~bestMatch->mVal;
  2920. }
  2921. if ((valueCount == 0) || (bitsLeft != 0))
  2922. {
  2923. if (valueCount > 0)
  2924. retVal += " | ";
  2925. retVal += StrFormat("%lld", bitsLeft);
  2926. }
  2927. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2928. retVal += "\n:canEdit";
  2929. return retVal;
  2930. }
  2931. else if (typedValue.mType->mDefineState >= BfTypeDefineState_Defined)
  2932. {
  2933. auto typeInst = typedValue.mType->ToTypeInstance();
  2934. BfType* dscrType = typeInst->GetDiscriminatorType();
  2935. BfType* payloadType = typeInst->GetUnionInnerType();
  2936. int dscrOffset = BF_ALIGN(payloadType->mSize, dscrType->mAlign);
  2937. int dscrVal = 0;
  2938. memcpy(&dscrVal, data + dscrOffset, dscrType->mSize);
  2939. for (auto& fieldInstance : typeInst->mFieldInstances)
  2940. {
  2941. auto fieldDef = fieldInstance.GetFieldDef();
  2942. if (!fieldInstance.mIsEnumPayloadCase)
  2943. continue;
  2944. int tagId = -fieldInstance.mDataIdx - 1;
  2945. if (dscrVal == tagId)
  2946. {
  2947. auto evalResult = BfTypedValue(module->mBfIRBuilder->CreateConstAggCE(
  2948. module->mBfIRBuilder->MapType(fieldInstance.mResolvedType), (addr_ce)dataAddr), fieldInstance.mResolvedType, true);
  2949. String innerResult = TypedValueToString(evalResult, "", formatInfo, NULL);
  2950. int crPos = (int)innerResult.IndexOf('\n');
  2951. if (crPos != -1)
  2952. innerResult.RemoveToEnd(crPos);
  2953. retVal += ".";
  2954. retVal += fieldDef->mName;
  2955. retVal += innerResult;
  2956. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2957. retVal += "\n:canEdit";
  2958. retVal += "\n" + GetMemberList(fieldInstance.mResolvedType, addr, dataAddr, false);
  2959. return retVal;
  2960. }
  2961. }
  2962. }
  2963. }
  2964. if (typedValue.mType->IsTypedPrimitive())
  2965. {
  2966. auto innerType = typedValue.mType->GetUnderlyingType();
  2967. BfTypedValue innerTypedVal = typedValue;
  2968. innerTypedVal.mType = innerType;
  2969. auto innerReturn = TypedValueToString(innerTypedVal, expr, formatInfo, fullPrecision);
  2970. if (innerReturn.StartsWith("!"))
  2971. return innerReturn;
  2972. int crPos = (int)innerReturn.IndexOf('\n');
  2973. if (crPos == -1)
  2974. return innerReturn;
  2975. retVal += "{ ";
  2976. retVal += innerReturn.Substring(0, crPos);
  2977. retVal += " }";
  2978. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  2979. return retVal;
  2980. }
  2981. bool isCompositeType = typedValue.mType->IsStruct() || typedValue.mType->IsObject();
  2982. if (isCompositeType)
  2983. {
  2984. BfTypeInstance* displayType = typedValue.mType->ToTypeInstance();
  2985. bool isMemoryValid = true;
  2986. if ((dataAddr == 0) && (addr != 0))
  2987. dataAddr = *(addr_ce*)data;
  2988. if ((((origTypedValue.mType->IsObjectOrInterface()) || (origTypedValue.mType->IsPointer())) &&
  2989. (!formatInfo.mHidePointers) || (dataAddr == 0)))
  2990. {
  2991. retVal = EncodeDataPtr((uint32)dataAddr, true);
  2992. retVal += " ";
  2993. if (!ceContext->CheckMemory(dataAddr, displayType->mInstSize))
  2994. isMemoryValid = false;
  2995. }
  2996. bool isBadSrc = false;
  2997. bool isNull = dataAddr == 0;
  2998. bool hadCustomDisplayString = false;
  2999. BfTypeInstance* actualType = displayType;
  3000. bool useActualRawType = false;
  3001. bool isTuple = typedValue.mType->IsTuple();
  3002. String ptrDataStr;
  3003. if (!formatInfo.mIgnoreDerivedClassInfo)
  3004. {
  3005. if (actualType->IsObject())
  3006. {
  3007. if (dataAddr != 0)
  3008. {
  3009. uint8* dataPtr = ceContext->GetMemoryPtr(dataAddr, 4);
  3010. if (dataPtr == 0)
  3011. return "!Invalid object address";
  3012. int actualTypeid = *(int32*)dataPtr;
  3013. auto checkType = mCompiler->mContext->FindTypeById(actualTypeid);
  3014. if (checkType != NULL)
  3015. actualType = checkType->ToTypeInstance();
  3016. }
  3017. }
  3018. }
  3019. BfTypedValue summaryTypedValue = typedValue;
  3020. if ((actualType != NULL) && (actualType != displayType))
  3021. {
  3022. summaryTypedValue = BfTypedValue(module->mBfIRBuilder->CreateIntToPtr(addr, module->mBfIRBuilder->MapType(actualType)), actualType);
  3023. }
  3024. DebugVisualizerEntry* debugVis = NULL;
  3025. Array<String> dbgVisWildcardCaptures;
  3026. if ((!formatInfo.mNoVisualizers) && (!isNull) && (!isBadSrc))
  3027. {
  3028. debugVis = FindVisualizerForType(summaryTypedValue.mType, &dbgVisWildcardCaptures);
  3029. }
  3030. bool wantsCustomExpandedItems = false;
  3031. String displayString;
  3032. if (debugVis != NULL)
  3033. {
  3034. auto& displayStringList = formatInfo.mRawString ? debugVis->mStringViews : debugVis->mDisplayStrings;
  3035. for (auto displayEntry : displayStringList)
  3036. {
  3037. if (!displayEntry->mCondition.empty())
  3038. {
  3039. if (!EvalCondition(debugVis, summaryTypedValue, formatInfo, displayEntry->mCondition, dbgVisWildcardCaptures, displayString))
  3040. continue;
  3041. }
  3042. hadCustomDisplayString = true;
  3043. String displayStr = mDebugManager->mDebugVisualizers->DoStringReplace(displayEntry->mString, dbgVisWildcardCaptures);
  3044. if (displayString.length() > 0)
  3045. displayString += " ";
  3046. ProcessEvalString(summaryTypedValue, displayStr, displayString, formatInfo, debugVis, true);
  3047. if (formatInfo.mRawString)
  3048. return displayString;
  3049. break;
  3050. }
  3051. if ((!debugVis->mExpandItems.empty()) || (debugVis->mCollectionType != DebugVisualizerEntry::CollectionType_None))
  3052. {
  3053. wantsCustomExpandedItems = true;
  3054. }
  3055. }
  3056. if (formatInfo.mRawString)
  3057. return "";
  3058. String reflectedTypeName;
  3059. if ((displayType->IsInstanceOf(mCompiler->mTypeTypeDef)) && (data != NULL))
  3060. {
  3061. auto typeInst = displayType->ToTypeInstance();
  3062. auto typeIdField = typeInst->mTypeDef->GetFieldByName("mTypeId");
  3063. if (typeIdField != NULL)
  3064. {
  3065. auto& fieldInstance = typeInst->mFieldInstances[typeIdField->mIdx];
  3066. int typeId = 0;
  3067. memcpy(&typeId, data + fieldInstance.mDataOffset, fieldInstance.mResolvedType->mSize);
  3068. auto typeType = mCompiler->mContext->FindTypeById(typeId);
  3069. if (typeType != NULL)
  3070. reflectedTypeName = module->TypeToString(typeType);
  3071. }
  3072. }
  3073. if (!reflectedTypeName.IsEmpty())
  3074. {
  3075. displayString += "{ ";
  3076. displayString += reflectedTypeName;
  3077. displayString += " }";
  3078. }
  3079. else if ((!isNull) && (!formatInfo.mNoVisualizers) && (!hadCustomDisplayString))
  3080. {
  3081. // Create our own custom display
  3082. String firstRet;
  3083. String bigRet = isTuple ? "(" : "{ ";
  3084. int memberIdx = 0;
  3085. BfType* summaryType = summaryTypedValue.mType;
  3086. bool summaryDone = false;
  3087. bool truncatedMemberList = false;
  3088. String summaryDataStr = ptrDataStr;
  3089. String splatStr;
  3090. if (dataAddr == -1)
  3091. splatStr = expr;
  3092. while ((summaryType != NULL) && (isMemoryValid))
  3093. {
  3094. if ((summaryType->IsTypedPrimitive())
  3095. //&& ((summaryType->mBaseTypes.IsEmpty()) || (!summaryType->mBaseTypes.front()->mBaseType->IsTypedPrimitive())))
  3096. )
  3097. {
  3098. if (formatInfo.mTotalSummaryLength + (int)displayString.length() > 255)
  3099. {
  3100. truncatedMemberList = true;
  3101. summaryDone = true;
  3102. bigRet += "...";
  3103. }
  3104. else
  3105. {
  3106. CeFormatInfo displayStrFormatInfo = formatInfo;
  3107. displayStrFormatInfo.mExpandItemDepth = 1;
  3108. displayStrFormatInfo.mTotalSummaryLength += (int)displayString.length();
  3109. displayStrFormatInfo.mHidePointers = false;
  3110. BfType* primType = summaryType->GetUnderlyingType();
  3111. String result;
  3112. if (primType->IsInteger())
  3113. formatInfo.mTypeKindFlags = (DbgTypeKindFlags)(formatInfo.mTypeKindFlags | DbgTypeKindFlag_Int);
  3114. if ((dataAddr != 0) && (dataAddr != -1))
  3115. {
  3116. //String evalString = "(" + primType->ToString() + ")" + ptrDataStr;
  3117. // BfTypedValue evalResult = EvaluateInContext(dbgCompileUnit, origTypedValue, evalString, &displayStrFormatInfo);
  3118. // if (evalResult)
  3119. // result = TypedValueToString(evalResult, evalString, displayStrFormatInfo, NULL);
  3120. }
  3121. else
  3122. {
  3123. // BfTypedValue evalResult = origTypedValue;
  3124. // evalResult.mType = primType;
  3125. // String evalString = "(" + primType->ToString() + ")" + expr;
  3126. // result = TypedValueToString(evalResult, evalString, displayStrFormatInfo, NULL);
  3127. }
  3128. if (formatInfo.mRawString)
  3129. return result;
  3130. int crPos = (int)result.IndexOf('\n');
  3131. if (crPos != -1)
  3132. result.RemoveToEnd(crPos);
  3133. if (memberIdx == 0)
  3134. firstRet = result;
  3135. bigRet += result;
  3136. memberIdx++;
  3137. }
  3138. }
  3139. auto summaryTypeInst = summaryType->ToTypeInstance();
  3140. if (summaryTypeInst == NULL)
  3141. break;
  3142. module->PopulateType(summaryTypeInst);
  3143. for (auto& fieldInst : summaryTypeInst->mFieldInstances)
  3144. {
  3145. auto fieldDef = fieldInst.GetFieldDef();
  3146. if (fieldDef == NULL)
  3147. continue;
  3148. if (fieldInst.mResolvedType == NULL)
  3149. continue;
  3150. if (!fieldDef->mIsStatic)
  3151. {
  3152. if (formatInfo.mTotalSummaryLength + retVal.length() + bigRet.length() > 255)
  3153. {
  3154. truncatedMemberList = true;
  3155. summaryDone = true;
  3156. bigRet += "...";
  3157. break;
  3158. }
  3159. //if (fieldDef->mName != NULL)
  3160. {
  3161. // if (member->mName[0] == '$')
  3162. // continue;
  3163. if (!isdigit(fieldDef->mName[0]))
  3164. {
  3165. if (memberIdx != 0)
  3166. bigRet += isTuple ? ", " : " ";
  3167. if ((!isTuple) || (fieldDef->mName[0] != '_'))
  3168. {
  3169. bigRet += String(fieldDef->mName);
  3170. bigRet += isTuple ? ":" : "=";
  3171. }
  3172. }
  3173. else
  3174. {
  3175. if (memberIdx != 0)
  3176. bigRet += ", ";
  3177. }
  3178. CeFormatInfo displayStrFormatInfo = formatInfo;
  3179. displayStrFormatInfo.mExpandItemDepth = 1;
  3180. displayStrFormatInfo.mHidePointers = false;
  3181. displayStrFormatInfo.mTotalSummaryLength = (int)(formatInfo.mTotalSummaryLength + retVal.length() + bigRet.length());
  3182. // String evalString;
  3183. // if (dataPtr != -1)
  3184. // {
  3185. // if ((fieldDef->mName[0] >= '0') && (fieldDef->mName[0] <= '9'))
  3186. // evalString += "this.";
  3187. // evalString += String(member->mName); // +", this=" + summaryDataStr;
  3188. // }
  3189. // else
  3190. // {
  3191. // evalString = "(";
  3192. // evalString += splatStr;
  3193. // evalString += ").";
  3194. // evalString += fieldDef->mName;
  3195. // }
  3196. String referenceId;
  3197. String result;
  3198. if (!fieldInst.mResolvedType->IsValuelessType())
  3199. {
  3200. auto addrVal = dataAddr + fieldInst.mDataOffset;
  3201. BfTypedValue evalResult;
  3202. if (fieldInst.mResolvedType->IsObjectOrInterface())
  3203. {
  3204. auto typeInst = fieldInst.mResolvedType->ToTypeInstance();
  3205. evalResult = BfTypedValue(module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapTypeInst(typeInst), (addr_ce)addrVal), typeInst, true);
  3206. }
  3207. else
  3208. {
  3209. evalResult = BfTypedValue(module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapType(fieldInst.mResolvedType), (addr_ce)addrVal), fieldInst.mResolvedType, true);
  3210. }
  3211. //BfTypedValue evalResult = EvaluateInContext(dbgCompileUnit, summaryTypedValue, evalString, &displayStrFormatInfo, &referenceId);
  3212. if (evalResult)
  3213. {
  3214. displayStrFormatInfo.mReferenceId = referenceId;
  3215. result = TypedValueToString(evalResult, "", displayStrFormatInfo, NULL);
  3216. int crPos = (int)result.IndexOf('\n');
  3217. if (crPos != -1)
  3218. result.RemoveToEnd(crPos);
  3219. }
  3220. else
  3221. result = "???";
  3222. }
  3223. if (fieldInst.mResolvedType->IsInteger())
  3224. formatInfo.mTypeKindFlags = (DbgTypeKindFlags)(formatInfo.mTypeKindFlags | DbgTypeKindFlag_Int);
  3225. if (formatInfo.mRawString)
  3226. return result;
  3227. if (memberIdx == 0)
  3228. firstRet = result;
  3229. bigRet += result;
  3230. //formatInfo.mEmbeddedDisplayCount = displayStrFormatInfo.mEmbeddedDisplayCount;
  3231. memberIdx++;
  3232. }
  3233. }
  3234. }
  3235. if (truncatedMemberList)
  3236. break;
  3237. // Find first base class with members
  3238. BfType* nextSummaryType = summaryTypeInst->mBaseType;
  3239. summaryType = nextSummaryType;
  3240. if ((summaryType == NULL) || (summaryType == module->mContext->mBfObjectType))
  3241. break;
  3242. // If we don't have many members then find a base class with some members to show
  3243. if ((memberIdx != 0) && (displayString.length() >= 255))
  3244. {
  3245. truncatedMemberList = true;
  3246. bigRet += "...";
  3247. break;
  3248. }
  3249. }
  3250. bigRet += isTuple ? ")" : " }";
  3251. if (displayString.length() > 0)
  3252. displayString += " ";
  3253. if ((memberIdx == 1) && (!truncatedMemberList) && (firstRet.IndexOf('{') == -1) && (!isTuple))
  3254. displayString += "{ " + firstRet + " }";
  3255. else
  3256. displayString += bigRet;
  3257. }
  3258. retVal += displayString;
  3259. retVal += "\n" + TypeToString(origTypedValue.mType, typeModKind);
  3260. BfType* memberListType = displayType;
  3261. if ((actualType != NULL) && (actualType != displayType))
  3262. {
  3263. if (displayType == module->mContext->mBfObjectType)
  3264. {
  3265. memberListType = actualType;
  3266. }
  3267. else
  3268. {
  3269. String actualTypeName = module->TypeToString(actualType);
  3270. retVal += StrFormat(" {%s}\n[%s]\tthis,this=%d@0x%X", actualTypeName.c_str(), actualTypeName.c_str(), actualType->mTypeId, addr);
  3271. }
  3272. }
  3273. if (formatInfo.mNoMembers)
  3274. {
  3275. //
  3276. }
  3277. else if (wantsCustomExpandedItems)
  3278. {
  3279. HandleCustomExpandedItems(retVal, debugVis, summaryTypedValue, addr, dataAddr, dbgVisWildcardCaptures, formatInfo);
  3280. }
  3281. else if ((!isNull) && (!isBadSrc))
  3282. {
  3283. retVal += "\n" + GetMemberList(memberListType, addr, dataAddr, false);
  3284. }
  3285. if ((origTypedValue.mType->IsObjectOrInterface()) || (origTypedValue.mType->IsPointer()))
  3286. {
  3287. retVal += "\n:editVal\t" + EncodeDataPtr((uint32)dataAddr, true);
  3288. }
  3289. return retVal;
  3290. }
  3291. return "!Failed to display value";
  3292. }
  3293. void CeDebugger::HandleCustomExpandedItems(String& retVal, DebugVisualizerEntry* debugVis, BfTypedValue typedValue, addr_ce addr, addr_ce addrInst, Array<String>& dbgVisWildcardCaptures, CeFormatInfo& formatInfo)
  3294. {
  3295. auto debugVisualizers = mDebugManager->mDebugVisualizers;
  3296. auto ceModule = mCeMachine->mCeModule;
  3297. if (formatInfo.mExpandItemDepth > 10) // Avoid crashing on circular ExpandItems
  3298. return;
  3299. bool isReadOnly = false;
  3300. // if (useTypedValue.mIsReadOnly)
  3301. // isReadOnly = true;
  3302. String ptrUseDataStr = StrFormat("%d@0x%X", typedValue.mType->mTypeId, addrInst);
  3303. for (auto entry : debugVis->mExpandItems)
  3304. {
  3305. if (!entry->mCondition.empty())
  3306. {
  3307. String error;
  3308. if (!EvalCondition(debugVis, typedValue, formatInfo, entry->mCondition, dbgVisWildcardCaptures, error))
  3309. {
  3310. if (!error.empty())
  3311. retVal += "\n" + entry->mName + "\t@!<DbgVis Failed>@!";
  3312. continue;
  3313. }
  3314. }
  3315. String replacedStr = debugVisualizers->DoStringReplace(entry->mValue, dbgVisWildcardCaptures);
  3316. retVal += "\n" + entry->mName + "\t" + replacedStr + ", this=" + ptrUseDataStr;
  3317. }
  3318. String referenceId = ceModule->TypeToString(typedValue.mType);
  3319. if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_ExpandedItem)
  3320. {
  3321. BfTypedValue itemValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures), &formatInfo);
  3322. if (itemValue)
  3323. {
  3324. CeFormatInfo itemFormatInfo = formatInfo;
  3325. itemFormatInfo.mExpandItemDepth++;
  3326. String itemRetVal = TypedValueToString(itemValue, "", itemFormatInfo, NULL);
  3327. int crIdx = (int)itemRetVal.IndexOf('\n');
  3328. if (crIdx != -1)
  3329. {
  3330. crIdx = (int)itemRetVal.IndexOf('\n', crIdx + 1);
  3331. if (crIdx != -1)
  3332. retVal += itemRetVal.Substring(crIdx);
  3333. }
  3334. }
  3335. }
  3336. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_Array)
  3337. {
  3338. BfTypedValue sizeValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mSize, dbgVisWildcardCaptures), &formatInfo);
  3339. Array<int> lowerDimSizes;
  3340. for (auto lowerDim : debugVis->mLowerDimSizes)
  3341. {
  3342. BfTypedValue lowerDimValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(lowerDim, dbgVisWildcardCaptures), &formatInfo);
  3343. int dimSize = 0;
  3344. if ((lowerDimValue) && (lowerDimValue.mType->IsInteger()))
  3345. dimSize = (int)ValueToInt(lowerDimValue);
  3346. dimSize = BF_MAX(dimSize, 1);
  3347. lowerDimSizes.push_back(dimSize);
  3348. }
  3349. if ((sizeValue) && (sizeValue.mType->IsInteger()) && (ValueToInt(sizeValue) > 0))
  3350. {
  3351. if (!debugVis->mCondition.IsEmpty())
  3352. {
  3353. int size = (int)ValueToInt(sizeValue);
  3354. BfTypedValue headPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures), &formatInfo);
  3355. BfTypedValue curNode = headPointer;
  3356. Array<addr_ce> parentList;
  3357. String continuationData;
  3358. int totalSize = 2;
  3359. auto valueType = headPointer.mType;
  3360. String addrs = GetArrayItems(debugVis, valueType, headPointer, totalSize, &continuationData);
  3361. String firstAddr;
  3362. String secondAddr;
  3363. bool hasSecondAddr = valueType == NULL;
  3364. if (addrs.length() > 0)
  3365. {
  3366. const char* addrsPtr = addrs.c_str();
  3367. firstAddr = addrs.Substring(0, sizeof(addr_ce) * 2);
  3368. if (hasSecondAddr)
  3369. secondAddr = addrs.Substring(sizeof(addr_ce) * 2, sizeof(addr_ce) * 2);
  3370. }
  3371. String evalStr;
  3372. if (valueType != NULL)
  3373. {
  3374. auto ptrType = valueType;
  3375. if (!valueType->IsPointer())
  3376. ptrType = ceModule->CreatePointerType(valueType);
  3377. evalStr = StrFormat("(comptype(%d)", ptrType->mTypeId);
  3378. evalStr += ")(void*)0x{1}";
  3379. }
  3380. else
  3381. {
  3382. evalStr += "({1})(void*)0x{2}";
  3383. }
  3384. if (!debugVis->mShowElementAddrs)
  3385. evalStr.Insert(0, "*");
  3386. if (addrs.length() > 0)
  3387. {
  3388. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3389. if (isReadOnly)
  3390. evalStr += ", ne";
  3391. retVal += "\n:repeat" + StrFormat("\t%d\t%d\t%d", 0, BF_MAX(size, 0), 10000) +
  3392. "\t[{0}]\t" + evalStr + "\t" + firstAddr;
  3393. if (hasSecondAddr)
  3394. retVal += "\t" + secondAddr;
  3395. if (size != 0)
  3396. {
  3397. retVal += "\n:addrs\t" + addrs;
  3398. if (valueType == NULL)
  3399. retVal += "\n:addrsEntrySize\t2";
  3400. if (continuationData.length() > 0)
  3401. retVal += "\n:continuation\t" + continuationData;
  3402. }
  3403. }
  3404. }
  3405. else if (lowerDimSizes.size() == 1)
  3406. {
  3407. int dimSize1 = lowerDimSizes[0];
  3408. String evalStr = "(" + debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures) +
  3409. StrFormat(" + {0} * %d), arraysize=%d, na, this=", dimSize1, dimSize1) + ptrUseDataStr;
  3410. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3411. if (isReadOnly)
  3412. evalStr += ", ne";
  3413. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, ValueToInt(sizeValue) / dimSize1, 50000) +
  3414. "\t[{0}]\t" + evalStr;
  3415. }
  3416. else if (lowerDimSizes.size() == 2)
  3417. {
  3418. int dimSize1 = lowerDimSizes[0];
  3419. int dimSize2 = lowerDimSizes[1];
  3420. BfTypedValue headPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures), &formatInfo);
  3421. if ((headPointer.mType != NULL) && (headPointer.mType->IsPointer()))
  3422. {
  3423. String evalStr = StrFormat("((%s[%d]*)", ceModule->TypeToString(headPointer.mType->GetUnderlyingType()).c_str(), dimSize2) + debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures) +
  3424. StrFormat(" + {0} * %d), arraysize=%d, na, this=", dimSize1, dimSize1) + ptrUseDataStr;
  3425. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3426. if (isReadOnly)
  3427. evalStr += ", ne";
  3428. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, ValueToInt(sizeValue) / dimSize1 / dimSize2, 50000) +
  3429. "\t[{0}]\t" + evalStr;
  3430. }
  3431. }
  3432. else if (lowerDimSizes.size() == 3)
  3433. {
  3434. int dimSize1 = lowerDimSizes[0];
  3435. int dimSize2 = lowerDimSizes[1];
  3436. int dimSize3 = lowerDimSizes[2];
  3437. BfTypedValue headPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures), &formatInfo);
  3438. if ((headPointer.mType != NULL) && (headPointer.mType->IsPointer()))
  3439. {
  3440. String evalStr = StrFormat("((%s[%d][%d]*)", ceModule->TypeToString(headPointer.mType->GetUnderlyingType()).c_str(), dimSize2, dimSize3) + debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures) +
  3441. StrFormat(" + {0} * %d), arraysize=%d, na, this=", dimSize1, dimSize1) + ptrUseDataStr;
  3442. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3443. if (isReadOnly)
  3444. evalStr += ", ne";
  3445. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, ValueToInt(sizeValue) / dimSize1 / dimSize2 / dimSize3, 50000) +
  3446. "\t[{0}]\t" + evalStr;
  3447. }
  3448. }
  3449. else
  3450. {
  3451. String evalStr = "*(" + debugVisualizers->DoStringReplace(debugVis->mValuePointer, dbgVisWildcardCaptures) + " + {0}), this=" + ptrUseDataStr;
  3452. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3453. if (isReadOnly)
  3454. evalStr += ", ne";
  3455. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, ValueToInt(sizeValue), 50000) +
  3456. "\t[{0}]\t" + evalStr;
  3457. }
  3458. }
  3459. }
  3460. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_IndexItems)
  3461. {
  3462. BfTypedValue sizeValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mSize, dbgVisWildcardCaptures), &formatInfo);
  3463. if ((sizeValue) && (sizeValue.mType->IsInteger()) && (ValueToInt(sizeValue) > 0))
  3464. {
  3465. String evalStr = debugVis->mValuePointer + ", this=" + ptrUseDataStr;
  3466. evalStr.Replace("$i", "{0}");
  3467. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3468. if (isReadOnly)
  3469. evalStr += ", ne";
  3470. retVal += "\n:repeat" + StrFormat("\t%d\t%lld\t%d", 0, ValueToInt(sizeValue), 50000) +
  3471. "\t[{0}]\t" + evalStr;
  3472. }
  3473. }
  3474. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_LinkedList)
  3475. {
  3476. BfType* valueType = NULL;
  3477. if (!debugVis->mValueType.empty())
  3478. {
  3479. valueType = FindType(debugVisualizers->DoStringReplace(debugVis->mValueType, dbgVisWildcardCaptures));
  3480. }
  3481. BfTypedValue headPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mHeadPointer, dbgVisWildcardCaptures), &formatInfo);
  3482. if (headPointer)
  3483. {
  3484. BfTypedValue endPointer;
  3485. if (!debugVis->mEndPointer.empty())
  3486. endPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mEndPointer, dbgVisWildcardCaptures), &formatInfo);
  3487. BfTypedValue nextPointer = EvaluateInContext(headPointer, debugVisualizers->DoStringReplace(debugVis->mNextPointer, dbgVisWildcardCaptures), &formatInfo);
  3488. int size = -1;
  3489. if (!debugVis->mSize.empty())
  3490. {
  3491. auto sizeValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mSize, dbgVisWildcardCaptures), &formatInfo);
  3492. if (sizeValue)
  3493. size = (int)ValueToInt(sizeValue);
  3494. }
  3495. auto ceEndPointerVal = GetAddr(endPointer);
  3496. BfTypedValue curNode = headPointer;
  3497. Array<addr_ce> parentList;
  3498. String continuationData;
  3499. int totalSize = 2;
  3500. String addrs = GetLinkedListItems(debugVis, (addr_ce)ceEndPointerVal.mAddr, valueType, curNode, totalSize, &continuationData);
  3501. String firstAddr;
  3502. String secondAddr;
  3503. bool hasSecondAddr = valueType == NULL;
  3504. if (addrs.length() > 0)
  3505. {
  3506. const char* addrsPtr = addrs.c_str();
  3507. firstAddr = addrs.Substring(0, sizeof(addr_ce) * 2);
  3508. if (hasSecondAddr)
  3509. secondAddr = addrs.Substring(sizeof(addr_ce) * 2, sizeof(addr_ce) * 2);
  3510. }
  3511. String evalStr;
  3512. if (valueType != NULL)
  3513. {
  3514. auto ptrType = valueType;
  3515. if (!valueType->IsPointer())
  3516. ptrType = ceModule->CreatePointerType(valueType);
  3517. evalStr = StrFormat("(comptype(%d)", ptrType->mTypeId);
  3518. evalStr += ")0x{1}";
  3519. }
  3520. else
  3521. {
  3522. evalStr += "({1})0x{2}";
  3523. }
  3524. if (!debugVis->mShowElementAddrs)
  3525. evalStr.Insert(0, "*");
  3526. if (addrs.length() > 0)
  3527. {
  3528. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3529. if (isReadOnly)
  3530. evalStr += ", ne";
  3531. retVal += "\n:repeat" + StrFormat("\t%d\t%d\t%d", 0, size, 10000) +
  3532. "\t[{0}]\t" + evalStr + "\t" + firstAddr;
  3533. if (hasSecondAddr)
  3534. retVal += "\t" + secondAddr;
  3535. if (size != 0)
  3536. {
  3537. retVal += "\n:addrs\t" + addrs;
  3538. if (valueType == NULL)
  3539. retVal += "\n:addrsEntrySize\t2";
  3540. if (continuationData.length() > 0)
  3541. retVal += "\n:continuation\t" + continuationData;
  3542. }
  3543. }
  3544. }
  3545. }
  3546. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_TreeItems)
  3547. {
  3548. BfType* valueType = NULL;
  3549. if (!debugVis->mValueType.empty())
  3550. {
  3551. valueType = FindType(debugVisualizers->DoStringReplace(debugVis->mValueType, dbgVisWildcardCaptures));
  3552. }
  3553. BfTypedValue sizeValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mSize, dbgVisWildcardCaptures), &formatInfo);
  3554. BfTypedValue headPointer = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mHeadPointer, dbgVisWildcardCaptures), &formatInfo);
  3555. if ((sizeValue) && (headPointer) && (sizeValue.mType->IsInteger()) && (ValueToInt(sizeValue) > 0))
  3556. {
  3557. BfTypedValue curNode = headPointer;
  3558. Array<addr_ce> parentList;
  3559. String continuationData;
  3560. int getItemCount = (int)BF_MIN(ValueToInt(sizeValue), 32LL);
  3561. String addrs = GetTreeItems(debugVis, parentList, valueType, curNode, getItemCount, &continuationData);
  3562. addr_ce firstAddr = 0;
  3563. addr_ce secondAddr = 0;
  3564. bool hasSecondAddr = valueType == NULL;
  3565. if (addrs.length() > 0)
  3566. {
  3567. const char* addrsPtr = addrs.c_str();
  3568. firstAddr = DecodeTargetDataPtr(addrsPtr);
  3569. if (hasSecondAddr)
  3570. secondAddr = DecodeTargetDataPtr(addrsPtr);
  3571. }
  3572. String evalStr;
  3573. if (valueType != NULL)
  3574. {
  3575. auto ptrType = valueType;
  3576. if (!valueType->IsPointer())
  3577. ptrType = ceModule->CreatePointerType(valueType);
  3578. evalStr = StrFormat("(comptype(%d)", ptrType->mTypeId);
  3579. evalStr += ")0x{1}";
  3580. }
  3581. else
  3582. {
  3583. evalStr += "*(_T_{1}*)0x{2}";
  3584. }
  3585. int size = (int)ValueToInt(sizeValue);
  3586. if (addrs.length() == 0)
  3587. {
  3588. evalStr = ""; // Failed
  3589. }
  3590. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3591. if (isReadOnly)
  3592. evalStr += ", ne";
  3593. retVal += "\n:repeat" + StrFormat("\t%d\t%d\t%d", 0, size, 10000) +
  3594. "\t[{0}]\t" + evalStr + "\t" + EncodeDataPtr(firstAddr, false);
  3595. if (hasSecondAddr)
  3596. retVal += "\t" + EncodeDataPtr(secondAddr, false);
  3597. if (addrs.length() > 0)
  3598. {
  3599. retVal += "\n:addrs\t" + addrs;
  3600. if (continuationData.length() > 0)
  3601. retVal += "\n:continuation\t" + continuationData;
  3602. }
  3603. }
  3604. }
  3605. else if (debugVis->mCollectionType == DebugVisualizerEntry::CollectionType_Dictionary)
  3606. {
  3607. BfTypedValue sizeValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mSize, dbgVisWildcardCaptures), &formatInfo);
  3608. BfTypedValue entriesPtrValue = EvaluateInContext(typedValue, debugVisualizers->DoStringReplace(debugVis->mEntries, dbgVisWildcardCaptures), &formatInfo);
  3609. if ((sizeValue) && (entriesPtrValue) && (sizeValue.mType->IsInteger()) && (ValueToInt(sizeValue) > 0))
  3610. {
  3611. String continuationData;
  3612. BfType* valueType = entriesPtrValue.mType;
  3613. int getItemCount = (int)BF_MIN(ValueToInt(sizeValue), 2LL);
  3614. BfType* useTypedValType = typedValue.mType;
  3615. String addrs = GetDictionaryItems(debugVis, typedValue, 0, -1, getItemCount, &continuationData);
  3616. addr_ce firstAddr = 0;
  3617. if (addrs.length() > 0)
  3618. {
  3619. const char* addrsPtr = addrs.c_str();
  3620. firstAddr = DecodeTargetDataPtr(addrsPtr);
  3621. }
  3622. String evalStr = "((comptype(" + StrFormat("%d", valueType->mTypeId) + "))(void*)0x{1}), na";
  3623. evalStr += ", refid=\"" + referenceId + ".[]\"";
  3624. if (isReadOnly)
  3625. evalStr += ", ne";
  3626. retVal += "\n:repeat" + StrFormat("\t%d\t%d\t%d", 0, (int)ValueToInt(sizeValue), 10000) +
  3627. "\t[{0}]\t" + evalStr + "\t" + EncodeDataPtr(firstAddr, false);
  3628. if (addrs.length() > 0)
  3629. {
  3630. retVal += "\n:addrs\t" + addrs;
  3631. if (continuationData.length() > 0)
  3632. retVal += "\n:continuation\t" + continuationData;
  3633. }
  3634. }
  3635. }
  3636. if (formatInfo.mExpandItemDepth == 0)
  3637. {
  3638. retVal += "\n[Raw View]\tthis,this=" + ptrUseDataStr + ", nv";
  3639. }
  3640. }
  3641. String CeDebugger::Evaluate(const StringImpl& expr, int callStackIdx, int cursorPos, int language, DwEvalExpressionFlags expressionFlags)
  3642. {
  3643. CeFormatInfo formatInfo;
  3644. return Evaluate(expr, formatInfo, callStackIdx, cursorPos, language, expressionFlags);
  3645. }
  3646. String CeDebugger::EvaluateContinue()
  3647. {
  3648. AutoCrit autoCrit(mDebugManager->mCritSect);
  3649. if (mDebugPendingExpr == NULL)
  3650. return "";
  3651. if (!mDebugPendingExpr->mDone)
  3652. return "!pending";
  3653. String result = mDebugPendingExpr->mResult;
  3654. delete mDebugPendingExpr;
  3655. mDebugPendingExpr = NULL;
  3656. return result;
  3657. }
  3658. void CeDebugger::EvaluateContinueKeep()
  3659. {
  3660. }
  3661. String CeDebugger::EvaluateToAddress(const StringImpl& expr, int callStackIdx, int cursorPos)
  3662. {
  3663. return String();
  3664. }
  3665. String CeDebugger::EvaluateAtAddress(const StringImpl& expr, intptr atAddr, int cursorPos)
  3666. {
  3667. return String();
  3668. }
  3669. String CeDebugger::GetAutoExpressions(int callStackIdx, uint64 memoryRangeStart, uint64 memoryRangeLen)
  3670. {
  3671. return String();
  3672. }
  3673. String CeDebugger::GetAutoLocals(int callStackIdx, bool showRegs)
  3674. {
  3675. AutoCrit autoCrit(mDebugManager->mCritSect);
  3676. if (!mCeMachine->mDbgPaused)
  3677. return "";
  3678. String result;
  3679. auto ceFrame = GetFrame(callStackIdx);
  3680. if (ceFrame == NULL)
  3681. return result;
  3682. int scopeIdx = -1;
  3683. auto ceEntry = ceFrame->mFunction->FindEmitEntry(ceFrame->GetInstIdx());
  3684. if (ceEntry != NULL)
  3685. scopeIdx = ceEntry->mScope;
  3686. int instIdx = ceFrame->GetInstIdx();
  3687. if (ceFrame->mFunction->mDbgInfo != NULL)
  3688. {
  3689. auto ceFunction = ceFrame->mFunction;
  3690. struct CeDbgInfo
  3691. {
  3692. int mShadowCount;
  3693. int mPrevShadowIdx;
  3694. bool mIncluded;
  3695. CeDbgInfo()
  3696. {
  3697. mShadowCount = 0;
  3698. mPrevShadowIdx = -1;
  3699. mIncluded = true;
  3700. }
  3701. };
  3702. Array<CeDbgInfo> dbgInfo;
  3703. Dictionary<String, int> nameIndices;
  3704. dbgInfo.Resize(ceFunction->mDbgInfo->mVariables.mSize);
  3705. for (int i = 0; i < ceFunction->mDbgInfo->mVariables.mSize; i++)
  3706. {
  3707. auto& dbgVar = ceFunction->mDbgInfo->mVariables[i];
  3708. if ((dbgVar.mScope == scopeIdx) && (instIdx >= dbgVar.mStartCodePos) && (instIdx < dbgVar.mEndCodePos))
  3709. {
  3710. int* idxPtr = NULL;
  3711. if (!nameIndices.TryAdd(dbgVar.mName, NULL, &idxPtr))
  3712. {
  3713. int checkIdx = *idxPtr;
  3714. dbgInfo[i].mPrevShadowIdx = checkIdx;
  3715. while (checkIdx != -1)
  3716. {
  3717. dbgInfo[checkIdx].mShadowCount++;
  3718. checkIdx = dbgInfo[checkIdx].mPrevShadowIdx;
  3719. }
  3720. }
  3721. *idxPtr = i;
  3722. }
  3723. else
  3724. {
  3725. dbgInfo[i].mIncluded = false;
  3726. }
  3727. }
  3728. for (int i = 0; i < ceFunction->mDbgInfo->mVariables.mSize; i++)
  3729. {
  3730. auto& dbgVar = ceFunction->mDbgInfo->mVariables[i];
  3731. if (dbgInfo[i].mIncluded)
  3732. {
  3733. for (int shadowIdx = 0; shadowIdx < dbgInfo[i].mShadowCount; shadowIdx++)
  3734. result += "@";
  3735. result += dbgVar.mName;
  3736. result += "\n";
  3737. }
  3738. }
  3739. }
  3740. return result;
  3741. }
  3742. String CeDebugger::CompactChildExpression(const StringImpl& expr, const StringImpl& parentExpr, int callStackIdx)
  3743. {
  3744. return String();
  3745. }
  3746. String CeDebugger::GetProcessInfo()
  3747. {
  3748. return String();
  3749. }
  3750. int CeDebugger::GetProcessId()
  3751. {
  3752. return 0;
  3753. }
  3754. DebugVisualizerEntry* CeDebugger::FindVisualizerForType(BfType* dbgType, Array<String>* wildcardCaptures)
  3755. {
  3756. auto ceModule = mCeMachine->mCeModule;
  3757. ceModule->PopulateType(dbgType);
  3758. auto entry = mDebugManager->mDebugVisualizers->FindEntryForType(ceModule->TypeToString(dbgType), DbgFlavor_Unknown, wildcardCaptures);
  3759. if (entry == NULL)
  3760. {
  3761. auto typeInst = dbgType->ToTypeInstance();
  3762. if ((typeInst != NULL) && (typeInst->mBaseType != NULL))
  3763. entry = FindVisualizerForType(typeInst->mBaseType, wildcardCaptures);
  3764. }
  3765. return entry;
  3766. }
  3767. String CeDebugger::GetThreadInfo()
  3768. {
  3769. return String();
  3770. }
  3771. void CeDebugger::SetActiveThread(int threadId)
  3772. {
  3773. }
  3774. int CeDebugger::GetActiveThread()
  3775. {
  3776. return 0;
  3777. }
  3778. void CeDebugger::FreezeThread(int threadId)
  3779. {
  3780. }
  3781. void CeDebugger::ThawThread(int threadId)
  3782. {
  3783. }
  3784. bool CeDebugger::IsActiveThreadWaiting()
  3785. {
  3786. return false;
  3787. }
  3788. void CeDebugger::ClearCallStack()
  3789. {
  3790. AutoCrit autoCrit(mCeMachine->mCritSect);
  3791. mDbgCallStack.Clear();
  3792. }
  3793. void CeDebugger::UpdateCallStack(bool slowEarlyOut)
  3794. {
  3795. AutoCrit autoCrit(mCeMachine->mCritSect);
  3796. if (!mDbgCallStack.IsEmpty())
  3797. return;
  3798. auto ceContext = mCeMachine->mCurContext;
  3799. for (int frameIdx = ceContext->mCallStack.mSize - 1; frameIdx >= 0; frameIdx--)
  3800. {
  3801. auto ceFrame = &ceContext->mCallStack[frameIdx];
  3802. auto instIdx = ceFrame->GetInstIdx();
  3803. auto emitEntry = ceFrame->mFunction->FindEmitEntry(instIdx);
  3804. if (emitEntry == NULL)
  3805. continue;
  3806. int scopeIdx = emitEntry->mScope;
  3807. int prevInlineIdx = -1;
  3808. while (scopeIdx != -1)
  3809. {
  3810. CeDbgStackInfo ceDbgStackInfo;
  3811. ceDbgStackInfo.mFrameIdx = frameIdx;
  3812. ceDbgStackInfo.mScopeIdx = scopeIdx;
  3813. ceDbgStackInfo.mInlinedFrom = prevInlineIdx;
  3814. mDbgCallStack.Add(ceDbgStackInfo);
  3815. auto ceScope = &ceFrame->mFunction->mDbgScopes[scopeIdx];
  3816. if (ceScope->mInlinedAt == -1)
  3817. break;
  3818. auto inlineInfo = &ceFrame->mFunction->mDbgInlineTable[ceScope->mInlinedAt];
  3819. scopeIdx = inlineInfo->mScope;
  3820. prevInlineIdx = ceScope->mInlinedAt;
  3821. }
  3822. }
  3823. CeDbgStackInfo ceDbgStackInfo;
  3824. ceDbgStackInfo.mFrameIdx = -1;
  3825. ceDbgStackInfo.mScopeIdx = -1;
  3826. ceDbgStackInfo.mInlinedFrom = -1;
  3827. mDbgCallStack.Add(ceDbgStackInfo);
  3828. }
  3829. int CeDebugger::GetCallStackCount()
  3830. {
  3831. AutoCrit autoCrit(mCeMachine->mCritSect);
  3832. if (!mCeMachine->mDbgPaused)
  3833. return 0;
  3834. UpdateCallStack();
  3835. return mDbgCallStack.mSize;
  3836. }
  3837. int CeDebugger::GetRequestedStackFrameIdx()
  3838. {
  3839. return mPendingActiveFrameOffset;
  3840. }
  3841. int CeDebugger::GetBreakStackFrameIdx()
  3842. {
  3843. return 0;
  3844. }
  3845. bool CeDebugger::ReadMemory(intptr address, uint64 length, void* dest, bool local)
  3846. {
  3847. auto ceContext = mCeMachine->mCurContext;
  3848. if (ceContext == NULL)
  3849. return false;
  3850. auto ptr = ceContext->GetMemoryPtr((addr_ce)address, (int32)length);
  3851. if (ptr == NULL)
  3852. return false;
  3853. memcpy(dest, ptr, length);
  3854. return true;
  3855. }
  3856. bool CeDebugger::WriteMemory(intptr address, void* src, uint64 length)
  3857. {
  3858. auto ceContext = mCeMachine->mCurContext;
  3859. if (ceContext == NULL)
  3860. return false;
  3861. auto ptr = ceContext->GetMemoryPtr((addr_ce)address, (int32)length);
  3862. if (ptr == NULL)
  3863. return false;
  3864. memcpy(ptr, src, length);
  3865. return true;
  3866. }
  3867. DbgMemoryFlags CeDebugger::GetMemoryFlags(intptr address)
  3868. {
  3869. return (DbgMemoryFlags)(DbgMemoryFlags_Read | DbgMemoryFlags_Write);
  3870. }
  3871. void CeDebugger::UpdateRegisterUsage(int stackFrameIdx)
  3872. {
  3873. }
  3874. void CeDebugger::UpdateCallStackMethod(int stackFrameIdx)
  3875. {
  3876. }
  3877. void CeDebugger::GetCodeAddrInfo(intptr addr, intptr inlineCallAddr, String* outFile, int* outHotIdx, int* outDefLineStart, int* outDefLineEnd, int* outLine, int* outColumn)
  3878. {
  3879. }
  3880. void CeDebugger::GetStackAllocInfo(intptr addr, int* outThreadId, int* outStackIdx)
  3881. {
  3882. AutoCrit autoCrit(mDebugManager->mCritSect);
  3883. *outThreadId = 0;
  3884. if (outStackIdx != NULL)
  3885. *outStackIdx = -1;
  3886. if (!mCeMachine->mDbgPaused)
  3887. return;
  3888. auto ceContext = mCeMachine->mCurContext;
  3889. for (int i = 0; i < (int)ceContext->mCallStack.mSize; i++)
  3890. {
  3891. auto ceFrame = &ceContext->mCallStack[i];
  3892. if ((addr_ce)addr > ceFrame->mFrameAddr)
  3893. {
  3894. if (outStackIdx != NULL)
  3895. *outStackIdx = (int)ceContext->mCallStack.mSize - i - 1;
  3896. }
  3897. }
  3898. }
  3899. String CeDebugger::GetStackFrameInfo(int stackFrameIdx, intptr* addr, String* outFile, int32* outHotIdx, int32* outDefLineStart, int32* outDefLineEnd, int32* outLine, int32* outColumn, int32* outLanguage, int32* outStackSize, int8* outFlags)
  3900. {
  3901. AutoCrit autoCrit(mDebugManager->mCritSect);
  3902. if (!mCeMachine->mDbgPaused)
  3903. return "";
  3904. UpdateCallStack();
  3905. enum FrameFlags
  3906. {
  3907. FrameFlags_Optimized = 1,
  3908. FrameFlags_HasPendingDebugInfo = 2,
  3909. FrameFlags_CanGetOldSource = 4,
  3910. FrameFlags_WasHotReplaced = 8,
  3911. FrameFlags_HadError = 0x10
  3912. };
  3913. auto ceContext = mCeMachine->mCurContext;
  3914. *addr = 0;
  3915. *outFile = "";
  3916. *outHotIdx = 0;
  3917. *outDefLineStart = -1;
  3918. *outDefLineEnd = -1;
  3919. *outLine = -1;
  3920. *outColumn = 0;
  3921. *outLanguage = DbgLanguage_Beef;
  3922. *outStackSize = 0;
  3923. *outFlags = 0;
  3924. if (ceContext == NULL)
  3925. return "";
  3926. auto& dbgCallstackInfo = mDbgCallStack[stackFrameIdx];
  3927. if (dbgCallstackInfo.mFrameIdx == -1)
  3928. {
  3929. if (ceContext->mCurCallSource != NULL)
  3930. {
  3931. int line = -1;
  3932. int lineChar = -1;
  3933. auto parserData = ceContext->mCurCallSource->mRefNode->GetParserData();
  3934. if (parserData != NULL)
  3935. {
  3936. parserData->GetLineCharAtIdx(ceContext->mCurCallSource->mRefNode->GetSrcStart(), line, lineChar);
  3937. *outLine = line;
  3938. *outColumn = lineChar;
  3939. *outFile = parserData->mFileName;
  3940. }
  3941. }
  3942. // Entry marker
  3943. String result = "const eval";
  3944. if (ceContext->mCurCallSource->mKind == CeCallSource::Kind_FieldInit)
  3945. {
  3946. return String("field init of : ") + ceContext->mCurModule->TypeToString(ceContext->mCurCallSource->mFieldInstance->mOwner) + "." +
  3947. ceContext->mCurCallSource->mFieldInstance->GetFieldDef()->mName;
  3948. }
  3949. else if (ceContext->mCurCallSource->mKind == CeCallSource::Kind_MethodInit)
  3950. {
  3951. return ("method init of : ") + ceContext->mCurModule->MethodToString(ceContext->mCallerMethodInstance);
  3952. }
  3953. else if (ceContext->mCurCallSource->mKind == CeCallSource::Kind_TypeInit)
  3954. result = "type init";
  3955. else if (ceContext->mCurCallSource->mKind == CeCallSource::Kind_TypeDone)
  3956. result = "type done";
  3957. if (ceContext->mCallerMethodInstance != NULL)
  3958. result += String(" in : ") + ceContext->mCurModule->MethodToString(ceContext->mCallerMethodInstance);
  3959. else if (ceContext->mCallerTypeInstance != NULL)
  3960. result += String(" in : ") + ceContext->mCurModule->TypeToString(ceContext->mCallerTypeInstance);
  3961. return result;
  3962. }
  3963. auto ceFrame = &ceContext->mCallStack[dbgCallstackInfo.mFrameIdx];
  3964. auto ceFunction = ceFrame->mFunction;
  3965. if (ceFunction->mFailed)
  3966. *outFlags |= FrameFlags_HadError;
  3967. int instIdx = (int)(ceFrame->mInstPtr - &ceFunction->mCode[0] - 2);
  3968. BF_ASSERT(ceFunction->mId != -1);
  3969. *addr = ((intptr)ceFunction->mId << 32) | instIdx;
  3970. CeEmitEntry* emitEntry = ceFunction->FindEmitEntry(instIdx);
  3971. *outStackSize = ceContext->mStackSize - ceFrame->mStackAddr;
  3972. if (stackFrameIdx < mDbgCallStack.mSize - 1)
  3973. {
  3974. auto& nextStackInfo = mDbgCallStack[stackFrameIdx + 1];
  3975. if (nextStackInfo.mFrameIdx >= 0)
  3976. {
  3977. auto prevFrame = &ceContext->mCallStack[nextStackInfo.mFrameIdx];
  3978. *outStackSize = prevFrame->mStackAddr - ceFrame->mStackAddr;
  3979. }
  3980. }
  3981. CeDbgScope* ceScope = NULL;
  3982. if (dbgCallstackInfo.mScopeIdx != -1)
  3983. ceScope = &ceFunction->mDbgScopes[dbgCallstackInfo.mScopeIdx];
  3984. if (emitEntry != NULL)
  3985. {
  3986. if (emitEntry->mScope != -1)
  3987. *outFile = ceFunction->mDbgScopes[emitEntry->mScope].mFilePath;
  3988. *outLine = emitEntry->mLine;
  3989. *outColumn = emitEntry->mColumn;
  3990. }
  3991. if (dbgCallstackInfo.mInlinedFrom != -1)
  3992. {
  3993. auto dbgInlineInfo = &ceFunction->mDbgInlineTable[dbgCallstackInfo.mInlinedFrom];
  3994. *outLine = dbgInlineInfo->mLine;
  3995. *outColumn = dbgInlineInfo->mColumn;
  3996. }
  3997. if ((ceScope != NULL) && (ceScope->mMethodVal != -1))
  3998. {
  3999. if ((ceScope->mMethodVal & CeDbgScope::MethodValFlag_MethodRef) != 0)
  4000. {
  4001. auto dbgMethodRef = &ceFunction->mDbgMethodRefTable[ceScope->mMethodVal & CeDbgScope::MethodValFlag_IdxMask];
  4002. return dbgMethodRef->ToString();
  4003. }
  4004. else
  4005. {
  4006. auto callTableEntry = &ceFunction->mCallTable[ceScope->mMethodVal];
  4007. return ceContext->mCurModule->MethodToString(callTableEntry->mFunctionInfo->mMethodInstance);
  4008. }
  4009. }
  4010. return ceContext->mCurModule->MethodToString(ceFrame->mFunction->mMethodInstance);
  4011. }
  4012. BfType* CeDebugger::FindType(const StringImpl& name)
  4013. {
  4014. if (name == "System.Object")
  4015. return mCeMachine->mCeModule->mContext->mBfObjectType;
  4016. BfParser parser(mCompiler->mSystem);
  4017. BfPassInstance passInstance(mCompiler->mSystem);
  4018. parser.SetSource(name.c_str(), (int)name.length());
  4019. parser.Parse(&passInstance);
  4020. BfReducer reducer;
  4021. reducer.mAlloc = parser.mAlloc;
  4022. reducer.mSystem = mCompiler->mSystem;
  4023. reducer.mPassInstance = &passInstance;
  4024. reducer.mVisitorPos = BfReducer::BfVisitorPos(parser.mRootNode);
  4025. reducer.mVisitorPos.MoveNext();
  4026. reducer.mSource = &parser;
  4027. auto typeRef = reducer.CreateTypeRef(parser.mRootNode->GetFirst());
  4028. parser.Close();
  4029. auto ceModule = mCeMachine->mCeModule;
  4030. SetAndRestoreValue<bool> prevIgnoreErrors(ceModule->mIgnoreErrors, true);
  4031. SetAndRestoreValue<bool> prevIgnoreWarning(ceModule->mIgnoreWarnings, true);
  4032. return ceModule->ResolveTypeRef(typeRef, {}, BfPopulateType_Declaration, BfResolveTypeRefFlag_IgnoreLookupError);
  4033. }
  4034. String CeDebugger::Callstack_GetStackFrameOldFileInfo(int stackFrameIdx)
  4035. {
  4036. return String();
  4037. }
  4038. #define CE_GET(T) *((T*)(ptr += sizeof(T)) - 1)
  4039. int CeDebugger::GetJmpState(int stackFrameIdx)
  4040. {
  4041. AutoCrit autoCrit(mDebugManager->mCritSect);
  4042. if (!mCeMachine->mDbgPaused)
  4043. return -1;
  4044. if (stackFrameIdx != 0)
  4045. return -1;
  4046. auto ceFrame = GetFrame(stackFrameIdx);
  4047. if (ceFrame == NULL)
  4048. return -1;
  4049. int instIdx = ceFrame->GetInstIdx();
  4050. auto ptr = &ceFrame->mFunction->mCode[instIdx];
  4051. auto op = CE_GET(CeOp);
  4052. switch (op)
  4053. {
  4054. case CeOp_Jmp:
  4055. return 1;
  4056. case CeOp_JmpIf:
  4057. case CeOp_JmpIfNot:
  4058. {
  4059. CE_GET(int32);
  4060. int frameOfs = CE_GET(int32);
  4061. bool willJump = *(bool*)(mCeMachine->mCurContext->mMemory.mVals + ceFrame->mFrameAddr + frameOfs);
  4062. if (op == CeOp_JmpIfNot)
  4063. willJump = !willJump;
  4064. return willJump ? 1 : 0;
  4065. }
  4066. }
  4067. return -1;
  4068. }
  4069. intptr CeDebugger::GetStackFrameCalleeAddr(int stackFrameIdx)
  4070. {
  4071. return intptr();
  4072. }
  4073. String CeDebugger::GetStackMethodOwner(int stackFrameIdx, int& language)
  4074. {
  4075. return String();
  4076. }
  4077. String CeDebugger::FindCodeAddresses(const StringImpl& fileName, int line, int column, bool allowAutoResolve)
  4078. {
  4079. return String();
  4080. }
  4081. String CeDebugger::GetAddressSourceLocation(intptr address)
  4082. {
  4083. return String();
  4084. }
  4085. String CeDebugger::GetAddressSymbolName(intptr address, bool demangle)
  4086. {
  4087. return String();
  4088. }
  4089. String CeDebugger::DisassembleAtRaw(intptr address)
  4090. {
  4091. return String();
  4092. }
  4093. String CeDebugger::DisassembleAt(intptr address)
  4094. {
  4095. AutoCrit autoCrit(mDebugManager->mCritSect);
  4096. if (!mCeMachine->mDbgPaused)
  4097. return "";
  4098. auto ceContext = mCeMachine->mCurContext;
  4099. mCurDisasmFuncId = (int)(address >> 32);
  4100. UpdateBreakpointAddrs();
  4101. CeFunction* ceFunction = NULL;
  4102. if (!mCeMachine->mFunctionIdMap.TryGetValue(mCurDisasmFuncId, &ceFunction))
  4103. return "";
  4104. CeDumpContext dumpCtx;
  4105. dumpCtx.mCeFunction = ceFunction;
  4106. dumpCtx.mStart = &ceFunction->mCode[0];
  4107. dumpCtx.mPtr = dumpCtx.mStart;
  4108. dumpCtx.mEnd = dumpCtx.mPtr + ceFunction->mCode.mSize;
  4109. uint8* start = dumpCtx.mStart;
  4110. dumpCtx.mStr += StrFormat("T Frame Size: %d\n", ceFunction->mFrameSize);
  4111. dumpCtx.mStr += StrFormat("A %llX\n", (intptr)mCurDisasmFuncId << 32);
  4112. int curEmitIdx = 0;
  4113. CeEmitEntry* prevEmitEntry = NULL;
  4114. CeEmitEntry* curEmitEntry = NULL;
  4115. String prevSourcePath;
  4116. while (dumpCtx.mPtr < dumpCtx.mEnd)
  4117. {
  4118. int ofs = (int)(dumpCtx.mPtr - start);
  4119. while ((curEmitIdx < ceFunction->mEmitTable.mSize - 1) && (ofs >= ceFunction->mEmitTable[curEmitIdx + 1].mCodePos))
  4120. curEmitIdx++;
  4121. if (curEmitIdx < ceFunction->mEmitTable.mSize)
  4122. curEmitEntry = &ceFunction->mEmitTable[curEmitIdx];
  4123. if (prevEmitEntry != curEmitEntry)
  4124. {
  4125. if ((curEmitEntry != NULL) && (curEmitEntry->mLine != -1))
  4126. {
  4127. bool pathChanged = false;
  4128. if ((prevEmitEntry == NULL) || (curEmitEntry->mScope != prevEmitEntry->mScope))
  4129. {
  4130. auto ceDbgScope = &ceFunction->mDbgScopes[curEmitEntry->mScope];
  4131. if (ceDbgScope->mFilePath != prevSourcePath)
  4132. {
  4133. pathChanged = true;
  4134. dumpCtx.mStr += StrFormat("S %s\n", ceDbgScope->mFilePath.c_str());
  4135. prevSourcePath = ceDbgScope->mFilePath;
  4136. }
  4137. }
  4138. if ((prevEmitEntry != NULL) && (!pathChanged) && (curEmitEntry->mLine >= prevEmitEntry->mLine))
  4139. {
  4140. dumpCtx.mStr += StrFormat("L %d %d\n", prevEmitEntry->mLine + 1, curEmitEntry->mLine - prevEmitEntry->mLine);
  4141. }
  4142. else
  4143. {
  4144. int startLine = BF_MAX(0, curEmitEntry->mLine - 5);
  4145. dumpCtx.mStr += StrFormat("L %d %d\n", startLine, curEmitEntry->mLine - startLine + 1);
  4146. }
  4147. prevEmitEntry = curEmitEntry;
  4148. }
  4149. }
  4150. dumpCtx.mStr += StrFormat("D %04X: ", ofs);
  4151. dumpCtx.Next();
  4152. dumpCtx.mStr += "\n";
  4153. if (dumpCtx.mJmp != -1)
  4154. {
  4155. dumpCtx.mStr += StrFormat("J %X\n", dumpCtx.mJmp);
  4156. dumpCtx.mJmp = -1;
  4157. }
  4158. }
  4159. return dumpCtx.mStr;
  4160. }
  4161. String CeDebugger::FindLineCallAddresses(intptr address)
  4162. {
  4163. return String();
  4164. }
  4165. String CeDebugger::GetCurrentException()
  4166. {
  4167. return String();
  4168. }
  4169. String CeDebugger::GetModulesInfo()
  4170. {
  4171. return String();
  4172. }
  4173. void CeDebugger::SetAliasPath(const StringImpl& origPath, const StringImpl& localPath)
  4174. {
  4175. }
  4176. void CeDebugger::CancelSymSrv()
  4177. {
  4178. }
  4179. bool CeDebugger::HasPendingDebugLoads()
  4180. {
  4181. return false;
  4182. }
  4183. int CeDebugger::LoadImageForModule(const StringImpl& moduleName, const StringImpl& debugFileName)
  4184. {
  4185. return 0;
  4186. }
  4187. int CeDebugger::LoadDebugInfoForModule(const StringImpl& moduleName)
  4188. {
  4189. return 0;
  4190. }
  4191. int CeDebugger::LoadDebugInfoForModule(const StringImpl& moduleName, const StringImpl& debugFileName)
  4192. {
  4193. return 0;
  4194. }
  4195. void CeDebugger::StopDebugging()
  4196. {
  4197. mRunState = RunState_Terminating;
  4198. mCeMachine->mDebugEvent.Set(true);
  4199. }
  4200. void CeDebugger::Terminate()
  4201. {
  4202. }
  4203. void CeDebugger::Detach()
  4204. {
  4205. mRunState = RunState_Terminated;
  4206. mDbgCallStack.Clear();
  4207. }
  4208. Profiler* CeDebugger::StartProfiling()
  4209. {
  4210. return nullptr;
  4211. }
  4212. Profiler* CeDebugger::PopProfiler()
  4213. {
  4214. return nullptr;
  4215. }
  4216. void CeDebugger::ReportMemory(MemReporter* memReporter)
  4217. {
  4218. }
  4219. bool CeDebugger::IsOnDemandDebugger()
  4220. {
  4221. return true;
  4222. }
  4223. bool CeDebugger::GetEmitSource(const StringImpl& filePath, String& outText)
  4224. {
  4225. return false;
  4226. }