Ghidra is a software reverse engineering (SRE) framework
implement function level history navigation
Ancietya1df6fabf219
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 15ec063355..e3c5dbbfbe 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,6 +17,9 @@ 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.tool.ToolConstants;@@ -207,6 +210,80 @@ 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+ */+ @Override+ public 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) {+ 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+ */+ @Override+ public 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) {+ navigate(navigatable, memento);+ }+ }++ @Override+ public boolean hasNextFunction(Navigatable navigatable) {+ return hasNextPreviousFunction(navigatable, true);+ }++ @Override+ public 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;+ }+ }/*** Positions the "current" view to the next view in the history list and@@ -526,6 +603,70 @@ public class NavigationHistoryPlugin extends Pluginreturn 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+ */+ LocationMemento nextFunction(FunctionManager functionManager, Function currentFunction, boolean moveTo) {+ if (list.isEmpty()) {+ return null;+ }++ 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) {+ if (moveTo) {+ currentLocation = i;+ }+ return memento;+ }+ }++ return null;+ }++ /**+ * 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+ */+ LocationMemento previousFunction(FunctionManager functionManager, Function currentFunction, boolean moveTo) {+ if (list.isEmpty()) {+ return null;+ }++ 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) {+ 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;+ }+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 ed6bd70079..9ef3f611ab 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@@ -67,11 +67,15 @@ public class NextPrevAddressPlugin extends Plugin {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[] CLEAR_MENUPATH = { "Navigation", "Clear History" };private NavigationHistoryService historyService;private MultiActionDockingAction nextAction;private MultiActionDockingAction previousAction;+ private MultiActionDockingAction nextFunctionAction;+ private MultiActionDockingAction previousFunctionAction;private DockingAction clearAction;private BrowserCodeUnitFormat codeUnitFormatter;@@ -172,6 +176,8 @@ 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);clearAction = new DockingAction("Clear History Buffer", getName()) {@Override@@ -208,6 +214,8 @@ public class NextPrevAddressPlugin extends Plugin {tool.addAction(previousAction);tool.addAction(nextAction);+ tool.addAction(previousFunctionAction);+ tool.addAction(nextFunctionAction);tool.addAction(clearAction);}@@ -396,4 +404,66 @@ public class NextPrevAddressPlugin extends Plugin {}}+ private class NextPreviousFunctionAction extends MultiActionDockingAction {++ 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");+ }++ @Override+ public boolean isValidContext(ActionContext context) {+ return (context instanceof NavigatableActionContext);+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {+ Navigatable navigatable = getNavigatable(context);+ if (navigatable == null) {+ return false;+ }+ if (isNext) {+ return historyService.hasNextFunction(navigatable);+ }+ return historyService.hasPreviousFunction(navigatable);+ }++ @Override+ public void actionPerformed(ActionContext context) {+ Navigatable navigatable = getNavigatable(context);+ if (isNext) {+ historyService.nextFunction(navigatable);+ }+ else {+ historyService.previousFunction(navigatable);+ }+ }++ @Override+ public boolean shouldAddToWindow(boolean isMainWindow, Set<Class<?>> contextTypes) {+ for (Class<?> class1 : contextTypes) {+ if (NavigationActionContext.class.isAssignableFrom(class1)) {+ return true;+ }+ }+ 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/services/NavigationHistoryService.java b/Ghidra/Features/Base/src/main/java/ghidra/app/services/NavigationHistoryService.javaindex 43bb16037f..281af2eb2c 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@@ -30,6 +30,22 @@ 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.@@ -74,6 +90,20 @@ public interface NavigationHistoryService {*/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.*/