Example #1
0
		public static IList<MethodSourceStatement> GetStatements(IDocumentViewer documentViewer, int textPosition) {
			if (documentViewer == null)
				return null;
			var methodDebugService = documentViewer.GetMethodDebugService();
			var methodStatements = methodDebugService.FindByTextPosition(textPosition, sameMethod: true);
			return methodStatements.Count == 0 ? null : methodStatements;
		}
		public HighlightRelatedKeywordsDocumentViewerReferenceEnabler(IDocumentViewer documentViewer) {
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			this.documentViewer = documentViewer;
			IsEnabled = documentViewer.TextView.Options.IsHighlightRelatedKeywordsEnabled();
			documentViewer.TextView.Options.OptionChanged += Options_OptionChanged;
		}
		public void RaiseRemovedEvent(IDocumentViewer documentViewer) {
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			var e = new DocumentViewerRemovedEventArgs(documentViewer);
			NotifyListeners(e);
			Removed?.Invoke(this, e);
		}
Example #4
0
		NodeTabSaver(IMessageBoxService messageBoxService, IDocumentTab tab, IDocumentTreeNodeDecompiler documentTreeNodeDecompiler, IDecompiler decompiler, IDocumentViewer documentViewer, DocumentTreeNodeData[] nodes) {
			this.messageBoxService = messageBoxService;
			this.tab = tab;
			this.documentTreeNodeDecompiler = documentTreeNodeDecompiler;
			this.decompiler = decompiler;
			this.documentViewer = documentViewer;
			this.nodes = nodes;
		}
		public void RaiseNewContentEvent(IDocumentViewer documentViewer, DocumentViewerContent content, IContentType contentType) {
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			if (content == null)
				throw new ArgumentNullException(nameof(content));
			if (contentType == null)
				throw new ArgumentNullException(nameof(contentType));
			var e = new DocumentViewerGotNewContentEventArgs(documentViewer, content, contentType);
			NotifyListeners(e);
			GotNewContent?.Invoke(this, e);
		}
Example #6
0
		public static bool MoveCaretTo(IDocumentViewer documentViewer, ModuleTokenId key, uint ilOffset) {
			if (documentViewer == null)
				return false;

			IMethodDebugService methodDebugService;
			if (!VerifyAndGetCurrentDebuggedMethod(documentViewer, key, out methodDebugService))
				return false;

			var sourceStatement = methodDebugService.TryGetMethodDebugInfo(key).GetSourceStatementByCodeOffset(ilOffset);
			if (sourceStatement == null)
				return false;

			documentViewer.MoveCaretToPosition(sourceStatement.Value.TextSpan.Start);
			return true;
		}
Example #7
0
		public static void Execute(IDocumentViewer documentViewer, Lazy<IMethodAnnotations> methodAnnotations) {
			if (!CanExecute(documentViewer))
				return;

			var copier = new InstructionILBytesCopier();
			var text = copier.Copy(FindInstructions(documentViewer), methodAnnotations);
			if (text.Length > 0) {
				try {
					Clipboard.SetText(text);
				}
				catch (ExternalException) { }
				if (copier.FoundUnknownBytes) {
					MsgBox.Instance.ShowIgnorableMessage(new Guid("141A1744-13CD-4835-A804-08D93D8E0D2B"),
						dnSpy_AsmEditor_Resources.UnknownBytesMsg,
						MsgBoxButton.OK);
				}
			}
		}
		public ToolTipProviderContext(IDotNetImageService dotNetImageService, IDecompiler decompiler, ICodeToolTipSettings codeToolTipSettings, IDocumentViewer documentViewer, IClassificationFormatMap classificationFormatMap, IThemeClassificationTypeService themeClassificationTypeService) {
			if (dotNetImageService == null)
				throw new ArgumentNullException(nameof(dotNetImageService));
			if (decompiler == null)
				throw new ArgumentNullException(nameof(decompiler));
			if (codeToolTipSettings == null)
				throw new ArgumentNullException(nameof(codeToolTipSettings));
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			if (classificationFormatMap == null)
				throw new ArgumentNullException(nameof(classificationFormatMap));
			if (themeClassificationTypeService == null)
				throw new ArgumentNullException(nameof(themeClassificationTypeService));
			DocumentViewer = documentViewer;
			this.dotNetImageService = dotNetImageService;
			Decompiler = decompiler;
			this.codeToolTipSettings = codeToolTipSettings;
			this.classificationFormatMap = classificationFormatMap;
			this.themeClassificationTypeService = themeClassificationTypeService;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="documentViewer">Document viewer</param>
		/// <param name="content">New content</param>
		/// <param name="contentType">Content type</param>
		public DocumentViewerGotNewContentEventArgs(IDocumentViewer documentViewer, DocumentViewerContent content, IContentType contentType)
			: base(documentViewer) {
			if (content == null)
				throw new ArgumentNullException(nameof(content));
			if (contentType == null)
				throw new ArgumentNullException(nameof(contentType));
			Content = content;
			ContentType = contentType;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="documentViewer"><see cref="IDocumentViewer"/> instance</param>
		public DocumentViewerRemovedEventArgs(IDocumentViewer documentViewer)
			: base(documentViewer) {
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="documentViewer"><see cref="IDocumentViewer"/> instance</param>
		public DocumentViewerAddedEventArgs(IDocumentViewer documentViewer)
			: base(documentViewer) {
		}
Example #12
0
		//TODO: This method (and all callers) should take an ITextView instead of an IDocumentViewer as a parameter
		List<ILCodeBreakpoint> GetILCodeBreakpoints(IDocumentViewer documentViewer, IList<MethodSourceStatement> methodStatements) {
			var list = new List<ILCodeBreakpoint>();
			if (methodStatements.Count == 0)
				return list;
			var service = documentViewer.TryGetMethodDebugService();
			if (service == null)
				return list;
			var methodStatement = methodStatements[0];
			foreach (var ilbp in GetILCodeBreakpoints()) {
				var info = service.TryGetMethodDebugInfo(ilbp.MethodToken);
				if (info == null)
					continue;
				var statement = info.GetSourceStatementByCodeOffset(ilbp.ILOffset);
				if (statement == null)
					continue;
				if (statement.Value.TextSpan != methodStatement.Statement.TextSpan)
					continue;

				list.Add(ilbp);
			}

			return list;
		}
Example #13
0
		bool MoveCaretToCurrentStatement(IDocumentViewer documentViewer, bool canRefreshMethods) {
			if (documentViewer == null)
				return false;
			if (currentLocation == null)
				return false;
			if (DebugUtils.MoveCaretTo(documentViewer, currentLocation.Value.SerializedDnToken, currentLocation.Value.Offset))
				return true;
			if (!canRefreshMethods)
				return false;

			RefreshMethodBodies(documentViewer);

			return false;
		}
Example #14
0
		public static bool VerifyAndGetCurrentDebuggedMethod(IDocumentViewer documentViewer, ModuleTokenId serToken, out IMethodDebugService methodDebugService) {
			methodDebugService = documentViewer.GetMethodDebugService();
			return methodDebugService.TryGetMethodDebugInfo(serToken) != null;
		}
Example #15
0
		public static bool CanExecute(IDocumentViewer documentViewer) =>
			documentViewer != null && FindInstructions(documentViewer).Any();
Example #16
0
		static IEnumerable<SpanData<ReferenceInfo>> FindInstructions(IDocumentViewer documentViewer) {
			foreach (var refInfo in documentViewer.GetSelectedReferences()) {
				if (refInfo.Data.IsDefinition && refInfo.Data.Reference is InstructionReference)
					yield return refInfo;
			}
		}
Example #17
0
		bool TryShowNextStatement(IDocumentViewer documentViewer) {
			// Always reset the selected frame
			StackFrameService.SelectedFrameNumber = 0;
			if (currentLocation == null)
				return false;
			return DebugUtils.MoveCaretTo(documentViewer, currentLocation.Value.SerializedDnToken, currentLocation.Value.Offset);
		}
Example #18
0
		bool DebugGetMethodSourceStatementForSetNextStatement(IDocumentViewer documentViewer, out string errMsg, out MethodSourceStatement methodStatement) {
			errMsg = string.Empty;
			methodStatement = default(MethodSourceStatement);

			if (ProcessState == DebuggerProcessState.Terminated) {
				errMsg = dnSpy_Debugger_Resources.Error_NotDebugging;
				return false;
			}
			if (ProcessState == DebuggerProcessState.Starting || ProcessState == DebuggerProcessState.Continuing || ProcessState == DebuggerProcessState.Running) {
				errMsg = dnSpy_Debugger_Resources.Error_CantSetNextStatementWhenNotStopped;
				return false;
			}

			if (documentViewer == null) {
				documentViewer = documentTabService.ActiveTab.TryGetDocumentViewer();
				if (documentViewer == null) {
					errMsg = dnSpy_Debugger_Resources.Error_NoTabAvailableDecompileCurrentMethod;
					return false;
				}
			}

			IMethodDebugService methodDebugService;
			if (currentLocation == null || !DebugUtils.VerifyAndGetCurrentDebuggedMethod(documentViewer, currentLocation.Value.SerializedDnToken, out methodDebugService)) {
				errMsg = dnSpy_Debugger_Resources.Error_NoDebugInfoAvailable;
				return false;
			}
			Debug.Assert(currentLocation != null);

			var methodStatements = methodDebugService.FindByTextPosition(documentViewer.Caret.Position.BufferPosition.Position, sameMethod: false);
			if (methodStatements.Count == 0) {
				errMsg = dnSpy_Debugger_Resources.Error_CantSetNextStatementHere;
				return false;
			}

			if (GetCurrentMethodILFrame() == null) {
				errMsg = dnSpy_Debugger_Resources.Error_CantSetNextStatementNoILFrame;
				return false;
			}

			foreach (var info in methodStatements) {
				var md = info.Method;
				if (currentLocation.Value.Function.Token != md.MDToken.Raw)
					continue;
				var moduleId = moduleIdProvider.Create(md.Module);
				if (!moduleId.Equals(currentLocation.Value.SerializedDnToken.Module))
					continue;

				methodStatement = info;
				break;
			}
			if (methodStatement.Method == null) {
				errMsg = dnSpy_Debugger_Resources.Error_CantSetNextStatementToAnotherMethod;
				return false;
			}

			return true;
		}
		public static IBlockStructureServiceDataProvider TryCreate(IDocumentViewer documentViewer) {
			var lazyColl = documentViewer.Content.GetCustomData<LazyBlockStructureCollection>(DocumentViewerContentDataIds.BlockStructure);
			return lazyColl == null ? null : new BlockStructureServiceDataProvider(lazyColl);
		}
Example #20
0
		void RefreshMethodBodies(IDocumentViewer documentViewer) {
			if (currentLocation == null)
				return;
			if (currentMethod == null)
				return;
			if (documentViewer == null)
				return;

			// If this fails, we're probably in the prolog or epilog. Shouldn't normally happen.
			if (!currentLocation.Value.IsExact && !currentLocation.Value.IsApproximate)
				return;
			var body = currentMethod.Body;
			if (body == null)
				return;
			// If the offset is a valid instruction in the body, the method is probably not encrypted
			if (body.Instructions.Any(i => i.Offset == currentLocation.Value.Offset))
				return;

			// No instruction with the current offset: it must be encrypted, and since we're executing
			// the method, we must be using an invalid method body. Use a copy of the module in
			// memory, and refresh the method bodies in case it's already loaded, and re-decompile
			// the method.

			var mod = currentMethod.Module;
			if (mod == null)
				return;
			var modNode = documentTabService.DocumentTreeView.FindNode(mod);
			if (modNode == null)
				return;
			var memFile = modNode.Document as MemoryModuleDefFile;
			IDsDocument document = memFile;
			if (memFile == null) {
				if (modNode.Document is CorModuleDefFile)
					return;
				var corMod = currentLocation.Value.Function.Module;
				if (corMod == null || corMod.IsDynamic)
					return;
				var dnMod = moduleLoader.Value.GetDnModule(corMod);
				document = inMemoryModuleService.Value.LoadDocument(dnMod, true);
				Debug.Assert(document != null);
				memFile = document as MemoryModuleDefFile;
			}
			if (document == null)
				return;
			// It's null if we couldn't load the file from memory because the PE / COR20 headers
			// are corrupt (eg. an obfuscator overwrote various fields with garbage). In that case,
			// file is a CorModuleDefFile and it's using the MD API to read the MD.
			if (memFile != null)
				inMemoryModuleService.Value.UpdateModuleMemory(memFile);
			UpdateCurrentMethod(document);
			JumpToCurrentStatement(documentViewer.DocumentTab, false);
		}
		public IDocumentViewerReferenceEnabler Create(IDocumentViewer documentViewer) =>
			new HighlightRelatedKeywordsDocumentViewerReferenceEnabler(documentViewer);
Example #22
0
		List<ILCodeBreakpoint> GetILCodeBreakpoints(IDocumentViewer documentViewer, int textPosition) =>
			GetILCodeBreakpoints(documentViewer, documentViewer.GetMethodDebugService().FindByTextPosition(textPosition, sameMethod: false));
		public DocumentViewerToolTipService GetService(IDocumentViewer documentViewer) =>
			documentViewer.TextView.Properties.GetOrCreateSingletonProperty(typeof(DocumentViewerToolTipService), () => new DocumentViewerToolTipService(dotNetImageService, codeToolTipSettings, documentViewerToolTipProviders, documentViewer, quickInfoBroker, classificationFormatMapService.GetClassificationFormatMap(AppearanceCategoryConstants.UIMisc), themeClassificationTypeService, decompilerService));
Example #24
0
		public void Toggle(IDocumentViewer documentViewer, int textPosition) {
			var statements = documentViewer.GetMethodDebugService().FindByTextPosition(textPosition, sameMethod: false);
			var ilbps = GetILCodeBreakpoints(documentViewer, statements);
			if (ilbps.Count > 0) {
				if (IsEnabled(ilbps)) {
					foreach (var ilbp in ilbps)
						Remove(ilbp);
				}
				else {
					foreach (var bpm in ilbps)
						bpm.IsEnabled = true;
				}
			}
			else if (statements.Count > 0) {
				foreach (var methodStatement in statements) {
					var md = methodStatement.Method;
					var modId = moduleIdProvider.Create(md.Module);
					var key = new ModuleTokenId(modId, md.MDToken);
					Add(new ILCodeBreakpoint(key, methodStatement.Statement.BinSpan.Start));
				}
				documentViewer.MoveCaretToPosition(statements[0].Statement.TextSpan.Start);
			}
		}
		public DocumentViewerToolTipService(IDotNetImageService dotNetImageService, ICodeToolTipSettings codeToolTipSettings, Lazy<IDocumentViewerToolTipProvider, IDocumentViewerToolTipProviderMetadata>[] documentViewerToolTipProviders, IDocumentViewer documentViewer, IQuickInfoBroker quickInfoBroker, IClassificationFormatMap classificationFormatMap, IThemeClassificationTypeService themeClassificationTypeService, IDecompilerService decompilerService) {
			if (dotNetImageService == null)
				throw new ArgumentNullException(nameof(dotNetImageService));
			if (codeToolTipSettings == null)
				throw new ArgumentNullException(nameof(codeToolTipSettings));
			if (documentViewerToolTipProviders == null)
				throw new ArgumentNullException(nameof(documentViewerToolTipProviders));
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			if (quickInfoBroker == null)
				throw new ArgumentNullException(nameof(quickInfoBroker));
			if (classificationFormatMap == null)
				throw new ArgumentNullException(nameof(classificationFormatMap));
			if (themeClassificationTypeService == null)
				throw new ArgumentNullException(nameof(themeClassificationTypeService));
			if (decompilerService == null)
				throw new ArgumentNullException(nameof(decompilerService));
			this.dotNetImageService = dotNetImageService;
			this.codeToolTipSettings = codeToolTipSettings;
			this.documentViewerToolTipProviders = documentViewerToolTipProviders;
			this.documentViewer = documentViewer;
			this.quickInfoBroker = quickInfoBroker;
			this.classificationFormatMap = classificationFormatMap;
			this.themeClassificationTypeService = themeClassificationTypeService;
			this.decompilerService = decompilerService;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="documentViewer"><see cref="IDocumentViewer"/> instance</param>
		protected DocumentViewerEventArgs(IDocumentViewer documentViewer) {
			if (documentViewer == null)
				throw new ArgumentNullException(nameof(documentViewer));
			DocumentViewer = documentViewer;
		}
Example #27
0
		BamlTabSaver(IDocumentTab tab, BamlResourceElementNode bamlNode, IDocumentViewer documentViewer, IMessageBoxService messageBoxService) {
			this.tab = tab;
			this.bamlNode = bamlNode;
			this.documentViewer = documentViewer;
			this.messageBoxService = messageBoxService;
		}