Java Code Examples for ghidra.program.model.listing.Function#getName()

The following examples show how to use ghidra.program.model.listing.Function#getName() . 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: FunctionGraphFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new function graph for the given function
 * 
 * @param function the function to graph
 * @param controller the controller needed by the function graph
 * @param program the function's program
 * @param monitor the task monitor
 * @return the new graph
 * @throws CancelledException if the task is cancelled via the monitor
 */
public static FGData createNewGraph(Function function, FGController controller, Program program,
		TaskMonitor monitor) throws CancelledException {

	FunctionGraph graph = createGraph(function, controller, monitor);
	if (graph.getVertices().size() == 0) {
		return new EmptyFunctionGraphData("No data in function: " + function.getName());
	}

	if (!isEntryPointValid(function, controller, monitor)) {
		return new EmptyFunctionGraphData(
			"No instruction at function entry point: " + function.getName());
	}

	FGVertex functionEntryVertex = graph.getVertexForAddress(function.getEntryPoint());
	graph.setRootVertex(functionEntryVertex);

	graph.setOptions(controller.getFunctionGraphOptions());

	// doing this here will keep the potentially slow work off of the Swing thread, as the
	// results are cached
	String errorMessage = layoutGraph(function, controller, graph, monitor);
	return new FGData(function, graph, errorMessage);
}
 
Example 2
Source File: DecompilerProvider.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the windows title and subtitle to reflect the currently decompiled function
 */
private void updateTitle() {
	Function function = controller.getDecompileData().getFunction();
	String programName = (program != null) ? program.getDomainFile().getName() : "";
	String title = "Decompiler";
	String subTitle = "";
	if (function != null) {
		title = "Decompile: " + function.getName();
		subTitle = " (" + programName + ")";
	}
	if (!isConnected()) {
		title = "[" + title + "]";
	}
	setTitle(title);
	setSubTitle(subTitle);
}
 
Example 3
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteOnlyFunctionLabel() throws Exception {

	Address addr = addr("0x1");

	String defaultName = SymbolUtilities.getDefaultFunctionName(addr);
	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals(defaultName, primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals(defaultName, function.getSymbol().getName());
}
 
Example 4
Source File: AbstractApplyFunctionSignatureAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to change the signature of a function to that of another
 * function
 * 
 * @param provider the parent component provider 
 * @param destinationFunction the function to change
 * @param sourceFunction the function to copy
 * @return true if the operation was successful
 */
protected boolean updateFunction(ComponentProvider provider, Function destinationFunction,
		Function sourceFunction) {

	Program program = destinationFunction.getProgram();
	int txID = program.startTransaction(ACTION_NAME);
	boolean commit = false;

	try {
		FunctionUtility.updateFunction(destinationFunction, sourceFunction);
		commit = true;
	}
	catch (InvalidInputException | DuplicateNameException e) {
		String message = "Couldn't apply the function signature from " +
			sourceFunction.getName() + " to " + destinationFunction.getName() + " @ " +
			destinationFunction.getEntryPoint().toString() + ". " + e.getMessage();
		Msg.showError(this, provider.getComponent(), ACTION_NAME, message, e);
	}
	finally {
		program.endTransaction(txID, commit);
	}

	return commit;
}
 
Example 5
Source File: FunctionComparison.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(Function o1, Function o2) {
	if (o2 == null) {
		return 1;
	}

	String o1Path = o1.getProgram().getDomainFile().getPathname();
	String o2Path = o2.getProgram().getDomainFile().getPathname();
	int result = o1Path.compareTo(o2Path);
	if (result != 0) {
		return result;
	}

	// equal paths
	String o1Name = o1.getName();
	String o2Name = o2.getName();
	result = o1Name.compareTo(o2Name);
	if (result != 0) {
		return result;
	}

	// equal names
	return o1.getEntryPoint().compareTo(o2.getEntryPoint());
}
 
Example 6
Source File: FunctionComparisonProvider.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public String toString() {
	StringBuffer buff = new StringBuffer();
	buff.append("FunctionComparisonProvider\n");
	buff.append("Name: ");
	buff.append(getName() + "\n");
	buff.append("Tab Text: ");
	buff.append(getTabText() + "\n");
	Function leftFunction = functionComparisonPanel.getLeftFunction();
	String leftName = (leftFunction != null) ? leftFunction.getName() : "No Function";
	buff.append("Function 1: " + leftName + "\n");
	Function rightFunction = functionComparisonPanel.getRightFunction();
	String rightName = (rightFunction != null) ? rightFunction.getName() : "No Function";
	buff.append("Function 2: " + rightName + "\n");
	buff.append("tool = " + tool + "\n");
	return buff.toString();
}
 
Example 7
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteDefaultFunctionLabel() throws Exception {
	// ensure that default function label can not be deleted

	Address addr = addr("0x1");

	String defaultName = SymbolUtilities.getDefaultFunctionName(addr);
	Function function = createFunction(addr);
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertTrue(functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals(1, symbols.length);
	assertEquals(defaultName, symbols[0].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(false, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals(primarySymbol, functionSymbol);
}
 
Example 8
Source File: IterateFunctionsByAddressScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void iterateForward() {
	// Use the iterator, there is no easy way to use the function iterator on addresses
	//   If the function begins at address zero, you won't get the function without
	//   a lot of extra more complicated code.
	FunctionIterator fiter = currentProgram.getFunctionManager().getFunctions(true);

	int count = 0;
	while (fiter.hasNext()) {
		Function function = fiter.next();

		if (monitor.isCancelled()) {
			break;
		}

		String string = count + "  :  " + function.getName() + " @ " + function.getEntryPoint();

		monitor.setMessage(string);

		println(string);

		count++;
	}
	println("found " + count + " functions ");
}
 
Example 9
Source File: X64DbgExport.java    From GhidraX64Dbg with MIT License 5 votes vote down vote up
/** Populates the functions and labels within the X64 database */
public void populateFunctions() {
	for (Function function : currentProgram.getFunctionManager().getFunctions(true)) {
		AddressSetView functionBody = function.getBody();

		// Retrieve required properties
		String start = "0x" + Long.toHexString(functionBody.getMinAddress().subtract(imageBase));
		String end = "0x" + Long.toHexString(functionBody.getMaxAddress().subtract(imageBase));
		String label = function.getName();
		String instructionCount = "0x" + Long.toHexString(functionBody.getNumAddresses());

		this.functions.add(new X64Function(start, end, instructionCount));
		this.labels.add(new X64Label(start, label));
	}
}
 
Example 10
Source File: FunctionEditorDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JPanel createThunkedFunctionTextPanel(Function thunkedFunction) {
	JPanel thunkedPanel = new JPanel(new BorderLayout());
	JTextField thunkedText = new JTextField(thunkedFunction.getName(true));
	thunkedText.setEditable(false);
	DockingUtils.setTransparent(thunkedText);
	CompoundBorder border =
		BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GRAY),
			BorderFactory.createEmptyBorder(0, 5, 0, 5));
	thunkedText.setBorder(border);
	thunkedText.setForeground(Color.BLUE);
	thunkedPanel.add(thunkedText);
	return thunkedPanel;
}
 
Example 11
Source File: FunctionSignatureTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(Function rowObject, Settings settings,
		Program program, ServiceProvider serviceProvider) {
	if (rowObject == null) {
		return null;
	}
	return new FunctionNameFieldLocation(program, rowObject.getEntryPoint(), 0,
		rowObject.getPrototypeString(false, false), rowObject.getName());
}
 
Example 12
Source File: FunctionNameTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(Address rowObject, Settings settings,
		Program program, ServiceProvider serviceProvider) {
	Function function = getFunctionContaining(rowObject, program);
	if (function != null) {
		return new FunctionNameFieldLocation(program, function.getEntryPoint(), 0,
			function.getPrototypeString(false, false), function.getName());
	}
	return null;
}
 
Example 13
Source File: FunctionNameTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getValue(Address rowObject, Settings settings, Program pgm,
		ServiceProvider serviceProvider) throws IllegalArgumentException {
	Function function = getFunctionContaining(rowObject, pgm);
	if (function != null) {
		return function.getName(true);
	}
	return null;
}
 
Example 14
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteFunctionLabelWhereMultiple() throws Exception {

	Address addr = addr("0x1");

	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	createSymbol(addr, "OtherSymbol");
	symbols = getSymbols(addr);
	assertEquals(2, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());
	assertEquals(true, symbols[0].isPrimary());
	assertEquals("OtherSymbol", symbols[1].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals("OtherSymbol", primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals("OtherSymbol", function.getSymbol().getName());
}
 
Example 15
Source File: FunctionParameterCountTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(Address rowObject, Settings settings,
		Program program, ServiceProvider serviceProvider) {
	Function function = getFunctionContaining(rowObject, program);
	if (function != null) {
		return new FunctionNameFieldLocation(program, function.getEntryPoint(), 0,
			function.getPrototypeString(false, false), function.getName());
	}
	return null;
}
 
Example 16
Source File: StructureEditorAlignmentTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testAlignedEditToFunctionDefinitionDataType() throws Exception {
	int value = 1;

	startTransaction("addExternal");
	ExternalLocation extLoc = program.getExternalManager().addExtFunction(Library.UNKNOWN,
		"extLabel", null, SourceType.USER_DEFINED);
	Function function = extLoc.createFunction();
	endTransaction(true);

	String name = function.getName();
	FunctionDefinitionDataType functionDefinitionDataType =
		new FunctionDefinitionDataType(function, true);
	FunctionDefinition functionDefinition = null;
	boolean commit = false;
	txId = program.startTransaction("Modify Program");
	try {
		simpleStructure.setInternallyAligned(true);
		simpleStructure.setPackingValue(value);

		programDTM = program.getListing().getDataTypeManager();
		functionDefinition =
			(FunctionDefinition) programDTM.resolve(functionDefinitionDataType, null);
		commit = true;
	}
	finally {
		program.endTransaction(txId, commit);
	}
	assertNotNull(functionDefinition);

	init(simpleStructure, pgmBbCat, false);

	int column = model.getDataTypeColumn();
	DataType dt = functionDefinition;

	assertEquals(28, model.getLength());
	assertEquals(-1, dt.getLength());
	clickTableCell(getTable(), 1, column, 2);
	assertIsEditingField(1, column);

	deleteAllInCellEditor();
	type(name);
	enter();

	assertNotEditingField();
	assertEquals(1, model.getNumSelectedRows());
	assertEquals(1, model.getMinIndexSelected());
	assertCellString(name + " *", 1, column);// Was function definition converted to a pointer?
	assertEquals(30, model.getLength());
	assertEquals(4, getDataType(1).getLength());
	assertEquals(4, model.getComponent(1).getLength());
}
 
Example 17
Source File: UnionEditorCellEditTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testEditToFunctionDefinitionDataType() throws Exception {

	init(simpleUnion, pgmBbCat, false);

	startTransaction("addExternal");
	ExternalLocation extLoc = program.getExternalManager().addExtFunction(Library.UNKNOWN,
		"extLabel", null, SourceType.USER_DEFINED);
	Function function = extLoc.createFunction();
	endTransaction(true);

	String name = function.getName();
	FunctionDefinitionDataType functionDefinitionDataType =
		new FunctionDefinitionDataType(function, true);
	FunctionDefinition functionDefinition = null;
	boolean commit = false;
	txId = program.startTransaction("Modify Program");
	try {
		programDTM = program.getListing().getDataTypeManager();
		functionDefinition =
			(FunctionDefinition) programDTM.resolve(functionDefinitionDataType, null);
		commit = true;
	}
	finally {
		program.endTransaction(txId, commit);
	}
	assertNotNull(functionDefinition);

	int column = model.getDataTypeColumn();
	DataType dt = getDataType(1);

	assertEquals(8, model.getLength());
	assertEquals(2, dt.getLength());
	clickTableCell(getTable(), 1, column, 2);
	assertIsEditingField(1, column);

	setText(name);
	enter();

	assertNotEditingField();
	assertEquals(1, model.getNumSelectedRows());
	assertEquals(1, model.getMinIndexSelected());
	assertCellString(name + " *", 1, column);
	assertEquals(8, model.getLength());
	dt = getDataType(1);
	assertEquals(name + " *", dt.getDisplayName());
	assertEquals(4, model.getComponent(1).getLength());
}
 
Example 18
Source File: StackEditorProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
static String getProviderSubTitle(Function function) {
	Program pgm = function.getProgram();
	return function.getName() + " (" + pgm.getDomainFile().getName() + ")";
}
 
Example 19
Source File: StructureEditorLockedCellEditTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testEditToFunctionDefinitionDataType() throws Exception {
	init(simpleStructure, pgmBbCat);

	startTransaction("addExternal");
	ExternalLocation extLoc = program.getExternalManager().addExtFunction(Library.UNKNOWN,
		"extLabel", null, SourceType.USER_DEFINED);
	Function function = extLoc.createFunction();
	endTransaction(true);

	String name = function.getName();
	FunctionDefinitionDataType functionDefinitionDataType =
		new FunctionDefinitionDataType(function, true);
	FunctionDefinition functionDefinition = null;
	boolean commit = false;
	txId = program.startTransaction("Modify Program");
	try {
		programDTM = program.getListing().getDataTypeManager();
		functionDefinition =
			(FunctionDefinition) programDTM.resolve(functionDefinitionDataType, null);
		commit = true;
	}
	finally {
		program.endTransaction(txId, commit);
	}
	assertNotNull(functionDefinition);

	int column = model.getDataTypeColumn();
	DataType dt = functionDefinition;
	model.clearComponents(new int[] { 3 });

	assertEquals(29, model.getLength());
	assertEquals(-1, dt.getLength());
	clickTableCell(getTable(), 2, column, 2);
	assertIsEditingField(2, column);

	setText(name);
	enter();

	assertNotEditingField();
	assertEquals(1, model.getNumSelectedRows());
	assertEquals(2, model.getMinIndexSelected());
	assertCellString(name + " *", 2, column);// Was function definition converted to a pointer?
	assertEquals(29, model.getLength());
	assertEquals(4, getDataType(2).getLength());
	assertEquals(4, model.getComponent(2).getLength());
}
 
Example 20
Source File: IterateFunctionsScript.java    From ghidra with Apache License 2.0 3 votes vote down vote up
private void iterateBackward() {

		Function function = getLastFunction();

		int count = 0;
		while (true) {

			if (monitor.isCancelled()) {
				break;
			}

			if (function == null) {
				break;
			}

			String string = count + "  :  " + function.getName() + " @ " + function.getEntryPoint();

			monitor.setMessage(string);

			println(string);

			function = getFunctionBefore(function);

			count++;
		}
		println("found forward = " + count);
	}