Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ElfImage"/> class.
        /// </summary>
        /// <param name="path">The image path.</param>
        /// <param name="loadOffset">Offset from where image was loaded.</param>
        public ElfImage(string path, ulong loadOffset = 0)
        {
            elf        = ELFReader.Load <ulong>(path);
            LoadOffset = loadOffset;
            foreach (var segment in elf.Segments)
            {
                if (segment.Type == ELFSharp.ELF.Segments.SegmentType.ProgramHeader)
                {
                    CodeSegmentOffset = segment.Address - (ulong)segment.Offset;
                    break;
                }
            }

            List <PublicSymbol> publicSymbols = new List <PublicSymbol>();
            SymbolTable <ulong> symbols       = elf.Sections.FirstOrDefault(s => s.Type == SectionType.SymbolTable) as SymbolTable <ulong>;

            if (symbols == null || !symbols.Entries.Any())
            {
                symbols = elf.Sections.FirstOrDefault(s => s.Type == SectionType.DynamicSymbolTable) as SymbolTable <ulong>;
            }

            if (symbols != null)
            {
                foreach (SymbolEntry <ulong> symbol in symbols.Entries)
                {
                    publicSymbols.Add(new PublicSymbol(symbol.Name, symbol.Value - CodeSegmentOffset));
                }
            }
            PublicSymbols       = publicSymbols;
            sectionRegions      = SimpleCache.Create(() => elf.Sections.Where(s => s.LoadAddress > 0).OrderBy(s => s.LoadAddress).ToArray());
            sectionRegionFinder = SimpleCache.Create(() => new MemoryRegionFinder(sectionRegions.Value.Select(s => new MemoryRegion {
                BaseAddress = s.LoadAddress, RegionSize = s.Size
            }).ToArray()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StackFrame" /> class.
        /// </summary>
        /// <param name="stackTrace">The stack trace.</param>
        /// <param name="frameContext">The frame context.</param>
        internal StackFrame(StackTrace stackTrace, ThreadContext frameContext)
        {
            StackTrace                  = stackTrace;
            FrameContext                = frameContext;
            sourceFileNameAndLine       = SimpleCache.Create(ReadSourceFileNameAndLine);
            functionNameAndDisplacement = SimpleCache.Create(ReadFunctionNameAndDisplacement);
            locals                  = SimpleCache.Create(GetLocals);
            arguments               = SimpleCache.Create(GetArguments);
            clrStackFrame           = SimpleCache.Create(() => Thread.ClrThread?.GetClrStackFrame(InstructionOffset));
            userTypeConvertedLocals = SimpleCache.Create(() =>
            {
                VariableCollection collection = Variable.CastVariableCollectionToUserType(locals.Value);

                GlobalCache.UserTypeCastedVariableCollections.Add(userTypeConvertedLocals);
                return(collection);
            });
            userTypeConvertedArguments = SimpleCache.Create(() =>
            {
                VariableCollection collection = Variable.CastVariableCollectionToUserType(arguments.Value);

                GlobalCache.UserTypeCastedVariableCollections.Add(userTypeConvertedArguments);
                return(collection);
            });
            module = SimpleCache.Create(() =>
            {
                var m = Process.GetModuleByInnerAddress(InstructionOffset);

                if (m == null && ClrStackFrame != null)
                {
                    m = Process.ClrModuleCache[ClrStackFrame.Module];
                }
                return(m);
            });
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClrMdHeap"/> class.
 /// </summary>
 /// <param name="runtime">The runtime.</param>
 public VSClrHeap(VSClrRuntime runtime)
 {
     VSRuntime           = runtime;
     canWalkHeapCache    = SimpleCache.Create(() => Proxy.GetClrHeapCanWalkHeap(VSRuntime.Process.Id, VSRuntime.Id));
     totalHeapSizeCache  = SimpleCache.Create(() => Proxy.GetClrHeapTotalHeapSize(VSRuntime.Process.Id, VSRuntime.Id));
     typesByAddressCache = new DictionaryCache <ulong, VSClrType>((a) => default(VSClrType));
 }
Ejemplo n.º 4
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]);
            });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Module" /> class.
        /// </summary>
        /// <param name="process">The process.</param>
        /// <param name="address">The module address.</param>
        internal Module(Process process, ulong address)
        {
            Address = address;
            Process = process;
            Id      = uint.MaxValue;
            name    = SimpleCache.Create(() =>
            {
                string name = Context.Debugger.GetModuleName(this);

                Process.UpdateModuleByNameCache(this, name);
                return(name);
            });
            imageName       = SimpleCache.Create(() => Context.Debugger.GetModuleImageName(this));
            loadedImageName = SimpleCache.Create(() => Context.Debugger.GetModuleLoadedImage(this));
            symbolFileName  = SimpleCache.Create(() => Context.Debugger.GetModuleSymbolFile(this));
            mappedImageName = SimpleCache.Create(() => Context.Debugger.GetModuleMappedImage(this));
            moduleVersion   = SimpleCache.Create(() =>
            {
                ModuleVersion version = new ModuleVersion();

                Context.Debugger.GetModuleVersion(this, out version.Major, out version.Minor, out version.Revision, out version.Patch);
                return(version);
            });
            timestampAndSize = SimpleCache.Create(() => Context.Debugger.GetModuleTimestampAndSize(this));
            clrModule        = SimpleCache.Create(() => Process.ClrRuntimes.SelectMany(r => r.Modules).Where(m => m.ImageBase == Address).FirstOrDefault());
            pointerSize      = SimpleCache.Create(() => Process.GetPointerSize());
            TypesByName      = new DictionaryCache <string, CodeType>(GetTypeByName);
            TypesById        = new DictionaryCache <uint, CodeType>(GetTypeById);
            ClrTypes         = new DictionaryCache <IClrType, CodeType>(GetClrCodeType);
            GlobalVariables  = new DictionaryCache <string, Variable>(GetGlobalVariable);
            UserTypeCastedGlobalVariables = Context.UserTypeMetadataCaches.CreateDictionaryCache <string, Variable>((name) => Process.CastVariableToUserType(GlobalVariables[name]));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClrThread"/> class.
        /// </summary>
        /// <param name="thread">The thread.</param>
        /// <param name="clrThread">The CLR thread.</param>
        /// <param name="process">The process.</param>
        internal ClrThread(Thread thread, Microsoft.Diagnostics.Runtime.ClrThread clrThread, Process process)
            : base(thread != null ? thread.Id : uint.MaxValue, clrThread.OSThreadId, process)
        {
            ClrThread     = clrThread;
            runtime       = SimpleCache.Create(() => Process.ClrRuntimes.Single(r => r.ClrRuntime == clrThread.Runtime));
            appDomain     = SimpleCache.Create(() => Runtime.AppDomains.Single(a => a.ClrAppDomain.Address == clrThread.AppDomain));
            clrStackTrace = SimpleCache.Create(() =>
            {
                StackTrace stackTrace = new StackTrace(this);
                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))
                    {
                        FrameNumber = frameNumber++,
                        InstructionOffset = f.InstructionPointer,
                        StackOffset = f.StackPointer,
                        FrameOffset = ulong.MaxValue,
                        ReturnOffset = ulong.MaxValue,
                        ClrStackFrame = f,
                    });
                }).ToArray();
                return(stackTrace);
            });
            lastThrownException = SimpleCache.Create(() => ClrThread.CurrentException != null ? new ClrException(this, ClrThread.CurrentException) : null);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeFunction" /> class.
 /// </summary>
 /// <param name="address">The function address.</param>
 /// <param name="process">The process.</param>
 public CodeFunction(ulong address, Process process = null)
 {
     Address = address;
     Process = process ?? Process.Current;
     sourceFileNameAndLine       = SimpleCache.Create(ReadSourceFileNameAndLine);
     functionNameAndDisplacement = SimpleCache.Create(ReadFunctionNameAndDisplacement);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeFunction" /> class.
 /// </summary>
 /// <param name="address">The function address.</param>
 /// <param name="process">The process.</param>
 public CodeFunction(ulong address, Process process = null)
 {
     OriginalAddress             = address;
     Process                     = process ?? Process.Current;
     functionAddress             = SimpleCache.Create(() => Debugger.ResolveFunctionAddress(Process, OriginalAddress));
     sourceFileNameAndLine       = SimpleCache.Create(ReadSourceFileNameAndLine);
     functionNameAndDisplacement = SimpleCache.Create(ReadFunctionNameAndDisplacement);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VSClrStackFrame"/> class.
 /// </summary>
 /// <param name="thread">The thread containing this stack frame.</param>
 /// <param name="id">The stack frame id.</param>
 /// <param name="instructionPointer">The instruction pointer.</param>
 /// <param name="stackPointer">The stack pointer.</param>
 /// <param name="moduleAddress">The module base address.</param>
 public VSClrStackFrame(VSClrThread thread, int id, ulong instructionPointer, ulong stackPointer, ulong moduleAddress)
 {
     Thread             = thread;
     InstructionPointer = instructionPointer;
     StackPointer       = stackPointer;
     argumentsCache     = SimpleCache.Create(() => CreateVariableCollection(Proxy.GetClrStackFrameArguments(Thread.VSRuntime.Process.Id, Thread.VSRuntime.Id, Thread.SystemId, Id)));
     localsCache        = SimpleCache.Create(() => CreateVariableCollection(Proxy.GetClrStackFrameLocals(Thread.VSRuntime.Process.Id, Thread.VSRuntime.Id, Thread.SystemId, Id)));
     moduleCache        = SimpleCache.Create(() => Thread.VSRuntime.GetModule(moduleAddress));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Module" /> class.
        /// </summary>
        /// <param name="process">The process.</param>
        /// <param name="address">The module address.</param>
        internal Module(Process process, ulong address)
        {
            Address = address;
            Process = process;
            name    = SimpleCache.Create(() =>
            {
                string name = Context.Debugger.GetModuleName(this);

                Process.UpdateModuleByNameCache(this, name);
                return(name);
            });
            imageName       = SimpleCache.Create(() => Context.Debugger.GetModuleImageName(this));
            loadedImageName = SimpleCache.Create(() => Context.Debugger.GetModuleLoadedImage(this));
            symbolFileName  = SimpleCache.Create(() => Context.Debugger.GetModuleSymbolFile(this));
            mappedImageName = SimpleCache.Create(() => Context.Debugger.GetModuleMappedImage(this));
            moduleVersion   = SimpleCache.Create(() =>
            {
                ModuleVersion version = new ModuleVersion();

                Context.Debugger.GetModuleVersion(this, out version.Major, out version.Minor, out version.Revision, out version.Patch);
                return(version);
            });
            timestampAndSize = SimpleCache.Create(() => Context.Debugger.GetModuleTimestampAndSize(this));
            clrModule        = SimpleCache.Create(() => Process.ClrRuntimes.SelectMany(r => r.ClrRuntime.Modules).Where(m => m.ImageBase == Address).FirstOrDefault());
            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);
            });
            TypesByName     = new DictionaryCache <string, CodeType>(GetTypeByName);
            TypesById       = new DictionaryCache <uint, CodeType>(GetTypeById);
            ClrTypes        = new DictionaryCache <Microsoft.Diagnostics.Runtime.ClrType, CodeType>(GetClrCodeType);
            GlobalVariables = new DictionaryCache <string, Variable>(GetGlobalVariable);
            UserTypeCastedGlobalVariables = new DictionaryCache <string, Variable>((name) =>
            {
                Variable variable = Process.CastVariableToUserType(GlobalVariables[name]);

                if (UserTypeCastedGlobalVariables.Count == 0)
                {
                    GlobalCache.VariablesUserTypeCastedFieldsByName.Add(UserTypeCastedGlobalVariables);
                }

                return(variable);
            });
        }
 public ObjectResultTreeItem(object obj, Type objType, string name, ImageSource image, InteractiveResultVisualizer interactiveResultVisualizer)
 {
     this.obj     = obj;
     this.objType = objType;
     this.interactiveResultVisualizer = interactiveResultVisualizer;
     Name        = name;
     Image       = image;
     valueString = SimpleCache.Create(() => Value.ToString());
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Thread" /> class.
 /// </summary>
 /// <param name="id">The identifier.</param>
 /// <param name="systemId">The system identifier.</param>
 /// <param name="process">The process.</param>
 internal Thread(uint id, uint systemId, Process process)
 {
     Id            = id;
     SystemId      = systemId;
     Process       = process;
     tebAddress    = SimpleCache.Create(GetTEB);
     stackTrace    = SimpleCache.Create(GetStackTrace);
     threadContext = SimpleCache.Create(GetThreadContext);
     clrThread     = SimpleCache.Create(() => Process.ClrRuntimes.SelectMany(r => r.Threads).Where(t => t.SystemId == SystemId).FirstOrDefault());
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Initializes the <see cref="UserTypeDelegates"/> class.
        /// </summary>
        static UserTypeDelegates()
        {
            moduleBuilder = SimpleCache.Create(() =>
            {
                AssemblyName assemblyName       = new AssemblyName("SharpDebug.DynamicAssembly");
                AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

                return(assemblyBuilder.DefineDynamicModule("SharpDebug.Dynamic"));
            });
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Symbol"/> class.
 /// </summary>
 /// <param name="module">The module.</param>
 public Symbol(Module module)
 {
     Module      = module;
     fields      = SimpleCache.Create(() => GetFields().ToArray());
     baseClasses = SimpleCache.Create(() => GetBaseClasses().ToArray());
     elementType = SimpleCache.Create(() => GetElementType());
     pointerType = SimpleCache.Create(() => GetPointerType());
     enumValues  = SimpleCache.Create(() => GetEnumValues().ToArray());
     namespaces  = SimpleCache.Create(() => SymbolNameHelper.GetSymbolNamespaces(Name));
     userType    = SimpleCache.Create(() => (UserType)null);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResultVisualizer"/> class.
 /// </summary>
 /// <param name="result">Resulting object that should be visualized.</param>
 /// <param name="resultType">Type of the resulting object that should be visualized.</param>
 /// <param name="name">Name of the variable / property.</param>
 /// <param name="dataType">Data type that will be used to generate icon of the variable / property</param>
 /// <param name="interactiveResultVisualizer">Interactive result visualizer that can be used for creating UI elements.</param>
 public ResultVisualizer(object result, Type resultType, string name, CompletionDataType dataType, InteractiveResultVisualizer interactiveResultVisualizer)
 {
     this.result     = result;
     this.resultType = resultType;
     this.interactiveResultVisualizer = interactiveResultVisualizer;
     Name        = name;
     DataType    = dataType;
     value       = SimpleCache.Create(GetValue);
     typeString  = SimpleCache.Create(GetTypeString);
     valueString = SimpleCache.Create(GetValueString);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResultVisualizer"/> class.
 /// </summary>
 /// <param name="result">Resulting object that should be visualized.</param>
 /// <param name="resultType">Type of the resulting object that should be visualized.</param>
 /// <param name="name">Name of the variable / property.</param>
 /// <param name="image">Image that represents icon of the variable / property</param>
 /// <param name="interactiveResultVisualizer">Interactive result visualizer that can be used for creating UI elements.</param>
 public ResultVisualizer(object result, Type resultType, string name, ImageSource image, InteractiveResultVisualizer interactiveResultVisualizer)
 {
     this.result     = result;
     this.resultType = resultType;
     this.interactiveResultVisualizer = interactiveResultVisualizer;
     Name        = name;
     Image       = image;
     value       = SimpleCache.Create(GetValue);
     typeString  = SimpleCache.Create(GetTypeString);
     valueString = SimpleCache.Create(GetValueString);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InteractiveExecutionInitialization" /> class.
 /// </summary>
 /// <param name="interactiveExecutionBehavior">Customization of interactive execution.</param>
 /// <param name="cacheInvalidator">Cache invalidator that will be used to create simple cache for creating interactive execution.</param>
 public InteractiveExecutionInitialization(InteractiveExecutionBehavior interactiveExecutionBehavior, CacheInvalidator cacheInvalidator = null)
 {
     InteractiveExecutionBehavior = interactiveExecutionBehavior;
     if (cacheInvalidator != null)
     {
         InteractiveExecutionCache = cacheInvalidator.CreateSimpleCache(CreateInteractiveExecution);
     }
     else
     {
         InteractiveExecutionCache = SimpleCache.Create(CreateInteractiveExecution);
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClrMdThread"/> class.
        /// </summary>
        /// <param name="runtime">The Visual Studio runtime.</param>
        /// <param name="threadId">The thread system id.</param>
        /// <param name="isFinalizerThread">Is this finalizer thread.</param>
        /// <param name="appDomainAddress">The application domain address.</param>
        public VSClrThread(VSClrRuntime runtime, uint threadId, bool isFinalizerThread, ulong appDomainAddress)
        {
            VSRuntime           = runtime;
            SystemId            = threadId;
            IsFinalizerThread   = isFinalizerThread;
            clrStackFramesCache = SimpleCache.Create(() =>
            {
                Tuple <int, ulong, ulong, ulong>[] frames = Proxy.GetClrThreadFrames(Runtime.Process.Id, runtime.Id, SystemId);
                VSClrStackFrame[] clrFrames = new VSClrStackFrame[frames.Length];

                for (int i = 0; i < frames.Length; i++)
                {
                    clrFrames[i] = new VSClrStackFrame(this, frames[i].Item1, frames[i].Item2, frames[i].Item3, frames[i].Item4);
                }
                return(clrFrames);
            });
            stackTraceCache = SimpleCache.Create(() =>
            {
                Thread thread         = VSRuntime.Process.Threads.First(t => t.SystemId == SystemId);
                StackTrace stackTrace = new StackTrace(thread);
                uint frameNumber      = 0;

                stackTrace.Frames = clrStackFramesCache.Value.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 = f,
                    });
                }).ToArray();
                return(stackTrace);
            });
            appDomainCache           = SimpleCache.Create(() => Runtime.AppDomains.Single(a => a.Address == appDomainAddress));
            lastThrownExceptionCache = SimpleCache.Create(() =>
            {
                Tuple <ulong, int> tuple = Proxy.GetClrThreadLastException(Runtime.Process.Id, runtime.Id, SystemId);
                ulong address            = tuple.Item1;
                IClrType clrType         = runtime.GetClrType(tuple.Item2);

                if (clrType == null)
                {
                    return(null);
                }
                return(Variable.CreatePointer(runtime.Process.FromClrType(clrType), address));
            });
        }
Ejemplo n.º 19
0
        public void Disposable()
        {
            int disposedCount = 0;

            using (SimpleCache <DisposableAction> cache = SimpleCache.Create(() => new DisposableAction(() => disposedCount++)))
            {
                var v0 = cache.Value;

                cache.InvalidateCache();
                Assert.Equal(1, disposedCount);

                var v1 = cache.Value;
            }
            Assert.Equal(2, disposedCount);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DiaModule"/> class.
        /// </summary>
        /// <param name="pdbPath">The PDB path.</param>
        /// <param name="module">The module.</param>
        public DiaModule(string pdbPath, Module module)
        {
            Module = module;
            dia    = new DiaSource();
            dia.loadDataFromPdb(pdbPath);
            dia.openSession(out session);
            globalScope   = session.globalScope;
            typeAllFields = new DictionaryCache <uint, List <Tuple <string, uint, int> > >(GetTypeAllFields);
            typeFields    = new DictionaryCache <uint, List <Tuple <string, uint, int> > >(GetTypeFields);
            basicTypes    = SimpleCache.Create(() =>
            {
                var types      = new Dictionary <string, IDiaSymbol>();
                var basicTypes = globalScope.GetChildren(SymTagEnum.SymTagBaseType);

                foreach (var type in basicTypes)
                {
                    try
                    {
                        string typeString = TypeToString.GetTypeString(type);

                        if (!types.ContainsKey(typeString))
                        {
                            types.Add(typeString, type);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                return(types);
            });
            symbolNamesByAddress = new DictionaryCache <uint, Tuple <string, ulong> >((distance) =>
            {
                IDiaSymbol symbol;
                int displacement;
                string name;

                session.findSymbolByRVAEx(distance, SymTagEnum.SymTagNull, out symbol, out displacement);
                symbol.get_undecoratedNameEx(0 | 0x8000 | 0x1000, out name);
                return(Tuple.Create(name, (ulong)displacement));
            });

            session.loadAddress = module.Address;
            enumTypeNames       = new DictionaryCache <uint, Dictionary <ulong, string> >(GetEnumName);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Module" /> class.
 /// </summary>
 /// <param name="path">The module path.</param>
 /// <param name="address">The module address.</param>
 /// <param name="_pointerSize">The module's native pointerSize.</param>
 public Module(string path, ulong address = 0, uint _pointerSize = 8)
 {
     Address         = address;
     Process         = default(Process);
     Id              = uint.MaxValue;
     name            = SimpleCache.Create(() => path);
     imageName       = SimpleCache.Create(() => path);
     loadedImageName = SimpleCache.Create(() => path);
     symbolFileName  = SimpleCache.Create(() => path);
     mappedImageName = SimpleCache.Create(() => path);
     clrModule       = null;
     pointerSize     = SimpleCache.Create(() => _pointerSize);
     TypesByName     = new DictionaryCache <string, CodeType>(GetTypeByName);
     TypesById       = new DictionaryCache <uint, CodeType>(GetTypeById);
     ClrTypes        = new DictionaryCache <IClrType, CodeType>(GetClrCodeType);
     GlobalVariables = new DictionaryCache <string, Variable>(GetGlobalVariable);
     UserTypeCastedGlobalVariables = Context.UserTypeMetadataCaches.CreateDictionaryCache <string, Variable>((name) => Process.CastVariableToUserType(GlobalVariables[name]));
 }
Ejemplo n.º 22
0
        public void SingleEvaluation()
        {
            int count = 0;
            SimpleCache <int> cache = SimpleCache.Create(() =>
            {
                count++;
                return(42);
            });

            Assert.Equal(0, count);
            Assert.False(cache.Cached);
            Assert.Equal(42, cache.Value);
            Assert.Equal(1, count);
            Assert.True(cache.Cached);
            Assert.Equal(42, cache.Value);
            Assert.Equal(1, count);
            Assert.Single(cache, 42);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Initializes this instance of the <see cref="DiaModule"/> class.
        /// </summary>
        /// <param name="diaSession">The DIA session.</param>
        /// <param name="module">The module.</param>
        private void Initialize(IDiaSession diaSession, Module module)
        {
            Module        = module;
            session       = diaSession;
            globalScope   = session.globalScope;
            typeAllFields = new DictionaryCache <uint, List <Tuple <string, uint, int> > >(GetTypeAllFields);
            typeFields    = new DictionaryCache <uint, List <Tuple <string, uint, int> > >(GetTypeFields);
            basicTypes    = SimpleCache.Create(() =>
            {
                var types      = new Dictionary <string, IDiaSymbol>();
                var basicTypes = globalScope.GetChildren(SymTagEnum.BaseType);

                foreach (var type in basicTypes)
                {
                    try
                    {
                        string typeString = TypeToString.GetTypeString(type);

                        if (!types.ContainsKey(typeString))
                        {
                            types.Add(typeString, type);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                return(types);
            });
            symbolNamesByAddress = new DictionaryCache <uint, Tuple <string, ulong> >((distance) =>
            {
                IDiaSymbol symbol;
                int displacement;
                string name;

                session.findSymbolByRVAEx(distance, SymTagEnum.Null, out symbol, out displacement);
                name = symbol.get_undecoratedNameEx(UndecoratedNameOptions.NameOnly | UndecoratedNameOptions.NoEscu);
                return(Tuple.Create(name, (ulong)displacement));
            });

            session.loadAddress = module.Address;
            enumTypeNames       = new DictionaryCache <uint, Dictionary <ulong, string> >(GetEnumName);
        }
Ejemplo n.º 24
0
        public void EvaluationAfterInvalidate()
        {
            int count = 0;
            SimpleCache <int> cache = SimpleCache.Create(() =>
            {
                count++;
                return(42);
            });

            Assert.Equal(0, count);
            Assert.Equal(42, cache.Value);
            Assert.Equal(1, count);
            Assert.Equal(42, cache.Value);
            Assert.Equal(1, count);
            cache.InvalidateCache();
            Assert.Equal(1, count);
            Assert.Equal(42, cache.Value);
            Assert.Equal(2, count);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Process"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        internal Process(uint id)
        {
            Id                     = id;
            systemId               = SimpleCache.Create(() => Context.Debugger.GetProcessSystemId(this));
            upTime                 = SimpleCache.Create(() => Context.Debugger.GetProcessUpTime(this));
            pebAddress             = SimpleCache.Create(() => Context.Debugger.GetProcessEnvironmentBlockAddress(this));
            executableName         = SimpleCache.Create(() => Context.Debugger.GetProcessExecutableName(this));
            dumpFileName           = SimpleCache.Create(() => Context.Debugger.GetProcessDumpFileName(this));
            actualProcessorType    = SimpleCache.Create(() => Context.Debugger.GetProcessActualProcessorType(this));
            effectiveProcessorType = SimpleCache.Create(() => Context.Debugger.GetProcessEffectiveProcessorType(this));
            threads                = SimpleCache.Create(() => Context.Debugger.GetProcessThreads(this));
            modules                = SimpleCache.Create(() => Context.Debugger.GetProcessModules(this));
            userTypes              = SimpleCache.Create(GetUserTypes);
            clrDataTarget          = SimpleCache.Create(() =>
            {
                var dataTarget = Microsoft.Diagnostics.Runtime.DataTarget.CreateFromDataReader(new CsDebugScript.CLR.DataReader(this));

                dataTarget.SymbolLocator.SymbolPath += ";http://symweb";
                return(dataTarget);
            });
            clrRuntimes             = SimpleCache.Create(() => ClrDataTarget.ClrVersions.Select(clrInfo => new Runtime(this, clrInfo.CreateRuntime())).ToArray());
            currentAppDomain        = SimpleCache.Create(() => ClrRuntimes.SelectMany(r => r.AppDomains).FirstOrDefault());
            ModulesByName           = new DictionaryCache <string, Module>(GetModuleByName);
            ModulesById             = new DictionaryCache <ulong, Module>(GetModuleByAddress);
            Variables               = new DictionaryCache <Tuple <CodeType, ulong, string, string>, Variable>((tuple) => new Variable(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4));
            UserTypeCastedVariables = new DictionaryCache <Variable, Variable>((variable) => Variable.CastVariableToUserType(variable));
            GlobalCache.UserTypeCastedVariables.Add(UserTypeCastedVariables);
            dumpFileMemoryReader = SimpleCache.Create(() =>
            {
                try
                {
                    return(string.IsNullOrEmpty(DumpFileName) ? null : new DumpFileMemoryReader(DumpFileName));
                }
                catch (Exception)
                {
                    return(null);
                }
            });
            TypeToUserTypeDescription = new DictionaryCache <Type, UserTypeDescription[]>(GetUserTypeDescription);
            ansiStringCache           = new DictionaryCache <Tuple <ulong, int>, string>(DoReadAnsiString);
            unicodeStringCache        = new DictionaryCache <Tuple <ulong, int>, string>(DoReadUnicodeString);
        }
Ejemplo n.º 26
0
        public void SettingValue()
        {
            int count = 0;
            SimpleCache <int> cache = SimpleCache.Create(() =>
            {
                count++;
                return(42);
            });

            Assert.Equal(0, count);
            Assert.Equal(42, cache.Value);
            Assert.Equal(1, count);
            cache.Value = 12345;
            Assert.Equal(12345, cache.Value);
            Assert.Equal(1, count);
            cache.InvalidateCache();
            Assert.Equal(1, count);
            Assert.Equal(42, cache.Value);
            Assert.Equal(2, count);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VSClrAppDomain" /> class.
        /// </summary>
        /// <param name="runtime">The runtime.</param>
        /// <param name="id">The application domain identifier.</param>
        /// <param name="address">The application domain address.</param>
        /// <param name="applicationBase">The application domain base directory.</param>
        /// <param name="configurationFile">The configuration file used for application domain.</param>
        public VSClrAppDomain(VSClrRuntime runtime, int id, string name, ulong address, string applicationBase, string configurationFile)
        {
            VSRuntime         = runtime;
            Id                = id;
            Name              = name;
            Address           = address;
            ApplicationBase   = applicationBase;
            ConfigurationFile = configurationFile;
            modulesCache      = SimpleCache.Create(() =>
            {
                ulong[] moduleAddresses = VSRuntime.Proxy.GetClrAppDomainModules(VSRuntime.Process.Id, VSRuntime.Id, Id);
                VSClrModule[] modules   = new VSClrModule[moduleAddresses.Length];

                for (int i = 0; i < modules.Length; i++)
                {
                    modules[i] = VSRuntime.GetModule(moduleAddresses[i]);
                }
                return(modules);
            });
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Initializes the <see cref="UserTypeDelegates"/> class.
        /// </summary>
        static UserTypeDelegates()
        {
            moduleBuilder = SimpleCache.Create(() =>
            {
                AssemblyName assemblyName = new AssemblyName("CsDebugScript.DynamicAssembly");
                byte[] bytes;

                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CsDebugScript.Key.snk"))
                {
                    bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                }

                assemblyName.KeyPair = new StrongNameKeyPair(bytes);

                AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

                return(assemblyBuilder.DefineDynamicModule("CsDebugScript.Dynamic"));
            });
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Runtime" /> class.
        /// </summary>
        /// <param name="process">The process.</param>
        /// <param name="clrRuntime">The CLR runtime.</param>
        internal Runtime(Process process, Microsoft.Diagnostics.Runtime.ClrRuntime clrRuntime)
        {
            Process      = process;
            ClrRuntime   = clrRuntime;
            appDomains   = SimpleCache.Create(() => ClrRuntime.AppDomains.Select(ad => new AppDomain(this, ad)).ToArray());
            modules      = SimpleCache.Create(() => ClrRuntime.Modules.Select(mm => Process.Modules.Where(m => m.Address == mm.ImageBase).FirstOrDefault()).Where(m => m != null).ToArray());
            sharedDomain = SimpleCache.Create(() => ClrRuntime.SharedDomain != null ? new AppDomain(this, ClrRuntime.SharedDomain) : null);
            systemDomain = SimpleCache.Create(() => ClrRuntime.SystemDomain != null ? new AppDomain(this, ClrRuntime.SystemDomain) : null);
            threads      = SimpleCache.Create(() => ClrRuntime.Threads.Select(tt => Process.Threads.Where(t => t.SystemId == tt.OSThreadId).FirstOrDefault()).Where(t => t != null).ToArray());
            gcThreads    = SimpleCache.Create(() => ClrRuntime.EnumerateGCThreads().Select(tt => Process.Threads.Where(t => t.SystemId == tt).FirstOrDefault()).Where(t => t != null).ToArray());

            var version = ClrRuntime.ClrInfo.Version;

            Version = new ModuleVersion()
            {
                Major    = version.Major,
                Minor    = version.Minor,
                Patch    = version.Patch,
                Revision = version.Revision,
            };
        }
Ejemplo n.º 30
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);
            });
        }