Ejemplo n.º 1
0
        private static bool InitApi(IntPtr ptrClient)
        {
            // On our first call to the API:
            //   1. Store a copy of IDebugClient in DebugClient.
            //   2. Replace Console's output stream to be the debugger window.
            //   3. Create an instance of DataTarget using the IDebugClient.
            if (DebugClient == null)
            {
                object client = Marshal.GetUniqueObjectForIUnknown(ptrClient);
                DebugClient = (IDebugClient)client;

                var stream = new StreamWriter(new DebugEngineStream(DebugClient));
                stream.AutoFlush = true;
                Console.SetOut(stream);

                DataTarget = DataTarget.CreateFromDebuggerInterface(DebugClient);
            }

            // If our ClrRuntime instance is null, it means that this is our first call, or
            // that the dac wasn't loaded on any previous call.  Find the dac loaded in the
            // process (the user must use .cordll), then construct our runtime from it.
            if (Runtime == null)
            {
                // Just find a module named mscordacwks and assume it's the one the user
                // loaded into windbg.
                using (var process = Process.GetCurrentProcess())
                {
                    foreach (ProcessModule module in process.Modules)
                    {
                        var fileName = module.FileName.ToLowerInvariant();

                        if (fileName.Contains("mscordacwks") || fileName.Contains("mscordaccore"))
                        {
                            // TODO:  This does not support side-by-side CLRs.
                            Runtime = DataTarget.ClrVersions.Single().CreateRuntime(module.FileName);
                            break;
                        }
                    }
                }

                // Otherwise, the user didn't run .cordll.
                if (Runtime == null)
                {
                    Console.WriteLine("Mscordacwks.dll not loaded into the debugger.");
                    Console.WriteLine("Run .cordll to load the dac before running this command.");
                }
            }
            else
            {
                // If we already had a runtime, flush it for this use.  This is ONLY required
                // for a live process or iDNA trace.  If you use the IDebug* apis to detect
                // that we are debugging a crash dump you may skip this call for better perf.
                Runtime.Flush();
            }

            return(Runtime != null);
        }
Ejemplo n.º 2
0
        internal static DataTarget GetDataTarget(this IntPtr debugClient)
        {
            if (_dataTarget == null)
            {
                _dataTarget = DataTarget.CreateFromDebuggerInterface((IDebugClient)debugClient.GetIUnknown());
            }

            return(_dataTarget);
        }
Ejemplo n.º 3
0
        private void Init(IntPtr debugClientPointer)
        {
            object unknownObject = Marshal.GetUniqueObjectForIUnknown(debugClientPointer);

            debugClient = (IDebugClient)unknownObject;
            StreamWriter stream = new StreamWriter(new DebugEngineStream(debugClient));

            stream.AutoFlush = true;
            Console.SetOut(stream);
            DataTarget    dt = DataTarget.CreateFromDebuggerInterface(debugClient);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"Version: {dt.ClrVersions[0].Version}");
            Console.WriteLine(sb);
        }
Ejemplo n.º 4
0
        private static bool InitApi(IntPtr ptrClient)
        {
            // On our first call to the API:
            //   1. Store a copy of IDebugClient in DebugClient.
            //   2. Replace Console's output stream to be the debugger window.
            //   3. Create an instance of DataTarget using the IDebugClient.
            if (DebugClient == null)
            {
                object client = Marshal.GetUniqueObjectForIUnknown(ptrClient);
                DebugClient = (IDebugClient)client;

                var stream = new StreamWriter(new DebugEngineStream(DebugClient));
                stream.AutoFlush = true;
                Console.SetOut(stream);

                DataTarget = DataTarget.CreateFromDebuggerInterface(DebugClient);
            }

            // If our ClrRuntime instance is null, it means that this is our first call, or
            // that the dac wasn't loaded on any previous call.  Find the dac loaded in the
            // process (the user must use .cordll), then construct our runtime from it.
            if (Runtime == null)
            {
                // Just find a module named mscordacwks and assume it's the one the user
                // loaded into windbg.
                using (var process = Process.GetCurrentProcess())
                {
                    foreach (ProcessModule module in process.Modules)
                    {
                        if (module.FileName.ToLower().Contains("mscordacwks"))
                        {
                            // TODO:  This does not support side-by-side CLRs.
                            Runtime = DataTarget.ClrVersions.Single().CreateRuntime(module.FileName);
                            break;
                        }
                    }
                }

                // Otherwise, the user didn't run .cordll.
                if (Runtime == null)
                {
                    Console.WriteLine("Mscordacwks.dll not loaded into the debugger.");
                    Console.WriteLine("Run .cordll to load the dac before running this command.");
                }
            }
            else
            {
                // If we already had a runtime, flush it for this use.  This is ONLY required
                // for a live process or iDNA trace.  If you use the IDebug* apis to detect
                // that we are debugging a crash dump you may skip this call for better perf.
                Runtime.Flush();

                // Temporary workaround for a bug in ClrMD
                // To be removed when https://github.com/Microsoft/clrmd/pull/94 get published on NuGet
                var type = Type.GetType("Microsoft.Diagnostics.Runtime.Desktop.DesktopRuntimeBase, Microsoft.Diagnostics.Runtime");

                var modules = type.GetField("_modules", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Runtime)
                              as IDictionary;

                modules?.Clear();

                var moduleFiles = type.GetField("_moduleFiles", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Runtime)
                                  as IDictionary;

                moduleFiles?.Clear();
            }

            return(Runtime != null);
        }
Ejemplo n.º 5
0
 public MDTarget(object iDebugClient)
 {
     m_target = DataTarget.CreateFromDebuggerInterface((IDebugClient)iDebugClient);
 }