Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7343 - Fixed a bug in how the ProgramActivatedPluginEvent is handled by the ProgramManager

dragonmacher89281d74ae69
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DynamicStaticSynchronizationPlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DynamicStaticSynchronizationPlugin.java
index a1d3a929f8..7f538773f8 100644
--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DynamicStaticSynchronizationPlugin.java
+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DynamicStaticSynchronizationPlugin.java
@@ -81,7 +81,8 @@ import ghidra.util.task.*;
},
servicesRequired = {
DebuggerStaticMappingService.class,
- })
+ }
+)
public class DynamicStaticSynchronizationPlugin extends Plugin {
interface SyncLocationsAction {
@@ -414,7 +415,8 @@ public class DynamicStaticSynchronizationPlugin extends Plugin {
}
if (currentStatic != staticLoc.getProgram()) {
currentStatic = staticLoc.getProgram();
- firePluginEvent(new ProgramActivatedPluginEvent(getName(), staticLoc.getProgram()));
+ ProgramManager pm = tool.getService(ProgramManager.class);
+ pm.setCurrentProgram(staticLoc.getProgram());
}
currentStaticLocation = staticLoc;
firePluginEvent(
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/events/ProgramActivatedPluginEvent.java b/Ghidra/Features/Base/src/main/java/ghidra/app/events/ProgramActivatedPluginEvent.java
index 4bcdec39e3..7a26339957 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/events/ProgramActivatedPluginEvent.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/events/ProgramActivatedPluginEvent.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,6 +17,7 @@ package ghidra.app.events;
import java.lang.ref.WeakReference;
+import ghidra.app.services.ProgramManager;
import ghidra.framework.plugintool.PluginEvent;
import ghidra.program.model.listing.Program;
@@ -35,7 +36,10 @@ public class ProgramActivatedPluginEvent extends PluginEvent {
* Construct a new plugin event.
* @param source name of the plugin that created this event
* @param activeProgram the program associated with this event
+ * @deprecated Clients should not be constructing and firing this event. Instead clients should
+ * be using {@link ProgramManager#setCurrentProgram(Program)}.
*/
+ @Deprecated(since = "12.3")
public ProgramActivatedPluginEvent(String source, Program activeProgram) {
super(source, NAME);
this.newProgramRef = new WeakReference<Program>(activeProgram);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/tool/ListingMergePanelPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/tool/ListingMergePanelPlugin.java
index f4db8e5b05..f5acacb157 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/tool/ListingMergePanelPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/tool/ListingMergePanelPlugin.java
@@ -17,13 +17,14 @@ package ghidra.app.merge.tool;
import docking.ComponentProvider;
import ghidra.app.CorePluginPackage;
-import ghidra.app.events.ProgramActivatedPluginEvent;
import ghidra.app.events.ProgramLocationPluginEvent;
import ghidra.app.merge.MergeConstants;
import ghidra.app.plugin.PluginCategoryNames;
+import ghidra.app.services.ProgramManager;
import ghidra.framework.main.ProgramaticUseOnly;
import ghidra.framework.plugintool.*;
import ghidra.framework.plugintool.util.PluginStatus;
+import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramLocation;
//@formatter:off
@@ -48,8 +49,11 @@ public class ListingMergePanelPlugin extends Plugin implements ProgramaticUseOnl
public ListingMergePanelPlugin(PluginTool tool, ListingMergePanel mergePanel) {
super(tool);
createProvider(mergePanel);
- firePluginEvent(new ProgramActivatedPluginEvent(this.getName(),
- mergePanel.getProgram(MergeConstants.RESULT)));
+
+ Program p = mergePanel.getProgram(MergeConstants.RESULT);
+ ProgramManager pm = tool.getService(ProgramManager.class);
+ pm.setCurrentProgram(p);
+
createActions();
}
@@ -62,14 +66,12 @@ public class ListingMergePanelPlugin extends Plugin implements ProgramaticUseOnl
return provider;
}
- /* (non-Javadoc)
- * @see ghidra.framework.plugintool.Plugin#dispose()
- */
@Override
public void dispose() {
if (provider != null) {
provider.dispose();
- firePluginEvent(new ProgramActivatedPluginEvent(this.getName(), null));
+ ProgramManager pm = tool.getService(ProgramManager.class);
+ pm.setCurrentProgram(null);
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/MultiProgramManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/MultiProgramManager.java
index 21ee8b0682..7a3f38512d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/MultiProgramManager.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/MultiProgramManager.java
@@ -140,11 +140,10 @@ class MultiProgramManager implements TransactionListener {
}
if (info.owner != null) {
- // persist program
info.setVisible(false);
if (info == currentInfo) {
ProgramInfo newCurrent = findNextCurrent();
- setCurrentProgram(newCurrent);
+ doSetCurrentProgram(newCurrent, true);
}
}
else {
@@ -153,7 +152,7 @@ class MultiProgramManager implements TransactionListener {
p.removeListener(domainObjectListener);
if (info == currentInfo) {
ProgramInfo newCurrent = findNextCurrent();
- setCurrentProgram(newCurrent);
+ doSetCurrentProgram(newCurrent, true);
}
fireCloseEvents(p);
p.release(tool);
@@ -190,7 +189,20 @@ class MultiProgramManager implements TransactionListener {
return null;
}
+ void programActivated(Program p) {
+
+ // When we respond to a fired event, we cannot tell if we got the event before other
+ // plugins. This means that we can't ask plugins to save their state, as their program
+ // may have changed.
+ boolean saveState = false;
+ setCurrentProgram(p, saveState);
+ }
+
void setCurrentProgram(Program p) {
+ setCurrentProgram(p, true);
+ }
+
+ private void setCurrentProgram(Program p, boolean saveState) {
if (currentInfo != null) {
if (currentInfo.program.equals(p)) {
return; // already active
@@ -203,7 +215,7 @@ class MultiProgramManager implements TransactionListener {
ProgramInfo info = getInfo(p);
if (info != null) {
- setCurrentProgram(info);
+ doSetCurrentProgram(info, saveState);
}
}
@@ -239,18 +251,17 @@ class MultiProgramManager implements TransactionListener {
}
- private void setCurrentProgram(ProgramInfo info) {
+ private void doSetCurrentProgram(ProgramInfo info, boolean saveState) {
if (currentInfo == info) {
return;
}
- Program newProgram = info == null ? null : info.program;
-
if (currentInfo != null) {
- currentInfo.lastState = tool.getTransientState();
+ currentInfo.lastState = saveState ? tool.getTransientState() : null;
tool.setSubTitle("");
txMonitor.setProgram(null);
}
+
currentInfo = info;
TransientToolState toolState = null;
if (currentInfo != null) {
@@ -261,10 +272,14 @@ class MultiProgramManager implements TransactionListener {
toolState = currentInfo.lastState;
}
}
+
+ Program newProgram = info == null ? null : info.program;
fireActivatedEvent(newProgram);
+
if (toolState != null) {
toolState.restoreTool();
}
+
// only fire the post activated event when a program is activated (we send activated with
// null program to represent a phantom de-activated event)
if (newProgram != null) {
@@ -280,9 +295,9 @@ class MultiProgramManager implements TransactionListener {
private void fireCloseEvents(Program program) {
plugin.firePluginEvent(new ProgramClosedPluginEvent(pluginName, program));
plugin.firePluginEvent(new CloseProgramPluginEvent(pluginName, program, true));
-// tool.contextChanged();
}
+ @SuppressWarnings("deprecation") // Ignore the deprecation; we are the authority of this event
private void fireActivatedEvent(Program newProgram) {
plugin.firePluginEvent(new ProgramActivatedPluginEvent(pluginName, newProgram));
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/ProgramManagerPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/ProgramManagerPlugin.java
index a618ae58af..6166b7286e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/ProgramManagerPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/progmgr/ProgramManagerPlugin.java
@@ -749,9 +749,6 @@ public class ProgramManagerPlugin extends Plugin implements ProgramManager, Opti
Swing.runIfSwingOrRunLater(() -> programSaveMgr.saveAs(program));
}
- /**
- * Write out my data state.
- */
@Override
public void writeDataState(SaveState saveState) {
@@ -781,15 +778,13 @@ public class ProgramManagerPlugin extends Plugin implements ProgramManager, Opti
}
}
- /**
- * Read in my data state.
- */
@Override
public void readDataState(SaveState saveState) {
if (!programMgr.isEmpty()) {
currentLocation = null;
- return; // don't do anything restoring toolstate
+ return; // don't do anything restoring tool state
}
+
loadPrograms(saveState);
String currentFile = saveState.getString("CURRENT_FILE", null);
@@ -950,7 +945,7 @@ public class ProgramManagerPlugin extends Plugin implements ProgramManager, Opti
}
else if (event instanceof ProgramActivatedPluginEvent) {
Program p = ((ProgramActivatedPluginEvent) event).getActiveProgram();
- programMgr.setCurrentProgram(p);
+ programMgr.programActivated(p);
}
else if (event instanceof ProgramLocationPluginEvent) {
ProgramLocationPluginEvent ev = (ProgramLocationPluginEvent) event;
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/marker/MarkerTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/marker/MarkerTest.java
index c6b4350f30..2790b1d5fc 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/marker/MarkerTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/marker/MarkerTest.java
@@ -35,7 +35,6 @@ import docking.widgets.fieldpanel.support.FieldLocation;
import docking.widgets.fieldpanel.support.FieldSelection;
import generic.theme.GThemeDefaults.Colors.Palette;
import ghidra.app.cmd.data.CreateStructureCmd;
-import ghidra.app.events.ProgramActivatedPluginEvent;
import ghidra.app.events.ProgramSelectionPluginEvent;
import ghidra.app.plugin.core.bookmark.BookmarkEditCmd;
import ghidra.app.plugin.core.bookmark.BookmarkPlugin;
@@ -439,7 +438,8 @@ public class MarkerTest extends AbstractGhidraHeadedIntegrationTest {
}
private void switchToProgram(Program p) {
- tool.firePluginEvent(new ProgramActivatedPluginEvent("Test", p));
+ ProgramManager pm = tool.getService(ProgramManager.class);
+ pm.setCurrentProgram(p);
}
private void setupTool(PluginTool tool) throws Exception {