Ghidra is a software reverse engineering (SRE) framework
GT-3291 -History - Added help and tests for PR-475, function history navigation (closes #475)
dragonmacher48f0b6e3145e
diff --git a/Ghidra/Features/Base/certification.manifest b/Ghidra/Features/Base/certification.manifestindex 55fb461fc3..0ce83479fd 100644--- a/Ghidra/Features/Base/certification.manifest+++ b/Ghidra/Features/Base/certification.manifest@@ -843,7 +843,6 @@ src/main/java/ghidra/app/util/bin/format/package.html||GHIDRA||||END|src/main/java/ghidra/app/util/bin/format/pe/package.html||GHIDRA||||END|src/main/java/ghidra/app/util/bin/format/pe/resource/package.html||GHIDRA||||END|src/main/java/ghidra/app/util/bin/package.html||GHIDRA||||END|-src/main/java/ghidra/app/util/cparser/build.xml||GHIDRA||||END|src/main/java/ghidra/app/util/exporter/package.html||GHIDRA||||END|src/main/java/ghidra/app/util/importer/package.html||GHIDRA||||END|src/main/java/ghidra/app/util/opinion/package.html||GHIDRA||||END|diff --git a/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml b/Ghidra/Features/Base/src/main/help/help/TOC_Source.xmlindex cf9ff3fb1c..d154fd594e 100644--- a/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml+++ b/Ghidra/Features/Base/src/main/help/help/TOC_Source.xml@@ -312,7 +312,7 @@<tocdef id="Set Fallthrough Address" sortgroup="m" text="Set Fallthrough Address" target="help/topics/FallThroughPlugin/Override_Fallthrough.htm" /><tocdef id="Navigation" sortgroup="n" text="Navigation" target="help/topics/Navigation/Navigation.htm" ><tocdef id="Go to Address Label" sortgroup="a" text="Go to Address Label" target="help/topics/Navigation/Navigation.htm" />- <tocdef id="Go to Next/Previous" sortgroup="b" text="Go to Next/Previous" target="help/topics/Navigation/Navigation.htm#History_Buffer" />+ <tocdef id="Go to Next/Previous" sortgroup="b" text="Go to Next/Previous" target="help/topics/Navigation/Navigation.htm#Navigation_History" /><tocdef id="Margin and Navigation Markers" sortgroup="c" text="Margin and Navigation Markers" target="help/topics/CodeBrowserPlugin/CodeBrowser.htm#CBNavigationMarkers" /></tocdef>diff --git a/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm b/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htmindex 77f0acc93f..feea3618cb 100644--- a/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm+++ b/Ghidra/Features/Base/src/main/help/help/topics/Navigation/Navigation.htm@@ -399,12 +399,14 @@previous Instruction, Data, Undefined, Function or Non Function. The search starts at thecurrent cursor location and proceeds either forward (next) or backwards (previous).</P>+ <BLOCKQUOTE><P><IMG src="../../shared/note.png" border="0">When searching for Instructions, Data orUndefined items, Ghidra will skip all contiguous items of the same type. For example, if thecursor is on an address with an Instruction, and you go to the next Instruction, then allInstructions immediately following the current one will be skipped until a non-Instruction isfound. Once that non-instruction is found, then Ghidra will take you to the next Instructionafter the address of that non-Instruction.</P>+ </BLOCKQUOTE><H3><A name="Toggle_Code_Unit_Search_Direction"></A>Search Direction</H3>@@ -548,8 +550,8 @@navigation history stack. The navigation history feature allows the user to revisitprevious locations.</P>- <H3><A name="History_Buffer"></A> <A name="Next_in_History_Buffer"></A> <A name=- "Previous_in_History_Buffer"></A> Go To Next/Previous Location</H3>+ <H3><A name="Next_Location_in_History"></A> <A name=+ "Previous_Location_in_History"></A> Go To Next/Previous Location</H3><BLOCKQUOTE><P>To traverse the history stack:</P>@@ -586,6 +588,29 @@button is only enabled after performing a <IMG src="images/right.png" border="0"></P></BLOCKQUOTE>+ <H3><A name="Next_Function_in_History"></A> <A name=+ "Previous_Function_in_History"></A> Go To Next/Previous <b>Function</b> in History</H3>++ <BLOCKQUOTE>+ <P>+ These actions allow you to navigate to the next/previous functions in the history+ list, skipping over any locations that are not in functions or are in the current+ function.+ </P>++ <BLOCKQUOTE>+ <P>+ <IMG src="../../shared/note.png" border="0">The behavior of the previous action will+ vary slightly depending upon what component is focused. It is possible for a+ non-Listing view to be showing a function that is not the current function in the+ Listing. In this case, if the Listing has focus, then the previously visited function+ will be the navigation destination. <B>Alternatively, if a non-Listing widget+ (e.g., the Decompiler) has focus and is showing a function, then that function+ being displayed will be ignored when navigating to the previous function.</B>+ </P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>+<H3><A name="Clear_History_Buffer"></A>Clear History</H3><BLOCKQUOTE>diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NavigationHistoryPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NavigationHistoryPlugin.javaindex e3c5dbbfbe..e23d517263 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NavigationHistoryPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NavigationHistoryPlugin.java@@ -17,11 +17,10 @@ package ghidra.app.plugin.core.navigation;import java.util.*;-import ghidra.program.model.listing.Function;-import ghidra.program.model.listing.FunctionManager;-import ghidra.program.util.ProgramLocation;import org.jdom.Element;+import docking.ComponentProvider;+import docking.DockingWindowManager;import docking.tool.ToolConstants;import ghidra.app.CorePluginPackage;import ghidra.app.events.ProgramClosedPluginEvent;@@ -31,7 +30,9 @@ import ghidra.app.services.*;import ghidra.framework.options.*;import ghidra.framework.plugintool.*;import ghidra.framework.plugintool.util.PluginStatus;-import ghidra.program.model.listing.Program;+import ghidra.program.model.address.Address;+import ghidra.program.model.listing.*;+import ghidra.program.util.ProgramLocation;import ghidra.util.Msg;import ghidra.util.bean.opteditor.OptionsVetoException;@@ -84,15 +85,9 @@ public class NavigationHistoryPlugin extends Pluginprivate SaveState dataSaveState;- /**- * Creates a new instance of the <CODE>ToolStateHistoryPlugin</CODE>,- * passing it the session and plugin tool that it is in.- * <P>- */- public NavigationHistoryPlugin(PluginTool plugintool) {- super(plugintool);-- plugintool.getOptions(ToolConstants.TOOL_OPTIONS);+ public NavigationHistoryPlugin(PluginTool tool) {+ super(tool);+ tool.getOptions(ToolConstants.TOOL_OPTIONS);}@Override@@ -210,86 +205,38 @@ public class NavigationHistoryPlugin extends PluginsaveLocation(i, saveState, location);}}- /**- * Positions the "current" location to the next location which is in a different function- * from current one or previous non-code location.- * If we are not inside any function, performs like "next".- * @param navigatable the Navigatable we perform history looking up in- */+@Overridepublic void nextFunction(Navigatable navigatable) {- HistoryList historyList = historyListMap.get(navigatable);- if (historyList == null) {- return;- }- ProgramLocation current = navigatable.getLocation();- Program program = current.getProgram();- FunctionManager functionManager = program.getFunctionManager();- Function currentFunction = functionManager.getFunctionContaining(current.getByteAddress());- LocationMemento memento = historyList.nextFunction(functionManager, currentFunction, true);- if (memento != null) {+ if (hasNextFunction(navigatable)) {+ HistoryList historyList = historyListMap.get(navigatable);+ LocationMemento memento = historyList.nextFunction(navigatable, true);navigate(navigatable, memento);}}- /**- * Positions the "current" location to the next location which is in a different function- * from current one or previous non-code location.- * If we are not inside any function, performs like "previous".- * @param navigatable the Navigatable we perform history looking up in- */@Overridepublic void previousFunction(Navigatable navigatable) {- HistoryList historyList = historyListMap.get(navigatable);- if (historyList == null) {- return;- }- addCurrentLocationToHistoryIfAppropriate(navigatable, historyList.getCurrentLocation());-- ProgramLocation current = navigatable.getLocation();- Program program = current.getProgram();- FunctionManager functionManager = program.getFunctionManager();- Function currentFunction = functionManager.getFunctionContaining(current.getByteAddress());- LocationMemento memento = historyList.previousFunction(functionManager, currentFunction, true);- if (memento != null) {+ if (hasPreviousFunction(navigatable)) {+ HistoryList historyList = historyListMap.get(navigatable);+ addCurrentLocationToHistoryIfAppropriate(navigatable, historyList.getCurrentLocation());+ LocationMemento memento = historyList.previousFunction(navigatable, true);navigate(navigatable, memento);}}@Overridepublic boolean hasNextFunction(Navigatable navigatable) {- return hasNextPreviousFunction(navigatable, true);+ HistoryList historyList = historyListMap.get(navigatable);+ return historyList != null && historyList.hasNextFunction(navigatable);}@Overridepublic boolean hasPreviousFunction(Navigatable navigatable) {- return hasNextPreviousFunction(navigatable, false);- }-- private boolean hasNextPreviousFunction(Navigatable navigatable, boolean isNext) {HistoryList historyList = historyListMap.get(navigatable);- if (historyList == null) {- return false;- }-- ProgramLocation current = navigatable.getLocation();- Program program = current.getProgram();- FunctionManager functionManager = program.getFunctionManager();- Function currentFunction = functionManager.getFunctionContaining(current.getByteAddress());- if (isNext && historyList.nextFunction(functionManager, currentFunction, false) != null) {- return true;- } else if (isNext) {- return false;- } else {- return historyList.previousFunction(functionManager, currentFunction, false) != null;- }+ return historyList != null && historyList.hasPreviousFunction(navigatable);}- /**- * Positions the "current" view to the next view in the history list and- * generates a "HistoryChangedEvent". If there is no "next" view, the- * history list remains unchanged.- */@Overridepublic void next(Navigatable navigatable) {if (hasNext(navigatable)) {@@ -299,11 +246,6 @@ public class NavigationHistoryPlugin extends Plugin}}- /**- * Positions the "current" view to the previous view in the history list and- * generates a "HistoryChangeEvent". If there is no "previous" view,- * the history list remains unchanged.- */@Overridepublic void previous(Navigatable navigatable) {if (hasPrevious(navigatable)) {@@ -339,6 +281,10 @@ public class NavigationHistoryPlugin extends Plugin}private void navigate(Navigatable navigatable, LocationMemento memento) {+ if (memento == null) {+ return;+ }+navigatable.goTo(memento.getProgram(), memento.getProgramLocation());navigatable.setMemento(memento);if (navigatable.isVisible()) {@@ -386,28 +332,18 @@ public class NavigationHistoryPlugin extends Pluginreturn previousLocations;}- /**- * Returns true if there is a valid "next" view in the history list.- */@Overridepublic boolean hasNext(Navigatable navigatable) {HistoryList historyList = historyListMap.get(navigatable);return historyList != null && historyList.hasNext();}- /**- * Returns true if there is a valid "previous" view in the history list.- */@Overridepublic boolean hasPrevious(Navigatable navigatable) {HistoryList historyList = historyListMap.get(navigatable);return historyList != null && historyList.hasPrevious();}- /**- * Removes all views from the history list and fires a change event. If the- * history list is already empty, then nothing happens.- */@Overridepublic void clear(Navigatable navigatable) {historyListMap.remove(navigatable);@@ -430,10 +366,6 @@ public class NavigationHistoryPlugin extends Plugin}}- /**- * Fires off a <CODE>HistoryChangePluginEvent</CODE>.- * <P>- */private void notifyHistoryChange() {tool.contextChanged(null);}@@ -493,13 +425,14 @@ public class NavigationHistoryPlugin extends Pluginif (mementoElement == null) {return null;}+SaveState mementoState = new SaveState(mementoElement);LocationMemento locationMemento = null;try {locationMemento = LocationMemento.getLocationMemento(mementoState, programs);}catch (IllegalArgumentException iae) {- Msg.debug(this, "Unable to restore LocationMemento: " + iae.getMessage());+ Msg.debug(this, "Unable to restore LocationMemento: " + iae.getMessage(), iae);}return locationMemento;}@@ -513,11 +446,11 @@ public class NavigationHistoryPlugin extends Pluginprivate int currentLocation = 0;private int maxLocations;- public HistoryList(int maxLocations) {+ HistoryList(int maxLocations) {this.maxLocations = maxLocations;}- public int getCurrentLocationIndex() {+ int getCurrentLocationIndex() {return currentLocation;}@@ -527,20 +460,15 @@ public class NavigationHistoryPlugin extends Plugin}}- void clear() {- list.clear();- currentLocation = 0;- }-- public int size() {+ int size() {return list.size();}- public LocationMemento getLocation(int index) {+ LocationMemento getLocation(int index) {return list.get(index);}- public LocationMemento getCurrentLocation() {+ LocationMemento getCurrentLocation() {return list.get(currentLocation);}@@ -550,6 +478,7 @@ public class NavigationHistoryPlugin extends PlugincurrentLocation = 0;return;}+while (list.size() - 1 > currentLocation) {list.remove(list.size() - 1);}@@ -562,11 +491,11 @@ public class NavigationHistoryPlugin extends Plugin// same location, but maybe different "extra" info replace equivalent locationlist.set(list.size() - 1, newValue);}+if (list.size() > maxLocations) {list.remove(0);}currentLocation = list.size() - 1;-}void setMaxLocations(int maxLocations) {@@ -603,29 +532,41 @@ public class NavigationHistoryPlugin extends Pluginreturn null;}+ boolean hasNextFunction(Navigatable navigatable) {+ return nextFunction(navigatable, false) != null;+ }++ boolean hasPreviousFunction(Navigatable navigatable) {+ return previousFunction(navigatable, false) != null;+ }+/**- * find next history LocationMemento that contains a different function. If current is not a function,- * go to next function found. If such- * LocationMemento not found, null is returned- * @param functionManager function manager that has all function information- * @param currentFunction current function that we are at- * @param moveTo true means after finding, get current location to it. false to just find and do nothing- * @return found next LocationMemento, or null if not found+ * Find the next history LocationMemento that contains a different function. If no such+ * LocationMemento is found, null is returned.+ *+ * @param navigatable the navigatable being navigated+ * @param moveTo true means after finding, get current location to it. false to just find+ * and do nothing+ * @return next LocationMemento, or null if no next function*/- LocationMemento nextFunction(FunctionManager functionManager, Function currentFunction, boolean moveTo) {+ private LocationMemento nextFunction(Navigatable navigatable, boolean moveTo) {+if (list.isEmpty()) {return null;}- for (int i = currentLocation + 1; i < list.size(); ++i) {+ ProgramLocation location = navigatable.getLocation();+ Program program = location.getProgram();+ FunctionManager functionManager = program.getFunctionManager();+ Function currentFunction =+ functionManager.getFunctionContaining(location.getAddress());++ for (int i = currentLocation + 1; i < list.size(); i++) {LocationMemento memento = list.get(i);- Function historyFunction = functionManager.getFunctionContaining(memento.getProgramLocation().getByteAddress());- if (currentFunction != null && historyFunction != null && historyFunction != currentFunction) {- if (moveTo) {- currentLocation = i;- }- return memento;- } else if (currentFunction == null && historyFunction != null) {+ ProgramLocation otherLocation = memento.getProgramLocation();+ Address address = otherLocation.getAddress();+ Function historyFunction = functionManager.getFunctionContaining(address);+ if (historyFunction != null && !historyFunction.equals(currentFunction)) {if (moveTo) {currentLocation = i;}@@ -637,36 +578,66 @@ public class NavigationHistoryPlugin extends Plugin}/**- * find previous history LocationMemento that contains a different function. If such- * LocationMemento not found, null is returned- * @param functionManager function manager that has all function information- * @param currentFunction current function that we are at- * @param moveTo true means after finding, get current location to it. false to just find and do nothing- * @return found next LocationMemento, or null if not found+ * Find the previous history LocationMemento that contains a different function. If no such+ * LocationMemento is found, null is returned.+ *+ * @param navigatable the navigatable being navigated+ * @param moveTo true means after finding, get current location to it. false to just find+ * and do nothing+ * @return previous LocationMemento, or null if no previous function found*/- LocationMemento previousFunction(FunctionManager functionManager, Function currentFunction, boolean moveTo) {+ private LocationMemento previousFunction(Navigatable navigatable, boolean moveTo) {if (list.isEmpty()) {return null;}- for (int i = currentLocation - 1; i >= 0; --i) {+ Function startFunction = getPreviousStartFunction(navigatable);+ ProgramLocation location = navigatable.getLocation();+ Program program = location.getProgram();+ FunctionManager functionManager = program.getFunctionManager();+ for (int i = currentLocation - 1; i >= 0; i--) {+LocationMemento memento = list.get(i);- Function historyFunction = functionManager.getFunctionContaining(memento.getProgramLocation().getByteAddress());- if (historyFunction != null && currentFunction != null && historyFunction != currentFunction) {+ ProgramLocation otherLocation = memento.getProgramLocation();+ Address address = otherLocation.getAddress();+ Function historyFunction = functionManager.getFunctionContaining(address);++ if (historyFunction != null && !historyFunction.equals(startFunction)) {if (moveTo) {currentLocation = i;}return memento;- } else if (currentFunction == null && historyFunction != null) {- // user navigates to this non-function location from a function, and usually wants to go to last- // function instead of that one as move here may not change decompiler view.- currentFunction = historyFunction;}}return null;}+ private Function getPreviousStartFunction(Navigatable navigatable) {+ ProgramLocation location = navigatable.getLocation();+ Address currentAddress = location.getAddress();++ //+ // The active component may still be showing the previously loaded function, instead+ // of the current location when that location is not in a function. In that case,+ // when that provider is focused, prefer its notion of the current function so that+ // users navigating from that view will go to the function before the one that is+ // on the history stack. This should feel more intuitive to the user, with the risk+ // that the navigation actions will sometimes feel inconsistent, depending upon+ // what view is focused.+ //+ DockingWindowManager manager = DockingWindowManager.getActiveInstance();+ ComponentProvider provider = manager.getActiveComponentProvider();+ if (provider instanceof Navigatable) {+ LocationMemento memento = ((Navigatable) provider).getMemento();+ currentAddress = memento.getProgramLocation().getAddress();+ }++ Program program = location.getProgram();+ FunctionManager functionManager = program.getFunctionManager();+ return functionManager.getFunctionContaining(currentAddress);+ }+void remove(LocationMemento location) {for (int i = 0; i < list.size(); i++) {LocationMemento loc = list.get(i);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevAddressPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevAddressPlugin.javaindex 9ef3f611ab..3bc359382a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevAddressPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevAddressPlugin.java@@ -47,8 +47,7 @@ import resources.ResourceManager;/*** <CODE>NextPrevAddressPlugin</CODE> allows the user to go back and forth in- * the history list and to clear it.- *+ * the history list and to clear it*///@formatter:off@PluginInfo(@@ -61,66 +60,60 @@ import resources.ResourceManager;)//@formatter:onpublic class NextPrevAddressPlugin extends Plugin {- private static final String NAV_GROUP = "GoTo";+ private static final String HISTORY_GROUP = "1_History";private static ImageIcon previousIcon = ResourceManager.loadImage("images/left.png");private static ImageIcon nextIcon = ResourceManager.loadImage("images/right.png");- private static final String PREVIOUS_ACTION_NAME = "Previous in History Buffer";- private static final String NEXT_ACTION_NAME = "Next in History Buffer";- private static final String PREVIOUS_FUNCTION_ACTION_NAME = "Previous Function in History Buffer";- private static final String NEXT_FUNCTION_ACTION_NAME = "Next Function in History Buffer";+ private static final String PREVIOUS_ACTION_NAME = "Previous Location in History";+ private static final String NEXT_ACTION_NAME = "Next Location in History";+ private static final String PREVIOUS_FUNCTION_ACTION_NAME =+ "Previous Function in History";+ private static final String NEXT_FUNCTION_ACTION_NAME = "Next Function in History";private static final String[] CLEAR_MENUPATH = { "Navigation", "Clear History" };private NavigationHistoryService historyService;private MultiActionDockingAction nextAction;private MultiActionDockingAction previousAction;- private MultiActionDockingAction nextFunctionAction;- private MultiActionDockingAction previousFunctionAction;+ private DockingAction nextFunctionAction;+ private DockingAction previousFunctionAction;private DockingAction clearAction;private BrowserCodeUnitFormat codeUnitFormatter;- //////////////////////////////////////////////////////////////////////- // //- // Constructor //- // //- //////////////////////////////////////////////////////////////////////-/**- * Creates a new instance of the plugin.- * <P>- * @param session the session the plugin will be operating in.- * @param plugintool the tool the plugin will be operating in.+ * Creates a new instance of the plugin+ *+ * @param tool the tool*/- public NextPrevAddressPlugin(PluginTool plugintool) {- super(plugintool);-- // no events produced- // no services provided- // will acquire history service in init()- // no focused context+ public NextPrevAddressPlugin(PluginTool tool) {+ super(tool);createActions();}- //////////////////////////////////////////////////////////////////////- // //- // overridden Plugin methods //- // //- //////////////////////////////////////////////////////////////////////-- /**- * Obtains a handle to the address history list service.- */@Overrideprotected void init() {historyService = tool.getService(NavigationHistoryService.class);codeUnitFormatter = new BrowserCodeUnitFormat(tool);}- //////////////////////////////////////////////////////////////////////- // //- // private methods //- // //- //////////////////////////////////////////////////////////////////////+ MultiActionDockingAction getPreviousAction() {+ return previousAction;+ }++ MultiActionDockingAction getNextAction() {+ return nextAction;+ }++ DockingAction getPreviousFunctionAction() {+ return previousFunctionAction;+ }++ DockingAction getNextFunctionAction() {+ return nextFunctionAction;+ }++//==================================================================================================+// Private Methods+//==================================================================================================private List<DockingActionIf> getPreviousActions(Navigatable navigatable) {Program lastProgram = null;@@ -176,8 +169,10 @@ public class NextPrevAddressPlugin extends Plugin {private void createActions() {nextAction = new NextPreviousAction(NEXT_ACTION_NAME, getName(), true);previousAction = new NextPreviousAction(PREVIOUS_ACTION_NAME, getName(), false);- nextFunctionAction = new NextPreviousFunctionAction(NEXT_FUNCTION_ACTION_NAME, getName(), true);- previousFunctionAction = new NextPreviousFunctionAction(PREVIOUS_FUNCTION_ACTION_NAME, getName(), false);+ nextFunctionAction =+ new NextPreviousFunctionAction(NEXT_FUNCTION_ACTION_NAME, getName(), true);+ previousFunctionAction =+ new NextPreviousFunctionAction(PREVIOUS_FUNCTION_ACTION_NAME, getName(), false);clearAction = new DockingAction("Clear History Buffer", getName()) {@Override@@ -208,7 +203,7 @@ public class NextPrevAddressPlugin extends Plugin {}};clearAction.setHelpLocation(new HelpLocation(HelpTopics.NAVIGATION, clearAction.getName()));- MenuData menuData = new MenuData(CLEAR_MENUPATH, NAV_GROUP);+ MenuData menuData = new MenuData(CLEAR_MENUPATH, HISTORY_GROUP);menuData.setMenuSubGroup("1"); // first in menu!clearAction.setMenuBarData(menuData);@@ -233,7 +228,7 @@ public class NextPrevAddressPlugin extends Plugin {// Display Format: "Address\t(FunctionName+Offset)\tLabel|Instruction"// where each tab character is a delimiter to separate columns- StringBuffer buffy = new StringBuffer();+ StringBuilder buffy = new StringBuilder();buffy.append(address.toString()).append('\t');// in a function?@@ -310,7 +305,8 @@ public class NextPrevAddressPlugin extends Plugin {NextPreviousAction(String name, String owner, boolean isNext) {super(name, owner);this.isNext = isNext;- setToolBarData(new ToolBarData(isNext ? nextIcon : previousIcon, NAV_GROUP));++ setToolBarData(new ToolBarData(isNext ? nextIcon : previousIcon, HISTORY_GROUP));setHelpLocation(new HelpLocation(HelpTopics.NAVIGATION, name));int keycode = isNext ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT;setKeyBindingData(new KeyBindingData(keycode, InputEvent.ALT_DOWN_MASK));@@ -388,6 +384,7 @@ public class NextPrevAddressPlugin extends Plugin {this.navigatable = navigatable;Icon navIcon = navigatable.getNavigatableIcon();+ setHelpLocation(new HelpLocation(HelpTopics.NAVIGATION, "Navigation_History"));setMenuBarData(new MenuData(new String[] { buildActionName(location, formatter) }, navIcon));setEnabled(true);@@ -404,22 +401,36 @@ public class NextPrevAddressPlugin extends Plugin {}}- private class NextPreviousFunctionAction extends MultiActionDockingAction {+ private class NextPreviousFunctionAction extends DockingAction {private final boolean isNext;NextPreviousFunctionAction(String name, String owner, boolean isNext) {super(name, owner);this.isNext = isNext;+setHelpLocation(new HelpLocation(HelpTopics.NAVIGATION, name));int keycode = isNext ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT;- setKeyBindingData(new KeyBindingData(keycode, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));- setDescription(isNext ? "Go to next function location" : "Go to previous function location");+ setKeyBindingData(+ new KeyBindingData(keycode, InputEvent.ALT_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));+ setDescription(+ isNext ? "Go to next function location" : "Go to previous function location");++ String menuItemName = isNext ? "Next History Function" : "Previous History Function";+ MenuData menuData =+ new MenuData(new String[] { "Navigation", menuItemName }, HISTORY_GROUP);+ menuData.setMenuSubGroup("2"); // after clear+ setMenuBarData(menuData);}@Overridepublic boolean isValidContext(ActionContext context) {- return (context instanceof NavigatableActionContext);+ return false;+ }++ @Override+ public boolean isValidGlobalContext(ActionContext globalContext) {+ return (globalContext instanceof NavigatableActionContext);}@Override@@ -454,16 +465,6 @@ public class NextPrevAddressPlugin extends Plugin {}return false;}-- @Override- public List<DockingActionIf> getActionList(ActionContext context) {- Navigatable navigatable = getNavigatable(context);- if (isNext) {- return getNextActions(navigatable);- }- return getPreviousActions(navigatable);- }-}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevCodeUnitPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevCodeUnitPlugin.javaindex b8e936e27d..b4fdfb8c10 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevCodeUnitPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/NextPrevCodeUnitPlugin.java@@ -15,6 +15,13 @@*/package ghidra.app.plugin.core.navigation;+import java.awt.event.InputEvent;+import java.awt.event.KeyEvent;++import javax.swing.Icon;++import docking.action.*;+import docking.tool.ToolConstants;import ghidra.app.CorePluginPackage;import ghidra.app.plugin.PluginCategoryNames;import ghidra.app.plugin.core.codebrowser.CodeViewerActionContext;@@ -22,17 +29,9 @@ import ghidra.app.plugin.core.codebrowser.actions.CodeViewerContextAction;import ghidra.app.services.GoToService;import ghidra.app.util.HelpTopics;import ghidra.framework.plugintool.*;-import ghidra.framework.plugintool.util.*;+import ghidra.framework.plugintool.util.PluginStatus;import ghidra.util.HelpLocation;--import java.awt.event.InputEvent;-import java.awt.event.KeyEvent;--import javax.swing.Icon;-import resources.ResourceManager;-import docking.action.*;-import docking.tool.ToolConstants;/*** The NextPrevCodeUnitPlugin generates a GoTo event based on where the cursor@@ -45,7 +44,6 @@ import docking.tool.ToolConstants;* <LI>Next-Previous Non-Function* <LI>Next-Previous Label* <LI>Next-Previous Bookmark- * <LI>Next-Previous Marker* </UL>*///@formatter:off@@ -67,22 +65,13 @@ public class NextPrevCodeUnitPlugin extends Plugin {private AbstractNextPreviousAction nonFunctionAction;private AbstractNextPreviousAction labelAction;private NextPreviousBookmarkAction bookmarkAction;- private NextPreviousMarkerAction markerAction;private NextPreviousDifferentByteAction differentValueAction;- /**- * Constructor for NextCodeUnitPlugin- * @param plugintool the tool the plugin will be operating in- */- public NextPrevCodeUnitPlugin(PluginTool pluginTool) {- super(pluginTool);+ public NextPrevCodeUnitPlugin(PluginTool tool) {+ super(tool);createActions();}- /**- * Create menu for finding next/previous instruction, defined data, and- * undefined data actions.- */private void createActions() {// use this index to make sure that the following actions are ordered in the way that// they are inserted@@ -117,15 +106,8 @@ public class NextPrevCodeUnitPlugin extends Plugin {bookmarkAction = new NextPreviousBookmarkAction(tool, getName(), "" + subGroupIndex++);tool.addAction(bookmarkAction);--/* markerAction = new NextPreviousMarkerAction(tool, getName(), "" + subGroupIndex++);- tool.addAction(markerAction);*/}- /**- * Set next/previous in action menu item names and action tooltips given- * code unit direction toggle.- */private void updateActions(boolean searchForward) {instructionAction.setDirection(searchForward);dataAction.setDirection(searchForward);@@ -135,7 +117,6 @@ public class NextPrevCodeUnitPlugin extends Plugin {labelAction.setDirection(searchForward);differentValueAction.setDirection(searchForward);bookmarkAction.setDirection(searchForward);- // markerAction.setDirection(searchForward);}private class ToggleDirectionAction extends CodeViewerContextAction {@@ -144,7 +125,7 @@ public class NextPrevCodeUnitPlugin extends Plugin {private boolean isForward = true;ToggleDirectionAction(String subGroup) {- super("Toggle Code Unit Search Direction", NextPrevCodeUnitPlugin.this.getName());+ super("Toggle Search Direction", NextPrevCodeUnitPlugin.this.getName());MenuData menuData =new MenuData(new String[] { ToolConstants.MENU_NAVIGATION, getName() },forwardIcon, ToolConstants.NEXT_CODE_UNIT_NAVIGATION_MENU_GROUP);@@ -155,8 +136,10 @@ public class NextPrevCodeUnitPlugin extends Plugin {setKeyBindingData(new KeyBindingData(KeyEvent.VK_T, InputEvent.CTRL_DOWN_MASK |InputEvent.ALT_DOWN_MASK));- setHelpLocation(new HelpLocation(HelpTopics.NAVIGATION, getName()));- setDescription("Toggle Code Unit Search Direction");+ String longName = "Toggle Code Unit Search Direction";+ setHelpLocation(+ new HelpLocation(HelpTopics.NAVIGATION, longName));+ setDescription(longName);}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/services/NavigationHistoryService.java b/Ghidra/Features/Base/src/main/java/ghidra/app/services/NavigationHistoryService.javaindex 281af2eb2c..969c93af07 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/services/NavigationHistoryService.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/services/NavigationHistoryService.java@@ -15,13 +15,13 @@*/package ghidra.app.services;+import java.util.List;+import ghidra.app.nav.LocationMemento;import ghidra.app.nav.Navigatable;import ghidra.app.plugin.core.navigation.NavigationHistoryPlugin;import ghidra.framework.plugintool.ServiceInfo;-import java.util.List;-/*** The ToolStateHistoryService maintains a stack of locations that the user* has visited via a navigation plugin.@@ -30,93 +30,112 @@ import java.util.List;@ServiceInfo(defaultProvider = NavigationHistoryPlugin.class, description = "Maintains a history of tool states")public interface NavigationHistoryService {- /**- * Positions the "current" location to the next location which is in a different function- * from current one or previous non-code location.- * If we are not inside any function, performs like "next".- * @param navigatable the Navigatable we perform history looking up in- */- void nextFunction(Navigatable navigatable);-- /**- * Positions the "previous" location to the next location which is in a different function- * from current one or previous non-code location.- * If we are not inside any function, performs like "next".- * @param navigatable the Navigatable we perform history looking up in- */- void previousFunction(Navigatable navigatable);-- /**- * Positions the "current" location to the next location in the history list.- * If there is no "next" location, the history list remains unchanged.- */- void next(Navigatable navigatable);-- /**- * Positions the "current" location to the previous location in the history list.- * If there is no "previous" location, the- * history list remains unchanged.- */- void previous(Navigatable navigatable);-- /**- * Navigates to the given location in the "next" list. If the location is not in the list, then- * nothing will happen.- * @param location The location within the "next" list to which to go.- */- public void next(Navigatable navigatable, LocationMemento location );-- /**- * Navigates to the given location in the "previous" list. If the location is not in- * the list, then nothing will happen.- * @param location The location within the "previous" list to which to go.- */- public void previous(Navigatable navigatable, LocationMemento location );-- /**- * Returns the LocationMemento objects in the "previous" list.- * @return the LocationMemento objects in the "previous" list.- */- public List<LocationMemento> getPreviousLocations(Navigatable navigatable);-- /**- * Returns the LocationMemento objects in the "next" list.- * @return the LocationMemento objects in the "next" list.- */- public List<LocationMemento> getNextLocations(Navigatable navigatable);-- /**- * Returns true if there is a valid "next" location in the history list.- */- boolean hasNext(Navigatable navigatable);-- /**- * tells if there is a valid "next" function location in the history list- * @param navigatable Navigatable object we are looking at- * @return true if there is a valid "next" function location in the history list- */- boolean hasNextFunction(Navigatable navigatable);-- /**- * tells if there is a valid "next" function location in the history list- * @param navigatable Navigatable object we are looking at- * @return true if there is a valid "next" function location in the history list- */- boolean hasPreviousFunction(Navigatable navigatable);-- /**- * Returns true if there is a valid "previous" location in the history list.- */- boolean hasPrevious(Navigatable navigatable);-- /**- * Adds the given locationMomento to the list of previous locations. Clears the list- * of next locations.- */- void addNewLocation(Navigatable navigatable);-- /**- * Removes all visited locations from the history list.- */- void clear(Navigatable navigatable);+ /**+ * Positions the current location to the next location in the history list.+ * If there is no "next" location, the history list remains unchanged.+ * @param navigatable the navigatable to be navigated+ */+ public void next(Navigatable navigatable);++ /**+ * Positions the "current" location to the previous location in the history list.+ * If there is no "previous" location, the history list remains unchanged.+ * @param navigatable the navigatable to be navigated+ */+ public void previous(Navigatable navigatable);++ /**+ * Navigates to the given location in the "next" list. If the location is not in the list, then+ * nothing will happen.+ *+ * @param navigatable the navigatable to be navigated+ * @param location The location within the "next" list to which to go+ */+ public void next(Navigatable navigatable, LocationMemento location);++ /**+ * Navigates to the given location in the "previous" list. If the location is not in+ * the list, then nothing will happen+ *+ * @param navigatable the navigatable to be navigated+ * @param location The location within the "previous" list to which to go.+ */+ public void previous(Navigatable navigatable, LocationMemento location);++ /**+ * Positions the "current" location to the next location which is in a different function+ * from current one or previous non-code location.+ * If we are not inside any function, performs like "next".+ * @param navigatable the navigatable to be navigated+ */+ public void nextFunction(Navigatable navigatable);++ /**+ * Positions the "previous" location to the next location which is in a different function+ * from current one or previous non-code location.+ * If we are not inside any function, performs like "next".+ * @param navigatable the navigatable to be navigated+ */+ public void previousFunction(Navigatable navigatable);++ /**+ * Returns the LocationMemento objects in the "previous" list+ *+ * @param navigatable the navigatable to be navigated+ * @return the LocationMemento objects in the "previous" list+ */+ public List<LocationMemento> getPreviousLocations(Navigatable navigatable);++ /**+ * Returns the LocationMemento objects in the "next" list+ *+ * @param navigatable the navigatable to be navigated+ * @return the LocationMemento objects in the "next" list+ */+ public List<LocationMemento> getNextLocations(Navigatable navigatable);++ /**+ * Returns true if there is a valid "next" location in the history list.+ *+ * @param navigatable the navigatable to be navigated+ * @return true if there is a "next" location+ */+ public boolean hasNext(Navigatable navigatable);++ /**+ * Returns true if there is a valid "previous" location in the history list+ *+ * @param navigatable the navigatable to be navigated+ * @return true if there is a "previous" location+ */+ public boolean hasPrevious(Navigatable navigatable);++ /**+ * Returns true if there is a valid "next" function location in the history list+ * @param navigatable Navigatable object we are looking at+ * @return true if there is a valid "next" function location+ */+ public boolean hasNextFunction(Navigatable navigatable);++ /**+ * Returns true if there is a valid "previous" function location in the history list+ * @param navigatable Navigatable object we are looking at+ * @return true if there is a valid "previous" function location+ */+ public boolean hasPreviousFunction(Navigatable navigatable);++ /**+ * Adds the given locationMomento to the list of previous locations. Clears the list+ * of next locations.+ *+ * @param navigatable the navigatable to be navigated+ */+ public void addNewLocation(Navigatable navigatable);++ /**+ * Removes all visited locations from the history list+ *+ * @param navigatable the navigatable to be navigated+ */+ public void clear(Navigatable navigatable);}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.javaindex 143c2d8b1a..16b2070549 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractGhidraHeadlessIntegrationTest.java@@ -27,6 +27,7 @@ import ghidra.app.events.ProgramLocationPluginEvent;import ghidra.app.events.ProgramSelectionPluginEvent;import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;import ghidra.app.script.GhidraScriptConstants;+import ghidra.app.services.GoToService;import ghidra.framework.*;import ghidra.framework.cmd.Command;import ghidra.framework.model.UndoableDomainObject;@@ -383,6 +384,14 @@ public abstract class AbstractGhidraHeadlessIntegrationTest extends AbstractDock}public void goTo(PluginTool tool, Program p, Address addr) {++ GoToService goTo = tool.getService(GoToService.class);+ if (goTo != null) {+ goTo.goTo(addr);+ waitForSwing();+ return;+ }+tool.firePluginEvent(new ProgramLocationPluginEvent("Test", new ProgramLocation(p, addr), p));waitForSwing();diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/NextPrevAddressPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/NextPrevAddressPluginTest.javaindex 426f3e3e71..a702c859b5 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/NextPrevAddressPluginTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/NextPrevAddressPluginTest.java@@ -33,10 +33,9 @@ import generic.test.TestUtils;import ghidra.app.nav.LocationMemento;import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;import ghidra.app.plugin.core.codebrowser.CodeViewerProvider;-import ghidra.app.util.navigation.GoToAddressLabelDialog;+import ghidra.app.services.GoToService;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;-import ghidra.program.model.address.AddressFactory;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.Memory;import ghidra.program.model.symbol.Symbol;@@ -47,37 +46,34 @@ import ghidra.test.*;public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTest {private TestEnv env;- private Program baseProgram;+ private Program program;private PluginTool tool;private MultiActionDockingAction previousAction;private MultiActionDockingAction nextAction;- private GoToAddressLabelDialog dialog;- private CodeBrowserPlugin codeBrowserPlugin;+ private DockingAction previousFunctionAction;+ private DockingAction nextFunctionAction;+ private CodeBrowserPlugin cbPlugin;private CodeViewerProvider provider;- private AddressFactory addressFactory;@Beforepublic void setUp() throws Exception {env = new TestEnv();ClassicSampleX86ProgramBuilder builder = new ClassicSampleX86ProgramBuilder();- baseProgram = builder.getProgram();+ program = builder.getProgram();- addressFactory = baseProgram.getAddressFactory();- tool = env.launchDefaultTool(baseProgram);+ tool = env.launchDefaultTool(program);builder.dispose();NextPrevAddressPlugin plugin = env.getPlugin(NextPrevAddressPlugin.class);- previousAction =- (MultiActionDockingAction) TestUtils.getInstanceField("previousAction", plugin);- nextAction = (MultiActionDockingAction) TestUtils.getInstanceField("nextAction", plugin);+ previousAction = plugin.getPreviousAction();+ nextAction = plugin.getNextAction();+ previousFunctionAction = plugin.getPreviousFunctionAction();+ nextFunctionAction = plugin.getNextFunctionAction();- GoToAddressLabelPlugin goToPlugin = env.getPlugin(GoToAddressLabelPlugin.class);- dialog = goToPlugin.getDialog();-- codeBrowserPlugin = env.getPlugin(CodeBrowserPlugin.class);- provider = codeBrowserPlugin.getProvider();+ cbPlugin = env.getPlugin(CodeBrowserPlugin.class);+ provider = cbPlugin.getProvider();}@After@@ -85,43 +81,6 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTeenv.dispose();}- @Test- public void testBackwardAndForward() throws Exception {- // disabled by default- assertTrue(!nextAction.isEnabledForContext(provider.getActionContext(null)));- assertTrue(!previousAction.isEnabledForContext(provider.getActionContext(null)));-- // perform a goto- Address startAddress = codeBrowserPlugin.getCurrentAddress();- goTo("010018a0");- codeBrowserPlugin.updateNow();- waitForPostedSwingRunnables();-- // go backward generically- backwardByAction();- codeBrowserPlugin.updateNow();- waitForPostedSwingRunnables();- assertEquals(startAddress, codeBrowserPlugin.getCurrentAddress());-- // go forward generically- forwardByAction();- codeBrowserPlugin.updateNow();- waitForPostedSwingRunnables();- assertEquals(addr("0x010018a0"), codeBrowserPlugin.getCurrentAddress());-- // go backward from the dropdown- backwardByDropdown();- codeBrowserPlugin.updateNow();- waitForPostedSwingRunnables();- assertEquals(startAddress, codeBrowserPlugin.getCurrentAddress());-- // go forward from the dropdown- forwardByDrowdown();- codeBrowserPlugin.updateNow();- waitForPostedSwingRunnables();- assertEquals(addr("0x010018a0"), codeBrowserPlugin.getCurrentAddress());- }-@Testpublic void testNavigationFromMiddleOfPulldownAction() throws Exception {List<Symbol> navigatedSymbols = doBulkGoTo();@@ -132,7 +91,7 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTe// the previous list of actions should be the size of the list - 1List<DockingActionIf> actionList =- previousAction.getActionList(provider.getActionContext(null));+ previousAction.getActionList(getContext());// verify the size...// (the navigated symbols plus the original location before we started)@@ -159,10 +118,10 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTeSymbol navigatedSymbol = navigatedSymbols.get(navigatedIndex);performAction(action, true);- assertEquals(navigatedSymbol.getAddress(), codeBrowserPlugin.getCurrentAddress());+ assertEquals(navigatedSymbol.getAddress(), currentAddress());// ...make sure the 'previous' list is updated with only actions after the one in our list- actionList = previousAction.getActionList(provider.getActionContext(null));+ actionList = previousAction.getActionList(getContext());n = actionList.size() - 1; // don't compare the first locationint navigatedIndexOffset = (navigatedIndex + 1); // don't count the navigated indexint remainingSymbols = navigatedSymbols.size() - navigatedIndexOffset;@@ -181,7 +140,7 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTe// ...make sure the next list is updated with the correct items from after that position// in the list- actionList = nextAction.getActionList(provider.getActionContext(null));+ actionList = nextAction.getActionList(getContext());List<Symbol> nextNavigatedSymbols = subList(navigatedSymbols, 0, navigatedIndex);nextNavigatedSymbols.add(0, bulkNavigationSymbol); // put on the original location from the bulk navigation@@ -206,10 +165,10 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTeperformAction(navigatedAction, true);- assertEquals(navigatedSymbol.getAddress(), codeBrowserPlugin.getCurrentAddress());+ assertEquals(navigatedSymbol.getAddress(), currentAddress());// ...make sure that the 'previous' list is properly updated- actionList = previousAction.getActionList(provider.getActionContext(null));+ actionList = previousAction.getActionList(getContext());n = actionList.size() - 1; // don't compare the first locationnavigatedIndexOffset = (navigatedIndex + 1); // don't count the navigated indexremainingSymbols = navigatedSymbols.size() - navigatedIndexOffset;@@ -227,7 +186,7 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTe}// ...make sure the 'next' list is properly updated- actionList = nextAction.getActionList(provider.getActionContext(null));+ actionList = nextAction.getActionList(getContext());nextNavigatedSymbols = subList(navigatedSymbols, 0, navigatedIndex);nextNavigatedSymbols.add(0, bulkNavigationSymbol); // put on the original location from the bulk navigation@@ -243,6 +202,129 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTe}}+ @Test+ public void testBackwardAndForward() throws Exception {+ // disabled by default; no history+ assertFalse(nextAction.isEnabledForContext(getContext()));+ assertFalse(previousAction.isEnabledForContext(getContext()));++ Address startAddress = currentAddress();+ Address secondAddress = addr("010018a0");+ goTo(secondAddress);++ previous();+ assertCurrentAddress(startAddress);++ next();+ assertCurrentAddress(secondAddress);++ // try the drop-down popup+ previousByDropdown();+ assertCurrentAddress(startAddress);++ nextByDrowdown();+ assertCurrentAddress(secondAddress);+ }++ @Test+ public void testFunctionNavigation_OnlyFunctionsInHistory() throws Exception {+ Address f1 = addr("01002cf5"); // ghidra+ Address f2 = addr("01006420"); // entry+ Address f3 = addr("0100415a"); // sscanf++ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);+ goTo(f1);+ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ goTo(f2);+ assertEnabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ goTo(f3);+ assertEnabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ previousFunction();+ assertCurrentAddress(f2);+ assertEnabled(previousFunctionAction);+ assertEnabled(nextFunctionAction);++ previousFunction();+ assertCurrentAddress(f1);+ assertDisabled(previousFunctionAction);+ assertEnabled(nextFunctionAction);++ nextFunction();+ assertCurrentAddress(f2);+ assertEnabled(previousFunctionAction);+ assertEnabled(nextFunctionAction);+ }++ @Test+ public void testFunctionNavigation_MixedHistory() throws Exception {+ Address f1 = addr("01002cf5"); // ghidra+ Address a1 = f1.add(1);+ Address a2 = f1.add(3);+ Address a3 = f1.add(8);+ Address f2 = addr("01006420"); // entry++ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);+ goTo(f1);+ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ goTo(a1);+ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);+ goTo(a2);+ goTo(a3);+ assertDisabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ // new function+ goTo(f2);+ assertEnabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);++ previousFunction();+ assertCurrentAddress(a3); // last location in a different function+ assertDisabled(previousFunctionAction);+ assertEnabled(nextFunctionAction);++ nextFunction();+ assertCurrentAddress(f2);+ assertEnabled(previousFunctionAction);+ assertDisabled(nextFunctionAction);+ }++//==================================================================================================+// Private Methods+//==================================================================================================++ private ComponentProvider showDecompiler() {+ ComponentProvider cp = tool.getComponentProvider("Decompiler");+ tool.showComponentProvider(cp, true);+ cp.requestFocus();+ return cp;+ }++ private void assertCurrentAddress(Address expected) {+ assertEquals(expected, currentAddress());+ }++ private void assertEnabled(DockingAction action) {+ assertTrue("Action should have been enabled: " + action.getName(),+ action.isEnabledForContext(getContext()));+ }++ private void assertDisabled(DockingAction action) {+ assertFalse("Action should have been disabled: " + action.getName(),+ action.isEnabledForContext(getContext()));+ }+private Symbol findSymbolForLocation(List<Symbol> navigatedSymbols, ProgramLocation location) {for (Symbol symbol : navigatedSymbols) {if (symbol.getAddress().equals(location.getAddress())) {@@ -262,38 +344,64 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTe}private Address addr(String address) {- return addressFactory.getAddress(address);+ return program.getAddressFactory().getAddress(address);+ }++ private void goTo(Symbol s) throws Exception {+ goTo(s.getAddress());}- private void goTo(String locationName) throws Exception {- setText(locationName);- performOkCallback();+ private void goTo(Address a) throws Exception {+ GoToService goToService = tool.getService(GoToService.class);+ goToService.goTo(a);+ cbPlugin.updateNow();+ waitForSwing();}- private void forwardByAction() {- assertTrue(nextAction.isEnabled());+ private void next() {+ assertTrue(nextAction.isEnabledForContext(getContext()));performAction(nextAction, true);+ cbPlugin.updateNow();+ waitForSwing();}- private void backwardByAction() {- assertTrue(previousAction.isEnabled());+ private void previous() {+ assertTrue(previousAction.isEnabledForContext(getContext()));performAction(previousAction, true);+ cbPlugin.updateNow();+ waitForSwing();+ }++ private void previousFunction() {+ assertTrue(previousFunctionAction.isEnabledForContext(getContext()));+ performAction(previousFunctionAction, getContext(), true);+ cbPlugin.updateNow();+ waitForSwing();+ }++ private void nextFunction() {+ assertTrue(nextFunctionAction.isEnabledForContext(getContext()));+ performAction(nextFunctionAction, getContext(), true);+ cbPlugin.updateNow();+ waitForSwing();}- private void backwardByDropdown() {- assertTrue(previousAction.isEnabled());+ private void previousByDropdown() {+ assertTrue(previousAction.isEnabledForContext(getContext()));JFrame toolFrame = tool.getToolFrame();DockingWindowManager dwm = DockingWindowManager.getInstance(toolFrame);JButton previousButton = findButtonForAction(dwm, previousAction);clickDropdownForButton(previousButton);+ waitForSwing();}- private void forwardByDrowdown() {- assertTrue(nextAction.isEnabled());+ private void nextByDrowdown() {+ assertTrue(nextAction.isEnabledForContext(getContext()));JFrame toolFrame = tool.getToolFrame();DockingWindowManager dwm = DockingWindowManager.getInstance(toolFrame);JButton nextButton = findButtonForAction(dwm, nextAction);clickDropdownForButton(nextButton);+ waitForSwing();}private void clickDropdownForButton(JButton button) {@@ -372,9 +480,9 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTeprivate List<Symbol> doBulkGoTo() throws Exception {List<Symbol> list = new ArrayList<>();- Memory memory = baseProgram.getMemory();+ Memory memory = program.getMemory();int count = 0;- SymbolIterator iter = baseProgram.getSymbolTable().getAllSymbols(true);+ SymbolIterator iter = program.getSymbolTable().getAllSymbols(true);while (iter.hasNext() && count < 11) {Symbol symbol = iter.next();Address addr = symbol.getAddress();@@ -382,19 +490,17 @@ public class NextPrevAddressPluginTest extends AbstractGhidraHeadedIntegrationTecontinue;}list.add(symbol);- setText(symbol.getName());- performOkCallback();+ goTo(symbol);++count;}return list;}- private void setText(final String text) throws Exception {- SwingUtilities.invokeAndWait(() -> dialog.setText(text));+ private Address currentAddress() {+ return cbPlugin.getCurrentAddress();}- private void performOkCallback() throws Exception {- runSwing(() -> dialog.okCallback());- waitForPostedSwingRunnables();+ private ActionContext getContext() {+ return provider.getActionContext(null);}}diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerNavigationTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerNavigationTest.javaindex 462db29f35..558a87bb6f 100644--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerNavigationTest.java+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerNavigationTest.java@@ -20,11 +20,14 @@ import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;+import docking.ActionContext;+import docking.action.DockingAction;import ghidra.app.cmd.function.CreateFunctionCmd;import ghidra.app.nav.Navigatable;import ghidra.app.plugin.core.codebrowser.CodeViewerProvider;import ghidra.app.plugin.core.gotoquery.GoToHelper;import ghidra.app.plugin.core.navigation.NavigationOptions;+import ghidra.app.plugin.core.navigation.NextPrevAddressPlugin;import ghidra.program.model.address.Address;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.*;@@ -94,8 +97,9 @@ public class DecompilerNavigationTest extends AbstractDecompilerTest {// this call triggers jMockit to load our spynew SpyGoToHelper();- tool.getOptions("Navigation").setEnum("External Navigation",- NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);+ tool.getOptions("Navigation")+ .setEnum("External Navigation",+ NavigationOptions.ExternalNavigationEnum.NavigateToExternalProgram);//// Take an existing function with a call reference and change it to call a thunk with@@ -129,8 +133,9 @@ public class DecompilerNavigationTest extends AbstractDecompilerTest {// this call triggers jMockit to load our spynew SpyGoToHelper();- tool.getOptions("Navigation").setEnum("External Navigation",- NavigationOptions.ExternalNavigationEnum.NavigateToLinkage);+ tool.getOptions("Navigation")+ .setEnum("External Navigation",+ NavigationOptions.ExternalNavigationEnum.NavigateToLinkage);//// Take an existing function with a call reference and change it to call a thunk with@@ -171,6 +176,52 @@ public class DecompilerNavigationTest extends AbstractDecompilerTest {assertListingAddress(addr("01002d32"));}+ @Test+ public void testFunctionNavigation_WithAViewThatCachesTheLastValidFunction()+ throws Exception {++ //+ // This is testing the case where the user starts on a function foo(). Ancillary windows+ // will display tool, such as a decompiled view. Now, if the user clicks to a+ // non-function location, such as data, the ancillary window may still show foo(), even+ // though the user is no longer in foo. At this point, if the user wishes to go to the+ // previous function, then from the ancillary window's perspective, it is the function+ // that came before foo().+ //++ Address f1 = addr("01002cf5"); // ghidra+ Address f2 = addr("0100415a"); // sscanf+ Address nonFunctionAddress = addr("01001000");++ goTo(f1);+ goTo(f2);+ goTo(nonFunctionAddress);++ String title = provider.getTitle();+ assertTrue("Decompiler did not retain last function visited", title.contains("sscanf"));++ provider.requestFocus();+ waitForSwing();++ //+ // The Decompiler is focused, showing 'entry'. Going back while it is focused should go+ // to the function before 'entry', which is 'ghidra'.+ //+ previousFunction();+ assertCurrentAddress(f1);+ }++ private void previousFunction() {+ NextPrevAddressPlugin plugin = env.getPlugin(NextPrevAddressPlugin.class);+ DockingAction previousFunctionAction =+ (DockingAction) getInstanceField("previousFunctionAction", plugin);++ ActionContext context = provider.getActionContext(null);+ assertTrue(previousFunctionAction.isEnabledForContext(context));+ performAction(previousFunctionAction, context, true);+ waitForSwing();+ }+private void assertListingAddress(Address expected) {ProgramLocation cbLocation = codeBrowser.getCurrentLocation();assertEquals("The Listing is not at the expected address", expected,