Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2939 Added option for programs to open back to their last location from when they were last closed.

ghidragon3f260a1577d4
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.htm
index d29d4dd35a..52643cce33 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
@@ -794,11 +794,15 @@
<P><B>Start At</B> - Choose a starting program location option:
<UL>
<LI>Lowest Address - The program will open at the lowest address.</LI>
- <LI>Lowest Code Block Address - The program will open at the firest executable
+ <LI>Lowest Code Block Address - The program will open at the first executable
memory block. If no executable block found, it will go to lowest address.</LI>
<LI>Preferred Symbol Name - The program will open at the first symbol name it
finds that matches a name in the "Start Symbols" option. If no symbol is found,
it will try the lowest code block, and finally the lowest address.</LI>
+ <LI>Location When Last Closed - The program will open to the location it was at the
+ last time the program was closed. If this is the first time the program
+ has been opened, it will try to find a preferred symbol, then it will look for the
+ lowest code block, and finally lowest overall address</LI>
</UL>
<P><B>Start Symbols</B> - A comma separated list of symbol names to be be used as the
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationOptions.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationOptions.java
index e898877589..ab9c0a6fef 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationOptions.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationOptions.java
@@ -36,7 +36,8 @@ public class ProgramStartingLocationOptions implements OptionsChangeListener {
private static final String START_LOCATION_DESCRIPTION =
"Determines the start location for newly opened programs.\n" +
- "Either lowest address, lowest code address, or symbol name.\nEach higher " +
+ "Either lowest address, lowest code address, preferred starting symbol name, or the" +
+ "location when last closed.\nEach higher " +
"option will revert to the next lower option if that option can't be satisfied.";
private static final String STARTING_SYSMBOLS_DESCRIPTION =
"A comma separated list of symbol names in preference order. " +
@@ -50,7 +51,8 @@ public class ProgramStartingLocationOptions implements OptionsChangeListener {
public static enum StartLocationType {
LOWEST_ADDRESS("Lowest Address"),
LOWEST_CODE_BLOCK("Lowest Code Block Address"),
- SYMBOL_NAME("Preferred Symbol Name");
+ SYMBOL_NAME("Preferred Symbol Name"),
+ LAST_LOCATION("Location When Last Closed");
private String label;
@@ -78,7 +80,7 @@ public class ProgramStartingLocationOptions implements OptionsChangeListener {
Options subOptions = options.getOptions(SUB_OPTION);
subOptions.setOptionsHelpLocation(help);
- options.registerOption(START_LOCATION_TYPE_OPTION, StartLocationType.SYMBOL_NAME, help,
+ options.registerOption(START_LOCATION_TYPE_OPTION, StartLocationType.LAST_LOCATION, help,
START_LOCATION_DESCRIPTION);
options.registerOption(START_SYMBOLS_OPTION, DEFAULT_STARTING_SYMBOLS, help,
@@ -109,27 +111,12 @@ public class ProgramStartingLocationOptions implements OptionsChangeListener {
}
/**
- * Returns true if the program should start on one of the starting symbols.
- * @return true if the program should start on one of the starting symbols
+ * Returns the StartLocationType (lowest address, lowest code address, staring symbol, or
+ * last location)
+ * @return the StartLocationType
*/
- public boolean shouldStartOnSymbol() {
- return startLocationType == StartLocationType.SYMBOL_NAME;
- }
-
- /**
- * Returns true if the program should start at the lowest address.
- * @return true if the program should start at the lowest address
- */
- public boolean shouldStartAtLowestAddress() {
- return startLocationType == StartLocationType.LOWEST_ADDRESS;
- }
-
- /**
- * Returns true if the program should start at the first code block.
- * @return true if the program should start at the first code block.
- */
- public boolean shouldStartAtLowestCodeBlock() {
- return startLocationType == StartLocationType.LOWEST_CODE_BLOCK;
+ public StartLocationType getStartLocationType() {
+ return startLocationType;
}
/**
@@ -171,5 +158,4 @@ public class ProgramStartingLocationOptions implements OptionsChangeListener {
useUnderscorePrefixes = (Boolean) newValue;
}
}
-
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationPlugin.java
index 0526f29484..75611f8b82 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/ProgramStartingLocationPlugin.java
@@ -15,22 +15,29 @@
*/
package ghidra.app.plugin.core.navigation;
-import java.util.Collections;
-import java.util.List;
+import java.io.IOException;
+import java.util.*;
+
+import org.jdom.Element;
+import org.jdom.JDOMException;
import ghidra.app.CorePluginPackage;
import ghidra.app.plugin.PluginCategoryNames;
import ghidra.app.plugin.ProgramPlugin;
+import ghidra.app.plugin.core.navigation.ProgramStartingLocationOptions.StartLocationType;
import ghidra.app.services.GoToService;
+import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.PluginInfo;
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.plugintool.util.PluginStatus;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.ProgramUserData;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.program.util.ProgramLocation;
import ghidra.util.Swing;
+import ghidra.util.xml.XmlUtilities;
//@formatter:off
@PluginInfo(
@@ -44,8 +51,10 @@ import ghidra.util.Swing;
//@formatter:on
public class ProgramStartingLocationPlugin extends ProgramPlugin {
+ private static final String LAST_LOCATION_PROPERTY = "LAST_PROGRAM_LOCATION";
private Program lastOpenedProgram;
private ProgramStartingLocationOptions startOptions;
+ private Map<Program, ProgramLocation> lastLocationMap = new HashMap<>();
public ProgramStartingLocationPlugin(PluginTool tool) {
super(tool);
@@ -59,13 +68,27 @@ public class ProgramStartingLocationPlugin extends ProgramPlugin {
if (tool.isRestoringDataState()) {
return;
}
- if (startOptions.shouldStartAtLowestAddress()) {
+ if (startOptions.getStartLocationType() == StartLocationType.LOWEST_ADDRESS) {
// this is what happens by default, so no need to do anything
return;
}
lastOpenedProgram = program;
}
+ protected void programClosed(Program program) {
+ ProgramLocation lastLocation = lastLocationMap.remove(program);
+ if (lastLocation == null) {
+ return;
+ }
+ // store a program's last location in the associated user program data.
+ ProgramUserData programUserData = program.getProgramUserData();
+ SaveState saveState = new SaveState("Last_Location");
+ lastLocation.saveState(saveState);
+ String xmlString = XmlUtilities.toString(saveState.saveToXml());
+ programUserData.setStringProperty(LAST_LOCATION_PROPERTY, xmlString);
+
+ }
+
@Override
protected void programActivated(Program program) {
super.programActivated(program);
@@ -75,6 +98,14 @@ public class ProgramStartingLocationPlugin extends ProgramPlugin {
lastOpenedProgram = null;
}
+ @Override
+ protected void locationChanged(ProgramLocation loc) {
+ if (loc != null) {
+ Program program = loc.getProgram();
+ lastLocationMap.put(program, loc);
+ }
+ }
+
private void setStartingLocationForNewProgram() {
if (currentProgram == null) {
return;
@@ -90,19 +121,53 @@ public class ProgramStartingLocationPlugin extends ProgramPlugin {
}
private ProgramLocation getStartingProgramLocation(Program program) {
- if (startOptions.shouldStartOnSymbol()) {
- List<String> symbolNames = startOptions.getStartingSymbolNames();
- boolean useUnderscores = startOptions.useUnderscorePrefixes();
- for (String symbolName : symbolNames) {
- Symbol symbol = findSymbol(program, symbolName, useUnderscores);
+ switch (startOptions.getStartLocationType()) {
+ case LAST_LOCATION:
+ ProgramLocation lastLocation = getLastSavedLocation(program);
+ if (lastLocation != null) {
+ return lastLocation;
+ }
+ // fall through and try symbol name
+ case SYMBOL_NAME:
+ Symbol symbol = fingStartingSymbol(program);
if (symbol != null) {
return symbol.getProgramLocation();
}
+ // fall through and try to find the lowest code block
+ case LOWEST_CODE_BLOCK:
+ return findLowestCodeBlockLocation(program);
+ case LOWEST_ADDRESS:
+ default:
+ return null; // the program opens to lowest address anyway, so nothing to do
+ }
+ }
+
+ private ProgramLocation getLastSavedLocation(Program program) {
+ ProgramUserData programUserData = program.getProgramUserData();
+ String value = programUserData.getStringProperty(LAST_LOCATION_PROPERTY, null);
+ if (value == null) {
+ return null;
+ }
+ try {
+ Element element = XmlUtilities.fromString(value);
+ SaveState saveState = new SaveState(element);
+ return ProgramLocation.getLocation(program, saveState);
+ }
+ catch (JDOMException | IOException e) {
+ return null;
+ }
+ }
+
+ private Symbol fingStartingSymbol(Program program) {
+ List<String> symbolNames = startOptions.getStartingSymbolNames();
+ boolean useUnderscores = startOptions.useUnderscorePrefixes();
+ for (String symbolName : symbolNames) {
+ Symbol symbol = findSymbol(program, symbolName, useUnderscores);
+ if (symbol != null) {
+ return symbol;
}
}
- // if the option is start on first code block, or we couldn't find a symbol, try and
- // find the first executable code block
- return findLowestCodeBlockLocation(program);
+ return null;
}
private ProgramLocation findLowestCodeBlockLocation(Program program) {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/ProgramStartPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/ProgramStartPluginTest.java
index c7c4a1d8ca..36af3269bb 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/ProgramStartPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/navigation/ProgramStartPluginTest.java
@@ -24,10 +24,12 @@ import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;
import ghidra.framework.options.Options;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressFactory;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.MemoryBlock;
+import ghidra.program.util.ProgramLocation;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv;
@@ -148,6 +150,18 @@ public class ProgramStartPluginTest extends AbstractGhidraHeadedIntegrationTest
assertEquals(addr("0x100"), cb.getCurrentAddress());
}
+ @Test
+ public void testOptionToStartAtLastLocation() throws Exception {
+ ProgramBuilder builder = getProgramBuilder("0x100");
+ ProgramDB program = builder.getProgram();
+ loadProgram(program);
+ cb.goTo(new ProgramLocation(program, addr("0x107")));
+ env.close(program);
+ env.open(program);
+ waitForSwing();
+ assertEquals(addr("0x107"), cb.getCurrentAddress());
+ }
+
private void setUnderscoreOption(boolean b) {
options.setBoolean(ProgramStartingLocationOptions.UNDERSCORE_OPTION, b);
}