CeDebugger.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #pragma once
  2. #include "BfSystem.h"
  3. #include "BfModule.h"
  4. #include "BeefySysLib/util/Heap.h"
  5. #include "BeefySysLib/util/AllocDebug.h"
  6. #include "../Debugger.h"
  7. NS_BF_BEGIN
  8. class BfCompiler;
  9. class CeFrame;
  10. class CeExprEvaluator;
  11. class CeContext;
  12. class CeMachine;
  13. class CeFunction;
  14. class BfReducer;
  15. class CeDebugger;
  16. class DebugVisualizerEntry;
  17. class CeBreakpoint : public Breakpoint
  18. {
  19. public:
  20. uintptr mCurBindAddr;
  21. bool mHasBound;
  22. int mIdx;
  23. public:
  24. CeBreakpoint()
  25. {
  26. mCurBindAddr = 1;
  27. mHasBound = false;
  28. mIdx = -1;
  29. }
  30. virtual uintptr GetAddr() { return mCurBindAddr; }
  31. virtual bool IsMemoryBreakpointBound() { return false; }
  32. };
  33. struct CeFormatInfo
  34. {
  35. int mCallStackIdx;
  36. bool mHidePointers;
  37. bool mIgnoreDerivedClassInfo;
  38. bool mNoVisualizers;
  39. bool mNoMembers;
  40. bool mRawString;
  41. bool mNoEdit;
  42. DbgTypeKindFlags mTypeKindFlags;
  43. intptr mArrayLength;
  44. intptr mOverrideCount;
  45. intptr mMaxCount;
  46. DwDisplayType mDisplayType;
  47. int mTotalSummaryLength;
  48. String mReferenceId;
  49. String mSubjectExpr;
  50. String mExpectedType;
  51. String mNamespaceSearch;
  52. int mExpandItemDepth;
  53. BfTypedValue mExplicitThis;
  54. CeFormatInfo()
  55. {
  56. mCallStackIdx = -1;
  57. mHidePointers = false;
  58. mIgnoreDerivedClassInfo = false;
  59. mRawString = false;
  60. mNoVisualizers = false;
  61. mNoMembers = false;
  62. mNoEdit = false;
  63. mTypeKindFlags = DbgTypeKindFlag_None;
  64. mArrayLength = -1;
  65. mOverrideCount = -1;
  66. mMaxCount = -1;
  67. mTotalSummaryLength = 0;
  68. mDisplayType = DwDisplayType_NotSpecified;
  69. mExpandItemDepth = 0;
  70. }
  71. };
  72. class CeEvaluationContext
  73. {
  74. public:
  75. CeDebugger* mDebugger;
  76. BfParser* mParser;
  77. BfReducer* mReducer;
  78. BfPassInstance* mPassInstance;
  79. BfExprEvaluator* mExprEvaluator;
  80. BfExpression* mExprNode;
  81. BfTypedValue mResultOverride;
  82. String mExprString;
  83. BfTypedValue mExplicitThis;
  84. int mCallStackIdx;
  85. public:
  86. CeEvaluationContext(CeDebugger* winDebugger, const StringImpl& expr, CeFormatInfo* formatInfo = NULL, BfTypedValue contextValue = BfTypedValue());
  87. void Init(CeDebugger* winDebugger, const StringImpl& expr, CeFormatInfo* formatInfo = NULL, BfTypedValue contextValue = BfTypedValue());
  88. bool HasExpression();
  89. ~CeEvaluationContext();
  90. BfTypedValue EvaluateInContext(BfTypedValue contextTypedValue);
  91. String GetErrorStr();
  92. bool HadError();
  93. };
  94. class CeDbgState
  95. {
  96. public:
  97. CeFrame* mActiveFrame;
  98. CeContext* mCeContext;
  99. BfTypedValue mExplicitThis;
  100. DwEvalExpressionFlags mDbgExpressionFlags;
  101. bool mHadSideEffects;
  102. bool mBlockedSideEffects;
  103. public:
  104. CeDbgState()
  105. {
  106. mActiveFrame = NULL;
  107. mCeContext = NULL;
  108. mDbgExpressionFlags = DwEvalExpressionFlag_None;
  109. mHadSideEffects = false;
  110. mBlockedSideEffects = false;
  111. }
  112. };
  113. class CePendingExpr
  114. {
  115. public:
  116. int mThreadId;
  117. BfParser* mParser;
  118. BfType* mExplitType;
  119. CeFormatInfo mFormatInfo;
  120. DwEvalExpressionFlags mExpressionFlags;
  121. int mCursorPos;
  122. BfAstNode* mExprNode;
  123. String mReferenceId;
  124. int mCallStackIdx;
  125. String mResult;
  126. int mIdleTicks;
  127. String mException;
  128. CePendingExpr();
  129. ~CePendingExpr();
  130. };
  131. class CeFileInfo
  132. {
  133. public:
  134. Array<CeBreakpoint*> mOrderedBreakpoints;
  135. };
  136. class CeDbgFieldEntry
  137. {
  138. public:
  139. BfType* mType;
  140. int mDataOffset;
  141. public:
  142. CeDbgFieldEntry()
  143. {
  144. mType = NULL;
  145. mDataOffset = 0;
  146. }
  147. };
  148. class CeDbgTypeInfo
  149. {
  150. public:
  151. struct ConstIntEntry
  152. {
  153. public:
  154. int mFieldIdx;
  155. int64 mVal;
  156. };
  157. public:
  158. BfType* mType;
  159. Array<CeDbgFieldEntry> mFieldOffsets;
  160. Array<ConstIntEntry> mConstIntEntries;
  161. };
  162. struct CeTypedValue
  163. {
  164. addr_ce mAddr;
  165. BfIRType mType;
  166. CeTypedValue()
  167. {
  168. mAddr = 0;
  169. mType = BfIRType();
  170. }
  171. CeTypedValue(addr_ce addr, BfIRType type)
  172. {
  173. mAddr = addr;
  174. mType = type;
  175. }
  176. operator bool() const
  177. {
  178. return mType.mKind != BfIRTypeData::TypeKind_None;
  179. }
  180. };
  181. class CeDebugger : public Debugger
  182. {
  183. public:
  184. BfCompiler* mCompiler;
  185. CeMachine* mCeMachine;
  186. DebugManager* mDebugManager;
  187. CePendingExpr* mDebugPendingExpr;
  188. CeDbgState* mCurDbgState;
  189. Array<CeBreakpoint*> mBreakpoints;
  190. Dictionary<String, CeFileInfo*> mFileInfo;
  191. Dictionary<int, CeDbgTypeInfo> mDbgTypeInfoMap;
  192. CeEvaluationContext* mCurEvaluationContext;
  193. CeBreakpoint* mActiveBreakpoint;
  194. int mBreakpointVersion;
  195. bool mBreakpointCacheDirty;
  196. bool mBreakpointFramesDirty;
  197. int mCurDisasmFuncId;
  198. public:
  199. bool SetupStep(int frameIdx = 0);
  200. CeFrame* GetFrame(int callStackIdx);
  201. String EvaluateContinue(CePendingExpr* pendingExpr, BfPassInstance& bfPassInstance);
  202. String Evaluate(const StringImpl& expr, CeFormatInfo formatInfo, int callStackIdx, int cursorPos, int language, DwEvalExpressionFlags expressionFlags);
  203. DwDisplayInfo* GetDisplayInfo(const StringImpl& referenceId);
  204. String GetMemberList(BfType* type, addr_ce addr, addr_ce addrInst, bool isStatic);
  205. DebugVisualizerEntry* FindVisualizerForType(BfType* dbgType, Array<String>* wildcardCaptures);
  206. bool ParseFormatInfo(const StringImpl& formatInfoStr, CeFormatInfo* formatInfo, BfPassInstance* bfPassInstance, int* assignExprOffset, String* assignExprString, String* errorString, BfTypedValue contextTypedValue = BfTypedValue());
  207. String MaybeQuoteFormatInfoParam(const StringImpl& str);
  208. BfTypedValue EvaluateInContext(const BfTypedValue& contextTypedValue, const StringImpl& subExpr, CeFormatInfo* formatInfo = NULL, String* outReferenceId = NULL, String* outErrors = NULL);
  209. void DbgVisFailed(DebugVisualizerEntry* debugVis, const StringImpl& evalString, const StringImpl& errors);
  210. String GetArrayItems(DebugVisualizerEntry* debugVis, BfType* valueType, BfTypedValue& curNode, int& count, String* outContinuationData);
  211. String GetLinkedListItems(DebugVisualizerEntry* debugVis, addr_ce endNodePtr, BfType* valueType, BfTypedValue& curNode, int& count, String* outContinuationData);
  212. String GetDictionaryItems(DebugVisualizerEntry* debugVis, BfTypedValue dictValue, int bucketIdx, int nodeIdx, int& count, String* outContinuationData);
  213. String GetTreeItems(DebugVisualizerEntry* debugVis, Array<addr_ce>& parentList, BfType*& valueType, BfTypedValue& curNode, int count, String* outContinuationData);
  214. bool EvalCondition(DebugVisualizerEntry* debugVis, BfTypedValue typedVal, CeFormatInfo& formatInfo, const StringImpl& condition, const Array<String>& dbgVisWildcardCaptures, String& errorStr);
  215. CeTypedValue GetAddr(BfConstant* constant);
  216. CeTypedValue GetAddr(const BfTypedValue typeVal);
  217. String ReadString(BfTypeCode charType, intptr addr, intptr maxLength, CeFormatInfo& formatInfo);
  218. void ProcessEvalString(BfTypedValue useTypedValue, String& evalStr, String& displayString, CeFormatInfo& formatInfo, DebugVisualizerEntry* debugVis, bool limitLength);
  219. String TypedValueToString(const BfTypedValue& typedValue, const StringImpl& expr, CeFormatInfo& formatFlags, bool fullPrecision = false);
  220. void HandleCustomExpandedItems(String& retVal, DebugVisualizerEntry* debugVis, BfTypedValue typedValue, addr_ce addr, addr_ce addrInst, Array<String>& dbgVisWildcardCaptures, CeFormatInfo& formatInfo);
  221. void ClearBreakpointCache();
  222. void UpdateBreakpointCache();
  223. void UpdateBreakpointFrames();
  224. void UpdateBreakpointAddrs();
  225. void UpdateBreakpoints(CeFunction* ceFunction);
  226. void Continue();
  227. CeDbgTypeInfo* GetDbgTypeInfo(int typeId);
  228. CeDbgTypeInfo* GetDbgTypeInfo(BfIRType irType);
  229. int64 ValueToInt(const BfTypedValue& typedVal);
  230. BfType* FindType(const StringImpl& name);
  231. public:
  232. CeDebugger(DebugManager* debugManager, BfCompiler* bfCompiler);
  233. ~CeDebugger();
  234. virtual void OutputMessage(const StringImpl& msg) override;
  235. virtual void OutputRawMessage(const StringImpl& msg) override;
  236. virtual int GetAddrSize() override;
  237. virtual bool CanOpen(const StringImpl& fileName, DebuggerResult* outResult) override;
  238. virtual void OpenFile(const StringImpl& launchPath, const StringImpl& targetPath, const StringImpl& args, const StringImpl& workingDir, const Array<uint8>& envBlock, bool hotSwapEnabled) override;
  239. virtual bool Attach(int processId, BfDbgAttachFlags attachFlags) override;
  240. virtual void Run() override;
  241. virtual void HotLoad(const Array<String>& objectFiles, int hotIdx) override;
  242. virtual void InitiateHotResolve(DbgHotResolveFlags flags) override;
  243. virtual intptr GetDbgAllocHeapSize() override;
  244. virtual String GetDbgAllocInfo() override;
  245. virtual void Update() override;
  246. virtual void ContinueDebugEvent() override;
  247. virtual void ForegroundTarget() override;
  248. virtual Breakpoint* CreateBreakpoint(const StringImpl& fileName, int lineNum, int wantColumn, int instrOffset) override;
  249. virtual Breakpoint* CreateMemoryBreakpoint(intptr addr, int byteCount) override;
  250. virtual Breakpoint* CreateSymbolBreakpoint(const StringImpl& symbolName) override;
  251. virtual Breakpoint* CreateAddressBreakpoint(intptr address) override;
  252. virtual uintptr GetBreakpointAddr(Breakpoint* breakpoint) override;
  253. virtual void CheckBreakpoint(Breakpoint* breakpoint) override;
  254. virtual void HotBindBreakpoint(Breakpoint* wdBreakpoint, int lineNum, int hotIdx) override;
  255. virtual void DeleteBreakpoint(Breakpoint* wdBreakpoint) override;
  256. virtual void DetachBreakpoint(Breakpoint* wdBreakpoint) override;
  257. virtual void MoveBreakpoint(Breakpoint* wdBreakpoint, int lineNum, int wantColumn, bool rebindNow) override;
  258. virtual void MoveMemoryBreakpoint(Breakpoint* wdBreakpoint, intptr addr, int byteCount) override;
  259. virtual void DisableBreakpoint(Breakpoint* wdBreakpoint) override;
  260. virtual void SetBreakpointCondition(Breakpoint* wdBreakpoint, const StringImpl& condition) override;
  261. virtual void SetBreakpointLogging(Breakpoint* wdBreakpoint, const StringImpl& logging, bool breakAfterLogging) override;
  262. virtual Breakpoint* FindBreakpointAt(intptr address) override;
  263. virtual Breakpoint* GetActiveBreakpoint() override;
  264. virtual void BreakAll() override;
  265. virtual bool TryRunContinue() override;
  266. virtual void StepInto(bool inAssembly) override;
  267. virtual void StepIntoSpecific(intptr addr) override;
  268. virtual void StepOver(bool inAssembly) override;
  269. virtual void StepOut(bool inAssembly) override;
  270. virtual void SetNextStatement(bool inAssembly, const StringImpl& fileName, int64 lineNumOrAsmAddr, int wantColumn) override;
  271. //virtual DbgTypedValue GetRegister(const StringImpl& regName, CPURegisters* registers, Array<RegForm>* regForms = NULL) override;
  272. virtual String Evaluate(const StringImpl& expr, int callStackIdx, int cursorPos, int language, DwEvalExpressionFlags expressionFlags) override;
  273. virtual String EvaluateContinue() override;
  274. virtual void EvaluateContinueKeep() override;
  275. virtual String EvaluateToAddress(const StringImpl& expr, int callStackIdx, int cursorPos) override;
  276. virtual String EvaluateAtAddress(const StringImpl& expr, intptr atAddr, int cursorPos) override;
  277. virtual String GetCollectionContinuation(const StringImpl& continuationData, int callStackIdx, int count) override;
  278. virtual String GetAutoExpressions(int callStackIdx, uint64 memoryRangeStart, uint64 memoryRangeLen) override;
  279. virtual String GetAutoLocals(int callStackIdx, bool showRegs) override;
  280. virtual String CompactChildExpression(const StringImpl& expr, const StringImpl& parentExpr, int callStackIdx) override;
  281. virtual String GetProcessInfo() override;
  282. virtual String GetThreadInfo() override;
  283. virtual void SetActiveThread(int threadId) override;
  284. virtual int GetActiveThread() override;
  285. virtual void FreezeThread(int threadId) override;
  286. virtual void ThawThread(int threadId) override;
  287. virtual bool IsActiveThreadWaiting() override;
  288. virtual void ClearCallStack() override;
  289. virtual void UpdateCallStack(bool slowEarlyOut = true) override;
  290. virtual int GetCallStackCount() override;
  291. virtual int GetRequestedStackFrameIdx() override;
  292. virtual int GetBreakStackFrameIdx() override;
  293. virtual bool ReadMemory(intptr address, uint64 length, void* dest, bool local = false) override;
  294. virtual bool WriteMemory(intptr address, void* src, uint64 length) override;
  295. virtual DbgMemoryFlags GetMemoryFlags(intptr address) override;
  296. virtual void UpdateRegisterUsage(int stackFrameIdx) override;
  297. virtual void UpdateCallStackMethod(int stackFrameIdx) override;
  298. virtual void GetCodeAddrInfo(intptr addr, String* outFile, int* outHotIdx, int* outDefLineStart, int* outDefLineEnd, int* outLine, int* outColumn) override;
  299. virtual void GetStackAllocInfo(intptr addr, int* outThreadId, int* outStackIdx) override;
  300. virtual String GetStackFrameInfo(int stackFrameIdx, intptr* addr, String* outFile, int32* outHotIdx, int32* outDefLineStart, int32* outDefLineEnd, int32* outLine, int32* outColumn, int32* outLanguage, int32* outStackSize, int8* outFlags) override;
  301. virtual String Callstack_GetStackFrameOldFileInfo(int stackFrameIdx) override;
  302. virtual int GetJmpState(int stackFrameIdx) override;
  303. virtual intptr GetStackFrameCalleeAddr(int stackFrameIdx) override;
  304. virtual String GetStackMethodOwner(int stackFrameIdx, int& language) override;
  305. virtual String FindCodeAddresses(const StringImpl& fileName, int line, int column, bool allowAutoResolve) override;
  306. virtual String GetAddressSourceLocation(intptr address) override;
  307. virtual String GetAddressSymbolName(intptr address, bool demangle) override;
  308. virtual String DisassembleAtRaw(intptr address) override;
  309. virtual String DisassembleAt(intptr address) override;
  310. virtual String FindLineCallAddresses(intptr address) override;
  311. virtual String GetCurrentException() override;
  312. virtual String GetModulesInfo() override;
  313. virtual void SetAliasPath(const StringImpl& origPath, const StringImpl& localPath) override;
  314. virtual void CancelSymSrv() override;
  315. virtual bool HasPendingDebugLoads() override;
  316. virtual int LoadImageForModule(const StringImpl& moduleName, const StringImpl& debugFileName) override;
  317. virtual int LoadDebugInfoForModule(const StringImpl& moduleName) override;
  318. virtual int LoadDebugInfoForModule(const StringImpl& moduleName, const StringImpl& debugFileName) override;
  319. virtual void StopDebugging() override;
  320. virtual void Terminate() override;
  321. virtual void Detach() override;
  322. virtual Profiler* StartProfiling() override;
  323. virtual Profiler* PopProfiler() override; // Profiler requested by target program
  324. virtual void ReportMemory(MemReporter* memReporter) override;
  325. virtual bool IsOnDemandDebugger() override;
  326. };
  327. NS_BF_END