summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorGeneviève Bastien2013-03-27 14:22:10 (EDT)
committer Alexandre Montplaisir2013-04-12 11:49:47 (EDT)
commitd01ca0ae8db629dd970a8c416a9c51a83f285509 (patch)
treec4234b518e87150ffc80947e1af9c4ae08183555
parentca21eab3d4d34ddf6d8a5f3708b11be16c8ae99c (diff)
downloadorg.eclipse.linuxtools-d01ca0ae8db629dd970a8c416a9c51a83f285509.zip
org.eclipse.linuxtools-d01ca0ae8db629dd970a8c416a9c51a83f285509.tar.gz
org.eclipse.linuxtools-d01ca0ae8db629dd970a8c416a9c51a83f285509.tar.bz2
Ctf: Correct endianness when reading ctf integersrefs/changes/28/11528/5
Endianness was not correctly taken into account when reading integer fields in different endianness. Also add the tests for endianness reading. Change-Id: I77d72b75116f8fabcbbdf962afa6ad2903c39c26 Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net> Reviewed-on: https://git.eclipse.org/r/11528 Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im> Tested-by: Hudson CI IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im> Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com> IP-Clean: Matthew Khouzam <matthew.khouzam@ericsson.com> Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java93
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/TestAll.java1
-rw-r--r--lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java30
3 files changed, 116 insertions, 8 deletions
diff --git a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java
new file mode 100644
index 0000000..6f2175b
--- /dev/null
+++ b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java
@@ -0,0 +1,93 @@
+/*******************************************************************************
+ * Copyright (c) 2013 École Polytechnique de Montréal, Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Geneviève Bastien - Initial API and implementation
+ * Alexandre Montplaisir - Split out in separate class
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.tests.types;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Endianness test for {@link IntegerDefinition}.
+ *
+ * @author Geneviève Bastien
+ */
+public class IntegerEndiannessTest {
+
+ private static String name = "testInt";
+ private static String clockName = "clock";
+
+ private ByteBuffer bb;
+ private BitBuffer input;
+
+ /**
+ * Set up the bit-buffer to be used
+ */
+ @Before
+ public void setUp() {
+ bb = java.nio.ByteBuffer.allocateDirect(8);
+ bb.put((byte) 0xab);
+ bb.put((byte) 0xcd);
+ bb.put((byte) 0xef);
+ bb.put((byte) 0x12);
+ bb.put((byte) 0x34);
+ bb.put((byte) 0x56);
+ bb.put((byte) 0x78);
+ bb.put((byte) 0x9a);
+ input = new BitBuffer(bb);
+ }
+
+ /** Read 32-bits BE */
+ @Test
+ public void test32BE() {
+ IntegerDeclaration be = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
+ IntegerDefinition fixture_be = be.createDefinition(null, name);
+ fixture_be.read(input);
+ assertEquals(0xabcdef12, fixture_be.getValue());
+ }
+
+ /** Read 64-bits BE */
+ @Test
+ public void test64BE() {
+ IntegerDeclaration be = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
+ IntegerDefinition fixture_be = be.createDefinition(null, name);
+ fixture_be.read(input);
+ assertEquals(0xabcdef123456789aL, fixture_be.getValue());
+ }
+
+ /** Read 32-bits LE */
+ @Test
+ public void test32LE() {
+ IntegerDeclaration le = new IntegerDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
+ IntegerDefinition fixture_le = le.createDefinition(null, name);
+ fixture_le.read(input);
+ assertEquals(0x12efcdab, fixture_le.getValue());
+ }
+
+ /** Read 64-bits LE */
+ @Test
+ public void test64LE() {
+ IntegerDeclaration le = new IntegerDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
+ IntegerDefinition fixture_le = le.createDefinition(null, name);
+ fixture_le.read(input);
+ assertEquals(0x9a78563412efcdabL, fixture_le.getValue());
+ }
+}
diff --git a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/TestAll.java b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/TestAll.java
index 5f10a52..87b306b 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/TestAll.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/TestAll.java
@@ -34,6 +34,7 @@ import org.junit.runners.Suite;
FloatDefinitionTest.class,
IntegerDeclarationTest.class,
IntegerDefinitionTest.class,
+ IntegerEndiannessTest.class,
SequenceDeclarationTest.class,
SequenceDefinitionTest.class,
StringDeclarationTest.class,
diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
index 6bf0cec..1a420e6 100644
--- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
+++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
@@ -22,9 +22,6 @@ import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
* The definition of a integer basic data type. It will take the data
* from a trace and store it (and make it fit) as a long.
*
- * TODO: Reading integers with an endianness different from the trace endianness
- * is not supported
- *
* @version 1.0
* @author Matthew Khouzam
* @author Simon Marchi
@@ -105,14 +102,24 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
public void read(BitBuffer input) {
final long longNegBit = 0x0000000080000000L;
int align = (int) declaration.getAlignment();
- int pos = input.position() + ((align-(input.position() % align))%align);
+ int pos = input.position() + ((align - (input.position() % align)) % align);
input.position(pos);
boolean signed = declaration.isSigned();
int length = declaration.getLength();
long bits = 0;
- // TODO: use the eventual getLong from BitBuffer
+ /*
+ * Is the endianness of this field the same as the endianness of the
+ * input buffer? If not, then temporarily set the buffer's endianness to
+ * this field's just to read the data
+ */
+ ByteOrder byteOrder = input.getByteOrder();
+ if ((this.declaration.getByteOrder() != null) &&
+ (this.declaration.getByteOrder() != input.getByteOrder())) {
+ input.setByteOrder(this.declaration.getByteOrder());
+ }
+ // TODO: use the eventual getLong from BitBuffer
if (length == 64) {
long low = input.getInt(32, false);
low = low & 0x00000000FFFFFFFFL;
@@ -127,13 +134,20 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
bits = input.getInt(length, signed);
bits = bits & 0x00000000FFFFFFFFL;
/*
- * The previous line loses sign information but is necessary, this fixes the sign
- * for 32 bit numbers. Sorry, in java all 64 bit ints are signed.
+ * The previous line loses sign information but is necessary, this
+ * fixes the sign for 32 bit numbers. Sorry, in java all 64 bit ints
+ * are signed.
*/
- if( (longNegBit == (bits & longNegBit)) && signed) {
+ if ((longNegBit == (bits & longNegBit)) && signed) {
bits |= 0xffffffff00000000L;
}
}
+ /*
+ * Put the input buffer's endianness back to original if it was changed
+ */
+ if (byteOrder != input.getByteOrder()) {
+ input.setByteOrder(byteOrder);
+ }
value = bits;
}