public static void ShowCmd(string arguments, O2Thread.FuncVoidT1<string> o2Callback) { MDbgSourcePosition pos = CommandBase.Debugger.Processes.Active.Threads.Active.CurrentSourcePosition; if (pos == null) { throw new MDbgShellException("No source location"); } string fileLoc = CommandBase.Shell.FileLocator.GetFileLocation(pos.Path); if (fileLoc == null) { throw new MDbgShellException(String.Format(CultureInfo.InvariantCulture, "Source file '{0}' not available.", pos.Path)); } IMDbgSourceFile file = CommandBase.Shell.SourceFileMgr.GetSourceFile(fileLoc); var ap = new ArgParser(arguments); if (ap.Count > 1) { throw new MDbgShellException("Wrong # of arguments."); } int around; if (ap.Exists(0)) { around = ap.AsInt(0); } else { around = 3; } int lo, hi; lo = pos.Line - around; if (lo < 1) { lo = 1; } hi = pos.Line + around; if (hi > file.Count) { hi = file.Count; } for (int i = lo; i < hi; i++) { CommandBase.WriteOutput(String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", i, i == pos.Line ? ":*" : " ", file[i])); } }
public static void ModeCmd(string arguments) { // get mode settings first var modeSettings = Shell.Properties[MDbgModeSettings.PropertyName] as MDbgModeSettings; Debug.Assert(modeSettings != null); if (modeSettings == null) throw new MDbgShellException("corrupted internal state."); var ap = new ArgParser(arguments); if (!ap.Exists(0)) { WriteOutput("Debugging modes:"); foreach (MDbgModeItem item in modeSettings.Items) { WriteOutput(string.Format(CultureInfo.InvariantCulture, "({0}) {1}: {2}", item.ShortCut, item.Description.PadRight(50), item.OnOff ? "On" : "Off")); } } else { bool on = ap.AsCommand(1, new CommandArgument("on", "off")) == "on"; string shortcut = ap.AsString(0); // now find the correct modeItem MDbgModeItem item = null; foreach (MDbgModeItem i in modeSettings.Items) if (i.ShortCut == shortcut) { item = i; break; } if (item == null) throw new MDbgShellException("Invalid mode option. Modes are in (here)."); item.OnOff = on; } }
// O2.Debugger.Mdbg.OriginalMdbgCode.mdbg.mdbgCommandsCustomizedForO2 // , O2Thread.FuncVoid<string> o2Callback) public static bool AttachCmd(string arguments, O2Thread.FuncVoidT1<string> o2Callback) { try { var ap = new ArgParser(arguments); if (ap.Count > 1) { DI.log.error("in AttachCmd: Wrong # of arguments."); return false; } if (!ap.Exists(0)) { DI.log.error("in AttachCmd: Please choose some process to attach"); MdbgCommands.ProcessEnumCmd(""); return false; } int pid = ap.AsInt(0); if (Process.GetCurrentProcess().Id == pid) { DI.log.error("in AttachCmd: Cannot attach to myself!"); return false; } MDbgProcess p = CommandBase.Debugger.Attach(pid); p.Go().WaitOne(); return true; } catch (Exception ex) { DI.log.ex(ex, "in AttachCmd"); return false; } }
private static void ThreadResumeSuspendHelper(string arguments, CorDebugThreadState newState) { int threadNumber; var ap = new ArgParser(arguments); if (ap.Exists(0)) { if (ap.AsString(0) == "*") { // do an action on all threads foreach (MDbgThread t in Debugger.Processes.Active.Threads) { SetDebugStateWrapper(t, newState); } } else if (ap.AsString(0).StartsWith("~")) { threadNumber = Int32.Parse(ap.AsString(0).Substring(1), CultureInfo.CurrentUICulture); // it's ~number syntax -- do on all threads except this one. foreach (MDbgThread t in Debugger.Processes.Active.Threads) { if (t.Number != threadNumber) { SetDebugStateWrapper(t, newState); } } } else { MDbgThread t = g_threadNickNames.GetThreadByNickName(ap.AsString(0)); if (t == null) { throw new ArgumentException(); } SetDebugStateWrapper(t, newState); } } else { SetDebugStateWrapper(Debugger.Processes.Active.Threads.Active, newState); } }
public static void InterceptCmd(string arguments) { var ap = new ArgParser(arguments); if (ap.Count > 1) { throw new MDbgShellException("Wrong number of arguments."); } if (!ap.Exists(0)) { throw new MDbgShellException("You must supply a frame number."); } int frameID = ap.AsInt(0); MDbgFrame f = Debugger.Processes.Active.Threads.Active.BottomFrame; while (--frameID >= 0) { f = f.NextUp; if (f == null) { break; } } if (f == null) { throw new MDbgShellException("Invalid frame number."); } CorThread t = Debugger.Processes.Active.Threads.Active.CorThread; t.InterceptCurrentException(f.CorFrame); WriteOutput("Interception point is set."); }
public static void WhenCmd(string arguments) { var ap = new ArgParser(arguments); if (ap.Count == 0) { // we want to list all actions foreach (ExecuteCmdAction a in m_events) { WriteOutput(a.Id + ".\t" + a); } } else if (ap.AsString(0) == "delete") { if (ap.AsString(1) == "all") { // delete all actions m_events.Clear(); } else { int idx = 1; while (true) { int actionToRemove = ap.AsInt(idx); foreach (ExecuteCmdAction a in m_events) { if (a.Id == actionToRemove) { m_events.Remove(a); break; // once we remove an item, we cannot iterate further, // this doesn't matter because id's are unique. } } idx++; if (!ap.Exists(idx)) { break; } } } } else { //we want to create an when action ExecuteCmdAction action = null; string cmdString; int argCount; GetDoPart(ap, out cmdString, out argCount); switch (ap.AsString(0)) { case "StepComplete": action = new ExecuteCmdAction(Shell, cmdString, typeof (StepCompleteStopReason)); switch (argCount) { case 0: break; default: throw new ArgumentException(); } break; case "ProcessExited": action = new ExecuteCmdAction(Shell, cmdString, typeof (ProcessExitedStopReason)); switch (argCount) { case 0: break; default: throw new ArgumentException(); } break; case "ThreadCreated": action = new ExecuteCmdAction(Shell, cmdString, typeof (ThreadCreatedStopReason)); switch (argCount) { case 0: break; case 1: action.Number = ap.AsInt(1); break; default: throw new ArgumentException(); } break; case "BreakpointHit": action = new ExecuteCmdAction(Shell, cmdString, typeof (BreakpointHitStopReason)); switch (argCount) { case 0: break; case 1: action.Number = ap.AsInt(1); break; default: throw new ArgumentException(); } break; case "ModuleLoaded": action = new ExecuteCmdAction(Shell, cmdString, typeof (BreakpointHitStopReason)); switch (argCount) { case 0: break; case 1: action.Name = ap.AsString(1); break; default: throw new ArgumentException(); } break; case "ClassLoaded": action = new ExecuteCmdAction(Shell, cmdString, typeof (ClassLoadedStopReason)); switch (argCount) { case 0: break; case 1: action.Name = ap.AsString(1); break; default: throw new ArgumentException(); } break; case "AssemblyLoaded": action = new ExecuteCmdAction(Shell, cmdString, typeof (AssemblyLoadedStopReason)); switch (argCount) { case 0: break; case 1: action.Name = ap.AsString(1); break; default: throw new ArgumentException(); } break; case "AssemblyUnloaded": throw new NotImplementedException(); case "ControlCTrapped": action = new ExecuteCmdAction(Shell, cmdString, typeof (ControlCTrappedStopReason)); switch (argCount) { case 0: break; default: throw new ArgumentException(); } break; case "ExceptionThrown": action = new ExecuteCmdAction(Shell, cmdString, typeof (ExceptionThrownStopReason)); switch (argCount) { case 0: break; case 1: action.Name = ap.AsString(1); break; default: throw new ArgumentException(); } break; case "UnhandledExceptionThrown": action = new ExecuteCmdAction(Shell, cmdString, typeof (UnhandledExceptionThrownStopReason)); switch (argCount) { case 0: break; case 1: action.Name = ap.AsString(1); break; default: throw new ArgumentException(); } break; case "AsyncStop": action = new ExecuteCmdAction(Shell, cmdString, typeof (AsyncStopStopReason)); switch (argCount) { case 0: break; default: throw new ArgumentException(); } break; case "AttachComplete": if (argCount != 0) { throw new ArgumentException(); } action = new ExecuteCmdAction(Shell, cmdString, typeof (AttachCompleteStopReason)); break; case "UserBreak": if (argCount != 0) { throw new ArgumentException(); } action = new ExecuteCmdAction(Shell, cmdString, typeof (UserBreakStopReason)); break; case "EvalComplete": if (argCount != 0) { throw new ArgumentException(); } action = new ExecuteCmdAction(Shell, cmdString, typeof (EvalCompleteStopReason)); break; case "EvalException": if (argCount != 0) { throw new ArgumentException(); } action = new ExecuteCmdAction(Shell, cmdString, typeof (EvalExceptionStopReason)); break; case "RemapOpportunityReached": if (argCount != 0) { throw new ArgumentException(); } action = new ExecuteCmdAction(Shell, cmdString, typeof (RemapOpportunityReachedStopReason)); break; default: throw new ArgumentException("invalid event name"); } m_events.Add(action); } }
public static void ConfigCmd(string arguments) { const string extPathCmd = "extpath"; const string extPathAddCmd = "extpath+"; var ap = new ArgParser(arguments); if (!ap.Exists(0)) { WriteOutput("Current configuration:"); WriteOutput(string.Format("\tExtensionPath={0}", ExtensionPath)); return; } switch (ap.AsCommand(0, new CommandArgument(extPathCmd, extPathAddCmd))) { case extPathCmd: ExtensionPath = ap.AsString(1); PrintExt: WriteOutput(string.Format("ExtensionPath={0}", ExtensionPath)); break; case extPathAddCmd: ExtensionPath = ExtensionPath + Path.PathSeparator + ap.AsString(1); goto PrintExt; default: Debug.Assert(false); break; } }
public static void PathCmd(string arguments) { var ap = new ArgParser(arguments); if (!ap.Exists(0)) { WriteOutput("path: " + Shell.FileLocator.Path); } else { Shell.FileLocator.Path = arguments; WriteOutput("Path set to: " + Shell.FileLocator.Path); if (Debugger.Processes.HaveActive) { ShowCmd(""); } } }
public static void ActiveProcess(string arguments) { var ap = new ArgParser(arguments); if (ap.Count > 1) { throw new MDbgShellException("Wrong # of arguments."); } if (ap.Exists(0)) { int logicalPID = ap.AsInt(0); bool found = false; foreach (MDbgProcess ps in Debugger.Processes) if (ps.Number == logicalPID) { Debugger.Processes.Active = ps; found = true; break; } if (found) { Shell.DisplayCurrentLocation(); } else { throw new MDbgShellException("Invalid process number"); } } else { MDbgProcess ActiveProcess = Debugger.Processes.HaveActive ? Debugger.Processes.Active : null; WriteOutput("Active Process:"); bool haveProcesses = false; CorPublish corPublish = null; foreach (MDbgProcess p in Debugger.Processes) { haveProcesses = true; string processName = p.Name; string launchMode; if (processName == null) { // in case we're attached (as opposed to launching), // we don't know process name. // Let's find it through CorPublishApi try { if (corPublish == null) { corPublish = new CorPublish(); } processName = corPublish.GetProcess(p.CorProcess.Id).DisplayName; } catch { processName = "N/A"; } launchMode = "attached"; } else { launchMode = "launched"; } WriteOutput((ActiveProcess == p ? "*" : " ") + string.Format(CultureInfo.InvariantCulture, "{0}. [PID: {1}, {2}] {3}", p.Number, p.CorProcess.Id, launchMode, processName)); } if (!haveProcesses) { WriteOutput("No Active Process!"); } } }
public static void DownCmd(string arguments) { string frameNum = "f"; var ap = new ArgParser(arguments, frameNum); if (ap.OptionPassed(frameNum)) { SwitchToFrame(ap.AsInt(0)); } else { int count = 1; if (ap.Exists(0)) { count = ap.AsInt(0); } while (--count >= 0) { Debugger.Processes.Active.Threads.Active.MoveCurrentFrame(true); } } if (Debugger.Processes.Active.Threads.Active.CurrentFrame.IsManaged) { WriteOutput("Current Frame:" + Debugger.Processes.Active.Threads.Active.CurrentFrame.Function.FullName ); } Shell.DisplayCurrentLocation(); }
/* * We want to have following commnads: * * symbol path "value" -- sets symbol paths * symbol addpath "value" -- adds symbol path * symbol reload [module] -- reloads symbol for a module * symbol list [module] -- shows currently loaded symbols */ public static void SymbolCmd(string arguments) { var ap = new ArgParser(arguments); if (!ap.Exists(0)) { ExecuteCommand("help symbol"); return; } switch (ap.AsCommand(0, new CommandArgument("path", "addpath", "reload", "list"))) { case "path": if (!ap.Exists(1)) { // we want to print current path string p = Debugger.Options.SymbolPath; WriteOutput("Current symbol path: " + p); } else { // we are setting path Debugger.Options.SymbolPath = ap.AsString(1); WriteOutput("Current symbol path: " + Debugger.Options.SymbolPath); } break; case "addpath": Debugger.Options.SymbolPath = Debugger.Options.SymbolPath + Path.PathSeparator + ap.AsString(1); WriteOutput("Current symbol path: " + Debugger.Options.SymbolPath); break; case "reload": { IEnumerable modules; if (ap.Exists(1)) { // we want to reload only one module MDbgModule m = Debugger.Processes.Active.Modules.Lookup(ap.AsString(1)); if (m == null) { throw new MDbgShellException("No such module."); } modules = new[] {m}; } else { modules = Debugger.Processes.Active.Modules; } foreach (MDbgModule m in modules) { WriteOutput("Reloading symbols for module " + m.CorModule.Name); m.ReloadSymbols(true); WriteModuleStatus(m, true); } } break; case "list": { IEnumerable modules; if (ap.Exists(1)) { // we want to list only one module MDbgModule m = Debugger.Processes.Active.Modules.Lookup(ap.AsString(1)); if (m == null) { throw new MDbgShellException("No such module."); } modules = new[] {m}; } else { modules = Debugger.Processes.Active.Modules; } foreach (MDbgModule m in modules) { WriteModuleStatus(m, false); } } break; default: Debug.Assert(false); break; } }
public static void ListCmd(string arguments) { const string verboseOpt = "v"; bool bVerbose; var ap = new ArgParser(arguments, verboseOpt); string listWhat = ap.AsCommand(0, new CommandArgument("modules", "appdomains", "assemblies")); switch (listWhat) { case "modules": bVerbose = ap.OptionPassed(verboseOpt); if (ap.Exists(1)) { // user specified module to display info for MDbgModule m = Debugger.Processes.Active.Modules.Lookup(ap.AsString(1)); if (m == null) { throw new MDbgShellException("No such module."); } ListModuleInternal(m, true); } else { // we list all modules WriteOutput("Loaded Modules:"); foreach (MDbgModule m in Debugger.Processes.Active.Modules) { ListModuleInternal(m, bVerbose); } } break; case "appdomains": WriteOutput("Current appDomains:"); foreach (MDbgAppDomain ad in Debugger.Processes.Active.AppDomains) { WriteOutput(ad.Number + ". - " + ad.CorAppDomain.Name); } break; case "assemblies": WriteOutput("Current assemblies:"); foreach (MDbgAppDomain ad in Debugger.Processes.Active.AppDomains) { foreach (CorAssembly assem in ad.CorAppDomain.Assemblies) { WriteOutput("\t" + assem.Name); } } break; default: Debug.Assert(false); break; } }
public static void NewObjCmd(string arguments) { var ap = new ArgParser(arguments); string className = ap.AsString(0); MDbgFunction func = Debugger.Processes.Active.ResolveFunctionName(null, className, ".ctor", Debugger.Processes.Active.Threads.Active. CorThread.AppDomain); if (null == func) throw new MDbgShellException(String.Format(CultureInfo.InvariantCulture, "Could not resolve {0}", ap.AsString(0))); CorEval eval = Debugger.Processes.Active.Threads.Active.CorThread.CreateEval(); var callArguments = new ArrayList(); // parse the arguments to newobj int i = 1; while (ap.Exists(i)) { string arg = ap.AsString(i); // this is a normal argument MDbgValue rsMVar = Debugger.Processes.Active.ResolveVariable(arg, Debugger.Processes.Active.Threads.Active. CurrentFrame); if (rsMVar == null) { // cordbg supports also limited literals -- currently only NULL & I4. if (string.Compare(arg, "null", true) == 0) { callArguments.Add(eval.CreateValue(CorElementType.ELEMENT_TYPE_CLASS, null)); } else { int v; if (!Int32.TryParse(arg, out v)) throw new MDbgShellException(string.Format(CultureInfo.InvariantCulture, "Argument '{0}' could not be resolved to variable or number", arg)); CorGenericValue gv = eval.CreateValue(CorElementType.ELEMENT_TYPE_I4, null).CastToGenericValue(); Debug.Assert(gv != null); gv.SetValue(v); callArguments.Add(gv); } } else { callArguments.Add(rsMVar.CorValue); } ++i; } eval.NewParameterizedObject(func.CorFunction, null, (CorValue[]) callArguments.ToArray(typeof (CorValue))); Debugger.Processes.Active.Go().WaitOne(); // now display result of the funceval if (! (Debugger.Processes.Active.StopReason is EvalCompleteStopReason)) { // we could have received also EvalExceptionStopReason but it's derived from EvalCompleteStopReason WriteOutput(MDbgOutputConstants.StdOutput,"Newobj command not fully completed and debuggee has stopped"); WriteOutput(MDbgOutputConstants.StdOutput,"Result of Newobj won't be printed when finished."); } else { eval = (Debugger.Processes.Active.StopReason as EvalCompleteStopReason).Eval; Debug.Assert(eval != null); CorValue cv = eval.Result; if (cv != null) { var mv = new MDbgValue(Debugger.Processes.Active, cv); WriteOutput("result = " + mv.GetStringValue(1)); if (Debugger.Processes.Active.DebuggerVars.SetEvalResult(cv)) WriteOutput(MDbgOutputConstants.StdOutput,"results saved to $result"); } } Shell.DisplayCurrentLocation(); }
public static void QuitCmd(string arguments) { // Look for optional exit code. var ap = new ArgParser(arguments); int exitCode; if (ap.Exists(0)) { exitCode = ap.AsInt(0); } else { exitCode = 0; } // we cannot modify the collection during enumeration, so // we need to collect all processes to kill in advance. var processesToKill = new ArrayList(); foreach (MDbgProcess p in Debugger.Processes) { processesToKill.Add(p); } foreach (MDbgProcess p in processesToKill) { if (p.IsAlive) { Debugger.Processes.Active = p; WriteOutput("Terminating current process..."); try { p.Kill().WaitOne(); } catch { // some processes cannot be killed (e.g. the one that have not loaded runtime) try { Process np = Process.GetProcessById(p.CorProcess.Id); np.Kill(); } catch { } } } } Shell.QuitWithExitCode(exitCode); }