Ejemplo n.º 1
0
        ModuleDef FindMemoryModule(DbgModule module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }
            if (!module.HasAddress)
            {
                return(null);
            }
            var key = MemoryModuleDefDocument.CreateKey(module.Process, module.Address);

            return(AllMemoryModuleDefDocuments.FirstOrDefault(a => key.Equals(a.Key))?.ModuleDef);
        }
Ejemplo n.º 2
0
        internal void UpdateModuleMemory(MemoryModuleDefDocument document)
        {
            uiDispatcher.VerifyAccess();
            if (document.TryUpdateMemory())
            {
                RefreshBodies(document);
            }

            // Always reset all breakpoints. If we set breakpoints (and fail) and later the module
            // gets decrypted and we then open the in-memory copy, TryUpdateMemory() will return
            // false ("no changes"), but the breakpoints will still need to be refreshed.
            var modules = GetModules(document.Process, document.Address);

            if (modules.Length > 0)
            {
                dbgModuleMemoryRefreshedNotifier.Value.RaiseModulesRefreshed(modules);
            }
        }
Ejemplo n.º 3
0
        void RefreshBodies(MemoryModuleDefDocument document)
        {
            uiDispatcher.VerifyAccess();

            if (document.ModuleDef.EnableTypeDefFindCache)
            {
                document.ModuleDef.EnableTypeDefFindCache = false;
                document.ModuleDef.EnableTypeDefFindCache = true;
            }

            // Free all method bodies and clear cache so the new bodies are shown if any
            // got modified (eg. decrypted in memory)
            for (uint rid = 1; ; rid++)
            {
                var md = document.ModuleDef.ResolveToken(new MDToken(Table.Method, rid)) as MethodDef;
                if (md == null)
                {
                    break;
                }
                methodAnnotations.Value.SetBodyModified(md, false);
                md.FreeMethodBody();
            }
            documentTabService.Value.RefreshModifiedDocument(document);
        }
Ejemplo n.º 4
0
        ModuleDef LoadMemoryModule(DbgModule module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            if (!TryGetRuntimeInfo(module.Runtime, out var info))
            {
                return(null);
            }

            Debug.Assert(!module.IsDynamic);
            if (!module.HasAddress)
            {
                return(null);
            }

            var doc = FindMemoryModule(module);

            if (doc != null)
            {
                return(doc);
            }

            MemoryModuleDefDocument result = null;

            lock (lockObj) {
                doc = FindMemoryModule(module);
                if (doc != null)
                {
                    return(doc);
                }

                var modules = info.AssemblyInfoProvider.GetAssemblyModules(module);
                if (modules.Length == 0)
                {
                    return(null);
                }
                var manifestModule = modules[0];
                var manifestKey    = MemoryModuleDefDocument.CreateKey(manifestModule.Process, manifestModule.Address);
                var manMod         = FindDocument(manifestKey);
                Debug.Assert(manMod == null);
                if (manMod != null)
                {
                    return(null);
                }

                var manDoc = FindMemoryModule(manifestModule);
                Debug.Assert(manDoc == null);
                if (manDoc != null)
                {
                    return(null);
                }

                var docs = new List <MemoryModuleDefDocument>(modules.Length);
                foreach (var m in modules)
                {
                    MemoryModuleDefDocument modDoc;
                    try {
                        modDoc = MemoryModuleDefDocument.Create(this, m, UseDebugSymbols);
                        UpdateResolver(modDoc.ModuleDef);
                        if (m == module)
                        {
                            result = modDoc;
                        }
                    }
                    catch {
                        // The PE headers and/or .NET headers are probably corrupt
                        return(LoadDynamicModule(module));
                    }
                    docs.Add(modDoc);
                }
                Debug.Assert(result != null);
                if (docs.Count == 0 || result == null)
                {
                    return(null);
                }
                var asmFile = MemoryModuleDefDocument.CreateAssembly(docs);
                var asm     = docs[0].AssemblyDef;
                if (asm == null)
                {
                    if (docs.Count > 1)
                    {
                        asm = docs[0].ModuleDef.UpdateRowId(new AssemblyDefUser("???"));
                        asm.Modules.Add(docs[0].ModuleDef);
                    }
                }
                asm.Modules.Clear();
                for (int i = 0; i < docs.Count; i++)
                {
                    RemoveFromAssembly(docs[i].ModuleDef);
                    asm.Modules.Add(docs[i].ModuleDef);
                }

                var addedFile = documentProvider.GetOrAdd(asmFile);
                Debug.Assert(addedFile == asmFile);
            }

            // The modules got loaded for the first time, but it's possible that the debugger is using the
            // old disk file modules. Raise an event so the debugger rereads the memory.
            var newModules = GetModules(result.Process, result.Address);

            if (newModules.Length > 0)
            {
                dbgModuleMemoryRefreshedNotifier.Value.RaiseModulesRefreshed(newModules);
            }

            return(result.ModuleDef);
        }
Ejemplo n.º 5
0
        void OnModuleAdded_UI(RuntimeInfo info, DbgModule manifestModule, DbgModule module)
        {
            uiDispatcher.VerifyAccess();

            // If an assembly is visible in the treeview, and a new netmodule gets added, add a
            // new netmodule node to the assembly in the treeview.

            // Update a dynamic assembly, if one exists
            if (info.DynamicModuleProvider != null)
            {
                var manifestKey = DynamicModuleDefDocument.CreateKey(manifestModule);
                var asmFile     = FindDocument(manifestKey);
                if (documentTreeView.Value.FindNode(asmFile) is AssemblyDocumentNode asmNode)
                {
                    var moduleKey = DynamicModuleDefDocument.CreateKey(module);
                    asmNode.TreeNode.EnsureChildrenLoaded();
                    Debug.Assert(asmNode.TreeNode.Children.Count >= 1);
                    var moduleNode = asmNode.TreeNode.DataChildren.OfType <ModuleDocumentNode>().FirstOrDefault(a => moduleKey.Equals(a.Document.Key));
                    Debug.Assert(moduleNode == null);
                    if (moduleNode == null)
                    {
                        var md = info.DynamicModuleProvider.GetDynamicMetadata(module, out var moduleId);
                        if (md != null)
                        {
                            UpdateResolver(md);
                            var newFile = new DynamicModuleDefDocument(moduleId, module, md, UseDebugSymbols);
                            asmNode.Document.Children.Add(newFile);
                            Initialize_UI(info, new[] { newFile });
                            asmNode.TreeNode.Children.Add(documentTreeView.Value.TreeView.Create(documentTreeView.Value.CreateNode(asmNode, newFile)));
                        }
                    }
                }
            }

            // Update an in-memory assembly, if one exists
            if (manifestModule.HasAddress && module.HasAddress)
            {
                var manifestKey = MemoryModuleDefDocument.CreateKey(manifestModule.Process, manifestModule.Address);
                var asmFile     = FindDocument(manifestKey);
                if (documentTreeView.Value.FindNode(asmFile) is AssemblyDocumentNode asmNode)
                {
                    var moduleKey = MemoryModuleDefDocument.CreateKey(module.Process, module.Address);
                    asmNode.TreeNode.EnsureChildrenLoaded();
                    Debug.Assert(asmNode.TreeNode.Children.Count >= 1);
                    var moduleNode = asmNode.TreeNode.DataChildren.OfType <ModuleDocumentNode>().FirstOrDefault(a => moduleKey.Equals(a.Document.Key));
                    Debug.Assert(moduleNode == null);
                    if (moduleNode == null)
                    {
                        MemoryModuleDefDocument newFile = null;
                        try {
                            newFile = MemoryModuleDefDocument.Create(this, module, UseDebugSymbols);
                        }
                        catch {
                        }

                        Debug.Assert(newFile != null);
                        if (newFile != null)
                        {
                            UpdateResolver(newFile.ModuleDef);
                            asmNode.Document.Children.Add(newFile);
                            RemoveFromAssembly(newFile.ModuleDef);
                            asmNode.Document.ModuleDef.Assembly.Modules.Add(newFile.ModuleDef);
                            asmNode.TreeNode.Children.Add(documentTreeView.Value.TreeView.Create(documentTreeView.Value.CreateNode(asmNode, newFile)));
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
 public Context(MemoryModuleDefDocument file) => MemoryModuleDefDocument = file;
Ejemplo n.º 7
0
        ModuleDef LoadMemoryModule(DbgModule module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            if (!TryGetRuntimeInfo(module.Runtime, out var info))
            {
                return(null);
            }

            Debug.Assert(!module.IsDynamic);
            if (!module.HasAddress)
            {
                return(null);
            }

            var doc = FindMemoryModule(module);

            if (doc != null)
            {
                return(doc);
            }

            lock (lockObj) {
                doc = FindMemoryModule(module);
                if (doc != null)
                {
                    return(doc);
                }

                var modules = info.AssemblyInfoProvider.GetAssemblyModules(module);
                if (modules.Length == 0)
                {
                    return(null);
                }
                var manifestModule = modules[0];
                var manifestKey    = MemoryModuleDefDocument.CreateKey(manifestModule.Process, manifestModule.Address);
                var manMod         = FindDocument(manifestKey);
                Debug.Assert(manMod == null);
                if (manMod != null)
                {
                    return(null);
                }

                var manDoc = FindMemoryModule(manifestModule);
                Debug.Assert(manDoc == null);
                if (manDoc != null)
                {
                    return(null);
                }

                var docs = new List <MemoryModuleDefDocument>(modules.Length);
                MemoryModuleDefDocument result = null;
                foreach (var m in modules)
                {
                    MemoryModuleDefDocument modDoc;
                    try {
                        modDoc = MemoryModuleDefDocument.Create(this, m, UseDebugSymbols);
                        UpdateResolver(modDoc.ModuleDef);
                        if (m == module)
                        {
                            result = modDoc;
                        }
                    }
                    catch {
                        // The PE headers and/or .NET headers are probably corrupt
                        return(LoadDynamicModule(module));
                    }
                    docs.Add(modDoc);
                }
                Debug.Assert(result != null);
                if (docs.Count == 0 || result == null)
                {
                    return(null);
                }
                var asmFile = MemoryModuleDefDocument.CreateAssembly(docs);
                var asm     = docs[0].AssemblyDef;
                if (asm == null)
                {
                    if (docs.Count > 1)
                    {
                        asm = docs[0].ModuleDef.UpdateRowId(new AssemblyDefUser("???"));
                        asm.Modules.Add(docs[0].ModuleDef);
                    }
                }
                asm.Modules.Clear();
                for (int i = 0; i < docs.Count; i++)
                {
                    RemoveFromAssembly(docs[i].ModuleDef);
                    asm.Modules.Add(docs[i].ModuleDef);
                }

                var addedFile = documentProvider.GetOrAdd(asmFile);
                Debug.Assert(addedFile == asmFile);

                return(result.ModuleDef);
            }
        }