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

The following examples show how to use ghidra.program.model.listing.Program#save() . 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: CallAnotherScriptForAllPrograms.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void processProgram(Program program) throws CancelledException, IOException {
	/* Do you program work here */
	println("Processing: " + program.getDomainFile().getPathname());
	monitor.setMessage("Processing: " + program.getDomainFile().getName());
	int id = program.startTransaction("Batch Script Transaction");
	try {
		GhidraState newState =
			new GhidraState(state.getTool(), state.getProject(), program, null, null, null);
		runScript(SUBSCRIPT_NAME, newState);
	}
	catch (Exception e) {
		printerr("ERROR! Exception occurred while processing file: " +
			program.getDomainFile().getPathname());
		printerr("       " + e.getMessage());
		e.printStackTrace();
		return;
	}
	finally {
		program.endTransaction(id, true);
	}

	// ...save any changes
	program.save("Changes made by script: " + SUBSCRIPT_NAME, monitor);
}
 
Example 3
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 4
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 5
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 6
Source File: VersionControlSlowScreenShots.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void createHistoryEntry(Program p, String symbolName)
		throws InvalidInputException, IOException, CancelledException {
	int transactionID = p.startTransaction("test");
	try {
		SymbolTable symTable = p.getSymbolTable();
		symTable.createLabel(p.getMinAddress().getNewAddress(0x010001000), symbolName,
			SourceType.USER_DEFINED);
	}
	finally {
		p.endTransaction(transactionID, true);
		p.save(null, TaskMonitor.DUMMY);
	}
}
 
Example 7
Source File: VersionControlAction1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testUndoCheckOutModified() throws Exception {
	GTreeNode node = getNode(PROGRAM_A);
	addToVersionControl(node, false);

	selectNode(node);
	DockingActionIf action = getAction("CheckOut");
	performAction(action, getDomainFileActionContext(node), false);

	waitForTasks();

	// 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();

	DockingActionIf undoAction = getAction("UndoCheckOut");
	performAction(undoAction, getDomainFileActionContext(node), false);
	UndoActionDialog dialog = waitForDialogComponent(UndoActionDialog.class);
	assertNotNull(dialog);

	JCheckBox cb = (JCheckBox) findAbstractButtonByText(dialog.getComponent(),
		"Save copy of the file with a .keep extension");
	assertNotNull(cb);
	assertTrue(cb.isSelected());

	pressButtonByText(dialog, "OK");
	waitForTasks();

	df = ((DomainFileNode) node).getDomainFile();
	assertTrue(!df.isCheckedOut());
	Icon icon = df.getIcon(false);
	Icon[] icons = ((MultiIcon) icon).getIcons();
	Icon checkOutIcon = ResourceManager.loadImage("images/checkex.png");
	for (Icon element : icons) {
		if (checkOutIcon.equals(element)) {
			Assert.fail("Found unexpected check out icon!");
		}
	}

	DomainFileNode keepNode = getNode(PROGRAM_A + ".keep");
	assertNotNull(keepNode);
}
 
Example 8
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)));
}
 
Example 9
Source File: VersionControlAction2Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckIn() throws Exception {
	final GTreeNode node = getNode(PROGRAM_A);
	addToVersionControl(node, false);

	selectNode(node);
	final DockingActionIf action = getAction("CheckOut");
	runSwing(() -> action.actionPerformed(getDomainFileActionContext(node)), false);
	waitForSwing();
	waitForTasks();

	Program program = (Program) ((DomainFileNode) node).getDomainFile()
			.getDomainObject(this,
				true, false, TaskMonitor.DUMMY);
	int transactionID = program.startTransaction("test");
	try {
		SymbolTable symTable = program.getSymbolTable();
		symTable.createLabel(program.getMinAddress().getNewAddress(0x010001000), "fred",
			SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
	program.release(this);
	final DockingActionIf checkInAction = getAction("CheckIn");
	runSwing(() -> checkInAction.actionPerformed(getDomainFileActionContext(node)), false);
	waitForSwing();
	VersionControlDialog dialog = waitForDialogComponent(VersionControlDialog.class);
	assertNotNull(dialog);
	final JTextArea textArea = findComponent(dialog, JTextArea.class);
	assertNotNull(textArea);
	final JCheckBox cb = findComponent(dialog, JCheckBox.class);
	assertNotNull(cb);
	runSwing(() -> {
		textArea.setText("This is a test");
		cb.setSelected(false);
	});
	pressButtonByText(dialog, "OK");
	waitForTasks();
	DomainFile df = ((DomainFileNode) node).getDomainFile();
	assertTrue(!df.isCheckedOut());

}
 
Example 10
Source File: CreateAppliedExactMatchingSessionScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void run() throws Exception {
	DomainFolder folder =
		askProjectFolder("Please choose a folder for the session domain object");
	String name = askString("Please enter a Version Tracking session name", "Session Name");
	Program sourceProgram = askProgram("Please select the source (existing annotated) program");
	Program destinationProgram = askProgram("Please select the destination (new) program");

	VTSession session =
		VTSessionDB.createVTSession(name, sourceProgram, destinationProgram, this);

	// it seems clunky to have to create this separately, but I'm not sure how else to do it
	folder.createFile(name, session, monitor);

	String description = "CreateAppliedExactMatchingSession";

	int sessionTransaction = session.startTransaction(description);
	try {
		PluginTool serviceProvider = state.getTool();
		VTAssociationManager manager = session.getAssociationManager();

		// should we have convenience methods in VTCorrelator that don't
		// take address sets, thus implying the entire address space should be used?
		AddressSetView sourceAddressSet = sourceProgram.getMemory().getLoadedAndInitializedAddressSet();
		AddressSetView destinationAddressSet =
			destinationProgram.getMemory().getLoadedAndInitializedAddressSet();

		VTProgramCorrelatorFactory factory;

		factory = new ExactDataMatchProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);

		factory = new ExactMatchBytesProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);

		factory = new ExactMatchInstructionsProgramCorrelatorFactory();
		correlateAndPossiblyApply(sourceProgram, destinationProgram, session, serviceProvider,
			manager, sourceAddressSet, destinationAddressSet, factory);
	}
	finally {
		try {
			session.endTransaction(sessionTransaction, true);
			destinationProgram.save(description, monitor);
			session.save(description, monitor);
		}
		finally {
			session.release(this);
		}
	}
}