Ghidra is a software reverse engineering (SRE) framework
GP-4000 decompiler diff view find and orientation toggle
James04a932bab283
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/FunctionComparison/FunctionComparison.htm b/Ghidra/Features/Base/src/main/help/help/topics/FunctionComparison/FunctionComparison.htmindex 40b4294fce..1d846c6c81 100644--- a/Ghidra/Features/Base/src/main/help/help/topics/FunctionComparison/FunctionComparison.htm+++ b/Ghidra/Features/Base/src/main/help/help/topics/FunctionComparison/FunctionComparison.htm@@ -176,8 +176,7 @@Header</A>.</P></BLOCKQUOTE>- <H4><A name="Dual_Listing_Toggle_Orientation"></A>Show Listings Side-by-Side</H4>-+ <H4><A name="Dual_Listing_Toggle_Orientation"></A>Show Listings Side-by-Side</H4><BLOCKQUOTE><P>The two listings which display each of the functions can be displayed side by side orone above the other. To change how the two listings are positioned relative to each@@ -448,7 +447,14 @@above will operate on the comparison panel that has focus, identified by thepink border.</p></blockquote>-++ <H3><A name="Decompiler_Code_Comparison_Options"></A>Decompiler Code Comparison Options</H3>++ <H4><A name="Dual_Decompiler_Toggle_Orientation"></A>Show Decompiler Side-by-Side</H4>+ <BLOCKQUOTE>+ <P>This toggles the decompiler panels between a vertical split and a horizontal split.+ </BLOCKQUOTE>+<H3>Other Function Comparison Actions</H3><P>The following are additional actions that are available in the Function Comparisondiff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCodeComparisonPanel.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCodeComparisonPanel.javaindex 1e1046f34a..5f636ab9f6 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCodeComparisonPanel.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCodeComparisonPanel.java@@ -23,7 +23,7 @@ import javax.swing.*;import docking.ActionContext;import docking.ComponentProvider;-import docking.action.DockingAction;+import docking.action.*;import docking.widgets.fieldpanel.FieldPanel;import docking.widgets.fieldpanel.internal.FieldPanelCoordinator;import docking.widgets.fieldpanel.support.FieldLocation;@@ -67,6 +67,9 @@ public abstract class DecompilerCodeComparisonPanel<T extends DualDecompilerFielprivate String rightTitle = NO_FUNCTION_TITLE;private ProgramLocationListener leftDecompilerLocationListener;private ProgramLocationListener rightDecompilerLocationListener;+ private DecompilerDiffViewFindAction diffViewFindAction;+ private boolean isSideBySide = true;+ private ToggleOrientationAction toggleOrientationAction;/*** Creates a comparison panel with two decompilers@@ -526,12 +529,15 @@ public abstract class DecompilerCodeComparisonPanel<T extends DualDecompilerFiel*/protected void createActions() {applyFunctionSignatureAction = new ApplyFunctionSignatureAction(owner);+ diffViewFindAction = new DecompilerDiffViewFindAction(owner, tool);+ toggleOrientationAction = new ToggleOrientationAction();}@Overridepublic DockingAction[] getActions() {DockingAction[] codeCompActions = super.getActions();- DockingAction[] otherActions = new DockingAction[] { applyFunctionSignatureAction };+ DockingAction[] otherActions = new DockingAction[] { applyFunctionSignatureAction,+ diffViewFindAction, toggleOrientationAction };int compCount = codeCompActions.length;int otherCount = otherActions.length;DockingAction[] actions = new DockingAction[compCount + otherCount];@@ -540,6 +546,24 @@ public abstract class DecompilerCodeComparisonPanel<T extends DualDecompilerFielreturn actions;}+ /**+ * Sets whether or not the decompilers are displayed side by side.+ *+ * @param sideBySide if true, the decompilers are side by side, otherwise one is above+ * the other.+ */+ private void showSideBySide(boolean sideBySide) {+ isSideBySide = sideBySide;+ splitPane.setOrientation(+ isSideBySide ? JSplitPane.HORIZONTAL_SPLIT : JSplitPane.VERTICAL_SPLIT);+ splitPane.setDividerLocation(0.5);+ toggleOrientationAction.setSelected(sideBySide);+ }++ private boolean isSideBySide() {+ return isSideBySide;+ }+@Overridepublic abstract Class<? extends DecompilerCodeComparisonPanel<? extends FieldPanelCoordinator>> getPanelThisSupersedes();@@ -639,6 +663,19 @@ public abstract class DecompilerCodeComparisonPanel<T extends DualDecompilerFielreturn (functions[RIGHT] != null) ? functions[RIGHT].getBody() : EMPTY_ADDRESS_SET;}+ @Override+ public FieldPanel getLeftFieldPanel() {+ return getLeftDecompilerPanel().getFieldPanel();+ }++ @Override+ public FieldPanel getRightFieldPanel() {+ return getRightDecompilerPanel().getFieldPanel();+ }++ @Override+ protected abstract DualDecompilerFieldPanelCoordinator createFieldPanelCoordinator();+private class MyDecompileResultsListener implements DualDecompileResultsListener {private FieldLocation leftCursorLocation;@@ -694,16 +731,22 @@ public abstract class DecompilerCodeComparisonPanel<T extends DualDecompilerFiel}}- @Override- public FieldPanel getLeftFieldPanel() {- return getLeftDecompilerPanel().getFieldPanel();- }+ private class ToggleOrientationAction extends ToggleDockingAction {+ ToggleOrientationAction() {+ super("Dual Decompiler Toggle Orientation", "FunctionComparison");+ setDescription("<HTML>Toggle the layout of the decompiler " ++ "<BR>between side-by-side and one above the other.");+ setEnabled(true);+ setSelected(isSideBySide);+ MenuData menuData =+ new MenuData(new String[] { "Show Decompilers Side-by-Side" }, "Dual Decompiler");+ setMenuBarData(menuData);+ }- @Override- public FieldPanel getRightFieldPanel() {- return getRightDecompilerPanel().getFieldPanel();+ @Override+ public void actionPerformed(ActionContext context) {+ boolean sideBySide = !isSideBySide();+ showSideBySide(sideBySide);+ }}-- @Override- protected abstract DualDecompilerFieldPanelCoordinator createFieldPanelCoordinator();}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerDiffViewFindAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerDiffViewFindAction.javanew file mode 100644index 0000000000..6e3b89bbd6--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerDiffViewFindAction.java@@ -0,0 +1,115 @@+/* ###+ * 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.decompiler.component;++import java.awt.event.KeyEvent;++import org.apache.commons.lang3.StringUtils;++import docking.ActionContext;+import docking.DockingUtils;+import docking.action.*;+import docking.widgets.FindDialog;+import docking.widgets.fieldpanel.internal.FieldPanelCoordinator;+import ghidra.app.plugin.core.decompile.actions.DecompilerSearcher;+import ghidra.app.util.HelpTopics;+import ghidra.app.util.viewer.util.CodeComparisonPanel;+import ghidra.framework.plugintool.PluginTool;+import ghidra.util.HelpLocation;++public class DecompilerDiffViewFindAction extends DockingAction {++ private FindDialog leftFindDialog;+ private FindDialog rightFindDialog;+ private PluginTool tool;++ public DecompilerDiffViewFindAction(String owner, PluginTool tool) {+ super("Find", owner, true);+ setHelpLocation(new HelpLocation(HelpTopics.DECOMPILER, "ActionFind"));+ setPopupMenuData(new MenuData(new String[] { "Find..." }, "Decompile"));+ setKeyBindingData(+ new KeyBindingData(KeyEvent.VK_F, DockingUtils.CONTROL_KEY_MODIFIER_MASK));+ setEnabled(true);+ this.tool = tool;+ }++ @Override+ public boolean isAddToPopup(ActionContext context) {+ return (context instanceof DualDecompilerActionContext);+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {+ if (!(context instanceof DualDecompilerActionContext dualContext)) {+ return false;+ }+ CodeComparisonPanel<? extends FieldPanelCoordinator> compPanel =+ dualContext.getCodeComparisonPanel();+ if (!(compPanel instanceof DecompilerCodeComparisonPanel decompilerCompPanel)) {+ return false;+ }+ return true;+ }++ @Override+ public void actionPerformed(ActionContext context) {+ DualDecompilerActionContext dualContext = (DualDecompilerActionContext) context;+ @SuppressWarnings("unchecked")+ DecompilerCodeComparisonPanel<? extends FieldPanelCoordinator> decompilerCompPanel =+ (DecompilerCodeComparisonPanel<? extends FieldPanelCoordinator>) dualContext+ .getCodeComparisonPanel();++ DecompilerPanel focusedPanel = decompilerCompPanel.getLeftDecompilerPanel();+ FindDialog dialog = leftFindDialog;+ if (!decompilerCompPanel.leftPanelHasFocus()) {+ focusedPanel = decompilerCompPanel.getRightDecompilerPanel();+ dialog = rightFindDialog;+ }+ if (dialog == null) {+ dialog = createFindDialog(focusedPanel, decompilerCompPanel.leftPanelHasFocus());+ }++ String text = focusedPanel.getSelectedText();+ if (!StringUtils.isBlank(text)) {+ dialog.setSearchText(text);+ }+ tool.showDialog(dialog);+ }++ private FindDialog createFindDialog(DecompilerPanel decompilerPanel, boolean isLeftFocused) {+ String title = (isLeftFocused ? "Left" : "Right");+ title += " Decompiler Find Text";++ FindDialog dialog = new FindDialog(title, new DecompilerSearcher(decompilerPanel)) {+ @Override+ protected void dialogClosed() {+ // clear the search results when the dialog is closed+ decompilerPanel.setSearchResults(null);+ }+ };+ dialog.setHelpLocation(new HelpLocation(HelpTopics.DECOMPILER, "ActionFind"));++ if (isLeftFocused) {+ leftFindDialog = dialog;+ }+ else {+ rightFindDialog = dialog;+ }++ return dialog;+ }++}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DecompilerSearcher.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DecompilerSearcher.javanew file mode 100644index 0000000000..30f6944857--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DecompilerSearcher.java@@ -0,0 +1,142 @@+/* ###+ * 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.decompile.actions;++import java.util.List;+import java.util.Objects;++import docking.widgets.*;+import docking.widgets.fieldpanel.field.Field;+import docking.widgets.fieldpanel.support.FieldLocation;+import ghidra.app.decompiler.component.ClangTextField;+import ghidra.app.decompiler.component.DecompilerPanel;++/**+ * A {@link FindDialogSearcher} for searching the text of the decompiler window.+ */+public class DecompilerSearcher implements FindDialogSearcher {++ private DecompilerPanel decompilerPanel;++ /**+ * Constructor+ * @param decompilerPanel decompiler panel+ */+ public DecompilerSearcher(DecompilerPanel decompilerPanel) {+ this.decompilerPanel = decompilerPanel;+ }++ @Override+ public CursorPosition getCursorPosition() {+ FieldLocation fieldLocation = decompilerPanel.getCursorPosition();+ return new DecompilerCursorPosition(fieldLocation);+ }++ @Override+ public CursorPosition getStart() {++ int lineNumber = 0;+ int fieldNumber = 0; // always 0, as the field is the entire line and it is the only field+ int column = 0; // or length for the end+ FieldLocation fieldLocation = new FieldLocation(lineNumber, fieldNumber, 0, column);+ return new DecompilerCursorPosition(fieldLocation);+ }++ @Override+ public CursorPosition getEnd() {++ List<Field> lines = decompilerPanel.getFields();+ int lineNumber = lines.size() - 1;+ ClangTextField textLine = (ClangTextField) lines.get(lineNumber);++ int fieldNumber = 0; // always 0, as the field is the entire line and it is the only field+ int rowCount = textLine.getNumRows();+ int row = rowCount - 1; // 0-based+ int column = textLine.getNumCols(row);+ FieldLocation fieldLocation = new FieldLocation(lineNumber, fieldNumber, row, column);+ return new DecompilerCursorPosition(fieldLocation);+ }++ @Override+ public void setCursorPosition(CursorPosition position) {+ decompilerPanel.setCursorPosition(((DecompilerCursorPosition) position).getFieldLocation());+ }++ @Override+ public void highlightSearchResults(SearchLocation location) {+ decompilerPanel.setSearchResults(location);+ }++ @Override+ public SearchLocation search(String text, CursorPosition position, boolean searchForward,+ boolean useRegex) {+ DecompilerCursorPosition decompilerCursorPosition = (DecompilerCursorPosition) position;+ FieldLocation startLocation =+ getNextSearchStartLocation(decompilerCursorPosition, searchForward);+ return useRegex ? decompilerPanel.searchTextRegex(text, startLocation, searchForward)+ : decompilerPanel.searchText(text, startLocation, searchForward);+ }++ private FieldLocation getNextSearchStartLocation(+ DecompilerCursorPosition decompilerCursorPosition, boolean searchForward) {++ FieldLocation startLocation = decompilerCursorPosition.getFieldLocation();+ FieldBasedSearchLocation currentSearchLocation = decompilerPanel.getSearchResults();+ if (currentSearchLocation == null) {+ return startLocation; // nothing to do; no prior search hit+ }++ //+ // Special Case Handling: Start the search at the cursor location by default.+ // However, if the cursor location is at the beginning of previous search hit, then+ // move the cursor forward by one character to ensure the previous search hit is not+ // found.+ //+ // Note: for a forward or backward search the cursor is placed at the beginning of the+ // match.+ //+ if (Objects.equals(startLocation, currentSearchLocation.getFieldLocation())) {++ if (searchForward) {+ // Given:+ // -search text: 'fox'+ // -search domain: 'What the |fox say'+ // -a previous search hit just before 'fox'+ //+ // Move the cursor just past the 'f' so the next forward search will not+ // find the current 'fox' hit. Thus the new search domain for this line+ // will be: "ox say"+ //+ startLocation.col += 1;+ }+ else {+ // Given:+ // -search text: 'fox'+ // -search domain: 'What the |fox say'+ // -a previous search hit just before 'fox'+ //+ // Move the cursor just past the 'o' so the next backward search will not+ // find the current 'fox' hit. Thus the new search domain for this line+ // will be: "What the fo"+ //+ int length = currentSearchLocation.getMatchLength();+ startLocation.col += length - 1;+ }+ }++ return startLocation;+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.javaindex 0860b4bf0c..be39bc4fba 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java@@ -17,17 +17,12 @@ package ghidra.app.plugin.core.decompile.actions;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;-import java.util.List;-import java.util.Objects;import org.apache.commons.lang3.StringUtils;import docking.action.KeyBindingData;import docking.action.MenuData;-import docking.widgets.*;-import docking.widgets.fieldpanel.field.Field;-import docking.widgets.fieldpanel.support.FieldLocation;-import ghidra.app.decompiler.component.ClangTextField;+import docking.widgets.FindDialog;import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.util.HelpTopics;@@ -91,115 +86,4 @@ public class FindAction extends AbstractDecompilerAction {// show over the root frame, so the user can still see the Decompiler windowcontext.getTool().showDialog(dialog);}-- private static class DecompilerSearcher implements FindDialogSearcher {-- private DecompilerPanel decompilerPanel;-- public DecompilerSearcher(DecompilerPanel dPanel) {- decompilerPanel = dPanel;- }-- @Override- public CursorPosition getCursorPosition() {- FieldLocation fieldLocation = decompilerPanel.getCursorPosition();- return new DecompilerCursorPosition(fieldLocation);- }-- @Override- public CursorPosition getStart() {-- int lineNumber = 0;- int fieldNumber = 0; // always 0, as the field is the entire line and it is the only field- int column = 0; // or length for the end- FieldLocation fieldLocation = new FieldLocation(lineNumber, fieldNumber, 0, column);- return new DecompilerCursorPosition(fieldLocation);- }-- @Override- public CursorPosition getEnd() {-- List<Field> lines = decompilerPanel.getFields();- int lineNumber = lines.size() - 1;- ClangTextField textLine = (ClangTextField) lines.get(lineNumber);-- int fieldNumber = 0; // always 0, as the field is the entire line and it is the only field- int rowCount = textLine.getNumRows();- int row = rowCount - 1; // 0-based- int column = textLine.getNumCols(row);- FieldLocation fieldLocation = new FieldLocation(lineNumber, fieldNumber, row, column);- return new DecompilerCursorPosition(fieldLocation);- }-- @Override- public void setCursorPosition(CursorPosition position) {- decompilerPanel.setCursorPosition(- ((DecompilerCursorPosition) position).getFieldLocation());- }-- @Override- public void highlightSearchResults(SearchLocation location) {- decompilerPanel.setSearchResults(location);- }-- @Override- public SearchLocation search(String text, CursorPosition position, boolean searchForward,- boolean useRegex) {- DecompilerCursorPosition decompilerCursorPosition = (DecompilerCursorPosition) position;- FieldLocation startLocation =- getNextSearchStartLocation(decompilerCursorPosition, searchForward);- return useRegex ? decompilerPanel.searchTextRegex(text, startLocation, searchForward)- : decompilerPanel.searchText(text, startLocation, searchForward);- }-- private FieldLocation getNextSearchStartLocation(- DecompilerCursorPosition decompilerCursorPosition, boolean searchForward) {-- FieldLocation startLocation = decompilerCursorPosition.getFieldLocation();- FieldBasedSearchLocation currentSearchLocation = decompilerPanel.getSearchResults();- if (currentSearchLocation == null) {- return startLocation; // nothing to do; no prior search hit- }-- //- // Special Case Handling: Start the search at the cursor location by default.- // However, if the cursor location is at the beginning of previous search hit, then- // move the cursor forward by one character to ensure the previous search hit is not- // found.- //- // Note: for a forward or backward search the cursor is placed at the beginning of the- // match.- //- if (Objects.equals(startLocation, currentSearchLocation.getFieldLocation())) {-- if (searchForward) {- // Given:- // -search text: 'fox'- // -search domain: 'What the |fox say'- // -a previous search hit just before 'fox'- //- // Move the cursor just past the 'f' so the next forward search will not- // find the current 'fox' hit. Thus the new search domain for this line- // will be: "ox say"- //- startLocation.col += 1;- }- else {- // Given:- // -search text: 'fox'- // -search domain: 'What the |fox say'- // -a previous search hit just before 'fox'- //- // Move the cursor just past the 'o' so the next backward search will not- // find the current 'fox' hit. Thus the new search domain for this line- // will be: "What the fo"- //- int length = currentSearchLocation.getMatchLength();- startLocation.col += length - 1;- }- }-- return startLocation;- }- }}