`ctf` plugin: Use is_log_level_set to infer ctf_event_class log_level validity
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta.h
CommitLineData
44c440bc
PP
1#ifndef _CTF_META_H
2#define _CTF_META_H
3
4/*
5 * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 */
17
3fadfbc0 18#include <babeltrace2/babeltrace.h>
578e048b 19#include "common/common.h"
6162e6b7 20#include "common/uuid.h"
578e048b 21#include "common/assert.h"
44c440bc
PP
22#include <glib.h>
23#include <stdint.h>
24#include <string.h>
25
864cad70
PP
26enum ctf_field_class_type {
27 CTF_FIELD_CLASS_TYPE_INT,
28 CTF_FIELD_CLASS_TYPE_ENUM,
29 CTF_FIELD_CLASS_TYPE_FLOAT,
30 CTF_FIELD_CLASS_TYPE_STRING,
31 CTF_FIELD_CLASS_TYPE_STRUCT,
32 CTF_FIELD_CLASS_TYPE_ARRAY,
33 CTF_FIELD_CLASS_TYPE_SEQUENCE,
34 CTF_FIELD_CLASS_TYPE_VARIANT,
44c440bc
PP
35};
36
5cd6d0e5
PP
37enum ctf_field_class_meaning {
38 CTF_FIELD_CLASS_MEANING_NONE,
39 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
40 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
41 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
42 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
43 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
44 CTF_FIELD_CLASS_MEANING_MAGIC,
45 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
46 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
47 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
48 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
49 CTF_FIELD_CLASS_MEANING_UUID,
44c440bc
PP
50};
51
52enum ctf_byte_order {
53 CTF_BYTE_ORDER_DEFAULT,
54 CTF_BYTE_ORDER_LITTLE,
55 CTF_BYTE_ORDER_BIG,
56};
57
58enum ctf_encoding {
59 CTF_ENCODING_NONE,
60 CTF_ENCODING_UTF8,
61};
62
83ebb7f1
PP
63enum ctf_scope {
64 CTF_SCOPE_PACKET_HEADER,
65 CTF_SCOPE_PACKET_CONTEXT,
66 CTF_SCOPE_EVENT_HEADER,
67 CTF_SCOPE_EVENT_COMMON_CONTEXT,
68 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
69 CTF_SCOPE_EVENT_PAYLOAD,
70};
71
0f2d58c9
PP
72struct ctf_clock_class {
73 GString *name;
74 GString *description;
75 uint64_t frequency;
76 uint64_t precision;
77 int64_t offset_seconds;
78 uint64_t offset_cycles;
6162e6b7 79 bt_uuid_t uuid;
0f2d58c9
PP
80 bool has_uuid;
81 bool is_absolute;
82
83 /* Weak, set during translation */
84 bt_clock_class *ir_cc;
85};
86
5cd6d0e5 87struct ctf_field_class {
864cad70 88 enum ctf_field_class_type type;
44c440bc
PP
89 unsigned int alignment;
90 bool is_compound;
91 bool in_ir;
92
93 /* Weak, set during translation. NULL if `in_ir` is false below. */
b19ff26f 94 bt_field_class *ir_fc;
44c440bc
PP
95};
96
5cd6d0e5
PP
97struct ctf_field_class_bit_array {
98 struct ctf_field_class base;
44c440bc
PP
99 enum ctf_byte_order byte_order;
100 unsigned int size;
101};
102
5cd6d0e5
PP
103struct ctf_field_class_int {
104 struct ctf_field_class_bit_array base;
105 enum ctf_field_class_meaning meaning;
44c440bc 106 bool is_signed;
4cdfc5e8 107 bt_field_class_integer_preferred_display_base disp_base;
44c440bc
PP
108 enum ctf_encoding encoding;
109 int64_t storing_index;
110
0f2d58c9
PP
111 /* Weak */
112 struct ctf_clock_class *mapped_clock_class;
44c440bc
PP
113};
114
115struct ctf_range {
116 union {
117 uint64_t u;
118 int64_t i;
119 } lower;
120
121 union {
122 uint64_t u;
123 int64_t i;
124 } upper;
125};
126
5cd6d0e5 127struct ctf_field_class_enum_mapping {
44c440bc 128 GString *label;
45c51519
PP
129
130 /* Array of `struct ctf_range` */
131 GArray *ranges;
44c440bc
PP
132};
133
5cd6d0e5
PP
134struct ctf_field_class_enum {
135 struct ctf_field_class_int base;
44c440bc 136
5cd6d0e5 137 /* Array of `struct ctf_field_class_enum_mapping` */
44c440bc
PP
138 GArray *mappings;
139};
140
5cd6d0e5
PP
141struct ctf_field_class_float {
142 struct ctf_field_class_bit_array base;
44c440bc
PP
143};
144
5cd6d0e5
PP
145struct ctf_field_class_string {
146 struct ctf_field_class base;
44c440bc
PP
147 enum ctf_encoding encoding;
148};
149
5cd6d0e5 150struct ctf_named_field_class {
45c51519
PP
151 /* Original name which can include a leading `_` */
152 GString *orig_name;
153
154 /* Name as translated to trace IR (leading `_` removed) */
44c440bc
PP
155 GString *name;
156
157 /* Owned by this */
5cd6d0e5 158 struct ctf_field_class *fc;
44c440bc
PP
159};
160
5cd6d0e5
PP
161struct ctf_field_class_struct {
162 struct ctf_field_class base;
44c440bc 163
5cd6d0e5 164 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
165 GArray *members;
166};
167
168struct ctf_field_path {
83ebb7f1 169 enum ctf_scope root;
44c440bc
PP
170
171 /* Array of `int64_t` */
172 GArray *path;
173};
174
5cd6d0e5 175struct ctf_field_class_variant_range {
44c440bc
PP
176 struct ctf_range range;
177 uint64_t option_index;
178};
179
5cd6d0e5
PP
180struct ctf_field_class_variant {
181 struct ctf_field_class base;
44c440bc
PP
182 GString *tag_ref;
183 struct ctf_field_path tag_path;
184 uint64_t stored_tag_index;
185
5cd6d0e5 186 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
187 GArray *options;
188
5cd6d0e5 189 /* Array of `struct ctf_field_class_variant_range` */
44c440bc
PP
190 GArray *ranges;
191
192 /* Weak */
5cd6d0e5 193 struct ctf_field_class_enum *tag_fc;
44c440bc
PP
194};
195
5cd6d0e5
PP
196struct ctf_field_class_array_base {
197 struct ctf_field_class base;
198 struct ctf_field_class *elem_fc;
44c440bc
PP
199 bool is_text;
200};
201
5cd6d0e5
PP
202struct ctf_field_class_array {
203 struct ctf_field_class_array_base base;
204 enum ctf_field_class_meaning meaning;
44c440bc
PP
205 uint64_t length;
206};
207
5cd6d0e5
PP
208struct ctf_field_class_sequence {
209 struct ctf_field_class_array_base base;
44c440bc
PP
210 GString *length_ref;
211 struct ctf_field_path length_path;
212 uint64_t stored_length_index;
213
214 /* Weak */
5cd6d0e5 215 struct ctf_field_class_int *length_fc;
44c440bc
PP
216};
217
218struct ctf_event_class {
219 GString *name;
220 uint64_t id;
221 GString *emf_uri;
4cdfc5e8 222 bt_event_class_log_level log_level;
44c440bc 223 bool is_translated;
6da709aa 224 bool is_log_level_set;
44c440bc
PP
225
226 /* Owned by this */
5cd6d0e5 227 struct ctf_field_class *spec_context_fc;
44c440bc
PP
228
229 /* Owned by this */
5cd6d0e5 230 struct ctf_field_class *payload_fc;
44c440bc
PP
231
232 /* Weak, set during translation */
b19ff26f 233 bt_event_class *ir_ec;
44c440bc
PP
234};
235
236struct ctf_stream_class {
237 uint64_t id;
238 bool is_translated;
afd45274
PP
239 bool packets_have_ts_begin;
240 bool packets_have_ts_end;
241 bool has_discarded_events;
242 bool has_discarded_packets;
243 bool discarded_events_have_default_cs;
244 bool discarded_packets_have_default_cs;
44c440bc
PP
245
246 /* Owned by this */
5cd6d0e5 247 struct ctf_field_class *packet_context_fc;
44c440bc
PP
248
249 /* Owned by this */
5cd6d0e5 250 struct ctf_field_class *event_header_fc;
44c440bc
PP
251
252 /* Owned by this */
5cd6d0e5 253 struct ctf_field_class *event_common_context_fc;
44c440bc
PP
254
255 /* Array of `struct ctf_event_class *`, owned by this */
256 GPtrArray *event_classes;
257
258 /*
259 * Hash table mapping event class IDs to `struct ctf_event_class *`,
260 * weak.
261 */
262 GHashTable *event_classes_by_id;
263
0f2d58c9
PP
264 /* Weak */
265 struct ctf_clock_class *default_clock_class;
44c440bc
PP
266
267 /* Weak, set during translation */
b19ff26f 268 bt_stream_class *ir_sc;
44c440bc
PP
269};
270
271enum ctf_trace_class_env_entry_type {
272 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
273 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
274};
275
276struct ctf_trace_class_env_entry {
277 enum ctf_trace_class_env_entry_type type;
278 GString *name;
279
280 struct {
281 int64_t i;
282 GString *str;
283 } value;
284};
285
286struct ctf_trace_class {
44c440bc
PP
287 unsigned int major;
288 unsigned int minor;
6162e6b7 289 bt_uuid_t uuid;
44c440bc
PP
290 bool is_uuid_set;
291 enum ctf_byte_order default_byte_order;
292
293 /* Owned by this */
5cd6d0e5 294 struct ctf_field_class *packet_header_fc;
44c440bc
PP
295
296 uint64_t stored_value_count;
297
0f2d58c9 298 /* Array of `struct ctf_clock_class *` (owned by this) */
44c440bc
PP
299 GPtrArray *clock_classes;
300
301 /* Array of `struct ctf_stream_class *` */
302 GPtrArray *stream_classes;
303
304 /* Array of `struct ctf_trace_class_env_entry` */
305 GArray *env_entries;
306
307 bool is_translated;
308
309 /* Weak, set during translation */
b19ff26f 310 bt_trace_class *ir_tc;
44c440bc
PP
311};
312
313static inline
5cd6d0e5 314void ctf_field_class_destroy(struct ctf_field_class *fc);
44c440bc
PP
315
316static inline
864cad70
PP
317void _ctf_field_class_init(struct ctf_field_class *fc,
318 enum ctf_field_class_type type, unsigned int alignment)
44c440bc 319{
5cd6d0e5 320 BT_ASSERT(fc);
864cad70 321 fc->type = type;
5cd6d0e5
PP
322 fc->alignment = alignment;
323 fc->in_ir = false;
44c440bc
PP
324}
325
326static inline
5cd6d0e5 327void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
864cad70 328 enum ctf_field_class_type type)
44c440bc 329{
864cad70 330 _ctf_field_class_init((void *) fc, type, 1);
44c440bc
PP
331}
332
333static inline
5cd6d0e5 334void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
864cad70 335 enum ctf_field_class_type type)
44c440bc 336{
864cad70 337 _ctf_field_class_bit_array_init((void *) fc, type);
5cd6d0e5
PP
338 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
339 fc->storing_index = -1;
44c440bc
PP
340}
341
342static inline
343void ctf_field_path_init(struct ctf_field_path *field_path)
344{
345 BT_ASSERT(field_path);
346 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
347 BT_ASSERT(field_path->path);
348}
349
350static inline
351void ctf_field_path_fini(struct ctf_field_path *field_path)
352{
353 BT_ASSERT(field_path);
354
355 if (field_path->path) {
356 g_array_free(field_path->path, TRUE);
357 }
358}
359
360static inline
5cd6d0e5 361void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
44c440bc 362{
5cd6d0e5
PP
363 BT_ASSERT(named_fc);
364 named_fc->name = g_string_new(NULL);
365 BT_ASSERT(named_fc->name);
45c51519
PP
366 named_fc->orig_name = g_string_new(NULL);
367 BT_ASSERT(named_fc->orig_name);
44c440bc
PP
368}
369
370static inline
5cd6d0e5 371void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
44c440bc 372{
5cd6d0e5 373 BT_ASSERT(named_fc);
44c440bc 374
5cd6d0e5
PP
375 if (named_fc->name) {
376 g_string_free(named_fc->name, TRUE);
44c440bc
PP
377 }
378
45c51519
PP
379 if (named_fc->orig_name) {
380 g_string_free(named_fc->orig_name, TRUE);
381 }
382
5cd6d0e5 383 ctf_field_class_destroy(named_fc->fc);
44c440bc
PP
384}
385
386static inline
5cd6d0e5
PP
387void _ctf_field_class_enum_mapping_init(
388 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
389{
390 BT_ASSERT(mapping);
391 mapping->label = g_string_new(NULL);
392 BT_ASSERT(mapping->label);
45c51519
PP
393 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
394 BT_ASSERT(mapping->ranges);
44c440bc
PP
395}
396
397static inline
5cd6d0e5
PP
398void _ctf_field_class_enum_mapping_fini(
399 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
400{
401 BT_ASSERT(mapping);
402
403 if (mapping->label) {
404 g_string_free(mapping->label, TRUE);
405 }
45c51519
PP
406
407 if (mapping->ranges) {
408 g_array_free(mapping->ranges, TRUE);
409 }
44c440bc
PP
410}
411
412static inline
5cd6d0e5 413struct ctf_field_class_int *ctf_field_class_int_create(void)
44c440bc 414{
5cd6d0e5 415 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
44c440bc 416
5cd6d0e5 417 BT_ASSERT(fc);
864cad70 418 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
5cd6d0e5 419 return fc;
44c440bc
PP
420}
421
422static inline
5cd6d0e5 423struct ctf_field_class_float *ctf_field_class_float_create(void)
44c440bc 424{
5cd6d0e5
PP
425 struct ctf_field_class_float *fc =
426 g_new0(struct ctf_field_class_float, 1);
44c440bc 427
5cd6d0e5 428 BT_ASSERT(fc);
864cad70 429 _ctf_field_class_bit_array_init((void *) fc, CTF_FIELD_CLASS_TYPE_FLOAT);
5cd6d0e5 430 return fc;
44c440bc
PP
431}
432
433static inline
5cd6d0e5 434struct ctf_field_class_string *ctf_field_class_string_create(void)
44c440bc 435{
5cd6d0e5
PP
436 struct ctf_field_class_string *fc =
437 g_new0(struct ctf_field_class_string, 1);
44c440bc 438
5cd6d0e5 439 BT_ASSERT(fc);
864cad70 440 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRING, 8);
5cd6d0e5 441 return fc;
44c440bc
PP
442}
443
444static inline
5cd6d0e5 445struct ctf_field_class_enum *ctf_field_class_enum_create(void)
44c440bc 446{
5cd6d0e5 447 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
44c440bc 448
5cd6d0e5 449 BT_ASSERT(fc);
864cad70 450 _ctf_field_class_int_init((void *) fc, CTF_FIELD_CLASS_TYPE_ENUM);
5cd6d0e5
PP
451 fc->mappings = g_array_new(FALSE, TRUE,
452 sizeof(struct ctf_field_class_enum_mapping));
453 BT_ASSERT(fc->mappings);
454 return fc;
44c440bc
PP
455}
456
457static inline
5cd6d0e5 458struct ctf_field_class_struct *ctf_field_class_struct_create(void)
44c440bc 459{
5cd6d0e5
PP
460 struct ctf_field_class_struct *fc =
461 g_new0(struct ctf_field_class_struct, 1);
44c440bc 462
5cd6d0e5 463 BT_ASSERT(fc);
864cad70 464 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
5cd6d0e5
PP
465 fc->members = g_array_new(FALSE, TRUE,
466 sizeof(struct ctf_named_field_class));
467 BT_ASSERT(fc->members);
468 fc->base.is_compound = true;
469 return fc;
44c440bc
PP
470}
471
472static inline
5cd6d0e5 473struct ctf_field_class_variant *ctf_field_class_variant_create(void)
44c440bc 474{
5cd6d0e5
PP
475 struct ctf_field_class_variant *fc =
476 g_new0(struct ctf_field_class_variant, 1);
44c440bc 477
5cd6d0e5 478 BT_ASSERT(fc);
864cad70 479 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
5cd6d0e5
PP
480 fc->options = g_array_new(FALSE, TRUE,
481 sizeof(struct ctf_named_field_class));
482 BT_ASSERT(fc->options);
483 fc->ranges = g_array_new(FALSE, TRUE,
484 sizeof(struct ctf_field_class_variant_range));
485 BT_ASSERT(fc->ranges);
486 fc->tag_ref = g_string_new(NULL);
487 BT_ASSERT(fc->tag_ref);
488 ctf_field_path_init(&fc->tag_path);
489 fc->base.is_compound = true;
490 return fc;
44c440bc
PP
491}
492
493static inline
5cd6d0e5 494struct ctf_field_class_array *ctf_field_class_array_create(void)
44c440bc 495{
5cd6d0e5
PP
496 struct ctf_field_class_array *fc =
497 g_new0(struct ctf_field_class_array, 1);
44c440bc 498
5cd6d0e5 499 BT_ASSERT(fc);
864cad70 500 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
5cd6d0e5
PP
501 fc->base.base.is_compound = true;
502 return fc;
44c440bc
PP
503}
504
505static inline
5cd6d0e5 506struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
44c440bc 507{
5cd6d0e5
PP
508 struct ctf_field_class_sequence *fc =
509 g_new0(struct ctf_field_class_sequence, 1);
44c440bc 510
5cd6d0e5 511 BT_ASSERT(fc);
864cad70 512 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
5cd6d0e5
PP
513 fc->length_ref = g_string_new(NULL);
514 BT_ASSERT(fc->length_ref);
515 ctf_field_path_init(&fc->length_path);
516 fc->base.base.is_compound = true;
517 return fc;
44c440bc
PP
518}
519
520static inline
5cd6d0e5 521void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
44c440bc 522{
5cd6d0e5 523 BT_ASSERT(fc);
5cd6d0e5 524 g_free(fc);
44c440bc
PP
525}
526
527static inline
5cd6d0e5 528void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
44c440bc 529{
5cd6d0e5 530 BT_ASSERT(fc);
44c440bc 531
5cd6d0e5 532 if (fc->mappings) {
44c440bc
PP
533 uint64_t i;
534
5cd6d0e5
PP
535 for (i = 0; i < fc->mappings->len; i++) {
536 struct ctf_field_class_enum_mapping *mapping =
537 &g_array_index(fc->mappings,
538 struct ctf_field_class_enum_mapping, i);
44c440bc 539
5cd6d0e5 540 _ctf_field_class_enum_mapping_fini(mapping);
44c440bc
PP
541 }
542
5cd6d0e5 543 g_array_free(fc->mappings, TRUE);
44c440bc
PP
544 }
545
5cd6d0e5 546 g_free(fc);
44c440bc
PP
547}
548
549static inline
5cd6d0e5 550void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
44c440bc 551{
5cd6d0e5
PP
552 BT_ASSERT(fc);
553 g_free(fc);
44c440bc
PP
554}
555
556static inline
5cd6d0e5 557void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
44c440bc 558{
5cd6d0e5
PP
559 BT_ASSERT(fc);
560 g_free(fc);
44c440bc
PP
561}
562
563static inline
5cd6d0e5 564void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
44c440bc 565{
5cd6d0e5 566 BT_ASSERT(fc);
44c440bc 567
5cd6d0e5 568 if (fc->members) {
44c440bc
PP
569 uint64_t i;
570
5cd6d0e5
PP
571 for (i = 0; i < fc->members->len; i++) {
572 struct ctf_named_field_class *named_fc =
573 &g_array_index(fc->members,
574 struct ctf_named_field_class, i);
44c440bc 575
5cd6d0e5 576 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
577 }
578
5cd6d0e5 579 g_array_free(fc->members, TRUE);
44c440bc
PP
580 }
581
5cd6d0e5 582 g_free(fc);
44c440bc
PP
583}
584
585static inline
5cd6d0e5 586void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
44c440bc 587{
5cd6d0e5
PP
588 BT_ASSERT(fc);
589 ctf_field_class_destroy(fc->elem_fc);
44c440bc
PP
590}
591
592static inline
5cd6d0e5 593void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
44c440bc 594{
5cd6d0e5
PP
595 BT_ASSERT(fc);
596 _ctf_field_class_array_base_fini((void *) fc);
597 g_free(fc);
44c440bc
PP
598}
599
600static inline
5cd6d0e5 601void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
44c440bc 602{
5cd6d0e5
PP
603 BT_ASSERT(fc);
604 _ctf_field_class_array_base_fini((void *) fc);
44c440bc 605
5cd6d0e5
PP
606 if (fc->length_ref) {
607 g_string_free(fc->length_ref, TRUE);
44c440bc
PP
608 }
609
5cd6d0e5
PP
610 ctf_field_path_fini(&fc->length_path);
611 g_free(fc);
44c440bc
PP
612}
613
614static inline
5cd6d0e5 615void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
44c440bc 616{
5cd6d0e5 617 BT_ASSERT(fc);
44c440bc 618
5cd6d0e5 619 if (fc->options) {
44c440bc
PP
620 uint64_t i;
621
5cd6d0e5
PP
622 for (i = 0; i < fc->options->len; i++) {
623 struct ctf_named_field_class *named_fc =
624 &g_array_index(fc->options,
625 struct ctf_named_field_class, i);
44c440bc 626
5cd6d0e5 627 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
628 }
629
5cd6d0e5 630 g_array_free(fc->options, TRUE);
44c440bc
PP
631 }
632
5cd6d0e5
PP
633 if (fc->ranges) {
634 g_array_free(fc->ranges, TRUE);
44c440bc
PP
635 }
636
5cd6d0e5
PP
637 if (fc->tag_ref) {
638 g_string_free(fc->tag_ref, TRUE);
44c440bc
PP
639 }
640
5cd6d0e5
PP
641 ctf_field_path_fini(&fc->tag_path);
642 g_free(fc);
44c440bc
PP
643}
644
645static inline
5cd6d0e5 646void ctf_field_class_destroy(struct ctf_field_class *fc)
44c440bc 647{
5cd6d0e5 648 if (!fc) {
44c440bc
PP
649 return;
650 }
651
864cad70
PP
652 switch (fc->type) {
653 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 654 _ctf_field_class_int_destroy((void *) fc);
44c440bc 655 break;
864cad70 656 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 657 _ctf_field_class_enum_destroy((void *) fc);
44c440bc 658 break;
864cad70 659 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 660 _ctf_field_class_float_destroy((void *) fc);
44c440bc 661 break;
864cad70 662 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 663 _ctf_field_class_string_destroy((void *) fc);
44c440bc 664 break;
864cad70 665 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 666 _ctf_field_class_struct_destroy((void *) fc);
44c440bc 667 break;
864cad70 668 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 669 _ctf_field_class_array_destroy((void *) fc);
44c440bc 670 break;
864cad70 671 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 672 _ctf_field_class_sequence_destroy((void *) fc);
44c440bc 673 break;
864cad70 674 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 675 _ctf_field_class_variant_destroy((void *) fc);
44c440bc
PP
676 break;
677 default:
678 abort();
679 }
680}
681
682static inline
45c51519
PP
683struct ctf_range *ctf_field_class_enum_mapping_borrow_range_by_index(
684 struct ctf_field_class_enum_mapping *mapping, uint64_t index)
44c440bc 685{
45c51519
PP
686 BT_ASSERT(mapping);
687 BT_ASSERT(index < mapping->ranges->len);
688 return &g_array_index(mapping->ranges, struct ctf_range, index);
44c440bc
PP
689}
690
691static inline
5cd6d0e5
PP
692struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_index(
693 struct ctf_field_class_enum *fc, uint64_t index)
44c440bc 694{
5cd6d0e5
PP
695 BT_ASSERT(fc);
696 BT_ASSERT(index < fc->mappings->len);
697 return &g_array_index(fc->mappings, struct ctf_field_class_enum_mapping,
44c440bc
PP
698 index);
699}
700
45c51519
PP
701static inline
702struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_label(
703 struct ctf_field_class_enum *fc, const char *label)
704{
705 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
706 uint64_t i;
707
708 BT_ASSERT(fc);
709 BT_ASSERT(label);
710
711 for (i = 0; i < fc->mappings->len; i++) {
712 struct ctf_field_class_enum_mapping *mapping =
713 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
714
715 if (strcmp(mapping->label->str, label) == 0) {
716 ret_mapping = mapping;
717 goto end;
718 }
719 }
720
721end:
722 return ret_mapping;
723}
724
725static inline
726void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
727 const char *label, uint64_t u_lower, uint64_t u_upper)
728{
729 struct ctf_field_class_enum_mapping *mapping = NULL;
730 struct ctf_range range = {
731 .lower.u = u_lower,
732 .upper.u = u_upper,
733 };
734 uint64_t i;
735
736 BT_ASSERT(fc);
737 BT_ASSERT(label);
738
739 for (i = 0; i < fc->mappings->len; i++) {
740 mapping = ctf_field_class_enum_borrow_mapping_by_index(
741 fc, i);
742
743 if (strcmp(mapping->label->str, label) == 0) {
744 break;
745 }
746 }
747
748 if (i == fc->mappings->len) {
749 mapping = NULL;
750 }
751
752 if (!mapping) {
753 g_array_set_size(fc->mappings, fc->mappings->len + 1);
754 mapping = ctf_field_class_enum_borrow_mapping_by_index(
755 fc, fc->mappings->len - 1);
756 _ctf_field_class_enum_mapping_init(mapping);
757 g_string_assign(mapping->label, label);
758 }
759
760 g_array_append_val(mapping->ranges, range);
761}
762
44c440bc 763static inline
5cd6d0e5
PP
764struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_index(
765 struct ctf_field_class_struct *fc, uint64_t index)
44c440bc 766{
5cd6d0e5
PP
767 BT_ASSERT(fc);
768 BT_ASSERT(index < fc->members->len);
769 return &g_array_index(fc->members, struct ctf_named_field_class,
44c440bc
PP
770 index);
771}
772
773static inline
5cd6d0e5
PP
774struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_name(
775 struct ctf_field_class_struct *fc, const char *name)
44c440bc
PP
776{
777 uint64_t i;
5cd6d0e5 778 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 779
5cd6d0e5 780 BT_ASSERT(fc);
44c440bc
PP
781 BT_ASSERT(name);
782
5cd6d0e5
PP
783 for (i = 0; i < fc->members->len; i++) {
784 struct ctf_named_field_class *named_fc =
785 ctf_field_class_struct_borrow_member_by_index(fc, i);
44c440bc 786
5cd6d0e5
PP
787 if (strcmp(name, named_fc->name->str) == 0) {
788 ret_named_fc = named_fc;
44c440bc
PP
789 goto end;
790 }
791 }
792
793end:
5cd6d0e5 794 return ret_named_fc;
44c440bc
PP
795}
796
797static inline
5cd6d0e5
PP
798struct ctf_field_class *ctf_field_class_struct_borrow_member_field_class_by_name(
799 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 800{
5cd6d0e5
PP
801 struct ctf_named_field_class *named_fc = NULL;
802 struct ctf_field_class *fc = NULL;
44c440bc 803
5cd6d0e5 804 if (!struct_fc) {
44c440bc
PP
805 goto end;
806 }
807
5cd6d0e5
PP
808 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
809 if (!named_fc) {
44c440bc
PP
810 goto end;
811 }
812
5cd6d0e5 813 fc = named_fc->fc;
44c440bc
PP
814
815end:
5cd6d0e5 816 return fc;
44c440bc
PP
817}
818
819static inline
5cd6d0e5
PP
820struct ctf_field_class_int *
821ctf_field_class_struct_borrow_member_int_field_class_by_name(
822 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 823{
5cd6d0e5 824 struct ctf_field_class_int *int_fc = NULL;
44c440bc 825
5cd6d0e5
PP
826 int_fc = (void *)
827 ctf_field_class_struct_borrow_member_field_class_by_name(
828 struct_fc, name);
829 if (!int_fc) {
44c440bc
PP
830 goto end;
831 }
832
864cad70
PP
833 if (int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_INT &&
834 int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_ENUM) {
5cd6d0e5 835 int_fc = NULL;
44c440bc
PP
836 goto end;
837 }
838
839end:
5cd6d0e5 840 return int_fc;
44c440bc
PP
841}
842
45c51519
PP
843static inline
844void _ctf_named_field_class_unescape_orig_name(
845 struct ctf_named_field_class *named_fc)
846{
847 const char *name = named_fc->orig_name->str;
848
849 if (name[0] == '_') {
850 name++;
851 }
852
853 g_string_assign(named_fc->name, name);
854}
44c440bc
PP
855
856static inline
5cd6d0e5 857void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
45c51519 858 const char *orig_name, struct ctf_field_class *member_fc)
44c440bc 859{
5cd6d0e5 860 struct ctf_named_field_class *named_fc;
44c440bc 861
5cd6d0e5 862 BT_ASSERT(fc);
45c51519 863 BT_ASSERT(orig_name);
5cd6d0e5 864 g_array_set_size(fc->members, fc->members->len + 1);
44c440bc 865
5cd6d0e5
PP
866 named_fc = &g_array_index(fc->members, struct ctf_named_field_class,
867 fc->members->len - 1);
868 _ctf_named_field_class_init(named_fc);
45c51519
PP
869 g_string_assign(named_fc->orig_name, orig_name);
870 _ctf_named_field_class_unescape_orig_name(named_fc);
5cd6d0e5 871 named_fc->fc = member_fc;
44c440bc 872
5cd6d0e5
PP
873 if (member_fc->alignment > fc->base.alignment) {
874 fc->base.alignment = member_fc->alignment;
44c440bc
PP
875 }
876}
877
878static inline
5cd6d0e5
PP
879struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_index(
880 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 881{
5cd6d0e5
PP
882 BT_ASSERT(fc);
883 BT_ASSERT(index < fc->options->len);
884 return &g_array_index(fc->options, struct ctf_named_field_class,
44c440bc
PP
885 index);
886}
887
888static inline
5cd6d0e5
PP
889struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_name(
890 struct ctf_field_class_variant *fc, const char *name)
44c440bc
PP
891{
892 uint64_t i;
5cd6d0e5 893 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 894
5cd6d0e5 895 BT_ASSERT(fc);
44c440bc
PP
896 BT_ASSERT(name);
897
5cd6d0e5
PP
898 for (i = 0; i < fc->options->len; i++) {
899 struct ctf_named_field_class *named_fc =
900 ctf_field_class_variant_borrow_option_by_index(fc, i);
44c440bc 901
5cd6d0e5
PP
902 if (strcmp(name, named_fc->name->str) == 0) {
903 ret_named_fc = named_fc;
44c440bc
PP
904 goto end;
905 }
906 }
907
908end:
5cd6d0e5 909 return ret_named_fc;
44c440bc
PP
910}
911
912static inline
5cd6d0e5
PP
913struct ctf_field_class_variant_range *
914ctf_field_class_variant_borrow_range_by_index(
915 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 916{
5cd6d0e5
PP
917 BT_ASSERT(fc);
918 BT_ASSERT(index < fc->ranges->len);
919 return &g_array_index(fc->ranges, struct ctf_field_class_variant_range,
44c440bc
PP
920 index);
921}
922
923static inline
5cd6d0e5 924void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
45c51519 925 const char *orig_name, struct ctf_field_class *option_fc)
44c440bc 926{
5cd6d0e5 927 struct ctf_named_field_class *named_fc;
44c440bc 928
5cd6d0e5 929 BT_ASSERT(fc);
45c51519 930 BT_ASSERT(orig_name);
5cd6d0e5 931 g_array_set_size(fc->options, fc->options->len + 1);
44c440bc 932
5cd6d0e5
PP
933 named_fc = &g_array_index(fc->options, struct ctf_named_field_class,
934 fc->options->len - 1);
935 _ctf_named_field_class_init(named_fc);
45c51519
PP
936 g_string_assign(named_fc->orig_name, orig_name);
937 _ctf_named_field_class_unescape_orig_name(named_fc);
5cd6d0e5 938 named_fc->fc = option_fc;
44c440bc
PP
939}
940
941static inline
5cd6d0e5
PP
942void ctf_field_class_variant_set_tag_field_class(
943 struct ctf_field_class_variant *fc,
944 struct ctf_field_class_enum *tag_fc)
44c440bc
PP
945{
946 uint64_t option_i;
947
5cd6d0e5
PP
948 BT_ASSERT(fc);
949 BT_ASSERT(tag_fc);
950 fc->tag_fc = tag_fc;
44c440bc 951
5cd6d0e5 952 for (option_i = 0; option_i < fc->options->len; option_i++) {
45c51519 953 uint64_t range_i;
5cd6d0e5
PP
954 struct ctf_named_field_class *named_fc =
955 ctf_field_class_variant_borrow_option_by_index(
956 fc, option_i);
45c51519 957 struct ctf_field_class_enum_mapping *mapping;
44c440bc 958
45c51519
PP
959 mapping = ctf_field_class_enum_borrow_mapping_by_label(
960 tag_fc, named_fc->orig_name->str);
961 if (!mapping) {
962 continue;
963 }
44c440bc 964
45c51519
PP
965 for (range_i = 0; range_i < mapping->ranges->len;
966 range_i++) {
967 struct ctf_range *range =
968 ctf_field_class_enum_mapping_borrow_range_by_index(
969 mapping, range_i);
970 struct ctf_field_class_variant_range var_range;
44c440bc 971
45c51519
PP
972 var_range.range = *range;
973 var_range.option_index = option_i;
974 g_array_append_val(fc->ranges, var_range);
44c440bc
PP
975 }
976 }
977}
978
979static inline
5cd6d0e5
PP
980struct ctf_field_class *ctf_field_class_compound_borrow_field_class_by_index(
981 struct ctf_field_class *comp_fc, uint64_t index)
44c440bc 982{
5cd6d0e5 983 struct ctf_field_class *fc = NULL;
44c440bc 984
864cad70
PP
985 switch (comp_fc->type) {
986 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 987 {
5cd6d0e5
PP
988 struct ctf_named_field_class *named_fc =
989 ctf_field_class_struct_borrow_member_by_index(
990 (void *) comp_fc, index);
44c440bc 991
5cd6d0e5
PP
992 BT_ASSERT(named_fc);
993 fc = named_fc->fc;
44c440bc
PP
994 break;
995 }
864cad70 996 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 997 {
5cd6d0e5
PP
998 struct ctf_named_field_class *named_fc =
999 ctf_field_class_variant_borrow_option_by_index(
1000 (void *) comp_fc, index);
44c440bc 1001
5cd6d0e5
PP
1002 BT_ASSERT(named_fc);
1003 fc = named_fc->fc;
44c440bc
PP
1004 break;
1005 }
864cad70
PP
1006 case CTF_FIELD_CLASS_TYPE_ARRAY:
1007 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc 1008 {
5cd6d0e5 1009 struct ctf_field_class_array_base *array_fc = (void *) comp_fc;
44c440bc 1010
5cd6d0e5 1011 fc = array_fc->elem_fc;
44c440bc
PP
1012 break;
1013 }
1014 default:
1015 break;
1016 }
1017
5cd6d0e5 1018 return fc;
44c440bc
PP
1019}
1020
1021static inline
5cd6d0e5 1022uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
44c440bc
PP
1023{
1024 uint64_t field_count;
1025
864cad70
PP
1026 switch (fc->type) {
1027 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 1028 {
5cd6d0e5 1029 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 1030
5cd6d0e5 1031 field_count = struct_fc->members->len;
44c440bc
PP
1032 break;
1033 }
864cad70 1034 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1035 {
5cd6d0e5 1036 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 1037
5cd6d0e5 1038 field_count = var_fc->options->len;
44c440bc
PP
1039 break;
1040 }
864cad70
PP
1041 case CTF_FIELD_CLASS_TYPE_ARRAY:
1042 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc
PP
1043 /*
1044 * Array and sequence types always contain a single
1045 * member (the element type).
1046 */
1047 field_count = 1;
1048 break;
1049 default:
1050 abort();
1051 }
1052
1053 return field_count;
1054}
1055
1056static inline
45c51519
PP
1057int64_t ctf_field_class_compound_get_field_class_index_from_orig_name(
1058 struct ctf_field_class *fc, const char *orig_name)
44c440bc
PP
1059{
1060 int64_t ret_index = -1;
1061 uint64_t i;
1062
864cad70
PP
1063 switch (fc->type) {
1064 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 1065 {
5cd6d0e5 1066 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 1067
5cd6d0e5
PP
1068 for (i = 0; i < struct_fc->members->len; i++) {
1069 struct ctf_named_field_class *named_fc =
1070 ctf_field_class_struct_borrow_member_by_index(
1071 struct_fc, i);
44c440bc 1072
45c51519 1073 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
44c440bc
PP
1074 ret_index = (int64_t) i;
1075 goto end;
1076 }
1077 }
1078
1079 break;
1080 }
864cad70 1081 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 1082 {
5cd6d0e5 1083 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 1084
5cd6d0e5
PP
1085 for (i = 0; i < var_fc->options->len; i++) {
1086 struct ctf_named_field_class *named_fc =
1087 ctf_field_class_variant_borrow_option_by_index(
1088 var_fc, i);
44c440bc 1089
45c51519 1090 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
44c440bc
PP
1091 ret_index = (int64_t) i;
1092 goto end;
1093 }
1094 }
1095
1096 break;
1097 }
1098 default:
1099 break;
1100 }
1101
1102end:
1103 return ret_index;
1104}
1105
1106static inline
1107void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1108{
1109 BT_ASSERT(fp);
1110 g_array_append_val(fp->path, index);
1111}
1112
1113static inline
1114int64_t ctf_field_path_borrow_index_by_index(struct ctf_field_path *fp,
1115 uint64_t index)
1116{
1117 BT_ASSERT(fp);
1118 BT_ASSERT(index < fp->path->len);
1119 return g_array_index(fp->path, int64_t, index);
1120}
1121
1122static inline
1123void ctf_field_path_clear(struct ctf_field_path *fp)
1124{
1125 BT_ASSERT(fp);
1126 g_array_set_size(fp->path, 0);
1127}
1128
83ebb7f1
PP
1129static inline
1130const char *ctf_scope_string(enum ctf_scope scope)
1131{
1132 switch (scope) {
1133 case CTF_SCOPE_PACKET_HEADER:
8a432889 1134 return "PACKET_HEADER";
83ebb7f1 1135 case CTF_SCOPE_PACKET_CONTEXT:
8a432889 1136 return "PACKET_CONTEXT";
83ebb7f1 1137 case CTF_SCOPE_EVENT_HEADER:
8a432889 1138 return "EVENT_HEADER";
83ebb7f1 1139 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
8a432889 1140 return "EVENT_COMMON_CONTEXT";
83ebb7f1 1141 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
8a432889 1142 return "EVENT_SPECIFIC_CONTEXT";
83ebb7f1 1143 case CTF_SCOPE_EVENT_PAYLOAD:
8a432889 1144 return "EVENT_PAYLOAD";
83ebb7f1
PP
1145 default:
1146 abort();
1147 }
1148}
1149
44c440bc
PP
1150static inline
1151GString *ctf_field_path_string(struct ctf_field_path *path)
1152{
1153 GString *str = g_string_new(NULL);
1154 uint64_t i;
1155
1156 BT_ASSERT(path);
1157
1158 if (!str) {
1159 goto end;
1160 }
1161
83ebb7f1 1162 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
44c440bc
PP
1163
1164 for (i = 0; i < path->path->len; i++) {
1165 g_string_append_printf(str, ", %" PRId64,
1166 ctf_field_path_borrow_index_by_index(path, i));
1167 }
1168
1169 g_string_append(str, "]");
1170
1171end:
1172 return str;
1173}
1174
1175static inline
5cd6d0e5 1176struct ctf_field_class *ctf_field_path_borrow_field_class(
44c440bc
PP
1177 struct ctf_field_path *field_path,
1178 struct ctf_trace_class *tc,
1179 struct ctf_stream_class *sc,
1180 struct ctf_event_class *ec)
1181{
1182 uint64_t i;
5cd6d0e5 1183 struct ctf_field_class *fc;
44c440bc
PP
1184
1185 switch (field_path->root) {
83ebb7f1 1186 case CTF_SCOPE_PACKET_HEADER:
5cd6d0e5 1187 fc = tc->packet_header_fc;
44c440bc 1188 break;
83ebb7f1 1189 case CTF_SCOPE_PACKET_CONTEXT:
5cd6d0e5 1190 fc = sc->packet_context_fc;
44c440bc 1191 break;
83ebb7f1 1192 case CTF_SCOPE_EVENT_HEADER:
5cd6d0e5 1193 fc = sc->event_header_fc;
44c440bc 1194 break;
83ebb7f1 1195 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
5cd6d0e5 1196 fc = sc->event_common_context_fc;
44c440bc 1197 break;
83ebb7f1 1198 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
5cd6d0e5 1199 fc = ec->spec_context_fc;
44c440bc 1200 break;
83ebb7f1 1201 case CTF_SCOPE_EVENT_PAYLOAD:
5cd6d0e5 1202 fc = ec->payload_fc;
44c440bc
PP
1203 break;
1204 default:
1205 abort();
1206 }
1207
5cd6d0e5 1208 BT_ASSERT(fc);
44c440bc
PP
1209
1210 for (i = 0; i < field_path->path->len; i++) {
1211 int64_t child_index =
1212 ctf_field_path_borrow_index_by_index(field_path, i);
5cd6d0e5
PP
1213 struct ctf_field_class *child_fc =
1214 ctf_field_class_compound_borrow_field_class_by_index(
1215 fc, child_index);
1216 BT_ASSERT(child_fc);
1217 fc = child_fc;
44c440bc
PP
1218 }
1219
5cd6d0e5
PP
1220 BT_ASSERT(fc);
1221 return fc;
44c440bc
PP
1222}
1223
1224static inline
5cd6d0e5 1225struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
44c440bc
PP
1226
1227static inline
5cd6d0e5
PP
1228void ctf_field_class_bit_array_copy_content(
1229 struct ctf_field_class_bit_array *dst_fc,
1230 struct ctf_field_class_bit_array *src_fc)
44c440bc 1231{
5cd6d0e5
PP
1232 BT_ASSERT(dst_fc);
1233 BT_ASSERT(src_fc);
1234 dst_fc->byte_order = src_fc->byte_order;
1235 dst_fc->size = src_fc->size;
44c440bc
PP
1236}
1237
1238static inline
5cd6d0e5
PP
1239void ctf_field_class_int_copy_content(
1240 struct ctf_field_class_int *dst_fc,
1241 struct ctf_field_class_int *src_fc)
44c440bc 1242{
5cd6d0e5
PP
1243 ctf_field_class_bit_array_copy_content((void *) dst_fc, (void *) src_fc);
1244 dst_fc->meaning = src_fc->meaning;
1245 dst_fc->is_signed = src_fc->is_signed;
1246 dst_fc->disp_base = src_fc->disp_base;
1247 dst_fc->encoding = src_fc->encoding;
398454ed 1248 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
5cd6d0e5 1249 dst_fc->storing_index = src_fc->storing_index;
44c440bc
PP
1250}
1251
1252static inline
5cd6d0e5
PP
1253struct ctf_field_class_int *_ctf_field_class_int_copy(
1254 struct ctf_field_class_int *fc)
44c440bc 1255{
5cd6d0e5 1256 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
44c440bc 1257
5cd6d0e5
PP
1258 BT_ASSERT(copy_fc);
1259 ctf_field_class_int_copy_content(copy_fc, fc);
1260 return copy_fc;
44c440bc
PP
1261}
1262
1263static inline
5cd6d0e5
PP
1264struct ctf_field_class_enum *_ctf_field_class_enum_copy(
1265 struct ctf_field_class_enum *fc)
44c440bc 1266{
5cd6d0e5 1267 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
44c440bc
PP
1268 uint64_t i;
1269
5cd6d0e5
PP
1270 BT_ASSERT(copy_fc);
1271 ctf_field_class_int_copy_content((void *) copy_fc, (void *) fc);
44c440bc 1272
5cd6d0e5 1273 for (i = 0; i < fc->mappings->len; i++) {
45c51519
PP
1274 uint64_t range_i;
1275
5cd6d0e5
PP
1276 struct ctf_field_class_enum_mapping *mapping =
1277 &g_array_index(fc->mappings,
1278 struct ctf_field_class_enum_mapping, i);
44c440bc 1279
45c51519
PP
1280 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1281 struct ctf_range *range =
1282 &g_array_index(mapping->ranges,
1283 struct ctf_range, range_i);
1284
1285 ctf_field_class_enum_map_range(copy_fc,
1286 mapping->label->str, range->lower.u,
1287 range->upper.u);
1288 }
44c440bc
PP
1289 }
1290
5cd6d0e5 1291 return copy_fc;
44c440bc
PP
1292}
1293
1294static inline
5cd6d0e5
PP
1295struct ctf_field_class_float *_ctf_field_class_float_copy(
1296 struct ctf_field_class_float *fc)
44c440bc 1297{
5cd6d0e5 1298 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
44c440bc 1299
5cd6d0e5
PP
1300 BT_ASSERT(copy_fc);
1301 ctf_field_class_bit_array_copy_content((void *) copy_fc, (void *) fc);
1302 return copy_fc;
44c440bc
PP
1303}
1304
1305static inline
5cd6d0e5
PP
1306struct ctf_field_class_string *_ctf_field_class_string_copy(
1307 struct ctf_field_class_string *fc)
44c440bc 1308{
5cd6d0e5 1309 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
44c440bc 1310
5cd6d0e5
PP
1311 BT_ASSERT(copy_fc);
1312 return copy_fc;
44c440bc
PP
1313}
1314
1315static inline
5cd6d0e5
PP
1316struct ctf_field_class_struct *_ctf_field_class_struct_copy(
1317 struct ctf_field_class_struct *fc)
44c440bc 1318{
5cd6d0e5 1319 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
44c440bc
PP
1320 uint64_t i;
1321
5cd6d0e5 1322 BT_ASSERT(copy_fc);
44c440bc 1323
5cd6d0e5
PP
1324 for (i = 0; i < fc->members->len; i++) {
1325 struct ctf_named_field_class *named_fc =
1326 &g_array_index(fc->members,
1327 struct ctf_named_field_class, i);
44c440bc 1328
5cd6d0e5
PP
1329 ctf_field_class_struct_append_member(copy_fc,
1330 named_fc->name->str,
1331 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1332 }
1333
5cd6d0e5 1334 return copy_fc;
44c440bc
PP
1335}
1336
1337static inline
1338void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1339 struct ctf_field_path *src_fp)
1340{
1341 uint64_t i;
1342
1343 BT_ASSERT(dst_fp);
1344 BT_ASSERT(src_fp);
1345 dst_fp->root = src_fp->root;
1346 ctf_field_path_clear(dst_fp);
1347
1348 for (i = 0; i < src_fp->path->len; i++) {
1349 int64_t index = ctf_field_path_borrow_index_by_index(
1350 src_fp, i);
1351
1352 ctf_field_path_append_index(dst_fp, index);
1353 }
1354}
1355
1356static inline
5cd6d0e5
PP
1357struct ctf_field_class_variant *_ctf_field_class_variant_copy(
1358 struct ctf_field_class_variant *fc)
44c440bc 1359{
5cd6d0e5
PP
1360 struct ctf_field_class_variant *copy_fc =
1361 ctf_field_class_variant_create();
44c440bc
PP
1362 uint64_t i;
1363
5cd6d0e5 1364 BT_ASSERT(copy_fc);
44c440bc 1365
5cd6d0e5
PP
1366 for (i = 0; i < fc->options->len; i++) {
1367 struct ctf_named_field_class *named_fc =
1368 &g_array_index(fc->options,
1369 struct ctf_named_field_class, i);
44c440bc 1370
5cd6d0e5
PP
1371 ctf_field_class_variant_append_option(copy_fc,
1372 named_fc->name->str,
1373 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1374 }
1375
5cd6d0e5
PP
1376 for (i = 0; i < fc->ranges->len; i++) {
1377 struct ctf_field_class_variant_range *range =
1378 &g_array_index(fc->ranges,
1379 struct ctf_field_class_variant_range, i);
44c440bc 1380
5cd6d0e5 1381 g_array_append_val(copy_fc->ranges, *range);
44c440bc
PP
1382 }
1383
5cd6d0e5
PP
1384 ctf_field_path_copy_content(&copy_fc->tag_path, &fc->tag_path);
1385 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1386 copy_fc->stored_tag_index = fc->stored_tag_index;
1387 return copy_fc;
44c440bc
PP
1388}
1389
1390static inline
5cd6d0e5
PP
1391void ctf_field_class_array_base_copy_content(
1392 struct ctf_field_class_array_base *dst_fc,
1393 struct ctf_field_class_array_base *src_fc)
44c440bc 1394{
5cd6d0e5
PP
1395 BT_ASSERT(dst_fc);
1396 BT_ASSERT(src_fc);
1397 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1398 dst_fc->is_text = src_fc->is_text;
44c440bc
PP
1399}
1400
1401static inline
5cd6d0e5
PP
1402struct ctf_field_class_array *_ctf_field_class_array_copy(
1403 struct ctf_field_class_array *fc)
44c440bc 1404{
5cd6d0e5 1405 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
44c440bc 1406
5cd6d0e5
PP
1407 BT_ASSERT(copy_fc);
1408 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1409 copy_fc->length = fc->length;
1410 return copy_fc;
44c440bc
PP
1411}
1412
1413static inline
5cd6d0e5
PP
1414struct ctf_field_class_sequence *_ctf_field_class_sequence_copy(
1415 struct ctf_field_class_sequence *fc)
44c440bc 1416{
5cd6d0e5
PP
1417 struct ctf_field_class_sequence *copy_fc =
1418 ctf_field_class_sequence_create();
44c440bc 1419
5cd6d0e5
PP
1420 BT_ASSERT(copy_fc);
1421 ctf_field_class_array_base_copy_content((void *) copy_fc, (void *) fc);
1422 ctf_field_path_copy_content(&copy_fc->length_path, &fc->length_path);
1423 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1424 copy_fc->stored_length_index = fc->stored_length_index;
1425 return copy_fc;
44c440bc
PP
1426}
1427
1428static inline
5cd6d0e5 1429struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
44c440bc 1430{
5cd6d0e5 1431 struct ctf_field_class *copy_fc = NULL;
44c440bc 1432
5cd6d0e5 1433 if (!fc) {
44c440bc
PP
1434 goto end;
1435 }
1436
1437 /*
1438 * Translation should not have happened yet.
1439 */
5cd6d0e5 1440 BT_ASSERT(!fc->ir_fc);
44c440bc 1441
864cad70
PP
1442 switch (fc->type) {
1443 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 1444 copy_fc = (void *) _ctf_field_class_int_copy((void *) fc);
44c440bc 1445 break;
864cad70 1446 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 1447 copy_fc = (void *) _ctf_field_class_enum_copy((void *) fc);
44c440bc 1448 break;
864cad70 1449 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 1450 copy_fc = (void *) _ctf_field_class_float_copy((void *) fc);
44c440bc 1451 break;
864cad70 1452 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 1453 copy_fc = (void *) _ctf_field_class_string_copy((void *) fc);
44c440bc 1454 break;
864cad70 1455 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 1456 copy_fc = (void *) _ctf_field_class_struct_copy((void *) fc);
44c440bc 1457 break;
864cad70 1458 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 1459 copy_fc = (void *) _ctf_field_class_array_copy((void *) fc);
44c440bc 1460 break;
864cad70 1461 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 1462 copy_fc = (void *) _ctf_field_class_sequence_copy((void *) fc);
44c440bc 1463 break;
864cad70 1464 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 1465 copy_fc = (void *) _ctf_field_class_variant_copy((void *) fc);
44c440bc
PP
1466 break;
1467 default:
1468 abort();
1469 }
1470
864cad70 1471 copy_fc->type = fc->type;
5cd6d0e5
PP
1472 copy_fc->alignment = fc->alignment;
1473 copy_fc->in_ir = fc->in_ir;
44c440bc
PP
1474
1475end:
5cd6d0e5 1476 return copy_fc;
44c440bc
PP
1477}
1478
1479static inline
1480struct ctf_event_class *ctf_event_class_create(void)
1481{
1482 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1483
1484 BT_ASSERT(ec);
1485 ec->name = g_string_new(NULL);
1486 BT_ASSERT(ec->name);
1487 ec->emf_uri = g_string_new(NULL);
1488 BT_ASSERT(ec->emf_uri);
6da709aa 1489 ec->is_log_level_set = false;
44c440bc
PP
1490 return ec;
1491}
1492
6da709aa
JR
1493static inline
1494void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1495 enum bt_event_class_log_level log_level)
1496{
1497 BT_ASSERT(ec);
1498 ec->log_level = log_level;
1499 ec->is_log_level_set = true;
1500}
1501
44c440bc
PP
1502static inline
1503void ctf_event_class_destroy(struct ctf_event_class *ec)
1504{
1505 if (!ec) {
1506 return;
1507 }
1508
1509 if (ec->name) {
1510 g_string_free(ec->name, TRUE);
1511 }
1512
1513 if (ec->emf_uri) {
1514 g_string_free(ec->emf_uri, TRUE);
1515 }
1516
5cd6d0e5
PP
1517 ctf_field_class_destroy(ec->spec_context_fc);
1518 ctf_field_class_destroy(ec->payload_fc);
44c440bc
PP
1519 g_free(ec);
1520}
1521
1522static inline
1523struct ctf_stream_class *ctf_stream_class_create(void)
1524{
1525 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1526
1527 BT_ASSERT(sc);
1528 sc->event_classes = g_ptr_array_new_with_free_func(
1529 (GDestroyNotify) ctf_event_class_destroy);
1530 BT_ASSERT(sc->event_classes);
1531 sc->event_classes_by_id = g_hash_table_new(g_direct_hash,
1532 g_direct_equal);
1533 BT_ASSERT(sc->event_classes_by_id);
1534 return sc;
1535}
1536
1537static inline
1538void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1539{
1540 if (!sc) {
1541 return;
1542 }
1543
1544 if (sc->event_classes) {
1545 g_ptr_array_free(sc->event_classes, TRUE);
1546 }
1547
1548 if (sc->event_classes_by_id) {
1549 g_hash_table_destroy(sc->event_classes_by_id);
1550 }
1551
5cd6d0e5
PP
1552 ctf_field_class_destroy(sc->packet_context_fc);
1553 ctf_field_class_destroy(sc->event_header_fc);
1554 ctf_field_class_destroy(sc->event_common_context_fc);
44c440bc
PP
1555 g_free(sc);
1556}
1557
1558static inline
1559void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1560 struct ctf_event_class *ec)
1561{
1562 g_ptr_array_add(sc->event_classes, ec);
1563 g_hash_table_insert(sc->event_classes_by_id,
1564 GUINT_TO_POINTER((guint) ec->id), ec);
1565}
1566
1567static inline
1568struct ctf_event_class *ctf_stream_class_borrow_event_class_by_id(
864cad70 1569 struct ctf_stream_class *sc, uint64_t type)
44c440bc
PP
1570{
1571 BT_ASSERT(sc);
1572 return g_hash_table_lookup(sc->event_classes_by_id,
864cad70 1573 GUINT_TO_POINTER((guint) type));
44c440bc
PP
1574}
1575
1576static inline
1577void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1578{
1579 BT_ASSERT(entry);
1580 entry->name = g_string_new(NULL);
1581 BT_ASSERT(entry->name);
1582 entry->value.str = g_string_new(NULL);
1583 BT_ASSERT(entry->value.str);
1584}
1585
1586static inline
1587void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1588{
1589 BT_ASSERT(entry);
1590
1591 if (entry->name) {
1592 g_string_free(entry->name, TRUE);
1593 }
1594
1595 if (entry->value.str) {
1596 g_string_free(entry->value.str, TRUE);
1597 }
1598}
1599
0f2d58c9
PP
1600static inline
1601struct ctf_clock_class *ctf_clock_class_create(void)
1602{
1603 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1604
1605 BT_ASSERT(cc);
1606 cc->name = g_string_new(NULL);
1607 BT_ASSERT(cc->name);
1608 cc->description = g_string_new(NULL);
1609 BT_ASSERT(cc->description);
1610 return cc;
1611}
1612
1613static inline
1614void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1615{
1616 if (!cc) {
1617 return;
1618 }
1619
1620 if (cc->name) {
1621 g_string_free(cc->name, TRUE);
1622 }
1623
1624 if (cc->description) {
1625 g_string_free(cc->description, TRUE);
1626 }
1627
1628 bt_clock_class_put_ref(cc->ir_cc);
1629 g_free(cc);
1630}
1631
44c440bc
PP
1632static inline
1633struct ctf_trace_class *ctf_trace_class_create(void)
1634{
1635 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1636
1637 BT_ASSERT(tc);
44c440bc 1638 tc->default_byte_order = -1;
44c440bc 1639 tc->clock_classes = g_ptr_array_new_with_free_func(
0f2d58c9 1640 (GDestroyNotify) ctf_clock_class_destroy);
44c440bc
PP
1641 BT_ASSERT(tc->clock_classes);
1642 tc->stream_classes = g_ptr_array_new_with_free_func(
1643 (GDestroyNotify) ctf_stream_class_destroy);
1644 BT_ASSERT(tc->stream_classes);
1645 tc->env_entries = g_array_new(FALSE, TRUE,
1646 sizeof(struct ctf_trace_class_env_entry));
1647 return tc;
1648}
1649
1650static inline
1651void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1652{
1653 if (!tc) {
1654 return;
1655 }
1656
5cd6d0e5 1657 ctf_field_class_destroy(tc->packet_header_fc);
44c440bc
PP
1658
1659 if (tc->clock_classes) {
1660 g_ptr_array_free(tc->clock_classes, TRUE);
1661 }
1662
1663 if (tc->stream_classes) {
1664 g_ptr_array_free(tc->stream_classes, TRUE);
1665 }
1666
1667 if (tc->env_entries) {
1668 uint64_t i;
1669
1670 for (i = 0; i < tc->env_entries->len; i++) {
1671 struct ctf_trace_class_env_entry *entry =
1672 &g_array_index(tc->env_entries,
1673 struct ctf_trace_class_env_entry, i);
1674
1675 _ctf_trace_class_env_entry_fini(entry);
1676 }
1677
1678 g_array_free(tc->env_entries, TRUE);
1679 }
1680
1681 g_free(tc);
1682}
1683
1684static inline
1685void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc,
1686 const char *name, enum ctf_trace_class_env_entry_type type,
1687 const char *str_value, int64_t i_value)
1688{
1689 struct ctf_trace_class_env_entry *entry;
1690
1691 BT_ASSERT(tc);
1692 BT_ASSERT(name);
1693 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1694
1695 entry = &g_array_index(tc->env_entries,
1696 struct ctf_trace_class_env_entry, tc->env_entries->len - 1);
1697 entry->type = type;
1698 _ctf_trace_class_env_entry_init(entry);
1699 g_string_assign(entry->name, name);
1700
1701 if (str_value) {
1702 g_string_assign(entry->value.str, str_value);
1703 }
1704
1705 entry->value.i = i_value;
1706}
1707
1708static inline
1709struct ctf_stream_class *ctf_trace_class_borrow_stream_class_by_id(
1710 struct ctf_trace_class *tc, uint64_t id)
1711{
1712 uint64_t i;
1713 struct ctf_stream_class *ret_sc = NULL;
1714
1715 BT_ASSERT(tc);
1716
1717 for (i = 0; i < tc->stream_classes->len; i++) {
1718 struct ctf_stream_class *sc = tc->stream_classes->pdata[i];
1719
1720 if (sc->id == id) {
1721 ret_sc = sc;
1722 goto end;
1723 }
1724 }
1725
1726end:
1727 return ret_sc;
1728}
1729
1730static inline
0f2d58c9 1731struct ctf_clock_class *ctf_trace_class_borrow_clock_class_by_name(
44c440bc
PP
1732 struct ctf_trace_class *tc, const char *name)
1733{
1734 uint64_t i;
0f2d58c9 1735 struct ctf_clock_class *ret_cc = NULL;
44c440bc
PP
1736
1737 BT_ASSERT(tc);
1738 BT_ASSERT(name);
1739
1740 for (i = 0; i < tc->clock_classes->len; i++) {
0f2d58c9 1741 struct ctf_clock_class *cc = tc->clock_classes->pdata[i];
44c440bc 1742
0f2d58c9
PP
1743 BT_ASSERT(cc->name);
1744 if (strcmp(cc->name->str, name) == 0) {
44c440bc
PP
1745 ret_cc = cc;
1746 goto end;
1747 }
1748 }
1749
1750end:
1751 return ret_cc;
1752}
1753
1754static inline
1755struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_index(
1756 struct ctf_trace_class *tc, uint64_t index)
1757{
1758 BT_ASSERT(tc);
1759 BT_ASSERT(index < tc->env_entries->len);
1760 return &g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1761 index);
1762}
1763
1764static inline
1765struct ctf_trace_class_env_entry *ctf_trace_class_borrow_env_entry_by_name(
1766 struct ctf_trace_class *tc, const char *name)
1767{
1768 struct ctf_trace_class_env_entry *ret_entry = NULL;
1769 uint64_t i;
1770
1771 BT_ASSERT(tc);
1772 BT_ASSERT(name);
1773
1774 for (i = 0; i < tc->env_entries->len; i++) {
1775 struct ctf_trace_class_env_entry *env_entry =
1776 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1777
1778 if (strcmp(env_entry->name->str, name) == 0) {
1779 ret_entry = env_entry;
1780 goto end;
1781 }
1782 }
1783
1784end:
1785 return ret_entry;
1786}
1787
1788#endif /* _CTF_META_H */
This page took 0.133152 seconds and 4 git commands to generate.