Java Code Examples for ghidra.program.database.ProgramDB#getListing()

The following examples show how to use ghidra.program.database.ProgramDB#getListing() . 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: AbstractCreateDataTypeModelTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected void checkArrayData(ProgramDB program, long address, DataType elementDt,
		int numElements) {
	Listing listing = program.getListing();
	Data data = listing.getDataAt(addr(program, address));
	DataType dataType = data.getDataType();
	if (!(dataType instanceof Array)) {
		fail("Data type " + dataType.getName() + " isn't an array.");
	}
	Array array = (Array) dataType;
	assertEquals(numElements, array.getNumElements());
	String name = dataType.getName();
	assertEquals(elementDt.getName() + "[" + numElements + "]", name);
	int expectedDtLength = elementDt.getLength() * numElements;
	assertEquals(expectedDtLength, expectedDtLength);

	DataType baseDataType = array.getDataType();
	assertTrue(baseDataType.isEquivalent(elementDt));
}
 
Example 2
Source File: AbstractCreateDataTypeModelTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected void CheckDynamicStructureData(ProgramDB program, long address, String expectedName,
		String[] expectedFieldNames, int expectedDtLength) {

	Listing listing = program.getListing();
	Address dataAddress = addr(program, address);
	Data data = listing.getDataAt(dataAddress);
	DataType dataType = data.getDataType();
	String name = dataType.getName();
	assertEquals(expectedName, name);
	assertEquals(expectedDtLength, data.getLength());

	assertTrue("DataType " + dataType.getName() + " isn't a DynamicDataType.",
		dataType instanceof DynamicDataType);
	DynamicDataType dynamicDt = (DynamicDataType) dataType;
	DumbMemBufferImpl memBuffer = new DumbMemBufferImpl(program.getMemory(), dataAddress);
	assertEquals(expectedFieldNames.length, dynamicDt.getNumComponents(memBuffer));
	DataTypeComponent[] components = dynamicDt.getComponents(memBuffer);
	for (int i = 0; i < components.length; i++) {
		assertEquals(
			"Expected component " + i + " to be named " + expectedFieldNames[i] + " but was " +
				components[i].getFieldName(),
			expectedFieldNames[i], components[i].getFieldName());
	}
}
 
Example 3
Source File: SymbolUtilities1Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	TestEnv env = new TestEnv();
	LanguageService ls = DefaultLanguageService.getLanguageService();
	Language x86 = ls.getDefaultLanguage(TestProcessorConstants.PROCESSOR_X86);
	CompilerSpec windows = x86.getCompilerSpecByID(new CompilerSpecID("windows"));
	program = new ProgramDB("Test", x86, windows, this);
	env.dispose();
	space = program.getAddressFactory().getDefaultAddressSpace();
	Memory memory = program.getMemory();
	transactionID = program.startTransaction("Test");
	memory.createInitializedBlock("test", addr(0), 5000, (byte) 0,
		TaskMonitorAdapter.DUMMY_MONITOR, false);
	symbolTable = program.getSymbolTable();
	refMgr = program.getReferenceManager();
	listing = program.getListing();
}
 
Example 4
Source File: SymbolUtilities2Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	TestEnv env = new TestEnv();
	LanguageService ls = DefaultLanguageService.getLanguageService();
	Language i8051 = ls.getDefaultLanguage(TestProcessorConstants.PROCESSOR_8051);
	program = new ProgramDB("Test", i8051, i8051.getDefaultCompilerSpec(), this);
	env.dispose();
	space = program.getAddressFactory().getDefaultAddressSpace();
	Memory memory = program.getMemory();
	transactionID = program.startTransaction("Test");
	memory.createInitializedBlock("test", addr(0), 5000, (byte) 0,
		TaskMonitorAdapter.DUMMY_MONITOR, false);
	symbolTable = program.getSymbolTable();
	refMgr = program.getReferenceManager();
	listing = program.getListing();
}
 
Example 5
Source File: AbstractCreateDataTypeModelTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void checkSimpleData(ProgramDB program, long address, DataType elementDt) {
	Listing listing = program.getListing();
	Data data = listing.getDataAt(addr(program, address));
	DataType dataType = data.getDataType();
	String name = dataType.getName();
	assertEquals(elementDt.getName(), name);
	int expectedDtLength = elementDt.getLength();
	assertEquals(expectedDtLength, expectedDtLength);
	assertTrue(dataType.isEquivalent(elementDt));
}
 
Example 6
Source File: FollowFlowProgramBuilder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private int createStructureWithPointer(String name, int startOfStruct, int to) throws Exception {
	int thisStructureSize = 12;
	int thisPointerSize = 4;
	int pointerOffset = 4;

	byte[] bytes = new byte[thisPointerSize];
	dataConverter.getBytes(to, bytes);

	String structureStart = "0x" + Integer.toHexString(startOfStruct);
	String pointerStart = "0x" + Integer.toHexString(startOfStruct + pointerOffset);
	String structureEnd = "0x" + Integer.toHexString(startOfStruct + thisStructureSize - 1);
	String toAddress = "0x" + Integer.toHexString(to);

	clearCodeUnits(structureStart, structureEnd, false);
	setBytes(pointerStart, bytes, false);
	startTransaction();
	ProgramDB program = getProgram();
	Listing listing = program.getListing();
	Structure struct = new StructureDataType(name, thisStructureSize, program.getDataTypeManager());
	struct.replaceAtOffset(0, new FloatDataType(), 4, null, null);
	struct.replaceAtOffset(pointerOffset, new Pointer32DataType(), 4, null, null);
	listing.createData(addr(startOfStruct), struct);
	createMemoryReference(pointerStart, toAddress, RefType.DATA, SourceType.ANALYSIS, 0);
	endTransaction();

	return thisPointerSize; // pointer size in bytes.
}
 
Example 7
Source File: FollowFlowProgramBuilder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private int createStructureWith2Pointers(String name, int startOfStruct, int to, int secondTo)
		throws Exception {
	int thisStructureSize = 12;
	int pointerSize = 4;
	int pointerOffset = 4;

	byte[] bytes = new byte[pointerSize];
	dataConverter.getBytes(to, bytes);

	String structureStart = "0x" + Integer.toHexString(startOfStruct);
	String pointerStart = "0x" + Integer.toHexString(startOfStruct + pointerOffset);
	String pointer2Start =
		"0x" + Integer.toHexString(startOfStruct + pointerOffset + pointerSize);
	String structureEnd = "0x" + Integer.toHexString(startOfStruct + thisStructureSize - 1);
	String toAddress = "0x" + Integer.toHexString(to);
	String toAddress2 = "0x" + Integer.toHexString(secondTo);

	clearCodeUnits(structureStart, structureEnd, false);
	setBytes(pointerStart, bytes, false);
	dataConverter.getBytes(secondTo, bytes);
	setBytes(pointerStart + pointerSize, bytes, false);
	startTransaction();
	ProgramDB program = getProgram();
	Listing listing = program.getListing();
	Structure struct = new StructureDataType(name, thisStructureSize, program.getDataTypeManager());
	struct.replaceAtOffset(0, new FloatDataType(), 4, null, null);
	struct.replaceAtOffset(pointerOffset, new Pointer32DataType(), 4, null, null);
	struct.replaceAtOffset(pointerOffset + pointerSize, new Pointer32DataType(), 4, null, null);
	listing.createData(addr(startOfStruct), struct);
	createMemoryReference(pointerStart, toAddress, RefType.DATA, SourceType.ANALYSIS, 0);
	createMemoryReference(pointer2Start, toAddress2, RefType.DATA, SourceType.ANALYSIS, 0);
	endTransaction();

	return pointerSize; // pointer size in bytes.
}
 
Example 8
Source File: AbstractCreateDataTypeModelTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void CheckStructureData(ProgramDB program, long address, String expectedName,
		String[] expectedFieldNames, String flexArrayName, int expectedDtLength,
		boolean isTypeDefOfStructure) {
	Listing listing = program.getListing();
	Data data = listing.getDataAt(addr(program, address));
	DataType dataType = data.getDataType();
	String name = dataType.getName();
	assertEquals(expectedName, name);
	assertEquals(expectedDtLength, dataType.getLength());

	DataType baseDataType = dataType;
	if (isTypeDefOfStructure) {
		assertTrue("DataType " + name + " wasn't a TypeDef.", dataType instanceof TypeDef);
		baseDataType = ((TypeDef) dataType).getBaseDataType();
	}
	assertTrue("DataType " + name + "'s base data type wasn't a Structure.",
		baseDataType instanceof Structure);
	Structure structure = (Structure) baseDataType;
	assertEquals("Mismatch in expected structure component count: " + name,
		expectedFieldNames.length, structure.getNumComponents());
	DataTypeComponent[] components = structure.getComponents();
	for (int i = 0; i < components.length; i++) {
		assertEquals(
			"Expected component " + i + " to be named " + expectedFieldNames[i] + " but was " +
				components[i].getFieldName(),
			expectedFieldNames[i], components[i].getFieldName());
	}
	if (flexArrayName != null) {
		DataTypeComponent flexibleArrayComponent = structure.getFlexibleArrayComponent();
		assertNotNull("Structure does not contain flexible array: " + name,
			flexibleArrayComponent);
		assertEquals(
			"Expected flexible array named " + flexArrayName + " but was " +
				flexibleArrayComponent.getFieldName(),
			flexArrayName, flexibleArrayComponent.getFieldName());
	}
	else {
		assertFalse("Structure contains unexpected flexible array component: " + name,
			structure.hasFlexibleArrayComponent());
	}
}
 
Example 9
Source File: AbstractCreateDataTypeModelTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected void checkNoData(ProgramDB program, long address) {
	Listing listing = program.getListing();
	Data data = listing.getDataAt(addr(program, address));
	assertEquals(DefaultDataType.dataType, data.getDataType());
}