Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
404b264a | 2 | * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal |
a3fc8213 AM |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
d4a8d935 BH |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
404b264a | 11 | * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField |
d4a8d935 | 12 | * Bernd Hufmann - Add Enum field handling |
404b264a | 13 | * Geneviève Bastien - Add Struct and Variant field handling |
459f705b | 14 | * Jean-Christian Kouame - Correct handling of unsigned integer fields |
a3fc8213 AM |
15 | *******************************************************************************/ |
16 | ||
17 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
18 | ||
459f705b | 19 | import java.math.BigInteger; |
a6223d74 | 20 | import java.util.ArrayList; |
7a6cee1a | 21 | import java.util.Arrays; |
a6223d74 | 22 | import java.util.List; |
7a6cee1a | 23 | import java.util.Map.Entry; |
a6223d74 | 24 | |
a3fc8213 AM |
25 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
26 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
27 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
21fb02fa | 28 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
a04464b1 | 29 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
a3fc8213 AM |
30 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
31 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
32 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
33 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
7a6cee1a | 35 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
404b264a GB |
36 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; |
37 | import org.eclipse.linuxtools.internal.tmf.core.Messages; | |
7a6cee1a | 38 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
68b18f2f | 39 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
a3fc8213 AM |
40 | |
41 | /** | |
7558a1e1 AM |
42 | * The CTF implementation of the TMF event field model |
43 | * | |
d4a8d935 | 44 | * @version 2.0 |
7558a1e1 | 45 | * @author Matthew Khouzam |
d09f973b | 46 | * @author Alexandre Montplaisir |
a3fc8213 | 47 | */ |
68b18f2f | 48 | public abstract class CtfTmfEventField extends TmfEventField { |
a3fc8213 | 49 | |
a3fc8213 | 50 | // ------------------------------------------------------------------------ |
7558a1e1 | 51 | // Constructor |
a3fc8213 AM |
52 | // ------------------------------------------------------------------------ |
53 | ||
b1baa808 | 54 | /** |
7558a1e1 AM |
55 | * Standard constructor. Only to be used internally, call parseField() to |
56 | * generate a new field object. | |
57 | * | |
58 | * @param name | |
59 | * The name of this field | |
68b18f2f AM |
60 | * @param value |
61 | * The value of this field. Its type should match the field type. | |
81ed27a8 MK |
62 | * @param fields |
63 | * The children fields. Useful for composite fields | |
68b18f2f | 64 | * @since 2.0 |
b1baa808 | 65 | */ |
81ed27a8 | 66 | protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) { |
68b18f2f AM |
67 | super(/* Strip the underscore from the field name if there is one */ |
68 | name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$ | |
69 | value, | |
81ed27a8 | 70 | fields); |
a3fc8213 AM |
71 | } |
72 | ||
73 | // ------------------------------------------------------------------------ | |
74 | // Operations | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
b1baa808 | 77 | /** |
7558a1e1 AM |
78 | * Factory method to instantiate CtfTmfEventField objects. |
79 | * | |
80 | * @param fieldDef | |
81 | * The CTF Definition of this event field | |
82 | * @param fieldName | |
83 | * String The name to assign to this field | |
84 | * @return The resulting CtfTmfEventField object | |
b1baa808 | 85 | */ |
a3fc8213 AM |
86 | public static CtfTmfEventField parseField(Definition fieldDef, |
87 | String fieldName) { | |
88 | CtfTmfEventField field = null; | |
89 | ||
90 | /* Determine the Definition type */ | |
91 | if (fieldDef instanceof IntegerDefinition) { | |
367bcd2b AM |
92 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
93 | int base = intDef.getDeclaration().getBase(); | |
459f705b | 94 | field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned()); |
a3fc8213 | 95 | |
21fb02fa MK |
96 | } else if (fieldDef instanceof EnumDefinition) { |
97 | EnumDefinition enumDef = (EnumDefinition) fieldDef; | |
68b18f2f | 98 | field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue())); |
21fb02fa | 99 | |
a3fc8213 | 100 | } else if (fieldDef instanceof StringDefinition) { |
68b18f2f AM |
101 | field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue()); |
102 | ||
103 | } else if (fieldDef instanceof FloatDefinition) { | |
104 | FloatDefinition floatDef = (FloatDefinition) fieldDef; | |
105 | field = new CTFFloatField(fieldName, floatDef.getValue()); | |
a3fc8213 AM |
106 | |
107 | } else if (fieldDef instanceof ArrayDefinition) { | |
108 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
109 | ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); | |
110 | ||
111 | if (arrayDef.isString()) { | |
112 | /* This is an array of UTF-8 bytes, a.k.a. a String! */ | |
68b18f2f | 113 | field = new CTFStringField(fieldName, fieldDef.toString()); |
a3fc8213 AM |
114 | |
115 | } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { | |
116 | /* This is a an array of CTF Integers */ | |
68b18f2f | 117 | List<Long> values = new ArrayList<Long>(arrayDecl.getLength()); |
a3fc8213 | 118 | for (int i = 0; i < arrayDecl.getLength(); i++) { |
68b18f2f | 119 | values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue()); |
a3fc8213 | 120 | } |
459f705b JCK |
121 | field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase(), |
122 | ((IntegerDeclaration) arrayDecl.getElementType()).isSigned()); | |
a3fc8213 AM |
123 | } |
124 | /* Add other types of arrays here */ | |
125 | ||
126 | } else if (fieldDef instanceof SequenceDefinition) { | |
127 | SequenceDefinition seqDef = (SequenceDefinition) fieldDef; | |
128 | SequenceDeclaration seqDecl = seqDef.getDeclaration(); | |
129 | ||
130 | if (seqDef.getLength() == 0) { | |
131 | /* Some sequences have length = 0. Simply use an empty string */ | |
68b18f2f | 132 | field = new CTFStringField(fieldName, ""); //$NON-NLS-1$ |
a3fc8213 AM |
133 | } else if (seqDef.isString()) { |
134 | /* Interpret this sequence as a String */ | |
68b18f2f | 135 | field = new CTFStringField(fieldName, seqDef.toString()); |
a3fc8213 AM |
136 | } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { |
137 | /* Sequence of integers => CTFIntegerArrayField */ | |
68b18f2f | 138 | List<Long> values = new ArrayList<Long>(seqDef.getLength()); |
a3fc8213 | 139 | for (int i = 0; i < seqDef.getLength(); i++) { |
68b18f2f | 140 | values.add(((IntegerDefinition) seqDef.getElem(i)).getValue()); |
a3fc8213 | 141 | } |
459f705b JCK |
142 | field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase(), |
143 | ((IntegerDeclaration) seqDecl.getElementType()).isSigned()); | |
a3fc8213 AM |
144 | } |
145 | /* Add other Sequence types here */ | |
367bcd2b | 146 | |
7a6cee1a GB |
147 | } else if (fieldDef instanceof StructDefinition) { |
148 | StructDefinition strDef = (StructDefinition) fieldDef; | |
149 | ||
150 | String curFieldName = null; | |
151 | Definition curFieldDef; | |
152 | CtfTmfEventField curField; | |
153 | List<ITmfEventField> list = new ArrayList<ITmfEventField>(); | |
154 | /* Recursively parse the fields */ | |
155 | for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) { | |
156 | curFieldName = entry.getKey(); | |
157 | curFieldDef = entry.getValue(); | |
158 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
159 | list.add(curField); | |
160 | } | |
161 | field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()])); | |
a0e9eac8 | 162 | |
404b264a GB |
163 | } else if (fieldDef instanceof VariantDefinition) { |
164 | VariantDefinition varDef = (VariantDefinition) fieldDef; | |
165 | ||
166 | String curFieldName = varDef.getCurrentFieldName(); | |
167 | Definition curFieldDef = varDef.getDefinitions().get(curFieldName); | |
168 | if (curFieldDef != null) { | |
51cc7ef4 GB |
169 | CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
170 | field = new CTFVariantField(fieldName, subField); | |
404b264a GB |
171 | } else { |
172 | /* A safe-guard, but curFieldDef should never be null */ | |
173 | field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$ | |
174 | } | |
175 | ||
176 | } else { | |
459f705b JCK |
177 | /* |
178 | * Safe-guard, to avoid null exceptions later, field is expected not | |
179 | * to be null | |
180 | */ | |
404b264a | 181 | field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString()); |
a3fc8213 | 182 | } |
a3fc8213 AM |
183 | return field; |
184 | } | |
185 | ||
a3fc8213 | 186 | @Override |
68b18f2f | 187 | public String toString() { |
51cc7ef4 | 188 | return getName() + '=' + getFormattedValue(); |
a3fc8213 AM |
189 | } |
190 | ||
404b264a GB |
191 | /** |
192 | * Print a numeric value as a string in a given base | |
193 | * | |
194 | * @param value | |
195 | * The value to print as string | |
196 | * @param base | |
197 | * The base for this value | |
459f705b JCK |
198 | * @param signed |
199 | * Is the value signed or not | |
404b264a GB |
200 | * @return formatted number string |
201 | * @since 2.0 | |
202 | */ | |
459f705b | 203 | public final static String formatNumber(long value, int base, boolean signed) { |
404b264a | 204 | String s; |
459f705b | 205 | |
404b264a GB |
206 | /* Format the number correctly according to the integer's base */ |
207 | switch (base) { | |
208 | case 2: | |
209 | s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$ | |
210 | break; | |
211 | case 8: | |
212 | s = "0" + Long.toOctalString(value); //$NON-NLS-1$ | |
459f705b | 213 | |
404b264a GB |
214 | break; |
215 | case 16: | |
216 | s = "0x" + Long.toHexString(value); //$NON-NLS-1$ | |
217 | break; | |
459f705b | 218 | case 10: |
404b264a | 219 | default: |
459f705b JCK |
220 | /* For non-standard base, we'll just print it as a decimal number */ |
221 | if (!signed && value < 0) { | |
222 | /* Since there are no 'unsigned long', handle this case with BigInteger */ | |
223 | BigInteger bigInteger = BigInteger.valueOf(value); | |
224 | /* | |
225 | * powerOfTwo = 2^64 we add 2^64 to the negative number to get | |
226 | * the real unsigned value | |
227 | */ | |
228 | BigInteger powerOfTwo = (BigInteger.valueOf(Long.MAX_VALUE)).add(BigInteger.valueOf(1)); | |
229 | powerOfTwo = powerOfTwo.multiply(BigInteger.valueOf(2)); | |
230 | bigInteger = bigInteger.add(powerOfTwo); | |
231 | ||
232 | s = bigInteger.toString(); | |
233 | } else { | |
234 | s = Long.toString(value); | |
235 | } | |
404b264a GB |
236 | break; |
237 | } | |
238 | return s; | |
239 | } | |
240 | ||
a3fc8213 AM |
241 | } |
242 | ||
243 | /** | |
7558a1e1 AM |
244 | * The CTF field implementation for integer fields. |
245 | * | |
246 | * @author alexmont | |
a3fc8213 AM |
247 | */ |
248 | final class CTFIntegerField extends CtfTmfEventField { | |
249 | ||
367bcd2b | 250 | private final int base; |
459f705b | 251 | private final boolean signed; |
a3fc8213 AM |
252 | |
253 | /** | |
254 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
255 | * Java parser this is interpreted as a long. | |
7558a1e1 | 256 | * |
7558a1e1 AM |
257 | * @param name |
258 | * The name of this field | |
459f705b JCK |
259 | * @param longValue |
260 | * The integer value of this field | |
261 | * @param signed | |
262 | * Is the value signed or not | |
a3fc8213 | 263 | */ |
459f705b | 264 | CTFIntegerField(String name, long longValue, int base, boolean signed) { |
81ed27a8 | 265 | super(name, longValue, null); |
459f705b | 266 | this.signed = signed; |
367bcd2b AM |
267 | this.base = base; |
268 | } | |
269 | ||
a3fc8213 AM |
270 | @Override |
271 | public Long getValue() { | |
68b18f2f | 272 | return (Long) super.getValue(); |
a3fc8213 AM |
273 | } |
274 | ||
8f86c552 GB |
275 | @Override |
276 | public String getFormattedValue() { | |
459f705b | 277 | return formatNumber(getValue(), base, signed); |
8f86c552 GB |
278 | } |
279 | ||
a3fc8213 AM |
280 | } |
281 | ||
282 | /** | |
7558a1e1 AM |
283 | * The CTF field implementation for string fields |
284 | * | |
285 | * @author alexmont | |
a3fc8213 AM |
286 | */ |
287 | final class CTFStringField extends CtfTmfEventField { | |
288 | ||
b1baa808 MK |
289 | /** |
290 | * Constructor for CTFStringField. | |
7558a1e1 AM |
291 | * |
292 | * @param strValue | |
293 | * The string value of this field | |
294 | * @param name | |
295 | * The name of this field | |
b1baa808 | 296 | */ |
68b18f2f | 297 | CTFStringField(String name, String strValue) { |
81ed27a8 | 298 | super(name, strValue, null); |
a3fc8213 AM |
299 | } |
300 | ||
a3fc8213 AM |
301 | @Override |
302 | public String getValue() { | |
68b18f2f | 303 | return (String) super.getValue(); |
a3fc8213 AM |
304 | } |
305 | } | |
306 | ||
307 | /** | |
7558a1e1 AM |
308 | * CTF field implementation for arrays of integers. |
309 | * | |
310 | * @author alexmont | |
a3fc8213 AM |
311 | */ |
312 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
313 | ||
404b264a | 314 | private final int base; |
459f705b | 315 | private final boolean signed; |
8f86c552 | 316 | private String formattedValue = null; |
404b264a | 317 | |
b1baa808 MK |
318 | /** |
319 | * Constructor for CTFIntegerArrayField. | |
7558a1e1 | 320 | * |
459f705b JCK |
321 | * @param name |
322 | * The name of this field | |
7558a1e1 AM |
323 | * @param longValues |
324 | * The array of integers (as longs) that compose this field's | |
325 | * value | |
459f705b JCK |
326 | * @param signed |
327 | * Are the values in the array signed or not | |
b1baa808 | 328 | */ |
459f705b | 329 | CTFIntegerArrayField(String name, List<Long> longValues, int base, boolean signed) { |
81ed27a8 | 330 | super(name, longValues, null); |
404b264a | 331 | this.base = base; |
459f705b | 332 | this.signed = signed; |
a3fc8213 AM |
333 | } |
334 | ||
a6223d74 MK |
335 | @Override |
336 | public List<Long> getValue() { | |
68b18f2f | 337 | return (List<Long>) super.getValue(); |
a3fc8213 | 338 | } |
404b264a | 339 | |
8f86c552 GB |
340 | @Override |
341 | public String getFormattedValue() { | |
342 | if (formattedValue == null) { | |
343 | List<String> strings = new ArrayList<String>(); | |
344 | for (Long value : getValue()) { | |
459f705b | 345 | strings.add(formatNumber(value, base, signed)); |
8f86c552 GB |
346 | } |
347 | formattedValue = strings.toString(); | |
348 | } | |
349 | return formattedValue; | |
350 | } | |
351 | ||
a3fc8213 AM |
352 | } |
353 | ||
b1baa808 | 354 | /** |
7558a1e1 AM |
355 | * CTF field implementation for floats. |
356 | * | |
357 | * @author emathko | |
b1baa808 | 358 | */ |
a04464b1 MK |
359 | final class CTFFloatField extends CtfTmfEventField { |
360 | ||
b1baa808 MK |
361 | /** |
362 | * Constructor for CTFFloatField. | |
7558a1e1 AM |
363 | * |
364 | * @param value | |
365 | * The float value (actually a double) of this field | |
366 | * @param name | |
367 | * The name of this field | |
b1baa808 | 368 | */ |
68b18f2f | 369 | protected CTFFloatField(String name, double value) { |
81ed27a8 | 370 | super(name, value, null); |
a04464b1 MK |
371 | } |
372 | ||
a04464b1 | 373 | @Override |
81c8e6f7 | 374 | public Double getValue() { |
68b18f2f | 375 | return (Double) super.getValue(); |
a04464b1 | 376 | } |
a04464b1 | 377 | } |
7558a1e1 | 378 | |
d4a8d935 BH |
379 | /** |
380 | * The CTF field implementation for Enum fields | |
381 | * | |
382 | * @author Bernd Hufmann | |
383 | */ | |
384 | final class CTFEnumField extends CtfTmfEventField { | |
385 | ||
d4a8d935 BH |
386 | /** |
387 | * Constructor for CTFEnumField. | |
388 | * | |
389 | * @param enumValue | |
459f705b JCK |
390 | * The Enum value consisting of a pair of Enum value name and its |
391 | * long value | |
d4a8d935 BH |
392 | * @param name |
393 | * The name of this field | |
394 | */ | |
68b18f2f AM |
395 | CTFEnumField(String name, CtfEnumPair enumValue) { |
396 | super(name, new CtfEnumPair(enumValue.getFirst(), | |
459f705b | 397 | enumValue.getSecond().longValue()), null); |
d4a8d935 BH |
398 | } |
399 | ||
d4a8d935 | 400 | @Override |
68b18f2f AM |
401 | public CtfEnumPair getValue() { |
402 | return (CtfEnumPair) super.getValue(); | |
d4a8d935 BH |
403 | } |
404 | } | |
405 | ||
7a6cee1a | 406 | /** |
51cc7ef4 | 407 | * The CTF field implementation for struct fields with sub-fields |
7a6cee1a GB |
408 | * |
409 | * @author gbastien | |
410 | */ | |
411 | final class CTFStructField extends CtfTmfEventField { | |
412 | ||
413 | /** | |
51cc7ef4 | 414 | * Constructor for CTFStructField. |
7a6cee1a | 415 | * |
51cc7ef4 GB |
416 | * @param fields |
417 | * The children of this field | |
7a6cee1a GB |
418 | * @param name |
419 | * The name of this field | |
420 | */ | |
421 | CTFStructField(String name, CtfTmfEventField[] fields) { | |
81ed27a8 | 422 | super(name, fields, fields); |
7a6cee1a GB |
423 | } |
424 | ||
7a6cee1a GB |
425 | @Override |
426 | public CtfTmfEventField[] getValue() { | |
427 | return (CtfTmfEventField[]) super.getValue(); | |
428 | } | |
429 | ||
430 | @Override | |
51cc7ef4 GB |
431 | public String getFormattedValue() { |
432 | return Arrays.toString(getValue()); | |
7a6cee1a | 433 | } |
51cc7ef4 GB |
434 | |
435 | } | |
436 | ||
437 | /** | |
438 | * The CTF field implementation for variant fields its child | |
439 | * | |
440 | * @author gbastien | |
441 | */ | |
442 | final class CTFVariantField extends CtfTmfEventField { | |
443 | ||
444 | /** | |
445 | * Constructor for CTFVariantField. | |
446 | * | |
447 | * @param field | |
448 | * The field selected for this variant | |
449 | * @param name | |
450 | * The name of this field | |
451 | */ | |
452 | CTFVariantField(String name, CtfTmfEventField field) { | |
459f705b | 453 | super(name, field, new CtfTmfEventField[] { field }); |
51cc7ef4 GB |
454 | } |
455 | ||
456 | @Override | |
457 | public CtfTmfEventField getValue() { | |
458 | return (CtfTmfEventField) super.getValue(); | |
459 | } | |
460 | ||
7a6cee1a GB |
461 | } |
462 | ||
a3fc8213 | 463 | /* Implement other possible fields types here... */ |