ghidra.framework.cmd.CompoundCmd Java Examples

The following examples show how to use ghidra.framework.cmd.CompoundCmd. 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: FunctionWindowPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteAndRestore() throws Exception {

	int numData = functionTable.getRowCount();

	CompoundCmd cmd = new CompoundCmd("Clear");
	FunctionIterator itr = program.getListing().getFunctions(true);
	while (itr.hasNext()) {
		Function f = itr.next();
		cmd.add(new ClearCmd(f.getBody(), new ClearOptions()));
	}
	applyCmd(program, cmd);
	waitForNotBusy(functionTable);

	assertEquals(0, functionTable.getRowCount());

	undo(program);
	waitForNotBusy(functionTable);

	assertEquals(numData, functionTable.getRowCount());

}
 
Example #2
Source File: ProgramTreeActionManager.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Merge a module with its parent. The module is deleted if it has
 * no other parents; called by the action listener on a menu.
 */
private void merge() {
	synchronized (root) {
		ArrayList<ProgramNode> list = tree.getSortedSelection();
		CompoundCmd compCmd = new CompoundCmd("Merge with Parent");
		String treeName = tree.getTreeName();
		for (int i = 0; i < list.size(); i++) {
			ProgramNode node = list.get(i);
			tree.removeSelectionPath(node.getTreePath());
			ProgramNode parentNode = (ProgramNode) node.getParent();
			if (node.isModule() && parentNode != null) {
				compCmd.add(new MergeFolderCmd(treeName, node.getName(), parentNode.getName()));
			}
		}
		if (!plugin.getTool().execute(compCmd, program)) {
			plugin.getTool().setStatusInfo(compCmd.getStatusMsg());
		}
	}
}
 
Example #3
Source File: SetStackDepthChangeAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void setFunctionPurge(final Program program, final Address fromAddress,
		final int newFunctionPurgeSize, final Address callToAddress) {
	Function function = program.getFunctionManager().getFunctionAt(callToAddress);
	if (function == null) {
		return;
	}
	// Set the function purge.
	Command purgeCmd = new SetFunctionPurgeCommand(function, newFunctionPurgeSize);

	Integer dephtChange = CallDepthChangeInfo.getStackDepthChange(program, fromAddress);
	if (dephtChange != null) {
		// If we have a stack depth change here, remove it since we are setting the purge.
		CompoundCmd compoundCmd = new CompoundCmd("Set Function Purge via StackDepthChange");
		compoundCmd.add(new RemoveStackDepthChangeCommand(program, fromAddress));
		compoundCmd.add(purgeCmd);
		funcPlugin.execute(program, compoundCmd);
	}
	else {
		funcPlugin.execute(program, purgeCmd);
	}
}
 
Example #4
Source File: BookmarkPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteOffcutBookmarks() throws Exception {
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	Address a = addr("0100b6db");
	for (int i = 0; i < 20; i++) {
		addCmd.add(new BookmarkEditCmd(a, "Type1", "Cat1a", "Cmt1A"));
		a = a.add(1);
	}
	applyCmd(program, addCmd);

	//make a structure
	CreateStructureCmd cmd = new CreateStructureCmd(addr("0100b6db"), 20);
	applyCmd(program, cmd);

	List<DockingActionIf> actions = runSwing(() -> plugin.getPopupActions(null,
		createContext(new MarkerLocation(null, addr("0100b6db"), 0, 0))));
	assertEquals(10, actions.size());
}
 
Example #5
Source File: BookmarkPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarkerAdded() throws Exception {
	MarkerService markerService = tool.getService(MarkerService.class);
	MarkerSet markerSet = markerService.getMarkerSet("Type1 Bookmarks", program);
	AddressSet addressSet = runSwing(() -> markerSet.getAddressSet());
	assertFalse(addressSet.contains(addr("01001100")));
	assertFalse(addressSet.contains(addr("01001120")));

	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001100"), "Type1", "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001120"), "Type1", "Cat1a", "Cmt1B"));
	applyCmd(program, addCmd);

	MarkerSet type1MarkerSet = markerService.getMarkerSet("Type1 Bookmarks", program);
	addressSet = runSwing(() -> type1MarkerSet.getAddressSet());
	assertTrue(addressSet.contains(addr("01001100")));
	assertTrue(addressSet.contains(addr("01001120")));
}
 
Example #6
Source File: RegisterValuesPanel.java    From ghidra with Apache License 2.0 6 votes vote down vote up
void deleteSelectedRanges() {
	CompoundCmd cmd = new CompoundCmd("Delete Register Value Ranges");
	int[] rows = table.getSelectedRows();
	boolean containsDefaultValues = false;
	for (int row : rows) {
		RegisterValueRange rvr = model.values.get(row);
		if (rvr.isDefault()) {
			containsDefaultValues = true;
		}
		cmd.add(new SetRegisterCmd(selectedRegister, rvr.getStartAddress(), rvr.getEndAddress(),
			null));
	}
	if (containsDefaultValues) {
		int result = OptionDialog.showOptionDialog(table, "Warning",
			"The selected ranges " +
				"contain default values that can't be deleted.\n  Do you want to continue?",
			"Yes", OptionDialog.WARNING_MESSAGE);
		if (result == OptionDialog.CANCEL_OPTION) {
			return;
		}
	}
	if (cmd.size() > 0) {
		tool.execute(cmd, currentProgram);
	}
}
 
Example #7
Source File: RegisterPlugin.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void applyRegisterValues(Program program, Register register, BigInteger value,
		AddressSetView addressSet) {
	if (program == null || addressSet.isEmpty()) {
		return;
	}

	CompoundCmd cmd = new CompoundCmd("Set Register Values");
	for (AddressRange range : addressSet) {
		SetRegisterCmd regCmd =
			new SetRegisterCmd(register, range.getMinAddress(), range.getMaxAddress(), value);
		cmd.add(regCmd);
	}
	if (!tool.execute(cmd, program)) {
		Msg.showError(this, tool.getToolFrame(), "Register Context Error", cmd.getStatusMsg());
	}
}
 
Example #8
Source File: FallThroughModel.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void override(AddressRange range, CompoundCmd cmd) {
	Address min = range.getMinAddress();
	Address max = range.getMaxAddress();
	Listing listing = program.getListing();
	Instruction inst = listing.getInstructionAt(min);
	if (inst == null) {
		inst = listing.getInstructionAfter(min);
	}
	while (inst != null && inst.getMinAddress().compareTo(max) <= 0) {
		
		Data data = listing.getDataAfter(inst.getMinAddress());
		if (data == null) {
			return;
		}
		inst = listing.getInstructionBefore(data.getMinAddress());
		if (inst.getFallThrough() != null &&
			range.contains(inst.getMinAddress())) {
			setFallThrough(inst.getMinAddress(), cmd);
		}
		inst = listing.getInstructionAfter(data.getMinAddress());
	}
}
 
Example #9
Source File: RegisterEvent.java    From gdbghidra with MIT License 6 votes vote down vote up
public static void handleEvent(RegisterEvent registerEvent, Program currentProgram, ProgramPlugin plugin, ProgramLocation currentLocation) {
	var register = currentProgram.getRegister(registerEvent.getName());
	if(register == null) {
		register = currentProgram.getRegister(registerEvent.getName().toUpperCase());
		if(register == null) {
			System.err.println("[GDBGHIDRA] Error unknown register: "+registerEvent.getName()+"\n");
			return;
		}
	}
	var address = currentLocation.getAddress();
	var cmd = new CompoundCmd("Set Register Values");
	var regCmd = new SetRegisterCmd(
			register, 
			address, 
			address,
			registerEvent.getValue());
	cmd.add(regCmd);
	plugin.getTool().execute(cmd, currentProgram);
}
 
Example #10
Source File: ClearExternalNameAssociationAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionContext context) {
	Program program = provider.getProgram();
	List<String> externalNames = provider.getSelectedExternalNames();
	CompoundCmd cmd = new CompoundCmd("Clear External Program Associations");
	for (String externalName : externalNames) {
		cmd.add(new ClearExternalNameCmd(externalName));
	}
	provider.getTool().execute(cmd, program);
}
 
Example #11
Source File: GCAnalyzer.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
protected boolean setRegisterValue(String registerName, long defaultValue, Program program, CodeManager cm, AddressSpace addrSpace) {
    Register reg = program.getRegister(registerName);
    Address startAddr = cm.getInstructionAfter(addrSpace.getMinAddress()).getAddress();
    Address endAddr = cm.getInstructionBefore(addrSpace.getMaxAddress()).getAddress();
    Msg.debug(this, String.format("Writing regs to minAddr=0x%08X through maxAddr=0x%08X", startAddr.getUnsignedOffset(), endAddr.getUnsignedOffset()));
    BigInteger val = BigInteger.valueOf(defaultValue);
    var cmd1 = new SetRegisterCmd(reg, startAddr, endAddr, null);
    var cmd2 = new SetRegisterCmd(reg, startAddr, endAddr, val);
    var cmd = new CompoundCmd("Update Register Range");
    cmd.add(cmd1);
    cmd.add(cmd2);
    var result =  cmd.applyTo(program);
    Msg.debug(this, String.format("Reg value: %08X", program.getProgramContext().getRegisterValue(reg, startAddr).getUnsignedValue().longValue()));
    return result;
}
 
Example #12
Source File: BookmarkPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initProgram() {
	addrFactory = program.getAddressFactory();

	// Remove all existing bookmarks
	BookmarkDeleteCmd delCmd = new BookmarkDeleteCmd(addrFactory.getAddressSet(), null, null);
	applyCmd(program, delCmd);

	// Add specific bookmarks
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001010"), "Type1", "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001020"), "Type1", "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001020"), "Type1", "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("01001030"), "Type1", "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("01001030"), "Type2", "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("01001040"), "Type2", "Cat2b", "Cmt2F"));
	addCmd.add(new BookmarkEditCmd(addr("01001040"), "Type3", "Cat3a", "Cmt2G"));
	addCmd.add(new BookmarkEditCmd(addr("01001050"), "Type3", "Cat3a", "Cmt2H"));
	addCmd.add(new BookmarkEditCmd(addr("01001060"), "Type3", "Cat3b", "Cmt2I"));
	applyCmd(program, addCmd);

	AddLabelCmd labelCmd =
		new AddLabelCmd(addr("01001020"), "Test20", false, SourceType.USER_DEFINED);
	applyCmd(program, labelCmd);

	labelCmd = new AddLabelCmd(addr("01001050"), "Test50", false, SourceType.USER_DEFINED);
	applyCmd(program, labelCmd);

	List<Bookmark> list = getBookmarks(program.getBookmarkManager());
	assertEquals(9, list.size());
	bookmarks = new Bookmark[9];
	list.toArray(bookmarks);
	Arrays.sort(bookmarks);
}
 
Example #13
Source File: BookmarkPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleDelete() throws Exception {
	showBookmarkProvider();

	// note: we want to test a specific code path, so we must delete more than 20 items

	// add more to our current set
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001110"), "Type1", "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001120"), "Type1", "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001120"), "Type1", "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("01001130"), "Type1", "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("01001130"), "Type2", "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("01001140"), "Type2", "Cat2b", "Cmt2F"));
	addCmd.add(new BookmarkEditCmd(addr("01001140"), "Type3", "Cat3a", "Cmt2G"));
	addCmd.add(new BookmarkEditCmd(addr("01001150"), "Type3", "Cat3a", "Cmt2H"));
	addCmd.add(new BookmarkEditCmd(addr("01001160"), "Type3", "Cat3b", "Cmt2I"));
	addCmd.add(new BookmarkEditCmd(addr("01001210"), "Type1", "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001220"), "Type1", "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001220"), "Type1", "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("01001230"), "Type1", "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("01001230"), "Type2", "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("01001240"), "Type2", "Cat2b", "Cmt2F"));
	addCmd.add(new BookmarkEditCmd(addr("01001240"), "Type3", "Cat3a", "Cmt2G"));
	addCmd.add(new BookmarkEditCmd(addr("01001250"), "Type3", "Cat3a", "Cmt2H"));
	addCmd.add(new BookmarkEditCmd(addr("01001260"), "Type3", "Cat3b", "Cmt2I"));
	applyCmd(program, addCmd);

	waitForTable();

	selectAllTableRows();
	runSwing(() -> provider.delete());
	waitForCondition(() -> table.getRowCount() == 0, "Bookmarks not deleted");
}
 
Example #14
Source File: BookmarkEditCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Address[] createBookmarks(ProgramLocation[] locs) {

		CompoundCmd compoundCmd = new CompoundCmd("Create Bookmarks");

		Address[] addrs = new Address[locs.length];
		for (int i = 0; i < locs.length; i++) {
			Address addr = locs[i].getAddress();
			BookmarkEditCmd cmd = new BookmarkEditCmd(addr, "Type" + i, "Cat" + i, "Cmt" + i);
			compoundCmd.add(cmd);
			addrs[i] = addr;
		}
		applyCmd(notepad, compoundCmd);
		System.out.println("Created " + addrs.length + " Bookmarks");
		return addrs;
	}
 
Example #15
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testOffcutBookmarkForwardAndBackward() throws Exception {
	loadProgram("notepad");
	showTool(tool);
	assertEquals(addr("01001000"), cb.getCurrentAddress());

	//
	// Setup:
	// 1000 - info bookmark
	// 1006 - offcut info bookmark
	// 101e - info bookmark
	//
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001000"), BookmarkType.INFO, "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001004"), BookmarkType.INFO, "Cat1a", "Cmt1BC"));
	addCmd.add(new BookmarkEditCmd(addr("01001006"), BookmarkType.INFO, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("0100101e"), BookmarkType.INFO, "Cat1a", "Cmt1B"));
	applyCmd(program, addCmd);

	selectBookmarkType(BookmarkType.ALL_TYPES);

	pressBookmark();
	assertEquals(addr("01001004"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100101e"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("01001004"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001000"), cb.getCurrentAddress());
}
 
Example #16
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchAllTypesBookmark() throws Exception {
	loadProgram("notepad");
	showTool(tool);
	assertEquals(addr("01001000"), cb.getCurrentAddress());

	//
	// Setup:
	// 100c - error bookmark
	// 101c - info bookmark
	// 101e - warning bookmark
	//
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("0100100c"), BookmarkType.ERROR, "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("0100101c"), BookmarkType.INFO, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("0100101e"), BookmarkType.WARNING, "Cat1a", "Cmt1B"));
	applyCmd(program, addCmd);

	selectBookmarkType(BookmarkType.ALL_TYPES);

	pressBookmark();
	assertEquals(addr("0100100c"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100101c"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100101e"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("0100101c"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100100c"), cb.getCurrentAddress());
}
 
Example #17
Source File: OperandLabelDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * This method gets called when the user clicks on the Ok Button.  The base
 * class calls this method.
 */
@Override
protected void okCallback() {
	Program program = programActionContext.getProgram();
	ProgramLocation loc = programActionContext.getLocation();
	OperandFieldLocation location = (OperandFieldLocation) loc;
	Symbol sym = getSymbol(programActionContext);
	String currentLabel = myChoice.getText();
	if (currentLabel.equals(sym.getName(true))) {
		close();
		return;
	}

	ReferenceManager refMgr = program.getReferenceManager();
	SymbolTable symTable = program.getSymbolTable();
	int opIndex = location.getOperandIndex();
	Address addr = location.getAddress();
	Address symAddr = sym.getAddress();
	Reference ref = refMgr.getReference(addr, symAddr, opIndex);

	CompoundCmd cmd = new CompoundCmd("Set Label");
	Namespace scope = null;

	Symbol newSym = findSymbol(symTable, currentLabel, symAddr);
	if (newSym == null) {
		cmd.add(new AddLabelCmd(symAddr, currentLabel, SourceType.USER_DEFINED));
	}
	else {
		scope = newSym.getParentNamespace();
		currentLabel = newSym.getName();
	}
	cmd.add(new AssociateSymbolCmd(ref, currentLabel, scope));

	if (!plugin.getTool().execute(cmd, program)) {
		setStatusText(cmd.getStatusMsg());
		return;
	}
	close();

}
 
Example #18
Source File: JavaAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void processInstructions(Program program, Data constantPoolData,
		ClassFileJava classFile, TaskMonitor monitor) throws CancelledException {

	InstructionIterator instructionIt =
		program.getListing().getInstructions(toAddr(program, JavaLoader.CODE_OFFSET), true);
	AbstractConstantPoolInfoJava[] constantPool = classFile.getConstantPool();
	Map<Integer, Integer> indexMap = getIndexMap(constantPool);
	BootstrapMethods[] bootstrapMethods =
		getBootStrapMethodAttribute(classFile, constantPool, indexMap);

	for (Instruction instruction : instructionIt) {
		monitor.checkCanceled();

		if (!hasConstantPoolReference(instruction.getMnemonicString())) {
			continue;
		}

		if (instruction.getMnemonicString().equals("invokedynamic")) {
			addInvokeDynamicComments(program, constantPool, indexMap, bootstrapMethods,
				instruction);
		}

		int index = (int) (instruction.getScalar(0).getValue() & 0xFFFFFFFF);

		Data referredData = constantPoolData.getComponent(indexMap.get(index));
		instruction.addOperandReference(0, referredData.getAddress(), RefType.DATA,
			SourceType.ANALYSIS);
		CompoundCmd cmd = new CompoundCmd("Add constant pool reference");
		String constantPoolLabel = "CPOOL[" + index + "]";
		cmd.add(
			new AddLabelCmd(referredData.getAddress(), constantPoolLabel, SourceType.ANALYSIS));

		Reference ref = instruction.getOperandReferences(0)[0];
		cmd.add(new AssociateSymbolCmd(ref, constantPoolLabel));
		cmd.applyTo(program);
	}
}
 
Example #19
Source File: DualProgramTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testOpenSameSecondProgram() throws Exception {
	openSecondProgram(diffTestP1, diffTestP1);
	assertNotNull(fp2.getTopLevelAncestor());

	// Check action enablement.
	assertTrue(viewDiffs.isEnabled());
	assertTrue(!applyDiffs.isEnabled());
	assertTrue(!applyDiffsNext.isEnabled());
	assertTrue(!ignoreDiffs.isEnabled());
	assertTrue(!nextDiff.isEnabled());
	assertTrue(!prevDiff.isEnabled());
	assertTrue(diffDetails.isEnabled());
	assertTrue(!diffApplySettings.isEnabled());
	assertTrue(getDiffs.isEnabled());
	assertTrue(selectAllDiffs.isEnabled());
	assertTrue(!setPgm2Selection.isEnabled());

	// Modify the active program.
	setLocation("100f3ff");
	CompoundCmd cmd = new CompoundCmd("test");
	cmd.add(new AddLabelCmd(addr("100f3ff"), "TestLabel", false, SourceType.USER_DEFINED));
	cmd.add(
		new AddLabelCmd(addr("100f3ff"), "AnotherTestLabel", false, SourceType.USER_DEFINED));
	try {
		txId = diffTestP1.startTransaction("Modify Program");
		cmd.applyTo(diffTestP1);
	}
	finally {
		diffTestP1.endTransaction(txId, true);
	}

	// Check that the two programs now differ.
	assertTrue(viewDiffs.isEnabled());
	LayoutModel lm1 = fp1.getLayoutModel();
	LayoutModel lm2 = fp2.getLayoutModel();
	int num1 = lm1.getNumIndexes().intValue();
	int num2 = lm2.getNumIndexes().intValue();
	assertEquals(num1, num2);
}
 
Example #20
Source File: AutoRenamePlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Perform Fragment Auto-Rename on selected Fragments.
 * Rename is performed on all selected Fragments within Program Tree.
 */
void autoRenameCallback(ActionContext context) {

    Object obj = context.getContextObject();
    if (obj instanceof ProgramNode) {
        ProgramNode node = (ProgramNode) obj;

        CompoundCmd cmd = new CompoundCmd("Auto Rename Fragment(s)");
        SymbolTable symTable = currentProgram.getSymbolTable();

        // Find selected Fragments
        TreePath[] selectedPaths = node.getTree().getSelectionPaths();
        boolean ignoreDuplicateNames = (selectedPaths.length > 1);
        for (int i = 0; i < selectedPaths.length; i++) {
            ProgramNode n = (ProgramNode) selectedPaths[i].getLastPathComponent();
            if (!n.isFragment())
                continue;

            // Rename Fragment using minimum address label	
            ProgramFragment f = n.getFragment();
            Address a = f.getMinAddress();
            if (a == null)
                continue; // empty Fragment
            Symbol s = symTable.getPrimarySymbol(a);
            if (s != null) {
                cmd.add(new RenameCmd(f.getTreeName(), false, f.getName(), s.getName(), 
                	ignoreDuplicateNames));
            }
        }

        if (cmd.size() > 0 && !tool.execute(cmd, currentProgram)) {
            tool.setStatusInfo("Error renaming fragment: " + cmd.getStatusMsg());
        }
    }
}
 
Example #21
Source File: RegisterValuesPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void updateValue(Address start, Address end, Address newStart, Address newEnd,
		BigInteger newValue) {
	CompoundCmd cmd = new CompoundCmd("Update Register Range");
	Command cmd1 = new SetRegisterCmd(selectedRegister, start, end, null);
	Command cmd2 = new SetRegisterCmd(selectedRegister, newStart, newEnd, newValue);
	cmd.add(cmd1);
	cmd.add(cmd2);
	tool.execute(cmd, currentProgram);

}
 
Example #22
Source File: FallThroughModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Method autoOverride.
 * @param currentSelection
 */
void autoOverride(AddressSetView view) {
	CompoundCmd cmd = new CompoundCmd("Auto-Override");
	AddressRangeIterator iter = view.getAddressRanges();
	while (iter.hasNext()) {
		override(iter.next(), cmd);
	}
	if (cmd.size() > 0) {
		if (!tool.execute(cmd, program)) {
			tool.setStatusInfo(cmd.getStatusMsg());
		}
	}
}
 
Example #23
Source File: FallThroughModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void clearOverride(AddressSetView view) {
	CompoundCmd cmd = new CompoundCmd("Clear FallThroughs");
	InstructionIterator it = program.getListing().getInstructions(view, true);
	while(it.hasNext()) {
		Instruction inst = it.next();
		if (inst.isFallThroughOverridden()) {
			cmd.add(new ClearFallThroughCmd(inst.getMinAddress()));
		}
	}
	if (cmd.size() > 0) {
		tool.execute(cmd, program);
	}
}
 
Example #24
Source File: BookmarkPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a new bookmark is to be added; called from the add bookmark dialog
 * 
 * @param addr bookmark address.  If null a Note bookmark will set at the 
 * 		  start address of each range in the current selection
 * @param category bookmark category
 * @param comment comment text
 */
public void setNote(Address addr, String category, String comment) {

	CompoundCmd cmd = new CompoundCmd("Set Note Bookmark");

	if (addr != null) {
		// Add address specified within bookmark
		cmd.add(new BookmarkDeleteCmd(addr, BookmarkType.NOTE));
		cmd.add(new BookmarkEditCmd(addr, BookmarkType.NOTE, category, comment));
	}
	else {

		// Create address set with first address only
		AddressSet set = new AddressSet();
		AddressRangeIterator iter = currentSelection.getAddressRanges();
		while (iter.hasNext()) {
			Address minAddr = iter.next().getMinAddress();
			set.addRange(minAddr, minAddr);
		}

		// Add a bookmark at the first address of each address range in
		//   the current selection
		cmd.add(new BookmarkDeleteCmd(set, BookmarkType.NOTE));
		cmd.add(new BookmarkEditCmd(set, BookmarkType.NOTE, category, comment));
	}
	tool.execute(cmd, currentProgram);
}
 
Example #25
Source File: DeleteExternalReferenceNameAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionContext context) {
	Program program = provider.getProgram();
	ExternalManager externalManager = program.getExternalManager();
	List<String> externalNames = provider.getSelectedExternalNames();
	StringBuffer buf = new StringBuffer();
	CompoundCmd cmd = new CompoundCmd("Delete External Program Name");
	for (String externalName : externalNames) {
		boolean hasLocations = externalManager.getExternalLocations(externalName).hasNext();
		if (hasLocations) {
			buf.append("\n     ");
			buf.append(externalName);
		}
		else {
			cmd.add(new RemoveExternalNameCmd(externalName));
		}
	}
	if (cmd.size() > 0) {
		provider.getTool().execute(cmd, program);
	}
	if (buf.length() > 0) {
		Msg.showError(getClass(), provider.getComponent(),
			"Delete Failure", "The following external reference names could not be deleted\n" +
				"because they contain external locations:\n" + buf.toString());
	}

}
 
Example #26
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorBookmark() throws Exception {
	loadProgram("notepad");
	showTool(tool);
	assertEquals(addr("01001000"), cb.getCurrentAddress());
	clearExisingBookmarks();

	// add more to our current set
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001110"), BookmarkType.ERROR, "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01001118"), BookmarkType.NOTE, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001120"), BookmarkType.ERROR, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001130"), BookmarkType.ERROR, "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("01001140"), BookmarkType.ERROR, "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("01001143"), BookmarkType.WARNING, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001150"), BookmarkType.ERROR, "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("01001154"), BookmarkType.INFO, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01001160"), BookmarkType.ERROR, "Cat2b", "Cmt2F"));
	applyCmd(program, addCmd);

	selectBookmarkType(BookmarkType.ERROR);

	pressBookmark();
	assertEquals(addr("01001110"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001120"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001130"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001140"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001150"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001160"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001160"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("01001150"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001140"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001130"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001120"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001110"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01001110"), cb.getCurrentAddress());
}
 
Example #27
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testWarningBookmark() throws Exception {
	loadProgram("notepad");
	showTool(tool);
	assertEquals(addr("01001000"), cb.getCurrentAddress());
	clearExisingBookmarks();

	// add more to our current set
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01003e2c"), BookmarkType.WARNING, "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("01003e2e"), BookmarkType.NOTE, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01003e30"), BookmarkType.WARNING, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01003e32"), BookmarkType.WARNING, "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("01003e38"), BookmarkType.WARNING, "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("01003e3a"), BookmarkType.WARNING, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01003e40"), BookmarkType.ANALYSIS, "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("01003e41"), BookmarkType.INFO, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("01003e43"), BookmarkType.WARNING, "Cat2b", "Cmt2F"));
	applyCmd(program, addCmd);

	selectBookmarkType(BookmarkType.WARNING);

	pressBookmark();
	assertEquals(addr("01003e2c"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e30"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e32"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e38"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e3a"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e43"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e43"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("01003e3a"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e38"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e32"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e30"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e2c"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("01003e2c"), cb.getCurrentAddress());
}
 
Example #28
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomBobBookmark() throws Exception {
	loadProgram("notepad");
	showTool(tool);

	assertEquals(addr("01001000"), cb.getCurrentAddress());
	ImageIcon bookmarkBobIcon =
		ResourceManager.loadImage("images/applications-engineering.png");

	BookmarkType bob = bookmarkManager.defineType("BOB", bookmarkBobIcon, Color.YELLOW, 0);

	String typeString = bob.getTypeString();

	// add more to our current set
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("0100529b"), typeString, "Cat1a", "Cmt1A"));
	addCmd.add(new BookmarkEditCmd(addr("0100529d"), BookmarkType.NOTE, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("0100529e"), typeString, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("010052a1"), typeString, "Cat1b", "Cmt1C"));
	addCmd.add(new BookmarkEditCmd(addr("010052a3"), BookmarkType.WARNING, "Cat1b", "Cmt1D"));
	addCmd.add(new BookmarkEditCmd(addr("010052a4"), typeString, "Cat1a", "Cmt1B"));
	addCmd.add(new BookmarkEditCmd(addr("010052a6"), BookmarkType.ANALYSIS, "Cat2a", "Cmt2E"));
	addCmd.add(new BookmarkEditCmd(addr("010052a7"), typeString, "Cat2b", "Cmt2F"));
	applyCmd(program, addCmd);

	selectBookmarkType("Custom");

	pressBookmark();
	assertEquals(addr("0100529b"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("0100529e"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("010052a1"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("010052a4"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("010052a7"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("010052a7"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("010052a4"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("010052a1"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("0100529e"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("0100529b"), cb.getCurrentAddress());

	pressBookmark();
	assertEquals(addr("0100529b"), cb.getCurrentAddress());
}
 
Example #29
Source File: NextPrevCodeUnitPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testBookmarksOffcutInStructures() throws Exception {
	loadProgram("notepad");
	showTool(tool);
	assertEquals(addr("01001000"), cb.getCurrentAddress());

	//
	// Setup:
	// 1000 - info bookmark
	// 1006 - offcut info bookmark
	// 101e - info bookmark
	//
	CompoundCmd addCmd = new CompoundCmd("Add Bookmarks");
	addCmd.add(new BookmarkEditCmd(addr("01001000"), BookmarkType.INFO, "Cat1a", "A"));
	addCmd.add(new BookmarkEditCmd(addr("01001004"), BookmarkType.INFO, "Cat1a", "B"));
	addCmd.add(new BookmarkEditCmd(addr("0100100d"), BookmarkType.INFO, "Cat1a", "C"));
	addCmd.add(new BookmarkEditCmd(addr("01001010"), BookmarkType.INFO, "Cat1a", "C"));
	addCmd.add(new BookmarkEditCmd(addr("01001015"), BookmarkType.INFO, "Cat1a", "D"));
	addCmd.add(new BookmarkEditCmd(addr("01001020"), BookmarkType.INFO, "Cat1a", "E"));

	StructureDataType struct = new StructureDataType("Test", 0);
	struct.add(new DWordDataType());
	struct.add(new DWordDataType());
	struct.add(new DWordDataType());
	assertEquals(12, struct.getLength());

	addCmd.add(new CreateDataCmd(addr("0100100c"), struct));

	applyCmd(program, addCmd);

	selectBookmarkType(BookmarkType.ALL_TYPES);

	pressBookmark();
	assertEquals(addr("01001004"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100100c"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001010"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001014"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001020"), cb.getCurrentAddress());

	toggleDirection();

	pressBookmark();
	assertEquals(addr("01001014"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001010"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("0100100c"), cb.getCurrentAddress());
	pressBookmark();
	assertEquals(addr("01001004"), cb.getCurrentAddress());
}
 
Example #30
Source File: FallThroughModel.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void setFallThrough(Address addr, CompoundCmd cmd) {
	Instruction inst = program.getListing().getInstructionAfter(addr);
	if (inst != null) {
		cmd.add(new SetFallThroughCmd(addr, inst.getMinAddress()));
	}
}