Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
* Copyright (c) 2011, 2024 Ericsson, Ecole Polytechnique de Montreal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
Expand All @@ -21,6 +21,10 @@
import java.math.BigInteger;
import java.nio.ByteOrder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.CTFException;
Expand Down Expand Up @@ -129,6 +133,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
private final long fAlignment;
private final String fClock;
private boolean fVarint = false;
private Map<String, List<IntegerRange>> fMappings = new HashMap<>();

// ------------------------------------------------------------------------
// Constructors
Expand Down Expand Up @@ -315,7 +320,38 @@ public static IntegerDeclaration createDeclaration(int len, boolean signed, int
}
}
}
return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role);
return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role, null);
}

/**
* Alternate create method for CTF2 integers which have roles and mappings
*
* @param len
* The length in bits
* @param signed
* Is the integer signed? false == unsigned
* @param base
* The base (10-16 are most common)
* @param byteOrder
* Big-endian little-endian or other
* @param encoding
* ascii, utf8 or none.
* @param clock
* The clock path, can be null
* @param alignment
* The minimum alignment. Should be >= 1
* @param role
* The role of the declaration
* @param mappings
* A mapped range of integers
* @return The integer declaration
* @since 4.5
*/
public static IntegerDeclaration createDeclaration(int len, boolean signed, int base,
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role, Map<String, List<IntegerRange>> mappings) {
IntegerDeclaration decl = createDeclaration(len, signed, base, byteOrder, encoding, clock, alignment, role);
decl.setMappings(mappings);
return decl;
}

/**
Expand All @@ -340,6 +376,33 @@ public static IntegerDeclaration createVarintDeclaration(boolean signed, int bas
return decl;
}

/**
* Create method for CTF2 varints with mappings
*
* @param signed
* Is the integer signed? false == unsigned
* @param base
* The base (10-16 are most common)
* @param role
* The role of the integer declaration
* @param varint
* A boolean indicating if the declaration is a varint
* @param mappings
* A mapped range of integers
* @return IntegerDeclaration
*
* @since 4.5
*/
public static IntegerDeclaration createVarintDeclaration(boolean signed, int base, @Nullable String role, boolean varint, @Nullable Map<String, List<IntegerRange>> mappings) {
IntegerDeclaration decl = new IntegerDeclaration(0, signed, base, null, Encoding.NONE, "", 0); //$NON-NLS-1$
decl.setRole(role);
decl.setVarint(varint);
if (mappings != null) {
decl.setMappings(mappings);
}
return decl;
}

private static boolean isBigEndian(@Nullable ByteOrder byteOrder) {
return (byteOrder != null) && byteOrder.equals(ByteOrder.BIG_ENDIAN);
}
Expand Down Expand Up @@ -396,13 +459,16 @@ private IntegerDeclaration(int len, boolean signed, int base,
* The role of the integer declaration
*/
private IntegerDeclaration(int len, boolean signed, int base,
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role) {
@Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment, @Nullable String role, @Nullable Map<String, List<IntegerRange>> mappings) {
this(len, signed, base, byteOrder, encoding, clock, alignment);
setRole(role);
if (mappings != null) {
setMappings(mappings);
}
}

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

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -438,6 +504,26 @@ private void setVarint(boolean varint) {
fVarint = varint;
}

/**
* Getter for mappings
*
* @return a mapped range of integers
* @since 4.5
*/
public Map<String, List<IntegerRange>> getMappings() {
return fMappings;
}

/**
* Setter for mappings
*
* @param mappings
* a mapped range of integers
*/
private void setMappings(Map<String, List<IntegerRange>> mappings) {
fMappings = mappings;
}

/**
* Get the integer base commonly decimal or hex
*
Expand Down Expand Up @@ -526,7 +612,11 @@ public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionS
input.setByteOrder(fByteOrder);
long value = read(input);
input.setByteOrder(byteOrder);
return new IntegerDefinition(this, definitionScope, fieldName, value);
if (fMappings.size() == 0) {
return new IntegerDefinition(this, definitionScope, fieldName, value);
}
String mappingName = getMappingForValue(value);
return new IntegerDefinition(this, definitionScope, fieldName, value, mappingName);
}

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

private String getMappingForValue(long value) {
String mapping = ""; //$NON-NLS-1$
int count = 0;
for (Map.Entry<String, List<IntegerRange>> entry : fMappings.entrySet()) {
String mappingName = entry.getKey();
List<IntegerRange> ranges = entry.getValue();

for (IntegerRange range : ranges) {
if (range.getStart() <= value && value <= range.getEnd()) {
if (count != 0) {
mapping += " " + mappingName; //$NON-NLS-1$
} else {
mapping += mappingName;
}
count++;

}
}
}
return mapping;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
* Copyright (c) 2011, 2024 Ericsson, Ecole Polytechnique de Montreal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
Expand Down Expand Up @@ -41,6 +41,7 @@ public final class IntegerDefinition extends SimpleDatatypeDefinition {
private static final int INT_BASE_8 = 8;
private static final int INT_BASE_2 = 2;
private final long fValue;
private final String fMapping;

// ------------------------------------------------------------------------
// Constructors
Expand All @@ -62,8 +63,30 @@ public IntegerDefinition(@NonNull IntegerDeclaration declaration,
IDefinitionScope definitionScope, @NonNull String fieldName, long value) {
super(declaration, definitionScope, fieldName);
fValue = value;
fMapping = null;
}

/**
* Constructor with mappings
*
* @param declaration
* the parent declaration
* @param definitionScope
* the parent scope
* @param fieldName
* the field name
* @param value
* integer value
* @param mappings
* mapping value
* @since 4.5
*/
public IntegerDefinition(@NonNull IntegerDeclaration declaration,
IDefinitionScope definitionScope, @NonNull String fieldName, long value, String mappings) {
super(declaration, definitionScope, fieldName);
fMapping = mappings;
fValue = value;
}
// ------------------------------------------------------------------------
// Getters/Setters/Predicates
// ------------------------------------------------------------------------
Expand All @@ -77,6 +100,16 @@ public long getValue() {
return fValue;
}

/**
* Gets the value of the mappings
*
* @return A mapped range of integers
* @since 4.5
*/
public String getMappings() {
return fMapping;
}

@Override
public IntegerDeclaration getDeclaration() {
return (IntegerDeclaration) super.getDeclaration();
Expand Down Expand Up @@ -159,10 +192,11 @@ public static String formatNumber(long value, int base, boolean signed) {

@Override
public byte[] getBytes() {
byte[] data = new byte[(int) Math.ceil(getDeclaration().getLength()/8.0)];
byte[] data = new byte[(int) Math.ceil(getDeclaration().getLength() / 8.0)];
ByteBuffer bb = ByteBuffer.wrap(data);
bb.order(getDeclaration().getByteOrder());
bb.putLong(fValue);
return data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2024 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.ctf.core.event.types;

/**
* A simple range class
*
* @author Vlad Arama
* @since 4.5
*/
public class IntegerRange {
private final long startRange;
private final long endRange;

/**
* Constructor
*
* @param start
* The start of the range
* @param end
* The end of the range
*/
public IntegerRange(long start, long end) {
startRange = start;
endRange = end;
}

/**
* Getter for the start of the range
*
* @return The start of the range
*/
public long getStart() {
if (startRange > endRange) {
return endRange;
}
return startRange;
}

/**
* Getter for the end of the range
*
* @return The end of the range
*/
public long getEnd() {
if (startRange > endRange) {
return startRange;
}
return endRange;
}
}
Loading