Ghidra is a software reverse engineering (SRE) framework
GP-5913: Added parent path to DataType Listing and Decompiler Hover
ghidranoob46febafe1651
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/hover/DataTypeListingHover.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/hover/DataTypeListingHover.javaindex b7ee1f6194..2263c30d47 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/hover/DataTypeListingHover.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/hover/DataTypeListingHover.java@@ -31,8 +31,7 @@ import ghidra.program.model.data.Enum;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.Equate;import ghidra.program.util.*;-import ghidra.util.HTMLUtilities;-import ghidra.util.UniversalID;+import ghidra.util.*;public class DataTypeListingHover extends AbstractConfigurableHover implements ListingHoverService {@@ -132,10 +131,48 @@ public class DataTypeListingHover extends AbstractConfigurableHover implements Ldt = data.getDataType();return createTooltipComponent(dt.getRepresentation(data, data, data.getLength()));}+ if (programLocation instanceof FieldNameFieldLocation nameLoc) {+ Data data = nameLoc.getDataComponent();+ Data parent = data == null ? null : data.getParent();+ if (parent == null) {+ return createTooltipComponent("Field Name: " + nameLoc.getFieldName());+ }+ StringBuilder sb = new StringBuilder(HTMLUtilities.HTML);+ sb.append("<TABLE>");+ if (parent != null) {+ DataType parentType = parent.getDataType();+ sb.append(row("Parent: ", html(parentType.getDataTypePath())));+ int offset = (int) data.getAddress().subtract(parent.getAddress());+ sb.append(row("Offset: ", NumericUtilities.toHexString(offset)));+ sb.append(row("Field Name: ", nameLoc.getFieldName()));+ if (parentType instanceof Structure pst) {+ DataTypeComponent dtc = pst.getComponentAt(offset);+ String comment = dtc == null ? null : dtc.getComment();+ if (comment != null) {+ sb.append(row("Comment: ", html(comment)));+ }+ }+ }+ sb.append("</TABLE>");+ return createTooltipComponent(sb.toString());+ }return null;}+ private String row(String... cols) {+ StringBuilder sb = new StringBuilder("<TR>");+ for (String col : cols) {+ sb.append("<TD>").append(col).append("</TD>");+ }+ sb.append("</TR>");+ return sb.toString();+ }++ private String html(Object obj) {+ return obj == null ? "" : HTMLUtilities.friendlyEncodeHTML(obj.toString());+ }+private String getLocationSupplimentalToolTipText(DataType dt, Data dataInstance) {String result = "";if (dt instanceof DataTypeWithCharset) {@@ -145,12 +182,10 @@ public class DataTypeListingHover extends AbstractConfigurableHover implements Lif (StringDataInstance.isString(dataInstance)) {StringDataInstance sdi = StringDataInstance.getStringDataInstance(dataInstance);if (sdi.isShowTranslation()) {- result += String.format("<br>Original value: %s",- HTMLUtilities.friendlyEncodeHTML(sdi.getStringValue()));+ result += String.format("<br>Original value: %s", html(sdi.getStringValue()));}if (!sdi.isShowTranslation() && sdi.getTranslatedValue() != null) {- result += String.format("<br>Translated value: %s",- HTMLUtilities.friendlyEncodeHTML(sdi.getTranslatedValue()));+ result += String.format("<br>Translated value: %s", html(sdi.getTranslatedValue()));}if (sdi.isMissingNullTerminator()) {result += "<br>Missing NULL terminator.";diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/DataTypeDecompilerHover.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/DataTypeDecompilerHover.javaindex d5699d3c94..7279d22fbe 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/DataTypeDecompilerHover.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/DataTypeDecompilerHover.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.@@ -119,7 +119,8 @@ public class DataTypeDecompilerHover extends AbstractConfigurableHover//@formatter:offbuffy.append("<TABLE>");buffy.append(- row("Parent: ", HTMLUtilities.friendlyEncodeHTML(parentType.getName())));+ row("Parent: ", HTMLUtilities.friendlyEncodeHTML(+ parentType.getDataTypePath().toString())));buffy.append(row("Offset: ", NumericUtilities.toHexString(offset)));buffy.append(diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/FieldNameFieldLocation.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/FieldNameFieldLocation.javaindex e1ef650ad7..cca11409cf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/FieldNameFieldLocation.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/FieldNameFieldLocation.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.@@ -21,6 +21,7 @@ import java.util.Objects;import ghidra.framework.options.SaveState;import ghidra.program.model.address.Address;+import ghidra.program.model.listing.Data;import ghidra.program.model.listing.Program;/**@@ -37,7 +38,7 @@ public class FieldNameFieldLocation extends CodeUnitLocation {* @param program the program of the location* @param addr the address of the code unit* @param componentPath if not null, it is the array of indexes that point to a specific data- * type inside of another data type+ * type inside of another data type* @param fieldName the field name* @param charOffset the character position within the field name for this location.*/@@ -57,6 +58,7 @@ public class FieldNameFieldLocation extends CodeUnitLocation {/*** Returns the field name of this location.+ ** @return the name.*/public String getFieldName() {@@ -105,4 +107,17 @@ public class FieldNameFieldLocation extends CodeUnitLocation {public String toString() {return super.toString() + ", Field Name = " + fieldName;}++ /**+ * Get the data component representing the named field+ *+ * @return the data unit+ */+ public Data getDataComponent() {+ Data data = program.getListing().getDataContaining(addr);+ if (data == null) {+ return null;+ }+ return data.getComponent(getComponentPath());+ }}