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

The following examples show how to use ghidra.program.model.listing.Program#flushEvents() . 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: CreateLibraryAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createExternalLibrary(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();

		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolTreeRootNode) {
			node = node.getChild("Imports");
		}

		String newExternalLibraryName = createExternalLibrary(program, parent);
		if (newExternalLibraryName == null) {
			return;
		}

		program.flushEvents();
		context.getSymbolTree().startEditing(node, newExternalLibraryName);
	}
 
Example 2
Source File: CreateClassAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createNewClass(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();
		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolNode) {
			Symbol symbol = ((SymbolNode) node).getSymbol();
			parent = (Namespace) symbol.getObject();
			if (parent == null) {
				return; // assume selected node has been deleted
			}
		}

		final String newClassName = createClass(program, parent);
		if (newClassName == null) {
			// error occurred
			return;
		}
		program.flushEvents();
		context.getSymbolTree().startEditing(node, newClassName);
	}
 
Example 3
Source File: CreateNamespaceAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createNamespace(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();

		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolNode) {
			Symbol symbol = ((SymbolNode) node).getSymbol();
			parent = (Namespace) symbol.getObject();
			if (parent == null) {
				return; // assume selected symbol has been deleted
			}
		}

		String newNamespaceName = createNamespace(program, parent);
		if (newNamespaceName == null) {
			// error occurred
			return;
		}

		program.flushEvents();
		context.getSymbolTree().startEditing(node, newNamespaceName);
	}
 
Example 4
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 5
Source File: AbstractGhidraHeadlessIntegrationTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Provides a convenient method for modifying the current program, handling the transaction
 * logic. 
 * 
 * @param p the program
 * @param c the code to execute
 */
public static <E extends Exception> void tx(Program p, ExceptionalCallback<E> c) {
	int txId = p.startTransaction("Test - Function in Transaction");
	boolean commit = true;
	try {
		c.call();
		p.flushEvents();
		waitForSwing();
	}
	catch (Exception e) {
		commit = false;
		failWithException("Exception modifying program '" + p.getName() + "'", e);
	}
	finally {
		p.endTransaction(txId, commit);
	}
}
 
Example 6
Source File: MultiTabPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testTabUpdatesOnProgramChange() throws Exception {
	ProgramBuilder builder = new ProgramBuilder("notepad", ProgramBuilder._TOY);
	builder.createMemory("test", "0x0", 100);
	Program p = doOpenProgram(builder.getProgram(), true);
	p.setTemporary(false); // we need to be notified of changes 

	// select notepad
	panel.setSelectedProgram(p);
	int transactionID = p.startTransaction("test");
	try {
		SymbolTable symTable = p.getSymbolTable();
		symTable.createLabel(builder.addr("0x10"), "fred", SourceType.USER_DEFINED);
	}
	finally {
		p.endTransaction(transactionID, true);
	}
	p.flushEvents();
	runSwing(() -> panel.refresh(p));

	JPanel tab = panel.getTab(p);
	JLabel label = (JLabel) findComponentByName(tab, "objectName");
	assertTrue(label.getText().startsWith("*"));
}
 
Example 7
Source File: AbstractGhidraHeadedIntegrationTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the given program's events before waiting for the swing update manager
 * 
 * @param program The program whose events will be flushed; may be null
 */
public static void waitForProgram(Program program) {
	if (program != null) {
		program.flushEvents();
	}

	waitForSwing();
}
 
Example 8
Source File: MultiTabPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void addComment(Program p) {
	int transactionID = p.startTransaction("test");
	try {
		p.getListing().setComment(p.getAddressFactory().getAddress("01000000"),
			CodeUnit.REPEATABLE_COMMENT, "This is a simple comment change.");
	}
	finally {
		p.endTransaction(transactionID, true);
	}
	p.flushEvents();
	waitForSwing();
}
 
Example 9
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 10
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)));
}