Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 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 |
4591bed9 | 15 | * François Doray - Add generic array field type |
a3fc8213 AM |
16 | *******************************************************************************/ |
17 | ||
9722e5d7 | 18 | package org.eclipse.tracecompass.tmf.ctf.core.event; |
a3fc8213 | 19 | |
a6223d74 | 20 | import java.util.ArrayList; |
7a6cee1a | 21 | import java.util.Arrays; |
7b4f13e6 | 22 | import java.util.Collection; |
a6223d74 MK |
23 | import java.util.List; |
24 | ||
f357bcd4 AM |
25 | import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration; |
26 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
32 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
33 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
34 | import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition; | |
35 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition; | |
36 | import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDefinition; | |
37 | import org.eclipse.tracecompass.internal.ctf.core.event.types.ByteArrayDefinition; | |
2bdf0193 AM |
38 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
39 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
9722e5d7 | 40 | import org.eclipse.tracecompass.tmf.ctf.core.CtfEnumPair; |
a3fc8213 AM |
41 | |
42 | /** | |
7558a1e1 AM |
43 | * The CTF implementation of the TMF event field model |
44 | * | |
d4a8d935 | 45 | * @version 2.0 |
7558a1e1 | 46 | * @author Matthew Khouzam |
d09f973b | 47 | * @author Alexandre Montplaisir |
a3fc8213 | 48 | */ |
68b18f2f | 49 | public abstract class CtfTmfEventField extends TmfEventField { |
a3fc8213 | 50 | |
a3fc8213 | 51 | // ------------------------------------------------------------------------ |
7558a1e1 | 52 | // Constructor |
a3fc8213 AM |
53 | // ------------------------------------------------------------------------ |
54 | ||
b1baa808 | 55 | /** |
7558a1e1 AM |
56 | * Standard constructor. Only to be used internally, call parseField() to |
57 | * generate a new field object. | |
58 | * | |
59 | * @param name | |
60 | * The name of this field | |
68b18f2f AM |
61 | * @param value |
62 | * The value of this field. Its type should match the field type. | |
81ed27a8 MK |
63 | * @param fields |
64 | * The children fields. Useful for composite fields | |
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 | |
cc98c947 | 85 | * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)} |
b1baa808 | 86 | */ |
cc98c947 | 87 | @Deprecated |
a3fc8213 AM |
88 | public static CtfTmfEventField parseField(Definition fieldDef, |
89 | String fieldName) { | |
cc98c947 MK |
90 | return parseField((IDefinition) fieldDef, fieldName); |
91 | } | |
92 | ||
93 | /** | |
94 | * Factory method to instantiate CtfTmfEventField objects. | |
95 | * | |
96 | * @param fieldDef | |
97 | * The CTF Definition of this event field | |
98 | * @param fieldName | |
99 | * String The name to assign to this field | |
100 | * @return The resulting CtfTmfEventField object | |
cc98c947 MK |
101 | */ |
102 | public static CtfTmfEventField parseField(IDefinition fieldDef, | |
103 | String fieldName) { | |
a3fc8213 AM |
104 | CtfTmfEventField field = null; |
105 | ||
106 | /* Determine the Definition type */ | |
107 | if (fieldDef instanceof IntegerDefinition) { | |
367bcd2b AM |
108 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
109 | int base = intDef.getDeclaration().getBase(); | |
459f705b | 110 | field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned()); |
a3fc8213 | 111 | |
21fb02fa MK |
112 | } else if (fieldDef instanceof EnumDefinition) { |
113 | EnumDefinition enumDef = (EnumDefinition) fieldDef; | |
68b18f2f | 114 | field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue())); |
21fb02fa | 115 | |
a3fc8213 | 116 | } else if (fieldDef instanceof StringDefinition) { |
68b18f2f AM |
117 | field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue()); |
118 | ||
119 | } else if (fieldDef instanceof FloatDefinition) { | |
120 | FloatDefinition floatDef = (FloatDefinition) fieldDef; | |
121 | field = new CTFFloatField(fieldName, floatDef.getValue()); | |
a3fc8213 AM |
122 | |
123 | } else if (fieldDef instanceof ArrayDefinition) { | |
124 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
7b4f13e6 MK |
125 | IDeclaration decl = arrayDef.getDeclaration(); |
126 | if (!(decl instanceof CompoundDeclaration)) { | |
127 | throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$ | |
128 | } | |
129 | CompoundDeclaration arrDecl = (CompoundDeclaration) decl; | |
130 | IDeclaration elemType = null; | |
131 | Collection<Definition> definitions = arrayDef.getDefinitions(); | |
132 | elemType = arrDecl.getElementType(); | |
133 | if (elemType instanceof IntegerDeclaration) { | |
373af311 | 134 | /* Array of integers => CTFIntegerArrayField, unless it's a CTFStringField */ |
7b4f13e6 | 135 | IntegerDeclaration elemIntType = (IntegerDeclaration) elemType; |
373af311 MK |
136 | /* Are the integers characters and encoded? */ |
137 | if (elemIntType.isCharacter()) { | |
138 | /* it's a CTFStringField */ | |
139 | field = new CTFStringField(fieldName, arrayDef.toString()); | |
140 | } else { | |
141 | /* it's a CTFIntegerArrayField */ | |
142 | long[] values = new long[arrayDef.getLength()]; | |
143 | for (int i = 0; i < arrayDef.getLength(); i++) { | |
144 | IDefinition elem = arrayDef.getDefinitions().get(i); | |
145 | if (elem == null) { | |
146 | break; | |
147 | } | |
148 | values[i] = ((IntegerDefinition) elem).getValue(); | |
7b4f13e6 | 149 | } |
373af311 MK |
150 | field = new CTFIntegerArrayField(fieldName, values, |
151 | elemIntType.getBase(), | |
152 | elemIntType.isSigned()); | |
7b4f13e6 | 153 | } |
4591bed9 FD |
154 | } else { |
155 | /* Arrays of elements of any other type */ | |
7b4f13e6 | 156 | CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()]; |
4591bed9 | 157 | /* Parse the elements of the array. */ |
7b4f13e6 | 158 | int i = 0; |
cc98c947 | 159 | for (IDefinition definition : definitions) { |
4591bed9 | 160 | CtfTmfEventField curField = CtfTmfEventField.parseField( |
7b4f13e6 | 161 | definition, fieldName + '[' + i + ']'); |
4591bed9 | 162 | elements[i] = curField; |
7b4f13e6 | 163 | i++; |
a3fc8213 | 164 | } |
a3fc8213 | 165 | |
4591bed9 FD |
166 | field = new CTFArrayField(fieldName, elements); |
167 | } | |
7b4f13e6 MK |
168 | } else if (fieldDef instanceof ByteArrayDefinition) { |
169 | /* This is an array of ascii bytes, a.k.a. a String! */ | |
170 | field = new CTFStringField(fieldName, fieldDef.toString()); | |
367bcd2b | 171 | |
009883d7 MK |
172 | } else if (fieldDef instanceof ICompositeDefinition) { |
173 | ICompositeDefinition strDef = (ICompositeDefinition) fieldDef; | |
7a6cee1a | 174 | |
a4524c1b | 175 | List<ITmfEventField> list = new ArrayList<>(); |
7a6cee1a | 176 | /* Recursively parse the fields */ |
a4fa4e36 MK |
177 | for (String curFieldName : strDef.getFieldNames()) { |
178 | list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName)); | |
7a6cee1a GB |
179 | } |
180 | field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()])); | |
a0e9eac8 | 181 | |
404b264a GB |
182 | } else if (fieldDef instanceof VariantDefinition) { |
183 | VariantDefinition varDef = (VariantDefinition) fieldDef; | |
184 | ||
185 | String curFieldName = varDef.getCurrentFieldName(); | |
cc98c947 | 186 | IDefinition curFieldDef = varDef.getCurrentField(); |
404b264a | 187 | if (curFieldDef != null) { |
51cc7ef4 GB |
188 | CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
189 | field = new CTFVariantField(fieldName, subField); | |
404b264a GB |
190 | } else { |
191 | /* A safe-guard, but curFieldDef should never be null */ | |
192 | field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$ | |
193 | } | |
194 | ||
195 | } else { | |
459f705b JCK |
196 | /* |
197 | * Safe-guard, to avoid null exceptions later, field is expected not | |
198 | * to be null | |
199 | */ | |
91e7f946 | 200 | field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString()); |
a3fc8213 | 201 | } |
a3fc8213 AM |
202 | return field; |
203 | } | |
204 | ||
a3fc8213 | 205 | @Override |
68b18f2f | 206 | public String toString() { |
51cc7ef4 | 207 | return getName() + '=' + getFormattedValue(); |
a3fc8213 AM |
208 | } |
209 | ||
a3fc8213 AM |
210 | } |
211 | ||
212 | /** | |
7558a1e1 AM |
213 | * The CTF field implementation for integer fields. |
214 | * | |
215 | * @author alexmont | |
a3fc8213 AM |
216 | */ |
217 | final class CTFIntegerField extends CtfTmfEventField { | |
218 | ||
6fc4ce56 MK |
219 | private final int fBase; |
220 | private final boolean fSigned; | |
a3fc8213 AM |
221 | |
222 | /** | |
223 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
224 | * Java parser this is interpreted as a long. | |
7558a1e1 | 225 | * |
7558a1e1 AM |
226 | * @param name |
227 | * The name of this field | |
459f705b JCK |
228 | * @param longValue |
229 | * The integer value of this field | |
230 | * @param signed | |
231 | * Is the value signed or not | |
a3fc8213 | 232 | */ |
459f705b | 233 | CTFIntegerField(String name, long longValue, int base, boolean signed) { |
81ed27a8 | 234 | super(name, longValue, null); |
6fc4ce56 MK |
235 | fSigned = signed; |
236 | fBase = base; | |
367bcd2b AM |
237 | } |
238 | ||
a3fc8213 AM |
239 | @Override |
240 | public Long getValue() { | |
68b18f2f | 241 | return (Long) super.getValue(); |
a3fc8213 AM |
242 | } |
243 | ||
8f86c552 GB |
244 | @Override |
245 | public String getFormattedValue() { | |
6fc4ce56 | 246 | return IntegerDefinition.formatNumber(getValue(), fBase, fSigned); |
8f86c552 GB |
247 | } |
248 | ||
a3fc8213 AM |
249 | } |
250 | ||
251 | /** | |
7558a1e1 AM |
252 | * The CTF field implementation for string fields |
253 | * | |
254 | * @author alexmont | |
a3fc8213 AM |
255 | */ |
256 | final class CTFStringField extends CtfTmfEventField { | |
257 | ||
b1baa808 MK |
258 | /** |
259 | * Constructor for CTFStringField. | |
7558a1e1 AM |
260 | * |
261 | * @param strValue | |
262 | * The string value of this field | |
263 | * @param name | |
264 | * The name of this field | |
b1baa808 | 265 | */ |
68b18f2f | 266 | CTFStringField(String name, String strValue) { |
81ed27a8 | 267 | super(name, strValue, null); |
a3fc8213 AM |
268 | } |
269 | ||
a3fc8213 AM |
270 | @Override |
271 | public String getValue() { | |
68b18f2f | 272 | return (String) super.getValue(); |
a3fc8213 AM |
273 | } |
274 | } | |
275 | ||
276 | /** | |
7558a1e1 AM |
277 | * CTF field implementation for arrays of integers. |
278 | * | |
279 | * @author alexmont | |
a3fc8213 AM |
280 | */ |
281 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
282 | ||
6fc4ce56 MK |
283 | private final int fBase; |
284 | private final boolean fSigned; | |
285 | private String fFormattedValue = null; | |
404b264a | 286 | |
b1baa808 MK |
287 | /** |
288 | * Constructor for CTFIntegerArrayField. | |
7558a1e1 | 289 | * |
459f705b JCK |
290 | * @param name |
291 | * The name of this field | |
7558a1e1 AM |
292 | * @param longValues |
293 | * The array of integers (as longs) that compose this field's | |
294 | * value | |
459f705b JCK |
295 | * @param signed |
296 | * Are the values in the array signed or not | |
b1baa808 | 297 | */ |
cefe3edf | 298 | CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) { |
81ed27a8 | 299 | super(name, longValues, null); |
6fc4ce56 MK |
300 | fBase = base; |
301 | fSigned = signed; | |
a3fc8213 AM |
302 | } |
303 | ||
a6223d74 | 304 | @Override |
cefe3edf AM |
305 | public long[] getValue() { |
306 | return (long[]) super.getValue(); | |
a3fc8213 | 307 | } |
404b264a | 308 | |
8f86c552 | 309 | @Override |
4591bed9 | 310 | public synchronized String getFormattedValue() { |
6fc4ce56 | 311 | if (fFormattedValue == null) { |
a4524c1b | 312 | List<String> strings = new ArrayList<>(); |
cefe3edf | 313 | for (long value : getValue()) { |
6fc4ce56 | 314 | strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned)); |
8f86c552 | 315 | } |
6fc4ce56 | 316 | fFormattedValue = strings.toString(); |
8f86c552 | 317 | } |
6fc4ce56 | 318 | return fFormattedValue; |
8f86c552 GB |
319 | } |
320 | ||
a3fc8213 AM |
321 | } |
322 | ||
4591bed9 FD |
323 | /** |
324 | * CTF field implementation for arrays of arbitrary types. | |
325 | * | |
326 | * @author fdoray | |
327 | */ | |
328 | final class CTFArrayField extends CtfTmfEventField { | |
329 | ||
6fc4ce56 | 330 | private String fFormattedValue = null; |
4591bed9 FD |
331 | |
332 | /** | |
333 | * Constructor for CTFArrayField. | |
334 | * | |
335 | * @param name | |
336 | * The name of this field | |
337 | * @param elements | |
338 | * The array elements of this field | |
339 | */ | |
340 | CTFArrayField(String name, CtfTmfEventField[] elements) { | |
341 | super(name, elements, elements); | |
342 | } | |
343 | ||
344 | @Override | |
345 | public CtfTmfEventField[] getValue() { | |
346 | return (CtfTmfEventField[]) super.getValue(); | |
347 | } | |
348 | ||
349 | @Override | |
350 | public synchronized String getFormattedValue() { | |
6fc4ce56 | 351 | if (fFormattedValue == null) { |
a4524c1b | 352 | List<String> strings = new ArrayList<>(); |
4591bed9 FD |
353 | for (CtfTmfEventField element : getValue()) { |
354 | strings.add(element.getFormattedValue()); | |
355 | } | |
6fc4ce56 | 356 | fFormattedValue = strings.toString(); |
4591bed9 | 357 | } |
6fc4ce56 | 358 | return fFormattedValue; |
4591bed9 FD |
359 | } |
360 | } | |
361 | ||
b1baa808 | 362 | /** |
7558a1e1 AM |
363 | * CTF field implementation for floats. |
364 | * | |
365 | * @author emathko | |
b1baa808 | 366 | */ |
a04464b1 MK |
367 | final class CTFFloatField extends CtfTmfEventField { |
368 | ||
b1baa808 MK |
369 | /** |
370 | * Constructor for CTFFloatField. | |
7558a1e1 AM |
371 | * |
372 | * @param value | |
373 | * The float value (actually a double) of this field | |
374 | * @param name | |
375 | * The name of this field | |
b1baa808 | 376 | */ |
68b18f2f | 377 | protected CTFFloatField(String name, double value) { |
81ed27a8 | 378 | super(name, value, null); |
a04464b1 MK |
379 | } |
380 | ||
a04464b1 | 381 | @Override |
81c8e6f7 | 382 | public Double getValue() { |
68b18f2f | 383 | return (Double) super.getValue(); |
a04464b1 | 384 | } |
a04464b1 | 385 | } |
7558a1e1 | 386 | |
d4a8d935 BH |
387 | /** |
388 | * The CTF field implementation for Enum fields | |
389 | * | |
390 | * @author Bernd Hufmann | |
391 | */ | |
392 | final class CTFEnumField extends CtfTmfEventField { | |
393 | ||
d4a8d935 BH |
394 | /** |
395 | * Constructor for CTFEnumField. | |
396 | * | |
397 | * @param enumValue | |
459f705b JCK |
398 | * The Enum value consisting of a pair of Enum value name and its |
399 | * long value | |
d4a8d935 BH |
400 | * @param name |
401 | * The name of this field | |
402 | */ | |
68b18f2f AM |
403 | CTFEnumField(String name, CtfEnumPair enumValue) { |
404 | super(name, new CtfEnumPair(enumValue.getFirst(), | |
0126a8ca | 405 | enumValue.getSecond()), null); |
d4a8d935 BH |
406 | } |
407 | ||
d4a8d935 | 408 | @Override |
68b18f2f AM |
409 | public CtfEnumPair getValue() { |
410 | return (CtfEnumPair) super.getValue(); | |
d4a8d935 BH |
411 | } |
412 | } | |
413 | ||
7a6cee1a | 414 | /** |
51cc7ef4 | 415 | * The CTF field implementation for struct fields with sub-fields |
7a6cee1a GB |
416 | * |
417 | * @author gbastien | |
418 | */ | |
419 | final class CTFStructField extends CtfTmfEventField { | |
420 | ||
421 | /** | |
51cc7ef4 | 422 | * Constructor for CTFStructField. |
7a6cee1a | 423 | * |
51cc7ef4 GB |
424 | * @param fields |
425 | * The children of this field | |
7a6cee1a GB |
426 | * @param name |
427 | * The name of this field | |
428 | */ | |
429 | CTFStructField(String name, CtfTmfEventField[] fields) { | |
81ed27a8 | 430 | super(name, fields, fields); |
7a6cee1a GB |
431 | } |
432 | ||
7a6cee1a GB |
433 | @Override |
434 | public CtfTmfEventField[] getValue() { | |
435 | return (CtfTmfEventField[]) super.getValue(); | |
436 | } | |
437 | ||
438 | @Override | |
51cc7ef4 GB |
439 | public String getFormattedValue() { |
440 | return Arrays.toString(getValue()); | |
7a6cee1a | 441 | } |
51cc7ef4 GB |
442 | |
443 | } | |
444 | ||
445 | /** | |
446 | * The CTF field implementation for variant fields its child | |
447 | * | |
448 | * @author gbastien | |
449 | */ | |
450 | final class CTFVariantField extends CtfTmfEventField { | |
451 | ||
452 | /** | |
453 | * Constructor for CTFVariantField. | |
454 | * | |
455 | * @param field | |
456 | * The field selected for this variant | |
457 | * @param name | |
458 | * The name of this field | |
459 | */ | |
460 | CTFVariantField(String name, CtfTmfEventField field) { | |
459f705b | 461 | super(name, field, new CtfTmfEventField[] { field }); |
51cc7ef4 GB |
462 | } |
463 | ||
464 | @Override | |
465 | public CtfTmfEventField getValue() { | |
466 | return (CtfTmfEventField) super.getValue(); | |
467 | } | |
468 | ||
7a6cee1a GB |
469 | } |
470 | ||
a3fc8213 | 471 | /* Implement other possible fields types here... */ |