Skip to content

Commit

Permalink
ctf.core: Initial field class implementation
Browse files Browse the repository at this point in the history
Implement the fixed unsigned integer field class and
create new structures from a trace fragment. Currently
the preamble fragment is not parsed as the code would
only deal with user attributes and extensions
which have not been implemented yet. Additionally,
field classes that are not Json Objects (field class
aliases) have not yet been handled.

Change-Id: Icfc28b0fbee9c037ebd8ca58a24da1eb389d2018
Signed-off-by: Sehr Moosabhoy <sehr.moosabhoy@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/202815
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Marco Miller <marco.miller@ericsson.com>
Reviewed-by: Marco Miller <marco.miller@ericsson.com>
  • Loading branch information
Sehr Moosabhoy authored and marco-miller committed Jul 25, 2023
1 parent cb97334 commit d1ad646
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.antlr.runtime.ANTLRReaderStream;
Expand Down Expand Up @@ -239,27 +240,26 @@ private static ICTFMetadataNode parseJsonToTree(String json) throws CTFException
ICTFMetadataNode root = new CTFJsonMetadataNode(null, CTFParser.tokenNames[CTFParser.ROOT], null);

for (int i = 1; i < jsonBlocks.length; i++) {
@Nullable
ICTFMetadataNode fragment;
try {
fragment = gson.fromJson(jsonBlocks[i], CTFJsonMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], CTFJsonMetadataNode.class));
} catch (JsonSyntaxException e) {
throw new CTFException("Trace cannot be parsed as CTF2"); //$NON-NLS-1$
}

String type = fragment.getType();
if (type.equals(JsonMetadataStrings.FRAGMENT_PREAMBLE)) {
fragment = gson.fromJson(jsonBlocks[i], JsonPreambleMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonPreambleMetadataNode.class));
} else if (type.equals(JsonMetadataStrings.FRAGMENT_TRACE)) {
fragment = gson.fromJson(jsonBlocks[i], JsonTraceMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonTraceMetadataNode.class));
} else if (type.equals(JsonMetadataStrings.FRAGMENT_CLOCK)) {
fragment = gson.fromJson(jsonBlocks[i], JsonClockMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonClockMetadataNode.class));
} else if (type.equals(JsonMetadataStrings.FRAGMENT_EVENT_RECORD)) {
fragment = gson.fromJson(jsonBlocks[i], JsonEventRecordMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonEventRecordMetadataNode.class));
} else if (type.equals(JsonMetadataStrings.FRAGMENT_DATA_STREAM)) {
fragment = gson.fromJson(jsonBlocks[i], JsonDataStreamMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonDataStreamMetadataNode.class));
} else if (type.equals(JsonMetadataStrings.FRAGMENT_FIELD_ALIAS)) {
fragment = gson.fromJson(jsonBlocks[i], JsonFieldClassAliasMetadataNode.class);
fragment = Objects.requireNonNull(gson.fromJson(jsonBlocks[i], JsonFieldClassAliasMetadataNode.class));
}

((CTFJsonMetadataNode) fragment).initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
import org.eclipse.tracecompass.ctf.core.event.CTFClock;
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
Expand All @@ -40,6 +42,7 @@
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.trace.TraceDeclarationParser;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;

import com.google.common.collect.Iterables;

Expand Down Expand Up @@ -142,7 +145,7 @@ private void parseRoot(ICTFMetadataNode root) throws ParseException {
final String type = child.getType();
if (CTFParser.tokenNames[CTFParser.DECLARATION].equals(type)) {
parseRootDeclaration(child);
} else if (CTFParser.tokenNames[CTFParser.TRACE].equals(type)) {
} else if (CTFParser.tokenNames[CTFParser.TRACE].equals(type) || JsonMetadataStrings.FRAGMENT_TRACE.equals(type)) {
if (traceNode != null) {
throw new ParseException("Only one trace block is allowed"); //$NON-NLS-1$
}
Expand All @@ -161,6 +164,8 @@ private void parseRoot(ICTFMetadataNode root) throws ParseException {
fTrace.setEnvironment(EnvironmentParser.INSTANCE.parse(child, null));
} else if (CTFParser.tokenNames[CTFParser.CALLSITE].equals(type)) {
callsites.add(CallSiteParser.INSTANCE.parse(child, null));
} else if (JsonMetadataStrings.FRAGMENT_PREAMBLE.equals(type)) {
// Do nothing for now
} else {
throw childTypeError(child);
}
Expand Down Expand Up @@ -235,6 +240,9 @@ private void parseTrace(ICTFMetadataNode traceNode) throws ParseException {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.TYPEALIAS].equals(type)) {
TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(trace, fRoot));
} else if (child instanceof JsonStructureFieldMetadataNode) {
IDeclaration packetHeaderDecl = TypeSpecifierListParser.INSTANCE.parse(child, new TypeSpecifierListParser.Param(fTrace, null, null, fRoot));
trace.setPacketHeader((StructDeclaration) packetHeaderDecl);
} else if (CTFParser.tokenNames[CTFParser.TYPEDEF].equals(type)) {
TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(trace, fRoot));
} else if (CTFParser.tokenNames[CTFParser.CTF_EXPRESSION_TYPE].equals(type) || CTFParser.tokenNames[CTFParser.CTF_EXPRESSION_VAL].equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
Expand Down Expand Up @@ -72,6 +73,24 @@ public JsonElement getFieldClass() {
return fFieldClass;
}

/**
* Get the role of the field class in this member
*
* @return the role
*/
public String getRole() {
String role = null;
if (fFieldClass != null && fFieldClass.isJsonObject()) {
JsonObject obj = fFieldClass.getAsJsonObject();
if (obj.has(JsonMetadataStrings.ROLES)) {
// Assuming only 1 role per field class
JsonArray roles = obj.get(JsonMetadataStrings.ROLES).getAsJsonArray();
role = roles.get(0).getAsString();
}
}
return role;
}

@Override
public void initialize() throws CTFException {
if (fFieldClass.isJsonObject() && fFieldClass.getAsJsonObject().has(JsonMetadataStrings.TYPE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public interface MetadataStrings {
String BE = "be";
/** Little endian */
String LE = "le";
/** Little endian for CTF2 */
String LITTLE_ENDIAN = "little-endian";
/** Big endian for CTF2 */
String BIG_ENDIAN = "big-endian";
/** Alignment of a field */
String ALIGN = "align";
/** Mantissa digits */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;

Expand Down Expand Up @@ -89,8 +90,16 @@ public Long parse(ICTFMetadataNode tree, ICommonTreeParserParameter param) throw
+ alignment);
}

return alignment;
} else if (tree instanceof JsonStructureFieldMetadataNode) {
long alignment = ((JsonStructureFieldMetadataNode) tree).getMinimumAlignment();
if (!isValidAlignment(alignment)) {
throw new ParseException(INVALID_VALUE_FOR_ALIGNMENT + " : " //$NON-NLS-1$
+ alignment);
}
return alignment;
}

throw new ParseException(INVALID_VALUE_FOR_ALIGNMENT); // $NON-NLS-1$
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.ByteOrder;

import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFAntlrMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFJsonMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
Expand Down Expand Up @@ -96,21 +98,30 @@ public final ByteOrder parse(ICTFMetadataNode byteOrderTree, ICommonTreeParserPa
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
}
CTFTrace trace = ((Param) param).fTrace;
ICTFMetadataNode firstChild = byteOrderTree.getChild(0);

if (isUnaryString(firstChild)) {
String strval = concatenateUnaryStrings(byteOrderTree.getChildren());
String strval = null;
ICTFMetadataNode firstChild = null;

if (strval.equals(MetadataStrings.LE)) {
return ByteOrder.LITTLE_ENDIAN;
} else if (strval.equals(MetadataStrings.BE)
|| strval.equals(MetadataStrings.NETWORK)) {
return ByteOrder.BIG_ENDIAN;
} else if (strval.equals(MetadataStrings.NATIVE)) {
ByteOrder byteOrder = trace.getByteOrder();
return (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder;
} else {
throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER);
if (byteOrderTree instanceof CTFAntlrMetadataNode) {
strval = concatenateUnaryStrings(byteOrderTree.getChildren());
firstChild = byteOrderTree.getChild(0);
} else if (byteOrderTree instanceof CTFJsonMetadataNode) {
strval = byteOrderTree.getText();
}

if (isUnaryString(byteOrderTree) || isUnaryString(firstChild)) {
if (strval != null) {
if (strval.equals(MetadataStrings.LE) || MetadataStrings.LITTLE_ENDIAN.equals(strval)) {
return ByteOrder.LITTLE_ENDIAN;
} else if (strval.equals(MetadataStrings.BE)
|| strval.equals(MetadataStrings.NETWORK) || strval.equals(MetadataStrings.BIG_ENDIAN)) {
return ByteOrder.BIG_ENDIAN;
} else if (strval.equals(MetadataStrings.NATIVE)) {
ByteOrder byteOrder = trace.getByteOrder();
return (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder;
} else {
throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER);
}
}
}
throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;

import com.google.gson.JsonObject;

/**
* The "typealias" declaration can be used to give a name (including pointer
Expand Down Expand Up @@ -81,26 +86,47 @@ public IDeclaration parse(ICTFMetadataNode typealias, ICommonTreeParserParameter

ICTFMetadataNode target = null;
ICTFMetadataNode alias = null;
IDeclaration targetDeclaration = null;
CTFTrace trace = ((Param) param).fTrace;

for (ICTFMetadataNode child : children) {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.TYPEALIAS_TARGET].equals(type)) {
target = child;
} else if (CTFParser.tokenNames[CTFParser.TYPEALIAS_ALIAS].equals(type)) {
alias = child;
String aliasString;
if (typealias instanceof JsonStructureFieldMemberMetadataNode) {
JsonStructureFieldMemberMetadataNode member = ((JsonStructureFieldMemberMetadataNode) typealias);
aliasString = member.getName();
if (member.getFieldClass().isJsonObject()) {
JsonObject fieldClass = member.getFieldClass().getAsJsonObject();
if (JsonMetadataStrings.FIXED_UNSIGNED_INTEGER_FIELD.equals(typealias.getType())) {
fieldClass.addProperty("signed", false); //$NON-NLS-1$
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
} else {
throw new ParseException("Invalid field class"); //$NON-NLS-1$
}
} else {
throw childTypeError(child);
// Should be changed once field-class-alias
// fragments are implemented
throw new ParseException("Field classes that are not Json Objects are not yet supported"); //$NON-NLS-1$
}
} else {
for (ICTFMetadataNode child : children) {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.TYPEALIAS_TARGET].equals(type)) {
target = child;
} else if (CTFParser.tokenNames[CTFParser.TYPEALIAS_ALIAS].equals(type)) {
alias = child;
} else {
throw childTypeError(child);
}
}
}
CTFTrace trace = ((Param) param).fTrace;
IDeclaration targetDeclaration = TypeAliasTargetParser.INSTANCE.parse(target, new TypeAliasTargetParser.Param(trace, scope));

if ((targetDeclaration instanceof VariantDeclaration)
&& ((VariantDeclaration) targetDeclaration).isTagged()) {
throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$
}
targetDeclaration = TypeAliasTargetParser.INSTANCE.parse(target, new TypeAliasTargetParser.Param(trace, scope));

if ((targetDeclaration instanceof VariantDeclaration)
&& ((VariantDeclaration) targetDeclaration).isTagged()) {
throw new ParseException("Typealias of untagged variant is not permitted"); //$NON-NLS-1$
}

String aliasString = TypeAliasAliasParser.INSTANCE.parse(alias, null);
aliasString = TypeAliasAliasParser.INSTANCE.parse(alias, null);
}

scope.registerType(aliasString, targetDeclaration);
return targetDeclaration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFAntlrMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.event.EventScopeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.stream.StreamScopeParser;
Expand Down Expand Up @@ -112,8 +113,8 @@ public IDeclaration parse(ICTFMetadataNode typeDeclarator, ICommonTreeParserPara
List<ICTFMetadataNode> lengths = new LinkedList<>();
ICTFMetadataNode identifier = null;

/* Separate the tokens by type */
if (typeDeclarator != null) {
if (typeDeclarator instanceof CTFAntlrMetadataNode) {
/* Separate the tokens by type */
children = typeDeclarator.getChildren();
for (ICTFMetadataNode child : children) {

Expand All @@ -128,7 +129,8 @@ public IDeclaration parse(ICTFMetadataNode typeDeclarator, ICommonTreeParserPara
throw childTypeError(child);
}
}

} else {
identifier = typeDeclarator;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFAntlrMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.CTFJsonMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMetadataNode;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.enumeration.EnumParser;
Expand Down Expand Up @@ -110,21 +113,28 @@ public IDeclaration parse(ICTFMetadataNode typeSpecifierList, ICommonTreeParserP
CTFTrace trace = ((Param) param).fTrace;
ICTFMetadataNode identifier = ((Param) param).fIdentifier;
IDeclaration declaration = null;
ICTFMetadataNode firstChild = null;
String type = null;

/*
* By looking at the first element of the type specifier list, we can
* determine which type it belongs to.
* determine which type it belongs to. If parsing JSON, there are no
* children
*/
ICTFMetadataNode firstChild = typeSpecifierList.getChild(0);
if (typeSpecifierList instanceof CTFAntlrMetadataNode) {
firstChild = typeSpecifierList.getChild(0);
type = firstChild.getType();
} else if (typeSpecifierList instanceof CTFJsonMetadataNode) {
firstChild = typeSpecifierList;
}

String type = firstChild.getType();
if (CTFParser.tokenNames[CTFParser.FLOATING_POINT].equals(type)) {
declaration = FloatDeclarationParser.INSTANCE.parse(firstChild, new FloatDeclarationParser.Param(trace));
} else if (CTFParser.tokenNames[CTFParser.INTEGER].equals(type)) {
declaration = IntegerDeclarationParser.INSTANCE.parse(firstChild, new IntegerDeclarationParser.Param(trace));
} else if (CTFParser.tokenNames[CTFParser.STRING].equals(type)) {
declaration = StringDeclarationParser.INSTANCE.parse(firstChild, null);
} else if (CTFParser.tokenNames[CTFParser.STRUCT].equals(type)) {
} else if (CTFParser.tokenNames[CTFParser.STRUCT].equals(type) || firstChild instanceof JsonStructureFieldMetadataNode) {
declaration = StructParser.INSTANCE.parse(firstChild, new StructParser.Param(trace, identifier, scope));
StructDeclaration structDeclaration = (StructDeclaration) declaration;
if (structDeclaration.hasField(MetadataStrings.ID)) {
Expand Down
Loading

0 comments on commit d1ad646

Please sign in to comment.