Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7143 - Xrefs Field - Show full xref count

dragonmacher39fb0015b40a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java
index 347bf54af2..06b9342a79 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java
@@ -44,6 +44,7 @@ import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.*;
import ghidra.program.util.*;
import ghidra.util.HelpLocation;
+import ghidra.util.datastruct.Counter;
import ghidra.util.table.ReferencesFromTableModel;
import ghidra.util.table.field.ReferenceEndpoint;
import resources.Icons;
@@ -68,17 +69,43 @@ public class XReferenceUtils {
* @return array first <b><code>max</code></b> xrefs to the code unit
*/
public final static List<Reference> getXReferences(CodeUnit cu, int max) {
+ return getXReferences(cu, max, null);
+ }
+
+ /**
+ * Returns an array containing the first <b><code>max</code></b>
+ * direct xref references to the specified code unit.
+ *
+ * @param cu the code unit to generate the xrefs
+ * @param max max number of xrefs to get, or -1 to get all references
+ * @param fullCount a counter that will be updated with the full count of xrefs. This will have
+ * the full xref count regardless of whether the max is reached. The counter will be added to,
+ * which allows it to be passed to multiple methods on this class.
+ *
+ * @return array first <b><code>max</code></b> xrefs to the code unit
+ */
+ public final static List<Reference> getXReferences(CodeUnit cu, int max, Counter fullCount) {
Program program = cu.getProgram();
if (program == null) {
Collections.emptyList();
}
+ if (max < 0) {
+ max = Integer.MAX_VALUE;
+ }
+
+ if (fullCount == null) {
+ fullCount = new Counter();
+ }
+
// lookup the direct xrefs to the current code unit
List<Reference> xrefs = new ArrayList<>();
Address minAddress = cu.getMinAddress();
- ReferenceIterator it = program.getReferenceManager().getReferencesTo(minAddress);
+ ReferenceManager referenceManager = program.getReferenceManager();
+ fullCount.add(referenceManager.getReferenceCountTo(minAddress));
+ ReferenceIterator it = referenceManager.getReferencesTo(minAddress);
while (it.hasNext()) {
- if (xrefs.size() - max == 0) {
+ if (xrefs.size() >= max) {
break;
}
@@ -93,10 +120,17 @@ public class XReferenceUtils {
}
Address[] thunkAddrs = function.getFunctionThunkAddresses(false);
- if (thunkAddrs != null) {
- for (Address thunkAddr : thunkAddrs) {
- xrefs.add(new ThunkReference(thunkAddr, function.getEntryPoint()));
+ if (thunkAddrs == null) {
+ return xrefs;
+ }
+
+ fullCount.add(thunkAddrs.length);
+ for (Address thunkAddr : thunkAddrs) {
+ if (xrefs.size() >= max) {
+ break;
}
+
+ xrefs.add(new ThunkReference(thunkAddr, function.getEntryPoint()));
}
return xrefs;
}
@@ -109,6 +143,21 @@ public class XReferenceUtils {
* @return array of all offcut xrefs to the code unit
*/
public static List<Reference> getOffcutXReferences(CodeUnit cu, int max) {
+ return getOffcutXReferences(cu, max, null);
+ }
+
+ /**
+ * Returns an array containing all offcut xref references to the specified code unit
+ *
+ * @param cu the code unit to generate the offcut xrefs
+ * @param max max number of offcut xrefs to get, or -1 to get all offcut references
+ * @param fullCount a counter that will be updated with the full count of xrefs. This will have
+ * the full xref count regardless of whether the max is reached. The counter will be added to,
+ * which allows it to be passed to multiple methods on this class.
+ *
+ * @return array of all offcut xrefs to the code unit
+ */
+ public static List<Reference> getOffcutXReferences(CodeUnit cu, int max, Counter fullCount) {
Program program = cu.getProgram();
if (program == null) {
return Collections.emptyList();
@@ -118,15 +167,28 @@ public class XReferenceUtils {
return Collections.emptyList();
}
+ if (max < 0) {
+ max = Integer.MAX_VALUE;
+ }
+
+ if (fullCount == null) {
+ fullCount = new Counter();
+ }
+
List<Reference> offcuts = new ArrayList<>();
ReferenceManager refMgr = program.getReferenceManager();
AddressSet set = new AddressSet(cu.getMinAddress().add(1), cu.getMaxAddress());
AddressIterator it = refMgr.getReferenceDestinationIterator(set, true);
while (it.hasNext()) {
Address addr = it.next();
+ int count = refMgr.getReferenceCountTo(addr);
+ fullCount.add(count);
ReferenceIterator refIter = refMgr.getReferencesTo(addr);
while (refIter.hasNext()) {
- if (offcuts.size() - max == 0) {
+ if (offcuts.size() >= max) {
+ // Note: we break instead of returning, allowing the address iterator to keep
+ // updating the count. If this leads to performance issues, then we can change
+ // this to be a return and update the documentation.
break;
}
@@ -160,18 +222,43 @@ public class XReferenceUtils {
*/
public static void getVariableRefs(Variable var, List<Reference> xrefs, List<Reference> offcuts,
int max) {
+ getVariableRefs(var, xrefs, offcuts, max, null);
+ }
+
+ /**
+ * Populates the provided lists with the direct and offcut xrefs to the specified variable
+ *
+ * @param var variable to get references
+ * @param xrefs list to put direct references in
+ * @param offcuts list to put offcut references in
+ * @param max max number of xrefs to get, or -1 to get all references
+ * @param fullCount a counter that will be updated with the full count of xrefs. This will have
+ * the full xref count regardless of whether the max is reached. The counter will be added to,
+ * which allows it to be passed to multiple methods on this class.
+ */
+ public static void getVariableRefs(Variable var, List<Reference> xrefs, List<Reference> offcuts,
+ int max, Counter fullCount) {
Address addr = var.getMinAddress();
if (addr == null) {
return;
}
+ if (max < 0) {
+ max = Integer.MAX_VALUE;
+ }
+
+ if (fullCount == null) {
+ fullCount = new Counter();
+ }
+
Program program = var.getFunction().getProgram();
ReferenceManager refMgr = program.getReferenceManager();
Reference[] refs = refMgr.getReferencesTo(var);
+ fullCount.add(refs.length);
int total = 0;
for (Reference vref : refs) {
- if (total++ - max == 0) {
+ if (total++ >= max) {
break;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldFactory.java
index 202f13fb38..7393cb2d83 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldFactory.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -34,6 +34,7 @@ import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.Reference;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.VariableXRefFieldLocation;
+import ghidra.util.datastruct.Counter;
/**
* Variable Cross-reference Field Factory
@@ -89,9 +90,10 @@ public class VariableXRefFieldFactory extends XRefFieldFactory {
Variable var = (Variable) obj;
List<Reference> xrefs = new ArrayList<>();
List<Reference> offcuts = new ArrayList<>();
- XReferenceUtils.getVariableRefs(var, xrefs, offcuts, maxXRefs);
+ Counter fullCount = new Counter();
+ XReferenceUtils.getVariableRefs(var, xrefs, offcuts, maxXRefs, fullCount);
- if (xrefs.size() + offcuts.size() == 0) {
+ if (fullCount.intValue() == 0) {
return null;
}
@@ -104,7 +106,7 @@ public class VariableXRefFieldFactory extends XRefFieldFactory {
displayBlockName = false; //provider.isDisplayBlockName();
int totalXrefs = xrefs.size() + offcuts.size();
- boolean tooMany = totalXrefs > maxXRefs;
+ boolean tooMany = fullCount.intValue() > maxXRefs;
AttributedString delimiter = new AttributedString(delim, Colors.FOREGROUND, getMetrics());
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefHeaderFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefHeaderFieldFactory.java
index b8506a5373..b93d35031d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefHeaderFieldFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefHeaderFieldFactory.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -55,7 +55,8 @@ public class VariableXRefHeaderFieldFactory extends VariableXRefFieldFactory {
* @param displayOptions the Options for display properties.
* @param fieldOptions the Options for field specific properties.
*/
- public VariableXRefHeaderFieldFactory(FieldFormatModel model, ListingHighlightProvider hlProvider,
+ public VariableXRefHeaderFieldFactory(FieldFormatModel model,
+ ListingHighlightProvider hlProvider,
Options displayOptions, ToolOptions fieldOptions) {
super(FIELD_NAME, model, hlProvider, displayOptions, fieldOptions);
colorOptionName = "XRef Color";
@@ -107,9 +108,6 @@ public class VariableXRefHeaderFieldFactory extends VariableXRefFieldFactory {
return null;
}
- /**
- * @see ghidra.app.util.viewer.field.FieldFactory#getProgramLocation(int, int, ghidra.app.util.viewer.field.ListingField)
- */
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
ProxyObj<?> proxy = bf.getProxy();
@@ -140,6 +138,7 @@ public class VariableXRefHeaderFieldFactory extends VariableXRefFieldFactory {
if (varnode == null) {
return null;
}
+
Address varAddr = varnode.getAddress();
Program program = var.getFunction().getProgram();
ReferenceManager refMgr = program.getReferenceManager();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldFactory.java
index fde84f6949..e4163c6735 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldFactory.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,6 +24,8 @@ import java.util.function.Predicate;
import javax.swing.event.ChangeListener;
+import org.apache.commons.lang3.mutable.MutableInt;
+
import docking.widgets.fieldpanel.field.*;
import docking.widgets.fieldpanel.support.*;
import generic.theme.GThemeDefaults.Colors;
@@ -41,6 +43,7 @@ import ghidra.program.model.symbol.*;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.XRefFieldLocation;
import ghidra.util.HelpLocation;
+import ghidra.util.datastruct.Counter;
import ghidra.util.exception.AssertException;
import util.CollectionUtils;
@@ -235,9 +238,10 @@ public class XRefFieldFactory extends FieldFactory {
}
CodeUnit cu = (CodeUnit) obj;
- List<Reference> xrefs = XReferenceUtils.getXReferences(cu, maxXRefs + 1);
+ Counter fullCount = new Counter();
+ List<Reference> xrefs = XReferenceUtils.getXReferences(cu, maxXRefs, fullCount);
int maxOffcuts = Math.max(0, maxXRefs - xrefs.size());
- List<Reference> offcuts = XReferenceUtils.getOffcutXReferences(cu, maxOffcuts);
+ List<Reference> offcuts = XReferenceUtils.getOffcutXReferences(cu, maxOffcuts, fullCount);
if (sortChoice == SORT_CHOICE.Address) {
xrefs.sort(null);
offcuts.sort(null);
@@ -248,9 +252,9 @@ public class XRefFieldFactory extends FieldFactory {
}
if (groupByFunction) {
- return getFieldByFunction(proxy, varWidth, xrefs, offcuts);
+ return getFieldByFunction(proxy, varWidth, xrefs, offcuts, fullCount);
}
- return getFieldByAddress(proxy, varWidth, xrefs, offcuts);
+ return getFieldByAddress(proxy, varWidth, xrefs, offcuts, fullCount);
}
/*
@@ -281,9 +285,9 @@ public class XRefFieldFactory extends FieldFactory {
*/
private ListingField getFieldByFunction(ProxyObj<?> proxy, int varWidth, List<Reference> xrefs,
- List<Reference> offcuts) {
+ List<Reference> offcuts, MutableInt fullCount) {
- int totalXrefs = xrefs.size() + offcuts.size();
+ int totalXrefs = fullCount.intValue();
if (totalXrefs == 0) {
return null;
}
@@ -326,9 +330,9 @@ public class XRefFieldFactory extends FieldFactory {
xrefsByFunction, isOffcut, varWidth, hlFactory);
//
- // TODO maxXRefs makes sense when simply displaying xrefs. What does max mean when
- // binning xrefs by function. Currently, we use the max as the max row count, but
- // this may need to be changed and it may require a new tool option.
+ // Note: maxXRefs makes sense when simply displaying xrefs. What does max mean when
+ // binning xrefs by function. Currently, we use the max as the max row count, but
+ // this may need to be changed and it may require a new tool option.
//
int maxLines = maxXRefs;
@@ -479,9 +483,9 @@ public class XRefFieldFactory extends FieldFactory {
*/
private ListingField getFieldByAddress(ProxyObj<?> proxy, int varWidth, List<Reference> xrefs,
- List<Reference> offcuts) {
+ List<Reference> offcuts, MutableInt fullCount) {
- int totalXrefs = xrefs.size() + offcuts.size();
+ int totalXrefs = fullCount.intValue();
if (totalXrefs == 0) {
return null;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefHeaderFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefHeaderFieldFactory.java
index 7c26e0a03a..9289d2f21e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefHeaderFieldFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefHeaderFieldFactory.java
@@ -33,6 +33,7 @@ import ghidra.program.model.listing.Data;
import ghidra.program.model.symbol.Reference;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.XRefHeaderFieldLocation;
+import ghidra.util.datastruct.Counter;
/**
* Field for display XRef headers.
@@ -134,22 +135,19 @@ public class XRefHeaderFieldFactory extends XRefFieldFactory {
return null;
}
- List<Reference> xrefs = XReferenceUtils.getXReferences(cu, maxXRefs);
- int xRefCount = xrefs.size();
+ Counter counter = new Counter();
+ XReferenceUtils.getXReferences(cu, maxXRefs, counter);
+ int fullCount = counter.intValue();
+ int xRefCount = fullCount;
String xRefCountText = Integer.toString(xRefCount);
- if (xRefCount == maxXRefs) {
- xRefCountText += "+";
- }
- List<Reference> offcuts = XReferenceUtils.getOffcutXReferences(cu, maxXRefs);
+ counter = new Counter();
+ List<Reference> offcuts = XReferenceUtils.getOffcutXReferences(cu, maxXRefs, counter);
int offcutCount = offcuts.size();
+ fullCount = counter.intValue();
String offcutCountText = Integer.toString(offcutCount);
- if (offcutCount == maxXRefs) {
- offcutCountText += "+";
- }
-
if (offcutCount > 0) {
return "XREF[" + xRefCountText + "," + offcutCountText + "]: ";
}