Java Code Examples for ghidra.program.model.listing.Program#startTransaction()

The following examples show how to use ghidra.program.model.listing.Program#startTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImporterUtilities.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that a {@link Program}'s metadata includes its import origin.
 *
 * @param program imported {@link Program} to modify
 * @param fsrl {@link FSRL} of the import source.
 * @param monitor {@link TaskMonitor} to use when accessing filesystem stuff.
 * @throws CancelledException if user cancels
 * @throws IOException if IO error
 */
public static void setProgramProperties(Program program, FSRL fsrl, TaskMonitor monitor)
		throws CancelledException, IOException {

	Objects.requireNonNull(monitor);

	int id = program.startTransaction("setImportProperties");
	try {
		fsrl = FileSystemService.getInstance().getFullyQualifiedFSRL(fsrl, monitor);

		Options propertyList = program.getOptions(Program.PROGRAM_INFO);
		propertyList.setString(ProgramMappingService.PROGRAM_SOURCE_FSRL, fsrl.toString());
		String md5 = program.getExecutableMD5();
		if ((md5 == null || md5.isEmpty()) && fsrl.getMD5() != null) {
			program.setExecutableMD5(fsrl.getMD5());
		}
	}
	finally {
		program.endTransaction(id, true);
	}
	if (program.canSave()) {
		program.save("Added import properties", monitor);
	}
}
 
Example 2
Source File: AutoAnalysisPlugin.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Show the options panel for the auto analysis options. 
 */
private boolean showOptionsDialog(Program program) {
	tool.clearStatusInfo();
	Options options = tool.getOptions(GhidraOptions.CATEGORY_AUTO_ANALYSIS);
	boolean showDialog = options.getBoolean(SHOW_ANALYSIS_OPTIONS, true);
	if (!showDialog) {
		return true;
	}
	int id = program.startTransaction("Analysis Options");
	try {
		AnalysisOptionsDialog dialog = new AnalysisOptionsDialog(program);
		tool.showDialog(dialog);
		return dialog.wasAnalyzeButtonSelected();
	}
	finally {
		program.endTransaction(id, true);
	}
}
 
Example 3
Source File: DataTypeManagerHandler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public void removeInvalidArchive(InvalidFileArchive archive) {
	archive.close();

	if (programArchive == null) {
		return;
	}

	ProgramDataTypeManager programDataTypeManager =
		(ProgramDataTypeManager) programArchive.getDataTypeManager();
	Program program = programArchive.getProgram();
	int ID = program.startTransaction("Remove Invalid Source Archive From Program");
	try {
		UniversalID sourceArchiveID = archive.getUniversalID();
		SourceArchive sourceArchive = programDataTypeManager.getSourceArchive(sourceArchiveID);
		if (sourceArchive != null) {
			programDataTypeManager.removeSourceArchive(sourceArchive);
		}
	}
	finally {
		program.endTransaction(ID, true);
	}
}
 
Example 4
Source File: AbstractProgramLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public final boolean loadInto(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
		MessageLog messageLog, Program program, TaskMonitor monitor)
		throws IOException, CancelledException {

	if (!loadSpec.isComplete()) {
		return false;
	}

	program.setEventsEnabled(false);
	int transactionID = program.startTransaction("Loading - " + getName());
	boolean success = false;
	try {
		success = loadProgramInto(provider, loadSpec, options, messageLog, program, monitor);
		return success;
	}
	finally {
		program.endTransaction(transactionID, success);
		program.setEventsEnabled(true);
	}
}
 
Example 5
Source File: AbstractApplyFunctionSignatureAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to change the signature of a function to that of another
 * function
 * 
 * @param provider the parent component provider 
 * @param destinationFunction the function to change
 * @param sourceFunction the function to copy
 * @return true if the operation was successful
 */
protected boolean updateFunction(ComponentProvider provider, Function destinationFunction,
		Function sourceFunction) {

	Program program = destinationFunction.getProgram();
	int txID = program.startTransaction(ACTION_NAME);
	boolean commit = false;

	try {
		FunctionUtility.updateFunction(destinationFunction, sourceFunction);
		commit = true;
	}
	catch (InvalidInputException | DuplicateNameException e) {
		String message = "Couldn't apply the function signature from " +
			sourceFunction.getName() + " to " + destinationFunction.getName() + " @ " +
			destinationFunction.getEntryPoint().toString() + ". " + e.getMessage();
		Msg.showError(this, provider.getComponent(), ACTION_NAME, message, e);
	}
	finally {
		program.endTransaction(txID, commit);
	}

	return commit;
}
 
Example 6
Source File: AbstractGhidraHeadlessIntegrationTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Run a command against the specified program within a transaction.
 * The transaction will be committed unless the command throws a RollbackException.
 * 
 * @param program the program
 * @param cmd the command to apply
 * @return result of command applyTo method
 * @throws RollbackException thrown if thrown by command applyTo method
 */
public static boolean applyCmd(Program program, Command cmd) throws RollbackException {
	int txId = program.startTransaction(cmd.getName());
	boolean commit = true;
	try {
		boolean status = cmd.applyTo(program);
		program.flushEvents();
		waitForSwing();

		if (!status) {
			Msg.error(null, "Could not apply command: " + cmd.getStatusMsg());
		}

		return status;
	}
	catch (RollbackException e) {
		commit = false;
		throw e;
	}
	finally {
		program.endTransaction(txId, commit);
	}
}
 
Example 7
Source File: LabelMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol addLabel(String name, Address address, Program program)
		throws InvalidInputException {

	SymbolTable symbolTable = program.getSymbolTable();
	int transaction = -1;
	try {
		transaction = program.startTransaction("Test - Add Label");
		return symbolTable.createLabel(address, name, SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transaction, true);
	}
}
 
Example 8
Source File: GhidraProject.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initializeProgram(Program program, boolean readOnly) {
	if (program == null) {
		return;
	}
	int id = -1;
	if (!readOnly) {
		id = program.startTransaction("Batch Processing");
		AutoAnalysisManager mgr = AutoAnalysisManager.getAnalysisManager(program);
		mgr.initializeOptions();
	}
	openPrograms.put(program, new Integer(id));
}
 
Example 9
Source File: CommitLocalsAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
	Program program = context.getProgram();
	int transaction = program.startTransaction("Commit Local Names");
	try {
		HighFunction hfunc = context.getHighFunction();
		HighFunctionDBUtil.commitLocalNamesToDatabase(hfunc, SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transaction, true);
	}
}
 
Example 10
Source File: ToolBasedColorProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void setVertexColor(FGVertex vertex, Color color) {
	Program program = plugin.getCurrentProgram();
	int id = program.startTransaction("Set Background Color");
	try {
		service.setBackgroundColor(vertex.getAddresses(), color);
	}
	finally {
		program.endTransaction(id, true);
	}

	vertex.setBackgroundColor(color);
}
 
Example 11
Source File: ClearPinSymbolAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected void actionPerformed(ProgramSymbolActionContext context) {
	Program program = context.getProgram();
	int transactionID = program.startTransaction("Clear Pinned");
	try {
		for (Symbol symbol : context.getSymbols()) {
			if (symbol.isPinned()) {
				symbol.setPinned(false);
			}
		}
	}
	finally {
		program.endTransaction(transactionID, true);
	}
}
 
Example 12
Source File: AbstractVersionControlActionTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void createHistoryEntry(Program program, String symbolName)
		throws InvalidInputException, IOException, CancelledException {
	int transactionID = program.startTransaction("test");
	try {
		SymbolTable symTable = program.getSymbolTable();
		symTable.createLabel(program.getMinAddress().getNewAddress(0x010001000), symbolName,
			SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
}
 
Example 13
Source File: DataLabelMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol addLabel(String name, Address address, Program program)
		throws InvalidInputException {

	SymbolTable symbolTable = program.getSymbolTable();
	int transaction = -1;
	try {
		transaction = program.startTransaction("Test - Add Label");
		return symbolTable.createLabel(address, name, SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transaction, true);
	}
}
 
Example 14
Source File: ToyProgramBuilder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void addInstructionWords(Address start, short... instrWords)
		throws MemoryAccessException {
	Program program = getProgram();
	int txId = program.startTransaction("Add Instruction Bytes");
	try {
		for (int i = 0; i < instrWords.length; i++) {
			program.getMemory().setShort(start.add(i * 2), instrWords[i]);
		}
		definedInstrAddresses.add(start);
	}
	finally {
		program.endTransaction(txId, true);
	}
}
 
Example 15
Source File: ProjectInfoDialogTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertProjectFilesCheckedOut() throws Exception {
	setErrorGUIEnabled(true); // we need the dialog below

	// check-out testA
	rootFolder = getProject().getProjectData().getRootFolder();
	DomainFile df = rootFolder.getFile("testA");
	assertTrue(df.checkout(false, TaskMonitor.DUMMY));

	// make simple change to checked-out file
	Program p = (Program) df.getDomainObject(this, false, false, TaskMonitor.DUMMY);
	try {

		int txId = p.startTransaction("test");
		try {
			p.setName("XYZ");
		}
		finally {
			p.endTransaction(txId, true);
		}
		p.save(null, TaskMonitor.DUMMY);
	}
	finally {
		p.release(this);
	}

	pressButtonByText(dialog, ProjectInfoDialog.CONVERT, false);
	windowForComponent(dialog.getComponent());

	stepThroughWizard(true);

	OptionDialog opt = waitForDialogComponent(OptionDialog.class);
	assertNotNull(opt);
	assertEquals("Confirm Convert Project", opt.getTitle());
	pressButtonByText(opt, "Convert");
}
 
Example 16
Source File: FrontEndTestEnv.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void editProgram(Program program, ModifyProgramCallback modifyProgramCallback)
		throws CancelledException, IOException {
	int transactionID = program.startTransaction("test");
	try {
		modifyProgramCallback.call(program);
	}
	catch (Exception e) {
		AbstractGTest.failWithException("Unexpected exception", e);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
}
 
Example 17
Source File: GhidraProgramUtilities.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the analyzed flag from the program properties.
 * With this property removed, the user will be prompted to analyze the
 * program the next time it is opened.
 * @param program the program containing the property to be removed
 */
public static void removeAnalyzedFlag(Program program) {
	int transactionID = program.startTransaction(Program.ANALYZED);
	try {
		Options options = program.getOptions(Program.PROGRAM_INFO);
		options.removeOption(Program.ANALYZED);
	}
	finally {
		program.endTransaction(transactionID, true);
	}
}
 
Example 18
Source File: LabelMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol addDefaultLabel(Address address, Program program) {

		SymbolTable symbolTable = program.getSymbolTable();
		int transaction = -1;
		try {
			transaction = program.startTransaction("Test - Add Label");
			ReferenceManager referenceManager = program.getReferenceManager();
			referenceManager.addMemoryReference(address, address, RefType.READ,
				SourceType.USER_DEFINED, 0);
			return symbolTable.getPrimarySymbol(address);
		}
		finally {
			program.endTransaction(transaction, true);
		}
	}
 
Example 19
Source File: GhidraDBTransaction.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Start a transaction on the given program with the given description
 * @param program the program to modify
 * @param description a description of the transaction
 */
public GhidraDBTransaction(Program program, String description) {
	this.program = program;
	this.tid = program.startTransaction(description);
	this.open = true;
}
 
Example 20
Source File: VersionControlAction1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testUndoHijack() throws Exception {

	// check out a file
	GTreeNode node = getNode(PROGRAM_A);
	addToVersionControl(node, false);

	selectNode(node);
	final DockingActionIf action = getAction("CheckOut");
	SwingUtilities.invokeLater(() -> action.actionPerformed(getDomainFileActionContext(node)));
	waitForSwing();

	waitForTasks();

	DockingActionIf undoHijackAction = getAction("Undo Hijack");
	assertTrue(!undoHijackAction.isEnabledForContext(getDomainFileActionContext(node)));

	// make a change to the program
	DomainFile df = ((DomainFileNode) node).getDomainFile();
	Program program = (Program) df.getDomainObject(this, true, false, TaskMonitor.DUMMY);
	int transactionID = program.startTransaction("test");
	try {
		program.getSymbolTable().createNameSpace(null, "myNamespace", SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
	program.release(this);
	program.flushEvents();

	// terminate the checkout to get a hijacked file
	ItemCheckoutStatus[] items = df.getCheckouts();
	assertEquals(1, items.length);
	df.terminateCheckout(items[0].getCheckoutId());
	waitForSwing();

	clearSelectionPaths();

	selectNode(node);
	assertTrue(undoHijackAction.isEnabledForContext(getDomainFileActionContext(node)));
	waitForSwing();

	// undo the hijack		
	performFrontEndAction(undoHijackAction);
	UndoActionDialog dialog = waitForDialogComponent(UndoActionDialog.class);
	assertNotNull(dialog);

	DomainFilesPanel panel = findComponent(dialog.getComponent(), DomainFilesPanel.class);
	assertNotNull(panel);

	DomainFile[] files = panel.getSelectedDomainFiles();
	assertEquals(1, files.length);
	assertEquals(df, files[0]);

	assertTrue(dialog.saveCopy());
	pressButtonByText(dialog.getComponent(), "OK");

	waitForSwing();
	waitForTasks();

	assertNotNull(getNode(PROGRAM_A + ".keep"));
	assertTrue(!undoHijackAction.isEnabledForContext(getDomainFileActionContext(node)));
}