コード例 #1
0
        void AddBreakpoint(BreakpointBookmark bookmark)
        {
            Breakpoint breakpoint = null;

            if (bookmark is DecompiledBreakpointBookmark)
            {
                try {
                    if (debuggerDecompilerService == null)
                    {
                        LoggingService.Warn("No IDebuggerDecompilerService found!");
                        return;
                    }
                    var             dbb             = (DecompiledBreakpointBookmark)bookmark;
                    MemberReference memberReference = null;

                    string assemblyFile, typeName;
                    if (DecompiledBreakpointBookmark.GetAssemblyAndType(dbb.FileName, out assemblyFile, out typeName))
                    {
                        memberReference = dbb.GetMemberReference(debuggerDecompilerService.GetAssemblyResolver(assemblyFile));
                    }

                    int token = memberReference.MetadataToken.ToInt32();
                    if (!debuggerDecompilerService.CheckMappings(token))
                    {
                        debuggerDecompilerService.DecompileOnDemand(memberReference as TypeDefinition);
                    }

                    int[] ilRanges;
                    int   methodToken;
                    if (debuggerDecompilerService.GetILAndTokenByLineNumber(token, dbb.LineNumber, out ilRanges, out methodToken))
                    {
                        // create BP
                        breakpoint = new ILBreakpoint(
                            debugger,
                            memberReference.FullName,
                            dbb.LineNumber,
                            memberReference.MetadataToken.ToInt32(),
                            methodToken,
                            ilRanges[0],
                            dbb.IsEnabled);

                        debugger.Breakpoints.Add(breakpoint);
                    }
                } catch (System.Exception ex) {
                    LoggingService.Error("Error on DecompiledBreakpointBookmark: " + ex.Message);
                }
            }
            else
            {
                breakpoint = debugger.Breakpoints.Add(bookmark.FileName, null, bookmark.LineNumber, 0, bookmark.IsEnabled);
            }

            if (breakpoint == null)
            {
                LoggingService.Warn(string.Format("unable to create breakpoint: {0}", bookmark.ToString()));
                return;
            }

            MethodInvoker setBookmarkColor = delegate {
                if (debugger.Processes.Count == 0)
                {
                    bookmark.IsHealthy = true;
                    bookmark.Tooltip   = null;
                }
                else if (!breakpoint.IsSet)
                {
                    bookmark.IsHealthy = false;
                    bookmark.Tooltip   = "Breakpoint was not found in any loaded modules";
                }
                else if (breakpoint.OriginalLocation == null || breakpoint.OriginalLocation.CheckSum == null)
                {
                    bookmark.IsHealthy = true;
                    bookmark.Tooltip   = null;
                }
                else
                {
                    if (!File.Exists(bookmark.FileName))
                    {
                        return;
                    }

                    byte[]    fileMD5;
                    IEditable file = FileService.GetOpenFile(bookmark.FileName) as IEditable;
                    if (file != null)
                    {
                        byte[] fileContent = Encoding.UTF8.GetBytesWithPreamble(file.Text);
                        fileMD5 = new MD5CryptoServiceProvider().ComputeHash(fileContent);
                    }
                    else
                    {
                        fileMD5 = new MD5CryptoServiceProvider().ComputeHash(File.ReadAllBytes(bookmark.FileName));
                    }
                    if (Compare(fileMD5, breakpoint.OriginalLocation.CheckSum))
                    {
                        bookmark.IsHealthy = true;
                        bookmark.Tooltip   = null;
                    }
                    else
                    {
                        bookmark.IsHealthy = false;
                        bookmark.Tooltip   = "Check sum or file does not match to the original";
                    }
                }
            };

            // event handlers on bookmark and breakpoint don't need deregistration
            bookmark.IsEnabledChanged += delegate {
                breakpoint.Enabled = bookmark.IsEnabled;
            };
            breakpoint.Set += delegate { setBookmarkColor(); };

            setBookmarkColor();

            EventHandler <CollectionItemEventArgs <Process> > bp_debugger_ProcessStarted = (sender, e) => {
                setBookmarkColor();
                // User can change line number by inserting or deleting lines
                breakpoint.Line = bookmark.LineNumber;
            };
            EventHandler <CollectionItemEventArgs <Process> > bp_debugger_ProcessExited = (sender, e) => {
                setBookmarkColor();
            };

            EventHandler <BreakpointEventArgs> bp_debugger_BreakpointHit =
                new EventHandler <BreakpointEventArgs>(
                    delegate(object sender, BreakpointEventArgs e)
            {
                LoggingService.Debug(bookmark.Action + " " + bookmark.ScriptLanguage + " " + bookmark.Condition);

                switch (bookmark.Action)
                {
                case BreakpointAction.Break:
                    break;

                case BreakpointAction.Condition:
                    if (Evaluate(bookmark.Condition, bookmark.ScriptLanguage))
                    {
                        DebuggerService.PrintDebugMessage(string.Format(StringParser.Parse("${res:MainWindow.Windows.Debug.Conditional.Breakpoints.BreakpointHitAtBecause}") + "\n", bookmark.LineNumber, bookmark.FileName, bookmark.Condition));
                    }
                    else
                    {
                        this.debuggedProcess.AsyncContinue();
                    }
                    break;

                case BreakpointAction.Trace:
                    DebuggerService.PrintDebugMessage(string.Format(StringParser.Parse("${res:MainWindow.Windows.Debug.Conditional.Breakpoints.BreakpointHitAt}") + "\n", bookmark.LineNumber, bookmark.FileName));
                    break;
                }
            });

            BookmarkEventHandler bp_bookmarkManager_Removed = null;

            bp_bookmarkManager_Removed = (sender, e) => {
                if (bookmark == e.Bookmark)
                {
                    debugger.Breakpoints.Remove(breakpoint);

                    // unregister the events
                    debugger.Processes.Added   -= bp_debugger_ProcessStarted;
                    debugger.Processes.Removed -= bp_debugger_ProcessExited;
                    breakpoint.Hit             -= bp_debugger_BreakpointHit;
                    BookmarkManager.Removed    -= bp_bookmarkManager_Removed;
                }
            };
            // register the events
            debugger.Processes.Added   += bp_debugger_ProcessStarted;
            debugger.Processes.Removed += bp_debugger_ProcessExited;
            breakpoint.Hit             += bp_debugger_BreakpointHit;
            BookmarkManager.Removed    += bp_bookmarkManager_Removed;
        }