Esempio n. 1
0
 bool EvaluateCondition(string code)
 {
     try {
         if (CurrentStackFrame == null || CurrentStackFrame.NextStatement == null)
         {
             return(false);
         }
         var val = Evaluate(code);
         if (val != null && val.Type.IsPrimitiveType() && val.PrimitiveValue is bool)
         {
             return((bool)val.PrimitiveValue);
         }
         else
         {
             return(false);
         }
     } catch (GetValueException e) {
         string errorMessage = "Error while evaluating breakpoint condition " + code + ":\n" + e.Message + "\n";
         BaseDebuggerService.PrintDebugMessage(errorMessage);
         SD.MainThread.InvokeAsyncAndForget(() => MessageService.ShowWarning(errorMessage));
         return(true);
     }
 }
Esempio n. 2
0
        void debuggedProcess_DebuggingPaused(object sender, DebuggerPausedEventArgs e)
        {
            OnIsProcessRunningChanged(EventArgs.Empty);

            CurrentProcess = e.Process;
            if (e.Thread != null)
            {
                CurrentThread = e.Thread;
            }
            else if (CurrentThread != null && CurrentThread.HasExited)
            {
                CurrentThread = null;
            }
            CurrentStackFrame = CurrentThread != null ? CurrentThread.MostRecentStackFrame : null;

            // We can have several events happening at the same time
            bool breakProcess = e.Break;

            // Handle thrown exceptions
            foreach (Thread exceptionThread in e.ExceptionsThrown)
            {
                JumpToCurrentLine();

                Thread evalThread = exceptionThread;

                bool         isUnhandled     = (exceptionThread.CurrentExceptionType == ExceptionType.Unhandled);
                Value        exception       = exceptionThread.CurrentException.GetPermanentReferenceOfHeapValue();
                List <Value> innerExceptions = new List <Value>();
                for (Value innerException = exception; !innerException.IsNull; innerException = innerException.GetFieldValue("_innerException"))
                {
                    innerExceptions.Add(innerException.GetPermanentReferenceOfHeapValue());
                }

                // Get the exception description
                string stacktrace = string.Empty;
                for (int i = 0; i < innerExceptions.Count; i++)
                {
                    if (i > 0)
                    {
                        stacktrace += " ---> ";
                    }
                    stacktrace += innerExceptions[i].Type.FullName;
                    Value messageValue = innerExceptions[i].GetFieldValue("_message");
                    if (!messageValue.IsNull)
                    {
                        stacktrace += ": " + messageValue.AsString();
                    }
                }
                stacktrace += Environment.NewLine + Environment.NewLine;

                // Get the stacktrace
                string formatSymbols   = StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.LineFormat.Symbols}");
                string formatNoSymbols = StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.LineFormat.NoSymbols}");
                if (isUnhandled)
                {
                    // Need to intercept now so that we can evaluate properties
                    // Intercept may fail (eg StackOverflow)
                    if (exceptionThread.InterceptException())
                    {
                        try {
                            // Try to evaluate the StackTrace property to get the .NET formated stacktrace
                            for (int i = innerExceptions.Count - 1; i >= 0; i--)
                            {
                                Value stackTraceValue = innerExceptions[i].GetPropertyValue(evalThread, "StackTrace");
                                if (!stackTraceValue.IsNull)
                                {
                                    stacktrace += stackTraceValue.AsString() + Environment.NewLine;
                                }
                                if (i > 0)
                                {
                                    stacktrace += "   " + StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.LineFormat.EndOfInnerException}") + Environment.NewLine;
                                }
                            }
                        } catch (GetValueException) {
                            stacktrace += exceptionThread.GetStackTrace(formatSymbols, formatNoSymbols);
                        }
                    }
                    else
                    {
                        stacktrace += StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.Error.CannotInterceptException}") + Environment.NewLine + Environment.NewLine;
                        stacktrace += exceptionThread.GetStackTrace(formatSymbols, formatNoSymbols);
                    }
                }
                else
                {
                    // Do not intercept handled expetions
                    stacktrace += exceptionThread.GetStackTrace(formatSymbols, formatNoSymbols);
                }

                string title = isUnhandled ? StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.Title.Unhandled}") : StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.Title.Handled}");
                string type  = string.Format(StringParser.Parse("${res:MainWindow.Windows.Debug.ExceptionForm.Message}"), exception.Type);
                Bitmap icon  = WinFormsResourceService.GetBitmap(isUnhandled ? "Icons.32x32.Error" : "Icons.32x32.Warning");

                if (DebuggeeExceptionForm.Show(e.Process, title, type, stacktrace, icon, isUnhandled))
                {
                    breakProcess = true;
                    // The dialog box is allowed to kill the process
                    if (e.Process.HasExited)
                    {
                        return;
                    }
                    // Intercept handled exception *after* the user decided to break
                    if (!isUnhandled)
                    {
                        if (!exceptionThread.InterceptException())
                        {
                            MessageService.ShowError("${res:MainWindow.Windows.Debug.ExceptionForm.Error.CannotInterceptHandledException}");
                        }
                    }
                }
            }

            // Handle breakpoints
            foreach (Breakpoint breakpoint in e.BreakpointsHit)
            {
                var bookmark = SD.BookmarkManager.Bookmarks.OfType <BreakpointBookmark>().First(bm => bm.InternalBreakpointObject == breakpoint);

                if (string.IsNullOrEmpty(bookmark.Condition))
                {
                    breakProcess = true;
                }
                else
                {
                    if (EvaluateCondition(bookmark.Condition))
                    {
                        breakProcess = true;
                        BaseDebuggerService.PrintDebugMessage(string.Format(StringParser.Parse("${res:MainWindow.Windows.Debug.Conditional.Breakpoints.BreakpointHitAtBecause}") + "\n", bookmark.LineNumber, bookmark.FileName, bookmark.Condition));
                    }
                }
            }

            if (breakProcess)
            {
                JumpToCurrentLine();
                RefreshPads();
            }
            else
            {
                e.Process.AsyncContinue();
            }
        }
Esempio n. 3
0
 void LogMessage(object sender, MessageEventArgs e)
 {
     BaseDebuggerService.PrintDebugMessage(e.Message);
 }