lib/ctf-ir/field-types.c: add verbose logging (modifying functions)
[babeltrace.git] / lib / ctf-ir / field-types.c
1 /*
2 * field-types.c
3 *
4 * Babeltrace CTF IR - Event Types
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "FIELD-TYPES"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/ctf-ir/field-types-internal.h>
33 #include <babeltrace/ctf-ir/field-path-internal.h>
34 #include <babeltrace/ctf-ir/utils.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/ctf-ir/clock-class.h>
37 #include <babeltrace/ctf-ir/clock-class-internal.h>
38 #include <babeltrace/ctf-writer/writer-internal.h>
39 #include <babeltrace/object-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/endian-internal.h>
43 #include <float.h>
44 #include <inttypes.h>
45 #include <stdlib.h>
46
47 struct range_overlap_query {
48 union {
49 uint64_t _unsigned;
50 int64_t _signed;
51 } range_start;
52
53 union {
54 uint64_t _unsigned;
55 int64_t _signed;
56 } range_end;
57 int overlaps;
58 GQuark mapping_name;
59 };
60
61 static
62 void bt_ctf_field_type_destroy(struct bt_object *);
63 static
64 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *);
65 static
66 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *);
67 static
68 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *);
69 static
70 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *);
71 static
72 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *);
73 static
74 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *);
75 static
76 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *);
77 static
78 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *);
79
80 static
81 void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = {
82 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_destroy,
83 [BT_CTF_FIELD_TYPE_ID_ENUM] =
84 bt_ctf_field_type_enumeration_destroy,
85 [BT_CTF_FIELD_TYPE_ID_FLOAT] =
86 bt_ctf_field_type_floating_point_destroy,
87 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_destroy,
88 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_destroy,
89 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_destroy,
90 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
91 [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_destroy,
92 };
93
94 static
95 void generic_field_type_freeze(struct bt_ctf_field_type *);
96 static
97 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *);
98 static
99 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *);
100 static
101 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *);
102 static
103 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *);
104 static
105 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *);
106 static
107 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *);
108
109 static
110 type_freeze_func const type_freeze_funcs[] = {
111 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_freeze,
112 [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_freeze,
113 [BT_CTF_FIELD_TYPE_ID_FLOAT] = generic_field_type_freeze,
114 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_freeze,
115 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_freeze,
116 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_freeze,
117 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
118 [BT_CTF_FIELD_TYPE_ID_STRING] = generic_field_type_freeze,
119 };
120
121 static
122 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *,
123 struct metadata_context *);
124 static
125 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *,
126 struct metadata_context *);
127 static
128 int bt_ctf_field_type_floating_point_serialize(
129 struct bt_ctf_field_type *, struct metadata_context *);
130 static
131 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *,
132 struct metadata_context *);
133 static
134 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *,
135 struct metadata_context *);
136 static
137 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *,
138 struct metadata_context *);
139 static
140 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *,
141 struct metadata_context *);
142 static
143 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *,
144 struct metadata_context *);
145
146 static
147 type_serialize_func const type_serialize_funcs[] = {
148 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_serialize,
149 [BT_CTF_FIELD_TYPE_ID_ENUM] =
150 bt_ctf_field_type_enumeration_serialize,
151 [BT_CTF_FIELD_TYPE_ID_FLOAT] =
152 bt_ctf_field_type_floating_point_serialize,
153 [BT_CTF_FIELD_TYPE_ID_STRUCT] =
154 bt_ctf_field_type_structure_serialize,
155 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_serialize,
156 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_serialize,
157 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
158 [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_serialize,
159 };
160
161 static
162 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *,
163 enum bt_ctf_byte_order byte_order);
164 static
165 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *,
166 enum bt_ctf_byte_order byte_order);
167 static
168 void bt_ctf_field_type_floating_point_set_byte_order(
169 struct bt_ctf_field_type *, enum bt_ctf_byte_order byte_order);
170 static
171 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *,
172 enum bt_ctf_byte_order byte_order);
173 static
174 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *,
175 enum bt_ctf_byte_order byte_order);
176 static
177 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *,
178 enum bt_ctf_byte_order byte_order);
179 static
180 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *,
181 enum bt_ctf_byte_order byte_order);
182
183 static
184 void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
185 enum bt_ctf_byte_order) = {
186 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_set_byte_order,
187 [BT_CTF_FIELD_TYPE_ID_ENUM] =
188 bt_ctf_field_type_enumeration_set_byte_order,
189 [BT_CTF_FIELD_TYPE_ID_FLOAT] =
190 bt_ctf_field_type_floating_point_set_byte_order,
191 [BT_CTF_FIELD_TYPE_ID_STRUCT] =
192 bt_ctf_field_type_structure_set_byte_order,
193 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_set_byte_order,
194 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_set_byte_order,
195 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order,
196 [BT_CTF_FIELD_TYPE_ID_STRING] = NULL,
197 };
198
199 static
200 struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
201 struct bt_ctf_field_type *);
202 static
203 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
204 struct bt_ctf_field_type *);
205 static
206 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
207 struct bt_ctf_field_type *);
208 static
209 struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
210 struct bt_ctf_field_type *);
211 static
212 struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
213 struct bt_ctf_field_type *);
214 static
215 struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
216 struct bt_ctf_field_type *);
217 static
218 struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
219 struct bt_ctf_field_type *);
220 static
221 struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
222 struct bt_ctf_field_type *);
223
224 static
225 struct bt_ctf_field_type *(* const type_copy_funcs[])(
226 struct bt_ctf_field_type *) = {
227 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_copy,
228 [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_copy,
229 [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_copy,
230 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_copy,
231 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_copy,
232 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_copy,
233 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_copy,
234 [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_copy,
235 };
236
237 static
238 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *,
239 struct bt_ctf_field_type *);
240 static
241 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *,
242 struct bt_ctf_field_type *);
243 static
244 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *,
245 struct bt_ctf_field_type *);
246 static
247 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *,
248 struct bt_ctf_field_type *);
249 static
250 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *,
251 struct bt_ctf_field_type *);
252 static
253 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *,
254 struct bt_ctf_field_type *);
255 static
256 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *,
257 struct bt_ctf_field_type *);
258 static
259 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *,
260 struct bt_ctf_field_type *);
261
262 static
263 int (* const type_compare_funcs[])(struct bt_ctf_field_type *,
264 struct bt_ctf_field_type *) = {
265 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_compare,
266 [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_compare,
267 [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_compare,
268 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_compare,
269 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_compare,
270 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_compare,
271 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_compare,
272 [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_type_string_compare,
273 };
274
275 static
276 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *);
277 static
278 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *);
279 static
280 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *);
281 static
282 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *);
283 static
284 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *);
285 static
286 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *);
287
288 static
289 int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = {
290 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_validate,
291 [BT_CTF_FIELD_TYPE_ID_FLOAT] = NULL,
292 [BT_CTF_FIELD_TYPE_ID_STRING] = NULL,
293 [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_validate,
294 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_validate,
295 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_validate,
296 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_type_array_validate,
297 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_validate,
298 };
299
300 static
301 void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
302 {
303 g_free(mapping);
304 }
305
306 static
307 void destroy_structure_field(struct structure_field *field)
308 {
309 bt_put(field->type);
310 g_free(field);
311 }
312
313 static
314 void check_ranges_overlap(gpointer element, gpointer query)
315 {
316 struct enumeration_mapping *mapping = element;
317 struct range_overlap_query *overlap_query = query;
318
319 if (mapping->range_start._signed <= overlap_query->range_end._signed
320 && overlap_query->range_start._signed <=
321 mapping->range_end._signed) {
322 overlap_query->overlaps = 1;
323 overlap_query->mapping_name = mapping->string;
324 }
325
326 overlap_query->overlaps |=
327 mapping->string == overlap_query->mapping_name;
328
329 if (overlap_query->overlaps) {
330 BT_LOGV("Overlapping enumeration field type mappings: "
331 "mapping-name=\"%s\", "
332 "mapping-a-range-start=%" PRId64 ", "
333 "mapping-a-range-end=%" PRId64 ", "
334 "mapping-b-range-start=%" PRId64 ", "
335 "mapping-b-range-end=%" PRId64,
336 g_quark_to_string(mapping->string),
337 mapping->range_start._signed,
338 mapping->range_end._signed,
339 overlap_query->range_start._signed,
340 overlap_query->range_end._signed);
341 }
342 }
343
344 static
345 void check_ranges_overlap_unsigned(gpointer element, gpointer query)
346 {
347 struct enumeration_mapping *mapping = element;
348 struct range_overlap_query *overlap_query = query;
349
350 if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned
351 && overlap_query->range_start._unsigned <=
352 mapping->range_end._unsigned) {
353 overlap_query->overlaps = 1;
354 overlap_query->mapping_name = mapping->string;
355 }
356
357 overlap_query->overlaps |=
358 mapping->string == overlap_query->mapping_name;
359
360 if (overlap_query->overlaps) {
361 BT_LOGW("Overlapping enumeration field type mappings: "
362 "mapping-name=\"%s\", "
363 "mapping-a-range-start=%" PRIu64 ", "
364 "mapping-a-range-end=%" PRIu64 ", "
365 "mapping-b-range-start=%" PRIu64 ", "
366 "mapping-b-range-end=%" PRIu64,
367 g_quark_to_string(mapping->string),
368 mapping->range_start._unsigned,
369 mapping->range_end._unsigned,
370 overlap_query->range_start._unsigned,
371 overlap_query->range_end._unsigned);
372 }
373 }
374
375 static
376 gint compare_enumeration_mappings_signed(struct enumeration_mapping **a,
377 struct enumeration_mapping **b)
378 {
379 return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1;
380 }
381
382 static
383 gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a,
384 struct enumeration_mapping **b)
385 {
386 return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1;
387 }
388
389 static
390 void bt_ctf_field_type_init(struct bt_ctf_field_type *type, bt_bool init_bo)
391 {
392 assert(type && (type->id > BT_CTF_FIELD_TYPE_ID_UNKNOWN) &&
393 (type->id < BT_CTF_NR_TYPE_IDS));
394
395 bt_object_init(type, bt_ctf_field_type_destroy);
396 type->freeze = type_freeze_funcs[type->id];
397 type->serialize = type_serialize_funcs[type->id];
398
399 if (init_bo) {
400 int ret = bt_ctf_field_type_set_byte_order(type,
401 BT_CTF_BYTE_ORDER_NATIVE);
402 assert(ret == 0);
403 }
404
405 type->alignment = 1;
406 }
407
408 static
409 int add_structure_field(GPtrArray *fields,
410 GHashTable *field_name_to_index,
411 struct bt_ctf_field_type *field_type,
412 const char *field_name)
413 {
414 int ret = 0;
415 GQuark name_quark = g_quark_from_string(field_name);
416 struct structure_field *field;
417
418 /* Make sure structure does not contain a field of the same name */
419 if (g_hash_table_lookup_extended(field_name_to_index,
420 GUINT_TO_POINTER(name_quark), NULL, NULL)) {
421 BT_LOGW("Structure or variant field type already contains a field type with this name: "
422 "field-name=\"%s\"", field_name);
423 ret = -1;
424 goto end;
425 }
426
427 field = g_new0(struct structure_field, 1);
428 if (!field) {
429 BT_LOGE_STR("Failed to allocate one structure/variant field type field.");
430 ret = -1;
431 goto end;
432 }
433
434 bt_get(field_type);
435 field->name = name_quark;
436 field->type = field_type;
437 g_hash_table_insert(field_name_to_index,
438 GUINT_TO_POINTER(name_quark),
439 GUINT_TO_POINTER(fields->len));
440 g_ptr_array_add(fields, field);
441 BT_LOGV("Added structure/variant field type field: field-ft-addr=%p, "
442 "field-name=\"%s\"", field_type, field_name);
443 end:
444 return ret;
445 }
446
447 static
448 void bt_ctf_field_type_destroy(struct bt_object *obj)
449 {
450 struct bt_ctf_field_type *type;
451 enum bt_ctf_field_type_id type_id;
452
453 BT_LOGD("Destroying field type object: addr=%p", obj);
454 type = container_of(obj, struct bt_ctf_field_type, base);
455 type_id = type->id;
456 if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN ||
457 type_id >= BT_CTF_NR_TYPE_IDS) {
458 BT_LOGW("Unknown field type ID: addr=%p, id=%d", obj, type_id);
459 return;
460 }
461
462 type_destroy_funcs[type_id](type);
463 }
464
465 static
466 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *type)
467 {
468 int ret = 0;
469
470 struct bt_ctf_field_type_integer *integer =
471 container_of(type, struct bt_ctf_field_type_integer,
472 parent);
473
474 if (integer->mapped_clock && integer->is_signed) {
475 BT_LOGW("Invalid integer field type: cannot be signed and have a mapped clock class: "
476 "ft-addr=%p, clock-class-addr=%p, clock-class-name=\"%s\"",
477 type, integer->mapped_clock,
478 bt_ctf_clock_class_get_name(integer->mapped_clock));
479 ret = -1;
480 goto end;
481 }
482
483 end:
484 return ret;
485 }
486
487 static
488 struct enumeration_mapping *get_enumeration_mapping(
489 struct bt_ctf_field_type *type, uint64_t index)
490 {
491 struct enumeration_mapping *mapping = NULL;
492 struct bt_ctf_field_type_enumeration *enumeration;
493
494 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
495 parent);
496 if (index >= enumeration->entries->len) {
497 BT_LOGW("Invalid parameter: index is out of bounds: "
498 "addr=%p, index=%" PRIu64 ", count=%u",
499 type, index, enumeration->entries->len);
500 goto end;
501 }
502
503 mapping = g_ptr_array_index(enumeration->entries, index);
504 end:
505 return mapping;
506 }
507
508 /*
509 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
510 * Only used when freezing an enumeration.
511 */
512 static
513 void set_enumeration_range_overlap(
514 struct bt_ctf_field_type *type)
515 {
516 int64_t i, j, len;
517 struct bt_ctf_field_type *container_type;
518 struct bt_ctf_field_type_enumeration *enumeration_type;
519 int is_signed;
520
521 BT_LOGV("Setting enumeration field type's overlap flag: addr=%p",
522 type);
523 enumeration_type = container_of(type,
524 struct bt_ctf_field_type_enumeration, parent);
525
526 len = enumeration_type->entries->len;
527 container_type = enumeration_type->container;
528 is_signed = bt_ctf_field_type_integer_get_signed(container_type);
529
530 for (i = 0; i < len; i++) {
531 for (j = i + 1; j < len; j++) {
532 struct enumeration_mapping *mapping[2];
533
534 mapping[0] = get_enumeration_mapping(type, i);
535 mapping[1] = get_enumeration_mapping(type, j);
536 if (is_signed) {
537 if (mapping[0]->range_start._signed
538 <= mapping[1]->range_end._signed
539 && mapping[0]->range_end._signed
540 >= mapping[1]->range_start._signed) {
541 enumeration_type->has_overlapping_ranges = BT_TRUE;
542 goto end;
543 }
544 } else {
545 if (mapping[0]->range_start._unsigned
546 <= mapping[1]->range_end._unsigned
547 && mapping[0]->range_end._unsigned
548 >= mapping[1]->range_start._unsigned) {
549 enumeration_type->has_overlapping_ranges = BT_TRUE;
550 goto end;
551 }
552 }
553 }
554 }
555
556 end:
557 if (enumeration_type->has_overlapping_ranges) {
558 BT_LOGV_STR("Enumeration field type has overlapping ranges.");
559 } else {
560 BT_LOGV_STR("Enumeration field type has no overlapping ranges.");
561 }
562 }
563
564 static
565 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type)
566 {
567 int ret = 0;
568
569 struct bt_ctf_field_type_enumeration *enumeration =
570 container_of(type, struct bt_ctf_field_type_enumeration,
571 parent);
572 struct bt_ctf_field_type *container_type =
573 bt_ctf_field_type_enumeration_get_container_type(type);
574
575 assert(container_type);
576 ret = bt_ctf_field_type_validate(container_type);
577 if (ret) {
578 BT_LOGW("Invalid enumeration field type: container type is invalid: "
579 "enum-ft-addr=%p, int-ft-addr=%p",
580 type, container_type);
581 goto end;
582 }
583
584 /* Ensure enum has entries */
585 if (enumeration->entries->len == 0) {
586 BT_LOGW("Invalid enumeration field type: no entries: "
587 "addr=%p", type);
588 ret = -1;
589 goto end;
590 }
591
592 end:
593 BT_PUT(container_type);
594 return ret;
595 }
596
597 static
598 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *type)
599 {
600 int ret = 0;
601 struct bt_ctf_field_type *element_type = NULL;
602 struct bt_ctf_field_type_sequence *sequence =
603 container_of(type, struct bt_ctf_field_type_sequence,
604 parent);
605
606 /* Length field name should be set at this point */
607 if (sequence->length_field_name->len == 0) {
608 BT_LOGW("Invalid sequence field type: no length field name: "
609 "addr=%p", type);
610 ret = -1;
611 goto end;
612 }
613
614 element_type = bt_ctf_field_type_sequence_get_element_type(type);
615 assert(element_type);
616 ret = bt_ctf_field_type_validate(element_type);
617 if (ret) {
618 BT_LOGW("Invalid sequence field type: invalid element field type: "
619 "seq-ft-addr=%p, element-ft-add=%p",
620 type, element_type);
621 }
622
623 end:
624 BT_PUT(element_type);
625
626 return ret;
627 }
628
629 static
630 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *type)
631 {
632 int ret = 0;
633 struct bt_ctf_field_type *element_type = NULL;
634
635 element_type = bt_ctf_field_type_array_get_element_type(type);
636 assert(element_type);
637 ret = bt_ctf_field_type_validate(element_type);
638 if (ret) {
639 BT_LOGW("Invalid array field type: invalid element field type: "
640 "array-ft-addr=%p, element-ft-add=%p",
641 type, element_type);
642 }
643
644 BT_PUT(element_type);
645 return ret;
646 }
647
648 static
649 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *type)
650 {
651 int ret = 0;
652 struct bt_ctf_field_type *child_type = NULL;
653 int64_t field_count = bt_ctf_field_type_structure_get_field_count(type);
654 int64_t i;
655
656 assert(field_count >= 0);
657
658 for (i = 0; i < field_count; ++i) {
659 const char *field_name;
660
661 ret = bt_ctf_field_type_structure_get_field_by_index(type,
662 &field_name, &child_type, i);
663 assert(ret == 0);
664 ret = bt_ctf_field_type_validate(child_type);
665 if (ret) {
666 BT_LOGW("Invalid structure field type: "
667 "a contained field type is invalid: "
668 "struct-ft-addr=%p, field-ft-addr=%p, "
669 "field-name=\"%s\", field-index=%" PRId64,
670 type, child_type, field_name, i);
671 goto end;
672 }
673
674 BT_PUT(child_type);
675 }
676
677 end:
678 BT_PUT(child_type);
679
680 return ret;
681 }
682
683 static
684 bt_bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
685 struct bt_ctf_field_type_enumeration *enumeration_type)
686 {
687 if (!enumeration_type->parent.frozen) {
688 set_enumeration_range_overlap(&enumeration_type->parent);
689 }
690 return enumeration_type->has_overlapping_ranges;
691 }
692
693 static
694 int bt_ctf_field_type_enumeration_get_mapping_name(
695 struct bt_ctf_field_type *enum_field_type,
696 uint64_t index,
697 const char **mapping_name)
698 {
699 int ret = 0;
700 struct enumeration_mapping *mapping;
701
702 assert(enum_field_type);
703 mapping = get_enumeration_mapping(enum_field_type, index);
704 assert(mapping);
705 if (mapping_name) {
706 *mapping_name = g_quark_to_string(mapping->string);
707 }
708
709 return ret;
710 }
711
712 static
713 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
714 {
715 int ret = 0;
716 int64_t field_count;
717 struct bt_ctf_field_type *child_type = NULL;
718 struct bt_ctf_field_type_variant *variant =
719 container_of(type, struct bt_ctf_field_type_variant,
720 parent);
721 int64_t i;
722 int64_t tag_mappings_count;
723
724 if (variant->tag_name->len == 0) {
725 BT_LOGW("Invalid variant field type: no tag field name: "
726 "addr=%p", type);
727 ret = -1;
728 goto end;
729 }
730
731 if (!variant->tag) {
732 BT_LOGW("Invalid variant field type: no tag field type: "
733 "addr=%p, tag-field-name=\"%s\"", type,
734 variant->tag_name->str);
735 ret = -1;
736 goto end;
737 }
738
739 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
740 variant->tag)) {
741 BT_LOGW("Invalid variant field type: enumeration tag field type has overlapping ranges: "
742 "variant-ft-addr=%p, tag-field-name=\"%s\", "
743 "enum-ft-addr=%p", type, variant->tag_name->str,
744 variant->tag);
745 ret = -1;
746 goto end;
747 }
748
749 tag_mappings_count =
750 bt_ctf_field_type_enumeration_get_mapping_count(
751 (struct bt_ctf_field_type *) variant->tag);
752
753 /*
754 * Validate that each mapping found in the tag has a name which
755 * is also the name of a field in this variant field type.
756 *
757 * The opposite is accepted: variant FT fields which cannot be
758 * selected because the variant FT tag has no mapping named as
759 * such. This scenario, while not ideal, cannot cause any error.
760 */
761 for (i = 0; i < tag_mappings_count; ++i) {
762 const char *label;
763 struct bt_ctf_field_type *ft;
764
765 ret = bt_ctf_field_type_enumeration_get_mapping_name(
766 (struct bt_ctf_field_type *) variant->tag,
767 i, &label);
768 assert(ret == 0);
769 assert(label);
770 ft = bt_ctf_field_type_variant_get_field_type_by_name(
771 type, label);
772 if (!ft) {
773 BT_LOGW("Invalid variant field type: "
774 "enumeration tag field type contains a mapping which does not name a variant field type field: "
775 "variant-ft-addr=%p, tag-field-name=\"%s\", "
776 "enum-ft-addr=%p, mapping-name=\"%s\"",
777 type, variant->tag_name->str, variant->tag,
778 label);
779 ret = -1;
780 goto end;
781 }
782
783 BT_PUT(ft);
784 }
785
786 field_count = bt_ctf_field_type_variant_get_field_count(type);
787 if (field_count < 0) {
788 BT_LOGW("Invalid variant field type: no fields: "
789 "addr=%p, tag-field-name=\"%s\"",
790 type, variant->tag_name->str);
791 ret = -1;
792 goto end;
793 }
794
795 for (i = 0; i < field_count; ++i) {
796 const char *field_name;
797
798 ret = bt_ctf_field_type_variant_get_field_by_index(type,
799 &field_name, &child_type, i);
800 assert(ret == 0);
801 ret = bt_ctf_field_type_validate(child_type);
802 if (ret) {
803 BT_LOGW("Invalid variant field type: "
804 "a contained field type is invalid: "
805 "variant-ft-addr=%p, tag-field-name=\"%s\", "
806 "field-ft-addr=%p, field-name=\"%s\", "
807 "field-index=%" PRId64,
808 type, variant->tag_name->str, child_type,
809 field_name, i);
810 goto end;
811 }
812
813 BT_PUT(child_type);
814 }
815
816 end:
817 BT_PUT(child_type);
818
819 return ret;
820 }
821
822 /*
823 * This function validates a given field type without considering
824 * where this field type is located. It only validates the properties
825 * of the given field type and the properties of its children if
826 * applicable.
827 */
828 BT_HIDDEN
829 int bt_ctf_field_type_validate(struct bt_ctf_field_type *type)
830 {
831 int ret = 0;
832 enum bt_ctf_field_type_id id = bt_ctf_field_type_get_type_id(type);
833
834 assert(type);
835
836 if (type->valid) {
837 /* Already marked as valid */
838 goto end;
839 }
840
841 if (type_validate_funcs[id]) {
842 ret = type_validate_funcs[id](type);
843 }
844
845 if (!ret && type->frozen) {
846 /* Field type is valid */
847 type->valid = 1;
848 }
849
850 end:
851 return ret;
852 }
853
854 struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size)
855 {
856 struct bt_ctf_field_type_integer *integer =
857 g_new0(struct bt_ctf_field_type_integer, 1);
858
859 BT_LOGD("Creating integer field type object: size=%u", size);
860
861 if (!integer) {
862 BT_LOGE_STR("Failed to allocate one integer field type.");
863 return NULL;
864 }
865
866 if (size == 0 || size > 64) {
867 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
868 "size=%u", size);
869 return NULL;
870 }
871
872 integer->parent.id = BT_CTF_FIELD_TYPE_ID_INTEGER;
873 integer->size = size;
874 integer->base = BT_CTF_INTEGER_BASE_DECIMAL;
875 integer->encoding = BT_CTF_STRING_ENCODING_NONE;
876 bt_ctf_field_type_init(&integer->parent, TRUE);
877 BT_LOGD("Created integer field type object: addr=%p, size=%u",
878 &integer->parent, size);
879 return &integer->parent;
880 }
881
882 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type)
883 {
884 int ret = 0;
885 struct bt_ctf_field_type_integer *integer;
886
887 if (!type) {
888 BT_LOGW_STR("Invalid parameter: field type is NULL.");
889 ret = -1;
890 goto end;
891 }
892
893 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
894 BT_LOGW("Invalid parameter: field type is not an integer field type: "
895 "addr=%p, ft-id=%s", type,
896 bt_ctf_field_type_id_string(type->id));
897 ret = -1;
898 goto end;
899 }
900
901 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
902 ret = (int) integer->size;
903 end:
904 return ret;
905 }
906
907 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type)
908 {
909 int ret = 0;
910 struct bt_ctf_field_type_integer *integer;
911
912 if (!type) {
913 BT_LOGW_STR("Invalid parameter: field type is NULL.");
914 ret = -1;
915 goto end;
916 }
917
918 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
919 BT_LOGW("Invalid parameter: field type is not an integer field type: "
920 "addr=%p, ft-id=%s", type,
921 bt_ctf_field_type_id_string(type->id));
922 ret = -1;
923 goto end;
924 }
925
926 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
927 ret = integer->is_signed;
928 end:
929 return ret;
930 }
931
932 bt_bool bt_ctf_field_type_integer_is_signed(
933 struct bt_ctf_field_type *int_field_type)
934 {
935 return bt_ctf_field_type_integer_get_signed(int_field_type) ?
936 BT_TRUE : BT_FALSE;
937 }
938
939 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type,
940 bt_bool is_signed)
941 {
942 int ret = 0;
943 struct bt_ctf_field_type_integer *integer;
944
945 if (!type) {
946 BT_LOGW_STR("Invalid parameter: field type is NULL.");
947 ret = -1;
948 goto end;
949 }
950
951 if (type->frozen) {
952 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
953 type);
954 ret = -1;
955 goto end;
956 }
957
958 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
959 BT_LOGW("Invalid parameter: field type is not an integer field type: "
960 "addr=%p, ft-id=%s", type,
961 bt_ctf_field_type_id_string(type->id));
962 ret = -1;
963 goto end;
964 }
965
966 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
967 integer->is_signed = !!is_signed;
968 BT_LOGV("Set integer field type's signedness: addr=%p, is-signed=%d",
969 type, is_signed);
970 end:
971 return ret;
972 }
973
974 int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type *type,
975 size_t size)
976 {
977 int ret = 0;
978 struct bt_ctf_field_type_integer *integer;
979
980 if (!type) {
981 BT_LOGW_STR("Invalid parameter: field type is NULL.");
982 ret = -1;
983 goto end;
984 }
985
986 if (type->frozen) {
987 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
988 type);
989 ret = -1;
990 goto end;
991 }
992
993 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
994 BT_LOGW("Invalid parameter: field type is not an integer field type: "
995 "addr=%p, ft-id=%s", type,
996 bt_ctf_field_type_id_string(type->id));
997 ret = -1;
998 goto end;
999 }
1000
1001 if (size == 0 || size > 64) {
1002 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
1003 "addr=%p, size=%u", type, size);
1004 ret = -1;
1005 goto end;
1006 }
1007
1008 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1009 integer->size = size;
1010 BT_LOGV("Set integer field type's size: addr=%p, size=%u",
1011 type, size);
1012 end:
1013 return ret;
1014 }
1015
1016 enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base(
1017 struct bt_ctf_field_type *type)
1018 {
1019 enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN;
1020 struct bt_ctf_field_type_integer *integer;
1021
1022 if (!type) {
1023 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1024 goto end;
1025 }
1026
1027 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1028 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1029 "addr=%p, ft-id=%s", type,
1030 bt_ctf_field_type_id_string(type->id));
1031 goto end;
1032 }
1033
1034 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1035 ret = integer->base;
1036 end:
1037 return ret;
1038 }
1039
1040 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type,
1041 enum bt_ctf_integer_base base)
1042 {
1043 int ret = 0;
1044
1045 if (!type) {
1046 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1047 ret = -1;
1048 goto end;
1049 }
1050
1051 if (type->frozen) {
1052 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1053 type);
1054 ret = -1;
1055 goto end;
1056 }
1057
1058 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1059 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1060 "addr=%p, ft-id=%s", type,
1061 bt_ctf_field_type_id_string(type->id));
1062 ret = -1;
1063 goto end;
1064 }
1065
1066 switch (base) {
1067 case BT_CTF_INTEGER_BASE_BINARY:
1068 case BT_CTF_INTEGER_BASE_OCTAL:
1069 case BT_CTF_INTEGER_BASE_DECIMAL:
1070 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
1071 {
1072 struct bt_ctf_field_type_integer *integer = container_of(type,
1073 struct bt_ctf_field_type_integer, parent);
1074 integer->base = base;
1075 break;
1076 }
1077 default:
1078 BT_LOGW("Invalid parameter: unknown integer field type base: "
1079 "addr=%p, base=%d", type, base);
1080 ret = -1;
1081 }
1082
1083 BT_LOGV("Set integer field type's base: addr=%p, base=%s",
1084 type, bt_ctf_integer_base_string(base));
1085
1086 end:
1087 return ret;
1088 }
1089
1090 enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding(
1091 struct bt_ctf_field_type *type)
1092 {
1093 enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN;
1094 struct bt_ctf_field_type_integer *integer;
1095
1096 if (!type) {
1097 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1098 goto end;
1099 }
1100
1101 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1102 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1103 "addr=%p, ft-id=%s", type,
1104 bt_ctf_field_type_id_string(type->id));
1105 goto end;
1106 }
1107
1108 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1109 ret = integer->encoding;
1110 end:
1111 return ret;
1112 }
1113
1114 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
1115 enum bt_ctf_string_encoding encoding)
1116 {
1117 int ret = 0;
1118 struct bt_ctf_field_type_integer *integer;
1119
1120 if (!type) {
1121 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1122 ret = -1;
1123 goto end;
1124 }
1125
1126 if (type->frozen) {
1127 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1128 type);
1129 ret = -1;
1130 goto end;
1131 }
1132
1133 if (type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1134 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1135 "addr=%p, ft-id=%s", type,
1136 bt_ctf_field_type_id_string(type->id));
1137 ret = -1;
1138 goto end;
1139 }
1140
1141 if (encoding < BT_CTF_STRING_ENCODING_NONE ||
1142 encoding >= BT_CTF_STRING_ENCODING_UNKNOWN) {
1143 BT_LOGW("Invalid parameter: unknown string encoding: "
1144 "addr=%p, encoding=%d", type, encoding);
1145 ret = -1;
1146 goto end;
1147 }
1148
1149 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1150 integer->encoding = encoding;
1151 BT_LOGV("Set integer field type's encoding: addr=%p, encoding=%s",
1152 type, bt_ctf_string_encoding_string(encoding));
1153 end:
1154 return ret;
1155 }
1156
1157 struct bt_ctf_clock_class *bt_ctf_field_type_integer_get_mapped_clock_class(
1158 struct bt_ctf_field_type *type)
1159 {
1160 struct bt_ctf_field_type_integer *integer;
1161 struct bt_ctf_clock_class *clock_class = NULL;
1162
1163 if (!type) {
1164 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1165 goto end;
1166 }
1167
1168 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1169 clock_class = integer->mapped_clock;
1170 bt_get(clock_class);
1171 end:
1172 return clock_class;
1173 }
1174
1175 int bt_ctf_field_type_integer_set_mapped_clock_class(
1176 struct bt_ctf_field_type *type,
1177 struct bt_ctf_clock_class *clock_class)
1178 {
1179 struct bt_ctf_field_type_integer *integer;
1180 int ret = 0;
1181
1182 if (!type) {
1183 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1184 ret = -1;
1185 goto end;
1186 }
1187
1188 if (!clock_class) {
1189 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
1190 ret = -1;
1191 goto end;
1192 }
1193
1194 if (type->frozen) {
1195 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1196 type);
1197 ret = -1;
1198 goto end;
1199 }
1200
1201 if (!bt_ctf_clock_class_is_valid(clock_class)) {
1202 BT_LOGW("Invalid parameter: clock class is invalid: ft-addr=%p"
1203 "clock-class-addr=%p, clock-class-name=\"%s\"",
1204 type, clock_class,
1205 bt_ctf_clock_class_get_name(clock_class));
1206 ret = -1;
1207 goto end;
1208 }
1209
1210 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
1211 bt_put(integer->mapped_clock);
1212 integer->mapped_clock = bt_get(clock_class);
1213 BT_LOGV("Set integer field type's mapped clock class: ft-addr=%p, "
1214 "clock-class-addr=%p, clock-class-name=\"%s\"",
1215 type, clock_class, bt_ctf_clock_class_get_name(clock_class));
1216 end:
1217 return ret;
1218 }
1219
1220 static
1221 void bt_ctf_field_type_enum_iter_destroy(struct bt_object *obj)
1222 {
1223 struct bt_ctf_field_type_enumeration_mapping_iterator *iter =
1224 container_of(obj,
1225 struct bt_ctf_field_type_enumeration_mapping_iterator,
1226 base);
1227
1228 bt_put(&iter->enumeration_type->parent);
1229 g_free(iter);
1230 }
1231
1232 static
1233 struct bt_ctf_field_type_enumeration_mapping_iterator *
1234 bt_ctf_field_type_enumeration_find_mappings_type(
1235 struct bt_ctf_field_type *type,
1236 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type)
1237 {
1238 struct bt_ctf_field_type_enumeration *enumeration_type;
1239 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
1240
1241 if (!type) {
1242 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1243 goto end;
1244 }
1245
1246 if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) {
1247 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1248 "addr=%p, ft-id=%s", type,
1249 bt_ctf_field_type_id_string(type->id));
1250 goto end;
1251 }
1252
1253 enumeration_type = container_of(type,
1254 struct bt_ctf_field_type_enumeration, parent);
1255 iter = g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator, 1);
1256 if (!iter) {
1257 BT_LOGE_STR("Failed to allocate one enumeration field type mapping.");
1258 goto end;
1259 }
1260
1261 bt_object_init(&iter->base, bt_ctf_field_type_enum_iter_destroy);
1262 bt_get(type);
1263 iter->enumeration_type = enumeration_type;
1264 iter->index = -1;
1265 iter->type = iterator_type;
1266 end:
1267 return iter;
1268 }
1269
1270 struct bt_ctf_field_type_enumeration_mapping_iterator *
1271 bt_ctf_field_type_enumeration_find_mappings_by_name(
1272 struct bt_ctf_field_type *type, const char *name)
1273 {
1274 struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
1275
1276 iter = bt_ctf_field_type_enumeration_find_mappings_type(
1277 type, ITERATOR_BY_NAME);
1278 if (!iter) {
1279 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1280 "ft-addr=%p, mapping-name=\"%s\"", type, name);
1281 goto error;
1282 }
1283
1284 iter->u.name_quark = g_quark_try_string(name);
1285 if (!iter->u.name_quark) {
1286 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
1287 goto error;
1288 }
1289
1290 /* Advance iterator to first entry, or leave index at -1. */
1291 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
1292 /* No entry found. */
1293 goto error;
1294 }
1295
1296 return iter;
1297 error:
1298 bt_put(iter);
1299 return NULL;
1300 }
1301
1302 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1303 struct bt_ctf_field_type_enumeration_mapping_iterator *iter)
1304 {
1305 struct bt_ctf_field_type_enumeration *enumeration;
1306 struct bt_ctf_field_type *type;
1307 int i, ret = 0, len;
1308
1309 if (!iter) {
1310 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1311 ret = -1;
1312 goto end;
1313 }
1314
1315 enumeration = iter->enumeration_type;
1316 type = &enumeration->parent;
1317 len = enumeration->entries->len;
1318 for (i = iter->index + 1; i < len; i++) {
1319 struct enumeration_mapping *mapping =
1320 get_enumeration_mapping(type, i);
1321
1322 switch (iter->type) {
1323 case ITERATOR_BY_NAME:
1324 if (mapping->string == iter->u.name_quark) {
1325 iter->index = i;
1326 goto end;
1327 }
1328 break;
1329 case ITERATOR_BY_SIGNED_VALUE:
1330 {
1331 int64_t value = iter->u.signed_value;
1332
1333 if (value >= mapping->range_start._signed &&
1334 value <= mapping->range_end._signed) {
1335 iter->index = i;
1336 goto end;
1337 }
1338 break;
1339 }
1340 case ITERATOR_BY_UNSIGNED_VALUE:
1341 {
1342 uint64_t value = iter->u.unsigned_value;
1343
1344 if (value >= mapping->range_start._unsigned &&
1345 value <= mapping->range_end._unsigned) {
1346 iter->index = i;
1347 goto end;
1348 }
1349 break;
1350 }
1351 default:
1352 abort();
1353 }
1354 }
1355
1356 ret = -1;
1357 end:
1358 return ret;
1359 }
1360
1361 struct bt_ctf_field_type_enumeration_mapping_iterator *
1362 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1363 struct bt_ctf_field_type *type, int64_t value)
1364 {
1365 struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
1366
1367 iter = bt_ctf_field_type_enumeration_find_mappings_type(
1368 type, ITERATOR_BY_SIGNED_VALUE);
1369 if (!iter) {
1370 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1371 "ft-addr=%p, value=%" PRId64, type, value);
1372 goto error;
1373 }
1374
1375 if (bt_ctf_field_type_integer_get_signed(
1376 iter->enumeration_type->container) != 1) {
1377 BT_LOGW("Invalid parameter: enumeration field type is unsigned: "
1378 "enum-ft-addr=%p, int-ft-addr=%p",
1379 type, iter->enumeration_type->container);
1380 goto error;
1381 }
1382
1383 iter->u.signed_value = value;
1384
1385 /* Advance iterator to first entry, or leave index at -1. */
1386 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
1387 /* No entry found. */
1388 goto error;
1389 }
1390
1391 return iter;
1392 error:
1393 bt_put(iter);
1394 return NULL;
1395 }
1396
1397 struct bt_ctf_field_type_enumeration_mapping_iterator *
1398 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1399 struct bt_ctf_field_type *type, uint64_t value)
1400 {
1401 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
1402
1403 if (!type) {
1404 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1405 goto error;
1406 }
1407
1408 iter = bt_ctf_field_type_enumeration_find_mappings_type(
1409 type, ITERATOR_BY_UNSIGNED_VALUE);
1410 if (!iter) {
1411 BT_LOGE("Cannot create enumeration field type mapping iterator: "
1412 "ft-addr=%p, value=%" PRIu64, type, value);
1413 goto error;
1414 }
1415
1416 if (bt_ctf_field_type_integer_get_signed(
1417 iter->enumeration_type->container) != 0) {
1418 BT_LOGW("Invalid parameter: enumeration field type is signed: "
1419 "enum-ft-addr=%p, int-ft-addr=%p",
1420 type, iter->enumeration_type->container);
1421 goto error;
1422 }
1423 iter->u.unsigned_value = value;
1424
1425 /* Advance iterator to first entry, or leave index at -1. */
1426 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
1427 /* No entry found. */
1428 goto error;
1429 }
1430
1431 return iter;
1432 error:
1433 bt_put(iter);
1434 return NULL;
1435 }
1436
1437 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1438 struct bt_ctf_field_type_enumeration_mapping_iterator *iter,
1439 const char **mapping_name, int64_t *range_begin,
1440 int64_t *range_end)
1441 {
1442 int ret = 0;
1443
1444 if (!iter) {
1445 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1446 ret = -1;
1447 goto end;
1448 }
1449
1450 ret = bt_ctf_field_type_enumeration_get_mapping_signed(
1451 &iter->enumeration_type->parent, iter->index,
1452 mapping_name, range_begin, range_end);
1453 end:
1454 return ret;
1455 }
1456
1457 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1458 struct bt_ctf_field_type_enumeration_mapping_iterator *iter,
1459 const char **mapping_name, uint64_t *range_begin,
1460 uint64_t *range_end)
1461 {
1462 int ret = 0;
1463
1464 if (!iter) {
1465 BT_LOGW_STR("Invalid parameter: enumeration field type mapping iterator is NULL.");
1466 ret = -1;
1467 goto end;
1468 }
1469
1470 ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(
1471 &iter->enumeration_type->parent, iter->index,
1472 mapping_name, range_begin, range_end);
1473 end:
1474 return ret;
1475 }
1476
1477 int bt_ctf_field_type_enumeration_get_mapping_signed(
1478 struct bt_ctf_field_type *enum_field_type,
1479 uint64_t index, const char **mapping_name, int64_t *range_begin,
1480 int64_t *range_end)
1481 {
1482 int ret = 0;
1483 struct enumeration_mapping *mapping;
1484
1485 if (!enum_field_type) {
1486 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1487 ret = -1;
1488 goto end;
1489 }
1490
1491 mapping = get_enumeration_mapping(enum_field_type, index);
1492 if (!mapping) {
1493 /* get_enumeration_mapping() reports any error */
1494 ret = -1;
1495 goto end;
1496 }
1497
1498 if (mapping_name) {
1499 *mapping_name = g_quark_to_string(mapping->string);
1500 }
1501
1502 if (range_begin) {
1503 *range_begin = mapping->range_start._signed;
1504 }
1505
1506 if (range_end) {
1507 *range_end = mapping->range_end._signed;
1508 }
1509 end:
1510 return ret;
1511 }
1512
1513 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1514 struct bt_ctf_field_type *enum_field_type,
1515 uint64_t index,
1516 const char **mapping_name, uint64_t *range_begin,
1517 uint64_t *range_end)
1518 {
1519 int ret = 0;
1520 struct enumeration_mapping *mapping;
1521
1522 if (!enum_field_type) {
1523 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1524 ret = -1;
1525 goto end;
1526 }
1527
1528 mapping = get_enumeration_mapping(enum_field_type, index);
1529 if (!mapping) {
1530 /* get_enumeration_mapping() reports any error */
1531 ret = -1;
1532 goto end;
1533 }
1534
1535 if (mapping_name) {
1536 *mapping_name = g_quark_to_string(mapping->string);
1537 }
1538
1539 if (range_begin) {
1540 *range_begin = mapping->range_start._unsigned;
1541 }
1542
1543 if (range_end) {
1544 *range_end = mapping->range_end._unsigned;
1545 }
1546 end:
1547 return ret;
1548 }
1549
1550 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(
1551 struct bt_ctf_field_type *integer_container_type)
1552 {
1553 struct bt_ctf_field_type_enumeration *enumeration = NULL;
1554
1555 BT_LOGD("Creating enumeration field type object: int-ft-addr=%p",
1556 integer_container_type);
1557
1558 if (!integer_container_type) {
1559 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1560 goto error;
1561 }
1562
1563 if (integer_container_type->id != BT_CTF_FIELD_TYPE_ID_INTEGER) {
1564 BT_LOGW("Invalid parameter: container field type is not an integer field type: "
1565 "container-ft-addr=%p, container-ft-id=%s",
1566 integer_container_type,
1567 bt_ctf_field_type_id_string(integer_container_type->id));
1568 goto error;
1569 }
1570
1571 enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
1572 if (!enumeration) {
1573 BT_LOGE_STR("Failed to allocate one enumeration field type.");
1574 goto error;
1575 }
1576
1577 enumeration->parent.id = BT_CTF_FIELD_TYPE_ID_ENUM;
1578 bt_get(integer_container_type);
1579 enumeration->container = integer_container_type;
1580 enumeration->entries = g_ptr_array_new_with_free_func(
1581 (GDestroyNotify)destroy_enumeration_mapping);
1582 bt_ctf_field_type_init(&enumeration->parent, FALSE);
1583 BT_LOGD("Created enumeration field type object: addr=%p, "
1584 "int-ft-addr=%p, int-ft-size=%u",
1585 &enumeration->parent, integer_container_type,
1586 bt_ctf_field_type_integer_get_size(integer_container_type));
1587 return &enumeration->parent;
1588 error:
1589 g_free(enumeration);
1590 return NULL;
1591 }
1592
1593 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type(
1594 struct bt_ctf_field_type *type)
1595 {
1596 struct bt_ctf_field_type *container_type = NULL;
1597 struct bt_ctf_field_type_enumeration *enumeration_type;
1598
1599 if (!type) {
1600 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1601 goto end;
1602 }
1603
1604 if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) {
1605 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1606 "addr=%p, ft-id=%s", type,
1607 bt_ctf_field_type_id_string(type->id));
1608 goto end;
1609 }
1610
1611 enumeration_type = container_of(type,
1612 struct bt_ctf_field_type_enumeration, parent);
1613 container_type = enumeration_type->container;
1614 bt_get(container_type);
1615 end:
1616 return container_type;
1617 }
1618
1619 int bt_ctf_field_type_enumeration_add_mapping_signed(
1620 struct bt_ctf_field_type *type, const char *string,
1621 int64_t range_start, int64_t range_end)
1622 {
1623 int ret = 0;
1624 GQuark mapping_name;
1625 struct enumeration_mapping *mapping;
1626 struct bt_ctf_field_type_enumeration *enumeration;
1627 char *escaped_string;
1628
1629 if (!type) {
1630 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1631 ret = -1;
1632 goto end;
1633 }
1634
1635 if (!string) {
1636 BT_LOGW_STR("Invalid parameter: string is NULL.");
1637 ret = -1;
1638 goto end;
1639 }
1640
1641 if (type->frozen) {
1642 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1643 type);
1644 ret = -1;
1645 goto end;
1646 }
1647
1648 if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) {
1649 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1650 "addr=%p, ft-id=%s", type,
1651 bt_ctf_field_type_id_string(type->id));
1652 ret = -1;
1653 goto end;
1654 }
1655
1656 if (range_end < range_start) {
1657 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1658 "addr=%p, range-start=%" PRId64 ", range-end=%" PRId64,
1659 type, range_start, range_end);
1660 ret = -1;
1661 goto end;
1662 }
1663
1664 if (strlen(string) == 0) {
1665 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1666 "enum-ft-addr=%p, mapping-name-addr=%p", type,
1667 string);
1668 ret = -1;
1669 goto end;
1670 }
1671
1672 escaped_string = g_strescape(string, NULL);
1673 if (!escaped_string) {
1674 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1675 "mapping-name-addr=%p, mapping-name=\"%s\"",
1676 type, string, string);
1677 ret = -1;
1678 goto end;
1679 }
1680
1681 mapping = g_new(struct enumeration_mapping, 1);
1682 if (!mapping) {
1683 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1684 ret = -1;
1685 goto error_free;
1686 }
1687 mapping_name = g_quark_from_string(escaped_string);
1688 *mapping = (struct enumeration_mapping) {
1689 .range_start._signed = range_start,
1690 .range_end._signed = range_end,
1691 .string = mapping_name,
1692 };
1693 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1694 parent);
1695 g_ptr_array_add(enumeration->entries, mapping);
1696 g_ptr_array_sort(enumeration->entries,
1697 (GCompareFunc)compare_enumeration_mappings_signed);
1698 BT_LOGV("Added mapping to signed enumeration field type: addr=%p, "
1699 "name=\"%s\", range-start=%" PRId64 ", "
1700 "range-end=%" PRId64,
1701 type, string, range_start, range_end);
1702 error_free:
1703 free(escaped_string);
1704 end:
1705 return ret;
1706 }
1707
1708 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1709 struct bt_ctf_field_type *type, const char *string,
1710 uint64_t range_start, uint64_t range_end)
1711 {
1712 int ret = 0;
1713 GQuark mapping_name;
1714 struct enumeration_mapping *mapping;
1715 struct bt_ctf_field_type_enumeration *enumeration;
1716 char *escaped_string;
1717
1718 if (!type) {
1719 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1720 ret = -1;
1721 goto end;
1722 }
1723
1724 if (!string) {
1725 BT_LOGW_STR("Invalid parameter: string is NULL.");
1726 ret = -1;
1727 goto end;
1728 }
1729
1730 if (type->frozen) {
1731 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1732 type);
1733 ret = -1;
1734 goto end;
1735 }
1736
1737 if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) {
1738 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1739 "addr=%p, ft-id=%s", type,
1740 bt_ctf_field_type_id_string(type->id));
1741 ret = -1;
1742 goto end;
1743 }
1744
1745 if (range_end < range_start) {
1746 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1747 "addr=%p, range-start=%" PRIu64 ", range-end=%" PRIu64,
1748 type, range_start, range_end);
1749 ret = -1;
1750 goto end;
1751 }
1752
1753 if (strlen(string) == 0) {
1754 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1755 "enum-ft-addr=%p, mapping-name-addr=%p", type,
1756 string);
1757 ret = -1;
1758 goto end;
1759 }
1760
1761 escaped_string = g_strescape(string, NULL);
1762 if (!escaped_string) {
1763 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1764 "mapping-name-addr=%p, mapping-name=\"%s\"",
1765 type, string, string);
1766 ret = -1;
1767 goto end;
1768 }
1769
1770 mapping = g_new(struct enumeration_mapping, 1);
1771 if (!mapping) {
1772 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1773 ret = -1;
1774 goto error_free;
1775 }
1776 mapping_name = g_quark_from_string(escaped_string);
1777 *mapping = (struct enumeration_mapping) {
1778 .range_start._unsigned = range_start,
1779 .range_end._unsigned = range_end,
1780 .string = mapping_name,
1781 };
1782 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1783 parent);
1784 g_ptr_array_add(enumeration->entries, mapping);
1785 g_ptr_array_sort(enumeration->entries,
1786 (GCompareFunc)compare_enumeration_mappings_unsigned);
1787 BT_LOGV("Added mapping to unsigned enumeration field type: addr=%p, "
1788 "name=\"%s\", range-start=%" PRIu64 ", "
1789 "range-end=%" PRIu64,
1790 type, string, range_start, range_end);
1791 error_free:
1792 free(escaped_string);
1793 end:
1794 return ret;
1795 }
1796
1797 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
1798 struct bt_ctf_field_type *type)
1799 {
1800 int64_t ret = 0;
1801 struct bt_ctf_field_type_enumeration *enumeration;
1802
1803 if (!type) {
1804 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1805 ret = (int64_t) -1;
1806 goto end;
1807 }
1808
1809 if (type->id != BT_CTF_FIELD_TYPE_ID_ENUM) {
1810 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1811 "addr=%p, ft-id=%s", type,
1812 bt_ctf_field_type_id_string(type->id));
1813 ret = (int64_t) -1;
1814 goto end;
1815 }
1816
1817 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
1818 parent);
1819 ret = (int64_t) enumeration->entries->len;
1820 end:
1821 return ret;
1822 }
1823
1824 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void)
1825 {
1826 struct bt_ctf_field_type_floating_point *floating_point =
1827 g_new0(struct bt_ctf_field_type_floating_point, 1);
1828
1829 BT_LOGD_STR("Creating floating point number field type object.");
1830
1831 if (!floating_point) {
1832 BT_LOGE_STR("Failed to allocate one floating point number field type.");
1833 goto end;
1834 }
1835
1836 floating_point->parent.id = BT_CTF_FIELD_TYPE_ID_FLOAT;
1837 floating_point->exp_dig = sizeof(float) * CHAR_BIT - FLT_MANT_DIG;
1838 floating_point->mant_dig = FLT_MANT_DIG;
1839 bt_ctf_field_type_init(&floating_point->parent, TRUE);
1840 BT_LOGD("Created floating point number field type object: addr=%p, "
1841 "exp-size=%u, mant-size=%u", &floating_point->parent,
1842 floating_point->exp_dig, floating_point->mant_dig);
1843 end:
1844 return floating_point ? &floating_point->parent : NULL;
1845 }
1846
1847 int bt_ctf_field_type_floating_point_get_exponent_digits(
1848 struct bt_ctf_field_type *type)
1849 {
1850 int ret = 0;
1851 struct bt_ctf_field_type_floating_point *floating_point;
1852
1853 if (!type) {
1854 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1855 ret = -1;
1856 goto end;
1857 }
1858
1859 if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) {
1860 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1861 "addr=%p, ft-id=%s", type,
1862 bt_ctf_field_type_id_string(type->id));
1863 ret = -1;
1864 goto end;
1865 }
1866
1867 floating_point = container_of(type,
1868 struct bt_ctf_field_type_floating_point, parent);
1869 ret = (int) floating_point->exp_dig;
1870 end:
1871 return ret;
1872 }
1873
1874 int bt_ctf_field_type_floating_point_set_exponent_digits(
1875 struct bt_ctf_field_type *type,
1876 unsigned int exponent_digits)
1877 {
1878 int ret = 0;
1879 struct bt_ctf_field_type_floating_point *floating_point;
1880
1881 if (!type) {
1882 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1883 ret = -1;
1884 goto end;
1885 }
1886
1887 if (type->frozen) {
1888 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1889 type);
1890 ret = -1;
1891 goto end;
1892 }
1893
1894 if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) {
1895 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1896 "addr=%p, ft-id=%s", type,
1897 bt_ctf_field_type_id_string(type->id));
1898 ret = -1;
1899 goto end;
1900 }
1901
1902 floating_point = container_of(type,
1903 struct bt_ctf_field_type_floating_point, parent);
1904 if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) &&
1905 (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) &&
1906 (exponent_digits !=
1907 sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) {
1908 BT_LOGW("Invalid parameter: invalid exponent size: "
1909 "addr=%p, exp-size=%u", type, exponent_digits);
1910 ret = -1;
1911 goto end;
1912 }
1913
1914 floating_point->exp_dig = exponent_digits;
1915 BT_LOGV("Set floating point number field type's exponent size: addr=%p, "
1916 "exp-size=%u", type, exponent_digits);
1917 end:
1918 return ret;
1919 }
1920
1921 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1922 struct bt_ctf_field_type *type)
1923 {
1924 int ret = 0;
1925 struct bt_ctf_field_type_floating_point *floating_point;
1926
1927 if (!type) {
1928 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1929 ret = -1;
1930 goto end;
1931 }
1932
1933 if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) {
1934 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1935 "addr=%p, ft-id=%s", type,
1936 bt_ctf_field_type_id_string(type->id));
1937 ret = -1;
1938 goto end;
1939 }
1940
1941 floating_point = container_of(type,
1942 struct bt_ctf_field_type_floating_point, parent);
1943 ret = (int) floating_point->mant_dig;
1944 end:
1945 return ret;
1946 }
1947
1948 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1949 struct bt_ctf_field_type *type,
1950 unsigned int mantissa_digits)
1951 {
1952 int ret = 0;
1953 struct bt_ctf_field_type_floating_point *floating_point;
1954
1955 if (!type) {
1956 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1957 ret = -1;
1958 goto end;
1959 }
1960
1961 if (type->frozen) {
1962 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1963 type);
1964 ret = -1;
1965 goto end;
1966 }
1967
1968 if (type->id != BT_CTF_FIELD_TYPE_ID_FLOAT) {
1969 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1970 "addr=%p, ft-id=%s", type,
1971 bt_ctf_field_type_id_string(type->id));
1972 ret = -1;
1973 goto end;
1974 }
1975
1976 floating_point = container_of(type,
1977 struct bt_ctf_field_type_floating_point, parent);
1978
1979 if ((mantissa_digits != FLT_MANT_DIG) &&
1980 (mantissa_digits != DBL_MANT_DIG) &&
1981 (mantissa_digits != LDBL_MANT_DIG)) {
1982 BT_LOGW("Invalid parameter: invalid mantissa size: "
1983 "addr=%p, mant-size=%u", type, mantissa_digits);
1984 ret = -1;
1985 goto end;
1986 }
1987
1988 floating_point->mant_dig = mantissa_digits;
1989 BT_LOGV("Set floating point number field type's mantissa size: addr=%p, "
1990 "mant-size=%u", type, mantissa_digits);
1991 end:
1992 return ret;
1993 }
1994
1995 struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void)
1996 {
1997 struct bt_ctf_field_type_structure *structure =
1998 g_new0(struct bt_ctf_field_type_structure, 1);
1999
2000 BT_LOGD_STR("Creating structure field type object.");
2001
2002 if (!structure) {
2003 BT_LOGE_STR("Failed to allocate one structure field type.");
2004 goto error;
2005 }
2006
2007 structure->parent.id = BT_CTF_FIELD_TYPE_ID_STRUCT;
2008 structure->fields = g_ptr_array_new_with_free_func(
2009 (GDestroyNotify)destroy_structure_field);
2010 structure->field_name_to_index = g_hash_table_new(NULL, NULL);
2011 bt_ctf_field_type_init(&structure->parent, TRUE);
2012 BT_LOGD("Created structure field type object: addr=%p",
2013 &structure->parent);
2014 return &structure->parent;
2015 error:
2016 return NULL;
2017 }
2018
2019 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
2020 struct bt_ctf_field_type *field_type,
2021 const char *field_name)
2022 {
2023 int ret = 0;
2024 struct bt_ctf_field_type_structure *structure;
2025
2026 /*
2027 * TODO: check that `field_type` does not contain `type`,
2028 * recursively.
2029 */
2030 if (!type) {
2031 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2032 ret = -1;
2033 goto end;
2034 }
2035
2036 if (!field_name) {
2037 BT_LOGW_STR("Invalid parameter: field name is NULL.");
2038 ret = -1;
2039 goto end;
2040 }
2041
2042 if (type->frozen) {
2043 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2044 type);
2045 ret = -1;
2046 goto end;
2047 }
2048
2049 if (bt_ctf_validate_identifier(field_name)) {
2050 BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
2051 "struct-ft-addr=%p, field-name=\"%s\"",
2052 type, field_name);
2053 ret = -1;
2054 goto end;
2055 }
2056
2057 if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
2058 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2059 "addr=%p, ft-id=%s", type,
2060 bt_ctf_field_type_id_string(type->id));
2061 ret = -1;
2062 goto end;
2063 }
2064
2065 if (type == field_type) {
2066 BT_LOGW("Invalid parameter: structure field type and field type to add are the same: "
2067 "addr=%p", type);
2068 ret = -1;
2069 goto end;
2070 }
2071
2072 structure = container_of(type,
2073 struct bt_ctf_field_type_structure, parent);
2074 if (add_structure_field(structure->fields,
2075 structure->field_name_to_index, field_type, field_name)) {
2076 BT_LOGW("Cannot add field to structure field type: "
2077 "struct-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2078 type, field_type, field_name);
2079 ret = -1;
2080 goto end;
2081 }
2082
2083 BT_LOGV("Added structure field type field: struct-ft-addr=%p, "
2084 "field-ft-addr=%p, field-name=\"%s\"", type,
2085 field_type, field_name);
2086 end:
2087 return ret;
2088 }
2089
2090 int64_t bt_ctf_field_type_structure_get_field_count(
2091 struct bt_ctf_field_type *type)
2092 {
2093 int64_t ret = 0;
2094 struct bt_ctf_field_type_structure *structure;
2095
2096 if (!type) {
2097 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2098 ret = (int64_t) -1;
2099 goto end;
2100 }
2101
2102 if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
2103 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2104 "addr=%p, ft-id=%s", type,
2105 bt_ctf_field_type_id_string(type->id));
2106 ret = (int64_t) -1;
2107 goto end;
2108 }
2109
2110 structure = container_of(type, struct bt_ctf_field_type_structure,
2111 parent);
2112 ret = (int64_t) structure->fields->len;
2113 end:
2114 return ret;
2115 }
2116
2117 int bt_ctf_field_type_structure_get_field_by_index(
2118 struct bt_ctf_field_type *type,
2119 const char **field_name, struct bt_ctf_field_type **field_type,
2120 uint64_t index)
2121 {
2122 struct bt_ctf_field_type_structure *structure;
2123 struct structure_field *field;
2124 int ret = 0;
2125
2126 if (!type) {
2127 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2128 ret = -1;
2129 goto end;
2130 }
2131
2132 if (type->id != BT_CTF_FIELD_TYPE_ID_STRUCT) {
2133 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2134 "addr=%p, ft-id=%s", type,
2135 bt_ctf_field_type_id_string(type->id));
2136 ret = -1;
2137 goto end;
2138 }
2139
2140 structure = container_of(type, struct bt_ctf_field_type_structure,
2141 parent);
2142 if (index >= structure->fields->len) {
2143 BT_LOGW("Invalid parameter: index is out of bounds: "
2144 "addr=%p, index=%" PRIu64 ", count=%u",
2145 type, index, structure->fields->len);
2146 ret = -1;
2147 goto end;
2148 }
2149
2150 field = g_ptr_array_index(structure->fields, index);
2151 if (field_type) {
2152 *field_type = field->type;
2153 bt_get(field->type);
2154 }
2155 if (field_name) {
2156 *field_name = g_quark_to_string(field->name);
2157 }
2158 end:
2159 return ret;
2160 }
2161
2162 struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name(
2163 struct bt_ctf_field_type *type,
2164 const char *name)
2165 {
2166 size_t index;
2167 GQuark name_quark;
2168 struct structure_field *field;
2169 struct bt_ctf_field_type_structure *structure;
2170 struct bt_ctf_field_type *field_type = NULL;
2171
2172 if (!type) {
2173 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2174 goto end;
2175 }
2176
2177 if (!name) {
2178 BT_LOGW_STR("Invalid parameter: name is NULL.");
2179 goto end;
2180 }
2181
2182 name_quark = g_quark_try_string(name);
2183 if (!name_quark) {
2184 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
2185 goto end;
2186 }
2187
2188 structure = container_of(type, struct bt_ctf_field_type_structure,
2189 parent);
2190 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
2191 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2192 goto end;
2193 }
2194
2195 field = structure->fields->pdata[index];
2196 field_type = field->type;
2197 bt_get(field_type);
2198 end:
2199 return field_type;
2200 }
2201
2202 struct bt_ctf_field_type *bt_ctf_field_type_variant_create(
2203 struct bt_ctf_field_type *enum_tag, const char *tag_name)
2204 {
2205 struct bt_ctf_field_type_variant *variant = NULL;
2206
2207 BT_LOGD("Creating variant field type object: "
2208 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2209 enum_tag, tag_name);
2210
2211 if (tag_name && bt_ctf_validate_identifier(tag_name)) {
2212 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2213 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2214 enum_tag, tag_name);
2215 goto error;
2216 }
2217
2218 variant = g_new0(struct bt_ctf_field_type_variant, 1);
2219 if (!variant) {
2220 BT_LOGE_STR("Failed to allocate one variant field type.");
2221 goto error;
2222 }
2223
2224 variant->parent.id = BT_CTF_FIELD_TYPE_ID_VARIANT;
2225 variant->tag_name = g_string_new(tag_name);
2226 variant->field_name_to_index = g_hash_table_new(NULL, NULL);
2227 variant->fields = g_ptr_array_new_with_free_func(
2228 (GDestroyNotify) destroy_structure_field);
2229 if (enum_tag) {
2230 bt_get(enum_tag);
2231 variant->tag = container_of(enum_tag,
2232 struct bt_ctf_field_type_enumeration, parent);
2233 }
2234
2235 bt_ctf_field_type_init(&variant->parent, TRUE);
2236 /* A variant's alignment is undefined */
2237 variant->parent.alignment = 0;
2238 BT_LOGD("Created variant field type object: addr=%p, "
2239 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2240 &variant->parent, enum_tag, tag_name);
2241 return &variant->parent;
2242 error:
2243 return NULL;
2244 }
2245
2246 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type(
2247 struct bt_ctf_field_type *type)
2248 {
2249 struct bt_ctf_field_type_variant *variant;
2250 struct bt_ctf_field_type *tag_type = NULL;
2251
2252 if (!type) {
2253 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2254 goto end;
2255 }
2256
2257 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2258 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2259 "addr=%p, ft-id=%s", type,
2260 bt_ctf_field_type_id_string(type->id));
2261 goto end;
2262 }
2263
2264 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
2265 if (!variant->tag) {
2266 BT_LOGV("Variant field type has no tag field type: "
2267 "addr=%p", type);
2268 goto end;
2269 }
2270
2271 tag_type = &variant->tag->parent;
2272 bt_get(tag_type);
2273 end:
2274 return tag_type;
2275 }
2276
2277 const char *bt_ctf_field_type_variant_get_tag_name(
2278 struct bt_ctf_field_type *type)
2279 {
2280 struct bt_ctf_field_type_variant *variant;
2281 const char *tag_name = NULL;
2282
2283 if (!type) {
2284 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2285 goto end;
2286 }
2287
2288 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2289 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2290 "addr=%p, ft-id=%s", type,
2291 bt_ctf_field_type_id_string(type->id));
2292 goto end;
2293 }
2294
2295 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
2296 if (variant->tag_name->len == 0) {
2297 BT_LOGV("Variant field type has no tag field name: "
2298 "addr=%p", type);
2299 goto end;
2300 }
2301
2302 tag_name = variant->tag_name->str;
2303 end:
2304 return tag_name;
2305 }
2306
2307 int bt_ctf_field_type_variant_set_tag_name(
2308 struct bt_ctf_field_type *type, const char *name)
2309 {
2310 int ret = 0;
2311 struct bt_ctf_field_type_variant *variant;
2312
2313 if (!type) {
2314 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2315 ret = -1;
2316 goto end;
2317 }
2318
2319 if (type->frozen) {
2320 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2321 type);
2322 ret = -1;
2323 goto end;
2324 }
2325
2326 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2327 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2328 "addr=%p, ft-id=%s", type,
2329 bt_ctf_field_type_id_string(type->id));
2330 ret = -1;
2331 goto end;
2332 }
2333
2334 if (bt_ctf_validate_identifier(name)) {
2335 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2336 "variant-ft-addr=%p, tag-field-name=\"%s\"",
2337 type, name);
2338 ret = -1;
2339 goto end;
2340 }
2341
2342 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
2343 g_string_assign(variant->tag_name, name);
2344 BT_LOGV("Set variant field type's tag field name: addr=%p, "
2345 "tag-field-name=\"%s\"", type, name);
2346 end:
2347 return ret;
2348 }
2349
2350 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
2351 struct bt_ctf_field_type *field_type,
2352 const char *field_name)
2353 {
2354 size_t i;
2355 int ret = 0;
2356 struct bt_ctf_field_type_variant *variant;
2357 GQuark field_name_quark = g_quark_from_string(field_name);
2358
2359 /*
2360 * TODO: check that `field_type` does not contain `type`,
2361 * recursively.
2362 */
2363 if (!type) {
2364 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2365 ret = -1;
2366 goto end;
2367 }
2368
2369 if (type->frozen) {
2370 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2371 type);
2372 ret = -1;
2373 goto end;
2374 }
2375
2376 if (bt_ctf_validate_identifier(field_name)) {
2377 BT_LOGW("Invalid parameter: field name is not a valid CTF identifier: "
2378 "variant-ft-addr=%p, field-name=\"%s\"",
2379 type, field_name);
2380 ret = -1;
2381 goto end;
2382 }
2383
2384 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2385 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2386 "addr=%p, ft-id=%s", type,
2387 bt_ctf_field_type_id_string(type->id));
2388 ret = -1;
2389 goto end;
2390 }
2391
2392 if (type == field_type) {
2393 BT_LOGW("Invalid parameter: variant field type and field type to add are the same: "
2394 "addr=%p", type);
2395 ret = -1;
2396 goto end;
2397 }
2398
2399 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
2400
2401 /* The user has explicitly provided a tag; validate against it. */
2402 if (variant->tag) {
2403 int name_found = 0;
2404
2405 /* Make sure this name is present in the enum tag */
2406 for (i = 0; i < variant->tag->entries->len; i++) {
2407 struct enumeration_mapping *mapping =
2408 g_ptr_array_index(variant->tag->entries, i);
2409
2410 if (mapping->string == field_name_quark) {
2411 name_found = 1;
2412 break;
2413 }
2414 }
2415
2416 if (!name_found) {
2417 /* Validation failed */
2418 BT_LOGW("Invalid parameter: field name does not name a tag field type's mapping: "
2419 "variant-ft-addr=%p, tag-ft-addr=%p, "
2420 "tag-field-name=\"%s\""
2421 "field-ft-addr=%p, field-name=\"%s\"",
2422 type, variant->tag, variant->tag_name->str,
2423 field_type, field_name);
2424 ret = -1;
2425 goto end;
2426 }
2427 }
2428
2429 if (add_structure_field(variant->fields, variant->field_name_to_index,
2430 field_type, field_name)) {
2431 BT_LOGW("Cannot add field to variant field type: "
2432 "variant-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2433 type, field_type, field_name);
2434 ret = -1;
2435 goto end;
2436 }
2437
2438 BT_LOGV("Added variant field type field: variant-ft-addr=%p, "
2439 "field-ft-addr=%p, field-name=\"%s\"", type,
2440 field_type, field_name);
2441
2442 end:
2443 return ret;
2444 }
2445
2446 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name(
2447 struct bt_ctf_field_type *type,
2448 const char *field_name)
2449 {
2450 size_t index;
2451 GQuark name_quark;
2452 struct structure_field *field;
2453 struct bt_ctf_field_type_variant *variant;
2454 struct bt_ctf_field_type *field_type = NULL;
2455
2456 if (!type) {
2457 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2458 goto end;
2459 }
2460
2461 if (!field_name) {
2462 BT_LOGW_STR("Invalid parameter: field name is NULL.");
2463 goto end;
2464 }
2465
2466 name_quark = g_quark_try_string(field_name);
2467 if (!name_quark) {
2468 BT_LOGE("Cannot get GQuark: string=\"%s\"", field_name);
2469 goto end;
2470 }
2471
2472 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
2473 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
2474 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2475 goto end;
2476 }
2477
2478 field = g_ptr_array_index(variant->fields, index);
2479 field_type = field->type;
2480 bt_get(field_type);
2481 end:
2482 return field_type;
2483 }
2484
2485 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
2486 struct bt_ctf_field_type *type,
2487 struct bt_ctf_field *tag)
2488 {
2489 int ret;
2490 const char *enum_value;
2491 struct bt_ctf_field_type *field_type = NULL;
2492 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
2493
2494 if (!type) {
2495 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
2496 goto end;
2497 }
2498
2499 if (!tag) {
2500 BT_LOGW_STR("Invalid parameter: tag field is NULL.");
2501 goto end;
2502 }
2503
2504 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2505 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2506 "addr=%p, ft-id=%s", type,
2507 bt_ctf_field_type_id_string(type->id));
2508 goto end;
2509 }
2510
2511 iter = bt_ctf_field_enumeration_get_mappings(tag);
2512 if (!iter) {
2513 BT_LOGE("Cannot get enumeration field type mapping iterator from enumeration field: "
2514 "enum-field-addr=%p", tag);
2515 goto end;
2516 }
2517
2518 ret = bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter,
2519 &enum_value, NULL, NULL);
2520 if (ret) {
2521 BT_LOGW("Cannot get enumeration field type mapping iterator's current mapping: "
2522 "iter-addr=%p", iter);
2523 goto end;
2524 }
2525
2526 field_type = bt_ctf_field_type_variant_get_field_type_by_name(
2527 type, enum_value);
2528 end:
2529 bt_put(iter);
2530 return field_type;
2531 }
2532
2533 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type)
2534 {
2535 int64_t ret = 0;
2536 struct bt_ctf_field_type_variant *variant;
2537
2538 if (!type) {
2539 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2540 ret = (int64_t) -1;
2541 goto end;
2542 }
2543
2544 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2545 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2546 "addr=%p, ft-id=%s", type,
2547 bt_ctf_field_type_id_string(type->id));
2548 ret = (int64_t) -1;
2549 goto end;
2550 }
2551
2552 variant = container_of(type, struct bt_ctf_field_type_variant,
2553 parent);
2554 ret = (int64_t) variant->fields->len;
2555 end:
2556 return ret;
2557
2558 }
2559
2560 int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type *type,
2561 const char **field_name, struct bt_ctf_field_type **field_type,
2562 uint64_t index)
2563 {
2564 struct bt_ctf_field_type_variant *variant;
2565 struct structure_field *field;
2566 int ret = 0;
2567
2568 if (!type) {
2569 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2570 ret = -1;
2571 goto end;
2572 }
2573
2574 if (type->id != BT_CTF_FIELD_TYPE_ID_VARIANT) {
2575 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2576 "addr=%p, ft-id=%s", type,
2577 bt_ctf_field_type_id_string(type->id));
2578 ret = -1;
2579 goto end;
2580 }
2581
2582 variant = container_of(type, struct bt_ctf_field_type_variant,
2583 parent);
2584 if (index >= variant->fields->len) {
2585 BT_LOGW("Invalid parameter: index is out of bounds: "
2586 "addr=%p, index=%" PRIu64 ", count=%u",
2587 type, index, variant->fields->len);
2588 ret = -1;
2589 goto end;
2590 }
2591
2592 field = g_ptr_array_index(variant->fields, index);
2593 if (field_type) {
2594 *field_type = field->type;
2595 bt_get(field->type);
2596 }
2597 if (field_name) {
2598 *field_name = g_quark_to_string(field->name);
2599 }
2600 end:
2601 return ret;
2602 }
2603
2604 struct bt_ctf_field_type *bt_ctf_field_type_array_create(
2605 struct bt_ctf_field_type *element_type,
2606 unsigned int length)
2607 {
2608 struct bt_ctf_field_type_array *array = NULL;
2609
2610 BT_LOGD("Creating array field type object: element-ft-addr=%p, "
2611 "length=%u", element_type, length);
2612
2613 if (!element_type) {
2614 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2615 goto error;
2616 }
2617
2618 if (length == 0) {
2619 BT_LOGW_STR("Invalid parameter: length is zero.");
2620 goto error;
2621 }
2622
2623 array = g_new0(struct bt_ctf_field_type_array, 1);
2624 if (!array) {
2625 BT_LOGE_STR("Failed to allocate one array field type.");
2626 goto error;
2627 }
2628
2629 array->parent.id = BT_CTF_FIELD_TYPE_ID_ARRAY;
2630 bt_get(element_type);
2631 array->element_type = element_type;
2632 array->length = length;
2633 bt_ctf_field_type_init(&array->parent, FALSE);
2634 BT_LOGD("Created array field type object: addr=%p, "
2635 "element-ft-addr=%p, length=%u",
2636 &array->parent, element_type, length);
2637 return &array->parent;
2638 error:
2639 return NULL;
2640 }
2641
2642 struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
2643 struct bt_ctf_field_type *type)
2644 {
2645 struct bt_ctf_field_type *ret = NULL;
2646 struct bt_ctf_field_type_array *array;
2647
2648 if (!type) {
2649 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2650 goto end;
2651 }
2652
2653 if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) {
2654 BT_LOGW("Invalid parameter: field type is not an array field type: "
2655 "addr=%p, ft-id=%s", type,
2656 bt_ctf_field_type_id_string(type->id));
2657 goto end;
2658 }
2659
2660 array = container_of(type, struct bt_ctf_field_type_array, parent);
2661 ret = array->element_type;
2662 bt_get(ret);
2663 end:
2664 return ret;
2665 }
2666
2667 BT_HIDDEN
2668 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type,
2669 struct bt_ctf_field_type *element_type)
2670 {
2671 int ret = 0;
2672 struct bt_ctf_field_type_array *array;
2673
2674 if (!type) {
2675 BT_LOGW_STR("Invalid parameter: array field type is NULL.");
2676 ret = -1;
2677 goto end;
2678 }
2679
2680 if (!element_type) {
2681 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2682 ret = -1;
2683 goto end;
2684 }
2685
2686 if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) {
2687 BT_LOGW("Invalid parameter: field type is not an array field type: "
2688 "addr=%p, ft-id=%s", type,
2689 bt_ctf_field_type_id_string(type->id));
2690 ret = -1;
2691 goto end;
2692 }
2693
2694 array = container_of(type, struct bt_ctf_field_type_array, parent);
2695
2696 if (array->element_type) {
2697 BT_PUT(array->element_type);
2698 }
2699
2700 array->element_type = element_type;
2701 bt_get(array->element_type);
2702 BT_LOGV("Set array field type's element field type: array-ft-addr=%p, "
2703 "element-ft-addr=%p", type, element_type);
2704
2705 end:
2706 return ret;
2707 }
2708
2709 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type)
2710 {
2711 int64_t ret;
2712 struct bt_ctf_field_type_array *array;
2713
2714 if (!type) {
2715 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2716 ret = (int64_t) -1;
2717 goto end;
2718 }
2719
2720 if (type->id != BT_CTF_FIELD_TYPE_ID_ARRAY) {
2721 BT_LOGW("Invalid parameter: field type is not an array field type: "
2722 "addr=%p, ft-id=%s", type,
2723 bt_ctf_field_type_id_string(type->id));
2724 ret = (int64_t) -1;
2725 goto end;
2726 }
2727
2728 array = container_of(type, struct bt_ctf_field_type_array, parent);
2729 ret = (int64_t) array->length;
2730 end:
2731 return ret;
2732 }
2733
2734 struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(
2735 struct bt_ctf_field_type *element_type,
2736 const char *length_field_name)
2737 {
2738 struct bt_ctf_field_type_sequence *sequence = NULL;
2739
2740 BT_LOGD("Creating sequence field type object: element-ft-addr=%p, "
2741 "length-field-name=\"%s\"", element_type, length_field_name);
2742
2743 if (!element_type) {
2744 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2745 goto error;
2746 }
2747
2748 if (bt_ctf_validate_identifier(length_field_name)) {
2749 BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: "
2750 "length-field-name=\"%s\"", length_field_name);
2751 goto error;
2752 }
2753
2754 sequence = g_new0(struct bt_ctf_field_type_sequence, 1);
2755 if (!sequence) {
2756 BT_LOGE_STR("Failed to allocate one sequence field type.");
2757 goto error;
2758 }
2759
2760 sequence->parent.id = BT_CTF_FIELD_TYPE_ID_SEQUENCE;
2761 bt_get(element_type);
2762 sequence->element_type = element_type;
2763 sequence->length_field_name = g_string_new(length_field_name);
2764 bt_ctf_field_type_init(&sequence->parent, FALSE);
2765 BT_LOGD("Created sequence field type object: addr=%p, "
2766 "element-ft-addr=%p, length-field-name=\"%s\"",
2767 &sequence->parent, element_type, length_field_name);
2768 return &sequence->parent;
2769 error:
2770 return NULL;
2771 }
2772
2773 struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
2774 struct bt_ctf_field_type *type)
2775 {
2776 struct bt_ctf_field_type *ret = NULL;
2777 struct bt_ctf_field_type_sequence *sequence;
2778
2779 if (!type) {
2780 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2781 goto end;
2782 }
2783
2784 if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
2785 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2786 "addr=%p, ft-id=%s", type,
2787 bt_ctf_field_type_id_string(type->id));
2788 goto end;
2789 }
2790
2791 sequence = container_of(type, struct bt_ctf_field_type_sequence,
2792 parent);
2793 ret = sequence->element_type;
2794 bt_get(ret);
2795 end:
2796 return ret;
2797 }
2798
2799 BT_HIDDEN
2800 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type,
2801 struct bt_ctf_field_type *element_type)
2802 {
2803 int ret = 0;
2804 struct bt_ctf_field_type_sequence *sequence;
2805
2806 if (!type) {
2807 BT_LOGW_STR("Invalid parameter: sequence field type is NULL.");
2808 ret = -1;
2809 goto end;
2810 }
2811
2812 if (!element_type) {
2813 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2814 ret = -1;
2815 goto end;
2816 }
2817
2818 if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
2819 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2820 "addr=%p, ft-id=%s", type,
2821 bt_ctf_field_type_id_string(type->id));
2822 ret = -1;
2823 goto end;
2824 }
2825
2826 sequence = container_of(type, struct bt_ctf_field_type_sequence, parent);
2827 if (sequence->element_type) {
2828 BT_PUT(sequence->element_type);
2829 }
2830
2831 sequence->element_type = element_type;
2832 bt_get(sequence->element_type);
2833 BT_LOGV("Set sequence field type's element field type: sequence-ft-addr=%p, "
2834 "element-ft-addr=%p", type, element_type);
2835
2836 end:
2837 return ret;
2838 }
2839
2840 const char *bt_ctf_field_type_sequence_get_length_field_name(
2841 struct bt_ctf_field_type *type)
2842 {
2843 const char *ret = NULL;
2844 struct bt_ctf_field_type_sequence *sequence;
2845
2846 if (!type) {
2847 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2848 goto end;
2849 }
2850
2851 if (type->id != BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
2852 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
2853 "addr=%p, ft-id=%s", type,
2854 bt_ctf_field_type_id_string(type->id));
2855 goto end;
2856 }
2857
2858 sequence = container_of(type, struct bt_ctf_field_type_sequence,
2859 parent);
2860 ret = sequence->length_field_name->str;
2861 end:
2862 return ret;
2863 }
2864
2865 struct bt_ctf_field_type *bt_ctf_field_type_string_create(void)
2866 {
2867 struct bt_ctf_field_type_string *string =
2868 g_new0(struct bt_ctf_field_type_string, 1);
2869
2870 BT_LOGD_STR("Creating string field type object.");
2871
2872 if (!string) {
2873 BT_LOGE_STR("Failed to allocate one string field type.");
2874 return NULL;
2875 }
2876
2877 string->parent.id = BT_CTF_FIELD_TYPE_ID_STRING;
2878 bt_ctf_field_type_init(&string->parent, TRUE);
2879 string->encoding = BT_CTF_STRING_ENCODING_UTF8;
2880 string->parent.alignment = CHAR_BIT;
2881 BT_LOGD("Created string field type object: addr=%p", &string->parent);
2882 return &string->parent;
2883 }
2884
2885 enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding(
2886 struct bt_ctf_field_type *type)
2887 {
2888 struct bt_ctf_field_type_string *string;
2889 enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN;
2890
2891 if (!type) {
2892 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2893 goto end;
2894 }
2895
2896 if (type->id != BT_CTF_FIELD_TYPE_ID_STRING) {
2897 BT_LOGW("Invalid parameter: field type is not a string field type: "
2898 "addr=%p, ft-id=%s", type,
2899 bt_ctf_field_type_id_string(type->id));
2900 goto end;
2901 }
2902
2903 string = container_of(type, struct bt_ctf_field_type_string,
2904 parent);
2905 ret = string->encoding;
2906 end:
2907 return ret;
2908 }
2909
2910 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type,
2911 enum bt_ctf_string_encoding encoding)
2912 {
2913 int ret = 0;
2914 struct bt_ctf_field_type_string *string;
2915
2916 if (!type) {
2917 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2918 ret = -1;
2919 goto end;
2920 }
2921
2922 if (type->id != BT_CTF_FIELD_TYPE_ID_STRING) {
2923 BT_LOGW("Invalid parameter: field type is not a string field type: "
2924 "addr=%p, ft-id=%s", type,
2925 bt_ctf_field_type_id_string(type->id));
2926 ret = -1;
2927 goto end;
2928 }
2929
2930 if ((encoding != BT_CTF_STRING_ENCODING_UTF8 &&
2931 encoding != BT_CTF_STRING_ENCODING_ASCII)) {
2932 BT_LOGW("Invalid parameter: unknown string encoding: "
2933 "addr=%p, encoding=%d", type, encoding);
2934 ret = -1;
2935 goto end;
2936 }
2937
2938 string = container_of(type, struct bt_ctf_field_type_string, parent);
2939 string->encoding = encoding;
2940 BT_LOGV("Set string field type's encoding: addr=%p, encoding=%s",
2941 type, bt_ctf_string_encoding_string(encoding));
2942 end:
2943 return ret;
2944 }
2945
2946 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type)
2947 {
2948 int ret;
2949 enum bt_ctf_field_type_id type_id;
2950
2951 if (!type) {
2952 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2953 ret = -1;
2954 goto end;
2955 }
2956
2957 if (type->frozen) {
2958 ret = (int) type->alignment;
2959 goto end;
2960 }
2961
2962 type_id = bt_ctf_field_type_get_type_id(type);
2963 switch (type_id) {
2964 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
2965 {
2966 struct bt_ctf_field_type *element =
2967 bt_ctf_field_type_sequence_get_element_type(type);
2968
2969 assert(element);
2970 ret = bt_ctf_field_type_get_alignment(element);
2971 bt_put(element);
2972 break;
2973 }
2974 case BT_CTF_FIELD_TYPE_ID_ARRAY:
2975 {
2976 struct bt_ctf_field_type *element =
2977 bt_ctf_field_type_array_get_element_type(type);
2978
2979 assert(element);
2980 ret = bt_ctf_field_type_get_alignment(element);
2981 bt_put(element);
2982 break;
2983 }
2984 case BT_CTF_FIELD_TYPE_ID_STRUCT:
2985 {
2986 int64_t i, element_count;
2987
2988 element_count = bt_ctf_field_type_structure_get_field_count(
2989 type);
2990 assert(element_count >= 0);
2991
2992 for (i = 0; i < element_count; i++) {
2993 struct bt_ctf_field_type *field;
2994 int field_alignment;
2995
2996 ret = bt_ctf_field_type_structure_get_field_by_index(
2997 type, NULL, &field, i);
2998 assert(ret == 0);
2999 assert(field);
3000 field_alignment = bt_ctf_field_type_get_alignment(
3001 field);
3002 bt_put(field);
3003 if (field_alignment < 0) {
3004 ret = field_alignment;
3005 goto end;
3006 }
3007
3008 type->alignment = MAX(field_alignment, type->alignment);
3009 }
3010 ret = (int) type->alignment;
3011 break;
3012 }
3013 case BT_CTF_FIELD_TYPE_ID_UNKNOWN:
3014 BT_LOGW("Invalid parameter: unknown field type ID: "
3015 "addr=%p, ft-id=%d", type, type_id);
3016 ret = -1;
3017 break;
3018 default:
3019 ret = (int) type->alignment;
3020 break;
3021 }
3022 end:
3023 return ret;
3024 }
3025
3026 static inline
3027 int is_power_of_two(unsigned int value)
3028 {
3029 return ((value & (value - 1)) == 0) && value > 0;
3030 }
3031
3032 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type,
3033 unsigned int alignment)
3034 {
3035 int ret = 0;
3036 enum bt_ctf_field_type_id type_id;
3037
3038 /* Alignment must be a power of two */
3039 if (!type) {
3040 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3041 ret = -1;
3042 goto end;
3043 }
3044
3045 if (type->frozen) {
3046 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
3047 type);
3048 ret = -1;
3049 goto end;
3050 }
3051
3052 if (!is_power_of_two(alignment)) {
3053 BT_LOGW("Invalid parameter: alignment is not a power of two: "
3054 "addr=%p, align=%u", type, alignment);
3055 ret = -1;
3056 goto end;
3057 }
3058
3059 type_id = bt_ctf_field_type_get_type_id(type);
3060 if (type_id == BT_CTF_FIELD_TYPE_ID_UNKNOWN) {
3061 BT_LOGW("Invalid parameter: unknown field type ID: "
3062 "addr=%p, ft-id=%d", type, type_id);
3063 ret = -1;
3064 goto end;
3065 }
3066
3067 if (type->id == BT_CTF_FIELD_TYPE_ID_STRING &&
3068 alignment != CHAR_BIT) {
3069 BT_LOGW("Invalid parameter: alignment must be %u for a string field type: "
3070 "addr=%p, align=%u", CHAR_BIT, type, alignment);
3071 ret = -1;
3072 goto end;
3073 }
3074
3075 if (type_id == BT_CTF_FIELD_TYPE_ID_VARIANT ||
3076 type_id == BT_CTF_FIELD_TYPE_ID_SEQUENCE ||
3077 type_id == BT_CTF_FIELD_TYPE_ID_ARRAY) {
3078 /* Setting an alignment on these types makes no sense */
3079 BT_LOGW("Invalid parameter: cannot set the alignment of this field type: "
3080 "addr=%p, ft-id=%s", type,
3081 bt_ctf_field_type_id_string(type->id));
3082 ret = -1;
3083 goto end;
3084 }
3085
3086 type->alignment = alignment;
3087 ret = 0;
3088 BT_LOGV("Set field type's alignment: addr=%p, align=%u",
3089 type, alignment);
3090 end:
3091 return ret;
3092 }
3093
3094 enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(
3095 struct bt_ctf_field_type *type)
3096 {
3097 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
3098
3099 if (!type) {
3100 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3101 goto end;
3102 }
3103
3104 switch (type->id) {
3105 case BT_CTF_FIELD_TYPE_ID_INTEGER:
3106 {
3107 struct bt_ctf_field_type_integer *integer = container_of(
3108 type, struct bt_ctf_field_type_integer, parent);
3109 ret = integer->user_byte_order;
3110 break;
3111 }
3112 case BT_CTF_FIELD_TYPE_ID_ENUM:
3113 {
3114 struct bt_ctf_field_type_enumeration *enum_ft = container_of(
3115 type, struct bt_ctf_field_type_enumeration, parent);
3116 ret = bt_ctf_field_type_get_byte_order(enum_ft->container);
3117 break;
3118 }
3119 case BT_CTF_FIELD_TYPE_ID_FLOAT:
3120 {
3121 struct bt_ctf_field_type_floating_point *floating_point =
3122 container_of(type,
3123 struct bt_ctf_field_type_floating_point,
3124 parent);
3125 ret = floating_point->user_byte_order;
3126 break;
3127 }
3128 default:
3129 BT_LOGW("Invalid parameter: cannot get the byte order of this field type: "
3130 "addr=%p, ft-id=%s", type,
3131 bt_ctf_field_type_id_string(type->id));
3132 goto end;
3133 }
3134
3135 assert(ret == BT_CTF_BYTE_ORDER_NATIVE ||
3136 ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
3137 ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
3138 ret == BT_CTF_BYTE_ORDER_NETWORK);
3139
3140 end:
3141 return ret;
3142 }
3143
3144 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type,
3145 enum bt_ctf_byte_order byte_order)
3146 {
3147 int ret = 0;
3148
3149 if (!type) {
3150 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3151 ret = -1;
3152 goto end;
3153 }
3154
3155 if (type->frozen) {
3156 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
3157 type);
3158 ret = -1;
3159 goto end;
3160 }
3161
3162 if (byte_order != BT_CTF_BYTE_ORDER_NATIVE &&
3163 byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
3164 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
3165 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
3166 BT_LOGW("Invalid parameter: unknown byte order: "
3167 "addr=%p, bo=%s", type,
3168 bt_ctf_byte_order_string(byte_order));
3169 ret = -1;
3170 goto end;
3171 }
3172
3173 if (set_byte_order_funcs[type->id]) {
3174 set_byte_order_funcs[type->id](type, byte_order);
3175 }
3176
3177 BT_LOGV("Set field type's byte order: addr=%p, bo=%s",
3178 type, bt_ctf_byte_order_string(byte_order));
3179
3180 end:
3181 return ret;
3182 }
3183
3184 enum bt_ctf_field_type_id bt_ctf_field_type_get_type_id(
3185 struct bt_ctf_field_type *type)
3186 {
3187 if (!type) {
3188 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3189 return BT_CTF_FIELD_TYPE_ID_UNKNOWN;
3190 }
3191
3192 return type->id;
3193 }
3194
3195 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type)
3196 {
3197 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_INTEGER;
3198 }
3199
3200 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type)
3201 {
3202 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_FLOAT;
3203 }
3204
3205 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type)
3206 {
3207 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_ENUM;
3208 }
3209
3210 int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type)
3211 {
3212 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_STRING;
3213 }
3214
3215 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type)
3216 {
3217 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_STRUCT;
3218 }
3219
3220 int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type)
3221 {
3222 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_ARRAY;
3223 }
3224
3225 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type)
3226 {
3227 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_SEQUENCE;
3228 }
3229
3230 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type)
3231 {
3232 return bt_ctf_field_type_get_type_id(type) == BT_CTF_FIELD_TYPE_ID_VARIANT;
3233 }
3234
3235 /* Pre-2.0 CTF writer backward compatibility */
3236 void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
3237 {
3238 bt_get(type);
3239 }
3240
3241 /* Pre-2.0 CTF writer backward compatibility */
3242 void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
3243 {
3244 bt_put(type);
3245 }
3246
3247 BT_HIDDEN
3248 void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
3249 {
3250 if (!type || type->frozen) {
3251 return;
3252 }
3253
3254 BT_LOGD("Freezing field type: addr=%p", type);
3255 type->freeze(type);
3256 }
3257
3258 BT_HIDDEN
3259 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
3260 struct bt_ctf_field_type_variant *variant,
3261 int64_t tag_value)
3262 {
3263 struct bt_ctf_field_type *type = NULL;
3264 GQuark field_name_quark;
3265 gpointer index;
3266 struct structure_field *field_entry;
3267 struct range_overlap_query query = {
3268 .range_start._signed = tag_value,
3269 .range_end._signed = tag_value,
3270 .mapping_name = 0,
3271 .overlaps = 0,
3272 };
3273
3274 g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap,
3275 &query);
3276 if (!query.overlaps) {
3277 goto end;
3278 }
3279
3280 field_name_quark = query.mapping_name;
3281 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
3282 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
3283 goto end;
3284 }
3285
3286 field_entry = g_ptr_array_index(variant->fields, (size_t) index);
3287 type = field_entry->type;
3288 end:
3289 return type;
3290 }
3291
3292 BT_HIDDEN
3293 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
3294 struct bt_ctf_field_type_variant *variant,
3295 uint64_t tag_value)
3296 {
3297 struct bt_ctf_field_type *type = NULL;
3298 GQuark field_name_quark;
3299 gpointer index;
3300 struct structure_field *field_entry;
3301 struct range_overlap_query query = {
3302 .range_start._unsigned = tag_value,
3303 .range_end._unsigned = tag_value,
3304 .mapping_name = 0,
3305 .overlaps = 0,
3306 };
3307
3308 g_ptr_array_foreach(variant->tag->entries,
3309 check_ranges_overlap_unsigned,
3310 &query);
3311 if (!query.overlaps) {
3312 goto end;
3313 }
3314
3315 field_name_quark = query.mapping_name;
3316 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
3317 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
3318 goto end;
3319 }
3320
3321 field_entry = g_ptr_array_index(variant->fields, (size_t)index);
3322 type = field_entry->type;
3323 end:
3324 return type;
3325 }
3326
3327 BT_HIDDEN
3328 int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
3329 struct metadata_context *context)
3330 {
3331 int ret;
3332
3333 assert(type);
3334 assert(context);
3335
3336 BT_LOGD("Serializing field type's metadata: "
3337 "ft-addr=%p, metadata-context-addr=%p", type, context);
3338
3339 /* Make sure field type is valid before serializing it */
3340 ret = bt_ctf_field_type_validate(type);
3341 if (ret) {
3342 BT_LOGW_STR("Cannot serialize field type's metadata: field type is invalid.");
3343 goto end;
3344 }
3345
3346 ret = type->serialize(type, context);
3347 end:
3348 return ret;
3349 }
3350
3351 struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type)
3352 {
3353 struct bt_ctf_field_type *copy = NULL;
3354
3355 BT_LOGD("Copying field type object: addr=%p", type);
3356
3357 if (!type) {
3358 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3359 goto end;
3360 }
3361
3362 copy = type_copy_funcs[type->id](type);
3363 if (!copy) {
3364 BT_LOGE_STR("Cannot copy field type.");
3365 goto end;
3366 }
3367
3368 copy->alignment = type->alignment;
3369 BT_LOGD("Copied field type object: copy-ft-addr=%p", copy);
3370 end:
3371 return copy;
3372 }
3373
3374 BT_HIDDEN
3375 int bt_ctf_field_type_structure_get_field_name_index(
3376 struct bt_ctf_field_type *type, const char *name)
3377 {
3378 int ret;
3379 size_t index;
3380 GQuark name_quark;
3381 struct bt_ctf_field_type_structure *structure;
3382
3383 if (!type) {
3384 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3385 ret = -1;
3386 goto end;
3387 }
3388
3389 if (!name) {
3390 BT_LOGW_STR("Invalid parameter: field name is NULL.");
3391 ret = -1;
3392 goto end;
3393 }
3394
3395 if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_STRUCT) {
3396 BT_LOGW("Invalid parameter: field type is not a structure field type: "
3397 "addr=%p, ft-id=%s", type,
3398 bt_ctf_field_type_id_string(type->id));
3399 ret = -1;
3400 goto end;
3401 }
3402
3403 name_quark = g_quark_try_string(name);
3404 if (!name_quark) {
3405 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
3406 ret = -1;
3407 goto end;
3408 }
3409
3410 structure = container_of(type, struct bt_ctf_field_type_structure,
3411 parent);
3412 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
3413 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
3414 ret = -1;
3415 goto end;
3416 }
3417 ret = (int) index;
3418 end:
3419 return ret;
3420 }
3421
3422 BT_HIDDEN
3423 int bt_ctf_field_type_variant_get_field_name_index(
3424 struct bt_ctf_field_type *type, const char *name)
3425 {
3426 int ret;
3427 size_t index;
3428 GQuark name_quark;
3429 struct bt_ctf_field_type_variant *variant;
3430
3431 if (!type) {
3432 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
3433 ret = -1;
3434 goto end;
3435 }
3436
3437 if (!name) {
3438 BT_LOGW_STR("Invalid parameter: field name is NULL.");
3439 ret = -1;
3440 goto end;
3441 }
3442
3443 if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_VARIANT) {
3444 BT_LOGW("Invalid parameter: field type is not a variant field type: "
3445 "addr=%p, ft-id=%s", type,
3446 bt_ctf_field_type_id_string(type->id));
3447 ret = -1;
3448 goto end;
3449 }
3450
3451 name_quark = g_quark_try_string(name);
3452 if (!name_quark) {
3453 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
3454 ret = -1;
3455 goto end;
3456 }
3457
3458 variant = container_of(type, struct bt_ctf_field_type_variant,
3459 parent);
3460 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
3461 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
3462 ret = -1;
3463 goto end;
3464 }
3465 ret = (int) index;
3466 end:
3467 return ret;
3468 }
3469
3470 BT_HIDDEN
3471 int bt_ctf_field_type_sequence_set_length_field_path(
3472 struct bt_ctf_field_type *type,
3473 struct bt_ctf_field_path *path)
3474 {
3475 int ret = 0;
3476 struct bt_ctf_field_type_sequence *sequence;
3477
3478 if (!type) {
3479 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3480 ret = -1;
3481 goto end;
3482 }
3483
3484 if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
3485 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
3486 "addr=%p, ft-id=%s", type,
3487 bt_ctf_field_type_id_string(type->id));
3488 ret = -1;
3489 goto end;
3490 }
3491
3492 sequence = container_of(type, struct bt_ctf_field_type_sequence,
3493 parent);
3494 bt_get(path);
3495 BT_MOVE(sequence->length_field_path, path);
3496 BT_LOGV("Set sequence field type's length field path: ft-addr=%p, "
3497 "field-path-addr=%p", type, path);
3498 end:
3499 return ret;
3500 }
3501
3502 BT_HIDDEN
3503 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
3504 struct bt_ctf_field_path *path)
3505 {
3506 int ret = 0;
3507 struct bt_ctf_field_type_variant *variant;
3508
3509 if (!type) {
3510 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3511 ret = -1;
3512 goto end;
3513 }
3514
3515 if (bt_ctf_field_type_get_type_id(type) != BT_CTF_FIELD_TYPE_ID_VARIANT) {
3516 BT_LOGW("Invalid parameter: field type is not a variant field type: "
3517 "addr=%p, ft-id=%s", type,
3518 bt_ctf_field_type_id_string(type->id));
3519 ret = -1;
3520 goto end;
3521 }
3522
3523 variant = container_of(type, struct bt_ctf_field_type_variant,
3524 parent);
3525 bt_get(path);
3526 BT_MOVE(variant->tag_field_path, path);
3527 BT_LOGV("Set variant field type's tag field path: ft-addr=%p, "
3528 "field-path-addr=%p", type, path);
3529 end:
3530 return ret;
3531 }
3532
3533 BT_HIDDEN
3534 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
3535 struct bt_ctf_field_type *tag)
3536 {
3537 int ret = 0;
3538 struct bt_ctf_field_type_variant *variant;
3539
3540 if (!type) {
3541 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
3542 ret = -1;
3543 goto end;
3544 }
3545
3546 if (!tag) {
3547 BT_LOGW_STR("Invalid parameter: tag field type is NULL.");
3548 ret = -1;
3549 goto end;
3550 }
3551
3552 if (bt_ctf_field_type_get_type_id(tag) != BT_CTF_FIELD_TYPE_ID_ENUM) {
3553 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
3554 "addr=%p, ft-id=%s", type,
3555 bt_ctf_field_type_id_string(type->id));
3556 ret = -1;
3557 goto end;
3558 }
3559
3560 variant = container_of(type, struct bt_ctf_field_type_variant,
3561 parent);
3562 bt_get(tag);
3563 if (variant->tag) {
3564 bt_put(&variant->tag->parent);
3565 }
3566 variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration,
3567 parent);
3568 BT_LOGV("Set variant field type's tag field type: variant-ft-addr=%p, "
3569 "tag-ft-addr=%p", type, tag);
3570 end:
3571 return ret;
3572 }
3573
3574 static
3575 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type)
3576 {
3577 struct bt_ctf_field_type_integer *integer =
3578 (struct bt_ctf_field_type_integer *) type;
3579
3580 if (!type) {
3581 return;
3582 }
3583
3584 BT_LOGD("Destroying integer field type object: addr=%p", type);
3585 bt_put(integer->mapped_clock);
3586 g_free(integer);
3587 }
3588
3589 static
3590 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type)
3591 {
3592 struct bt_ctf_field_type_enumeration *enumeration =
3593 (struct bt_ctf_field_type_enumeration *) type;
3594
3595 if (!type) {
3596 return;
3597 }
3598
3599 BT_LOGD("Destroying enumeration field type object: addr=%p", type);
3600 g_ptr_array_free(enumeration->entries, TRUE);
3601 bt_put(enumeration->container);
3602 g_free(enumeration);
3603 }
3604
3605 static
3606 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type)
3607 {
3608 struct bt_ctf_field_type_floating_point *floating_point =
3609 (struct bt_ctf_field_type_floating_point *) type;
3610
3611 if (!type) {
3612 return;
3613 }
3614
3615 BT_LOGD("Destroying floating point number field type object: addr=%p", type);
3616 g_free(floating_point);
3617 }
3618
3619 static
3620 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type)
3621 {
3622 struct bt_ctf_field_type_structure *structure =
3623 (struct bt_ctf_field_type_structure *) type;
3624
3625 if (!type) {
3626 return;
3627 }
3628
3629 BT_LOGD("Destroying structure field type object: addr=%p", type);
3630 g_ptr_array_free(structure->fields, TRUE);
3631 g_hash_table_destroy(structure->field_name_to_index);
3632 g_free(structure);
3633 }
3634
3635 static
3636 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type)
3637 {
3638 struct bt_ctf_field_type_variant *variant =
3639 (struct bt_ctf_field_type_variant *) type;
3640
3641 if (!type) {
3642 return;
3643 }
3644
3645 BT_LOGD("Destroying variant field type object: addr=%p", type);
3646 g_ptr_array_free(variant->fields, TRUE);
3647 g_hash_table_destroy(variant->field_name_to_index);
3648 g_string_free(variant->tag_name, TRUE);
3649 bt_put(&variant->tag->parent);
3650 BT_PUT(variant->tag_field_path);
3651 g_free(variant);
3652 }
3653
3654 static
3655 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type)
3656 {
3657 struct bt_ctf_field_type_array *array =
3658 (struct bt_ctf_field_type_array *) type;
3659
3660 if (!type) {
3661 return;
3662 }
3663
3664 BT_LOGD("Destroying array field type object: addr=%p", type);
3665 bt_put(array->element_type);
3666 g_free(array);
3667 }
3668
3669 static
3670 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type)
3671 {
3672 struct bt_ctf_field_type_sequence *sequence =
3673 (struct bt_ctf_field_type_sequence *) type;
3674
3675 if (!type) {
3676 return;
3677 }
3678
3679 BT_LOGD("Destroying sequence field type object: addr=%p", type);
3680 bt_put(sequence->element_type);
3681 g_string_free(sequence->length_field_name, TRUE);
3682 BT_PUT(sequence->length_field_path);
3683 g_free(sequence);
3684 }
3685
3686 static
3687 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type)
3688 {
3689 struct bt_ctf_field_type_string *string =
3690 (struct bt_ctf_field_type_string *) type;
3691
3692 if (!type) {
3693 return;
3694 }
3695
3696 BT_LOGD("Destroying string field type object: addr=%p", type);
3697 g_free(string);
3698 }
3699
3700 static
3701 void generic_field_type_freeze(struct bt_ctf_field_type *type)
3702 {
3703 type->frozen = 1;
3704 }
3705
3706 static
3707 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *type)
3708 {
3709 struct bt_ctf_field_type_integer *integer_type = container_of(
3710 type, struct bt_ctf_field_type_integer, parent);
3711
3712 BT_LOGD("Freezing integer field type object: addr=%p", type);
3713
3714 if (integer_type->mapped_clock) {
3715 bt_ctf_clock_class_freeze(integer_type->mapped_clock);
3716 }
3717
3718 generic_field_type_freeze(type);
3719 }
3720
3721 static
3722 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
3723 {
3724 struct bt_ctf_field_type_enumeration *enumeration_type = container_of(
3725 type, struct bt_ctf_field_type_enumeration, parent);
3726
3727 BT_LOGD("Freezing enumeration field type object: addr=%p", type);
3728 set_enumeration_range_overlap(type);
3729 generic_field_type_freeze(type);
3730 bt_ctf_field_type_freeze(enumeration_type->container);
3731 }
3732
3733 static
3734 void freeze_structure_field(struct structure_field *field)
3735 {
3736 BT_LOGD("Freezing structure field type field: addr=%p", field);
3737 bt_ctf_field_type_freeze(field->type);
3738 }
3739
3740 static
3741 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type)
3742 {
3743 struct bt_ctf_field_type_structure *structure_type = container_of(
3744 type, struct bt_ctf_field_type_structure, parent);
3745
3746 /* Cache the alignment */
3747 BT_LOGD("Freezing structure field type object: addr=%p", type);
3748 type->alignment = bt_ctf_field_type_get_alignment(type);
3749 generic_field_type_freeze(type);
3750 g_ptr_array_foreach(structure_type->fields,
3751 (GFunc) freeze_structure_field, NULL);
3752 }
3753
3754 static
3755 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type)
3756 {
3757 struct bt_ctf_field_type_variant *variant_type = container_of(
3758 type, struct bt_ctf_field_type_variant, parent);
3759
3760 BT_LOGD("Freezing variant field type object: addr=%p", type);
3761 generic_field_type_freeze(type);
3762 g_ptr_array_foreach(variant_type->fields,
3763 (GFunc) freeze_structure_field, NULL);
3764 }
3765
3766 static
3767 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type)
3768 {
3769 struct bt_ctf_field_type_array *array_type = container_of(
3770 type, struct bt_ctf_field_type_array, parent);
3771
3772 /* Cache the alignment */
3773 BT_LOGD("Freezing array field type object: addr=%p", type);
3774 type->alignment = bt_ctf_field_type_get_alignment(type);
3775 generic_field_type_freeze(type);
3776 bt_ctf_field_type_freeze(array_type->element_type);
3777 }
3778
3779 static
3780 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type)
3781 {
3782 struct bt_ctf_field_type_sequence *sequence_type = container_of(
3783 type, struct bt_ctf_field_type_sequence, parent);
3784
3785 /* Cache the alignment */
3786 BT_LOGD("Freezing sequence field type object: addr=%p", type);
3787 type->alignment = bt_ctf_field_type_get_alignment(type);
3788 generic_field_type_freeze(type);
3789 bt_ctf_field_type_freeze(sequence_type->element_type);
3790 }
3791
3792 static
3793 const char *get_encoding_string(enum bt_ctf_string_encoding encoding)
3794 {
3795 const char *encoding_string;
3796
3797 switch (encoding) {
3798 case BT_CTF_STRING_ENCODING_NONE:
3799 encoding_string = "none";
3800 break;
3801 case BT_CTF_STRING_ENCODING_ASCII:
3802 encoding_string = "ASCII";
3803 break;
3804 case BT_CTF_STRING_ENCODING_UTF8:
3805 encoding_string = "UTF8";
3806 break;
3807 default:
3808 encoding_string = "unknown";
3809 break;
3810 }
3811
3812 return encoding_string;
3813 }
3814
3815 static
3816 const char *get_integer_base_string(enum bt_ctf_integer_base base)
3817 {
3818 const char *base_string;
3819
3820 switch (base) {
3821 case BT_CTF_INTEGER_BASE_DECIMAL:
3822 base_string = "decimal";
3823 break;
3824 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
3825 base_string = "hexadecimal";
3826 break;
3827 case BT_CTF_INTEGER_BASE_OCTAL:
3828 base_string = "octal";
3829 break;
3830 case BT_CTF_INTEGER_BASE_BINARY:
3831 base_string = "binary";
3832 break;
3833 default:
3834 base_string = "unknown";
3835 break;
3836 }
3837
3838 return base_string;
3839 }
3840
3841 static
3842 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
3843 struct metadata_context *context)
3844 {
3845 struct bt_ctf_field_type_integer *integer = container_of(type,
3846 struct bt_ctf_field_type_integer, parent);
3847 int ret = 0;
3848
3849 BT_LOGD("Serializing integer field type's metadata: "
3850 "ft-addr=%p, metadata-context-addr=%p", type, context);
3851 g_string_append_printf(context->string,
3852 "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s",
3853 integer->size, type->alignment,
3854 (integer->is_signed ? "true" : "false"),
3855 get_encoding_string(integer->encoding),
3856 get_integer_base_string(integer->base),
3857 get_byte_order_string(integer->user_byte_order));
3858 if (integer->mapped_clock) {
3859 const char *clock_name = bt_ctf_clock_class_get_name(
3860 integer->mapped_clock);
3861
3862 assert(clock_name);
3863 g_string_append_printf(context->string,
3864 "; map = clock.%s.value", clock_name);
3865 }
3866
3867 g_string_append(context->string, "; }");
3868 return ret;
3869 }
3870
3871 static
3872 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type,
3873 struct metadata_context *context)
3874 {
3875 size_t entry;
3876 int ret;
3877 struct bt_ctf_field_type_enumeration *enumeration = container_of(type,
3878 struct bt_ctf_field_type_enumeration, parent);
3879 struct bt_ctf_field_type *container_type;
3880 int container_signed;
3881
3882 BT_LOGD("Serializing enumeration field type's metadata: "
3883 "ft-addr=%p, metadata-context-addr=%p", type, context);
3884 container_type = bt_ctf_field_type_enumeration_get_container_type(type);
3885 assert(container_type);
3886 container_signed = bt_ctf_field_type_integer_get_signed(container_type);
3887 assert(container_signed >= 0);
3888 g_string_append(context->string, "enum : ");
3889 ret = bt_ctf_field_type_serialize(enumeration->container, context);
3890 if (ret) {
3891 BT_LOGW("Cannot serialize enumeration field type's container field type's metadata: "
3892 "container-ft-addr=%p", enumeration->container);
3893 goto end;
3894 }
3895
3896 g_string_append(context->string, " { ");
3897 for (entry = 0; entry < enumeration->entries->len; entry++) {
3898 struct enumeration_mapping *mapping =
3899 enumeration->entries->pdata[entry];
3900
3901 if (container_signed) {
3902 if (mapping->range_start._signed ==
3903 mapping->range_end._signed) {
3904 g_string_append_printf(context->string,
3905 "\"%s\" = %" PRId64,
3906 g_quark_to_string(mapping->string),
3907 mapping->range_start._signed);
3908 } else {
3909 g_string_append_printf(context->string,
3910 "\"%s\" = %" PRId64 " ... %" PRId64,
3911 g_quark_to_string(mapping->string),
3912 mapping->range_start._signed,
3913 mapping->range_end._signed);
3914 }
3915 } else {
3916 if (mapping->range_start._unsigned ==
3917 mapping->range_end._unsigned) {
3918 g_string_append_printf(context->string,
3919 "\"%s\" = %" PRIu64,
3920 g_quark_to_string(mapping->string),
3921 mapping->range_start._unsigned);
3922 } else {
3923 g_string_append_printf(context->string,
3924 "\"%s\" = %" PRIu64 " ... %" PRIu64,
3925 g_quark_to_string(mapping->string),
3926 mapping->range_start._unsigned,
3927 mapping->range_end._unsigned);
3928 }
3929 }
3930
3931 g_string_append(context->string,
3932 ((entry != (enumeration->entries->len - 1)) ?
3933 ", " : " }"));
3934 }
3935
3936 if (context->field_name->len) {
3937 g_string_append_printf(context->string, " %s",
3938 context->field_name->str);
3939 g_string_assign(context->field_name, "");
3940 }
3941 end:
3942 bt_put(container_type);
3943 return ret;
3944 }
3945
3946 static
3947 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type,
3948 struct metadata_context *context)
3949 {
3950 struct bt_ctf_field_type_floating_point *floating_point = container_of(
3951 type, struct bt_ctf_field_type_floating_point, parent);
3952
3953 BT_LOGD("Serializing floating point number field type's metadata: "
3954 "ft-addr=%p, metadata-context-addr=%p", type, context);
3955 g_string_append_printf(context->string,
3956 "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }",
3957 floating_point->exp_dig,
3958 floating_point->mant_dig,
3959 get_byte_order_string(floating_point->user_byte_order),
3960 type->alignment);
3961 return 0;
3962 }
3963
3964 static
3965 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
3966 struct metadata_context *context)
3967 {
3968 size_t i;
3969 unsigned int indent;
3970 int64_t ret = 0;
3971 struct bt_ctf_field_type_structure *structure = container_of(type,
3972 struct bt_ctf_field_type_structure, parent);
3973 GString *structure_field_name = context->field_name;
3974
3975 BT_LOGD("Serializing structure field type's metadata: "
3976 "ft-addr=%p, metadata-context-addr=%p", type, context);
3977 context->field_name = g_string_new("");
3978
3979 context->current_indentation_level++;
3980 g_string_append(context->string, "struct {\n");
3981
3982 for (i = 0; i < structure->fields->len; i++) {
3983 struct structure_field *field = structure->fields->pdata[i];
3984
3985 BT_LOGV("Serializing structure field type's field metadata: "
3986 "index=%" PRId64 ", "
3987 "field-ft-addr=%p, field-name=\"%s\"",
3988 i, field, g_quark_to_string(field->name));
3989
3990 for (indent = 0; indent < context->current_indentation_level;
3991 indent++) {
3992 g_string_append_c(context->string, '\t');
3993 }
3994
3995 g_string_assign(context->field_name,
3996 g_quark_to_string(field->name));
3997 ret = bt_ctf_field_type_serialize(field->type, context);
3998 if (ret) {
3999 BT_LOGW("Cannot serialize structure field type's field's metadata: "
4000 "index=%" PRId64 ", "
4001 "field-ft-addr=%p, field-name=\"%s\"",
4002 i, field->type,
4003 g_quark_to_string(field->name));
4004 goto end;
4005 }
4006
4007 if (context->field_name->len) {
4008 g_string_append_printf(context->string, " %s",
4009 context->field_name->str);
4010 }
4011 g_string_append(context->string, ";\n");
4012 }
4013
4014 context->current_indentation_level--;
4015 for (indent = 0; indent < context->current_indentation_level;
4016 indent++) {
4017 g_string_append_c(context->string, '\t');
4018 }
4019
4020 g_string_append_printf(context->string, "} align(%u)",
4021 type->alignment);
4022 end:
4023 g_string_free(context->field_name, TRUE);
4024 context->field_name = structure_field_name;
4025 return ret;
4026 }
4027
4028 static
4029 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
4030 struct metadata_context *context)
4031 {
4032 size_t i;
4033 unsigned int indent;
4034 int ret = 0;
4035 struct bt_ctf_field_type_variant *variant = container_of(
4036 type, struct bt_ctf_field_type_variant, parent);
4037 GString *variant_field_name = context->field_name;
4038
4039 BT_LOGD("Serializing variant field type's metadata: "
4040 "ft-addr=%p, metadata-context-addr=%p", type, context);
4041 context->field_name = g_string_new("");
4042 if (variant->tag_name->len > 0) {
4043 g_string_append_printf(context->string,
4044 "variant <%s> {\n", variant->tag_name->str);
4045 } else {
4046 g_string_append(context->string, "variant {\n");
4047 }
4048
4049 context->current_indentation_level++;
4050 for (i = 0; i < variant->fields->len; i++) {
4051 struct structure_field *field = variant->fields->pdata[i];
4052
4053 BT_LOGV("Serializing variant field type's field metadata: "
4054 "index=%" PRId64 ", "
4055 "field-ft-addr=%p, field-name=\"%s\"",
4056 i, field, g_quark_to_string(field->name));
4057
4058 g_string_assign(context->field_name,
4059 g_quark_to_string(field->name));
4060 for (indent = 0; indent < context->current_indentation_level;
4061 indent++) {
4062 g_string_append_c(context->string, '\t');
4063 }
4064
4065 g_string_assign(context->field_name,
4066 g_quark_to_string(field->name));
4067 ret = bt_ctf_field_type_serialize(field->type, context);
4068 if (ret) {
4069 BT_LOGW("Cannot serialize variant field type's field's metadata: "
4070 "index=%" PRId64 ", "
4071 "field-ft-addr=%p, field-name=\"%s\"",
4072 i, field->type,
4073 g_quark_to_string(field->name));
4074 goto end;
4075 }
4076
4077 if (context->field_name->len) {
4078 g_string_append_printf(context->string, " %s;",
4079 context->field_name->str);
4080 }
4081
4082 g_string_append_c(context->string, '\n');
4083 }
4084
4085 context->current_indentation_level--;
4086 for (indent = 0; indent < context->current_indentation_level;
4087 indent++) {
4088 g_string_append_c(context->string, '\t');
4089 }
4090
4091 g_string_append(context->string, "}");
4092 end:
4093 g_string_free(context->field_name, TRUE);
4094 context->field_name = variant_field_name;
4095 return ret;
4096 }
4097
4098 static
4099 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type,
4100 struct metadata_context *context)
4101 {
4102 int ret = 0;
4103 struct bt_ctf_field_type_array *array = container_of(type,
4104 struct bt_ctf_field_type_array, parent);
4105
4106 BT_LOGD("Serializing array field type's metadata: "
4107 "ft-addr=%p, metadata-context-addr=%p", type, context);
4108 ret = bt_ctf_field_type_serialize(array->element_type, context);
4109 if (ret) {
4110 BT_LOGW("Cannot serialize array field type's element field type's metadata: "
4111 "element-ft-addr=%p", array->element_type);
4112 goto end;
4113 }
4114
4115 if (context->field_name->len) {
4116 g_string_append_printf(context->string, " %s[%u]",
4117 context->field_name->str, array->length);
4118 g_string_assign(context->field_name, "");
4119 } else {
4120 g_string_append_printf(context->string, "[%u]", array->length);
4121 }
4122 end:
4123 return ret;
4124 }
4125
4126 static
4127 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type,
4128 struct metadata_context *context)
4129 {
4130 int ret = 0;
4131 struct bt_ctf_field_type_sequence *sequence = container_of(
4132 type, struct bt_ctf_field_type_sequence, parent);
4133
4134 BT_LOGD("Serializing sequence field type's metadata: "
4135 "ft-addr=%p, metadata-context-addr=%p", type, context);
4136 ret = bt_ctf_field_type_serialize(sequence->element_type, context);
4137 if (ret) {
4138 BT_LOGW("Cannot serialize sequence field type's element field type's metadata: "
4139 "element-ft-addr=%p", sequence->element_type);
4140 goto end;
4141 }
4142
4143 if (context->field_name->len) {
4144 g_string_append_printf(context->string, " %s[%s]",
4145 context->field_name->str,
4146 sequence->length_field_name->str);
4147 g_string_assign(context->field_name, "");
4148 } else {
4149 g_string_append_printf(context->string, "[%s]",
4150 sequence->length_field_name->str);
4151 }
4152 end:
4153 return ret;
4154 }
4155
4156 static
4157 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type,
4158 struct metadata_context *context)
4159 {
4160 struct bt_ctf_field_type_string *string = container_of(
4161 type, struct bt_ctf_field_type_string, parent);
4162
4163 BT_LOGD("Serializing string field type's metadata: "
4164 "ft-addr=%p, metadata-context-addr=%p", type, context);
4165 g_string_append_printf(context->string,
4166 "string { encoding = %s; }",
4167 get_encoding_string(string->encoding));
4168 return 0;
4169 }
4170
4171 static
4172 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
4173 enum bt_ctf_byte_order byte_order)
4174 {
4175 struct bt_ctf_field_type_integer *integer_type = container_of(type,
4176 struct bt_ctf_field_type_integer, parent);
4177
4178 integer_type->user_byte_order = byte_order;
4179 }
4180
4181 static
4182 void bt_ctf_field_type_enumeration_set_byte_order(
4183 struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order)
4184 {
4185 struct bt_ctf_field_type_enumeration *enum_type = container_of(type,
4186 struct bt_ctf_field_type_enumeration, parent);
4187
4188 /* Safe to assume that container is an integer */
4189 bt_ctf_field_type_integer_set_byte_order(enum_type->container,
4190 byte_order);
4191 }
4192
4193 static
4194 void bt_ctf_field_type_floating_point_set_byte_order(
4195 struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order)
4196 {
4197 struct bt_ctf_field_type_floating_point *floating_point_type =
4198 container_of(type, struct bt_ctf_field_type_floating_point,
4199 parent);
4200
4201 floating_point_type->user_byte_order = byte_order;
4202 }
4203
4204 static
4205 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type,
4206 enum bt_ctf_byte_order byte_order)
4207 {
4208 int i;
4209 struct bt_ctf_field_type_structure *structure_type =
4210 container_of(type, struct bt_ctf_field_type_structure,
4211 parent);
4212
4213 for (i = 0; i < structure_type->fields->len; i++) {
4214 struct structure_field *field = g_ptr_array_index(
4215 structure_type->fields, i);
4216 struct bt_ctf_field_type *field_type = field->type;
4217
4218 if (set_byte_order_funcs[field_type->id]) {
4219 set_byte_order_funcs[field_type->id](
4220 field_type, byte_order);
4221 }
4222 }
4223 }
4224
4225 static
4226 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type,
4227 enum bt_ctf_byte_order byte_order)
4228 {
4229 int i;
4230 struct bt_ctf_field_type_variant *variant_type =
4231 container_of(type, struct bt_ctf_field_type_variant,
4232 parent);
4233
4234 for (i = 0; i < variant_type->fields->len; i++) {
4235 struct structure_field *field = g_ptr_array_index(
4236 variant_type->fields, i);
4237 struct bt_ctf_field_type *field_type = field->type;
4238
4239 if (set_byte_order_funcs[field_type->id]) {
4240 set_byte_order_funcs[field_type->id](
4241 field_type, byte_order);
4242 }
4243 }
4244 }
4245
4246 static
4247 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type,
4248 enum bt_ctf_byte_order byte_order)
4249 {
4250 struct bt_ctf_field_type_array *array_type =
4251 container_of(type, struct bt_ctf_field_type_array,
4252 parent);
4253
4254 if (set_byte_order_funcs[array_type->element_type->id]) {
4255 set_byte_order_funcs[array_type->element_type->id](
4256 array_type->element_type, byte_order);
4257 }
4258 }
4259
4260 static
4261 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type,
4262 enum bt_ctf_byte_order byte_order)
4263 {
4264 struct bt_ctf_field_type_sequence *sequence_type =
4265 container_of(type, struct bt_ctf_field_type_sequence,
4266 parent);
4267
4268 if (set_byte_order_funcs[
4269 sequence_type->element_type->id]) {
4270 set_byte_order_funcs[
4271 sequence_type->element_type->id](
4272 sequence_type->element_type, byte_order);
4273 }
4274 }
4275
4276 static
4277 struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
4278 struct bt_ctf_field_type *type)
4279 {
4280 struct bt_ctf_field_type *copy;
4281 struct bt_ctf_field_type_integer *integer, *copy_integer;
4282
4283 BT_LOGD("Copying integer field type's: addr=%p", type);
4284 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
4285 copy = bt_ctf_field_type_integer_create(integer->size);
4286 if (!copy) {
4287 BT_LOGE_STR("Cannot create integer field type.");
4288 goto end;
4289 }
4290
4291 copy_integer = container_of(copy, struct bt_ctf_field_type_integer,
4292 parent);
4293 copy_integer->mapped_clock = bt_get(integer->mapped_clock);
4294 copy_integer->user_byte_order = integer->user_byte_order;
4295 copy_integer->is_signed = integer->is_signed;
4296 copy_integer->size = integer->size;
4297 copy_integer->base = integer->base;
4298 copy_integer->encoding = integer->encoding;
4299 BT_LOGD("Copied integer field type: original-ft-addr=%p, copy-ft-addr=%p",
4300 type, copy);
4301
4302 end:
4303 return copy;
4304 }
4305
4306 static
4307 struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
4308 struct bt_ctf_field_type *type)
4309 {
4310 size_t i;
4311 struct bt_ctf_field_type *copy = NULL, *copy_container;
4312 struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration;
4313
4314 BT_LOGD("Copying enumeration field type's: addr=%p", type);
4315 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
4316 parent);
4317
4318 /* Copy the source enumeration's container */
4319 copy_container = bt_ctf_field_type_copy(enumeration->container);
4320 if (!copy_container) {
4321 BT_LOGE_STR("Cannot copy enumeration field type's container field type.");
4322 goto end;
4323 }
4324
4325 copy = bt_ctf_field_type_enumeration_create(copy_container);
4326 if (!copy) {
4327 BT_LOGE_STR("Cannot create enumeration field type.");
4328 goto end;
4329 }
4330 copy_enumeration = container_of(copy,
4331 struct bt_ctf_field_type_enumeration, parent);
4332
4333 /* Copy all enumaration entries */
4334 for (i = 0; i < enumeration->entries->len; i++) {
4335 struct enumeration_mapping *mapping = g_ptr_array_index(
4336 enumeration->entries, i);
4337 struct enumeration_mapping *copy_mapping = g_new0(
4338 struct enumeration_mapping, 1);
4339
4340 if (!copy_mapping) {
4341 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
4342 goto error;
4343 }
4344
4345 *copy_mapping = *mapping;
4346 g_ptr_array_add(copy_enumeration->entries, copy_mapping);
4347 }
4348
4349 BT_LOGD("Copied enumeration field type: original-ft-addr=%p, copy-ft-addr=%p",
4350 type, copy);
4351
4352 end:
4353 bt_put(copy_container);
4354 return copy;
4355 error:
4356 bt_put(copy_container);
4357 BT_PUT(copy);
4358 return copy;
4359 }
4360
4361 static
4362 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
4363 struct bt_ctf_field_type *type)
4364 {
4365 struct bt_ctf_field_type *copy;
4366 struct bt_ctf_field_type_floating_point *floating_point, *copy_float;
4367
4368 BT_LOGD("Copying floating point number field type's: addr=%p", type);
4369 floating_point = container_of(type,
4370 struct bt_ctf_field_type_floating_point, parent);
4371 copy = bt_ctf_field_type_floating_point_create();
4372 if (!copy) {
4373 BT_LOGE_STR("Cannot create floating point number field type.");
4374 goto end;
4375 }
4376
4377 copy_float = container_of(copy,
4378 struct bt_ctf_field_type_floating_point, parent);
4379 copy_float->user_byte_order = floating_point->user_byte_order;
4380 copy_float->exp_dig = floating_point->exp_dig;
4381 copy_float->mant_dig = floating_point->mant_dig;
4382 BT_LOGD("Copied floating point number field type: original-ft-addr=%p, copy-ft-addr=%p",
4383 type, copy);
4384 end:
4385 return copy;
4386 }
4387
4388 static
4389 struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
4390 struct bt_ctf_field_type *type)
4391 {
4392 int64_t i;
4393 GHashTableIter iter;
4394 gpointer key, value;
4395 struct bt_ctf_field_type *copy;
4396 struct bt_ctf_field_type_structure *structure, *copy_structure;
4397
4398 BT_LOGD("Copying structure field type's: addr=%p", type);
4399 structure = container_of(type, struct bt_ctf_field_type_structure,
4400 parent);
4401 copy = bt_ctf_field_type_structure_create();
4402 if (!copy) {
4403 BT_LOGE_STR("Cannot create structure field type.");
4404 goto end;
4405 }
4406
4407 copy_structure = container_of(copy,
4408 struct bt_ctf_field_type_structure, parent);
4409
4410 /* Copy field_name_to_index */
4411 g_hash_table_iter_init(&iter, structure->field_name_to_index);
4412 while (g_hash_table_iter_next (&iter, &key, &value)) {
4413 g_hash_table_insert(copy_structure->field_name_to_index,
4414 key, value);
4415 }
4416
4417 for (i = 0; i < structure->fields->len; i++) {
4418 struct structure_field *entry, *copy_entry;
4419 struct bt_ctf_field_type *copy_field;
4420
4421 entry = g_ptr_array_index(structure->fields, i);
4422 BT_LOGV("Copying structure field type's field: "
4423 "index=%" PRId64 ", "
4424 "field-ft-addr=%p, field-name=\"%s\"",
4425 i, entry, g_quark_to_string(entry->name));
4426 copy_entry = g_new0(struct structure_field, 1);
4427 if (!copy_entry) {
4428 BT_LOGE_STR("Failed to allocate one structure field type field.");
4429 goto error;
4430 }
4431
4432 copy_field = bt_ctf_field_type_copy(entry->type);
4433 if (!copy_field) {
4434 BT_LOGE("Cannot copy structure field type's field: "
4435 "index=%" PRId64 ", "
4436 "field-ft-addr=%p, field-name=\"%s\"",
4437 i, entry, g_quark_to_string(entry->name));
4438 g_free(copy_entry);
4439 goto error;
4440 }
4441
4442 copy_entry->name = entry->name;
4443 copy_entry->type = copy_field;
4444 g_ptr_array_add(copy_structure->fields, copy_entry);
4445 }
4446
4447 BT_LOGD("Copied structure field type: original-ft-addr=%p, copy-ft-addr=%p",
4448 type, copy);
4449
4450 end:
4451 return copy;
4452 error:
4453 BT_PUT(copy);
4454 return copy;
4455 }
4456
4457 static
4458 struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
4459 struct bt_ctf_field_type *type)
4460 {
4461 int64_t i;
4462 GHashTableIter iter;
4463 gpointer key, value;
4464 struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL;
4465 struct bt_ctf_field_type_variant *variant, *copy_variant;
4466
4467 BT_LOGD("Copying variant field type's: addr=%p", type);
4468 variant = container_of(type, struct bt_ctf_field_type_variant,
4469 parent);
4470 if (variant->tag) {
4471 copy_tag = bt_ctf_field_type_copy(&variant->tag->parent);
4472 if (!copy_tag) {
4473 BT_LOGE_STR("Cannot copy variant field type's tag field type.");
4474 goto end;
4475 }
4476 }
4477
4478 copy = bt_ctf_field_type_variant_create(copy_tag,
4479 variant->tag_name->len ? variant->tag_name->str : NULL);
4480 if (!copy) {
4481 BT_LOGE_STR("Cannot create variant field type.");
4482 goto end;
4483 }
4484
4485 copy_variant = container_of(copy, struct bt_ctf_field_type_variant,
4486 parent);
4487
4488 /* Copy field_name_to_index */
4489 g_hash_table_iter_init(&iter, variant->field_name_to_index);
4490 while (g_hash_table_iter_next (&iter, &key, &value)) {
4491 g_hash_table_insert(copy_variant->field_name_to_index,
4492 key, value);
4493 }
4494
4495 for (i = 0; i < variant->fields->len; i++) {
4496 struct structure_field *entry, *copy_entry;
4497 struct bt_ctf_field_type *copy_field;
4498
4499 entry = g_ptr_array_index(variant->fields, i);
4500 BT_LOGV("Copying variant field type's field: "
4501 "index=%" PRId64 ", "
4502 "field-ft-addr=%p, field-name=\"%s\"",
4503 i, entry, g_quark_to_string(entry->name));
4504 copy_entry = g_new0(struct structure_field, 1);
4505 if (!copy_entry) {
4506 BT_LOGE_STR("Failed to allocate one variant field type field.");
4507 goto error;
4508 }
4509
4510 copy_field = bt_ctf_field_type_copy(entry->type);
4511 if (!copy_field) {
4512 BT_LOGE("Cannot copy variant field type's field: "
4513 "index=%" PRId64 ", "
4514 "field-ft-addr=%p, field-name=\"%s\"",
4515 i, entry, g_quark_to_string(entry->name));
4516 g_free(copy_entry);
4517 goto error;
4518 }
4519
4520 copy_entry->name = entry->name;
4521 copy_entry->type = copy_field;
4522 g_ptr_array_add(copy_variant->fields, copy_entry);
4523 }
4524
4525 if (variant->tag_field_path) {
4526 BT_LOGV_STR("Copying variant field type's tag field path.");
4527 copy_variant->tag_field_path = bt_ctf_field_path_copy(
4528 variant->tag_field_path);
4529 if (!copy_variant->tag_field_path) {
4530 BT_LOGE_STR("Cannot copy variant field type's tag field path.");
4531 goto error;
4532 }
4533 }
4534
4535 BT_LOGD("Copied variant field type: original-ft-addr=%p, copy-ft-addr=%p",
4536 type, copy);
4537
4538 end:
4539 bt_put(copy_tag);
4540 return copy;
4541 error:
4542 bt_put(copy_tag);
4543 BT_PUT(copy);
4544 return copy;
4545 }
4546
4547 static
4548 struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
4549 struct bt_ctf_field_type *type)
4550 {
4551 struct bt_ctf_field_type *copy = NULL, *copy_element;
4552 struct bt_ctf_field_type_array *array;
4553
4554 BT_LOGD("Copying array field type's: addr=%p", type);
4555 array = container_of(type, struct bt_ctf_field_type_array,
4556 parent);
4557 copy_element = bt_ctf_field_type_copy(array->element_type);
4558 if (!copy_element) {
4559 BT_LOGE_STR("Cannot copy array field type's element field type.");
4560 goto end;
4561 }
4562
4563 copy = bt_ctf_field_type_array_create(copy_element, array->length);
4564 if (!copy) {
4565 BT_LOGE_STR("Cannot create array field type.");
4566 goto end;
4567 }
4568
4569 BT_LOGD("Copied array field type: original-ft-addr=%p, copy-ft-addr=%p",
4570 type, copy);
4571
4572 end:
4573 bt_put(copy_element);
4574 return copy;
4575 }
4576
4577 static
4578 struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
4579 struct bt_ctf_field_type *type)
4580 {
4581 struct bt_ctf_field_type *copy = NULL, *copy_element;
4582 struct bt_ctf_field_type_sequence *sequence, *copy_sequence;
4583
4584 BT_LOGD("Copying sequence field type's: addr=%p", type);
4585 sequence = container_of(type, struct bt_ctf_field_type_sequence,
4586 parent);
4587 copy_element = bt_ctf_field_type_copy(sequence->element_type);
4588 if (!copy_element) {
4589 BT_LOGE_STR("Cannot copy sequence field type's element field type.");
4590 goto end;
4591 }
4592
4593 copy = bt_ctf_field_type_sequence_create(copy_element,
4594 sequence->length_field_name->len ?
4595 sequence->length_field_name->str : NULL);
4596 if (!copy) {
4597 BT_LOGE_STR("Cannot create sequence field type.");
4598 goto end;
4599 }
4600
4601 copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence,
4602 parent);
4603 if (sequence->length_field_path) {
4604 BT_LOGV_STR("Copying sequence field type's length field path.");
4605 copy_sequence->length_field_path = bt_ctf_field_path_copy(
4606 sequence->length_field_path);
4607 if (!copy_sequence->length_field_path) {
4608 BT_LOGE_STR("Cannot copy sequence field type's length field path.");
4609 goto error;
4610 }
4611 }
4612
4613 BT_LOGD("Copied sequence field type: original-ft-addr=%p, copy-ft-addr=%p",
4614 type, copy);
4615
4616 end:
4617 bt_put(copy_element);
4618 return copy;
4619 error:
4620 BT_PUT(copy);
4621 goto end;
4622 }
4623
4624 static
4625 struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
4626 struct bt_ctf_field_type *type)
4627 {
4628 struct bt_ctf_field_type *copy;
4629 struct bt_ctf_field_type_string *string;
4630
4631 BT_LOGD("Copying string field type's: addr=%p", type);
4632 copy = bt_ctf_field_type_string_create();
4633 if (!copy) {
4634 BT_LOGE_STR("Cannot create string field type.");
4635 goto end;
4636 }
4637
4638 string = container_of(type, struct bt_ctf_field_type_string,
4639 parent);
4640 BT_LOGD("Copied string field type: original-ft-addr=%p, copy-ft-addr=%p",
4641 type, copy);
4642 end:
4643 return copy;
4644 }
4645
4646 static
4647 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a,
4648 struct bt_ctf_field_type *type_b)
4649 {
4650 int ret = 1;
4651 struct bt_ctf_field_type_integer *int_type_a;
4652 struct bt_ctf_field_type_integer *int_type_b;
4653
4654 int_type_a = container_of(type_a, struct bt_ctf_field_type_integer,
4655 parent);
4656 int_type_b = container_of(type_b, struct bt_ctf_field_type_integer,
4657 parent);
4658
4659 /* Length */
4660 if (int_type_a->size != int_type_b->size) {
4661 BT_LOGV("Integer field types differ: different sizes: "
4662 "ft-a-size=%u, ft-b-size=%u",
4663 int_type_a->size, int_type_b->size);
4664 goto end;
4665 }
4666
4667 /* Byte order */
4668 if (int_type_a->user_byte_order != int_type_b->user_byte_order) {
4669 BT_LOGV("Integer field types differ: different byte orders: "
4670 "ft-a-bo=%s, ft-b-bo=%s",
4671 bt_ctf_byte_order_string(int_type_a->user_byte_order),
4672 bt_ctf_byte_order_string(int_type_b->user_byte_order));
4673 goto end;
4674 }
4675
4676 /* Signedness */
4677 if (int_type_a->is_signed != int_type_b->is_signed) {
4678 BT_LOGV("Integer field types differ: different signedness: "
4679 "ft-a-is-signed=%d, ft-b-is-signed=%d",
4680 int_type_a->is_signed,
4681 int_type_b->is_signed);
4682 goto end;
4683 }
4684
4685 /* Base */
4686 if (int_type_a->base != int_type_b->base) {
4687 BT_LOGV("Integer field types differ: different bases: "
4688 "ft-a-base=%s, ft-b-base=%s",
4689 bt_ctf_integer_base_string(int_type_a->base),
4690 bt_ctf_integer_base_string(int_type_b->base));
4691 goto end;
4692 }
4693
4694 /* Encoding */
4695 if (int_type_a->encoding != int_type_b->encoding) {
4696 BT_LOGV("Integer field types differ: different encodings: "
4697 "ft-a-encoding=%s, ft-b-encoding=%s",
4698 bt_ctf_string_encoding_string(int_type_a->encoding),
4699 bt_ctf_string_encoding_string(int_type_b->encoding));
4700 goto end;
4701 }
4702
4703 /* Mapped clock */
4704 if (int_type_a->mapped_clock != int_type_b->mapped_clock) {
4705 BT_LOGV("Integer field types differ: different mapped clock classes: "
4706 "ft-a-mapped-clock-class-addr=%p, "
4707 "ft-b-mapped-clock-class-addr=%p, ",
4708 "ft-a-mapped-clock-class-name=\"%s\", ",
4709 "ft-b-mapped-clock-class-name=\"%s\"",
4710 int_type_a->mapped_clock, int_type_b->mapped_clock,
4711 int_type_a->mapped_clock ? bt_ctf_clock_class_get_name(int_type_a->mapped_clock) : "",
4712 int_type_b->mapped_clock ? bt_ctf_clock_class_get_name(int_type_b->mapped_clock) : "");
4713 goto end;
4714 }
4715
4716 /* Equal */
4717 ret = 0;
4718
4719 end:
4720 return ret;
4721 }
4722
4723 static
4724 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a,
4725 struct bt_ctf_field_type *type_b)
4726 {
4727 int ret = 1;
4728 struct bt_ctf_field_type_floating_point *float_a;
4729 struct bt_ctf_field_type_floating_point *float_b;
4730
4731 float_a = container_of(type_a,
4732 struct bt_ctf_field_type_floating_point, parent);
4733 float_b = container_of(type_b,
4734 struct bt_ctf_field_type_floating_point, parent);
4735
4736 /* Byte order */
4737 if (float_a->user_byte_order != float_b->user_byte_order) {
4738 BT_LOGV("Floating point number field types differ: different byte orders: "
4739 "ft-a-bo=%s, ft-b-bo=%s",
4740 bt_ctf_byte_order_string(float_a->user_byte_order),
4741 bt_ctf_byte_order_string(float_b->user_byte_order));
4742 goto end;
4743 }
4744
4745 /* Exponent length */
4746 if (float_a->exp_dig != float_b->exp_dig) {
4747 BT_LOGV("Floating point number field types differ: different exponent sizes: "
4748 "ft-a-exp-size=%s, ft-b-exp-size=%s",
4749 float_a->exp_dig, float_b->exp_dig);
4750 goto end;
4751 }
4752
4753 /* Mantissa length */
4754 if (float_a->mant_dig != float_b->mant_dig) {
4755 BT_LOGV("Floating point number field types differ: different mantissa sizes: "
4756 "ft-a-mant-size=%s, ft-b-mant-size=%s",
4757 float_a->mant_dig, float_b->mant_dig);
4758 goto end;
4759 }
4760
4761 /* Equal */
4762 ret = 0;
4763
4764 end:
4765 return ret;
4766 }
4767
4768 static
4769 int compare_enumeration_mappings(struct enumeration_mapping *mapping_a,
4770 struct enumeration_mapping *mapping_b)
4771 {
4772 int ret = 1;
4773
4774 /* Label */
4775 if (mapping_a->string != mapping_b->string) {
4776 BT_LOGV("Enumeration field type mappings differ: different names: "
4777 "mapping-a-name=\"%s\", mapping-b-name=\"%s\"",
4778 g_quark_to_string(mapping_a->string),
4779 g_quark_to_string(mapping_b->string));
4780 goto end;
4781 }
4782
4783 /* Range start */
4784 if (mapping_a->range_start._unsigned !=
4785 mapping_b->range_start._unsigned) {
4786 BT_LOGV("Enumeration field type mappings differ: different starts of range: "
4787 "mapping-a-range-start-unsigned=%" PRIu64 ", "
4788 "mapping-b-range-start-unsigned=%" PRIu64,
4789 mapping_a->range_start._unsigned,
4790 mapping_b->range_start._unsigned);
4791 goto end;
4792 }
4793
4794 /* Range end */
4795 if (mapping_a->range_end._unsigned !=
4796 mapping_b->range_end._unsigned) {
4797 BT_LOGV("Enumeration field type mappings differ: different ends of range: "
4798 "mapping-a-range-end-unsigned=%" PRIu64 ", "
4799 "mapping-b-range-end-unsigned=%" PRIu64,
4800 mapping_a->range_end._unsigned,
4801 mapping_b->range_end._unsigned);
4802 goto end;
4803 }
4804
4805 /* Equal */
4806 ret = 0;
4807
4808 end:
4809 return ret;
4810 }
4811
4812 static
4813 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a,
4814 struct bt_ctf_field_type *type_b)
4815 {
4816 int ret = 1;
4817 int i;
4818 struct bt_ctf_field_type_enumeration *enum_a;
4819 struct bt_ctf_field_type_enumeration *enum_b;
4820
4821 enum_a = container_of(type_a,
4822 struct bt_ctf_field_type_enumeration, parent);
4823 enum_b = container_of(type_b,
4824 struct bt_ctf_field_type_enumeration, parent);
4825
4826 /* Container field type */
4827 ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container);
4828 if (ret) {
4829 BT_LOGV("Enumeration field types differ: different container field types: "
4830 "ft-a-container-ft-addr=%p, ft-b-container-ft-addr=%p",
4831 enum_a->container, enum_b->container);
4832 goto end;
4833 }
4834
4835 ret = 1;
4836
4837 /* Entries */
4838 if (enum_a->entries->len != enum_b->entries->len) {
4839 goto end;
4840 }
4841
4842 for (i = 0; i < enum_a->entries->len; ++i) {
4843 struct enumeration_mapping *mapping_a =
4844 g_ptr_array_index(enum_a->entries, i);
4845 struct enumeration_mapping *mapping_b =
4846 g_ptr_array_index(enum_b->entries, i);
4847
4848 if (compare_enumeration_mappings(mapping_a, mapping_b)) {
4849 BT_LOGV("Enumeration field types differ: different mappings: "
4850 "ft-a-mapping-addr=%p, ft-b-mapping-addr=%p, "
4851 "ft-a-mapping-name=\"%s\", ft-b-mapping-name=\"%s\"",
4852 mapping_a, mapping_b,
4853 g_quark_to_string(mapping_a->string),
4854 g_quark_to_string(mapping_b->string));
4855 goto end;
4856 }
4857 }
4858
4859 /* Equal */
4860 ret = 0;
4861
4862 end:
4863 return ret;
4864 }
4865
4866 static
4867 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a,
4868 struct bt_ctf_field_type *type_b)
4869 {
4870 int ret = 1;
4871 struct bt_ctf_field_type_string *string_a;
4872 struct bt_ctf_field_type_string *string_b;
4873
4874 string_a = container_of(type_a,
4875 struct bt_ctf_field_type_string, parent);
4876 string_b = container_of(type_b,
4877 struct bt_ctf_field_type_string, parent);
4878
4879 /* Encoding */
4880 if (string_a->encoding != string_b->encoding) {
4881 BT_LOGV("String field types differ: different encodings: "
4882 "ft-a-encoding=%s, ft-b-encoding=%s",
4883 bt_ctf_string_encoding_string(string_a->encoding),
4884 bt_ctf_string_encoding_string(string_b->encoding));
4885 goto end;
4886 }
4887
4888 /* Equal */
4889 ret = 0;
4890
4891 end:
4892 return ret;
4893 }
4894
4895 static
4896 int compare_structure_fields(struct structure_field *field_a,
4897 struct structure_field *field_b)
4898 {
4899 int ret = 1;
4900
4901 /* Label */
4902 if (field_a->name != field_b->name) {
4903 BT_LOGV("Structure/variant field type fields differ: different names: "
4904 "field-a-name=%s, field-b-name=%s",
4905 g_quark_to_string(field_a->name),
4906 g_quark_to_string(field_b->name));
4907 goto end;
4908 }
4909
4910 /* Type */
4911 ret = bt_ctf_field_type_compare(field_a->type, field_b->type);
4912 if (ret == 1) {
4913 BT_LOGV("Structure/variant field type fields differ: different field types: "
4914 "field-name=\"%s\", field-a-ft-addr=%s, field-b-ft-addr=%s",
4915 g_quark_to_string(field_a->name),
4916 field_a->type, field_b->type);
4917 }
4918
4919 end:
4920 return ret;
4921 }
4922
4923 static
4924 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a,
4925 struct bt_ctf_field_type *type_b)
4926 {
4927 int ret = 1;
4928 int i;
4929 struct bt_ctf_field_type_structure *struct_a;
4930 struct bt_ctf_field_type_structure *struct_b;
4931
4932 struct_a = container_of(type_a,
4933 struct bt_ctf_field_type_structure, parent);
4934 struct_b = container_of(type_b,
4935 struct bt_ctf_field_type_structure, parent);
4936
4937 /* Alignment */
4938 if (bt_ctf_field_type_get_alignment(type_a) !=
4939 bt_ctf_field_type_get_alignment(type_b)) {
4940 BT_LOGV("Structure field types differ: different alignments: "
4941 "ft-a-align=%u, ft-b-align=%u",
4942 bt_ctf_field_type_get_alignment(type_a),
4943 bt_ctf_field_type_get_alignment(type_b));
4944 goto end;
4945 }
4946
4947 /* Fields */
4948 if (struct_a->fields->len != struct_b->fields->len) {
4949 BT_LOGV("Structure field types differ: different field counts: "
4950 "ft-a-field-count=%u, ft-b-field-count=%u",
4951 struct_a->fields->len, struct_b->fields->len);
4952 goto end;
4953 }
4954
4955 for (i = 0; i < struct_a->fields->len; ++i) {
4956 struct structure_field *field_a =
4957 g_ptr_array_index(struct_a->fields, i);
4958 struct structure_field *field_b =
4959 g_ptr_array_index(struct_b->fields, i);
4960
4961 ret = compare_structure_fields(field_a, field_b);
4962 if (ret) {
4963 /* compare_structure_fields() logs what differs */
4964 BT_LOGV_STR("Structure field types differ: different fields.");
4965 goto end;
4966 }
4967 }
4968
4969 /* Equal */
4970 ret = 0;
4971
4972 end:
4973 return ret;
4974 }
4975
4976 static
4977 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a,
4978 struct bt_ctf_field_type *type_b)
4979 {
4980 int ret = 1;
4981 int i;
4982 struct bt_ctf_field_type_variant *variant_a;
4983 struct bt_ctf_field_type_variant *variant_b;
4984
4985 variant_a = container_of(type_a,
4986 struct bt_ctf_field_type_variant, parent);
4987 variant_b = container_of(type_b,
4988 struct bt_ctf_field_type_variant, parent);
4989
4990 /* Tag name */
4991 if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) {
4992 BT_LOGV("Variant field types differ: different tag field names: "
4993 "ft-a-tag-field-name=%u, ft-b-tag-field-name=%u",
4994 variant_a->tag_name->str, variant_b->tag_name->str);
4995 goto end;
4996 }
4997
4998 /* Tag type */
4999 ret = bt_ctf_field_type_compare(
5000 (struct bt_ctf_field_type *) variant_a->tag,
5001 (struct bt_ctf_field_type *) variant_b->tag);
5002 if (ret) {
5003 BT_LOGV("Variant field types differ: different tag field types: "
5004 "ft-a-tag-ft-addr=%p, ft-b-tag-ft-addr=%p",
5005 variant_a->tag, variant_b->tag);
5006 goto end;
5007 }
5008
5009 ret = 1;
5010
5011 /* Fields */
5012 if (variant_a->fields->len != variant_b->fields->len) {
5013 BT_LOGV("Structure field types differ: different field counts: "
5014 "ft-a-field-count=%u, ft-b-field-count=%u",
5015 variant_a->fields->len, variant_b->fields->len);
5016 goto end;
5017 }
5018
5019 for (i = 0; i < variant_a->fields->len; ++i) {
5020 struct structure_field *field_a =
5021 g_ptr_array_index(variant_a->fields, i);
5022 struct structure_field *field_b =
5023 g_ptr_array_index(variant_b->fields, i);
5024
5025 ret = compare_structure_fields(field_a, field_b);
5026 if (ret) {
5027 /* compare_structure_fields() logs what differs */
5028 BT_LOGV_STR("Variant field types differ: different fields.");
5029 goto end;
5030 }
5031 }
5032
5033 /* Equal */
5034 ret = 0;
5035
5036 end:
5037 return ret;
5038 }
5039
5040 static
5041 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a,
5042 struct bt_ctf_field_type *type_b)
5043 {
5044 int ret = 1;
5045 struct bt_ctf_field_type_array *array_a;
5046 struct bt_ctf_field_type_array *array_b;
5047
5048 array_a = container_of(type_a,
5049 struct bt_ctf_field_type_array, parent);
5050 array_b = container_of(type_b,
5051 struct bt_ctf_field_type_array, parent);
5052
5053 /* Length */
5054 if (array_a->length != array_b->length) {
5055 BT_LOGV("Structure field types differ: different lengths: "
5056 "ft-a-length=%u, ft-b-length=%u",
5057 array_a->length, array_b->length);
5058 goto end;
5059 }
5060
5061 /* Element type */
5062 ret = bt_ctf_field_type_compare(array_a->element_type,
5063 array_b->element_type);
5064 if (ret == 1) {
5065 BT_LOGV("Array field types differ: different element field types: "
5066 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
5067 array_a->element_type, array_b->element_type);
5068 }
5069
5070 end:
5071 return ret;
5072 }
5073
5074 static
5075 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a,
5076 struct bt_ctf_field_type *type_b)
5077 {
5078 int ret = -1;
5079 struct bt_ctf_field_type_sequence *sequence_a;
5080 struct bt_ctf_field_type_sequence *sequence_b;
5081
5082 sequence_a = container_of(type_a,
5083 struct bt_ctf_field_type_sequence, parent);
5084 sequence_b = container_of(type_b,
5085 struct bt_ctf_field_type_sequence, parent);
5086
5087 /* Length name */
5088 if (strcmp(sequence_a->length_field_name->str,
5089 sequence_b->length_field_name->str)) {
5090 BT_LOGV("Sequence field types differ: different length field names: "
5091 "ft-a-length-field-name=%u, ft-b-length-field-name=%u",
5092 sequence_a->length_field_name->str,
5093 sequence_b->length_field_name->str);
5094 goto end;
5095 }
5096
5097 /* Element type */
5098 ret = bt_ctf_field_type_compare(sequence_a->element_type,
5099 sequence_b->element_type);
5100 if (ret == 1) {
5101 BT_LOGV("Sequence field types differ: different element field types: "
5102 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
5103 sequence_a->element_type, sequence_b->element_type);
5104 }
5105
5106 end:
5107 return ret;
5108 }
5109
5110 int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a,
5111 struct bt_ctf_field_type *type_b)
5112 {
5113 int ret = 1;
5114
5115 if (type_a == type_b) {
5116 /* Same reference: equal (even if both are NULL) */
5117 ret = 0;
5118 goto end;
5119 }
5120
5121 if (!type_a) {
5122 BT_LOGW_STR("Invalid parameter: field type A is NULL.");
5123 ret = -1;
5124 goto end;
5125 }
5126
5127 if (!type_b) {
5128 BT_LOGW_STR("Invalid parameter: field type B is NULL.");
5129 ret = -1;
5130 goto end;
5131 }
5132
5133 if (type_a->id != type_b->id) {
5134 /* Different type IDs */
5135 BT_LOGV("Field types differ: different IDs: "
5136 "ft-a-addr=%p, ft-b-addr=%p, "
5137 "ft-a-id=%s, ft-b-id=%s",
5138 type_a, type_b,
5139 bt_ctf_field_type_id_string(type_a->id),
5140 bt_ctf_field_type_id_string(type_b->id));
5141 goto end;
5142 }
5143
5144 if (type_a->id == BT_CTF_FIELD_TYPE_ID_UNKNOWN) {
5145 /* Both have unknown type IDs */
5146 BT_LOGW_STR("Invalid parameter: field type IDs are unknown.");
5147 goto end;
5148 }
5149
5150 ret = type_compare_funcs[type_a->id](type_a, type_b);
5151 if (ret == 1) {
5152 BT_LOGV("Field types differ: ft-a-addr=%p, ft-b-addr=%p",
5153 type_a, type_b);
5154 }
5155
5156 end:
5157 return ret;
5158 }
5159
5160 BT_HIDDEN
5161 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *field_type)
5162 {
5163 int64_t field_count = -1;
5164 enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
5165
5166 switch (type_id) {
5167 case CTF_TYPE_STRUCT:
5168 field_count =
5169 bt_ctf_field_type_structure_get_field_count(field_type);
5170 break;
5171 case CTF_TYPE_VARIANT:
5172 field_count =
5173 bt_ctf_field_type_variant_get_field_count(field_type);
5174 break;
5175 case CTF_TYPE_ARRAY:
5176 case CTF_TYPE_SEQUENCE:
5177 /*
5178 * Array and sequence types always contain a single member
5179 * (the element type).
5180 */
5181 field_count = 1;
5182 break;
5183 default:
5184 break;
5185 }
5186
5187 return field_count;
5188 }
5189
5190 BT_HIDDEN
5191 struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
5192 struct bt_ctf_field_type *field_type, int index)
5193 {
5194 struct bt_ctf_field_type *field = NULL;
5195 enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
5196
5197 switch (type_id) {
5198 case CTF_TYPE_STRUCT:
5199 bt_ctf_field_type_structure_get_field_by_index(field_type,
5200 NULL, &field, index);
5201 break;
5202 case CTF_TYPE_VARIANT:
5203 {
5204 int ret = bt_ctf_field_type_variant_get_field_by_index(
5205 field_type, NULL, &field, index);
5206 if (ret) {
5207 field = NULL;
5208 goto end;
5209 }
5210 break;
5211 }
5212 case CTF_TYPE_ARRAY:
5213 field = bt_ctf_field_type_array_get_element_type(field_type);
5214 break;
5215 case CTF_TYPE_SEQUENCE:
5216 field = bt_ctf_field_type_sequence_get_element_type(field_type);
5217 break;
5218 default:
5219 break;
5220 }
5221 end:
5222 return field;
5223 }
5224
5225 BT_HIDDEN
5226 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *field_type,
5227 const char *name)
5228 {
5229 int field_index = -1;
5230 enum bt_ctf_field_type_id type_id = bt_ctf_field_type_get_type_id(field_type);
5231
5232 switch (type_id) {
5233 case CTF_TYPE_STRUCT:
5234 field_index = bt_ctf_field_type_structure_get_field_name_index(
5235 field_type, name);
5236 break;
5237 case CTF_TYPE_VARIANT:
5238 field_index = bt_ctf_field_type_variant_get_field_name_index(
5239 field_type, name);
5240 break;
5241 default:
5242 break;
5243 }
5244
5245 return field_index;
5246 }
5247
5248 struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path(
5249 struct bt_ctf_field_type *type)
5250 {
5251 struct bt_ctf_field_path *field_path = NULL;
5252 struct bt_ctf_field_type_variant *variant;
5253
5254 if (!type) {
5255 BT_LOGW_STR("Invalid parameter: field type is NULL.");
5256 goto end;
5257 }
5258
5259 if (!bt_ctf_field_type_is_variant(type)) {
5260 BT_LOGW("Invalid parameter: field type is not a variant field type: "
5261 "addr=%p, ft-id=%s", type,
5262 bt_ctf_field_type_id_string(type->id));
5263 goto end;
5264 }
5265
5266 variant = container_of(type, struct bt_ctf_field_type_variant,
5267 parent);
5268 field_path = bt_get(variant->tag_field_path);
5269 end:
5270 return field_path;
5271 }
5272
5273 struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path(
5274 struct bt_ctf_field_type *type)
5275 {
5276 struct bt_ctf_field_path *field_path = NULL;
5277 struct bt_ctf_field_type_sequence *sequence;
5278
5279 if (!type) {
5280 BT_LOGW_STR("Invalid parameter: field type is NULL.");
5281 goto end;
5282 }
5283
5284 if (!bt_ctf_field_type_is_sequence(type)) {
5285 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
5286 "addr=%p, ft-id=%s", type,
5287 bt_ctf_field_type_id_string(type->id));
5288 goto end;
5289 }
5290
5291 sequence = container_of(type, struct bt_ctf_field_type_sequence,
5292 parent);
5293 field_path = bt_get(sequence->length_field_path);
5294 end:
5295 return field_path;
5296 }
This page took 0.175167 seconds and 5 git commands to generate.