public static ICorDebug CreateDebuggingInterfaceFromVersion(string strDebuggeeVersion) { ICorDebug ICorDbgIntf; int ret; int numTries = 0; do { Trace.WriteLine("In CreateDebuggingInterfaceFromVersion, numTries=" + numTries.ToString()); ret = (int)m_CreateDebuggingInterfaceFromVersion( strDebuggeeVersion, out ICorDbgIntf ); // CreateDebuggingInterfaceFromVersionEx uses the OS API CreateToolhelp32Snapshot which can return // ERROR_BAD_LENGTH or ERROR_PARTIAL_COPY. If we get either of those, we try wait 1/10th of a second // try again (that is the recommendation of the OS API owners) if (((int)HResult.E_BAD_LENGTH == ret) || ((int)HResult.E_PARTIAL_COPY == ret)) { System.Threading.Thread.Sleep(100); } else { break; } numTries++; }while (numTries < 10); // if we're not OK then throw an exception with the returned hResult if ((int)HResult.S_OK != ret) { throw new COMException("CreateDebuggingInterfaceFromVersion for debuggee version " + strDebuggeeVersion + " failed returning " + Silverlight.HResultToString(ret), ret); } return(ICorDbgIntf); }
public static ICorDebug CreateDebuggingInterfaceFromVersionEx(SilverlightExtension.CorDebugInterfaceVersion iDebuggerVersion, string strDebuggeeVersion) { if (null == m_CreateDebuggingInterfaceFromVersionEx) { return(null); } ICorDebug ICorDbgIntf; int ret; int numTries = 0; do { Trace.WriteLine("In CreateDebuggingInterfaceFromVersionEx, numTries=" + numTries.ToString()); ret = (int)m_CreateDebuggingInterfaceFromVersionEx( (int)iDebuggerVersion, strDebuggeeVersion, out ICorDbgIntf ); // CreateDebuggingInterfaceFromVersionEx uses the OS API CreateToolhelp32Snapshot which can return // ERROR_BAD_LENGTH or ERROR_PARTIAL_COPY. If we get either of those, we try wait 1/10th of a second // try again (that is the recommendation of the OS API owners) if (((int)HResult.E_BAD_LENGTH == ret) || ((int)HResult.E_PARTIAL_COPY == ret)) { System.Threading.Thread.Sleep(100); } else { // else we've hit one of the HRESULTS that we shouldn't try again for, if the result isn't OK then the error will ge reported below break; } numTries++; }while (numTries < 10); // if we're not OK then throw an exception with the returned hResult if ((int)HResult.S_OK != ret) { throw new COMException("CreateDebuggingInterfaceFromVersionEx for debuggee version " + strDebuggeeVersion + " requesting interface version " + iDebuggerVersion + " failed returning " + Silverlight.HResultToString(ret), ret); } return(ICorDbgIntf); }
public static void EnumerateCLRs( UInt32 debuggeePid, out string[] fullPaths, out EventWaitHandle[] continueStartupEvents) { UInt32 elementCount; IntPtr pEventArray; IntPtr pStringArray; int ret; int numTries = 0; do { Trace.WriteLine(numTries > 0, "In EnumerateCLRs, numTries=" + numTries.ToString()); ret = (int)m_EnumerateCLRs( debuggeePid, out pEventArray, out pStringArray, out elementCount ); // EnumerateCLRs uses the OS API CreateToolhelp32Snapshot which can return // ERROR_BAD_LENGTH or ERROR_PARTIAL_COPY. If we get either of those, we try wait 1/10th of a second // try again (that is the recommendation of the OS API owners) if (((int)HResult.E_BAD_LENGTH == ret) || ((int)HResult.E_PARTIAL_COPY == ret)) { System.Threading.Thread.Sleep(100); } else { break; } numTries++; } while (((int)HResult.S_OK != ret) && (numTries < 10)); if ((int)HResult.S_OK != ret) { fullPaths = new string[0]; continueStartupEvents = new EventWaitHandle[0]; return; } MarshalCLREnumeration( pEventArray, pStringArray, elementCount, out fullPaths, out continueStartupEvents); ret = (int)m_CloseCLREnumeration( pEventArray, pStringArray, elementCount); if ((int)HResult.S_OK != ret) { throw new COMException("CloseCLREnumeration failed for process PID=" + debuggeePid + ", HResult= " + Silverlight.HResultToString(ret), ret); } }
/////////////////////////////////////////////////////////////////////////////////////////// public static string CreateVersionStringFromModule(UInt32 debuggeePid, string clrPath) { UInt32 reqBufferSize = 0; // first call is getting the reqBufferSize m_CreateVersionStringFromModule( 0, clrPath, null, 0, out reqBufferSize); StringBuilder sb = new StringBuilder((int)reqBufferSize); // this call can fail because the underlying call uses CreateToolhelp32Snapshot // int ret; int numTries = 0; do { Trace.WriteLine("In CreateVersionStringFromModule, numTries=" + numTries.ToString()); ret = (int)m_CreateVersionStringFromModule( debuggeePid, clrPath, sb, (UInt32)sb.Capacity, out reqBufferSize); // m_CreateVersionStringFromModule uses the OS API CreateToolhelp32Snapshot which can return // ERROR_BAD_LENGTH or ERROR_PARTIAL_COPY. If we get either of those, we try wait 1/10th of a second // try again (that is the recommendation of the OS API owners) if (((int)HResult.E_BAD_LENGTH == ret) || ((int)HResult.E_PARTIAL_COPY == ret)) { System.Threading.Thread.Sleep(100); } else { break; } numTries++; } while (numTries < 10); if ((int)HResult.S_OK != ret) { throw new COMException("CreateVersionStringFromModule failed returning the following HResult: " + Silverlight.HResultToString(ret), ret); } return(sb.ToString()); }
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); }