public static void FuncVersionCmd(string args)
        {
            var          ap = new ArgParser(args);
            MDbgFunction function;

            if (ap.AsString(0) == ".")
            {
                function = Debugger.Processes.Active.Threads.Active.CurrentFrame.Function;
            }
            else
            {
                function = Debugger.Processes.Active.ResolveFunctionNameFromScope(ap.AsString(0));
            }
            WriteOutput("Current version: " + function.CorFunction.Version);
        }
Esempio n. 2
0
        public static void Con(string args)
        {
            ArgParser ap = new ArgParser(args);

            if (ap.Exists(0))
            {
                if (ap.AsString(0) == "close")
                {
                    if (_console != null)
                    {
                        _console.Stop();
                        return;
                    }
                    else
                    {
                        throw new MDbgShellException("Console not started.");
                    }
                }
                else
                {
                    throw new MDbgShellException("invalid argument");
                }
            }

            _console = new Screen(Shell);
            _console.Start();
        }
Esempio n. 3
0
        public static void WhereCmd(string arguments, O2Thread.FuncVoidT1 <string> o2Callback)
        {
            const int default_depth = 100; // default number of frames to print

            const string countOpt   = "c";
            const string verboseOpt = "v";

            var ap    = new ArgParser(arguments, countOpt + ":1;" + verboseOpt);
            int depth = default_depth;

            if (ap.OptionPassed(countOpt))
            {
                ArgToken countArg = ap.GetOption(countOpt);
                if (countArg.AsString == "all")
                {
                    depth = 0; // 0 means print entire stack0
                }
                else
                {
                    depth = countArg.AsInt;
                    if (depth <= 0)
                    {
                        throw new MDbgShellException("Depth must be positive number or string \"all\"");
                    }
                }
            }
            if (ap.Count != 0 && ap.Count != 1)
            {
                throw new MDbgShellException("Wrong # of arguments.");
            }

            if (ap.Count == 0)
            {
                // print current thread only
                InternalWhereCommand(CommandBase.Debugger.Processes.Active.Threads.Active, depth, ap.OptionPassed(verboseOpt));
            }
            else if (ap.AsString(0).Equals("all"))
            {
                foreach (MDbgThread t in CommandBase.Debugger.Processes.Active.Threads)
                {
                    InternalWhereCommand(t, depth, ap.OptionPassed(verboseOpt));
                }
            }
            else
            {
                MDbgThread t = CommandBase.Debugger.Processes.Active.Threads[ap.AsInt(0)];
                if (t == null)
                {
                    throw new MDbgShellException("Wrong thread number");
                }
                else
                {
                    InternalWhereCommand(t, depth, ap.OptionPassed(verboseOpt));
                }
            }
        }
        public static void ILDasmCmd(string args)
        {
            var ap = new ArgParser(args);
            if (ap.Count > 1)
            {
                WriteError("Wrong # of arguments.");
                return;
            }

            MDbgFrame functionFrame = null;
            MDbgFunction function = null;
            if (ap.Exists(0))
            {
                function = Debugger.Processes.Active.ResolveFunctionNameFromScope(ap.AsString(0));
                if (function == null)
                    throw new MDbgShellException("no such function.");
            }
            else
            {
                functionFrame = Debugger.Processes.Active.Threads.Active.CurrentFrame;
                function = functionFrame.Function;
            }


            CorCode ilCode = function.CorFunction.ILCode;
            //CorMetadataImport importer = functionFrame.Function.Module.Importer;
            Debug.Assert(ilCode.IsIL);

            WriteOutput("code size: " + ilCode.Size);

            var doc = new ILVirtualDocument(function);

            int currentLine;
            if (functionFrame != null)
            {
                // we have frame and therefore we should show current location
                uint ip;
                CorDebugMappingResult mappingResult;
                functionFrame.CorFrame.GetIP(out ip, out mappingResult);
                WriteOutput("current IL-IP: " + ip);
                WriteOutput("mapping: " + mappingResult);
                WriteOutput("URL: " + doc.Path);
                currentLine = doc.Ip2LineNo((int) ip);
            }
            else
            {
                // no current IP.
                currentLine = -1;
            }
            for (int i = 0; i < doc.Count; i++)
                if (i == currentLine)
                    WriteOutput("*  " + doc[i]);
                else
                    WriteOutput("   " + doc[i]);
        }
Esempio n. 5
0
        public static void Gui(string args)
        {
            // just do the check that gui is not already loaded,
            // strange things are happening else:
            var ap = new ArgParser(args);

            if (ap.Exists(0))
            {
                if (ap.AsString(0) == "close")
                {
                    if (m_mainForm != null)
                    {
                        m_mainForm.CloseGui();
                        Application.Exit(); // this line will cause the message pump on other thread to quit.
                        return;
                    }
                    else
                    {
                        throw new MDbgShellException("GUI not started.");
                    }
                }
                else
                {
                    throw new MDbgShellException("invalid argument");
                }
            }

            if (Shell.IO == m_mainForm)
            {
                WriteOutput("GUI already started. Cannot start second instance.");
                return;
            }

            WriteOutput("starting gui");

            m_mainForm = new MainForm(Shell);

            var t = new Thread(RunMessageLoop);

            // Only MTA Threads can access CorDebug interfaces because mscordbi doesn't provide marshalling to make them accessable from STA Threads
            // However, UI thread must be STA. So we'll make cross-thread calls to a worker thread for all ICorDebug access.
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;

            t.Start();
            m_mainForm.InitComplete.WaitOne(); // wait till form is fully displayed.

            WriteOutput(MainForm.MetaInfoOutputConstant, "GUI: Simple Extension for Managed debugger (MDbg) started");
            WriteOutput(MainForm.MetaInfoOutputConstant,
                        "for information on how to use the extension select in menu bar help|about");
            WriteOutput(MainForm.MetaInfoOutputConstant,
                        "!!This is just a sample. It is not intended for Production purposes!!\n");
        }
        public static void Gui(string args)
        {
            // just do the check that gui is not already loaded,
            // strange things are happening else:
            var ap = new ArgParser(args);
            if (ap.Exists(0))
            {
                if (ap.AsString(0) == "close")
                {
                    if (m_mainForm != null)
                    {
                        m_mainForm.CloseGui();
                        Application.Exit(); // this line will cause the message pump on other thread to quit.
                        return;
                    }
                    else
                        throw new MDbgShellException("GUI not started.");
                }
                else
                    throw new MDbgShellException("invalid argument");
            }

            if (Shell.IO == m_mainForm)
            {
                WriteOutput("GUI already started. Cannot start second instance.");
                return;
            }

            WriteOutput("starting gui");

            m_mainForm = new MainForm(Shell);

            var t = new Thread(RunMessageLoop);

            // Only MTA Threads can access CorDebug interfaces because mscordbi doesn't provide marshalling to make them accessable from STA Threads
            // However, UI thread must be STA. So we'll make cross-thread calls to a worker thread for all ICorDebug access.
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;

            t.Start();
            m_mainForm.InitComplete.WaitOne(); // wait till form is fully displayed.

            WriteOutput(MainForm.MetaInfoOutputConstant, "GUI: Simple Extension for Managed debugger (MDbg) started");
            WriteOutput(MainForm.MetaInfoOutputConstant,
                        "for information on how to use the extension select in menu bar help|about");
            WriteOutput(MainForm.MetaInfoOutputConstant,
                        "!!This is just a sample. It is not intended for Production purposes!!\n");
        }
Esempio n. 7
0
        public static void Gui(string args)
        {
            //System.Diagnostics.Debug.Assert(false);

            if (nppDebugger == null)
            {
                nppDebugger = new DebuggerClient(Shell);
            }

            var ap = new ArgParser(args);

            if (ap.Exists(0))
            {
                if (ap.AsString(0) == "close")
                {
                    if (nppDebugger != null)
                    {
                        nppDebugger.Close();
                        Application.Exit(); // this line will cause the message pump on other thread to quit.
                        return;
                    }
                    else
                    {
                        throw new MDbgShellException("NPP not started.");
                    }
                }
                else
                {
                    throw new MDbgShellException("invalid argument");
                }
            }

            if (Shell.IO == nppDebugger)
            {
                WriteOutput("NPP already started. Cannot start second instance.");
                return;
            }

            WriteOutput("starting npp");

            Shell.IO = nppDebugger;
        }
        public static void EncCmd(string args)
        {
            var ap = new ArgParser(args);

            if (ap.Count == 0)
            {
                WriteOutput("Performed edits:");
                foreach (MDbgModule module in Debugger.Processes.Active.Modules)
                {
                    if (module.EditsCounter > 0)
                    {
                        WriteOutput(module.CorModule.Name);
                        int edits = module.EditsCounter;
                        for (int j = 1; j <= edits; j++)
                        {
                            string editFile = module.GetEditsSourceFile(j);
                            WriteOutput(String.Format(CultureInfo.InvariantCulture, "   {0}. - {1}",
                                                      new Object[] { j, editFile == null ? "N/A" : editFile }));
                        }
                    }
                }
                return;
            }
            if (ap.Count < 1 && ap.Count > 3)
            {
                WriteError("Wrong amount of arguments!");
                return;
            }
            string encModule = ap.AsString(0);

            MDbgModule m       = Debugger.Processes.Active.Modules.Lookup(encModule);
            string     modName = Path.DirectorySeparatorChar +
                                 Path.GetFileName(m.CorModule.Name);

            string pathToEncFiles = Path.GetDirectoryName(m.CorModule.Name); // defaults to location of the module

            string editSourceFile;

            if (ap.Exists(1))
            {
                editSourceFile = ap.AsString(1);
            }
            else
            {
                editSourceFile = null;
            }

            string deltasBaseName;

            if (ap.Exists(2))
            {
                deltasBaseName = ap.AsString(2);
            }
            else
            {
                deltasBaseName = pathToEncFiles + modName + ".1";
            }

            string deltaPdbFile = deltasBaseName + ".pdb";

            if (!File.Exists(deltaPdbFile))
            {
                WriteOutput("Delta PDB file not found; debug symbols won't be updated.");
                deltaPdbFile = null;
            }

            m.ApplyEdit(deltasBaseName + ".dmeta",
                        deltasBaseName + ".dil",
                        deltaPdbFile,
                        editSourceFile);

            WriteOutput("ENC successfully applied.");
        }
Esempio n. 9
0
        public static void ILDasmCmd(string args)
        {
            // Provides disassembly of IL opcodes.

            ArgParser ap = new ArgParser(args);

            if (ap.Count > 1)
            {
                WriteError("Wrong # of arguments.");
                return;
            }

            MDbgFrame    functionFrame = null;
            MDbgFunction function      = null;

            if (ap.Exists(0))
            {
                function = Debugger.Processes.Active.ResolveFunctionNameFromScope(ap.AsString(0));
                if (function == null)
                {
                    throw new MDbgShellException("no such function.");
                }
            }
            else
            {
                functionFrame = Debugger.Processes.Active.Threads.Active.CurrentFrame;
                function      = functionFrame.Function;
            }


            CorCode ilCode = function.CorFunction.ILCode;

            //CorMetadataImport importer = functionFrame.Function.Module.Importer;
            Debug.Assert(true == ilCode.IsIL);

            WriteOutput("code size: " + ilCode.Size);

            ILVirtualDocument doc = new ILVirtualDocument(function);

            int currentLine;

            if (functionFrame != null)
            {
                // we have frame and therefore we should show current location
                uint ip;
                CorDebugMappingResult mappingResult;
                functionFrame.CorFrame.GetIP(out ip, out mappingResult);
                WriteOutput("current IL-IP: " + ip);
                WriteOutput("mapping: " + mappingResult.ToString());
                WriteOutput("URL: " + doc.Path);
                currentLine = doc.Ip2LineNo((int)ip);
            }
            else
            {
                // no current IP.
                currentLine = -1;
            }
            for (int i = 0; i < doc.Count; i++)
            {
                if (i == currentLine)
                {
                    WriteOutput("*  " + doc[i]);
                }
                else
                {
                    WriteOutput("   " + doc[i]);
                }
            }
        }
        public static void EncCmd(string args)
        {
            var ap = new ArgParser(args);
            if (ap.Count == 0)
            {
                WriteOutput("Performed edits:");
                foreach (MDbgModule module in Debugger.Processes.Active.Modules)
                {
                    if (module.EditsCounter > 0)
                    {
                        WriteOutput(module.CorModule.Name);
                        int edits = module.EditsCounter;
                        for (int j = 1; j <= edits; j++)
                        {
                            string editFile = module.GetEditsSourceFile(j);
                            WriteOutput(String.Format(CultureInfo.InvariantCulture, "   {0}. - {1}",
                                                      new Object[] {j, editFile == null ? "N/A" : editFile}));
                        }
                    }
                }
                return;
            }
            if (ap.Count < 1 && ap.Count > 3)
            {
                WriteError("Wrong amount of arguments!");
                return;
            }
            string encModule = ap.AsString(0);

            MDbgModule m = Debugger.Processes.Active.Modules.Lookup(encModule);
            string modName = Path.DirectorySeparatorChar +
                             Path.GetFileName(m.CorModule.Name);

            string pathToEncFiles = Path.GetDirectoryName(m.CorModule.Name); // defaults to location of the module

            string editSourceFile;
            if (ap.Exists(1))
                editSourceFile = ap.AsString(1);
            else
                editSourceFile = null;

            string deltasBaseName;
            if (ap.Exists(2))
                deltasBaseName = ap.AsString(2);
            else
                deltasBaseName = pathToEncFiles + modName + ".1";

            string deltaPdbFile = deltasBaseName + ".pdb";
            if (!File.Exists(deltaPdbFile))
            {
                WriteOutput("Delta PDB file not found; debug symbols won't be updated.");
                deltaPdbFile = null;
            }

            m.ApplyEdit(deltasBaseName + ".dmeta",
                        deltasBaseName + ".dil",
                        deltaPdbFile,
                        editSourceFile);

            WriteOutput("ENC successfully applied.");
        }
 public static void FuncVersionCmd(string args)
 {
     var ap = new ArgParser(args);
     MDbgFunction function;
     if (ap.AsString(0) == ".")
         function = Debugger.Processes.Active.Threads.Active.CurrentFrame.Function;
     else
         function = Debugger.Processes.Active.ResolveFunctionNameFromScope(ap.AsString(0));
     WriteOutput("Current version: " + function.CorFunction.Version);
 }
Esempio n. 12
0
        public static void PrintCmd(string arguments, O2Thread.FuncVoidT1 <string> o2Callback)
        {
            const string debuggerVarsOpt = "d";
            const string noFuncevalOpt   = "nf";
            const string expandDepthOpt  = "r";

            var  ap            = new ArgParser(arguments, debuggerVarsOpt + ";" + noFuncevalOpt + ";" + expandDepthOpt + ":1");
            bool canDoFunceval = !ap.OptionPassed(noFuncevalOpt);

            int?expandDepth = null;  // we use optional here because

            // different codes bellow has different
            // default values.
            if (ap.OptionPassed(expandDepthOpt))
            {
                expandDepth = ap.GetOption(expandDepthOpt).AsInt;
                if (expandDepth < 0)
                {
                    throw new MDbgShellException("Depth cannot be negative.");
                }
            }

            MDbgFrame frame = CommandBase.Debugger.Processes.Active.Threads.Active.CurrentFrame;

            if (ap.OptionPassed(debuggerVarsOpt))
            {
                // let's print all debugger variables
                MDbgProcess p = CommandBase.Debugger.Processes.Active;
                foreach (MDbgDebuggerVar dv in p.DebuggerVars)
                {
                    var v = new MDbgValue(p, dv.CorValue);
                    CommandBase.WriteOutput(dv.Name + "=" + v.GetStringValue(expandDepth == null ? 0 : (int)expandDepth,
                                                                             canDoFunceval));
                }
            }
            else
            {
                if (ap.Count == 0)
                {
                    // get all active variables
                    MDbgFunction f = frame.Function;

                    var         vars = new ArrayList();
                    MDbgValue[] vals = f.GetActiveLocalVars(frame);
                    if (vals != null)
                    {
                        vars.AddRange(vals);
                    }

                    vals = f.GetArguments(frame);
                    if (vals != null)
                    {
                        vars.AddRange(vals);
                    }
                    foreach (MDbgValue v in vars)
                    {
                        CommandBase.WriteOutput(v.Name + "=" + v.GetStringValue(expandDepth == null ? 0 : (int)expandDepth,
                                                                                canDoFunceval));
                    }
                }
                else
                {
                    // user requested printing of specific variables
                    for (int j = 0; j < ap.Count; ++j)
                    {
                        MDbgValue var = CommandBase.Debugger.Processes.Active.ResolveVariable(ap.AsString(j), frame);
                        if (var != null)
                        {
                            CommandBase.WriteOutput(ap.AsString(j) + "=" + var.GetStringValue(expandDepth == null
                                                                                      ? 1
                                                                                      : (int)expandDepth, canDoFunceval));
                        }
                        else
                        {
                            throw new MDbgShellException("Variable not found");
                        }
                    }
                }
            }
        }
Esempio n. 13
0
        public static void FuncEvalCmd(string arguments, IMDbgShell Shell, O2Thread.FuncVoidT1 <string> execOnEval)
        {
            try
            {
                var          activeProcess   = DI.o2MDbg.ActiveProcess; //Debugger.Processes.Active
                const string appDomainOption = "ad";
                var          ap = new ArgParser(arguments, appDomainOption + ":1");
                if (!(ap.Count >= 1))
                {
                    throw new MDbgShellException("Not Enough arguments");
                }


                // Currently debugger picks first function -- we have not implementing resolving overloaded functions.
                // Good example is Console.WriteLine -- there is 18 different types:
                // 1) [06000575] Void WriteLine()
                // 2) [06000576] Void WriteLine(Boolean)
                // 3) [06000577] Void WriteLine(Char)
                // 4) [06000578] Void WriteLine(Char[])
                // 5) [06000579] Void WriteLine(Char[], Int32, Int32)
                // 6) [0600057a] Void WriteLine(Decimal)
                // 7) [0600057b] Void WriteLine(Double)
                // 8) [0600057c] Void WriteLine(Single)
                // 9) [0600057d] Void WriteLine(Int32)
                // 10) [0600057e] Void WriteLine(UInt32)
                // 11) [0600057f] Void WriteLine(Int64)
                // 12) [06000580] Void WriteLine(UInt64)
                // 13) [06000581] Void WriteLine(Object)
                // 14) [06000582] Void WriteLine(String)
                // 15) [06000583] Void WriteLine(String, Object)
                // 16) [06000584] Void WriteLine(String, Object, Object)
                // 17) [06000585] Void WriteLine(String, Object, Object, Object)
                // 18) [06000586] Void WriteLine(String, Object, Object, Object, Object, ...)
                // 19) [06000587] Void WriteLine(String, Object[])
                //
                CorAppDomain appDomain;
                if (ap.OptionPassed(appDomainOption))
                {
                    MDbgAppDomain ad = activeProcess.AppDomains[ap.GetOption(appDomainOption).AsInt];
                    if (ad == null)
                    {
                        throw new ArgumentException("Invalid Appdomain Number");
                    }
                    appDomain = ad.CorAppDomain;
                }
                else
                {
                    appDomain = activeProcess.Threads.Active.CorThread.AppDomain;
                }

                MDbgFunction func = activeProcess.ResolveFunctionNameFromScope(ap.AsString(0), appDomain);
                if (null == func)
                {
                    throw new MDbgShellException(String.Format(CultureInfo.InvariantCulture, "Could not resolve {0}",
                                                               new Object[] { ap.AsString(0) }));
                }

                CorEval eval = activeProcess.Threads.Active.CorThread.CreateEval();

                // Get Variables
                var    vars = new ArrayList();
                String arg;
                for (int i = 1; i < ap.Count; i++)
                {
                    arg = ap.AsString(i);

                    CorValue v = Shell.ExpressionParser.ParseExpression2(arg, activeProcess,
                                                                         activeProcess.Threads.Active.
                                                                         CurrentFrame);

                    if (v == null)
                    {
                        throw new MDbgShellException("Cannot resolve expression or variable " + ap.AsString(i));
                    }

                    if (v is CorGenericValue)
                    {
                        vars.Add(v);
                    }

                    else
                    {
                        CorHeapValue hv = v.CastToHeapValue();
                        if (hv != null)
                        {
                            // we cannot pass directly heap values, we need to pass reference to heap valus
                            CorReferenceValue myref =
                                eval.CreateValue(CorElementType.ELEMENT_TYPE_CLASS, null).CastToReferenceValue();
                            myref.Value = hv.Address;
                            vars.Add(myref);
                        }
                        else
                        {
                            vars.Add(v);
                        }
                    }
                }

                eval.CallFunction(func.CorFunction, (CorValue[])vars.ToArray(typeof(CorValue)));
                activeProcess.Go().WaitOne();

                // now display result of the funceval
                if (!(activeProcess.StopReason is EvalCompleteStopReason))
                {
                    // we could have received also EvalExceptionStopReason but it's derived from EvalCompleteStopReason
                    Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput,
                                         "Func-eval not fully completed and debuggee has stopped");
                    Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput,
                                         "Result of funceval won't be printed when finished.");
                }
                else
                {
                    eval = (activeProcess.StopReason as EvalCompleteStopReason).Eval;
                    Debug.Assert(eval != null);

                    CorValue cv = eval.Result;
                    if (cv != null)
                    {
                        var mv = new MDbgValue(activeProcess, cv);
                        if (execOnEval != null) // if this callback is set then execute
                        {
                            execOnEval(mv.GetStringValue(1));
                            return;
                        }
                        Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput, "result = " + mv.GetStringValue(1));
                        if (cv.CastToReferenceValue() != null)
                        {
                            if (activeProcess.DebuggerVars.SetEvalResult(cv))
                            {
                                Shell.IO.WriteOutput(MDbgOutputConstants.StdOutput, "results saved to $result");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DI.log.ex(ex, "in FuncEvalCmd");
            }
            if (execOnEval != null)                         // need to call this here so that the sync AutoResetEvent is set
            {
                execOnEval(null);
            }
        }