Package ghidra.base.project
Class GhidraProject
- java.lang.Object
-
- ghidra.base.project.GhidraProject
-
public class GhidraProject extends java.lang.ObjectHelper 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidanalyze(Program program)Invokes the auto-analyzer on the program.voidanalyze(Program program, boolean debug)Debug version of the auto_analyzer.voidcheckPoint(Program program)Creates a checkpoint in the program.voidclose()Closes the ghidra project, closing (without saving!) any open programs in that project.voidclose(Program program)Closes the given program.static GhidraProjectcreateProject(java.lang.String projectDirPath, java.lang.String projectName, boolean temporary)Creates a new non-shared Ghidra project to be used for storing programs.voidexecute(Command cmd, Program program)Executes the give command on the program.OptionsgetAnalysisOptions(Program program)Returns a PropertList containing all the analysis option properties that can be set.ProjectgetProject()Returns the underlying Project instance or null if project was opened for READ access only.DefaultProjectManagergetProjectManager()Returns the project managerDomainFoldergetRootFolder()Get the root folder for the Ghidra project.static RepositoryAdaptergetServerRepository(java.lang.String host, int port, java.lang.String repositoryName, boolean createIfNeeded)Get/Create shared repository.ProgramimportProgram(java.io.File file)ProgramimportProgram(java.io.File file, DomainFolder domainFolder)ProgramimportProgram(java.io.File file, DomainFolder domainFolder, Language language, CompilerSpec compilerSpec)ProgramimportProgram(java.io.File file, Language language, CompilerSpec compilerSpec)ProgramimportProgram(java.io.File file, Processor processor)ProgramimportProgram(java.io.File file, java.lang.Class<? extends Loader> loaderClass)ProgramimportProgram(java.io.File file, java.lang.Class<? extends Loader> loaderClass, Language language, CompilerSpec compilerSpec)ProgramimportProgramFast(java.io.File file)ProgramopenProgram(java.lang.String folderPath, java.lang.String programName, boolean readOnly)Opens a program.static GhidraProjectopenProject(java.lang.String projectsDir, java.lang.String projectName)Returns an instance of an open Ghidra Project that can be used to open/save programs.static GhidraProjectopenProject(java.lang.String projectsDir, java.lang.String projectName, boolean restoreProject)Returns an instance of an open Ghidra Project that can be used to open/save programs.voidrollback(Program program)Rolls back any changes to the program since the last checkpoint.voidsave(Program program)Saves any changes in the program back to its file.voidsaveAs(Program program, java.lang.String folderPath, java.lang.String name, boolean overWrite)Saves the given program to the project with the given name.voidsaveAsPackedFile(Program program, java.io.File file, boolean overWrite)Saves the given program to as a packed file.voidsetDeleteOnClose(boolean toDelete)Updates the flag passed to this project at construction time.
-
-
-
Method Detail
-
openProject
public static GhidraProject openProject(java.lang.String projectsDir, java.lang.String projectName) throws java.io.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.- Returns:
- an open ghidra project.
- Throws:
java.io.IOException- if there was a problem accessing the project
-
openProject
public static GhidraProject openProject(java.lang.String projectsDir, java.lang.String projectName, boolean restoreProject) throws java.io.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:
java.io.IOException- if there was a problem accessing the project
-
createProject
public static GhidraProject createProject(java.lang.String projectDirPath, java.lang.String projectName, boolean temporary) throws java.io.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:
java.io.IOException- if there was a problem accessing the project
-
getServerRepository
public static RepositoryAdapter getServerRepository(java.lang.String host, int port, java.lang.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
public DefaultProjectManager getProjectManager()
Returns the project manager- Returns:
- the project manager
-
getProject
public Project getProject()
Returns the underlying Project instance or null if project was opened for READ access only.
-
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
public void close(Program program)
Closes the given program. Any changes in the program will be lost.- Parameters:
program- the program to close.
-
openProgram
public Program openProgram(java.lang.String folderPath, java.lang.String programName, boolean readOnly) throws java.io.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:
java.io.IOException- if there was a problem accessing the program
-
save
public void save(Program program) throws java.io.IOException
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:
java.io.IOException- if there was a problem accessing the program
-
getRootFolder
public DomainFolder getRootFolder()
Get the root folder for the Ghidra project.
-
saveAs
public void saveAs(Program program, java.lang.String folderPath, java.lang.String name, boolean overWrite) throws InvalidNameException, java.io.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.java.io.IOException- if there was a problem accessing the program
-
saveAsPackedFile
public void saveAsPackedFile(Program program, java.io.File file, boolean overWrite) throws InvalidNameException, java.io.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:
DuplicateNameException- if a file exists with that name and overwrite is false or overwrite failedInvalidNameException- the name is null or has invalid characters.java.io.IOException- if there was a problem accessing the program
-
checkPoint
public void checkPoint(Program program)
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
public void rollback(Program program)
Rolls back any changes to the program since the last checkpoint.- Parameters:
program- the program to be rolled back.
-
analyze
public static void analyze(Program program)
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
public void analyze(Program program, boolean debug)
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
public Options getAnalysisOptions(Program program)
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
public void execute(Command cmd, Program program)
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(java.io.File file, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file, DomainFolder domainFolder, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file, Processor processor) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file, java.lang.Class<? extends Loader> loaderClass) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file, java.lang.Class<? extends Loader> loaderClass, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgram
public Program importProgram(java.io.File file, DomainFolder domainFolder) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
importProgramFast
public Program importProgramFast(java.io.File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, java.io.IOException
- Throws:
CancelledExceptionDuplicateNameExceptionInvalidNameExceptionVersionExceptionjava.io.IOException
-
-