/// <summary> /// Attach to a process with the given Process ID /// Only debug the specified CLR version; all others are ignored. /// </summary> /// <param name="processId">The Process ID to attach to.</param> /// <param name="attachContinuationEvent">A OS event handle which must be set to unblock the debuggee /// when first continuing from attach</param> /// <param name="version">The version string for the CLR instance to debug.</param> /// <returns>The resulting MDbgProcess.</returns> public MDbgProcess Attach(int processId, Microsoft.Samples.Debugging.Native.SafeWin32Handle attachContinuationEvent, string version) { Debug.Assert(version != null); MDbgProcess p = m_processMgr.CreateLocalProcess(new CorDebugger(version)); p.Attach(processId, attachContinuationEvent); return(p); }
/// <summary> /// Attach to a process with the given Process ID /// </summary> /// <param name="processId">The Process ID to attach to.</param> /// <returns>The resulting MDbgProcess.</returns> public MDbgProcess Attach(int processId) { string deeVersion; try { deeVersion = CorDebugger.GetDebuggerVersionFromPid(processId); } catch { // GetDebuggerVersionFromPid isn't implemented on Win9x and so will // throw NotImplementedException. We'll also get an ArgumentException // if the specified process doesn't have the CLR loaded yet. // Rather than be selective (and potentially brittle), we'll handle all errors. // Ideally we'd assert (or log) that we're only getting the errors we expect, // but it's complex and ugly to do that in C#. // Fall back to guessing the version based on the filename. // Environment variables (eg. COMPLUS_Version) may have resulted // in a different choice. try { var cp = new CorPublish.CorPublish(); CorPublishProcess cpp = cp.GetProcess(processId); string programBinary = cpp.DisplayName; deeVersion = CorDebugger.GetDebuggerVersionFromFile(programBinary); } catch { // This will also fail if the process doesn't have the CLR loaded yet. // It could also fail if the image EXE has been renamed since it started. // For whatever reason, fall back to using the default CLR deeVersion = null; } } MDbgProcess p = m_processMgr.CreateLocalProcess(deeVersion); p.Attach(processId); return(p); }
private static bool DebugActiveSilverlightProcess(int processId, DebugModeFlag debugMode) { MDbgProcess p = null; string[] fullPaths; EventWaitHandle[] continueStartupEvents; bool bMatchFound = false; // some pre-condition checks if (processId <= 0) { throw new MDbgShellException("Invalid arguments passed in"); } // Get all funcs exported by the coreclr's dbg shim. Silverlight.InitSLApi(); // Enumerate all coreclr instances in the process Silverlight.EnumerateCLRs((uint)processId, out fullPaths, out continueStartupEvents); int nSilverlight = fullPaths.Length; if (fullPaths == null || nSilverlight == 0) { throw new MDbgShellException("Could not enumerate any CLRs in specifed Silverlight process"); } // for each coreclr instance found..... for (int i = 0; i < nSilverlight && !bMatchFound; i++) { // Attach to the first one WriteOutput("FOUND: " + fullPaths[i]); string slVersion = Silverlight.CreateVersionStringFromModule((uint)processId, fullPaths[i]); sVersionString = slVersion; // we'll get the required ICorDebug interface from dbgshim.dll ICorDebug cordbg = null; try { cordbg = Silverlight.CreateDebuggingInterfaceFromVersionEx(CorDebugInterfaceVersion.CorDebugLatestVersion, slVersion); } catch (COMException ce) { Console.WriteLine("CDIFVEx failed, will retry with CDIFV.\n" + ce.ToString()); } if (cordbg == null) { cordbg = Silverlight.CreateDebuggingInterfaceFromVersion(slVersion); } p = GetProcessFromCordb(cordbg); // specify JIT flages here p.DebugMode = debugMode; p.Attach((int)processId); bMatchFound = true; // signal the continue event if (!continueStartupEvents[i].SafeWaitHandle.IsInvalid) { continueStartupEvents[i].Set(); } if (null != p) { p.Go().WaitOne(); } } return(bMatchFound); }