Ejemplo n.º 1
0
        public virtual void DisplayCurrentLocation()
        {
            if (!Debugger.Processes.HaveActive)
            {
                CommandBase.WriteOutput("STOP: Process Exited");
                return; // don't try to display current location
            }
            else
            {
                Debug.Assert(Debugger.Processes.HaveActive);
                Object stopReason     = Debugger.Processes.Active.StopReason;
                Type   stopReasonType = stopReason.GetType();
                if (stopReasonType == typeof(StepCompleteStopReason))
                {
                    // just ignore those
                }
                else if (stopReasonType == typeof(ThreadCreatedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Thread Created");
                }
                else if (stopReasonType == typeof(BreakpointHitStopReason))
                {
                    MDbgBreakpoint b = (stopReason as BreakpointHitStopReason).Breakpoint;
                    if (b.Number == 0)                     // specal case to keep compatibility with our test scripts.
                    {
                        CommandBase.WriteOutput("STOP: Breakpoint Hit");
                    }
                    else
                    {
                        CommandBase.WriteOutput(String.Format(CultureInfo.InvariantCulture, "STOP: Breakpoint {0} Hit", new Object[] { b.Number }));
                    }
                }
                else if (stopReasonType == typeof(ExceptionThrownStopReason))
                {
                    ExceptionThrownStopReason ex = (ExceptionThrownStopReason)stopReason;
                    CommandBase.WriteOutput("STOP: Exception thrown");
                    PrintCurrentException();
                    if (Debugger.Options.StopOnExceptionEnhanced ||
                        ex.ExceptionEnhancedOn)
                    {
                        // when we are in ExceptionEnhanced mode, we print more information
                        CommandBase.WriteOutput("\tOffset:    " + ex.Offset);
                        CommandBase.WriteOutput("\tEventType: " + ex.EventType);
                        CommandBase.WriteOutput("\tIntercept: " + (ex.Flags != 0));
                    }
                }
                else if (stopReasonType == typeof(UnhandledExceptionThrownStopReason))
                {
                    CommandBase.WriteOutput("STOP: Unhandled Exception thrown");
                    PrintCurrentException();
                    CommandBase.WriteOutput("");
                    CommandBase.WriteOutput("This is unhandled exception, continuing will end the process");
                }
                else if (stopReasonType == typeof(ExceptionUnwindStopReason))
                {
                    CommandBase.WriteOutput("STOP: Exception unwind");
                    CommandBase.WriteOutput("EventType: " + (stopReason as ExceptionUnwindStopReason).EventType);
                }
                else if (stopReasonType == typeof(ModuleLoadedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Module loaded: " + (stopReason as ModuleLoadedStopReason).Module.CorModule.Name);
                }
                else if (stopReasonType == typeof(AssemblyLoadedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Assembly loaded: " + (stopReason as AssemblyLoadedStopReason).Assembly.Name);
                }
                else if (stopReasonType == typeof(MDANotificationStopReason))
                {
                    CorMDA mda = (stopReason as MDANotificationStopReason).CorMDA;

                    CommandBase.WriteOutput("STOP: MDANotification");
                    CommandBase.WriteOutput("Name=" + mda.Name);
                    CommandBase.WriteOutput("XML=" + mda.XML);
                }
                else if (stopReasonType == typeof(MDbgErrorStopReason))
                {
                    Exception e = (stopReason as MDbgErrorStopReason).ExceptionThrown;
                    CommandBase.WriteOutput("STOP: MdbgError");
                    CommandBase.WriteOutput(FormatExceptionDiagnosticText(e));
                }
                else
                {
                    CommandBase.WriteOutput("STOP " + Debugger.Processes.Active.StopReason);
                }
            }

            if (!Debugger.Processes.Active.Threads.HaveActive)
            {
                return;                                     // we won't try to show current location
            }

            MDbgThread thr = Debugger.Processes.Active.Threads.Active;

            MDbgSourcePosition pos = thr.CurrentSourcePosition;

            if (pos == null)
            {
                MDbgFrame f = thr.CurrentFrame;
                if (f.IsManaged)
                {
                    CorDebugMappingResult mappingResult;
                    uint ip;
                    f.CorFrame.GetIP(out ip, out mappingResult);
                    string s = "IP: " + ip + " @ " + f.Function.FullName + " - " + mappingResult;
                    CommandBase.WriteOutput(s);
                }
                else
                {
                    CommandBase.WriteOutput("<Located in native code.>");
                }
            }
            else
            {
                string fileLoc = FileLocator.GetFileLocation(pos.Path);
                if (fileLoc == null)
                {
                    // Using the full path makes debugging output inconsistant during automated test runs.
                    // For testing purposes we'll get rid of them.
                    //CommandBase.WriteOutput("located at line "+pos.Line + " in "+ pos.Path);
                    CommandBase.WriteOutput("located at line " + pos.Line + " in " + System.IO.Path.GetFileName(pos.Path));
                }
                else
                {
                    IMDbgSourceFile file      = SourceFileMgr.GetSourceFile(fileLoc);
                    string          prefixStr = pos.Line.ToString(CultureInfo.InvariantCulture) + ":";

                    if (pos.Line < 1 || pos.Line > file.Count)
                    {
                        CommandBase.WriteOutput("located at line " + pos.Line + " in " + pos.Path);
                        throw new MDbgShellException(string.Format("Could not display current location; file {0} doesn't have line {1}.",
                                                                   file.Path, pos.Line));
                    }
                    Debug.Assert((pos.Line > 0) && (pos.Line <= file.Count));
                    string lineContent = file[pos.Line];

                    if (pos.StartColumn == 0 && pos.EndColumn == 0 ||
                        !(CommandBase.Shell.IO is IMDbgIO2))    // or we don't have support for IMDbgIO2
                    {
                        // we don't know location in the line
                        CommandBase.Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput, prefixStr + lineContent + "\n");
                    }
                    else
                    {
                        int hiStart;
                        if (pos.StartColumn > 0)
                        {
                            hiStart = pos.StartColumn - 1;
                        }
                        else
                        {
                            hiStart = 0;
                        }

                        int hiLen;
                        if (pos.EndColumn == 0 ||             // we don't know ending position
                            (pos.EndLine > pos.StartLine))    // multi-line statement, select whole 1st line
                        {
                            hiLen = lineContent.Length;
                        }
                        else
                        {
                            hiLen = pos.EndColumn - 1 - hiStart;
                        }
                        Debug.Assert(CommandBase.Shell.IO is IMDbgIO2); // see if condition above
                        (CommandBase.Shell.IO as IMDbgIO2).WriteOutput(MDbgOutputConstants.StdOutput, prefixStr + lineContent + "\n",
                                                                       hiStart + prefixStr.Length, hiLen);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public virtual LocationState DisplayCurrentLocation()
        {
            var locationState = new LocationState(ProcessStateEnum.Stop);

            if (!Debugger.Processes.HaveActive)
            {
                CommandBase.WriteOutput("STOP: Process Exited");
                return(new LocationState(processState: ProcessStateEnum.Stop, message: "Process Exited"));
            }
            else
            {
                Debug.Assert(Debugger.Processes.HaveActive);
                Object stopReason     = Debugger.Processes.Active.StopReason;
                Type   stopReasonType = stopReason.GetType();
                if (stopReasonType == typeof(StepCompleteStopReason))
                {
                    // just ignore those
                }
                else if (stopReasonType == typeof(ThreadCreatedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Thread Created");
                    return(new LocationState(processState: ProcessStateEnum.Stop, message: "Thread Created"));
                }
                else if (stopReasonType == typeof(BreakpointHitStopReason))
                {
                    MDbgBreakpoint b = (stopReason as BreakpointHitStopReason).Breakpoint;
                    if (b.Number == 0)                     // specal case to keep compatibility with our test scripts.
                    {
                        CommandBase.WriteOutput("STOP: Breakpoint Hit");
                        locationState = new LocationState(processState: ProcessStateEnum.Stop, message: "Breakpoint Hit");
                    }
                    else
                    {
                        CommandBase.WriteOutput(String.Format(CultureInfo.InvariantCulture, "STOP: Breakpoint {0} Hit", new Object[] { b.Number }));
                        locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Breakpoint { b.Number } Hit");
                    }
                }

                else if (stopReasonType == typeof(ExceptionThrownStopReason))
                {
                    ExceptionThrownStopReason ex = (ExceptionThrownStopReason)stopReason;
                    CommandBase.WriteOutput("STOP: Exception thrown");
                    PrintCurrentException();
                    if (Debugger.Options.StopOnExceptionEnhanced ||
                        ex.ExceptionEnhancedOn)
                    {
                        // when we are in ExceptionEnhanced mode, we print more information
                        CommandBase.WriteOutput("\tOffset:    " + ex.Offset);
                        CommandBase.WriteOutput("\tEventType: " + ex.EventType);
                        CommandBase.WriteOutput("\tIntercept: " + (ex.Flags != 0));
                    }

                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Exception thrown");
                }

                else if (stopReasonType == typeof(UnhandledExceptionThrownStopReason))
                {
                    CommandBase.WriteOutput("STOP: Unhandled Exception thrown");
                    PrintCurrentException();
                    CommandBase.WriteOutput("");
                    CommandBase.WriteOutput("This is unhandled exception, continuing will end the process");

                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Unhandled Exception thrown");
                }

                else if (stopReasonType == typeof(ExceptionUnwindStopReason))
                {
                    CommandBase.WriteOutput("STOP: Exception unwind");
                    CommandBase.WriteOutput("EventType: " + (stopReason as ExceptionUnwindStopReason).EventType);

                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Exception unwind");
                }

                else if (stopReasonType == typeof(ModuleLoadedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Module loaded: " + (stopReason as ModuleLoadedStopReason).Module.CorModule.Name);
                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Module loaded: {(stopReason as ModuleLoadedStopReason).Module.CorModule.Name}");
                }
                else if (stopReasonType == typeof(AssemblyLoadedStopReason))
                {
                    CommandBase.WriteOutput("STOP: Assembly loaded: " + (stopReason as AssemblyLoadedStopReason).Assembly.Name);
                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"Assembly loaded: {(stopReason as AssemblyLoadedStopReason).Assembly.Name}");
                }
                else if (stopReasonType == typeof(MDANotificationStopReason))
                {
                    CorMDA mda = (stopReason as MDANotificationStopReason).CorMDA;

                    CommandBase.WriteOutput("STOP: MDANotification");
                    CommandBase.WriteOutput("Name=" + mda.Name);
                    CommandBase.WriteOutput("XML=" + mda.XML);
                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"MDANotification");
                }
                else if (stopReasonType == typeof(MDbgErrorStopReason))
                {
                    Exception e = (stopReason as MDbgErrorStopReason).ExceptionThrown;
                    CommandBase.WriteOutput("STOP: MdbgError");
                    CommandBase.WriteOutput(FormatExceptionDiagnosticText(e));
                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: $"MdbgError");
                }
                else
                {
                    CommandBase.WriteOutput("STOP " + Debugger.Processes.Active.StopReason);
                    locationState = new LocationState(processState: ProcessStateEnum.Stop, message: Debugger.Processes.Active.StopReason.ToString());
                }
            }

            if (!Debugger.Processes.Active.Threads.HaveActive)
            {
                return(locationState);
            }

            MDbgThread thr = Debugger.Processes.Active.Threads.Active;

            MDbgSourcePosition pos = thr.CurrentSourcePosition;

            if (pos == null)
            {
                MDbgFrame f = thr.CurrentFrame;
                if (f.IsManaged)
                {
                    CorDebugMappingResult mappingResult;
                    uint offset;
                    f.CorFrame.GetIP(out offset, out mappingResult);
                    CommandBase.WriteOutput($"Offset:{offset} Function:{f.Function.FullName}");
                    locationState = new LocationState(processState: ProcessStateEnum.Running, message: $"Offset:{offset} Function:{f.Function.FullName}", function: f.Function.FullName);
                }
                else
                {
                    CommandBase.WriteOutput("<Located in native code.>");
                    locationState = new LocationState(processState: ProcessStateEnum.Running, message: "<Located in native code.>");
                }
            }
            else
            {
                string    fileLoc = FileLocator.GetFileLocation(pos.Path);
                MDbgFrame f       = thr.CurrentFrame;
                if (fileLoc == null)
                {
                    // Using the full path makes debugging output inconsistant during automated test runs.
                    // For testing purposes we'll get rid of them.
                    //CommandBase.WriteOutput("located at line "+pos.Line + " in "+ pos.Path);
                    Console.WriteLine($@"ln:{pos.Line} col:{pos.StartColumn}:{pos.EndColumn} in [{f.Function.FullName}] - {pos.Path}");
                    locationState = new LocationState(processState: ProcessStateEnum.Running, function: f.Function.FullName, lineNumber: pos.Line, file: pos.Path);
                }
                else
                {
                    IMDbgSourceFile file      = SourceFileMgr.GetSourceFile(fileLoc);
                    string          prefixStr = pos.Line.ToString(CultureInfo.InvariantCulture) + ":";

                    if (pos.Line < 1 || pos.Line > file.Count)
                    {
                        Console.WriteLine($@"ln:{pos.Line} col:{pos.StartColumn}:{pos.EndColumn} in [{f.Function.FullName}] - {pos.Path}");
                        locationState = new LocationState(processState: ProcessStateEnum.Running, function: f.Function.FullName, lineNumber: pos.Line, file: pos.Path);
                        throw new MDbgShellException(string.Format("Could not display current location; file {0} doesn't have line {1}.",
                                                                   file.Path, pos.Line));
                    }
                    Debug.Assert((pos.Line > 0) && (pos.Line <= file.Count));
                    string lineContent = file[pos.Line];

                    if (pos.StartColumn == 0 && pos.EndColumn == 0 ||
                        !(CommandBase.Shell.IO is IMDbgIO2))    // or we don't have support for IMDbgIO2
                    {
                        // we don't know location in the line
                        CommandBase.Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput, prefixStr + lineContent + "\n");
                    }
                    else
                    {
                        Console.WriteLine($@"ln:{pos.Line} col:{pos.StartColumn}:{pos.EndColumn} in [{f.Function.FullName}] - {pos.Path}");
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(lineContent);
                        Console.ResetColor();

                        locationState = new LocationState(processState: ProcessStateEnum.Running, function: f.Function.FullName, lineNumber: pos.Line, file: pos.Path, code: lineContent);
                    }
                }
            }

            return(locationState);
        }