Ghidra is a software reverse engineering (SRE) framework
GP-5775 added view source file action to source file table
James988dadd7cf3f
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/SourceFilesTablePlugin/SourceFilesTable.html b/Ghidra/Features/Base/src/main/help/help/topics/SourceFilesTablePlugin/SourceFilesTable.htmlindex 7adfe22243..cd26d0274f 100644--- a/Ghidra/Features/Base/src/main/help/help/topics/SourceFilesTablePlugin/SourceFilesTable.html+++ b/Ghidra/Features/Base/src/main/help/help/topics/SourceFilesTablePlugin/SourceFilesTable.html@@ -38,6 +38,12 @@<P>This action brings up a table which displays all of the source map entries for theselected source file.</P>+ <H3><A name="View_Source_File"></A>View Source Files</H3>++ <P>This action opens the a source file (at line 1) in the configured viewer. Options for+ configuring the viewer are described <A href=+ "#Source_Files_Table_Plugin_Options">below</A>.</P>+<H3><A name="Transform_File"></A>Transform File</H3><P>This action allows you to create a file transform for the selected source file.</P>@@ -79,8 +85,8 @@<H2><A name="Source_Files_Table_Plugin_Options"></A>Plugin Options</H2>- <P>These options can be changed from the Code Browser via Edit->-Tool Options->Source- Files and Transforms.</P>+ <P>These options can be changed from the Code Browser via Edit->-Tool Options->Source+ Files and Transforms.</P><BLOCKQUOTE><H3><A name="Use_Existing_As_Default"></A>Use Existing As Default</H3>diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTablePlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTablePlugin.javaindex ce3fe6bab1..0e2c3e1297 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTablePlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTablePlugin.java@@ -38,6 +38,7 @@ import ghidra.framework.options.*;import ghidra.framework.plugintool.PluginInfo;import ghidra.framework.plugintool.PluginTool;import ghidra.framework.plugintool.util.PluginStatus;+import ghidra.program.database.sourcemap.SourceFile;import ghidra.program.database.sourcemap.UserDataPathTransformer;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Program;@@ -174,14 +175,16 @@ public class SourceFilesTablePlugin extends ProgramPlugin implements OptionsChan}// if there's only one entry associated with the address, just view itif (entries.size() == 1) {- openInViewer(entries.get(0));+ SourceMapEntry entry = entries.get(0);+ openInViewer(entry.getSourceFile(), entry.getLineNumber());return;}// if there are multiple entries, we need to decide which one to view// if the user right-clicked in the SourceMapField in the Listing, open// the associated entryif (context.getLocation() instanceof SourceMapFieldLocation sourceLoc) {- openInViewer(sourceLoc.getSourceMapEntry());+ SourceMapEntry entry = sourceLoc.getSourceMapEntry();+ openInViewer(entry.getSourceFile(), entry.getLineNumber());return;}// otherwise pop up a window and ask the user to select an entry@@ -207,21 +210,21 @@ public class SourceFilesTablePlugin extends ProgramPlugin implements OptionsChanreturn;}SourceMapEntry entryToShow = stringsToEntries.get(selected);- openInViewer(entryToShow);+ openInViewer(entryToShow.getSourceFile(), entryToShow.getLineNumber());}- private void openInViewer(SourceMapEntry entry) {- if (entry == null) {+ void openInViewer(SourceFile sourceFile, int lineNum) {+ if (sourceFile == null) {return;}SourcePathTransformer transformer =UserDataPathTransformer.getPathTransformer(currentProgram);String transformedPath =- transformer.getTransformedPath(entry.getSourceFile(), useExistingAsDefault);+ transformer.getTransformedPath(sourceFile, useExistingAsDefault);if (transformedPath == null) {Msg.showWarn(this, null, "No Path Transform",- "No path transformation applies to " + entry.getSourceFile().toString());+ "No path transformation applies to " + sourceFile.toString());return;}@@ -234,10 +237,10 @@ public class SourceFilesTablePlugin extends ProgramPlugin implements OptionsChanswitch (selectedViewer) {case ECLIPSE:- openFileInEclipse(localSourceFile.getAbsolutePath(), entry.getLineNumber());+ openFileInEclipse(localSourceFile.getAbsolutePath(), lineNum);break;case VS_CODE:- openFileInVsCode(localSourceFile.getAbsolutePath(), entry.getLineNumber());+ openFileInVsCode(localSourceFile.getAbsolutePath(), lineNum);break;default:throw new AssertionError("Unsupported Viewer: " + selectedViewer);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTableProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTableProvider.javaindex 8b57381b09..5bff0611d6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTableProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/sourcefilestable/SourceFilesTableProvider.java@@ -232,6 +232,16 @@ public class SourceFilesTableProvider extends ComponentProviderAdapter {.onAction(this::showSourceMapEntries).buildAndInstallLocal(this);+ new ActionBuilder("View Source File", getName())+ .popupMenuPath("View Source File")+ .description("View the SourceFile")+ .helpLocation(+ new HelpLocation(sourceFilesTablePlugin.getName(), "View_Source_File"))+ .withContext(SourceFilesTableActionContext.class)+ .enabledWhen(c -> c.getSelectedRowCount() == 1)+ .onAction(this::viewSourceFile)+ .buildAndInstallLocal(this);+new ActionBuilder("Transform File", getName()).popupMenuPath("Tranform File").description("Enter a file transform for a SourceFile").helpLocation(new HelpLocation(sourceFilesTablePlugin.getName(), "Transform_File"))@@ -466,6 +476,17 @@ public class SourceFilesTableProvider extends ComponentProviderAdapter {new HelpLocation(sourceFilesTablePlugin.getName(), "Show_Source_Map_Entries"));}+ private void viewSourceFile(SourceFilesTableActionContext actionContext) {+ TableService tableService = sourceFilesTablePlugin.getTool().getService(TableService.class);+ if (tableService == null) {+ Msg.showWarn(this, null, "No Table Service", "Please add the TableServicePlugin.");+ return;+ }+ SourceFileRowObject sourceFileRow = sourceFilesTable.getSelectedRowObject();+ SourceFile sourceFile = sourceFileRow.getSourceFile();+ sourceFilesTablePlugin.openInViewer(sourceFile, 1);+ }+private class SourceFilesTableActionContext extends DefaultActionContext {SourceFilesTableActionContext() {