Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3515 - New copy special action tests and help

dragonmacher5fc9e80d409d
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/Clipboard.htm b/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/Clipboard.htm
index 9670bda131..d83c687558 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/Clipboard.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/Clipboard.htm
@@ -175,6 +175,10 @@
example: <CODE>{ 0x66, 0x2e, 0x0f, 0x1f, 0x84 }</CODE>.</LI>
<LI><B>Address</B> - Copies the address at the top of the selection.</LI>
+
+ <LI><B>Address w/ Offset</B> - Copies the address at the cursor or each address in the
+ current selection. The text is formatted to show the offset from the entry point of the
+ function, for example: <CODE>main + 0x2</CODE></LI>
</UL>
<P>The Code Browser <B>Listing</B> window can paste the following formats:</P>
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/images/CopySpecialAgain.png b/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/images/CopySpecialAgain.png
index 352de79ede..e2f2a75779 100644
Binary files a/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/images/CopySpecialAgain.png and b/Ghidra/Features/Base/src/main/help/help/topics/ClipboardPlugin/images/CopySpecialAgain.png differ
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProvider.java
index 3ea29313a4..7432ce4ce1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProvider.java
@@ -25,7 +25,6 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.exception.ExceptionUtils;
import docking.ActionContext;
import docking.ComponentProvider;
@@ -61,6 +60,8 @@ public class CodeBrowserClipboardProvider extends ByteCopier
public static final ClipboardType ADDRESS_TEXT_TYPE =
new ClipboardType(DataFlavor.stringFlavor, "Address");
+ public static final ClipboardType ADDRESS_TEXT_WITH_OFFSET_TYPE =
+ new ClipboardType(DataFlavor.stringFlavor, "Address w/ Offset");
public static final ClipboardType CODE_TEXT_TYPE =
new ClipboardType(DataFlavor.stringFlavor, "Formatted Code");
public static final ClipboardType LABELS_COMMENTS_TYPE =
@@ -69,8 +70,6 @@ public class CodeBrowserClipboardProvider extends ByteCopier
new ClipboardType(CodeUnitInfoTransferable.localDataTypeFlavor, "Labels");
public static final ClipboardType COMMENTS_TYPE =
new ClipboardType(CodeUnitInfoTransferable.localDataTypeFlavor, "Comments");
- public static final ClipboardType ADDRESS_SYM_TEXT_TYPE =
- new ClipboardType(DataFlavor.stringFlavor, "Address SymString");
private static final List<ClipboardType> COPY_TYPES = createCopyTypesList();
@@ -87,7 +86,7 @@ public class CodeBrowserClipboardProvider extends ByteCopier
list.add(PYTHON_LIST_TYPE);
list.add(CPP_BYTE_ARRAY_TYPE);
list.add(ADDRESS_TEXT_TYPE);
- list.add(ADDRESS_SYM_TEXT_TYPE);
+ list.add(ADDRESS_TEXT_WITH_OFFSET_TYPE);
return list;
}
@@ -187,7 +186,7 @@ public class CodeBrowserClipboardProvider extends ByteCopier
if (copyType == ADDRESS_TEXT_TYPE) {
return copyAddress();
}
- else if (copyType == ADDRESS_SYM_TEXT_TYPE) {
+ else if (copyType == ADDRESS_TEXT_WITH_OFFSET_TYPE) {
return copySymbolString();
}
else if (copyType == CODE_TEXT_TYPE) {
@@ -310,89 +309,69 @@ public class CodeBrowserClipboardProvider extends ByteCopier
}
private Transferable copyAddress() {
-
- AddressSetView addressSet = getSelectedAddresses();
- AddressIterator it = addressSet.getAddresses(true);
- String joined = StringUtils.join((Iterator<Address>) it, "\n");
- return createStringTransferable(joined);
+ AddressSetView addrs = getSelectedAddresses();
+ Iterable<Address> it = addrs.getAddresses(true);
+ return createStringTransferable(StringUtils.join(it, "\n"));
}
private Transferable copySymbolString() {
Listing listing = currentProgram.getListing();
- CodeUnitIterator codeUnits =
- listing.getCodeUnits(getSelectedAddresses(), true);
- StringBuilder builder = new StringBuilder();
+ List<String> strings = new ArrayList<>();
+ CodeUnitIterator codeUnits = listing.getCodeUnits(getSelectedAddresses(), true);
while (codeUnits.hasNext()) {
- // TODO: Can we improve this for the usual case where all the address(es)
- // are in the same function?
CodeUnit cu = codeUnits.next();
Address addr = cu.getAddress();
- Function foo = listing.getFunctionContaining(addr);
- boolean insideFunction = foo != null;
- String addrStr;
- if (insideFunction) {
- Address entry = foo.getEntryPoint();
- int delta = addr.compareTo(entry);
- if (delta > 0) {
- addrStr = String.format("%s + %#x\n", foo, addr.subtract(entry));
- }
- else if (delta == 0) {
- addrStr = String.format("%s\n", foo);
- } else {
- addrStr = String.format("%s - %#x\n", foo, entry.subtract(addr));
- }
- } else {
- // TODO: Probably better to have a second version for addresses of instructions
- addrStr = String.format("%s\n", addr);
+ Function function = listing.getFunctionContaining(addr);
+ if (function == null) {
+ strings.add(addr.toString());
+ continue;
+ }
+
+ String name = function.getName(true);
+ Address entry = function.getEntryPoint();
+ int delta = addr.compareTo(entry);
+ if (delta == 0) {
+ strings.add(name);
+ }
+ else if (delta > 0) {
+ strings.add(String.format("%s + %#x", name, addr.subtract(entry)));
+ }
+ else {
+ strings.add(String.format("%s - %#x", name, entry.subtract(addr)));
}
- builder.append(addrStr);
}
- String joined = builder.toString();
- return createStringTransferable(joined);
+ return createStringTransferable(StringUtils.join(strings, "\n"));
}
protected Transferable copyCode(TaskMonitor monitor) {
AddressSetView addressSet = getSelectedAddresses();
- try {
- TextLayoutGraphics g = new TextLayoutGraphics();
-
- Rectangle rect = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE);
-
- AddressRangeIterator rangeItr = addressSet.getAddressRanges();
- while (rangeItr.hasNext()) {
- AddressRange curRange = rangeItr.next();
- Address curAddress = curRange.getMinAddress();
- Address maxAddress = curRange.getMaxAddress();
-
- // getAddressAfter(curAddress) returns null in certain situations
- while (curAddress != null && curAddress.compareTo(maxAddress) <= 0) {
- if (monitor.isCancelled()) {
- break;
- }
-
- //Add the layout for the present address
- Layout layout = getListingModel().getLayout(curAddress, false);
- if (layout != null) {
- LayoutBackgroundColorManager layoutColorMap =
- new EmptyLayoutBackgroundColorManager(PAINT_CONTEXT.getBackground());
- layout.paint(null, g, PAINT_CONTEXT, rect, layoutColorMap, null);
- g.flush();
- }
- // may be null
- curAddress = getListingModel().getAddressAfter(curAddress);
+ ListingModel listingModel = getListingModel();
+ TextLayoutGraphics g = new TextLayoutGraphics();
+ LayoutBackgroundColorManager colorMap =
+ new EmptyLayoutBackgroundColorManager(PAINT_CONTEXT.getBackground());
+ Rectangle rect = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE);
+ AddressRangeIterator ranges = addressSet.getAddressRanges();
+ while (ranges.hasNext() && !monitor.isCancelled()) {
+ AddressRange curRange = ranges.next();
+ Address address = curRange.getMinAddress();
+ Address maxAddress = curRange.getMaxAddress();
+
+ while (address != null && address.compareTo(maxAddress) <= 0) {
+ if (monitor.isCancelled()) {
+ break;
}
- }
- return createStringTransferable(g.getBuffer().toString());
- }
- catch (Exception e) {
- String message = "Copy failed: " + ExceptionUtils.getMessage(e);
- Msg.error(this, message, e);
- tool.setStatusInfo(message, true);
+ Layout layout = listingModel.getLayout(address, false);
+ if (layout != null) {
+ layout.paint(null, g, PAINT_CONTEXT, rect, colorMap, null);
+ g.flush();
+ }
+ address = listingModel.getAddressAfter(address);
+ }
}
- return null;
+ return createStringTransferable(g.getBuffer());
}
private Transferable copyByteString(Address address) {
@@ -401,7 +380,6 @@ public class CodeBrowserClipboardProvider extends ByteCopier
}
private CodeUnitInfoTransferable copyLabelsComments(boolean copyLabels, boolean copyComments) {
-
AddressSetView addressSet = getSelectedAddresses();
List<CodeUnitInfo> list = new ArrayList<>();
Address startAddr = addressSet.getMinAddress();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProviderTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProviderTest.java
index 8d6d698144..a97abfb40f 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProviderTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/clipboard/CodeBrowserClipboardProviderTest.java
@@ -93,6 +93,19 @@ public class CodeBrowserClipboardProviderTest extends AbstractGhidraHeadedIntegr
builder.createEmptyFunction("ghidra", "0x01002cf5", 1, dt, p);
builder.createEmptyFunction("sscanf", "0x0100415a", 1, dt, p);
+ // create a function for offset address copying
+ String address = "0x010023f5";
+ int length = 0x121;
+ builder.createEmptyFunction("main", address, length, dt, p);
+ builder.setBytes(address,
+ "55 8b ec 83 7d 14 00 56 8b 35 e0 10 00 01 57 74 09 ff 75 14 ff d6 8b f8 eb 02 33 " +
+ "ff ff 75 10 ff d6 03 c7 8d 44 00 02 50 6a 40 ff 15 dc 10 00 01 8b f0 85 f6 74 27 " +
+ "56 ff 75 14 ff 75 10 e8 5c ff ff ff ff 75 18 ff 75 0c 56 ff 75 08 ff 15 04 12 00 " +
+ "01 56 8b f8 ff 15 c0 10 00 01 eb 14 ff 75 18 ff 75 0c ff 75 10 ff 75 08 ff 15 04 " +
+ "12 00 01 8b f8 8b c7 5f 5e 5d c2 14");
+ builder.disassemble(address, length, true);
+ builder.createFunction(address);
+
builder.setBytes("0x0100418c", "ff 15 08 10 00 01");
builder.disassemble("0x0100418c", 6);
@@ -175,9 +188,48 @@ public class CodeBrowserClipboardProviderTest extends AbstractGhidraHeadedIntegr
assertBytesAt(pasteAddress, asciiByteString, 4);
}
+ @Test
+ public void testCopy_AddressWithOffset_InsideFunction() throws Exception {
+
+ String main = "0x010023f5";
+ String address = main;
+ clipboardProvider.setLocation(location(address));
+
+ ClipboardType type = CodeBrowserClipboardProvider.ADDRESS_TEXT_WITH_OFFSET_TYPE;
+ Transferable transferable = clipboardProvider.copySpecial(type, TaskMonitor.DUMMY);
+ String copiedString = (String) transferable.getTransferData(DataFlavor.stringFlavor);
+ assertEquals("main", copiedString);
+
+ address = "0x010023f7"; // 0x010023f5 + 2
+ clipboardProvider.setLocation(location(address));
+
+ type = CodeBrowserClipboardProvider.ADDRESS_TEXT_WITH_OFFSET_TYPE;
+ transferable = clipboardProvider.copySpecial(type, TaskMonitor.DUMMY);
+ copiedString = (String) transferable.getTransferData(DataFlavor.stringFlavor);
+ assertEquals("main + 0x2", copiedString);
+
+ clipboardProvider.setSelection(selection(main, 6));
+ type = CodeBrowserClipboardProvider.ADDRESS_TEXT_WITH_OFFSET_TYPE;
+ transferable = clipboardProvider.copySpecial(type, TaskMonitor.DUMMY);
+ copiedString = (String) transferable.getTransferData(DataFlavor.stringFlavor);
+ assertEquals("main\nmain + 0x2\nmain + 0x4", copiedString);
+ }
+
+ @Test
+ public void testCopy_AddressWithOffset_NoFunction() throws Exception {
+
+ String address = "0x01002cc0"; // data
+ clipboardProvider.setLocation(location(address));
+
+ ClipboardType type = CodeBrowserClipboardProvider.ADDRESS_TEXT_WITH_OFFSET_TYPE;
+ Transferable transferable = clipboardProvider.copySpecial(type, TaskMonitor.DUMMY);
+ String copiedString = (String) transferable.getTransferData(DataFlavor.stringFlavor);
+ assertEquals("01002cc0", copiedString);
+ }
+
//==================================================================================================
// Private Methods
-//==================================================================================================
+//==================================================================================================
private void paste(String address, Transferable transferable) {
diff --git a/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/ClipboardPluginScreenShots.java b/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/ClipboardPluginScreenShots.java
index 064fa3d2be..8689027ac3 100644
--- a/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/ClipboardPluginScreenShots.java
+++ b/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/ClipboardPluginScreenShots.java
@@ -110,10 +110,11 @@ public class ClipboardPluginScreenShots extends GhidraScreenShotGenerator {
JMenuItem item = (JMenuItem) elements[index];
Rectangle itemBounds = item.getBounds();
- copySectionBounds.x = itemBounds.x;
- copySectionBounds.y = itemBounds.y;
- copySectionBounds.width = itemBounds.width;
- height += itemBounds.height;
+ int padding = 5; // add some padding to get the menu item borders in the cropped image
+ copySectionBounds.x = itemBounds.x - padding;
+ copySectionBounds.y = itemBounds.y - padding;
+ copySectionBounds.width = itemBounds.width + (padding * 2);
+ height += itemBounds.height + (padding * 2);
item = (JMenuItem) elements[index + 1];
itemBounds = item.getBounds();
@@ -195,8 +196,7 @@ public class ClipboardPluginScreenShots extends GhidraScreenShotGenerator {
return waitForDialogComponent(CopyPasteSpecialDialog.class);
}
- private ClipboardContentProviderService getClipboardService(
- ClipboardPlugin clipboardPlugin) {
+ private ClipboardContentProviderService getClipboardService(ClipboardPlugin clipboardPlugin) {
Map<?, ?> serviceMap = (Map<?, ?>) getInstanceField("serviceActionMap", clipboardPlugin);
Set<?> keySet = serviceMap.keySet();
for (Object name : keySet) {