コード例 #1
0
ファイル: ModuleCreator.cs プロジェクト: azureidea/dnSpy-1
        Func <DmdLazyMetadataBytes> CreateNormalGetMetadataDelegate(ClosedListenerCollection closedListenerCollection, DbgImageLayout imageLayout)
        {
            Debug.Assert(!isDynamic);

            var moduleAddressTmp = moduleAddress;
            var moduleSizeTmp    = moduleSize;
            var engine           = this.engine;
            var runtime          = objectFactory.Runtime;
            var filename         = monoModule.FullyQualifiedName;

            return(() => {
                DbgRawMetadata rawMd = null;
                try {
                    if (moduleAddressTmp != 0 && moduleSizeTmp != 0)
                    {
                        rawMd = engine.RawMetadataService.Create(runtime, imageLayout == DbgImageLayout.File, moduleAddressTmp, (int)moduleSizeTmp);
                    }
                    else if (File.Exists(filename))
                    {
                        rawMd = engine.RawMetadataService.Create(runtime, true, File.ReadAllBytes(filename));
                    }
                    else
                    {
                        //TODO:
                        rawMd = engine.RawMetadataService.Create(runtime, imageLayout == DbgImageLayout.File, Array.Empty <byte>());
                    }
                    closedListenerCollection.Closed += (s, e) => rawMd.Release();
                    return new DmdLazyMetadataBytesPtr(rawMd.Address, (uint)rawMd.Size, rawMd.IsFileLayout);
                }
                catch {
                    rawMd?.Release();
                    throw;
                }
            });
        }
コード例 #2
0
 Func <DmdLazyMetadataBytes> CreateGetMetadataDelegate(ClosedListenerCollection closedListenerCollection, DbgImageLayout imageLayout)
 {
     if (isDynamic)
     {
         return(CreateDynamicGetMetadataDelegate());
     }
     else
     {
         return(CreateNormalGetMetadataDelegate(closedListenerCollection, imageLayout));
     }
 }
コード例 #3
0
 public DbgMonoDebugInternalModuleImpl(DmdModule reflectionModule, ClosedListenerCollection closedListenerCollection)
 {
     ReflectionModule = reflectionModule ?? throw new ArgumentNullException(nameof(reflectionModule));
     this.closedListenerCollection = closedListenerCollection ?? throw new ArgumentNullException(nameof(closedListenerCollection));
 }
コード例 #4
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)));
        }