CreateTemporaryDbgEngTarget() public method

Creates a temporary DbgEng DataTarget. It is used for a single command's execution, such as !lm or !mk, and disposed immediately thereafter. If there is already a persistent DbgEng DataTarget, i.e. the debugger is currently in native DbgEng "mode", this method fails.
public CreateTemporaryDbgEngTarget ( ) : Microsoft.Diagnostics.Runtime.DataTarget
return Microsoft.Diagnostics.Runtime.DataTarget
Esempio n. 1
0
        private void VirtualMemoryStatistics()
        {
            var   sizeByType            = new Dictionary <MEM, ulong>();
            var   sizeByState           = new Dictionary <MEM, ulong>();
            ulong largestFreeRegionSize = 0;

            using (var target = _context.CreateTemporaryDbgEngTarget())
            {
                foreach (var region in target.EnumerateVMRegions())
                {
                    if (region.Type != 0)
                    {
                        if (!sizeByType.ContainsKey(region.Type))
                        {
                            sizeByType.Add(region.Type, 0);
                        }

                        sizeByType[region.Type] += region.RegionSize;
                    }

                    if (!sizeByState.ContainsKey(region.State))
                    {
                        sizeByState.Add(region.State, 0);
                    }

                    sizeByState[region.State] += region.RegionSize;

                    if (region.State == MEM.FREE)
                    {
                        largestFreeRegionSize = Math.Max(largestFreeRegionSize, region.RegionSize);
                    }
                }
            }

            _context.WriteLine("Virtual memory statistics:");
            _context.WriteLine();
            foreach (var kvp in sizeByType)
            {
                _context.WriteLine("{0,-10} {1,-16}", kvp.Key, kvp.Value.ToMemoryUnits());
            }
            _context.WriteLine();
            foreach (var kvp in sizeByState)
            {
                _context.WriteLine("{0,-10} {1,-16}", kvp.Key, kvp.Value.ToMemoryUnits());
            }
            _context.WriteLine();
            _context.WriteLine("Largest free region size: {0}", largestFreeRegionSize.ToMemoryUnits());
            _context.WriteLine();
        }
Esempio n. 2
0
 public TriageInformation GetTriageInformation(CommandExecutionContext context)
 {
     using (_dbgEngTarget = context.CreateTemporaryDbgEngTarget())
     {
         FillModuleInformation();
         FillMemoryUsageInformation(context);
         FillFaultingThreadAndModuleInformation(context);
     }
     return(_triageInformation);
 }
Esempio n. 3
0
 public TriageInformation GetTriageInformation(CommandExecutionContext context)
 {
     using (_dbgEngTarget = context.CreateTemporaryDbgEngTarget())
     {
         FillModuleInformation();
         FillMemoryUsageInformation(context);
         FillFaultingThreadAndModuleInformation(context);
     }
     return _triageInformation;
 }
Esempio n. 4
0
 public void Execute(CommandExecutionContext context)
 {
     if (context.IsInDbgEngNativeMode)
     {
         context.NativeDbgEngTarget.ExecuteDbgEngCommand(Command, context);
     }
     else
     {
         using (var target = context.CreateTemporaryDbgEngTarget())
         {
             target.ExecuteDbgEngCommand(Command, context);
         }
     }
 }
Esempio n. 5
0
 public void Execute(CommandExecutionContext context)
 {
     if (context.IsInDbgEngNativeMode)
     {
         context.NativeDbgEngTarget.ExecuteDbgEngCommand(Command, context);
     }
     else
     {
         using (var target = context.CreateTemporaryDbgEngTarget())
         {
             target.ExecuteDbgEngCommand(Command, context);
         }
     }
 }
Esempio n. 6
0
        public void Execute(CommandExecutionContext context)
        {
            if (OSThreadId == 0)
            {
                OSThreadId = context.CurrentThread.OSThreadId;
            }

            context.WriteLine("{0,-10} {1,-20} {2}", "Type", "IP", "Function");
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                var stackTracer = new UnifiedStackTraces(target.DebuggerInterface, context);
                stackTracer.PrintStackTrace(context, (from thr in stackTracer.Threads
                                                      where thr.OSThreadId == OSThreadId
                                                      select thr.Index).Single());
            }
        }
Esempio n. 7
0
        public void Execute(CommandExecutionContext context)
        {
            if (OSThreadId == 0)
            {
                OSThreadId = context.CurrentThread.OSThreadId;
            }

            context.WriteLine("{0,-10} {1,-20} {2}", "Type", "IP", "Function");
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                var stackTracer = new UnifiedStackTraces(target.DebuggerInterface, context);
                stackTracer.PrintStackTrace(context, (from thr in stackTracer.Threads
                                                      where thr.OSThreadId == OSThreadId
                                                      select thr.Index).Single());
            }
        }
Esempio n. 8
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            ClrMethod method = context.Runtime.GetMethodByAddress(InstructionPointer);
            if (method == null)
            {
                context.WriteError("There is no managed method at the address {0:x16}.", InstructionPointer);
                return;
            }

            _sourceFileCache = new Dictionary<string, string[]>();
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                _control = (IDebugControl)target.DebuggerInterface;
                DisassembleMethod(method);
            }
        }
Esempio n. 9
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            ClrMethod method = context.Runtime.GetMethodByAddress(InstructionPointer);

            if (method == null)
            {
                context.WriteError("There is no managed method at the address {0:x16}.", InstructionPointer);
                return;
            }

            _sourceFileCache = new Dictionary <string, string[]>();
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                _control = (IDebugControl)target.DebuggerInterface;
                DisassembleMethod(method);
            }
        }
Esempio n. 10
0
        private void SetStrategy()
        {
            if (_context.TargetType == TargetType.DumpFile ||
                _context.TargetType == TargetType.DumpFileNoHeap)
            {
                _temporaryDbgEngTarget   = _context.CreateTemporaryDbgEngTarget();
                _unifiedStackTraces      = new UnifiedStackTraces(_temporaryDbgEngTarget.DebuggerInterface, _context);
                _blockingObjectsStrategy = new DumpFileBlockingObjectsStrategy(_context.Runtime, _unifiedStackTraces, _temporaryDbgEngTarget);

                _threads.AddRange(_unifiedStackTraces.Threads.Select(ti => _blockingObjectsStrategy.GetThreadWithBlockingObjects(ti)));
            }
            else
            {
                _blockingObjectsStrategy = new LiveProcessBlockingObjectsStrategy(_context.Runtime);

                // Currently, we are only enumerating the managed threads because we don't have
                // an alternative source of information for threads in live processes. In the future,
                // we can consider using System.Diagnostics or some other means of enumerating threads
                // in live processes.
                _threads.AddRange(_context.Runtime.Threads.Select(thr => _blockingObjectsStrategy.GetThreadWithBlockingObjects(thr)));
            }
        }
Esempio n. 11
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            if (IncludeNativeThreads)
            {
                using (var target = context.CreateTemporaryDbgEngTarget())
                {
                    var tracer = new UnifiedStackTraces(target.DebuggerInterface, context);
                    context.WriteLine("Stack tree for {0} threads:", tracer.NumThreads);
                    var allStacks = from thread in tracer.Threads
                                    let frames = from frame in tracer.GetStackTrace(thread.Index)
                                                 where frame.Type != UnifiedStackFrameType.Special
                                                 select frame.DisplayString
                                                 select new ThreadAndStack
                    {
                        ManagedThreadId = thread.IsManagedThread ? thread.ManagedThread.ManagedThreadId : 0,
                        OSThreadId      = thread.OSThreadId,
                        Stack           = frames.Reverse()
                    };
                    ProcessStacks(allStacks);
                }
            }
            else
            {
                context.WriteLine("Stack tree for {0} threads:", context.Runtime.Threads.Count);
                var allStacks = from thread in context.Runtime.Threads
                                let frames = from frame in thread.StackTrace
                                             where frame.Kind == ClrStackFrameType.ManagedMethod
                                             select frame.DisplayString
                                             select new ThreadAndStack
                {
                    ManagedThreadId = thread.ManagedThreadId,
                    Stack           = frames.Reverse()
                };
                ProcessStacks(allStacks);
            }
        }
Esempio n. 12
0
        public void Execute(CommandExecutionContext context)
        {
            _context = context;

            if (IncludeNativeThreads)
            {
                using (var target = context.CreateTemporaryDbgEngTarget())
                {
                    var tracer = new UnifiedStackTraces(target.DebuggerInterface, context);
                    context.WriteLine("Stack tree for {0} threads:", tracer.NumThreads);
                    var allStacks = from thread in tracer.Threads
                                let frames = from frame in tracer.GetStackTrace(thread.Index)
                                             where frame.Type != UnifiedStackFrameType.Special
                                             select frame.DisplayString
                                select new ThreadAndStack
                                {
                                    ManagedThreadId = thread.IsManagedThread ? thread.ManagedThread.ManagedThreadId : 0,
                                    OSThreadId = thread.OSThreadId,
                                    Stack = frames.Reverse()
                                };
                    ProcessStacks(allStacks);
                }
            }
            else
            {
                context.WriteLine("Stack tree for {0} threads:", context.Runtime.Threads.Count);
                var allStacks = from thread in context.Runtime.Threads
                                let frames = from frame in thread.StackTrace
                                             where frame.Kind == ClrStackFrameType.ManagedMethod
                                             select frame.DisplayString
                                select new ThreadAndStack
                                {
                                    ManagedThreadId = thread.ManagedThreadId,
                                    Stack = frames.Reverse()
                                };
                ProcessStacks(allStacks);
            }
        }
Esempio n. 13
0
        public void Execute(CommandExecutionContext context)
        {
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                LastEventInformation lastEventInformation = target.GetLastEventInformation();
                if (lastEventInformation == null)
                {
                    context.WriteLine("Last event information is not available");
                    return;
                }

                context.Write("Thread OSID = {0} ", lastEventInformation.OSThreadId);
                var managedThread = context.Runtime.Threads.SingleOrDefault(t => t.OSThreadId == lastEventInformation.OSThreadId);
                if (managedThread != null)
                {
                    context.WriteLine("(managed id = {0})", managedThread.ManagedThreadId);
                }
                else
                {
                    context.WriteLine("(unmanaged)");
                }
                context.WriteLine("{0} - {1}", lastEventInformation.EventType, lastEventInformation.EventDescription);
            }
        }
Esempio n. 14
0
        public void Execute(CommandExecutionContext context)
        {
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                LastEventInformation lastEventInformation = target.GetLastEventInformation();
                if (lastEventInformation == null)
                {
                    context.WriteLine("Last event information is not available");
                    return;
                }

                context.Write("Thread OSID = {0} ", lastEventInformation.OSThreadId);
                var managedThread = context.Runtime.Threads.SingleOrDefault(t => t.OSThreadId == lastEventInformation.OSThreadId);
                if (managedThread != null)
                {
                    context.WriteLine("(managed id = {0})", managedThread.ManagedThreadId);
                }
                else
                {
                    context.WriteLine("(unmanaged)");
                }
                context.WriteLine("{0} - {1}", lastEventInformation.EventType, lastEventInformation.EventDescription);
            }
        }
Esempio n. 15
0
        public void Execute(CommandExecutionContext context)
        {
            context.WriteLine("{0} CLR threads, {1} CLR thread pool threads, {2} background threads",
                context.Runtime.Threads.Count, context.Runtime.Threads.Count(t => t.IsThreadPoolThread()),
                context.Runtime.Threads.Count(t => t.IsBackground));
            context.WriteLine("{0,-6} {1,-6} {2,-6} {3,-6} {4,-20} {5,-30}",
                "MgdId", "OSId", "Lock#", "Apt", "Special", "Exception");
            foreach (var thread in context.Runtime.Threads)
            {
                context.Write("{0,-6} {1,-6} {2,-6} {3,-6} {4,-20} {5,-30} ",
                    thread.ManagedThreadId, thread.OSThreadId, thread.LockCount, thread.ApartmentDescription(),
                    thread.SpecialDescription(), (thread.CurrentException != null ? thread.CurrentException.Type.Name : "").TrimStartToLength(30));
                context.WriteLink("", String.Format("~ {0}; {1}",
                    thread.ManagedThreadId, thread.CurrentException != null ? "!pe" : "!clrstack"));
                context.WriteLine();
            }

            if (!DisplayNativeThreads)
                return;

            context.WriteLine();
            context.WriteLine("{0,-6} {1,-6} {2,-10} {3}", "OSId", "MgdId", "ExitCode", "StartAddress");
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                var osThreadIds = target.GetOSThreadIds();
                var symbols = (IDebugSymbols)target.DebuggerInterface;
                var advanced = (IDebugAdvanced2)target.DebuggerInterface;
                int size;
                byte[] buffer = new byte[Marshal.SizeOf(typeof(DEBUG_THREAD_BASIC_INFORMATION))];
                GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                try
                {
                    for (uint engineThreadId = 0; engineThreadId < osThreadIds.Length; ++engineThreadId)
                    {
                        if (0 != advanced.GetSystemObjectInformation(DEBUG_SYSOBJINFO.THREAD_BASIC_INFORMATION, 0,
                            engineThreadId, buffer, buffer.Length, out size))
                            continue;

                        var info = (DEBUG_THREAD_BASIC_INFORMATION)Marshal.PtrToStructure(
                            gch.AddrOfPinnedObject(), typeof(DEBUG_THREAD_BASIC_INFORMATION));
                        var managedThread = context.Runtime.Threads.SingleOrDefault(t => t.OSThreadId == osThreadIds[engineThreadId]);
                        context.Write("{0,-6} {1,-6} {2,-10} {3:x16} ", osThreadIds[engineThreadId],
                            managedThread != null ? managedThread.ManagedThreadId.ToString() : "",
                            info.ExitStatus == 259 ? "active" : info.ExitStatus.ToString(), info.StartOffset);

                        uint symSize;
                        ulong displacement;
                        StringBuilder symbolName = new StringBuilder(2048);
                        if (0 == symbols.GetNameByOffset(info.StartOffset, symbolName, symbolName.Capacity, out symSize, out displacement))
                        {
                            context.Write("{0} ", symbolName.ToString());
                        }

                        context.WriteLink("", String.Format("!mk {0}", osThreadIds[engineThreadId]));
                        context.WriteLine();
                    }
                }
                finally
                {
                    gch.Free();
                }
            }
        }
Esempio n. 16
0
        public void Execute(CommandExecutionContext context)
        {
            context.WriteLine("{0} CLR threads, {1} CLR thread pool threads, {2} background threads",
                              context.Runtime.Threads.Count, context.Runtime.Threads.Count(t => t.IsThreadPoolThread()),
                              context.Runtime.Threads.Count(t => t.IsBackground));
            context.WriteLine("{0,-6} {1,-6} {2,-6} {3,-6} {4,-20} {5,-30}",
                              "MgdId", "OSId", "Lock#", "Apt", "Special", "Exception");
            foreach (var thread in context.Runtime.Threads)
            {
                context.Write("{0,-6} {1,-6} {2,-6} {3,-6} {4,-20} {5,-30} ",
                              thread.ManagedThreadId, thread.OSThreadId, thread.LockCount, thread.ApartmentDescription(),
                              thread.SpecialDescription(), (thread.CurrentException != null ? thread.CurrentException.Type.Name : "").TrimStartToLength(30));
                context.WriteLink("", String.Format("~ {0}; {1}",
                                                    thread.ManagedThreadId, thread.CurrentException != null ? "!pe" : "!clrstack"));
                context.WriteLine();
            }

            if (!DisplayNativeThreads)
            {
                return;
            }

            context.WriteLine();
            context.WriteLine("{0,-6} {1,-6} {2,-10} {3}", "OSId", "MgdId", "ExitCode", "StartAddress");
            using (var target = context.CreateTemporaryDbgEngTarget())
            {
                var      osThreadIds = target.GetOSThreadIds();
                var      symbols     = (IDebugSymbols)target.DebuggerInterface;
                var      advanced    = (IDebugAdvanced2)target.DebuggerInterface;
                int      size;
                byte[]   buffer = new byte[Marshal.SizeOf(typeof(DEBUG_THREAD_BASIC_INFORMATION))];
                GCHandle gch    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                try
                {
                    for (uint engineThreadId = 0; engineThreadId < osThreadIds.Length; ++engineThreadId)
                    {
                        if (0 != advanced.GetSystemObjectInformation(DEBUG_SYSOBJINFO.THREAD_BASIC_INFORMATION, 0,
                                                                     engineThreadId, buffer, buffer.Length, out size))
                        {
                            continue;
                        }

                        var info = (DEBUG_THREAD_BASIC_INFORMATION)Marshal.PtrToStructure(
                            gch.AddrOfPinnedObject(), typeof(DEBUG_THREAD_BASIC_INFORMATION));
                        var managedThread = context.Runtime.Threads.SingleOrDefault(t => t.OSThreadId == osThreadIds[engineThreadId]);
                        context.Write("{0,-6} {1,-6} {2,-10} {3:x16} ", osThreadIds[engineThreadId],
                                      managedThread != null ? managedThread.ManagedThreadId.ToString() : "",
                                      info.ExitStatus == 259 ? "active" : info.ExitStatus.ToString(), info.StartOffset);

                        uint          symSize;
                        ulong         displacement;
                        StringBuilder symbolName = new StringBuilder(2048);
                        if (0 == symbols.GetNameByOffset(info.StartOffset, symbolName, symbolName.Capacity, out symSize, out displacement))
                        {
                            context.Write("{0} ", symbolName.ToString());
                        }

                        context.WriteLink("", String.Format("!mk {0}", osThreadIds[engineThreadId]));
                        context.WriteLine();
                    }
                }
                finally
                {
                    gch.Free();
                }
            }
        }
Esempio n. 17
0
File: LM.cs Progetto: tiandian/msos
        public void Execute(CommandExecutionContext context)
        {
            if (!String.IsNullOrEmpty(SpecificModule))
            {
                var module = context.Runtime.Modules.FirstOrDefault(
                    m => String.Equals(Path.GetFileName(m.Name), SpecificModule, StringComparison.InvariantCultureIgnoreCase));
                if (module != null)
                {
                    var moduleInfo = context.Runtime.DataTarget.EnumerateModules().Single(
                        m => String.Equals(m.FileName, module.FileName, StringComparison.InvariantCultureIgnoreCase));
                    context.WriteLine("Module:     {0}", module.Name);
                    context.WriteLine("PDB name:   {0}", moduleInfo.Pdb.FileName);
                    context.WriteLine("Debug mode: {0}", module.DebuggingMode);
                }
                else
                {
                    // Couldn't find managed module, try to find native:
                    using (var target = context.CreateTemporaryDbgEngTarget())
                    {
                        var moduleInfo = context.Runtime.DataTarget.EnumerateModules().FirstOrDefault(
                            m => String.Equals(Path.GetFileName(m.FileName), SpecificModule, StringComparison.InvariantCultureIgnoreCase));
                        if (moduleInfo == null)
                        {
                            return;
                        }

                        IDebugSymbols3 debugSymbols = (IDebugSymbols3)target.DebuggerInterface;

                        uint loaded, unloaded;
                        if (0 != debugSymbols.GetNumberModules(out loaded, out unloaded))
                        {
                            return;
                        }

                        for (uint moduleIdx = 0; moduleIdx < loaded; ++moduleIdx)
                        {
                            StringBuilder name = new StringBuilder(2048);
                            uint          nameSize;
                            if (0 != debugSymbols.GetModuleNameString(DEBUG_MODNAME.IMAGE, moduleIdx, 0, name, (uint)name.Capacity, out nameSize))
                            {
                                continue;
                            }

                            if (!String.Equals(name.ToString(), moduleInfo.FileName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                continue;
                            }

                            DEBUG_MODULE_PARAMETERS[] modInfo = new DEBUG_MODULE_PARAMETERS[1];
                            if (0 != debugSymbols.GetModuleParameters(1, null, moduleIdx, modInfo))
                            {
                                return;
                            }

                            name = new StringBuilder(2048);
                            debugSymbols.GetModuleNameString(DEBUG_MODNAME.SYMBOL_FILE, moduleIdx, 0, name, (uint)name.Capacity, out nameSize);

                            context.WriteLine("Module:     {0}", moduleInfo.FileName);
                            context.WriteLine("PDB loaded: {0}", modInfo[0].SymbolType == DEBUG_SYMTYPE.DIA || modInfo[0].SymbolType == DEBUG_SYMTYPE.PDB);
                            context.WriteLine("PDB name:   {0}", name.ToString());
                        }
                    }
                }
                return;
            }

            context.WriteLine("{0,-20:x16} {1,-10} {2,-20} {3}", "start", "size", "version", "filename");
            foreach (var module in context.Runtime.DataTarget.EnumerateModules())
            {
                context.WriteLine("{0,-20:x16} {1,-10:x} {2,-20} {3}",
                                  module.ImageBase, module.FileSize, module.Version, module.FileName);
            }
        }
Esempio n. 18
0
File: LM.cs Progetto: goldshtn/msos
        public void Execute(CommandExecutionContext context)
        {
            if (!String.IsNullOrEmpty(SpecificModule))
            {
                var module = context.Runtime.Modules.FirstOrDefault(
                    m => String.Equals(Path.GetFileName(m.Name), SpecificModule, StringComparison.InvariantCultureIgnoreCase));
                if (module != null)
                {
                    var moduleInfo = context.Runtime.DataTarget.EnumerateModules().Single(
                        m => String.Equals(m.FileName, module.FileName, StringComparison.InvariantCultureIgnoreCase));
                    context.WriteLine("Module:     {0}", module.Name);
                    context.WriteLine("PDB name:   {0}", moduleInfo.Pdb.FileName);
                    context.WriteLine("Debug mode: {0}", module.DebuggingMode);
                }
                else
                {
                    // Couldn't find managed module, try to find native:
                    using (var target = context.CreateTemporaryDbgEngTarget())
                    {
                        var moduleInfo = context.Runtime.DataTarget.EnumerateModules().FirstOrDefault(
                            m => String.Equals(Path.GetFileName(m.FileName), SpecificModule, StringComparison.InvariantCultureIgnoreCase));
                        if (moduleInfo == null)
                            return;

                        IDebugSymbols3 debugSymbols = (IDebugSymbols3)target.DebuggerInterface;

                        uint loaded, unloaded;
                        if (0 != debugSymbols.GetNumberModules(out loaded, out unloaded))
                            return;

                        for (uint moduleIdx = 0; moduleIdx < loaded; ++moduleIdx)
                        {
                            StringBuilder name = new StringBuilder(2048);
                            uint nameSize;
                            if (0 != debugSymbols.GetModuleNameString(DEBUG_MODNAME.IMAGE, moduleIdx, 0, name, (uint)name.Capacity, out nameSize))
                                continue;

                            if (!String.Equals(name.ToString(), moduleInfo.FileName, StringComparison.InvariantCultureIgnoreCase))
                                continue;

                            DEBUG_MODULE_PARAMETERS[] modInfo = new DEBUG_MODULE_PARAMETERS[1];
                            if (0 != debugSymbols.GetModuleParameters(1, null, moduleIdx, modInfo))
                                return;

                            name = new StringBuilder(2048);
                            debugSymbols.GetModuleNameString(DEBUG_MODNAME.SYMBOL_FILE, moduleIdx, 0, name, (uint)name.Capacity, out nameSize);

                            context.WriteLine("Module:     {0}", moduleInfo.FileName);
                            context.WriteLine("PDB loaded: {0}", modInfo[0].SymbolType == DEBUG_SYMTYPE.DIA || modInfo[0].SymbolType == DEBUG_SYMTYPE.PDB);
                            context.WriteLine("PDB name:   {0}", name.ToString());
                        }
                    }
                }
                return;
            }

            context.WriteLine("{0,-20:x16} {1,-10} {2,-20} {3}", "start", "size", "version", "filename");
            foreach (var module in context.Runtime.DataTarget.EnumerateModules())
            {
                context.WriteLine("{0,-20:x16} {1,-10:x} {2,-20} {3}",
                    module.ImageBase, module.FileSize, module.Version, module.FileName);
            }
        }