Skip to content

Commit 0afc8a5

Browse files
vladaramabhufmann
authored andcommitted
ctf: add support for mappings
Signed-off-by: Vlad Arama <vlad.arama@ericsson.com>
1 parent 26f21e9 commit 0afc8a5

5 files changed

Lines changed: 287 additions & 13 deletions

File tree

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

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
2+
* Copyright (c) 2011, 2024 Ericsson, Ecole Polytechnique de Montreal and others
33
*
44
* All rights reserved. This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0 which
@@ -21,6 +21,10 @@
2121
import java.math.BigInteger;
2222
import java.nio.ByteOrder;
2323

24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
2428
import org.eclipse.jdt.annotation.NonNullByDefault;
2529
import org.eclipse.jdt.annotation.Nullable;
2630
import org.eclipse.tracecompass.ctf.core.CTFException;
@@ -129,6 +133,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
129133
private final long fAlignment;
130134
private final String fClock;
131135
private boolean fVarint = false;
136+
private Map<String, List<IntegerRange>> fMappings = new HashMap<>();
132137

133138
// ------------------------------------------------------------------------
134139
// Constructors
@@ -315,7 +320,38 @@ public static IntegerDeclaration createDeclaration(int len, boolean signed, int
315320
}
316321
}
317322
}
318-
return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role);
323+
return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role, null);
324+
}
325+
326+
/**
327+
* Alternate create method for CTF2 integers which have roles and mappings
328+
*
329+
* @param len
330+
* The length in bits
331+
* @param signed
332+
* Is the integer signed? false == unsigned
333+
* @param base
334+
* The base (10-16 are most common)
335+
* @param byteOrder
336+
* Big-endian little-endian or other
337+
* @param encoding
338+
* ascii, utf8 or none.
339+
* @param clock
340+
* The clock path, can be null
341+
* @param alignment
342+
* The minimum alignment. Should be >= 1
343+
* @param role
344+
* The role of the declaration
345+
* @param mappings
346+
* A mapped range of integers
347+
* @return The integer declaration
348+
* @since 4.5
349+
*/
350+
public static IntegerDeclaration createDeclaration(int len, boolean signed, int base,
351+
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role, Map<String, List<IntegerRange>> mappings) {
352+
IntegerDeclaration decl = createDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role);
353+
decl.setMappings(mappings);
354+
return decl;
319355
}
320356

321357
/**
@@ -340,6 +376,33 @@ public static IntegerDeclaration createVarintDeclaration(boolean signed, int bas
340376
return decl;
341377
}
342378

379+
/**
380+
* Create method for CTF2 varints with mappings
381+
*
382+
* @param signed
383+
* Is the integer signed? false == unsigned
384+
* @param base
385+
* The base (10-16 are most common)
386+
* @param role
387+
* The role of the integer declaration
388+
* @param varint
389+
* A boolean indicating if the declaration is a varint
390+
* @param mappings
391+
* A mapped range of integers
392+
* @return IntegerDeclaration
393+
*
394+
* @since 4.5
395+
*/
396+
public static IntegerDeclaration createVarintDeclaration(boolean signed, int base, @Nullable String role, boolean varint, @Nullable Map<String, List<IntegerRange>> mappings) {
397+
IntegerDeclaration decl = new IntegerDeclaration(0, signed, base, null, Encoding.NONE, "", 0); //$NON-NLS-1$
398+
decl.setRole(role);
399+
decl.setVarint(varint);
400+
if (mappings != null) {
401+
decl.setMappings(mappings);
402+
}
403+
return decl;
404+
}
405+
343406
private static boolean isBigEndian(@Nullable ByteOrder byteOrder) {
344407
return (byteOrder != null) && byteOrder.equals(ByteOrder.BIG_ENDIAN);
345408
}
@@ -396,13 +459,16 @@ private IntegerDeclaration(int len, boolean signed, int base,
396459
* The role of the integer declaration
397460
*/
398461
private IntegerDeclaration(int len, boolean signed, int base,
399-
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role) {
462+
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role, @Nullable Map<String, List<IntegerRange>> mappings) {
400463
this(len, signed, base, byteOrder, encoding, clock, alignment);
401464
setRole(role);
465+
if (mappings != null) {
466+
setMappings(mappings);
467+
}
402468
}
403469

404470
private IntegerDeclaration(int len, boolean signed, @Nullable ByteOrder byteOrder) {
405-
this(len, signed, BASE_10, byteOrder, Encoding.NONE, "", BYTE_ALIGN, null); //$NON-NLS-1$
471+
this(len, signed, BASE_10, byteOrder, Encoding.NONE, "", BYTE_ALIGN, null, null); //$NON-NLS-1$
406472
}
407473

408474
// ------------------------------------------------------------------------
@@ -438,6 +504,26 @@ private void setVarint(boolean varint) {
438504
fVarint = varint;
439505
}
440506

507+
/**
508+
* Getter for mappings
509+
*
510+
* @return a mapped range of integers
511+
* @since 4.5
512+
*/
513+
public Map<String, List<IntegerRange>> getMappings() {
514+
return fMappings;
515+
}
516+
517+
/**
518+
* Setter for mappings
519+
*
520+
* @param mappings
521+
* a mapped range of integers
522+
*/
523+
private void setMappings(Map<String, List<IntegerRange>> mappings) {
524+
fMappings = mappings;
525+
}
526+
441527
/**
442528
* Get the integer base commonly decimal or hex
443529
*
@@ -526,7 +612,11 @@ public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionS
526612
input.setByteOrder(fByteOrder);
527613
long value = read(input);
528614
input.setByteOrder(byteOrder);
529-
return new IntegerDefinition(this, definitionScope, fieldName, value);
615+
if (fMappings.size() == 0) {
616+
return new IntegerDefinition(this, definitionScope, fieldName, value);
617+
}
618+
String mappingName = getMappingForValue(value);
619+
return new IntegerDefinition(this, definitionScope, fieldName, value, mappingName);
530620
}
531621

532622
@Override
@@ -687,4 +777,25 @@ private boolean isBinaryEquivalent(IntegerDeclaration other) {
687777
return !((fLength != BYTE_ALIGN) && !fByteOrder.equals(other.fByteOrder));
688778
}
689779

780+
private String getMappingForValue(long value) {
781+
String mapping = ""; //$NON-NLS-1$
782+
int count = 0;
783+
for (Map.Entry<String, List<IntegerRange>> entry : fMappings.entrySet()) {
784+
String mappingName = entry.getKey();
785+
List<IntegerRange> ranges = entry.getValue();
786+
787+
for (IntegerRange range : ranges) {
788+
if (range.getStart() <= value && value <= range.getEnd()) {
789+
if (count != 0) {
790+
mapping += " " + mappingName; //$NON-NLS-1$
791+
} else {
792+
mapping += mappingName;
793+
}
794+
count++;
795+
796+
}
797+
}
798+
}
799+
return mapping;
800+
}
690801
}

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
2+
* Copyright (c) 2011, 2024 Ericsson, Ecole Polytechnique de Montreal and others
33
*
44
* All rights reserved. This program and the accompanying materials are made
55
* available under the terms of the Eclipse Public License 2.0 which
@@ -41,6 +41,7 @@ public final class IntegerDefinition extends SimpleDatatypeDefinition {
4141
private static final int INT_BASE_8 = 8;
4242
private static final int INT_BASE_2 = 2;
4343
private final long fValue;
44+
private final String fMapping;
4445

4546
// ------------------------------------------------------------------------
4647
// Constructors
@@ -62,8 +63,30 @@ public IntegerDefinition(@NonNull IntegerDeclaration declaration,
6263
IDefinitionScope definitionScope, @NonNull String fieldName, long value) {
6364
super(declaration, definitionScope, fieldName);
6465
fValue = value;
66+
fMapping = null;
6567
}
6668

69+
/**
70+
* Constructor with mappings
71+
*
72+
* @param declaration
73+
* the parent declaration
74+
* @param definitionScope
75+
* the parent scope
76+
* @param fieldName
77+
* the field name
78+
* @param value
79+
* integer value
80+
* @param mappings
81+
* mapping value
82+
* @since 4.5
83+
*/
84+
public IntegerDefinition(@NonNull IntegerDeclaration declaration,
85+
IDefinitionScope definitionScope, @NonNull String fieldName, long value, String mappings) {
86+
super(declaration, definitionScope, fieldName);
87+
fMapping = mappings;
88+
fValue = value;
89+
}
6790
// ------------------------------------------------------------------------
6891
// Getters/Setters/Predicates
6992
// ------------------------------------------------------------------------
@@ -77,6 +100,16 @@ public long getValue() {
77100
return fValue;
78101
}
79102

103+
/**
104+
* Gets the value of the mappings
105+
*
106+
* @return A mapped range of integers
107+
* @since 4.5
108+
*/
109+
public String getMappings() {
110+
return fMapping;
111+
}
112+
80113
@Override
81114
public IntegerDeclaration getDeclaration() {
82115
return (IntegerDeclaration) super.getDeclaration();
@@ -159,10 +192,11 @@ public static String formatNumber(long value, int base, boolean signed) {
159192

160193
@Override
161194
public byte[] getBytes() {
162-
byte[] data = new byte[(int) Math.ceil(getDeclaration().getLength()/8.0)];
195+
byte[] data = new byte[(int) Math.ceil(getDeclaration().getLength() / 8.0)];
163196
ByteBuffer bb = ByteBuffer.wrap(data);
164197
bb.order(getDeclaration().getByteOrder());
165198
bb.putLong(fValue);
166199
return data;
167200
}
201+
168202
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Ericsson
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License 2.0 which
6+
* accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.tracecompass.ctf.core.event.types;
12+
13+
/**
14+
* A simple range class
15+
*
16+
* @author Vlad Arama
17+
* @since 4.5
18+
*/
19+
public class IntegerRange {
20+
private final long startRange;
21+
private final long endRange;
22+
23+
/**
24+
* Constructor
25+
*
26+
* @param start
27+
* The start of the range
28+
* @param end
29+
* The end of the range
30+
*/
31+
public IntegerRange(long start, long end) {
32+
startRange = start;
33+
endRange = end;
34+
}
35+
36+
/**
37+
* Getter for the start of the range
38+
*
39+
* @return The start of the range
40+
*/
41+
public long getStart() {
42+
if (startRange > endRange) {
43+
return endRange;
44+
}
45+
return startRange;
46+
}
47+
48+
/**
49+
* Getter for the end of the range
50+
*
51+
* @return The end of the range
52+
*/
53+
public long getEnd() {
54+
if (startRange > endRange) {
55+
return startRange;
56+
}
57+
return endRange;
58+
}
59+
}

0 commit comments

Comments
 (0)