Ghidra is a software reverse engineering (SRE) framework
GP-0: Alternative visibility tracking strategy. Save and restore selection.
Dan98a7aa256fc3
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProvider.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProvider.javaindex d185e040ff..34cfc19162 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProvider.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProvider.java@@ -20,6 +20,7 @@ import java.awt.event.*;import java.lang.invoke.MethodHandles;import java.util.List;import java.util.Objects;+import java.util.Set;import java.util.function.Function;import java.util.stream.Collectors;@@ -368,52 +369,50 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveablereturn;}- try (ListenerSuppressor suppressor = objectsTreePanel.suppressShowingListener()) {- mainPanel.removeAll();- mainPanel.add(queryPanel, BorderLayout.NORTH);+ mainPanel.removeAll();+ mainPanel.add(queryPanel, BorderLayout.NORTH);- if (showObjectsTree && showElementsTable && showAttributesTable) {- JSplitPane lrSplit = createLrSplit();- mainPanel.add(lrSplit, BorderLayout.CENTER);- JSplitPane tbSplit = createTbSplit();- lrSplit.setRightComponent(tbSplit);- lrSplit.setLeftComponent(objectsTreePanel);- tbSplit.setLeftComponent(createLabeledElementsTable());- tbSplit.setRightComponent(createLabeledAttributesTable());- }- else if (showObjectsTree && showElementsTable) {- JSplitPane lrSplit = createLrSplit();- mainPanel.add(lrSplit, BorderLayout.CENTER);- lrSplit.setLeftComponent(objectsTreePanel);- lrSplit.setRightComponent(elementsTablePanel);- }- else if (showObjectsTree && showAttributesTable) {- JSplitPane lrSplit = createLrSplit();- mainPanel.add(lrSplit, BorderLayout.CENTER);- lrSplit.setLeftComponent(objectsTreePanel);- lrSplit.setRightComponent(attributesTablePanel);- }- else if (showElementsTable && showAttributesTable) {- JSplitPane tbSplit = createTbSplit();- tbSplit.setLeftComponent(createLabeledElementsTable());- tbSplit.setRightComponent(createLabeledAttributesTable());- mainPanel.add(tbSplit, BorderLayout.CENTER);- }- else if (showObjectsTree) {- mainPanel.add(objectsTreePanel);- }- else if (showElementsTable) {- mainPanel.add(elementsTablePanel);- }- else if (showAttributesTable) {- mainPanel.add(attributesTablePanel);- }- else {- // The actions should not allow this, but in case it happens, help the user out.- mainPanel.add(new JLabel("""- <html>Well, you should probably enable at least one panel.- Use the local toolbar buttons."""));- }+ if (showObjectsTree && showElementsTable && showAttributesTable) {+ JSplitPane lrSplit = createLrSplit();+ mainPanel.add(lrSplit, BorderLayout.CENTER);+ JSplitPane tbSplit = createTbSplit();+ lrSplit.setRightComponent(tbSplit);+ lrSplit.setLeftComponent(objectsTreePanel);+ tbSplit.setLeftComponent(createLabeledElementsTable());+ tbSplit.setRightComponent(createLabeledAttributesTable());+ }+ else if (showObjectsTree && showElementsTable) {+ JSplitPane lrSplit = createLrSplit();+ mainPanel.add(lrSplit, BorderLayout.CENTER);+ lrSplit.setLeftComponent(objectsTreePanel);+ lrSplit.setRightComponent(elementsTablePanel);+ }+ else if (showObjectsTree && showAttributesTable) {+ JSplitPane lrSplit = createLrSplit();+ mainPanel.add(lrSplit, BorderLayout.CENTER);+ lrSplit.setLeftComponent(objectsTreePanel);+ lrSplit.setRightComponent(attributesTablePanel);+ }+ else if (showElementsTable && showAttributesTable) {+ JSplitPane tbSplit = createTbSplit();+ tbSplit.setLeftComponent(createLabeledElementsTable());+ tbSplit.setRightComponent(createLabeledAttributesTable());+ mainPanel.add(tbSplit, BorderLayout.CENTER);+ }+ else if (showObjectsTree) {+ mainPanel.add(objectsTreePanel);+ }+ else if (showElementsTable) {+ mainPanel.add(elementsTablePanel);+ }+ else if (showAttributesTable) {+ mainPanel.add(attributesTablePanel);+ }+ else {+ // The actions should not allow this, but in case it happens, help the user out.+ mainPanel.add(new JLabel("""+ <html>Well, you should probably enable at least one panel.+ Use the local toolbar buttons."""));}mainPanel.revalidate();}@@ -426,8 +425,7 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveablepublic boolean verify(JComponent input) {try {TraceObjectKeyPath path = TraceObjectKeyPath.parse(pathField.getText());- setPath(path);- objectsTreePanel.setSelectedKeyPaths(List.of(path));+ setPath(path, null, EventOrigin.USER_GENERATED);return true;}catch (IllegalArgumentException e) {@@ -501,7 +499,7 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveable}TraceObjectValue value = sel.get(0).getValue();setPath(value == null ? TraceObjectKeyPath.of() : value.getCanonicalPath(),- objectsTreePanel);+ objectsTreePanel, EventOrigin.INTERNAL_GENERATED);});objectsTreePanel.tree.addMouseListener(new MouseAdapter() {@Override@@ -661,10 +659,13 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveableclone.readConfigState(configState);SaveState dataState = new SaveState();this.writeDataState(dataState);+ // Selection is not saved to the tool state+ Set<TraceObjectKeyPath> selection = this.objectsTreePanel.getSelectedKeyPaths();// coords are omitted by main window// also, cannot save unless trace is in a projectclone.coordinatesActivated(current);clone.readDataState(dataState);+ clone.objectsTreePanel.setSelectedKeyPaths(selection);plugin.getTool().showComponentProvider(clone, true);}@@ -827,14 +828,14 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveable}}- protected void setPath(TraceObjectKeyPath path, JComponent source) {+ protected void setPath(TraceObjectKeyPath path, JComponent source, EventOrigin origin) {if (Objects.equals(this.path, path) && getTreeSelection() != null) {return;}this.path = path;if (source != objectsTreePanel) {- setTreeSelection(path);+ setTreeSelection(path, origin);}pathField.setText(path.toString());@@ -844,7 +845,7 @@ public class DebuggerModelProvider extends ComponentProvider implements Saveable}public void setPath(TraceObjectKeyPath path) {- setPath(path, null);+ setPath(path, null, EventOrigin.API_GENERATED);}public TraceObjectKeyPath getPath() {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ListenerSuppressor.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ListenerSuppressor.javadeleted file mode 100644index 8bd713dd6b..0000000000--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ListenerSuppressor.java+++ /dev/null@@ -1,21 +0,0 @@-/* ###- * IP: GHIDRA- *- * 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.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.app.plugin.core.debug.gui.model;--public interface ListenerSuppressor extends AutoCloseable {- @Override- void close();-}diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ObjectsTreePanel.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ObjectsTreePanel.javaindex 926c7ae10c..5a1be08cc9 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ObjectsTreePanel.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/model/ObjectsTreePanel.java@@ -37,6 +37,7 @@ import ghidra.debug.api.tracemgr.DebuggerCoordinates;import ghidra.trace.model.Lifespan;import ghidra.trace.model.Trace;import ghidra.trace.model.target.*;+import ghidra.util.Swing;public class ObjectsTreePanel extends JPanel {@@ -97,8 +98,33 @@ public class ObjectsTreePanel extends JPanel {}}+ protected static class DelayedSwingHack implements Runnable {+ public static void runWayLater(int delay, Runnable runnable) {+ Swing.runLater(new DelayedSwingHack(delay, runnable));+ }++ private int delay;+ private final Runnable runnable;++ public DelayedSwingHack(int delay, Runnable runnable) {+ this.delay = delay;+ this.runnable = runnable;+ }++ @Override+ public void run() {+ if (--delay == 0) {+ runnable.run();+ }+ else {+ Swing.runLater(this);+ }+ }+ }+protected class ListenerForShowing implements AncestorListener {boolean showing = false;+ int version = 0;@Overridepublic void ancestorRemoved(AncestorEvent event) {@@ -116,7 +142,22 @@ public class ObjectsTreePanel extends JPanel {}public void updateShowing() {- setShowing(ObjectsTreePanel.this.isShowing());+ /**+ * There may be several changes to visibility throughout some routines running on the+ * Swing thread. To avoid processing such frantic changes, we aim to wait for the last+ * change. Thus, we increment a version counter and schedule a delayed lambda on the+ * Swing thread. That lambda will only actually do anything if the version number hasn't+ * been incremented, i.e., some additional change hasn't occurred. This should prevent+ * intermediate changes, e.g., when re-docking the window, from causing needless+ * updates.+ */+ final int v = ++version;+ DelayedSwingHack.runWayLater(2, () -> {+ if (v == version) {+ version = 0;+ setShowing(ObjectsTreePanel.this.isShowing());+ }+ });}private void setShowing(boolean showing) {@@ -128,22 +169,11 @@ public class ObjectsTreePanel extends JPanel {}}- protected class ListenerForShowingSuppressor implements ListenerSuppressor {- public ListenerForShowingSuppressor() {- removeAncestorListener(listenerForShowing);- }-- @Override- public void close() {- addAncestorListener(listenerForShowing);- listenerForShowing.updateShowing();- }- }-protected final ObjectTreeModel treeModel;protected final ObjectGTree tree;protected boolean showing = false;+ protected Set<TraceObjectKeyPath> savedSelection = null;protected DebuggerCoordinates current = DebuggerCoordinates.NOWHERE;protected DebuggerCoordinates previous = DebuggerCoordinates.NOWHERE;protected boolean limitToSnap = true;@@ -168,10 +198,6 @@ public class ObjectsTreePanel extends JPanel {add(tree, BorderLayout.CENTER);}- public ListenerSuppressor suppressShowingListener() {- return new ListenerForShowingSuppressor();- }-protected ObjectTreeModel createModel() {return new ObjectTreeModel();}@@ -181,15 +207,17 @@ public class ObjectsTreePanel extends JPanel {}protected void showingChanged(boolean showing) {+ if (!showing) {+ savedSelection = getSelectedKeyPaths();+ }this.showing = showing;updateTreeModelForCoordinates();updateTreeModelForSpan();updateTreeModelForShowHidden();updateTreeModelForShowPrimitives();updateTreeModelForShowMethods();- if (showing) {- // Not going to restore the actual selection- selectCurrent();+ if (showing && savedSelection != null) {+ setSelectedKeyPaths(savedSelection, EventOrigin.INTERNAL_GENERATED);}// Restore expansion? Nah.}@@ -380,6 +408,7 @@ public class ObjectsTreePanel extends JPanel {}public void setSelectedKeyPaths(Collection<TraceObjectKeyPath> keyPaths, EventOrigin origin) {+ savedSelection = keyPaths instanceof Set<TraceObjectKeyPath> s ? s : Set.copyOf(keyPaths);List<TreePath> treePaths = new ArrayList<>();for (TraceObjectKeyPath path : keyPaths) {AbstractNode node = getNode(path);@@ -390,6 +419,20 @@ public class ObjectsTreePanel extends JPanel {tree.setSelectionPaths(treePaths.toArray(TreePath[]::new), origin);}+ public Set<TraceObjectKeyPath> getSelectedKeyPaths() {+ Set<TraceObjectKeyPath> result = new HashSet<>();+ for (AbstractNode node : getSelectedItems()) {+ TraceObjectValue value = node.getValue();+ if (value == null) {+ result.add(TraceObjectKeyPath.of());+ }+ else {+ result.add(value.getCanonicalPath());+ }+ }+ return result;+ }+public void setSelectedKeyPaths(Collection<TraceObjectKeyPath> keyPaths) {setSelectedKeyPaths(keyPaths, EventOrigin.API_GENERATED);}diff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProviderTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProviderTest.javaindex d30dfa9a9a..d8eff8c898 100644--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProviderTest.java+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/model/DebuggerModelProviderTest.java@@ -647,6 +647,9 @@ public class DebuggerModelProviderTest extends AbstractGhidraHeadedDebuggerTestmodelProvider.setPath(TraceObjectKeyPath.parse("Processes[0].Threads[2]"));waitForTasks();+ // Pre-check+ assertEquals(TraceObjectKeyPath.parse("Processes[0].Threads[2]"), modelProvider.path);+performAction(modelProvider.actionCloneWindow);DebuggerModelProvider clone = Unique.assertOne(modelPlugin.getDisconnectedProviders());