Exemplo n.º 1
0
        public static void WriteThisToLog(LocationState locationState)
        {
            if (locationState.File != null && locationState.LineNumber > -1)
            {
                LocationState = locationState;
            }

            if (locationState != null && WriteToLog && locationState.ProcessState == ProcessStateEnum.Running && locationState.FileName != string.Empty)
            {
                var text = $"L; {locationState.File}; {locationState.Function}; {locationState.LineNumber}; {locationState.Code}";

                if (_buffer != null)
                {
                    _buffer.Line(text);
                }
                else
                {
                    File.AppendAllLines(LogFilePath, new string[] { text });
                }
            }
        }
Exemplo 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);
        }