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