/
CtfTmfEventField.java
537 lines (472 loc) · 17.8 KB
/
CtfTmfEventField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
* Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
*
* 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
*
* Contributors:
* Matthew Khouzam - Initial API and implementation
* Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
* Bernd Hufmann - Add Enum field handling
* Geneviève Bastien - Add Struct and Variant field handling
* Jean-Christian Kouame - Correct handling of unsigned integer fields
* François Doray - Add generic array field type
*******************************************************************************/
package org.eclipse.tracecompass.tmf.ctf.core.event;
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ByteArrayDefinition;
import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
import org.eclipse.tracecompass.tmf.ctf.core.CtfEnumPair;
/**
* The CTF implementation of the TMF event field model
*
* @version 2.0
* @author Matthew Khouzam
* @author Alexandre Montplaisir
*/
public abstract class CtfTmfEventField extends TmfEventField {
/**
* Value that can be used in the {@link #getField(String...)} for variants.
* Using this field value means that the selected field will be returned
* whatever the selected choice for the event
*
* @since 2.1
*/
public static final @NonNull String FIELD_VARIANT_SELECTED = "Any"; //$NON-NLS-1$
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
/**
* Standard constructor. Only to be used internally, call parseField() to
* generate a new field object.
*
* @param name
* The name of this field
* @param value
* The value of this field. Its type should match the field type.
* @param fields
* The children fields. Useful for composite fields
*/
protected CtfTmfEventField(@NonNull String name, Object value, ITmfEventField[] fields) {
super(/* Strip the underscore from the field name if there is one */
name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
value,
fields);
}
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
/**
* Factory method to instantiate CtfTmfEventField objects.
*
* @param fieldDef
* The CTF Definition of this event field
* @param fieldName
* String The name to assign to this field
* @return The resulting CtfTmfEventField object
*/
public static @NonNull CtfTmfEventField parseField(IDefinition fieldDef,
@NonNull String fieldName) {
CtfTmfEventField field = null;
/* Determine the Definition type */
if (fieldDef instanceof IntegerDefinition) {
IntegerDefinition intDef = (IntegerDefinition) fieldDef;
int base = intDef.getDeclaration().getBase();
field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
} else if (fieldDef instanceof EnumDefinition) {
EnumDefinition enumDef = (EnumDefinition) fieldDef;
field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()));
} else if (fieldDef instanceof StringDefinition) {
field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue());
} else if (fieldDef instanceof FloatDefinition) {
FloatDefinition floatDef = (FloatDefinition) fieldDef;
field = new CTFFloatField(fieldName, floatDef.getValue());
} else if (fieldDef instanceof AbstractArrayDefinition) {
AbstractArrayDefinition arrayDef = (AbstractArrayDefinition) fieldDef;
IDeclaration decl = arrayDef.getDeclaration();
if (!(decl instanceof CompoundDeclaration)) {
throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$
}
CompoundDeclaration arrDecl = (CompoundDeclaration) decl;
IDeclaration elemType = null;
elemType = arrDecl.getElementType();
if (elemType instanceof IntegerDeclaration) {
/*
* Array of integers => CTFIntegerArrayField, unless it's a
* CTFStringField
*/
IntegerDeclaration elemIntType = (IntegerDeclaration) elemType;
/* Are the integers characters and encoded? */
if (elemIntType.isCharacter()) {
/* it's a CTFStringField */
field = new CTFStringField(fieldName, arrayDef.toString());
} else if (arrayDef instanceof ByteArrayDefinition) { // unsigned byte array
ByteArrayDefinition byteArrayDefinition = (ByteArrayDefinition) arrayDef;
/* it's a CTFIntegerArrayField */
int size = arrayDef.getLength();
long[] values = new long[size];
for (int i = 0; i < size; i++) {
values[i] = Byte.toUnsignedLong(byteArrayDefinition.getByte(i));
}
field = new CTFIntegerArrayField(fieldName, values,
elemIntType.getBase(),
elemIntType.isSigned());
} else {
/* it's a CTFIntegerArrayField */
int size = arrayDef.getLength();
long[] values = new long[size];
for (int i = 0; i < size; i++) {
IDefinition elem = arrayDef.getDefinitions().get(i);
if (elem == null) {
break;
}
values[i] = ((IntegerDefinition) elem).getValue();
}
field = new CTFIntegerArrayField(fieldName, values,
elemIntType.getBase(),
elemIntType.isSigned());
}
} else {
/* Arrays of elements of any other type */
CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()];
/* Parse the elements of the array. */
int i = 0;
List<Definition> definitions = arrayDef.getDefinitions();
for (IDefinition definition : definitions) {
CtfTmfEventField curField = CtfTmfEventField.parseField(
definition, fieldName + '[' + i + ']');
elements[i] = curField;
i++;
}
field = new CTFArrayField(fieldName, elements);
}
} else if (fieldDef instanceof ICompositeDefinition) {
ICompositeDefinition strDef = (ICompositeDefinition) fieldDef;
List<ITmfEventField> list = new ArrayList<>();
/* Recursively parse the fields */
for (String fn : strDef.getFieldNames()) {
list.add(CtfTmfEventField.parseField(strDef.getDefinition(fn), fn));
}
field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
} else if (fieldDef instanceof VariantDefinition) {
VariantDefinition varDef = (VariantDefinition) fieldDef;
String curFieldName = checkNotNull(varDef.getCurrentFieldName());
IDefinition curFieldDef = varDef.getCurrentField();
if (curFieldDef != null) {
CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
field = new CTFVariantField(fieldName, subField);
} else {
/* A safe-guard, but curFieldDef should never be null */
field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
}
} else {
/*
* Safe-guard, to avoid null exceptions later, field is expected not
* to be null
*/
field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString());
}
return field;
}
@Override
public String toString() {
return getName() + '=' + getFormattedValue();
}
}
/**
* The CTF field implementation for integer fields.
*
* @author alexmont
*/
final class CTFIntegerField extends CtfTmfEventField {
private final int fBase;
private final boolean fSigned;
/**
* A CTF "IntegerDefinition" can be an integer of any byte size, so in the
* Java parser this is interpreted as a long.
*
* @param name
* The name of this field
* @param longValue
* The integer value of this field
* @param signed
* Is the value signed or not
*/
CTFIntegerField(@NonNull String name, long longValue, int base, boolean signed) {
super(name, Long.valueOf(longValue), null);
fSigned = signed;
fBase = base;
}
@Override
public Long getValue() {
return (Long) super.getValue();
}
@Override
public String getFormattedValue() {
return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
}
}
/**
* The CTF field implementation for string fields
*
* @author alexmont
*/
final class CTFStringField extends CtfTmfEventField {
/**
* Constructor for CTFStringField.
*
* @param strValue
* The string value of this field
* @param name
* The name of this field
*/
CTFStringField(@NonNull String name, String strValue) {
super(name, strValue, null);
}
@Override
public String getValue() {
return (String) super.getValue();
}
@Override
public String getFormattedValue() {
return "\""+getValue()+"\""; //$NON-NLS-1$//$NON-NLS-2$
}
}
/**
* CTF field implementation for arrays of integers.
*
* @author alexmont
*/
final class CTFIntegerArrayField extends CtfTmfEventField {
private final int fBase;
private final boolean fSigned;
private String fFormattedValue = null;
/**
* Constructor for CTFIntegerArrayField.
*
* @param name
* The name of this field
* @param longValues
* The array of integers (as longs) that compose this field's
* value
* @param signed
* Are the values in the array signed or not
*/
CTFIntegerArrayField(@NonNull String name, long[] longValues, int base, boolean signed) {
super(name, longValues, null);
fBase = base;
fSigned = signed;
}
@Override
public long[] getValue() {
return (long[]) super.getValue();
}
@Override
public synchronized String getFormattedValue() {
if (fFormattedValue == null) {
List<String> strings = new ArrayList<>();
for (long value : getValue()) {
strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
}
fFormattedValue = strings.toString();
}
return fFormattedValue;
}
}
/**
* CTF field implementation for arrays of arbitrary types.
*
* @author fdoray
*/
final class CTFArrayField extends CtfTmfEventField {
private String fFormattedValue = null;
/**
* Constructor for CTFArrayField.
*
* @param name
* The name of this field
* @param elements
* The array elements of this field
*/
CTFArrayField(@NonNull String name, CtfTmfEventField[] elements) {
super(name, elements, elements);
}
@Override
public CtfTmfEventField[] getValue() {
return (CtfTmfEventField[]) super.getValue();
}
@Override
public synchronized String getFormattedValue() {
if (fFormattedValue == null) {
List<String> strings = new ArrayList<>();
for (CtfTmfEventField element : getValue()) {
strings.add(element.getFormattedValue());
}
fFormattedValue = strings.toString();
}
return fFormattedValue;
}
}
/**
* CTF field implementation for floats.
*
* @author emathko
*/
final class CTFFloatField extends CtfTmfEventField {
/**
* Constructor for CTFFloatField.
*
* @param value
* The float value (actually a double) of this field
* @param name
* The name of this field
*/
protected CTFFloatField(@NonNull String name, double value) {
super(name, value, null);
}
@Override
public Double getValue() {
return (Double) super.getValue();
}
}
/**
* The CTF field implementation for Enum fields
*
* @author Bernd Hufmann
*/
final class CTFEnumField extends CtfTmfEventField {
/**
* Constructor for CTFEnumField.
*
* @param enumValue
* The Enum value consisting of a pair of Enum value name and its
* long value
* @param name
* The name of this field
*/
CTFEnumField(@NonNull String name, CtfEnumPair enumValue) {
super(name, new CtfEnumPair(enumValue.getFirst(),
enumValue.getSecond()), null);
}
@Override
public <T> @Nullable T getFieldValue(Class<T> type) {
CtfEnumPair value = getValue();
if (type.isAssignableFrom(CtfEnumPair.class)) {
@SuppressWarnings("unchecked")
T ret = (T) value;
return ret;
}
// If the type is a known number type, use the long value
if (type.equals(Long.class)) {
@SuppressWarnings("unchecked")
T ret = (T) value.getLongValue();
return ret;
}
if (type.equals(Integer.class)) {
@SuppressWarnings("unchecked")
T ret = (T) ((Integer) value.getLongValue().intValue());
return ret;
}
if (type.equals(Double.class)) {
@SuppressWarnings("unchecked")
T ret = (T) ((Double) value.getLongValue().doubleValue());
return ret;
}
// If type is string, use the string value
if (type.equals(String.class)) {
@SuppressWarnings("unchecked")
T ret = (T) value.getStringValue();
return ret;
}
// Don't know what to do, return null
return null;
}
@Override
public CtfEnumPair getValue() {
return (CtfEnumPair) super.getValue();
}
}
/**
* The CTF field implementation for struct fields with sub-fields
*
* @author gbastien
*/
final class CTFStructField extends CtfTmfEventField {
/**
* Constructor for CTFStructField.
*
* @param fields
* The children of this field
* @param name
* The name of this field
*/
CTFStructField(@NonNull String name, CtfTmfEventField[] fields) {
super(name, fields, fields);
}
@Override
public CtfTmfEventField[] getValue() {
return (CtfTmfEventField[]) super.getValue();
}
@Override
public String getFormattedValue() {
return Arrays.toString(getValue());
}
}
/**
* The CTF field implementation for variant fields its child
*
* @author gbastien
*/
final class CTFVariantField extends CtfTmfEventField {
/**
* Constructor for CTFVariantField.
*
* @param field
* The field selected for this variant
* @param name
* The name of this field
*/
CTFVariantField(@NonNull String name, CtfTmfEventField field) {
super(name, field, new CtfTmfEventField[] { field });
}
@Override
public CtfTmfEventField getValue() {
return (CtfTmfEventField) super.getValue();
}
@Override
public ITmfEventField getField(final String... path) {
/*
* We use the == to make sure that this constant was used, otherwise, it could
* conflict with a field with the same name
*/
if (path.length == 1 && path[0] == FIELD_VARIANT_SELECTED) {
Iterator<ITmfEventField> it = getFields().iterator();
return it.hasNext() ? it.next() : null;
}
return super.getField(path);
}
}
/* Implement other possible fields types here... */