Java Code Examples for ghidra.util.task.TaskMonitor#DUMMY

The following examples show how to use ghidra.util.task.TaskMonitor#DUMMY . 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: DWARFAttributeFactoryTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	ToyProgramBuilder builder = new ToyProgramBuilder("Test", true);
	Program ghidraProgram = builder.getProgram();

	prog = new DWARFProgram(ghidraProgram, new DWARFImportOptions(), TaskMonitor.DUMMY,
		new NullSectionProvider());
	stringTable = prog.getDebugStrings();
	attribFactory = prog.getAttributeFactory();

	cu = new DWARFCompilationUnit(prog, 0x1000, 0x2000, 0, DWARFCompilationUnit.DWARF_32,
		(short) 4, 0, (byte) 8, 0, 0, null);
	cu64 = new DWARFCompilationUnit(prog, 0x2000, 0x4000, 0, DWARFCompilationUnit.DWARF_64,
		(short) 4, 0, (byte) 8, 0, 0, null);

	assertTrue("These tests were written for big endian", prog.isBigEndian());
}
 
Example 2
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleStepForwardAll() {

	// search all fields in the forward direction
	Address entry = addr(0x0100415a);
	Function func = program.getFunctionManager().getFunctionAt(entry);
	String signature = func.getPrototypeString(false, false);
	ProgramLocation startLoc =
		new FunctionSignatureFieldLocation(program, entry, null, 34, signature);

	// example
	// dword ptr param, EAX
	String searchText = "param";
	SearchOptions options = new SearchOptions(searchText, false, true, false);
	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, null, options, TaskMonitor.DUMMY);

	assertTrue(searcher.hasNext());
	while (searcher.hasNext()) {
		ProgramLocation location = searcher.next();
		testForMatchingText(searchText, location);
	}
}
 
Example 3
Source File: DWARFTestBase.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	program = createDefaultProgram(testName.getMethodName(), ProgramBuilder._TOY, this);
	dataMgr = program.getDataTypeManager();
	startTransaction();

	AutoAnalysisManager mgr = AutoAnalysisManager.getAnalysisManager(program);
	DataTypeManagerService dtms = mgr.getDataTypeManagerService();
	builtInDTM = dtms.getBuiltInDataTypesManager();

	dwarfProg = new DWARFProgram(program, new DWARFImportOptions(), TaskMonitor.DUMMY,
		new NullSectionProvider());
	rootCP = dwarfProg.getUncategorizedRootDNI().asCategoryPath();

	cu = new MockDWARFCompilationUnit(dwarfProg, 0x1000, 0x2000, 0,
		DWARFCompilationUnit.DWARF_32, (short) 4, 0, (byte) 8, 0);
	cu2 = new MockDWARFCompilationUnit(dwarfProg, 0x3000, 0x4000, 0,
		DWARFCompilationUnit.DWARF_32, (short) 4, 0, (byte) 8, 1);

	dwarfProg.getCompilationUnits().add(cu);
	dwarfProg.getCompilationUnits().add(cu2);

	DWARFImportSummary importSummary = new DWARFImportSummary();
	dwarfDTM = new DWARFDataTypeManager(dwarfProg, dataMgr, builtInDTM, importSummary);
}
 
Example 4
Source File: AbstractVisualGraphLayout.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected LayoutPositions<V, E> doCalculateLocations(VisualGraph<V, E> g,
		TaskMonitor taskMonitor) {

	GridLocationMap<V, E> gridLocations = null;
	try {
		monitor = taskMonitor;
		gridLocations = performInitialGridLayout(g);
		LayoutPositions<V, E> positions = positionInLayoutSpaceFromGrid(g, gridLocations);
		return positions;
	}
	catch (CancelledException ce) {
		return LayoutPositions.createEmptyPositions();
	}
	finally {
		if (gridLocations != null) {
			gridLocations.dispose();
		}
		monitor = TaskMonitor.DUMMY;
	}
}
 
Example 5
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchInstructions() {
	Address entry = addr(0x0100415a);

	Function func = program.getFunctionManager().getFunctionAt(entry);
	String signature = func.getPrototypeString(false, false);
	ProgramLocation startLoc =
		new FunctionSignatureFieldLocation(program, entry, null, 34, signature);

	// example
	// dword ptr param, EAX
	SearchOptions options = new SearchOptions("param", false, false, false, false, true, true,
		false, false, false, true, false, false);
	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, null, options, TaskMonitor.DUMMY);

	//set up list of answers
	ArrayList<Address> startList = new ArrayList<>();
	startList.add(addr(0x0100416c));
	startList.add(addr(0x01004186));
	startList.add(addr(0x01004189));
	startList.add(addr(0x0100419c));

	//check that the text is found there in the correct field
	checkTextFound(startList, OperandFieldLocation.class);
}
 
Example 6
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void doTestComments(AddressSetView as, int matchCount) {
	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x01001000));
	String searchText = "EOL comment";
	//@formatter:off
	SearchOptions options =
		new SearchOptions(searchText, 
			false /*quick*/, 
			false /*functions*/, 
			true  /*comments*/, 
			false /*labels*/, 
			false /*instruction mnemonics*/, 
			false /*instruction operands*/, 
			false /*data mnemonics*/, 
			false /*data operands*/, 
			false /*case sensitive*/,
			true  /*is forward*/,
			false /* includeNonLoadedBlocks */,
			false /* searchAll */
			);
	//@formatter:on

	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, null, options, TaskMonitor.DUMMY);

	//set up list of answers
	ArrayList<Address> startList = new ArrayList<>();

	startList.add(addr(0x01006642));
	startList.add(addr(0x01006648));

	//check that the text is found there in the correct field
	checkTextFound(startList, EolCommentFieldLocation.class);
}
 
Example 7
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void doTestFunctions(AddressSetView as) {
	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x01001000));
	String searchText = "ghidra";
	//@formatter:off
	SearchOptions options =
		new SearchOptions(searchText, 
			false /*quick*/, 
			true  /*functions*/, 
			false /*comments*/, 
			false /*labels*/, 
			false /*instruction mnemonics*/, 
			false /*instruction operands*/, 
			false /*data mnemonics*/, 
			false /*data operands*/, 
			false /*case sensitive*/,
			true  /*is forward*/,
			false /* includeNonLoadedBlocks */,
			false /* searchAll */
			);
	//@formatter:on

	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, as, options, TaskMonitor.DUMMY);

	//set up list of answers
	ArrayList<Address> startList = new ArrayList<>();

	startList.add(addr(0x01002cf5));

	//check that the text is found there in the correct field
	checkTextFound(startList, FunctionNameFieldLocation.class);
}
 
Example 8
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchDataBackward() {
	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x01002000));
	//@formatter:off
	SearchOptions options =
		new SearchOptions("unicode", 
			false /*quick*/, 
			false /*functions*/, 
			false /*comments*/, 
			false /*labels*/, 
			true  /*instruction mnemonics*/, 
			true  /*instruction operands*/, 
			true  /*data mnemonics*/, 
			true  /*data operands*/, 
			false /*case sensitive*/,
			false /*is forward*/,
			false /* includeNonLoadedBlocks */,
			false /* search All */);
	//@formatter:on

	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, null, options, TaskMonitor.DUMMY);

	//set up list of answers
	ArrayList<Address> startList = new ArrayList<>();
	startList.add(addr(0x01001740));
	startList.add(addr(0x01001708));

	//check that the text is found there in the correct field
	checkTextFound(startList, MnemonicFieldLocation.class);
}
 
Example 9
Source File: GraphAlgorithmsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testDominance_GetDominators() throws CancelledException {

	/*
	 		v1->.		 		
	 		 |  |
	 		v2  |
	 		 |  |
	 		v3  |
	 		 |  |
	 		v4--<
	 */
	TestV v1 = vertex(1);
	TestV v2 = vertex(2);
	TestV v3 = vertex(3);
	TestV v4 = vertex(4);

	edge(v1, v2);
	edge(v2, v3);
	edge(v3, v4);
	edge(v1, v4);

	ChkDominanceAlgorithm<TestV, TestE> algo =
		new ChkDominanceAlgorithm<>(g, TaskMonitor.DUMMY);

	assertContainsExactly(algo.getDominators(v1), v1);
	assertContainsExactly(algo.getDominators(v2), v1, v2);
	assertContainsExactly(algo.getDominators(v3), v1, v2, v3);
	assertContainsExactly(algo.getDominators(v4), v1, v4);
}
 
Example 10
Source File: SimpleBlockIterator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
	 * Creates a new iterator that will iterate over the
	 * program within a given address range set. All blocks which 
	 * overlap the address set will be returned.
	 * <P>
	 *
	 * @param model  the BlockModel the iterator will use in its operations.
	 * @param set    the address range set which the iterator is to be
	 *               restricted to.
	 * @param monitor task monitor which allows user to cancel operation.
	 * @throws CancelledException if the monitor cancels the operation.
	 */
	public SimpleBlockIterator(SimpleBlockModel model, AddressSetView set, TaskMonitor monitor)
			throws CancelledException {

// ?? ITERATOR HAS BEEN MODIFIED TO ONLY RETURN INSTRUCTION BLOCKS

		this.model = model;
		this.monitor = monitor != null ? monitor : TaskMonitor.DUMMY;
		listing = model.getProgram().getListing();
		addrSet = set;
		rangeIter = set.getAddressRanges();
		nextAddr = set.getMinAddress();
		if (nextAddr == null) {
			nextBlock = null;
		}
		else {
			nextBlock = model.getFirstCodeBlockContaining(nextAddr, monitor);
		}
		if (nextBlock != null) {
			nextAddr = nextBlock.getMaxAddress();
//        	// Data block only included if it references an instruction
			CodeUnit codeUnit = listing.getCodeUnitAt(nextBlock.getMinAddress());
			if (codeUnit instanceof Data) {
//        		if (!dataReferencesInstruction((Data)codeUnit)) {
				nextBlock = null;
//        		}
			}
		}
	}
 
Example 11
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleMatchesAtSameAddress() {

	// one address in this range contains an instruction with multiple matches for that address
	AddressSet as = new AddressSet();
	as.add(addr(0x1001000), addr(0x1001100));

	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x01001000));
	SearchOptions searchAllOptions = new SearchOptions("CreateDCW",
		false /* not case sensitive */, true /*forward*/, false /*includeNonLoadedBlocks*/);

	searcher = new ListingDisplaySearcher(tool, program, startLoc, as, searchAllOptions,
		TaskMonitor.DUMMY);

	assertTrue(searcher.hasNext());

	ProgramLocation loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x01001068), loc.getByteAddress());
	assertTrue(loc instanceof LabelFieldLocation);
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x01001068), loc.getByteAddress());
	assertTrue(loc instanceof EolCommentFieldLocation);
	loc = searcher.next();
	assertNull(loc);

}
 
Example 12
Source File: SymbolDB.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Reference[] getReferences(TaskMonitor monitor) {
	lock.acquire();
	try {
		checkIsValid();
		if (monitor == null) {
			monitor = TaskMonitor.DUMMY;
		}

		if (monitor.getMaximum() == 0) {
			// If the monitor has not been initialized, then the progress will not correctly
			// display anything as setProgress() is called below.  We can't know what to
			// initialize to without counting all the references, which is as much work as
			// this method.
			monitor = new UnknownProgressWrappingTaskMonitor(monitor, 20);
		}

		ReferenceManager rm = symbolMgr.getReferenceManager();
		ReferenceIterator iter = rm.getReferencesTo(address);
		boolean isPrimary = this.isPrimary();
		ArrayList<Reference> list = new ArrayList<>();
		int cnt = 0;
		while (iter.hasNext()) {
			if (monitor.isCancelled()) {
				break; // return partial list
			}
			Reference ref = iter.next();
			long symbolID = ref.getSymbolID();
			if (symbolID == key || (isPrimary && symbolID < 0)) {
				list.add(ref);
				monitor.setProgress(cnt++);
			}
		}
		Reference[] refs = new Reference[list.size()];
		return list.toArray(refs);
	}
	finally {
		lock.release();
	}
}
 
Example 13
Source File: PdbParserTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testApplyFunctions() throws Exception {

	createdFiles = createFiles(PdbLocation.NONE, PdbXmlLocation.OWN_DIR);

	buildPdbXml();

	File pdb = PdbParser.findPDB(testProgram, false, pdbXmlDir.getAbsolutePath());

	AutoAnalysisManager mgr = AutoAnalysisManager.getAnalysisManager(testProgram);
	DataTypeManagerService dataTypeManagerService = mgr.getDataTypeManagerService();
	PdbParser parser =
		new PdbParser(pdb, testProgram, dataTypeManagerService, false, TaskMonitor.DUMMY);

	parser.openDataTypeArchives();
	parser.parse();
	parser.applyTo(new MessageLog());

	// Now check program to see if the function has been successfully applied
	AddressFactory addressFactory = testProgram.getAddressFactory();

	FunctionManager functionManager = testProgram.getFunctionManager();

	for (TestFunction currentFunction : programFunctions) {
		String currentFunctionAddress = currentFunction.getAddress();
		FunctionDB possibleFunction = (FunctionDB) functionManager.getFunctionAt(
			addressFactory.getAddress(currentFunctionAddress));

		assertNotNull("Expected function at address: " + currentFunctionAddress,
			possibleFunction);
		assertEquals("function1", possibleFunction.getName());
	}
}
 
Example 14
Source File: AutoTableDisassemblerModel.java    From ghidra with Apache License 2.0 4 votes vote down vote up
AutoTableDisassemblerModel(ServiceProvider sp, AutoTableDisassemblerPlugin plugin) {

		super(MODEL_NAME, sp, null, TaskMonitor.DUMMY, true);
		this.plugin = plugin;
	}
 
Example 15
Source File: GraphAlgorithmsTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDominated_MultipleSources() throws CancelledException {
	/*		 
	source 		v1
		 		 |
	source 	v2	v3
		 	 |	 |
		 	v4	v5
		 	 \   |
		 	  \  |
		 	   -v6			 	    
	             |
	 			v7
			   / |
	          /  | 
	        v8   v9
	         \   |
	          \  |
	           - v10
	             |
	             |
	            v11					
				 	   
	*/

	TestV v1 = vertex(1);  // Root
	TestV v2 = vertex(2);  // Root
	TestV v3 = vertex(3);
	TestV v4 = vertex(4);
	TestV v5 = vertex(5);
	TestV v6 = vertex(6);
	TestV v7 = vertex(7);
	TestV v8 = vertex(8);
	TestV v9 = vertex(9);
	TestV v10 = vertex(10);
	TestV v11 = vertex(11);

	edge(v1, v3);
	edge(v3, v5);
	edge(v5, v6);

	edge(v2, v4);
	edge(v4, v6);

	edge(v6, v7);
	edge(v7, v8);
	edge(v7, v9);
	edge(v8, v10);
	edge(v9, v10);
	edge(v10, v11);

	ChkDominanceAlgorithm<TestV, TestE> algo =
		new ChkDominanceAlgorithm<>(g, TaskMonitor.DUMMY);

	//@formatter:off
	Collection<TestE> dominated = findDominance(v1, algo);
	assertContainsExactly(dominated, edge(v1, v3),
								  	 edge(v3, v5));
	//@formatter:on

	dominated = findDominance(v2, algo);
	assertContainsExactly(dominated, edge(v2, v4));

	dominated = findDominance(v3, algo);
	assertContainsExactly(dominated, edge(v3, v5));

	dominated = findDominance(v4, algo);
	assertTrue(dominated.isEmpty());

	dominated = findDominance(v5, algo);
	assertTrue(dominated.isEmpty());

	//@formatter:off
	dominated = findDominance(v6, algo);
	assertContainsExactly(dominated, edge(v6, v7),
								  	 edge(v7, v8),
								  	 edge(v7, v9),
								  	 edge(v8, v10),
								  	 edge(v9, v10),
								  	 edge(v10, v11));
	
	dominated = findDominance(v7, algo);
	assertContainsExactly(dominated, edge(v7, v8),
								  	 edge(v7, v9),
								  	 edge(v8, v10),
								  	 edge(v9, v10),
								  	 edge(v10, v11));
	//@formatter:on

	dominated = findDominance(v8, algo);
	assertTrue(dominated.isEmpty());

	dominated = findDominance(v9, algo);
	assertTrue(dominated.isEmpty());

	dominated = findDominance(v10, algo);
	assertContainsExactly(dominated, edge(v10, v11));

}
 
Example 16
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testRangeWithAddressInsideOfThatRange() {
	//
	// Set up a range that contains multiple matches.    Put the start address past one 
	// of the addresses and make sure that match is not found.
	// 
	AddressSet as = new AddressSet();
	as.add(addr(0x100415a), addr(0x10041a5));

	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x1004178));
	//@formatter:off
	SearchOptions options =
		new SearchOptions("LAB", 
			false /*quick*/, 
			false /*functions*/, 
			false /*comments*/, 
			true  /*labels*/, 
			true  /*instruction mnemonics*/, 
			true  /*instruction operands*/, 
			false /*data mnemonics*/, 
			false /*data operands*/, 
			false /*case sensitive*/,
			true  /*is forward*/,
			false /* includeNonLoadedBlocks */,
			false /* searchAll */
			);
	//@formatter:on

	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, as, options, TaskMonitor.DUMMY);

	// originally had 8 matches, but since we moved the start address past the start of the
	// range, we now only have 7
	//check that the text is found there in the correct field
	checkTextFound(addr(0x01004192), LabelFieldLocation.class);
	checkTextFound(addr(0x01004194), OperandFieldLocation.class);
	checkTextFound(addr(0x0100419a), OperandFieldLocation.class);
	checkTextFound(addr(0x0100419c), LabelFieldLocation.class);
	checkTextFound(addr(0x0100419f), OperandFieldLocation.class);
	checkTextFound(addr(0x010041a1), LabelFieldLocation.class);
	checkTextFound(addr(0x010041a4), LabelFieldLocation.class);
}
 
Example 17
Source File: ReferenceUtils.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Returns all references (locations) that use the given datatype.
 * <br>
 * <b>Note: </b> This method call may take a long time, as it must search all of the
 * data within the program and may also perform long running tasks, like decompiling every
 * function in the program.
 * <br>
 * @param accumulator the results storage
 * @param dataType The datatype for which to find references.
 * @param fieldName optional field name for which to search; the <tt>dataType</tt> must be
 *                  a {@link Composite} to search for a field
 * @param program The program from within which to find references.
 * @param discoverTypes if true, the {@link DataTypeReferenceFinder} service 
 *                will be used to search for data types that are not applied in memory.  
 *                Using the service will be slower, but will recover type usage that could
 *                not be found by examining the Listing.
 * @param monitor A task monitor to be updated as data is searched; if this is null, then a
 *        dummy monitor will be used.
 * @throws CancelledException if the monitor is cancelled
 */
public static void findDataTypeReferences(Accumulator<LocationReference> accumulator,
		DataType dataType, String fieldName, Program program, boolean discoverTypes,
		TaskMonitor monitor) throws CancelledException {

	// Note: none of the params can be null, but this one gets used much later, so check now
	Objects.requireNonNull(dataType, () -> "Data Type cannot be null");

	// sanity check
	if (fieldName != null && !(dataType instanceof Composite)) {
		throw new IllegalArgumentException("Can only search for a field with a Composite type");
	}

	if (monitor == null) {
		monitor = TaskMonitor.DUMMY;
	}

	Listing listing = program.getListing();

	long dataCount = listing.getNumDefinedData();
	int functionCount = program.getFunctionManager().getFunctionCount();
	int totalCount = (int) dataCount + functionCount;

	monitor.initialize(totalCount);

	// Mimic a set in case the client passes in an accumulator that allows duplicates.  This
	// seems a bit cleaner than adding checks for 'accumulator.contains(ref)' throughout
	// the code.
	Accumulator<LocationReference> asSet = asSet(accumulator);

	if (fieldName == null) {
		// It only makes sense to search here when we do not have a field

		boolean localsOnly = discoverTypes;
		FunctionIterator iterator = listing.getFunctions(false);
		findDataTypeMatchesInFunctionHeaders(asSet, iterator, dataType, localsOnly, monitor);

		// external functions don't get searched by type discovery  
		localsOnly = false;
		iterator = listing.getExternalFunctions();
		findDataTypeMatchesInFunctionHeaders(asSet, iterator, dataType, localsOnly, monitor);
	}

	Predicate<Data> dataMatcher = data -> {
		DataType baseType = getBaseDataType(data.getDataType());
		boolean matches = dataTypesMatch(dataType, baseType);
		return matches;
	};
	findDataTypeMatchesInDefinedData(asSet, program, dataMatcher, fieldName, monitor);

	if (discoverTypes) {
		findDataTypeMatchesOutsideOfListing(asSet, program, dataType, fieldName, monitor);
	}

	monitor.checkCanceled();
}
 
Example 18
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testStructures2() throws Exception {

	showTool(tool);
	Structure struct = new StructureDataType("aStruct", 0);
	DataType floatDt = new FloatDataType();
	DataTypeComponent dtc = struct.add(floatDt);
	dtc = struct.add(floatDt);
	dtc.setComment("this is another float");
	Listing listing = program.getListing();

	int transactionID = program.startTransaction("test");
	try {
		DataType dt = program.getDataTypeManager().addDataType(struct,
			DataTypeConflictHandler.DEFAULT_HANDLER);
		floatDt = program.getDataTypeManager().addDataType(new FloatDataType(),
			DataTypeConflictHandler.DEFAULT_HANDLER);
		listing.createData(addr(0x0100689b), dt);
		listing.createData(addr(0x0100688c), floatDt);
		listing.createData(addr(0x01006890), floatDt);

		Data data = listing.getDataAt(addr(0x0100688c));
		data.setComment(CodeUnit.EOL_COMMENT, "this is a float data type");
		data = listing.getDataAt(addr(0x01006890));
		data.setComment(CodeUnit.EOL_COMMENT, "this is another float data type");

	}
	finally {
		program.endTransaction(transactionID, true);
	}
	program.flushEvents();
	waitForPostedSwingRunnables();

	// open the structure
	cb.goToField(addr(0x0100689b), "+", 0, 0);
	click(cb, 1);
	waitForPostedSwingRunnables();

	ProgramSelection sel = new ProgramSelection(addr(0x0100688c), addr(0x0100689f));
	tool.firePluginEvent(new ProgramSelectionPluginEvent("test", sel, program));
	SearchOptions options = new SearchOptions("float", false, true, false); // all fields

	ProgramLocation startLoc = new EolCommentFieldLocation(program, addr(0x0100688c), null,
		new String[] { "this is a float data type" }, 0, 3, 0);
	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, sel, options, TaskMonitor.DUMMY);

	ProgramLocation loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x0100688c), loc.getByteAddress());
	assertTrue(loc instanceof CommentFieldLocation);
	//
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x01006890), loc.getByteAddress());
	assertTrue(loc instanceof MnemonicFieldLocation);
	MnemonicFieldLocation mloc = (MnemonicFieldLocation) loc;
	assertEquals("float", mloc.getMnemonic());
	//
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x01006890), loc.getByteAddress());
	assertTrue(loc instanceof CommentFieldLocation);
	//
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x0100689b), loc.getByteAddress());
	assertTrue(loc instanceof MnemonicFieldLocation);
	mloc = (MnemonicFieldLocation) loc;
	assertEquals("float", mloc.getMnemonic());
	//
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x0100689f), loc.getByteAddress());
	assertTrue(loc instanceof MnemonicFieldLocation);
	mloc = (MnemonicFieldLocation) loc;
	assertEquals("float", mloc.getMnemonic());
	//
	loc = searcher.next();
	assertNotNull(loc);
	assertEquals(addr(0x0100689f), loc.getByteAddress());
	assertTrue(loc instanceof CommentFieldLocation);

	assertTrue(!searcher.hasNext());
	assertNull(searcher.next());
}
 
Example 19
Source File: ListingDisplaySearcherTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testStructures() throws Exception {

	showTool(tool);
	Structure struct = new StructureDataType("aStruct", 0);
	DataType floatDt = new FloatDataType();
	DataTypeComponent dtc = struct.add(floatDt);
	dtc = struct.add(floatDt);
	dtc.setComment("this is another float");
	Listing listing = program.getListing();

	int transactionID = program.startTransaction("test");
	try {
		DataType dt = program.getDataTypeManager().addDataType(struct,
			DataTypeConflictHandler.DEFAULT_HANDLER);
		floatDt = program.getDataTypeManager().addDataType(new FloatDataType(),
			DataTypeConflictHandler.DEFAULT_HANDLER);
		listing.createData(addr(0x0100689b), dt);
		listing.createData(addr(0x0100688c), floatDt);
		listing.createData(addr(0x01006890), floatDt);

		Data data = listing.getDataAt(addr(0x0100688c));
		data.setComment(CodeUnit.EOL_COMMENT, "this is a float data type");
		data = listing.getDataAt(addr(0x01006890));
		data.setComment(CodeUnit.EOL_COMMENT, "this is another float data type");

	}
	finally {
		program.endTransaction(transactionID, true);
	}
	// open the structure
	cb.goToField(addr(0x0100689b), "+", 0, 0);
	click(cb, 1);
	waitForPostedSwingRunnables();

	ProgramSelection sel = new ProgramSelection(addr(0x0100688c), addr(0x010068a3));
	tool.firePluginEvent(new ProgramSelectionPluginEvent("test", sel, program));

	//search mnemonics and operands
	SearchOptions options = new SearchOptions("float", false /*quick*/, false /*functions*/,
		false /*comments*/, false /*labels*/, true /*instruction mnemonics*/,
		true /*instruction operands*/, true /*data mnemonics*/, true /*data operands*/,
		false /*case sensitive*/, true /*is forward*/, false, false);
	ProgramLocation startLoc = new AddressFieldLocation(program, addr(0x0100688c));
	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, sel, options, TaskMonitor.DUMMY);

	//set up list of answers
	ArrayList<Address> startList = new ArrayList<>();
	startList.add(addr(0x0100688c));
	startList.add(addr(0x01006890));
	startList.add(addr(0x0100689b));
	startList.add(addr(0x0100689f));

	//check that the text is found there in the correct field
	checkTextFound(startList, MnemonicFieldLocation.class);

	//now search comments
	options = new SearchOptions("float", false /*quick*/, false /*functions*/,
		true /*comments*/, false /*labels*/, false /*instruction mnemonics*/,
		false /*instruction operands*/, false /*data mnemonics*/, false /*data operands*/,
		false /*case sensitive*/, true /*is forward*/, false, false);
	startLoc = new AddressFieldLocation(program, addr(0x0100688c));
	searcher =
		new ListingDisplaySearcher(tool, program, startLoc, sel, options, TaskMonitor.DUMMY);

	//remove the one without a comment

	ArrayList<Address> startList2 = new ArrayList<>();
	startList2.add(addr(0x0100688c));
	startList2.add(addr(0x01006890));
	startList2.add(addr(0x0100689f));

	//check that the text is found there in the correct field
	checkTextFound(startList2, EolCommentFieldLocation.class); //waiting for Bill to fix the search
}
 
Example 20
Source File: PartitionCodeSubIterator.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new iterator that will iterate over the
 * program within a given address range set. A block will
 * be returned by this iterator if and only if the block's
 * starting address is within the address range set.  The blocks
 * themselves may lie outside of set.
 *
 * @param model  the SubroutineModel the iterator will use in its operations.
 * @param set    the address range set which the iterator is to be
 *               restricted to.
 * @param monitor task monitor which allows user to cancel operation.
 */
PartitionCodeSubIterator(PartitionCodeSubModel model, AddressSetView set, TaskMonitor monitor) {
	this.model = model;
	this.monitor = monitor != null ? monitor : TaskMonitor.DUMMY;
	this.listing = model.getListing();
	this.instIter = listing.getInstructions(set, true);
	this.nextSub = null;
}