Skip to content

Commit b35c9e2

Browse files
vladaramaMatthewKhouzam
authored andcommitted
ctf2: add varints support and testing
Signed-off-by: Vlad Arama <vlad.arama@ericsson.com>
1 parent 1f335d8 commit b35c9e2

7 files changed

Lines changed: 145 additions & 22 deletions

File tree

ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/types/IntegerDeclarationTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ public void testIntegerDeclaration() {
7171
assertEquals(false, result.isSigned());
7272
}
7373

74+
/**
75+
* Run the createVarintDeclaration method test (boolean, int, String,
76+
* boolean)
77+
*/
78+
@Test
79+
public void testVarintDeclaration() {
80+
boolean signed = false;
81+
boolean varint = true;
82+
int base = 1;
83+
ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;
84+
85+
IntegerDeclaration result = IntegerDeclaration.createVarintDeclaration(signed, base, null, varint);
86+
87+
assertNotNull(result);
88+
assertEquals(1, result.getBase());
89+
assertEquals(true, result.isVarint());
90+
String outputValue = "[declaration] integer[";
91+
assertEquals(outputValue,
92+
result.toString().substring(0, outputValue.length()));
93+
assertEquals(byteOrder, result.getByteOrder());
94+
assertEquals(false, result.isSigned());
95+
}
96+
7497
/**
7598
* Test the factory part more rigorously to make sure there are no
7699
* regressions
@@ -159,6 +182,15 @@ public void testIsCharacter() {
159182
assertEquals(false, result);
160183
}
161184

185+
/**
186+
* Run the boolean isVarint() method test.
187+
*/
188+
@Test
189+
public void testIsVarint() {
190+
boolean result = fixture.isVarint();
191+
assertEquals(false, result);
192+
}
193+
162194
/**
163195
* Run the boolean isCharacter() method test.
164196
*/

ctf/org.eclipse.tracecompass.ctf.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-Vendor: %Bundle-Vendor
5-
Bundle-Version: 4.4.1.qualifier
5+
Bundle-Version: 4.5.0.qualifier
66
Bundle-Localization: plugin
77
Bundle-SymbolicName: org.eclipse.tracecompass.ctf.core;singleton:=true
88
Bundle-ActivationPolicy: lazy

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.eclipse.tracecompass.ctf.core.CTFException;
2727
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
2828
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
29+
import org.eclipse.tracecompass.internal.ctf.core.utils.LEB128;
2930

3031
/**
3132
* A CTF integer declaration.
@@ -127,6 +128,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
127128
private final Encoding fEncoding;
128129
private final long fAlignment;
129130
private final String fClock;
131+
private boolean fVarint = false;
130132

131133
// ------------------------------------------------------------------------
132134
// Constructors
@@ -154,7 +156,8 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
154156
* @param alignment
155157
* The minimum alignment. Should be >= 1
156158
* @return the integer declaration
157-
* @deprecated use {@link #createDeclaration(int, boolean, int, ByteOrder, Encoding, String, long, String)}
159+
* @deprecated use
160+
* {@link #createDeclaration(int, boolean, int, ByteOrder, Encoding, String, long, String)}
158161
*/
159162
@Deprecated
160163
public static IntegerDeclaration createDeclaration(int len, boolean signed, int base,
@@ -315,6 +318,28 @@ public static IntegerDeclaration createDeclaration(int len, boolean signed, int
315318
return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role);
316319
}
317320

321+
/**
322+
* Create method for CTF2 varints
323+
*
324+
* @param signed
325+
* Is the integer signed? false == unsigned
326+
* @param base
327+
* The base (10-16 are most common)
328+
* @param role
329+
* The role of the integer declaration
330+
* @param varint
331+
* A boolean indicating if the declaration is a varint
332+
* @return IntegerDeclaration
333+
*
334+
* @since 4.5
335+
*/
336+
public static IntegerDeclaration createVarintDeclaration(boolean signed, int base, @Nullable String role, boolean varint) {
337+
IntegerDeclaration decl = new IntegerDeclaration(0, signed, base, null, Encoding.NONE, "", 0); //$NON-NLS-1$
338+
decl.setRole(role);
339+
decl.setVarint(varint);
340+
return decl;
341+
}
342+
318343
private static boolean isBigEndian(@Nullable ByteOrder byteOrder) {
319344
return (byteOrder != null) && byteOrder.equals(ByteOrder.BIG_ENDIAN);
320345
}
@@ -336,6 +361,7 @@ private static boolean isBigEndian(@Nullable ByteOrder byteOrder) {
336361
* The clock path, can be null
337362
* @param alignment
338363
* The minimum alignment. Should be &ge; 1
364+
*
339365
*/
340366
private IntegerDeclaration(int len, boolean signed, int base,
341367
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
@@ -392,6 +418,26 @@ public boolean isSigned() {
392418
return fSigned;
393419
}
394420

421+
/**
422+
* Is the integer a varint ?
423+
*
424+
* @return a boolean indicating varint status
425+
* @since 4.5
426+
*/
427+
public boolean isVarint() {
428+
return fVarint;
429+
}
430+
431+
/**
432+
* Setter for fVarint
433+
*
434+
* @param varint
435+
* a boolean indicating varint status
436+
*/
437+
private void setVarint(boolean varint) {
438+
fVarint = varint;
439+
}
440+
395441
/**
396442
* Get the integer base commonly decimal or hex
397443
*
@@ -529,6 +575,12 @@ public BigInteger getMinValue() {
529575
}
530576

531577
private long read(BitBuffer input) throws CTFException {
578+
if (isVarint()) {
579+
if (isSigned()) {
580+
return LEB128.readSignedLeb(input);
581+
}
582+
return LEB128.readUnsignedLeb(input);
583+
}
532584
/* Offset the buffer position wrt the current alignment */
533585
alignRead(input);
534586

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/TypeAliasParser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
public final class TypeAliasParser extends AbstractScopedCommonTreeParser {
5050

5151
private static final String SIGNED = "signed"; //$NON-NLS-1$
52+
private static final String VARINT = "varint"; //$NON-NLS-1$
5253

5354
/**
5455
* Parameters for the typealias parser
@@ -136,9 +137,19 @@ public IDeclaration parse(ICTFMetadataNode typealias, ICommonTreeParserParameter
136137
if (fieldClass != null) {
137138
if (JsonMetadataStrings.FIXED_UNSIGNED_INTEGER_FIELD.equals(type)) {
138139
fieldClass.addProperty(SIGNED, false);
140+
fieldClass.addProperty(VARINT, false);
139141
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
140142
} else if (JsonMetadataStrings.FIXED_SIGNED_INTEGER_FIELD.equals(type)) {
141143
fieldClass.addProperty(SIGNED, true);
144+
fieldClass.addProperty(VARINT, false);
145+
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
146+
} else if (JsonMetadataStrings.VARIABLE_UNSIGNED_INTEGER_FIELD.equals(type)) {
147+
fieldClass.addProperty(SIGNED, false);
148+
fieldClass.addProperty(VARINT, true);
149+
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
150+
} else if (JsonMetadataStrings.VARIABLE_SIGNED_INTEGER_FIELD.equals(type)) {
151+
fieldClass.addProperty(SIGNED, true);
152+
fieldClass.addProperty(VARINT, true);
142153
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
143154
} else if (JsonMetadataStrings.STATIC_LENGTH_BLOB.equals(type)) {
144155
targetDeclaration = BlobDeclarationParser.INSTANCE.parse(typealias, null);

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/integer/IntegerDeclarationParser.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public Param(CTFTrace trace) {
110110
private static final @NonNull String LENGTH = "length"; //$NON-NLS-1$
111111
private static final @NonNull String SIGNED = "signed"; //$NON-NLS-1$
112112
private static final @NonNull String ALIGNMENT = "alignment"; //$NON-NLS-1$
113+
private static final @NonNull String VARINT = "varint"; //$NON-NLS-1$
113114

114115
private IntegerDeclarationParser() {
115116
}
@@ -131,6 +132,7 @@ public IntegerDeclaration parse(ICTFMetadataNode integer, ICommonTreeParserParam
131132

132133
/* The return value */
133134
boolean signed = false;
135+
boolean varint = false;
134136
ByteOrder byteOrder = trace.getByteOrder();
135137
long size = 0;
136138
long alignment = 0;
@@ -144,21 +146,28 @@ public IntegerDeclaration parse(ICTFMetadataNode integer, ICommonTreeParserParam
144146
if (integer instanceof JsonStructureFieldMemberMetadataNode) {
145147
JsonStructureFieldMemberMetadataNode member = (JsonStructureFieldMemberMetadataNode) integer;
146148
JsonObject fieldclass = member.getFieldClass().getAsJsonObject();
147-
148-
size = fieldclass.get(LENGTH).getAsInt();
149+
role = member.getRole();
149150
// by default fieldclass is unsigned
150151
if (fieldclass.has(SIGNED)) {
151152
signed = fieldclass.get(SIGNED).getAsBoolean();
152153
}
153-
CTFJsonMetadataNode bo = new CTFJsonMetadataNode(integer, CTFParser.tokenNames[CTFParser.UNARY_EXPRESSION_STRING], fieldclass.get(BYTE_ORDER).getAsString());
154-
byteOrder = ByteOrderParser.INSTANCE.parse(bo, new ByteOrderParser.Param(trace));
155-
if (fieldclass.has(ALIGNMENT)) {
156-
alignment = fieldclass.get(ALIGNMENT).getAsInt();
157-
}
158154
if (fieldclass.has(PREFERRED_BASE)) {
159155
base = fieldclass.get(PREFERRED_BASE).getAsInt();
160156
}
161-
role = member.getRole();
157+
if (fieldclass.has(VARINT)) {
158+
varint = fieldclass.get(VARINT).getAsBoolean();
159+
}
160+
if (varint) {
161+
return IntegerDeclaration.createVarintDeclaration(signed, base, role, true);
162+
}
163+
if (fieldclass.has(ALIGNMENT)) {
164+
alignment = fieldclass.get(ALIGNMENT).getAsInt();
165+
}
166+
size = fieldclass.get(LENGTH).getAsInt();
167+
168+
CTFJsonMetadataNode bo = new CTFJsonMetadataNode(integer, CTFParser.tokenNames[CTFParser.UNARY_EXPRESSION_STRING], fieldclass.get(BYTE_ORDER).getAsString());
169+
byteOrder = ByteOrderParser.INSTANCE.parse(bo, new ByteOrderParser.Param(trace));
170+
162171
} else if (integer instanceof CTFAntlrMetadataNode) {
163172
List<ICTFMetadataNode> children = integer.getChildren();
164173
/*

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/utils/JsonMetadataStrings.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,25 @@ private JsonMetadataStrings() {
123123
public static final String ALIAS = "alias"; //$NON-NLS-1$
124124

125125
/**
126-
* Type string for an unsigned integer field class
126+
* Type string for an unsigned fixed integer field class
127127
*/
128128
public static final String FIXED_UNSIGNED_INTEGER_FIELD = "fixed-length-unsigned-integer"; //$NON-NLS-1$
129129

130130
/**
131-
* Type string for a signed integer field class
131+
* Type string for a signed fixed integer field class
132132
*/
133133
public static final String FIXED_SIGNED_INTEGER_FIELD = "fixed-length-signed-integer"; //$NON-NLS-1$
134134

135+
/**
136+
* Type string for an unsigned variable integer field class
137+
*/
138+
public static final String VARIABLE_UNSIGNED_INTEGER_FIELD = "variable-length-unsigned-integer"; //$NON-NLS-1$
139+
140+
/**
141+
* Type string for a signed variable integer field class
142+
*/
143+
public static final String VARIABLE_SIGNED_INTEGER_FIELD = "variable-length-signed-integer"; //$NON-NLS-1$
144+
135145
/**
136146
* Type string for a static length blob field class
137147
*/

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/utils/LEB128.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
import java.io.DataInput;
1515
import java.io.IOException;
16-
import java.nio.ByteBuffer;
16+
import org.eclipse.tracecompass.ctf.core.CTFException;
17+
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
1718

1819
/**
19-
* LEB128 decoding utility functions.
20+
* LEB128 decoding utility functions.
2021
* Original implementation: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/200680
2122
*
2223
* @author Matthew Khouzam
@@ -25,20 +26,24 @@
2526
*/
2627
public class LEB128 {
2728
/**
28-
* Reads an unsigned LEB128 encoded integer from a ByteBuffer. The decoding
29+
* Reads an unsigned LEB128 encoded integer from a BitBuffer. The decoding
2930
* stops if the shift exceeds 64 bits to prevent overflow, as a long can
3031
* only support up to 64 bits.
3132
*
3233
* @param in
33-
* ByteBuffer containing the LEB128 encoded integer.
34+
* BitBuffer containing the LEB128 encoded integer.
3435
* @return The decoded integer as a long.
36+
* @throws CTFException
37+
* An error occurred reading the data. If more than 64 bits at a
38+
* time are read, or the buffer is read beyond its end, this
39+
* exception will be raised.
3540
*/
36-
public static long readUnsignedLeb(ByteBuffer in) {
41+
public static long readUnsignedLeb(BitBuffer in) throws CTFException {
3742
long result = 0;
3843
long shift = 0;
3944
byte current = 0;
4045
do {
41-
current = in.get();
46+
current = (byte) in.get(8, false);
4247
result |= ((long) (current & 0x7f)) << shift;
4348
shift += 7;
4449
} while ((current & 0x80) != 0 && shift < 64);
@@ -70,20 +75,24 @@ public static long readUnsignedLeb(DataInput in) throws IOException {
7075
}
7176

7277
/**
73-
* Reads a signed LEB128 encoded integer from a ByteBuffer. The decoding
78+
* Reads a signed LEB128 encoded integer from a BitBuffer. The decoding
7479
* stops if the shift exceeds 64 bits to prevent overflow, as a long can
7580
* only support up to 64 bits.
7681
*
7782
* @param in
78-
* ByteBuffer containing the LEB128 encoded integer.
83+
* BitBuffer containing the LEB128 encoded integer.
7984
* @return The decoded signed integer as a long.
85+
* @throws CTFException
86+
* An error occurred reading the data. If more than 64 bits at a
87+
* time are read, or the buffer is read beyond its end, this
88+
* exception will be raised.
8089
*/
81-
public static long readSignedLeb(ByteBuffer in) {
90+
public static long readSignedLeb(BitBuffer in) throws CTFException {
8291
long result = 0;
8392
int shift = 0;
8493
byte current;
8594
do {
86-
current = in.get();
95+
current = (byte) in.get(8, false);
8796
result |= ((long) (current & 0x7f)) << shift;
8897
shift += 7;
8998
} while ((current & 0x80) != 0 && shift < 64);

0 commit comments

Comments
 (0)