Ghidra is a software reverse engineering (SRE) framework
GP-762: Converting GDB to use a model-wide object cache
d-millar3407d6af258a
diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelImpl.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelImpl.javaindex 78d830992e..35c7b035a8 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelImpl.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelImpl.java@@ -16,6 +16,8 @@package agent.gdb.model.impl;import java.io.IOException;+import java.util.HashMap;+import java.util.Map;import java.util.concurrent.CompletableFuture;import org.apache.commons.lang3.exception.ExceptionUtils;@@ -63,6 +65,8 @@ public class GdbModelImpl extends AbstractDebuggerObjectModel {protected final CompletableFuture<GdbModelTargetSession> completedSession;protected final GdbStateListener gdbExitListener = this::checkExited;+ protected Map<Object, TargetObject> objectMap = new HashMap<>();+public GdbModelImpl() {this.gdb = GdbManager.newInstance();this.session = new GdbModelTargetSession(this, ROOT_SCHEMA);@@ -162,4 +166,17 @@ public class GdbModelImpl extends AbstractDebuggerObjectModel {return CompletableFuture.failedFuture(t);}}++ public void addModelObject(Object object, TargetObject targetObject) {+ objectMap.put(object, targetObject);+ }++ public TargetObject getModelObject(Object object) {+ return objectMap.get(object);+ }++ public void deleteModelObject(Object object) {+ objectMap.remove(object);+ }+}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointLocation.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointLocation.javaindex b7c0207c7d..0d26511bd4 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointLocation.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointLocation.java@@ -33,11 +33,9 @@ import ghidra.program.model.address.Address;@TargetObjectSchemaInfo(name = "BreakpointLocation",elements = {- @TargetElementType(type = Void.class)- },+ @TargetElementType(type = Void.class) },attributes = {- @TargetAttributeType(type = Void.class)- })+ @TargetAttributeType(type = Void.class) })public class GdbModelTargetBreakpointLocationextends DefaultTargetObject<TargetObject, GdbModelTargetBreakpointSpec>implements TargetBreakpointLocation {@@ -61,6 +59,7 @@ public class GdbModelTargetBreakpointLocationsuper(spec.impl, spec, keyLocation(loc), "BreakpointLocation");this.impl = spec.impl;this.loc = loc;+ impl.addModelObject(loc, this);if (!spec.info.getType().isWatchpoint()) {this.address = doGetAddress();@@ -74,8 +73,8 @@ public class GdbModelTargetBreakpointLocationSPEC_ATTRIBUTE_NAME, parent,ADDRESS_ATTRIBUTE_NAME, address,LENGTH_ATTRIBUTE_NAME, length,- DISPLAY_ATTRIBUTE_NAME, display = computeDisplay() //- ), reason);+ DISPLAY_ATTRIBUTE_NAME, display = computeDisplay()),+ reason);placeLocations();}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointSpec.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointSpec.javaindex 48b780f9f2..79b7d1267b 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointSpec.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetBreakpointSpec.java@@ -36,8 +36,7 @@ import ghidra.util.datastruct.WeakValueHashMap;@TargetObjectSchemaInfo(name = "BreakpointSpec",attributes = {- @TargetAttributeType(type = Void.class)- },+ @TargetAttributeType(type = Void.class) },canonicalContainer = true)public class GdbModelTargetBreakpointSpec extendsDefaultTargetObject<GdbModelTargetBreakpointLocation, GdbModelTargetBreakpointContainer>@@ -74,12 +73,10 @@ public class GdbModelTargetBreakpointSpec extendssuper(breakpoints.impl, breakpoints, keyBreakpoint(info), "BreakpointSpec");this.impl = breakpoints.impl;this.number = info.getNumber();-this.info = info;+ impl.addModelObject(info, this);- changeAttributes(List.of(), Map.of(- CONTAINER_ATTRIBUTE_NAME, breakpoints),- "Initialized");+ changeAttributes(List.of(), Map.of(CONTAINER_ATTRIBUTE_NAME, breakpoints), "Initialized");}protected CompletableFuture<Void> init() {@@ -177,8 +174,8 @@ public class GdbModelTargetBreakpointSpec extendsENABLED_ATTRIBUTE_NAME, enabled = info.isEnabled(),EXPRESSION_ATTRIBUTE_NAME, expression = info.getOriginalLocation(),KINDS_ATTRIBUTE_NAME, kinds = computeKinds(info),- DISPLAY_ATTRIBUTE_NAME, updateDisplay() //- ), reason);+ DISPLAY_ATTRIBUTE_NAME, updateDisplay()),+ reason);}protected synchronized List<GdbModelTargetBreakpointLocation> setInfoAndComputeLocations(@@ -197,8 +194,7 @@ public class GdbModelTargetBreakpointSpec extendsprotected CompletableFuture<Void> updateBktpInfo(GdbBreakpointInfo oldInfo,GdbBreakpointInfo newInfo, String reason) {- List<GdbModelTargetBreakpointLocation> locs =- setInfoAndComputeLocations(oldInfo, newInfo);+ List<GdbModelTargetBreakpointLocation> locs = setInfoAndComputeLocations(oldInfo, newInfo);updateAttributesFromInfo(reason);setElements(locs, reason);return AsyncUtils.NIL;@@ -206,8 +202,7 @@ public class GdbModelTargetBreakpointSpec extendsprotected CompletableFuture<Void> updateWptInfo(GdbBreakpointInfo oldInfo,GdbBreakpointInfo newInfo, String reason) {- List<GdbModelTargetBreakpointLocation> locs =- setInfoAndComputeLocations(oldInfo, newInfo);+ List<GdbModelTargetBreakpointLocation> locs = setInfoAndComputeLocations(oldInfo, newInfo);updateAttributesFromInfo(reason);assert locs.size() == 1;return locs.get(0).initWpt().thenAccept(__ -> {diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferior.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferior.javaindex f90daf4c89..5b994469dc 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferior.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferior.java@@ -31,8 +31,11 @@ import ghidra.dbg.target.schema.*;import ghidra.dbg.util.PathUtils;import ghidra.lifecycle.Internal;-@TargetObjectSchemaInfo(name = "Inferior", elements = {- @TargetElementType(type = Void.class) }, attributes = {+@TargetObjectSchemaInfo(+ name = "Inferior",+ elements = {+ @TargetElementType(type = Void.class) },+ attributes = {@TargetAttributeType(type = Void.class) })public class GdbModelTargetInferiorextends DefaultTargetObject<TargetObject, GdbModelTargetInferiorContainer>@@ -81,6 +84,8 @@ public class GdbModelTargetInferiorsuper(inferiors.impl, inferiors, keyInferior(inferior), "Inferior");this.impl = inferiors.impl;this.inferior = inferior;+ impl.addModelObject(inferior, this);+ impl.addModelObject(inferior.getId(), this);this.environment = new GdbModelTargetEnvironment(this);this.memory = new GdbModelTargetProcessMemory(this);@@ -135,7 +140,10 @@ public class GdbModelTargetInferiorreturn threads;}- @TargetAttributeType(name = GdbModelTargetBreakpointLocationContainer.NAME, required = true, fixed = true)+ @TargetAttributeType(+ name = GdbModelTargetBreakpointLocationContainer.NAME,+ required = true,+ fixed = true)public GdbModelTargetBreakpointLocationContainer getBreakpoints() {return breakpoints;}@@ -231,16 +239,16 @@ public class GdbModelTargetInferiorp = inferior.getPid();}if (p == null) {- changeAttributes(List.of(),- Map.ofEntries(Map.entry(STATE_ATTRIBUTE_NAME, state = realState),- Map.entry(DISPLAY_ATTRIBUTE_NAME, updateDisplay())),+ changeAttributes(List.of(), Map.ofEntries(+ Map.entry(STATE_ATTRIBUTE_NAME, state = realState),+ Map.entry(DISPLAY_ATTRIBUTE_NAME, updateDisplay())),"Refresh on started");}else {- changeAttributes(List.of(),- Map.ofEntries(Map.entry(STATE_ATTRIBUTE_NAME, state = realState),- Map.entry(PID_ATTRIBUTE_NAME, p),- Map.entry(DISPLAY_ATTRIBUTE_NAME, updateDisplay())),+ changeAttributes(List.of(), Map.ofEntries(+ Map.entry(STATE_ATTRIBUTE_NAME, state = realState),+ Map.entry(PID_ATTRIBUTE_NAME, p),+ Map.entry(DISPLAY_ATTRIBUTE_NAME, updateDisplay())),"Refresh on started");}});diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferiorContainer.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferiorContainer.javaindex 56a728b267..00bb4c1687 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferiorContainer.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetInferiorContainer.java@@ -25,9 +25,9 @@ import agent.gdb.manager.impl.cmd.GdbStateChangeRecord;import ghidra.async.AsyncUtils;import ghidra.dbg.agent.DefaultTargetObject;import ghidra.dbg.target.TargetEventScope.TargetEventType;+import ghidra.dbg.target.TargetObject;import ghidra.dbg.target.schema.TargetAttributeType;import ghidra.dbg.target.schema.TargetObjectSchemaInfo;-import ghidra.util.datastruct.WeakValueHashMap;@TargetObjectSchemaInfo(name = "InferiorContainer",@@ -41,8 +41,6 @@ public class GdbModelTargetInferiorContainerprotected final GdbModelImpl impl;- protected final Map<Integer, GdbModelTargetInferior> inferiorsById = new WeakValueHashMap<>();-public GdbModelTargetInferiorContainer(GdbModelTargetSession session) {super(session.impl, session, NAME, "InferiorContainer");this.impl = session.impl;@@ -76,7 +74,7 @@ public class GdbModelTargetInferiorContainer@Overridepublic void inferiorRemoved(int inferiorId, GdbCause cause) {synchronized (this) {- inferiorsById.remove(inferiorId);+ impl.deleteModelObject(inferiorId);}changeElements(List.of(GdbModelTargetInferior.indexInferior(inferiorId)), List.of(),"Removed");@@ -93,7 +91,8 @@ public class GdbModelTargetInferiorContainer@Overridepublic void threadExited(int threadId, GdbInferior inf, GdbCause cause) {GdbModelTargetInferior inferior = getTargetInferior(inf);- GdbModelTargetThread targetThread = inferior.threads.getTargetThreadIfPresent(threadId);+ GdbThread thread = inf.getKnownThreads().get(threadId);+ GdbModelTargetThread targetThread = inferior.threads.getTargetThreadIfPresent(thread);parent.getListeners().fire.event(parent, targetThread, TargetEventType.THREAD_EXITED,"Thread " + threadId + " exited", List.of(targetThread));inferior.threads.threadExited(threadId);@@ -144,25 +143,25 @@ public class GdbModelTargetInferiorContainer// Cache should be kept in sync all the time, anywaypublic synchronized GdbModelTargetInferior getTargetInferior(int id) {- return inferiorsById.computeIfAbsent(id,- i -> new GdbModelTargetInferior(this, impl.gdb.getKnownInferiors().get(id)));+ TargetObject modelObject = impl.getModelObject(id);+ if (modelObject != null) {+ return (GdbModelTargetInferior) modelObject;+ }+ return new GdbModelTargetInferior(this, impl.gdb.getKnownInferiors().get(id));}public synchronized GdbModelTargetInferior getTargetInferior(GdbInferior inferior) {- GdbModelTargetInferior modelInferior = inferiorsById.get(inferior.getId());- if (modelInferior != null) {- modelInferior.updateDisplayAttribute();- }- else {- modelInferior = new GdbModelTargetInferior(this, inferior);- inferiorsById.put(inferior.getId(), modelInferior);+ TargetObject modelObject = impl.getModelObject(inferior);+ if (modelObject != null) {+ return (GdbModelTargetInferior) modelObject;}- return modelInferior;+ return new GdbModelTargetInferior(this, inferior);}protected void invalidateMemoryAndRegisterCaches() {- for (GdbModelTargetInferior inf : inferiorsById.values()) {- inf.invalidateMemoryAndRegisterCaches();+ for (GdbInferior inf : impl.gdb.getKnownInferiors().values()) {+ GdbModelTargetInferior targetInf = (GdbModelTargetInferior) impl.getModelObject(inf);+ targetInf.invalidateMemoryAndRegisterCaches();}}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetMemoryRegion.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetMemoryRegion.javaindex f3b13b89c5..348f8d917e 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetMemoryRegion.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetMemoryRegion.java@@ -66,6 +66,7 @@ public class GdbModelTargetMemoryRegionpublic GdbModelTargetMemoryRegion(GdbModelTargetProcessMemory memory,GdbMemoryMapping mapping) {super(memory.impl, memory, keyRegion(mapping), "MemoryRegion");+ memory.impl.addModelObject(mapping, this);try {Address min = memory.impl.getAddressFactory().getDefaultAddressSpace()@@ -75,7 +76,8 @@ public class GdbModelTargetMemoryRegioncatch (AddressFormatException | AddressOverflowException e) {throw new AssertionError(e);}- changeAttributes(List.of(), Map.of(MEMORY_ATTRIBUTE_NAME, memory, //+ changeAttributes(List.of(), Map.of( //+ MEMORY_ATTRIBUTE_NAME, memory, //RANGE_ATTRIBUTE_NAME, range, //READABLE_ATTRIBUTE_NAME, isReadable(), //WRITABLE_ATTRIBUTE_NAME, isWritable(), //@@ -133,7 +135,11 @@ public class GdbModelTargetMemoryRegionreturn objfile;}- @TargetAttributeType(name = OFFSET_ATTRIBUTE_NAME, required = true, fixed = true, hidden = true)+ @TargetAttributeType(+ name = OFFSET_ATTRIBUTE_NAME,+ required = true,+ fixed = true,+ hidden = true)public long getOffset() {return offset;}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModule.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModule.javaindex 5388ed508c..58dd958d71 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModule.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModule.java@@ -33,12 +33,11 @@ import ghidra.program.model.address.*;elements = { //@TargetElementType(type = Void.class) //},- attributes = { //+ attributes = { //@TargetAttributeType(type = Void.class) //})-public class GdbModelTargetModule- extends DefaultTargetObject<TargetObject, GdbModelTargetModuleContainer>- implements TargetModule {+public class GdbModelTargetModule extends+ DefaultTargetObject<TargetObject, GdbModelTargetModuleContainer> implements TargetModule {public static final String VISIBLE_RANGE_ATTRIBUTE_NAME = "range";public static final String VISIBLE_MODULE_NAME_ATTRIBUTE_NAME = "module name";@@ -66,6 +65,8 @@ public class GdbModelTargetModulethis.impl = modules.impl;this.inferior = modules.inferior;this.module = module;+ impl.addModelObject(module, this);+ impl.addModelObject(module.getName(), this);this.sections = new GdbModelTargetSectionContainer(this);this.symbols = new GdbModelTargetSymbolContainer(this);diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModuleContainer.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModuleContainer.javaindex e816fdecd9..faad546fb0 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModuleContainer.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetModuleContainer.java@@ -24,8 +24,7 @@ import agent.gdb.manager.GdbModule;import ghidra.async.AsyncFence;import ghidra.dbg.agent.DefaultTargetObject;import ghidra.dbg.error.DebuggerUserException;-import ghidra.dbg.target.TargetModule;-import ghidra.dbg.target.TargetModuleContainer;+import ghidra.dbg.target.*;import ghidra.dbg.target.schema.TargetAttributeType;import ghidra.dbg.target.schema.TargetObjectSchema.ResyncMode;import ghidra.dbg.target.schema.TargetObjectSchemaInfo;@@ -34,9 +33,7 @@ import ghidra.lifecycle.Internal;@TargetObjectSchemaInfo(name = "ModuleContainer",elementResync = ResyncMode.ONCE, // TODO: Should this be NEVER?- attributes = {- @TargetAttributeType(type = Void.class)- },+ attributes = { @TargetAttributeType(type = Void.class) },canonicalContainer = true)public class GdbModelTargetModuleContainerextends DefaultTargetObject<GdbModelTargetModule, GdbModelTargetInferior>@@ -47,9 +44,6 @@ public class GdbModelTargetModuleContainerprotected final GdbModelImpl impl;protected final GdbInferior inferior;- // TODO: Is it possible to load the same object twice?- protected final Map<String, GdbModelTargetModule> modulesByName = new HashMap<>();-public GdbModelTargetModuleContainer(GdbModelTargetInferior inferior) {super(inferior.impl, inferior, NAME, "ModuleContainer");this.impl = inferior.impl;@@ -67,7 +61,7 @@ public class GdbModelTargetModuleContainer@Internalpublic void libraryUnloaded(String name) {synchronized (this) {- modulesByName.remove(name);+ impl.deleteModelObject(name);}changeElements(List.of(name), List.of(), "Unloaded");}@@ -85,10 +79,8 @@ public class GdbModelTargetModuleContainerprotected CompletableFuture<Void> updateUsingModules(Map<String, GdbModule> byName) {List<GdbModelTargetModule> modules;synchronized (this) {- modules = byName.values()- .stream()- .map(this::getTargetModule)- .collect(Collectors.toList());+ modules =+ byName.values().stream().map(this::getTargetModule).collect(Collectors.toList());}AsyncFence fence = new AsyncFence();for (GdbModelTargetModule mod : modules) {@@ -100,9 +92,8 @@ public class GdbModelTargetModuleContainer* modules, since any additions should cause a follow-on update.*/return fence.ready().thenAccept(__ -> {- List<GdbModelTargetModule> validOnly = modules.stream()- .filter(m -> m.isValid())- .collect(Collectors.toList());+ List<GdbModelTargetModule> validOnly =+ modules.stream().filter(m -> m.isValid()).collect(Collectors.toList());changeElements(List.of(), validOnly, "Refreshed");});}@@ -115,18 +106,25 @@ public class GdbModelTargetModuleContainerprotected CompletableFuture<Void> doRefresh() {return inferior.listModules().thenCompose(byName -> {- modulesByName.keySet().retainAll(byName.keySet());+ for (String modName : inferior.getKnownModules().keySet()) {+ if (!byName.keySet().contains(modName)) {+ impl.deleteModelObject(byName.get(modName));+ }+ }return updateUsingModules(byName);});}protected synchronized GdbModelTargetModule getTargetModule(GdbModule module) {- return modulesByName.computeIfAbsent(module.getName(),- n -> new GdbModelTargetModule(this, module));+ TargetObject modelObject = impl.getModelObject(module);+ if (modelObject != null) {+ return (GdbModelTargetModule) modelObject;+ }+ return new GdbModelTargetModule(this, module);}public synchronized GdbModelTargetModule getTargetModuleIfPresent(String name) {- return modulesByName.get(name);+ return (GdbModelTargetModule) impl.getModelObject(name);}public CompletableFuture<?> refreshInternal() {diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetRegister.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetRegister.javaindex 3004012c33..f445d20bac 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetRegister.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetRegister.java@@ -54,6 +54,7 @@ public class GdbModelTargetRegistersuper(registers.impl, registers, keyRegister(register), "Register");this.impl = registers.impl;this.register = register;+ impl.addModelObject(register, this);this.bitLength = register.getSize() * 8;diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSection.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSection.javaindex 53652c74b0..fcfae1180b 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSection.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSection.java@@ -29,14 +29,11 @@ import ghidra.program.model.address.*;@TargetObjectSchemaInfo(name = "Section",elements = {- @TargetElementType(type = Void.class)- },+ @TargetElementType(type = Void.class) },attributes = {- @TargetAttributeType(type = Void.class)- })-public class GdbModelTargetSection- extends DefaultTargetObject<TargetObject, GdbModelTargetSectionContainer>- implements TargetSection {+ @TargetAttributeType(type = Void.class) })+public class GdbModelTargetSection extends+ DefaultTargetObject<TargetObject, GdbModelTargetSectionContainer> implements TargetSection {public static final String VISIBLE_RANGE_ATTRIBUTE_NAME = "range";@@ -59,6 +56,7 @@ public class GdbModelTargetSectionsuper(sections.impl, sections, keySection(section), "Section");this.impl = sections.impl;this.section = section;+ impl.addModelObject(section, this);this.module = module;this.range = doGetRange();diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSectionContainer.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSectionContainer.javaindex 1eab6e72df..6f81d384e4 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSectionContainer.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSectionContainer.java@@ -22,13 +22,16 @@ import java.util.stream.Collectors;import agent.gdb.manager.GdbModuleSection;import ghidra.dbg.agent.DefaultTargetObject;+import ghidra.dbg.target.TargetObject;import ghidra.dbg.target.TargetSectionContainer;import ghidra.dbg.target.schema.TargetAttributeType;import ghidra.dbg.target.schema.TargetObjectSchemaInfo;-import ghidra.util.datastruct.WeakValueHashMap;-@TargetObjectSchemaInfo(name = "SectionContainer", attributes = {- @TargetAttributeType(type = Void.class) }, canonicalContainer = true)+@TargetObjectSchemaInfo(+ name = "SectionContainer",+ attributes = {+ @TargetAttributeType(type = Void.class) },+ canonicalContainer = true)public class GdbModelTargetSectionContainerextends DefaultTargetObject<GdbModelTargetSection, GdbModelTargetModule>implements TargetSectionContainer {@@ -37,8 +40,6 @@ public class GdbModelTargetSectionContainerprotected final GdbModelImpl impl;protected final GdbModelTargetModule module;- protected final Map<String, GdbModelTargetSection> sectionsByName = new WeakValueHashMap<>();-public GdbModelTargetSectionContainer(GdbModelTargetModule module) {super(module.impl, module, NAME, "SectionContainer");this.impl = module.impl;@@ -65,13 +66,11 @@ public class GdbModelTargetSectionContainerreturn module.module.listSections().thenAccept(this::updateUsingSections);}- protected synchronized GdbModelTargetSection getTargetSection(String name) {- return sectionsByName.computeIfAbsent(name, n -> new GdbModelTargetSection(this, module,- module.module.getKnownSections().get(name)));- }-protected synchronized GdbModelTargetSection getTargetSection(GdbModuleSection section) {- return sectionsByName.computeIfAbsent(section.getName(),- n -> new GdbModelTargetSection(this, module, section));+ TargetObject modelObject = impl.getModelObject(section);+ if (modelObject != null) {+ return (GdbModelTargetSection) modelObject;+ }+ return new GdbModelTargetSection(this, module, section);}}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSession.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSession.javaindex 3c38b0f07e..37b3f6e957 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSession.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSession.java@@ -34,8 +34,11 @@ import ghidra.dbg.target.schema.*;import ghidra.dbg.util.PathUtils;import ghidra.util.Msg;-@TargetObjectSchemaInfo(name = "Session", elements = {- @TargetElementType(type = Void.class) }, attributes = {+@TargetObjectSchemaInfo(+ name = "Session",+ elements = {+ @TargetElementType(type = Void.class) },+ attributes = {@TargetAttributeType(type = Void.class) })public class GdbModelTargetSession extends DefaultTargetModelRootimplements TargetAccessConditioned, TargetAttacher, TargetInterpreter, TargetInterruptible,@@ -58,6 +61,7 @@ public class GdbModelTargetSession extends DefaultTargetModelRootpublic GdbModelTargetSession(GdbModelImpl impl, TargetObjectSchema schema) {super(impl, "Session", schema);this.impl = impl;+ //impl.addModelObject(session, this);this.inferiors = new GdbModelTargetInferiorContainer(this);this.available = new GdbModelTargetAvailableContainer(this);@@ -85,12 +89,18 @@ public class GdbModelTargetSession extends DefaultTargetModelRootreturn inferiors;}- @TargetAttributeType(name = GdbModelTargetAvailableContainer.NAME, required = true, fixed = true)+ @TargetAttributeType(+ name = GdbModelTargetAvailableContainer.NAME,+ required = true,+ fixed = true)public GdbModelTargetAvailableContainer getAvailable() {return available;}- @TargetAttributeType(name = GdbModelTargetBreakpointContainer.NAME, required = true, fixed = true)+ @TargetAttributeType(+ name = GdbModelTargetBreakpointContainer.NAME,+ required = true,+ fixed = true)public GdbModelTargetBreakpointContainer getBreakpoints() {return breakpoints;}@@ -322,14 +332,9 @@ public class GdbModelTargetSession extends DefaultTargetModelRoot@Overridepublic void threadStateChanged(GdbThread thread, GdbState state, GdbCause cause,GdbReason reason) {-- /* REQUIRES GP-762TargetThread targetThread = (TargetThread) impl.getModelObject(thread);changeAttributes(List.of(), List.of(), Map.of( //TargetEventScope.EVENT_OBJECT_ATTRIBUTE_NAME, targetThread //), reason.desc());- */-}-}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrame.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrame.javaindex 2c9d09f05e..79c4a02675 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrame.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrame.java@@ -32,11 +32,9 @@ import ghidra.program.model.address.Address;@TargetObjectSchemaInfo(name = "StackFrame",elements = {- @TargetElementType(type = Void.class)- },+ @TargetElementType(type = Void.class) },attributes = {- @TargetAttributeType(type = Void.class)- })+ @TargetAttributeType(type = Void.class) })public class GdbModelTargetStackFrame extends DefaultTargetObject<TargetObject, GdbModelTargetStack>implements TargetStackFrame, GdbModelSelectableObject {public static final String FUNC_ATTRIBUTE_NAME = PREFIX_INVISIBLE + "function";@@ -73,11 +71,13 @@ public class GdbModelTargetStackFrame extends DefaultTargetObject<TargetObject,this.impl = stack.impl;this.thread = thread;this.inferior = inferior;+ impl.addModelObject(frame, this);this.registers = new GdbModelTargetStackFrameRegisterContainer(this);changeAttributes(List.of(), List.of(registers), Map.of( //- DISPLAY_ATTRIBUTE_NAME, display = computeDisplay(frame)), "Initialized");+ DISPLAY_ATTRIBUTE_NAME, display = computeDisplay(frame)),+ "Initialized");setFrame(frame);}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrameRegister.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrameRegister.javaindex bceda23751..33add4b14a 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrameRegister.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetStackFrameRegister.java@@ -26,8 +26,11 @@ import ghidra.dbg.target.TargetRegister;import ghidra.dbg.target.schema.*;import ghidra.dbg.util.PathUtils;-@TargetObjectSchemaInfo(name = "RegisterValue", elements = {- @TargetElementType(type = Void.class) }, attributes = {+@TargetObjectSchemaInfo(+ name = "RegisterValue",+ elements = {+ @TargetElementType(type = Void.class) },+ attributes = {@TargetAttributeType(type = Void.class) })public class GdbModelTargetStackFrameRegisterextends DefaultTargetObject<TargetObject, GdbModelTargetStackFrameRegisterContainer>@@ -55,6 +58,7 @@ public class GdbModelTargetStackFrameRegistersuper(registers.impl, registers, keyRegister(register), "Register");this.impl = registers.impl;this.register = register;+ impl.addModelObject(register, this);this.bitLength = register.getSize() * 8;diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbol.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbol.javaindex 879e8d01a4..99be18e1d6 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbol.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbol.java@@ -29,14 +29,11 @@ import ghidra.program.model.address.Address;@TargetObjectSchemaInfo(name = "Symbol",elements = {- @TargetElementType(type = Void.class)- },+ @TargetElementType(type = Void.class) },attributes = {- @TargetAttributeType(type = Void.class)- })-public class GdbModelTargetSymbol- extends DefaultTargetObject<TargetObject, GdbModelTargetSymbolContainer>- implements TargetSymbol {+ @TargetAttributeType(type = Void.class) })+public class GdbModelTargetSymbol extends+ DefaultTargetObject<TargetObject, GdbModelTargetSymbolContainer> implements TargetSymbol {protected static String indexSymbol(GdbMinimalSymbol symbol) {return symbol.getName();}@@ -51,6 +48,7 @@ public class GdbModelTargetSymbolpublic GdbModelTargetSymbol(GdbModelTargetSymbolContainer symbols, GdbMinimalSymbol symbol) {super(symbols.impl, symbols, keySymbol(symbol), "Symbol");+ symbols.impl.addModelObject(symbol, this);this.constant = false;this.value = symbols.impl.space.getAddress(symbol.getAddress());this.size = 0;diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbolContainer.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbolContainer.javaindex 8de3610ce4..0e4465b0af 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbolContainer.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetSymbolContainer.java@@ -16,24 +16,22 @@package agent.gdb.model.impl;import java.util.List;-import java.util.Map;import java.util.concurrent.CompletableFuture;import java.util.stream.Collectors;import agent.gdb.manager.impl.GdbMinimalSymbol;import ghidra.dbg.agent.DefaultTargetObject;+import ghidra.dbg.target.TargetObject;import ghidra.dbg.target.TargetSymbolNamespace;import ghidra.dbg.target.schema.TargetAttributeType;import ghidra.dbg.target.schema.TargetObjectSchema.ResyncMode;import ghidra.dbg.target.schema.TargetObjectSchemaInfo;-import ghidra.util.datastruct.WeakValueHashMap;@TargetObjectSchemaInfo(name = "SymbolContainer",elementResync = ResyncMode.ONCE,attributes = {- @TargetAttributeType(type = Void.class)- },+ @TargetAttributeType(type = Void.class) },canonicalContainer = true)public class GdbModelTargetSymbolContainerextends DefaultTargetObject<GdbModelTargetSymbol, GdbModelTargetModule>@@ -43,8 +41,6 @@ public class GdbModelTargetSymbolContainerprotected final GdbModelImpl impl;protected final GdbModelTargetModule module;- protected final Map<String, GdbModelTargetSymbol> symbolsByName = new WeakValueHashMap<>();-public GdbModelTargetSymbolContainer(GdbModelTargetModule module) {super(module.impl, module, NAME, "SymbolContainer");this.impl = module.impl;@@ -66,7 +62,10 @@ public class GdbModelTargetSymbolContainer}protected synchronized GdbModelTargetSymbol getTargetSymbol(GdbMinimalSymbol symbol) {- return symbolsByName.computeIfAbsent(symbol.getName(),- n -> new GdbModelTargetSymbol(this, symbol));+ TargetObject modelObject = impl.getModelObject(symbol);+ if (modelObject != null) {+ return (GdbModelTargetSymbol) modelObject;+ }+ return new GdbModelTargetSymbol(this, symbol);}}diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThread.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThread.javaindex 0ea66a88ed..e5406236a1 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThread.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThread.java@@ -35,8 +35,10 @@ import ghidra.util.Msg;@TargetObjectSchemaInfo(name = "Thread",- elements = { @TargetElementType(type = Void.class) },- attributes = { @TargetAttributeType(type = Void.class) })+ elements = {+ @TargetElementType(type = Void.class) },+ attributes = {+ @TargetAttributeType(type = Void.class) })public class GdbModelTargetThreadextends DefaultTargetObject<TargetObject, GdbModelTargetThreadContainer> implementsTargetThread, TargetExecutionStateful, TargetSteppable, GdbModelSelectableObject {@@ -77,6 +79,7 @@ public class GdbModelTargetThreadthis.impl = threads.impl;this.inferior = inferior.inferior;this.thread = thread;+ impl.addModelObject(thread, this);this.stack = new GdbModelTargetStack(this, inferior);@@ -232,9 +235,7 @@ public class GdbModelTargetThreadCompletableFuture<Void> result = AsyncUtils.NIL;if (state == GdbState.STOPPED) {Msg.debug(this, "Updating stack for " + this);- result = CompletableFuture.allOf(- updateInfo(),- stack.stateChanged(sco));+ result = CompletableFuture.allOf(updateInfo(), stack.stateChanged(sco));}TargetExecutionState targetState = convertState(state);changeAttributes(List.of(), Map.of( //diff --git a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThreadContainer.java b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThreadContainer.javaindex 92dab00b2f..7d31ee506e 100644--- a/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThreadContainer.java+++ b/Ghidra/Debug/Debugger-agent-gdb/src/main/java/agent/gdb/model/impl/GdbModelTargetThreadContainer.java@@ -26,10 +26,10 @@ import agent.gdb.manager.reason.GdbBreakpointHitReason;import ghidra.async.AsyncFence;import ghidra.async.AsyncUtils;import ghidra.dbg.agent.DefaultTargetObject;+import ghidra.dbg.target.TargetObject;import ghidra.dbg.target.schema.TargetAttributeType;import ghidra.dbg.target.schema.TargetObjectSchemaInfo;import ghidra.util.Msg;-import ghidra.util.datastruct.WeakValueHashMap;@TargetObjectSchemaInfo(name = "ThreadContainer",@@ -43,8 +43,6 @@ public class GdbModelTargetThreadContainerprotected final GdbModelImpl impl;protected final GdbInferior inferior;- protected final Map<Integer, GdbModelTargetThread> threadsById = new WeakValueHashMap<>();-public GdbModelTargetThreadContainer(GdbModelTargetInferior inferior) {super(inferior.impl, inferior, NAME, "ThreadContainer");this.impl = inferior.impl;@@ -60,7 +58,7 @@ public class GdbModelTargetThreadContainerpublic void threadExited(int threadId) {synchronized (this) {- threadsById.remove(threadId);+ impl.deleteModelObject(threadId);}changeElements(List.of(GdbModelTargetThread.indexThread(threadId)), List.of(), "Exited");}@@ -81,24 +79,32 @@ public class GdbModelTargetThreadContainerreturn AsyncUtils.NIL;}return inferior.listThreads().thenAccept(byTID -> {- threadsById.keySet().retainAll(byTID.keySet());+ for (Integer tid : inferior.getKnownThreads().keySet()) {+ if (!byTID.keySet().contains(tid)) {+ impl.deleteModelObject(byTID.get(tid));+ }+ }updateUsingThreads(byTID);});}public synchronized GdbModelTargetThread getTargetThread(GdbThread thread) {assert thread.getInferior() == inferior;- return threadsById.computeIfAbsent(thread.getId(),- i -> new GdbModelTargetThread(this, parent, thread));+ TargetObject modelObject = impl.getModelObject(thread);+ if (modelObject != null) {+ return (GdbModelTargetThread) modelObject;+ }+ return new GdbModelTargetThread(this, parent, thread);}- public synchronized GdbModelTargetThread getTargetThreadIfPresent(int threadId) {- return threadsById.get(threadId);+ public synchronized GdbModelTargetThread getTargetThreadIfPresent(GdbThread thread) {+ return (GdbModelTargetThread) impl.getModelObject(thread);}protected void invalidateRegisterCaches() {- for (GdbModelTargetThread thread : threadsById.values()) {- thread.invalidateRegisterCaches();+ for (GdbThread thread : inferior.getKnownThreads().values()) {+ GdbModelTargetThread targetThread = (GdbModelTargetThread) impl.getModelObject(thread);+ targetThread.invalidateRegisterCaches();}}@@ -114,8 +120,10 @@ public class GdbModelTargetThreadContainer}return requestElements(false).thenCompose(__ -> {AsyncFence fence = new AsyncFence();- for (GdbModelTargetThread modelThread : threadsById.values()) {- fence.include(modelThread.stateChanged(sco));+ for (GdbThread thread : inferior.getKnownThreads().values()) {+ GdbModelTargetThread targetThread =+ (GdbModelTargetThread) impl.getModelObject(thread);+ fence.include(targetThread.stateChanged(sco));}return fence.ready();}).exceptionally(__ -> {