Package ghidra.base.project
Class GhidraProject
java.lang.Object
ghidra.base.project.GhidraProject
Helper class for using Ghidra in a "batch" mode. This class provides methods
for importing, opening, saving, and analyzing program.
Note: Before using this class you must initialize the Ghidra system. See
Application.initializeApplication(utility.application.ApplicationLayout, ghidra.framework.ApplicationConfiguration)
for more information.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Invokes the auto-analyzer on the program.void
Debug version of the auto_analyzer.void
checkPoint
(Program program) Creates a checkpoint in the program.void
close()
Closes the ghidra project, closing (without saving!) any open programs in that project.void
Closes the given program.static GhidraProject
createProject
(String projectDirPath, String projectName, boolean temporary) Creates a new non-shared Ghidra project to be used for storing programs.void
Executes the give command on the program.getAnalysisOptions
(Program program) Returns a PropertList containing all the analysis option properties that can be set.Returns the underlying Project instance or null if project was opened for READ access only.Returns the underlying ProjectData instance.Returns the project managerGet the root folder for the Ghidra project.static RepositoryAdapter
getServerRepository
(String host, int port, String repositoryName, boolean createIfNeeded) Get/Create shared repository.importProgram
(File file) importProgram
(File file, Language language, CompilerSpec compilerSpec) importProgram
(File file, Processor processor) importProgram
(File file, Class<? extends Loader> loaderClass) importProgram
(File file, Class<? extends Loader> loaderClass, Language language, CompilerSpec compilerSpec) importProgramFast
(File file) openProgram
(String folderPath, String programName, boolean readOnly) Opens a program.static GhidraProject
openProject
(String projectsDir, String projectName) Returns an instance of an open Ghidra Project that can be used to open/save programs.static GhidraProject
openProject
(String projectsDir, String projectName, boolean restoreProject) Returns an instance of an open Ghidra Project that can be used to open/save programs.void
Rolls back any changes to the program since the last checkpoint.void
Saves any changes in the program back to its file.void
Saves the given program to the project with the given name.void
saveAsPackedFile
(Program program, File file, boolean overWrite) Saves the given program to as a packed file.void
setDeleteOnClose
(boolean toDelete) Updates the flag passed to this project at construction time.
-
Method Details
-
openProject
Returns an instance of an open Ghidra Project that can be used to open/save programs.- Parameters:
projectsDir
- the directory containing the Ghidra project.projectName
- the name of the ghidra project.- Returns:
- an open ghidra project.
- Throws:
IOException
- if there was a problem accessing the project
-
openProject
public static GhidraProject openProject(String projectsDir, String projectName, boolean restoreProject) throws IOException Returns an instance of an open Ghidra Project that can be used to open/save programs.- Parameters:
projectsDir
- the directory containing the Ghidra project.projectName
- the name of the ghidra project.restoreProject
- if true the project tool state is restored- Returns:
- an open ghidra project.
- Throws:
IOException
- if there was a problem accessing the project
-
createProject
public static GhidraProject createProject(String projectDirPath, String projectName, boolean temporary) throws IOException Creates a new non-shared Ghidra project to be used for storing programs.Note: Calling this method will delete any existing project files on disk that match the given project name.
- Parameters:
projectDirPath
- the directory path to contain the new Ghidra project.projectName
- the name of the project to be created.temporary
- if true, deletes the the project when it is closed - useful for testing.- Returns:
- an open ghidra project.
- Throws:
IOException
- if there was a problem accessing the project
-
getServerRepository
public static RepositoryAdapter getServerRepository(String host, int port, String repositoryName, boolean createIfNeeded) throws DuplicateNameException Get/Create shared repository.- Parameters:
host
- Ghidra Server hostport
- Ghidra Server port (0 = use default port)repositoryName
-createIfNeeded
- if true repository will be created if it does not exist- Throws:
DuplicateNameException
-
getProjectManager
Returns the project manager- Returns:
- the project manager
-
getProject
Returns the underlying Project instance or null if project was opened for READ access only. -
getProjectData
Returns the underlying ProjectData instance. -
close
public void close()Closes the ghidra project, closing (without saving!) any open programs in that project. Also deletes the project if created as a temporary project. -
setDeleteOnClose
public void setDeleteOnClose(boolean toDelete) Updates the flag passed to this project at construction time.- Parameters:
toDelete
- true to delete on close; false in the opposite condition
-
close
Closes the given program. Any changes in the program will be lost.- Parameters:
program
- the program to close.
-
openProgram
public Program openProgram(String folderPath, String programName, boolean readOnly) throws IOException Opens a program.- Parameters:
folderPath
- the path of the program within the project. ("\" is root)programName
- the name of the program to open.readOnly
- flag if the program will only be read and not written.- Returns:
- an open program.
- Throws:
IOException
- if there was a problem accessing the program
-
save
Saves any changes in the program back to its file. If the program does not have an associated file (it was created), then it is an error to call this method, use saveAs instead. Any open transaction will be terminated.- Parameters:
program
- the program to be saved.- Throws:
IOException
- if there was a problem accessing the program
-
getRootFolder
Get the root folder for the Ghidra project. -
saveAs
public void saveAs(Program program, String folderPath, String name, boolean overWrite) throws InvalidNameException, IOException Saves the given program to the project with the given name.- Parameters:
program
- the program to be savedfolderPath
- the path where to save the program.name
- the name to save the program as.overWrite
- if true, any existing program with that name will be over-written.- Throws:
DuplicateFileException
- if a file exists with that name and overwrite is false or overwrite failedInvalidNameException
- the name is null or has invalid characters.IOException
- if there was a problem accessing the program
-
saveAsPackedFile
public void saveAsPackedFile(Program program, File file, boolean overWrite) throws InvalidNameException, IOException Saves the given program to as a packed file.- Parameters:
program
- the program to be savedfile
- the packed file destination.overWrite
- if true, any existing program with that name will be over-written.- Throws:
InvalidNameException
- the name is null or has invalid characters.IOException
- if there was a problem accessing the program
-
checkPoint
Creates a checkpoint in the program. Any changes since the last checkpoint can be instantly undone by calling the rollback command.- Parameters:
program
- the program to be checkpointed.
-
rollback
Rolls back any changes to the program since the last checkpoint.- Parameters:
program
- the program to be rolled back.
-
analyze
Invokes the auto-analyzer on the program. Depending on which analyzers are in the classpath, generally will disassemble at entry points, and create and analyze functions that are called.- Parameters:
program
- the program to analyze.
-
analyze
Debug version of the auto_analyzer. Same as regular analyzer except that any stack traces are not trapped.- Parameters:
program
- the program to be analyzeddebug
- true to allow stack traces to propagate out.
-
getAnalysisOptions
Returns a PropertList containing all the analysis option properties that can be set. Changing the value of the analysis properties will affect what happens when the analyze call is made.- Parameters:
program
- the program whose analysis options are to be set.
-
execute
Executes the give command on the program.- Parameters:
cmd
- the command to be applied to the program.program
- the program on which the command is to be applied.
-
importProgram
public Program importProgram(File file, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException -
importProgram
public Program importProgram(File file, Processor processor) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException -
importProgram
public Program importProgram(File file, Class<? extends Loader> loaderClass) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException -
importProgram
public Program importProgram(File file, Class<? extends Loader> loaderClass, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException -
importProgram
public Program importProgram(File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException -
importProgramFast
public Program importProgramFast(File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
-