Java Code Examples for ghidra.program.model.address.Address#toString()

The following examples show how to use ghidra.program.model.address.Address#toString() . 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: EditExternalLocationDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private String getEditName() {
	if (externalLocation != null) {
		Symbol symbol = externalLocation.getSymbol();
		String name = symbol.getName(true);
		Address addr = externalLocation.getAddress();
		if (addr != null) {
			name += " @ " + addr.toString(true);
		}
		return name;
	}
	String editName;
	editName = externalLocation.getSymbol().getParentNamespace().getName(true);
	boolean hasName = locationName != null && locationName.length() > 0;
	if (hasName) {
		editName += "::" + locationName;
	}
	if (address != null) {
		editName += " @ " + address.toString();
	}
	return editName;
}
 
Example 2
Source File: AddressFieldFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField lf) {
	Object obj = lf.getProxy().getObject();
	if (!(obj instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	Address addr = cu.getMinAddress();

	int[] cpath = null;
	if (cu instanceof Data) {
		cpath = ((Data) cu).getComponentPath();
	}

	return new AddressFieldLocation(cu.getProgram(), addr, cpath, addr.toString(), col);
}
 
Example 3
Source File: SetStackDepthChangeAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private int showPurgeQuestionDialog(final PluginTool tool, final Address currentAddress,
		final Address functionAddress) {

	// @formatter:off
	String message =
		"If this function changes the stack depth (or purge) the same for all calls to the function,\n" +
			"then you should globally set the function purge which affects all calls to the function.\n \n" +
			"If each call to the function affects the stack depth differently, then you should\n" +
			"locally set the stack depth change at the current Call instruction only.\n \n \n" +
			"Choose Global to set the function purge for the Function at " +
			functionAddress.toString() + ".\n" +
			"Choose Local to set the stack depth change for this call only at " +
			currentAddress.toString() + ".";
	// @formatter:on

	StackChangeOptionDialog dialog = new StackChangeOptionDialog(message);
	dialog.show();
	return dialog.getResult();
}
 
Example 4
Source File: ViewInstructionDetailsAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void actionPerformed(ListingActionContext context) {
	// The location indicates the program for the current listing.
	ProgramLocation location = context.getLocation();
	Program program = location.getProgram();
	ListingMergePanel listingMergePanel =
		(ListingMergePanel) listingMergePanelPlugin.getProvider().getComponent();
	String version = listingMergePanel.getVersionName(program);
	Address address = location.getAddress();
	CodeUnit cu = program.getListing().getCodeUnitContaining(address);
	String detailString = CodeUnitDetails.getInstructionDetails(cu);
	String title = version + " version's Instruction Details @ " + address.toString();
	Dialog dialog = new Dialog(title, createDetailsPane(detailString));

	dialog.setHelpLocation(HELP_LOCATION);

	listingMergePanelPlugin.getTool().showDialog(dialog, listingMergePanel);
}
 
Example 5
Source File: AbstractFrameSection.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a label indicating there is an CIE at the address.
 * @param curAddress the address with the CIE
 */
protected void createCieLabel(Address curAddress) {
	try {
		String cieLabel = "cie_" + curAddress.toString();
		Symbol cieSym = program.getSymbolTable().getPrimarySymbol(curAddress);
		if (cieSym == null) {
			cieSym =
				program.getSymbolTable().createLabel(curAddress, cieLabel, SourceType.ANALYSIS);
		}
		else {
			cieSym.setName(cieLabel, SourceType.ANALYSIS);
		}
	}
	catch (InvalidInputException | DuplicateNameException e) {
		/* Output message and keep going even though couldn't create the label. */
		Msg.info(this, "Unable to label CIE -- " + e.getMessage(), e);
	}
}
 
Example 6
Source File: PointerDB.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public String getRepresentation(MemBuffer buf, Settings settings, int length) {
	lock.acquire();
	try {
		checkIsValid();

		Address addr = (Address) getValue(buf, settings, length);
		if (addr == null) { // could not create address, so return "Not a pointer (NaP)"
			return "NaP";
		}
		return addr.toString();
	}
	finally {
		lock.release();
	}
}
 
Example 7
Source File: AutoTableDisassemblerPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void updateOffsetString(int[] selectedRows) {
	if (addressTableDialog == null) {
		return;
	}

	// now rows selected, disable and exit
	if (selectedRows.length <= 0) {
		addressTableDialog.clearMakeTableOptions();
		return;
	}

	if (!validateSelectionOffsets()) {
		return;
	}

	// make sure the options are enabled when there is a selection
	addressTableDialog.setEnableMakeTableOptions(true);

	// if there is only one row, then put the address at that row in the offset text field
	String offsetText = " ";
	int offset = addressTableDialog.getOffset();

	if (selectedRows.length == 1) {
		Address addr = model.getAddress(selectedRows[0]);
		Address addressWithOffset = addr.addWrap(offset * 4);
		offsetText = addressWithOffset.toString();
	}

	offsetLen = offset;
	addressTableDialog.setDialogText("");
	addressTableDialog.setOffsetText(offsetText);
}
 
Example 8
Source File: AbstractFindReferencesToAddressAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(NavigatableActionContext context) {

	LocationReferencesService service = tool.getService(LocationReferencesService.class);
	if (service == null) {
		Msg.showError(this, null, "Missing Plugin",
			"The " + LocationReferencesService.class.getSimpleName() + " is not installed.\n" +
				"Please add the plugin implementing this service.");
		return;
	}

	Program program = context.getProgram();
	ProgramLocation location = getLocation(context);
	Address address = location.getAddress();
	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitContaining(address);

	int[] path = location.getComponentPath();
	if (cu instanceof Data) {
		Data outerData = (Data) cu;
		Data data = outerData.getComponent(location.getComponentPath());
		address = data.getMinAddress();
	}

	AddressFieldLocation addressLocation =
		new AddressFieldLocation(program, address, path, address.toString(), 0);
	service.showReferencesToLocation(addressLocation, context.getNavigatable());
}
 
Example 9
Source File: EditableListingAddress.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Address getAddress() throws InvalidInputException {
	Address selectedAddress = addressField.getAddress();
	if (selectedAddress == null) {
		throw new InvalidInputException(
			"\"" + addressField.getValue() + "\" is not a valid address.");
	}
	if (!program.getMemory().contains(selectedAddress)) {
		throw new InvalidInputException(
			"\"" + selectedAddress.toString() + "\" is not an address in the program.");
	}
	address = selectedAddress;
	return address;
}
 
Example 10
Source File: AddressAnnotatedStringHandler.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public AttributedString createAnnotatedString(AttributedString prototypeString, String[] text,
		Program program) throws AnnotationException {
	// if the text is not of adequate size, then show an error string
	if (text.length <= 1) {
		throw new AnnotationException(INVALID_SYMBOL_TEXT);
	}

	if (program == null) { // this can happen during merge operations
		return createUndecoratedString(prototypeString, text);
	}

	Address address = program.getAddressFactory().getAddress(text[1]);

	if (address == null) {
		return new AttributedString("No address: " + text[1], Color.RED,
			prototypeString.getFontMetrics(0), false, null);
	}

	String addressText = address.toString();
	if (text.length > 2) { // address and display text
		StringBuffer buffer = new StringBuffer();
		for (int i = 2; i < text.length; i++) {
			buffer.append(text[i]).append(" ");
		}
		buffer.deleteCharAt(buffer.length() - 1);  // remove last space
		addressText = buffer.toString();
	}

	return new AttributedString(addressText, prototypeString.getColor(0),
		prototypeString.getFontMetrics(0), true, prototypeString.getColor(0));
}
 
Example 11
Source File: AddressFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getAddressString(CodeUnit cu) {
	Address addr = cu.getMinAddress();
	AddressSpace space = addr.getAddressSpace();
	if (displayBlockName) {
		String text = addr.toString(false, padZeros ? 16 : minHexDigits);
		MemoryBlock block = cu.getProgram().getMemory().getBlock(addr);
		if (block != null) {
			return block.getName() + ":" + text;
		}
	}
	return addr.toString(space.showSpaceName(), padZeros ? 16 : minHexDigits);
}
 
Example 12
Source File: AbstractLocationReferencesTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void goToDataAddressField(Address a, int row) {
	openData(a);

	AddressFieldLocation location =
		new AddressFieldLocation(program, a, new int[] { row }, a.toString(), 0);
	ProgramLocationPluginEvent event =
		new ProgramLocationPluginEvent("Test", location, program);
	tool.firePluginEvent(event);
}
 
Example 13
Source File: AbstractExternalMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param externalLocation
 * @param expectedAddress
 */
void checkExternalAddress(ExternalLocation externalLocation, String expectedAddress) {
	Address address = externalLocation.getAddress();
	String addressString = (address != null) ? address.toString() : null;
	String failureMessage =
		"Expected external address '" + expectedAddress + "' but was '" + addressString + "'";
	assertEquals(failureMessage, expectedAddress, addressString);
}
 
Example 14
Source File: BlockGraphTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getVertexId(CodeBlock bb) {
	// vertex has attributes of Name       = Label
	//                          Address    = address of blocks start
	//                          VertexType = flow type of vertex
	Address addr = bb.getFirstStartAddress();
	if (addr.isExternalAddress()) {
		Symbol s = bb.getModel().getProgram().getSymbolTable().getPrimarySymbol(addr);
		return s.getName(true);
	}
	return addr.toString();
}
 
Example 15
Source File: AbstractImageBaseOffsetDataType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getRepresentation(MemBuffer buf, Settings settings, int length) {
	Address addr = (Address) getValue(buf, settings, length);
	if (addr == null) { // could not create address, so return "Not a pointer (NaP)"
		return "NaP";
	}
	return addr.toString();
}
 
Example 16
Source File: ShowInstructionInfoPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the addresses of the table models of the "Instruction Info" dialog.
 * The method will fail the current test if the result is not as 
 * expected by the caller of this method.  For example, if 
 * <tt>expectedSame</tt> is true, then the method expects the values to
 * be the same when compared with the given address and will fail if 
 * they are not.  If <tt>expectedSame</tt> is false, then the method will
 * fail if the test values are the same.
 * 
 * @param instructionAddress The address to compare against the address
 *        stored in the table model of the dialog.
 * @param expectedSame True means a match is expected; false means a 
 *        match is not expected.
 */
private void verifyAddressWithTableModels(Address instructionAddress, boolean fromConnected,
		boolean expectedSame) {
	ComponentProvider provider = fromConnected ? getCurrentComponentProviderFromPlugin()
			: getFirstDisconnectedProviderFromPlugin();

	JTextArea instructionText = (JTextArea) getInstanceField("instructionText", provider);
	JTable opTable = (JTable) getInstanceField("opTable", provider);

	// get the instruction address from each table model and make sure that
	// it is the same as the current instruction
	String stateString = expectedSame ? "is not" : "is";
	String text = instructionText.getText();

	String address = instructionAddress.toString(true);
	Pattern pattern = Pattern.compile("Address\\s*:\\s*" + address);
	Matcher matcher = pattern.matcher(text);
	boolean comparisonResult = matcher.find();

	// if the caller of this method expects the results to be NOT equal, 
	// then toggle the comparison result
	if (!expectedSame) {
		comparisonResult = !comparisonResult;
	}

	assertTrue("The address of the mnemonic table " + stateString +
		" the same as that of the current program instruction.", comparisonResult);

	Instruction opInstr = (Instruction) getInstanceField("instruction", opTable.getModel());
	comparisonResult = instructionAddress.equals(opInstr.getMinAddress());

	if (!expectedSame) {
		comparisonResult = !comparisonResult;
	}

	assertTrue("The address of the op table " + stateString +
		" the same as that of the current program instruction.", comparisonResult);
}
 
Example 17
Source File: AbstractLineDispenser.java    From ghidra with Apache License 2.0 5 votes vote down vote up
static String getUniqueAddressString(Address addr) {
    return addr.toString();
    /*
    return  addr.getAddressSpace().getSpaceID() + "_" +
            addr.getOverlayId() + "_" +
            addr.getOffset() + "_";
            */
}
 
Example 18
Source File: AddressBasedLocation.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static String buildStringRepresentation(Program program, Address address,
		Reference reference, ShowBlockName showBlockName) {
	if (address == null) {
		return "<NULL>";
	}
	if (address.getAddressSpace().getType() == AddressSpace.TYPE_NONE) {
		return ""; // NO_ADDRESS or EXT_FROM_ADDRESS not rendered
	}
	if (address.isExternalAddress()) {
		return getExternalAddressRepresentation(program, address);
	}
	if (address.isVariableAddress()) {
		return getVariableAddressRepresentation();
	}
	if (address.isStackAddress()) {
		return getStackAddressRepresentation(address);
	}
	if (address.isConstantAddress()) {
		return getConstantAddressRepresentation(address);
	}
	if (address.isRegisterAddress()) {
		return getRegisterAddressRepresentation(program, address);
	}

	// Handle all other spaces (e.g., memory, other, overlays, hash, etc.)
	String addrStr;
	if (reference != null && reference.isOffsetReference()) {
		OffsetReference offsetRef = (OffsetReference) reference;
		long offset = offsetRef.getOffset();
		boolean neg = (offset < 0);
		Address baseAddr = offsetRef.getBaseAddress();
		addrStr = baseAddr.toString() + (neg ? "-" : "+") + "0x" +
			Long.toHexString(neg ? -offset : offset);
	}
	else if (reference != null && reference.isShiftedReference()) {
		// TODO: unsure of rendering which has never really been addressed
		// TODO: shifted references have never addressed concerns related to
		// addressable unit size
		ShiftedReference shiftedRef = (ShiftedReference) reference;
		StringBuilder buf = new StringBuilder();
		buf.append(address.toString());
		buf.append("(0x");
		buf.append(Long.toHexString(shiftedRef.getValue()));
		buf.append("<<");
		buf.append(Long.toString(shiftedRef.getShift()));
		buf.append(")");
		addrStr = buf.toString();
	}
	else {
		addrStr = address.toString();
	}

	if (showBlockName != ShowBlockName.NEVER) {
		Memory mem = program.getMemory();
		MemoryBlock toBlock = mem.getBlock(address);
		if (toBlock != null && showBlockName == ShowBlockName.NON_LOCAL && reference != null &&
			toBlock.equals(mem.getBlock(reference.getFromAddress()))) {
			toBlock = null;
		}
		if (toBlock != null) {
			addrStr = toBlock.getName() + "::" + addrStr;
		}
	}

	return addrStr;
}
 
Example 19
Source File: FindReferencesTableModel.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public FindReferencesTableModel(Address fromAddr, ServiceProvider provider, Program prog) {
	super(fromAddr.toString(), provider, prog, null);
	this.fromAddr = fromAddr;
	setAlignment(prog.getLanguage().getInstructionAlignment());
}
 
Example 20
Source File: AddressFieldLocation.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Construct a new default AddressFieldLocation for a given program address.
 * 
 * @param program the program of the location
 * @param addr address of the location
 */
public AddressFieldLocation(Program program, Address addr) {
	super(program, addr, 0, 0, 0);
	this.addrRepresentation = addr.toString();
}