Java Code Examples for ghidra.program.model.listing.Listing#getCodeUnitAt()

The following examples show how to use ghidra.program.model.listing.Listing#getCodeUnitAt() . 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: DisplayableEolTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testReferenceToOffcutStringData() throws Exception {

	Address dataStartAddress = addr("0x1001234");
	Address offcutAddress = dataStartAddress.add(2);
	Command cmd =
		new AddMemRefCmd(addr("0x1001000"), offcutAddress, SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));

	// with this at false, all of the string will be rendered
	boolean useAbbreviatedComments = false;

	DisplayableEol displayableEol =
		new DisplayableEol(cu, true, true, true, false, 5, useAbbreviatedComments);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"one.two\"", comments[0]);
}
 
Example 2
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testReferenceToOffcutStringData_UseAbbreviatedCommentOption() throws Exception {
	//
	// When on, the 'Use Abbreviated Automatic Comments' option will show only the offcut
	// portion of offcut string data.
	//
	Address dataStartAddress = addr("0x1001234");
	Address offcutAddress = dataStartAddress.add(4);
	Command cmd =
		new AddMemRefCmd(addr("0x1001000"), offcutAddress, SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));

	// with this at true, only the used part of the string will be rendered
	boolean useAbbreviatedComments = true;

	DisplayableEol displayableEol =
		new DisplayableEol(cu, true, true, true, false, 5, useAbbreviatedComments);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"two\"", comments[0]);// full string is one.two
}
 
Example 3
Source File: DeleteExitCommentsScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.POST_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		String[] comment = cu.getCommentAsArray(CodeUnit.POST_COMMENT);
		if (comment.length == 1 && comment[0].endsWith(EXIT_COMMENT)) {
			cu.setComment(CodeUnit.POST_COMMENT, null);
			++updateCount;
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " exit post " + cmt + ".");
	}
	else {
		println("Did not find any exit post comments.");
	}
}
 
Example 4
Source File: X64DbgExport.java    From GhidraX64Dbg with MIT License 5 votes vote down vote up
/**
 * Adds comments labels to the X64 database. Does not retrieve plate comments
 * and pre/post comments as X64 does not really support them.
 */
public void populateComments() {

	Listing listing = currentProgram.getListing();

	for (Address commentedAddress : listing.getCommentAddressIterator(currentProgram.getMemory(), true)) {

		CodeUnit cu = listing.getCodeUnitAt(commentedAddress);

		// Skip empty code units or empty comment
		if (cu == null) {
			continue;
		}

		String comment;
		String start = "0x" + Long.toHexString(commentedAddress.subtract(imageBase));

		// Process EOL comments
		comment = cu.getComment(CodeUnit.EOL_COMMENT);
		if (comment != null && comment.length() != 0) {
			this.comments.add(new X64Label(start, comment));
		}

		// Process repeatable comments
		comment = cu.getComment(CodeUnit.REPEATABLE_COMMENT);
		if (cu.getComment(CodeUnit.REPEATABLE_COMMENT) != null) {
			this.comments.add(new X64Label(start, comment));
		}
	}
}
 
Example 5
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickLatest() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Post", addr("0x100230d"), KEEP_LATEST, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Repeatable", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Post", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example 6
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickMine() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Post", addr("0x100230d"), KEEP_MY, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Repeatable", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Post", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("My Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("My EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("My Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("My Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("My EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("My Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example 7
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickVarious() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_MY, false);
		chooseComment("Post", addr("0x100230d"), KEEP_LATEST, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
		chooseComment("Repeatable", addr("0x10028b1"), KEEP_BOTH, false);
//		chooseComment("Post", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
			cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example 8
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoPossiblePointerEOL() throws Exception {

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x110"));
	DisplayableEol displayableEol = new DisplayableEol(cu, true, true, true, false, 5, true);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("?  ->  00000120", comments[0]);
}
 
Example 9
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testReferenceToStringData() throws Exception {

	Command cmd = new AddMemRefCmd(addr("0x1001000"), addr("0x1001234"),
		SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));
	DisplayableEol displayableEol = new DisplayableEol(cu, true, true, true, false, 5, true);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"one.two\"", comments[0]);
}
 
Example 10
Source File: DeleteDeadDefaultPlatesScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.PLATE_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		if (cu != null) {
			String[] comment = cu.getCommentAsArray(CodeUnit.PLATE_COMMENT);
			if (comment.length == 1 && comment[0].equals(DEAD_PLATE)) {
				cu.setComment(CodeUnit.PLATE_COMMENT, null);
				++updateCount;
			}
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " default plate " + cmt + ".");
	}
	else {
		println("Did not find any dead plate comments.");
	}
}
 
Example 11
Source File: DeleteEmptyPlateCommentsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.PLATE_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		if (cu != null) {
			String[] comment = cu.getCommentAsArray(CodeUnit.PLATE_COMMENT);
			if (comment.length == 1 && comment[0].equals(EMPTY_PLATE)) {
				cu.setComment(CodeUnit.PLATE_COMMENT, null);
				++updateCount;
			}
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " emtpy plate " + cmt + ".");
	}
	else {
		println("Did not find any empty plate comments.");
	}
}
 
Example 12
Source File: AddCommentToProgramScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
    Address minAddress = currentProgram.getMinAddress();
    Listing listing = currentProgram.getListing();
    CodeUnit codeUnit = listing.getCodeUnitAt( minAddress );
    codeUnit.setComment( CodeUnit.PLATE_COMMENT, "AddCommentToProgramScript - This is an added comment!" );
}
 
Example 13
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddDiffDoNotUseForAllPickBoth() throws Exception {
	setupAddDiffCommentUseForAll();

	executeMerge(ASK_USER);

	chooseComment("Plate", addr("0x100230d"), KEEP_BOTH, false);
	chooseComment("Pre", addr("0x100230d"), KEEP_BOTH, false);
	chooseComment("Eol", addr("0x100230d"), KEEP_BOTH, false);
	chooseComment("Repeatable", addr("0x100230d"), KEEP_BOTH, false);
	chooseComment("Post", addr("0x100230d"), KEEP_BOTH, false);

	chooseComment("Plate", addr("0x10028b1"), KEEP_BOTH, false);
	chooseComment("Pre", addr("0x10028b1"), KEEP_BOTH, false);
	chooseComment("Eol", addr("0x10028b1"), KEEP_BOTH, false);
	chooseComment("Repeatable", addr("0x10028b1"), KEEP_BOTH, false);
	chooseComment("Post", addr("0x10028b1"), KEEP_BOTH, false);

	waitForMergeCompletion();

	Listing listing = resultProgram.getListing();

	CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
	assertEquals("Latest Plate Comment" + "\n" + "My Plate Comment",
		cu.getComment(CodeUnit.PLATE_COMMENT));
	assertEquals("Latest Pre Comment" + "\n" + "My Pre Comment",
		cu.getComment(CodeUnit.PRE_COMMENT));
	assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
		cu.getComment(CodeUnit.EOL_COMMENT));
	assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
		cu.getComment(CodeUnit.REPEATABLE_COMMENT));
	assertEquals("Latest Post Comment" + "\n" + "My Post Comment",
		cu.getComment(CodeUnit.POST_COMMENT));

	cu = listing.getCodeUnitAt(addr("0x100230d"));
	assertEquals("Latest Plate Comment" + "\n" + "My Plate Comment",
		cu.getComment(CodeUnit.PLATE_COMMENT));
	assertEquals("Latest Pre Comment" + "\n" + "My Pre Comment",
		cu.getComment(CodeUnit.PRE_COMMENT));
	assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
		cu.getComment(CodeUnit.EOL_COMMENT));
	assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
		cu.getComment(CodeUnit.REPEATABLE_COMMENT));
	assertEquals("Latest Post Comment" + "\n" + "My Post Comment",
		cu.getComment(CodeUnit.POST_COMMENT));
}
 
Example 14
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickBoth() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Post", addr("0x100230d"), KEEP_BOTH, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Repeatable", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Post", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("Latest Plate Comment" + "\n" + "My Plate Comment",
			cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment" + "\n" + "My Pre Comment",
			cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
			cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment" + "\n" + "My Post Comment",
			cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("Latest Plate Comment" + "\n" + "My Plate Comment",
			cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment" + "\n" + "My Pre Comment",
			cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
			cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment" + "\n" + "My Post Comment",
			cu.getComment(CodeUnit.POST_COMMENT));
	}