Package ghidra.base.project

# Class GhidraProject

java.lang.Object
ghidra.base.project.GhidraProject

public class GhidraProject extends Object
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 Type
  Method
  Description
  `static void`
  `analyze(Program program)`
  Invokes the auto-analyzer on the program.
  `void`
  `analyze(Program program, boolean debug)`
  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`
  `close(Program program)`
  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`
  `execute(Command cmd, Program program)`
  Executes the give command on the program.
  `Options`
  `getAnalysisOptions(Program program)`
  Returns a PropertList containing all the analysis option properties that can be set.
  `Project`
  `getProject()`
  Returns the underlying Project instance or null if project was opened for READ access only.
  `ProjectData`
  `getProjectData()`
  Returns the underlying ProjectData instance.
  `DefaultProjectManager`
  `getProjectManager()`
  Returns the project manager
  `DomainFolder`
  `getRootFolder()`
  Get the root folder for the Ghidra project.
  `static RepositoryAdapter`
  `getServerRepository(String host, int port, String repositoryName, boolean createIfNeeded)`
  Get/Create shared repository.
  `Program`
  `importProgram(File file)`
  `Program`
  `importProgram(File file, Language language, CompilerSpec compilerSpec)`
  `Program`
  `importProgram(File file, Processor processor)`
  `Program`
  `importProgram(File file, Class<? extends Loader> loaderClass)`
  `Program`
  `importProgram(File file, Class<? extends Loader> loaderClass, Language language, CompilerSpec compilerSpec)`
  `Program`
  `importProgramFast(File file)`
  `Program`
  `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`
  `rollback(Program program)`
  Rolls back any changes to the program since the last checkpoint.
  `void`
  `save(Program program)`
  Saves any changes in the program back to its file.
  `void`
  `saveAs(Program program, String folderPath, String name, boolean overWrite)`
  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.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### openProject

    public static GhidraProject openProject(String projectsDir, String projectName) 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.
    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 host
      `port` - 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.

  - ### getProjectData

    public ProjectData 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

    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(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

    public void save(Program program) throws 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:
      `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, 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 saved
      `folderPath` - 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 failed
      `InvalidNameException` - 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 saved
      `file` - 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

    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 analyzed
      `debug` - 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(File file, Language language, CompilerSpec compilerSpec) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
    Throws:
      `CancelledException`
      `DuplicateNameException`
      `InvalidNameException`
      `VersionException`
      `IOException`

  - ### importProgram

    public Program importProgram(File file, Processor processor) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
    Throws:
      `CancelledException`
      `DuplicateNameException`
      `InvalidNameException`
      `VersionException`
      `IOException`

  - ### importProgram

    public Program importProgram(File file, Class<? extends Loader> loaderClass) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
    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
    Throws:
      `CancelledException`
      `DuplicateNameException`
      `InvalidNameException`
      `VersionException`
      `IOException`

  - ### importProgram

    public Program importProgram(File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
    Throws:
      `CancelledException`
      `DuplicateNameException`
      `InvalidNameException`
      `VersionException`
      `IOException`

  - ### importProgramFast

    public Program importProgramFast(File file) throws CancelledException, DuplicateNameException, InvalidNameException, VersionException, IOException
    Throws:
      `CancelledException`
      `DuplicateNameException`
      `InvalidNameException`
      `VersionException`
      `IOException`