Ghidra is a software reverse engineering (SRE) framework
GP-1232: Added 'Map Identically' action to Modules provider.
Danc7815b582905
diff --git a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerModulesPlugin/DebuggerModulesPlugin.html b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerModulesPlugin/DebuggerModulesPlugin.htmlindex dc7ae1517f..cc413e410c 100644--- a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerModulesPlugin/DebuggerModulesPlugin.html+++ b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerModulesPlugin/DebuggerModulesPlugin.html@@ -96,6 +96,15 @@selected modules to Ghidra data types in the trace. These are often the same types that Ghidraimports from the static image, anyway.</P>+ <H3><A name="map_identically"></A>Map Identically</H3>++ <P>+ This action is available when both a trace and a program are opened. It maps the current trace+ to the current program using identical addresses. This action ignores the module list. It is a suitable mapping when the current+ program is loaded in the trace <EM>without relocation</EM>. It is also a fallback worth trying+ in the absence of a module list.+ </P>+<H3><A name="map_modules"></A>Map Modules</H3><P>This action is available from the modules' or sections' pop-up menu. It searches the tool'sdiff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/DebuggerResources.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/DebuggerResources.javaindex 1a3661221c..a6bf9b40b3 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/DebuggerResources.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/DebuggerResources.java@@ -146,6 +146,7 @@ public interface DebuggerResources {//ResourceManager.loadImage("images/capture-memory.png");// TODO: Draw an icon+ ImageIcon ICON_MAP_IDENTICALLY = ResourceManager.loadImage("images/doubleArrow.png");ImageIcon ICON_MAP_MODULES = ResourceManager.loadImage("images/modules.png");ImageIcon ICON_MAP_SECTIONS = ICON_MAP_MODULES; // TODOImageIcon ICON_BLOCK = ICON_MAP_SECTIONS; // TODO@@ -1209,6 +1210,23 @@ public interface DebuggerResources {}}+ interface MapIdenticallyAction {+ String NAME = "Map Identically";+ String DESCRIPTION =+ "Map the current trace to the current program using identical addresses";+ Icon ICON = ICON_MAP_IDENTICALLY;+ String GROUP = GROUP_MAPPING;+ String HELP_ANCHOR = "map_identically";++ static ActionBuilder builder(Plugin owner) {+ String ownerName = owner.getName();+ return new ActionBuilder(NAME, ownerName).description(DESCRIPTION)+ .toolBarIcon(ICON)+ .toolBarGroup(GROUP)+ .helpLocation(new HelpLocation(ownerName, HELP_ANCHOR));+ }+ }+interface MapModulesAction {String NAME = "Map Modules";String DESCRIPTION = "Map selected modules to program images";diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProvider.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProvider.javaindex c647351cd5..1fe24fe85f 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProvider.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProvider.java@@ -539,6 +539,7 @@ public class DebuggerModulesProvider extends ComponentProviderAdapter {private Program currentProgram;private ProgramLocation currentLocation;+ DockingAction actionMapIdentically;DockingAction actionMapModules;DockingAction actionMapModuleTo;DockingAction actionMapSections;@@ -747,6 +748,10 @@ public class DebuggerModulesProvider extends ComponentProviderAdapter {}protected void createActions() {+ actionMapIdentically = MapIdenticallyAction.builder(plugin)+ .enabledWhen(ctx -> currentProgram != null && currentTrace != null)+ .onAction(this::activatedMapIdentically)+ .buildAndInstallLocal(this);actionMapModules = MapModulesAction.builder(plugin).enabledWhen(this::isContextNonEmpty).popupWhen(this::isContextNonEmpty)@@ -822,6 +827,14 @@ public class DebuggerModulesProvider extends ComponentProviderAdapter {return sel.stream().map(TraceSection::getModule).distinct().count() == 1;}+ private void activatedMapIdentically(ActionContext ignored) {+ if (currentProgram == null || currentTrace == null) {+ return;+ }+ staticMappingService.addIdentityMapping(currentTrace, currentProgram,+ Range.atLeast(traceManager.getCurrentSnap()), true);+ }+private void activatedMapModules(ActionContext ignored) {Set<TraceModule> sel = getSelectedModules(myActionContext);if (sel == null || sel.isEmpty()) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.javaindex 3f695ff998..f217dc8f92 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java@@ -980,6 +980,69 @@ public class DebuggerStaticMappingServicePlugin extends Pluginmanager.add(range, fromLifespan, toURL, toAddress.toString(true));}+ static protected AddressRange clippedRange(Trace trace, String spaceName, long min,+ long max) {+ AddressSpace space = trace.getBaseAddressFactory().getAddressSpace(spaceName);+ if (space == null) {+ return null;+ }+ Address spaceMax = space.getMaxAddress();+ if (Long.compareUnsigned(min, spaceMax.getOffset()) > 0) {+ return null;+ }+ if (Long.compareUnsigned(max, spaceMax.getOffset()) > 0) {+ return new AddressRangeImpl(space.getAddress(min), spaceMax);+ }+ return new AddressRangeImpl(space.getAddress(min), space.getAddress(max));+ }++ @Override+ public void addIdentityMapping(Trace from, Program toProgram, Range<Long> lifespan,+ boolean truncateExisting) {+ try (UndoableTransaction tid =+ UndoableTransaction.start(from, "Add identity mappings", false)) {+ doAddIdentityMapping(from, toProgram, lifespan, truncateExisting);+ tid.commit();+ }+ }++ protected void doAddIdentityMapping(Trace from, Program toProgram, Range<Long> lifespan,+ boolean truncateExisting) {+ Map<String, Address> mins = new HashMap<>();+ Map<String, Address> maxs = new HashMap<>();+ for (AddressRange range : toProgram.getMemory().getAddressRanges()) {+ mins.compute(range.getAddressSpace().getName(), (n, min) -> {+ Address can = range.getMinAddress();+ if (min == null || can.compareTo(min) < 0) {+ return can;+ }+ return min;+ });+ maxs.compute(range.getAddressSpace().getName(), (n, max) -> {+ Address can = range.getMaxAddress();+ if (max == null || can.compareTo(max) > 0) {+ return can;+ }+ return max;+ });+ }+ for (String name : mins.keySet()) {+ AddressRange range = clippedRange(from, name, mins.get(name).getOffset(),+ maxs.get(name).getOffset());+ if (range == null) {+ continue;+ }+ try {+ addMapping(new DefaultTraceLocation(from, null, lifespan, range.getMinAddress()),+ new ProgramLocation(toProgram, mins.get(name)), range.getLength(),+ truncateExisting);+ }+ catch (TraceConflictedMappingException e) {+ Msg.error(this, "Could not add identity mapping " + range + ": " + e.getMessage());+ }+ }+ }+@Overridepublic void addModuleMapping(TraceModule from, long length, Program toProgram,boolean truncateExisting) throws TraceConflictedMappingException {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.javaindex d0b0739975..62d15a4fa8 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java@@ -532,7 +532,6 @@ public interface DebuggerStaticMappingService {* Note if the trace is backed by a Ghidra database, the caller must already have started a* transaction on the relevant domain object.*- ** @param from the source trace location, including lifespan* @param to the destination program location* @param length the length of the mapped region, where 0 indicates {@code 1 << 64}.@@ -543,6 +542,18 @@ public interface DebuggerStaticMappingService {void addMapping(TraceLocation from, ProgramLocation to, long length, boolean truncateExisting)throws TraceConflictedMappingException;+ /**+ * Add a static mapping from the given trace to the given program, using identical addresses+ *+ * @param from the source trace+ * @param toProgram the destination program+ * @param lifespan the lifespan of the mapping+ * @param truncateExisting true to delete or truncate the lifespan of overlapping entries. If+ * false, overlapping entries are omitted.+ */+ void addIdentityMapping(Trace from, Program toProgram, Range<Long> lifespan,+ boolean truncateExisting);+/*** Add a static mapping (relocation) from the given module to the given program*diff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProviderTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProviderTest.javaindex 044a266bed..58f7e110de 100644--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProviderTest.java+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/modules/DebuggerModulesProviderTest.java@@ -327,6 +327,41 @@ public class DebuggerModulesProviderTest extends AbstractGhidraHeadedDebuggerGUIassertProviderEmpty();}+ @Test+ public void testActionMapIdentically() throws Exception {+ assertFalse(modulesProvider.actionMapIdentically.isEnabled());++ createAndOpenTrace();+ createAndOpenProgramFromTrace();+ intoProject(tb.trace);+ intoProject(program);++ // No modules necessary+ traceManager.activateTrace(tb.trace);+ waitForSwing();++ assertTrue(modulesProvider.actionMapIdentically.isEnabled());++ // Need some substance in the program+ try (UndoableTransaction tid = UndoableTransaction.start(program, "Populate", true)) {+ addBlock();+ }+ waitForDomainObject(program);++ performAction(modulesProvider.actionMapIdentically);+ waitForDomainObject(tb.trace);++ Collection<? extends TraceStaticMapping> mappings =+ tb.trace.getStaticMappingManager().getAllEntries();+ assertEquals(1, mappings.size());++ TraceStaticMapping sm = mappings.iterator().next();+ assertEquals(Range.atLeast(0L), sm.getLifespan());+ assertEquals("ram:00400000", sm.getStaticAddress());+ assertEquals(0x1000, sm.getLength()); // Block is 0x1000 in length+ assertEquals(tb.addr(0x00400000), sm.getMinTraceAddress());+ }+@Testpublic void testActionMapModules() throws Exception {assertFalse(modulesProvider.actionMapModules.isEnabled());