Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClrMdModule"/> class.
        /// </summary>
        /// <param name="provider">The CLR provider.</param>
        /// <param name="clrModule">The CLR module.</param>
        public ClrMdModule(CLR.ClrMdProvider provider, Microsoft.Diagnostics.Runtime.ClrModule clrModule)
        {
            Provider     = provider;
            ClrModule    = clrModule;
            clrPdbReader = SimpleCache.Create(() =>
            {
                try
                {
                    string pdbPath = ClrModule.Runtime.DataTarget.SymbolLocator.FindPdb(ClrModule.Pdb);

                    if (!string.IsNullOrEmpty(pdbPath))
                    {
                        return(new Microsoft.Diagnostics.Runtime.Utilities.Pdb.PdbReader(pdbPath));
                    }
                }
                catch (Exception)
                {
                }

                return(null);
            });
            module = SimpleCache.Create(() =>
            {
                return(Provider.GetProcess(ClrModule.Runtime)?.ClrModuleCache[this]);
            });
        }
Example #2
0
        /// <summary>
        /// Enumerates all objects on the heap.
        /// </summary>
        /// <returns>An enumeration of all objects on the heap.</returns>
        public IEnumerable <Variable> EnumerateObjects()
        {
            CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;

            // TODO: Because of ClrMD bug, we must enumerate all modules here...
            var modules = Runtime.Modules;

            // Enumerate all objects from heap
            foreach (ulong address in ClrHeap.EnumerateObjectAddresses())
            {
                var clrType = ClrHeap.GetObjectType(address);

                if (clrType.IsFree)
                {
                    continue;
                }

                CodeType codeType = Runtime.Process.FromClrType(provider.FromClrType(clrType));
                Variable variable;

                if (codeType.IsPointer)
                {
                    variable = Variable.CreatePointerNoCast(codeType, address);
                }
                else
                {
                    // TODO: This address unboxing should be part of ClrMD.
                    ulong address2 = address + Runtime.Process.GetPointerSize();

                    variable = Variable.CreateNoCast(codeType, address2);
                }

                yield return(Variable.UpcastClrVariable(variable));
            }
        }
Example #3
0
        /// <summary>
        /// Gets the CLR stack frame with the specified instruction offset.
        /// </summary>
        /// <param name="instructionOffset">The instruction offset.</param>
        public IClrStackFrame GetClrStackFrame(ulong instructionOffset)
        {
            var clrStackFrame = ClrThread.StackTrace?.FirstOrDefault(f => f.InstructionPointer == instructionOffset);

            if (clrStackFrame != null && clrStackFrame.Method != null)
            {
                CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;

                return(new ClrMdStackFrame(provider, clrStackFrame));
            }
            return(null);
        }
Example #4
0
        /// <summary>
        /// Enumerates the GC references (objects) on the stack.
        /// </summary>
        public IEnumerable <Variable> EnumerateStackObjects()
        {
            CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;

            foreach (Microsoft.Diagnostics.Runtime.ClrRoot root in ClrThread.EnumerateStackObjects())
            {
                if (root.Type.IsFree || root.Type.Module == null)
                {
                    continue;
                }

                Variable field = Variable.CreateNoCast(Process.FromClrType(provider.FromClrType(root.Type)), root.Address);

                yield return(Variable.UpcastClrVariable(field));
            }
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClrMdThread"/> class.
        /// </summary>
        /// <param name="thread">The thread.</param>
        /// <param name="clrThread">The CLR thread.</param>
        /// <param name="process">The process.</param>
        internal ClrMdThread(Thread thread, Microsoft.Diagnostics.Runtime.ClrThread clrThread, Process process)
        {
            Thread        = thread;
            Process       = process;
            ClrThread     = clrThread;
            runtime       = SimpleCache.Create(() => Process.ClrRuntimes.Single(r => ((ClrMdRuntime)r).ClrRuntime == clrThread.Runtime));
            appDomain     = SimpleCache.Create(() => Runtime.AppDomains.Single(a => a.Address == clrThread.AppDomain));
            clrStackTrace = SimpleCache.Create(() =>
            {
                CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;
                StackTrace stackTrace      = new StackTrace(Thread);
                uint frameNumber           = 0;

                stackTrace.Frames = ClrThread.StackTrace.Where(f => f.Method != null).Select(f =>
                {
                    return(new StackFrame(stackTrace, new ThreadContext(f.InstructionPointer, f.StackPointer, ulong.MaxValue, null))
                    {
                        FrameNumber = frameNumber++,
                        InstructionOffset = f.InstructionPointer,
                        StackOffset = f.StackPointer,
                        FrameOffset = ulong.MaxValue,
                        ReturnOffset = ulong.MaxValue,
                        ClrStackFrame = new ClrMdStackFrame(provider, f),
                    });
                }).ToArray();
                return(stackTrace);
            });
            lastThrownException = SimpleCache.Create(() =>
            {
                if (ClrThread.CurrentException != null)
                {
                    CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;
                    Microsoft.Diagnostics.Runtime.ClrException clrException = ClrThread.CurrentException;

                    return(Variable.CreatePointer(Process.FromClrType(provider.FromClrType(clrException.Type)), clrException.Address));
                }
                return(null);
            });
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClrMdRuntime" /> class.
        /// </summary>
        /// <param name="provider">The ClrMD provider.</param>
        /// <param name="process">The process.</param>
        /// <param name="clrRuntime">The CLR runtime.</param>
        internal ClrMdRuntime(CLR.ClrMdProvider provider, Process process, Microsoft.Diagnostics.Runtime.ClrRuntime clrRuntime)
        {
            Provider     = provider;
            Process      = process;
            ClrRuntime   = clrRuntime;
            appDomains   = SimpleCache.Create(() => ClrRuntime.AppDomains.Select(ad => new ClrMdAppDomain(this, ad)).ToArray());
            modules      = SimpleCache.Create(() => ClrRuntime.Modules.Select(mm => Provider.FromClrModule(mm)).ToArray());
            sharedDomain = SimpleCache.Create(() => ClrRuntime.SharedDomain != null ? new ClrMdAppDomain(this, ClrRuntime.SharedDomain) : null);
            systemDomain = SimpleCache.Create(() => ClrRuntime.SystemDomain != null ? new ClrMdAppDomain(this, ClrRuntime.SystemDomain) : null);
            threads      = SimpleCache.Create(() => ClrRuntime.Threads.Select(tt => new ClrMdThread(Process.Threads.Where(t => t.SystemId == tt.OSThreadId).FirstOrDefault(), tt, Process)).ToArray());
            gcThreads    = SimpleCache.Create(() => ClrRuntime.EnumerateGCThreads().Select(tt => new ClrMdThread(Process.Threads.Where(t => t.SystemId == tt).FirstOrDefault(), ClrRuntime.Threads.First(ct => ct.OSThreadId == tt), Process)).ToArray());
            heap         = SimpleCache.Create(() => new ClrMdHeap(this, ClrRuntime.GetHeap()));

            var version = ClrRuntime.ClrInfo.Version;

            Version = new ModuleVersion()
            {
                Major    = version.Major,
                Minor    = version.Minor,
                Patch    = version.Patch,
                Revision = version.Revision,
            };
        }
Example #7
0
        /// <summary>
        /// Gets the type of the object at the specified address.
        /// </summary>
        /// <param name="objectAddress">The object address.</param>
        public IClrType GetObjectType(ulong objectAddress)
        {
            CLR.ClrMdProvider provider = ((ClrMdRuntime)Runtime).Provider;

            return(provider.FromClrType(ClrHeap.GetObjectType(objectAddress)));
        }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClrMdType"/> class.
 /// </summary>
 /// <param name="provider">The CLR provider.</param>
 /// <param name="clrType">The CLR type.</param>
 public ClrMdType(CLR.ClrMdProvider provider, Microsoft.Diagnostics.Runtime.ClrType clrType)
 {
     Provider = provider;
     ClrType  = clrType;
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClrMdStackFrame"/> class.
 /// </summary>
 /// <param name="provider">The CLR provider.</param>
 /// <param name="clrStackFrame">The CLR stack frame.</param>
 public ClrMdStackFrame(CLR.ClrMdProvider provider, Microsoft.Diagnostics.Runtime.ClrStackFrame clrStackFrame)
 {
     Provider      = provider;
     ClrStackFrame = clrStackFrame;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClrMdInstanceField"/> class.
 /// </summary>
 /// <param name="provider">The CLR provider.</param>
 /// <param name="clrInstanceField">The CLR instance field.</param>
 public ClrMdInstanceField(CLR.ClrMdProvider provider, Microsoft.Diagnostics.Runtime.ClrInstanceField clrInstanceField)
 {
     Provider         = provider;
     ClrInstanceField = clrInstanceField;
 }
Example #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClrMdStaticField"/> class.
 /// </summary>
 /// <param name="provider">The CLR provider.</param>
 /// <param name="clrStaticField">The CLR static field.</param>
 public ClrMdStaticField(CLR.ClrMdProvider provider, Microsoft.Diagnostics.Runtime.ClrStaticField clrStaticField)
 {
     Provider       = provider;
     ClrStaticField = clrStaticField;
 }