Beispiel #1
0
        DbgEngineModule CreateModuleCore <T>(T data) where T : class
        {
            DbgImageLayout imageLayout;

            string       filename             = monoModule.FullyQualifiedName;
            const string InMemoryModulePrefix = "data-";

            if (filename.StartsWith(InMemoryModulePrefix, StringComparison.Ordinal))
            {
                isDynamic     = false;
                isInMemory    = true;
                moduleAddress = 0;
                moduleSize    = 0;
                imageLayout   = DbgImageLayout.File;

                var  hexAddrString = filename.Substring(InMemoryModulePrefix.Length);
                bool b             = ulong.TryParse(hexAddrString, NumberStyles.HexNumber, null, out var inMemoryAddr);
                Debug.Assert(b);
                if (b)
                {
                    moduleAddress = inMemoryAddr;
                    b             = PortableExecutableHelper.TryGetSizeOfImage(engine.DbgRuntime.Process, moduleAddress, imageLayout == DbgImageLayout.File, out uint imageSize);
                    Debug.Assert(b);
                    if (b)
                    {
                        moduleSize = imageSize;
                    }
                }
            }
            else if (File.Exists(filename))
            {
                filename      = NormalizeFilename(filename);
                isDynamic     = false;
                isInMemory    = false;
                moduleAddress = 0;
                moduleSize    = 0;
                if (PortableExecutableHelper.TryGetModuleAddressAndSize(engine.DbgRuntime.Process, filename, out var imageAddr, out var imageSize))
                {
                    moduleAddress = imageAddr;
                    moduleSize    = imageSize;
                }
                imageLayout = moduleAddress == 0 ? DbgImageLayout.File : DbgImageLayout.Memory;
            }
            else
            {
                isDynamic     = true;
                isInMemory    = true;
                moduleAddress = 0;
                moduleSize    = 0;
                imageLayout   = DbgImageLayout.Unknown;
            }

            string name        = GetFilename(filename);
            bool?  isOptimized = CalculateIsOptimized();

            InitializeExeFields(filename, imageLayout, out var isExe, out var isDll, out var timestamp, out var version, out var assemblySimpleName);

            var reflectionAppDomain = ((DbgMonoDebugInternalAppDomainImpl)appDomain.InternalAppDomain).ReflectionAppDomain;

            var         closedListenerCollection = new ClosedListenerCollection();
            var         getMetadata        = CreateGetMetadataDelegate(closedListenerCollection, imageLayout);
            var         fullyQualifiedName = DmdModule.GetFullyQualifiedName(isInMemory, isDynamic, filename);
            DmdAssembly reflectionAssembly;
            DmdModule   reflectionModule;

            if (monoModule == monoModule.Assembly.ManifestModule)
            {
                var assemblyLocation = isInMemory || isDynamic ? string.Empty : filename;
                var asmOptions       = DmdCreateAssemblyOptions.None;
                if (isInMemory)
                {
                    asmOptions |= DmdCreateAssemblyOptions.InMemory;
                }
                if (isDynamic)
                {
                    asmOptions |= DmdCreateAssemblyOptions.Dynamic;
                }
                if (isExe)
                {
                    asmOptions |= DmdCreateAssemblyOptions.IsEXE;
                }
                else if (isDll)
                {
                    asmOptions |= DmdCreateAssemblyOptions.IsDLL;
                }
                var asmInfo = new DmdCreateAssemblyInfo(asmOptions, fullyQualifiedName, assemblyLocation, assemblySimpleName);
                reflectionAssembly = reflectionAppDomain.CreateAssembly(getMetadata, asmInfo);
                reflectionModule   = reflectionAssembly.ManifestModule;
            }
            else
            {
                var manifestModule = engine.TryGetModule(monoModule.Assembly.ManifestModule);
                if (manifestModule == null)
                {
                    throw new InvalidOperationException();
                }
                reflectionAssembly = ((DbgMonoDebugInternalModuleImpl)manifestModule.InternalModule).ReflectionModule.Assembly;
                reflectionModule   = reflectionAppDomain.CreateModule(reflectionAssembly, getMetadata, isInMemory, isDynamic, fullyQualifiedName);
            }

            var internalModule = new DbgMonoDebugInternalModuleImpl(reflectionModule, closedListenerCollection);

            return(objectFactory.CreateModule(appDomain, internalModule, isExe, moduleAddress, moduleSize, imageLayout, name, filename, isDynamic, isInMemory, isOptimized, moduleOrder, timestamp, version, engine.GetMessageFlags(), data: data, onCreated: engineModule => internalModule.SetModule(engineModule.Module)));
        }
Beispiel #2
0
        DmdType?CreateCore(TypeMirror type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (recursionCounter++ > 100)
            {
                throw new InvalidOperationException();
            }

            List <DmdType>?types;
            DmdType?       result;

            if (!typeCache.TryGetType(type, out result))
            {
                bool canAddType = true;
                if (type.IsByRef)
                {
                    result = CreateCore(type.GetElementType())?.MakeByRefType();
                }
                else if (type.IsArray)
                {
                    if (type.GetArrayRank() == 1)
                    {
                        if (type.FullName.EndsWith("[*]", StringComparison.Ordinal))
                        {
                            result = CreateCore(type.GetElementType())?.MakeArrayType(1);
                        }
                        else
                        {
                            result = CreateCore(type.GetElementType())?.MakeArrayType();
                        }
                    }
                    else
                    {
                        result = CreateCore(type.GetElementType())?.MakeArrayType(type.GetArrayRank());
                    }
                }
                else if (type.IsPointer)
                {
                    result = CreateCore(type.GetElementType())?.MakePointerType();
                }
                else
                {
                    var module         = engine.TryGetModule(type.Module)?.GetReflectionModule() ?? throw new InvalidOperationException();
                    var reflectionType = module.ResolveType(type.MetadataToken, DmdResolveOptions.None);
                    if (!(reflectionType is null) && reflectionType.GetGenericArguments().Count != 0)
                    {
                        DmdType?     parsedType = null;
                        TypeMirror[] genericArgs;
                        if (type.VirtualMachine.Version.AtLeast(2, 15))
                        {
                            genericArgs = type.GetGenericArguments();
                        }
                        else
                        {
                            parsedType = reflectionType.Assembly.GetType(type.FullName);
                            if (!(parsedType is null) && parsedType.MetadataToken != type.MetadataToken)
                            {
                                parsedType = null;
                            }
                            genericArgs = Array.Empty <TypeMirror>();
                            canAddType  = !(parsedType is null);
                        }

                        if (!(parsedType is null))
                        {
                            reflectionType = parsedType;
                        }
                        else
                        {
                            types = GetTypesList();
                            foreach (var t in genericArgs)
                            {
                                var newType = CreateCore(t);
                                if (newType is null)
                                {
                                    reflectionType = null;
                                    break;
                                }
                                types.Add(newType);
                            }
                            if (!(reflectionType is null))
                            {
                                Debug.Assert(types.Count == 0 || reflectionType.GetGenericArguments().Count == types.Count);
                                if (types.Count != 0)
                                {
                                    reflectionType = reflectionType.MakeGenericType(types.ToArray());
                                }
                            }
                            FreeTypesList(ref types);
                        }
                    }